org-export: Fix smart quotes with isolated quotes
[org-mode.git] / lisp / org-element.el
blob1e185530bd3f7146f6f0bf3e981740124d635027
1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (`clock', `headline', `inlinetask', `item',
34 ;; `planning', `node-property', `quote-section' `section' and
35 ;; `table-row' types), it can also accept a fixed set of keywords as
36 ;; attributes. Those are called "affiliated keywords" to distinguish
37 ;; them from other keywords, which are full-fledged elements. Almost
38 ;; all affiliated keywords are referenced in
39 ;; `org-element-affiliated-keywords'; the others are export attributes
40 ;; and start with "ATTR_" prefix.
42 ;; Element containing other elements (and only elements) are called
43 ;; greater elements. Concerned types are: `center-block', `drawer',
44 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
45 ;; `item', `plain-list', `property-drawer', `quote-block', `section'
46 ;; and `special-block'.
48 ;; Other element types are: `babel-call', `clock', `comment',
49 ;; `comment-block', `diary-sexp', `example-block', `export-block',
50 ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
51 ;; `node-property', `paragraph', `planning', `quote-section',
52 ;; `src-block', `table', `table-row' and `verse-block'. Among them,
53 ;; `paragraph' and `verse-block' types can contain Org objects and
54 ;; plain text.
56 ;; Objects are related to document's contents. Some of them are
57 ;; recursive. Associated types are of the following: `bold', `code',
58 ;; `entity', `export-snippet', `footnote-reference',
59 ;; `inline-babel-call', `inline-src-block', `italic',
60 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
61 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
62 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
64 ;; Some elements also have special properties whose value can hold
65 ;; objects themselves (i.e. an item tag or an headline name). Such
66 ;; values are called "secondary strings". Any object belongs to
67 ;; either an element or a secondary string.
69 ;; Notwithstanding affiliated keywords, each greater element, element
70 ;; and object has a fixed set of properties attached to it. Among
71 ;; them, four are shared by all types: `:begin' and `:end', which
72 ;; refer to the beginning and ending buffer positions of the
73 ;; considered element or object, `:post-blank', which holds the number
74 ;; of blank lines, or white spaces, at its end and `:parent' which
75 ;; refers to the element or object containing it. Greater elements
76 ;; and elements containing objects will also have `:contents-begin'
77 ;; and `:contents-end' properties to delimit contents and
78 ;; a `:post-affiliated', referring to the buffer position after any
79 ;; affiliated keyword, if applicable.
81 ;; At the lowest level, a `:parent' property is also attached to any
82 ;; string, as a text property.
84 ;; Lisp-wise, an element or an object can be represented as a list.
85 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
86 ;; TYPE is a symbol describing the Org element or object.
87 ;; PROPERTIES is the property list attached to it. See docstring of
88 ;; appropriate parsing function to get an exhaustive
89 ;; list.
90 ;; CONTENTS is a list of elements, objects or raw strings contained
91 ;; in the current element or object, when applicable.
93 ;; An Org buffer is a nested list of such elements and objects, whose
94 ;; type is `org-data' and properties is nil.
96 ;; The first part of this file defines Org syntax, while the second
97 ;; one provide accessors and setters functions.
99 ;; The next part implements a parser and an interpreter for each
100 ;; element and object type in Org syntax.
102 ;; The following part creates a fully recursive buffer parser. It
103 ;; also provides a tool to map a function to elements or objects
104 ;; matching some criteria in the parse tree. Functions of interest
105 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
106 ;; extent, `org-element-parse-secondary-string'.
108 ;; The penultimate part is the cradle of an interpreter for the
109 ;; obtained parse tree: `org-element-interpret-data'.
111 ;; The library ends by furnishing `org-element-at-point' function, and
112 ;; a way to give information about document structure around point
113 ;; with `org-element-context'.
116 ;;; Code:
118 (eval-when-compile (require 'cl))
119 (require 'org)
121 (declare-function org-clocking-buffer "org-clock" ())
125 ;;; Definitions And Rules
127 ;; Define elements, greater elements and specify recursive objects,
128 ;; along with the affiliated keywords recognized. Also set up
129 ;; restrictions on recursive objects combinations.
131 ;; These variables really act as a control center for the parsing
132 ;; process.
134 (defconst org-element-paragraph-separate
135 (concat "^\\(?:"
136 ;; Headlines, inlinetasks.
137 org-outline-regexp "\\|"
138 ;; Footnote definitions.
139 "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
140 ;; Diary sexps.
141 "%%(" "\\|"
142 "[ \t]*\\(?:"
143 ;; Empty lines.
144 "$" "\\|"
145 ;; Tables (any type).
146 "\\(?:|\\|\\+-[-+]\\)" "\\|"
147 ;; Blocks (any type), Babel calls, drawers (any type),
148 ;; fixed-width areas and keywords. Note: this is only an
149 ;; indication and need some thorough check.
150 "[#:]" "\\|"
151 ;; Horizontal rules.
152 "-\\{5,\\}[ \t]*$" "\\|"
153 ;; LaTeX environments.
154 "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
155 ;; Planning and Clock lines.
156 (regexp-opt (list org-scheduled-string
157 org-deadline-string
158 org-closed-string
159 org-clock-string))
160 "\\|"
161 ;; Lists.
162 (let ((term (case org-plain-list-ordered-item-terminator
163 (?\) ")") (?. "\\.") (otherwise "[.)]")))
164 (alpha (and org-alphabetical-lists "\\|[A-Za-z]")))
165 (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
166 "\\(?:[ \t]\\|$\\)"))
167 "\\)\\)")
168 "Regexp to separate paragraphs in an Org buffer.
169 In the case of lines starting with \"#\" and \":\", this regexp
170 is not sufficient to know if point is at a paragraph ending. See
171 `org-element-paragraph-parser' for more information.")
173 (defconst org-element-all-elements
174 '(babel-call center-block clock comment comment-block diary-sexp drawer
175 dynamic-block example-block export-block fixed-width
176 footnote-definition headline horizontal-rule inlinetask item
177 keyword latex-environment node-property paragraph plain-list
178 planning property-drawer quote-block quote-section section
179 special-block src-block table table-row verse-block)
180 "Complete list of element types.")
182 (defconst org-element-greater-elements
183 '(center-block drawer dynamic-block footnote-definition headline inlinetask
184 item plain-list property-drawer quote-block section
185 special-block table)
186 "List of recursive element types aka Greater Elements.")
188 (defconst org-element-all-successors
189 '(export-snippet footnote-reference inline-babel-call inline-src-block
190 latex-or-entity line-break link macro radio-target
191 statistics-cookie sub/superscript table-cell target
192 text-markup timestamp)
193 "Complete list of successors.")
195 (defconst org-element-object-successor-alist
196 '((subscript . sub/superscript) (superscript . sub/superscript)
197 (bold . text-markup) (code . text-markup) (italic . text-markup)
198 (strike-through . text-markup) (underline . text-markup)
199 (verbatim . text-markup) (entity . latex-or-entity)
200 (latex-fragment . latex-or-entity))
201 "Alist of translations between object type and successor name.
203 Sharing the same successor comes handy when, for example, the
204 regexp matching one object can also match the other object.")
206 (defconst org-element-all-objects
207 '(bold code entity export-snippet footnote-reference inline-babel-call
208 inline-src-block italic line-break latex-fragment link macro
209 radio-target statistics-cookie strike-through subscript superscript
210 table-cell target timestamp underline verbatim)
211 "Complete list of object types.")
213 (defconst org-element-recursive-objects
214 '(bold italic link subscript radio-target strike-through superscript
215 table-cell underline)
216 "List of recursive object types.")
218 (defconst org-element-block-name-alist
219 '(("CENTER" . org-element-center-block-parser)
220 ("COMMENT" . org-element-comment-block-parser)
221 ("EXAMPLE" . org-element-example-block-parser)
222 ("QUOTE" . org-element-quote-block-parser)
223 ("SRC" . org-element-src-block-parser)
224 ("VERSE" . org-element-verse-block-parser))
225 "Alist between block names and the associated parsing function.
226 Names must be uppercase. Any block whose name has no association
227 is parsed with `org-element-special-block-parser'.")
229 (defconst org-element-link-type-is-file
230 '("file" "file+emacs" "file+sys" "docview")
231 "List of link types equivalent to \"file\".
232 Only these types can accept search options and an explicit
233 application to open them.")
235 (defconst org-element-affiliated-keywords
236 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
237 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
238 "List of affiliated keywords as strings.
239 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
240 are affiliated keywords and need not to be in this list.")
242 (defconst org-element--affiliated-re
243 (format "[ \t]*#\\+%s:"
244 ;; Regular affiliated keywords.
245 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
246 (regexp-opt org-element-affiliated-keywords)))
247 "Regexp matching any affiliated keyword.
249 Keyword name is put in match group 1. Moreover, if keyword
250 belongs to `org-element-dual-keywords', put the dual value in
251 match group 2.
253 Don't modify it, set `org-element-affiliated-keywords' instead.")
255 (defconst org-element-keyword-translation-alist
256 '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
257 ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
258 ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
259 "Alist of usual translations for keywords.
260 The key is the old name and the value the new one. The property
261 holding their value will be named after the translated name.")
263 (defconst org-element-multiple-keywords '("CAPTION" "HEADER")
264 "List of affiliated keywords that can occur more than once in an element.
266 Their value will be consed into a list of strings, which will be
267 returned as the value of the property.
269 This list is checked after translations have been applied. See
270 `org-element-keyword-translation-alist'.
272 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
273 allow multiple occurrences and need not to be in this list.")
275 (defconst org-element-parsed-keywords '("CAPTION")
276 "List of affiliated keywords whose value can be parsed.
278 Their value will be stored as a secondary string: a list of
279 strings and objects.
281 This list is checked after translations have been applied. See
282 `org-element-keyword-translation-alist'.")
284 (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
285 "List of affiliated keywords which can have a secondary value.
287 In Org syntax, they can be written with optional square brackets
288 before the colons. For example, RESULTS keyword can be
289 associated to a hash value with the following:
291 #+RESULTS[hash-string]: some-source
293 This list is checked after translations have been applied. See
294 `org-element-keyword-translation-alist'.")
296 (defconst org-element-document-properties '("AUTHOR" "DATE" "TITLE")
297 "List of properties associated to the whole document.
298 Any keyword in this list will have its value parsed and stored as
299 a secondary string.")
301 (defconst org-element-object-restrictions
302 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
303 radio-target sub/superscript target text-markup timestamp)
304 (footnote-reference export-snippet footnote-reference inline-babel-call
305 inline-src-block latex-or-entity line-break link macro
306 radio-target sub/superscript target text-markup
307 timestamp)
308 (headline inline-babel-call inline-src-block latex-or-entity link macro
309 radio-target statistics-cookie sub/superscript target text-markup
310 timestamp)
311 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
312 radio-target sub/superscript target text-markup timestamp)
313 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
314 link radio-target sub/superscript target text-markup timestamp)
315 (item export-snippet footnote-reference inline-babel-call latex-or-entity
316 link macro radio-target sub/superscript target text-markup)
317 (keyword inline-babel-call inline-src-block latex-or-entity link macro
318 sub/superscript text-markup)
319 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
320 sub/superscript text-markup)
321 (paragraph export-snippet footnote-reference inline-babel-call
322 inline-src-block latex-or-entity line-break link macro
323 radio-target statistics-cookie sub/superscript target text-markup
324 timestamp)
325 (radio-target export-snippet latex-or-entity sub/superscript)
326 (strike-through export-snippet inline-babel-call inline-src-block
327 latex-or-entity link radio-target sub/superscript target
328 text-markup timestamp)
329 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
330 sub/superscript target text-markup)
331 (superscript export-snippet inline-babel-call inline-src-block
332 latex-or-entity sub/superscript target text-markup)
333 (table-cell export-snippet latex-or-entity link macro radio-target
334 sub/superscript target text-markup timestamp)
335 (table-row table-cell)
336 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
337 link radio-target sub/superscript target text-markup timestamp)
338 (verse-block footnote-reference inline-babel-call inline-src-block
339 latex-or-entity line-break link macro radio-target
340 sub/superscript target text-markup timestamp))
341 "Alist of objects restrictions.
343 CAR is an element or object type containing objects and CDR is
344 a list of successors that will be called within an element or
345 object of such type.
347 For example, in a `radio-target' object, one can only find
348 entities, export snippets, latex-fragments, subscript and
349 superscript.
351 This alist also applies to secondary string. For example, an
352 `headline' type element doesn't directly contain objects, but
353 still has an entry since one of its properties (`:title') does.")
355 (defconst org-element-secondary-value-alist
356 '((headline . :title)
357 (inlinetask . :title)
358 (item . :tag)
359 (footnote-reference . :inline-definition))
360 "Alist between element types and location of secondary value.")
364 ;;; Accessors and Setters
366 ;; Provide four accessors: `org-element-type', `org-element-property'
367 ;; `org-element-contents' and `org-element-restriction'.
369 ;; Setter functions allow to modify elements by side effect. There is
370 ;; `org-element-put-property', `org-element-set-contents',
371 ;; `org-element-set-element' and `org-element-adopt-element'. Note
372 ;; that `org-element-set-element' and `org-element-adopt-elements' are
373 ;; higher level functions since also update `:parent' property.
375 (defsubst org-element-type (element)
376 "Return type of ELEMENT.
378 The function returns the type of the element or object provided.
379 It can also return the following special value:
380 `plain-text' for a string
381 `org-data' for a complete document
382 nil in any other case."
383 (cond
384 ((not (consp element)) (and (stringp element) 'plain-text))
385 ((symbolp (car element)) (car element))))
387 (defsubst org-element-property (property element)
388 "Extract the value from the PROPERTY of an ELEMENT."
389 (if (stringp element) (get-text-property 0 property element)
390 (plist-get (nth 1 element) property)))
392 (defsubst org-element-contents (element)
393 "Extract contents from an ELEMENT."
394 (cond ((not (consp element)) nil)
395 ((symbolp (car element)) (nthcdr 2 element))
396 (t element)))
398 (defsubst org-element-restriction (element)
399 "Return restriction associated to ELEMENT.
400 ELEMENT can be an element, an object or a symbol representing an
401 element or object type."
402 (cdr (assq (if (symbolp element) element (org-element-type element))
403 org-element-object-restrictions)))
405 (defsubst org-element-put-property (element property value)
406 "In ELEMENT set PROPERTY to VALUE.
407 Return modified element."
408 (if (stringp element) (org-add-props element nil property value)
409 (setcar (cdr element) (plist-put (nth 1 element) property value))
410 element))
412 (defsubst org-element-set-contents (element &rest contents)
413 "Set ELEMENT contents to CONTENTS.
414 Return modified element."
415 (cond ((not element) (list contents))
416 ((not (symbolp (car element))) contents)
417 ((cdr element) (setcdr (cdr element) contents))
418 (t (nconc element contents))))
420 (defsubst org-element-set-element (old new)
421 "Replace element or object OLD with element or object NEW.
422 The function takes care of setting `:parent' property for NEW."
423 ;; Since OLD is going to be changed into NEW by side-effect, first
424 ;; make sure that every element or object within NEW has OLD as
425 ;; parent.
426 (mapc (lambda (blob) (org-element-put-property blob :parent old))
427 (org-element-contents new))
428 ;; Transfer contents.
429 (apply 'org-element-set-contents old (org-element-contents new))
430 ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
431 ;; with NEW's.
432 (org-element-put-property new :parent (org-element-property :parent old))
433 (setcar (cdr old) (nth 1 new))
434 ;; Transfer type.
435 (setcar old (car new)))
437 (defsubst org-element-adopt-elements (parent &rest children)
438 "Append elements to the contents of another element.
440 PARENT is an element or object. CHILDREN can be elements,
441 objects, or a strings.
443 The function takes care of setting `:parent' property for CHILD.
444 Return parent element."
445 ;; Link every child to PARENT. If PARENT is nil, it is a secondary
446 ;; string: parent is the list itself.
447 (mapc (lambda (child)
448 (org-element-put-property child :parent (or parent children)))
449 children)
450 ;; Add CHILDREN at the end of PARENT contents.
451 (when parent
452 (apply 'org-element-set-contents
453 parent
454 (nconc (org-element-contents parent) children)))
455 ;; Return modified PARENT element.
456 (or parent children))
460 ;;; Greater elements
462 ;; For each greater element type, we define a parser and an
463 ;; interpreter.
465 ;; A parser returns the element or object as the list described above.
466 ;; Most of them accepts no argument. Though, exceptions exist. Hence
467 ;; every element containing a secondary string (see
468 ;; `org-element-secondary-value-alist') will accept an optional
469 ;; argument to toggle parsing of that secondary string. Moreover,
470 ;; `item' parser requires current list's structure as its first
471 ;; element.
473 ;; An interpreter accepts two arguments: the list representation of
474 ;; the element or object, and its contents. The latter may be nil,
475 ;; depending on the element or object considered. It returns the
476 ;; appropriate Org syntax, as a string.
478 ;; Parsing functions must follow the naming convention:
479 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
480 ;; defined in `org-element-greater-elements'.
482 ;; Similarly, interpreting functions must follow the naming
483 ;; convention: org-element-TYPE-interpreter.
485 ;; With the exception of `headline' and `item' types, greater elements
486 ;; cannot contain other greater elements of their own type.
488 ;; Beside implementing a parser and an interpreter, adding a new
489 ;; greater element requires to tweak `org-element--current-element'.
490 ;; Moreover, the newly defined type must be added to both
491 ;; `org-element-all-elements' and `org-element-greater-elements'.
494 ;;;; Center Block
496 (defun org-element-center-block-parser (limit affiliated)
497 "Parse a center block.
499 LIMIT bounds the search. AFFILIATED is a list of which CAR is
500 the buffer position at the beginning of the first affiliated
501 keyword and CDR is a plist of affiliated keywords along with
502 their value.
504 Return a list whose CAR is `center-block' and CDR is a plist
505 containing `:begin', `:end', `:hiddenp', `:contents-begin',
506 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
508 Assume point is at the beginning of the block."
509 (let ((case-fold-search t))
510 (if (not (save-excursion
511 (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
512 ;; Incomplete block: parse it as a paragraph.
513 (org-element-paragraph-parser limit affiliated)
514 (let ((block-end-line (match-beginning 0)))
515 (let* ((begin (car affiliated))
516 (post-affiliated (point))
517 ;; Empty blocks have no contents.
518 (contents-begin (progn (forward-line)
519 (and (< (point) block-end-line)
520 (point))))
521 (contents-end (and contents-begin block-end-line))
522 (hidden (org-invisible-p2))
523 (pos-before-blank (progn (goto-char block-end-line)
524 (forward-line)
525 (point)))
526 (end (save-excursion (skip-chars-forward " \r\t\n" limit)
527 (skip-chars-backward " \t")
528 (if (bolp) (point) (line-end-position)))))
529 (list 'center-block
530 (nconc
531 (list :begin begin
532 :end end
533 :hiddenp hidden
534 :contents-begin contents-begin
535 :contents-end contents-end
536 :post-blank (count-lines pos-before-blank end)
537 :post-affiliated post-affiliated)
538 (cdr affiliated))))))))
540 (defun org-element-center-block-interpreter (center-block contents)
541 "Interpret CENTER-BLOCK element as Org syntax.
542 CONTENTS is the contents of the element."
543 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
546 ;;;; Drawer
548 (defun org-element-drawer-parser (limit affiliated)
549 "Parse a drawer.
551 LIMIT bounds the search. AFFILIATED is a list of which CAR is
552 the buffer position at the beginning of the first affiliated
553 keyword and CDR is a plist of affiliated keywords along with
554 their value.
556 Return a list whose CAR is `drawer' and CDR is a plist containing
557 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
558 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
560 Assume point is at beginning of drawer."
561 (let ((case-fold-search t))
562 (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
563 ;; Incomplete drawer: parse it as a paragraph.
564 (org-element-paragraph-parser limit affiliated)
565 (save-excursion
566 (let* ((drawer-end-line (match-beginning 0))
567 (name (progn (looking-at org-drawer-regexp)
568 (org-match-string-no-properties 1)))
569 (begin (car affiliated))
570 (post-affiliated (point))
571 ;; Empty drawers have no contents.
572 (contents-begin (progn (forward-line)
573 (and (< (point) drawer-end-line)
574 (point))))
575 (contents-end (and contents-begin drawer-end-line))
576 (hidden (org-invisible-p2))
577 (pos-before-blank (progn (goto-char drawer-end-line)
578 (forward-line)
579 (point)))
580 (end (progn (skip-chars-forward " \r\t\n" limit)
581 (skip-chars-backward " \t")
582 (if (bolp) (point) (line-end-position)))))
583 (list 'drawer
584 (nconc
585 (list :begin begin
586 :end end
587 :drawer-name name
588 :hiddenp hidden
589 :contents-begin contents-begin
590 :contents-end contents-end
591 :post-blank (count-lines pos-before-blank end)
592 :post-affiliated post-affiliated)
593 (cdr affiliated))))))))
595 (defun org-element-drawer-interpreter (drawer contents)
596 "Interpret DRAWER element as Org syntax.
597 CONTENTS is the contents of the element."
598 (format ":%s:\n%s:END:"
599 (org-element-property :drawer-name drawer)
600 contents))
603 ;;;; Dynamic Block
605 (defun org-element-dynamic-block-parser (limit affiliated)
606 "Parse a dynamic block.
608 LIMIT bounds the search. AFFILIATED is a list of which CAR is
609 the buffer position at the beginning of the first affiliated
610 keyword and CDR is a plist of affiliated keywords along with
611 their value.
613 Return a list whose CAR is `dynamic-block' and CDR is a plist
614 containing `:block-name', `:begin', `:end', `:hiddenp',
615 `:contents-begin', `:contents-end', `:arguments', `:post-blank'
616 and `:post-affiliated' keywords.
618 Assume point is at beginning of dynamic block."
619 (let ((case-fold-search t))
620 (if (not (save-excursion
621 (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
622 ;; Incomplete block: parse it as a paragraph.
623 (org-element-paragraph-parser limit affiliated)
624 (let ((block-end-line (match-beginning 0)))
625 (save-excursion
626 (let* ((name (progn (looking-at org-dblock-start-re)
627 (org-match-string-no-properties 1)))
628 (arguments (org-match-string-no-properties 3))
629 (begin (car affiliated))
630 (post-affiliated (point))
631 ;; Empty blocks have no contents.
632 (contents-begin (progn (forward-line)
633 (and (< (point) block-end-line)
634 (point))))
635 (contents-end (and contents-begin block-end-line))
636 (hidden (org-invisible-p2))
637 (pos-before-blank (progn (goto-char block-end-line)
638 (forward-line)
639 (point)))
640 (end (progn (skip-chars-forward " \r\t\n" limit)
641 (skip-chars-backward " \t")
642 (if (bolp) (point) (line-end-position)))))
643 (list 'dynamic-block
644 (nconc
645 (list :begin begin
646 :end end
647 :block-name name
648 :arguments arguments
649 :hiddenp hidden
650 :contents-begin contents-begin
651 :contents-end contents-end
652 :post-blank (count-lines pos-before-blank end)
653 :post-affiliated post-affiliated)
654 (cdr affiliated)))))))))
656 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
657 "Interpret DYNAMIC-BLOCK element as Org syntax.
658 CONTENTS is the contents of the element."
659 (format "#+BEGIN: %s%s\n%s#+END:"
660 (org-element-property :block-name dynamic-block)
661 (let ((args (org-element-property :arguments dynamic-block)))
662 (and args (concat " " args)))
663 contents))
666 ;;;; Footnote Definition
668 (defun org-element-footnote-definition-parser (limit affiliated)
669 "Parse a footnote definition.
671 LIMIT bounds the search. AFFILIATED is a list of which CAR is
672 the buffer position at the beginning of the first affiliated
673 keyword and CDR is a plist of affiliated keywords along with
674 their value.
676 Return a list whose CAR is `footnote-definition' and CDR is
677 a plist containing `:label', `:begin' `:end', `:contents-begin',
678 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
680 Assume point is at the beginning of the footnote definition."
681 (save-excursion
682 (let* ((label (progn (looking-at org-footnote-definition-re)
683 (org-match-string-no-properties 1)))
684 (begin (car affiliated))
685 (post-affiliated (point))
686 (ending (save-excursion
687 (if (progn
688 (end-of-line)
689 (re-search-forward
690 (concat org-outline-regexp-bol "\\|"
691 org-footnote-definition-re "\\|"
692 "^[ \t]*$") limit 'move))
693 (match-beginning 0)
694 (point))))
695 (contents-begin (progn (search-forward "]")
696 (skip-chars-forward " \r\t\n" ending)
697 (and (/= (point) ending) (point))))
698 (contents-end (and contents-begin ending))
699 (end (progn (goto-char ending)
700 (skip-chars-forward " \r\t\n" limit)
701 (skip-chars-backward " \t")
702 (if (bolp) (point) (line-end-position)))))
703 (list 'footnote-definition
704 (nconc
705 (list :label label
706 :begin begin
707 :end end
708 :contents-begin contents-begin
709 :contents-end contents-end
710 :post-blank (count-lines ending end)
711 :post-affiliated post-affiliated)
712 (cdr affiliated))))))
714 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
715 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
716 CONTENTS is the contents of the footnote-definition."
717 (concat (format "[%s]" (org-element-property :label footnote-definition))
719 contents))
722 ;;;; Headline
724 (defun org-element-headline-parser (limit &optional raw-secondary-p)
725 "Parse an headline.
727 Return a list whose CAR is `headline' and CDR is a plist
728 containing `:raw-value', `:title', `:begin', `:end',
729 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
730 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
731 `:scheduled', `:deadline', `:closed', `:clockedp', `:quotedp',
732 `:archivedp', `:commentedp' and `:footnote-section-p' keywords.
734 The plist also contains any property set in the property drawer,
735 with its name in lowercase, the underscores replaced with hyphens
736 and colons at the beginning (i.e. `:custom-id').
738 When RAW-SECONDARY-P is non-nil, headline's title will not be
739 parsed as a secondary string, but as a plain string instead.
741 Assume point is at beginning of the headline."
742 (save-excursion
743 (let* ((components (org-heading-components))
744 (level (nth 1 components))
745 (todo (nth 2 components))
746 (todo-type
747 (and todo (if (member todo org-done-keywords) 'done 'todo)))
748 (tags (let ((raw-tags (nth 5 components)))
749 (and raw-tags (org-split-string raw-tags ":"))))
750 (raw-value (or (nth 4 components) ""))
751 (quotedp
752 (let ((case-fold-search nil))
753 (string-match (format "^%s\\( \\|$\\)" org-quote-string)
754 raw-value)))
755 (commentedp
756 (let ((case-fold-search nil))
757 (string-match (format "^%s\\( \\|$\\)" org-comment-string)
758 raw-value)))
759 (archivedp (member org-archive-tag tags))
760 (footnote-section-p (and org-footnote-section
761 (string= org-footnote-section raw-value)))
762 ;; Normalize property names: ":SOME_PROP:" becomes
763 ;; ":some-prop".
764 (standard-props
765 (let (plist)
766 (mapc
767 (lambda (p)
768 (setq plist
769 (plist-put plist
770 (intern (concat ":"
771 (replace-regexp-in-string
772 "_" "-" (downcase (car p)))))
773 (cdr p))))
774 (org-entry-properties nil 'standard))
775 plist))
776 (time-props
777 ;; Read time properties on the line below the headline.
778 (save-excursion
779 (when (progn (forward-line)
780 (looking-at org-planning-or-clock-line-re))
781 (let ((end (line-end-position)) plist)
782 (while (re-search-forward
783 org-keyword-time-not-clock-regexp end t)
784 (goto-char (match-end 1))
785 (skip-chars-forward " \t")
786 (let ((keyword (match-string 1))
787 (time (org-element-timestamp-parser)))
788 (cond ((equal keyword org-scheduled-string)
789 (setq plist (plist-put plist :scheduled time)))
790 ((equal keyword org-deadline-string)
791 (setq plist (plist-put plist :deadline time)))
792 (t (setq plist (plist-put plist :closed time))))))
793 plist))))
794 (begin (point))
795 (end (save-excursion (goto-char (org-end-of-subtree t t))))
796 (pos-after-head (progn (forward-line) (point)))
797 (contents-begin (save-excursion
798 (skip-chars-forward " \r\t\n" end)
799 (and (/= (point) end) (line-beginning-position))))
800 (hidden (org-invisible-p2))
801 (contents-end (and contents-begin
802 (progn (goto-char end)
803 (skip-chars-backward " \r\t\n")
804 (forward-line)
805 (point))))
806 (clockedp (and (featurep 'org-clock)
807 (eq (org-clocking-buffer)
808 (or (buffer-base-buffer) (current-buffer)))
809 (save-excursion
810 (goto-char (marker-position org-clock-marker))
811 (org-back-to-heading t)
812 (= (point) begin)))))
813 ;; Clean RAW-VALUE from any quote or comment string.
814 (when (or quotedp commentedp)
815 (let ((case-fold-search nil))
816 (setq raw-value
817 (replace-regexp-in-string
818 (concat
819 (regexp-opt (list org-quote-string org-comment-string))
820 "\\(?: \\|$\\)")
822 raw-value))))
823 ;; Clean TAGS from archive tag, if any.
824 (when archivedp (setq tags (delete org-archive-tag tags)))
825 (let ((headline
826 (list 'headline
827 (nconc
828 (list :raw-value raw-value
829 :begin begin
830 :end end
831 :pre-blank
832 (if (not contents-begin) 0
833 (count-lines pos-after-head contents-begin))
834 :hiddenp hidden
835 :contents-begin contents-begin
836 :contents-end contents-end
837 :level level
838 :priority (nth 3 components)
839 :tags tags
840 :todo-keyword todo
841 :todo-type todo-type
842 :post-blank (count-lines
843 (if (not contents-end) pos-after-head
844 (goto-char contents-end)
845 (forward-line)
846 (point))
847 end)
848 :footnote-section-p footnote-section-p
849 :archivedp archivedp
850 :clockedp clockedp
851 :commentedp commentedp
852 :quotedp quotedp)
853 time-props
854 standard-props))))
855 (org-element-put-property
856 headline :title
857 (if raw-secondary-p raw-value
858 (org-element-parse-secondary-string
859 raw-value (org-element-restriction 'headline) headline)))))))
861 (defun org-element-headline-interpreter (headline contents)
862 "Interpret HEADLINE element as Org syntax.
863 CONTENTS is the contents of the element."
864 (let* ((level (org-element-property :level headline))
865 (todo (org-element-property :todo-keyword headline))
866 (priority (org-element-property :priority headline))
867 (title (org-element-interpret-data
868 (org-element-property :title headline)))
869 (tags (let ((tag-list (if (org-element-property :archivedp headline)
870 (cons org-archive-tag
871 (org-element-property :tags headline))
872 (org-element-property :tags headline))))
873 (and tag-list
874 (format ":%s:" (mapconcat 'identity tag-list ":")))))
875 (commentedp (org-element-property :commentedp headline))
876 (quotedp (org-element-property :quotedp headline))
877 (pre-blank (or (org-element-property :pre-blank headline) 0))
878 (heading (concat (make-string level ?*)
879 (and todo (concat " " todo))
880 (and quotedp (concat " " org-quote-string))
881 (and commentedp (concat " " org-comment-string))
882 (and priority
883 (format " [#%s]" (char-to-string priority)))
884 (cond ((and org-footnote-section
885 (org-element-property
886 :footnote-section-p headline))
887 (concat " " org-footnote-section))
888 (title (concat " " title))))))
889 (concat heading
890 ;; Align tags.
891 (when tags
892 (cond
893 ((zerop org-tags-column) (format " %s" tags))
894 ((< org-tags-column 0)
895 (concat
896 (make-string
897 (max (- (+ org-tags-column (length heading) (length tags))) 1)
899 tags))
901 (concat
902 (make-string (max (- org-tags-column (length heading)) 1) ? )
903 tags))))
904 (make-string (1+ pre-blank) 10)
905 contents)))
908 ;;;; Inlinetask
910 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
911 "Parse an inline task.
913 Return a list whose CAR is `inlinetask' and CDR is a plist
914 containing `:title', `:begin', `:end', `:hiddenp',
915 `:contents-begin' and `:contents-end', `:level', `:priority',
916 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
917 `:scheduled', `:deadline', `:clockedp', `:closed' and
918 `:post-blank' keywords.
920 The plist also contains any property set in the property drawer,
921 with its name in lowercase, the underscores replaced with hyphens
922 and colons at the beginning (i.e. `:custom-id').
924 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
925 title will not be parsed as a secondary string, but as a plain
926 string instead.
928 Assume point is at beginning of the inline task."
929 (save-excursion
930 (let* ((begin (point))
931 (components (org-heading-components))
932 (todo (nth 2 components))
933 (todo-type (and todo
934 (if (member todo org-done-keywords) 'done 'todo)))
935 (tags (let ((raw-tags (nth 5 components)))
936 (and raw-tags (org-split-string raw-tags ":"))))
937 (raw-value (or (nth 4 components) ""))
938 ;; Normalize property names: ":SOME_PROP:" becomes
939 ;; ":some-prop".
940 (standard-props
941 (let (plist)
942 (mapc
943 (lambda (p)
944 (setq plist
945 (plist-put plist
946 (intern (concat ":"
947 (replace-regexp-in-string
948 "_" "-" (downcase (car p)))))
949 (cdr p))))
950 (org-entry-properties nil 'standard))
951 plist))
952 (time-props
953 ;; Read time properties on the line below the inlinetask
954 ;; opening string.
955 (save-excursion
956 (when (progn (forward-line)
957 (looking-at org-planning-or-clock-line-re))
958 (let ((end (line-end-position)) plist)
959 (while (re-search-forward
960 org-keyword-time-not-clock-regexp end t)
961 (goto-char (match-end 1))
962 (skip-chars-forward " \t")
963 (let ((keyword (match-string 1))
964 (time (org-element-timestamp-parser)))
965 (cond ((equal keyword org-scheduled-string)
966 (setq plist (plist-put plist :scheduled time)))
967 ((equal keyword org-deadline-string)
968 (setq plist (plist-put plist :deadline time)))
969 (t (setq plist (plist-put plist :closed time))))))
970 plist))))
971 (task-end (save-excursion
972 (end-of-line)
973 (and (re-search-forward "^\\*+ END" limit t)
974 (match-beginning 0))))
975 (clockedp (and (featurep 'org-clock)
976 (eq (org-clocking-buffer)
977 (or (buffer-base-buffer) (current-buffer)))
978 (let ((clock (marker-position org-clock-marker)))
979 (and (> clock begin) (< clock task-end)))))
980 (contents-begin (progn (forward-line)
981 (and task-end (< (point) task-end) (point))))
982 (hidden (and contents-begin (org-invisible-p2)))
983 (contents-end (and contents-begin task-end))
984 (before-blank (if (not task-end) (point)
985 (goto-char task-end)
986 (forward-line)
987 (point)))
988 (end (progn (skip-chars-forward " \r\t\n" limit)
989 (skip-chars-backward " \t")
990 (if (bolp) (point) (line-end-position))))
991 (inlinetask
992 (list 'inlinetask
993 (nconc
994 (list :raw-value raw-value
995 :begin begin
996 :end end
997 :hiddenp hidden
998 :contents-begin contents-begin
999 :contents-end contents-end
1000 :level (nth 1 components)
1001 :priority (nth 3 components)
1002 :tags tags
1003 :todo-keyword todo
1004 :todo-type todo-type
1005 :clockedp clockedp
1006 :post-blank (count-lines before-blank end))
1007 time-props
1008 standard-props))))
1009 (org-element-put-property
1010 inlinetask :title
1011 (if raw-secondary-p raw-value
1012 (org-element-parse-secondary-string
1013 raw-value
1014 (org-element-restriction 'inlinetask)
1015 inlinetask))))))
1017 (defun org-element-inlinetask-interpreter (inlinetask contents)
1018 "Interpret INLINETASK element as Org syntax.
1019 CONTENTS is the contents of inlinetask."
1020 (let* ((level (org-element-property :level inlinetask))
1021 (todo (org-element-property :todo-keyword inlinetask))
1022 (priority (org-element-property :priority inlinetask))
1023 (title (org-element-interpret-data
1024 (org-element-property :title inlinetask)))
1025 (tags (let ((tag-list (org-element-property :tags inlinetask)))
1026 (and tag-list
1027 (format ":%s:" (mapconcat 'identity tag-list ":")))))
1028 (task (concat (make-string level ?*)
1029 (and todo (concat " " todo))
1030 (and priority
1031 (format " [#%s]" (char-to-string priority)))
1032 (and title (concat " " title)))))
1033 (concat task
1034 ;; Align tags.
1035 (when tags
1036 (cond
1037 ((zerop org-tags-column) (format " %s" tags))
1038 ((< org-tags-column 0)
1039 (concat
1040 (make-string
1041 (max (- (+ org-tags-column (length task) (length tags))) 1)
1043 tags))
1045 (concat
1046 (make-string (max (- org-tags-column (length task)) 1) ? )
1047 tags))))
1048 ;; Prefer degenerate inlinetasks when there are no
1049 ;; contents.
1050 (when contents
1051 (concat "\n"
1052 contents
1053 (make-string level ?*) " END")))))
1056 ;;;; Item
1058 (defun org-element-item-parser (limit struct &optional raw-secondary-p)
1059 "Parse an item.
1061 STRUCT is the structure of the plain list.
1063 Return a list whose CAR is `item' and CDR is a plist containing
1064 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
1065 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
1066 `:post-blank' keywords.
1068 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
1069 any, will not be parsed as a secondary string, but as a plain
1070 string instead.
1072 Assume point is at the beginning of the item."
1073 (save-excursion
1074 (beginning-of-line)
1075 (looking-at org-list-full-item-re)
1076 (let* ((begin (point))
1077 (bullet (org-match-string-no-properties 1))
1078 (checkbox (let ((box (org-match-string-no-properties 3)))
1079 (cond ((equal "[ ]" box) 'off)
1080 ((equal "[X]" box) 'on)
1081 ((equal "[-]" box) 'trans))))
1082 (counter (let ((c (org-match-string-no-properties 2)))
1083 (save-match-data
1084 (cond
1085 ((not c) nil)
1086 ((string-match "[A-Za-z]" c)
1087 (- (string-to-char (upcase (match-string 0 c)))
1088 64))
1089 ((string-match "[0-9]+" c)
1090 (string-to-number (match-string 0 c)))))))
1091 (end (save-excursion (goto-char (org-list-get-item-end begin struct))
1092 (unless (bolp) (forward-line))
1093 (point)))
1094 (contents-begin
1095 (progn (goto-char
1096 ;; Ignore tags in un-ordered lists: they are just
1097 ;; a part of item's body.
1098 (if (and (match-beginning 4)
1099 (save-match-data (string-match "[.)]" bullet)))
1100 (match-beginning 4)
1101 (match-end 0)))
1102 (skip-chars-forward " \r\t\n" limit)
1103 ;; If first line isn't empty, contents really start
1104 ;; at the text after item's meta-data.
1105 (if (= (point-at-bol) begin) (point) (point-at-bol))))
1106 (hidden (progn (forward-line)
1107 (and (not (= (point) end)) (org-invisible-p2))))
1108 (contents-end (progn (goto-char end)
1109 (skip-chars-backward " \r\t\n")
1110 (forward-line)
1111 (point)))
1112 (item
1113 (list 'item
1114 (list :bullet bullet
1115 :begin begin
1116 :end end
1117 ;; CONTENTS-BEGIN and CONTENTS-END may be
1118 ;; mixed up in the case of an empty item
1119 ;; separated from the next by a blank line.
1120 ;; Thus ensure the former is always the
1121 ;; smallest.
1122 :contents-begin (min contents-begin contents-end)
1123 :contents-end (max contents-begin contents-end)
1124 :checkbox checkbox
1125 :counter counter
1126 :hiddenp hidden
1127 :structure struct
1128 :post-blank (count-lines contents-end end)))))
1129 (org-element-put-property
1130 item :tag
1131 (let ((raw-tag (org-list-get-tag begin struct)))
1132 (and raw-tag
1133 (if raw-secondary-p raw-tag
1134 (org-element-parse-secondary-string
1135 raw-tag (org-element-restriction 'item) item))))))))
1137 (defun org-element-item-interpreter (item contents)
1138 "Interpret ITEM element as Org syntax.
1139 CONTENTS is the contents of the element."
1140 (let* ((bullet (org-list-bullet-string (org-element-property :bullet item)))
1141 (checkbox (org-element-property :checkbox item))
1142 (counter (org-element-property :counter item))
1143 (tag (let ((tag (org-element-property :tag item)))
1144 (and tag (org-element-interpret-data tag))))
1145 ;; Compute indentation.
1146 (ind (make-string (length bullet) 32))
1147 (item-starts-with-par-p
1148 (eq (org-element-type (car (org-element-contents item)))
1149 'paragraph)))
1150 ;; Indent contents.
1151 (concat
1152 bullet
1153 (and counter (format "[@%d] " counter))
1154 (case checkbox
1155 (on "[X] ")
1156 (off "[ ] ")
1157 (trans "[-] "))
1158 (and tag (format "%s :: " tag))
1159 (let ((contents (replace-regexp-in-string
1160 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1161 (if item-starts-with-par-p (org-trim contents)
1162 (concat "\n" contents))))))
1165 ;;;; Plain List
1167 (defun org-element-plain-list-parser (limit affiliated structure)
1168 "Parse a plain list.
1170 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1171 the buffer position at the beginning of the first affiliated
1172 keyword and CDR is a plist of affiliated keywords along with
1173 their value. STRUCTURE is the structure of the plain list being
1174 parsed.
1176 Return a list whose CAR is `plain-list' and CDR is a plist
1177 containing `:type', `:begin', `:end', `:contents-begin' and
1178 `:contents-end', `:structure', `:post-blank' and
1179 `:post-affiliated' keywords.
1181 Assume point is at the beginning of the list."
1182 (save-excursion
1183 (let* ((struct (or structure (org-list-struct)))
1184 (prevs (org-list-prevs-alist struct))
1185 (parents (org-list-parents-alist struct))
1186 (type (org-list-get-list-type (point) struct prevs))
1187 (contents-begin (point))
1188 (begin (car affiliated))
1189 (contents-end
1190 (progn (goto-char (org-list-get-list-end (point) struct prevs))
1191 (unless (bolp) (forward-line))
1192 (point)))
1193 (end (progn (skip-chars-forward " \r\t\n" limit)
1194 (skip-chars-backward " \t")
1195 (if (bolp) (point) (line-end-position)))))
1196 ;; Return value.
1197 (list 'plain-list
1198 (nconc
1199 (list :type type
1200 :begin begin
1201 :end end
1202 :contents-begin contents-begin
1203 :contents-end contents-end
1204 :structure struct
1205 :post-blank (count-lines contents-end end)
1206 :post-affiliated contents-begin)
1207 (cdr affiliated))))))
1209 (defun org-element-plain-list-interpreter (plain-list contents)
1210 "Interpret PLAIN-LIST element as Org syntax.
1211 CONTENTS is the contents of the element."
1212 (with-temp-buffer
1213 (insert contents)
1214 (goto-char (point-min))
1215 (org-list-repair)
1216 (buffer-string)))
1219 ;;;; Property Drawer
1221 (defun org-element-property-drawer-parser (limit affiliated)
1222 "Parse a property drawer.
1224 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1225 the buffer position at the beginning of the first affiliated
1226 keyword and CDR is a plist of affiliated keywords along with
1227 their value.
1229 Return a list whose CAR is `property-drawer' and CDR is a plist
1230 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1231 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1233 Assume point is at the beginning of the property drawer."
1234 (save-excursion
1235 (let ((case-fold-search t))
1236 (if (not (save-excursion
1237 (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
1238 ;; Incomplete drawer: parse it as a paragraph.
1239 (org-element-paragraph-parser limit affiliated)
1240 (save-excursion
1241 (let* ((drawer-end-line (match-beginning 0))
1242 (begin (car affiliated))
1243 (post-affiliated (point))
1244 (contents-begin (progn (forward-line)
1245 (and (< (point) drawer-end-line)
1246 (point))))
1247 (contents-end (and contents-begin drawer-end-line))
1248 (hidden (org-invisible-p2))
1249 (pos-before-blank (progn (goto-char drawer-end-line)
1250 (forward-line)
1251 (point)))
1252 (end (progn (skip-chars-forward " \r\t\n" limit)
1253 (skip-chars-backward " \t")
1254 (if (bolp) (point) (line-end-position)))))
1255 (list 'property-drawer
1256 (nconc
1257 (list :begin begin
1258 :end end
1259 :hiddenp hidden
1260 :contents-begin contents-begin
1261 :contents-end contents-end
1262 :post-blank (count-lines pos-before-blank end)
1263 :post-affiliated post-affiliated)
1264 (cdr affiliated)))))))))
1266 (defun org-element-property-drawer-interpreter (property-drawer contents)
1267 "Interpret PROPERTY-DRAWER element as Org syntax.
1268 CONTENTS is the properties within the drawer."
1269 (format ":PROPERTIES:\n%s:END:" contents))
1272 ;;;; Quote Block
1274 (defun org-element-quote-block-parser (limit affiliated)
1275 "Parse a quote block.
1277 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1278 the buffer position at the beginning of the first affiliated
1279 keyword and CDR is a plist of affiliated keywords along with
1280 their value.
1282 Return a list whose CAR is `quote-block' and CDR is a plist
1283 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1284 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
1286 Assume point is at the beginning of the block."
1287 (let ((case-fold-search t))
1288 (if (not (save-excursion
1289 (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
1290 ;; Incomplete block: parse it as a paragraph.
1291 (org-element-paragraph-parser limit affiliated)
1292 (let ((block-end-line (match-beginning 0)))
1293 (save-excursion
1294 (let* ((begin (car affiliated))
1295 (post-affiliated (point))
1296 ;; Empty blocks have no contents.
1297 (contents-begin (progn (forward-line)
1298 (and (< (point) block-end-line)
1299 (point))))
1300 (contents-end (and contents-begin block-end-line))
1301 (hidden (org-invisible-p2))
1302 (pos-before-blank (progn (goto-char block-end-line)
1303 (forward-line)
1304 (point)))
1305 (end (progn (skip-chars-forward " \r\t\n" limit)
1306 (skip-chars-backward " \t")
1307 (if (bolp) (point) (line-end-position)))))
1308 (list 'quote-block
1309 (nconc
1310 (list :begin begin
1311 :end end
1312 :hiddenp hidden
1313 :contents-begin contents-begin
1314 :contents-end contents-end
1315 :post-blank (count-lines pos-before-blank end)
1316 :post-affiliated post-affiliated)
1317 (cdr affiliated)))))))))
1319 (defun org-element-quote-block-interpreter (quote-block contents)
1320 "Interpret QUOTE-BLOCK element as Org syntax.
1321 CONTENTS is the contents of the element."
1322 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1325 ;;;; Section
1327 (defun org-element-section-parser (limit)
1328 "Parse a section.
1330 LIMIT bounds the search.
1332 Return a list whose CAR is `section' and CDR is a plist
1333 containing `:begin', `:end', `:contents-begin', `contents-end'
1334 and `:post-blank' keywords."
1335 (save-excursion
1336 ;; Beginning of section is the beginning of the first non-blank
1337 ;; line after previous headline.
1338 (let ((begin (point))
1339 (end (progn (org-with-limited-levels (outline-next-heading))
1340 (point)))
1341 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1342 (forward-line)
1343 (point))))
1344 (list 'section
1345 (list :begin begin
1346 :end end
1347 :contents-begin begin
1348 :contents-end pos-before-blank
1349 :post-blank (count-lines pos-before-blank end))))))
1351 (defun org-element-section-interpreter (section contents)
1352 "Interpret SECTION element as Org syntax.
1353 CONTENTS is the contents of the element."
1354 contents)
1357 ;;;; Special Block
1359 (defun org-element-special-block-parser (limit affiliated)
1360 "Parse a special block.
1362 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1363 the buffer position at the beginning of the first affiliated
1364 keyword and CDR is a plist of affiliated keywords along with
1365 their value.
1367 Return a list whose CAR is `special-block' and CDR is a plist
1368 containing `:type', `:begin', `:end', `:hiddenp',
1369 `:contents-begin', `:contents-end', `:post-blank' and
1370 `:post-affiliated' keywords.
1372 Assume point is at the beginning of the block."
1373 (let* ((case-fold-search t)
1374 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1375 (upcase (match-string-no-properties 1)))))
1376 (if (not (save-excursion
1377 (re-search-forward
1378 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1379 ;; Incomplete block: parse it as a paragraph.
1380 (org-element-paragraph-parser limit affiliated)
1381 (let ((block-end-line (match-beginning 0)))
1382 (save-excursion
1383 (let* ((begin (car affiliated))
1384 (post-affiliated (point))
1385 ;; Empty blocks have no contents.
1386 (contents-begin (progn (forward-line)
1387 (and (< (point) block-end-line)
1388 (point))))
1389 (contents-end (and contents-begin block-end-line))
1390 (hidden (org-invisible-p2))
1391 (pos-before-blank (progn (goto-char block-end-line)
1392 (forward-line)
1393 (point)))
1394 (end (progn (skip-chars-forward " \r\t\n" limit)
1395 (skip-chars-backward " \t")
1396 (if (bolp) (point) (line-end-position)))))
1397 (list 'special-block
1398 (nconc
1399 (list :type type
1400 :begin begin
1401 :end end
1402 :hiddenp hidden
1403 :contents-begin contents-begin
1404 :contents-end contents-end
1405 :post-blank (count-lines pos-before-blank end)
1406 :post-affiliated post-affiliated)
1407 (cdr affiliated)))))))))
1409 (defun org-element-special-block-interpreter (special-block contents)
1410 "Interpret SPECIAL-BLOCK element as Org syntax.
1411 CONTENTS is the contents of the element."
1412 (let ((block-type (org-element-property :type special-block)))
1413 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1417 ;;; Elements
1419 ;; For each element, a parser and an interpreter are also defined.
1420 ;; Both follow the same naming convention used for greater elements.
1422 ;; Also, as for greater elements, adding a new element type is done
1423 ;; through the following steps: implement a parser and an interpreter,
1424 ;; tweak `org-element--current-element' so that it recognizes the new
1425 ;; type and add that new type to `org-element-all-elements'.
1427 ;; As a special case, when the newly defined type is a block type,
1428 ;; `org-element-block-name-alist' has to be modified accordingly.
1431 ;;;; Babel Call
1433 (defun org-element-babel-call-parser (limit affiliated)
1434 "Parse a babel call.
1436 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1437 the buffer position at the beginning of the first affiliated
1438 keyword and CDR is a plist of affiliated keywords along with
1439 their value.
1441 Return a list whose CAR is `babel-call' and CDR is a plist
1442 containing `:begin', `:end', `:info', `:post-blank' and
1443 `:post-affiliated' as keywords."
1444 (save-excursion
1445 (let ((case-fold-search t)
1446 (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
1447 (org-babel-lob-get-info)))
1448 (begin (car affiliated))
1449 (post-affiliated (point))
1450 (pos-before-blank (progn (forward-line) (point)))
1451 (end (progn (skip-chars-forward " \r\t\n" limit)
1452 (skip-chars-backward " \t")
1453 (if (bolp) (point) (line-end-position)))))
1454 (list 'babel-call
1455 (nconc
1456 (list :begin begin
1457 :end end
1458 :info info
1459 :post-blank (count-lines pos-before-blank end)
1460 :post-affiliated post-affiliated)
1461 (cdr affiliated))))))
1463 (defun org-element-babel-call-interpreter (babel-call contents)
1464 "Interpret BABEL-CALL element as Org syntax.
1465 CONTENTS is nil."
1466 (let* ((babel-info (org-element-property :info babel-call))
1467 (main (car babel-info))
1468 (post-options (nth 1 babel-info)))
1469 (concat "#+CALL: "
1470 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
1471 ;; Remove redundant square brackets.
1472 (replace-match (match-string 1 main) nil nil main))
1473 (and post-options (format "[%s]" post-options)))))
1476 ;;;; Clock
1478 (defun org-element-clock-parser (limit)
1479 "Parse a clock.
1481 LIMIT bounds the search.
1483 Return a list whose CAR is `clock' and CDR is a plist containing
1484 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1485 as keywords."
1486 (save-excursion
1487 (let* ((case-fold-search nil)
1488 (begin (point))
1489 (value (progn (search-forward org-clock-string (line-end-position) t)
1490 (skip-chars-forward " \t")
1491 (org-element-timestamp-parser)))
1492 (duration (and (search-forward " => " (line-end-position) t)
1493 (progn (skip-chars-forward " \t")
1494 (looking-at "\\(\\S-+\\)[ \t]*$"))
1495 (org-match-string-no-properties 1)))
1496 (status (if duration 'closed 'running))
1497 (post-blank (let ((before-blank (progn (forward-line) (point))))
1498 (skip-chars-forward " \r\t\n" limit)
1499 (skip-chars-backward " \t")
1500 (unless (bolp) (end-of-line))
1501 (count-lines before-blank (point))))
1502 (end (point)))
1503 (list 'clock
1504 (list :status status
1505 :value value
1506 :duration duration
1507 :begin begin
1508 :end end
1509 :post-blank post-blank)))))
1511 (defun org-element-clock-interpreter (clock contents)
1512 "Interpret CLOCK element as Org syntax.
1513 CONTENTS is nil."
1514 (concat org-clock-string " "
1515 (org-element-timestamp-interpreter
1516 (org-element-property :value clock) nil)
1517 (let ((duration (org-element-property :duration clock)))
1518 (and duration
1519 (concat " => "
1520 (apply 'format
1521 "%2s:%02s"
1522 (org-split-string duration ":")))))))
1525 ;;;; Comment
1527 (defun org-element-comment-parser (limit affiliated)
1528 "Parse a comment.
1530 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1531 the buffer position at the beginning of the first affiliated
1532 keyword and CDR is a plist of affiliated keywords along with
1533 their value.
1535 Return a list whose CAR is `comment' and CDR is a plist
1536 containing `:begin', `:end', `:value', `:post-blank',
1537 `:post-affiliated' keywords.
1539 Assume point is at comment beginning."
1540 (save-excursion
1541 (let* ((begin (car affiliated))
1542 (post-affiliated (point))
1543 (value (prog2 (looking-at "[ \t]*# ?")
1544 (buffer-substring-no-properties
1545 (match-end 0) (line-end-position))
1546 (forward-line)))
1547 (com-end
1548 ;; Get comments ending.
1549 (progn
1550 (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
1551 ;; Accumulate lines without leading hash and first
1552 ;; whitespace.
1553 (setq value
1554 (concat value
1555 "\n"
1556 (buffer-substring-no-properties
1557 (match-end 0) (line-end-position))))
1558 (forward-line))
1559 (point)))
1560 (end (progn (goto-char com-end)
1561 (skip-chars-forward " \r\t\n" limit)
1562 (skip-chars-backward " \t")
1563 (if (bolp) (point) (line-end-position)))))
1564 (list 'comment
1565 (nconc
1566 (list :begin begin
1567 :end end
1568 :value value
1569 :post-blank (count-lines com-end end)
1570 :post-affiliated post-affiliated)
1571 (cdr affiliated))))))
1573 (defun org-element-comment-interpreter (comment contents)
1574 "Interpret COMMENT element as Org syntax.
1575 CONTENTS is nil."
1576 (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
1579 ;;;; Comment Block
1581 (defun org-element-comment-block-parser (limit affiliated)
1582 "Parse an export block.
1584 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1585 the buffer position at the beginning of the first affiliated
1586 keyword and CDR is a plist of affiliated keywords along with
1587 their value.
1589 Return a list whose CAR is `comment-block' and CDR is a plist
1590 containing `:begin', `:end', `:hiddenp', `:value', `:post-blank'
1591 and `:post-affiliated' keywords.
1593 Assume point is at comment block beginning."
1594 (let ((case-fold-search t))
1595 (if (not (save-excursion
1596 (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
1597 ;; Incomplete block: parse it as a paragraph.
1598 (org-element-paragraph-parser limit affiliated)
1599 (let ((contents-end (match-beginning 0)))
1600 (save-excursion
1601 (let* ((begin (car affiliated))
1602 (post-affiliated (point))
1603 (contents-begin (progn (forward-line) (point)))
1604 (hidden (org-invisible-p2))
1605 (pos-before-blank (progn (goto-char contents-end)
1606 (forward-line)
1607 (point)))
1608 (end (progn (skip-chars-forward " \r\t\n" limit)
1609 (skip-chars-backward " \t")
1610 (if (bolp) (point) (line-end-position))))
1611 (value (buffer-substring-no-properties
1612 contents-begin contents-end)))
1613 (list 'comment-block
1614 (nconc
1615 (list :begin begin
1616 :end end
1617 :value value
1618 :hiddenp hidden
1619 :post-blank (count-lines pos-before-blank end)
1620 :post-affiliated post-affiliated)
1621 (cdr affiliated)))))))))
1623 (defun org-element-comment-block-interpreter (comment-block contents)
1624 "Interpret COMMENT-BLOCK element as Org syntax.
1625 CONTENTS is nil."
1626 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1627 (org-remove-indentation (org-element-property :value comment-block))))
1630 ;;;; Diary Sexp
1632 (defun org-element-diary-sexp-parser (limit affiliated)
1633 "Parse a diary sexp.
1635 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1636 the buffer position at the beginning of the first affiliated
1637 keyword and CDR is a plist of affiliated keywords along with
1638 their value.
1640 Return a list whose CAR is `diary-sexp' and CDR is a plist
1641 containing `:begin', `:end', `:value', `:post-blank' and
1642 `:post-affiliated' keywords."
1643 (save-excursion
1644 (let ((begin (car affiliated))
1645 (post-affiliated (point))
1646 (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
1647 (org-match-string-no-properties 1)))
1648 (pos-before-blank (progn (forward-line) (point)))
1649 (end (progn (skip-chars-forward " \r\t\n" limit)
1650 (skip-chars-backward " \t")
1651 (if (bolp) (point) (line-end-position)))))
1652 (list 'diary-sexp
1653 (nconc
1654 (list :value value
1655 :begin begin
1656 :end end
1657 :post-blank (count-lines pos-before-blank end)
1658 :post-affiliated post-affiliated)
1659 (cdr affiliated))))))
1661 (defun org-element-diary-sexp-interpreter (diary-sexp contents)
1662 "Interpret DIARY-SEXP as Org syntax.
1663 CONTENTS is nil."
1664 (org-element-property :value diary-sexp))
1667 ;;;; Example Block
1669 (defun org-element-example-block-parser (limit affiliated)
1670 "Parse an example block.
1672 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1673 the buffer position at the beginning of the first affiliated
1674 keyword and CDR is a plist of affiliated keywords along with
1675 their value.
1677 Return a list whose CAR is `example-block' and CDR is a plist
1678 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1679 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1680 `:switches', `:value', `:post-blank' and `:post-affiliated'
1681 keywords."
1682 (let ((case-fold-search t))
1683 (if (not (save-excursion
1684 (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
1685 ;; Incomplete block: parse it as a paragraph.
1686 (org-element-paragraph-parser limit affiliated)
1687 (let ((contents-end (match-beginning 0)))
1688 (save-excursion
1689 (let* ((switches
1690 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1691 (org-match-string-no-properties 1)))
1692 ;; Switches analysis
1693 (number-lines (cond ((not switches) nil)
1694 ((string-match "-n\\>" switches) 'new)
1695 ((string-match "+n\\>" switches) 'continued)))
1696 (preserve-indent (and switches (string-match "-i\\>" switches)))
1697 ;; Should labels be retained in (or stripped from) example
1698 ;; blocks?
1699 (retain-labels
1700 (or (not switches)
1701 (not (string-match "-r\\>" switches))
1702 (and number-lines (string-match "-k\\>" switches))))
1703 ;; What should code-references use - labels or
1704 ;; line-numbers?
1705 (use-labels
1706 (or (not switches)
1707 (and retain-labels (not (string-match "-k\\>" switches)))))
1708 (label-fmt (and switches
1709 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1710 (match-string 1 switches)))
1711 ;; Standard block parsing.
1712 (begin (car affiliated))
1713 (post-affiliated (point))
1714 (contents-begin (progn (forward-line) (point)))
1715 (hidden (org-invisible-p2))
1716 (value (org-unescape-code-in-string
1717 (buffer-substring-no-properties
1718 contents-begin contents-end)))
1719 (pos-before-blank (progn (goto-char contents-end)
1720 (forward-line)
1721 (point)))
1722 (end (progn (skip-chars-forward " \r\t\n" limit)
1723 (skip-chars-backward " \t")
1724 (if (bolp) (point) (line-end-position)))))
1725 (list 'example-block
1726 (nconc
1727 (list :begin begin
1728 :end end
1729 :value value
1730 :switches switches
1731 :number-lines number-lines
1732 :preserve-indent preserve-indent
1733 :retain-labels retain-labels
1734 :use-labels use-labels
1735 :label-fmt label-fmt
1736 :hiddenp hidden
1737 :post-blank (count-lines pos-before-blank end)
1738 :post-affiliated post-affiliated)
1739 (cdr affiliated)))))))))
1741 (defun org-element-example-block-interpreter (example-block contents)
1742 "Interpret EXAMPLE-BLOCK element as Org syntax.
1743 CONTENTS is nil."
1744 (let ((switches (org-element-property :switches example-block)))
1745 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1746 (org-remove-indentation
1747 (org-escape-code-in-string
1748 (org-element-property :value example-block)))
1749 "#+END_EXAMPLE")))
1752 ;;;; Export Block
1754 (defun org-element-export-block-parser (limit affiliated)
1755 "Parse an export block.
1757 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1758 the buffer position at the beginning of the first affiliated
1759 keyword and CDR is a plist of affiliated keywords along with
1760 their value.
1762 Return a list whose CAR is `export-block' and CDR is a plist
1763 containing `:begin', `:end', `:type', `:hiddenp', `:value',
1764 `:post-blank' and `:post-affiliated' keywords.
1766 Assume point is at export-block beginning."
1767 (let* ((case-fold-search t)
1768 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1769 (upcase (org-match-string-no-properties 1)))))
1770 (if (not (save-excursion
1771 (re-search-forward
1772 (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
1773 ;; Incomplete block: parse it as a paragraph.
1774 (org-element-paragraph-parser limit affiliated)
1775 (let ((contents-end (match-beginning 0)))
1776 (save-excursion
1777 (let* ((begin (car affiliated))
1778 (post-affiliated (point))
1779 (contents-begin (progn (forward-line) (point)))
1780 (hidden (org-invisible-p2))
1781 (pos-before-blank (progn (goto-char contents-end)
1782 (forward-line)
1783 (point)))
1784 (end (progn (skip-chars-forward " \r\t\n" limit)
1785 (skip-chars-backward " \t")
1786 (if (bolp) (point) (line-end-position))))
1787 (value (buffer-substring-no-properties contents-begin
1788 contents-end)))
1789 (list 'export-block
1790 (nconc
1791 (list :begin begin
1792 :end end
1793 :type type
1794 :value value
1795 :hiddenp hidden
1796 :post-blank (count-lines pos-before-blank end)
1797 :post-affiliated post-affiliated)
1798 (cdr affiliated)))))))))
1800 (defun org-element-export-block-interpreter (export-block contents)
1801 "Interpret EXPORT-BLOCK element as Org syntax.
1802 CONTENTS is nil."
1803 (let ((type (org-element-property :type export-block)))
1804 (concat (format "#+BEGIN_%s\n" type)
1805 (org-element-property :value export-block)
1806 (format "#+END_%s" type))))
1809 ;;;; Fixed-width
1811 (defun org-element-fixed-width-parser (limit affiliated)
1812 "Parse a fixed-width section.
1814 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1815 the buffer position at the beginning of the first affiliated
1816 keyword and CDR is a plist of affiliated keywords along with
1817 their value.
1819 Return a list whose CAR is `fixed-width' and CDR is a plist
1820 containing `:begin', `:end', `:value', `:post-blank' and
1821 `:post-affiliated' keywords.
1823 Assume point is at the beginning of the fixed-width area."
1824 (save-excursion
1825 (let* ((begin (car affiliated))
1826 (post-affiliated (point))
1827 value
1828 (end-area
1829 (progn
1830 (while (and (< (point) limit)
1831 (looking-at "[ \t]*:\\( \\|$\\)"))
1832 ;; Accumulate text without starting colons.
1833 (setq value
1834 (concat value
1835 (buffer-substring-no-properties
1836 (match-end 0) (point-at-eol))
1837 "\n"))
1838 (forward-line))
1839 (point)))
1840 (end (progn (skip-chars-forward " \r\t\n" limit)
1841 (skip-chars-backward " \t")
1842 (if (bolp) (point) (line-end-position)))))
1843 (list 'fixed-width
1844 (nconc
1845 (list :begin begin
1846 :end end
1847 :value value
1848 :post-blank (count-lines end-area end)
1849 :post-affiliated post-affiliated)
1850 (cdr affiliated))))))
1852 (defun org-element-fixed-width-interpreter (fixed-width contents)
1853 "Interpret FIXED-WIDTH element as Org syntax.
1854 CONTENTS is nil."
1855 (replace-regexp-in-string
1856 "^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
1859 ;;;; Horizontal Rule
1861 (defun org-element-horizontal-rule-parser (limit affiliated)
1862 "Parse an horizontal rule.
1864 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1865 the buffer position at the beginning of the first affiliated
1866 keyword and CDR is a plist of affiliated keywords along with
1867 their value.
1869 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1870 containing `:begin', `:end', `:post-blank' and `:post-affiliated'
1871 keywords."
1872 (save-excursion
1873 (let ((begin (car affiliated))
1874 (post-affiliated (point))
1875 (post-hr (progn (forward-line) (point)))
1876 (end (progn (skip-chars-forward " \r\t\n" limit)
1877 (skip-chars-backward " \t")
1878 (if (bolp) (point) (line-end-position)))))
1879 (list 'horizontal-rule
1880 (nconc
1881 (list :begin begin
1882 :end end
1883 :post-blank (count-lines post-hr end)
1884 :post-affiliated post-affiliated)
1885 (cdr affiliated))))))
1887 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1888 "Interpret HORIZONTAL-RULE element as Org syntax.
1889 CONTENTS is nil."
1890 "-----")
1893 ;;;; Keyword
1895 (defun org-element-keyword-parser (limit affiliated)
1896 "Parse a keyword at point.
1898 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1899 the buffer position at the beginning of the first affiliated
1900 keyword and CDR is a plist of affiliated keywords along with
1901 their value.
1903 Return a list whose CAR is `keyword' and CDR is a plist
1904 containing `:key', `:value', `:begin', `:end', `:post-blank' and
1905 `:post-affiliated' keywords."
1906 (save-excursion
1907 (let ((begin (car affiliated))
1908 (post-affiliated (point))
1909 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
1910 (upcase (org-match-string-no-properties 1))))
1911 (value (org-trim (buffer-substring-no-properties
1912 (match-end 0) (point-at-eol))))
1913 (pos-before-blank (progn (forward-line) (point)))
1914 (end (progn (skip-chars-forward " \r\t\n" limit)
1915 (skip-chars-backward " \t")
1916 (if (bolp) (point) (line-end-position)))))
1917 (list 'keyword
1918 (nconc
1919 (list :key key
1920 :value value
1921 :begin begin
1922 :end end
1923 :post-blank (count-lines pos-before-blank end)
1924 :post-affiliated post-affiliated)
1925 (cdr affiliated))))))
1927 (defun org-element-keyword-interpreter (keyword contents)
1928 "Interpret KEYWORD element as Org syntax.
1929 CONTENTS is nil."
1930 (format "#+%s: %s"
1931 (org-element-property :key keyword)
1932 (org-element-property :value keyword)))
1935 ;;;; Latex Environment
1937 (defun org-element-latex-environment-parser (limit affiliated)
1938 "Parse a LaTeX environment.
1940 LIMIT bounds the search. AFFILIATED is a list of which CAR is
1941 the buffer position at the beginning of the first affiliated
1942 keyword and CDR is a plist of affiliated keywords along with
1943 their value.
1945 Return a list whose CAR is `latex-environment' and CDR is a plist
1946 containing `:begin', `:end', `:value', `:post-blank' and
1947 `:post-affiliated' keywords.
1949 Assume point is at the beginning of the latex environment."
1950 (save-excursion
1951 (let ((case-fold-search t)
1952 (code-begin (point)))
1953 (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
1954 (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
1955 (regexp-quote (match-string 1)))
1956 limit t))
1957 ;; Incomplete latex environment: parse it as a paragraph.
1958 (org-element-paragraph-parser limit affiliated)
1959 (let* ((code-end (progn (forward-line) (point)))
1960 (begin (car affiliated))
1961 (post-affiliated (point))
1962 (value (buffer-substring-no-properties code-begin code-end))
1963 (end (progn (skip-chars-forward " \r\t\n" limit)
1964 (skip-chars-backward " \t")
1965 (if (bolp) (point) (line-end-position)))))
1966 (list 'latex-environment
1967 (nconc
1968 (list :begin begin
1969 :end end
1970 :value value
1971 :post-blank (count-lines code-end end)
1972 :post-affiliated post-affiliated)
1973 (cdr affiliated))))))))
1975 (defun org-element-latex-environment-interpreter (latex-environment contents)
1976 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1977 CONTENTS is nil."
1978 (org-element-property :value latex-environment))
1981 ;;;; Node Property
1983 (defun org-element-node-property-parser (limit)
1984 "Parse a node-property at point.
1986 LIMIT bounds the search.
1988 Return a list whose CAR is `node-property' and CDR is a plist
1989 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1990 keywords."
1991 (save-excursion
1992 (let ((case-fold-search t)
1993 (begin (point))
1994 (key (progn (looking-at "[ \t]*:\\(.*?\\):[ \t]+\\(.*?\\)[ \t]*$")
1995 (org-match-string-no-properties 1)))
1996 (value (org-match-string-no-properties 2))
1997 (pos-before-blank (progn (forward-line) (point)))
1998 (end (progn (skip-chars-forward " \r\t\n" limit)
1999 (if (eobp) (point) (point-at-bol)))))
2000 (list 'node-property
2001 (list :key key
2002 :value value
2003 :begin begin
2004 :end end
2005 :post-blank (count-lines pos-before-blank end))))))
2007 (defun org-element-node-property-interpreter (node-property contents)
2008 "Interpret NODE-PROPERTY element as Org syntax.
2009 CONTENTS is nil."
2010 (format org-property-format
2011 (format ":%s:" (org-element-property :key node-property))
2012 (org-element-property :value node-property)))
2015 ;;;; Paragraph
2017 (defun org-element-paragraph-parser (limit affiliated)
2018 "Parse a paragraph.
2020 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2021 the buffer position at the beginning of the first affiliated
2022 keyword and CDR is a plist of affiliated keywords along with
2023 their value.
2025 Return a list whose CAR is `paragraph' and CDR is a plist
2026 containing `:begin', `:end', `:contents-begin' and
2027 `:contents-end', `:post-blank' and `:post-affiliated' keywords.
2029 Assume point is at the beginning of the paragraph."
2030 (save-excursion
2031 (let* ((begin (car affiliated))
2032 (contents-begin (point))
2033 (before-blank
2034 (let ((case-fold-search t))
2035 (end-of-line)
2036 (if (not (re-search-forward
2037 org-element-paragraph-separate limit 'm))
2038 limit
2039 ;; A matching `org-element-paragraph-separate' is not
2040 ;; necessarily the end of the paragraph. In
2041 ;; particular, lines starting with # or : as a first
2042 ;; non-space character are ambiguous. We have check
2043 ;; if they are valid Org syntax (i.e. not an
2044 ;; incomplete keyword).
2045 (beginning-of-line)
2046 (while (not
2048 ;; There's no ambiguity for other symbols or
2049 ;; empty lines: stop here.
2050 (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
2051 ;; Stop at valid fixed-width areas.
2052 (looking-at "[ \t]*:\\(?: \\|$\\)")
2053 ;; Stop at drawers.
2054 (and (looking-at org-drawer-regexp)
2055 (save-excursion
2056 (re-search-forward
2057 "^[ \t]*:END:[ \t]*$" limit t)))
2058 ;; Stop at valid comments.
2059 (looking-at "[ \t]*#\\(?: \\|$\\)")
2060 ;; Stop at valid dynamic blocks.
2061 (and (looking-at org-dblock-start-re)
2062 (save-excursion
2063 (re-search-forward
2064 "^[ \t]*#\\+END:?[ \t]*$" limit t)))
2065 ;; Stop at valid blocks.
2066 (and (looking-at
2067 "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
2068 (save-excursion
2069 (re-search-forward
2070 (format "^[ \t]*#\\+END_%s[ \t]*$"
2071 (match-string 1))
2072 limit t)))
2073 ;; Stop at valid latex environments.
2074 (and (looking-at
2075 "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}[ \t]*$")
2076 (save-excursion
2077 (re-search-forward
2078 (format "^[ \t]*\\\\end{%s}[ \t]*$"
2079 (match-string 1))
2080 limit t)))
2081 ;; Stop at valid keywords.
2082 (looking-at "[ \t]*#\\+\\S-+:")
2083 ;; Skip everything else.
2084 (not
2085 (progn
2086 (end-of-line)
2087 (re-search-forward org-element-paragraph-separate
2088 limit 'm)))))
2089 (beginning-of-line)))
2090 (if (= (point) limit) limit
2091 (goto-char (line-beginning-position)))))
2092 (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
2093 (forward-line)
2094 (point)))
2095 (end (progn (skip-chars-forward " \r\t\n" limit)
2096 (skip-chars-backward " \t")
2097 (if (bolp) (point) (line-end-position)))))
2098 (list 'paragraph
2099 (nconc
2100 (list :begin begin
2101 :end end
2102 :contents-begin contents-begin
2103 :contents-end contents-end
2104 :post-blank (count-lines before-blank end)
2105 :post-affiliated contents-begin)
2106 (cdr affiliated))))))
2108 (defun org-element-paragraph-interpreter (paragraph contents)
2109 "Interpret PARAGRAPH element as Org syntax.
2110 CONTENTS is the contents of the element."
2111 contents)
2114 ;;;; Planning
2116 (defun org-element-planning-parser (limit)
2117 "Parse a planning.
2119 LIMIT bounds the search.
2121 Return a list whose CAR is `planning' and CDR is a plist
2122 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
2123 and `:post-blank' keywords."
2124 (save-excursion
2125 (let* ((case-fold-search nil)
2126 (begin (point))
2127 (post-blank (let ((before-blank (progn (forward-line) (point))))
2128 (skip-chars-forward " \r\t\n" limit)
2129 (skip-chars-backward " \t")
2130 (unless (bolp) (end-of-line))
2131 (count-lines before-blank (point))))
2132 (end (point))
2133 closed deadline scheduled)
2134 (goto-char begin)
2135 (while (re-search-forward org-keyword-time-not-clock-regexp end t)
2136 (goto-char (match-end 1))
2137 (skip-chars-forward " \t" end)
2138 (let ((keyword (match-string 1))
2139 (time (org-element-timestamp-parser)))
2140 (cond ((equal keyword org-closed-string) (setq closed time))
2141 ((equal keyword org-deadline-string) (setq deadline time))
2142 (t (setq scheduled time)))))
2143 (list 'planning
2144 (list :closed closed
2145 :deadline deadline
2146 :scheduled scheduled
2147 :begin begin
2148 :end end
2149 :post-blank post-blank)))))
2151 (defun org-element-planning-interpreter (planning contents)
2152 "Interpret PLANNING element as Org syntax.
2153 CONTENTS is nil."
2154 (mapconcat
2155 'identity
2156 (delq nil
2157 (list (let ((deadline (org-element-property :deadline planning)))
2158 (when deadline
2159 (concat org-deadline-string " "
2160 (org-element-timestamp-interpreter deadline nil))))
2161 (let ((scheduled (org-element-property :scheduled planning)))
2162 (when scheduled
2163 (concat org-scheduled-string " "
2164 (org-element-timestamp-interpreter scheduled nil))))
2165 (let ((closed (org-element-property :closed planning)))
2166 (when closed
2167 (concat org-closed-string " "
2168 (org-element-timestamp-interpreter closed nil))))))
2169 " "))
2172 ;;;; Quote Section
2174 (defun org-element-quote-section-parser (limit)
2175 "Parse a quote section.
2177 LIMIT bounds the search.
2179 Return a list whose CAR is `quote-section' and CDR is a plist
2180 containing `:begin', `:end', `:value' and `:post-blank' keywords.
2182 Assume point is at beginning of the section."
2183 (save-excursion
2184 (let* ((begin (point))
2185 (end (progn (org-with-limited-levels (outline-next-heading))
2186 (point)))
2187 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
2188 (forward-line)
2189 (point)))
2190 (value (buffer-substring-no-properties begin pos-before-blank)))
2191 (list 'quote-section
2192 (list :begin begin
2193 :end end
2194 :value value
2195 :post-blank (count-lines pos-before-blank end))))))
2197 (defun org-element-quote-section-interpreter (quote-section contents)
2198 "Interpret QUOTE-SECTION element as Org syntax.
2199 CONTENTS is nil."
2200 (org-element-property :value quote-section))
2203 ;;;; Src Block
2205 (defun org-element-src-block-parser (limit affiliated)
2206 "Parse a src block.
2208 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2209 the buffer position at the beginning of the first affiliated
2210 keyword and CDR is a plist of affiliated keywords along with
2211 their value.
2213 Return a list whose CAR is `src-block' and CDR is a plist
2214 containing `:language', `:switches', `:parameters', `:begin',
2215 `:end', `:hiddenp', `:number-lines', `:retain-labels',
2216 `:use-labels', `:label-fmt', `:preserve-indent', `:value',
2217 `:post-blank' and `:post-affiliated' keywords.
2219 Assume point is at the beginning of the block."
2220 (let ((case-fold-search t))
2221 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
2222 limit t)))
2223 ;; Incomplete block: parse it as a paragraph.
2224 (org-element-paragraph-parser limit affiliated)
2225 (let ((contents-end (match-beginning 0)))
2226 (save-excursion
2227 (let* ((begin (car affiliated))
2228 (post-affiliated (point))
2229 ;; Get language as a string.
2230 (language
2231 (progn
2232 (looking-at
2233 (concat "^[ \t]*#\\+BEGIN_SRC"
2234 "\\(?: +\\(\\S-+\\)\\)?"
2235 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
2236 "\\(.*\\)[ \t]*$"))
2237 (org-match-string-no-properties 1)))
2238 ;; Get switches.
2239 (switches (org-match-string-no-properties 2))
2240 ;; Get parameters.
2241 (parameters (org-match-string-no-properties 3))
2242 ;; Switches analysis
2243 (number-lines (cond ((not switches) nil)
2244 ((string-match "-n\\>" switches) 'new)
2245 ((string-match "+n\\>" switches) 'continued)))
2246 (preserve-indent (and switches (string-match "-i\\>" switches)))
2247 (label-fmt (and switches
2248 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2249 (match-string 1 switches)))
2250 ;; Should labels be retained in (or stripped from)
2251 ;; src blocks?
2252 (retain-labels
2253 (or (not switches)
2254 (not (string-match "-r\\>" switches))
2255 (and number-lines (string-match "-k\\>" switches))))
2256 ;; What should code-references use - labels or
2257 ;; line-numbers?
2258 (use-labels
2259 (or (not switches)
2260 (and retain-labels (not (string-match "-k\\>" switches)))))
2261 ;; Get visibility status.
2262 (hidden (progn (forward-line) (org-invisible-p2)))
2263 ;; Retrieve code.
2264 (value (org-unescape-code-in-string
2265 (buffer-substring-no-properties (point) contents-end)))
2266 (pos-before-blank (progn (goto-char contents-end)
2267 (forward-line)
2268 (point)))
2269 ;; Get position after ending blank lines.
2270 (end (progn (skip-chars-forward " \r\t\n" limit)
2271 (skip-chars-backward " \t")
2272 (if (bolp) (point) (line-end-position)))))
2273 (list 'src-block
2274 (nconc
2275 (list :language language
2276 :switches (and (org-string-nw-p switches)
2277 (org-trim switches))
2278 :parameters (and (org-string-nw-p parameters)
2279 (org-trim parameters))
2280 :begin begin
2281 :end end
2282 :number-lines number-lines
2283 :preserve-indent preserve-indent
2284 :retain-labels retain-labels
2285 :use-labels use-labels
2286 :label-fmt label-fmt
2287 :hiddenp hidden
2288 :value value
2289 :post-blank (count-lines pos-before-blank end)
2290 :post-affiliated post-affiliated)
2291 (cdr affiliated)))))))))
2293 (defun org-element-src-block-interpreter (src-block contents)
2294 "Interpret SRC-BLOCK element as Org syntax.
2295 CONTENTS is nil."
2296 (let ((lang (org-element-property :language src-block))
2297 (switches (org-element-property :switches src-block))
2298 (params (org-element-property :parameters src-block))
2299 (value (let ((val (org-element-property :value src-block)))
2300 (cond
2301 (org-src-preserve-indentation val)
2302 ((zerop org-edit-src-content-indentation)
2303 (org-remove-indentation val))
2305 (let ((ind (make-string
2306 org-edit-src-content-indentation 32)))
2307 (replace-regexp-in-string
2308 "\\(^\\)[ \t]*\\S-" ind
2309 (org-remove-indentation val) nil nil 1)))))))
2310 (concat (format "#+BEGIN_SRC%s\n"
2311 (concat (and lang (concat " " lang))
2312 (and switches (concat " " switches))
2313 (and params (concat " " params))))
2314 (org-escape-code-in-string value)
2315 "#+END_SRC")))
2318 ;;;; Table
2320 (defun org-element-table-parser (limit affiliated)
2321 "Parse a table at point.
2323 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2324 the buffer position at the beginning of the first affiliated
2325 keyword and CDR is a plist of affiliated keywords along with
2326 their value.
2328 Return a list whose CAR is `table' and CDR is a plist containing
2329 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
2330 `:contents-end', `:value', `:post-blank' and `:post-affiliated'
2331 keywords.
2333 Assume point is at the beginning of the table."
2334 (save-excursion
2335 (let* ((case-fold-search t)
2336 (table-begin (point))
2337 (type (if (org-at-table.el-p) 'table.el 'org))
2338 (begin (car affiliated))
2339 (table-end
2340 (if (re-search-forward org-table-any-border-regexp limit 'm)
2341 (goto-char (match-beginning 0))
2342 (point)))
2343 (tblfm (let (acc)
2344 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
2345 (push (org-match-string-no-properties 1) acc)
2346 (forward-line))
2347 acc))
2348 (pos-before-blank (point))
2349 (end (progn (skip-chars-forward " \r\t\n" limit)
2350 (skip-chars-backward " \t")
2351 (if (bolp) (point) (line-end-position)))))
2352 (list 'table
2353 (nconc
2354 (list :begin begin
2355 :end end
2356 :type type
2357 :tblfm tblfm
2358 ;; Only `org' tables have contents. `table.el' tables
2359 ;; use a `:value' property to store raw table as
2360 ;; a string.
2361 :contents-begin (and (eq type 'org) table-begin)
2362 :contents-end (and (eq type 'org) table-end)
2363 :value (and (eq type 'table.el)
2364 (buffer-substring-no-properties
2365 table-begin table-end))
2366 :post-blank (count-lines pos-before-blank end)
2367 :post-affiliated table-begin)
2368 (cdr affiliated))))))
2370 (defun org-element-table-interpreter (table contents)
2371 "Interpret TABLE element as Org syntax.
2372 CONTENTS is nil."
2373 (if (eq (org-element-property :type table) 'table.el)
2374 (org-remove-indentation (org-element-property :value table))
2375 (concat (with-temp-buffer (insert contents)
2376 (org-table-align)
2377 (buffer-string))
2378 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2379 (reverse (org-element-property :tblfm table))
2380 "\n"))))
2383 ;;;; Table Row
2385 (defun org-element-table-row-parser (limit)
2386 "Parse table row at point.
2388 LIMIT bounds the search.
2390 Return a list whose CAR is `table-row' and CDR is a plist
2391 containing `:begin', `:end', `:contents-begin', `:contents-end',
2392 `:type' and `:post-blank' keywords."
2393 (save-excursion
2394 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2395 (begin (point))
2396 ;; A table rule has no contents. In that case, ensure
2397 ;; CONTENTS-BEGIN matches CONTENTS-END.
2398 (contents-begin (and (eq type 'standard)
2399 (search-forward "|")
2400 (point)))
2401 (contents-end (and (eq type 'standard)
2402 (progn
2403 (end-of-line)
2404 (skip-chars-backward " \t")
2405 (point))))
2406 (end (progn (forward-line) (point))))
2407 (list 'table-row
2408 (list :type type
2409 :begin begin
2410 :end end
2411 :contents-begin contents-begin
2412 :contents-end contents-end
2413 :post-blank 0)))))
2415 (defun org-element-table-row-interpreter (table-row contents)
2416 "Interpret TABLE-ROW element as Org syntax.
2417 CONTENTS is the contents of the table row."
2418 (if (eq (org-element-property :type table-row) 'rule) "|-"
2419 (concat "| " contents)))
2422 ;;;; Verse Block
2424 (defun org-element-verse-block-parser (limit affiliated)
2425 "Parse a verse block.
2427 LIMIT bounds the search. AFFILIATED is a list of which CAR is
2428 the buffer position at the beginning of the first affiliated
2429 keyword and CDR is a plist of affiliated keywords along with
2430 their value.
2432 Return a list whose CAR is `verse-block' and CDR is a plist
2433 containing `:begin', `:end', `:contents-begin', `:contents-end',
2434 `:hiddenp', `:post-blank' and `:post-affiliated' keywords.
2436 Assume point is at beginning of the block."
2437 (let ((case-fold-search t))
2438 (if (not (save-excursion
2439 (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
2440 ;; Incomplete block: parse it as a paragraph.
2441 (org-element-paragraph-parser limit affiliated)
2442 (let ((contents-end (match-beginning 0)))
2443 (save-excursion
2444 (let* ((begin (car affiliated))
2445 (post-affiliated (point))
2446 (hidden (progn (forward-line) (org-invisible-p2)))
2447 (contents-begin (point))
2448 (pos-before-blank (progn (goto-char contents-end)
2449 (forward-line)
2450 (point)))
2451 (end (progn (skip-chars-forward " \r\t\n" limit)
2452 (skip-chars-backward " \t")
2453 (if (bolp) (point) (line-end-position)))))
2454 (list 'verse-block
2455 (nconc
2456 (list :begin begin
2457 :end end
2458 :contents-begin contents-begin
2459 :contents-end contents-end
2460 :hiddenp hidden
2461 :post-blank (count-lines pos-before-blank end)
2462 :post-affiliated post-affiliated)
2463 (cdr affiliated)))))))))
2465 (defun org-element-verse-block-interpreter (verse-block contents)
2466 "Interpret VERSE-BLOCK element as Org syntax.
2467 CONTENTS is verse block contents."
2468 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2472 ;;; Objects
2474 ;; Unlike to elements, interstices can be found between objects.
2475 ;; That's why, along with the parser, successor functions are provided
2476 ;; for each object. Some objects share the same successor (i.e. `code'
2477 ;; and `verbatim' objects).
2479 ;; A successor must accept a single argument bounding the search. It
2480 ;; will return either a cons cell whose CAR is the object's type, as
2481 ;; a symbol, and CDR the position of its next occurrence, or nil.
2483 ;; Successors follow the naming convention:
2484 ;; org-element-NAME-successor, where NAME is the name of the
2485 ;; successor, as defined in `org-element-all-successors'.
2487 ;; Some object types (i.e. `italic') are recursive. Restrictions on
2488 ;; object types they can contain will be specified in
2489 ;; `org-element-object-restrictions'.
2491 ;; Adding a new type of object is simple. Implement a successor,
2492 ;; a parser, and an interpreter for it, all following the naming
2493 ;; convention. Register type in `org-element-all-objects' and
2494 ;; successor in `org-element-all-successors'. Maybe tweak
2495 ;; restrictions about it, and that's it.
2498 ;;;; Bold
2500 (defun org-element-bold-parser ()
2501 "Parse bold object at point.
2503 Return a list whose CAR is `bold' and CDR is a plist with
2504 `:begin', `:end', `:contents-begin' and `:contents-end' and
2505 `:post-blank' keywords.
2507 Assume point is at the first star marker."
2508 (save-excursion
2509 (unless (bolp) (backward-char 1))
2510 (looking-at org-emph-re)
2511 (let ((begin (match-beginning 2))
2512 (contents-begin (match-beginning 4))
2513 (contents-end (match-end 4))
2514 (post-blank (progn (goto-char (match-end 2))
2515 (skip-chars-forward " \t")))
2516 (end (point)))
2517 (list 'bold
2518 (list :begin begin
2519 :end end
2520 :contents-begin contents-begin
2521 :contents-end contents-end
2522 :post-blank post-blank)))))
2524 (defun org-element-bold-interpreter (bold contents)
2525 "Interpret BOLD object as Org syntax.
2526 CONTENTS is the contents of the object."
2527 (format "*%s*" contents))
2529 (defun org-element-text-markup-successor (limit)
2530 "Search for the next text-markup object.
2532 LIMIT bounds the search.
2534 Return value is a cons cell whose CAR is a symbol among `bold',
2535 `italic', `underline', `strike-through', `code' and `verbatim'
2536 and CDR is beginning position."
2537 (save-excursion
2538 (unless (bolp) (backward-char))
2539 (when (re-search-forward org-emph-re limit t)
2540 (let ((marker (match-string 3)))
2541 (cons (cond
2542 ((equal marker "*") 'bold)
2543 ((equal marker "/") 'italic)
2544 ((equal marker "_") 'underline)
2545 ((equal marker "+") 'strike-through)
2546 ((equal marker "~") 'code)
2547 ((equal marker "=") 'verbatim)
2548 (t (error "Unknown marker at %d" (match-beginning 3))))
2549 (match-beginning 2))))))
2552 ;;;; Code
2554 (defun org-element-code-parser ()
2555 "Parse code object at point.
2557 Return a list whose CAR is `code' and CDR is a plist with
2558 `:value', `:begin', `:end' and `:post-blank' keywords.
2560 Assume point is at the first tilde marker."
2561 (save-excursion
2562 (unless (bolp) (backward-char 1))
2563 (looking-at org-emph-re)
2564 (let ((begin (match-beginning 2))
2565 (value (org-match-string-no-properties 4))
2566 (post-blank (progn (goto-char (match-end 2))
2567 (skip-chars-forward " \t")))
2568 (end (point)))
2569 (list 'code
2570 (list :value value
2571 :begin begin
2572 :end end
2573 :post-blank post-blank)))))
2575 (defun org-element-code-interpreter (code contents)
2576 "Interpret CODE object as Org syntax.
2577 CONTENTS is nil."
2578 (format "~%s~" (org-element-property :value code)))
2581 ;;;; Entity
2583 (defun org-element-entity-parser ()
2584 "Parse entity at point.
2586 Return a list whose CAR is `entity' and CDR a plist with
2587 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2588 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2589 keywords.
2591 Assume point is at the beginning of the entity."
2592 (save-excursion
2593 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2594 (let* ((value (org-entity-get (match-string 1)))
2595 (begin (match-beginning 0))
2596 (bracketsp (string= (match-string 2) "{}"))
2597 (post-blank (progn (goto-char (match-end 1))
2598 (when bracketsp (forward-char 2))
2599 (skip-chars-forward " \t")))
2600 (end (point)))
2601 (list 'entity
2602 (list :name (car value)
2603 :latex (nth 1 value)
2604 :latex-math-p (nth 2 value)
2605 :html (nth 3 value)
2606 :ascii (nth 4 value)
2607 :latin1 (nth 5 value)
2608 :utf-8 (nth 6 value)
2609 :begin begin
2610 :end end
2611 :use-brackets-p bracketsp
2612 :post-blank post-blank)))))
2614 (defun org-element-entity-interpreter (entity contents)
2615 "Interpret ENTITY object as Org syntax.
2616 CONTENTS is nil."
2617 (concat "\\"
2618 (org-element-property :name entity)
2619 (when (org-element-property :use-brackets-p entity) "{}")))
2621 (defun org-element-latex-or-entity-successor (limit)
2622 "Search for the next latex-fragment or entity object.
2624 LIMIT bounds the search.
2626 Return value is a cons cell whose CAR is `entity' or
2627 `latex-fragment' and CDR is beginning position."
2628 (save-excursion
2629 (unless (bolp) (backward-char))
2630 (let ((matchers
2631 (remove "begin" (plist-get org-format-latex-options :matchers)))
2632 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2633 (entity-re
2634 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2635 (when (re-search-forward
2636 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
2637 matchers "\\|")
2638 "\\|" entity-re)
2639 limit t)
2640 (goto-char (match-beginning 0))
2641 (if (looking-at entity-re)
2642 ;; Determine if it's a real entity or a LaTeX command.
2643 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2644 (match-beginning 0))
2645 ;; No entity nor command: point is at a LaTeX fragment.
2646 ;; Determine its type to get the correct beginning position.
2647 (cons 'latex-fragment
2648 (catch 'return
2649 (mapc (lambda (e)
2650 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
2651 (throw 'return
2652 (match-beginning
2653 (nth 2 (assoc e org-latex-regexps))))))
2654 matchers)
2655 (point))))))))
2658 ;;;; Export Snippet
2660 (defun org-element-export-snippet-parser ()
2661 "Parse export snippet at point.
2663 Return a list whose CAR is `export-snippet' and CDR a plist with
2664 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2665 keywords.
2667 Assume point is at the beginning of the snippet."
2668 (save-excursion
2669 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2670 (let* ((begin (match-beginning 0))
2671 (back-end (org-match-string-no-properties 1))
2672 (value (buffer-substring-no-properties
2673 (point)
2674 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2675 (post-blank (skip-chars-forward " \t"))
2676 (end (point)))
2677 (list 'export-snippet
2678 (list :back-end back-end
2679 :value value
2680 :begin begin
2681 :end end
2682 :post-blank post-blank)))))
2684 (defun org-element-export-snippet-interpreter (export-snippet contents)
2685 "Interpret EXPORT-SNIPPET object as Org syntax.
2686 CONTENTS is nil."
2687 (format "@@%s:%s@@"
2688 (org-element-property :back-end export-snippet)
2689 (org-element-property :value export-snippet)))
2691 (defun org-element-export-snippet-successor (limit)
2692 "Search for the next export-snippet object.
2694 LIMIT bounds the search.
2696 Return value is a cons cell whose CAR is `export-snippet' and CDR
2697 its beginning position."
2698 (save-excursion
2699 (let (beg)
2700 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
2701 (setq beg (match-beginning 0))
2702 (search-forward "@@" limit t))
2703 (cons 'export-snippet beg)))))
2706 ;;;; Footnote Reference
2708 (defun org-element-footnote-reference-parser ()
2709 "Parse footnote reference at point.
2711 Return a list whose CAR is `footnote-reference' and CDR a plist
2712 with `:label', `:type', `:inline-definition', `:begin', `:end'
2713 and `:post-blank' as keywords."
2714 (save-excursion
2715 (looking-at org-footnote-re)
2716 (let* ((begin (point))
2717 (label (or (org-match-string-no-properties 2)
2718 (org-match-string-no-properties 3)
2719 (and (match-string 1)
2720 (concat "fn:" (org-match-string-no-properties 1)))))
2721 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2722 (inner-begin (match-end 0))
2723 (inner-end
2724 (let ((count 1))
2725 (forward-char)
2726 (while (and (> count 0) (re-search-forward "[][]" nil t))
2727 (if (equal (match-string 0) "[") (incf count) (decf count)))
2728 (1- (point))))
2729 (post-blank (progn (goto-char (1+ inner-end))
2730 (skip-chars-forward " \t")))
2731 (end (point))
2732 (footnote-reference
2733 (list 'footnote-reference
2734 (list :label label
2735 :type type
2736 :begin begin
2737 :end end
2738 :post-blank post-blank))))
2739 (org-element-put-property
2740 footnote-reference :inline-definition
2741 (and (eq type 'inline)
2742 (org-element-parse-secondary-string
2743 (buffer-substring inner-begin inner-end)
2744 (org-element-restriction 'footnote-reference)
2745 footnote-reference))))))
2747 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2748 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2749 CONTENTS is nil."
2750 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2751 (def
2752 (let ((inline-def
2753 (org-element-property :inline-definition footnote-reference)))
2754 (if (not inline-def) ""
2755 (concat ":" (org-element-interpret-data inline-def))))))
2756 (format "[%s]" (concat label def))))
2758 (defun org-element-footnote-reference-successor (limit)
2759 "Search for the next footnote-reference object.
2761 LIMIT bounds the search.
2763 Return value is a cons cell whose CAR is `footnote-reference' and
2764 CDR is beginning position."
2765 (save-excursion
2766 (catch 'exit
2767 (while (re-search-forward org-footnote-re limit t)
2768 (save-excursion
2769 (let ((beg (match-beginning 0))
2770 (count 1))
2771 (backward-char)
2772 (while (re-search-forward "[][]" limit t)
2773 (if (equal (match-string 0) "[") (incf count) (decf count))
2774 (when (zerop count)
2775 (throw 'exit (cons 'footnote-reference beg))))))))))
2778 ;;;; Inline Babel Call
2780 (defun org-element-inline-babel-call-parser ()
2781 "Parse inline babel call at point.
2783 Return a list whose CAR is `inline-babel-call' and CDR a plist
2784 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2786 Assume point is at the beginning of the babel call."
2787 (save-excursion
2788 (unless (bolp) (backward-char))
2789 (looking-at org-babel-inline-lob-one-liner-regexp)
2790 (let ((info (save-match-data (org-babel-lob-get-info)))
2791 (begin (match-end 1))
2792 (post-blank (progn (goto-char (match-end 0))
2793 (skip-chars-forward " \t")))
2794 (end (point)))
2795 (list 'inline-babel-call
2796 (list :begin begin
2797 :end end
2798 :info info
2799 :post-blank post-blank)))))
2801 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2802 "Interpret INLINE-BABEL-CALL object as Org syntax.
2803 CONTENTS is nil."
2804 (let* ((babel-info (org-element-property :info inline-babel-call))
2805 (main-source (car babel-info))
2806 (post-options (nth 1 babel-info)))
2807 (concat "call_"
2808 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2809 ;; Remove redundant square brackets.
2810 (replace-match
2811 (match-string 1 main-source) nil nil main-source)
2812 main-source)
2813 (and post-options (format "[%s]" post-options)))))
2815 (defun org-element-inline-babel-call-successor (limit)
2816 "Search for the next inline-babel-call object.
2818 LIMIT bounds the search.
2820 Return value is a cons cell whose CAR is `inline-babel-call' and
2821 CDR is beginning position."
2822 (save-excursion
2823 ;; Use a simplified version of
2824 ;; `org-babel-inline-lob-one-liner-regexp'.
2825 (when (re-search-forward
2826 "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
2827 limit t)
2828 (cons 'inline-babel-call (match-beginning 0)))))
2831 ;;;; Inline Src Block
2833 (defun org-element-inline-src-block-parser ()
2834 "Parse inline source block at point.
2836 LIMIT bounds the search.
2838 Return a list whose CAR is `inline-src-block' and CDR a plist
2839 with `:begin', `:end', `:language', `:value', `:parameters' and
2840 `:post-blank' as keywords.
2842 Assume point is at the beginning of the inline src block."
2843 (save-excursion
2844 (unless (bolp) (backward-char))
2845 (looking-at org-babel-inline-src-block-regexp)
2846 (let ((begin (match-beginning 1))
2847 (language (org-match-string-no-properties 2))
2848 (parameters (org-match-string-no-properties 4))
2849 (value (org-match-string-no-properties 5))
2850 (post-blank (progn (goto-char (match-end 0))
2851 (skip-chars-forward " \t")))
2852 (end (point)))
2853 (list 'inline-src-block
2854 (list :language language
2855 :value value
2856 :parameters parameters
2857 :begin begin
2858 :end end
2859 :post-blank post-blank)))))
2861 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2862 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2863 CONTENTS is nil."
2864 (let ((language (org-element-property :language inline-src-block))
2865 (arguments (org-element-property :parameters inline-src-block))
2866 (body (org-element-property :value inline-src-block)))
2867 (format "src_%s%s{%s}"
2868 language
2869 (if arguments (format "[%s]" arguments) "")
2870 body)))
2872 (defun org-element-inline-src-block-successor (limit)
2873 "Search for the next inline-babel-call element.
2875 LIMIT bounds the search.
2877 Return value is a cons cell whose CAR is `inline-babel-call' and
2878 CDR is beginning position."
2879 (save-excursion
2880 (unless (bolp) (backward-char))
2881 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
2882 (cons 'inline-src-block (match-beginning 1)))))
2884 ;;;; Italic
2886 (defun org-element-italic-parser ()
2887 "Parse italic object at point.
2889 Return a list whose CAR is `italic' and CDR is a plist with
2890 `:begin', `:end', `:contents-begin' and `:contents-end' and
2891 `:post-blank' keywords.
2893 Assume point is at the first slash marker."
2894 (save-excursion
2895 (unless (bolp) (backward-char 1))
2896 (looking-at org-emph-re)
2897 (let ((begin (match-beginning 2))
2898 (contents-begin (match-beginning 4))
2899 (contents-end (match-end 4))
2900 (post-blank (progn (goto-char (match-end 2))
2901 (skip-chars-forward " \t")))
2902 (end (point)))
2903 (list 'italic
2904 (list :begin begin
2905 :end end
2906 :contents-begin contents-begin
2907 :contents-end contents-end
2908 :post-blank post-blank)))))
2910 (defun org-element-italic-interpreter (italic contents)
2911 "Interpret ITALIC object as Org syntax.
2912 CONTENTS is the contents of the object."
2913 (format "/%s/" contents))
2916 ;;;; Latex Fragment
2918 (defun org-element-latex-fragment-parser ()
2919 "Parse latex fragment at point.
2921 Return a list whose CAR is `latex-fragment' and CDR a plist with
2922 `:value', `:begin', `:end', and `:post-blank' as keywords.
2924 Assume point is at the beginning of the latex fragment."
2925 (save-excursion
2926 (let* ((begin (point))
2927 (substring-match
2928 (catch 'exit
2929 (mapc (lambda (e)
2930 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
2931 (when (or (looking-at latex-regexp)
2932 (and (not (bobp))
2933 (save-excursion
2934 (backward-char)
2935 (looking-at latex-regexp))))
2936 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
2937 (plist-get org-format-latex-options :matchers))
2938 ;; None found: it's a macro.
2939 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2941 (value (match-string-no-properties substring-match))
2942 (post-blank (progn (goto-char (match-end substring-match))
2943 (skip-chars-forward " \t")))
2944 (end (point)))
2945 (list 'latex-fragment
2946 (list :value value
2947 :begin begin
2948 :end end
2949 :post-blank post-blank)))))
2951 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
2952 "Interpret LATEX-FRAGMENT object as Org syntax.
2953 CONTENTS is nil."
2954 (org-element-property :value latex-fragment))
2956 ;;;; Line Break
2958 (defun org-element-line-break-parser ()
2959 "Parse line break at point.
2961 Return a list whose CAR is `line-break', and CDR a plist with
2962 `:begin', `:end' and `:post-blank' keywords.
2964 Assume point is at the beginning of the line break."
2965 (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
2967 (defun org-element-line-break-interpreter (line-break contents)
2968 "Interpret LINE-BREAK object as Org syntax.
2969 CONTENTS is nil."
2970 "\\\\")
2972 (defun org-element-line-break-successor (limit)
2973 "Search for the next line-break object.
2975 LIMIT bounds the search.
2977 Return value is a cons cell whose CAR is `line-break' and CDR is
2978 beginning position."
2979 (save-excursion
2980 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
2981 (goto-char (match-beginning 1)))))
2982 ;; A line break can only happen on a non-empty line.
2983 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
2984 (cons 'line-break beg)))))
2987 ;;;; Link
2989 (defun org-element-link-parser ()
2990 "Parse link at point.
2992 Return a list whose CAR is `link' and CDR a plist with `:type',
2993 `:path', `:raw-link', `:application', `:search-option', `:begin',
2994 `:end', `:contents-begin', `:contents-end' and `:post-blank' as
2995 keywords.
2997 Assume point is at the beginning of the link."
2998 (save-excursion
2999 (let ((begin (point))
3000 end contents-begin contents-end link-end post-blank path type
3001 raw-link link search-option application)
3002 (cond
3003 ;; Type 1: Text targeted from a radio target.
3004 ((and org-target-link-regexp (looking-at org-target-link-regexp))
3005 (setq type "radio"
3006 link-end (match-end 0)
3007 path (org-match-string-no-properties 0)))
3008 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
3009 ((looking-at org-bracket-link-regexp)
3010 (setq contents-begin (match-beginning 3)
3011 contents-end (match-end 3)
3012 link-end (match-end 0)
3013 ;; RAW-LINK is the original link.
3014 raw-link (org-match-string-no-properties 1)
3015 link (org-translate-link
3016 (org-link-expand-abbrev
3017 (org-link-unescape raw-link))))
3018 ;; Determine TYPE of link and set PATH accordingly.
3019 (cond
3020 ;; File type.
3021 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
3022 (setq type "file" path link))
3023 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
3024 ((string-match org-link-re-with-space3 link)
3025 (setq type (match-string 1 link) path (match-string 2 link)))
3026 ;; Id type: PATH is the id.
3027 ((string-match "^id:\\([-a-f0-9]+\\)" link)
3028 (setq type "id" path (match-string 1 link)))
3029 ;; Code-ref type: PATH is the name of the reference.
3030 ((string-match "^(\\(.*\\))$" link)
3031 (setq type "coderef" path (match-string 1 link)))
3032 ;; Custom-id type: PATH is the name of the custom id.
3033 ((= (aref link 0) ?#)
3034 (setq type "custom-id" path (substring link 1)))
3035 ;; Fuzzy type: Internal link either matches a target, an
3036 ;; headline name or nothing. PATH is the target or
3037 ;; headline's name.
3038 (t (setq type "fuzzy" path link))))
3039 ;; Type 3: Plain link, i.e. http://orgmode.org
3040 ((looking-at org-plain-link-re)
3041 (setq raw-link (org-match-string-no-properties 0)
3042 type (org-match-string-no-properties 1)
3043 path (org-match-string-no-properties 2)
3044 link-end (match-end 0)))
3045 ;; Type 4: Angular link, i.e. <http://orgmode.org>
3046 ((looking-at org-angle-link-re)
3047 (setq raw-link (buffer-substring-no-properties
3048 (match-beginning 1) (match-end 2))
3049 type (org-match-string-no-properties 1)
3050 path (org-match-string-no-properties 2)
3051 link-end (match-end 0))))
3052 ;; In any case, deduce end point after trailing white space from
3053 ;; LINK-END variable.
3054 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
3055 end (point))
3056 ;; Extract search option and opening application out of
3057 ;; "file"-type links.
3058 (when (member type org-element-link-type-is-file)
3059 ;; Application.
3060 (cond ((string-match "^file\\+\\(.*\\)$" type)
3061 (setq application (match-string 1 type)))
3062 ((not (string-match "^file" type))
3063 (setq application type)))
3064 ;; Extract search option from PATH.
3065 (when (string-match "::\\(.*\\)$" path)
3066 (setq search-option (match-string 1 path)
3067 path (replace-match "" nil nil path)))
3068 ;; Make sure TYPE always report "file".
3069 (setq type "file"))
3070 (list 'link
3071 (list :type type
3072 :path path
3073 :raw-link (or raw-link path)
3074 :application application
3075 :search-option search-option
3076 :begin begin
3077 :end end
3078 :contents-begin contents-begin
3079 :contents-end contents-end
3080 :post-blank post-blank)))))
3082 (defun org-element-link-interpreter (link contents)
3083 "Interpret LINK object as Org syntax.
3084 CONTENTS is the contents of the object, or nil."
3085 (let ((type (org-element-property :type link))
3086 (raw-link (org-element-property :raw-link link)))
3087 (if (string= type "radio") raw-link
3088 (format "[[%s]%s]"
3089 raw-link
3090 (if contents (format "[%s]" contents) "")))))
3092 (defun org-element-link-successor (limit)
3093 "Search for the next link object.
3095 LIMIT bounds the search.
3097 Return value is a cons cell whose CAR is `link' and CDR is
3098 beginning position."
3099 (save-excursion
3100 (let ((link-regexp
3101 (if (not org-target-link-regexp) org-any-link-re
3102 (concat org-any-link-re "\\|" org-target-link-regexp))))
3103 (when (re-search-forward link-regexp limit t)
3104 (cons 'link (match-beginning 0))))))
3107 ;;;; Macro
3109 (defun org-element-macro-parser ()
3110 "Parse macro at point.
3112 Return a list whose CAR is `macro' and CDR a plist with `:key',
3113 `:args', `:begin', `:end', `:value' and `:post-blank' as
3114 keywords.
3116 Assume point is at the macro."
3117 (save-excursion
3118 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
3119 (let ((begin (point))
3120 (key (downcase (org-match-string-no-properties 1)))
3121 (value (org-match-string-no-properties 0))
3122 (post-blank (progn (goto-char (match-end 0))
3123 (skip-chars-forward " \t")))
3124 (end (point))
3125 (args (let ((args (org-match-string-no-properties 3)) args2)
3126 (when args
3127 (setq args (org-split-string args ","))
3128 (while args
3129 (while (string-match "\\\\\\'" (car args))
3130 ;; Repair bad splits.
3131 (setcar (cdr args) (concat (substring (car args) 0 -1)
3132 "," (nth 1 args)))
3133 (pop args))
3134 (push (pop args) args2))
3135 (mapcar 'org-trim (nreverse args2))))))
3136 (list 'macro
3137 (list :key key
3138 :value value
3139 :args args
3140 :begin begin
3141 :end end
3142 :post-blank post-blank)))))
3144 (defun org-element-macro-interpreter (macro contents)
3145 "Interpret MACRO object as Org syntax.
3146 CONTENTS is nil."
3147 (org-element-property :value macro))
3149 (defun org-element-macro-successor (limit)
3150 "Search for the next macro object.
3152 LIMIT bounds the search.
3154 Return value is cons cell whose CAR is `macro' and CDR is
3155 beginning position."
3156 (save-excursion
3157 (when (re-search-forward
3158 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
3159 limit t)
3160 (cons 'macro (match-beginning 0)))))
3163 ;;;; Radio-target
3165 (defun org-element-radio-target-parser ()
3166 "Parse radio target at point.
3168 Return a list whose CAR is `radio-target' and CDR a plist with
3169 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
3170 and `:post-blank' as keywords.
3172 Assume point is at the radio target."
3173 (save-excursion
3174 (looking-at org-radio-target-regexp)
3175 (let ((begin (point))
3176 (contents-begin (match-beginning 1))
3177 (contents-end (match-end 1))
3178 (value (org-match-string-no-properties 1))
3179 (post-blank (progn (goto-char (match-end 0))
3180 (skip-chars-forward " \t")))
3181 (end (point)))
3182 (list 'radio-target
3183 (list :begin begin
3184 :end end
3185 :contents-begin contents-begin
3186 :contents-end contents-end
3187 :post-blank post-blank
3188 :value value)))))
3190 (defun org-element-radio-target-interpreter (target contents)
3191 "Interpret TARGET object as Org syntax.
3192 CONTENTS is the contents of the object."
3193 (concat "<<<" contents ">>>"))
3195 (defun org-element-radio-target-successor (limit)
3196 "Search for the next radio-target object.
3198 LIMIT bounds the search.
3200 Return value is a cons cell whose CAR is `radio-target' and CDR
3201 is beginning position."
3202 (save-excursion
3203 (when (re-search-forward org-radio-target-regexp limit t)
3204 (cons 'radio-target (match-beginning 0)))))
3207 ;;;; Statistics Cookie
3209 (defun org-element-statistics-cookie-parser ()
3210 "Parse statistics cookie at point.
3212 Return a list whose CAR is `statistics-cookie', and CDR a plist
3213 with `:begin', `:end', `:value' and `:post-blank' keywords.
3215 Assume point is at the beginning of the statistics-cookie."
3216 (save-excursion
3217 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
3218 (let* ((begin (point))
3219 (value (buffer-substring-no-properties
3220 (match-beginning 0) (match-end 0)))
3221 (post-blank (progn (goto-char (match-end 0))
3222 (skip-chars-forward " \t")))
3223 (end (point)))
3224 (list 'statistics-cookie
3225 (list :begin begin
3226 :end end
3227 :value value
3228 :post-blank post-blank)))))
3230 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
3231 "Interpret STATISTICS-COOKIE object as Org syntax.
3232 CONTENTS is nil."
3233 (org-element-property :value statistics-cookie))
3235 (defun org-element-statistics-cookie-successor (limit)
3236 "Search for the next statistics cookie object.
3238 LIMIT bounds the search.
3240 Return value is a cons cell whose CAR is `statistics-cookie' and
3241 CDR is beginning position."
3242 (save-excursion
3243 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
3244 (cons 'statistics-cookie (match-beginning 0)))))
3247 ;;;; Strike-Through
3249 (defun org-element-strike-through-parser ()
3250 "Parse strike-through object at point.
3252 Return a list whose CAR is `strike-through' and CDR is a plist
3253 with `:begin', `:end', `:contents-begin' and `:contents-end' and
3254 `:post-blank' keywords.
3256 Assume point is at the first plus sign marker."
3257 (save-excursion
3258 (unless (bolp) (backward-char 1))
3259 (looking-at org-emph-re)
3260 (let ((begin (match-beginning 2))
3261 (contents-begin (match-beginning 4))
3262 (contents-end (match-end 4))
3263 (post-blank (progn (goto-char (match-end 2))
3264 (skip-chars-forward " \t")))
3265 (end (point)))
3266 (list 'strike-through
3267 (list :begin begin
3268 :end end
3269 :contents-begin contents-begin
3270 :contents-end contents-end
3271 :post-blank post-blank)))))
3273 (defun org-element-strike-through-interpreter (strike-through contents)
3274 "Interpret STRIKE-THROUGH object as Org syntax.
3275 CONTENTS is the contents of the object."
3276 (format "+%s+" contents))
3279 ;;;; Subscript
3281 (defun org-element-subscript-parser ()
3282 "Parse subscript at point.
3284 Return a list whose CAR is `subscript' and CDR a plist with
3285 `:begin', `:end', `:contents-begin', `:contents-end',
3286 `:use-brackets-p' and `:post-blank' as keywords.
3288 Assume point is at the underscore."
3289 (save-excursion
3290 (unless (bolp) (backward-char))
3291 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
3293 (not (looking-at org-match-substring-regexp))))
3294 (begin (match-beginning 2))
3295 (contents-begin (or (match-beginning 5)
3296 (match-beginning 3)))
3297 (contents-end (or (match-end 5) (match-end 3)))
3298 (post-blank (progn (goto-char (match-end 0))
3299 (skip-chars-forward " \t")))
3300 (end (point)))
3301 (list 'subscript
3302 (list :begin begin
3303 :end end
3304 :use-brackets-p bracketsp
3305 :contents-begin contents-begin
3306 :contents-end contents-end
3307 :post-blank post-blank)))))
3309 (defun org-element-subscript-interpreter (subscript contents)
3310 "Interpret SUBSCRIPT object as Org syntax.
3311 CONTENTS is the contents of the object."
3312 (format
3313 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
3314 contents))
3316 (defun org-element-sub/superscript-successor (limit)
3317 "Search for the next sub/superscript object.
3319 LIMIT bounds the search.
3321 Return value is a cons cell whose CAR is either `subscript' or
3322 `superscript' and CDR is beginning position."
3323 (save-excursion
3324 (unless (bolp) (backward-char))
3325 (when (re-search-forward org-match-substring-regexp limit t)
3326 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
3327 (match-beginning 2)))))
3330 ;;;; Superscript
3332 (defun org-element-superscript-parser ()
3333 "Parse superscript at point.
3335 Return a list whose CAR is `superscript' and CDR a plist with
3336 `:begin', `:end', `:contents-begin', `:contents-end',
3337 `:use-brackets-p' and `:post-blank' as keywords.
3339 Assume point is at the caret."
3340 (save-excursion
3341 (unless (bolp) (backward-char))
3342 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
3343 (not (looking-at org-match-substring-regexp))))
3344 (begin (match-beginning 2))
3345 (contents-begin (or (match-beginning 5)
3346 (match-beginning 3)))
3347 (contents-end (or (match-end 5) (match-end 3)))
3348 (post-blank (progn (goto-char (match-end 0))
3349 (skip-chars-forward " \t")))
3350 (end (point)))
3351 (list 'superscript
3352 (list :begin begin
3353 :end end
3354 :use-brackets-p bracketsp
3355 :contents-begin contents-begin
3356 :contents-end contents-end
3357 :post-blank post-blank)))))
3359 (defun org-element-superscript-interpreter (superscript contents)
3360 "Interpret SUPERSCRIPT object as Org syntax.
3361 CONTENTS is the contents of the object."
3362 (format
3363 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
3364 contents))
3367 ;;;; Table Cell
3369 (defun org-element-table-cell-parser ()
3370 "Parse table cell at point.
3372 Return a list whose CAR is `table-cell' and CDR is a plist
3373 containing `:begin', `:end', `:contents-begin', `:contents-end'
3374 and `:post-blank' keywords."
3375 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
3376 (let* ((begin (match-beginning 0))
3377 (end (match-end 0))
3378 (contents-begin (match-beginning 1))
3379 (contents-end (match-end 1)))
3380 (list 'table-cell
3381 (list :begin begin
3382 :end end
3383 :contents-begin contents-begin
3384 :contents-end contents-end
3385 :post-blank 0))))
3387 (defun org-element-table-cell-interpreter (table-cell contents)
3388 "Interpret TABLE-CELL element as Org syntax.
3389 CONTENTS is the contents of the cell, or nil."
3390 (concat " " contents " |"))
3392 (defun org-element-table-cell-successor (limit)
3393 "Search for the next table-cell object.
3395 LIMIT bounds the search.
3397 Return value is a cons cell whose CAR is `table-cell' and CDR is
3398 beginning position."
3399 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
3402 ;;;; Target
3404 (defun org-element-target-parser ()
3405 "Parse target at point.
3407 Return a list whose CAR is `target' and CDR a plist with
3408 `:begin', `:end', `:value' and `:post-blank' as keywords.
3410 Assume point is at the target."
3411 (save-excursion
3412 (looking-at org-target-regexp)
3413 (let ((begin (point))
3414 (value (org-match-string-no-properties 1))
3415 (post-blank (progn (goto-char (match-end 0))
3416 (skip-chars-forward " \t")))
3417 (end (point)))
3418 (list 'target
3419 (list :begin begin
3420 :end end
3421 :value value
3422 :post-blank post-blank)))))
3424 (defun org-element-target-interpreter (target contents)
3425 "Interpret TARGET object as Org syntax.
3426 CONTENTS is nil."
3427 (format "<<%s>>" (org-element-property :value target)))
3429 (defun org-element-target-successor (limit)
3430 "Search for the next target object.
3432 LIMIT bounds the search.
3434 Return value is a cons cell whose CAR is `target' and CDR is
3435 beginning position."
3436 (save-excursion
3437 (when (re-search-forward org-target-regexp limit t)
3438 (cons 'target (match-beginning 0)))))
3441 ;;;; Timestamp
3443 (defun org-element-timestamp-parser ()
3444 "Parse time stamp at point.
3446 Return a list whose CAR is `timestamp', and CDR a plist with
3447 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3449 Assume point is at the beginning of the timestamp."
3450 (save-excursion
3451 (let* ((begin (point))
3452 (activep (eq (char-after) ?<))
3453 (raw-value
3454 (progn
3455 (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
3456 (match-string-no-properties 0)))
3457 (date-start (match-string-no-properties 1))
3458 (date-end (match-string 3))
3459 (diaryp (match-beginning 2))
3460 (type (cond (diaryp 'diary)
3461 ((and activep date-end) 'active-range)
3462 (activep 'active)
3463 (date-end 'inactive-range)
3464 (t 'inactive)))
3465 (post-blank (progn (goto-char (match-end 0))
3466 (skip-chars-forward " \t")))
3467 (end (point))
3468 (with-time-p (string-match "[012]?[0-9]:[0-5][0-9]" date-start))
3469 (repeater-props
3470 (and (not diaryp)
3471 (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)>"
3472 raw-value)
3473 (list
3474 :repeater-type
3475 (let ((type (match-string 1 raw-value)))
3476 (cond ((equal "++" type) 'catch-up)
3477 ((equal ".+" type) 'restart)
3478 (t 'cumulate)))
3479 :repeater-value (string-to-number (match-string 2 raw-value))
3480 :repeater-unit
3481 (case (string-to-char (match-string 3 raw-value))
3482 (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
3483 time-range year-start month-start day-start hour-start minute-start
3484 year-end month-end day-end hour-end minute-end)
3485 ;; Extract time range, if any, and remove it from date start.
3486 (setq time-range
3487 (and (not diaryp)
3488 (string-match
3489 "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
3490 date-start)
3491 (cons (string-to-number (match-string 2 date-start))
3492 (string-to-number (match-string 3 date-start)))))
3493 (when time-range
3494 (setq date-start (replace-match "" nil nil date-start 1)))
3495 ;; Parse date-start.
3496 (unless diaryp
3497 (let ((date (org-parse-time-string date-start)))
3498 (setq year-start (nth 5 date)
3499 month-start (nth 4 date)
3500 day-start (nth 3 date)
3501 hour-start (and with-time-p (nth 2 date))
3502 minute-start (and with-time-p (nth 1 date)))))
3503 ;; Compute date-end. It can be provided directly in time-stamp,
3504 ;; or extracted from time range. Otherwise, it defaults to the
3505 ;; same values as date-start.
3506 (unless diaryp
3507 (let ((date (and date-end (org-parse-time-string date-end))))
3508 (setq year-end (or (nth 5 date) year-start)
3509 month-end (or (nth 4 date) month-start)
3510 day-end (or (nth 3 date) day-start)
3511 hour-end (or (nth 2 date) (car time-range) hour-start)
3512 minute-end (or (nth 1 date) (cdr time-range) minute-start))))
3513 (list 'timestamp
3514 (nconc (list :type type
3515 :raw-value raw-value
3516 :year-start year-start
3517 :month-start month-start
3518 :day-start day-start
3519 :hour-start hour-start
3520 :minute-start minute-start
3521 :year-end year-end
3522 :month-end month-end
3523 :day-end day-end
3524 :hour-end hour-end
3525 :minute-end minute-end
3526 :begin begin
3527 :end end
3528 :post-blank post-blank)
3529 repeater-props)))))
3531 (defun org-element-timestamp-interpreter (timestamp contents)
3532 "Interpret TIMESTAMP object as Org syntax.
3533 CONTENTS is nil."
3534 ;; Use `:raw-value' if specified.
3535 (or (org-element-property :raw-value timestamp)
3536 ;; Otherwise, build timestamp string.
3537 (let ((build-ts-string
3538 ;; Build an Org timestamp string from TIME. ACTIVEP is
3539 ;; non-nil when time stamp is active. If WITH-TIME-P is
3540 ;; non-nil, add a time part. HOUR-END and MINUTE-END
3541 ;; specify a time range in the timestamp. REPEAT-STRING
3542 ;; is the repeater string, if any.
3543 (lambda (time activep
3544 &optional with-time-p hour-end minute-end repeat-string)
3545 (let ((ts (format-time-string
3546 (funcall (if with-time-p 'cdr 'car)
3547 org-time-stamp-formats)
3548 time)))
3549 (when (and hour-end minute-end)
3550 (string-match "[012]?[0-9]:[0-5][0-9]" ts)
3551 (setq ts
3552 (replace-match
3553 (format "\\&-%02d:%02d" hour-end minute-end)
3554 nil nil ts)))
3555 (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
3556 (when (org-string-nw-p repeat-string)
3557 (setq ts (concat (substring ts 0 -1)
3559 repeat-string
3560 (substring ts -1))))
3561 ;; Return value.
3562 ts)))
3563 (type (org-element-property :type timestamp)))
3564 (case type
3565 ((active inactive)
3566 (let* ((minute-start (org-element-property :minute-start timestamp))
3567 (minute-end (org-element-property :minute-end timestamp))
3568 (hour-start (org-element-property :hour-start timestamp))
3569 (hour-end (org-element-property :hour-end timestamp))
3570 (time-range-p (and hour-start hour-end minute-start minute-end
3571 (or (/= hour-start hour-end)
3572 (/= minute-start minute-end)))))
3573 (funcall
3574 build-ts-string
3575 (encode-time 0
3576 (or minute-start 0)
3577 (or hour-start 0)
3578 (org-element-property :day-start timestamp)
3579 (org-element-property :month-start timestamp)
3580 (org-element-property :year-start timestamp))
3581 (eq type 'active)
3582 (and hour-start minute-start)
3583 (and time-range-p hour-end)
3584 (and time-range-p minute-end)
3585 (concat (case (org-element-property :repeater-type timestamp)
3586 (cumulate "+") (catch-up "++") (restart ".+"))
3587 (org-element-property :repeater-value timestamp)
3588 (org-element-property :repeater-unit timestamp)))))
3589 ((active-range inactive-range)
3590 (let ((minute-start (org-element-property :minute-start timestamp))
3591 (minute-end (org-element-property :minute-end timestamp))
3592 (hour-start (org-element-property :hour-start timestamp))
3593 (hour-end (org-element-property :hour-end timestamp)))
3594 (concat
3595 (funcall
3596 build-ts-string (encode-time
3598 (or minute-start 0)
3599 (or hour-start 0)
3600 (org-element-property :day-start timestamp)
3601 (org-element-property :month-start timestamp)
3602 (org-element-property :year-start timestamp))
3603 (eq type 'active-range)
3604 (and hour-start minute-start))
3605 "--"
3606 (funcall build-ts-string
3607 (encode-time 0
3608 (or minute-end 0)
3609 (or hour-end 0)
3610 (org-element-property :day-end timestamp)
3611 (org-element-property :month-end timestamp)
3612 (org-element-property :year-end timestamp))
3613 (eq type 'active-range)
3614 (and hour-end minute-end)))))))))
3616 (defun org-element-timestamp-successor (limit)
3617 "Search for the next timestamp object.
3619 LIMIT bounds the search.
3621 Return value is a cons cell whose CAR is `timestamp' and CDR is
3622 beginning position."
3623 (save-excursion
3624 (when (re-search-forward
3625 (concat org-ts-regexp-both
3626 "\\|"
3627 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3628 "\\|"
3629 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3630 limit t)
3631 (cons 'timestamp (match-beginning 0)))))
3634 ;;;; Underline
3636 (defun org-element-underline-parser ()
3637 "Parse underline object at point.
3639 Return a list whose CAR is `underline' and CDR is a plist with
3640 `:begin', `:end', `:contents-begin' and `:contents-end' and
3641 `:post-blank' keywords.
3643 Assume point is at the first underscore marker."
3644 (save-excursion
3645 (unless (bolp) (backward-char 1))
3646 (looking-at org-emph-re)
3647 (let ((begin (match-beginning 2))
3648 (contents-begin (match-beginning 4))
3649 (contents-end (match-end 4))
3650 (post-blank (progn (goto-char (match-end 2))
3651 (skip-chars-forward " \t")))
3652 (end (point)))
3653 (list 'underline
3654 (list :begin begin
3655 :end end
3656 :contents-begin contents-begin
3657 :contents-end contents-end
3658 :post-blank post-blank)))))
3660 (defun org-element-underline-interpreter (underline contents)
3661 "Interpret UNDERLINE object as Org syntax.
3662 CONTENTS is the contents of the object."
3663 (format "_%s_" contents))
3666 ;;;; Verbatim
3668 (defun org-element-verbatim-parser ()
3669 "Parse verbatim object at point.
3671 Return a list whose CAR is `verbatim' and CDR is a plist with
3672 `:value', `:begin', `:end' and `:post-blank' keywords.
3674 Assume point is at the first equal sign marker."
3675 (save-excursion
3676 (unless (bolp) (backward-char 1))
3677 (looking-at org-emph-re)
3678 (let ((begin (match-beginning 2))
3679 (value (org-match-string-no-properties 4))
3680 (post-blank (progn (goto-char (match-end 2))
3681 (skip-chars-forward " \t")))
3682 (end (point)))
3683 (list 'verbatim
3684 (list :value value
3685 :begin begin
3686 :end end
3687 :post-blank post-blank)))))
3689 (defun org-element-verbatim-interpreter (verbatim contents)
3690 "Interpret VERBATIM object as Org syntax.
3691 CONTENTS is nil."
3692 (format "=%s=" (org-element-property :value verbatim)))
3696 ;;; Parsing Element Starting At Point
3698 ;; `org-element--current-element' is the core function of this section.
3699 ;; It returns the Lisp representation of the element starting at
3700 ;; point.
3702 ;; `org-element--current-element' makes use of special modes. They
3703 ;; are activated for fixed element chaining (i.e. `plain-list' >
3704 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3705 ;; `section'). Special modes are: `first-section', `item',
3706 ;; `node-property', `quote-section', `section' and `table-row'.
3708 (defun org-element--current-element
3709 (limit &optional granularity special structure)
3710 "Parse the element starting at point.
3712 LIMIT bounds the search.
3714 Return value is a list like (TYPE PROPS) where TYPE is the type
3715 of the element and PROPS a plist of properties associated to the
3716 element.
3718 Possible types are defined in `org-element-all-elements'.
3720 Optional argument GRANULARITY determines the depth of the
3721 recursion. Allowed values are `headline', `greater-element',
3722 `element', `object' or nil. When it is broader than `object' (or
3723 nil), secondary values will not be parsed, since they only
3724 contain objects.
3726 Optional argument SPECIAL, when non-nil, can be either
3727 `first-section', `item', `node-property', `quote-section',
3728 `section', and `table-row'.
3730 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3731 be computed.
3733 This function assumes point is always at the beginning of the
3734 element it has to parse."
3735 (save-excursion
3736 (let ((case-fold-search t)
3737 ;; Determine if parsing depth allows for secondary strings
3738 ;; parsing. It only applies to elements referenced in
3739 ;; `org-element-secondary-value-alist'.
3740 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3741 (cond
3742 ;; Item.
3743 ((eq special 'item)
3744 (org-element-item-parser limit structure raw-secondary-p))
3745 ;; Table Row.
3746 ((eq special 'table-row) (org-element-table-row-parser limit))
3747 ;; Node Property.
3748 ((eq special 'node-property) (org-element-node-property-parser limit))
3749 ;; Headline.
3750 ((org-with-limited-levels (org-at-heading-p))
3751 (org-element-headline-parser limit raw-secondary-p))
3752 ;; Sections (must be checked after headline).
3753 ((eq special 'section) (org-element-section-parser limit))
3754 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3755 ((eq special 'first-section)
3756 (org-element-section-parser
3757 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3758 limit)))
3759 ;; When not at bol, point is at the beginning of an item or
3760 ;; a footnote definition: next item is always a paragraph.
3761 ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
3762 ;; Planning and Clock.
3763 ((looking-at org-planning-or-clock-line-re)
3764 (if (equal (match-string 1) org-clock-string)
3765 (org-element-clock-parser limit)
3766 (org-element-planning-parser limit)))
3767 ;; Inlinetask.
3768 ((org-at-heading-p)
3769 (org-element-inlinetask-parser limit raw-secondary-p))
3770 ;; From there, elements can have affiliated keywords.
3771 (t (let ((affiliated (org-element--collect-affiliated-keywords)))
3772 (cond
3773 ;; LaTeX Environment.
3774 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}[ \t]*$")
3775 (org-element-latex-environment-parser limit affiliated))
3776 ;; Drawer and Property Drawer.
3777 ((looking-at org-drawer-regexp)
3778 (if (equal (match-string 1) "PROPERTIES")
3779 (org-element-property-drawer-parser limit affiliated)
3780 (org-element-drawer-parser limit affiliated)))
3781 ;; Fixed Width
3782 ((looking-at "[ \t]*:\\( \\|$\\)")
3783 (org-element-fixed-width-parser limit affiliated))
3784 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3785 ;; Keywords.
3786 ((looking-at "[ \t]*#")
3787 (goto-char (match-end 0))
3788 (cond ((looking-at "\\(?: \\|$\\)")
3789 (beginning-of-line)
3790 (org-element-comment-parser limit affiliated))
3791 ((looking-at "\\+BEGIN_\\(\\S-+\\)")
3792 (beginning-of-line)
3793 (let ((parser (assoc (upcase (match-string 1))
3794 org-element-block-name-alist)))
3795 (if parser (funcall (cdr parser) limit affiliated)
3796 (org-element-special-block-parser limit affiliated))))
3797 ((looking-at "\\+CALL:")
3798 (beginning-of-line)
3799 (org-element-babel-call-parser limit affiliated))
3800 ((looking-at "\\+BEGIN:? ")
3801 (beginning-of-line)
3802 (org-element-dynamic-block-parser limit affiliated))
3803 ((looking-at "\\+\\S-+:")
3804 (beginning-of-line)
3805 (org-element-keyword-parser limit affiliated))
3807 (beginning-of-line)
3808 (org-element-paragraph-parser limit affiliated))))
3809 ;; Footnote Definition.
3810 ((looking-at org-footnote-definition-re)
3811 (org-element-footnote-definition-parser limit affiliated))
3812 ;; Horizontal Rule.
3813 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3814 (org-element-horizontal-rule-parser limit affiliated))
3815 ;; Diary Sexp.
3816 ((looking-at "%%(")
3817 (org-element-diary-sexp-parser limit affiliated))
3818 ;; Table.
3819 ((org-at-table-p t) (org-element-table-parser limit affiliated))
3820 ;; List.
3821 ((looking-at (org-item-re))
3822 (org-element-plain-list-parser
3823 limit affiliated (or structure (org-list-struct))))
3824 ;; Default element: Paragraph.
3825 (t (org-element-paragraph-parser limit affiliated)))))))))
3828 ;; Most elements can have affiliated keywords. When looking for an
3829 ;; element beginning, we want to move before them, as they belong to
3830 ;; that element, and, in the meantime, collect information they give
3831 ;; into appropriate properties. Hence the following function.
3833 (defun org-element--collect-affiliated-keywords ()
3834 "Collect affiliated keywords from point.
3836 Return a list whose CAR is the position at the first of them and
3837 CDR a plist of keywords and values and move point to the
3838 beginning of the first line after them.
3840 As a special case, if element doesn't start at the beginning of
3841 the line (i.e. a paragraph starting an item), CAR is current
3842 position of point and CDR is nil."
3843 (if (not (bolp)) (list (point))
3844 (let ((case-fold-search t)
3845 (origin (point))
3846 ;; RESTRICT is the list of objects allowed in parsed
3847 ;; keywords value.
3848 (restrict (org-element-restriction 'keyword))
3849 output)
3850 (while (and (not (eobp)) (looking-at org-element--affiliated-re))
3851 (let* ((raw-kwd (upcase (match-string 1)))
3852 ;; Apply translation to RAW-KWD. From there, KWD is
3853 ;; the official keyword.
3854 (kwd (or (cdr (assoc raw-kwd
3855 org-element-keyword-translation-alist))
3856 raw-kwd))
3857 ;; Find main value for any keyword.
3858 (value
3859 (save-match-data
3860 (org-trim
3861 (buffer-substring-no-properties
3862 (match-end 0) (point-at-eol)))))
3863 ;; PARSEDP is non-nil when keyword should have its
3864 ;; value parsed.
3865 (parsedp (member kwd org-element-parsed-keywords))
3866 ;; If KWD is a dual keyword, find its secondary
3867 ;; value. Maybe parse it.
3868 (dualp (member kwd org-element-dual-keywords))
3869 (dual-value
3870 (and dualp
3871 (let ((sec (org-match-string-no-properties 2)))
3872 (if (or (not sec) (not parsedp)) sec
3873 (org-element-parse-secondary-string sec restrict)))))
3874 ;; Attribute a property name to KWD.
3875 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3876 ;; Now set final shape for VALUE.
3877 (when parsedp
3878 (setq value (org-element-parse-secondary-string value restrict)))
3879 (when dualp
3880 (setq value (and (or value dual-value) (cons value dual-value))))
3881 (when (or (member kwd org-element-multiple-keywords)
3882 ;; Attributes can always appear on multiple lines.
3883 (string-match "^ATTR_" kwd))
3884 (setq value (cons value (plist-get output kwd-sym))))
3885 ;; Eventually store the new value in OUTPUT.
3886 (setq output (plist-put output kwd-sym value))
3887 ;; Move to next keyword.
3888 (forward-line)))
3889 ;; If affiliated keywords are orphaned: move back to first one.
3890 ;; They will be parsed as a paragraph.
3891 (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
3892 ;; Return value.
3893 (cons origin output))))
3897 ;;; The Org Parser
3899 ;; The two major functions here are `org-element-parse-buffer', which
3900 ;; parses Org syntax inside the current buffer, taking into account
3901 ;; region, narrowing, or even visibility if specified, and
3902 ;; `org-element-parse-secondary-string', which parses objects within
3903 ;; a given string.
3905 ;; The (almost) almighty `org-element-map' allows to apply a function
3906 ;; on elements or objects matching some type, and accumulate the
3907 ;; resulting values. In an export situation, it also skips unneeded
3908 ;; parts of the parse tree.
3910 (defun org-element-parse-buffer (&optional granularity visible-only)
3911 "Recursively parse the buffer and return structure.
3912 If narrowing is in effect, only parse the visible part of the
3913 buffer.
3915 Optional argument GRANULARITY determines the depth of the
3916 recursion. It can be set to the following symbols:
3918 `headline' Only parse headlines.
3919 `greater-element' Don't recurse into greater elements excepted
3920 headlines and sections. Thus, elements
3921 parsed are the top-level ones.
3922 `element' Parse everything but objects and plain text.
3923 `object' Parse the complete buffer (default).
3925 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3926 elements.
3928 Assume buffer is in Org mode."
3929 (save-excursion
3930 (goto-char (point-min))
3931 (org-skip-whitespace)
3932 (org-element--parse-elements
3933 (point-at-bol) (point-max)
3934 ;; Start in `first-section' mode so text before the first
3935 ;; headline belongs to a section.
3936 'first-section nil granularity visible-only (list 'org-data nil))))
3938 (defun org-element-parse-secondary-string (string restriction &optional parent)
3939 "Recursively parse objects in STRING and return structure.
3941 RESTRICTION is a symbol limiting the object types that will be
3942 looked after.
3944 Optional argument PARENT, when non-nil, is the element or object
3945 containing the secondary string. It is used to set correctly
3946 `:parent' property within the string."
3947 (with-temp-buffer
3948 (insert string)
3949 (let ((secondary (org-element--parse-objects
3950 (point-min) (point-max) nil restriction)))
3951 (when parent
3952 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
3953 secondary))
3954 secondary)))
3956 (defun org-element-map
3957 (data types fun &optional info first-match no-recursion with-affiliated)
3958 "Map a function on selected elements or objects.
3960 DATA is the parsed tree, as returned by, i.e,
3961 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3962 of elements or objects types. FUN is the function called on the
3963 matching element or object. It must accept one arguments: the
3964 element or object itself.
3966 When optional argument INFO is non-nil, it should be a plist
3967 holding export options. In that case, parts of the parse tree
3968 not exportable according to that property list will be skipped.
3970 When optional argument FIRST-MATCH is non-nil, stop at the first
3971 match for which FUN doesn't return nil, and return that value.
3973 Optional argument NO-RECURSION is a symbol or a list of symbols
3974 representing elements or objects types. `org-element-map' won't
3975 enter any recursive element or object whose type belongs to that
3976 list. Though, FUN can still be applied on them.
3978 When optional argument WITH-AFFILIATED is non-nil, also move into
3979 affiliated keywords to find objects.
3981 Nil values returned from FUN do not appear in the results."
3982 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3983 (unless (listp types) (setq types (list types)))
3984 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
3985 ;; Recursion depth is determined by --CATEGORY.
3986 (let* ((--category
3987 (catch 'found
3988 (let ((category 'greater-elements))
3989 (mapc (lambda (type)
3990 (cond ((or (memq type org-element-all-objects)
3991 (eq type 'plain-text))
3992 ;; If one object is found, the function
3993 ;; has to recurse into every object.
3994 (throw 'found 'objects))
3995 ((not (memq type org-element-greater-elements))
3996 ;; If one regular element is found, the
3997 ;; function has to recurse, at least,
3998 ;; into every element it encounters.
3999 (and (not (eq category 'elements))
4000 (setq category 'elements)))))
4001 types)
4002 category)))
4003 ;; Compute properties for affiliated keywords if necessary.
4004 (--affiliated-alist
4005 (and with-affiliated
4006 (mapcar (lambda (kwd)
4007 (cons kwd (intern (concat ":" (downcase kwd)))))
4008 org-element-affiliated-keywords)))
4009 --acc
4010 --walk-tree
4011 (--walk-tree
4012 (function
4013 (lambda (--data)
4014 ;; Recursively walk DATA. INFO, if non-nil, is a plist
4015 ;; holding contextual information.
4016 (let ((--type (org-element-type --data)))
4017 (cond
4018 ((not --data))
4019 ;; Ignored element in an export context.
4020 ((and info (memq --data (plist-get info :ignore-list))))
4021 ;; Secondary string: only objects can be found there.
4022 ((not --type)
4023 (when (eq --category 'objects) (mapc --walk-tree --data)))
4024 ;; Unconditionally enter parse trees.
4025 ((eq --type 'org-data)
4026 (mapc --walk-tree (org-element-contents --data)))
4028 ;; Check if TYPE is matching among TYPES. If so,
4029 ;; apply FUN to --DATA and accumulate return value
4030 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
4031 (when (memq --type types)
4032 (let ((result (funcall fun --data)))
4033 (cond ((not result))
4034 (first-match (throw '--map-first-match result))
4035 (t (push result --acc)))))
4036 ;; If --DATA has a secondary string that can contain
4037 ;; objects with their type among TYPES, look into it.
4038 (when (and (eq --category 'objects) (not (stringp --data)))
4039 (let ((sec-prop
4040 (assq --type org-element-secondary-value-alist)))
4041 (when sec-prop
4042 (funcall --walk-tree
4043 (org-element-property (cdr sec-prop) --data)))))
4044 ;; If --DATA has any affiliated keywords and
4045 ;; WITH-AFFILIATED is non-nil, look for objects in
4046 ;; them.
4047 (when (and with-affiliated
4048 (eq --category 'objects)
4049 (memq --type org-element-all-elements))
4050 (mapc (lambda (kwd-pair)
4051 (let ((kwd (car kwd-pair))
4052 (value (org-element-property
4053 (cdr kwd-pair) --data)))
4054 ;; Pay attention to the type of value.
4055 ;; Preserve order for multiple keywords.
4056 (cond
4057 ((not value))
4058 ((and (member kwd org-element-multiple-keywords)
4059 (member kwd org-element-dual-keywords))
4060 (mapc (lambda (line)
4061 (funcall --walk-tree (cdr line))
4062 (funcall --walk-tree (car line)))
4063 (reverse value)))
4064 ((member kwd org-element-multiple-keywords)
4065 (mapc (lambda (line) (funcall --walk-tree line))
4066 (reverse value)))
4067 ((member kwd org-element-dual-keywords)
4068 (funcall --walk-tree (cdr value))
4069 (funcall --walk-tree (car value)))
4070 (t (funcall --walk-tree value)))))
4071 --affiliated-alist))
4072 ;; Determine if a recursion into --DATA is possible.
4073 (cond
4074 ;; --TYPE is explicitly removed from recursion.
4075 ((memq --type no-recursion))
4076 ;; --DATA has no contents.
4077 ((not (org-element-contents --data)))
4078 ;; Looking for greater elements but --DATA is simply
4079 ;; an element or an object.
4080 ((and (eq --category 'greater-elements)
4081 (not (memq --type org-element-greater-elements))))
4082 ;; Looking for elements but --DATA is an object.
4083 ((and (eq --category 'elements)
4084 (memq --type org-element-all-objects)))
4085 ;; In any other case, map contents.
4086 (t (mapc --walk-tree (org-element-contents --data)))))))))))
4087 (catch '--map-first-match
4088 (funcall --walk-tree data)
4089 ;; Return value in a proper order.
4090 (nreverse --acc))))
4092 ;; The following functions are internal parts of the parser.
4094 ;; The first one, `org-element--parse-elements' acts at the element's
4095 ;; level.
4097 ;; The second one, `org-element--parse-objects' applies on all objects
4098 ;; of a paragraph or a secondary string. It uses
4099 ;; `org-element--get-next-object-candidates' to optimize the search of
4100 ;; the next object in the buffer.
4102 ;; More precisely, that function looks for every allowed object type
4103 ;; first. Then, it discards failed searches, keeps further matches,
4104 ;; and searches again types matched behind point, for subsequent
4105 ;; calls. Thus, searching for a given type fails only once, and every
4106 ;; object is searched only once at top level (but sometimes more for
4107 ;; nested types).
4109 (defun org-element--parse-elements
4110 (beg end special structure granularity visible-only acc)
4111 "Parse elements between BEG and END positions.
4113 SPECIAL prioritize some elements over the others. It can be set
4114 to `first-section', `quote-section', `section' `item' or
4115 `table-row'.
4117 When value is `item', STRUCTURE will be used as the current list
4118 structure.
4120 GRANULARITY determines the depth of the recursion. See
4121 `org-element-parse-buffer' for more information.
4123 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
4124 elements.
4126 Elements are accumulated into ACC."
4127 (save-excursion
4128 (goto-char beg)
4129 ;; When parsing only headlines, skip any text before first one.
4130 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
4131 (org-with-limited-levels (outline-next-heading)))
4132 ;; Main loop start.
4133 (while (< (point) end)
4134 ;; Find current element's type and parse it accordingly to
4135 ;; its category.
4136 (let* ((element (org-element--current-element
4137 end granularity special structure))
4138 (type (org-element-type element))
4139 (cbeg (org-element-property :contents-begin element)))
4140 (goto-char (org-element-property :end element))
4141 ;; Fill ELEMENT contents by side-effect.
4142 (cond
4143 ;; If VISIBLE-ONLY is true and element is hidden or if it has
4144 ;; no contents, don't modify it.
4145 ((or (and visible-only (org-element-property :hiddenp element))
4146 (not cbeg)))
4147 ;; Greater element: parse it between `contents-begin' and
4148 ;; `contents-end'. Make sure GRANULARITY allows the
4149 ;; recursion, or ELEMENT is an headline, in which case going
4150 ;; inside is mandatory, in order to get sub-level headings.
4151 ((and (memq type org-element-greater-elements)
4152 (or (memq granularity '(element object nil))
4153 (and (eq granularity 'greater-element)
4154 (eq type 'section))
4155 (eq type 'headline)))
4156 (org-element--parse-elements
4157 cbeg (org-element-property :contents-end element)
4158 ;; Possibly switch to a special mode.
4159 (case type
4160 (headline
4161 (if (org-element-property :quotedp element) 'quote-section
4162 'section))
4163 (plain-list 'item)
4164 (property-drawer 'node-property)
4165 (table 'table-row))
4166 (org-element-property :structure element)
4167 granularity visible-only element))
4168 ;; ELEMENT has contents. Parse objects inside, if
4169 ;; GRANULARITY allows it.
4170 ((memq granularity '(object nil))
4171 (org-element--parse-objects
4172 cbeg (org-element-property :contents-end element) element
4173 (org-element-restriction type))))
4174 (org-element-adopt-elements acc element)))
4175 ;; Return result.
4176 acc))
4178 (defun org-element--parse-objects (beg end acc restriction)
4179 "Parse objects between BEG and END and return recursive structure.
4181 Objects are accumulated in ACC.
4183 RESTRICTION is a list of object types which are allowed in the
4184 current object."
4185 (let (candidates)
4186 (save-excursion
4187 (goto-char beg)
4188 (while (and (< (point) end)
4189 (setq candidates (org-element--get-next-object-candidates
4190 end restriction candidates)))
4191 (let ((next-object
4192 (let ((pos (apply 'min (mapcar 'cdr candidates))))
4193 (save-excursion
4194 (goto-char pos)
4195 (funcall (intern (format "org-element-%s-parser"
4196 (car (rassq pos candidates)))))))))
4197 ;; 1. Text before any object. Untabify it.
4198 (let ((obj-beg (org-element-property :begin next-object)))
4199 (unless (= (point) obj-beg)
4200 (setq acc
4201 (org-element-adopt-elements
4203 (replace-regexp-in-string
4204 "\t" (make-string tab-width ? )
4205 (buffer-substring-no-properties (point) obj-beg))))))
4206 ;; 2. Object...
4207 (let ((obj-end (org-element-property :end next-object))
4208 (cont-beg (org-element-property :contents-begin next-object)))
4209 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
4210 ;; a recursive type.
4211 (when (and cont-beg
4212 (memq (car next-object) org-element-recursive-objects))
4213 (save-restriction
4214 (narrow-to-region
4215 cont-beg
4216 (org-element-property :contents-end next-object))
4217 (org-element--parse-objects
4218 (point-min) (point-max) next-object
4219 (org-element-restriction next-object))))
4220 (setq acc (org-element-adopt-elements acc next-object))
4221 (goto-char obj-end))))
4222 ;; 3. Text after last object. Untabify it.
4223 (unless (= (point) end)
4224 (setq acc
4225 (org-element-adopt-elements
4227 (replace-regexp-in-string
4228 "\t" (make-string tab-width ? )
4229 (buffer-substring-no-properties (point) end)))))
4230 ;; Result.
4231 acc)))
4233 (defun org-element--get-next-object-candidates (limit restriction objects)
4234 "Return an alist of candidates for the next object.
4236 LIMIT bounds the search, and RESTRICTION narrows candidates to
4237 some object types.
4239 Return value is an alist whose CAR is position and CDR the object
4240 type, as a symbol.
4242 OBJECTS is the previous candidates alist."
4243 ;; Filter out any object found but not belonging to RESTRICTION.
4244 (setq objects
4245 (org-remove-if-not
4246 (lambda (obj)
4247 (let ((type (car obj)))
4248 (memq (or (cdr (assq type org-element-object-successor-alist))
4249 type)
4250 restriction)))
4251 objects))
4252 (let (next-candidates types-to-search)
4253 ;; If no previous result, search every object type in RESTRICTION.
4254 ;; Otherwise, keep potential candidates (old objects located after
4255 ;; point) and ask to search again those which had matched before.
4256 (if (not objects) (setq types-to-search restriction)
4257 (mapc (lambda (obj)
4258 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
4259 (push obj next-candidates)))
4260 objects))
4261 ;; Call the appropriate successor function for each type to search
4262 ;; and accumulate matches.
4263 (mapc
4264 (lambda (type)
4265 (let* ((successor-fun
4266 (intern
4267 (format "org-element-%s-successor"
4268 (or (cdr (assq type org-element-object-successor-alist))
4269 type))))
4270 (obj (funcall successor-fun limit)))
4271 (and obj (push obj next-candidates))))
4272 types-to-search)
4273 ;; Return alist.
4274 next-candidates))
4278 ;;; Towards A Bijective Process
4280 ;; The parse tree obtained with `org-element-parse-buffer' is really
4281 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
4282 ;; interpreted and expanded into a string with canonical Org syntax.
4283 ;; Hence `org-element-interpret-data'.
4285 ;; The function relies internally on
4286 ;; `org-element--interpret-affiliated-keywords'.
4288 ;;;###autoload
4289 (defun org-element-interpret-data (data &optional parent)
4290 "Interpret DATA as Org syntax.
4292 DATA is a parse tree, an element, an object or a secondary string
4293 to interpret.
4295 Optional argument PARENT is used for recursive calls. It contains
4296 the element or object containing data, or nil.
4298 Return Org syntax as a string."
4299 (let* ((type (org-element-type data))
4300 (results
4301 (cond
4302 ;; Secondary string.
4303 ((not type)
4304 (mapconcat
4305 (lambda (obj) (org-element-interpret-data obj parent))
4306 data ""))
4307 ;; Full Org document.
4308 ((eq type 'org-data)
4309 (mapconcat
4310 (lambda (obj) (org-element-interpret-data obj parent))
4311 (org-element-contents data) ""))
4312 ;; Plain text.
4313 ((stringp data) data)
4314 ;; Element/Object without contents.
4315 ((not (org-element-contents data))
4316 (funcall (intern (format "org-element-%s-interpreter" type))
4317 data nil))
4318 ;; Element/Object with contents.
4320 (let* ((greaterp (memq type org-element-greater-elements))
4321 (objectp (and (not greaterp)
4322 (memq type org-element-recursive-objects)))
4323 (contents
4324 (mapconcat
4325 (lambda (obj) (org-element-interpret-data obj data))
4326 (org-element-contents
4327 (if (or greaterp objectp) data
4328 ;; Elements directly containing objects must
4329 ;; have their indentation normalized first.
4330 (org-element-normalize-contents
4331 data
4332 ;; When normalizing first paragraph of an
4333 ;; item or a footnote-definition, ignore
4334 ;; first line's indentation.
4335 (and (eq type 'paragraph)
4336 (equal data (car (org-element-contents parent)))
4337 (memq (org-element-type parent)
4338 '(footnote-definition item))))))
4339 "")))
4340 (funcall (intern (format "org-element-%s-interpreter" type))
4341 data
4342 (if greaterp (org-element-normalize-contents contents)
4343 contents)))))))
4344 (if (memq type '(org-data plain-text nil)) results
4345 ;; Build white spaces. If no `:post-blank' property is
4346 ;; specified, assume its value is 0.
4347 (let ((post-blank (or (org-element-property :post-blank data) 0)))
4348 (if (memq type org-element-all-objects)
4349 (concat results (make-string post-blank 32))
4350 (concat
4351 (org-element--interpret-affiliated-keywords data)
4352 (org-element-normalize-string results)
4353 (make-string post-blank 10)))))))
4355 (defun org-element--interpret-affiliated-keywords (element)
4356 "Return ELEMENT's affiliated keywords as Org syntax.
4357 If there is no affiliated keyword, return the empty string."
4358 (let ((keyword-to-org
4359 (function
4360 (lambda (key value)
4361 (let (dual)
4362 (when (member key org-element-dual-keywords)
4363 (setq dual (cdr value) value (car value)))
4364 (concat "#+" key
4365 (and dual
4366 (format "[%s]" (org-element-interpret-data dual)))
4367 ": "
4368 (if (member key org-element-parsed-keywords)
4369 (org-element-interpret-data value)
4370 value)
4371 "\n"))))))
4372 (mapconcat
4373 (lambda (prop)
4374 (let ((value (org-element-property prop element))
4375 (keyword (upcase (substring (symbol-name prop) 1))))
4376 (when value
4377 (if (or (member keyword org-element-multiple-keywords)
4378 ;; All attribute keywords can have multiple lines.
4379 (string-match "^ATTR_" keyword))
4380 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
4381 (reverse value)
4383 (funcall keyword-to-org keyword value)))))
4384 ;; List all ELEMENT's properties matching an attribute line or an
4385 ;; affiliated keyword, but ignore translated keywords since they
4386 ;; cannot belong to the property list.
4387 (loop for prop in (nth 1 element) by 'cddr
4388 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
4389 (or (string-match "^ATTR_" keyword)
4390 (and
4391 (member keyword org-element-affiliated-keywords)
4392 (not (assoc keyword
4393 org-element-keyword-translation-alist)))))
4394 collect prop)
4395 "")))
4397 ;; Because interpretation of the parse tree must return the same
4398 ;; number of blank lines between elements and the same number of white
4399 ;; space after objects, some special care must be given to white
4400 ;; spaces.
4402 ;; The first function, `org-element-normalize-string', ensures any
4403 ;; string different from the empty string will end with a single
4404 ;; newline character.
4406 ;; The second function, `org-element-normalize-contents', removes
4407 ;; global indentation from the contents of the current element.
4409 (defun org-element-normalize-string (s)
4410 "Ensure string S ends with a single newline character.
4412 If S isn't a string return it unchanged. If S is the empty
4413 string, return it. Otherwise, return a new string with a single
4414 newline character at its end."
4415 (cond
4416 ((not (stringp s)) s)
4417 ((string= "" s) "")
4418 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
4419 (replace-match "\n" nil nil s)))))
4421 (defun org-element-normalize-contents (element &optional ignore-first)
4422 "Normalize plain text in ELEMENT's contents.
4424 ELEMENT must only contain plain text and objects.
4426 If optional argument IGNORE-FIRST is non-nil, ignore first line's
4427 indentation to compute maximal common indentation.
4429 Return the normalized element that is element with global
4430 indentation removed from its contents. The function assumes that
4431 indentation is not done with TAB characters."
4432 (let* (ind-list ; for byte-compiler
4433 collect-inds ; for byte-compiler
4434 (collect-inds
4435 (function
4436 ;; Return list of indentations within BLOB. This is done by
4437 ;; walking recursively BLOB and updating IND-LIST along the
4438 ;; way. FIRST-FLAG is non-nil when the first string hasn't
4439 ;; been seen yet. It is required as this string is the only
4440 ;; one whose indentation doesn't happen after a newline
4441 ;; character.
4442 (lambda (blob first-flag)
4443 (mapc
4444 (lambda (object)
4445 (when (and first-flag (stringp object))
4446 (setq first-flag nil)
4447 (string-match "\\`\\( *\\)" object)
4448 (let ((len (length (match-string 1 object))))
4449 ;; An indentation of zero means no string will be
4450 ;; modified. Quit the process.
4451 (if (zerop len) (throw 'zero (setq ind-list nil))
4452 (push len ind-list))))
4453 (cond
4454 ((stringp object)
4455 (let ((start 0))
4456 ;; Avoid matching blank or empty lines.
4457 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
4458 (not (equal (match-string 2 object) " ")))
4459 (setq start (match-end 0))
4460 (push (length (match-string 1 object)) ind-list))))
4461 ((memq (org-element-type object) org-element-recursive-objects)
4462 (funcall collect-inds object first-flag))))
4463 (org-element-contents blob))))))
4464 ;; Collect indentation list in ELEMENT. Possibly remove first
4465 ;; value if IGNORE-FIRST is non-nil.
4466 (catch 'zero (funcall collect-inds element (not ignore-first)))
4467 (if (not ind-list) element
4468 ;; Build ELEMENT back, replacing each string with the same
4469 ;; string minus common indentation.
4470 (let* (build ; For byte compiler.
4471 (build
4472 (function
4473 (lambda (blob mci first-flag)
4474 ;; Return BLOB with all its strings indentation
4475 ;; shortened from MCI white spaces. FIRST-FLAG is
4476 ;; non-nil when the first string hasn't been seen
4477 ;; yet.
4478 (setcdr (cdr blob)
4479 (mapcar
4480 (lambda (object)
4481 (when (and first-flag (stringp object))
4482 (setq first-flag nil)
4483 (setq object
4484 (replace-regexp-in-string
4485 (format "\\` \\{%d\\}" mci) "" object)))
4486 (cond
4487 ((stringp object)
4488 (replace-regexp-in-string
4489 (format "\n \\{%d\\}" mci) "\n" object))
4490 ((memq (org-element-type object)
4491 org-element-recursive-objects)
4492 (funcall build object mci first-flag))
4493 (t object)))
4494 (org-element-contents blob)))
4495 blob))))
4496 (funcall build element (apply 'min ind-list) (not ignore-first))))))
4500 ;;; The Toolbox
4502 ;; The first move is to implement a way to obtain the smallest element
4503 ;; containing point. This is the job of `org-element-at-point'. It
4504 ;; basically jumps back to the beginning of section containing point
4505 ;; and moves, element after element, with
4506 ;; `org-element--current-element' until the container is found. Note:
4507 ;; When using `org-element-at-point', secondary values are never
4508 ;; parsed since the function focuses on elements, not on objects.
4510 ;; At a deeper level, `org-element-context' lists all elements and
4511 ;; objects containing point.
4513 ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
4514 ;; internally by navigation and manipulation tools.
4516 ;;;###autoload
4517 (defun org-element-at-point (&optional keep-trail)
4518 "Determine closest element around point.
4520 Return value is a list like (TYPE PROPS) where TYPE is the type
4521 of the element and PROPS a plist of properties associated to the
4522 element.
4524 Possible types are defined in `org-element-all-elements'.
4525 Properties depend on element or object type, but always
4526 include :begin, :end, :parent and :post-blank properties.
4528 As a special case, if point is at the very beginning of a list or
4529 sub-list, returned element will be that list instead of the first
4530 item. In the same way, if point is at the beginning of the first
4531 row of a table, returned element will be the table instead of the
4532 first row.
4534 If optional argument KEEP-TRAIL is non-nil, the function returns
4535 a list of of elements leading to element at point. The list's
4536 CAR is always the element at point. Following positions contain
4537 element's siblings, then parents, siblings of parents, until the
4538 first element of current section."
4539 (org-with-wide-buffer
4540 ;; If at an headline, parse it. It is the sole element that
4541 ;; doesn't require to know about context. Be sure to disallow
4542 ;; secondary string parsing, though.
4543 (if (org-with-limited-levels (org-at-heading-p))
4544 (progn
4545 (beginning-of-line)
4546 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4547 (list (org-element-headline-parser (point-max) t))))
4548 ;; Otherwise move at the beginning of the section containing
4549 ;; point.
4550 (let ((origin (point))
4551 (end (save-excursion
4552 (org-with-limited-levels (outline-next-heading)) (point)))
4553 element type special-flag trail struct prevs parent)
4554 (org-with-limited-levels
4555 (if (org-with-limited-levels (org-before-first-heading-p))
4556 (goto-char (point-min))
4557 (org-back-to-heading)
4558 (forward-line)))
4559 (org-skip-whitespace)
4560 (beginning-of-line)
4561 ;; Parse successively each element, skipping those ending
4562 ;; before original position.
4563 (catch 'exit
4564 (while t
4565 (setq element
4566 (org-element--current-element end 'element special-flag struct)
4567 type (car element))
4568 (org-element-put-property element :parent parent)
4569 (when keep-trail (push element trail))
4570 (cond
4571 ;; 1. Skip any element ending before point. Also skip
4572 ;; element ending at point when we're sure that another
4573 ;; element has started.
4574 ((let ((elem-end (org-element-property :end element)))
4575 (when (or (< elem-end origin)
4576 (and (= elem-end origin) (/= elem-end end)))
4577 (goto-char elem-end))))
4578 ;; 2. An element containing point is always the element at
4579 ;; point.
4580 ((not (memq type org-element-greater-elements))
4581 (throw 'exit (if keep-trail trail element)))
4582 ;; 3. At any other greater element type, if point is
4583 ;; within contents, move into it.
4585 (let ((cbeg (org-element-property :contents-begin element))
4586 (cend (org-element-property :contents-end element)))
4587 (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
4588 ;; Create an anchor for tables and plain lists:
4589 ;; when point is at the very beginning of these
4590 ;; elements, ignoring affiliated keywords,
4591 ;; target them instead of their contents.
4592 (and (= cbeg origin) (memq type '(plain-list table)))
4593 ;; When point is at contents end, do not move
4594 ;; into elements with an explicit ending, but
4595 ;; return that element instead.
4596 (and (= cend origin)
4597 (memq type
4598 '(center-block
4599 drawer dynamic-block inlinetask item
4600 plain-list property-drawer quote-block
4601 special-block))))
4602 (throw 'exit (if keep-trail trail element))
4603 (setq parent element)
4604 (case type
4605 (plain-list
4606 (setq special-flag 'item
4607 struct (org-element-property :structure element)))
4608 (property-drawer (setq special-flag 'node-property))
4609 (table (setq special-flag 'table-row))
4610 (otherwise (setq special-flag nil)))
4611 (setq end cend)
4612 (goto-char cbeg)))))))))))
4614 ;;;###autoload
4615 (defun org-element-context ()
4616 "Return closest element or object around point.
4618 Return value is a list like (TYPE PROPS) where TYPE is the type
4619 of the element or object and PROPS a plist of properties
4620 associated to it.
4622 Possible types are defined in `org-element-all-elements' and
4623 `org-element-all-objects'. Properties depend on element or
4624 object type, but always include :begin, :end, :parent
4625 and :post-blank properties."
4626 (org-with-wide-buffer
4627 (let* ((origin (point))
4628 (element (org-element-at-point))
4629 (type (car element))
4630 end)
4631 ;; Check if point is inside an element containing objects or at
4632 ;; a secondary string. In that case, move to beginning of the
4633 ;; element or secondary string and set END to the other side.
4634 (if (not (or (let ((post (org-element-property :post-affiliated element)))
4635 (and post (> post origin)
4636 (< (org-element-property :begin element) origin)
4637 (progn (beginning-of-line)
4638 (looking-at org-element--affiliated-re)
4639 (member (upcase (match-string 1))
4640 org-element-parsed-keywords))
4641 ;; We're at an affiliated keyword. Change
4642 ;; type to retrieve correct restrictions.
4643 (setq type 'keyword)
4644 ;; Determine if we're at main or dual value.
4645 (if (and (match-end 2) (<= origin (match-end 2)))
4646 (progn (goto-char (match-beginning 2))
4647 (setq end (match-end 2)))
4648 (goto-char (match-end 0))
4649 (setq end (line-end-position)))))
4650 (and (eq type 'item)
4651 (let ((tag (org-element-property :tag element)))
4652 (and tag
4653 (progn
4654 (beginning-of-line)
4655 (search-forward tag (point-at-eol))
4656 (goto-char (match-beginning 0))
4657 (and (>= origin (point))
4658 (<= origin
4659 ;; `1+' is required so some
4660 ;; successors can match
4661 ;; properly their object.
4662 (setq end (1+ (match-end 0)))))))))
4663 (and (memq type '(headline inlinetask))
4664 (progn (beginning-of-line)
4665 (skip-chars-forward "* ")
4666 (setq end (point-at-eol))))
4667 (and (memq type '(paragraph table-row verse-block))
4668 (let ((cbeg (org-element-property
4669 :contents-begin element))
4670 (cend (org-element-property
4671 :contents-end element)))
4672 (and (>= origin cbeg)
4673 (<= origin cend)
4674 (progn (goto-char cbeg) (setq end cend)))))
4675 (and (eq type 'keyword)
4676 (let ((key (org-element-property :key element)))
4677 (and (member key org-element-document-properties)
4678 (progn (beginning-of-line)
4679 (search-forward key (line-end-position) t)
4680 (forward-char)
4681 (setq end (line-end-position))))))))
4682 element
4683 (let ((restriction (org-element-restriction type))
4684 (parent element)
4685 candidates)
4686 (catch 'exit
4687 (while (setq candidates (org-element--get-next-object-candidates
4688 end restriction candidates))
4689 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4690 candidates)))
4691 ;; If ORIGIN is before next object in element, there's
4692 ;; no point in looking further.
4693 (if (> (cdr closest-cand) origin) (throw 'exit parent)
4694 (let* ((object
4695 (progn (goto-char (cdr closest-cand))
4696 (funcall (intern (format "org-element-%s-parser"
4697 (car closest-cand))))))
4698 (cbeg (org-element-property :contents-begin object))
4699 (cend (org-element-property :contents-end object)))
4700 (cond
4701 ;; ORIGIN is after OBJECT, so skip it.
4702 ((< (org-element-property :end object) origin)
4703 (goto-char (org-element-property :end object)))
4704 ;; ORIGIN is within a non-recursive object or at an
4705 ;; object boundaries: Return that object.
4706 ((or (not cbeg) (> cbeg origin) (< cend origin))
4707 (throw 'exit
4708 (org-element-put-property object :parent parent)))
4709 ;; Otherwise, move within current object and restrict
4710 ;; search to the end of its contents.
4711 (t (goto-char cbeg)
4712 (org-element-put-property object :parent parent)
4713 (setq parent object
4714 restriction (org-element-restriction object)
4715 end cend)))))))
4716 parent))))))
4718 (defsubst org-element-nested-p (elem-A elem-B)
4719 "Non-nil when elements ELEM-A and ELEM-B are nested."
4720 (let ((beg-A (org-element-property :begin elem-A))
4721 (beg-B (org-element-property :begin elem-B))
4722 (end-A (org-element-property :end elem-A))
4723 (end-B (org-element-property :end elem-B)))
4724 (or (and (>= beg-A beg-B) (<= end-A end-B))
4725 (and (>= beg-B beg-A) (<= end-B end-A)))))
4727 (defun org-element-swap-A-B (elem-A elem-B)
4728 "Swap elements ELEM-A and ELEM-B.
4729 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4730 end of ELEM-A."
4731 (goto-char (org-element-property :begin elem-A))
4732 ;; There are two special cases when an element doesn't start at bol:
4733 ;; the first paragraph in an item or in a footnote definition.
4734 (let ((specialp (not (bolp))))
4735 ;; Only a paragraph without any affiliated keyword can be moved at
4736 ;; ELEM-A position in such a situation. Note that the case of
4737 ;; a footnote definition is impossible: it cannot contain two
4738 ;; paragraphs in a row because it cannot contain a blank line.
4739 (if (and specialp
4740 (or (not (eq (org-element-type elem-B) 'paragraph))
4741 (/= (org-element-property :begin elem-B)
4742 (org-element-property :contents-begin elem-B))))
4743 (error "Cannot swap elements"))
4744 ;; In a special situation, ELEM-A will have no indentation. We'll
4745 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4746 (let* ((ind-B (when specialp
4747 (goto-char (org-element-property :begin elem-B))
4748 (org-get-indentation)))
4749 (beg-A (org-element-property :begin elem-A))
4750 (end-A (save-excursion
4751 (goto-char (org-element-property :end elem-A))
4752 (skip-chars-backward " \r\t\n")
4753 (point-at-eol)))
4754 (beg-B (org-element-property :begin elem-B))
4755 (end-B (save-excursion
4756 (goto-char (org-element-property :end elem-B))
4757 (skip-chars-backward " \r\t\n")
4758 (point-at-eol)))
4759 ;; Store overlays responsible for visibility status. We
4760 ;; also need to store their boundaries as they will be
4761 ;; removed from buffer.
4762 (overlays
4763 (cons
4764 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4765 (overlays-in beg-A end-A))
4766 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4767 (overlays-in beg-B end-B))))
4768 ;; Get contents.
4769 (body-A (buffer-substring beg-A end-A))
4770 (body-B (delete-and-extract-region beg-B end-B)))
4771 (goto-char beg-B)
4772 (when specialp
4773 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4774 (org-indent-to-column ind-B))
4775 (insert body-A)
4776 ;; Restore ex ELEM-A overlays.
4777 (let ((offset (- beg-B beg-A)))
4778 (mapc (lambda (ov)
4779 (move-overlay
4780 (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
4781 (car overlays))
4782 (goto-char beg-A)
4783 (delete-region beg-A end-A)
4784 (insert body-B)
4785 ;; Restore ex ELEM-B overlays.
4786 (mapc (lambda (ov)
4787 (move-overlay
4788 (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
4789 (cdr overlays)))
4790 (goto-char (org-element-property :end elem-B)))))
4792 (provide 'org-element)
4794 ;; Local variables:
4795 ;; generated-autoload-file: "org-loaddefs.el"
4796 ;; End:
4798 ;;; org-element.el ends here