org-element: Fix timestamp interpreter
[org-mode.git] / contrib / lisp / org-element.el
blob80da2b04705f7c8654e84bfa12e5511f239e5b12
1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; This file is not part of GNU Emacs.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
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 (namely `babel-call', `clock', `headline', `item',
34 ;; `keyword', `planning', `property-drawer' and `section' types), it
35 ;; can also accept a fixed set of keywords as attributes. Those are
36 ;; called "affiliated keywords" to distinguish them from other
37 ;; keywords, which are full-fledged elements. Almost all affiliated
38 ;; keywords are referenced in `org-element-affiliated-keywords'; the
39 ;; others are export attributes and start with "ATTR_" prefix.
41 ;; Element containing other elements (and only elements) are called
42 ;; greater elements. Concerned types are: `center-block', `drawer',
43 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
44 ;; `item', `plain-list', `quote-block', `section' and `special-block'.
46 ;; Other element types are: `babel-call', `clock', `comment',
47 ;; `comment-block', `example-block', `export-block', `fixed-width',
48 ;; `horizontal-rule', `keyword', `latex-environment', `paragraph',
49 ;; `planning', `property-drawer', `quote-section', `src-block',
50 ;; `table', `table-row' and `verse-block'. Among them, `paragraph'
51 ;; and `verse-block' types can contain Org objects and plain text.
53 ;; Objects are related to document's contents. Some of them are
54 ;; recursive. Associated types are of the following: `bold', `code',
55 ;; `entity', `export-snippet', `footnote-reference',
56 ;; `inline-babel-call', `inline-src-block', `italic',
57 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
58 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
59 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
61 ;; Some elements also have special properties whose value can hold
62 ;; objects themselves (i.e. an item tag or an headline name). Such
63 ;; values are called "secondary strings". Any object belongs to
64 ;; either an element or a secondary string.
66 ;; Notwithstanding affiliated keywords, each greater element, element
67 ;; and object has a fixed set of properties attached to it. Among
68 ;; them, four are shared by all types: `:begin' and `:end', which
69 ;; refer to the beginning and ending buffer positions of the
70 ;; considered element or object, `:post-blank', which holds the number
71 ;; of blank lines, or white spaces, at its end and `:parent' which
72 ;; refers to the element or object containing it. Greater elements
73 ;; and elements containing objects will also have `:contents-begin'
74 ;; and `:contents-end' properties to delimit contents.
76 ;; Lisp-wise, an element or an object can be represented as a list.
77 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
78 ;; TYPE is a symbol describing the Org element or object.
79 ;; PROPERTIES is the property list attached to it. See docstring of
80 ;; appropriate parsing function to get an exhaustive
81 ;; list.
82 ;; CONTENTS is a list of elements, objects or raw strings contained
83 ;; in the current element or object, when applicable.
85 ;; An Org buffer is a nested list of such elements and objects, whose
86 ;; type is `org-data' and properties is nil.
88 ;; The first part of this file defines Org syntax, while the second
89 ;; one provide accessors and setters functions.
91 ;; The next part implements a parser and an interpreter for each
92 ;; element and object type in Org syntax.
94 ;; The following part creates a fully recursive buffer parser. It
95 ;; also provides a tool to map a function to elements or objects
96 ;; matching some criteria in the parse tree. Functions of interest
97 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
98 ;; extent, `org-element-parse-secondary-string'.
100 ;; The penultimate part is the cradle of an interpreter for the
101 ;; obtained parse tree: `org-element-interpret-data'.
103 ;; The library ends by furnishing a set of interactive tools for
104 ;; element's navigation and manipulation, mostly based on
105 ;; `org-element-at-point' function, and a way to give information
106 ;; about document structure around point with `org-element-context'.
109 ;;; Code:
111 (eval-when-compile (require 'cl))
112 (require 'org)
113 (declare-function org-inlinetask-goto-end "org-inlinetask" ())
117 ;;; Definitions And Rules
119 ;; Define elements, greater elements and specify recursive objects,
120 ;; along with the affiliated keywords recognized. Also set up
121 ;; restrictions on recursive objects combinations.
123 ;; These variables really act as a control center for the parsing
124 ;; process.
125 (defconst org-element-paragraph-separate
126 (concat "\f" "\\|" "^[ \t]*$" "\\|"
127 ;; Headlines and inlinetasks.
128 org-outline-regexp-bol "\\|"
129 ;; Comments, blocks (any type), keywords and babel calls.
130 "^[ \t]*#\\+" "\\|" "^#\\(?: \\|$\\)" "\\|"
131 ;; Lists.
132 (org-item-beginning-re) "\\|"
133 ;; Fixed-width, drawers (any type) and tables.
134 "^[ \t]*[:|]" "\\|"
135 ;; Footnote definitions.
136 org-footnote-definition-re "\\|"
137 ;; Horizontal rules.
138 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
139 ;; LaTeX environments.
140 "^[ \t]*\\\\\\(begin\\|end\\)" "\\|"
141 ;; Planning and Clock lines.
142 "^[ \t]*\\(?:"
143 org-clock-string "\\|"
144 org-closed-string "\\|"
145 org-deadline-string "\\|"
146 org-scheduled-string "\\)")
147 "Regexp to separate paragraphs in an Org buffer.")
149 (defconst org-element-all-elements
150 '(center-block clock comment comment-block drawer dynamic-block example-block
151 export-block fixed-width footnote-definition headline
152 horizontal-rule inlinetask item keyword latex-environment
153 babel-call paragraph plain-list planning property-drawer
154 quote-block quote-section section special-block src-block table
155 table-row verse-block)
156 "Complete list of element types.")
158 (defconst org-element-greater-elements
159 '(center-block drawer dynamic-block footnote-definition headline inlinetask
160 item plain-list quote-block section special-block table)
161 "List of recursive element types aka Greater Elements.")
163 (defconst org-element-all-successors
164 '(export-snippet footnote-reference inline-babel-call inline-src-block
165 latex-or-entity line-break link macro radio-target
166 statistics-cookie sub/superscript table-cell target
167 text-markup timestamp)
168 "Complete list of successors.")
170 (defconst org-element-object-successor-alist
171 '((subscript . sub/superscript) (superscript . sub/superscript)
172 (bold . text-markup) (code . text-markup) (italic . text-markup)
173 (strike-through . text-markup) (underline . text-markup)
174 (verbatim . text-markup) (entity . latex-or-entity)
175 (latex-fragment . latex-or-entity))
176 "Alist of translations between object type and successor name.
178 Sharing the same successor comes handy when, for example, the
179 regexp matching one object can also match the other object.")
181 (defconst org-element-all-objects
182 '(bold code entity export-snippet footnote-reference inline-babel-call
183 inline-src-block italic line-break latex-fragment link macro
184 radio-target statistics-cookie strike-through subscript superscript
185 table-cell target timestamp underline verbatim)
186 "Complete list of object types.")
188 (defconst org-element-recursive-objects
189 '(bold italic link macro subscript radio-target strike-through superscript
190 table-cell underline)
191 "List of recursive object types.")
193 (defconst org-element-block-name-alist
194 '(("ASCII" . org-element-export-block-parser)
195 ("CENTER" . org-element-center-block-parser)
196 ("COMMENT" . org-element-comment-block-parser)
197 ("DOCBOOK" . org-element-export-block-parser)
198 ("EXAMPLE" . org-element-example-block-parser)
199 ("HTML" . org-element-export-block-parser)
200 ("LATEX" . org-element-export-block-parser)
201 ("ODT" . org-element-export-block-parser)
202 ("QUOTE" . org-element-quote-block-parser)
203 ("SRC" . org-element-src-block-parser)
204 ("VERSE" . org-element-verse-block-parser))
205 "Alist between block names and the associated parsing function.
206 Names must be uppercase. Any block whose name has no association
207 is parsed with `org-element-special-block-parser'.")
209 (defconst org-element-affiliated-keywords
210 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
211 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
212 "List of affiliated keywords as strings.
213 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
214 are affiliated keywords and need not to be in this list.")
216 (defconst org-element--affiliated-re
217 (format "[ \t]*#\\+%s:"
218 ;; Regular affiliated keywords.
219 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
220 (regexp-opt org-element-affiliated-keywords)))
221 "Regexp matching any affiliated keyword.
223 Keyword name is put in match group 1. Moreover, if keyword
224 belongs to `org-element-dual-keywords', put the dual value in
225 match group 2.
227 Don't modify it, set `org-element-affiliated-keywords' instead.")
229 (defconst org-element-keyword-translation-alist
230 '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
231 ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
232 ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
233 "Alist of usual translations for keywords.
234 The key is the old name and the value the new one. The property
235 holding their value will be named after the translated name.")
237 (defconst org-element-multiple-keywords '("HEADER")
238 "List of affiliated keywords that can occur more that once in an element.
240 Their value will be consed into a list of strings, which will be
241 returned as the value of the property.
243 This list is checked after translations have been applied. See
244 `org-element-keyword-translation-alist'.
246 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
247 allow multiple occurrences and need not to be in this list.")
249 (defconst org-element-parsed-keywords '("AUTHOR" "CAPTION" "DATE" "TITLE")
250 "List of keywords whose value can be parsed.
252 Their value will be stored as a secondary string: a list of
253 strings and objects.
255 This list is checked after translations have been applied. See
256 `org-element-keyword-translation-alist'.")
258 (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
259 "List of keywords which can have a secondary value.
261 In Org syntax, they can be written with optional square brackets
262 before the colons. For example, results keyword can be
263 associated to a hash value with the following:
265 #+RESULTS[hash-string]: some-source
267 This list is checked after translations have been applied. See
268 `org-element-keyword-translation-alist'.")
270 (defconst org-element-object-restrictions
271 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
272 radio-target sub/superscript target text-markup timestamp)
273 (footnote-reference export-snippet footnote-reference inline-babel-call
274 inline-src-block latex-or-entity line-break link macro
275 radio-target sub/superscript target text-markup
276 timestamp)
277 (headline inline-babel-call inline-src-block latex-or-entity link macro
278 radio-target statistics-cookie sub/superscript target text-markup
279 timestamp)
280 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
281 radio-target sub/superscript target text-markup timestamp)
282 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
283 link radio-target sub/superscript target text-markup timestamp)
284 (item footnote-reference inline-babel-call latex-or-entity macro
285 radio-target sub/superscript target text-markup)
286 (keyword latex-or-entity macro sub/superscript text-markup)
287 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
288 sub/superscript text-markup)
289 (macro macro)
290 (paragraph export-snippet footnote-reference inline-babel-call
291 inline-src-block latex-or-entity line-break link macro
292 radio-target statistics-cookie sub/superscript target text-markup
293 timestamp)
294 (radio-target export-snippet latex-or-entity sub/superscript)
295 (strike-through export-snippet inline-babel-call inline-src-block
296 latex-or-entity link radio-target sub/superscript target
297 text-markup timestamp)
298 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
299 sub/superscript target text-markup)
300 (superscript export-snippet inline-babel-call inline-src-block
301 latex-or-entity sub/superscript target text-markup)
302 (table-cell export-snippet latex-or-entity link macro radio-target
303 sub/superscript target text-markup timestamp)
304 (table-row table-cell)
305 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
306 link radio-target sub/superscript target text-markup timestamp)
307 (verse-block footnote-reference inline-babel-call inline-src-block
308 latex-or-entity line-break link macro radio-target
309 sub/superscript target text-markup timestamp))
310 "Alist of objects restrictions.
312 CAR is an element or object type containing objects and CDR is
313 a list of successors that will be called within an element or
314 object of such type.
316 For example, in a `radio-target' object, one can only find
317 entities, export snippets, latex-fragments, subscript and
318 superscript.
320 This alist also applies to secondary string. For example, an
321 `headline' type element doesn't directly contain objects, but
322 still has an entry since one of its properties (`:title') does.")
324 (defconst org-element-secondary-value-alist
325 '((headline . :title)
326 (inlinetask . :title)
327 (item . :tag)
328 (footnote-reference . :inline-definition))
329 "Alist between element types and location of secondary value.")
333 ;;; Accessors and Setters
335 ;; Provide four accessors: `org-element-type', `org-element-property'
336 ;; `org-element-contents' and `org-element-restriction'.
338 ;; Setter functions allow to modify elements by side effect. There is
339 ;; `org-element-put-property', `org-element-set-contents',
340 ;; `org-element-set-element' and `org-element-adopt-element'. Note
341 ;; that `org-element-set-element' and `org-element-adopt-element' are
342 ;; higher level functions since also update `:parent' property.
344 (defsubst org-element-type (element)
345 "Return type of ELEMENT.
347 The function returns the type of the element or object provided.
348 It can also return the following special value:
349 `plain-text' for a string
350 `org-data' for a complete document
351 nil in any other case."
352 (cond
353 ((not (consp element)) (and (stringp element) 'plain-text))
354 ((symbolp (car element)) (car element))))
356 (defsubst org-element-property (property element)
357 "Extract the value from the PROPERTY of an ELEMENT."
358 (plist-get (nth 1 element) property))
360 (defsubst org-element-contents (element)
361 "Extract contents from an ELEMENT."
362 (and (consp element) (nthcdr 2 element)))
364 (defsubst org-element-restriction (element)
365 "Return restriction associated to ELEMENT.
366 ELEMENT can be an element, an object or a symbol representing an
367 element or object type."
368 (cdr (assq (if (symbolp element) element (org-element-type element))
369 org-element-object-restrictions)))
371 (defsubst org-element-put-property (element property value)
372 "In ELEMENT set PROPERTY to VALUE.
373 Return modified element."
374 (when (consp element)
375 (setcar (cdr element) (plist-put (nth 1 element) property value)))
376 element)
378 (defsubst org-element-set-contents (element &rest contents)
379 "Set ELEMENT contents to CONTENTS.
380 Return modified element."
381 (cond ((not element) (list contents))
382 ((cdr element) (setcdr (cdr element) contents))
383 (t (nconc element contents))))
385 (defsubst org-element-set-element (old new)
386 "Replace element or object OLD with element or object NEW.
387 The function takes care of setting `:parent' property for NEW."
388 ;; OLD can belong to the contents of PARENT or to its secondary
389 ;; string.
390 (let* ((parent (org-element-property :parent old))
391 (sec-loc (cdr (assq (org-element-type parent)
392 org-element-secondary-value-alist)))
393 (sec-value (and sec-loc (org-element-property sec-loc parent)))
394 (place (or (member old sec-value) (member old parent))))
395 ;; Make sure NEW has correct `:parent' property.
396 (org-element-put-property new :parent parent)
397 ;; Replace OLD with NEW in PARENT.
398 (setcar place new)))
400 (defsubst org-element-adopt-element (parent child &optional append)
401 "Add an element to the contents of another element.
403 PARENT is an element or object. CHILD is an element, an object,
404 or a string.
406 CHILD is added at the beginning of PARENT contents, unless the
407 optional argument APPEND is non-nil, in which case CHILD is added
408 at the end.
410 The function takes care of setting `:parent' property for CHILD.
411 Return parent element."
412 (if (not parent) (list child)
413 (let ((contents (org-element-contents parent)))
414 (apply 'org-element-set-contents
415 parent
416 (if append (append contents (list child)) (cons child contents))))
417 ;; Link the CHILD element with PARENT.
418 (when (consp child) (org-element-put-property child :parent parent))
419 ;; Return the parent element.
420 parent))
424 ;;; Greater elements
426 ;; For each greater element type, we define a parser and an
427 ;; interpreter.
429 ;; A parser returns the element or object as the list described above.
430 ;; Most of them accepts no argument. Though, exceptions exist. Hence
431 ;; every element containing a secondary string (see
432 ;; `org-element-secondary-value-alist') will accept an optional
433 ;; argument to toggle parsing of that secondary string. Moreover,
434 ;; `item' parser requires current list's structure as its first
435 ;; element.
437 ;; An interpreter accepts two arguments: the list representation of
438 ;; the element or object, and its contents. The latter may be nil,
439 ;; depending on the element or object considered. It returns the
440 ;; appropriate Org syntax, as a string.
442 ;; Parsing functions must follow the naming convention:
443 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
444 ;; defined in `org-element-greater-elements'.
446 ;; Similarly, interpreting functions must follow the naming
447 ;; convention: org-element-TYPE-interpreter.
449 ;; With the exception of `headline' and `item' types, greater elements
450 ;; cannot contain other greater elements of their own type.
452 ;; Beside implementing a parser and an interpreter, adding a new
453 ;; greater element requires to tweak `org-element-current-element'.
454 ;; Moreover, the newly defined type must be added to both
455 ;; `org-element-all-elements' and `org-element-greater-elements'.
458 ;;;; Center Block
460 (defun org-element-center-block-parser (limit)
461 "Parse a center block.
463 LIMIT bounds the search.
465 Return a list whose CAR is `center-block' and CDR is a plist
466 containing `:begin', `:end', `:hiddenp', `:contents-begin',
467 `:contents-end' and `:post-blank' keywords.
469 Assume point is at the beginning of the block."
470 (let ((case-fold-search t))
471 (if (not (save-excursion
472 (re-search-forward "^[ \t]*#\\+END_CENTER" limit t)))
473 ;; Incomplete-block: parse it as a comment.
474 (org-element-comment-parser limit)
475 (let ((contents-end (match-beginning 0)))
476 (save-excursion
477 (let* ((keywords (org-element-collect-affiliated-keywords))
478 (begin (car keywords))
479 (contents-begin (progn (forward-line) (point)))
480 (hidden (org-invisible-p2))
481 (pos-before-blank (progn (goto-char contents-end)
482 (forward-line)
483 (point)))
484 (end (progn (org-skip-whitespace)
485 (if (eobp) (point) (point-at-bol)))))
486 (list 'center-block
487 (nconc
488 (list :begin begin
489 :end end
490 :hiddenp hidden
491 :contents-begin contents-begin
492 :contents-end contents-end
493 :post-blank (count-lines pos-before-blank end))
494 (cadr keywords)))))))))
496 (defun org-element-center-block-interpreter (center-block contents)
497 "Interpret CENTER-BLOCK element as Org syntax.
498 CONTENTS is the contents of the element."
499 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
502 ;;;; Drawer
504 (defun org-element-drawer-parser (limit)
505 "Parse a drawer.
507 LIMIT bounds the search.
509 Return a list whose CAR is `drawer' and CDR is a plist containing
510 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
511 `:contents-end' and `:post-blank' keywords.
513 Assume point is at beginning of drawer."
514 (save-excursion
515 (let* ((case-fold-search t)
516 (name (progn (looking-at org-drawer-regexp)
517 (org-match-string-no-properties 1)))
518 (keywords (org-element-collect-affiliated-keywords))
519 (begin (car keywords))
520 (contents-begin (progn (forward-line) (point)))
521 (hidden (org-invisible-p2))
522 (contents-end (progn (re-search-forward "^[ \t]*:END:" limit t)
523 (point-at-bol)))
524 (pos-before-blank (progn (forward-line) (point)))
525 (end (progn (org-skip-whitespace)
526 (if (eobp) (point) (point-at-bol)))))
527 (list 'drawer
528 (nconc
529 (list :begin begin
530 :end end
531 :drawer-name name
532 :hiddenp hidden
533 :contents-begin contents-begin
534 :contents-end contents-end
535 :post-blank (count-lines pos-before-blank end))
536 (cadr keywords))))))
538 (defun org-element-drawer-interpreter (drawer contents)
539 "Interpret DRAWER element as Org syntax.
540 CONTENTS is the contents of the element."
541 (format ":%s:\n%s:END:"
542 (org-element-property :drawer-name drawer)
543 contents))
546 ;;;; Dynamic Block
548 (defun org-element-dynamic-block-parser (limit)
549 "Parse a dynamic block.
551 LIMIT bounds the search.
553 Return a list whose CAR is `dynamic-block' and CDR is a plist
554 containing `:block-name', `:begin', `:end', `:hiddenp',
555 `:contents-begin', `:contents-end', `:arguments' and
556 `:post-blank' keywords.
558 Assume point is at beginning of dynamic block."
559 (let ((case-fold-search t))
560 (if (not (save-excursion (re-search-forward org-dblock-end-re limit t)))
561 ;; Incomplete block: parse it as a comment.
562 (org-element-comment-parser limit)
563 (let ((contents-end (match-beginning 0)))
564 (save-excursion
565 (let* ((name (progn (looking-at org-dblock-start-re)
566 (org-match-string-no-properties 1)))
567 (arguments (org-match-string-no-properties 3))
568 (keywords (org-element-collect-affiliated-keywords))
569 (begin (car keywords))
570 (contents-begin (progn (forward-line) (point)))
571 (hidden (org-invisible-p2))
572 (pos-before-blank (progn (goto-char contents-end)
573 (forward-line)
574 (point)))
575 (end (progn (org-skip-whitespace)
576 (if (eobp) (point) (point-at-bol)))))
577 (list 'dynamic-block
578 (nconc
579 (list :begin begin
580 :end end
581 :block-name name
582 :arguments arguments
583 :hiddenp hidden
584 :contents-begin contents-begin
585 :contents-end contents-end
586 :post-blank (count-lines pos-before-blank end))
587 (cadr keywords)))))))))
589 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
590 "Interpret DYNAMIC-BLOCK element as Org syntax.
591 CONTENTS is the contents of the element."
592 (format "#+BEGIN: %s%s\n%s#+END:"
593 (org-element-property :block-name dynamic-block)
594 (let ((args (org-element-property :arguments dynamic-block)))
595 (and args (concat " " args)))
596 contents))
599 ;;;; Footnote Definition
601 (defun org-element-footnote-definition-parser (limit)
602 "Parse a footnote definition.
604 LIMIT bounds the search.
606 Return a list whose CAR is `footnote-definition' and CDR is
607 a plist containing `:label', `:begin' `:end', `:contents-begin',
608 `:contents-end' and `:post-blank' keywords.
610 Assume point is at the beginning of the footnote definition."
611 (save-excursion
612 (looking-at org-footnote-definition-re)
613 (let* ((label (org-match-string-no-properties 1))
614 (keywords (org-element-collect-affiliated-keywords))
615 (begin (car keywords))
616 (contents-begin (progn (search-forward "]")
617 (org-skip-whitespace)
618 (point)))
619 (contents-end (if (progn
620 (end-of-line)
621 (re-search-forward
622 (concat org-outline-regexp-bol "\\|"
623 org-footnote-definition-re "\\|"
624 "^[ \t]*$") limit 'move))
625 (match-beginning 0)
626 (point)))
627 (end (progn (org-skip-whitespace)
628 (if (eobp) (point) (point-at-bol)))))
629 (list 'footnote-definition
630 (nconc
631 (list :label label
632 :begin begin
633 :end end
634 :contents-begin contents-begin
635 :contents-end contents-end
636 :post-blank (count-lines contents-end end))
637 (cadr keywords))))))
639 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
640 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
641 CONTENTS is the contents of the footnote-definition."
642 (concat (format "[%s]" (org-element-property :label footnote-definition))
644 contents))
647 ;;;; Headline
649 (defun org-element-headline-parser (limit &optional raw-secondary-p)
650 "Parse an headline.
652 Return a list whose CAR is `headline' and CDR is a plist
653 containing `:raw-value', `:title', `:begin', `:end',
654 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
655 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
656 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
657 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
658 keywords.
660 The plist also contains any property set in the property drawer,
661 with its name in lowercase, the underscores replaced with hyphens
662 and colons at the beginning (i.e. `:custom-id').
664 When RAW-SECONDARY-P is non-nil, headline's title will not be
665 parsed as a secondary string, but as a plain string instead.
667 Assume point is at beginning of the headline."
668 (save-excursion
669 (let* ((components (org-heading-components))
670 (level (nth 1 components))
671 (todo (nth 2 components))
672 (todo-type
673 (and todo (if (member todo org-done-keywords) 'done 'todo)))
674 (tags (let ((raw-tags (nth 5 components)))
675 (and raw-tags (org-split-string raw-tags ":"))))
676 (raw-value (nth 4 components))
677 (quotedp
678 (let ((case-fold-search nil))
679 (string-match (format "^%s +" org-quote-string) raw-value)))
680 (commentedp
681 (let ((case-fold-search nil))
682 (string-match (format "^%s +" org-comment-string) raw-value)))
683 (archivedp (member org-archive-tag tags))
684 (footnote-section-p (and org-footnote-section
685 (string= org-footnote-section raw-value)))
686 (standard-props (let (plist)
687 (mapc
688 (lambda (p)
689 (let ((p-name (downcase (car p))))
690 (while (string-match "_" p-name)
691 (setq p-name
692 (replace-match "-" nil nil p-name)))
693 (setq p-name (intern (concat ":" p-name)))
694 (setq plist
695 (plist-put plist p-name (cdr p)))))
696 (org-entry-properties nil 'standard))
697 plist))
698 (time-props (org-entry-properties nil 'special "CLOCK"))
699 (scheduled (cdr (assoc "SCHEDULED" time-props)))
700 (deadline (cdr (assoc "DEADLINE" time-props)))
701 (clock (cdr (assoc "CLOCK" time-props)))
702 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
703 (begin (point))
704 (pos-after-head (save-excursion (forward-line) (point)))
705 (contents-begin (save-excursion (forward-line)
706 (org-skip-whitespace)
707 (if (eobp) (point) (point-at-bol))))
708 (hidden (save-excursion (forward-line) (org-invisible-p2)))
709 (end (progn (goto-char (org-end-of-subtree t t))))
710 (contents-end (progn (skip-chars-backward " \r\t\n")
711 (forward-line)
712 (point))))
713 ;; Clean RAW-VALUE from any quote or comment string.
714 (when (or quotedp commentedp)
715 (setq raw-value
716 (replace-regexp-in-string
717 (concat "\\(" org-quote-string "\\|" org-comment-string "\\) +")
719 raw-value)))
720 ;; Clean TAGS from archive tag, if any.
721 (when archivedp (setq tags (delete org-archive-tag tags)))
722 (let ((headline
723 (list 'headline
724 (nconc
725 (list :raw-value raw-value
726 :begin begin
727 :end end
728 :pre-blank (count-lines pos-after-head contents-begin)
729 :hiddenp hidden
730 :contents-begin contents-begin
731 :contents-end contents-end
732 :level level
733 :priority (nth 3 components)
734 :tags tags
735 :todo-keyword todo
736 :todo-type todo-type
737 :scheduled scheduled
738 :deadline deadline
739 :timestamp timestamp
740 :clock clock
741 :post-blank (count-lines contents-end end)
742 :footnote-section-p footnote-section-p
743 :archivedp archivedp
744 :commentedp commentedp
745 :quotedp quotedp)
746 standard-props))))
747 (org-element-put-property
748 headline :title
749 (if raw-secondary-p raw-value
750 (org-element-parse-secondary-string
751 raw-value (org-element-restriction 'headline) headline)))))))
753 (defun org-element-headline-interpreter (headline contents)
754 "Interpret HEADLINE element as Org syntax.
755 CONTENTS is the contents of the element."
756 (let* ((level (org-element-property :level headline))
757 (todo (org-element-property :todo-keyword headline))
758 (priority (org-element-property :priority headline))
759 (title (org-element-interpret-data
760 (org-element-property :title headline)))
761 (tags (let ((tag-list (if (org-element-property :archivedp headline)
762 (cons org-archive-tag
763 (org-element-property :tags headline))
764 (org-element-property :tags headline))))
765 (and tag-list
766 (format ":%s:" (mapconcat 'identity tag-list ":")))))
767 (commentedp (org-element-property :commentedp headline))
768 (quotedp (org-element-property :quotedp headline))
769 (pre-blank (or (org-element-property :pre-blank headline) 0))
770 (heading (concat (make-string level ?*)
771 (and todo (concat " " todo))
772 (and quotedp (concat " " org-quote-string))
773 (and commentedp (concat " " org-comment-string))
774 (and priority
775 (format " [#%s]" (char-to-string priority)))
776 (cond ((and org-footnote-section
777 (org-element-property
778 :footnote-section-p headline))
779 (concat " " org-footnote-section))
780 (title (concat " " title))))))
781 (concat heading
782 ;; Align tags.
783 (when tags
784 (cond
785 ((zerop org-tags-column) (format " %s" tags))
786 ((< org-tags-column 0)
787 (concat
788 (make-string
789 (max (- (+ org-tags-column (length heading) (length tags))) 1)
791 tags))
793 (concat
794 (make-string (max (- org-tags-column (length heading)) 1) ? )
795 tags))))
796 (make-string (1+ pre-blank) 10)
797 contents)))
800 ;;;; Inlinetask
802 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
803 "Parse an inline task.
805 Return a list whose CAR is `inlinetask' and CDR is a plist
806 containing `:title', `:begin', `:end', `:hiddenp',
807 `:contents-begin' and `:contents-end', `:level', `:priority',
808 `:tags', `:todo-keyword', `:todo-type', `:scheduled',
809 `:deadline', `:timestamp', `:clock' and `:post-blank' keywords.
811 The plist also contains any property set in the property drawer,
812 with its name in lowercase, the underscores replaced with hyphens
813 and colons at the beginning (i.e. `:custom-id').
815 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
816 title will not be parsed as a secondary string, but as a plain
817 string instead.
819 Assume point is at beginning of the inline task."
820 (save-excursion
821 (let* ((keywords (org-element-collect-affiliated-keywords))
822 (begin (car keywords))
823 (components (org-heading-components))
824 (todo (nth 2 components))
825 (todo-type (and todo
826 (if (member todo org-done-keywords) 'done 'todo)))
827 (tags (let ((raw-tags (nth 5 components)))
828 (and raw-tags (org-split-string raw-tags ":"))))
829 (standard-props (let (plist)
830 (mapc
831 (lambda (p)
832 (let ((p-name (downcase (car p))))
833 (while (string-match "_" p-name)
834 (setq p-name
835 (replace-match "-" nil nil p-name)))
836 (setq p-name (intern (concat ":" p-name)))
837 (setq plist
838 (plist-put plist p-name (cdr p)))))
839 (org-entry-properties nil 'standard))
840 plist))
841 (time-props (org-entry-properties nil 'special "CLOCK"))
842 (scheduled (cdr (assoc "SCHEDULED" time-props)))
843 (deadline (cdr (assoc "DEADLINE" time-props)))
844 (clock (cdr (assoc "CLOCK" time-props)))
845 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
846 (contents-begin (save-excursion (forward-line) (point)))
847 (hidden (org-invisible-p2))
848 (pos-before-blank (org-inlinetask-goto-end))
849 ;; In the case of a single line task, CONTENTS-BEGIN and
850 ;; CONTENTS-END might overlap.
851 (contents-end (max contents-begin
852 (if (not (bolp)) (point-at-bol)
853 (save-excursion (forward-line -1) (point)))))
854 (end (progn (org-skip-whitespace)
855 (if (eobp) (point) (point-at-bol))))
856 (inlinetask
857 (list 'inlinetask
858 (nconc
859 (list :begin begin
860 :end end
861 :hiddenp (and (> contents-end contents-begin) hidden)
862 :contents-begin contents-begin
863 :contents-end contents-end
864 :level (nth 1 components)
865 :priority (nth 3 components)
866 :tags tags
867 :todo-keyword todo
868 :todo-type todo-type
869 :scheduled scheduled
870 :deadline deadline
871 :timestamp timestamp
872 :clock clock
873 :post-blank (count-lines pos-before-blank end)
874 standard-props)
875 (cadr keywords)))))
876 (org-element-put-property
877 inlinetask :title
878 (if raw-secondary-p (nth 4 components)
879 (org-element-parse-secondary-string
880 (nth 4 components)
881 (org-element-restriction 'inlinetask)
882 inlinetask))))))
884 (defun org-element-inlinetask-interpreter (inlinetask contents)
885 "Interpret INLINETASK element as Org syntax.
886 CONTENTS is the contents of inlinetask."
887 (let* ((level (org-element-property :level inlinetask))
888 (todo (org-element-property :todo-keyword inlinetask))
889 (priority (org-element-property :priority inlinetask))
890 (title (org-element-interpret-data
891 (org-element-property :title inlinetask)))
892 (tags (let ((tag-list (org-element-property :tags inlinetask)))
893 (and tag-list
894 (format ":%s:" (mapconcat 'identity tag-list ":")))))
895 (task (concat (make-string level ?*)
896 (and todo (concat " " todo))
897 (and priority
898 (format " [#%s]" (char-to-string priority)))
899 (and title (concat " " title)))))
900 (concat task
901 ;; Align tags.
902 (when tags
903 (cond
904 ((zerop org-tags-column) (format " %s" tags))
905 ((< org-tags-column 0)
906 (concat
907 (make-string
908 (max (- (+ org-tags-column (length task) (length tags))) 1)
910 tags))
912 (concat
913 (make-string (max (- org-tags-column (length task)) 1) ? )
914 tags))))
915 ;; Prefer degenerate inlinetasks when there are no
916 ;; contents.
917 (when contents
918 (concat "\n"
919 contents
920 (make-string level ?*) " END")))))
923 ;;;; Item
925 (defun org-element-item-parser (limit struct &optional raw-secondary-p)
926 "Parse an item.
928 STRUCT is the structure of the plain list.
930 Return a list whose CAR is `item' and CDR is a plist containing
931 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
932 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
933 `:post-blank' keywords.
935 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
936 any, will not be parsed as a secondary string, but as a plain
937 string instead.
939 Assume point is at the beginning of the item."
940 (save-excursion
941 (beginning-of-line)
942 (let* ((begin (point))
943 (bullet (org-list-get-bullet (point) struct))
944 (checkbox (let ((box (org-list-get-checkbox begin struct)))
945 (cond ((equal "[ ]" box) 'off)
946 ((equal "[X]" box) 'on)
947 ((equal "[-]" box) 'trans))))
948 (counter (let ((c (org-list-get-counter begin struct)))
949 (cond
950 ((not c) nil)
951 ((string-match "[A-Za-z]" c)
952 (- (string-to-char (upcase (match-string 0 c)))
953 64))
954 ((string-match "[0-9]+" c)
955 (string-to-number (match-string 0 c))))))
956 (end (org-list-get-item-end begin struct))
957 (contents-begin (progn (looking-at org-list-full-item-re)
958 (goto-char (match-end 0))
959 (org-skip-whitespace)
960 ;; If first line isn't empty,
961 ;; contents really start at the text
962 ;; after item's meta-data.
963 (if (= (point-at-bol) begin) (point)
964 (point-at-bol))))
965 (hidden (progn (forward-line)
966 (and (not (= (point) end))
967 (org-invisible-p2))))
968 (contents-end (progn (goto-char end)
969 (skip-chars-backward " \r\t\n")
970 (forward-line)
971 (point)))
972 (item
973 (list 'item
974 (list :bullet bullet
975 :begin begin
976 :end end
977 ;; CONTENTS-BEGIN and CONTENTS-END may be
978 ;; mixed up in the case of an empty item
979 ;; separated from the next by a blank line.
980 ;; Thus ensure the former is always the
981 ;; smallest.
982 :contents-begin (min contents-begin contents-end)
983 :contents-end (max contents-begin contents-end)
984 :checkbox checkbox
985 :counter counter
986 :hiddenp hidden
987 :structure struct
988 :post-blank (count-lines contents-end end)))))
989 (org-element-put-property
990 item :tag
991 (let ((raw-tag (org-list-get-tag begin struct)))
992 (and raw-tag
993 (if raw-secondary-p raw-tag
994 (org-element-parse-secondary-string
995 raw-tag (org-element-restriction 'item) item))))))))
997 (defun org-element-item-interpreter (item contents)
998 "Interpret ITEM element as Org syntax.
999 CONTENTS is the contents of the element."
1000 (let* ((bullet
1001 (let* ((beg (org-element-property :begin item))
1002 (struct (org-element-property :structure item))
1003 (pre (org-list-prevs-alist struct))
1004 (bul (org-element-property :bullet item)))
1005 (org-list-bullet-string
1006 (if (not (eq (org-list-get-list-type beg struct pre) 'ordered)) "-"
1007 (let ((num
1008 (car
1009 (last
1010 (org-list-get-item-number
1011 beg struct pre (org-list-parents-alist struct))))))
1012 (format "%d%s"
1014 (if (eq org-plain-list-ordered-item-terminator ?\)) ")"
1015 ".")))))))
1016 (checkbox (org-element-property :checkbox item))
1017 (counter (org-element-property :counter item))
1018 (tag (let ((tag (org-element-property :tag item)))
1019 (and tag (org-element-interpret-data tag))))
1020 ;; Compute indentation.
1021 (ind (make-string (length bullet) 32))
1022 (item-starts-with-par-p
1023 (eq (org-element-type (car (org-element-contents item)))
1024 'paragraph)))
1025 ;; Indent contents.
1026 (concat
1027 bullet
1028 (and counter (format "[@%d] " counter))
1029 (cond
1030 ((eq checkbox 'on) "[X] ")
1031 ((eq checkbox 'off) "[ ] ")
1032 ((eq checkbox 'trans) "[-] "))
1033 (and tag (format "%s :: " tag))
1034 (let ((contents (replace-regexp-in-string
1035 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1036 (if item-starts-with-par-p (org-trim contents)
1037 (concat "\n" contents))))))
1040 ;;;; Plain List
1042 (defun org-element-plain-list-parser (limit &optional structure)
1043 "Parse a plain list.
1045 Optional argument STRUCTURE, when non-nil, is the structure of
1046 the plain list being parsed.
1048 Return a list whose CAR is `plain-list' and CDR is a plist
1049 containing `:type', `:begin', `:end', `:contents-begin' and
1050 `:contents-end', `:structure' and `:post-blank' keywords.
1052 Assume point is at the beginning of the list."
1053 (save-excursion
1054 (let* ((struct (or structure (org-list-struct)))
1055 (prevs (org-list-prevs-alist struct))
1056 (parents (org-list-parents-alist struct))
1057 (type (org-list-get-list-type (point) struct prevs))
1058 (contents-begin (point))
1059 (keywords (org-element-collect-affiliated-keywords))
1060 (begin (car keywords))
1061 (contents-end
1062 (goto-char (org-list-get-list-end (point) struct prevs)))
1063 (end (save-excursion (org-skip-whitespace)
1064 (if (eobp) (point) (point-at-bol)))))
1065 ;; Blank lines below list belong to the top-level list only.
1066 (unless (= (org-list-get-top-point struct) contents-begin)
1067 (setq end (min (org-list-get-bottom-point struct)
1068 (progn (org-skip-whitespace)
1069 (if (eobp) (point) (point-at-bol))))))
1070 ;; Return value.
1071 (list 'plain-list
1072 (nconc
1073 (list :type type
1074 :begin begin
1075 :end end
1076 :contents-begin contents-begin
1077 :contents-end contents-end
1078 :structure struct
1079 :post-blank (count-lines contents-end end))
1080 (cadr keywords))))))
1082 (defun org-element-plain-list-interpreter (plain-list contents)
1083 "Interpret PLAIN-LIST element as Org syntax.
1084 CONTENTS is the contents of the element."
1085 contents)
1088 ;;;; Quote Block
1090 (defun org-element-quote-block-parser (limit)
1091 "Parse a quote block.
1093 LIMIT bounds the search.
1095 Return a list whose CAR is `quote-block' and CDR is a plist
1096 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1097 `:contents-end' and `:post-blank' keywords.
1099 Assume point is at the beginning of the block."
1100 (let ((case-fold-search t))
1101 (if (not (save-excursion
1102 (re-search-forward "^[ \t]*#\\+END_QUOTE" limit t)))
1103 ;; Incomplete block: parse it as a comment.
1104 (org-element-comment-parser limit)
1105 (let ((contents-end (match-beginning 0)))
1106 (save-excursion
1107 (let* ((keywords (org-element-collect-affiliated-keywords))
1108 (begin (car keywords))
1109 (contents-begin (progn (forward-line) (point)))
1110 (hidden (org-invisible-p2))
1111 (pos-before-blank (progn (goto-char contents-end)
1112 (forward-line)
1113 (point)))
1114 (end (progn (org-skip-whitespace)
1115 (if (eobp) (point) (point-at-bol)))))
1116 (list 'quote-block
1117 (nconc
1118 (list :begin begin
1119 :end end
1120 :hiddenp hidden
1121 :contents-begin contents-begin
1122 :contents-end contents-end
1123 :post-blank (count-lines pos-before-blank end))
1124 (cadr keywords)))))))))
1126 (defun org-element-quote-block-interpreter (quote-block contents)
1127 "Interpret QUOTE-BLOCK element as Org syntax.
1128 CONTENTS is the contents of the element."
1129 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1132 ;;;; Section
1134 (defun org-element-section-parser (limit)
1135 "Parse a section.
1137 LIMIT bounds the search.
1139 Return a list whose CAR is `section' and CDR is a plist
1140 containing `:begin', `:end', `:contents-begin', `contents-end'
1141 and `:post-blank' keywords."
1142 (save-excursion
1143 ;; Beginning of section is the beginning of the first non-blank
1144 ;; line after previous headline.
1145 (org-with-limited-levels
1146 (let ((begin (point))
1147 (end (progn (goto-char limit) (point)))
1148 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1149 (forward-line)
1150 (point))))
1151 (list 'section
1152 (list :begin begin
1153 :end end
1154 :contents-begin begin
1155 :contents-end pos-before-blank
1156 :post-blank (count-lines pos-before-blank end)))))))
1158 (defun org-element-section-interpreter (section contents)
1159 "Interpret SECTION element as Org syntax.
1160 CONTENTS is the contents of the element."
1161 contents)
1164 ;;;; Special Block
1166 (defun org-element-special-block-parser (limit)
1167 "Parse a special block.
1169 LIMIT bounds the search.
1171 Return a list whose CAR is `special-block' and CDR is a plist
1172 containing `:type', `:begin', `:end', `:hiddenp',
1173 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1175 Assume point is at the beginning of the block."
1176 (let* ((case-fold-search t)
1177 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1178 (upcase (match-string-no-properties 1)))))
1179 (if (not (save-excursion
1180 (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
1181 ;; Incomplete block: parse it as a comment.
1182 (org-element-comment-parser limit)
1183 (let ((contents-end (match-beginning 0)))
1184 (save-excursion
1185 (let* ((keywords (org-element-collect-affiliated-keywords))
1186 (begin (car keywords))
1187 (contents-begin (progn (forward-line) (point)))
1188 (hidden (org-invisible-p2))
1189 (pos-before-blank (progn (goto-char contents-end)
1190 (forward-line)
1191 (point)))
1192 (end (progn (org-skip-whitespace)
1193 (if (eobp) (point) (point-at-bol)))))
1194 (list 'special-block
1195 (nconc
1196 (list :type type
1197 :begin begin
1198 :end end
1199 :hiddenp hidden
1200 :contents-begin contents-begin
1201 :contents-end contents-end
1202 :post-blank (count-lines pos-before-blank end))
1203 (cadr keywords)))))))))
1205 (defun org-element-special-block-interpreter (special-block contents)
1206 "Interpret SPECIAL-BLOCK element as Org syntax.
1207 CONTENTS is the contents of the element."
1208 (let ((block-type (org-element-property :type special-block)))
1209 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1213 ;;; Elements
1215 ;; For each element, a parser and an interpreter are also defined.
1216 ;; Both follow the same naming convention used for greater elements.
1218 ;; Also, as for greater elements, adding a new element type is done
1219 ;; through the following steps: implement a parser and an interpreter,
1220 ;; tweak `org-element-current-element' so that it recognizes the new
1221 ;; type and add that new type to `org-element-all-elements'.
1223 ;; As a special case, when the newly defined type is a block type,
1224 ;; `org-element-block-name-alist' has to be modified accordingly.
1227 ;;;; Babel Call
1229 (defun org-element-babel-call-parser (limit)
1230 "Parse a babel call.
1232 LIMIT bounds the search.
1234 Return a list whose CAR is `babel-call' and CDR is a plist
1235 containing `:begin', `:end', `:info' and `:post-blank' as
1236 keywords."
1237 (save-excursion
1238 (let ((case-fold-search t)
1239 (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
1240 (org-babel-lob-get-info)))
1241 (begin (point-at-bol))
1242 (pos-before-blank (progn (forward-line) (point)))
1243 (end (progn (org-skip-whitespace)
1244 (if (eobp) (point) (point-at-bol)))))
1245 (list 'babel-call
1246 (list :begin begin
1247 :end end
1248 :info info
1249 :post-blank (count-lines pos-before-blank end))))))
1251 (defun org-element-babel-call-interpreter (babel-call contents)
1252 "Interpret BABEL-CALL element as Org syntax.
1253 CONTENTS is nil."
1254 (let* ((babel-info (org-element-property :info babel-call))
1255 (main (car babel-info))
1256 (post-options (nth 1 babel-info)))
1257 (concat "#+CALL: "
1258 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
1259 ;; Remove redundant square brackets.
1260 (replace-match (match-string 1 main) nil nil main))
1261 (and post-options (format "[%s]" post-options)))))
1264 ;;;; Clock
1266 (defun org-element-clock-parser (limit)
1267 "Parse a clock.
1269 LIMIT bounds the search.
1271 Return a list whose CAR is `clock' and CDR is a plist containing
1272 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1273 as keywords."
1274 (save-excursion
1275 (let* ((case-fold-search nil)
1276 (begin (point))
1277 (value (progn (search-forward org-clock-string (line-end-position) t)
1278 (org-skip-whitespace)
1279 (looking-at "\\[.*\\]")
1280 (org-match-string-no-properties 0)))
1281 (time (and (progn (goto-char (match-end 0))
1282 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1283 (org-match-string-no-properties 1)))
1284 (status (if time 'closed 'running))
1285 (post-blank (let ((before-blank (progn (forward-line) (point))))
1286 (org-skip-whitespace)
1287 (unless (eobp) (beginning-of-line))
1288 (count-lines before-blank (point))))
1289 (end (point)))
1290 (list 'clock
1291 (list :status status
1292 :value value
1293 :time time
1294 :begin begin
1295 :end end
1296 :post-blank post-blank)))))
1298 (defun org-element-clock-interpreter (clock contents)
1299 "Interpret CLOCK element as Org syntax.
1300 CONTENTS is nil."
1301 (concat org-clock-string " "
1302 (org-element-property :value clock)
1303 (let ((time (org-element-property :time clock)))
1304 (and time
1305 (concat " => "
1306 (apply 'format
1307 "%2s:%02s"
1308 (org-split-string time ":")))))))
1311 ;;;; Comment
1313 (defun org-element-comment-parser (limit)
1314 "Parse a comment.
1316 LIMIT bounds the search.
1318 Return a list whose CAR is `comment' and CDR is a plist
1319 containing `:begin', `:end', `:value' and `:post-blank'
1320 keywords.
1322 Assume point is at comment beginning."
1323 (save-excursion
1324 (let* ((keywords (org-element-collect-affiliated-keywords))
1325 (begin (car keywords))
1326 ;; Match first line with a loose regexp since it might as
1327 ;; well be an ill-defined keyword.
1328 (value (progn
1329 (looking-at "#\\+? ?")
1330 (buffer-substring-no-properties
1331 (match-end 0) (progn (forward-line) (point)))))
1332 (com-end
1333 ;; Get comments ending.
1334 (progn
1335 (while (and (< (point) limit)
1336 (looking-at "\\(\\(# ?\\)[^+]\\|[ \t]*#\\+\\( \\|$\\)\\)"))
1337 ;; Accumulate lines without leading hash and plus sign
1338 ;; if any. First whitespace is also ignored.
1339 (setq value
1340 (concat value
1341 (buffer-substring-no-properties
1342 (or (match-end 2) (match-end 3))
1343 (progn (forward-line) (point))))))
1344 (point)))
1345 (end (progn (goto-char com-end)
1346 (org-skip-whitespace)
1347 (if (eobp) (point) (point-at-bol)))))
1348 (list 'comment
1349 (nconc
1350 (list :begin begin
1351 :end end
1352 :value value
1353 :post-blank (count-lines com-end end))
1354 (cadr keywords))))))
1356 (defun org-element-comment-interpreter (comment contents)
1357 "Interpret COMMENT element as Org syntax.
1358 CONTENTS is nil."
1359 (replace-regexp-in-string "^" "#+ " (org-element-property :value comment)))
1362 ;;;; Comment Block
1364 (defun org-element-comment-block-parser (limit)
1365 "Parse an export block.
1367 LIMIT bounds the search.
1369 Return a list whose CAR is `comment-block' and CDR is a plist
1370 containing `:begin', `:end', `:hiddenp', `:value' and
1371 `:post-blank' keywords.
1373 Assume point is at comment block beginning."
1374 (let ((case-fold-search t))
1375 (if (not (save-excursion
1376 (re-search-forward "^[ \t]*#\\+END_COMMENT" limit t)))
1377 ;; Incomplete block: parse it as a comment.
1378 (org-element-comment-parser limit)
1379 (let ((contents-end (match-beginning 0)))
1380 (save-excursion
1381 (let* ((keywords (org-element-collect-affiliated-keywords))
1382 (begin (car keywords))
1383 (contents-begin (progn (forward-line) (point)))
1384 (hidden (org-invisible-p2))
1385 (pos-before-blank (progn (goto-char contents-end)
1386 (forward-line)
1387 (point)))
1388 (end (progn (org-skip-whitespace)
1389 (if (eobp) (point) (point-at-bol))))
1390 (value (buffer-substring-no-properties
1391 contents-begin contents-end)))
1392 (list 'comment-block
1393 (nconc
1394 (list :begin begin
1395 :end end
1396 :value value
1397 :hiddenp hidden
1398 :post-blank (count-lines pos-before-blank end))
1399 (cadr keywords)))))))))
1401 (defun org-element-comment-block-interpreter (comment-block contents)
1402 "Interpret COMMENT-BLOCK element as Org syntax.
1403 CONTENTS is nil."
1404 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1405 (org-remove-indentation (org-element-property :value comment-block))))
1408 ;;;; Example Block
1410 (defun org-element-example-block-parser (limit)
1411 "Parse an example block.
1413 LIMIT bounds the search.
1415 Return a list whose CAR is `example-block' and CDR is a plist
1416 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1417 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1418 `:switches', `:value' and `:post-blank' keywords."
1419 (let ((case-fold-search t))
1420 (if (not (save-excursion
1421 (re-search-forward "^[ \t]*#\\+END_EXAMPLE" limit t)))
1422 ;; Incomplete block: parse it as a comment.
1423 (org-element-comment-parser limit)
1424 (let ((contents-end (match-beginning 0)))
1425 (save-excursion
1426 (let* ((switches
1427 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1428 (org-match-string-no-properties 1)))
1429 ;; Switches analysis
1430 (number-lines (cond ((not switches) nil)
1431 ((string-match "-n\\>" switches) 'new)
1432 ((string-match "+n\\>" switches) 'continued)))
1433 (preserve-indent (and switches (string-match "-i\\>" switches)))
1434 ;; Should labels be retained in (or stripped from) example
1435 ;; blocks?
1436 (retain-labels
1437 (or (not switches)
1438 (not (string-match "-r\\>" switches))
1439 (and number-lines (string-match "-k\\>" switches))))
1440 ;; What should code-references use - labels or
1441 ;; line-numbers?
1442 (use-labels
1443 (or (not switches)
1444 (and retain-labels (not (string-match "-k\\>" switches)))))
1445 (label-fmt (and switches
1446 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1447 (match-string 1 switches)))
1448 ;; Standard block parsing.
1449 (keywords (org-element-collect-affiliated-keywords))
1450 (begin (car keywords))
1451 (contents-begin (progn (forward-line) (point)))
1452 (hidden (org-invisible-p2))
1453 (value (buffer-substring-no-properties contents-begin contents-end))
1454 (pos-before-blank (progn (goto-char contents-end)
1455 (forward-line)
1456 (point)))
1457 (end (progn (org-skip-whitespace)
1458 (if (eobp) (point) (point-at-bol)))))
1459 (list 'example-block
1460 (nconc
1461 (list :begin begin
1462 :end end
1463 :value value
1464 :switches switches
1465 :number-lines number-lines
1466 :preserve-indent preserve-indent
1467 :retain-labels retain-labels
1468 :use-labels use-labels
1469 :label-fmt label-fmt
1470 :hiddenp hidden
1471 :post-blank (count-lines pos-before-blank end))
1472 (cadr keywords)))))))))
1474 (defun org-element-example-block-interpreter (example-block contents)
1475 "Interpret EXAMPLE-BLOCK element as Org syntax.
1476 CONTENTS is nil."
1477 (let ((switches (org-element-property :switches example-block)))
1478 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1479 (org-remove-indentation
1480 (org-element-property :value example-block))
1481 "#+END_EXAMPLE")))
1484 ;;;; Export Block
1486 (defun org-element-export-block-parser (limit)
1487 "Parse an export block.
1489 LIMIT bounds the search.
1491 Return a list whose CAR is `export-block' and CDR is a plist
1492 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1493 `:post-blank' keywords.
1495 Assume point is at export-block beginning."
1496 (let* ((case-fold-search t)
1497 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1498 (upcase (org-match-string-no-properties 1)))))
1499 (if (not (save-excursion
1500 (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
1501 ;; Incomplete block: parse it as a comment.
1502 (org-element-comment-parser limit)
1503 (let ((contents-end (match-beginning 0)))
1504 (save-excursion
1505 (let* ((keywords (org-element-collect-affiliated-keywords))
1506 (begin (car keywords))
1507 (contents-begin (progn (forward-line) (point)))
1508 (hidden (org-invisible-p2))
1509 (pos-before-blank (progn (goto-char contents-end)
1510 (forward-line)
1511 (point)))
1512 (end (progn (org-skip-whitespace)
1513 (if (eobp) (point) (point-at-bol))))
1514 (value (buffer-substring-no-properties contents-begin
1515 contents-end)))
1516 (list 'export-block
1517 (nconc
1518 (list :begin begin
1519 :end end
1520 :type type
1521 :value value
1522 :hiddenp hidden
1523 :post-blank (count-lines pos-before-blank end))
1524 (cadr keywords)))))))))
1526 (defun org-element-export-block-interpreter (export-block contents)
1527 "Interpret EXPORT-BLOCK element as Org syntax.
1528 CONTENTS is nil."
1529 (let ((type (org-element-property :type export-block)))
1530 (concat (format "#+BEGIN_%s\n" type)
1531 (org-element-property :value export-block)
1532 (format "#+END_%s" type))))
1535 ;;;; Fixed-width
1537 (defun org-element-fixed-width-parser (limit)
1538 "Parse a fixed-width section.
1540 LIMIT bounds the search.
1542 Return a list whose CAR is `fixed-width' and CDR is a plist
1543 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1545 Assume point is at the beginning of the fixed-width area."
1546 (save-excursion
1547 (let* ((keywords (org-element-collect-affiliated-keywords))
1548 (begin (car keywords))
1549 value
1550 (end-area
1551 (progn
1552 (while (and (< (point) limit)
1553 (looking-at "[ \t]*:\\( \\|$\\)"))
1554 ;; Accumulate text without starting colons.
1555 (setq value
1556 (concat value
1557 (buffer-substring-no-properties
1558 (match-end 0) (point-at-eol))
1559 "\n"))
1560 (forward-line))
1561 (point)))
1562 (end (progn (org-skip-whitespace)
1563 (if (eobp) (point) (point-at-bol)))))
1564 (list 'fixed-width
1565 (nconc
1566 (list :begin begin
1567 :end end
1568 :value value
1569 :post-blank (count-lines end-area end))
1570 (cadr keywords))))))
1572 (defun org-element-fixed-width-interpreter (fixed-width contents)
1573 "Interpret FIXED-WIDTH element as Org syntax.
1574 CONTENTS is nil."
1575 (replace-regexp-in-string
1576 "^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
1579 ;;;; Horizontal Rule
1581 (defun org-element-horizontal-rule-parser (limit)
1582 "Parse an horizontal rule.
1584 LIMIT bounds the search.
1586 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1587 containing `:begin', `:end' and `:post-blank' keywords."
1588 (save-excursion
1589 (let* ((keywords (org-element-collect-affiliated-keywords))
1590 (begin (car keywords))
1591 (post-hr (progn (forward-line) (point)))
1592 (end (progn (org-skip-whitespace)
1593 (if (eobp) (point) (point-at-bol)))))
1594 (list 'horizontal-rule
1595 (nconc
1596 (list :begin begin
1597 :end end
1598 :post-blank (count-lines post-hr end))
1599 (cadr keywords))))))
1601 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1602 "Interpret HORIZONTAL-RULE element as Org syntax.
1603 CONTENTS is nil."
1604 "-----")
1607 ;;;; Keyword
1609 (defun org-element-keyword-parser (limit)
1610 "Parse a keyword at point.
1612 LIMIT bounds the search.
1614 Return a list whose CAR is `keyword' and CDR is a plist
1615 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1616 keywords."
1617 (save-excursion
1618 (let* ((case-fold-search t)
1619 (begin (point))
1620 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+\\):")
1621 (upcase (org-match-string-no-properties 1))))
1622 (value (org-trim (buffer-substring-no-properties
1623 (match-end 0) (point-at-eol))))
1624 (pos-before-blank (progn (forward-line) (point)))
1625 (end (progn (org-skip-whitespace)
1626 (if (eobp) (point) (point-at-bol)))))
1627 (list 'keyword
1628 (list :key key
1629 :value value
1630 :begin begin
1631 :end end
1632 :post-blank (count-lines pos-before-blank end))))))
1634 (defun org-element-keyword-interpreter (keyword contents)
1635 "Interpret KEYWORD element as Org syntax.
1636 CONTENTS is nil."
1637 (format "#+%s: %s"
1638 (org-element-property :key keyword)
1639 (org-element-property :value keyword)))
1642 ;;;; Latex Environment
1644 (defun org-element-latex-environment-parser (limit)
1645 "Parse a LaTeX environment.
1647 LIMIT bounds the search.
1649 Return a list whose CAR is `latex-environment' and CDR is a plist
1650 containing `:begin', `:end', `:value' and `:post-blank'
1651 keywords.
1653 Assume point is at the beginning of the latex environment."
1654 (save-excursion
1655 (let* ((case-fold-search t)
1656 (code-begin (point))
1657 (keywords (org-element-collect-affiliated-keywords))
1658 (begin (car keywords))
1659 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
1660 (regexp-quote (match-string 1))))
1661 (code-end
1662 (progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env) limit t)
1663 (forward-line)
1664 (point)))
1665 (value (buffer-substring-no-properties code-begin code-end))
1666 (end (progn (org-skip-whitespace)
1667 (if (eobp) (point) (point-at-bol)))))
1668 (list 'latex-environment
1669 (nconc
1670 (list :begin begin
1671 :end end
1672 :value value
1673 :post-blank (count-lines code-end end))
1674 (cadr keywords))))))
1676 (defun org-element-latex-environment-interpreter (latex-environment contents)
1677 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1678 CONTENTS is nil."
1679 (org-element-property :value latex-environment))
1682 ;;;; Paragraph
1684 (defun org-element-paragraph-parser (limit)
1685 "Parse a paragraph.
1687 LIMIT bounds the search.
1689 Return a list whose CAR is `paragraph' and CDR is a plist
1690 containing `:begin', `:end', `:contents-begin' and
1691 `:contents-end' and `:post-blank' keywords.
1693 Assume point is at the beginning of the paragraph."
1694 (save-excursion
1695 (let* ((contents-begin (point))
1696 (keywords (org-element-collect-affiliated-keywords))
1697 (begin (car keywords))
1698 (contents-end
1699 (progn (end-of-line)
1700 (if (re-search-forward org-element-paragraph-separate
1701 limit
1703 (progn (forward-line -1) (end-of-line) (point))
1704 (point))))
1705 (pos-before-blank (progn (forward-line) (point)))
1706 (end (progn (org-skip-whitespace)
1707 (if (eobp) (point) (point-at-bol)))))
1708 (list 'paragraph
1709 ;; If paragraph has no affiliated keywords, it may not begin
1710 ;; at beginning of line if it starts an item.
1711 (nconc
1712 (list :begin (if (cadr keywords) begin contents-begin)
1713 :end end
1714 :contents-begin contents-begin
1715 :contents-end contents-end
1716 :post-blank (count-lines pos-before-blank end))
1717 (cadr keywords))))))
1719 (defun org-element-paragraph-interpreter (paragraph contents)
1720 "Interpret PARAGRAPH element as Org syntax.
1721 CONTENTS is the contents of the element."
1722 contents)
1725 ;;;; Planning
1727 (defun org-element-planning-parser (limit)
1728 "Parse a planning.
1730 LIMIT bounds the search.
1732 Return a list whose CAR is `planning' and CDR is a plist
1733 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1734 and `:post-blank' keywords."
1735 (save-excursion
1736 (let* ((case-fold-search nil)
1737 (begin (point))
1738 (post-blank (let ((before-blank (progn (forward-line) (point))))
1739 (org-skip-whitespace)
1740 (unless (eobp) (beginning-of-line))
1741 (count-lines before-blank (point))))
1742 (end (point))
1743 closed deadline scheduled)
1744 (goto-char begin)
1745 (while (re-search-forward org-keyword-time-not-clock-regexp
1746 (line-end-position) t)
1747 (goto-char (match-end 1))
1748 (org-skip-whitespace)
1749 (let ((time (buffer-substring-no-properties
1750 (1+ (point)) (1- (match-end 0))))
1751 (keyword (match-string 1)))
1752 (cond ((equal keyword org-closed-string) (setq closed time))
1753 ((equal keyword org-deadline-string) (setq deadline time))
1754 (t (setq scheduled time)))))
1755 (list 'planning
1756 (list :closed closed
1757 :deadline deadline
1758 :scheduled scheduled
1759 :begin begin
1760 :end end
1761 :post-blank post-blank)))))
1763 (defun org-element-planning-interpreter (planning contents)
1764 "Interpret PLANNING element as Org syntax.
1765 CONTENTS is nil."
1766 (mapconcat
1767 'identity
1768 (delq nil
1769 (list (let ((closed (org-element-property :closed planning)))
1770 (when closed (concat org-closed-string " [" closed "]")))
1771 (let ((deadline (org-element-property :deadline planning)))
1772 (when deadline (concat org-deadline-string " <" deadline ">")))
1773 (let ((scheduled (org-element-property :scheduled planning)))
1774 (when scheduled
1775 (concat org-scheduled-string " <" scheduled ">")))))
1776 " "))
1779 ;;;; Property Drawer
1781 (defun org-element-property-drawer-parser (limit)
1782 "Parse a property drawer.
1784 LIMIT bounds the search.
1786 Return a list whose CAR is `property-drawer' and CDR is a plist
1787 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1788 `:contents-end', `:properties' and `:post-blank' keywords.
1790 Assume point is at the beginning of the property drawer."
1791 (save-excursion
1792 (let ((case-fold-search t)
1793 (begin (point))
1794 (prop-begin (progn (forward-line) (point)))
1795 (hidden (org-invisible-p2))
1796 (properties
1797 (let (val)
1798 (while (not (looking-at "^[ \t]*:END:"))
1799 (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
1800 (push (cons (org-match-string-no-properties 1)
1801 (org-trim
1802 (buffer-substring-no-properties
1803 (match-end 0) (point-at-eol))))
1804 val))
1805 (forward-line))
1806 val))
1807 (prop-end (progn (re-search-forward "^[ \t]*:END:" limit t)
1808 (point-at-bol)))
1809 (pos-before-blank (progn (forward-line) (point)))
1810 (end (progn (org-skip-whitespace)
1811 (if (eobp) (point) (point-at-bol)))))
1812 (list 'property-drawer
1813 (list :begin begin
1814 :end end
1815 :hiddenp hidden
1816 :properties properties
1817 :post-blank (count-lines pos-before-blank end))))))
1819 (defun org-element-property-drawer-interpreter (property-drawer contents)
1820 "Interpret PROPERTY-DRAWER element as Org syntax.
1821 CONTENTS is nil."
1822 (let ((props (org-element-property :properties property-drawer)))
1823 (concat
1824 ":PROPERTIES:\n"
1825 (mapconcat (lambda (p)
1826 (format org-property-format (format ":%s:" (car p)) (cdr p)))
1827 (nreverse props) "\n")
1828 "\n:END:")))
1831 ;;;; Quote Section
1833 (defun org-element-quote-section-parser (limit)
1834 "Parse a quote section.
1836 LIMIT bounds the search.
1838 Return a list whose CAR is `quote-section' and CDR is a plist
1839 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1841 Assume point is at beginning of the section."
1842 (save-excursion
1843 (let* ((begin (point))
1844 (end (progn (org-with-limited-levels (outline-next-heading))
1845 (point)))
1846 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1847 (forward-line)
1848 (point)))
1849 (value (buffer-substring-no-properties begin pos-before-blank)))
1850 (list 'quote-section
1851 (list :begin begin
1852 :end end
1853 :value value
1854 :post-blank (count-lines pos-before-blank end))))))
1856 (defun org-element-quote-section-interpreter (quote-section contents)
1857 "Interpret QUOTE-SECTION element as Org syntax.
1858 CONTENTS is nil."
1859 (org-element-property :value quote-section))
1862 ;;;; Src Block
1864 (defun org-element-src-block-parser (limit)
1865 "Parse a src block.
1867 LIMIT bounds the search.
1869 Return a list whose CAR is `src-block' and CDR is a plist
1870 containing `:language', `:switches', `:parameters', `:begin',
1871 `:end', `:hiddenp', `:number-lines', `:retain-labels',
1872 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
1873 `:post-blank' keywords.
1875 Assume point is at the beginning of the block."
1876 (let ((case-fold-search t))
1877 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC" limit t)))
1878 ;; Incomplete block: parse it as a comment.
1879 (org-element-comment-parser limit)
1880 (let ((contents-end (match-beginning 0)))
1881 (save-excursion
1882 (let* ((keywords (org-element-collect-affiliated-keywords))
1883 ;; Get beginning position.
1884 (begin (car keywords))
1885 ;; Get language as a string.
1886 (language
1887 (progn
1888 (looking-at
1889 (concat "^[ \t]*#\\+BEGIN_SRC"
1890 "\\(?: +\\(\\S-+\\)\\)?"
1891 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
1892 "\\(.*\\)[ \t]*$"))
1893 (org-match-string-no-properties 1)))
1894 ;; Get switches.
1895 (switches (org-match-string-no-properties 2))
1896 ;; Get parameters.
1897 (parameters (org-match-string-no-properties 3))
1898 ;; Switches analysis
1899 (number-lines (cond ((not switches) nil)
1900 ((string-match "-n\\>" switches) 'new)
1901 ((string-match "+n\\>" switches) 'continued)))
1902 (preserve-indent (and switches (string-match "-i\\>" switches)))
1903 (label-fmt (and switches
1904 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1905 (match-string 1 switches)))
1906 ;; Should labels be retained in (or stripped from)
1907 ;; src blocks?
1908 (retain-labels
1909 (or (not switches)
1910 (not (string-match "-r\\>" switches))
1911 (and number-lines (string-match "-k\\>" switches))))
1912 ;; What should code-references use - labels or
1913 ;; line-numbers?
1914 (use-labels
1915 (or (not switches)
1916 (and retain-labels (not (string-match "-k\\>" switches)))))
1917 ;; Get visibility status.
1918 (hidden (progn (forward-line) (org-invisible-p2)))
1919 ;; Retrieve code.
1920 (value (buffer-substring-no-properties (point) contents-end))
1921 (pos-before-blank (progn (goto-char contents-end)
1922 (forward-line)
1923 (point)))
1924 ;; Get position after ending blank lines.
1925 (end (progn (org-skip-whitespace)
1926 (if (eobp) (point) (point-at-bol)))))
1927 (list 'src-block
1928 (nconc
1929 (list :language language
1930 :switches (and (org-string-nw-p switches)
1931 (org-trim switches))
1932 :parameters (and (org-string-nw-p parameters)
1933 (org-trim parameters))
1934 :begin begin
1935 :end end
1936 :number-lines number-lines
1937 :preserve-indent preserve-indent
1938 :retain-labels retain-labels
1939 :use-labels use-labels
1940 :label-fmt label-fmt
1941 :hiddenp hidden
1942 :value value
1943 :post-blank (count-lines pos-before-blank end))
1944 (cadr keywords)))))))))
1946 (defun org-element-src-block-interpreter (src-block contents)
1947 "Interpret SRC-BLOCK element as Org syntax.
1948 CONTENTS is nil."
1949 (let ((lang (org-element-property :language src-block))
1950 (switches (org-element-property :switches src-block))
1951 (params (org-element-property :parameters src-block))
1952 (value (let ((val (org-element-property :value src-block)))
1953 (cond
1955 (org-src-preserve-indentation val)
1956 ((zerop org-edit-src-content-indentation)
1957 (org-remove-indentation val))
1959 (let ((ind (make-string
1960 org-edit-src-content-indentation 32)))
1961 (replace-regexp-in-string
1962 "\\(^\\)[ \t]*\\S-" ind
1963 (org-remove-indentation val) nil nil 1)))))))
1964 (concat (format "#+BEGIN_SRC%s\n"
1965 (concat (and lang (concat " " lang))
1966 (and switches (concat " " switches))
1967 (and params (concat " " params))))
1968 value
1969 "#+END_SRC")))
1972 ;;;; Table
1974 (defun org-element-table-parser (limit)
1975 "Parse a table at point.
1977 LIMIT bounds the search.
1979 Return a list whose CAR is `table' and CDR is a plist containing
1980 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
1981 `:contents-end', `:value' and `:post-blank' keywords.
1983 Assume point is at the beginning of the table."
1984 (save-excursion
1985 (let* ((case-fold-search t)
1986 (table-begin (point))
1987 (type (if (org-at-table.el-p) 'table.el 'org))
1988 (keywords (org-element-collect-affiliated-keywords))
1989 (begin (car keywords))
1990 (table-end (goto-char (marker-position (org-table-end t))))
1991 (tblfm (let (acc)
1992 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
1993 (push (org-match-string-no-properties 1) acc)
1994 (forward-line))
1995 acc))
1996 (pos-before-blank (point))
1997 (end (progn (org-skip-whitespace)
1998 (if (eobp) (point) (point-at-bol)))))
1999 (list 'table
2000 (nconc
2001 (list :begin begin
2002 :end end
2003 :type type
2004 :tblfm tblfm
2005 ;; Only `org' tables have contents. `table.el' tables
2006 ;; use a `:value' property to store raw table as
2007 ;; a string.
2008 :contents-begin (and (eq type 'org) table-begin)
2009 :contents-end (and (eq type 'org) table-end)
2010 :value (and (eq type 'table.el)
2011 (buffer-substring-no-properties
2012 table-begin table-end))
2013 :post-blank (count-lines pos-before-blank end))
2014 (cadr keywords))))))
2016 (defun org-element-table-interpreter (table contents)
2017 "Interpret TABLE element as Org syntax.
2018 CONTENTS is nil."
2019 (if (eq (org-element-property :type table) 'table.el)
2020 (org-remove-indentation (org-element-property :value table))
2021 (concat (with-temp-buffer (insert contents)
2022 (org-table-align)
2023 (buffer-string))
2024 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2025 (reverse (org-element-property :tblfm table))
2026 "\n"))))
2029 ;;;; Table Row
2031 (defun org-element-table-row-parser (limit)
2032 "Parse table row at point.
2034 LIMIT bounds the search.
2036 Return a list whose CAR is `table-row' and CDR is a plist
2037 containing `:begin', `:end', `:contents-begin', `:contents-end',
2038 `:type' and `:post-blank' keywords."
2039 (save-excursion
2040 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2041 (begin (point))
2042 ;; A table rule has no contents. In that case, ensure
2043 ;; CONTENTS-BEGIN matches CONTENTS-END.
2044 (contents-begin (and (eq type 'standard)
2045 (search-forward "|")
2046 (point)))
2047 (contents-end (and (eq type 'standard)
2048 (progn
2049 (end-of-line)
2050 (skip-chars-backward " \t")
2051 (point))))
2052 (end (progn (forward-line) (point))))
2053 (list 'table-row
2054 (list :type type
2055 :begin begin
2056 :end end
2057 :contents-begin contents-begin
2058 :contents-end contents-end
2059 :post-blank 0)))))
2061 (defun org-element-table-row-interpreter (table-row contents)
2062 "Interpret TABLE-ROW element as Org syntax.
2063 CONTENTS is the contents of the table row."
2064 (if (eq (org-element-property :type table-row) 'rule) "|-"
2065 (concat "| " contents)))
2068 ;;;; Verse Block
2070 (defun org-element-verse-block-parser (limit)
2071 "Parse a verse block.
2073 LIMIT bounds the search.
2075 Return a list whose CAR is `verse-block' and CDR is a plist
2076 containing `:begin', `:end', `:contents-begin', `:contents-end',
2077 `:hiddenp' and `:post-blank' keywords.
2079 Assume point is at beginning of the block."
2080 (let ((case-fold-search t))
2081 (if (not (save-excursion
2082 (re-search-forward "^[ \t]*#\\+END_VERSE" limit t)))
2083 ;; Incomplete block: parse it as a comment.
2084 (org-element-comment-parser limit)
2085 (let ((contents-end (match-beginning 0)))
2086 (save-excursion
2087 (let* ((keywords (org-element-collect-affiliated-keywords))
2088 (begin (car keywords))
2089 (hidden (progn (forward-line) (org-invisible-p2)))
2090 (contents-begin (point))
2091 (pos-before-blank (progn (goto-char contents-end)
2092 (forward-line)
2093 (point)))
2094 (end (progn (org-skip-whitespace)
2095 (if (eobp) (point) (point-at-bol)))))
2096 (list 'verse-block
2097 (nconc
2098 (list :begin begin
2099 :end end
2100 :contents-begin contents-begin
2101 :contents-end contents-end
2102 :hiddenp hidden
2103 :post-blank (count-lines pos-before-blank end))
2104 (cadr keywords)))))))))
2106 (defun org-element-verse-block-interpreter (verse-block contents)
2107 "Interpret VERSE-BLOCK element as Org syntax.
2108 CONTENTS is verse block contents."
2109 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2113 ;;; Objects
2115 ;; Unlike to elements, interstices can be found between objects.
2116 ;; That's why, along with the parser, successor functions are provided
2117 ;; for each object. Some objects share the same successor (i.e. `code'
2118 ;; and `verbatim' objects).
2120 ;; A successor must accept a single argument bounding the search. It
2121 ;; will return either a cons cell whose CAR is the object's type, as
2122 ;; a symbol, and CDR the position of its next occurrence, or nil.
2124 ;; Successors follow the naming convention:
2125 ;; org-element-NAME-successor, where NAME is the name of the
2126 ;; successor, as defined in `org-element-all-successors'.
2128 ;; Some object types (i.e. `emphasis') are recursive. Restrictions on
2129 ;; object types they can contain will be specified in
2130 ;; `org-element-object-restrictions'.
2132 ;; Adding a new type of object is simple. Implement a successor,
2133 ;; a parser, and an interpreter for it, all following the naming
2134 ;; convention. Register type in `org-element-all-objects' and
2135 ;; successor in `org-element-all-successors'. Maybe tweak
2136 ;; restrictions about it, and that's it.
2139 ;;;; Bold
2141 (defun org-element-bold-parser ()
2142 "Parse bold object at point.
2144 Return a list whose CAR is `bold' and CDR is a plist with
2145 `:begin', `:end', `:contents-begin' and `:contents-end' and
2146 `:post-blank' keywords.
2148 Assume point is at the first star marker."
2149 (save-excursion
2150 (unless (bolp) (backward-char 1))
2151 (looking-at org-emph-re)
2152 (let ((begin (match-beginning 2))
2153 (contents-begin (match-beginning 4))
2154 (contents-end (match-end 4))
2155 (post-blank (progn (goto-char (match-end 2))
2156 (skip-chars-forward " \t")))
2157 (end (point)))
2158 (list 'bold
2159 (list :begin begin
2160 :end end
2161 :contents-begin contents-begin
2162 :contents-end contents-end
2163 :post-blank post-blank)))))
2165 (defun org-element-bold-interpreter (bold contents)
2166 "Interpret BOLD object as Org syntax.
2167 CONTENTS is the contents of the object."
2168 (format "*%s*" contents))
2170 (defun org-element-text-markup-successor (limit)
2171 "Search for the next text-markup object.
2173 LIMIT bounds the search.
2175 LIMIT bounds the search.
2177 Return value is a cons cell whose CAR is a symbol among `bold',
2178 `italic', `underline', `strike-through', `code' and `verbatim'
2179 and CDR is beginning position."
2180 (save-excursion
2181 (unless (bolp) (backward-char))
2182 (when (re-search-forward org-emph-re limit t)
2183 (let ((marker (match-string 3)))
2184 (cons (cond
2185 ((equal marker "*") 'bold)
2186 ((equal marker "/") 'italic)
2187 ((equal marker "_") 'underline)
2188 ((equal marker "+") 'strike-through)
2189 ((equal marker "~") 'code)
2190 ((equal marker "=") 'verbatim)
2191 (t (error "Unknown marker at %d" (match-beginning 3))))
2192 (match-beginning 2))))))
2195 ;;;; Code
2197 (defun org-element-code-parser ()
2198 "Parse code object at point.
2200 Return a list whose CAR is `code' and CDR is a plist with
2201 `:value', `:begin', `:end' and `:post-blank' keywords.
2203 Assume point is at the first tilde marker."
2204 (save-excursion
2205 (unless (bolp) (backward-char 1))
2206 (looking-at org-emph-re)
2207 (let ((begin (match-beginning 2))
2208 (value (org-match-string-no-properties 4))
2209 (post-blank (progn (goto-char (match-end 2))
2210 (skip-chars-forward " \t")))
2211 (end (point)))
2212 (list 'code
2213 (list :value value
2214 :begin begin
2215 :end end
2216 :post-blank post-blank)))))
2218 (defun org-element-code-interpreter (code contents)
2219 "Interpret CODE object as Org syntax.
2220 CONTENTS is nil."
2221 (format "~%s~" (org-element-property :value code)))
2224 ;;;; Entity
2226 (defun org-element-entity-parser ()
2227 "Parse entity at point.
2229 Return a list whose CAR is `entity' and CDR a plist with
2230 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2231 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2232 keywords.
2234 Assume point is at the beginning of the entity."
2235 (save-excursion
2236 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2237 (let* ((value (org-entity-get (match-string 1)))
2238 (begin (match-beginning 0))
2239 (bracketsp (string= (match-string 2) "{}"))
2240 (post-blank (progn (goto-char (match-end 1))
2241 (when bracketsp (forward-char 2))
2242 (skip-chars-forward " \t")))
2243 (end (point)))
2244 (list 'entity
2245 (list :name (car value)
2246 :latex (nth 1 value)
2247 :latex-math-p (nth 2 value)
2248 :html (nth 3 value)
2249 :ascii (nth 4 value)
2250 :latin1 (nth 5 value)
2251 :utf-8 (nth 6 value)
2252 :begin begin
2253 :end end
2254 :use-brackets-p bracketsp
2255 :post-blank post-blank)))))
2257 (defun org-element-entity-interpreter (entity contents)
2258 "Interpret ENTITY object as Org syntax.
2259 CONTENTS is nil."
2260 (concat "\\"
2261 (org-element-property :name entity)
2262 (when (org-element-property :use-brackets-p entity) "{}")))
2264 (defun org-element-latex-or-entity-successor (limit)
2265 "Search for the next latex-fragment or entity object.
2267 LIMIT bounds the search.
2269 Return value is a cons cell whose CAR is `entity' or
2270 `latex-fragment' and CDR is beginning position."
2271 (save-excursion
2272 (let ((matchers (plist-get org-format-latex-options :matchers))
2273 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2274 (entity-re
2275 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2276 (when (re-search-forward
2277 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
2278 matchers "\\|")
2279 "\\|" entity-re)
2280 limit t)
2281 (goto-char (match-beginning 0))
2282 (if (looking-at entity-re)
2283 ;; Determine if it's a real entity or a LaTeX command.
2284 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2285 (match-beginning 0))
2286 ;; No entity nor command: point is at a LaTeX fragment.
2287 ;; Determine its type to get the correct beginning position.
2288 (cons 'latex-fragment
2289 (catch 'return
2290 (mapc (lambda (e)
2291 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
2292 (throw 'return
2293 (match-beginning
2294 (nth 2 (assoc e org-latex-regexps))))))
2295 matchers)
2296 (point))))))))
2299 ;;;; Export Snippet
2301 (defun org-element-export-snippet-parser ()
2302 "Parse export snippet at point.
2304 Return a list whose CAR is `export-snippet' and CDR a plist with
2305 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2306 keywords.
2308 Assume point is at the beginning of the snippet."
2309 (save-excursion
2310 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2311 (let* ((begin (match-beginning 0))
2312 (back-end (org-match-string-no-properties 1))
2313 (value (buffer-substring-no-properties
2314 (point)
2315 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2316 (post-blank (skip-chars-forward " \t"))
2317 (end (point)))
2318 (list 'export-snippet
2319 (list :back-end back-end
2320 :value value
2321 :begin begin
2322 :end end
2323 :post-blank post-blank)))))
2325 (defun org-element-export-snippet-interpreter (export-snippet contents)
2326 "Interpret EXPORT-SNIPPET object as Org syntax.
2327 CONTENTS is nil."
2328 (format "@@%s:%s@@"
2329 (org-element-property :back-end export-snippet)
2330 (org-element-property :value export-snippet)))
2332 (defun org-element-export-snippet-successor (limit)
2333 "Search for the next export-snippet object.
2335 LIMIT bounds the search.
2337 Return value is a cons cell whose CAR is `export-snippet' and CDR
2338 its beginning position."
2339 (save-excursion
2340 (let (beg)
2341 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
2342 (setq beg (match-beginning 0))
2343 (re-search-forward "@@" limit t))
2344 (cons 'export-snippet beg)))))
2347 ;;;; Footnote Reference
2349 (defun org-element-footnote-reference-parser ()
2350 "Parse footnote reference at point.
2352 Return a list whose CAR is `footnote-reference' and CDR a plist
2353 with `:label', `:type', `:inline-definition', `:begin', `:end'
2354 and `:post-blank' as keywords."
2355 (save-excursion
2356 (looking-at org-footnote-re)
2357 (let* ((begin (point))
2358 (label (or (org-match-string-no-properties 2)
2359 (org-match-string-no-properties 3)
2360 (and (match-string 1)
2361 (concat "fn:" (org-match-string-no-properties 1)))))
2362 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2363 (inner-begin (match-end 0))
2364 (inner-end
2365 (let ((count 1))
2366 (forward-char)
2367 (while (and (> count 0) (re-search-forward "[][]" nil t))
2368 (if (equal (match-string 0) "[") (incf count) (decf count)))
2369 (1- (point))))
2370 (post-blank (progn (goto-char (1+ inner-end))
2371 (skip-chars-forward " \t")))
2372 (end (point))
2373 (footnote-reference
2374 (list 'footnote-reference
2375 (list :label label
2376 :type type
2377 :begin begin
2378 :end end
2379 :post-blank post-blank))))
2380 (org-element-put-property
2381 footnote-reference :inline-definition
2382 (and (eq type 'inline)
2383 (org-element-parse-secondary-string
2384 (buffer-substring inner-begin inner-end)
2385 (org-element-restriction 'footnote-reference)
2386 footnote-reference))))))
2388 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2389 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2390 CONTENTS is nil."
2391 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2392 (def
2393 (let ((inline-def
2394 (org-element-property :inline-definition footnote-reference)))
2395 (if (not inline-def) ""
2396 (concat ":" (org-element-interpret-data inline-def))))))
2397 (format "[%s]" (concat label def))))
2399 (defun org-element-footnote-reference-successor (limit)
2400 "Search for the next footnote-reference object.
2402 LIMIT bounds the search.
2404 Return value is a cons cell whose CAR is `footnote-reference' and
2405 CDR is beginning position."
2406 (save-excursion
2407 (catch 'exit
2408 (while (re-search-forward org-footnote-re limit t)
2409 (save-excursion
2410 (let ((beg (match-beginning 0))
2411 (count 1))
2412 (backward-char)
2413 (while (re-search-forward "[][]" limit t)
2414 (if (equal (match-string 0) "[") (incf count) (decf count))
2415 (when (zerop count)
2416 (throw 'exit (cons 'footnote-reference beg))))))))))
2419 ;;;; Inline Babel Call
2421 (defun org-element-inline-babel-call-parser ()
2422 "Parse inline babel call at point.
2424 Return a list whose CAR is `inline-babel-call' and CDR a plist
2425 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2427 Assume point is at the beginning of the babel call."
2428 (save-excursion
2429 (unless (bolp) (backward-char))
2430 (looking-at org-babel-inline-lob-one-liner-regexp)
2431 (let ((info (save-match-data (org-babel-lob-get-info)))
2432 (begin (match-end 1))
2433 (post-blank (progn (goto-char (match-end 0))
2434 (skip-chars-forward " \t")))
2435 (end (point)))
2436 (list 'inline-babel-call
2437 (list :begin begin
2438 :end end
2439 :info info
2440 :post-blank post-blank)))))
2442 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2443 "Interpret INLINE-BABEL-CALL object as Org syntax.
2444 CONTENTS is nil."
2445 (let* ((babel-info (org-element-property :info inline-babel-call))
2446 (main-source (car babel-info))
2447 (post-options (nth 1 babel-info)))
2448 (concat "call_"
2449 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2450 ;; Remove redundant square brackets.
2451 (replace-match
2452 (match-string 1 main-source) nil nil main-source)
2453 main-source)
2454 (and post-options (format "[%s]" post-options)))))
2456 (defun org-element-inline-babel-call-successor (limit)
2457 "Search for the next inline-babel-call object.
2459 LIMIT bounds the search.
2461 Return value is a cons cell whose CAR is `inline-babel-call' and
2462 CDR is beginning position."
2463 (save-excursion
2464 ;; Use a simplified version of
2465 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
2466 (when (re-search-forward
2467 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
2468 limit t)
2469 (cons 'inline-babel-call (match-beginning 0)))))
2472 ;;;; Inline Src Block
2474 (defun org-element-inline-src-block-parser ()
2475 "Parse inline source block at point.
2477 LIMIT bounds the search.
2479 Return a list whose CAR is `inline-src-block' and CDR a plist
2480 with `:begin', `:end', `:language', `:value', `:parameters' and
2481 `:post-blank' as keywords.
2483 Assume point is at the beginning of the inline src block."
2484 (save-excursion
2485 (unless (bolp) (backward-char))
2486 (looking-at org-babel-inline-src-block-regexp)
2487 (let ((begin (match-beginning 1))
2488 (language (org-match-string-no-properties 2))
2489 (parameters (org-match-string-no-properties 4))
2490 (value (org-match-string-no-properties 5))
2491 (post-blank (progn (goto-char (match-end 0))
2492 (skip-chars-forward " \t")))
2493 (end (point)))
2494 (list 'inline-src-block
2495 (list :language language
2496 :value value
2497 :parameters parameters
2498 :begin begin
2499 :end end
2500 :post-blank post-blank)))))
2502 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2503 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2504 CONTENTS is nil."
2505 (let ((language (org-element-property :language inline-src-block))
2506 (arguments (org-element-property :parameters inline-src-block))
2507 (body (org-element-property :value inline-src-block)))
2508 (format "src_%s%s{%s}"
2509 language
2510 (if arguments (format "[%s]" arguments) "")
2511 body)))
2513 (defun org-element-inline-src-block-successor (limit)
2514 "Search for the next inline-babel-call element.
2516 LIMIT bounds the search.
2518 Return value is a cons cell whose CAR is `inline-babel-call' and
2519 CDR is beginning position."
2520 (save-excursion
2521 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
2522 (cons 'inline-src-block (match-beginning 1)))))
2524 ;;;; Italic
2526 (defun org-element-italic-parser ()
2527 "Parse italic object at point.
2529 Return a list whose CAR is `italic' and CDR is a plist with
2530 `:begin', `:end', `:contents-begin' and `:contents-end' and
2531 `:post-blank' keywords.
2533 Assume point is at the first slash marker."
2534 (save-excursion
2535 (unless (bolp) (backward-char 1))
2536 (looking-at org-emph-re)
2537 (let ((begin (match-beginning 2))
2538 (contents-begin (match-beginning 4))
2539 (contents-end (match-end 4))
2540 (post-blank (progn (goto-char (match-end 2))
2541 (skip-chars-forward " \t")))
2542 (end (point)))
2543 (list 'italic
2544 (list :begin begin
2545 :end end
2546 :contents-begin contents-begin
2547 :contents-end contents-end
2548 :post-blank post-blank)))))
2550 (defun org-element-italic-interpreter (italic contents)
2551 "Interpret ITALIC object as Org syntax.
2552 CONTENTS is the contents of the object."
2553 (format "/%s/" contents))
2556 ;;;; Latex Fragment
2558 (defun org-element-latex-fragment-parser ()
2559 "Parse latex fragment at point.
2561 Return a list whose CAR is `latex-fragment' and CDR a plist with
2562 `:value', `:begin', `:end', and `:post-blank' as keywords.
2564 Assume point is at the beginning of the latex fragment."
2565 (save-excursion
2566 (let* ((begin (point))
2567 (substring-match
2568 (catch 'exit
2569 (mapc (lambda (e)
2570 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
2571 (when (or (looking-at latex-regexp)
2572 (and (not (bobp))
2573 (save-excursion
2574 (backward-char)
2575 (looking-at latex-regexp))))
2576 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
2577 (plist-get org-format-latex-options :matchers))
2578 ;; None found: it's a macro.
2579 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2581 (value (match-string-no-properties substring-match))
2582 (post-blank (progn (goto-char (match-end substring-match))
2583 (skip-chars-forward " \t")))
2584 (end (point)))
2585 (list 'latex-fragment
2586 (list :value value
2587 :begin begin
2588 :end end
2589 :post-blank post-blank)))))
2591 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
2592 "Interpret LATEX-FRAGMENT object as Org syntax.
2593 CONTENTS is nil."
2594 (org-element-property :value latex-fragment))
2596 ;;;; Line Break
2598 (defun org-element-line-break-parser ()
2599 "Parse line break at point.
2601 Return a list whose CAR is `line-break', and CDR a plist with
2602 `:begin', `:end' and `:post-blank' keywords.
2604 Assume point is at the beginning of the line break."
2605 (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
2607 (defun org-element-line-break-interpreter (line-break contents)
2608 "Interpret LINE-BREAK object as Org syntax.
2609 CONTENTS is nil."
2610 "\\\\")
2612 (defun org-element-line-break-successor (limit)
2613 "Search for the next line-break object.
2615 LIMIT bounds the search.
2617 Return value is a cons cell whose CAR is `line-break' and CDR is
2618 beginning position."
2619 (save-excursion
2620 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
2621 (goto-char (match-beginning 1)))))
2622 ;; A line break can only happen on a non-empty line.
2623 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
2624 (cons 'line-break beg)))))
2627 ;;;; Link
2629 (defun org-element-link-parser ()
2630 "Parse link at point.
2632 Return a list whose CAR is `link' and CDR a plist with `:type',
2633 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2634 `:contents-end' and `:post-blank' as keywords.
2636 Assume point is at the beginning of the link."
2637 (save-excursion
2638 (let ((begin (point))
2639 end contents-begin contents-end link-end post-blank path type
2640 raw-link link)
2641 (cond
2642 ;; Type 1: Text targeted from a radio target.
2643 ((and org-target-link-regexp (looking-at org-target-link-regexp))
2644 (setq type "radio"
2645 link-end (match-end 0)
2646 path (org-match-string-no-properties 0)))
2647 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2648 ((looking-at org-bracket-link-regexp)
2649 (setq contents-begin (match-beginning 3)
2650 contents-end (match-end 3)
2651 link-end (match-end 0)
2652 ;; RAW-LINK is the original link.
2653 raw-link (org-match-string-no-properties 1)
2654 link (org-translate-link
2655 (org-link-expand-abbrev
2656 (org-link-unescape raw-link))))
2657 ;; Determine TYPE of link and set PATH accordingly.
2658 (cond
2659 ;; File type.
2660 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
2661 (setq type "file" path link))
2662 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2663 ((string-match org-link-re-with-space3 link)
2664 (setq type (match-string 1 link) path (match-string 2 link)))
2665 ;; Id type: PATH is the id.
2666 ((string-match "^id:\\([-a-f0-9]+\\)" link)
2667 (setq type "id" path (match-string 1 link)))
2668 ;; Code-ref type: PATH is the name of the reference.
2669 ((string-match "^(\\(.*\\))$" link)
2670 (setq type "coderef" path (match-string 1 link)))
2671 ;; Custom-id type: PATH is the name of the custom id.
2672 ((= (aref link 0) ?#)
2673 (setq type "custom-id" path (substring link 1)))
2674 ;; Fuzzy type: Internal link either matches a target, an
2675 ;; headline name or nothing. PATH is the target or
2676 ;; headline's name.
2677 (t (setq type "fuzzy" path link))))
2678 ;; Type 3: Plain link, i.e. http://orgmode.org
2679 ((looking-at org-plain-link-re)
2680 (setq raw-link (org-match-string-no-properties 0)
2681 type (org-match-string-no-properties 1)
2682 path (org-match-string-no-properties 2)
2683 link-end (match-end 0)))
2684 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2685 ((looking-at org-angle-link-re)
2686 (setq raw-link (buffer-substring-no-properties
2687 (match-beginning 1) (match-end 2))
2688 type (org-match-string-no-properties 1)
2689 path (org-match-string-no-properties 2)
2690 link-end (match-end 0))))
2691 ;; In any case, deduce end point after trailing white space from
2692 ;; LINK-END variable.
2693 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
2694 end (point))
2695 (list 'link
2696 (list :type type
2697 :path path
2698 :raw-link (or raw-link path)
2699 :begin begin
2700 :end end
2701 :contents-begin contents-begin
2702 :contents-end contents-end
2703 :post-blank post-blank)))))
2705 (defun org-element-link-interpreter (link contents)
2706 "Interpret LINK object as Org syntax.
2707 CONTENTS is the contents of the object, or nil."
2708 (let ((type (org-element-property :type link))
2709 (raw-link (org-element-property :raw-link link)))
2710 (if (string= type "radio") raw-link
2711 (format "[[%s]%s]"
2712 raw-link
2713 (if contents (format "[%s]" contents) "")))))
2715 (defun org-element-link-successor (limit)
2716 "Search for the next link object.
2718 LIMIT bounds the search.
2720 Return value is a cons cell whose CAR is `link' and CDR is
2721 beginning position."
2722 (save-excursion
2723 (let ((link-regexp
2724 (if (not org-target-link-regexp) org-any-link-re
2725 (concat org-any-link-re "\\|" org-target-link-regexp))))
2726 (when (re-search-forward link-regexp limit t)
2727 (cons 'link (match-beginning 0))))))
2730 ;;;; Macro
2732 (defun org-element-macro-parser ()
2733 "Parse macro at point.
2735 Return a list whose CAR is `macro' and CDR a plist with `:key',
2736 `:args', `:begin', `:end', `:value' and `:post-blank' as
2737 keywords.
2739 Assume point is at the macro."
2740 (save-excursion
2741 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2742 (let ((begin (point))
2743 (key (downcase (org-match-string-no-properties 1)))
2744 (value (org-match-string-no-properties 0))
2745 (post-blank (progn (goto-char (match-end 0))
2746 (skip-chars-forward " \t")))
2747 (end (point))
2748 (args (let ((args (org-match-string-no-properties 3)) args2)
2749 (when args
2750 (setq args (org-split-string args ","))
2751 (while args
2752 (while (string-match "\\\\\\'" (car args))
2753 ;; Repair bad splits.
2754 (setcar (cdr args) (concat (substring (car args) 0 -1)
2755 "," (nth 1 args)))
2756 (pop args))
2757 (push (pop args) args2))
2758 (mapcar 'org-trim (nreverse args2))))))
2759 (list 'macro
2760 (list :key key
2761 :value value
2762 :args args
2763 :begin begin
2764 :end end
2765 :post-blank post-blank)))))
2767 (defun org-element-macro-interpreter (macro contents)
2768 "Interpret MACRO object as Org syntax.
2769 CONTENTS is nil."
2770 (org-element-property :value macro))
2772 (defun org-element-macro-successor (limit)
2773 "Search for the next macro object.
2775 LIMIT bounds the search.
2777 Return value is cons cell whose CAR is `macro' and CDR is
2778 beginning position."
2779 (save-excursion
2780 (when (re-search-forward
2781 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2782 limit t)
2783 (cons 'macro (match-beginning 0)))))
2786 ;;;; Radio-target
2788 (defun org-element-radio-target-parser ()
2789 "Parse radio target at point.
2791 Return a list whose CAR is `radio-target' and CDR a plist with
2792 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2793 and `:post-blank' as keywords.
2795 Assume point is at the radio target."
2796 (save-excursion
2797 (looking-at org-radio-target-regexp)
2798 (let ((begin (point))
2799 (contents-begin (match-beginning 1))
2800 (contents-end (match-end 1))
2801 (value (org-match-string-no-properties 1))
2802 (post-blank (progn (goto-char (match-end 0))
2803 (skip-chars-forward " \t")))
2804 (end (point)))
2805 (list 'radio-target
2806 (list :begin begin
2807 :end end
2808 :contents-begin contents-begin
2809 :contents-end contents-end
2810 :post-blank post-blank
2811 :value value)))))
2813 (defun org-element-radio-target-interpreter (target contents)
2814 "Interpret TARGET object as Org syntax.
2815 CONTENTS is the contents of the object."
2816 (concat "<<<" contents ">>>"))
2818 (defun org-element-radio-target-successor (limit)
2819 "Search for the next radio-target object.
2821 LIMIT bounds the search.
2823 Return value is a cons cell whose CAR is `radio-target' and CDR
2824 is beginning position."
2825 (save-excursion
2826 (when (re-search-forward org-radio-target-regexp limit t)
2827 (cons 'radio-target (match-beginning 0)))))
2830 ;;;; Statistics Cookie
2832 (defun org-element-statistics-cookie-parser ()
2833 "Parse statistics cookie at point.
2835 Return a list whose CAR is `statistics-cookie', and CDR a plist
2836 with `:begin', `:end', `:value' and `:post-blank' keywords.
2838 Assume point is at the beginning of the statistics-cookie."
2839 (save-excursion
2840 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2841 (let* ((begin (point))
2842 (value (buffer-substring-no-properties
2843 (match-beginning 0) (match-end 0)))
2844 (post-blank (progn (goto-char (match-end 0))
2845 (skip-chars-forward " \t")))
2846 (end (point)))
2847 (list 'statistics-cookie
2848 (list :begin begin
2849 :end end
2850 :value value
2851 :post-blank post-blank)))))
2853 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
2854 "Interpret STATISTICS-COOKIE object as Org syntax.
2855 CONTENTS is nil."
2856 (org-element-property :value statistics-cookie))
2858 (defun org-element-statistics-cookie-successor (limit)
2859 "Search for the next statistics cookie object.
2861 LIMIT bounds the search.
2863 Return value is a cons cell whose CAR is `statistics-cookie' and
2864 CDR is beginning position."
2865 (save-excursion
2866 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
2867 (cons 'statistics-cookie (match-beginning 0)))))
2870 ;;;; Strike-Through
2872 (defun org-element-strike-through-parser ()
2873 "Parse strike-through object at point.
2875 Return a list whose CAR is `strike-through' and CDR is a plist
2876 with `:begin', `:end', `:contents-begin' and `:contents-end' and
2877 `:post-blank' keywords.
2879 Assume point is at the first plus sign marker."
2880 (save-excursion
2881 (unless (bolp) (backward-char 1))
2882 (looking-at org-emph-re)
2883 (let ((begin (match-beginning 2))
2884 (contents-begin (match-beginning 4))
2885 (contents-end (match-end 4))
2886 (post-blank (progn (goto-char (match-end 2))
2887 (skip-chars-forward " \t")))
2888 (end (point)))
2889 (list 'strike-through
2890 (list :begin begin
2891 :end end
2892 :contents-begin contents-begin
2893 :contents-end contents-end
2894 :post-blank post-blank)))))
2896 (defun org-element-strike-through-interpreter (strike-through contents)
2897 "Interpret STRIKE-THROUGH object as Org syntax.
2898 CONTENTS is the contents of the object."
2899 (format "+%s+" contents))
2902 ;;;; Subscript
2904 (defun org-element-subscript-parser ()
2905 "Parse subscript at point.
2907 Return a list whose CAR is `subscript' and CDR a plist with
2908 `:begin', `:end', `:contents-begin', `:contents-end',
2909 `:use-brackets-p' and `:post-blank' as keywords.
2911 Assume point is at the underscore."
2912 (save-excursion
2913 (unless (bolp) (backward-char))
2914 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
2916 (not (looking-at org-match-substring-regexp))))
2917 (begin (match-beginning 2))
2918 (contents-begin (or (match-beginning 5)
2919 (match-beginning 3)))
2920 (contents-end (or (match-end 5) (match-end 3)))
2921 (post-blank (progn (goto-char (match-end 0))
2922 (skip-chars-forward " \t")))
2923 (end (point)))
2924 (list 'subscript
2925 (list :begin begin
2926 :end end
2927 :use-brackets-p bracketsp
2928 :contents-begin contents-begin
2929 :contents-end contents-end
2930 :post-blank post-blank)))))
2932 (defun org-element-subscript-interpreter (subscript contents)
2933 "Interpret SUBSCRIPT object as Org syntax.
2934 CONTENTS is the contents of the object."
2935 (format
2936 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
2937 contents))
2939 (defun org-element-sub/superscript-successor (limit)
2940 "Search for the next sub/superscript object.
2942 LIMIT bounds the search.
2944 Return value is a cons cell whose CAR is either `subscript' or
2945 `superscript' and CDR is beginning position."
2946 (save-excursion
2947 (when (re-search-forward org-match-substring-regexp limit t)
2948 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
2949 (match-beginning 2)))))
2952 ;;;; Superscript
2954 (defun org-element-superscript-parser ()
2955 "Parse superscript at point.
2957 Return a list whose CAR is `superscript' and CDR a plist with
2958 `:begin', `:end', `:contents-begin', `:contents-end',
2959 `:use-brackets-p' and `:post-blank' as keywords.
2961 Assume point is at the caret."
2962 (save-excursion
2963 (unless (bolp) (backward-char))
2964 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
2965 (not (looking-at org-match-substring-regexp))))
2966 (begin (match-beginning 2))
2967 (contents-begin (or (match-beginning 5)
2968 (match-beginning 3)))
2969 (contents-end (or (match-end 5) (match-end 3)))
2970 (post-blank (progn (goto-char (match-end 0))
2971 (skip-chars-forward " \t")))
2972 (end (point)))
2973 (list 'superscript
2974 (list :begin begin
2975 :end end
2976 :use-brackets-p bracketsp
2977 :contents-begin contents-begin
2978 :contents-end contents-end
2979 :post-blank post-blank)))))
2981 (defun org-element-superscript-interpreter (superscript contents)
2982 "Interpret SUPERSCRIPT object as Org syntax.
2983 CONTENTS is the contents of the object."
2984 (format
2985 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
2986 contents))
2989 ;;;; Table Cell
2991 (defun org-element-table-cell-parser ()
2992 "Parse table cell at point.
2994 Return a list whose CAR is `table-cell' and CDR is a plist
2995 containing `:begin', `:end', `:contents-begin', `:contents-end'
2996 and `:post-blank' keywords."
2997 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
2998 (let* ((begin (match-beginning 0))
2999 (end (match-end 0))
3000 (contents-begin (match-beginning 1))
3001 (contents-end (match-end 1)))
3002 (list 'table-cell
3003 (list :begin begin
3004 :end end
3005 :contents-begin contents-begin
3006 :contents-end contents-end
3007 :post-blank 0))))
3009 (defun org-element-table-cell-interpreter (table-cell contents)
3010 "Interpret TABLE-CELL element as Org syntax.
3011 CONTENTS is the contents of the cell, or nil."
3012 (concat " " contents " |"))
3014 (defun org-element-table-cell-successor (limit)
3015 "Search for the next table-cell object.
3017 LIMIT bounds the search.
3019 Return value is a cons cell whose CAR is `table-cell' and CDR is
3020 beginning position."
3021 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
3024 ;;;; Target
3026 (defun org-element-target-parser ()
3027 "Parse target at point.
3029 Return a list whose CAR is `target' and CDR a plist with
3030 `:begin', `:end', `:value' and `:post-blank' as keywords.
3032 Assume point is at the target."
3033 (save-excursion
3034 (looking-at org-target-regexp)
3035 (let ((begin (point))
3036 (value (org-match-string-no-properties 1))
3037 (post-blank (progn (goto-char (match-end 0))
3038 (skip-chars-forward " \t")))
3039 (end (point)))
3040 (list 'target
3041 (list :begin begin
3042 :end end
3043 :value value
3044 :post-blank post-blank)))))
3046 (defun org-element-target-interpreter (target contents)
3047 "Interpret TARGET object as Org syntax.
3048 CONTENTS is nil."
3049 (format "<<%s>>" (org-element-property :value target)))
3051 (defun org-element-target-successor (limit)
3052 "Search for the next target object.
3054 LIMIT bounds the search.
3056 Return value is a cons cell whose CAR is `target' and CDR is
3057 beginning position."
3058 (save-excursion
3059 (when (re-search-forward org-target-regexp limit t)
3060 (cons 'target (match-beginning 0)))))
3063 ;;;; Timestamp
3065 (defun org-element-timestamp-parser ()
3066 "Parse time stamp at point.
3068 Return a list whose CAR is `timestamp', and CDR a plist with
3069 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3071 Assume point is at the beginning of the timestamp."
3072 (save-excursion
3073 (let* ((begin (point))
3074 (activep (eq (char-after) ?<))
3075 (main-value
3076 (progn
3077 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3078 (match-string-no-properties 1)))
3079 (range-end (match-string-no-properties 3))
3080 (type (cond ((match-string 2) 'diary)
3081 ((and activep range-end) 'active-range)
3082 (activep 'active)
3083 (range-end 'inactive-range)
3084 (t 'inactive)))
3085 (post-blank (progn (goto-char (match-end 0))
3086 (skip-chars-forward " \t")))
3087 (end (point)))
3088 (list 'timestamp
3089 (list :type type
3090 :value main-value
3091 :range-end range-end
3092 :begin begin
3093 :end end
3094 :post-blank post-blank)))))
3096 (defun org-element-timestamp-interpreter (timestamp contents)
3097 "Interpret TIMESTAMP object as Org syntax.
3098 CONTENTS is nil."
3099 (let ((type (org-element-property :type timestamp) ))
3100 (concat
3101 (format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>")
3102 (org-element-property :value timestamp))
3103 (let ((range-end (org-element-property :range-end timestamp)))
3104 (when range-end
3105 (concat "--"
3106 (format (if (eq type 'inactive-range) "[%s]" "<%s>")
3107 range-end)))))))
3109 (defun org-element-timestamp-successor (limit)
3110 "Search for the next timestamp object.
3112 LIMIT bounds the search.
3114 Return value is a cons cell whose CAR is `timestamp' and CDR is
3115 beginning position."
3116 (save-excursion
3117 (when (re-search-forward
3118 (concat org-ts-regexp-both
3119 "\\|"
3120 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3121 "\\|"
3122 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3123 limit t)
3124 (cons 'timestamp (match-beginning 0)))))
3127 ;;;; Underline
3129 (defun org-element-underline-parser ()
3130 "Parse underline object at point.
3132 Return a list whose CAR is `underline' and CDR is a plist with
3133 `:begin', `:end', `:contents-begin' and `:contents-end' and
3134 `:post-blank' keywords.
3136 Assume point is at the first underscore marker."
3137 (save-excursion
3138 (unless (bolp) (backward-char 1))
3139 (looking-at org-emph-re)
3140 (let ((begin (match-beginning 2))
3141 (contents-begin (match-beginning 4))
3142 (contents-end (match-end 4))
3143 (post-blank (progn (goto-char (match-end 2))
3144 (skip-chars-forward " \t")))
3145 (end (point)))
3146 (list 'underline
3147 (list :begin begin
3148 :end end
3149 :contents-begin contents-begin
3150 :contents-end contents-end
3151 :post-blank post-blank)))))
3153 (defun org-element-underline-interpreter (underline contents)
3154 "Interpret UNDERLINE object as Org syntax.
3155 CONTENTS is the contents of the object."
3156 (format "_%s_" contents))
3159 ;;;; Verbatim
3161 (defun org-element-verbatim-parser ()
3162 "Parse verbatim object at point.
3164 Return a list whose CAR is `verbatim' and CDR is a plist with
3165 `:value', `:begin', `:end' and `:post-blank' keywords.
3167 Assume point is at the first equal sign marker."
3168 (save-excursion
3169 (unless (bolp) (backward-char 1))
3170 (looking-at org-emph-re)
3171 (let ((begin (match-beginning 2))
3172 (value (org-match-string-no-properties 4))
3173 (post-blank (progn (goto-char (match-end 2))
3174 (skip-chars-forward " \t")))
3175 (end (point)))
3176 (list 'verbatim
3177 (list :value value
3178 :begin begin
3179 :end end
3180 :post-blank post-blank)))))
3182 (defun org-element-verbatim-interpreter (verbatim contents)
3183 "Interpret VERBATIM object as Org syntax.
3184 CONTENTS is nil."
3185 (format "=%s=" (org-element-property :value verbatim)))
3189 ;;; Parsing Element Starting At Point
3191 ;; `org-element-current-element' is the core function of this section.
3192 ;; It returns the Lisp representation of the element starting at
3193 ;; point.
3195 ;; `org-element-current-element' makes use of special modes. They are
3196 ;; activated for fixed element chaining (i.e. `plain-list' > `item')
3197 ;; or fixed conditional element chaining (i.e. `headline' >
3198 ;; `section'). Special modes are: `section', `quote-section', `item'
3199 ;; and `table-row'.
3201 (defun org-element-current-element
3202 (limit &optional granularity special structure)
3203 "Parse the element starting at point.
3205 LIMIT bounds the search.
3207 Return value is a list like (TYPE PROPS) where TYPE is the type
3208 of the element and PROPS a plist of properties associated to the
3209 element.
3211 Possible types are defined in `org-element-all-elements'.
3213 Optional argument GRANULARITY determines the depth of the
3214 recursion. Allowed values are `headline', `greater-element',
3215 `element', `object' or nil. When it is broader than `object' (or
3216 nil), secondary values will not be parsed, since they only
3217 contain objects.
3219 Optional argument SPECIAL, when non-nil, can be either `section',
3220 `quote-section', `table-row' and `item'.
3222 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3223 be computed.
3225 This function assumes point is always at the beginning of the
3226 element it has to parse."
3227 (save-excursion
3228 ;; If point is at an affiliated keyword, try moving to the
3229 ;; beginning of the associated element. If none is found, the
3230 ;; keyword is orphaned and will be treated as plain text.
3231 (when (looking-at org-element--affiliated-re)
3232 (let ((opoint (point)))
3233 (while (looking-at org-element--affiliated-re) (forward-line))
3234 (when (looking-at "[ \t]*$") (goto-char opoint))))
3235 (let ((case-fold-search t)
3236 ;; Determine if parsing depth allows for secondary strings
3237 ;; parsing. It only applies to elements referenced in
3238 ;; `org-element-secondary-value-alist'.
3239 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3240 (cond
3241 ;; Item.
3242 ((eq special 'item)
3243 (org-element-item-parser limit structure raw-secondary-p))
3244 ;; Quote Section.
3245 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3246 ;; Table Row.
3247 ((eq special 'table-row) (org-element-table-row-parser limit))
3248 ;; Headline.
3249 ((org-with-limited-levels (org-at-heading-p))
3250 (org-element-headline-parser limit raw-secondary-p))
3251 ;; Section (must be checked after headline).
3252 ((eq special 'section) (org-element-section-parser limit))
3253 ;; Planning and Clock.
3254 ((and (looking-at org-planning-or-clock-line-re))
3255 (if (equal (match-string 1) org-clock-string)
3256 (org-element-clock-parser limit)
3257 (org-element-planning-parser limit)))
3258 ;; Inlinetask.
3259 ((org-at-heading-p)
3260 (org-element-inlinetask-parser limit raw-secondary-p))
3261 ;; LaTeX Environment.
3262 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
3263 (if (save-excursion
3264 (re-search-forward
3265 (format "[ \t]*\\\\end{%s}[ \t]*"
3266 (regexp-quote (match-string 1)))
3267 nil t))
3268 (org-element-latex-environment-parser limit)
3269 (org-element-paragraph-parser limit)))
3270 ;; Drawer and Property Drawer.
3271 ((looking-at org-drawer-regexp)
3272 (let ((name (match-string 1)))
3273 (cond
3274 ((not (save-excursion
3275 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
3276 (org-element-paragraph-parser limit))
3277 ((equal "PROPERTIES" name)
3278 (org-element-property-drawer-parser limit))
3279 (t (org-element-drawer-parser limit)))))
3280 ;; Fixed Width
3281 ((looking-at "[ \t]*:\\( \\|$\\)")
3282 (org-element-fixed-width-parser limit))
3283 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3284 ;; Keywords.
3285 ((looking-at "[ \t]*#\\+")
3286 (goto-char (match-end 0))
3287 (cond ((looking-at "$\\| ")
3288 (beginning-of-line)
3289 (org-element-comment-parser limit))
3290 ((looking-at "BEGIN_\\(\\S-+\\)")
3291 (beginning-of-line)
3292 (let ((parser (assoc (upcase (match-string 1))
3293 org-element-block-name-alist)))
3294 (if parser (funcall (cdr parser) limit)
3295 (org-element-special-block-parser limit))))
3296 ((looking-at "CALL")
3297 (beginning-of-line)
3298 (org-element-babel-call-parser limit))
3299 ((looking-at "BEGIN:? ")
3300 (beginning-of-line)
3301 (org-element-dynamic-block-parser limit))
3302 ((looking-at "\\S-+:")
3303 (beginning-of-line)
3304 (org-element-keyword-parser limit))
3305 ;; Ill-formed syntax is considered as a comment.
3307 (beginning-of-line)
3308 (org-element-comment-parser limit))))
3309 ;; Comments.
3310 ((eq (char-after) ?#) (org-element-comment-parser limit))
3311 ;; Footnote Definition.
3312 ((looking-at org-footnote-definition-re)
3313 (org-element-footnote-definition-parser limit))
3314 ;; Horizontal Rule.
3315 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3316 (org-element-horizontal-rule-parser limit))
3317 ;; Table.
3318 ((org-at-table-p t) (org-element-table-parser limit))
3319 ;; List.
3320 ((looking-at (org-item-re))
3321 (org-element-plain-list-parser limit (or structure (org-list-struct))))
3322 ;; Default element: Paragraph.
3323 (t (org-element-paragraph-parser limit))))))
3326 ;; Most elements can have affiliated keywords. When looking for an
3327 ;; element beginning, we want to move before them, as they belong to
3328 ;; that element, and, in the meantime, collect information they give
3329 ;; into appropriate properties. Hence the following function.
3331 ;; Usage of optional arguments may not be obvious at first glance:
3333 ;; - TRANS-LIST is used to polish keywords names that have evolved
3334 ;; during Org history. In example, even though =result= and
3335 ;; =results= coexist, we want to have them under the same =result=
3336 ;; property. It's also true for "srcname" and "name", where the
3337 ;; latter seems to be preferred nowadays (thus the "name" property).
3339 ;; - CONSED allows to regroup multi-lines keywords under the same
3340 ;; property, while preserving their own identity. This is mostly
3341 ;; used for "attr_latex" and al.
3343 ;; - PARSED prepares a keyword value for export. This is useful for
3344 ;; "caption". Objects restrictions for such keywords are defined in
3345 ;; `org-element-object-restrictions'.
3347 ;; - DUALS is used to take care of keywords accepting a main and an
3348 ;; optional secondary values. For example "results" has its
3349 ;; source's name as the main value, and may have an hash string in
3350 ;; optional square brackets as the secondary one.
3352 ;; A keyword may belong to more than one category.
3354 (defun org-element-collect-affiliated-keywords
3355 (&optional key-re trans-list consed parsed duals)
3356 "Collect affiliated keywords before point.
3358 Optional argument KEY-RE is a regexp matching keywords, which
3359 puts matched keyword in group 1. It defaults to
3360 `org-element--affiliated-re'.
3362 TRANS-LIST is an alist where key is the keyword and value the
3363 property name it should be translated to, without the colons. It
3364 defaults to `org-element-keyword-translation-alist'.
3366 CONSED is a list of strings. Any keyword belonging to that list
3367 will have its value consed. The check is done after keyword
3368 translation. It defaults to `org-element-multiple-keywords'.
3370 PARSED is a list of strings. Any keyword member of this list
3371 will have its value parsed. The check is done after keyword
3372 translation. If a keyword is a member of both CONSED and PARSED,
3373 it's value will be a list of parsed strings. It defaults to
3374 `org-element-parsed-keywords'.
3376 DUALS is a list of strings. Any keyword member of this list can
3377 have two parts: one mandatory and one optional. Its value is
3378 a cons cell whose CAR is the former, and the CDR the latter. If
3379 a keyword is a member of both PARSED and DUALS, both values will
3380 be parsed. It defaults to `org-element-dual-keywords'.
3382 Return a list whose CAR is the position at the first of them and
3383 CDR a plist of keywords and values."
3384 (save-excursion
3385 (let ((case-fold-search t)
3386 (key-re (or key-re org-element--affiliated-re))
3387 (trans-list (or trans-list org-element-keyword-translation-alist))
3388 (consed (or consed org-element-multiple-keywords))
3389 (parsed (or parsed org-element-parsed-keywords))
3390 (duals (or duals org-element-dual-keywords))
3391 ;; RESTRICT is the list of objects allowed in parsed
3392 ;; keywords value.
3393 (restrict (org-element-restriction 'keyword))
3394 output)
3395 (unless (bobp)
3396 (while (and (not (bobp)) (progn (forward-line -1) (looking-at key-re)))
3397 (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
3398 ;; Apply translation to RAW-KWD. From there, KWD is
3399 ;; the official keyword.
3400 (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
3401 ;; Find main value for any keyword.
3402 (value
3403 (save-match-data
3404 (org-trim
3405 (buffer-substring-no-properties
3406 (match-end 0) (point-at-eol)))))
3407 ;; If KWD is a dual keyword, find its secondary
3408 ;; value. Maybe parse it.
3409 (dual-value
3410 (and (member kwd duals)
3411 (let ((sec (org-match-string-no-properties 3)))
3412 (if (or (not sec) (not (member kwd parsed))) sec
3413 (org-element-parse-secondary-string sec restrict)))))
3414 ;; Attribute a property name to KWD.
3415 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3416 ;; Now set final shape for VALUE.
3417 (when (member kwd parsed)
3418 (setq value (org-element-parse-secondary-string value restrict)))
3419 (when (member kwd duals)
3420 ;; VALUE is mandatory. Set it to nil if there is none.
3421 (setq value (and value (cons value dual-value))))
3422 ;; Attributes are always consed.
3423 (when (or (member kwd consed) (string-match "^ATTR_" kwd))
3424 (setq value (cons value (plist-get output kwd-sym))))
3425 ;; Eventually store the new value in OUTPUT.
3426 (setq output (plist-put output kwd-sym value))))
3427 (unless (looking-at key-re) (forward-line 1)))
3428 (list (point) output))))
3432 ;;; The Org Parser
3434 ;; The two major functions here are `org-element-parse-buffer', which
3435 ;; parses Org syntax inside the current buffer, taking into account
3436 ;; region, narrowing, or even visibility if specified, and
3437 ;; `org-element-parse-secondary-string', which parses objects within
3438 ;; a given string.
3440 ;; The (almost) almighty `org-element-map' allows to apply a function
3441 ;; on elements or objects matching some type, and accumulate the
3442 ;; resulting values. In an export situation, it also skips unneeded
3443 ;; parts of the parse tree.
3445 (defun org-element-parse-buffer (&optional granularity visible-only)
3446 "Recursively parse the buffer and return structure.
3447 If narrowing is in effect, only parse the visible part of the
3448 buffer.
3450 Optional argument GRANULARITY determines the depth of the
3451 recursion. It can be set to the following symbols:
3453 `headline' Only parse headlines.
3454 `greater-element' Don't recurse into greater elements excepted
3455 headlines and sections. Thus, elements
3456 parsed are the top-level ones.
3457 `element' Parse everything but objects and plain text.
3458 `object' Parse the complete buffer (default).
3460 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3461 elements.
3463 Assume buffer is in Org mode."
3464 (save-excursion
3465 (goto-char (point-min))
3466 (org-skip-whitespace)
3467 (org-element-parse-elements
3468 (point-at-bol) (point-max)
3469 ;; Start in `section' mode so text before the first
3470 ;; headline belongs to a section.
3471 'section nil granularity visible-only (list 'org-data nil))))
3473 (defun org-element-parse-secondary-string (string restriction &optional parent)
3474 "Recursively parse objects in STRING and return structure.
3476 RESTRICTION is a symbol limiting the object types that will be
3477 looked after.
3479 Optional argument PARENT, when non-nil, is the element or object
3480 containing the secondary string. It is used to set correctly
3481 `:parent' property within the string."
3482 (with-temp-buffer
3483 (insert string)
3484 (let ((secondary (org-element-parse-objects
3485 (point-min) (point-max) nil restriction)))
3486 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
3487 secondary))))
3489 (defun org-element-map (data types fun &optional info first-match no-recursion)
3490 "Map a function on selected elements or objects.
3492 DATA is the parsed tree, as returned by, i.e,
3493 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3494 of elements or objects types. FUN is the function called on the
3495 matching element or object. It must accept one arguments: the
3496 element or object itself.
3498 When optional argument INFO is non-nil, it should be a plist
3499 holding export options. In that case, parts of the parse tree
3500 not exportable according to that property list will be skipped.
3502 When optional argument FIRST-MATCH is non-nil, stop at the first
3503 match for which FUN doesn't return nil, and return that value.
3505 Optional argument NO-RECURSION is a symbol or a list of symbols
3506 representing elements or objects types. `org-element-map' won't
3507 enter any recursive element or object whose type belongs to that
3508 list. Though, FUN can still be applied on them.
3510 Nil values returned from FUN do not appear in the results."
3511 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3512 (unless (listp types) (setq types (list types)))
3513 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
3514 ;; Recursion depth is determined by --CATEGORY.
3515 (let* ((--category
3516 (catch 'found
3517 (let ((category 'greater-elements))
3518 (mapc (lambda (type)
3519 (cond ((or (memq type org-element-all-objects)
3520 (eq type 'plain-text))
3521 ;; If one object is found, the function
3522 ;; has to recurse into every object.
3523 (throw 'found 'objects))
3524 ((not (memq type org-element-greater-elements))
3525 ;; If one regular element is found, the
3526 ;; function has to recurse, at lest, into
3527 ;; every element it encounters.
3528 (and (not (eq category 'elements))
3529 (setq category 'elements)))))
3530 types)
3531 category)))
3532 --acc
3533 --walk-tree
3534 (--walk-tree
3535 (function
3536 (lambda (--data)
3537 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3538 ;; holding contextual information.
3539 (let ((--type (org-element-type --data)))
3540 (cond
3541 ((not --data))
3542 ;; Ignored element in an export context.
3543 ((and info (memq --data (plist-get info :ignore-list))))
3544 ;; Secondary string: only objects can be found there.
3545 ((not --type)
3546 (when (eq --category 'objects) (mapc --walk-tree --data)))
3547 ;; Unconditionally enter parse trees.
3548 ((eq --type 'org-data)
3549 (mapc --walk-tree (org-element-contents --data)))
3551 ;; Check if TYPE is matching among TYPES. If so,
3552 ;; apply FUN to --DATA and accumulate return value
3553 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3554 (when (memq --type types)
3555 (let ((result (funcall fun --data)))
3556 (cond ((not result))
3557 (first-match (throw '--map-first-match result))
3558 (t (push result --acc)))))
3559 ;; If --DATA has a secondary string that can contain
3560 ;; objects with their type among TYPES, look into it.
3561 (when (eq --category 'objects)
3562 (let ((sec-prop
3563 (assq --type org-element-secondary-value-alist)))
3564 (when sec-prop
3565 (funcall --walk-tree
3566 (org-element-property (cdr sec-prop) --data)))))
3567 ;; Determine if a recursion into --DATA is possible.
3568 (cond
3569 ;; --TYPE is explicitly removed from recursion.
3570 ((memq --type no-recursion))
3571 ;; --DATA has no contents.
3572 ((not (org-element-contents --data)))
3573 ;; Looking for greater elements but --DATA is simply
3574 ;; an element or an object.
3575 ((and (eq --category 'greater-elements)
3576 (not (memq --type org-element-greater-elements))))
3577 ;; Looking for elements but --DATA is an object.
3578 ((and (eq --category 'elements)
3579 (memq --type org-element-all-objects)))
3580 ;; In any other case, map contents.
3581 (t (mapc --walk-tree (org-element-contents --data)))))))))))
3582 (catch '--map-first-match
3583 (funcall --walk-tree data)
3584 ;; Return value in a proper order.
3585 (nreverse --acc))))
3587 ;; The following functions are internal parts of the parser.
3589 ;; The first one, `org-element-parse-elements' acts at the element's
3590 ;; level.
3592 ;; The second one, `org-element-parse-objects' applies on all objects
3593 ;; of a paragraph or a secondary string. It uses
3594 ;; `org-element-get-candidates' to optimize the search of the next
3595 ;; object in the buffer.
3597 ;; More precisely, that function looks for every allowed object type
3598 ;; first. Then, it discards failed searches, keeps further matches,
3599 ;; and searches again types matched behind point, for subsequent
3600 ;; calls. Thus, searching for a given type fails only once, and every
3601 ;; object is searched only once at top level (but sometimes more for
3602 ;; nested types).
3604 (defun org-element-parse-elements
3605 (beg end special structure granularity visible-only acc)
3606 "Parse elements between BEG and END positions.
3608 SPECIAL prioritize some elements over the others. It can be set
3609 to `quote-section', `section' `item' or `table-row'.
3611 When value is `item', STRUCTURE will be used as the current list
3612 structure.
3614 GRANULARITY determines the depth of the recursion. See
3615 `org-element-parse-buffer' for more information.
3617 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3618 elements.
3620 Elements are accumulated into ACC."
3621 (save-excursion
3622 (goto-char beg)
3623 ;; When parsing only headlines, skip any text before first one.
3624 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
3625 (org-with-limited-levels (outline-next-heading)))
3626 ;; Main loop start.
3627 (while (< (point) end)
3628 ;; Find current element's type and parse it accordingly to
3629 ;; its category.
3630 (let* ((element (org-element-current-element
3631 end granularity special structure))
3632 (type (org-element-type element))
3633 (cbeg (org-element-property :contents-begin element)))
3634 (goto-char (org-element-property :end element))
3635 ;; Fill ELEMENT contents by side-effect.
3636 (cond
3637 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3638 ;; no contents, don't modify it.
3639 ((or (and visible-only (org-element-property :hiddenp element))
3640 (not cbeg)))
3641 ;; Greater element: parse it between `contents-begin' and
3642 ;; `contents-end'. Make sure GRANULARITY allows the
3643 ;; recursion, or ELEMENT is an headline, in which case going
3644 ;; inside is mandatory, in order to get sub-level headings.
3645 ((and (memq type org-element-greater-elements)
3646 (or (memq granularity '(element object nil))
3647 (and (eq granularity 'greater-element)
3648 (eq type 'section))
3649 (eq type 'headline)))
3650 (org-element-parse-elements
3651 cbeg (org-element-property :contents-end element)
3652 ;; Possibly switch to a special mode.
3653 (case type
3654 (headline
3655 (if (org-element-property :quotedp element) 'quote-section
3656 'section))
3657 (plain-list 'item)
3658 (table 'table-row))
3659 (org-element-property :structure element)
3660 granularity visible-only element))
3661 ;; ELEMENT has contents. Parse objects inside, if
3662 ;; GRANULARITY allows it.
3663 ((memq granularity '(object nil))
3664 (org-element-parse-objects
3665 cbeg (org-element-property :contents-end element) element
3666 (org-element-restriction type))))
3667 (org-element-adopt-element acc element t)))
3668 ;; Return result.
3669 acc))
3671 (defun org-element-parse-objects (beg end acc restriction)
3672 "Parse objects between BEG and END and return recursive structure.
3674 Objects are accumulated in ACC.
3676 RESTRICTION is a list of object types which are allowed in the
3677 current object."
3678 (let (candidates)
3679 (save-excursion
3680 (goto-char beg)
3681 (while (and (< (point) end)
3682 (setq candidates (org-element-get-next-object-candidates
3683 end restriction candidates)))
3684 (let ((next-object
3685 (let ((pos (apply 'min (mapcar 'cdr candidates))))
3686 (save-excursion
3687 (goto-char pos)
3688 (funcall (intern (format "org-element-%s-parser"
3689 (car (rassq pos candidates)))))))))
3690 ;; 1. Text before any object. Untabify it.
3691 (let ((obj-beg (org-element-property :begin next-object)))
3692 (unless (= (point) obj-beg)
3693 (setq acc
3694 (org-element-adopt-element
3696 (replace-regexp-in-string
3697 "\t" (make-string tab-width ? )
3698 (buffer-substring-no-properties (point) obj-beg)) t))))
3699 ;; 2. Object...
3700 (let ((obj-end (org-element-property :end next-object))
3701 (cont-beg (org-element-property :contents-begin next-object)))
3702 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3703 ;; a recursive type.
3704 (when (and cont-beg
3705 (memq (car next-object) org-element-recursive-objects))
3706 (save-restriction
3707 (narrow-to-region
3708 cont-beg
3709 (org-element-property :contents-end next-object))
3710 (org-element-parse-objects
3711 (point-min) (point-max) next-object
3712 (org-element-restriction next-object))))
3713 (setq acc (org-element-adopt-element acc next-object t))
3714 (goto-char obj-end))))
3715 ;; 3. Text after last object. Untabify it.
3716 (unless (= (point) end)
3717 (setq acc
3718 (org-element-adopt-element
3720 (replace-regexp-in-string
3721 "\t" (make-string tab-width ? )
3722 (buffer-substring-no-properties (point) end)) t)))
3723 ;; Result.
3724 acc)))
3726 (defun org-element-get-next-object-candidates (limit restriction objects)
3727 "Return an alist of candidates for the next object.
3729 LIMIT bounds the search, and RESTRICTION narrows candidates to
3730 some object types.
3732 Return value is an alist whose CAR is position and CDR the object
3733 type, as a symbol.
3735 OBJECTS is the previous candidates alist."
3736 (let (next-candidates types-to-search)
3737 ;; If no previous result, search every object type in RESTRICTION.
3738 ;; Otherwise, keep potential candidates (old objects located after
3739 ;; point) and ask to search again those which had matched before.
3740 (if (not objects) (setq types-to-search restriction)
3741 (mapc (lambda (obj)
3742 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
3743 (push obj next-candidates)))
3744 objects))
3745 ;; Call the appropriate successor function for each type to search
3746 ;; and accumulate matches.
3747 (mapc
3748 (lambda (type)
3749 (let* ((successor-fun
3750 (intern
3751 (format "org-element-%s-successor"
3752 (or (cdr (assq type org-element-object-successor-alist))
3753 type))))
3754 (obj (funcall successor-fun limit)))
3755 (and obj (push obj next-candidates))))
3756 types-to-search)
3757 ;; Return alist.
3758 next-candidates))
3762 ;;; Towards A Bijective Process
3764 ;; The parse tree obtained with `org-element-parse-buffer' is really
3765 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3766 ;; interpreted and expanded into a string with canonical Org syntax.
3767 ;; Hence `org-element-interpret-data'.
3769 ;; The function relies internally on
3770 ;; `org-element-interpret--affiliated-keywords'.
3772 (defun org-element-interpret-data (data &optional parent)
3773 "Interpret DATA as Org syntax.
3775 DATA is a parse tree, an element, an object or a secondary string
3776 to interpret.
3778 Optional argument PARENT is used for recursive calls. It contains
3779 the element or object containing data, or nil.
3781 Return Org syntax as a string."
3782 (let* ((type (org-element-type data))
3783 (results
3784 (cond
3785 ;; Secondary string.
3786 ((not type)
3787 (mapconcat
3788 (lambda (obj) (org-element-interpret-data obj parent))
3789 data ""))
3790 ;; Full Org document.
3791 ((eq type 'org-data)
3792 (mapconcat
3793 (lambda (obj) (org-element-interpret-data obj parent))
3794 (org-element-contents data) ""))
3795 ;; Plain text.
3796 ((stringp data) data)
3797 ;; Element/Object without contents.
3798 ((not (org-element-contents data))
3799 (funcall (intern (format "org-element-%s-interpreter" type))
3800 data nil))
3801 ;; Element/Object with contents.
3803 (let* ((greaterp (memq type org-element-greater-elements))
3804 (objectp (and (not greaterp)
3805 (memq type org-element-recursive-objects)))
3806 (contents
3807 (mapconcat
3808 (lambda (obj) (org-element-interpret-data obj data))
3809 (org-element-contents
3810 (if (or greaterp objectp) data
3811 ;; Elements directly containing objects must
3812 ;; have their indentation normalized first.
3813 (org-element-normalize-contents
3814 data
3815 ;; When normalizing first paragraph of an
3816 ;; item or a footnote-definition, ignore
3817 ;; first line's indentation.
3818 (and (eq type 'paragraph)
3819 (equal data (car (org-element-contents parent)))
3820 (memq (org-element-type parent)
3821 '(footnote-definiton item))))))
3822 "")))
3823 (funcall (intern (format "org-element-%s-interpreter" type))
3824 data
3825 (if greaterp (org-element-normalize-contents contents)
3826 contents)))))))
3827 (if (memq type '(org-data plain-text nil)) results
3828 ;; Build white spaces. If no `:post-blank' property is
3829 ;; specified, assume its value is 0.
3830 (let ((post-blank (or (org-element-property :post-blank data) 0)))
3831 (if (memq type org-element-all-objects)
3832 (concat results (make-string post-blank 32))
3833 (concat
3834 (org-element-interpret--affiliated-keywords data)
3835 (org-element-normalize-string results)
3836 (make-string post-blank 10)))))))
3838 (defun org-element-interpret--affiliated-keywords (element)
3839 "Return ELEMENT's affiliated keywords as Org syntax.
3840 If there is no affiliated keyword, return the empty string."
3841 (let ((keyword-to-org
3842 (function
3843 (lambda (key value)
3844 (let (dual)
3845 (when (member key org-element-dual-keywords)
3846 (setq dual (cdr value) value (car value)))
3847 (concat "#+" key
3848 (and dual
3849 (format "[%s]" (org-element-interpret-data dual)))
3850 ": "
3851 (if (member key org-element-parsed-keywords)
3852 (org-element-interpret-data value)
3853 value)
3854 "\n"))))))
3855 (mapconcat
3856 (lambda (prop)
3857 (let ((value (org-element-property prop element))
3858 (keyword (upcase (substring (symbol-name prop) 1))))
3859 (when value
3860 (if (or (member keyword org-element-multiple-keywords)
3861 ;; All attribute keywords can have multiple lines.
3862 (string-match "^ATTR_" keyword))
3863 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
3864 value
3866 (funcall keyword-to-org keyword value)))))
3867 ;; List all ELEMENT's properties matching an attribute line or an
3868 ;; affiliated keyword, but ignore translated keywords since they
3869 ;; cannot belong to the property list.
3870 (loop for prop in (nth 1 element) by 'cddr
3871 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
3872 (or (string-match "^ATTR_" keyword)
3873 (and
3874 (member keyword org-element-affiliated-keywords)
3875 (not (assoc keyword
3876 org-element-keyword-translation-alist)))))
3877 collect prop)
3878 "")))
3880 ;; Because interpretation of the parse tree must return the same
3881 ;; number of blank lines between elements and the same number of white
3882 ;; space after objects, some special care must be given to white
3883 ;; spaces.
3885 ;; The first function, `org-element-normalize-string', ensures any
3886 ;; string different from the empty string will end with a single
3887 ;; newline character.
3889 ;; The second function, `org-element-normalize-contents', removes
3890 ;; global indentation from the contents of the current element.
3892 (defun org-element-normalize-string (s)
3893 "Ensure string S ends with a single newline character.
3895 If S isn't a string return it unchanged. If S is the empty
3896 string, return it. Otherwise, return a new string with a single
3897 newline character at its end."
3898 (cond
3899 ((not (stringp s)) s)
3900 ((string= "" s) "")
3901 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
3902 (replace-match "\n" nil nil s)))))
3904 (defun org-element-normalize-contents (element &optional ignore-first)
3905 "Normalize plain text in ELEMENT's contents.
3907 ELEMENT must only contain plain text and objects.
3909 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3910 indentation to compute maximal common indentation.
3912 Return the normalized element that is element with global
3913 indentation removed from its contents. The function assumes that
3914 indentation is not done with TAB characters."
3915 (let* (ind-list ; for byte-compiler
3916 collect-inds ; for byte-compiler
3917 (collect-inds
3918 (function
3919 ;; Return list of indentations within BLOB. This is done by
3920 ;; walking recursively BLOB and updating IND-LIST along the
3921 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3922 ;; been seen yet. It is required as this string is the only
3923 ;; one whose indentation doesn't happen after a newline
3924 ;; character.
3925 (lambda (blob first-flag)
3926 (mapc
3927 (lambda (object)
3928 (when (and first-flag (stringp object))
3929 (setq first-flag nil)
3930 (string-match "\\`\\( *\\)" object)
3931 (let ((len (length (match-string 1 object))))
3932 ;; An indentation of zero means no string will be
3933 ;; modified. Quit the process.
3934 (if (zerop len) (throw 'zero (setq ind-list nil))
3935 (push len ind-list))))
3936 (cond
3937 ((stringp object)
3938 (let ((start 0))
3939 ;; Avoid matching blank or empty lines.
3940 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
3941 (not (equal (match-string 2 object) " ")))
3942 (setq start (match-end 0))
3943 (push (length (match-string 1 object)) ind-list))))
3944 ((memq (org-element-type object) org-element-recursive-objects)
3945 (funcall collect-inds object first-flag))))
3946 (org-element-contents blob))))))
3947 ;; Collect indentation list in ELEMENT. Possibly remove first
3948 ;; value if IGNORE-FIRST is non-nil.
3949 (catch 'zero (funcall collect-inds element (not ignore-first)))
3950 (if (not ind-list) element
3951 ;; Build ELEMENT back, replacing each string with the same
3952 ;; string minus common indentation.
3953 (let* (build ; For byte compiler.
3954 (build
3955 (function
3956 (lambda (blob mci first-flag)
3957 ;; Return BLOB with all its strings indentation
3958 ;; shortened from MCI white spaces. FIRST-FLAG is
3959 ;; non-nil when the first string hasn't been seen
3960 ;; yet.
3961 (setcdr (cdr blob)
3962 (mapcar
3963 (lambda (object)
3964 (when (and first-flag (stringp object))
3965 (setq first-flag nil)
3966 (setq object
3967 (replace-regexp-in-string
3968 (format "\\` \\{%d\\}" mci) "" object)))
3969 (cond
3970 ((stringp object)
3971 (replace-regexp-in-string
3972 (format "\n \\{%d\\}" mci) "\n" object))
3973 ((memq (org-element-type object)
3974 org-element-recursive-objects)
3975 (funcall build object mci first-flag))
3976 (t object)))
3977 (org-element-contents blob)))
3978 blob))))
3979 (funcall build element (apply 'min ind-list) (not ignore-first))))))
3983 ;;; The Toolbox
3985 ;; The first move is to implement a way to obtain the smallest element
3986 ;; containing point. This is the job of `org-element-at-point'. It
3987 ;; basically jumps back to the beginning of section containing point
3988 ;; and moves, element after element, with
3989 ;; `org-element-current-element' until the container is found.
3991 ;; At a deeper level, `org-element-context' lists all elements and
3992 ;; objects containing point.
3994 ;; Note: When using `org-element-at-point', secondary values are never
3995 ;; parsed since the function focuses on elements, not on objects.
3997 (defun org-element-at-point (&optional keep-trail)
3998 "Determine closest element around point.
4000 Return value is a list like (TYPE PROPS) where TYPE is the type
4001 of the element and PROPS a plist of properties associated to the
4002 element. Possible types are defined in
4003 `org-element-all-elements'.
4005 As a special case, if point is at the very beginning of a list or
4006 sub-list, returned element will be that list instead of the first
4007 item. In the same way, if point is at the beginning of the first
4008 row of a table, returned element will be the table instead of the
4009 first row.
4011 If optional argument KEEP-TRAIL is non-nil, the function returns
4012 a list of of elements leading to element at point. The list's
4013 CAR is always the element at point. Following positions contain
4014 element's siblings, then parents, siblings of parents, until the
4015 first element of current section."
4016 (org-with-wide-buffer
4017 ;; If at an headline, parse it. It is the sole element that
4018 ;; doesn't require to know about context. Be sure to disallow
4019 ;; secondary string parsing, though.
4020 (if (org-with-limited-levels (org-at-heading-p))
4021 (progn
4022 (beginning-of-line)
4023 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4024 (list (org-element-headline-parser (point-max) t))))
4025 ;; Otherwise move at the beginning of the section containing
4026 ;; point.
4027 (let ((origin (point))
4028 (end (save-excursion (outline-next-heading) (point)))
4029 element type special-flag trail struct prevs)
4030 (org-with-limited-levels
4031 (if (org-before-first-heading-p) (goto-char (point-min))
4032 (org-back-to-heading)
4033 (forward-line)))
4034 (org-skip-whitespace)
4035 (beginning-of-line)
4036 ;; Parse successively each element, skipping those ending
4037 ;; before original position.
4038 (catch 'exit
4039 (while t
4040 (setq element
4041 (org-element-current-element end 'element special-flag struct)
4042 type (car element))
4043 (push element trail)
4044 (cond
4045 ;; 1. Skip any element ending before point or at point.
4046 ((let ((end (org-element-property :end element)))
4047 (when (<= end origin)
4048 (if (> (point-max) end) (goto-char end)
4049 (throw 'exit (if keep-trail trail element))))))
4050 ;; 2. An element containing point is always the element at
4051 ;; point.
4052 ((not (memq type org-element-greater-elements))
4053 (throw 'exit (if keep-trail trail element)))
4054 ;; 3. At any other greater element type, if point is
4055 ;; within contents, move into it. Otherwise, return
4056 ;; that element.
4058 (let ((cbeg (org-element-property :contents-begin element))
4059 (cend (org-element-property :contents-end element)))
4060 (if (or (not cbeg) (not cend) (> cbeg origin) (<= cend origin)
4061 (and (= cbeg origin) (memq type '(plain-list table))))
4062 (throw 'exit (if keep-trail trail element))
4063 (case type
4064 (plain-list
4065 (setq special-flag 'item
4066 struct (org-element-property :structure element)))
4067 (table (setq special-flag 'table-row))
4068 (otherwise (setq special-flag nil)))
4069 (setq end cend)
4070 (goto-char cbeg)))))))))))
4072 (defun org-element-context ()
4073 "Return list of all elements and objects around point.
4075 Return value is a list like (TYPE PROPS) where TYPE is the type
4076 of the element or object and PROPS a plist of properties
4077 associated to it. Possible types are defined in
4078 `org-element-all-elements' and `org-element-all-objects'.
4080 All elements and objects returned belong to the current section
4081 and are ordered from closest to farthest."
4082 (org-with-wide-buffer
4083 (let* ((origin (point))
4084 ;; Remove elements not containing point from trail.
4085 (elements (org-remove-if
4086 (lambda (el)
4087 (or (> (org-element-property :begin el) origin)
4088 (< (org-element-property :end el) origin)))
4089 (org-element-at-point 'keep-trail)))
4090 (element (car elements))
4091 (type (car element)) end)
4092 ;; Check if point is inside an element containing objects or at
4093 ;; a secondary string. In that case, move to beginning of the
4094 ;; element or secondary string and set END to the other side.
4095 (if (not (or (and (eq type 'item)
4096 (let ((tag (org-element-property :tag element)))
4097 (and tag
4098 (progn
4099 (beginning-of-line)
4100 (search-forward tag (point-at-eol))
4101 (goto-char (match-beginning 0))
4102 (and (>= origin (point))
4103 (<= origin
4104 ;; `1+' is required so some
4105 ;; successors can match
4106 ;; properly their object.
4107 (setq end (1+ (match-end 0)))))))))
4108 (and (memq type '(headline inlinetask))
4109 (progn (beginning-of-line)
4110 (skip-chars-forward "* ")
4111 (setq end (point-at-eol))))
4112 (and (memq (car element) '(paragraph table-cell verse-block))
4113 (let ((cbeg (org-element-property
4114 :contents-begin element))
4115 (cend (org-element-property
4116 :contents-end element)))
4117 (and (>= origin cbeg)
4118 (<= origin cend)
4119 (progn (goto-char cbeg) (setq end cend)))))))
4120 elements
4121 (let ((restriction (org-element-restriction element)) candidates)
4122 (catch 'exit
4123 (while (setq candidates (org-element-get-next-object-candidates
4124 end restriction candidates))
4125 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4126 candidates)))
4127 ;; If ORIGIN is before next object in element, there's
4128 ;; no point in looking further.
4129 (if (> (cdr closest-cand) origin) (throw 'exit elements)
4130 (let* ((object
4131 (progn (goto-char (cdr closest-cand))
4132 (funcall (intern (format "org-element-%s-parser"
4133 (car closest-cand))))))
4134 (cbeg (org-element-property :contents-begin object))
4135 (cend (org-element-property :contents-end object)))
4136 (cond
4137 ;; ORIGIN is after OBJECT, so skip it.
4138 ((< (org-element-property :end object) origin)
4139 (goto-char (org-element-property :end object)))
4140 ;; ORIGIN is within a non-recursive object or at an
4141 ;; object boundaries: Return that object.
4142 ((or (not cbeg) (> cbeg origin) (< cend origin))
4143 (throw 'exit (cons object elements)))
4144 ;; Otherwise, move within current object and restrict
4145 ;; search to the end of its contents.
4146 (t (goto-char cbeg)
4147 (setq end cend)
4148 (push object elements)))))))
4149 elements))))))
4152 ;; Once the local structure around point is well understood, it's easy
4153 ;; to implement some replacements for `forward-paragraph'
4154 ;; `backward-paragraph', namely `org-element-forward' and
4155 ;; `org-element-backward'.
4157 ;; Also, `org-transpose-elements' mimics the behaviour of
4158 ;; `transpose-words', at the element's level, whereas
4159 ;; `org-element-drag-forward', `org-element-drag-backward', and
4160 ;; `org-element-up' generalize, respectively, functions
4161 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
4163 ;; `org-element-unindent-buffer' will, as its name almost suggests,
4164 ;; smartly remove global indentation from buffer, making it possible
4165 ;; to use Org indent mode on a file created with hard indentation.
4167 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
4168 ;; internally by some of the previously cited tools.
4170 (defsubst org-element-nested-p (elem-A elem-B)
4171 "Non-nil when elements ELEM-A and ELEM-B are nested."
4172 (let ((beg-A (org-element-property :begin elem-A))
4173 (beg-B (org-element-property :begin elem-B))
4174 (end-A (org-element-property :end elem-A))
4175 (end-B (org-element-property :end elem-B)))
4176 (or (and (>= beg-A beg-B) (<= end-A end-B))
4177 (and (>= beg-B beg-A) (<= end-B end-A)))))
4179 (defun org-element-swap-A-B (elem-A elem-B)
4180 "Swap elements ELEM-A and ELEM-B.
4181 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4182 end of ELEM-A."
4183 (goto-char (org-element-property :begin elem-A))
4184 ;; There are two special cases when an element doesn't start at bol:
4185 ;; the first paragraph in an item or in a footnote definition.
4186 (let ((specialp (not (bolp))))
4187 ;; Only a paragraph without any affiliated keyword can be moved at
4188 ;; ELEM-A position in such a situation. Note that the case of
4189 ;; a footnote definition is impossible: it cannot contain two
4190 ;; paragraphs in a row because it cannot contain a blank line.
4191 (if (and specialp
4192 (or (not (eq (org-element-type elem-B) 'paragraph))
4193 (/= (org-element-property :begin elem-B)
4194 (org-element-property :contents-begin elem-B))))
4195 (error "Cannot swap elements"))
4196 ;; In a special situation, ELEM-A will have no indentation. We'll
4197 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4198 (let* ((ind-B (when specialp
4199 (goto-char (org-element-property :begin elem-B))
4200 (org-get-indentation)))
4201 (beg-A (org-element-property :begin elem-A))
4202 (end-A (save-excursion
4203 (goto-char (org-element-property :end elem-A))
4204 (skip-chars-backward " \r\t\n")
4205 (point-at-eol)))
4206 (beg-B (org-element-property :begin elem-B))
4207 (end-B (save-excursion
4208 (goto-char (org-element-property :end elem-B))
4209 (skip-chars-backward " \r\t\n")
4210 (point-at-eol)))
4211 ;; Store overlays responsible for visibility status. We
4212 ;; also need to store their boundaries as they will be
4213 ;; removed from buffer.
4214 (overlays
4215 (cons
4216 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4217 (overlays-in beg-A end-A))
4218 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4219 (overlays-in beg-B end-B))))
4220 ;; Get contents.
4221 (body-A (buffer-substring beg-A end-A))
4222 (body-B (delete-and-extract-region beg-B end-B)))
4223 (goto-char beg-B)
4224 (when specialp
4225 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4226 (org-indent-to-column ind-B))
4227 (insert body-A)
4228 ;; Restore ex ELEM-A overlays.
4229 (mapc (lambda (ov)
4230 (move-overlay
4231 (car ov)
4232 (+ (nth 1 ov) (- beg-B beg-A))
4233 (+ (nth 2 ov) (- beg-B beg-A))))
4234 (car overlays))
4235 (goto-char beg-A)
4236 (delete-region beg-A end-A)
4237 (insert body-B)
4238 ;; Restore ex ELEM-B overlays.
4239 (mapc (lambda (ov)
4240 (move-overlay (car ov)
4241 (+ (nth 1 ov) (- beg-A beg-B))
4242 (+ (nth 2 ov) (- beg-A beg-B))))
4243 (cdr overlays))
4244 (goto-char (org-element-property :end elem-B)))))
4246 (defun org-element-forward ()
4247 "Move forward by one element.
4248 Move to the next element at the same level, when possible."
4249 (interactive)
4250 (if (org-with-limited-levels (org-at-heading-p))
4251 (let ((origin (point)))
4252 (org-forward-same-level 1)
4253 (unless (org-with-limited-levels (org-at-heading-p))
4254 (goto-char origin)
4255 (error "Cannot move further down")))
4256 (let* ((trail (org-element-at-point 'keep-trail))
4257 (elem (pop trail))
4258 (end (org-element-property :end elem))
4259 (parent (loop for prev in trail
4260 when (>= (org-element-property :end prev) end)
4261 return prev)))
4262 (cond
4263 ((eobp) (error "Cannot move further down"))
4264 ((and parent (= (org-element-property :contents-end parent) end))
4265 (goto-char (org-element-property :end parent)))
4266 (t (goto-char end))))))
4268 (defun org-element-backward ()
4269 "Move backward by one element.
4270 Move to the previous element at the same level, when possible."
4271 (interactive)
4272 (if (org-with-limited-levels (org-at-heading-p))
4273 ;; At an headline, move to the previous one, if any, or stay
4274 ;; here.
4275 (let ((origin (point)))
4276 (org-backward-same-level 1)
4277 (unless (org-with-limited-levels (org-at-heading-p))
4278 (goto-char origin)
4279 (error "Cannot move further up")))
4280 (let* ((trail (org-element-at-point 'keep-trail))
4281 (elem (car trail))
4282 (prev-elem (nth 1 trail))
4283 (beg (org-element-property :begin elem)))
4284 (cond
4285 ;; Move to beginning of current element if point isn't there
4286 ;; already.
4287 ((/= (point) beg) (goto-char beg))
4288 ((not prev-elem) (error "Cannot move further up"))
4289 (t (goto-char (org-element-property :begin prev-elem)))))))
4291 (defun org-element-up ()
4292 "Move to upper element."
4293 (interactive)
4294 (if (org-with-limited-levels (org-at-heading-p))
4295 (unless (org-up-heading-safe)
4296 (error "No surrounding element"))
4297 (let* ((trail (org-element-at-point 'keep-trail))
4298 (elem (pop trail))
4299 (end (org-element-property :end elem))
4300 (parent (loop for prev in trail
4301 when (>= (org-element-property :end prev) end)
4302 return prev)))
4303 (cond
4304 (parent (goto-char (org-element-property :begin parent)))
4305 ((org-before-first-heading-p) (error "No surrounding element"))
4306 (t (org-back-to-heading))))))
4308 (defun org-element-down ()
4309 "Move to inner element."
4310 (interactive)
4311 (let ((element (org-element-at-point)))
4312 (cond
4313 ((memq (org-element-type element) '(plain-list table))
4314 (goto-char (org-element-property :contents-begin element))
4315 (forward-char))
4316 ((memq (org-element-type element) org-element-greater-elements)
4317 ;; If contents are hidden, first disclose them.
4318 (when (org-element-property :hiddenp element) (org-cycle))
4319 (goto-char (org-element-property :contents-begin element)))
4320 (t (error "No inner element")))))
4322 (defun org-element-drag-backward ()
4323 "Move backward element at point."
4324 (interactive)
4325 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
4326 (let* ((trail (org-element-at-point 'keep-trail))
4327 (elem (car trail))
4328 (prev-elem (nth 1 trail)))
4329 ;; Error out if no previous element or previous element is
4330 ;; a parent of the current one.
4331 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
4332 (error "Cannot drag element backward")
4333 (let ((pos (point)))
4334 (org-element-swap-A-B prev-elem elem)
4335 (goto-char (+ (org-element-property :begin prev-elem)
4336 (- pos (org-element-property :begin elem)))))))))
4338 (defun org-element-drag-forward ()
4339 "Move forward element at point."
4340 (interactive)
4341 (let* ((pos (point))
4342 (elem (org-element-at-point)))
4343 (when (= (point-max) (org-element-property :end elem))
4344 (error "Cannot drag element forward"))
4345 (goto-char (org-element-property :end elem))
4346 (let ((next-elem (org-element-at-point)))
4347 (when (or (org-element-nested-p elem next-elem)
4348 (and (eq (org-element-type next-elem) 'headline)
4349 (not (eq (org-element-type elem) 'headline))))
4350 (goto-char pos)
4351 (error "Cannot drag element forward"))
4352 ;; Compute new position of point: it's shifted by NEXT-ELEM
4353 ;; body's length (without final blanks) and by the length of
4354 ;; blanks between ELEM and NEXT-ELEM.
4355 (let ((size-next (- (save-excursion
4356 (goto-char (org-element-property :end next-elem))
4357 (skip-chars-backward " \r\t\n")
4358 (forward-line)
4359 ;; Small correction if buffer doesn't end
4360 ;; with a newline character.
4361 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
4362 (org-element-property :begin next-elem)))
4363 (size-blank (- (org-element-property :end elem)
4364 (save-excursion
4365 (goto-char (org-element-property :end elem))
4366 (skip-chars-backward " \r\t\n")
4367 (forward-line)
4368 (point)))))
4369 (org-element-swap-A-B elem next-elem)
4370 (goto-char (+ pos size-next size-blank))))))
4372 (defun org-element-mark-element ()
4373 "Put point at beginning of this element, mark at end.
4375 Interactively, if this command is repeated or (in Transient Mark
4376 mode) if the mark is active, it marks the next element after the
4377 ones already marked."
4378 (interactive)
4379 (let (deactivate-mark)
4380 (if (or (and (eq last-command this-command) (mark t))
4381 (and transient-mark-mode mark-active))
4382 (set-mark
4383 (save-excursion
4384 (goto-char (mark))
4385 (goto-char (org-element-property :end (org-element-at-point)))))
4386 (let ((element (org-element-at-point)))
4387 (end-of-line)
4388 (push-mark (org-element-property :end element) t t)
4389 (goto-char (org-element-property :begin element))))))
4391 (defun org-narrow-to-element ()
4392 "Narrow buffer to current element."
4393 (interactive)
4394 (let ((elem (org-element-at-point)))
4395 (cond
4396 ((eq (car elem) 'headline)
4397 (narrow-to-region
4398 (org-element-property :begin elem)
4399 (org-element-property :end elem)))
4400 ((memq (car elem) org-element-greater-elements)
4401 (narrow-to-region
4402 (org-element-property :contents-begin elem)
4403 (org-element-property :contents-end elem)))
4405 (narrow-to-region
4406 (org-element-property :begin elem)
4407 (org-element-property :end elem))))))
4409 (defun org-element-transpose ()
4410 "Transpose current and previous elements, keeping blank lines between.
4411 Point is moved after both elements."
4412 (interactive)
4413 (org-skip-whitespace)
4414 (let ((end (org-element-property :end (org-element-at-point))))
4415 (org-element-drag-backward)
4416 (goto-char end)))
4418 (defun org-element-unindent-buffer ()
4419 "Un-indent the visible part of the buffer.
4420 Relative indentation (between items, inside blocks, etc.) isn't
4421 modified."
4422 (interactive)
4423 (unless (eq major-mode 'org-mode)
4424 (error "Cannot un-indent a buffer not in Org mode"))
4425 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
4426 unindent-tree ; For byte-compiler.
4427 (unindent-tree
4428 (function
4429 (lambda (contents)
4430 (mapc
4431 (lambda (element)
4432 (if (memq (org-element-type element) '(headline section))
4433 (funcall unindent-tree (org-element-contents element))
4434 (save-excursion
4435 (save-restriction
4436 (narrow-to-region
4437 (org-element-property :begin element)
4438 (org-element-property :end element))
4439 (org-do-remove-indentation)))))
4440 (reverse contents))))))
4441 (funcall unindent-tree (org-element-contents parse-tree))))
4443 (defun org-element-fill-paragraph (&optional justify)
4444 "Fill element at point, when applicable.
4446 This function only applies to paragraph, comment blocks, example
4447 blocks and fixed-width areas. Also, as a special case, re-align
4448 table when point is at one.
4450 If JUSTIFY is non-nil (interactively, with prefix argument),
4451 justify as well. If `sentence-end-double-space' is non-nil, then
4452 period followed by one space does not end a sentence, so don't
4453 break a line there. The variable `fill-column' controls the
4454 width for filling."
4455 (let ((element (org-element-at-point)))
4456 (case (org-element-type element)
4457 ;; Align Org tables, leave table.el tables as-is.
4458 (table-row (org-table-align) t)
4459 (table
4460 (when (eq (org-element-property :type element) 'org) (org-table-align))
4462 ;; Elements that may contain `line-break' type objects.
4463 ((paragraph verse-block)
4464 (let ((beg (org-element-property :contents-begin element))
4465 (end (org-element-property :contents-end element)))
4466 ;; Do nothing if point is at an affiliated keyword or at
4467 ;; verse block markers.
4468 (if (or (< (point) beg) (>= (point) end)) t
4469 ;; At a verse block, first narrow to current "paragraph"
4470 ;; and set current element to that paragraph.
4471 (save-restriction
4472 (when (eq (org-element-type element) 'verse-block)
4473 (narrow-to-region beg end)
4474 (save-excursion
4475 (end-of-line)
4476 (let ((bol-pos (point-at-bol)))
4477 (re-search-backward org-element-paragraph-separate nil 'move)
4478 (unless (or (bobp) (= (point-at-bol) bol-pos))
4479 (forward-line))
4480 (setq element (org-element-paragraph-parser end)
4481 beg (org-element-property :contents-begin element)
4482 end (org-element-property :contents-end element)))))
4483 ;; Fill paragraph, taking line breaks into consideration.
4484 ;; For that, slice the paragraph using line breaks as
4485 ;; separators, and fill the parts in reverse order to
4486 ;; avoid messing with markers.
4487 (save-excursion
4488 (goto-char end)
4489 (mapc
4490 (lambda (pos)
4491 (fill-region-as-paragraph pos (point) justify)
4492 (goto-char pos))
4493 ;; Find the list of ending positions for line breaks
4494 ;; in the current paragraph. Add paragraph beginning
4495 ;; to include first slice.
4496 (nreverse
4497 (cons beg
4498 (org-element-map
4499 (org-element-parse-objects
4500 beg end nil org-element-all-objects)
4501 'line-break
4502 (lambda (lb) (org-element-property :end lb)))))))) t)))
4503 ;; Elements whose contents should be filled as plain text.
4504 ((comment-block example-block)
4505 (save-restriction
4506 (narrow-to-region
4507 (save-excursion
4508 (goto-char (org-element-property :begin element))
4509 (while (looking-at org-element--affiliated-re) (forward-line))
4510 (forward-line)
4511 (point))
4512 (save-excursion
4513 (goto-char (org-element-property :end element))
4514 (if (bolp) (forward-line -1) (beginning-of-line))
4515 (point)))
4516 (fill-paragraph justify) t))
4517 ;; Ignore every other element.
4518 (otherwise t))))
4521 (provide 'org-element)
4522 ;;; org-element.el ends here