org-export: `org-export-define-backend': the standard way to create a back-end
[org-mode.git] / contrib / lisp / org-element.el
blob8f460471d5b67944857de27835a38aa934a79a6e
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.
126 (defconst org-element-paragraph-separate
127 (concat "\f" "\\|" "^[ \t]*$" "\\|"
128 ;; Headlines and inlinetasks.
129 org-outline-regexp-bol "\\|"
130 ;; Comments, blocks (any type), keywords and babel calls.
131 "^[ \t]*#\\+" "\\|" "^#\\(?: \\|$\\)" "\\|"
132 ;; Lists.
133 (org-item-beginning-re) "\\|"
134 ;; Fixed-width, drawers (any type) and tables.
135 "^[ \t]*[:|]" "\\|"
136 ;; Footnote definitions.
137 org-footnote-definition-re "\\|"
138 ;; Horizontal rules.
139 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
140 ;; LaTeX environments.
141 "^[ \t]*\\\\\\(begin\\|end\\)" "\\|"
142 ;; Planning and Clock lines.
143 "^[ \t]*\\(?:"
144 org-clock-string "\\|"
145 org-closed-string "\\|"
146 org-deadline-string "\\|"
147 org-scheduled-string "\\)")
148 "Regexp to separate paragraphs in an Org buffer.")
150 (defconst org-element-all-elements
151 '(center-block clock comment comment-block drawer dynamic-block example-block
152 export-block fixed-width footnote-definition headline
153 horizontal-rule inlinetask item keyword latex-environment
154 babel-call paragraph plain-list planning property-drawer
155 quote-block quote-section section special-block src-block table
156 table-row verse-block)
157 "Complete list of element types.")
159 (defconst org-element-greater-elements
160 '(center-block drawer dynamic-block footnote-definition headline inlinetask
161 item plain-list quote-block section special-block table)
162 "List of recursive element types aka Greater Elements.")
164 (defconst org-element-all-successors
165 '(export-snippet footnote-reference inline-babel-call inline-src-block
166 latex-or-entity line-break link macro radio-target
167 statistics-cookie sub/superscript table-cell target
168 text-markup timestamp)
169 "Complete list of successors.")
171 (defconst org-element-object-successor-alist
172 '((subscript . sub/superscript) (superscript . sub/superscript)
173 (bold . text-markup) (code . text-markup) (italic . text-markup)
174 (strike-through . text-markup) (underline . text-markup)
175 (verbatim . text-markup) (entity . latex-or-entity)
176 (latex-fragment . latex-or-entity))
177 "Alist of translations between object type and successor name.
179 Sharing the same successor comes handy when, for example, the
180 regexp matching one object can also match the other object.")
182 (defconst org-element-all-objects
183 '(bold code entity export-snippet footnote-reference inline-babel-call
184 inline-src-block italic line-break latex-fragment link macro
185 radio-target statistics-cookie strike-through subscript superscript
186 table-cell target timestamp underline verbatim)
187 "Complete list of object types.")
189 (defconst org-element-recursive-objects
190 '(bold italic link macro subscript radio-target strike-through superscript
191 table-cell underline)
192 "List of recursive object types.")
194 (defconst org-element-block-name-alist
195 '(("CENTER" . org-element-center-block-parser)
196 ("COMMENT" . org-element-comment-block-parser)
197 ("EXAMPLE" . org-element-example-block-parser)
198 ("QUOTE" . org-element-quote-block-parser)
199 ("SRC" . org-element-src-block-parser)
200 ("VERSE" . org-element-verse-block-parser))
201 "Alist between block names and the associated parsing function.
202 Names must be uppercase. Any block whose name has no association
203 is parsed with `org-element-special-block-parser'.")
205 (defconst org-element-affiliated-keywords
206 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
207 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
208 "List of affiliated keywords as strings.
209 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
210 are affiliated keywords and need not to be in this list.")
212 (defconst org-element--affiliated-re
213 (format "[ \t]*#\\+%s:"
214 ;; Regular affiliated keywords.
215 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
216 (regexp-opt org-element-affiliated-keywords)))
217 "Regexp matching any affiliated keyword.
219 Keyword name is put in match group 1. Moreover, if keyword
220 belongs to `org-element-dual-keywords', put the dual value in
221 match group 2.
223 Don't modify it, set `org-element-affiliated-keywords' instead.")
225 (defconst org-element-keyword-translation-alist
226 '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
227 ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
228 ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
229 "Alist of usual translations for keywords.
230 The key is the old name and the value the new one. The property
231 holding their value will be named after the translated name.")
233 (defconst org-element-multiple-keywords '("HEADER")
234 "List of affiliated keywords that can occur more that once in an element.
236 Their value will be consed into a list of strings, which will be
237 returned as the value of the property.
239 This list is checked after translations have been applied. See
240 `org-element-keyword-translation-alist'.
242 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
243 allow multiple occurrences and need not to be in this list.")
245 (defconst org-element-parsed-keywords '("AUTHOR" "CAPTION" "DATE" "TITLE")
246 "List of keywords whose value can be parsed.
248 Their value will be stored as a secondary string: a list of
249 strings and objects.
251 This list is checked after translations have been applied. See
252 `org-element-keyword-translation-alist'.")
254 (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
255 "List of keywords which can have a secondary value.
257 In Org syntax, they can be written with optional square brackets
258 before the colons. For example, results keyword can be
259 associated to a hash value with the following:
261 #+RESULTS[hash-string]: some-source
263 This list is checked after translations have been applied. See
264 `org-element-keyword-translation-alist'.")
266 (defconst org-element-object-restrictions
267 '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
268 radio-target sub/superscript target text-markup timestamp)
269 (footnote-reference export-snippet footnote-reference inline-babel-call
270 inline-src-block latex-or-entity line-break link macro
271 radio-target sub/superscript target text-markup
272 timestamp)
273 (headline inline-babel-call inline-src-block latex-or-entity link macro
274 radio-target statistics-cookie sub/superscript target text-markup
275 timestamp)
276 (inlinetask inline-babel-call inline-src-block latex-or-entity link macro
277 radio-target sub/superscript target text-markup timestamp)
278 (italic export-snippet inline-babel-call inline-src-block latex-or-entity
279 link radio-target sub/superscript target text-markup timestamp)
280 (item export-snippet footnote-reference inline-babel-call latex-or-entity
281 link macro radio-target sub/superscript target text-markup)
282 (keyword latex-or-entity macro sub/superscript text-markup)
283 (link export-snippet inline-babel-call inline-src-block latex-or-entity link
284 sub/superscript text-markup)
285 (macro macro)
286 (paragraph export-snippet footnote-reference inline-babel-call
287 inline-src-block latex-or-entity line-break link macro
288 radio-target statistics-cookie sub/superscript target text-markup
289 timestamp)
290 (radio-target export-snippet latex-or-entity sub/superscript)
291 (strike-through export-snippet inline-babel-call inline-src-block
292 latex-or-entity link radio-target sub/superscript target
293 text-markup timestamp)
294 (subscript export-snippet inline-babel-call inline-src-block latex-or-entity
295 sub/superscript target text-markup)
296 (superscript export-snippet inline-babel-call inline-src-block
297 latex-or-entity sub/superscript target text-markup)
298 (table-cell export-snippet latex-or-entity link macro radio-target
299 sub/superscript target text-markup timestamp)
300 (table-row table-cell)
301 (underline export-snippet inline-babel-call inline-src-block latex-or-entity
302 link radio-target sub/superscript target text-markup timestamp)
303 (verse-block footnote-reference inline-babel-call inline-src-block
304 latex-or-entity line-break link macro radio-target
305 sub/superscript target text-markup timestamp))
306 "Alist of objects restrictions.
308 CAR is an element or object type containing objects and CDR is
309 a list of successors that will be called within an element or
310 object of such type.
312 For example, in a `radio-target' object, one can only find
313 entities, export snippets, latex-fragments, subscript and
314 superscript.
316 This alist also applies to secondary string. For example, an
317 `headline' type element doesn't directly contain objects, but
318 still has an entry since one of its properties (`:title') does.")
320 (defconst org-element-secondary-value-alist
321 '((headline . :title)
322 (inlinetask . :title)
323 (item . :tag)
324 (footnote-reference . :inline-definition))
325 "Alist between element types and location of secondary value.")
329 ;;; Accessors and Setters
331 ;; Provide four accessors: `org-element-type', `org-element-property'
332 ;; `org-element-contents' and `org-element-restriction'.
334 ;; Setter functions allow to modify elements by side effect. There is
335 ;; `org-element-put-property', `org-element-set-contents',
336 ;; `org-element-set-element' and `org-element-adopt-element'. Note
337 ;; that `org-element-set-element' and `org-element-adopt-element' are
338 ;; higher level functions since also update `:parent' property.
340 (defsubst org-element-type (element)
341 "Return type of ELEMENT.
343 The function returns the type of the element or object provided.
344 It can also return the following special value:
345 `plain-text' for a string
346 `org-data' for a complete document
347 nil in any other case."
348 (cond
349 ((not (consp element)) (and (stringp element) 'plain-text))
350 ((symbolp (car element)) (car element))))
352 (defsubst org-element-property (property element)
353 "Extract the value from the PROPERTY of an ELEMENT."
354 (plist-get (nth 1 element) property))
356 (defsubst org-element-contents (element)
357 "Extract contents from an ELEMENT."
358 (and (consp element) (nthcdr 2 element)))
360 (defsubst org-element-restriction (element)
361 "Return restriction associated to ELEMENT.
362 ELEMENT can be an element, an object or a symbol representing an
363 element or object type."
364 (cdr (assq (if (symbolp element) element (org-element-type element))
365 org-element-object-restrictions)))
367 (defsubst org-element-put-property (element property value)
368 "In ELEMENT set PROPERTY to VALUE.
369 Return modified element."
370 (when (consp element)
371 (setcar (cdr element) (plist-put (nth 1 element) property value)))
372 element)
374 (defsubst org-element-set-contents (element &rest contents)
375 "Set ELEMENT contents to CONTENTS.
376 Return modified element."
377 (cond ((not element) (list contents))
378 ((cdr element) (setcdr (cdr element) contents))
379 (t (nconc element contents))))
381 (defsubst org-element-set-element (old new)
382 "Replace element or object OLD with element or object NEW.
383 The function takes care of setting `:parent' property for NEW."
384 ;; OLD can belong to the contents of PARENT or to its secondary
385 ;; string.
386 (let* ((parent (org-element-property :parent old))
387 (sec-loc (cdr (assq (org-element-type parent)
388 org-element-secondary-value-alist)))
389 (sec-value (and sec-loc (org-element-property sec-loc parent)))
390 (place (or (memq old sec-value) (memq old parent))))
391 ;; Make sure NEW has correct `:parent' property.
392 (org-element-put-property new :parent parent)
393 ;; Replace OLD with NEW in PARENT.
394 (setcar place new)))
396 (defsubst org-element-adopt-element (parent child &optional append)
397 "Add an element to the contents of another element.
399 PARENT is an element or object. CHILD is an element, an object,
400 or a string.
402 CHILD is added at the beginning of PARENT contents, unless the
403 optional argument APPEND is non-nil, in which case CHILD is added
404 at the end.
406 The function takes care of setting `:parent' property for CHILD.
407 Return parent element."
408 (if (not parent) (list child)
409 (let ((contents (org-element-contents parent)))
410 (apply 'org-element-set-contents
411 parent
412 (if append (append contents (list child)) (cons child contents))))
413 ;; Link the CHILD element with PARENT.
414 (when (consp child) (org-element-put-property child :parent parent))
415 ;; Return the parent element.
416 parent))
420 ;;; Greater elements
422 ;; For each greater element type, we define a parser and an
423 ;; interpreter.
425 ;; A parser returns the element or object as the list described above.
426 ;; Most of them accepts no argument. Though, exceptions exist. Hence
427 ;; every element containing a secondary string (see
428 ;; `org-element-secondary-value-alist') will accept an optional
429 ;; argument to toggle parsing of that secondary string. Moreover,
430 ;; `item' parser requires current list's structure as its first
431 ;; element.
433 ;; An interpreter accepts two arguments: the list representation of
434 ;; the element or object, and its contents. The latter may be nil,
435 ;; depending on the element or object considered. It returns the
436 ;; appropriate Org syntax, as a string.
438 ;; Parsing functions must follow the naming convention:
439 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
440 ;; defined in `org-element-greater-elements'.
442 ;; Similarly, interpreting functions must follow the naming
443 ;; convention: org-element-TYPE-interpreter.
445 ;; With the exception of `headline' and `item' types, greater elements
446 ;; cannot contain other greater elements of their own type.
448 ;; Beside implementing a parser and an interpreter, adding a new
449 ;; greater element requires to tweak `org-element--current-element'.
450 ;; Moreover, the newly defined type must be added to both
451 ;; `org-element-all-elements' and `org-element-greater-elements'.
454 ;;;; Center Block
456 (defun org-element-center-block-parser (limit)
457 "Parse a center block.
459 LIMIT bounds the search.
461 Return a list whose CAR is `center-block' and CDR is a plist
462 containing `:begin', `:end', `:hiddenp', `:contents-begin',
463 `:contents-end' and `:post-blank' keywords.
465 Assume point is at the beginning of the block."
466 (let ((case-fold-search t))
467 (if (not (save-excursion
468 (re-search-forward "^[ \t]*#\\+END_CENTER" limit t)))
469 ;; Incomplete-block: parse it as a comment.
470 (org-element-comment-parser limit)
471 (let ((contents-end (match-beginning 0)))
472 (save-excursion
473 (let* ((keywords (org-element--collect-affiliated-keywords))
474 (begin (car keywords))
475 (contents-begin (progn (forward-line) (point)))
476 (hidden (org-invisible-p2))
477 (pos-before-blank (progn (goto-char contents-end)
478 (forward-line)
479 (point)))
480 (end (progn (org-skip-whitespace)
481 (if (eobp) (point) (point-at-bol)))))
482 (list 'center-block
483 (nconc
484 (list :begin begin
485 :end end
486 :hiddenp hidden
487 :contents-begin contents-begin
488 :contents-end contents-end
489 :post-blank (count-lines pos-before-blank end))
490 (cadr keywords)))))))))
492 (defun org-element-center-block-interpreter (center-block contents)
493 "Interpret CENTER-BLOCK element as Org syntax.
494 CONTENTS is the contents of the element."
495 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
498 ;;;; Drawer
500 (defun org-element-drawer-parser (limit)
501 "Parse a drawer.
503 LIMIT bounds the search.
505 Return a list whose CAR is `drawer' and CDR is a plist containing
506 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
507 `:contents-end' and `:post-blank' keywords.
509 Assume point is at beginning of drawer."
510 (save-excursion
511 (let* ((case-fold-search t)
512 (name (progn (looking-at org-drawer-regexp)
513 (org-match-string-no-properties 1)))
514 (keywords (org-element--collect-affiliated-keywords))
515 (begin (car keywords))
516 (contents-begin (progn (forward-line) (point)))
517 (hidden (org-invisible-p2))
518 (contents-end (progn (re-search-forward "^[ \t]*:END:" limit t)
519 (point-at-bol)))
520 (pos-before-blank (progn (forward-line) (point)))
521 (end (progn (org-skip-whitespace)
522 (if (eobp) (point) (point-at-bol)))))
523 (list 'drawer
524 (nconc
525 (list :begin begin
526 :end end
527 :drawer-name name
528 :hiddenp hidden
529 :contents-begin contents-begin
530 :contents-end contents-end
531 :post-blank (count-lines pos-before-blank end))
532 (cadr keywords))))))
534 (defun org-element-drawer-interpreter (drawer contents)
535 "Interpret DRAWER element as Org syntax.
536 CONTENTS is the contents of the element."
537 (format ":%s:\n%s:END:"
538 (org-element-property :drawer-name drawer)
539 contents))
542 ;;;; Dynamic Block
544 (defun org-element-dynamic-block-parser (limit)
545 "Parse a dynamic block.
547 LIMIT bounds the search.
549 Return a list whose CAR is `dynamic-block' and CDR is a plist
550 containing `:block-name', `:begin', `:end', `:hiddenp',
551 `:contents-begin', `:contents-end', `:arguments' and
552 `:post-blank' keywords.
554 Assume point is at beginning of dynamic block."
555 (let ((case-fold-search t))
556 (if (not (save-excursion (re-search-forward org-dblock-end-re limit t)))
557 ;; Incomplete block: parse it as a comment.
558 (org-element-comment-parser limit)
559 (let ((contents-end (match-beginning 0)))
560 (save-excursion
561 (let* ((name (progn (looking-at org-dblock-start-re)
562 (org-match-string-no-properties 1)))
563 (arguments (org-match-string-no-properties 3))
564 (keywords (org-element--collect-affiliated-keywords))
565 (begin (car keywords))
566 (contents-begin (progn (forward-line) (point)))
567 (hidden (org-invisible-p2))
568 (pos-before-blank (progn (goto-char contents-end)
569 (forward-line)
570 (point)))
571 (end (progn (org-skip-whitespace)
572 (if (eobp) (point) (point-at-bol)))))
573 (list 'dynamic-block
574 (nconc
575 (list :begin begin
576 :end end
577 :block-name name
578 :arguments arguments
579 :hiddenp hidden
580 :contents-begin contents-begin
581 :contents-end contents-end
582 :post-blank (count-lines pos-before-blank end))
583 (cadr keywords)))))))))
585 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
586 "Interpret DYNAMIC-BLOCK element as Org syntax.
587 CONTENTS is the contents of the element."
588 (format "#+BEGIN: %s%s\n%s#+END:"
589 (org-element-property :block-name dynamic-block)
590 (let ((args (org-element-property :arguments dynamic-block)))
591 (and args (concat " " args)))
592 contents))
595 ;;;; Footnote Definition
597 (defun org-element-footnote-definition-parser (limit)
598 "Parse a footnote definition.
600 LIMIT bounds the search.
602 Return a list whose CAR is `footnote-definition' and CDR is
603 a plist containing `:label', `:begin' `:end', `:contents-begin',
604 `:contents-end' and `:post-blank' keywords.
606 Assume point is at the beginning of the footnote definition."
607 (save-excursion
608 (looking-at org-footnote-definition-re)
609 (let* ((label (org-match-string-no-properties 1))
610 (keywords (org-element--collect-affiliated-keywords))
611 (begin (car keywords))
612 (contents-begin (progn (search-forward "]")
613 (org-skip-whitespace)
614 (point)))
615 (contents-end (if (progn
616 (end-of-line)
617 (re-search-forward
618 (concat org-outline-regexp-bol "\\|"
619 org-footnote-definition-re "\\|"
620 "^[ \t]*$") limit 'move))
621 (match-beginning 0)
622 (point)))
623 (end (progn (org-skip-whitespace)
624 (if (eobp) (point) (point-at-bol)))))
625 (list 'footnote-definition
626 (nconc
627 (list :label label
628 :begin begin
629 :end end
630 :contents-begin contents-begin
631 :contents-end contents-end
632 :post-blank (count-lines contents-end end))
633 (cadr keywords))))))
635 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
636 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
637 CONTENTS is the contents of the footnote-definition."
638 (concat (format "[%s]" (org-element-property :label footnote-definition))
640 contents))
643 ;;;; Headline
645 (defun org-element-headline-parser (limit &optional raw-secondary-p)
646 "Parse an headline.
648 Return a list whose CAR is `headline' and CDR is a plist
649 containing `:raw-value', `:title', `:begin', `:end',
650 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
651 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
652 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
653 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
654 keywords.
656 The plist also contains any property set in the property drawer,
657 with its name in lowercase, the underscores replaced with hyphens
658 and colons at the beginning (i.e. `:custom-id').
660 When RAW-SECONDARY-P is non-nil, headline's title will not be
661 parsed as a secondary string, but as a plain string instead.
663 Assume point is at beginning of the headline."
664 (save-excursion
665 (let* ((components (org-heading-components))
666 (level (nth 1 components))
667 (todo (nth 2 components))
668 (todo-type
669 (and todo (if (member todo org-done-keywords) 'done 'todo)))
670 (tags (let ((raw-tags (nth 5 components)))
671 (and raw-tags (org-split-string raw-tags ":"))))
672 (raw-value (nth 4 components))
673 (quotedp
674 (let ((case-fold-search nil))
675 (string-match (format "^%s +" org-quote-string) raw-value)))
676 (commentedp
677 (let ((case-fold-search nil))
678 (string-match (format "^%s +" org-comment-string) raw-value)))
679 (archivedp (member org-archive-tag tags))
680 (footnote-section-p (and org-footnote-section
681 (string= org-footnote-section raw-value)))
682 (standard-props (let (plist)
683 (mapc
684 (lambda (p)
685 (let ((p-name (downcase (car p))))
686 (while (string-match "_" p-name)
687 (setq p-name
688 (replace-match "-" nil nil p-name)))
689 (setq p-name (intern (concat ":" p-name)))
690 (setq plist
691 (plist-put plist p-name (cdr p)))))
692 (org-entry-properties nil 'standard))
693 plist))
694 (time-props (org-entry-properties nil 'special "CLOCK"))
695 (scheduled (cdr (assoc "SCHEDULED" time-props)))
696 (deadline (cdr (assoc "DEADLINE" time-props)))
697 (clock (cdr (assoc "CLOCK" time-props)))
698 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
699 (begin (point))
700 (pos-after-head (save-excursion (forward-line) (point)))
701 (contents-begin (save-excursion (forward-line)
702 (org-skip-whitespace)
703 (if (eobp) (point) (point-at-bol))))
704 (hidden (save-excursion (forward-line) (org-invisible-p2)))
705 (end (progn (goto-char (org-end-of-subtree t t))))
706 (contents-end (progn (skip-chars-backward " \r\t\n")
707 (forward-line)
708 (point))))
709 ;; Clean RAW-VALUE from any quote or comment string.
710 (when (or quotedp commentedp)
711 (setq raw-value
712 (replace-regexp-in-string
713 (concat "\\(" org-quote-string "\\|" org-comment-string "\\) +")
715 raw-value)))
716 ;; Clean TAGS from archive tag, if any.
717 (when archivedp (setq tags (delete org-archive-tag tags)))
718 (let ((headline
719 (list 'headline
720 (nconc
721 (list :raw-value raw-value
722 :begin begin
723 :end end
724 :pre-blank (count-lines pos-after-head contents-begin)
725 :hiddenp hidden
726 :contents-begin contents-begin
727 :contents-end contents-end
728 :level level
729 :priority (nth 3 components)
730 :tags tags
731 :todo-keyword todo
732 :todo-type todo-type
733 :scheduled scheduled
734 :deadline deadline
735 :timestamp timestamp
736 :clock clock
737 :post-blank (count-lines contents-end end)
738 :footnote-section-p footnote-section-p
739 :archivedp archivedp
740 :commentedp commentedp
741 :quotedp quotedp)
742 standard-props))))
743 (org-element-put-property
744 headline :title
745 (if raw-secondary-p raw-value
746 (org-element-parse-secondary-string
747 raw-value (org-element-restriction 'headline) headline)))))))
749 (defun org-element-headline-interpreter (headline contents)
750 "Interpret HEADLINE element as Org syntax.
751 CONTENTS is the contents of the element."
752 (let* ((level (org-element-property :level headline))
753 (todo (org-element-property :todo-keyword headline))
754 (priority (org-element-property :priority headline))
755 (title (org-element-interpret-data
756 (org-element-property :title headline)))
757 (tags (let ((tag-list (if (org-element-property :archivedp headline)
758 (cons org-archive-tag
759 (org-element-property :tags headline))
760 (org-element-property :tags headline))))
761 (and tag-list
762 (format ":%s:" (mapconcat 'identity tag-list ":")))))
763 (commentedp (org-element-property :commentedp headline))
764 (quotedp (org-element-property :quotedp headline))
765 (pre-blank (or (org-element-property :pre-blank headline) 0))
766 (heading (concat (make-string level ?*)
767 (and todo (concat " " todo))
768 (and quotedp (concat " " org-quote-string))
769 (and commentedp (concat " " org-comment-string))
770 (and priority
771 (format " [#%s]" (char-to-string priority)))
772 (cond ((and org-footnote-section
773 (org-element-property
774 :footnote-section-p headline))
775 (concat " " org-footnote-section))
776 (title (concat " " title))))))
777 (concat heading
778 ;; Align tags.
779 (when tags
780 (cond
781 ((zerop org-tags-column) (format " %s" tags))
782 ((< org-tags-column 0)
783 (concat
784 (make-string
785 (max (- (+ org-tags-column (length heading) (length tags))) 1)
787 tags))
789 (concat
790 (make-string (max (- org-tags-column (length heading)) 1) ? )
791 tags))))
792 (make-string (1+ pre-blank) 10)
793 contents)))
796 ;;;; Inlinetask
798 (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
799 "Parse an inline task.
801 Return a list whose CAR is `inlinetask' and CDR is a plist
802 containing `:title', `:begin', `:end', `:hiddenp',
803 `:contents-begin' and `:contents-end', `:level', `:priority',
804 `:tags', `:todo-keyword', `:todo-type', `:scheduled',
805 `:deadline', `:timestamp', `:clock' and `:post-blank' keywords.
807 The plist also contains any property set in the property drawer,
808 with its name in lowercase, the underscores replaced with hyphens
809 and colons at the beginning (i.e. `:custom-id').
811 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
812 title will not be parsed as a secondary string, but as a plain
813 string instead.
815 Assume point is at beginning of the inline task."
816 (save-excursion
817 (let* ((keywords (org-element--collect-affiliated-keywords))
818 (begin (car keywords))
819 (components (org-heading-components))
820 (todo (nth 2 components))
821 (todo-type (and todo
822 (if (member todo org-done-keywords) 'done 'todo)))
823 (tags (let ((raw-tags (nth 5 components)))
824 (and raw-tags (org-split-string raw-tags ":"))))
825 (standard-props (let (plist)
826 (mapc
827 (lambda (p)
828 (let ((p-name (downcase (car p))))
829 (while (string-match "_" p-name)
830 (setq p-name
831 (replace-match "-" nil nil p-name)))
832 (setq p-name (intern (concat ":" p-name)))
833 (setq plist
834 (plist-put plist p-name (cdr p)))))
835 (org-entry-properties nil 'standard))
836 plist))
837 (time-props (org-entry-properties nil 'special "CLOCK"))
838 (scheduled (cdr (assoc "SCHEDULED" time-props)))
839 (deadline (cdr (assoc "DEADLINE" time-props)))
840 (clock (cdr (assoc "CLOCK" time-props)))
841 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
842 (contents-begin (save-excursion (forward-line) (point)))
843 (hidden (org-invisible-p2))
844 (pos-before-blank (org-inlinetask-goto-end))
845 ;; In the case of a single line task, CONTENTS-BEGIN and
846 ;; CONTENTS-END might overlap.
847 (contents-end (max contents-begin
848 (if (not (bolp)) (point-at-bol)
849 (save-excursion (forward-line -1) (point)))))
850 (end (progn (org-skip-whitespace)
851 (if (eobp) (point) (point-at-bol))))
852 (inlinetask
853 (list 'inlinetask
854 (nconc
855 (list :begin begin
856 :end end
857 :hiddenp (and (> contents-end contents-begin) hidden)
858 :contents-begin contents-begin
859 :contents-end contents-end
860 :level (nth 1 components)
861 :priority (nth 3 components)
862 :tags tags
863 :todo-keyword todo
864 :todo-type todo-type
865 :scheduled scheduled
866 :deadline deadline
867 :timestamp timestamp
868 :clock clock
869 :post-blank (count-lines pos-before-blank end))
870 standard-props
871 (cadr keywords)))))
872 (org-element-put-property
873 inlinetask :title
874 (if raw-secondary-p (nth 4 components)
875 (org-element-parse-secondary-string
876 (nth 4 components)
877 (org-element-restriction 'inlinetask)
878 inlinetask))))))
880 (defun org-element-inlinetask-interpreter (inlinetask contents)
881 "Interpret INLINETASK element as Org syntax.
882 CONTENTS is the contents of inlinetask."
883 (let* ((level (org-element-property :level inlinetask))
884 (todo (org-element-property :todo-keyword inlinetask))
885 (priority (org-element-property :priority inlinetask))
886 (title (org-element-interpret-data
887 (org-element-property :title inlinetask)))
888 (tags (let ((tag-list (org-element-property :tags inlinetask)))
889 (and tag-list
890 (format ":%s:" (mapconcat 'identity tag-list ":")))))
891 (task (concat (make-string level ?*)
892 (and todo (concat " " todo))
893 (and priority
894 (format " [#%s]" (char-to-string priority)))
895 (and title (concat " " title)))))
896 (concat task
897 ;; Align tags.
898 (when tags
899 (cond
900 ((zerop org-tags-column) (format " %s" tags))
901 ((< org-tags-column 0)
902 (concat
903 (make-string
904 (max (- (+ org-tags-column (length task) (length tags))) 1)
906 tags))
908 (concat
909 (make-string (max (- org-tags-column (length task)) 1) ? )
910 tags))))
911 ;; Prefer degenerate inlinetasks when there are no
912 ;; contents.
913 (when contents
914 (concat "\n"
915 contents
916 (make-string level ?*) " END")))))
919 ;;;; Item
921 (defun org-element-item-parser (limit struct &optional raw-secondary-p)
922 "Parse an item.
924 STRUCT is the structure of the plain list.
926 Return a list whose CAR is `item' and CDR is a plist containing
927 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
928 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
929 `:post-blank' keywords.
931 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
932 any, will not be parsed as a secondary string, but as a plain
933 string instead.
935 Assume point is at the beginning of the item."
936 (save-excursion
937 (beginning-of-line)
938 (let* ((begin (point))
939 (bullet (org-list-get-bullet (point) struct))
940 (checkbox (let ((box (org-list-get-checkbox begin struct)))
941 (cond ((equal "[ ]" box) 'off)
942 ((equal "[X]" box) 'on)
943 ((equal "[-]" box) 'trans))))
944 (counter (let ((c (org-list-get-counter begin struct)))
945 (cond
946 ((not c) nil)
947 ((string-match "[A-Za-z]" c)
948 (- (string-to-char (upcase (match-string 0 c)))
949 64))
950 ((string-match "[0-9]+" c)
951 (string-to-number (match-string 0 c))))))
952 (end (org-list-get-item-end begin struct))
953 (contents-begin (progn (looking-at org-list-full-item-re)
954 (goto-char (match-end 0))
955 (org-skip-whitespace)
956 ;; If first line isn't empty,
957 ;; contents really start at the text
958 ;; after item's meta-data.
959 (if (= (point-at-bol) begin) (point)
960 (point-at-bol))))
961 (hidden (progn (forward-line)
962 (and (not (= (point) end))
963 (org-invisible-p2))))
964 (contents-end (progn (goto-char end)
965 (skip-chars-backward " \r\t\n")
966 (forward-line)
967 (point)))
968 (item
969 (list 'item
970 (list :bullet bullet
971 :begin begin
972 :end end
973 ;; CONTENTS-BEGIN and CONTENTS-END may be
974 ;; mixed up in the case of an empty item
975 ;; separated from the next by a blank line.
976 ;; Thus ensure the former is always the
977 ;; smallest.
978 :contents-begin (min contents-begin contents-end)
979 :contents-end (max contents-begin contents-end)
980 :checkbox checkbox
981 :counter counter
982 :hiddenp hidden
983 :structure struct
984 :post-blank (count-lines contents-end end)))))
985 (org-element-put-property
986 item :tag
987 (let ((raw-tag (org-list-get-tag begin struct)))
988 (and raw-tag
989 (if raw-secondary-p raw-tag
990 (org-element-parse-secondary-string
991 raw-tag (org-element-restriction 'item) item))))))))
993 (defun org-element-item-interpreter (item contents)
994 "Interpret ITEM element as Org syntax.
995 CONTENTS is the contents of the element."
996 (let* ((bullet
997 (let* ((beg (org-element-property :begin item))
998 (struct (org-element-property :structure item))
999 (pre (org-list-prevs-alist struct))
1000 (bul (org-element-property :bullet item)))
1001 (org-list-bullet-string
1002 (if (not (eq (org-list-get-list-type beg struct pre) 'ordered)) "-"
1003 (let ((num
1004 (car
1005 (last
1006 (org-list-get-item-number
1007 beg struct pre (org-list-parents-alist struct))))))
1008 (format "%d%s"
1010 (if (eq org-plain-list-ordered-item-terminator ?\)) ")"
1011 ".")))))))
1012 (checkbox (org-element-property :checkbox item))
1013 (counter (org-element-property :counter item))
1014 (tag (let ((tag (org-element-property :tag item)))
1015 (and tag (org-element-interpret-data tag))))
1016 ;; Compute indentation.
1017 (ind (make-string (length bullet) 32))
1018 (item-starts-with-par-p
1019 (eq (org-element-type (car (org-element-contents item)))
1020 'paragraph)))
1021 ;; Indent contents.
1022 (concat
1023 bullet
1024 (and counter (format "[@%d] " counter))
1025 (cond
1026 ((eq checkbox 'on) "[X] ")
1027 ((eq checkbox 'off) "[ ] ")
1028 ((eq checkbox 'trans) "[-] "))
1029 (and tag (format "%s :: " tag))
1030 (let ((contents (replace-regexp-in-string
1031 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
1032 (if item-starts-with-par-p (org-trim contents)
1033 (concat "\n" contents))))))
1036 ;;;; Plain List
1038 (defun org-element-plain-list-parser (limit &optional structure)
1039 "Parse a plain list.
1041 Optional argument STRUCTURE, when non-nil, is the structure of
1042 the plain list being parsed.
1044 Return a list whose CAR is `plain-list' and CDR is a plist
1045 containing `:type', `:begin', `:end', `:contents-begin' and
1046 `:contents-end', `:structure' and `:post-blank' keywords.
1048 Assume point is at the beginning of the list."
1049 (save-excursion
1050 (let* ((struct (or structure (org-list-struct)))
1051 (prevs (org-list-prevs-alist struct))
1052 (parents (org-list-parents-alist struct))
1053 (type (org-list-get-list-type (point) struct prevs))
1054 (contents-begin (point))
1055 (keywords (org-element--collect-affiliated-keywords))
1056 (begin (car keywords))
1057 (contents-end
1058 (goto-char (org-list-get-list-end (point) struct prevs)))
1059 (end (save-excursion (org-skip-whitespace)
1060 (if (eobp) (point) (point-at-bol)))))
1061 ;; Blank lines below list belong to the top-level list only.
1062 (unless (= (org-list-get-top-point struct) contents-begin)
1063 (setq end (min (org-list-get-bottom-point struct)
1064 (progn (org-skip-whitespace)
1065 (if (eobp) (point) (point-at-bol))))))
1066 ;; Return value.
1067 (list 'plain-list
1068 (nconc
1069 (list :type type
1070 :begin begin
1071 :end end
1072 :contents-begin contents-begin
1073 :contents-end contents-end
1074 :structure struct
1075 :post-blank (count-lines contents-end end))
1076 (cadr keywords))))))
1078 (defun org-element-plain-list-interpreter (plain-list contents)
1079 "Interpret PLAIN-LIST element as Org syntax.
1080 CONTENTS is the contents of the element."
1081 contents)
1084 ;;;; Quote Block
1086 (defun org-element-quote-block-parser (limit)
1087 "Parse a quote block.
1089 LIMIT bounds the search.
1091 Return a list whose CAR is `quote-block' and CDR is a plist
1092 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1093 `:contents-end' and `:post-blank' keywords.
1095 Assume point is at the beginning of the block."
1096 (let ((case-fold-search t))
1097 (if (not (save-excursion
1098 (re-search-forward "^[ \t]*#\\+END_QUOTE" limit t)))
1099 ;; Incomplete block: parse it as a comment.
1100 (org-element-comment-parser limit)
1101 (let ((contents-end (match-beginning 0)))
1102 (save-excursion
1103 (let* ((keywords (org-element--collect-affiliated-keywords))
1104 (begin (car keywords))
1105 (contents-begin (progn (forward-line) (point)))
1106 (hidden (org-invisible-p2))
1107 (pos-before-blank (progn (goto-char contents-end)
1108 (forward-line)
1109 (point)))
1110 (end (progn (org-skip-whitespace)
1111 (if (eobp) (point) (point-at-bol)))))
1112 (list 'quote-block
1113 (nconc
1114 (list :begin begin
1115 :end end
1116 :hiddenp hidden
1117 :contents-begin contents-begin
1118 :contents-end contents-end
1119 :post-blank (count-lines pos-before-blank end))
1120 (cadr keywords)))))))))
1122 (defun org-element-quote-block-interpreter (quote-block contents)
1123 "Interpret QUOTE-BLOCK element as Org syntax.
1124 CONTENTS is the contents of the element."
1125 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
1128 ;;;; Section
1130 (defun org-element-section-parser (limit)
1131 "Parse a section.
1133 LIMIT bounds the search.
1135 Return a list whose CAR is `section' and CDR is a plist
1136 containing `:begin', `:end', `:contents-begin', `contents-end'
1137 and `:post-blank' keywords."
1138 (save-excursion
1139 ;; Beginning of section is the beginning of the first non-blank
1140 ;; line after previous headline.
1141 (org-with-limited-levels
1142 (let ((begin (point))
1143 (end (progn (goto-char limit) (point)))
1144 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1145 (forward-line)
1146 (point))))
1147 (list 'section
1148 (list :begin begin
1149 :end end
1150 :contents-begin begin
1151 :contents-end pos-before-blank
1152 :post-blank (count-lines pos-before-blank end)))))))
1154 (defun org-element-section-interpreter (section contents)
1155 "Interpret SECTION element as Org syntax.
1156 CONTENTS is the contents of the element."
1157 contents)
1160 ;;;; Special Block
1162 (defun org-element-special-block-parser (limit)
1163 "Parse a special block.
1165 LIMIT bounds the search.
1167 Return a list whose CAR is `special-block' and CDR is a plist
1168 containing `:type', `:begin', `:end', `:hiddenp',
1169 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1171 Assume point is at the beginning of the block."
1172 (let* ((case-fold-search t)
1173 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
1174 (upcase (match-string-no-properties 1)))))
1175 (if (not (save-excursion
1176 (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
1177 ;; Incomplete block: parse it as a comment.
1178 (org-element-comment-parser limit)
1179 (let ((contents-end (match-beginning 0)))
1180 (save-excursion
1181 (let* ((keywords (org-element--collect-affiliated-keywords))
1182 (begin (car keywords))
1183 (contents-begin (progn (forward-line) (point)))
1184 (hidden (org-invisible-p2))
1185 (pos-before-blank (progn (goto-char contents-end)
1186 (forward-line)
1187 (point)))
1188 (end (progn (org-skip-whitespace)
1189 (if (eobp) (point) (point-at-bol)))))
1190 (list 'special-block
1191 (nconc
1192 (list :type type
1193 :begin begin
1194 :end end
1195 :hiddenp hidden
1196 :contents-begin contents-begin
1197 :contents-end contents-end
1198 :post-blank (count-lines pos-before-blank end))
1199 (cadr keywords)))))))))
1201 (defun org-element-special-block-interpreter (special-block contents)
1202 "Interpret SPECIAL-BLOCK element as Org syntax.
1203 CONTENTS is the contents of the element."
1204 (let ((block-type (org-element-property :type special-block)))
1205 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
1209 ;;; Elements
1211 ;; For each element, a parser and an interpreter are also defined.
1212 ;; Both follow the same naming convention used for greater elements.
1214 ;; Also, as for greater elements, adding a new element type is done
1215 ;; through the following steps: implement a parser and an interpreter,
1216 ;; tweak `org-element--current-element' so that it recognizes the new
1217 ;; type and add that new type to `org-element-all-elements'.
1219 ;; As a special case, when the newly defined type is a block type,
1220 ;; `org-element-block-name-alist' has to be modified accordingly.
1223 ;;;; Babel Call
1225 (defun org-element-babel-call-parser (limit)
1226 "Parse a babel call.
1228 LIMIT bounds the search.
1230 Return a list whose CAR is `babel-call' and CDR is a plist
1231 containing `:begin', `:end', `:info' and `:post-blank' as
1232 keywords."
1233 (save-excursion
1234 (let ((case-fold-search t)
1235 (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
1236 (org-babel-lob-get-info)))
1237 (begin (point-at-bol))
1238 (pos-before-blank (progn (forward-line) (point)))
1239 (end (progn (org-skip-whitespace)
1240 (if (eobp) (point) (point-at-bol)))))
1241 (list 'babel-call
1242 (list :begin begin
1243 :end end
1244 :info info
1245 :post-blank (count-lines pos-before-blank end))))))
1247 (defun org-element-babel-call-interpreter (babel-call contents)
1248 "Interpret BABEL-CALL element as Org syntax.
1249 CONTENTS is nil."
1250 (let* ((babel-info (org-element-property :info babel-call))
1251 (main (car babel-info))
1252 (post-options (nth 1 babel-info)))
1253 (concat "#+CALL: "
1254 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
1255 ;; Remove redundant square brackets.
1256 (replace-match (match-string 1 main) nil nil main))
1257 (and post-options (format "[%s]" post-options)))))
1260 ;;;; Clock
1262 (defun org-element-clock-parser (limit)
1263 "Parse a clock.
1265 LIMIT bounds the search.
1267 Return a list whose CAR is `clock' and CDR is a plist containing
1268 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1269 as keywords."
1270 (save-excursion
1271 (let* ((case-fold-search nil)
1272 (begin (point))
1273 (value (progn (search-forward org-clock-string (line-end-position) t)
1274 (org-skip-whitespace)
1275 (looking-at "\\[.*\\]")
1276 (org-match-string-no-properties 0)))
1277 (time (and (progn (goto-char (match-end 0))
1278 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1279 (org-match-string-no-properties 1)))
1280 (status (if time 'closed 'running))
1281 (post-blank (let ((before-blank (progn (forward-line) (point))))
1282 (org-skip-whitespace)
1283 (unless (eobp) (beginning-of-line))
1284 (count-lines before-blank (point))))
1285 (end (point)))
1286 (list 'clock
1287 (list :status status
1288 :value value
1289 :time time
1290 :begin begin
1291 :end end
1292 :post-blank post-blank)))))
1294 (defun org-element-clock-interpreter (clock contents)
1295 "Interpret CLOCK element as Org syntax.
1296 CONTENTS is nil."
1297 (concat org-clock-string " "
1298 (org-element-property :value clock)
1299 (let ((time (org-element-property :time clock)))
1300 (and time
1301 (concat " => "
1302 (apply 'format
1303 "%2s:%02s"
1304 (org-split-string time ":")))))))
1307 ;;;; Comment
1309 (defun org-element-comment-parser (limit)
1310 "Parse a comment.
1312 LIMIT bounds the search.
1314 Return a list whose CAR is `comment' and CDR is a plist
1315 containing `:begin', `:end', `:value' and `:post-blank'
1316 keywords.
1318 Assume point is at comment beginning."
1319 (save-excursion
1320 (let* ((keywords (org-element--collect-affiliated-keywords))
1321 (begin (car keywords))
1322 ;; Match first line with a loose regexp since it might as
1323 ;; well be an ill-defined keyword.
1324 (value (progn
1325 (looking-at "#\\+? ?")
1326 (buffer-substring-no-properties
1327 (match-end 0) (progn (forward-line) (point)))))
1328 (com-end
1329 ;; Get comments ending.
1330 (progn
1331 (while (and (< (point) limit)
1332 (looking-at "\\(\\(# ?\\)[^+]\\|[ \t]*#\\+\\( \\|$\\)\\)"))
1333 ;; Accumulate lines without leading hash and plus sign
1334 ;; if any. First whitespace is also ignored.
1335 (setq value
1336 (concat value
1337 (buffer-substring-no-properties
1338 (or (match-end 2) (match-end 3))
1339 (progn (forward-line) (point))))))
1340 (point)))
1341 (end (progn (goto-char com-end)
1342 (org-skip-whitespace)
1343 (if (eobp) (point) (point-at-bol)))))
1344 (list 'comment
1345 (nconc
1346 (list :begin begin
1347 :end end
1348 :value value
1349 :post-blank (count-lines com-end end))
1350 (cadr keywords))))))
1352 (defun org-element-comment-interpreter (comment contents)
1353 "Interpret COMMENT element as Org syntax.
1354 CONTENTS is nil."
1355 (replace-regexp-in-string "^" "#+ " (org-element-property :value comment)))
1358 ;;;; Comment Block
1360 (defun org-element-comment-block-parser (limit)
1361 "Parse an export block.
1363 LIMIT bounds the search.
1365 Return a list whose CAR is `comment-block' and CDR is a plist
1366 containing `:begin', `:end', `:hiddenp', `:value' and
1367 `:post-blank' keywords.
1369 Assume point is at comment block beginning."
1370 (let ((case-fold-search t))
1371 (if (not (save-excursion
1372 (re-search-forward "^[ \t]*#\\+END_COMMENT" limit t)))
1373 ;; Incomplete block: parse it as a comment.
1374 (org-element-comment-parser limit)
1375 (let ((contents-end (match-beginning 0)))
1376 (save-excursion
1377 (let* ((keywords (org-element--collect-affiliated-keywords))
1378 (begin (car keywords))
1379 (contents-begin (progn (forward-line) (point)))
1380 (hidden (org-invisible-p2))
1381 (pos-before-blank (progn (goto-char contents-end)
1382 (forward-line)
1383 (point)))
1384 (end (progn (org-skip-whitespace)
1385 (if (eobp) (point) (point-at-bol))))
1386 (value (buffer-substring-no-properties
1387 contents-begin contents-end)))
1388 (list 'comment-block
1389 (nconc
1390 (list :begin begin
1391 :end end
1392 :value value
1393 :hiddenp hidden
1394 :post-blank (count-lines pos-before-blank end))
1395 (cadr keywords)))))))))
1397 (defun org-element-comment-block-interpreter (comment-block contents)
1398 "Interpret COMMENT-BLOCK element as Org syntax.
1399 CONTENTS is nil."
1400 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1401 (org-remove-indentation (org-element-property :value comment-block))))
1404 ;;;; Example Block
1406 (defun org-element-example-block-parser (limit)
1407 "Parse an example block.
1409 LIMIT bounds the search.
1411 Return a list whose CAR is `example-block' and CDR is a plist
1412 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1413 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1414 `:switches', `:value' and `:post-blank' keywords."
1415 (let ((case-fold-search t))
1416 (if (not (save-excursion
1417 (re-search-forward "^[ \t]*#\\+END_EXAMPLE" limit t)))
1418 ;; Incomplete block: parse it as a comment.
1419 (org-element-comment-parser limit)
1420 (let ((contents-end (match-beginning 0)))
1421 (save-excursion
1422 (let* ((switches
1423 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1424 (org-match-string-no-properties 1)))
1425 ;; Switches analysis
1426 (number-lines (cond ((not switches) nil)
1427 ((string-match "-n\\>" switches) 'new)
1428 ((string-match "+n\\>" switches) 'continued)))
1429 (preserve-indent (and switches (string-match "-i\\>" switches)))
1430 ;; Should labels be retained in (or stripped from) example
1431 ;; blocks?
1432 (retain-labels
1433 (or (not switches)
1434 (not (string-match "-r\\>" switches))
1435 (and number-lines (string-match "-k\\>" switches))))
1436 ;; What should code-references use - labels or
1437 ;; line-numbers?
1438 (use-labels
1439 (or (not switches)
1440 (and retain-labels (not (string-match "-k\\>" switches)))))
1441 (label-fmt (and switches
1442 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1443 (match-string 1 switches)))
1444 ;; Standard block parsing.
1445 (keywords (org-element--collect-affiliated-keywords))
1446 (begin (car keywords))
1447 (contents-begin (progn (forward-line) (point)))
1448 (hidden (org-invisible-p2))
1449 (value (buffer-substring-no-properties contents-begin contents-end))
1450 (pos-before-blank (progn (goto-char contents-end)
1451 (forward-line)
1452 (point)))
1453 (end (progn (org-skip-whitespace)
1454 (if (eobp) (point) (point-at-bol)))))
1455 (list 'example-block
1456 (nconc
1457 (list :begin begin
1458 :end end
1459 :value value
1460 :switches switches
1461 :number-lines number-lines
1462 :preserve-indent preserve-indent
1463 :retain-labels retain-labels
1464 :use-labels use-labels
1465 :label-fmt label-fmt
1466 :hiddenp hidden
1467 :post-blank (count-lines pos-before-blank end))
1468 (cadr keywords)))))))))
1470 (defun org-element-example-block-interpreter (example-block contents)
1471 "Interpret EXAMPLE-BLOCK element as Org syntax.
1472 CONTENTS is nil."
1473 (let ((switches (org-element-property :switches example-block)))
1474 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1475 (org-remove-indentation
1476 (org-element-property :value example-block))
1477 "#+END_EXAMPLE")))
1480 ;;;; Export Block
1482 (defun org-element-export-block-parser (limit)
1483 "Parse an export block.
1485 LIMIT bounds the search.
1487 Return a list whose CAR is `export-block' and CDR is a plist
1488 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1489 `:post-blank' keywords.
1491 Assume point is at export-block beginning."
1492 (let* ((case-fold-search t)
1493 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
1494 (upcase (org-match-string-no-properties 1)))))
1495 (if (not (save-excursion
1496 (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
1497 ;; Incomplete block: parse it as a comment.
1498 (org-element-comment-parser limit)
1499 (let ((contents-end (match-beginning 0)))
1500 (save-excursion
1501 (let* ((keywords (org-element--collect-affiliated-keywords))
1502 (begin (car keywords))
1503 (contents-begin (progn (forward-line) (point)))
1504 (hidden (org-invisible-p2))
1505 (pos-before-blank (progn (goto-char contents-end)
1506 (forward-line)
1507 (point)))
1508 (end (progn (org-skip-whitespace)
1509 (if (eobp) (point) (point-at-bol))))
1510 (value (buffer-substring-no-properties contents-begin
1511 contents-end)))
1512 (list 'export-block
1513 (nconc
1514 (list :begin begin
1515 :end end
1516 :type type
1517 :value value
1518 :hiddenp hidden
1519 :post-blank (count-lines pos-before-blank end))
1520 (cadr keywords)))))))))
1522 (defun org-element-export-block-interpreter (export-block contents)
1523 "Interpret EXPORT-BLOCK element as Org syntax.
1524 CONTENTS is nil."
1525 (let ((type (org-element-property :type export-block)))
1526 (concat (format "#+BEGIN_%s\n" type)
1527 (org-element-property :value export-block)
1528 (format "#+END_%s" type))))
1531 ;;;; Fixed-width
1533 (defun org-element-fixed-width-parser (limit)
1534 "Parse a fixed-width section.
1536 LIMIT bounds the search.
1538 Return a list whose CAR is `fixed-width' and CDR is a plist
1539 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1541 Assume point is at the beginning of the fixed-width area."
1542 (save-excursion
1543 (let* ((keywords (org-element--collect-affiliated-keywords))
1544 (begin (car keywords))
1545 value
1546 (end-area
1547 (progn
1548 (while (and (< (point) limit)
1549 (looking-at "[ \t]*:\\( \\|$\\)"))
1550 ;; Accumulate text without starting colons.
1551 (setq value
1552 (concat value
1553 (buffer-substring-no-properties
1554 (match-end 0) (point-at-eol))
1555 "\n"))
1556 (forward-line))
1557 (point)))
1558 (end (progn (org-skip-whitespace)
1559 (if (eobp) (point) (point-at-bol)))))
1560 (list 'fixed-width
1561 (nconc
1562 (list :begin begin
1563 :end end
1564 :value value
1565 :post-blank (count-lines end-area end))
1566 (cadr keywords))))))
1568 (defun org-element-fixed-width-interpreter (fixed-width contents)
1569 "Interpret FIXED-WIDTH element as Org syntax.
1570 CONTENTS is nil."
1571 (replace-regexp-in-string
1572 "^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
1575 ;;;; Horizontal Rule
1577 (defun org-element-horizontal-rule-parser (limit)
1578 "Parse an horizontal rule.
1580 LIMIT bounds the search.
1582 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1583 containing `:begin', `:end' and `:post-blank' keywords."
1584 (save-excursion
1585 (let* ((keywords (org-element--collect-affiliated-keywords))
1586 (begin (car keywords))
1587 (post-hr (progn (forward-line) (point)))
1588 (end (progn (org-skip-whitespace)
1589 (if (eobp) (point) (point-at-bol)))))
1590 (list 'horizontal-rule
1591 (nconc
1592 (list :begin begin
1593 :end end
1594 :post-blank (count-lines post-hr end))
1595 (cadr keywords))))))
1597 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1598 "Interpret HORIZONTAL-RULE element as Org syntax.
1599 CONTENTS is nil."
1600 "-----")
1603 ;;;; Keyword
1605 (defun org-element-keyword-parser (limit)
1606 "Parse a keyword at point.
1608 LIMIT bounds the search.
1610 Return a list whose CAR is `keyword' and CDR is a plist
1611 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1612 keywords."
1613 (save-excursion
1614 (let* ((case-fold-search t)
1615 (begin (point))
1616 (key (progn (looking-at "[ \t]*#\\+\\(\\S-+\\):")
1617 (upcase (org-match-string-no-properties 1))))
1618 (value (org-trim (buffer-substring-no-properties
1619 (match-end 0) (point-at-eol))))
1620 (pos-before-blank (progn (forward-line) (point)))
1621 (end (progn (org-skip-whitespace)
1622 (if (eobp) (point) (point-at-bol)))))
1623 (list 'keyword
1624 (list :key key
1625 :value value
1626 :begin begin
1627 :end end
1628 :post-blank (count-lines pos-before-blank end))))))
1630 (defun org-element-keyword-interpreter (keyword contents)
1631 "Interpret KEYWORD element as Org syntax.
1632 CONTENTS is nil."
1633 (format "#+%s: %s"
1634 (org-element-property :key keyword)
1635 (org-element-property :value keyword)))
1638 ;;;; Latex Environment
1640 (defun org-element-latex-environment-parser (limit)
1641 "Parse a LaTeX environment.
1643 LIMIT bounds the search.
1645 Return a list whose CAR is `latex-environment' and CDR is a plist
1646 containing `:begin', `:end', `:value' and `:post-blank'
1647 keywords.
1649 Assume point is at the beginning of the latex environment."
1650 (save-excursion
1651 (let* ((case-fold-search t)
1652 (code-begin (point))
1653 (keywords (org-element--collect-affiliated-keywords))
1654 (begin (car keywords))
1655 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
1656 (regexp-quote (match-string 1))))
1657 (code-end
1658 (progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env) limit t)
1659 (forward-line)
1660 (point)))
1661 (value (buffer-substring-no-properties code-begin code-end))
1662 (end (progn (org-skip-whitespace)
1663 (if (eobp) (point) (point-at-bol)))))
1664 (list 'latex-environment
1665 (nconc
1666 (list :begin begin
1667 :end end
1668 :value value
1669 :post-blank (count-lines code-end end))
1670 (cadr keywords))))))
1672 (defun org-element-latex-environment-interpreter (latex-environment contents)
1673 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1674 CONTENTS is nil."
1675 (org-element-property :value latex-environment))
1678 ;;;; Paragraph
1680 (defun org-element-paragraph-parser (limit)
1681 "Parse a paragraph.
1683 LIMIT bounds the search.
1685 Return a list whose CAR is `paragraph' and CDR is a plist
1686 containing `:begin', `:end', `:contents-begin' and
1687 `:contents-end' and `:post-blank' keywords.
1689 Assume point is at the beginning of the paragraph."
1690 (save-excursion
1691 (let* ((contents-begin (point))
1692 (keywords (org-element--collect-affiliated-keywords))
1693 (begin (car keywords))
1694 (contents-end
1695 (progn (end-of-line)
1696 (if (re-search-forward org-element-paragraph-separate
1697 limit
1699 (progn (forward-line -1) (end-of-line) (point))
1700 (point))))
1701 (pos-before-blank (progn (forward-line) (point)))
1702 (end (progn (org-skip-whitespace)
1703 (if (eobp) (point) (point-at-bol)))))
1704 (list 'paragraph
1705 ;; If paragraph has no affiliated keywords, it may not begin
1706 ;; at beginning of line if it starts an item.
1707 (nconc
1708 (list :begin (if (cadr keywords) begin contents-begin)
1709 :end end
1710 :contents-begin contents-begin
1711 :contents-end contents-end
1712 :post-blank (count-lines pos-before-blank end))
1713 (cadr keywords))))))
1715 (defun org-element-paragraph-interpreter (paragraph contents)
1716 "Interpret PARAGRAPH element as Org syntax.
1717 CONTENTS is the contents of the element."
1718 contents)
1721 ;;;; Planning
1723 (defun org-element-planning-parser (limit)
1724 "Parse a planning.
1726 LIMIT bounds the search.
1728 Return a list whose CAR is `planning' and CDR is a plist
1729 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1730 and `:post-blank' keywords."
1731 (save-excursion
1732 (let* ((case-fold-search nil)
1733 (begin (point))
1734 (post-blank (let ((before-blank (progn (forward-line) (point))))
1735 (org-skip-whitespace)
1736 (unless (eobp) (beginning-of-line))
1737 (count-lines before-blank (point))))
1738 (end (point))
1739 closed deadline scheduled)
1740 (goto-char begin)
1741 (while (re-search-forward org-keyword-time-not-clock-regexp
1742 (line-end-position) t)
1743 (goto-char (match-end 1))
1744 (org-skip-whitespace)
1745 (let ((time (buffer-substring-no-properties
1746 (1+ (point)) (1- (match-end 0))))
1747 (keyword (match-string 1)))
1748 (cond ((equal keyword org-closed-string) (setq closed time))
1749 ((equal keyword org-deadline-string) (setq deadline time))
1750 (t (setq scheduled time)))))
1751 (list 'planning
1752 (list :closed closed
1753 :deadline deadline
1754 :scheduled scheduled
1755 :begin begin
1756 :end end
1757 :post-blank post-blank)))))
1759 (defun org-element-planning-interpreter (planning contents)
1760 "Interpret PLANNING element as Org syntax.
1761 CONTENTS is nil."
1762 (mapconcat
1763 'identity
1764 (delq nil
1765 (list (let ((closed (org-element-property :closed planning)))
1766 (when closed (concat org-closed-string " [" closed "]")))
1767 (let ((deadline (org-element-property :deadline planning)))
1768 (when deadline (concat org-deadline-string " <" deadline ">")))
1769 (let ((scheduled (org-element-property :scheduled planning)))
1770 (when scheduled
1771 (concat org-scheduled-string " <" scheduled ">")))))
1772 " "))
1775 ;;;; Property Drawer
1777 (defun org-element-property-drawer-parser (limit)
1778 "Parse a property drawer.
1780 LIMIT bounds the search.
1782 Return a list whose CAR is `property-drawer' and CDR is a plist
1783 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1784 `:contents-end', `:properties' and `:post-blank' keywords.
1786 Assume point is at the beginning of the property drawer."
1787 (save-excursion
1788 (let ((case-fold-search t)
1789 (begin (point))
1790 (prop-begin (progn (forward-line) (point)))
1791 (hidden (org-invisible-p2))
1792 (properties
1793 (let (val)
1794 (while (not (looking-at "^[ \t]*:END:"))
1795 (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
1796 (push (cons (org-match-string-no-properties 1)
1797 (org-trim
1798 (buffer-substring-no-properties
1799 (match-end 0) (point-at-eol))))
1800 val))
1801 (forward-line))
1802 val))
1803 (prop-end (progn (re-search-forward "^[ \t]*:END:" limit t)
1804 (point-at-bol)))
1805 (pos-before-blank (progn (forward-line) (point)))
1806 (end (progn (org-skip-whitespace)
1807 (if (eobp) (point) (point-at-bol)))))
1808 (list 'property-drawer
1809 (list :begin begin
1810 :end end
1811 :hiddenp hidden
1812 :properties properties
1813 :post-blank (count-lines pos-before-blank end))))))
1815 (defun org-element-property-drawer-interpreter (property-drawer contents)
1816 "Interpret PROPERTY-DRAWER element as Org syntax.
1817 CONTENTS is nil."
1818 (let ((props (org-element-property :properties property-drawer)))
1819 (concat
1820 ":PROPERTIES:\n"
1821 (mapconcat (lambda (p)
1822 (format org-property-format (format ":%s:" (car p)) (cdr p)))
1823 (nreverse props) "\n")
1824 "\n:END:")))
1827 ;;;; Quote Section
1829 (defun org-element-quote-section-parser (limit)
1830 "Parse a quote section.
1832 LIMIT bounds the search.
1834 Return a list whose CAR is `quote-section' and CDR is a plist
1835 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1837 Assume point is at beginning of the section."
1838 (save-excursion
1839 (let* ((begin (point))
1840 (end (progn (org-with-limited-levels (outline-next-heading))
1841 (point)))
1842 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1843 (forward-line)
1844 (point)))
1845 (value (buffer-substring-no-properties begin pos-before-blank)))
1846 (list 'quote-section
1847 (list :begin begin
1848 :end end
1849 :value value
1850 :post-blank (count-lines pos-before-blank end))))))
1852 (defun org-element-quote-section-interpreter (quote-section contents)
1853 "Interpret QUOTE-SECTION element as Org syntax.
1854 CONTENTS is nil."
1855 (org-element-property :value quote-section))
1858 ;;;; Src Block
1860 (defun org-element-src-block-parser (limit)
1861 "Parse a src block.
1863 LIMIT bounds the search.
1865 Return a list whose CAR is `src-block' and CDR is a plist
1866 containing `:language', `:switches', `:parameters', `:begin',
1867 `:end', `:hiddenp', `:number-lines', `:retain-labels',
1868 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
1869 `:post-blank' keywords.
1871 Assume point is at the beginning of the block."
1872 (let ((case-fold-search t))
1873 (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC" limit t)))
1874 ;; Incomplete block: parse it as a comment.
1875 (org-element-comment-parser limit)
1876 (let ((contents-end (match-beginning 0)))
1877 (save-excursion
1878 (let* ((keywords (org-element--collect-affiliated-keywords))
1879 ;; Get beginning position.
1880 (begin (car keywords))
1881 ;; Get language as a string.
1882 (language
1883 (progn
1884 (looking-at
1885 (concat "^[ \t]*#\\+BEGIN_SRC"
1886 "\\(?: +\\(\\S-+\\)\\)?"
1887 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
1888 "\\(.*\\)[ \t]*$"))
1889 (org-match-string-no-properties 1)))
1890 ;; Get switches.
1891 (switches (org-match-string-no-properties 2))
1892 ;; Get parameters.
1893 (parameters (org-match-string-no-properties 3))
1894 ;; Switches analysis
1895 (number-lines (cond ((not switches) nil)
1896 ((string-match "-n\\>" switches) 'new)
1897 ((string-match "+n\\>" switches) 'continued)))
1898 (preserve-indent (and switches (string-match "-i\\>" switches)))
1899 (label-fmt (and switches
1900 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1901 (match-string 1 switches)))
1902 ;; Should labels be retained in (or stripped from)
1903 ;; src blocks?
1904 (retain-labels
1905 (or (not switches)
1906 (not (string-match "-r\\>" switches))
1907 (and number-lines (string-match "-k\\>" switches))))
1908 ;; What should code-references use - labels or
1909 ;; line-numbers?
1910 (use-labels
1911 (or (not switches)
1912 (and retain-labels (not (string-match "-k\\>" switches)))))
1913 ;; Get visibility status.
1914 (hidden (progn (forward-line) (org-invisible-p2)))
1915 ;; Retrieve code.
1916 (value (buffer-substring-no-properties (point) contents-end))
1917 (pos-before-blank (progn (goto-char contents-end)
1918 (forward-line)
1919 (point)))
1920 ;; Get position after ending blank lines.
1921 (end (progn (org-skip-whitespace)
1922 (if (eobp) (point) (point-at-bol)))))
1923 (list 'src-block
1924 (nconc
1925 (list :language language
1926 :switches (and (org-string-nw-p switches)
1927 (org-trim switches))
1928 :parameters (and (org-string-nw-p parameters)
1929 (org-trim parameters))
1930 :begin begin
1931 :end end
1932 :number-lines number-lines
1933 :preserve-indent preserve-indent
1934 :retain-labels retain-labels
1935 :use-labels use-labels
1936 :label-fmt label-fmt
1937 :hiddenp hidden
1938 :value value
1939 :post-blank (count-lines pos-before-blank end))
1940 (cadr keywords)))))))))
1942 (defun org-element-src-block-interpreter (src-block contents)
1943 "Interpret SRC-BLOCK element as Org syntax.
1944 CONTENTS is nil."
1945 (let ((lang (org-element-property :language src-block))
1946 (switches (org-element-property :switches src-block))
1947 (params (org-element-property :parameters src-block))
1948 (value (let ((val (org-element-property :value src-block)))
1949 (cond
1951 (org-src-preserve-indentation val)
1952 ((zerop org-edit-src-content-indentation)
1953 (org-remove-indentation val))
1955 (let ((ind (make-string
1956 org-edit-src-content-indentation 32)))
1957 (replace-regexp-in-string
1958 "\\(^\\)[ \t]*\\S-" ind
1959 (org-remove-indentation val) nil nil 1)))))))
1960 (concat (format "#+BEGIN_SRC%s\n"
1961 (concat (and lang (concat " " lang))
1962 (and switches (concat " " switches))
1963 (and params (concat " " params))))
1964 value
1965 "#+END_SRC")))
1968 ;;;; Table
1970 (defun org-element-table-parser (limit)
1971 "Parse a table at point.
1973 LIMIT bounds the search.
1975 Return a list whose CAR is `table' and CDR is a plist containing
1976 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
1977 `:contents-end', `:value' and `:post-blank' keywords.
1979 Assume point is at the beginning of the table."
1980 (save-excursion
1981 (let* ((case-fold-search t)
1982 (table-begin (point))
1983 (type (if (org-at-table.el-p) 'table.el 'org))
1984 (keywords (org-element--collect-affiliated-keywords))
1985 (begin (car keywords))
1986 (table-end (goto-char (marker-position (org-table-end t))))
1987 (tblfm (let (acc)
1988 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
1989 (push (org-match-string-no-properties 1) acc)
1990 (forward-line))
1991 acc))
1992 (pos-before-blank (point))
1993 (end (progn (org-skip-whitespace)
1994 (if (eobp) (point) (point-at-bol)))))
1995 (list 'table
1996 (nconc
1997 (list :begin begin
1998 :end end
1999 :type type
2000 :tblfm tblfm
2001 ;; Only `org' tables have contents. `table.el' tables
2002 ;; use a `:value' property to store raw table as
2003 ;; a string.
2004 :contents-begin (and (eq type 'org) table-begin)
2005 :contents-end (and (eq type 'org) table-end)
2006 :value (and (eq type 'table.el)
2007 (buffer-substring-no-properties
2008 table-begin table-end))
2009 :post-blank (count-lines pos-before-blank end))
2010 (cadr keywords))))))
2012 (defun org-element-table-interpreter (table contents)
2013 "Interpret TABLE element as Org syntax.
2014 CONTENTS is nil."
2015 (if (eq (org-element-property :type table) 'table.el)
2016 (org-remove-indentation (org-element-property :value table))
2017 (concat (with-temp-buffer (insert contents)
2018 (org-table-align)
2019 (buffer-string))
2020 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
2021 (reverse (org-element-property :tblfm table))
2022 "\n"))))
2025 ;;;; Table Row
2027 (defun org-element-table-row-parser (limit)
2028 "Parse table row at point.
2030 LIMIT bounds the search.
2032 Return a list whose CAR is `table-row' and CDR is a plist
2033 containing `:begin', `:end', `:contents-begin', `:contents-end',
2034 `:type' and `:post-blank' keywords."
2035 (save-excursion
2036 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
2037 (begin (point))
2038 ;; A table rule has no contents. In that case, ensure
2039 ;; CONTENTS-BEGIN matches CONTENTS-END.
2040 (contents-begin (and (eq type 'standard)
2041 (search-forward "|")
2042 (point)))
2043 (contents-end (and (eq type 'standard)
2044 (progn
2045 (end-of-line)
2046 (skip-chars-backward " \t")
2047 (point))))
2048 (end (progn (forward-line) (point))))
2049 (list 'table-row
2050 (list :type type
2051 :begin begin
2052 :end end
2053 :contents-begin contents-begin
2054 :contents-end contents-end
2055 :post-blank 0)))))
2057 (defun org-element-table-row-interpreter (table-row contents)
2058 "Interpret TABLE-ROW element as Org syntax.
2059 CONTENTS is the contents of the table row."
2060 (if (eq (org-element-property :type table-row) 'rule) "|-"
2061 (concat "| " contents)))
2064 ;;;; Verse Block
2066 (defun org-element-verse-block-parser (limit)
2067 "Parse a verse block.
2069 LIMIT bounds the search.
2071 Return a list whose CAR is `verse-block' and CDR is a plist
2072 containing `:begin', `:end', `:contents-begin', `:contents-end',
2073 `:hiddenp' and `:post-blank' keywords.
2075 Assume point is at beginning of the block."
2076 (let ((case-fold-search t))
2077 (if (not (save-excursion
2078 (re-search-forward "^[ \t]*#\\+END_VERSE" limit t)))
2079 ;; Incomplete block: parse it as a comment.
2080 (org-element-comment-parser limit)
2081 (let ((contents-end (match-beginning 0)))
2082 (save-excursion
2083 (let* ((keywords (org-element--collect-affiliated-keywords))
2084 (begin (car keywords))
2085 (hidden (progn (forward-line) (org-invisible-p2)))
2086 (contents-begin (point))
2087 (pos-before-blank (progn (goto-char contents-end)
2088 (forward-line)
2089 (point)))
2090 (end (progn (org-skip-whitespace)
2091 (if (eobp) (point) (point-at-bol)))))
2092 (list 'verse-block
2093 (nconc
2094 (list :begin begin
2095 :end end
2096 :contents-begin contents-begin
2097 :contents-end contents-end
2098 :hiddenp hidden
2099 :post-blank (count-lines pos-before-blank end))
2100 (cadr keywords)))))))))
2102 (defun org-element-verse-block-interpreter (verse-block contents)
2103 "Interpret VERSE-BLOCK element as Org syntax.
2104 CONTENTS is verse block contents."
2105 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
2109 ;;; Objects
2111 ;; Unlike to elements, interstices can be found between objects.
2112 ;; That's why, along with the parser, successor functions are provided
2113 ;; for each object. Some objects share the same successor (i.e. `code'
2114 ;; and `verbatim' objects).
2116 ;; A successor must accept a single argument bounding the search. It
2117 ;; will return either a cons cell whose CAR is the object's type, as
2118 ;; a symbol, and CDR the position of its next occurrence, or nil.
2120 ;; Successors follow the naming convention:
2121 ;; org-element-NAME-successor, where NAME is the name of the
2122 ;; successor, as defined in `org-element-all-successors'.
2124 ;; Some object types (i.e. `emphasis') are recursive. Restrictions on
2125 ;; object types they can contain will be specified in
2126 ;; `org-element-object-restrictions'.
2128 ;; Adding a new type of object is simple. Implement a successor,
2129 ;; a parser, and an interpreter for it, all following the naming
2130 ;; convention. Register type in `org-element-all-objects' and
2131 ;; successor in `org-element-all-successors'. Maybe tweak
2132 ;; restrictions about it, and that's it.
2135 ;;;; Bold
2137 (defun org-element-bold-parser ()
2138 "Parse bold object at point.
2140 Return a list whose CAR is `bold' and CDR is a plist with
2141 `:begin', `:end', `:contents-begin' and `:contents-end' and
2142 `:post-blank' keywords.
2144 Assume point is at the first star marker."
2145 (save-excursion
2146 (unless (bolp) (backward-char 1))
2147 (looking-at org-emph-re)
2148 (let ((begin (match-beginning 2))
2149 (contents-begin (match-beginning 4))
2150 (contents-end (match-end 4))
2151 (post-blank (progn (goto-char (match-end 2))
2152 (skip-chars-forward " \t")))
2153 (end (point)))
2154 (list 'bold
2155 (list :begin begin
2156 :end end
2157 :contents-begin contents-begin
2158 :contents-end contents-end
2159 :post-blank post-blank)))))
2161 (defun org-element-bold-interpreter (bold contents)
2162 "Interpret BOLD object as Org syntax.
2163 CONTENTS is the contents of the object."
2164 (format "*%s*" contents))
2166 (defun org-element-text-markup-successor (limit)
2167 "Search for the next text-markup object.
2169 LIMIT bounds the search.
2171 LIMIT bounds the search.
2173 Return value is a cons cell whose CAR is a symbol among `bold',
2174 `italic', `underline', `strike-through', `code' and `verbatim'
2175 and CDR is beginning position."
2176 (save-excursion
2177 (unless (bolp) (backward-char))
2178 (when (re-search-forward org-emph-re limit t)
2179 (let ((marker (match-string 3)))
2180 (cons (cond
2181 ((equal marker "*") 'bold)
2182 ((equal marker "/") 'italic)
2183 ((equal marker "_") 'underline)
2184 ((equal marker "+") 'strike-through)
2185 ((equal marker "~") 'code)
2186 ((equal marker "=") 'verbatim)
2187 (t (error "Unknown marker at %d" (match-beginning 3))))
2188 (match-beginning 2))))))
2191 ;;;; Code
2193 (defun org-element-code-parser ()
2194 "Parse code object at point.
2196 Return a list whose CAR is `code' and CDR is a plist with
2197 `:value', `:begin', `:end' and `:post-blank' keywords.
2199 Assume point is at the first tilde marker."
2200 (save-excursion
2201 (unless (bolp) (backward-char 1))
2202 (looking-at org-emph-re)
2203 (let ((begin (match-beginning 2))
2204 (value (org-match-string-no-properties 4))
2205 (post-blank (progn (goto-char (match-end 2))
2206 (skip-chars-forward " \t")))
2207 (end (point)))
2208 (list 'code
2209 (list :value value
2210 :begin begin
2211 :end end
2212 :post-blank post-blank)))))
2214 (defun org-element-code-interpreter (code contents)
2215 "Interpret CODE object as Org syntax.
2216 CONTENTS is nil."
2217 (format "~%s~" (org-element-property :value code)))
2220 ;;;; Entity
2222 (defun org-element-entity-parser ()
2223 "Parse entity at point.
2225 Return a list whose CAR is `entity' and CDR a plist with
2226 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2227 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2228 keywords.
2230 Assume point is at the beginning of the entity."
2231 (save-excursion
2232 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2233 (let* ((value (org-entity-get (match-string 1)))
2234 (begin (match-beginning 0))
2235 (bracketsp (string= (match-string 2) "{}"))
2236 (post-blank (progn (goto-char (match-end 1))
2237 (when bracketsp (forward-char 2))
2238 (skip-chars-forward " \t")))
2239 (end (point)))
2240 (list 'entity
2241 (list :name (car value)
2242 :latex (nth 1 value)
2243 :latex-math-p (nth 2 value)
2244 :html (nth 3 value)
2245 :ascii (nth 4 value)
2246 :latin1 (nth 5 value)
2247 :utf-8 (nth 6 value)
2248 :begin begin
2249 :end end
2250 :use-brackets-p bracketsp
2251 :post-blank post-blank)))))
2253 (defun org-element-entity-interpreter (entity contents)
2254 "Interpret ENTITY object as Org syntax.
2255 CONTENTS is nil."
2256 (concat "\\"
2257 (org-element-property :name entity)
2258 (when (org-element-property :use-brackets-p entity) "{}")))
2260 (defun org-element-latex-or-entity-successor (limit)
2261 "Search for the next latex-fragment or entity object.
2263 LIMIT bounds the search.
2265 Return value is a cons cell whose CAR is `entity' or
2266 `latex-fragment' and CDR is beginning position."
2267 (save-excursion
2268 (let ((matchers (plist-get org-format-latex-options :matchers))
2269 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2270 (entity-re
2271 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2272 (when (re-search-forward
2273 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
2274 matchers "\\|")
2275 "\\|" entity-re)
2276 limit t)
2277 (goto-char (match-beginning 0))
2278 (if (looking-at entity-re)
2279 ;; Determine if it's a real entity or a LaTeX command.
2280 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
2281 (match-beginning 0))
2282 ;; No entity nor command: point is at a LaTeX fragment.
2283 ;; Determine its type to get the correct beginning position.
2284 (cons 'latex-fragment
2285 (catch 'return
2286 (mapc (lambda (e)
2287 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
2288 (throw 'return
2289 (match-beginning
2290 (nth 2 (assoc e org-latex-regexps))))))
2291 matchers)
2292 (point))))))))
2295 ;;;; Export Snippet
2297 (defun org-element-export-snippet-parser ()
2298 "Parse export snippet at point.
2300 Return a list whose CAR is `export-snippet' and CDR a plist with
2301 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2302 keywords.
2304 Assume point is at the beginning of the snippet."
2305 (save-excursion
2306 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
2307 (let* ((begin (match-beginning 0))
2308 (back-end (org-match-string-no-properties 1))
2309 (value (buffer-substring-no-properties
2310 (point)
2311 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
2312 (post-blank (skip-chars-forward " \t"))
2313 (end (point)))
2314 (list 'export-snippet
2315 (list :back-end back-end
2316 :value value
2317 :begin begin
2318 :end end
2319 :post-blank post-blank)))))
2321 (defun org-element-export-snippet-interpreter (export-snippet contents)
2322 "Interpret EXPORT-SNIPPET object as Org syntax.
2323 CONTENTS is nil."
2324 (format "@@%s:%s@@"
2325 (org-element-property :back-end export-snippet)
2326 (org-element-property :value export-snippet)))
2328 (defun org-element-export-snippet-successor (limit)
2329 "Search for the next export-snippet object.
2331 LIMIT bounds the search.
2333 Return value is a cons cell whose CAR is `export-snippet' and CDR
2334 its beginning position."
2335 (save-excursion
2336 (let (beg)
2337 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
2338 (setq beg (match-beginning 0))
2339 (re-search-forward "@@" limit t))
2340 (cons 'export-snippet beg)))))
2343 ;;;; Footnote Reference
2345 (defun org-element-footnote-reference-parser ()
2346 "Parse footnote reference at point.
2348 Return a list whose CAR is `footnote-reference' and CDR a plist
2349 with `:label', `:type', `:inline-definition', `:begin', `:end'
2350 and `:post-blank' as keywords."
2351 (save-excursion
2352 (looking-at org-footnote-re)
2353 (let* ((begin (point))
2354 (label (or (org-match-string-no-properties 2)
2355 (org-match-string-no-properties 3)
2356 (and (match-string 1)
2357 (concat "fn:" (org-match-string-no-properties 1)))))
2358 (type (if (or (not label) (match-string 1)) 'inline 'standard))
2359 (inner-begin (match-end 0))
2360 (inner-end
2361 (let ((count 1))
2362 (forward-char)
2363 (while (and (> count 0) (re-search-forward "[][]" nil t))
2364 (if (equal (match-string 0) "[") (incf count) (decf count)))
2365 (1- (point))))
2366 (post-blank (progn (goto-char (1+ inner-end))
2367 (skip-chars-forward " \t")))
2368 (end (point))
2369 (footnote-reference
2370 (list 'footnote-reference
2371 (list :label label
2372 :type type
2373 :begin begin
2374 :end end
2375 :post-blank post-blank))))
2376 (org-element-put-property
2377 footnote-reference :inline-definition
2378 (and (eq type 'inline)
2379 (org-element-parse-secondary-string
2380 (buffer-substring inner-begin inner-end)
2381 (org-element-restriction 'footnote-reference)
2382 footnote-reference))))))
2384 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
2385 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2386 CONTENTS is nil."
2387 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
2388 (def
2389 (let ((inline-def
2390 (org-element-property :inline-definition footnote-reference)))
2391 (if (not inline-def) ""
2392 (concat ":" (org-element-interpret-data inline-def))))))
2393 (format "[%s]" (concat label def))))
2395 (defun org-element-footnote-reference-successor (limit)
2396 "Search for the next footnote-reference object.
2398 LIMIT bounds the search.
2400 Return value is a cons cell whose CAR is `footnote-reference' and
2401 CDR is beginning position."
2402 (save-excursion
2403 (catch 'exit
2404 (while (re-search-forward org-footnote-re limit t)
2405 (save-excursion
2406 (let ((beg (match-beginning 0))
2407 (count 1))
2408 (backward-char)
2409 (while (re-search-forward "[][]" limit t)
2410 (if (equal (match-string 0) "[") (incf count) (decf count))
2411 (when (zerop count)
2412 (throw 'exit (cons 'footnote-reference beg))))))))))
2415 ;;;; Inline Babel Call
2417 (defun org-element-inline-babel-call-parser ()
2418 "Parse inline babel call at point.
2420 Return a list whose CAR is `inline-babel-call' and CDR a plist
2421 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2423 Assume point is at the beginning of the babel call."
2424 (save-excursion
2425 (unless (bolp) (backward-char))
2426 (looking-at org-babel-inline-lob-one-liner-regexp)
2427 (let ((info (save-match-data (org-babel-lob-get-info)))
2428 (begin (match-end 1))
2429 (post-blank (progn (goto-char (match-end 0))
2430 (skip-chars-forward " \t")))
2431 (end (point)))
2432 (list 'inline-babel-call
2433 (list :begin begin
2434 :end end
2435 :info info
2436 :post-blank post-blank)))))
2438 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2439 "Interpret INLINE-BABEL-CALL object as Org syntax.
2440 CONTENTS is nil."
2441 (let* ((babel-info (org-element-property :info inline-babel-call))
2442 (main-source (car babel-info))
2443 (post-options (nth 1 babel-info)))
2444 (concat "call_"
2445 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2446 ;; Remove redundant square brackets.
2447 (replace-match
2448 (match-string 1 main-source) nil nil main-source)
2449 main-source)
2450 (and post-options (format "[%s]" post-options)))))
2452 (defun org-element-inline-babel-call-successor (limit)
2453 "Search for the next inline-babel-call object.
2455 LIMIT bounds the search.
2457 Return value is a cons cell whose CAR is `inline-babel-call' and
2458 CDR is beginning position."
2459 (save-excursion
2460 ;; Use a simplified version of
2461 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
2462 (when (re-search-forward
2463 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
2464 limit t)
2465 (cons 'inline-babel-call (match-beginning 0)))))
2468 ;;;; Inline Src Block
2470 (defun org-element-inline-src-block-parser ()
2471 "Parse inline source block at point.
2473 LIMIT bounds the search.
2475 Return a list whose CAR is `inline-src-block' and CDR a plist
2476 with `:begin', `:end', `:language', `:value', `:parameters' and
2477 `:post-blank' as keywords.
2479 Assume point is at the beginning of the inline src block."
2480 (save-excursion
2481 (unless (bolp) (backward-char))
2482 (looking-at org-babel-inline-src-block-regexp)
2483 (let ((begin (match-beginning 1))
2484 (language (org-match-string-no-properties 2))
2485 (parameters (org-match-string-no-properties 4))
2486 (value (org-match-string-no-properties 5))
2487 (post-blank (progn (goto-char (match-end 0))
2488 (skip-chars-forward " \t")))
2489 (end (point)))
2490 (list 'inline-src-block
2491 (list :language language
2492 :value value
2493 :parameters parameters
2494 :begin begin
2495 :end end
2496 :post-blank post-blank)))))
2498 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2499 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2500 CONTENTS is nil."
2501 (let ((language (org-element-property :language inline-src-block))
2502 (arguments (org-element-property :parameters inline-src-block))
2503 (body (org-element-property :value inline-src-block)))
2504 (format "src_%s%s{%s}"
2505 language
2506 (if arguments (format "[%s]" arguments) "")
2507 body)))
2509 (defun org-element-inline-src-block-successor (limit)
2510 "Search for the next inline-babel-call element.
2512 LIMIT bounds the search.
2514 Return value is a cons cell whose CAR is `inline-babel-call' and
2515 CDR is beginning position."
2516 (save-excursion
2517 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
2518 (cons 'inline-src-block (match-beginning 1)))))
2520 ;;;; Italic
2522 (defun org-element-italic-parser ()
2523 "Parse italic object at point.
2525 Return a list whose CAR is `italic' and CDR is a plist with
2526 `:begin', `:end', `:contents-begin' and `:contents-end' and
2527 `:post-blank' keywords.
2529 Assume point is at the first slash marker."
2530 (save-excursion
2531 (unless (bolp) (backward-char 1))
2532 (looking-at org-emph-re)
2533 (let ((begin (match-beginning 2))
2534 (contents-begin (match-beginning 4))
2535 (contents-end (match-end 4))
2536 (post-blank (progn (goto-char (match-end 2))
2537 (skip-chars-forward " \t")))
2538 (end (point)))
2539 (list 'italic
2540 (list :begin begin
2541 :end end
2542 :contents-begin contents-begin
2543 :contents-end contents-end
2544 :post-blank post-blank)))))
2546 (defun org-element-italic-interpreter (italic contents)
2547 "Interpret ITALIC object as Org syntax.
2548 CONTENTS is the contents of the object."
2549 (format "/%s/" contents))
2552 ;;;; Latex Fragment
2554 (defun org-element-latex-fragment-parser ()
2555 "Parse latex fragment at point.
2557 Return a list whose CAR is `latex-fragment' and CDR a plist with
2558 `:value', `:begin', `:end', and `:post-blank' as keywords.
2560 Assume point is at the beginning of the latex fragment."
2561 (save-excursion
2562 (let* ((begin (point))
2563 (substring-match
2564 (catch 'exit
2565 (mapc (lambda (e)
2566 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
2567 (when (or (looking-at latex-regexp)
2568 (and (not (bobp))
2569 (save-excursion
2570 (backward-char)
2571 (looking-at latex-regexp))))
2572 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
2573 (plist-get org-format-latex-options :matchers))
2574 ;; None found: it's a macro.
2575 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2577 (value (match-string-no-properties substring-match))
2578 (post-blank (progn (goto-char (match-end substring-match))
2579 (skip-chars-forward " \t")))
2580 (end (point)))
2581 (list 'latex-fragment
2582 (list :value value
2583 :begin begin
2584 :end end
2585 :post-blank post-blank)))))
2587 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
2588 "Interpret LATEX-FRAGMENT object as Org syntax.
2589 CONTENTS is nil."
2590 (org-element-property :value latex-fragment))
2592 ;;;; Line Break
2594 (defun org-element-line-break-parser ()
2595 "Parse line break at point.
2597 Return a list whose CAR is `line-break', and CDR a plist with
2598 `:begin', `:end' and `:post-blank' keywords.
2600 Assume point is at the beginning of the line break."
2601 (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
2603 (defun org-element-line-break-interpreter (line-break contents)
2604 "Interpret LINE-BREAK object as Org syntax.
2605 CONTENTS is nil."
2606 "\\\\")
2608 (defun org-element-line-break-successor (limit)
2609 "Search for the next line-break object.
2611 LIMIT bounds the search.
2613 Return value is a cons cell whose CAR is `line-break' and CDR is
2614 beginning position."
2615 (save-excursion
2616 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
2617 (goto-char (match-beginning 1)))))
2618 ;; A line break can only happen on a non-empty line.
2619 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
2620 (cons 'line-break beg)))))
2623 ;;;; Link
2625 (defun org-element-link-parser ()
2626 "Parse link at point.
2628 Return a list whose CAR is `link' and CDR a plist with `:type',
2629 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2630 `:contents-end' and `:post-blank' as keywords.
2632 Assume point is at the beginning of the link."
2633 (save-excursion
2634 (let ((begin (point))
2635 end contents-begin contents-end link-end post-blank path type
2636 raw-link link)
2637 (cond
2638 ;; Type 1: Text targeted from a radio target.
2639 ((and org-target-link-regexp (looking-at org-target-link-regexp))
2640 (setq type "radio"
2641 link-end (match-end 0)
2642 path (org-match-string-no-properties 0)))
2643 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2644 ((looking-at org-bracket-link-regexp)
2645 (setq contents-begin (match-beginning 3)
2646 contents-end (match-end 3)
2647 link-end (match-end 0)
2648 ;; RAW-LINK is the original link.
2649 raw-link (org-match-string-no-properties 1)
2650 link (org-translate-link
2651 (org-link-expand-abbrev
2652 (org-link-unescape raw-link))))
2653 ;; Determine TYPE of link and set PATH accordingly.
2654 (cond
2655 ;; File type.
2656 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
2657 (setq type "file" path link))
2658 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2659 ((string-match org-link-re-with-space3 link)
2660 (setq type (match-string 1 link) path (match-string 2 link)))
2661 ;; Id type: PATH is the id.
2662 ((string-match "^id:\\([-a-f0-9]+\\)" link)
2663 (setq type "id" path (match-string 1 link)))
2664 ;; Code-ref type: PATH is the name of the reference.
2665 ((string-match "^(\\(.*\\))$" link)
2666 (setq type "coderef" path (match-string 1 link)))
2667 ;; Custom-id type: PATH is the name of the custom id.
2668 ((= (aref link 0) ?#)
2669 (setq type "custom-id" path (substring link 1)))
2670 ;; Fuzzy type: Internal link either matches a target, an
2671 ;; headline name or nothing. PATH is the target or
2672 ;; headline's name.
2673 (t (setq type "fuzzy" path link))))
2674 ;; Type 3: Plain link, i.e. http://orgmode.org
2675 ((looking-at org-plain-link-re)
2676 (setq raw-link (org-match-string-no-properties 0)
2677 type (org-match-string-no-properties 1)
2678 path (org-match-string-no-properties 2)
2679 link-end (match-end 0)))
2680 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2681 ((looking-at org-angle-link-re)
2682 (setq raw-link (buffer-substring-no-properties
2683 (match-beginning 1) (match-end 2))
2684 type (org-match-string-no-properties 1)
2685 path (org-match-string-no-properties 2)
2686 link-end (match-end 0))))
2687 ;; In any case, deduce end point after trailing white space from
2688 ;; LINK-END variable.
2689 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
2690 end (point))
2691 (list 'link
2692 (list :type type
2693 :path path
2694 :raw-link (or raw-link path)
2695 :begin begin
2696 :end end
2697 :contents-begin contents-begin
2698 :contents-end contents-end
2699 :post-blank post-blank)))))
2701 (defun org-element-link-interpreter (link contents)
2702 "Interpret LINK object as Org syntax.
2703 CONTENTS is the contents of the object, or nil."
2704 (let ((type (org-element-property :type link))
2705 (raw-link (org-element-property :raw-link link)))
2706 (if (string= type "radio") raw-link
2707 (format "[[%s]%s]"
2708 raw-link
2709 (if contents (format "[%s]" contents) "")))))
2711 (defun org-element-link-successor (limit)
2712 "Search for the next link object.
2714 LIMIT bounds the search.
2716 Return value is a cons cell whose CAR is `link' and CDR is
2717 beginning position."
2718 (save-excursion
2719 (let ((link-regexp
2720 (if (not org-target-link-regexp) org-any-link-re
2721 (concat org-any-link-re "\\|" org-target-link-regexp))))
2722 (when (re-search-forward link-regexp limit t)
2723 (cons 'link (match-beginning 0))))))
2726 ;;;; Macro
2728 (defun org-element-macro-parser ()
2729 "Parse macro at point.
2731 Return a list whose CAR is `macro' and CDR a plist with `:key',
2732 `:args', `:begin', `:end', `:value' and `:post-blank' as
2733 keywords.
2735 Assume point is at the macro."
2736 (save-excursion
2737 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2738 (let ((begin (point))
2739 (key (downcase (org-match-string-no-properties 1)))
2740 (value (org-match-string-no-properties 0))
2741 (post-blank (progn (goto-char (match-end 0))
2742 (skip-chars-forward " \t")))
2743 (end (point))
2744 (args (let ((args (org-match-string-no-properties 3)) args2)
2745 (when args
2746 (setq args (org-split-string args ","))
2747 (while args
2748 (while (string-match "\\\\\\'" (car args))
2749 ;; Repair bad splits.
2750 (setcar (cdr args) (concat (substring (car args) 0 -1)
2751 "," (nth 1 args)))
2752 (pop args))
2753 (push (pop args) args2))
2754 (mapcar 'org-trim (nreverse args2))))))
2755 (list 'macro
2756 (list :key key
2757 :value value
2758 :args args
2759 :begin begin
2760 :end end
2761 :post-blank post-blank)))))
2763 (defun org-element-macro-interpreter (macro contents)
2764 "Interpret MACRO object as Org syntax.
2765 CONTENTS is nil."
2766 (org-element-property :value macro))
2768 (defun org-element-macro-successor (limit)
2769 "Search for the next macro object.
2771 LIMIT bounds the search.
2773 Return value is cons cell whose CAR is `macro' and CDR is
2774 beginning position."
2775 (save-excursion
2776 (when (re-search-forward
2777 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2778 limit t)
2779 (cons 'macro (match-beginning 0)))))
2782 ;;;; Radio-target
2784 (defun org-element-radio-target-parser ()
2785 "Parse radio target at point.
2787 Return a list whose CAR is `radio-target' and CDR a plist with
2788 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2789 and `:post-blank' as keywords.
2791 Assume point is at the radio target."
2792 (save-excursion
2793 (looking-at org-radio-target-regexp)
2794 (let ((begin (point))
2795 (contents-begin (match-beginning 1))
2796 (contents-end (match-end 1))
2797 (value (org-match-string-no-properties 1))
2798 (post-blank (progn (goto-char (match-end 0))
2799 (skip-chars-forward " \t")))
2800 (end (point)))
2801 (list 'radio-target
2802 (list :begin begin
2803 :end end
2804 :contents-begin contents-begin
2805 :contents-end contents-end
2806 :post-blank post-blank
2807 :value value)))))
2809 (defun org-element-radio-target-interpreter (target contents)
2810 "Interpret TARGET object as Org syntax.
2811 CONTENTS is the contents of the object."
2812 (concat "<<<" contents ">>>"))
2814 (defun org-element-radio-target-successor (limit)
2815 "Search for the next radio-target object.
2817 LIMIT bounds the search.
2819 Return value is a cons cell whose CAR is `radio-target' and CDR
2820 is beginning position."
2821 (save-excursion
2822 (when (re-search-forward org-radio-target-regexp limit t)
2823 (cons 'radio-target (match-beginning 0)))))
2826 ;;;; Statistics Cookie
2828 (defun org-element-statistics-cookie-parser ()
2829 "Parse statistics cookie at point.
2831 Return a list whose CAR is `statistics-cookie', and CDR a plist
2832 with `:begin', `:end', `:value' and `:post-blank' keywords.
2834 Assume point is at the beginning of the statistics-cookie."
2835 (save-excursion
2836 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2837 (let* ((begin (point))
2838 (value (buffer-substring-no-properties
2839 (match-beginning 0) (match-end 0)))
2840 (post-blank (progn (goto-char (match-end 0))
2841 (skip-chars-forward " \t")))
2842 (end (point)))
2843 (list 'statistics-cookie
2844 (list :begin begin
2845 :end end
2846 :value value
2847 :post-blank post-blank)))))
2849 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
2850 "Interpret STATISTICS-COOKIE object as Org syntax.
2851 CONTENTS is nil."
2852 (org-element-property :value statistics-cookie))
2854 (defun org-element-statistics-cookie-successor (limit)
2855 "Search for the next statistics cookie object.
2857 LIMIT bounds the search.
2859 Return value is a cons cell whose CAR is `statistics-cookie' and
2860 CDR is beginning position."
2861 (save-excursion
2862 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
2863 (cons 'statistics-cookie (match-beginning 0)))))
2866 ;;;; Strike-Through
2868 (defun org-element-strike-through-parser ()
2869 "Parse strike-through object at point.
2871 Return a list whose CAR is `strike-through' and CDR is a plist
2872 with `:begin', `:end', `:contents-begin' and `:contents-end' and
2873 `:post-blank' keywords.
2875 Assume point is at the first plus sign marker."
2876 (save-excursion
2877 (unless (bolp) (backward-char 1))
2878 (looking-at org-emph-re)
2879 (let ((begin (match-beginning 2))
2880 (contents-begin (match-beginning 4))
2881 (contents-end (match-end 4))
2882 (post-blank (progn (goto-char (match-end 2))
2883 (skip-chars-forward " \t")))
2884 (end (point)))
2885 (list 'strike-through
2886 (list :begin begin
2887 :end end
2888 :contents-begin contents-begin
2889 :contents-end contents-end
2890 :post-blank post-blank)))))
2892 (defun org-element-strike-through-interpreter (strike-through contents)
2893 "Interpret STRIKE-THROUGH object as Org syntax.
2894 CONTENTS is the contents of the object."
2895 (format "+%s+" contents))
2898 ;;;; Subscript
2900 (defun org-element-subscript-parser ()
2901 "Parse subscript at point.
2903 Return a list whose CAR is `subscript' and CDR a plist with
2904 `:begin', `:end', `:contents-begin', `:contents-end',
2905 `:use-brackets-p' and `:post-blank' as keywords.
2907 Assume point is at the underscore."
2908 (save-excursion
2909 (unless (bolp) (backward-char))
2910 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
2912 (not (looking-at org-match-substring-regexp))))
2913 (begin (match-beginning 2))
2914 (contents-begin (or (match-beginning 5)
2915 (match-beginning 3)))
2916 (contents-end (or (match-end 5) (match-end 3)))
2917 (post-blank (progn (goto-char (match-end 0))
2918 (skip-chars-forward " \t")))
2919 (end (point)))
2920 (list 'subscript
2921 (list :begin begin
2922 :end end
2923 :use-brackets-p bracketsp
2924 :contents-begin contents-begin
2925 :contents-end contents-end
2926 :post-blank post-blank)))))
2928 (defun org-element-subscript-interpreter (subscript contents)
2929 "Interpret SUBSCRIPT object as Org syntax.
2930 CONTENTS is the contents of the object."
2931 (format
2932 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
2933 contents))
2935 (defun org-element-sub/superscript-successor (limit)
2936 "Search for the next sub/superscript object.
2938 LIMIT bounds the search.
2940 Return value is a cons cell whose CAR is either `subscript' or
2941 `superscript' and CDR is beginning position."
2942 (save-excursion
2943 (when (re-search-forward org-match-substring-regexp limit t)
2944 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
2945 (match-beginning 2)))))
2948 ;;;; Superscript
2950 (defun org-element-superscript-parser ()
2951 "Parse superscript at point.
2953 Return a list whose CAR is `superscript' and CDR a plist with
2954 `:begin', `:end', `:contents-begin', `:contents-end',
2955 `:use-brackets-p' and `:post-blank' as keywords.
2957 Assume point is at the caret."
2958 (save-excursion
2959 (unless (bolp) (backward-char))
2960 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
2961 (not (looking-at org-match-substring-regexp))))
2962 (begin (match-beginning 2))
2963 (contents-begin (or (match-beginning 5)
2964 (match-beginning 3)))
2965 (contents-end (or (match-end 5) (match-end 3)))
2966 (post-blank (progn (goto-char (match-end 0))
2967 (skip-chars-forward " \t")))
2968 (end (point)))
2969 (list 'superscript
2970 (list :begin begin
2971 :end end
2972 :use-brackets-p bracketsp
2973 :contents-begin contents-begin
2974 :contents-end contents-end
2975 :post-blank post-blank)))))
2977 (defun org-element-superscript-interpreter (superscript contents)
2978 "Interpret SUPERSCRIPT object as Org syntax.
2979 CONTENTS is the contents of the object."
2980 (format
2981 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
2982 contents))
2985 ;;;; Table Cell
2987 (defun org-element-table-cell-parser ()
2988 "Parse table cell at point.
2990 Return a list whose CAR is `table-cell' and CDR is a plist
2991 containing `:begin', `:end', `:contents-begin', `:contents-end'
2992 and `:post-blank' keywords."
2993 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
2994 (let* ((begin (match-beginning 0))
2995 (end (match-end 0))
2996 (contents-begin (match-beginning 1))
2997 (contents-end (match-end 1)))
2998 (list 'table-cell
2999 (list :begin begin
3000 :end end
3001 :contents-begin contents-begin
3002 :contents-end contents-end
3003 :post-blank 0))))
3005 (defun org-element-table-cell-interpreter (table-cell contents)
3006 "Interpret TABLE-CELL element as Org syntax.
3007 CONTENTS is the contents of the cell, or nil."
3008 (concat " " contents " |"))
3010 (defun org-element-table-cell-successor (limit)
3011 "Search for the next table-cell object.
3013 LIMIT bounds the search.
3015 Return value is a cons cell whose CAR is `table-cell' and CDR is
3016 beginning position."
3017 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
3020 ;;;; Target
3022 (defun org-element-target-parser ()
3023 "Parse target at point.
3025 Return a list whose CAR is `target' and CDR a plist with
3026 `:begin', `:end', `:value' and `:post-blank' as keywords.
3028 Assume point is at the target."
3029 (save-excursion
3030 (looking-at org-target-regexp)
3031 (let ((begin (point))
3032 (value (org-match-string-no-properties 1))
3033 (post-blank (progn (goto-char (match-end 0))
3034 (skip-chars-forward " \t")))
3035 (end (point)))
3036 (list 'target
3037 (list :begin begin
3038 :end end
3039 :value value
3040 :post-blank post-blank)))))
3042 (defun org-element-target-interpreter (target contents)
3043 "Interpret TARGET object as Org syntax.
3044 CONTENTS is nil."
3045 (format "<<%s>>" (org-element-property :value target)))
3047 (defun org-element-target-successor (limit)
3048 "Search for the next target object.
3050 LIMIT bounds the search.
3052 Return value is a cons cell whose CAR is `target' and CDR is
3053 beginning position."
3054 (save-excursion
3055 (when (re-search-forward org-target-regexp limit t)
3056 (cons 'target (match-beginning 0)))))
3059 ;;;; Timestamp
3061 (defun org-element-timestamp-parser ()
3062 "Parse time stamp at point.
3064 Return a list whose CAR is `timestamp', and CDR a plist with
3065 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
3067 Assume point is at the beginning of the timestamp."
3068 (save-excursion
3069 (let* ((begin (point))
3070 (activep (eq (char-after) ?<))
3071 (main-value
3072 (progn
3073 (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
3074 (match-string-no-properties 1)))
3075 (range-end (match-string-no-properties 3))
3076 (type (cond ((match-string 2) 'diary)
3077 ((and activep range-end) 'active-range)
3078 (activep 'active)
3079 (range-end 'inactive-range)
3080 (t 'inactive)))
3081 (post-blank (progn (goto-char (match-end 0))
3082 (skip-chars-forward " \t")))
3083 (end (point)))
3084 (list 'timestamp
3085 (list :type type
3086 :value main-value
3087 :range-end range-end
3088 :begin begin
3089 :end end
3090 :post-blank post-blank)))))
3092 (defun org-element-timestamp-interpreter (timestamp contents)
3093 "Interpret TIMESTAMP object as Org syntax.
3094 CONTENTS is nil."
3095 (let ((type (org-element-property :type timestamp) ))
3096 (concat
3097 (format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>")
3098 (org-element-property :value timestamp))
3099 (let ((range-end (org-element-property :range-end timestamp)))
3100 (when range-end
3101 (concat "--"
3102 (format (if (eq type 'inactive-range) "[%s]" "<%s>")
3103 range-end)))))))
3105 (defun org-element-timestamp-successor (limit)
3106 "Search for the next timestamp object.
3108 LIMIT bounds the search.
3110 Return value is a cons cell whose CAR is `timestamp' and CDR is
3111 beginning position."
3112 (save-excursion
3113 (when (re-search-forward
3114 (concat org-ts-regexp-both
3115 "\\|"
3116 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3117 "\\|"
3118 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
3119 limit t)
3120 (cons 'timestamp (match-beginning 0)))))
3123 ;;;; Underline
3125 (defun org-element-underline-parser ()
3126 "Parse underline object at point.
3128 Return a list whose CAR is `underline' and CDR is a plist with
3129 `:begin', `:end', `:contents-begin' and `:contents-end' and
3130 `:post-blank' keywords.
3132 Assume point is at the first underscore marker."
3133 (save-excursion
3134 (unless (bolp) (backward-char 1))
3135 (looking-at org-emph-re)
3136 (let ((begin (match-beginning 2))
3137 (contents-begin (match-beginning 4))
3138 (contents-end (match-end 4))
3139 (post-blank (progn (goto-char (match-end 2))
3140 (skip-chars-forward " \t")))
3141 (end (point)))
3142 (list 'underline
3143 (list :begin begin
3144 :end end
3145 :contents-begin contents-begin
3146 :contents-end contents-end
3147 :post-blank post-blank)))))
3149 (defun org-element-underline-interpreter (underline contents)
3150 "Interpret UNDERLINE object as Org syntax.
3151 CONTENTS is the contents of the object."
3152 (format "_%s_" contents))
3155 ;;;; Verbatim
3157 (defun org-element-verbatim-parser ()
3158 "Parse verbatim object at point.
3160 Return a list whose CAR is `verbatim' and CDR is a plist with
3161 `:value', `:begin', `:end' and `:post-blank' keywords.
3163 Assume point is at the first equal sign marker."
3164 (save-excursion
3165 (unless (bolp) (backward-char 1))
3166 (looking-at org-emph-re)
3167 (let ((begin (match-beginning 2))
3168 (value (org-match-string-no-properties 4))
3169 (post-blank (progn (goto-char (match-end 2))
3170 (skip-chars-forward " \t")))
3171 (end (point)))
3172 (list 'verbatim
3173 (list :value value
3174 :begin begin
3175 :end end
3176 :post-blank post-blank)))))
3178 (defun org-element-verbatim-interpreter (verbatim contents)
3179 "Interpret VERBATIM object as Org syntax.
3180 CONTENTS is nil."
3181 (format "=%s=" (org-element-property :value verbatim)))
3185 ;;; Parsing Element Starting At Point
3187 ;; `org-element--current-element' is the core function of this section.
3188 ;; It returns the Lisp representation of the element starting at
3189 ;; point.
3191 ;; `org-element--current-element' makes use of special modes. They
3192 ;; are activated for fixed element chaining (i.e. `plain-list' >
3193 ;; `item') or fixed conditional element chaining (i.e. `headline' >
3194 ;; `section'). Special modes are: `first-section', `section',
3195 ;; `quote-section', `item' and `table-row'.
3197 (defun org-element--current-element
3198 (limit &optional granularity special structure)
3199 "Parse the element starting at point.
3201 LIMIT bounds the search.
3203 Return value is a list like (TYPE PROPS) where TYPE is the type
3204 of the element and PROPS a plist of properties associated to the
3205 element.
3207 Possible types are defined in `org-element-all-elements'.
3209 Optional argument GRANULARITY determines the depth of the
3210 recursion. Allowed values are `headline', `greater-element',
3211 `element', `object' or nil. When it is broader than `object' (or
3212 nil), secondary values will not be parsed, since they only
3213 contain objects.
3215 Optional argument SPECIAL, when non-nil, can be either
3216 `first-section', `section', `quote-section', `table-row' and
3217 `item'.
3219 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3220 be computed.
3222 This function assumes point is always at the beginning of the
3223 element it has to parse."
3224 (save-excursion
3225 ;; If point is at an affiliated keyword, try moving to the
3226 ;; beginning of the associated element. If none is found, the
3227 ;; keyword is orphaned and will be treated as plain text.
3228 (when (looking-at org-element--affiliated-re)
3229 (let ((opoint (point)))
3230 (while (looking-at org-element--affiliated-re) (forward-line))
3231 (when (looking-at "[ \t]*$") (goto-char opoint))))
3232 (let ((case-fold-search t)
3233 ;; Determine if parsing depth allows for secondary strings
3234 ;; parsing. It only applies to elements referenced in
3235 ;; `org-element-secondary-value-alist'.
3236 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3237 (cond
3238 ;; Item.
3239 ((eq special 'item)
3240 (org-element-item-parser limit structure raw-secondary-p))
3241 ;; Quote Section.
3242 ((eq special 'quote-section) (org-element-quote-section-parser limit))
3243 ;; Table Row.
3244 ((eq special 'table-row) (org-element-table-row-parser limit))
3245 ;; Headline.
3246 ((org-with-limited-levels (org-at-heading-p))
3247 (org-element-headline-parser limit raw-secondary-p))
3248 ;; Section (must be checked after headline).
3249 ((eq special 'section) (org-element-section-parser limit))
3250 ((eq special 'first-section)
3251 (org-element-section-parser
3252 (or (save-excursion (org-with-limited-levels (outline-next-heading)))
3253 limit)))
3254 ;; Planning and Clock.
3255 ((and (looking-at org-planning-or-clock-line-re))
3256 (if (equal (match-string 1) org-clock-string)
3257 (org-element-clock-parser limit)
3258 (org-element-planning-parser limit)))
3259 ;; Inlinetask.
3260 ((org-at-heading-p)
3261 (org-element-inlinetask-parser limit raw-secondary-p))
3262 ;; LaTeX Environment.
3263 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
3264 (if (save-excursion
3265 (re-search-forward
3266 (format "[ \t]*\\\\end{%s}[ \t]*"
3267 (regexp-quote (match-string 1)))
3268 nil t))
3269 (org-element-latex-environment-parser limit)
3270 (org-element-paragraph-parser limit)))
3271 ;; Drawer and Property Drawer.
3272 ((looking-at org-drawer-regexp)
3273 (let ((name (match-string 1)))
3274 (cond
3275 ((not (save-excursion
3276 (re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
3277 (org-element-paragraph-parser limit))
3278 ((equal "PROPERTIES" name)
3279 (org-element-property-drawer-parser limit))
3280 (t (org-element-drawer-parser limit)))))
3281 ;; Fixed Width
3282 ((looking-at "[ \t]*:\\( \\|$\\)")
3283 (org-element-fixed-width-parser limit))
3284 ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
3285 ;; Keywords.
3286 ((looking-at "[ \t]*#\\+")
3287 (goto-char (match-end 0))
3288 (cond ((looking-at "$\\| ")
3289 (beginning-of-line)
3290 (org-element-comment-parser limit))
3291 ((looking-at "BEGIN_\\(\\S-+\\)")
3292 (beginning-of-line)
3293 (let ((parser (assoc (upcase (match-string 1))
3294 org-element-block-name-alist)))
3295 (if parser (funcall (cdr parser) limit)
3296 (org-element-special-block-parser limit))))
3297 ((looking-at "CALL")
3298 (beginning-of-line)
3299 (org-element-babel-call-parser limit))
3300 ((looking-at "BEGIN:? ")
3301 (beginning-of-line)
3302 (org-element-dynamic-block-parser limit))
3303 ((looking-at "\\S-+:")
3304 (beginning-of-line)
3305 (org-element-keyword-parser limit))
3306 ;; Ill-formed syntax is considered as a comment.
3308 (beginning-of-line)
3309 (org-element-comment-parser limit))))
3310 ;; Comments.
3311 ((eq (char-after) ?#) (org-element-comment-parser limit))
3312 ;; Footnote Definition.
3313 ((looking-at org-footnote-definition-re)
3314 (org-element-footnote-definition-parser limit))
3315 ;; Horizontal Rule.
3316 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3317 (org-element-horizontal-rule-parser limit))
3318 ;; Table.
3319 ((org-at-table-p t) (org-element-table-parser limit))
3320 ;; List.
3321 ((looking-at (org-item-re))
3322 (org-element-plain-list-parser limit (or structure (org-list-struct))))
3323 ;; Default element: Paragraph.
3324 (t (org-element-paragraph-parser limit))))))
3327 ;; Most elements can have affiliated keywords. When looking for an
3328 ;; element beginning, we want to move before them, as they belong to
3329 ;; that element, and, in the meantime, collect information they give
3330 ;; into appropriate properties. Hence the following function.
3332 ;; Usage of optional arguments may not be obvious at first glance:
3334 ;; - TRANS-LIST is used to polish keywords names that have evolved
3335 ;; during Org history. In example, even though =result= and
3336 ;; =results= coexist, we want to have them under the same =result=
3337 ;; property. It's also true for "srcname" and "name", where the
3338 ;; latter seems to be preferred nowadays (thus the "name" property).
3340 ;; - CONSED allows to regroup multi-lines keywords under the same
3341 ;; property, while preserving their own identity. This is mostly
3342 ;; used for "attr_latex" and al.
3344 ;; - PARSED prepares a keyword value for export. This is useful for
3345 ;; "caption". Objects restrictions for such keywords are defined in
3346 ;; `org-element-object-restrictions'.
3348 ;; - DUALS is used to take care of keywords accepting a main and an
3349 ;; optional secondary values. For example "results" has its
3350 ;; source's name as the main value, and may have an hash string in
3351 ;; optional square brackets as the secondary one.
3353 ;; A keyword may belong to more than one category.
3355 (defun org-element--collect-affiliated-keywords
3356 (&optional key-re trans-list consed parsed duals)
3357 "Collect affiliated keywords before point.
3359 Optional argument KEY-RE is a regexp matching keywords, which
3360 puts matched keyword in group 1. It defaults to
3361 `org-element--affiliated-re'.
3363 TRANS-LIST is an alist where key is the keyword and value the
3364 property name it should be translated to, without the colons. It
3365 defaults to `org-element-keyword-translation-alist'.
3367 CONSED is a list of strings. Any keyword belonging to that list
3368 will have its value consed. The check is done after keyword
3369 translation. It defaults to `org-element-multiple-keywords'.
3371 PARSED is a list of strings. Any keyword member of this list
3372 will have its value parsed. The check is done after keyword
3373 translation. If a keyword is a member of both CONSED and PARSED,
3374 it's value will be a list of parsed strings. It defaults to
3375 `org-element-parsed-keywords'.
3377 DUALS is a list of strings. Any keyword member of this list can
3378 have two parts: one mandatory and one optional. Its value is
3379 a cons cell whose CAR is the former, and the CDR the latter. If
3380 a keyword is a member of both PARSED and DUALS, both values will
3381 be parsed. It defaults to `org-element-dual-keywords'.
3383 Return a list whose CAR is the position at the first of them and
3384 CDR a plist of keywords and values."
3385 (save-excursion
3386 (let ((case-fold-search t)
3387 (key-re (or key-re org-element--affiliated-re))
3388 (trans-list (or trans-list org-element-keyword-translation-alist))
3389 (consed (or consed org-element-multiple-keywords))
3390 (parsed (or parsed org-element-parsed-keywords))
3391 (duals (or duals org-element-dual-keywords))
3392 ;; RESTRICT is the list of objects allowed in parsed
3393 ;; keywords value.
3394 (restrict (org-element-restriction 'keyword))
3395 output)
3396 (unless (bobp)
3397 (while (and (not (bobp)) (progn (forward-line -1) (looking-at key-re)))
3398 (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
3399 ;; Apply translation to RAW-KWD. From there, KWD is
3400 ;; the official keyword.
3401 (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
3402 ;; Find main value for any keyword.
3403 (value
3404 (save-match-data
3405 (org-trim
3406 (buffer-substring-no-properties
3407 (match-end 0) (point-at-eol)))))
3408 ;; If KWD is a dual keyword, find its secondary
3409 ;; value. Maybe parse it.
3410 (dual-value
3411 (and (member kwd duals)
3412 (let ((sec (org-match-string-no-properties 3)))
3413 (if (or (not sec) (not (member kwd parsed))) sec
3414 (org-element-parse-secondary-string sec restrict)))))
3415 ;; Attribute a property name to KWD.
3416 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3417 ;; Now set final shape for VALUE.
3418 (when (member kwd parsed)
3419 (setq value (org-element-parse-secondary-string value restrict)))
3420 (when (member kwd duals)
3421 ;; VALUE is mandatory. Set it to nil if there is none.
3422 (setq value (and value (cons value dual-value))))
3423 ;; Attributes are always consed.
3424 (when (or (member kwd consed) (string-match "^ATTR_" kwd))
3425 (setq value (cons value (plist-get output kwd-sym))))
3426 ;; Eventually store the new value in OUTPUT.
3427 (setq output (plist-put output kwd-sym value))))
3428 (unless (looking-at key-re) (forward-line 1)))
3429 (list (point) output))))
3433 ;;; The Org Parser
3435 ;; The two major functions here are `org-element-parse-buffer', which
3436 ;; parses Org syntax inside the current buffer, taking into account
3437 ;; region, narrowing, or even visibility if specified, and
3438 ;; `org-element-parse-secondary-string', which parses objects within
3439 ;; a given string.
3441 ;; The (almost) almighty `org-element-map' allows to apply a function
3442 ;; on elements or objects matching some type, and accumulate the
3443 ;; resulting values. In an export situation, it also skips unneeded
3444 ;; parts of the parse tree.
3446 (defun org-element-parse-buffer (&optional granularity visible-only)
3447 "Recursively parse the buffer and return structure.
3448 If narrowing is in effect, only parse the visible part of the
3449 buffer.
3451 Optional argument GRANULARITY determines the depth of the
3452 recursion. It can be set to the following symbols:
3454 `headline' Only parse headlines.
3455 `greater-element' Don't recurse into greater elements excepted
3456 headlines and sections. Thus, elements
3457 parsed are the top-level ones.
3458 `element' Parse everything but objects and plain text.
3459 `object' Parse the complete buffer (default).
3461 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3462 elements.
3464 Assume buffer is in Org mode."
3465 (save-excursion
3466 (goto-char (point-min))
3467 (org-skip-whitespace)
3468 (org-element--parse-elements
3469 (point-at-bol) (point-max)
3470 ;; Start in `first-section' mode so text before the first
3471 ;; headline belongs to a section.
3472 'first-section nil granularity visible-only (list 'org-data nil))))
3474 (defun org-element-parse-secondary-string (string restriction &optional parent)
3475 "Recursively parse objects in STRING and return structure.
3477 RESTRICTION is a symbol limiting the object types that will be
3478 looked after.
3480 Optional argument PARENT, when non-nil, is the element or object
3481 containing the secondary string. It is used to set correctly
3482 `:parent' property within the string."
3483 (with-temp-buffer
3484 (insert string)
3485 (let ((secondary (org-element--parse-objects
3486 (point-min) (point-max) nil restriction)))
3487 (mapc (lambda (obj) (org-element-put-property obj :parent parent))
3488 secondary))))
3490 (defun org-element-map (data types fun &optional info first-match no-recursion)
3491 "Map a function on selected elements or objects.
3493 DATA is the parsed tree, as returned by, i.e,
3494 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3495 of elements or objects types. FUN is the function called on the
3496 matching element or object. It must accept one arguments: the
3497 element or object itself.
3499 When optional argument INFO is non-nil, it should be a plist
3500 holding export options. In that case, parts of the parse tree
3501 not exportable according to that property list will be skipped.
3503 When optional argument FIRST-MATCH is non-nil, stop at the first
3504 match for which FUN doesn't return nil, and return that value.
3506 Optional argument NO-RECURSION is a symbol or a list of symbols
3507 representing elements or objects types. `org-element-map' won't
3508 enter any recursive element or object whose type belongs to that
3509 list. Though, FUN can still be applied on them.
3511 Nil values returned from FUN do not appear in the results."
3512 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3513 (unless (listp types) (setq types (list types)))
3514 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
3515 ;; Recursion depth is determined by --CATEGORY.
3516 (let* ((--category
3517 (catch 'found
3518 (let ((category 'greater-elements))
3519 (mapc (lambda (type)
3520 (cond ((or (memq type org-element-all-objects)
3521 (eq type 'plain-text))
3522 ;; If one object is found, the function
3523 ;; has to recurse into every object.
3524 (throw 'found 'objects))
3525 ((not (memq type org-element-greater-elements))
3526 ;; If one regular element is found, the
3527 ;; function has to recurse, at lest, into
3528 ;; every element it encounters.
3529 (and (not (eq category 'elements))
3530 (setq category 'elements)))))
3531 types)
3532 category)))
3533 --acc
3534 --walk-tree
3535 (--walk-tree
3536 (function
3537 (lambda (--data)
3538 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3539 ;; holding contextual information.
3540 (let ((--type (org-element-type --data)))
3541 (cond
3542 ((not --data))
3543 ;; Ignored element in an export context.
3544 ((and info (memq --data (plist-get info :ignore-list))))
3545 ;; Secondary string: only objects can be found there.
3546 ((not --type)
3547 (when (eq --category 'objects) (mapc --walk-tree --data)))
3548 ;; Unconditionally enter parse trees.
3549 ((eq --type 'org-data)
3550 (mapc --walk-tree (org-element-contents --data)))
3552 ;; Check if TYPE is matching among TYPES. If so,
3553 ;; apply FUN to --DATA and accumulate return value
3554 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3555 (when (memq --type types)
3556 (let ((result (funcall fun --data)))
3557 (cond ((not result))
3558 (first-match (throw '--map-first-match result))
3559 (t (push result --acc)))))
3560 ;; If --DATA has a secondary string that can contain
3561 ;; objects with their type among TYPES, look into it.
3562 (when (eq --category 'objects)
3563 (let ((sec-prop
3564 (assq --type org-element-secondary-value-alist)))
3565 (when sec-prop
3566 (funcall --walk-tree
3567 (org-element-property (cdr sec-prop) --data)))))
3568 ;; Determine if a recursion into --DATA is possible.
3569 (cond
3570 ;; --TYPE is explicitly removed from recursion.
3571 ((memq --type no-recursion))
3572 ;; --DATA has no contents.
3573 ((not (org-element-contents --data)))
3574 ;; Looking for greater elements but --DATA is simply
3575 ;; an element or an object.
3576 ((and (eq --category 'greater-elements)
3577 (not (memq --type org-element-greater-elements))))
3578 ;; Looking for elements but --DATA is an object.
3579 ((and (eq --category 'elements)
3580 (memq --type org-element-all-objects)))
3581 ;; In any other case, map contents.
3582 (t (mapc --walk-tree (org-element-contents --data)))))))))))
3583 (catch '--map-first-match
3584 (funcall --walk-tree data)
3585 ;; Return value in a proper order.
3586 (nreverse --acc))))
3588 ;; The following functions are internal parts of the parser.
3590 ;; The first one, `org-element--parse-elements' acts at the element's
3591 ;; level.
3593 ;; The second one, `org-element--parse-objects' applies on all objects
3594 ;; of a paragraph or a secondary string. It uses
3595 ;; `org-element--get-next-object-candidates' to optimize the search of
3596 ;; the next object in the buffer.
3598 ;; More precisely, that function looks for every allowed object type
3599 ;; first. Then, it discards failed searches, keeps further matches,
3600 ;; and searches again types matched behind point, for subsequent
3601 ;; calls. Thus, searching for a given type fails only once, and every
3602 ;; object is searched only once at top level (but sometimes more for
3603 ;; nested types).
3605 (defun org-element--parse-elements
3606 (beg end special structure granularity visible-only acc)
3607 "Parse elements between BEG and END positions.
3609 SPECIAL prioritize some elements over the others. It can be set
3610 to `first-section', `quote-section', `section' `item' or
3611 `table-row'.
3613 When value is `item', STRUCTURE will be used as the current list
3614 structure.
3616 GRANULARITY determines the depth of the recursion. See
3617 `org-element-parse-buffer' for more information.
3619 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3620 elements.
3622 Elements are accumulated into ACC."
3623 (save-excursion
3624 (goto-char beg)
3625 ;; When parsing only headlines, skip any text before first one.
3626 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
3627 (org-with-limited-levels (outline-next-heading)))
3628 ;; Main loop start.
3629 (while (< (point) end)
3630 ;; Find current element's type and parse it accordingly to
3631 ;; its category.
3632 (let* ((element (org-element--current-element
3633 end granularity special structure))
3634 (type (org-element-type element))
3635 (cbeg (org-element-property :contents-begin element)))
3636 (goto-char (org-element-property :end element))
3637 ;; Fill ELEMENT contents by side-effect.
3638 (cond
3639 ;; If VISIBLE-ONLY is true and element is hidden or if it has
3640 ;; no contents, don't modify it.
3641 ((or (and visible-only (org-element-property :hiddenp element))
3642 (not cbeg)))
3643 ;; Greater element: parse it between `contents-begin' and
3644 ;; `contents-end'. Make sure GRANULARITY allows the
3645 ;; recursion, or ELEMENT is an headline, in which case going
3646 ;; inside is mandatory, in order to get sub-level headings.
3647 ((and (memq type org-element-greater-elements)
3648 (or (memq granularity '(element object nil))
3649 (and (eq granularity 'greater-element)
3650 (eq type 'section))
3651 (eq type 'headline)))
3652 (org-element--parse-elements
3653 cbeg (org-element-property :contents-end element)
3654 ;; Possibly switch to a special mode.
3655 (case type
3656 (headline
3657 (if (org-element-property :quotedp element) 'quote-section
3658 'section))
3659 (plain-list 'item)
3660 (table 'table-row))
3661 (org-element-property :structure element)
3662 granularity visible-only element))
3663 ;; ELEMENT has contents. Parse objects inside, if
3664 ;; GRANULARITY allows it.
3665 ((memq granularity '(object nil))
3666 (org-element--parse-objects
3667 cbeg (org-element-property :contents-end element) element
3668 (org-element-restriction type))))
3669 (org-element-adopt-element acc element t)))
3670 ;; Return result.
3671 acc))
3673 (defun org-element--parse-objects (beg end acc restriction)
3674 "Parse objects between BEG and END and return recursive structure.
3676 Objects are accumulated in ACC.
3678 RESTRICTION is a list of object types which are allowed in the
3679 current object."
3680 (let (candidates)
3681 (save-excursion
3682 (goto-char beg)
3683 (while (and (< (point) end)
3684 (setq candidates (org-element--get-next-object-candidates
3685 end restriction candidates)))
3686 (let ((next-object
3687 (let ((pos (apply 'min (mapcar 'cdr candidates))))
3688 (save-excursion
3689 (goto-char pos)
3690 (funcall (intern (format "org-element-%s-parser"
3691 (car (rassq pos candidates)))))))))
3692 ;; 1. Text before any object. Untabify it.
3693 (let ((obj-beg (org-element-property :begin next-object)))
3694 (unless (= (point) obj-beg)
3695 (setq acc
3696 (org-element-adopt-element
3698 (replace-regexp-in-string
3699 "\t" (make-string tab-width ? )
3700 (buffer-substring-no-properties (point) obj-beg)) t))))
3701 ;; 2. Object...
3702 (let ((obj-end (org-element-property :end next-object))
3703 (cont-beg (org-element-property :contents-begin next-object)))
3704 ;; Fill contents of NEXT-OBJECT by side-effect, if it has
3705 ;; a recursive type.
3706 (when (and cont-beg
3707 (memq (car next-object) org-element-recursive-objects))
3708 (save-restriction
3709 (narrow-to-region
3710 cont-beg
3711 (org-element-property :contents-end next-object))
3712 (org-element--parse-objects
3713 (point-min) (point-max) next-object
3714 (org-element-restriction next-object))))
3715 (setq acc (org-element-adopt-element acc next-object t))
3716 (goto-char obj-end))))
3717 ;; 3. Text after last object. Untabify it.
3718 (unless (= (point) end)
3719 (setq acc
3720 (org-element-adopt-element
3722 (replace-regexp-in-string
3723 "\t" (make-string tab-width ? )
3724 (buffer-substring-no-properties (point) end)) t)))
3725 ;; Result.
3726 acc)))
3728 (defun org-element--get-next-object-candidates (limit restriction objects)
3729 "Return an alist of candidates for the next object.
3731 LIMIT bounds the search, and RESTRICTION narrows candidates to
3732 some object types.
3734 Return value is an alist whose CAR is position and CDR the object
3735 type, as a symbol.
3737 OBJECTS is the previous candidates alist."
3738 (let (next-candidates types-to-search)
3739 ;; If no previous result, search every object type in RESTRICTION.
3740 ;; Otherwise, keep potential candidates (old objects located after
3741 ;; point) and ask to search again those which had matched before.
3742 (if (not objects) (setq types-to-search restriction)
3743 (mapc (lambda (obj)
3744 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
3745 (push obj next-candidates)))
3746 objects))
3747 ;; Call the appropriate successor function for each type to search
3748 ;; and accumulate matches.
3749 (mapc
3750 (lambda (type)
3751 (let* ((successor-fun
3752 (intern
3753 (format "org-element-%s-successor"
3754 (or (cdr (assq type org-element-object-successor-alist))
3755 type))))
3756 (obj (funcall successor-fun limit)))
3757 (and obj (push obj next-candidates))))
3758 types-to-search)
3759 ;; Return alist.
3760 next-candidates))
3764 ;;; Towards A Bijective Process
3766 ;; The parse tree obtained with `org-element-parse-buffer' is really
3767 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3768 ;; interpreted and expanded into a string with canonical Org syntax.
3769 ;; Hence `org-element-interpret-data'.
3771 ;; The function relies internally on
3772 ;; `org-element--interpret-affiliated-keywords'.
3774 (defun org-element-interpret-data (data &optional parent)
3775 "Interpret DATA as Org syntax.
3777 DATA is a parse tree, an element, an object or a secondary string
3778 to interpret.
3780 Optional argument PARENT is used for recursive calls. It contains
3781 the element or object containing data, or nil.
3783 Return Org syntax as a string."
3784 (let* ((type (org-element-type data))
3785 (results
3786 (cond
3787 ;; Secondary string.
3788 ((not type)
3789 (mapconcat
3790 (lambda (obj) (org-element-interpret-data obj parent))
3791 data ""))
3792 ;; Full Org document.
3793 ((eq type 'org-data)
3794 (mapconcat
3795 (lambda (obj) (org-element-interpret-data obj parent))
3796 (org-element-contents data) ""))
3797 ;; Plain text.
3798 ((stringp data) data)
3799 ;; Element/Object without contents.
3800 ((not (org-element-contents data))
3801 (funcall (intern (format "org-element-%s-interpreter" type))
3802 data nil))
3803 ;; Element/Object with contents.
3805 (let* ((greaterp (memq type org-element-greater-elements))
3806 (objectp (and (not greaterp)
3807 (memq type org-element-recursive-objects)))
3808 (contents
3809 (mapconcat
3810 (lambda (obj) (org-element-interpret-data obj data))
3811 (org-element-contents
3812 (if (or greaterp objectp) data
3813 ;; Elements directly containing objects must
3814 ;; have their indentation normalized first.
3815 (org-element-normalize-contents
3816 data
3817 ;; When normalizing first paragraph of an
3818 ;; item or a footnote-definition, ignore
3819 ;; first line's indentation.
3820 (and (eq type 'paragraph)
3821 (equal data (car (org-element-contents parent)))
3822 (memq (org-element-type parent)
3823 '(footnote-definiton item))))))
3824 "")))
3825 (funcall (intern (format "org-element-%s-interpreter" type))
3826 data
3827 (if greaterp (org-element-normalize-contents contents)
3828 contents)))))))
3829 (if (memq type '(org-data plain-text nil)) results
3830 ;; Build white spaces. If no `:post-blank' property is
3831 ;; specified, assume its value is 0.
3832 (let ((post-blank (or (org-element-property :post-blank data) 0)))
3833 (if (memq type org-element-all-objects)
3834 (concat results (make-string post-blank 32))
3835 (concat
3836 (org-element--interpret-affiliated-keywords data)
3837 (org-element-normalize-string results)
3838 (make-string post-blank 10)))))))
3840 (defun org-element--interpret-affiliated-keywords (element)
3841 "Return ELEMENT's affiliated keywords as Org syntax.
3842 If there is no affiliated keyword, return the empty string."
3843 (let ((keyword-to-org
3844 (function
3845 (lambda (key value)
3846 (let (dual)
3847 (when (member key org-element-dual-keywords)
3848 (setq dual (cdr value) value (car value)))
3849 (concat "#+" key
3850 (and dual
3851 (format "[%s]" (org-element-interpret-data dual)))
3852 ": "
3853 (if (member key org-element-parsed-keywords)
3854 (org-element-interpret-data value)
3855 value)
3856 "\n"))))))
3857 (mapconcat
3858 (lambda (prop)
3859 (let ((value (org-element-property prop element))
3860 (keyword (upcase (substring (symbol-name prop) 1))))
3861 (when value
3862 (if (or (member keyword org-element-multiple-keywords)
3863 ;; All attribute keywords can have multiple lines.
3864 (string-match "^ATTR_" keyword))
3865 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
3866 value
3868 (funcall keyword-to-org keyword value)))))
3869 ;; List all ELEMENT's properties matching an attribute line or an
3870 ;; affiliated keyword, but ignore translated keywords since they
3871 ;; cannot belong to the property list.
3872 (loop for prop in (nth 1 element) by 'cddr
3873 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
3874 (or (string-match "^ATTR_" keyword)
3875 (and
3876 (member keyword org-element-affiliated-keywords)
3877 (not (assoc keyword
3878 org-element-keyword-translation-alist)))))
3879 collect prop)
3880 "")))
3882 ;; Because interpretation of the parse tree must return the same
3883 ;; number of blank lines between elements and the same number of white
3884 ;; space after objects, some special care must be given to white
3885 ;; spaces.
3887 ;; The first function, `org-element-normalize-string', ensures any
3888 ;; string different from the empty string will end with a single
3889 ;; newline character.
3891 ;; The second function, `org-element-normalize-contents', removes
3892 ;; global indentation from the contents of the current element.
3894 (defun org-element-normalize-string (s)
3895 "Ensure string S ends with a single newline character.
3897 If S isn't a string return it unchanged. If S is the empty
3898 string, return it. Otherwise, return a new string with a single
3899 newline character at its end."
3900 (cond
3901 ((not (stringp s)) s)
3902 ((string= "" s) "")
3903 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
3904 (replace-match "\n" nil nil s)))))
3906 (defun org-element-normalize-contents (element &optional ignore-first)
3907 "Normalize plain text in ELEMENT's contents.
3909 ELEMENT must only contain plain text and objects.
3911 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3912 indentation to compute maximal common indentation.
3914 Return the normalized element that is element with global
3915 indentation removed from its contents. The function assumes that
3916 indentation is not done with TAB characters."
3917 (let* (ind-list ; for byte-compiler
3918 collect-inds ; for byte-compiler
3919 (collect-inds
3920 (function
3921 ;; Return list of indentations within BLOB. This is done by
3922 ;; walking recursively BLOB and updating IND-LIST along the
3923 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3924 ;; been seen yet. It is required as this string is the only
3925 ;; one whose indentation doesn't happen after a newline
3926 ;; character.
3927 (lambda (blob first-flag)
3928 (mapc
3929 (lambda (object)
3930 (when (and first-flag (stringp object))
3931 (setq first-flag nil)
3932 (string-match "\\`\\( *\\)" object)
3933 (let ((len (length (match-string 1 object))))
3934 ;; An indentation of zero means no string will be
3935 ;; modified. Quit the process.
3936 (if (zerop len) (throw 'zero (setq ind-list nil))
3937 (push len ind-list))))
3938 (cond
3939 ((stringp object)
3940 (let ((start 0))
3941 ;; Avoid matching blank or empty lines.
3942 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
3943 (not (equal (match-string 2 object) " ")))
3944 (setq start (match-end 0))
3945 (push (length (match-string 1 object)) ind-list))))
3946 ((memq (org-element-type object) org-element-recursive-objects)
3947 (funcall collect-inds object first-flag))))
3948 (org-element-contents blob))))))
3949 ;; Collect indentation list in ELEMENT. Possibly remove first
3950 ;; value if IGNORE-FIRST is non-nil.
3951 (catch 'zero (funcall collect-inds element (not ignore-first)))
3952 (if (not ind-list) element
3953 ;; Build ELEMENT back, replacing each string with the same
3954 ;; string minus common indentation.
3955 (let* (build ; For byte compiler.
3956 (build
3957 (function
3958 (lambda (blob mci first-flag)
3959 ;; Return BLOB with all its strings indentation
3960 ;; shortened from MCI white spaces. FIRST-FLAG is
3961 ;; non-nil when the first string hasn't been seen
3962 ;; yet.
3963 (setcdr (cdr blob)
3964 (mapcar
3965 (lambda (object)
3966 (when (and first-flag (stringp object))
3967 (setq first-flag nil)
3968 (setq object
3969 (replace-regexp-in-string
3970 (format "\\` \\{%d\\}" mci) "" object)))
3971 (cond
3972 ((stringp object)
3973 (replace-regexp-in-string
3974 (format "\n \\{%d\\}" mci) "\n" object))
3975 ((memq (org-element-type object)
3976 org-element-recursive-objects)
3977 (funcall build object mci first-flag))
3978 (t object)))
3979 (org-element-contents blob)))
3980 blob))))
3981 (funcall build element (apply 'min ind-list) (not ignore-first))))))
3985 ;;; The Toolbox
3987 ;; The first move is to implement a way to obtain the smallest element
3988 ;; containing point. This is the job of `org-element-at-point'. It
3989 ;; basically jumps back to the beginning of section containing point
3990 ;; and moves, element after element, with
3991 ;; `org-element--current-element' until the container is found.
3993 ;; At a deeper level, `org-element-context' lists all elements and
3994 ;; objects containing point.
3996 ;; Note: When using `org-element-at-point', secondary values are never
3997 ;; parsed since the function focuses on elements, not on objects.
3999 (defun org-element-at-point (&optional keep-trail)
4000 "Determine closest element around point.
4002 Return value is a list like (TYPE PROPS) where TYPE is the type
4003 of the element and PROPS a plist of properties associated to the
4004 element. Possible types are defined in
4005 `org-element-all-elements'.
4007 As a special case, if point is at the very beginning of a list or
4008 sub-list, returned element will be that list instead of the first
4009 item. In the same way, if point is at the beginning of the first
4010 row of a table, returned element will be the table instead of the
4011 first row.
4013 If optional argument KEEP-TRAIL is non-nil, the function returns
4014 a list of of elements leading to element at point. The list's
4015 CAR is always the element at point. Following positions contain
4016 element's siblings, then parents, siblings of parents, until the
4017 first element of current section."
4018 (org-with-wide-buffer
4019 ;; If at an headline, parse it. It is the sole element that
4020 ;; doesn't require to know about context. Be sure to disallow
4021 ;; secondary string parsing, though.
4022 (if (org-with-limited-levels (org-at-heading-p))
4023 (progn
4024 (beginning-of-line)
4025 (if (not keep-trail) (org-element-headline-parser (point-max) t)
4026 (list (org-element-headline-parser (point-max) t))))
4027 ;; Otherwise move at the beginning of the section containing
4028 ;; point.
4029 (let ((origin (point))
4030 (end (save-excursion (outline-next-heading) (point)))
4031 element type special-flag trail struct prevs)
4032 (org-with-limited-levels
4033 (if (org-before-first-heading-p) (goto-char (point-min))
4034 (org-back-to-heading)
4035 (forward-line)))
4036 (org-skip-whitespace)
4037 (beginning-of-line)
4038 ;; Parse successively each element, skipping those ending
4039 ;; before original position.
4040 (catch 'exit
4041 (while t
4042 (setq element
4043 (org-element--current-element end 'element special-flag struct)
4044 type (car element))
4045 (push element trail)
4046 (cond
4047 ;; 1. Skip any element ending before point or at point.
4048 ((let ((end (org-element-property :end element)))
4049 (when (<= end origin)
4050 (if (> (point-max) end) (goto-char end)
4051 (throw 'exit (if keep-trail trail element))))))
4052 ;; 2. An element containing point is always the element at
4053 ;; point.
4054 ((not (memq type org-element-greater-elements))
4055 (throw 'exit (if keep-trail trail element)))
4056 ;; 3. At any other greater element type, if point is
4057 ;; within contents, move into it. Otherwise, return
4058 ;; that element.
4060 (let ((cbeg (org-element-property :contents-begin element))
4061 (cend (org-element-property :contents-end element)))
4062 (if (or (not cbeg) (not cend) (> cbeg origin) (<= cend origin)
4063 (and (= cbeg origin) (memq type '(plain-list table))))
4064 (throw 'exit (if keep-trail trail element))
4065 (case type
4066 (plain-list
4067 (setq special-flag 'item
4068 struct (org-element-property :structure element)))
4069 (table (setq special-flag 'table-row))
4070 (otherwise (setq special-flag nil)))
4071 (setq end cend)
4072 (goto-char cbeg)))))))))))
4074 (defun org-element-context ()
4075 "Return list of all elements and objects around point.
4077 Return value is a list like (TYPE PROPS) where TYPE is the type
4078 of the element or object and PROPS a plist of properties
4079 associated to it. Possible types are defined in
4080 `org-element-all-elements' and `org-element-all-objects'.
4082 All elements and objects returned belong to the current section
4083 and are ordered from closest to farthest."
4084 (org-with-wide-buffer
4085 (let* ((origin (point))
4086 ;; Remove elements not containing point from trail.
4087 (elements (org-remove-if
4088 (lambda (el)
4089 (or (> (org-element-property :begin el) origin)
4090 (< (org-element-property :end el) origin)))
4091 (org-element-at-point 'keep-trail)))
4092 (element (car elements))
4093 (type (car element)) end)
4094 ;; Check if point is inside an element containing objects or at
4095 ;; a secondary string. In that case, move to beginning of the
4096 ;; element or secondary string and set END to the other side.
4097 (if (not (or (and (eq type 'item)
4098 (let ((tag (org-element-property :tag element)))
4099 (and tag
4100 (progn
4101 (beginning-of-line)
4102 (search-forward tag (point-at-eol))
4103 (goto-char (match-beginning 0))
4104 (and (>= origin (point))
4105 (<= origin
4106 ;; `1+' is required so some
4107 ;; successors can match
4108 ;; properly their object.
4109 (setq end (1+ (match-end 0)))))))))
4110 (and (memq type '(headline inlinetask))
4111 (progn (beginning-of-line)
4112 (skip-chars-forward "* ")
4113 (setq end (point-at-eol))))
4114 (and (memq (car element) '(paragraph table-cell verse-block))
4115 (let ((cbeg (org-element-property
4116 :contents-begin element))
4117 (cend (org-element-property
4118 :contents-end element)))
4119 (and (>= origin cbeg)
4120 (<= origin cend)
4121 (progn (goto-char cbeg) (setq end cend)))))))
4122 elements
4123 (let ((restriction (org-element-restriction element)) candidates)
4124 (catch 'exit
4125 (while (setq candidates (org-element--get-next-object-candidates
4126 end restriction candidates))
4127 (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
4128 candidates)))
4129 ;; If ORIGIN is before next object in element, there's
4130 ;; no point in looking further.
4131 (if (> (cdr closest-cand) origin) (throw 'exit elements)
4132 (let* ((object
4133 (progn (goto-char (cdr closest-cand))
4134 (funcall (intern (format "org-element-%s-parser"
4135 (car closest-cand))))))
4136 (cbeg (org-element-property :contents-begin object))
4137 (cend (org-element-property :contents-end object)))
4138 (cond
4139 ;; ORIGIN is after OBJECT, so skip it.
4140 ((< (org-element-property :end object) origin)
4141 (goto-char (org-element-property :end object)))
4142 ;; ORIGIN is within a non-recursive object or at an
4143 ;; object boundaries: Return that object.
4144 ((or (not cbeg) (> cbeg origin) (< cend origin))
4145 (throw 'exit (cons object elements)))
4146 ;; Otherwise, move within current object and restrict
4147 ;; search to the end of its contents.
4148 (t (goto-char cbeg)
4149 (setq end cend)
4150 (push object elements)))))))
4151 elements))))))
4154 ;; Once the local structure around point is well understood, it's easy
4155 ;; to implement some replacements for `forward-paragraph'
4156 ;; `backward-paragraph', namely `org-element-forward' and
4157 ;; `org-element-backward'.
4159 ;; Also, `org-transpose-elements' mimics the behaviour of
4160 ;; `transpose-words', at the element's level, whereas
4161 ;; `org-element-drag-forward', `org-element-drag-backward', and
4162 ;; `org-element-up' generalize, respectively, functions
4163 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
4165 ;; `org-element-unindent-buffer' will, as its name almost suggests,
4166 ;; smartly remove global indentation from buffer, making it possible
4167 ;; to use Org indent mode on a file created with hard indentation.
4169 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
4170 ;; internally by some of the previously cited tools.
4172 (defsubst org-element-nested-p (elem-A elem-B)
4173 "Non-nil when elements ELEM-A and ELEM-B are nested."
4174 (let ((beg-A (org-element-property :begin elem-A))
4175 (beg-B (org-element-property :begin elem-B))
4176 (end-A (org-element-property :end elem-A))
4177 (end-B (org-element-property :end elem-B)))
4178 (or (and (>= beg-A beg-B) (<= end-A end-B))
4179 (and (>= beg-B beg-A) (<= end-B end-A)))))
4181 (defun org-element-swap-A-B (elem-A elem-B)
4182 "Swap elements ELEM-A and ELEM-B.
4183 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4184 end of ELEM-A."
4185 (goto-char (org-element-property :begin elem-A))
4186 ;; There are two special cases when an element doesn't start at bol:
4187 ;; the first paragraph in an item or in a footnote definition.
4188 (let ((specialp (not (bolp))))
4189 ;; Only a paragraph without any affiliated keyword can be moved at
4190 ;; ELEM-A position in such a situation. Note that the case of
4191 ;; a footnote definition is impossible: it cannot contain two
4192 ;; paragraphs in a row because it cannot contain a blank line.
4193 (if (and specialp
4194 (or (not (eq (org-element-type elem-B) 'paragraph))
4195 (/= (org-element-property :begin elem-B)
4196 (org-element-property :contents-begin elem-B))))
4197 (error "Cannot swap elements"))
4198 ;; In a special situation, ELEM-A will have no indentation. We'll
4199 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4200 (let* ((ind-B (when specialp
4201 (goto-char (org-element-property :begin elem-B))
4202 (org-get-indentation)))
4203 (beg-A (org-element-property :begin elem-A))
4204 (end-A (save-excursion
4205 (goto-char (org-element-property :end elem-A))
4206 (skip-chars-backward " \r\t\n")
4207 (point-at-eol)))
4208 (beg-B (org-element-property :begin elem-B))
4209 (end-B (save-excursion
4210 (goto-char (org-element-property :end elem-B))
4211 (skip-chars-backward " \r\t\n")
4212 (point-at-eol)))
4213 ;; Store overlays responsible for visibility status. We
4214 ;; also need to store their boundaries as they will be
4215 ;; removed from buffer.
4216 (overlays
4217 (cons
4218 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4219 (overlays-in beg-A end-A))
4220 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
4221 (overlays-in beg-B end-B))))
4222 ;; Get contents.
4223 (body-A (buffer-substring beg-A end-A))
4224 (body-B (delete-and-extract-region beg-B end-B)))
4225 (goto-char beg-B)
4226 (when specialp
4227 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4228 (org-indent-to-column ind-B))
4229 (insert body-A)
4230 ;; Restore ex ELEM-A overlays.
4231 (mapc (lambda (ov)
4232 (move-overlay
4233 (car ov)
4234 (+ (nth 1 ov) (- beg-B beg-A))
4235 (+ (nth 2 ov) (- beg-B beg-A))))
4236 (car overlays))
4237 (goto-char beg-A)
4238 (delete-region beg-A end-A)
4239 (insert body-B)
4240 ;; Restore ex ELEM-B overlays.
4241 (mapc (lambda (ov)
4242 (move-overlay (car ov)
4243 (+ (nth 1 ov) (- beg-A beg-B))
4244 (+ (nth 2 ov) (- beg-A beg-B))))
4245 (cdr overlays))
4246 (goto-char (org-element-property :end elem-B)))))
4248 (defun org-element-forward ()
4249 "Move forward by one element.
4250 Move to the next element at the same level, when possible."
4251 (interactive)
4252 (if (org-with-limited-levels (org-at-heading-p))
4253 (let ((origin (point)))
4254 (org-forward-same-level 1)
4255 (unless (org-with-limited-levels (org-at-heading-p))
4256 (goto-char origin)
4257 (error "Cannot move further down")))
4258 (let* ((trail (org-element-at-point 'keep-trail))
4259 (elem (pop trail))
4260 (end (org-element-property :end elem))
4261 (parent (loop for prev in trail
4262 when (>= (org-element-property :end prev) end)
4263 return prev)))
4264 (cond
4265 ((eobp) (error "Cannot move further down"))
4266 ((and parent (= (org-element-property :contents-end parent) end))
4267 (goto-char (org-element-property :end parent)))
4268 (t (goto-char end))))))
4270 (defun org-element-backward ()
4271 "Move backward by one element.
4272 Move to the previous element at the same level, when possible."
4273 (interactive)
4274 (if (org-with-limited-levels (org-at-heading-p))
4275 ;; At an headline, move to the previous one, if any, or stay
4276 ;; here.
4277 (let ((origin (point)))
4278 (org-backward-same-level 1)
4279 (unless (org-with-limited-levels (org-at-heading-p))
4280 (goto-char origin)
4281 (error "Cannot move further up")))
4282 (let* ((trail (org-element-at-point 'keep-trail))
4283 (elem (car trail))
4284 (prev-elem (nth 1 trail))
4285 (beg (org-element-property :begin elem)))
4286 (cond
4287 ;; Move to beginning of current element if point isn't there
4288 ;; already.
4289 ((/= (point) beg) (goto-char beg))
4290 ((not prev-elem) (error "Cannot move further up"))
4291 (t (goto-char (org-element-property :begin prev-elem)))))))
4293 (defun org-element-up ()
4294 "Move to upper element."
4295 (interactive)
4296 (if (org-with-limited-levels (org-at-heading-p))
4297 (unless (org-up-heading-safe)
4298 (error "No surrounding element"))
4299 (let* ((trail (org-element-at-point 'keep-trail))
4300 (elem (pop trail))
4301 (end (org-element-property :end elem))
4302 (parent (loop for prev in trail
4303 when (>= (org-element-property :end prev) end)
4304 return prev)))
4305 (cond
4306 (parent (goto-char (org-element-property :begin parent)))
4307 ((org-before-first-heading-p) (error "No surrounding element"))
4308 (t (org-back-to-heading))))))
4310 (defun org-element-down ()
4311 "Move to inner element."
4312 (interactive)
4313 (let ((element (org-element-at-point)))
4314 (cond
4315 ((memq (org-element-type element) '(plain-list table))
4316 (goto-char (org-element-property :contents-begin element))
4317 (forward-char))
4318 ((memq (org-element-type element) org-element-greater-elements)
4319 ;; If contents are hidden, first disclose them.
4320 (when (org-element-property :hiddenp element) (org-cycle))
4321 (goto-char (org-element-property :contents-begin element)))
4322 (t (error "No inner element")))))
4324 (defun org-element-drag-backward ()
4325 "Move backward element at point."
4326 (interactive)
4327 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
4328 (let* ((trail (org-element-at-point 'keep-trail))
4329 (elem (car trail))
4330 (prev-elem (nth 1 trail)))
4331 ;; Error out if no previous element or previous element is
4332 ;; a parent of the current one.
4333 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
4334 (error "Cannot drag element backward")
4335 (let ((pos (point)))
4336 (org-element-swap-A-B prev-elem elem)
4337 (goto-char (+ (org-element-property :begin prev-elem)
4338 (- pos (org-element-property :begin elem)))))))))
4340 (defun org-element-drag-forward ()
4341 "Move forward element at point."
4342 (interactive)
4343 (let* ((pos (point))
4344 (elem (org-element-at-point)))
4345 (when (= (point-max) (org-element-property :end elem))
4346 (error "Cannot drag element forward"))
4347 (goto-char (org-element-property :end elem))
4348 (let ((next-elem (org-element-at-point)))
4349 (when (or (org-element-nested-p elem next-elem)
4350 (and (eq (org-element-type next-elem) 'headline)
4351 (not (eq (org-element-type elem) 'headline))))
4352 (goto-char pos)
4353 (error "Cannot drag element forward"))
4354 ;; Compute new position of point: it's shifted by NEXT-ELEM
4355 ;; body's length (without final blanks) and by the length of
4356 ;; blanks between ELEM and NEXT-ELEM.
4357 (let ((size-next (- (save-excursion
4358 (goto-char (org-element-property :end next-elem))
4359 (skip-chars-backward " \r\t\n")
4360 (forward-line)
4361 ;; Small correction if buffer doesn't end
4362 ;; with a newline character.
4363 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
4364 (org-element-property :begin next-elem)))
4365 (size-blank (- (org-element-property :end elem)
4366 (save-excursion
4367 (goto-char (org-element-property :end elem))
4368 (skip-chars-backward " \r\t\n")
4369 (forward-line)
4370 (point)))))
4371 (org-element-swap-A-B elem next-elem)
4372 (goto-char (+ pos size-next size-blank))))))
4374 (defun org-element-mark-element ()
4375 "Put point at beginning of this element, mark at end.
4377 Interactively, if this command is repeated or (in Transient Mark
4378 mode) if the mark is active, it marks the next element after the
4379 ones already marked."
4380 (interactive)
4381 (let (deactivate-mark)
4382 (if (or (and (eq last-command this-command) (mark t))
4383 (and transient-mark-mode mark-active))
4384 (set-mark
4385 (save-excursion
4386 (goto-char (mark))
4387 (goto-char (org-element-property :end (org-element-at-point)))))
4388 (let ((element (org-element-at-point)))
4389 (end-of-line)
4390 (push-mark (org-element-property :end element) t t)
4391 (goto-char (org-element-property :begin element))))))
4393 (defun org-narrow-to-element ()
4394 "Narrow buffer to current element."
4395 (interactive)
4396 (let ((elem (org-element-at-point)))
4397 (cond
4398 ((eq (car elem) 'headline)
4399 (narrow-to-region
4400 (org-element-property :begin elem)
4401 (org-element-property :end elem)))
4402 ((memq (car elem) org-element-greater-elements)
4403 (narrow-to-region
4404 (org-element-property :contents-begin elem)
4405 (org-element-property :contents-end elem)))
4407 (narrow-to-region
4408 (org-element-property :begin elem)
4409 (org-element-property :end elem))))))
4411 (defun org-element-transpose ()
4412 "Transpose current and previous elements, keeping blank lines between.
4413 Point is moved after both elements."
4414 (interactive)
4415 (org-skip-whitespace)
4416 (let ((end (org-element-property :end (org-element-at-point))))
4417 (org-element-drag-backward)
4418 (goto-char end)))
4420 (defun org-element-unindent-buffer ()
4421 "Un-indent the visible part of the buffer.
4422 Relative indentation (between items, inside blocks, etc.) isn't
4423 modified."
4424 (interactive)
4425 (unless (eq major-mode 'org-mode)
4426 (error "Cannot un-indent a buffer not in Org mode"))
4427 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
4428 unindent-tree ; For byte-compiler.
4429 (unindent-tree
4430 (function
4431 (lambda (contents)
4432 (mapc
4433 (lambda (element)
4434 (if (memq (org-element-type element) '(headline section))
4435 (funcall unindent-tree (org-element-contents element))
4436 (save-excursion
4437 (save-restriction
4438 (narrow-to-region
4439 (org-element-property :begin element)
4440 (org-element-property :end element))
4441 (org-do-remove-indentation)))))
4442 (reverse contents))))))
4443 (funcall unindent-tree (org-element-contents parse-tree))))
4445 (defun org-element-fill-paragraph (&optional justify)
4446 "Fill element at point, when applicable.
4448 This function only applies to paragraph, comment blocks, example
4449 blocks and fixed-width areas. Also, as a special case, re-align
4450 table when point is at one.
4452 If JUSTIFY is non-nil (interactively, with prefix argument),
4453 justify as well. If `sentence-end-double-space' is non-nil, then
4454 period followed by one space does not end a sentence, so don't
4455 break a line there. The variable `fill-column' controls the
4456 width for filling."
4457 (let ((element (org-element-at-point)))
4458 (case (org-element-type element)
4459 ;; Align Org tables, leave table.el tables as-is.
4460 (table-row (org-table-align) t)
4461 (table
4462 (when (eq (org-element-property :type element) 'org) (org-table-align))
4464 ;; Elements that may contain `line-break' type objects.
4465 ((paragraph verse-block)
4466 (let ((beg (org-element-property :contents-begin element))
4467 (end (org-element-property :contents-end element)))
4468 ;; Do nothing if point is at an affiliated keyword or at
4469 ;; verse block markers.
4470 (if (or (< (point) beg) (>= (point) end)) t
4471 ;; At a verse block, first narrow to current "paragraph"
4472 ;; and set current element to that paragraph.
4473 (save-restriction
4474 (when (eq (org-element-type element) 'verse-block)
4475 (narrow-to-region beg end)
4476 (save-excursion
4477 (end-of-line)
4478 (let ((bol-pos (point-at-bol)))
4479 (re-search-backward org-element-paragraph-separate nil 'move)
4480 (unless (or (bobp) (= (point-at-bol) bol-pos))
4481 (forward-line))
4482 (setq element (org-element-paragraph-parser end)
4483 beg (org-element-property :contents-begin element)
4484 end (org-element-property :contents-end element)))))
4485 ;; Fill paragraph, taking line breaks into consideration.
4486 ;; For that, slice the paragraph using line breaks as
4487 ;; separators, and fill the parts in reverse order to
4488 ;; avoid messing with markers.
4489 (save-excursion
4490 (goto-char end)
4491 (mapc
4492 (lambda (pos)
4493 (fill-region-as-paragraph pos (point) justify)
4494 (goto-char pos))
4495 ;; Find the list of ending positions for line breaks
4496 ;; in the current paragraph. Add paragraph beginning
4497 ;; to include first slice.
4498 (nreverse
4499 (cons beg
4500 (org-element-map
4501 (org-element--parse-objects
4502 beg end nil org-element-all-objects)
4503 'line-break
4504 (lambda (lb) (org-element-property :end lb)))))))) t)))
4505 ;; Elements whose contents should be filled as plain text.
4506 ((comment-block example-block)
4507 (save-restriction
4508 (narrow-to-region
4509 (save-excursion
4510 (goto-char (org-element-property :begin element))
4511 (while (looking-at org-element--affiliated-re) (forward-line))
4512 (forward-line)
4513 (point))
4514 (save-excursion
4515 (goto-char (org-element-property :end element))
4516 (if (bolp) (forward-line -1) (beginning-of-line))
4517 (point)))
4518 (fill-paragraph justify) t))
4519 ;; Ignore every other element.
4520 (otherwise t))))
4523 (provide 'org-element)
4524 ;;; org-element.el ends here