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 program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; This file is not part of GNU Emacs.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. 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 (namely `babel-call', `clock', `headline', `item',
34 ;; `keyword', `planning', `property-drawer' and `section' types), it
35 ;; can also accept a fixed set of keywords as attributes. Those are
36 ;; called "affiliated keywords" to distinguish them from other
37 ;; keywords, which are full-fledged elements. Almost all affiliated
38 ;; keywords are referenced in `org-element-affiliated-keywords'; the
39 ;; others are export attributes and start with "ATTR_" prefix.
41 ;; Element containing other elements (and only elements) are called
42 ;; greater elements. Concerned types are: `center-block', `drawer',
43 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
44 ;; `item', `plain-list', `quote-block', `section' and `special-block'.
46 ;; Other element types are: `babel-call', `clock', `comment',
47 ;; `comment-block', `example-block', `export-block', `fixed-width',
48 ;; `horizontal-rule', `keyword', `latex-environment', `paragraph',
49 ;; `planning', `property-drawer', `quote-section', `src-block',
50 ;; `table', `table-row' and `verse-block'. Among them, `paragraph'
51 ;; and `verse-block' types can contain Org objects and plain text.
53 ;; Objects are related to document's contents. Some of them are
54 ;; recursive. Associated types are of the following: `bold', `code',
55 ;; `entity', `export-snippet', `footnote-reference',
56 ;; `inline-babel-call', `inline-src-block', `italic',
57 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
58 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
59 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
61 ;; Some elements also have special properties whose value can hold
62 ;; objects themselves (i.e. an item tag or an headline name). Such
63 ;; values are called "secondary strings". Any object belongs to
64 ;; either an element or a secondary string.
66 ;; Notwithstanding affiliated keywords, each greater element, element
67 ;; and object has a fixed set of properties attached to it. Among
68 ;; them, four are shared by all types: `:begin' and `:end', which
69 ;; refer to the beginning and ending buffer positions of the
70 ;; considered element or object, `:post-blank', which holds the number
71 ;; of blank lines, or white spaces, at its end and `:parent' which
72 ;; refers to the element or object containing it. Greater elements
73 ;; and elements containing objects will also have `:contents-begin'
74 ;; and `:contents-end' properties to delimit contents.
76 ;; Lisp-wise, an element or an object can be represented as a list.
77 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
78 ;; TYPE is a symbol describing the Org element or object.
79 ;; PROPERTIES is the property list attached to it. See docstring of
80 ;; appropriate parsing function to get an exhaustive
82 ;; CONTENTS is a list of elements, objects or raw strings contained
83 ;; in the current element or object, when applicable.
85 ;; An Org buffer is a nested list of such elements and objects, whose
86 ;; type is `org-data' and properties is nil.
88 ;; The first part of this file defines Org syntax, while the second
89 ;; one provide accessors and setters functions.
91 ;; The next part implements a parser and an interpreter for each
92 ;; element and object type in Org syntax.
94 ;; The following part creates a fully recursive buffer parser. It
95 ;; also provides a tool to map a function to elements or objects
96 ;; matching some criteria in the parse tree. Functions of interest
97 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
98 ;; extent, `org-element-parse-secondary-string'.
100 ;; The penultimate part is the cradle of an interpreter for the
101 ;; obtained parse tree: `org-element-interpret-data'.
103 ;; The library ends by furnishing a set of interactive tools for
104 ;; element's navigation and manipulation, mostly based on
105 ;; `org-element-at-point' function, and a way to give information
106 ;; about document structure around point with `org-element-context'.
116 ;;; Definitions And Rules
118 ;; Define elements, greater elements and specify recursive objects,
119 ;; along with the affiliated keywords recognized. Also set up
120 ;; restrictions on recursive objects combinations.
122 ;; These variables really act as a control center for the parsing
125 (defconst org-element-paragraph-separate
126 (concat "^[ \t]*$" "\\|"
127 ;; Headlines and inlinetasks.
128 org-outline-regexp-bol
"\\|"
129 ;; Comments, blocks (any type), keywords and babel calls.
130 "^[ \t]*#\\+" "\\|" "^#\\(?: \\|$\\)" "\\|"
132 (org-item-beginning-re) "\\|"
133 ;; Fixed-width, drawers (any type) and tables.
135 ;; Footnote definitions.
136 org-footnote-definition-re
"\\|"
138 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
139 ;; LaTeX environments.
140 "^[ \t]*\\\\\\(begin\\|end\\)" "\\|"
141 ;; Planning and Clock lines.
142 org-planning-or-clock-line-re
)
143 "Regexp to separate paragraphs in an Org buffer.")
145 (defconst org-element-all-elements
146 '(center-block clock comment comment-block drawer dynamic-block example-block
147 export-block fixed-width footnote-definition headline
148 horizontal-rule inlinetask item keyword latex-environment
149 babel-call paragraph plain-list planning property-drawer
150 quote-block quote-section section special-block src-block table
151 table-row verse-block
)
152 "Complete list of element types.")
154 (defconst org-element-greater-elements
155 '(center-block drawer dynamic-block footnote-definition headline inlinetask
156 item plain-list quote-block section special-block table
)
157 "List of recursive element types aka Greater Elements.")
159 (defconst org-element-all-successors
160 '(export-snippet footnote-reference inline-babel-call inline-src-block
161 latex-or-entity line-break link macro radio-target
162 statistics-cookie sub
/superscript table-cell target
163 text-markup timestamp
)
164 "Complete list of successors.")
166 (defconst org-element-object-successor-alist
167 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
168 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
169 (strike-through . text-markup
) (underline . text-markup
)
170 (verbatim . text-markup
) (entity . latex-or-entity
)
171 (latex-fragment . latex-or-entity
))
172 "Alist of translations between object type and successor name.
174 Sharing the same successor comes handy when, for example, the
175 regexp matching one object can also match the other object.")
177 (defconst org-element-all-objects
178 '(bold code entity export-snippet footnote-reference inline-babel-call
179 inline-src-block italic line-break latex-fragment link macro
180 radio-target statistics-cookie strike-through subscript superscript
181 table-cell target timestamp underline verbatim
)
182 "Complete list of object types.")
184 (defconst org-element-recursive-objects
185 '(bold italic link macro subscript radio-target strike-through superscript
186 table-cell underline
)
187 "List of recursive object types.")
189 (defconst org-element-block-name-alist
190 '(("CENTER" . org-element-center-block-parser
)
191 ("COMMENT" . org-element-comment-block-parser
)
192 ("EXAMPLE" . org-element-example-block-parser
)
193 ("QUOTE" . org-element-quote-block-parser
)
194 ("SRC" . org-element-src-block-parser
)
195 ("VERSE" . org-element-verse-block-parser
))
196 "Alist between block names and the associated parsing function.
197 Names must be uppercase. Any block whose name has no association
198 is parsed with `org-element-special-block-parser'.")
200 (defconst org-element-affiliated-keywords
201 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
202 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
203 "List of affiliated keywords as strings.
204 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
205 are affiliated keywords and need not to be in this list.")
207 (defconst org-element--affiliated-re
208 (format "[ \t]*#\\+%s:"
209 ;; Regular affiliated keywords.
210 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
211 (regexp-opt org-element-affiliated-keywords
)))
212 "Regexp matching any affiliated keyword.
214 Keyword name is put in match group 1. Moreover, if keyword
215 belongs to `org-element-dual-keywords', put the dual value in
218 Don't modify it, set `org-element-affiliated-keywords' instead.")
220 (defconst org-element-keyword-translation-alist
221 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
222 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
223 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
224 "Alist of usual translations for keywords.
225 The key is the old name and the value the new one. The property
226 holding their value will be named after the translated name.")
228 (defconst org-element-multiple-keywords
'("HEADER")
229 "List of affiliated keywords that can occur more that once in an element.
231 Their value will be consed into a list of strings, which will be
232 returned as the value of the property.
234 This list is checked after translations have been applied. See
235 `org-element-keyword-translation-alist'.
237 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
238 allow multiple occurrences and need not to be in this list.")
240 (defconst org-element-parsed-keywords
'("AUTHOR" "CAPTION" "DATE" "TITLE")
241 "List of keywords whose value can be parsed.
243 Their value will be stored as a secondary string: a list of
246 This list is checked after translations have been applied. See
247 `org-element-keyword-translation-alist'.")
249 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
250 "List of keywords which can have a secondary value.
252 In Org syntax, they can be written with optional square brackets
253 before the colons. For example, results keyword can be
254 associated to a hash value with the following:
256 #+RESULTS[hash-string]: some-source
258 This list is checked after translations have been applied. See
259 `org-element-keyword-translation-alist'.")
261 (defconst org-element-object-restrictions
262 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
263 radio-target sub
/superscript target text-markup timestamp
)
264 (footnote-reference export-snippet footnote-reference inline-babel-call
265 inline-src-block latex-or-entity line-break link macro
266 radio-target sub
/superscript target text-markup
268 (headline inline-babel-call inline-src-block latex-or-entity link macro
269 radio-target statistics-cookie sub
/superscript target text-markup
271 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
272 radio-target sub
/superscript target text-markup timestamp
)
273 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
274 link radio-target sub
/superscript target text-markup timestamp
)
275 (item export-snippet footnote-reference inline-babel-call latex-or-entity
276 link macro radio-target sub
/superscript target text-markup
)
277 (keyword latex-or-entity macro sub
/superscript text-markup
)
278 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
279 sub
/superscript text-markup
)
281 (paragraph export-snippet footnote-reference inline-babel-call
282 inline-src-block latex-or-entity line-break link macro
283 radio-target statistics-cookie sub
/superscript target text-markup
285 (radio-target export-snippet latex-or-entity sub
/superscript
)
286 (strike-through export-snippet inline-babel-call inline-src-block
287 latex-or-entity link radio-target sub
/superscript target
288 text-markup timestamp
)
289 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
290 sub
/superscript target text-markup
)
291 (superscript export-snippet inline-babel-call inline-src-block
292 latex-or-entity sub
/superscript target text-markup
)
293 (table-cell export-snippet latex-or-entity link macro radio-target
294 sub
/superscript target text-markup timestamp
)
295 (table-row table-cell
)
296 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
297 link radio-target sub
/superscript target text-markup timestamp
)
298 (verse-block footnote-reference inline-babel-call inline-src-block
299 latex-or-entity line-break link macro radio-target
300 sub
/superscript target text-markup timestamp
))
301 "Alist of objects restrictions.
303 CAR is an element or object type containing objects and CDR is
304 a list of successors that will be called within an element or
307 For example, in a `radio-target' object, one can only find
308 entities, export snippets, latex-fragments, subscript and
311 This alist also applies to secondary string. For example, an
312 `headline' type element doesn't directly contain objects, but
313 still has an entry since one of its properties (`:title') does.")
315 (defconst org-element-secondary-value-alist
316 '((headline .
:title
)
317 (inlinetask .
:title
)
319 (footnote-reference .
:inline-definition
))
320 "Alist between element types and location of secondary value.")
324 ;;; Accessors and Setters
326 ;; Provide four accessors: `org-element-type', `org-element-property'
327 ;; `org-element-contents' and `org-element-restriction'.
329 ;; Setter functions allow to modify elements by side effect. There is
330 ;; `org-element-put-property', `org-element-set-contents',
331 ;; `org-element-set-element' and `org-element-adopt-element'. Note
332 ;; that `org-element-set-element' and `org-element-adopt-element' are
333 ;; higher level functions since also update `:parent' property.
335 (defsubst org-element-type
(element)
336 "Return type of ELEMENT.
338 The function returns the type of the element or object provided.
339 It can also return the following special value:
340 `plain-text' for a string
341 `org-data' for a complete document
342 nil in any other case."
344 ((not (consp element
)) (and (stringp element
) 'plain-text
))
345 ((symbolp (car element
)) (car element
))))
347 (defsubst org-element-property
(property element
)
348 "Extract the value from the PROPERTY of an ELEMENT."
349 (plist-get (nth 1 element
) property
))
351 (defsubst org-element-contents
(element)
352 "Extract contents from an ELEMENT."
353 (and (consp element
) (nthcdr 2 element
)))
355 (defsubst org-element-restriction
(element)
356 "Return restriction associated to ELEMENT.
357 ELEMENT can be an element, an object or a symbol representing an
358 element or object type."
359 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
360 org-element-object-restrictions
)))
362 (defsubst org-element-put-property
(element property value
)
363 "In ELEMENT set PROPERTY to VALUE.
364 Return modified element."
365 (when (consp element
)
366 (setcar (cdr element
) (plist-put (nth 1 element
) property value
)))
369 (defsubst org-element-set-contents
(element &rest contents
)
370 "Set ELEMENT contents to CONTENTS.
371 Return modified element."
372 (cond ((not element
) (list contents
))
373 ((cdr element
) (setcdr (cdr element
) contents
))
374 (t (nconc element contents
))))
376 (defsubst org-element-set-element
(old new
)
377 "Replace element or object OLD with element or object NEW.
378 The function takes care of setting `:parent' property for NEW."
379 ;; OLD can belong to the contents of PARENT or to its secondary
381 (let* ((parent (org-element-property :parent old
))
382 (sec-loc (cdr (assq (org-element-type parent
)
383 org-element-secondary-value-alist
)))
384 (sec-value (and sec-loc
(org-element-property sec-loc parent
)))
385 (place (or (memq old sec-value
) (memq old parent
))))
386 ;; Make sure NEW has correct `:parent' property.
387 (org-element-put-property new
:parent parent
)
388 ;; Replace OLD with NEW in PARENT.
391 (defsubst org-element-adopt-element
(parent child
&optional append
)
392 "Add an element to the contents of another element.
394 PARENT is an element or object. CHILD is an element, an object,
397 CHILD is added at the beginning of PARENT contents, unless the
398 optional argument APPEND is non-nil, in which case CHILD is added
401 The function takes care of setting `:parent' property for CHILD.
402 Return parent element."
403 (if (not parent
) (list child
)
404 (let ((contents (org-element-contents parent
)))
405 (apply 'org-element-set-contents
407 (if append
(append contents
(list child
)) (cons child contents
))))
408 ;; Link the CHILD element with PARENT.
409 (when (consp child
) (org-element-put-property child
:parent parent
))
410 ;; Return the parent element.
417 ;; For each greater element type, we define a parser and an
420 ;; A parser returns the element or object as the list described above.
421 ;; Most of them accepts no argument. Though, exceptions exist. Hence
422 ;; every element containing a secondary string (see
423 ;; `org-element-secondary-value-alist') will accept an optional
424 ;; argument to toggle parsing of that secondary string. Moreover,
425 ;; `item' parser requires current list's structure as its first
428 ;; An interpreter accepts two arguments: the list representation of
429 ;; the element or object, and its contents. The latter may be nil,
430 ;; depending on the element or object considered. It returns the
431 ;; appropriate Org syntax, as a string.
433 ;; Parsing functions must follow the naming convention:
434 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
435 ;; defined in `org-element-greater-elements'.
437 ;; Similarly, interpreting functions must follow the naming
438 ;; convention: org-element-TYPE-interpreter.
440 ;; With the exception of `headline' and `item' types, greater elements
441 ;; cannot contain other greater elements of their own type.
443 ;; Beside implementing a parser and an interpreter, adding a new
444 ;; greater element requires to tweak `org-element--current-element'.
445 ;; Moreover, the newly defined type must be added to both
446 ;; `org-element-all-elements' and `org-element-greater-elements'.
451 (defun org-element-center-block-parser (limit)
452 "Parse a center block.
454 LIMIT bounds the search.
456 Return a list whose CAR is `center-block' and CDR is a plist
457 containing `:begin', `:end', `:hiddenp', `:contents-begin',
458 `:contents-end' and `:post-blank' keywords.
460 Assume point is at the beginning of the block."
461 (let ((case-fold-search t
))
462 (if (not (save-excursion
463 (re-search-forward "^[ \t]*#\\+END_CENTER" limit t
)))
464 ;; Incomplete block: parse it as a comment.
465 (org-element-comment-parser limit
)
466 (let ((block-end-line (match-beginning 0)))
467 (let* ((keywords (org-element--collect-affiliated-keywords))
468 (begin (car keywords
))
469 ;; Empty blocks have no contents.
470 (contents-begin (progn (forward-line)
471 (and (< (point) block-end-line
)
473 (contents-end (and contents-begin block-end-line
))
474 (hidden (org-invisible-p2))
475 (pos-before-blank (progn (goto-char block-end-line
)
478 (end (save-excursion (skip-chars-forward " \r\t\n" limit
)
479 (if (eobp) (point) (point-at-bol)))))
485 :contents-begin contents-begin
486 :contents-end contents-end
487 :post-blank
(count-lines pos-before-blank end
))
488 (cadr keywords
))))))))
490 (defun org-element-center-block-interpreter (center-block contents
)
491 "Interpret CENTER-BLOCK element as Org syntax.
492 CONTENTS is the contents of the element."
493 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
498 (defun org-element-drawer-parser (limit)
501 LIMIT bounds the search.
503 Return a list whose CAR is `drawer' and CDR is a plist containing
504 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
505 `:contents-end' and `:post-blank' keywords.
507 Assume point is at beginning of drawer."
508 (let ((case-fold-search t
))
509 (if (not (save-excursion (re-search-forward "^[ \t]*:END:" limit t
)))
510 ;; Incomplete drawer: parse it as a paragraph.
511 (org-element-paragraph-parser limit
)
512 (let ((drawer-end-line (match-beginning 0)))
514 (let* ((case-fold-search t
)
515 (name (progn (looking-at org-drawer-regexp
)
516 (org-match-string-no-properties 1)))
517 (keywords (org-element--collect-affiliated-keywords))
518 (begin (car keywords
))
519 ;; Empty drawers have no contents.
520 (contents-begin (progn (forward-line)
521 (and (< (point) drawer-end-line
)
523 (contents-end (and contents-begin drawer-end-line
))
524 (hidden (org-invisible-p2))
525 (pos-before-blank (progn (goto-char drawer-end-line
)
528 (end (progn (skip-chars-forward " \r\t\n" limit
)
529 (if (eobp) (point) (point-at-bol)))))
536 :contents-begin contents-begin
537 :contents-end contents-end
538 :post-blank
(count-lines pos-before-blank end
))
539 (cadr keywords
)))))))))
541 (defun org-element-drawer-interpreter (drawer contents
)
542 "Interpret DRAWER element as Org syntax.
543 CONTENTS is the contents of the element."
544 (format ":%s:\n%s:END:"
545 (org-element-property :drawer-name drawer
)
551 (defun org-element-dynamic-block-parser (limit)
552 "Parse a dynamic block.
554 LIMIT bounds the search.
556 Return a list whose CAR is `dynamic-block' and CDR is a plist
557 containing `:block-name', `:begin', `:end', `:hiddenp',
558 `:contents-begin', `:contents-end', `:arguments' and
559 `:post-blank' keywords.
561 Assume point is at beginning of dynamic block."
562 (let ((case-fold-search t
))
563 (if (not (save-excursion (re-search-forward org-dblock-end-re limit t
)))
564 ;; Incomplete block: parse it as a comment.
565 (org-element-comment-parser limit
)
566 (let ((block-end-line (match-beginning 0)))
568 (let* ((name (progn (looking-at org-dblock-start-re
)
569 (org-match-string-no-properties 1)))
570 (arguments (org-match-string-no-properties 3))
571 (keywords (org-element--collect-affiliated-keywords))
572 (begin (car keywords
))
573 ;; Empty blocks have no contents.
574 (contents-begin (progn (forward-line)
575 (and (< (point) block-end-line
)
577 (contents-end (and contents-begin block-end-line
))
578 (hidden (org-invisible-p2))
579 (pos-before-blank (progn (goto-char block-end-line
)
582 (end (progn (skip-chars-forward " \r\t\n" limit
)
583 (if (eobp) (point) (point-at-bol)))))
591 :contents-begin contents-begin
592 :contents-end contents-end
593 :post-blank
(count-lines pos-before-blank end
))
594 (cadr keywords
)))))))))
596 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
597 "Interpret DYNAMIC-BLOCK element as Org syntax.
598 CONTENTS is the contents of the element."
599 (format "#+BEGIN: %s%s\n%s#+END:"
600 (org-element-property :block-name dynamic-block
)
601 (let ((args (org-element-property :arguments dynamic-block
)))
602 (and args
(concat " " args
)))
606 ;;;; Footnote Definition
608 (defun org-element-footnote-definition-parser (limit)
609 "Parse a footnote definition.
611 LIMIT bounds the search.
613 Return a list whose CAR is `footnote-definition' and CDR is
614 a plist containing `:label', `:begin' `:end', `:contents-begin',
615 `:contents-end' and `:post-blank' keywords.
617 Assume point is at the beginning of the footnote definition."
619 (let* ((label (progn (looking-at org-footnote-definition-re
)
620 (org-match-string-no-properties 1)))
621 (keywords (org-element--collect-affiliated-keywords))
622 (begin (car keywords
))
623 (ending (save-excursion
627 (concat org-outline-regexp-bol
"\\|"
628 org-footnote-definition-re
"\\|"
629 "^[ \t]*$") limit
'move
))
632 (contents-begin (progn (search-forward "]")
633 (skip-chars-forward " \r\t\n" ending
)
634 (and (/= (point) ending
) (point))))
635 (contents-end (and contents-begin ending
))
636 (end (progn (goto-char ending
)
637 (skip-chars-forward " \r\t\n" limit
)
638 (if (eobp) (point) (point-at-bol)))))
639 (list 'footnote-definition
644 :contents-begin contents-begin
645 :contents-end contents-end
646 :post-blank
(count-lines ending end
))
649 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
650 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
651 CONTENTS is the contents of the footnote-definition."
652 (concat (format "[%s]" (org-element-property :label footnote-definition
))
659 (defun org-element-headline-parser (limit &optional raw-secondary-p
)
662 Return a list whose CAR is `headline' and CDR is a plist
663 containing `:raw-value', `:title', `:begin', `:end',
664 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
665 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
666 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
667 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
670 The plist also contains any property set in the property drawer,
671 with its name in lowercase, the underscores replaced with hyphens
672 and colons at the beginning (i.e. `:custom-id').
674 When RAW-SECONDARY-P is non-nil, headline's title will not be
675 parsed as a secondary string, but as a plain string instead.
677 Assume point is at beginning of the headline."
679 (let* ((components (org-heading-components))
680 (level (nth 1 components
))
681 (todo (nth 2 components
))
683 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
684 (tags (let ((raw-tags (nth 5 components
)))
685 (and raw-tags
(org-split-string raw-tags
":"))))
686 (raw-value (nth 4 components
))
688 (let ((case-fold-search nil
))
689 (string-match (format "^%s +" org-quote-string
) raw-value
)))
691 (let ((case-fold-search nil
))
692 (string-match (format "^%s +" org-comment-string
) raw-value
)))
693 (archivedp (member org-archive-tag tags
))
694 (footnote-section-p (and org-footnote-section
695 (string= org-footnote-section raw-value
)))
696 ;; Normalize property names: ":SOME_PROP:" becomes
698 (standard-props (let (plist)
701 (let ((p-name (downcase (car p
))))
702 (while (string-match "_" p-name
)
704 (replace-match "-" nil nil p-name
)))
705 (setq p-name
(intern (concat ":" p-name
)))
707 (plist-put plist p-name
(cdr p
)))))
708 (org-entry-properties nil
'standard
))
710 (time-props (org-entry-properties nil
'special
"CLOCK"))
711 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
712 (deadline (cdr (assoc "DEADLINE" time-props
)))
713 (clock (cdr (assoc "CLOCK" time-props
)))
714 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
716 (end (save-excursion (goto-char (org-end-of-subtree t t
))))
717 (pos-after-head (progn (forward-line) (point)))
718 (contents-begin (save-excursion
719 (skip-chars-forward " \r\t\n" end
)
720 (and (/= (point) end
) (line-beginning-position))))
721 (hidden (org-invisible-p2))
722 (contents-end (and contents-begin
723 (progn (goto-char end
)
724 (skip-chars-backward " \r\t\n")
727 ;; Clean RAW-VALUE from any quote or comment string.
728 (when (or quotedp commentedp
)
730 (replace-regexp-in-string
731 (concat "\\(" org-quote-string
"\\|" org-comment-string
"\\) +")
734 ;; Clean TAGS from archive tag, if any.
735 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
739 (list :raw-value raw-value
743 (if (not contents-begin
) 0
744 (count-lines pos-after-head contents-begin
))
746 :contents-begin contents-begin
747 :contents-end contents-end
749 :priority
(nth 3 components
)
757 :post-blank
(count-lines
758 (if (not contents-end
) pos-after-head
759 (goto-char contents-end
)
763 :footnote-section-p footnote-section-p
765 :commentedp commentedp
768 (org-element-put-property
770 (if raw-secondary-p raw-value
771 (org-element-parse-secondary-string
772 raw-value
(org-element-restriction 'headline
) headline
)))))))
774 (defun org-element-headline-interpreter (headline contents
)
775 "Interpret HEADLINE element as Org syntax.
776 CONTENTS is the contents of the element."
777 (let* ((level (org-element-property :level headline
))
778 (todo (org-element-property :todo-keyword headline
))
779 (priority (org-element-property :priority headline
))
780 (title (org-element-interpret-data
781 (org-element-property :title headline
)))
782 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
783 (cons org-archive-tag
784 (org-element-property :tags headline
))
785 (org-element-property :tags headline
))))
787 (format ":%s:" (mapconcat 'identity tag-list
":")))))
788 (commentedp (org-element-property :commentedp headline
))
789 (quotedp (org-element-property :quotedp headline
))
790 (pre-blank (or (org-element-property :pre-blank headline
) 0))
791 (heading (concat (make-string level ?
*)
792 (and todo
(concat " " todo
))
793 (and quotedp
(concat " " org-quote-string
))
794 (and commentedp
(concat " " org-comment-string
))
796 (format " [#%s]" (char-to-string priority
)))
797 (cond ((and org-footnote-section
798 (org-element-property
799 :footnote-section-p headline
))
800 (concat " " org-footnote-section
))
801 (title (concat " " title
))))))
806 ((zerop org-tags-column
) (format " %s" tags
))
807 ((< org-tags-column
0)
810 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
815 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
817 (make-string (1+ pre-blank
) 10)
823 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p
)
824 "Parse an inline task.
826 Return a list whose CAR is `inlinetask' and CDR is a plist
827 containing `:title', `:begin', `:end', `:hiddenp',
828 `:contents-begin' and `:contents-end', `:level', `:priority',
829 `:tags', `:todo-keyword', `:todo-type', `:scheduled',
830 `:deadline', `:timestamp', `:clock' and `:post-blank' keywords.
832 The plist also contains any property set in the property drawer,
833 with its name in lowercase, the underscores replaced with hyphens
834 and colons at the beginning (i.e. `:custom-id').
836 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
837 title will not be parsed as a secondary string, but as a plain
840 Assume point is at beginning of the inline task."
842 (let* ((keywords (org-element--collect-affiliated-keywords))
843 (begin (car keywords
))
844 (components (org-heading-components))
845 (todo (nth 2 components
))
847 (if (member todo org-done-keywords
) 'done
'todo
)))
848 (tags (let ((raw-tags (nth 5 components
)))
849 (and raw-tags
(org-split-string raw-tags
":"))))
850 ;; Normalize property names: ":SOME_PROP:" becomes
852 (standard-props (let (plist)
855 (let ((p-name (downcase (car p
))))
856 (while (string-match "_" p-name
)
858 (replace-match "-" nil nil p-name
)))
859 (setq p-name
(intern (concat ":" p-name
)))
861 (plist-put plist p-name
(cdr p
)))))
862 (org-entry-properties nil
'standard
))
864 (time-props (org-entry-properties nil
'special
"CLOCK"))
865 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
866 (deadline (cdr (assoc "DEADLINE" time-props
)))
867 (clock (cdr (assoc "CLOCK" time-props
)))
868 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
869 (task-end (save-excursion
871 (and (re-search-forward "^\\*+ END" limit t
)
872 (match-beginning 0))))
873 (contents-begin (progn (forward-line)
874 (and task-end
(< (point) task-end
) (point))))
875 (hidden (and contents-begin
(org-invisible-p2)))
876 (contents-end (and contents-begin task-end
))
877 (before-blank (if (not task-end
) (point)
881 (end (progn (skip-chars-forward " \r\t\n" limit
)
882 (if (eobp) (point) (point-at-bol))))
889 :contents-begin contents-begin
890 :contents-end contents-end
891 :level
(nth 1 components
)
892 :priority
(nth 3 components
)
900 :post-blank
(count-lines before-blank end
))
903 (org-element-put-property
905 (if raw-secondary-p
(nth 4 components
)
906 (org-element-parse-secondary-string
908 (org-element-restriction 'inlinetask
)
911 (defun org-element-inlinetask-interpreter (inlinetask contents
)
912 "Interpret INLINETASK element as Org syntax.
913 CONTENTS is the contents of inlinetask."
914 (let* ((level (org-element-property :level inlinetask
))
915 (todo (org-element-property :todo-keyword inlinetask
))
916 (priority (org-element-property :priority inlinetask
))
917 (title (org-element-interpret-data
918 (org-element-property :title inlinetask
)))
919 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
921 (format ":%s:" (mapconcat 'identity tag-list
":")))))
922 (task (concat (make-string level ?
*)
923 (and todo
(concat " " todo
))
925 (format " [#%s]" (char-to-string priority
)))
926 (and title
(concat " " title
)))))
931 ((zerop org-tags-column
) (format " %s" tags
))
932 ((< org-tags-column
0)
935 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
940 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
942 ;; Prefer degenerate inlinetasks when there are no
947 (make-string level ?
*) " END")))))
952 (defun org-element-item-parser (limit struct
&optional raw-secondary-p
)
955 STRUCT is the structure of the plain list.
957 Return a list whose CAR is `item' and CDR is a plist containing
958 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
959 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
960 `:post-blank' keywords.
962 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
963 any, will not be parsed as a secondary string, but as a plain
966 Assume point is at the beginning of the item."
969 (let* ((begin (point))
970 (bullet (org-list-get-bullet (point) struct
))
971 (checkbox (let ((box (org-list-get-checkbox begin struct
)))
972 (cond ((equal "[ ]" box
) 'off
)
973 ((equal "[X]" box
) 'on
)
974 ((equal "[-]" box
) 'trans
))))
975 (counter (let ((c (org-list-get-counter begin struct
)))
978 ((string-match "[A-Za-z]" c
)
979 (- (string-to-char (upcase (match-string 0 c
)))
981 ((string-match "[0-9]+" c
)
982 (string-to-number (match-string 0 c
))))))
983 (end (org-list-get-item-end begin struct
))
984 (contents-begin (progn (looking-at org-list-full-item-re
)
985 (goto-char (match-end 0))
986 (org-skip-whitespace)
987 ;; If first line isn't empty,
988 ;; contents really start at the text
989 ;; after item's meta-data.
990 (if (= (point-at-bol) begin
) (point)
992 (hidden (progn (forward-line)
993 (and (not (= (point) end
))
994 (org-invisible-p2))))
995 (contents-end (progn (goto-char end
)
996 (skip-chars-backward " \r\t\n")
1001 (list :bullet bullet
1004 ;; CONTENTS-BEGIN and CONTENTS-END may be
1005 ;; mixed up in the case of an empty item
1006 ;; separated from the next by a blank line.
1007 ;; Thus ensure the former is always the
1009 :contents-begin
(min contents-begin contents-end
)
1010 :contents-end
(max contents-begin contents-end
)
1015 :post-blank
(count-lines contents-end end
)))))
1016 (org-element-put-property
1018 (let ((raw-tag (org-list-get-tag begin struct
)))
1020 (if raw-secondary-p raw-tag
1021 (org-element-parse-secondary-string
1022 raw-tag
(org-element-restriction 'item
) item
))))))))
1024 (defun org-element-item-interpreter (item contents
)
1025 "Interpret ITEM element as Org syntax.
1026 CONTENTS is the contents of the element."
1028 (let* ((beg (org-element-property :begin item
))
1029 (struct (org-element-property :structure item
))
1030 (pre (org-list-prevs-alist struct
))
1031 (bul (org-element-property :bullet item
)))
1032 (org-list-bullet-string
1033 (if (not (eq (org-list-get-list-type beg struct pre
) 'ordered
)) "-"
1037 (org-list-get-item-number
1038 beg struct pre
(org-list-parents-alist struct
))))))
1041 (if (eq org-plain-list-ordered-item-terminator ?\
)) ")"
1043 (checkbox (org-element-property :checkbox item
))
1044 (counter (org-element-property :counter item
))
1045 (tag (let ((tag (org-element-property :tag item
)))
1046 (and tag
(org-element-interpret-data tag
))))
1047 ;; Compute indentation.
1048 (ind (make-string (length bullet
) 32))
1049 (item-starts-with-par-p
1050 (eq (org-element-type (car (org-element-contents item
)))
1055 (and counter
(format "[@%d] " counter
))
1057 ((eq checkbox
'on
) "[X] ")
1058 ((eq checkbox
'off
) "[ ] ")
1059 ((eq checkbox
'trans
) "[-] "))
1060 (and tag
(format "%s :: " tag
))
1061 (let ((contents (replace-regexp-in-string
1062 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
1063 (if item-starts-with-par-p
(org-trim contents
)
1064 (concat "\n" contents
))))))
1069 (defun org-element-plain-list-parser (limit &optional structure
)
1070 "Parse a plain list.
1072 Optional argument STRUCTURE, when non-nil, is the structure of
1073 the plain list being parsed.
1075 Return a list whose CAR is `plain-list' and CDR is a plist
1076 containing `:type', `:begin', `:end', `:contents-begin' and
1077 `:contents-end', `:structure' and `:post-blank' keywords.
1079 Assume point is at the beginning of the list."
1081 (let* ((struct (or structure
(org-list-struct)))
1082 (prevs (org-list-prevs-alist struct
))
1083 (parents (org-list-parents-alist struct
))
1084 (type (org-list-get-list-type (point) struct prevs
))
1085 (contents-begin (point))
1086 (keywords (org-element--collect-affiliated-keywords))
1087 (begin (car keywords
))
1089 (goto-char (org-list-get-list-end (point) struct prevs
)))
1090 (end (save-excursion (org-skip-whitespace)
1091 (if (eobp) (point) (point-at-bol)))))
1092 ;; Blank lines below list belong to the top-level list only.
1093 (unless (= (org-list-get-top-point struct
) contents-begin
)
1094 (setq end
(min (org-list-get-bottom-point struct
)
1095 (progn (skip-chars-forward " \r\t\n" limit
)
1096 (if (eobp) (point) (point-at-bol))))))
1103 :contents-begin contents-begin
1104 :contents-end contents-end
1106 :post-blank
(count-lines contents-end end
))
1107 (cadr keywords
))))))
1109 (defun org-element-plain-list-interpreter (plain-list contents
)
1110 "Interpret PLAIN-LIST element as Org syntax.
1111 CONTENTS is the contents of the element."
1117 (defun org-element-quote-block-parser (limit)
1118 "Parse a quote block.
1120 LIMIT bounds the search.
1122 Return a list whose CAR is `quote-block' and CDR is a plist
1123 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1124 `:contents-end' and `:post-blank' keywords.
1126 Assume point is at the beginning of the block."
1127 (let ((case-fold-search t
))
1128 (if (not (save-excursion
1129 (re-search-forward "^[ \t]*#\\+END_QUOTE" limit t
)))
1130 ;; Incomplete block: parse it as a comment.
1131 (org-element-comment-parser limit
)
1132 (let ((block-end-line (match-beginning 0)))
1134 (let* ((keywords (org-element--collect-affiliated-keywords))
1135 (begin (car keywords
))
1136 ;; Empty blocks have no contents.
1137 (contents-begin (progn (forward-line)
1138 (and (< (point) block-end-line
)
1140 (contents-end (and contents-begin block-end-line
))
1141 (hidden (org-invisible-p2))
1142 (pos-before-blank (progn (goto-char block-end-line
)
1145 (end (progn (skip-chars-forward " \r\t\n" limit
)
1146 (if (eobp) (point) (point-at-bol)))))
1152 :contents-begin contents-begin
1153 :contents-end contents-end
1154 :post-blank
(count-lines pos-before-blank end
))
1155 (cadr keywords
)))))))))
1157 (defun org-element-quote-block-interpreter (quote-block contents
)
1158 "Interpret QUOTE-BLOCK element as Org syntax.
1159 CONTENTS is the contents of the element."
1160 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1165 (defun org-element-section-parser (limit)
1168 LIMIT bounds the search.
1170 Return a list whose CAR is `section' and CDR is a plist
1171 containing `:begin', `:end', `:contents-begin', `contents-end'
1172 and `:post-blank' keywords."
1174 ;; Beginning of section is the beginning of the first non-blank
1175 ;; line after previous headline.
1176 (org-with-limited-levels
1177 (let ((begin (point))
1178 (end (progn (goto-char limit
) (point)))
1179 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1185 :contents-begin begin
1186 :contents-end pos-before-blank
1187 :post-blank
(count-lines pos-before-blank end
)))))))
1189 (defun org-element-section-interpreter (section contents
)
1190 "Interpret SECTION element as Org syntax.
1191 CONTENTS is the contents of the element."
1197 (defun org-element-special-block-parser (limit)
1198 "Parse a special block.
1200 LIMIT bounds the search.
1202 Return a list whose CAR is `special-block' and CDR is a plist
1203 containing `:type', `:begin', `:end', `:hiddenp',
1204 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1206 Assume point is at the beginning of the block."
1207 (let* ((case-fold-search t
)
1208 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1209 (upcase (match-string-no-properties 1)))))
1210 (if (not (save-excursion
1211 (re-search-forward (concat "^[ \t]*#\\+END_" type
) limit t
)))
1212 ;; Incomplete block: parse it as a comment.
1213 (org-element-comment-parser limit
)
1214 (let ((block-end-line (match-beginning 0)))
1216 (let* ((keywords (org-element--collect-affiliated-keywords))
1217 (begin (car keywords
))
1218 ;; Empty blocks have no contents.
1219 (contents-begin (progn (forward-line)
1220 (and (< (point) block-end-line
)
1222 (contents-end (and contents-begin block-end-line
))
1223 (hidden (org-invisible-p2))
1224 (pos-before-blank (progn (goto-char block-end-line
)
1227 (end (progn (org-skip-whitespace)
1228 (if (eobp) (point) (point-at-bol)))))
1229 (list 'special-block
1235 :contents-begin contents-begin
1236 :contents-end contents-end
1237 :post-blank
(count-lines pos-before-blank end
))
1238 (cadr keywords
)))))))))
1240 (defun org-element-special-block-interpreter (special-block contents
)
1241 "Interpret SPECIAL-BLOCK element as Org syntax.
1242 CONTENTS is the contents of the element."
1243 (let ((block-type (org-element-property :type special-block
)))
1244 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1250 ;; For each element, a parser and an interpreter are also defined.
1251 ;; Both follow the same naming convention used for greater elements.
1253 ;; Also, as for greater elements, adding a new element type is done
1254 ;; through the following steps: implement a parser and an interpreter,
1255 ;; tweak `org-element--current-element' so that it recognizes the new
1256 ;; type and add that new type to `org-element-all-elements'.
1258 ;; As a special case, when the newly defined type is a block type,
1259 ;; `org-element-block-name-alist' has to be modified accordingly.
1264 (defun org-element-babel-call-parser (limit)
1265 "Parse a babel call.
1267 LIMIT bounds the search.
1269 Return a list whose CAR is `babel-call' and CDR is a plist
1270 containing `:begin', `:end', `:info' and `:post-blank' as
1273 (let ((case-fold-search t
)
1274 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1275 (org-babel-lob-get-info)))
1276 (begin (point-at-bol))
1277 (pos-before-blank (progn (forward-line) (point)))
1278 (end (progn (skip-chars-forward " \r\t\n" limit
)
1279 (if (eobp) (point) (point-at-bol)))))
1284 :post-blank
(count-lines pos-before-blank end
))))))
1286 (defun org-element-babel-call-interpreter (babel-call contents
)
1287 "Interpret BABEL-CALL element as Org syntax.
1289 (let* ((babel-info (org-element-property :info babel-call
))
1290 (main (car babel-info
))
1291 (post-options (nth 1 babel-info
)))
1293 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1294 ;; Remove redundant square brackets.
1295 (replace-match (match-string 1 main
) nil nil main
))
1296 (and post-options
(format "[%s]" post-options
)))))
1301 (defun org-element-clock-parser (limit)
1304 LIMIT bounds the search.
1306 Return a list whose CAR is `clock' and CDR is a plist containing
1307 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1310 (let* ((case-fold-search nil
)
1312 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1313 (org-skip-whitespace)
1314 (looking-at "\\[.*\\]")
1315 (org-match-string-no-properties 0)))
1316 (time (and (progn (goto-char (match-end 0))
1317 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1318 (org-match-string-no-properties 1)))
1319 (status (if time
'closed
'running
))
1320 (post-blank (let ((before-blank (progn (forward-line) (point))))
1321 (skip-chars-forward " \r\t\n" limit
)
1322 (unless (eobp) (beginning-of-line))
1323 (count-lines before-blank
(point))))
1326 (list :status status
1331 :post-blank post-blank
)))))
1333 (defun org-element-clock-interpreter (clock contents
)
1334 "Interpret CLOCK element as Org syntax.
1336 (concat org-clock-string
" "
1337 (org-element-property :value clock
)
1338 (let ((time (org-element-property :time clock
)))
1343 (org-split-string time
":")))))))
1348 (defun org-element-comment-parser (limit)
1351 LIMIT bounds the search.
1353 Return a list whose CAR is `comment' and CDR is a plist
1354 containing `:begin', `:end', `:value' and `:post-blank'
1357 Assume point is at comment beginning."
1359 (let* ((keywords (org-element--collect-affiliated-keywords))
1360 (begin (car keywords
))
1361 ;; Match first line with a loose regexp since it might as
1362 ;; well be an ill-defined keyword.
1364 (looking-at "#\\+? ?")
1365 (buffer-substring-no-properties
1366 (match-end 0) (progn (forward-line) (point)))))
1368 ;; Get comments ending.
1370 (while (and (< (point) limit
)
1371 (looking-at "\\(\\(# ?\\)[^+]\\|[ \t]*#\\+\\( \\|$\\)\\)"))
1372 ;; Accumulate lines without leading hash and plus sign
1373 ;; if any. First whitespace is also ignored.
1376 (buffer-substring-no-properties
1377 (or (match-end 2) (match-end 3))
1378 (progn (forward-line) (point))))))
1380 (end (progn (goto-char com-end
)
1381 (skip-chars-forward " \r\t\n" limit
)
1382 (if (eobp) (point) (point-at-bol)))))
1388 :post-blank
(count-lines com-end end
))
1389 (cadr keywords
))))))
1391 (defun org-element-comment-interpreter (comment contents
)
1392 "Interpret COMMENT element as Org syntax.
1394 (replace-regexp-in-string "^" "#+ " (org-element-property :value comment
)))
1399 (defun org-element-comment-block-parser (limit)
1400 "Parse an export block.
1402 LIMIT bounds the search.
1404 Return a list whose CAR is `comment-block' and CDR is a plist
1405 containing `:begin', `:end', `:hiddenp', `:value' and
1406 `:post-blank' keywords.
1408 Assume point is at comment block beginning."
1409 (let ((case-fold-search t
))
1410 (if (not (save-excursion
1411 (re-search-forward "^[ \t]*#\\+END_COMMENT" limit t
)))
1412 ;; Incomplete block: parse it as a comment.
1413 (org-element-comment-parser limit
)
1414 (let ((contents-end (match-beginning 0)))
1416 (let* ((keywords (org-element--collect-affiliated-keywords))
1417 (begin (car keywords
))
1418 (contents-begin (progn (forward-line) (point)))
1419 (hidden (org-invisible-p2))
1420 (pos-before-blank (progn (goto-char contents-end
)
1423 (end (progn (skip-chars-forward " \r\t\n" limit
)
1424 (if (eobp) (point) (point-at-bol))))
1425 (value (buffer-substring-no-properties
1426 contents-begin contents-end
)))
1427 (list 'comment-block
1433 :post-blank
(count-lines pos-before-blank end
))
1434 (cadr keywords
)))))))))
1436 (defun org-element-comment-block-interpreter (comment-block contents
)
1437 "Interpret COMMENT-BLOCK element as Org syntax.
1439 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1440 (org-remove-indentation (org-element-property :value comment-block
))))
1445 (defun org-element-example-block-parser (limit)
1446 "Parse an example block.
1448 LIMIT bounds the search.
1450 Return a list whose CAR is `example-block' and CDR is a plist
1451 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1452 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1453 `:switches', `:value' and `:post-blank' keywords."
1454 (let ((case-fold-search t
))
1455 (if (not (save-excursion
1456 (re-search-forward "^[ \t]*#\\+END_EXAMPLE" limit t
)))
1457 ;; Incomplete block: parse it as a comment.
1458 (org-element-comment-parser limit
)
1459 (let ((contents-end (match-beginning 0)))
1462 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1463 (org-match-string-no-properties 1)))
1464 ;; Switches analysis
1465 (number-lines (cond ((not switches
) nil
)
1466 ((string-match "-n\\>" switches
) 'new
)
1467 ((string-match "+n\\>" switches
) 'continued
)))
1468 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1469 ;; Should labels be retained in (or stripped from) example
1473 (not (string-match "-r\\>" switches
))
1474 (and number-lines
(string-match "-k\\>" switches
))))
1475 ;; What should code-references use - labels or
1479 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1480 (label-fmt (and switches
1481 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1482 (match-string 1 switches
)))
1483 ;; Standard block parsing.
1484 (keywords (org-element--collect-affiliated-keywords))
1485 (begin (car keywords
))
1486 (contents-begin (progn (forward-line) (point)))
1487 (hidden (org-invisible-p2))
1488 (value (buffer-substring-no-properties contents-begin contents-end
))
1489 (pos-before-blank (progn (goto-char contents-end
)
1492 (end (progn (skip-chars-forward " \r\t\n" limit
)
1493 (if (eobp) (point) (point-at-bol)))))
1494 (list 'example-block
1500 :number-lines number-lines
1501 :preserve-indent preserve-indent
1502 :retain-labels retain-labels
1503 :use-labels use-labels
1504 :label-fmt label-fmt
1506 :post-blank
(count-lines pos-before-blank end
))
1507 (cadr keywords
)))))))))
1509 (defun org-element-example-block-interpreter (example-block contents
)
1510 "Interpret EXAMPLE-BLOCK element as Org syntax.
1512 (let ((switches (org-element-property :switches example-block
)))
1513 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1514 (org-remove-indentation
1515 (org-element-property :value example-block
))
1521 (defun org-element-export-block-parser (limit)
1522 "Parse an export block.
1524 LIMIT bounds the search.
1526 Return a list whose CAR is `export-block' and CDR is a plist
1527 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1528 `:post-blank' keywords.
1530 Assume point is at export-block beginning."
1531 (let* ((case-fold-search t
)
1532 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1533 (upcase (org-match-string-no-properties 1)))))
1534 (if (not (save-excursion
1535 (re-search-forward (concat "^[ \t]*#\\+END_" type
) limit t
)))
1536 ;; Incomplete block: parse it as a comment.
1537 (org-element-comment-parser limit
)
1538 (let ((contents-end (match-beginning 0)))
1540 (let* ((keywords (org-element--collect-affiliated-keywords))
1541 (begin (car keywords
))
1542 (contents-begin (progn (forward-line) (point)))
1543 (hidden (org-invisible-p2))
1544 (pos-before-blank (progn (goto-char contents-end
)
1547 (end (progn (skip-chars-forward " \r\t\n" limit
)
1548 (if (eobp) (point) (point-at-bol))))
1549 (value (buffer-substring-no-properties contents-begin
1558 :post-blank
(count-lines pos-before-blank end
))
1559 (cadr keywords
)))))))))
1561 (defun org-element-export-block-interpreter (export-block contents
)
1562 "Interpret EXPORT-BLOCK element as Org syntax.
1564 (let ((type (org-element-property :type export-block
)))
1565 (concat (format "#+BEGIN_%s\n" type
)
1566 (org-element-property :value export-block
)
1567 (format "#+END_%s" type
))))
1572 (defun org-element-fixed-width-parser (limit)
1573 "Parse a fixed-width section.
1575 LIMIT bounds the search.
1577 Return a list whose CAR is `fixed-width' and CDR is a plist
1578 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1580 Assume point is at the beginning of the fixed-width area."
1582 (let* ((keywords (org-element--collect-affiliated-keywords))
1583 (begin (car keywords
))
1587 (while (and (< (point) limit
)
1588 (looking-at "[ \t]*:\\( \\|$\\)"))
1589 ;; Accumulate text without starting colons.
1592 (buffer-substring-no-properties
1593 (match-end 0) (point-at-eol))
1597 (end (progn (skip-chars-forward " \r\t\n" limit
)
1598 (if (eobp) (point) (point-at-bol)))))
1604 :post-blank
(count-lines end-area end
))
1605 (cadr keywords
))))))
1607 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1608 "Interpret FIXED-WIDTH element as Org syntax.
1610 (replace-regexp-in-string
1611 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1614 ;;;; Horizontal Rule
1616 (defun org-element-horizontal-rule-parser (limit)
1617 "Parse an horizontal rule.
1619 LIMIT bounds the search.
1621 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1622 containing `:begin', `:end' and `:post-blank' keywords."
1624 (let* ((keywords (org-element--collect-affiliated-keywords))
1625 (begin (car keywords
))
1626 (post-hr (progn (forward-line) (point)))
1627 (end (progn (skip-chars-forward " \r\t\n" limit
)
1628 (if (eobp) (point) (point-at-bol)))))
1629 (list 'horizontal-rule
1633 :post-blank
(count-lines post-hr end
))
1634 (cadr keywords
))))))
1636 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1637 "Interpret HORIZONTAL-RULE element as Org syntax.
1644 (defun org-element-keyword-parser (limit)
1645 "Parse a keyword at point.
1647 LIMIT bounds the search.
1649 Return a list whose CAR is `keyword' and CDR is a plist
1650 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1653 (let* ((case-fold-search t
)
1655 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+\\):")
1656 (upcase (org-match-string-no-properties 1))))
1657 (value (org-trim (buffer-substring-no-properties
1658 (match-end 0) (point-at-eol))))
1659 (pos-before-blank (progn (forward-line) (point)))
1660 (end (progn (skip-chars-forward " \r\t\n" limit
)
1661 (if (eobp) (point) (point-at-bol)))))
1667 :post-blank
(count-lines pos-before-blank end
))))))
1669 (defun org-element-keyword-interpreter (keyword contents
)
1670 "Interpret KEYWORD element as Org syntax.
1673 (org-element-property :key keyword
)
1674 (org-element-property :value keyword
)))
1677 ;;;; Latex Environment
1679 (defun org-element-latex-environment-parser (limit)
1680 "Parse a LaTeX environment.
1682 LIMIT bounds the search.
1684 Return a list whose CAR is `latex-environment' and CDR is a plist
1685 containing `:begin', `:end', `:value' and `:post-blank'
1688 Assume point is at the beginning of the latex environment."
1690 (let* ((case-fold-search t
)
1691 (code-begin (point))
1692 (keywords (org-element--collect-affiliated-keywords))
1693 (begin (car keywords
))
1694 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
1695 (regexp-quote (match-string 1))))
1697 (progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env
) limit t
)
1700 (value (buffer-substring-no-properties code-begin code-end
))
1701 (end (progn (skip-chars-forward " \r\t\n" limit
)
1702 (if (eobp) (point) (point-at-bol)))))
1703 (list 'latex-environment
1708 :post-blank
(count-lines code-end end
))
1709 (cadr keywords
))))))
1711 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1712 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1714 (org-element-property :value latex-environment
))
1719 (defun org-element-paragraph-parser (limit)
1722 LIMIT bounds the search.
1724 Return a list whose CAR is `paragraph' and CDR is a plist
1725 containing `:begin', `:end', `:contents-begin' and
1726 `:contents-end' and `:post-blank' keywords.
1728 Assume point is at the beginning of the paragraph."
1730 (let* ((contents-begin (point))
1731 (keywords (org-element--collect-affiliated-keywords))
1732 (begin (car keywords
))
1734 (progn (end-of-line)
1735 (if (re-search-forward org-element-paragraph-separate
1738 (goto-char (match-beginning 0))
1740 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin
)
1743 (end (progn (skip-chars-forward " \r\t\n" limit
)
1744 (if (eobp) (point) (point-at-bol)))))
1746 ;; If paragraph has no affiliated keywords, it may not begin
1747 ;; at beginning of line if it starts an item.
1749 (list :begin
(if (cadr keywords
) begin contents-begin
)
1751 :contents-begin contents-begin
1752 :contents-end contents-end
1753 :post-blank
(count-lines before-blank end
))
1754 (cadr keywords
))))))
1756 (defun org-element-paragraph-interpreter (paragraph contents
)
1757 "Interpret PARAGRAPH element as Org syntax.
1758 CONTENTS is the contents of the element."
1764 (defun org-element-planning-parser (limit)
1767 LIMIT bounds the search.
1769 Return a list whose CAR is `planning' and CDR is a plist
1770 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1771 and `:post-blank' keywords."
1773 (let* ((case-fold-search nil
)
1775 (post-blank (let ((before-blank (progn (forward-line) (point))))
1776 (skip-chars-forward " \r\t\n" limit
)
1777 (unless (eobp) (beginning-of-line))
1778 (count-lines before-blank
(point))))
1780 closed deadline scheduled
)
1782 (while (re-search-forward org-keyword-time-not-clock-regexp
1783 (line-end-position) t
)
1784 (goto-char (match-end 1))
1785 (org-skip-whitespace)
1786 (let ((time (buffer-substring-no-properties
1787 (1+ (point)) (1- (match-end 0))))
1788 (keyword (match-string 1)))
1789 (cond ((equal keyword org-closed-string
) (setq closed time
))
1790 ((equal keyword org-deadline-string
) (setq deadline time
))
1791 (t (setq scheduled time
)))))
1793 (list :closed closed
1795 :scheduled scheduled
1798 :post-blank post-blank
)))))
1800 (defun org-element-planning-interpreter (planning contents
)
1801 "Interpret PLANNING element as Org syntax.
1806 (list (let ((closed (org-element-property :closed planning
)))
1807 (when closed
(concat org-closed-string
" [" closed
"]")))
1808 (let ((deadline (org-element-property :deadline planning
)))
1809 (when deadline
(concat org-deadline-string
" <" deadline
">")))
1810 (let ((scheduled (org-element-property :scheduled planning
)))
1812 (concat org-scheduled-string
" <" scheduled
">")))))
1816 ;;;; Property Drawer
1818 (defun org-element-property-drawer-parser (limit)
1819 "Parse a property drawer.
1821 LIMIT bounds the search.
1823 Return a list whose CAR is `property-drawer' and CDR is a plist
1824 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1825 `:contents-end', `:properties' and `:post-blank' keywords.
1827 Assume point is at the beginning of the property drawer."
1829 (let ((case-fold-search t
)
1831 (prop-begin (progn (forward-line) (point)))
1832 (hidden (org-invisible-p2))
1835 (while (not (looking-at "^[ \t]*:END:"))
1836 (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
1837 (push (cons (org-match-string-no-properties 1)
1839 (buffer-substring-no-properties
1840 (match-end 0) (point-at-eol))))
1844 (prop-end (progn (re-search-forward "^[ \t]*:END:" limit t
)
1846 (pos-before-blank (progn (forward-line) (point)))
1847 (end (progn (skip-chars-forward " \r\t\n" limit
)
1848 (if (eobp) (point) (point-at-bol)))))
1849 (list 'property-drawer
1853 :properties properties
1854 :post-blank
(count-lines pos-before-blank end
))))))
1856 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1857 "Interpret PROPERTY-DRAWER element as Org syntax.
1859 (let ((props (org-element-property :properties property-drawer
)))
1862 (mapconcat (lambda (p)
1863 (format org-property-format
(format ":%s:" (car p
)) (cdr p
)))
1864 (nreverse props
) "\n")
1870 (defun org-element-quote-section-parser (limit)
1871 "Parse a quote section.
1873 LIMIT bounds the search.
1875 Return a list whose CAR is `quote-section' and CDR is a plist
1876 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1878 Assume point is at beginning of the section."
1880 (let* ((begin (point))
1881 (end (progn (org-with-limited-levels (outline-next-heading))
1883 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1886 (value (buffer-substring-no-properties begin pos-before-blank
)))
1887 (list 'quote-section
1891 :post-blank
(count-lines pos-before-blank end
))))))
1893 (defun org-element-quote-section-interpreter (quote-section contents
)
1894 "Interpret QUOTE-SECTION element as Org syntax.
1896 (org-element-property :value quote-section
))
1901 (defun org-element-src-block-parser (limit)
1904 LIMIT bounds the search.
1906 Return a list whose CAR is `src-block' and CDR is a plist
1907 containing `:language', `:switches', `:parameters', `:begin',
1908 `:end', `:hiddenp', `:number-lines', `:retain-labels',
1909 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
1910 `:post-blank' keywords.
1912 Assume point is at the beginning of the block."
1913 (let ((case-fold-search t
))
1914 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC" limit t
)))
1915 ;; Incomplete block: parse it as a comment.
1916 (org-element-comment-parser limit
)
1917 (let ((contents-end (match-beginning 0)))
1919 (let* ((keywords (org-element--collect-affiliated-keywords))
1920 ;; Get beginning position.
1921 (begin (car keywords
))
1922 ;; Get language as a string.
1926 (concat "^[ \t]*#\\+BEGIN_SRC"
1927 "\\(?: +\\(\\S-+\\)\\)?"
1928 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
1930 (org-match-string-no-properties 1)))
1932 (switches (org-match-string-no-properties 2))
1934 (parameters (org-match-string-no-properties 3))
1935 ;; Switches analysis
1936 (number-lines (cond ((not switches
) nil
)
1937 ((string-match "-n\\>" switches
) 'new
)
1938 ((string-match "+n\\>" switches
) 'continued
)))
1939 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1940 (label-fmt (and switches
1941 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1942 (match-string 1 switches
)))
1943 ;; Should labels be retained in (or stripped from)
1947 (not (string-match "-r\\>" switches
))
1948 (and number-lines
(string-match "-k\\>" switches
))))
1949 ;; What should code-references use - labels or
1953 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1954 ;; Get visibility status.
1955 (hidden (progn (forward-line) (org-invisible-p2)))
1957 (value (buffer-substring-no-properties (point) contents-end
))
1958 (pos-before-blank (progn (goto-char contents-end
)
1961 ;; Get position after ending blank lines.
1962 (end (progn (skip-chars-forward " \r\t\n" limit
)
1963 (if (eobp) (point) (point-at-bol)))))
1966 (list :language language
1967 :switches
(and (org-string-nw-p switches
)
1968 (org-trim switches
))
1969 :parameters
(and (org-string-nw-p parameters
)
1970 (org-trim parameters
))
1973 :number-lines number-lines
1974 :preserve-indent preserve-indent
1975 :retain-labels retain-labels
1976 :use-labels use-labels
1977 :label-fmt label-fmt
1980 :post-blank
(count-lines pos-before-blank end
))
1981 (cadr keywords
)))))))))
1983 (defun org-element-src-block-interpreter (src-block contents
)
1984 "Interpret SRC-BLOCK element as Org syntax.
1986 (let ((lang (org-element-property :language src-block
))
1987 (switches (org-element-property :switches src-block
))
1988 (params (org-element-property :parameters src-block
))
1989 (value (let ((val (org-element-property :value src-block
)))
1992 (org-src-preserve-indentation val
)
1993 ((zerop org-edit-src-content-indentation
)
1994 (org-remove-indentation val
))
1996 (let ((ind (make-string
1997 org-edit-src-content-indentation
32)))
1998 (replace-regexp-in-string
1999 "\\(^\\)[ \t]*\\S-" ind
2000 (org-remove-indentation val
) nil nil
1)))))))
2001 (concat (format "#+BEGIN_SRC%s\n"
2002 (concat (and lang
(concat " " lang
))
2003 (and switches
(concat " " switches
))
2004 (and params
(concat " " params
))))
2011 (defun org-element-table-parser (limit)
2012 "Parse a table at point.
2014 LIMIT bounds the search.
2016 Return a list whose CAR is `table' and CDR is a plist containing
2017 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2018 `:contents-end', `:value' and `:post-blank' keywords.
2020 Assume point is at the beginning of the table."
2022 (let* ((case-fold-search t
)
2023 (table-begin (point))
2024 (type (if (org-at-table.el-p
) 'table.el
'org
))
2025 (keywords (org-element--collect-affiliated-keywords))
2026 (begin (car keywords
))
2027 (table-end (goto-char (marker-position (org-table-end t
))))
2029 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2030 (push (org-match-string-no-properties 1) acc
)
2033 (pos-before-blank (point))
2034 (end (progn (skip-chars-forward " \r\t\n" limit
)
2035 (if (eobp) (point) (point-at-bol)))))
2042 ;; Only `org' tables have contents. `table.el' tables
2043 ;; use a `:value' property to store raw table as
2045 :contents-begin
(and (eq type
'org
) table-begin
)
2046 :contents-end
(and (eq type
'org
) table-end
)
2047 :value
(and (eq type
'table.el
)
2048 (buffer-substring-no-properties
2049 table-begin table-end
))
2050 :post-blank
(count-lines pos-before-blank end
))
2051 (cadr keywords
))))))
2053 (defun org-element-table-interpreter (table contents
)
2054 "Interpret TABLE element as Org syntax.
2056 (if (eq (org-element-property :type table
) 'table.el
)
2057 (org-remove-indentation (org-element-property :value table
))
2058 (concat (with-temp-buffer (insert contents
)
2061 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
2062 (reverse (org-element-property :tblfm table
))
2068 (defun org-element-table-row-parser (limit)
2069 "Parse table row at point.
2071 LIMIT bounds the search.
2073 Return a list whose CAR is `table-row' and CDR is a plist
2074 containing `:begin', `:end', `:contents-begin', `:contents-end',
2075 `:type' and `:post-blank' keywords."
2077 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
2079 ;; A table rule has no contents. In that case, ensure
2080 ;; CONTENTS-BEGIN matches CONTENTS-END.
2081 (contents-begin (and (eq type
'standard
)
2082 (search-forward "|")
2084 (contents-end (and (eq type
'standard
)
2087 (skip-chars-backward " \t")
2089 (end (progn (forward-line) (point))))
2094 :contents-begin contents-begin
2095 :contents-end contents-end
2098 (defun org-element-table-row-interpreter (table-row contents
)
2099 "Interpret TABLE-ROW element as Org syntax.
2100 CONTENTS is the contents of the table row."
2101 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
2102 (concat "| " contents
)))
2107 (defun org-element-verse-block-parser (limit)
2108 "Parse a verse block.
2110 LIMIT bounds the search.
2112 Return a list whose CAR is `verse-block' and CDR is a plist
2113 containing `:begin', `:end', `:contents-begin', `:contents-end',
2114 `:hiddenp' and `:post-blank' keywords.
2116 Assume point is at beginning of the block."
2117 (let ((case-fold-search t
))
2118 (if (not (save-excursion
2119 (re-search-forward "^[ \t]*#\\+END_VERSE" limit t
)))
2120 ;; Incomplete block: parse it as a comment.
2121 (org-element-comment-parser limit
)
2122 (let ((contents-end (match-beginning 0)))
2124 (let* ((keywords (org-element--collect-affiliated-keywords))
2125 (begin (car keywords
))
2126 (hidden (progn (forward-line) (org-invisible-p2)))
2127 (contents-begin (point))
2128 (pos-before-blank (progn (goto-char contents-end
)
2131 (end (progn (skip-chars-forward " \r\t\n" limit
)
2132 (if (eobp) (point) (point-at-bol)))))
2137 :contents-begin contents-begin
2138 :contents-end contents-end
2140 :post-blank
(count-lines pos-before-blank end
))
2141 (cadr keywords
)))))))))
2143 (defun org-element-verse-block-interpreter (verse-block contents
)
2144 "Interpret VERSE-BLOCK element as Org syntax.
2145 CONTENTS is verse block contents."
2146 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
2152 ;; Unlike to elements, interstices can be found between objects.
2153 ;; That's why, along with the parser, successor functions are provided
2154 ;; for each object. Some objects share the same successor (i.e. `code'
2155 ;; and `verbatim' objects).
2157 ;; A successor must accept a single argument bounding the search. It
2158 ;; will return either a cons cell whose CAR is the object's type, as
2159 ;; a symbol, and CDR the position of its next occurrence, or nil.
2161 ;; Successors follow the naming convention:
2162 ;; org-element-NAME-successor, where NAME is the name of the
2163 ;; successor, as defined in `org-element-all-successors'.
2165 ;; Some object types (i.e. `emphasis') are recursive. Restrictions on
2166 ;; object types they can contain will be specified in
2167 ;; `org-element-object-restrictions'.
2169 ;; Adding a new type of object is simple. Implement a successor,
2170 ;; a parser, and an interpreter for it, all following the naming
2171 ;; convention. Register type in `org-element-all-objects' and
2172 ;; successor in `org-element-all-successors'. Maybe tweak
2173 ;; restrictions about it, and that's it.
2178 (defun org-element-bold-parser ()
2179 "Parse bold object at point.
2181 Return a list whose CAR is `bold' and CDR is a plist with
2182 `:begin', `:end', `:contents-begin' and `:contents-end' and
2183 `:post-blank' keywords.
2185 Assume point is at the first star marker."
2187 (unless (bolp) (backward-char 1))
2188 (looking-at org-emph-re
)
2189 (let ((begin (match-beginning 2))
2190 (contents-begin (match-beginning 4))
2191 (contents-end (match-end 4))
2192 (post-blank (progn (goto-char (match-end 2))
2193 (skip-chars-forward " \t")))
2198 :contents-begin contents-begin
2199 :contents-end contents-end
2200 :post-blank post-blank
)))))
2202 (defun org-element-bold-interpreter (bold contents
)
2203 "Interpret BOLD object as Org syntax.
2204 CONTENTS is the contents of the object."
2205 (format "*%s*" contents
))
2207 (defun org-element-text-markup-successor (limit)
2208 "Search for the next text-markup object.
2210 LIMIT bounds the search.
2212 LIMIT bounds the search.
2214 Return value is a cons cell whose CAR is a symbol among `bold',
2215 `italic', `underline', `strike-through', `code' and `verbatim'
2216 and CDR is beginning position."
2218 (unless (bolp) (backward-char))
2219 (when (re-search-forward org-emph-re limit t
)
2220 (let ((marker (match-string 3)))
2222 ((equal marker
"*") 'bold
)
2223 ((equal marker
"/") 'italic
)
2224 ((equal marker
"_") 'underline
)
2225 ((equal marker
"+") 'strike-through
)
2226 ((equal marker
"~") 'code
)
2227 ((equal marker
"=") 'verbatim
)
2228 (t (error "Unknown marker at %d" (match-beginning 3))))
2229 (match-beginning 2))))))
2234 (defun org-element-code-parser ()
2235 "Parse code object at point.
2237 Return a list whose CAR is `code' and CDR is a plist with
2238 `:value', `:begin', `:end' and `:post-blank' keywords.
2240 Assume point is at the first tilde marker."
2242 (unless (bolp) (backward-char 1))
2243 (looking-at org-emph-re
)
2244 (let ((begin (match-beginning 2))
2245 (value (org-match-string-no-properties 4))
2246 (post-blank (progn (goto-char (match-end 2))
2247 (skip-chars-forward " \t")))
2253 :post-blank post-blank
)))))
2255 (defun org-element-code-interpreter (code contents
)
2256 "Interpret CODE object as Org syntax.
2258 (format "~%s~" (org-element-property :value code
)))
2263 (defun org-element-entity-parser ()
2264 "Parse entity at point.
2266 Return a list whose CAR is `entity' and CDR a plist with
2267 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2268 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2271 Assume point is at the beginning of the entity."
2273 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2274 (let* ((value (org-entity-get (match-string 1)))
2275 (begin (match-beginning 0))
2276 (bracketsp (string= (match-string 2) "{}"))
2277 (post-blank (progn (goto-char (match-end 1))
2278 (when bracketsp
(forward-char 2))
2279 (skip-chars-forward " \t")))
2282 (list :name
(car value
)
2283 :latex
(nth 1 value
)
2284 :latex-math-p
(nth 2 value
)
2286 :ascii
(nth 4 value
)
2287 :latin1
(nth 5 value
)
2288 :utf-8
(nth 6 value
)
2291 :use-brackets-p bracketsp
2292 :post-blank post-blank
)))))
2294 (defun org-element-entity-interpreter (entity contents
)
2295 "Interpret ENTITY object as Org syntax.
2298 (org-element-property :name entity
)
2299 (when (org-element-property :use-brackets-p entity
) "{}")))
2301 (defun org-element-latex-or-entity-successor (limit)
2302 "Search for the next latex-fragment or entity object.
2304 LIMIT bounds the search.
2306 Return value is a cons cell whose CAR is `entity' or
2307 `latex-fragment' and CDR is beginning position."
2309 (let ((matchers (plist-get org-format-latex-options
:matchers
))
2310 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2312 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2313 (when (re-search-forward
2314 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2318 (goto-char (match-beginning 0))
2319 (if (looking-at entity-re
)
2320 ;; Determine if it's a real entity or a LaTeX command.
2321 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2322 (match-beginning 0))
2323 ;; No entity nor command: point is at a LaTeX fragment.
2324 ;; Determine its type to get the correct beginning position.
2325 (cons 'latex-fragment
2328 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2331 (nth 2 (assoc e org-latex-regexps
))))))
2338 (defun org-element-export-snippet-parser ()
2339 "Parse export snippet at point.
2341 Return a list whose CAR is `export-snippet' and CDR a plist with
2342 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2345 Assume point is at the beginning of the snippet."
2347 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2348 (let* ((begin (match-beginning 0))
2349 (back-end (org-match-string-no-properties 1))
2350 (value (buffer-substring-no-properties
2352 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2353 (post-blank (skip-chars-forward " \t"))
2355 (list 'export-snippet
2356 (list :back-end back-end
2360 :post-blank post-blank
)))))
2362 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2363 "Interpret EXPORT-SNIPPET object as Org syntax.
2366 (org-element-property :back-end export-snippet
)
2367 (org-element-property :value export-snippet
)))
2369 (defun org-element-export-snippet-successor (limit)
2370 "Search for the next export-snippet object.
2372 LIMIT bounds the search.
2374 Return value is a cons cell whose CAR is `export-snippet' and CDR
2375 its beginning position."
2378 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2379 (setq beg
(match-beginning 0))
2380 (re-search-forward "@@" limit t
))
2381 (cons 'export-snippet beg
)))))
2384 ;;;; Footnote Reference
2386 (defun org-element-footnote-reference-parser ()
2387 "Parse footnote reference at point.
2389 Return a list whose CAR is `footnote-reference' and CDR a plist
2390 with `:label', `:type', `:inline-definition', `:begin', `:end'
2391 and `:post-blank' as keywords."
2393 (looking-at org-footnote-re
)
2394 (let* ((begin (point))
2395 (label (or (org-match-string-no-properties 2)
2396 (org-match-string-no-properties 3)
2397 (and (match-string 1)
2398 (concat "fn:" (org-match-string-no-properties 1)))))
2399 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2400 (inner-begin (match-end 0))
2404 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2405 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2407 (post-blank (progn (goto-char (1+ inner-end
))
2408 (skip-chars-forward " \t")))
2411 (list 'footnote-reference
2416 :post-blank post-blank
))))
2417 (org-element-put-property
2418 footnote-reference
:inline-definition
2419 (and (eq type
'inline
)
2420 (org-element-parse-secondary-string
2421 (buffer-substring inner-begin inner-end
)
2422 (org-element-restriction 'footnote-reference
)
2423 footnote-reference
))))))
2425 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2426 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2428 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2431 (org-element-property :inline-definition footnote-reference
)))
2432 (if (not inline-def
) ""
2433 (concat ":" (org-element-interpret-data inline-def
))))))
2434 (format "[%s]" (concat label def
))))
2436 (defun org-element-footnote-reference-successor (limit)
2437 "Search for the next footnote-reference object.
2439 LIMIT bounds the search.
2441 Return value is a cons cell whose CAR is `footnote-reference' and
2442 CDR is beginning position."
2445 (while (re-search-forward org-footnote-re limit t
)
2447 (let ((beg (match-beginning 0))
2450 (while (re-search-forward "[][]" limit t
)
2451 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2453 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2456 ;;;; Inline Babel Call
2458 (defun org-element-inline-babel-call-parser ()
2459 "Parse inline babel call at point.
2461 Return a list whose CAR is `inline-babel-call' and CDR a plist
2462 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2464 Assume point is at the beginning of the babel call."
2466 (unless (bolp) (backward-char))
2467 (looking-at org-babel-inline-lob-one-liner-regexp
)
2468 (let ((info (save-match-data (org-babel-lob-get-info)))
2469 (begin (match-end 1))
2470 (post-blank (progn (goto-char (match-end 0))
2471 (skip-chars-forward " \t")))
2473 (list 'inline-babel-call
2477 :post-blank post-blank
)))))
2479 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2480 "Interpret INLINE-BABEL-CALL object as Org syntax.
2482 (let* ((babel-info (org-element-property :info inline-babel-call
))
2483 (main-source (car babel-info
))
2484 (post-options (nth 1 babel-info
)))
2486 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2487 ;; Remove redundant square brackets.
2489 (match-string 1 main-source
) nil nil main-source
)
2491 (and post-options
(format "[%s]" post-options
)))))
2493 (defun org-element-inline-babel-call-successor (limit)
2494 "Search for the next inline-babel-call object.
2496 LIMIT bounds the search.
2498 Return value is a cons cell whose CAR is `inline-babel-call' and
2499 CDR is beginning position."
2501 ;; Use a simplified version of
2502 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
2503 (when (re-search-forward
2504 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
2506 (cons 'inline-babel-call
(match-beginning 0)))))
2509 ;;;; Inline Src Block
2511 (defun org-element-inline-src-block-parser ()
2512 "Parse inline source block at point.
2514 LIMIT bounds the search.
2516 Return a list whose CAR is `inline-src-block' and CDR a plist
2517 with `:begin', `:end', `:language', `:value', `:parameters' and
2518 `:post-blank' as keywords.
2520 Assume point is at the beginning of the inline src block."
2522 (unless (bolp) (backward-char))
2523 (looking-at org-babel-inline-src-block-regexp
)
2524 (let ((begin (match-beginning 1))
2525 (language (org-match-string-no-properties 2))
2526 (parameters (org-match-string-no-properties 4))
2527 (value (org-match-string-no-properties 5))
2528 (post-blank (progn (goto-char (match-end 0))
2529 (skip-chars-forward " \t")))
2531 (list 'inline-src-block
2532 (list :language language
2534 :parameters parameters
2537 :post-blank post-blank
)))))
2539 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2540 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2542 (let ((language (org-element-property :language inline-src-block
))
2543 (arguments (org-element-property :parameters inline-src-block
))
2544 (body (org-element-property :value inline-src-block
)))
2545 (format "src_%s%s{%s}"
2547 (if arguments
(format "[%s]" arguments
) "")
2550 (defun org-element-inline-src-block-successor (limit)
2551 "Search for the next inline-babel-call element.
2553 LIMIT bounds the search.
2555 Return value is a cons cell whose CAR is `inline-babel-call' and
2556 CDR is beginning position."
2558 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2559 (cons 'inline-src-block
(match-beginning 1)))))
2563 (defun org-element-italic-parser ()
2564 "Parse italic object at point.
2566 Return a list whose CAR is `italic' and CDR is a plist with
2567 `:begin', `:end', `:contents-begin' and `:contents-end' and
2568 `:post-blank' keywords.
2570 Assume point is at the first slash marker."
2572 (unless (bolp) (backward-char 1))
2573 (looking-at org-emph-re
)
2574 (let ((begin (match-beginning 2))
2575 (contents-begin (match-beginning 4))
2576 (contents-end (match-end 4))
2577 (post-blank (progn (goto-char (match-end 2))
2578 (skip-chars-forward " \t")))
2583 :contents-begin contents-begin
2584 :contents-end contents-end
2585 :post-blank post-blank
)))))
2587 (defun org-element-italic-interpreter (italic contents
)
2588 "Interpret ITALIC object as Org syntax.
2589 CONTENTS is the contents of the object."
2590 (format "/%s/" contents
))
2595 (defun org-element-latex-fragment-parser ()
2596 "Parse latex fragment at point.
2598 Return a list whose CAR is `latex-fragment' and CDR a plist with
2599 `:value', `:begin', `:end', and `:post-blank' as keywords.
2601 Assume point is at the beginning of the latex fragment."
2603 (let* ((begin (point))
2607 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2608 (when (or (looking-at latex-regexp
)
2612 (looking-at latex-regexp
))))
2613 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2614 (plist-get org-format-latex-options
:matchers
))
2615 ;; None found: it's a macro.
2616 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2618 (value (match-string-no-properties substring-match
))
2619 (post-blank (progn (goto-char (match-end substring-match
))
2620 (skip-chars-forward " \t")))
2622 (list 'latex-fragment
2626 :post-blank post-blank
)))))
2628 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2629 "Interpret LATEX-FRAGMENT object as Org syntax.
2631 (org-element-property :value latex-fragment
))
2635 (defun org-element-line-break-parser ()
2636 "Parse line break at point.
2638 Return a list whose CAR is `line-break', and CDR a plist with
2639 `:begin', `:end' and `:post-blank' keywords.
2641 Assume point is at the beginning of the line break."
2642 (list 'line-break
(list :begin
(point) :end
(point-at-eol) :post-blank
0)))
2644 (defun org-element-line-break-interpreter (line-break contents
)
2645 "Interpret LINE-BREAK object as Org syntax.
2649 (defun org-element-line-break-successor (limit)
2650 "Search for the next line-break object.
2652 LIMIT bounds the search.
2654 Return value is a cons cell whose CAR is `line-break' and CDR is
2655 beginning position."
2657 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2658 (goto-char (match-beginning 1)))))
2659 ;; A line break can only happen on a non-empty line.
2660 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2661 (cons 'line-break beg
)))))
2666 (defun org-element-link-parser ()
2667 "Parse link at point.
2669 Return a list whose CAR is `link' and CDR a plist with `:type',
2670 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2671 `:contents-end' and `:post-blank' as keywords.
2673 Assume point is at the beginning of the link."
2675 (let ((begin (point))
2676 end contents-begin contents-end link-end post-blank path type
2679 ;; Type 1: Text targeted from a radio target.
2680 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2682 link-end
(match-end 0)
2683 path
(org-match-string-no-properties 0)))
2684 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2685 ((looking-at org-bracket-link-regexp
)
2686 (setq contents-begin
(match-beginning 3)
2687 contents-end
(match-end 3)
2688 link-end
(match-end 0)
2689 ;; RAW-LINK is the original link.
2690 raw-link
(org-match-string-no-properties 1)
2691 link
(org-translate-link
2692 (org-link-expand-abbrev
2693 (org-link-unescape raw-link
))))
2694 ;; Determine TYPE of link and set PATH accordingly.
2697 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2698 (setq type
"file" path link
))
2699 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2700 ((string-match org-link-re-with-space3 link
)
2701 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2702 ;; Id type: PATH is the id.
2703 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2704 (setq type
"id" path
(match-string 1 link
)))
2705 ;; Code-ref type: PATH is the name of the reference.
2706 ((string-match "^(\\(.*\\))$" link
)
2707 (setq type
"coderef" path
(match-string 1 link
)))
2708 ;; Custom-id type: PATH is the name of the custom id.
2709 ((= (aref link
0) ?
#)
2710 (setq type
"custom-id" path
(substring link
1)))
2711 ;; Fuzzy type: Internal link either matches a target, an
2712 ;; headline name or nothing. PATH is the target or
2714 (t (setq type
"fuzzy" path link
))))
2715 ;; Type 3: Plain link, i.e. http://orgmode.org
2716 ((looking-at org-plain-link-re
)
2717 (setq raw-link
(org-match-string-no-properties 0)
2718 type
(org-match-string-no-properties 1)
2719 path
(org-match-string-no-properties 2)
2720 link-end
(match-end 0)))
2721 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2722 ((looking-at org-angle-link-re
)
2723 (setq raw-link
(buffer-substring-no-properties
2724 (match-beginning 1) (match-end 2))
2725 type
(org-match-string-no-properties 1)
2726 path
(org-match-string-no-properties 2)
2727 link-end
(match-end 0))))
2728 ;; In any case, deduce end point after trailing white space from
2729 ;; LINK-END variable.
2730 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2735 :raw-link
(or raw-link path
)
2738 :contents-begin contents-begin
2739 :contents-end contents-end
2740 :post-blank post-blank
)))))
2742 (defun org-element-link-interpreter (link contents
)
2743 "Interpret LINK object as Org syntax.
2744 CONTENTS is the contents of the object, or nil."
2745 (let ((type (org-element-property :type link
))
2746 (raw-link (org-element-property :raw-link link
)))
2747 (if (string= type
"radio") raw-link
2750 (if contents
(format "[%s]" contents
) "")))))
2752 (defun org-element-link-successor (limit)
2753 "Search for the next link object.
2755 LIMIT bounds the search.
2757 Return value is a cons cell whose CAR is `link' and CDR is
2758 beginning position."
2761 (if (not org-target-link-regexp
) org-any-link-re
2762 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2763 (when (re-search-forward link-regexp limit t
)
2764 (cons 'link
(match-beginning 0))))))
2769 (defun org-element-macro-parser ()
2770 "Parse macro at point.
2772 Return a list whose CAR is `macro' and CDR a plist with `:key',
2773 `:args', `:begin', `:end', `:value' and `:post-blank' as
2776 Assume point is at the macro."
2778 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2779 (let ((begin (point))
2780 (key (downcase (org-match-string-no-properties 1)))
2781 (value (org-match-string-no-properties 0))
2782 (post-blank (progn (goto-char (match-end 0))
2783 (skip-chars-forward " \t")))
2785 (args (let ((args (org-match-string-no-properties 3)) args2
)
2787 (setq args
(org-split-string args
","))
2789 (while (string-match "\\\\\\'" (car args
))
2790 ;; Repair bad splits.
2791 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2794 (push (pop args
) args2
))
2795 (mapcar 'org-trim
(nreverse args2
))))))
2802 :post-blank post-blank
)))))
2804 (defun org-element-macro-interpreter (macro contents
)
2805 "Interpret MACRO object as Org syntax.
2807 (org-element-property :value macro
))
2809 (defun org-element-macro-successor (limit)
2810 "Search for the next macro object.
2812 LIMIT bounds the search.
2814 Return value is cons cell whose CAR is `macro' and CDR is
2815 beginning position."
2817 (when (re-search-forward
2818 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2820 (cons 'macro
(match-beginning 0)))))
2825 (defun org-element-radio-target-parser ()
2826 "Parse radio target at point.
2828 Return a list whose CAR is `radio-target' and CDR a plist with
2829 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2830 and `:post-blank' as keywords.
2832 Assume point is at the radio target."
2834 (looking-at org-radio-target-regexp
)
2835 (let ((begin (point))
2836 (contents-begin (match-beginning 1))
2837 (contents-end (match-end 1))
2838 (value (org-match-string-no-properties 1))
2839 (post-blank (progn (goto-char (match-end 0))
2840 (skip-chars-forward " \t")))
2845 :contents-begin contents-begin
2846 :contents-end contents-end
2847 :post-blank post-blank
2850 (defun org-element-radio-target-interpreter (target contents
)
2851 "Interpret TARGET object as Org syntax.
2852 CONTENTS is the contents of the object."
2853 (concat "<<<" contents
">>>"))
2855 (defun org-element-radio-target-successor (limit)
2856 "Search for the next radio-target object.
2858 LIMIT bounds the search.
2860 Return value is a cons cell whose CAR is `radio-target' and CDR
2861 is beginning position."
2863 (when (re-search-forward org-radio-target-regexp limit t
)
2864 (cons 'radio-target
(match-beginning 0)))))
2867 ;;;; Statistics Cookie
2869 (defun org-element-statistics-cookie-parser ()
2870 "Parse statistics cookie at point.
2872 Return a list whose CAR is `statistics-cookie', and CDR a plist
2873 with `:begin', `:end', `:value' and `:post-blank' keywords.
2875 Assume point is at the beginning of the statistics-cookie."
2877 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2878 (let* ((begin (point))
2879 (value (buffer-substring-no-properties
2880 (match-beginning 0) (match-end 0)))
2881 (post-blank (progn (goto-char (match-end 0))
2882 (skip-chars-forward " \t")))
2884 (list 'statistics-cookie
2888 :post-blank post-blank
)))))
2890 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
2891 "Interpret STATISTICS-COOKIE object as Org syntax.
2893 (org-element-property :value statistics-cookie
))
2895 (defun org-element-statistics-cookie-successor (limit)
2896 "Search for the next statistics cookie object.
2898 LIMIT bounds the search.
2900 Return value is a cons cell whose CAR is `statistics-cookie' and
2901 CDR is beginning position."
2903 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
2904 (cons 'statistics-cookie
(match-beginning 0)))))
2909 (defun org-element-strike-through-parser ()
2910 "Parse strike-through object at point.
2912 Return a list whose CAR is `strike-through' and CDR is a plist
2913 with `:begin', `:end', `:contents-begin' and `:contents-end' and
2914 `:post-blank' keywords.
2916 Assume point is at the first plus sign marker."
2918 (unless (bolp) (backward-char 1))
2919 (looking-at org-emph-re
)
2920 (let ((begin (match-beginning 2))
2921 (contents-begin (match-beginning 4))
2922 (contents-end (match-end 4))
2923 (post-blank (progn (goto-char (match-end 2))
2924 (skip-chars-forward " \t")))
2926 (list 'strike-through
2929 :contents-begin contents-begin
2930 :contents-end contents-end
2931 :post-blank post-blank
)))))
2933 (defun org-element-strike-through-interpreter (strike-through contents
)
2934 "Interpret STRIKE-THROUGH object as Org syntax.
2935 CONTENTS is the contents of the object."
2936 (format "+%s+" contents
))
2941 (defun org-element-subscript-parser ()
2942 "Parse subscript at point.
2944 Return a list whose CAR is `subscript' and CDR a plist with
2945 `:begin', `:end', `:contents-begin', `:contents-end',
2946 `:use-brackets-p' and `:post-blank' as keywords.
2948 Assume point is at the underscore."
2950 (unless (bolp) (backward-char))
2951 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2953 (not (looking-at org-match-substring-regexp
))))
2954 (begin (match-beginning 2))
2955 (contents-begin (or (match-beginning 5)
2956 (match-beginning 3)))
2957 (contents-end (or (match-end 5) (match-end 3)))
2958 (post-blank (progn (goto-char (match-end 0))
2959 (skip-chars-forward " \t")))
2964 :use-brackets-p bracketsp
2965 :contents-begin contents-begin
2966 :contents-end contents-end
2967 :post-blank post-blank
)))))
2969 (defun org-element-subscript-interpreter (subscript contents
)
2970 "Interpret SUBSCRIPT object as Org syntax.
2971 CONTENTS is the contents of the object."
2973 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
2976 (defun org-element-sub/superscript-successor
(limit)
2977 "Search for the next sub/superscript object.
2979 LIMIT bounds the search.
2981 Return value is a cons cell whose CAR is either `subscript' or
2982 `superscript' and CDR is beginning position."
2984 (when (re-search-forward org-match-substring-regexp limit t
)
2985 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
2986 (match-beginning 2)))))
2991 (defun org-element-superscript-parser ()
2992 "Parse superscript at point.
2994 Return a list whose CAR is `superscript' and CDR a plist with
2995 `:begin', `:end', `:contents-begin', `:contents-end',
2996 `:use-brackets-p' and `:post-blank' as keywords.
2998 Assume point is at the caret."
3000 (unless (bolp) (backward-char))
3001 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
3002 (not (looking-at org-match-substring-regexp
))))
3003 (begin (match-beginning 2))
3004 (contents-begin (or (match-beginning 5)
3005 (match-beginning 3)))
3006 (contents-end (or (match-end 5) (match-end 3)))
3007 (post-blank (progn (goto-char (match-end 0))
3008 (skip-chars-forward " \t")))
3013 :use-brackets-p bracketsp
3014 :contents-begin contents-begin
3015 :contents-end contents-end
3016 :post-blank post-blank
)))))
3018 (defun org-element-superscript-interpreter (superscript contents
)
3019 "Interpret SUPERSCRIPT object as Org syntax.
3020 CONTENTS is the contents of the object."
3022 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
3028 (defun org-element-table-cell-parser ()
3029 "Parse table cell at point.
3031 Return a list whose CAR is `table-cell' and CDR is a plist
3032 containing `:begin', `:end', `:contents-begin', `:contents-end'
3033 and `:post-blank' keywords."
3034 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3035 (let* ((begin (match-beginning 0))
3037 (contents-begin (match-beginning 1))
3038 (contents-end (match-end 1)))
3042 :contents-begin contents-begin
3043 :contents-end contents-end
3046 (defun org-element-table-cell-interpreter (table-cell contents
)
3047 "Interpret TABLE-CELL element as Org syntax.
3048 CONTENTS is the contents of the cell, or nil."
3049 (concat " " contents
" |"))
3051 (defun org-element-table-cell-successor (limit)
3052 "Search for the next table-cell object.
3054 LIMIT bounds the search.
3056 Return value is a cons cell whose CAR is `table-cell' and CDR is
3057 beginning position."
3058 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
3063 (defun org-element-target-parser ()
3064 "Parse target at point.
3066 Return a list whose CAR is `target' and CDR a plist with
3067 `:begin', `:end', `:value' and `:post-blank' as keywords.
3069 Assume point is at the target."
3071 (looking-at org-target-regexp
)
3072 (let ((begin (point))
3073 (value (org-match-string-no-properties 1))
3074 (post-blank (progn (goto-char (match-end 0))
3075 (skip-chars-forward " \t")))
3081 :post-blank post-blank
)))))
3083 (defun org-element-target-interpreter (target contents
)
3084 "Interpret TARGET object as Org syntax.
3086 (format "<<%s>>" (org-element-property :value target
)))
3088 (defun org-element-target-successor (limit)
3089 "Search for the next target object.
3091 LIMIT bounds the search.
3093 Return value is a cons cell whose CAR is `target' and CDR is
3094 beginning position."
3096 (when (re-search-forward org-target-regexp limit t
)
3097 (cons 'target
(match-beginning 0)))))
3102 (defun org-element-timestamp-parser ()
3103 "Parse time stamp at point.
3105 Return a list whose CAR is `timestamp', and CDR a plist with
3106 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3108 Assume point is at the beginning of the timestamp."
3110 (let* ((begin (point))
3111 (activep (eq (char-after) ?
<))
3114 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3115 (match-string-no-properties 1)))
3116 (range-end (match-string-no-properties 3))
3117 (type (cond ((match-string 2) 'diary
)
3118 ((and activep range-end
) 'active-range
)
3120 (range-end 'inactive-range
)
3122 (post-blank (progn (goto-char (match-end 0))
3123 (skip-chars-forward " \t")))
3128 :range-end range-end
3131 :post-blank post-blank
)))))
3133 (defun org-element-timestamp-interpreter (timestamp contents
)
3134 "Interpret TIMESTAMP object as Org syntax.
3136 (let ((type (org-element-property :type timestamp
) ))
3138 (format (if (memq type
'(inactive inactive-range
)) "[%s]" "<%s>")
3139 (org-element-property :value timestamp
))
3140 (let ((range-end (org-element-property :range-end timestamp
)))
3143 (format (if (eq type
'inactive-range
) "[%s]" "<%s>")
3146 (defun org-element-timestamp-successor (limit)
3147 "Search for the next timestamp object.
3149 LIMIT bounds the search.
3151 Return value is a cons cell whose CAR is `timestamp' and CDR is
3152 beginning position."
3154 (when (re-search-forward
3155 (concat org-ts-regexp-both
3157 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3159 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3161 (cons 'timestamp
(match-beginning 0)))))
3166 (defun org-element-underline-parser ()
3167 "Parse underline object at point.
3169 Return a list whose CAR is `underline' and CDR is a plist with
3170 `:begin', `:end', `:contents-begin' and `:contents-end' and
3171 `:post-blank' keywords.
3173 Assume point is at the first underscore marker."
3175 (unless (bolp) (backward-char 1))
3176 (looking-at org-emph-re
)
3177 (let ((begin (match-beginning 2))
3178 (contents-begin (match-beginning 4))
3179 (contents-end (match-end 4))
3180 (post-blank (progn (goto-char (match-end 2))
3181 (skip-chars-forward " \t")))
3186 :contents-begin contents-begin
3187 :contents-end contents-end
3188 :post-blank post-blank
)))))
3190 (defun org-element-underline-interpreter (underline contents
)
3191 "Interpret UNDERLINE object as Org syntax.
3192 CONTENTS is the contents of the object."
3193 (format "_%s_" contents
))
3198 (defun org-element-verbatim-parser ()
3199 "Parse verbatim object at point.
3201 Return a list whose CAR is `verbatim' and CDR is a plist with
3202 `:value', `:begin', `:end' and `:post-blank' keywords.
3204 Assume point is at the first equal sign marker."
3206 (unless (bolp) (backward-char 1))
3207 (looking-at org-emph-re
)
3208 (let ((begin (match-beginning 2))
3209 (value (org-match-string-no-properties 4))
3210 (post-blank (progn (goto-char (match-end 2))
3211 (skip-chars-forward " \t")))
3217 :post-blank post-blank
)))))
3219 (defun org-element-verbatim-interpreter (verbatim contents
)
3220 "Interpret VERBATIM object as Org syntax.
3222 (format "=%s=" (org-element-property :value verbatim
)))
3226 ;;; Parsing Element Starting At Point
3228 ;; `org-element--current-element' is the core function of this section.
3229 ;; It returns the Lisp representation of the element starting at
3232 ;; `org-element--current-element' makes use of special modes. They
3233 ;; are activated for fixed element chaining (i.e. `plain-list' >
3234 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3235 ;; `section'). Special modes are: `first-section', `section',
3236 ;; `quote-section', `item' and `table-row'.
3238 (defun org-element--current-element
3239 (limit &optional granularity special structure
)
3240 "Parse the element starting at point.
3242 LIMIT bounds the search.
3244 Return value is a list like (TYPE PROPS) where TYPE is the type
3245 of the element and PROPS a plist of properties associated to the
3248 Possible types are defined in `org-element-all-elements'.
3250 Optional argument GRANULARITY determines the depth of the
3251 recursion. Allowed values are `headline', `greater-element',
3252 `element', `object' or nil. When it is broader than `object' (or
3253 nil), secondary values will not be parsed, since they only
3256 Optional argument SPECIAL, when non-nil, can be either
3257 `first-section', `section', `quote-section', `table-row' and
3260 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3263 This function assumes point is always at the beginning of the
3264 element it has to parse."
3266 ;; If point is at an affiliated keyword, try moving to the
3267 ;; beginning of the associated element. If none is found, the
3268 ;; keyword is orphaned and will be treated as plain text.
3269 (when (looking-at org-element--affiliated-re
)
3270 (let ((opoint (point)))
3271 (while (looking-at org-element--affiliated-re
) (forward-line))
3272 (when (looking-at "[ \t]*$") (goto-char opoint
))))
3273 (let ((case-fold-search t
)
3274 ;; Determine if parsing depth allows for secondary strings
3275 ;; parsing. It only applies to elements referenced in
3276 ;; `org-element-secondary-value-alist'.
3277 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3281 (org-element-item-parser limit structure raw-secondary-p
))
3283 ((eq special
'quote-section
) (org-element-quote-section-parser limit
))
3285 ((eq special
'table-row
) (org-element-table-row-parser limit
))
3287 ((org-with-limited-levels (org-at-heading-p))
3288 (org-element-headline-parser limit raw-secondary-p
))
3289 ;; Section (must be checked after headline).
3290 ((eq special
'section
) (org-element-section-parser limit
))
3291 ((eq special
'first-section
)
3292 (org-element-section-parser
3293 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3295 ;; Planning and Clock.
3296 ((and (looking-at org-planning-or-clock-line-re
))
3297 (if (equal (match-string 1) org-clock-string
)
3298 (org-element-clock-parser limit
)
3299 (org-element-planning-parser limit
)))
3302 (org-element-inlinetask-parser limit raw-secondary-p
))
3303 ;; LaTeX Environment.
3304 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
3307 (format "[ \t]*\\\\end{%s}[ \t]*"
3308 (regexp-quote (match-string 1)))
3310 (org-element-latex-environment-parser limit
)
3311 (org-element-paragraph-parser limit
)))
3312 ;; Drawer and Property Drawer.
3313 ((looking-at org-drawer-regexp
)
3314 (let ((name (match-string 1)))
3316 ((not (save-excursion
3317 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
)))
3318 (org-element-paragraph-parser limit
))
3319 ((equal "PROPERTIES" name
)
3320 (org-element-property-drawer-parser limit
))
3321 (t (org-element-drawer-parser limit
)))))
3323 ((looking-at "[ \t]*:\\( \\|$\\)")
3324 (org-element-fixed-width-parser limit
))
3325 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3327 ((looking-at "[ \t]*#\\+")
3328 (goto-char (match-end 0))
3329 (cond ((looking-at "$\\| ")
3331 (org-element-comment-parser limit
))
3332 ((looking-at "BEGIN_\\(\\S-+\\)")
3334 (let ((parser (assoc (upcase (match-string 1))
3335 org-element-block-name-alist
)))
3336 (if parser
(funcall (cdr parser
) limit
)
3337 (org-element-special-block-parser limit
))))
3338 ((looking-at "CALL")
3340 (org-element-babel-call-parser limit
))
3341 ((looking-at "BEGIN:? ")
3343 (org-element-dynamic-block-parser limit
))
3344 ((looking-at "\\S-+:")
3346 (org-element-keyword-parser limit
))
3347 ;; Ill-formed syntax is considered as a comment.
3350 (org-element-comment-parser limit
))))
3352 ((eq (char-after) ?
#) (org-element-comment-parser limit
))
3353 ;; Footnote Definition.
3354 ((looking-at org-footnote-definition-re
)
3355 (org-element-footnote-definition-parser limit
))
3357 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3358 (org-element-horizontal-rule-parser limit
))
3360 ((org-at-table-p t
) (org-element-table-parser limit
))
3362 ((looking-at (org-item-re))
3363 (org-element-plain-list-parser limit
(or structure
(org-list-struct))))
3364 ;; Default element: Paragraph.
3365 (t (org-element-paragraph-parser limit
))))))
3368 ;; Most elements can have affiliated keywords. When looking for an
3369 ;; element beginning, we want to move before them, as they belong to
3370 ;; that element, and, in the meantime, collect information they give
3371 ;; into appropriate properties. Hence the following function.
3373 ;; Usage of optional arguments may not be obvious at first glance:
3375 ;; - TRANS-LIST is used to polish keywords names that have evolved
3376 ;; during Org history. In example, even though =result= and
3377 ;; =results= coexist, we want to have them under the same =result=
3378 ;; property. It's also true for "srcname" and "name", where the
3379 ;; latter seems to be preferred nowadays (thus the "name" property).
3381 ;; - CONSED allows to regroup multi-lines keywords under the same
3382 ;; property, while preserving their own identity. This is mostly
3383 ;; used for "attr_latex" and al.
3385 ;; - PARSED prepares a keyword value for export. This is useful for
3386 ;; "caption". Objects restrictions for such keywords are defined in
3387 ;; `org-element-object-restrictions'.
3389 ;; - DUALS is used to take care of keywords accepting a main and an
3390 ;; optional secondary values. For example "results" has its
3391 ;; source's name as the main value, and may have an hash string in
3392 ;; optional square brackets as the secondary one.
3394 ;; A keyword may belong to more than one category.
3396 (defun org-element--collect-affiliated-keywords
3397 (&optional key-re trans-list consed parsed duals
)
3398 "Collect affiliated keywords before point.
3400 Optional argument KEY-RE is a regexp matching keywords, which
3401 puts matched keyword in group 1. It defaults to
3402 `org-element--affiliated-re'.
3404 TRANS-LIST is an alist where key is the keyword and value the
3405 property name it should be translated to, without the colons. It
3406 defaults to `org-element-keyword-translation-alist'.
3408 CONSED is a list of strings. Any keyword belonging to that list
3409 will have its value consed. The check is done after keyword
3410 translation. It defaults to `org-element-multiple-keywords'.
3412 PARSED is a list of strings. Any keyword member of this list
3413 will have its value parsed. The check is done after keyword
3414 translation. If a keyword is a member of both CONSED and PARSED,
3415 it's value will be a list of parsed strings. It defaults to
3416 `org-element-parsed-keywords'.
3418 DUALS is a list of strings. Any keyword member of this list can
3419 have two parts: one mandatory and one optional. Its value is
3420 a cons cell whose CAR is the former, and the CDR the latter. If
3421 a keyword is a member of both PARSED and DUALS, both values will
3422 be parsed. It defaults to `org-element-dual-keywords'.
3424 Return a list whose CAR is the position at the first of them and
3425 CDR a plist of keywords and values."
3427 (let ((case-fold-search t
)
3428 (key-re (or key-re org-element--affiliated-re
))
3429 (trans-list (or trans-list org-element-keyword-translation-alist
))
3430 (consed (or consed org-element-multiple-keywords
))
3431 (parsed (or parsed org-element-parsed-keywords
))
3432 (duals (or duals org-element-dual-keywords
))
3433 ;; RESTRICT is the list of objects allowed in parsed
3435 (restrict (org-element-restriction 'keyword
))
3438 (while (and (not (bobp)) (progn (forward-line -
1) (looking-at key-re
)))
3439 (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
3440 ;; Apply translation to RAW-KWD. From there, KWD is
3441 ;; the official keyword.
3442 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
3443 ;; Find main value for any keyword.
3447 (buffer-substring-no-properties
3448 (match-end 0) (point-at-eol)))))
3449 ;; If KWD is a dual keyword, find its secondary
3450 ;; value. Maybe parse it.
3452 (and (member kwd duals
)
3453 (let ((sec (org-match-string-no-properties 3)))
3454 (if (or (not sec
) (not (member kwd parsed
))) sec
3455 (org-element-parse-secondary-string sec restrict
)))))
3456 ;; Attribute a property name to KWD.
3457 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3458 ;; Now set final shape for VALUE.
3459 (when (member kwd parsed
)
3460 (setq value
(org-element-parse-secondary-string value restrict
)))
3461 (when (member kwd duals
)
3462 ;; VALUE is mandatory. Set it to nil if there is none.
3463 (setq value
(and value
(cons value dual-value
))))
3464 ;; Attributes are always consed.
3465 (when (or (member kwd consed
) (string-match "^ATTR_" kwd
))
3466 (setq value
(cons value
(plist-get output kwd-sym
))))
3467 ;; Eventually store the new value in OUTPUT.
3468 (setq output
(plist-put output kwd-sym value
))))
3469 (unless (looking-at key-re
) (forward-line 1)))
3470 (list (point) output
))))
3476 ;; The two major functions here are `org-element-parse-buffer', which
3477 ;; parses Org syntax inside the current buffer, taking into account
3478 ;; region, narrowing, or even visibility if specified, and
3479 ;; `org-element-parse-secondary-string', which parses objects within
3482 ;; The (almost) almighty `org-element-map' allows to apply a function
3483 ;; on elements or objects matching some type, and accumulate the
3484 ;; resulting values. In an export situation, it also skips unneeded
3485 ;; parts of the parse tree.
3487 (defun org-element-parse-buffer (&optional granularity visible-only
)
3488 "Recursively parse the buffer and return structure.
3489 If narrowing is in effect, only parse the visible part of the
3492 Optional argument GRANULARITY determines the depth of the
3493 recursion. It can be set to the following symbols:
3495 `headline' Only parse headlines.
3496 `greater-element' Don't recurse into greater elements excepted
3497 headlines and sections. Thus, elements
3498 parsed are the top-level ones.
3499 `element' Parse everything but objects and plain text.
3500 `object' Parse the complete buffer (default).
3502 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3505 Assume buffer is in Org mode."
3507 (goto-char (point-min))
3508 (org-skip-whitespace)
3509 (org-element--parse-elements
3510 (point-at-bol) (point-max)
3511 ;; Start in `first-section' mode so text before the first
3512 ;; headline belongs to a section.
3513 'first-section nil granularity visible-only
(list 'org-data nil
))))
3515 (defun org-element-parse-secondary-string (string restriction
&optional parent
)
3516 "Recursively parse objects in STRING and return structure.
3518 RESTRICTION is a symbol limiting the object types that will be
3521 Optional argument PARENT, when non-nil, is the element or object
3522 containing the secondary string. It is used to set correctly
3523 `:parent' property within the string."
3526 (let ((secondary (org-element--parse-objects
3527 (point-min) (point-max) nil restriction
)))
3528 (mapc (lambda (obj) (org-element-put-property obj
:parent parent
))
3531 (defun org-element-map (data types fun
&optional info first-match no-recursion
)
3532 "Map a function on selected elements or objects.
3534 DATA is the parsed tree, as returned by, i.e,
3535 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3536 of elements or objects types. FUN is the function called on the
3537 matching element or object. It must accept one arguments: the
3538 element or object itself.
3540 When optional argument INFO is non-nil, it should be a plist
3541 holding export options. In that case, parts of the parse tree
3542 not exportable according to that property list will be skipped.
3544 When optional argument FIRST-MATCH is non-nil, stop at the first
3545 match for which FUN doesn't return nil, and return that value.
3547 Optional argument NO-RECURSION is a symbol or a list of symbols
3548 representing elements or objects types. `org-element-map' won't
3549 enter any recursive element or object whose type belongs to that
3550 list. Though, FUN can still be applied on them.
3552 Nil values returned from FUN do not appear in the results."
3553 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3554 (unless (listp types
) (setq types
(list types
)))
3555 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3556 ;; Recursion depth is determined by --CATEGORY.
3559 (let ((category 'greater-elements
))
3560 (mapc (lambda (type)
3561 (cond ((or (memq type org-element-all-objects
)
3562 (eq type
'plain-text
))
3563 ;; If one object is found, the function
3564 ;; has to recurse into every object.
3565 (throw 'found
'objects
))
3566 ((not (memq type org-element-greater-elements
))
3567 ;; If one regular element is found, the
3568 ;; function has to recurse, at lest, into
3569 ;; every element it encounters.
3570 (and (not (eq category
'elements
))
3571 (setq category
'elements
)))))
3579 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3580 ;; holding contextual information.
3581 (let ((--type (org-element-type --data
)))
3584 ;; Ignored element in an export context.
3585 ((and info
(memq --data
(plist-get info
:ignore-list
))))
3586 ;; Secondary string: only objects can be found there.
3588 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
3589 ;; Unconditionally enter parse trees.
3590 ((eq --type
'org-data
)
3591 (mapc --walk-tree
(org-element-contents --data
)))
3593 ;; Check if TYPE is matching among TYPES. If so,
3594 ;; apply FUN to --DATA and accumulate return value
3595 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3596 (when (memq --type types
)
3597 (let ((result (funcall fun --data
)))
3598 (cond ((not result
))
3599 (first-match (throw '--map-first-match result
))
3600 (t (push result --acc
)))))
3601 ;; If --DATA has a secondary string that can contain
3602 ;; objects with their type among TYPES, look into it.
3603 (when (eq --category
'objects
)
3605 (assq --type org-element-secondary-value-alist
)))
3607 (funcall --walk-tree
3608 (org-element-property (cdr sec-prop
) --data
)))))
3609 ;; Determine if a recursion into --DATA is possible.
3611 ;; --TYPE is explicitly removed from recursion.
3612 ((memq --type no-recursion
))
3613 ;; --DATA has no contents.
3614 ((not (org-element-contents --data
)))
3615 ;; Looking for greater elements but --DATA is simply
3616 ;; an element or an object.
3617 ((and (eq --category
'greater-elements
)
3618 (not (memq --type org-element-greater-elements
))))
3619 ;; Looking for elements but --DATA is an object.
3620 ((and (eq --category
'elements
)
3621 (memq --type org-element-all-objects
)))
3622 ;; In any other case, map contents.
3623 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
3624 (catch '--map-first-match
3625 (funcall --walk-tree data
)
3626 ;; Return value in a proper order.
3629 ;; The following functions are internal parts of the parser.
3631 ;; The first one, `org-element--parse-elements' acts at the element's
3634 ;; The second one, `org-element--parse-objects' applies on all objects
3635 ;; of a paragraph or a secondary string. It uses
3636 ;; `org-element--get-next-object-candidates' to optimize the search of
3637 ;; the next object in the buffer.
3639 ;; More precisely, that function looks for every allowed object type
3640 ;; first. Then, it discards failed searches, keeps further matches,
3641 ;; and searches again types matched behind point, for subsequent
3642 ;; calls. Thus, searching for a given type fails only once, and every
3643 ;; object is searched only once at top level (but sometimes more for
3646 (defun org-element--parse-elements
3647 (beg end special structure granularity visible-only acc
)
3648 "Parse elements between BEG and END positions.
3650 SPECIAL prioritize some elements over the others. It can be set
3651 to `first-section', `quote-section', `section' `item' or
3654 When value is `item', STRUCTURE will be used as the current list
3657 GRANULARITY determines the depth of the recursion. See
3658 `org-element-parse-buffer' for more information.
3660 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3663 Elements are accumulated into ACC."
3666 ;; When parsing only headlines, skip any text before first one.
3667 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3668 (org-with-limited-levels (outline-next-heading)))
3670 (while (< (point) end
)
3671 ;; Find current element's type and parse it accordingly to
3673 (let* ((element (org-element--current-element
3674 end granularity special structure
))
3675 (type (org-element-type element
))
3676 (cbeg (org-element-property :contents-begin element
)))
3677 (goto-char (org-element-property :end element
))
3678 ;; Fill ELEMENT contents by side-effect.
3680 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3681 ;; no contents, don't modify it.
3682 ((or (and visible-only
(org-element-property :hiddenp element
))
3684 ;; Greater element: parse it between `contents-begin' and
3685 ;; `contents-end'. Make sure GRANULARITY allows the
3686 ;; recursion, or ELEMENT is an headline, in which case going
3687 ;; inside is mandatory, in order to get sub-level headings.
3688 ((and (memq type org-element-greater-elements
)
3689 (or (memq granularity
'(element object nil
))
3690 (and (eq granularity
'greater-element
)
3692 (eq type
'headline
)))
3693 (org-element--parse-elements
3694 cbeg
(org-element-property :contents-end element
)
3695 ;; Possibly switch to a special mode.
3698 (if (org-element-property :quotedp element
) 'quote-section
3702 (org-element-property :structure element
)
3703 granularity visible-only element
))
3704 ;; ELEMENT has contents. Parse objects inside, if
3705 ;; GRANULARITY allows it.
3706 ((memq granularity
'(object nil
))
3707 (org-element--parse-objects
3708 cbeg
(org-element-property :contents-end element
) element
3709 (org-element-restriction type
))))
3710 (org-element-adopt-element acc element t
)))
3714 (defun org-element--parse-objects (beg end acc restriction
)
3715 "Parse objects between BEG and END and return recursive structure.
3717 Objects are accumulated in ACC.
3719 RESTRICTION is a list of object types which are allowed in the
3724 (while (and (< (point) end
)
3725 (setq candidates
(org-element--get-next-object-candidates
3726 end restriction candidates
)))
3728 (let ((pos (apply 'min
(mapcar 'cdr candidates
))))
3731 (funcall (intern (format "org-element-%s-parser"
3732 (car (rassq pos candidates
)))))))))
3733 ;; 1. Text before any object. Untabify it.
3734 (let ((obj-beg (org-element-property :begin next-object
)))
3735 (unless (= (point) obj-beg
)
3737 (org-element-adopt-element
3739 (replace-regexp-in-string
3740 "\t" (make-string tab-width ?
)
3741 (buffer-substring-no-properties (point) obj-beg
)) t
))))
3743 (let ((obj-end (org-element-property :end next-object
))
3744 (cont-beg (org-element-property :contents-begin next-object
)))
3745 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3746 ;; a recursive type.
3748 (memq (car next-object
) org-element-recursive-objects
))
3752 (org-element-property :contents-end next-object
))
3753 (org-element--parse-objects
3754 (point-min) (point-max) next-object
3755 (org-element-restriction next-object
))))
3756 (setq acc
(org-element-adopt-element acc next-object t
))
3757 (goto-char obj-end
))))
3758 ;; 3. Text after last object. Untabify it.
3759 (unless (= (point) end
)
3761 (org-element-adopt-element
3763 (replace-regexp-in-string
3764 "\t" (make-string tab-width ?
)
3765 (buffer-substring-no-properties (point) end
)) t
)))
3769 (defun org-element--get-next-object-candidates (limit restriction objects
)
3770 "Return an alist of candidates for the next object.
3772 LIMIT bounds the search, and RESTRICTION narrows candidates to
3775 Return value is an alist whose CAR is position and CDR the object
3778 OBJECTS is the previous candidates alist."
3779 (let (next-candidates types-to-search
)
3780 ;; If no previous result, search every object type in RESTRICTION.
3781 ;; Otherwise, keep potential candidates (old objects located after
3782 ;; point) and ask to search again those which had matched before.
3783 (if (not objects
) (setq types-to-search restriction
)
3785 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3786 (push obj next-candidates
)))
3788 ;; Call the appropriate successor function for each type to search
3789 ;; and accumulate matches.
3792 (let* ((successor-fun
3794 (format "org-element-%s-successor"
3795 (or (cdr (assq type org-element-object-successor-alist
))
3797 (obj (funcall successor-fun limit
)))
3798 (and obj
(push obj next-candidates
))))
3805 ;;; Towards A Bijective Process
3807 ;; The parse tree obtained with `org-element-parse-buffer' is really
3808 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3809 ;; interpreted and expanded into a string with canonical Org syntax.
3810 ;; Hence `org-element-interpret-data'.
3812 ;; The function relies internally on
3813 ;; `org-element--interpret-affiliated-keywords'.
3815 (defun org-element-interpret-data (data &optional parent
)
3816 "Interpret DATA as Org syntax.
3818 DATA is a parse tree, an element, an object or a secondary string
3821 Optional argument PARENT is used for recursive calls. It contains
3822 the element or object containing data, or nil.
3824 Return Org syntax as a string."
3825 (let* ((type (org-element-type data
))
3828 ;; Secondary string.
3831 (lambda (obj) (org-element-interpret-data obj parent
))
3833 ;; Full Org document.
3834 ((eq type
'org-data
)
3836 (lambda (obj) (org-element-interpret-data obj parent
))
3837 (org-element-contents data
) ""))
3839 ((stringp data
) data
)
3840 ;; Element/Object without contents.
3841 ((not (org-element-contents data
))
3842 (funcall (intern (format "org-element-%s-interpreter" type
))
3844 ;; Element/Object with contents.
3846 (let* ((greaterp (memq type org-element-greater-elements
))
3847 (objectp (and (not greaterp
)
3848 (memq type org-element-recursive-objects
)))
3851 (lambda (obj) (org-element-interpret-data obj data
))
3852 (org-element-contents
3853 (if (or greaterp objectp
) data
3854 ;; Elements directly containing objects must
3855 ;; have their indentation normalized first.
3856 (org-element-normalize-contents
3858 ;; When normalizing first paragraph of an
3859 ;; item or a footnote-definition, ignore
3860 ;; first line's indentation.
3861 (and (eq type
'paragraph
)
3862 (equal data
(car (org-element-contents parent
)))
3863 (memq (org-element-type parent
)
3864 '(footnote-definiton item
))))))
3866 (funcall (intern (format "org-element-%s-interpreter" type
))
3868 (if greaterp
(org-element-normalize-contents contents
)
3870 (if (memq type
'(org-data plain-text nil
)) results
3871 ;; Build white spaces. If no `:post-blank' property is
3872 ;; specified, assume its value is 0.
3873 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
3874 (if (memq type org-element-all-objects
)
3875 (concat results
(make-string post-blank
32))
3877 (org-element--interpret-affiliated-keywords data
)
3878 (org-element-normalize-string results
)
3879 (make-string post-blank
10)))))))
3881 (defun org-element--interpret-affiliated-keywords (element)
3882 "Return ELEMENT's affiliated keywords as Org syntax.
3883 If there is no affiliated keyword, return the empty string."
3884 (let ((keyword-to-org
3888 (when (member key org-element-dual-keywords
)
3889 (setq dual
(cdr value
) value
(car value
)))
3892 (format "[%s]" (org-element-interpret-data dual
)))
3894 (if (member key org-element-parsed-keywords
)
3895 (org-element-interpret-data value
)
3900 (let ((value (org-element-property prop element
))
3901 (keyword (upcase (substring (symbol-name prop
) 1))))
3903 (if (or (member keyword org-element-multiple-keywords
)
3904 ;; All attribute keywords can have multiple lines.
3905 (string-match "^ATTR_" keyword
))
3906 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
3909 (funcall keyword-to-org keyword value
)))))
3910 ;; List all ELEMENT's properties matching an attribute line or an
3911 ;; affiliated keyword, but ignore translated keywords since they
3912 ;; cannot belong to the property list.
3913 (loop for prop in
(nth 1 element
) by
'cddr
3914 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
3915 (or (string-match "^ATTR_" keyword
)
3917 (member keyword org-element-affiliated-keywords
)
3919 org-element-keyword-translation-alist
)))))
3923 ;; Because interpretation of the parse tree must return the same
3924 ;; number of blank lines between elements and the same number of white
3925 ;; space after objects, some special care must be given to white
3928 ;; The first function, `org-element-normalize-string', ensures any
3929 ;; string different from the empty string will end with a single
3930 ;; newline character.
3932 ;; The second function, `org-element-normalize-contents', removes
3933 ;; global indentation from the contents of the current element.
3935 (defun org-element-normalize-string (s)
3936 "Ensure string S ends with a single newline character.
3938 If S isn't a string return it unchanged. If S is the empty
3939 string, return it. Otherwise, return a new string with a single
3940 newline character at its end."
3942 ((not (stringp s
)) s
)
3944 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3945 (replace-match "\n" nil nil s
)))))
3947 (defun org-element-normalize-contents (element &optional ignore-first
)
3948 "Normalize plain text in ELEMENT's contents.
3950 ELEMENT must only contain plain text and objects.
3952 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3953 indentation to compute maximal common indentation.
3955 Return the normalized element that is element with global
3956 indentation removed from its contents. The function assumes that
3957 indentation is not done with TAB characters."
3958 (let* (ind-list ; for byte-compiler
3959 collect-inds
; for byte-compiler
3962 ;; Return list of indentations within BLOB. This is done by
3963 ;; walking recursively BLOB and updating IND-LIST along the
3964 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3965 ;; been seen yet. It is required as this string is the only
3966 ;; one whose indentation doesn't happen after a newline
3968 (lambda (blob first-flag
)
3971 (when (and first-flag
(stringp object
))
3972 (setq first-flag nil
)
3973 (string-match "\\`\\( *\\)" object
)
3974 (let ((len (length (match-string 1 object
))))
3975 ;; An indentation of zero means no string will be
3976 ;; modified. Quit the process.
3977 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
3978 (push len ind-list
))))
3982 ;; Avoid matching blank or empty lines.
3983 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
3984 (not (equal (match-string 2 object
) " ")))
3985 (setq start
(match-end 0))
3986 (push (length (match-string 1 object
)) ind-list
))))
3987 ((memq (org-element-type object
) org-element-recursive-objects
)
3988 (funcall collect-inds object first-flag
))))
3989 (org-element-contents blob
))))))
3990 ;; Collect indentation list in ELEMENT. Possibly remove first
3991 ;; value if IGNORE-FIRST is non-nil.
3992 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
3993 (if (not ind-list
) element
3994 ;; Build ELEMENT back, replacing each string with the same
3995 ;; string minus common indentation.
3996 (let* (build ; For byte compiler.
3999 (lambda (blob mci first-flag
)
4000 ;; Return BLOB with all its strings indentation
4001 ;; shortened from MCI white spaces. FIRST-FLAG is
4002 ;; non-nil when the first string hasn't been seen
4007 (when (and first-flag
(stringp object
))
4008 (setq first-flag nil
)
4010 (replace-regexp-in-string
4011 (format "\\` \\{%d\\}" mci
) "" object
)))
4014 (replace-regexp-in-string
4015 (format "\n \\{%d\\}" mci
) "\n" object
))
4016 ((memq (org-element-type object
)
4017 org-element-recursive-objects
)
4018 (funcall build object mci first-flag
))
4020 (org-element-contents blob
)))
4022 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
4028 ;; The first move is to implement a way to obtain the smallest element
4029 ;; containing point. This is the job of `org-element-at-point'. It
4030 ;; basically jumps back to the beginning of section containing point
4031 ;; and moves, element after element, with
4032 ;; `org-element--current-element' until the container is found.
4034 ;; At a deeper level, `org-element-context' lists all elements and
4035 ;; objects containing point.
4037 ;; Note: When using `org-element-at-point', secondary values are never
4038 ;; parsed since the function focuses on elements, not on objects.
4040 (defun org-element-at-point (&optional keep-trail
)
4041 "Determine closest element around point.
4043 Return value is a list like (TYPE PROPS) where TYPE is the type
4044 of the element and PROPS a plist of properties associated to the
4047 Possible types are defined in `org-element-all-elements'.
4048 Properties depend on element or object type, but always
4049 include :begin, :end, :parent and :post-blank properties.
4051 As a special case, if point is at the very beginning of a list or
4052 sub-list, returned element will be that list instead of the first
4053 item. In the same way, if point is at the beginning of the first
4054 row of a table, returned element will be the table instead of the
4057 If optional argument KEEP-TRAIL is non-nil, the function returns
4058 a list of of elements leading to element at point. The list's
4059 CAR is always the element at point. Following positions contain
4060 element's siblings, then parents, siblings of parents, until the
4061 first element of current section."
4062 (org-with-wide-buffer
4063 ;; If at an headline, parse it. It is the sole element that
4064 ;; doesn't require to know about context. Be sure to disallow
4065 ;; secondary string parsing, though.
4066 (if (org-with-limited-levels (org-at-heading-p))
4069 (if (not keep-trail
) (org-element-headline-parser (point-max) t
)
4070 (list (org-element-headline-parser (point-max) t
))))
4071 ;; Otherwise move at the beginning of the section containing
4073 (let ((origin (point))
4074 (end (save-excursion
4075 (org-with-limited-levels (outline-next-heading)) (point)))
4076 element type special-flag trail struct prevs parent
)
4077 (org-with-limited-levels
4078 (if (org-with-limited-levels (org-before-first-heading-p))
4079 (goto-char (point-min))
4080 (org-back-to-heading)
4082 (org-skip-whitespace)
4084 ;; Parse successively each element, skipping those ending
4085 ;; before original position.
4089 (org-element--current-element end
'element special-flag struct
)
4091 (org-element-put-property element
:parent parent
)
4092 (when keep-trail
(push element trail
))
4094 ;; 1. Skip any element ending before point or at point
4095 ;; because the following element has started. On the
4096 ;; other hand, if the element ends at point and that
4097 ;; point is also the end of the buffer, do not skip it.
4098 ((let ((end (org-element-property :end element
)))
4099 (when (or (< end origin
)
4100 (and (= end origin
) (/= (point-max) end
)))
4101 (if (> (point-max) end
) (goto-char end
)
4102 (throw 'exit
(if keep-trail trail element
))))))
4103 ;; 2. An element containing point is always the element at
4105 ((not (memq type org-element-greater-elements
))
4106 (throw 'exit
(if keep-trail trail element
)))
4107 ;; 3. At any other greater element type, if point is
4108 ;; within contents, move into it. Otherwise, return
4109 ;; that element. As a special case, when ORIGIN is
4110 ;; contents end and is also the end of the buffer, try
4111 ;; to move inside the greater element to find the end
4112 ;; of the innermost element.
4114 (let ((cbeg (org-element-property :contents-begin element
))
4115 (cend (org-element-property :contents-end element
)))
4116 (if (or (not cbeg
) (not cend
) (> cbeg origin
) (< cend origin
)
4117 (and (= cend origin
) (/= (point-max) origin
))
4118 (and (= cbeg origin
) (memq type
'(plain-list table
))))
4119 (throw 'exit
(if keep-trail trail element
))
4120 (setq parent element
)
4123 (setq special-flag
'item
4124 struct
(org-element-property :structure element
)))
4125 (table (setq special-flag
'table-row
))
4126 (otherwise (setq special-flag nil
)))
4128 (goto-char cbeg
)))))))))))
4130 (defun org-element-context ()
4131 "Return closest element or object around point.
4133 Return value is a list like (TYPE PROPS) where TYPE is the type
4134 of the element or object and PROPS a plist of properties
4137 Possible types are defined in `org-element-all-elements' and
4138 `org-element-all-objects'. Properties depend on element or
4139 object type, but always include :begin, :end, :parent
4140 and :post-blank properties."
4141 (org-with-wide-buffer
4142 (let* ((origin (point))
4143 (element (org-element-at-point))
4144 (type (car element
))
4146 ;; Check if point is inside an element containing objects or at
4147 ;; a secondary string. In that case, move to beginning of the
4148 ;; element or secondary string and set END to the other side.
4149 (if (not (or (and (eq type
'item
)
4150 (let ((tag (org-element-property :tag element
)))
4154 (search-forward tag
(point-at-eol))
4155 (goto-char (match-beginning 0))
4156 (and (>= origin
(point))
4158 ;; `1+' is required so some
4159 ;; successors can match
4160 ;; properly their object.
4161 (setq end
(1+ (match-end 0)))))))))
4162 (and (memq type
'(headline inlinetask
))
4163 (progn (beginning-of-line)
4164 (skip-chars-forward "* ")
4165 (setq end
(point-at-eol))))
4166 (and (memq type
'(paragraph table-cell verse-block
))
4167 (let ((cbeg (org-element-property
4168 :contents-begin element
))
4169 (cend (org-element-property
4170 :contents-end element
)))
4171 (and (>= origin cbeg
)
4173 (progn (goto-char cbeg
) (setq end cend
)))))))
4175 (let ((restriction (org-element-restriction element
))
4179 (while (setq candidates
(org-element--get-next-object-candidates
4180 end restriction candidates
))
4181 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4183 ;; If ORIGIN is before next object in element, there's
4184 ;; no point in looking further.
4185 (if (> (cdr closest-cand
) origin
) (throw 'exit element
)
4187 (progn (goto-char (cdr closest-cand
))
4188 (funcall (intern (format "org-element-%s-parser"
4189 (car closest-cand
))))))
4190 (cbeg (org-element-property :contents-begin object
))
4191 (cend (org-element-property :contents-end object
)))
4193 ;; ORIGIN is after OBJECT, so skip it.
4194 ((< (org-element-property :end object
) origin
)
4195 (goto-char (org-element-property :end object
)))
4196 ;; ORIGIN is within a non-recursive object or at an
4197 ;; object boundaries: Return that object.
4198 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4200 (org-element-put-property object
:parent parent
)))
4201 ;; Otherwise, move within current object and restrict
4202 ;; search to the end of its contents.
4204 (org-element-put-property object
:parent parent
)
4205 (setq parent object end cend
)))))))
4209 ;; Once the local structure around point is well understood, it's easy
4210 ;; to implement some replacements for `forward-paragraph'
4211 ;; `backward-paragraph', namely `org-element-forward' and
4212 ;; `org-element-backward'.
4214 ;; Also, `org-transpose-elements' mimics the behaviour of
4215 ;; `transpose-words', at the element's level, whereas
4216 ;; `org-element-drag-forward', `org-element-drag-backward', and
4217 ;; `org-element-up' generalize, respectively, functions
4218 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
4220 ;; `org-element-unindent-buffer' will, as its name almost suggests,
4221 ;; smartly remove global indentation from buffer, making it possible
4222 ;; to use Org indent mode on a file created with hard indentation.
4224 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
4225 ;; internally by some of the previously cited tools.
4227 (defsubst org-element-nested-p
(elem-A elem-B
)
4228 "Non-nil when elements ELEM-A and ELEM-B are nested."
4229 (let ((beg-A (org-element-property :begin elem-A
))
4230 (beg-B (org-element-property :begin elem-B
))
4231 (end-A (org-element-property :end elem-A
))
4232 (end-B (org-element-property :end elem-B
)))
4233 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4234 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4236 (defun org-element-swap-A-B (elem-A elem-B
)
4237 "Swap elements ELEM-A and ELEM-B.
4238 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4240 (goto-char (org-element-property :begin elem-A
))
4241 ;; There are two special cases when an element doesn't start at bol:
4242 ;; the first paragraph in an item or in a footnote definition.
4243 (let ((specialp (not (bolp))))
4244 ;; Only a paragraph without any affiliated keyword can be moved at
4245 ;; ELEM-A position in such a situation. Note that the case of
4246 ;; a footnote definition is impossible: it cannot contain two
4247 ;; paragraphs in a row because it cannot contain a blank line.
4249 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4250 (/= (org-element-property :begin elem-B
)
4251 (org-element-property :contents-begin elem-B
))))
4252 (error "Cannot swap elements"))
4253 ;; In a special situation, ELEM-A will have no indentation. We'll
4254 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4255 (let* ((ind-B (when specialp
4256 (goto-char (org-element-property :begin elem-B
))
4257 (org-get-indentation)))
4258 (beg-A (org-element-property :begin elem-A
))
4259 (end-A (save-excursion
4260 (goto-char (org-element-property :end elem-A
))
4261 (skip-chars-backward " \r\t\n")
4263 (beg-B (org-element-property :begin elem-B
))
4264 (end-B (save-excursion
4265 (goto-char (org-element-property :end elem-B
))
4266 (skip-chars-backward " \r\t\n")
4268 ;; Store overlays responsible for visibility status. We
4269 ;; also need to store their boundaries as they will be
4270 ;; removed from buffer.
4273 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4274 (overlays-in beg-A end-A
))
4275 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4276 (overlays-in beg-B end-B
))))
4278 (body-A (buffer-substring beg-A end-A
))
4279 (body-B (delete-and-extract-region beg-B end-B
)))
4282 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4283 (org-indent-to-column ind-B
))
4285 ;; Restore ex ELEM-A overlays.
4289 (+ (nth 1 ov
) (- beg-B beg-A
))
4290 (+ (nth 2 ov
) (- beg-B beg-A
))))
4293 (delete-region beg-A end-A
)
4295 ;; Restore ex ELEM-B overlays.
4297 (move-overlay (car ov
)
4298 (+ (nth 1 ov
) (- beg-A beg-B
))
4299 (+ (nth 2 ov
) (- beg-A beg-B
))))
4301 (goto-char (org-element-property :end elem-B
)))))
4303 (defun org-element-forward ()
4304 "Move forward by one element.
4305 Move to the next element at the same level, when possible."
4307 (cond ((eobp) (error "Cannot move further down"))
4308 ((org-with-limited-levels (org-at-heading-p))
4309 (let ((origin (point)))
4310 (org-forward-same-level 1)
4311 (unless (org-with-limited-levels (org-at-heading-p))
4313 (error "Cannot move further down"))))
4315 (let* ((elem (org-element-at-point))
4316 (end (org-element-property :end elem
))
4317 (parent (org-element-property :parent elem
)))
4318 (if (and parent
(= (org-element-property :contents-end parent
) end
))
4319 (goto-char (org-element-property :end parent
))
4320 (goto-char end
))))))
4322 (defun org-element-backward ()
4323 "Move backward by one element.
4324 Move to the previous element at the same level, when possible."
4326 (if (org-with-limited-levels (org-at-heading-p))
4327 ;; At an headline, move to the previous one, if any, or stay
4329 (let ((origin (point)))
4330 (org-backward-same-level 1)
4331 (unless (org-with-limited-levels (org-at-heading-p))
4333 (error "Cannot move further up")))
4334 (let* ((trail (org-element-at-point 'keep-trail
))
4336 (prev-elem (nth 1 trail
))
4337 (beg (org-element-property :begin elem
)))
4339 ;; Move to beginning of current element if point isn't there
4341 ((/= (point) beg
) (goto-char beg
))
4342 ((not prev-elem
) (error "Cannot move further up"))
4343 (t (goto-char (org-element-property :begin prev-elem
)))))))
4345 (defun org-element-up ()
4346 "Move to upper element."
4348 (if (org-with-limited-levels (org-at-heading-p))
4349 (unless (org-up-heading-safe) (error "No surrounding element"))
4350 (let* ((elem (org-element-at-point))
4351 (parent (org-element-property :parent elem
)))
4352 (if parent
(goto-char (org-element-property :begin parent
))
4353 (if (org-with-limited-levels (org-before-first-heading-p))
4354 (error "No surrounding element")
4355 (org-with-limited-levels (org-back-to-heading)))))))
4357 (defun org-element-down ()
4358 "Move to inner element."
4360 (let ((element (org-element-at-point)))
4362 ((memq (org-element-type element
) '(plain-list table
))
4363 (goto-char (org-element-property :contents-begin element
))
4365 ((memq (org-element-type element
) org-element-greater-elements
)
4366 ;; If contents are hidden, first disclose them.
4367 (when (org-element-property :hiddenp element
) (org-cycle))
4368 (goto-char (org-element-property :contents-begin element
)))
4369 (t (error "No inner element")))))
4371 (defun org-element-drag-backward ()
4372 "Move backward element at point."
4374 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
4375 (let* ((trail (org-element-at-point 'keep-trail
))
4377 (prev-elem (nth 1 trail
)))
4378 ;; Error out if no previous element or previous element is
4379 ;; a parent of the current one.
4380 (if (or (not prev-elem
) (org-element-nested-p elem prev-elem
))
4381 (error "Cannot drag element backward")
4382 (let ((pos (point)))
4383 (org-element-swap-A-B prev-elem elem
)
4384 (goto-char (+ (org-element-property :begin prev-elem
)
4385 (- pos
(org-element-property :begin elem
)))))))))
4387 (defun org-element-drag-forward ()
4388 "Move forward element at point."
4390 (let* ((pos (point))
4391 (elem (org-element-at-point)))
4392 (when (= (point-max) (org-element-property :end elem
))
4393 (error "Cannot drag element forward"))
4394 (goto-char (org-element-property :end elem
))
4395 (let ((next-elem (org-element-at-point)))
4396 (when (or (org-element-nested-p elem next-elem
)
4397 (and (eq (org-element-type next-elem
) 'headline
)
4398 (not (eq (org-element-type elem
) 'headline
))))
4400 (error "Cannot drag element forward"))
4401 ;; Compute new position of point: it's shifted by NEXT-ELEM
4402 ;; body's length (without final blanks) and by the length of
4403 ;; blanks between ELEM and NEXT-ELEM.
4404 (let ((size-next (- (save-excursion
4405 (goto-char (org-element-property :end next-elem
))
4406 (skip-chars-backward " \r\t\n")
4408 ;; Small correction if buffer doesn't end
4409 ;; with a newline character.
4410 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
4411 (org-element-property :begin next-elem
)))
4412 (size-blank (- (org-element-property :end elem
)
4414 (goto-char (org-element-property :end elem
))
4415 (skip-chars-backward " \r\t\n")
4418 (org-element-swap-A-B elem next-elem
)
4419 (goto-char (+ pos size-next size-blank
))))))
4421 (defun org-element-mark-element ()
4422 "Put point at beginning of this element, mark at end.
4424 Interactively, if this command is repeated or (in Transient Mark
4425 mode) if the mark is active, it marks the next element after the
4426 ones already marked."
4428 (let (deactivate-mark)
4429 (if (or (and (eq last-command this-command
) (mark t
))
4430 (and transient-mark-mode mark-active
))
4434 (goto-char (org-element-property :end
(org-element-at-point)))))
4435 (let ((element (org-element-at-point)))
4437 (push-mark (org-element-property :end element
) t t
)
4438 (goto-char (org-element-property :begin element
))))))
4440 (defun org-narrow-to-element ()
4441 "Narrow buffer to current element."
4443 (let ((elem (org-element-at-point)))
4445 ((eq (car elem
) 'headline
)
4447 (org-element-property :begin elem
)
4448 (org-element-property :end elem
)))
4449 ((memq (car elem
) org-element-greater-elements
)
4451 (org-element-property :contents-begin elem
)
4452 (org-element-property :contents-end elem
)))
4455 (org-element-property :begin elem
)
4456 (org-element-property :end elem
))))))
4458 (defun org-element-transpose ()
4459 "Transpose current and previous elements, keeping blank lines between.
4460 Point is moved after both elements."
4462 (org-skip-whitespace)
4463 (let ((end (org-element-property :end
(org-element-at-point))))
4464 (org-element-drag-backward)
4467 (defun org-element-unindent-buffer ()
4468 "Un-indent the visible part of the buffer.
4469 Relative indentation (between items, inside blocks, etc.) isn't
4472 (unless (eq major-mode
'org-mode
)
4473 (error "Cannot un-indent a buffer not in Org mode"))
4474 (let* ((parse-tree (org-element-parse-buffer 'greater-element
))
4475 unindent-tree
; For byte-compiler.
4481 (if (memq (org-element-type element
) '(headline section
))
4482 (funcall unindent-tree
(org-element-contents element
))
4486 (org-element-property :begin element
)
4487 (org-element-property :end element
))
4488 (org-do-remove-indentation)))))
4489 (reverse contents
))))))
4490 (funcall unindent-tree
(org-element-contents parse-tree
))))
4493 (provide 'org-element
)
4494 ;;; org-element.el ends here