1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; This file is not part of GNU Emacs.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (namely `babel-call', `clock', `headline', `item',
34 ;; `keyword', `planning', `property-drawer' and `section' types), it
35 ;; can also accept a fixed set of keywords as attributes. Those are
36 ;; called "affiliated keywords" to distinguish them from other
37 ;; keywords, which are full-fledged elements. Almost all affiliated
38 ;; keywords are referenced in `org-element-affiliated-keywords'; the
39 ;; others are export attributes and start with "ATTR_" prefix.
41 ;; Element containing other elements (and only elements) are called
42 ;; greater elements. Concerned types are: `center-block', `drawer',
43 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
44 ;; `item', `plain-list', `quote-block', `section' and `special-block'.
46 ;; Other element types are: `babel-call', `clock', `comment',
47 ;; `comment-block', `example-block', `export-block', `fixed-width',
48 ;; `horizontal-rule', `keyword', `latex-environment', `paragraph',
49 ;; `planning', `property-drawer', `quote-section', `src-block',
50 ;; `table', `table-row' and `verse-block'. Among them, `paragraph'
51 ;; and `verse-block' types can contain Org objects and plain text.
53 ;; Objects are related to document's contents. Some of them are
54 ;; recursive. Associated types are of the following: `bold', `code',
55 ;; `entity', `export-snippet', `footnote-reference',
56 ;; `inline-babel-call', `inline-src-block', `italic',
57 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
58 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
59 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
61 ;; Some elements also have special properties whose value can hold
62 ;; objects themselves (i.e. an item tag or an headline name). Such
63 ;; values are called "secondary strings". Any object belongs to
64 ;; either an element or a secondary string.
66 ;; Notwithstanding affiliated keywords, each greater element, element
67 ;; and object has a fixed set of properties attached to it. Among
68 ;; them, four are shared by all types: `:begin' and `:end', which
69 ;; refer to the beginning and ending buffer positions of the
70 ;; considered element or object, `:post-blank', which holds the number
71 ;; of blank lines, or white spaces, at its end and `:parent' which
72 ;; refers to the element or object containing it. Greater elements
73 ;; and elements containing objects will also have `:contents-begin'
74 ;; and `:contents-end' properties to delimit contents.
76 ;; Lisp-wise, an element or an object can be represented as a list.
77 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
78 ;; TYPE is a symbol describing the Org element or object.
79 ;; PROPERTIES is the property list attached to it. See docstring of
80 ;; appropriate parsing function to get an exhaustive
82 ;; CONTENTS is a list of elements, objects or raw strings contained
83 ;; in the current element or object, when applicable.
85 ;; An Org buffer is a nested list of such elements and objects, whose
86 ;; type is `org-data' and properties is nil.
88 ;; The first part of this file implements a parser and an interpreter
89 ;; for each type of Org syntax.
91 ;; The next two parts introduce four accessors and a function
92 ;; retrieving the element starting at point (respectively
93 ;; `org-element-type', `org-element-property', `org-element-contents',
94 ;; `org-element-restriction' and `org-element-current-element').
96 ;; The following part creates a fully recursive buffer parser. It
97 ;; also provides a tool to map a function to elements or objects
98 ;; matching some criteria in the parse tree. Functions of interest
99 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
100 ;; extent, `org-element-parse-secondary-string'.
102 ;; The penultimate part is the cradle of an interpreter for the
103 ;; obtained parse tree: `org-element-interpret-data'.
105 ;; The library ends by furnishing a set of interactive tools for
106 ;; element's navigation and manipulation, mostly based on
107 ;; `org-element-at-point' function.
112 (eval-when-compile (require 'cl
))
114 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
118 ;;; Definitions And Rules
120 ;; Define elements, greater elements and specify recursive objects,
121 ;; along with the affiliated keywords recognized. Also set up
122 ;; restrictions on recursive objects combinations.
124 ;; These variables really act as a control center for the parsing
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]*#\\+" "\\|" "^#\\(?: \\|$\\)" "\\|"
133 (org-item-beginning-re) "\\|"
134 ;; Fixed-width, drawers (any type) and tables.
136 ;; Footnote definitions.
137 org-footnote-definition-re
"\\|"
139 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
140 ;; LaTeX environments.
141 "^[ \t]*\\\\\\(begin\\|end\\)" "\\|"
142 ;; Planning and Clock lines.
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 '(("ASCII" . org-element-export-block-parser
)
196 ("CENTER" . org-element-center-block-parser
)
197 ("COMMENT" . org-element-comment-block-parser
)
198 ("DOCBOOK" . org-element-export-block-parser
)
199 ("EXAMPLE" . org-element-example-block-parser
)
200 ("HTML" . org-element-export-block-parser
)
201 ("LATEX" . org-element-export-block-parser
)
202 ("ODT" . org-element-export-block-parser
)
203 ("QUOTE" . org-element-quote-block-parser
)
204 ("SRC" . org-element-src-block-parser
)
205 ("VERSE" . org-element-verse-block-parser
))
206 "Alist between block names and the associated parsing function.
207 Names must be uppercase. Any block whose name has no association
208 is parsed with `org-element-special-block-parser'.")
210 (defconst org-element-affiliated-keywords
211 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
212 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
213 "List of affiliated keywords as strings.
214 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
215 are affiliated keywords and need not to be in this list.")
217 (defconst org-element--affiliated-re
218 (format "[ \t]*#\\+%s:"
219 ;; Regular affiliated keywords.
220 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
221 (regexp-opt org-element-affiliated-keywords
)))
222 "Regexp matching any affiliated keyword.
224 Keyword name is put in match group 1. Moreover, if keyword
225 belongs to `org-element-dual-keywords', put the dual value in
228 Don't modify it, set `org-element-affiliated-keywords' instead.")
230 (defconst org-element-keyword-translation-alist
231 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
232 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
233 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
234 "Alist of usual translations for keywords.
235 The key is the old name and the value the new one. The property
236 holding their value will be named after the translated name.")
238 (defconst org-element-multiple-keywords
'("HEADER")
239 "List of affiliated keywords that can occur more that once in an element.
241 Their value will be consed into a list of strings, which will be
242 returned as the value of the property.
244 This list is checked after translations have been applied. See
245 `org-element-keyword-translation-alist'.
247 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
248 allow multiple occurrences and need not to be in this list.")
250 (defconst org-element-parsed-keywords
'("AUTHOR" "CAPTION" "DATE" "TITLE")
251 "List of keywords whose value can be parsed.
253 Their value will be stored as a secondary string: a list of
256 This list is checked after translations have been applied. See
257 `org-element-keyword-translation-alist'.")
259 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
260 "List of keywords which can have a secondary value.
262 In Org syntax, they can be written with optional square brackets
263 before the colons. For example, results keyword can be
264 associated to a hash value with the following:
266 #+RESULTS[hash-string]: some-source
268 This list is checked after translations have been applied. See
269 `org-element-keyword-translation-alist'.")
271 (defconst org-element-object-restrictions
272 `((bold entity export-snippet inline-babel-call inline-src-block link
273 radio-target sub
/superscript target text-markup timestamp
)
274 (footnote-reference entity export-snippet footnote-reference
275 inline-babel-call inline-src-block latex-fragment
276 line-break link macro radio-target sub
/superscript
277 target text-markup timestamp
)
278 (headline entity inline-babel-call inline-src-block latex-fragment link
279 macro radio-target statistics-cookie sub
/superscript target
280 text-markup timestamp
)
281 (inlinetask entity inline-babel-call inline-src-block latex-fragment link
282 macro radio-target sub
/superscript target text-markup timestamp
)
283 (italic entity export-snippet inline-babel-call inline-src-block link
284 radio-target sub
/superscript target text-markup timestamp
)
285 (item entity footnote-reference inline-babel-call latex-fragment macro
286 radio-target sub
/superscript target text-markup
)
287 (keyword entity latex-fragment macro sub
/superscript text-markup
)
288 (link entity export-snippet inline-babel-call inline-src-block
289 latex-fragment link sub
/superscript text-markup
)
291 (paragraph ,@org-element-all-successors
)
292 (radio-target entity export-snippet latex-fragment sub
/superscript
)
293 (strike-through entity export-snippet inline-babel-call inline-src-block
294 link radio-target sub
/superscript target text-markup
296 (subscript entity export-snippet inline-babel-call inline-src-block
297 latex-fragment sub
/superscript target text-markup
)
298 (superscript entity export-snippet inline-babel-call inline-src-block
299 latex-fragment sub
/superscript target text-markup
)
300 (table-cell entity export-snippet latex-fragment link macro radio-target
301 sub
/superscript target text-markup timestamp
)
302 (table-row table-cell
)
303 (underline entity export-snippet inline-babel-call inline-src-block link
304 radio-target sub
/superscript target text-markup timestamp
)
305 (verse-block entity footnote-reference inline-babel-call inline-src-block
306 latex-fragment line-break link macro radio-target
307 sub
/superscript target text-markup timestamp
))
308 "Alist of objects restrictions.
310 CAR is an element or object type containing objects and CDR is
311 a list of successors that will be called within an element or
314 For example, in a `radio-target' object, one can only find
315 entities, export snippets, latex-fragments, subscript and
318 This alist also applies to secondary string. For example, an
319 `headline' type element doesn't directly contain objects, but
320 still has an entry since one of its properties (`:title') does.")
322 (defconst org-element-secondary-value-alist
323 '((headline .
:title
)
324 (inlinetask .
:title
)
326 (footnote-reference .
:inline-definition
))
327 "Alist between element types and location of secondary value.")
333 ;; For each greater element type, we define a parser and an
336 ;; A parser returns the element or object as the list described above.
337 ;; Most of them accepts no argument. Though, exceptions exist. Hence
338 ;; every element containing a secondary string (see
339 ;; `org-element-secondary-value-alist') will accept an optional
340 ;; argument to toggle parsing of that secondary string. Moreover,
341 ;; `item' parser requires current list's structure as its first
344 ;; An interpreter accepts two arguments: the list representation of
345 ;; the element or object, and its contents. The latter may be nil,
346 ;; depending on the element or object considered. It returns the
347 ;; appropriate Org syntax, as a string.
349 ;; Parsing functions must follow the naming convention:
350 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
351 ;; defined in `org-element-greater-elements'.
353 ;; Similarly, interpreting functions must follow the naming
354 ;; convention: org-element-TYPE-interpreter.
356 ;; With the exception of `headline' and `item' types, greater elements
357 ;; cannot contain other greater elements of their own type.
359 ;; Beside implementing a parser and an interpreter, adding a new
360 ;; greater element requires to tweak `org-element-current-element'.
361 ;; Moreover, the newly defined type must be added to both
362 ;; `org-element-all-elements' and `org-element-greater-elements'.
367 (defun org-element-center-block-parser ()
368 "Parse a center block.
370 Return a list whose CAR is `center-block' and CDR is a plist
371 containing `:begin', `:end', `:hiddenp', `:contents-begin',
372 `:contents-end' and `:post-blank' keywords.
374 Assume point is at the beginning of the block."
376 (let* ((case-fold-search t
)
377 (keywords (org-element-collect-affiliated-keywords))
378 (begin (car keywords
))
379 (contents-begin (progn (forward-line) (point)))
380 (hidden (org-truely-invisible-p))
382 (progn (re-search-forward "^[ \t]*#\\+END_CENTER" nil t
)
384 (pos-before-blank (progn (forward-line) (point)))
385 (end (progn (org-skip-whitespace)
386 (if (eobp) (point) (point-at-bol)))))
391 :contents-begin
,contents-begin
392 :contents-end
,contents-end
393 :post-blank
,(count-lines pos-before-blank end
)
394 ,@(cadr keywords
))))))
396 (defun org-element-center-block-interpreter (center-block contents
)
397 "Interpret CENTER-BLOCK element as Org syntax.
398 CONTENTS is the contents of the element."
399 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
404 (defun org-element-drawer-parser ()
407 Return a list whose CAR is `drawer' and CDR is a plist containing
408 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
409 `:contents-end' and `:post-blank' keywords.
411 Assume point is at beginning of drawer."
413 (let* ((case-fold-search t
)
414 (name (progn (looking-at org-drawer-regexp
)
415 (org-match-string-no-properties 1)))
416 (keywords (org-element-collect-affiliated-keywords))
417 (begin (car keywords
))
418 (contents-begin (progn (forward-line) (point)))
419 (hidden (org-truely-invisible-p))
420 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
422 (pos-before-blank (progn (forward-line) (point)))
423 (end (progn (org-skip-whitespace)
424 (if (eobp) (point) (point-at-bol)))))
430 :contents-begin
,contents-begin
431 :contents-end
,contents-end
432 :post-blank
,(count-lines pos-before-blank end
)
433 ,@(cadr keywords
))))))
435 (defun org-element-drawer-interpreter (drawer contents
)
436 "Interpret DRAWER element as Org syntax.
437 CONTENTS is the contents of the element."
438 (format ":%s:\n%s:END:"
439 (org-element-property :drawer-name drawer
)
445 (defun org-element-dynamic-block-parser ()
446 "Parse a dynamic block.
448 Return a list whose CAR is `dynamic-block' and CDR is a plist
449 containing `:block-name', `:begin', `:end', `:hiddenp',
450 `:contents-begin', `:contents-end', `:arguments' and
451 `:post-blank' keywords.
453 Assume point is at beginning of dynamic block."
455 (let* ((case-fold-search t
)
456 (name (progn (looking-at org-dblock-start-re
)
457 (org-match-string-no-properties 1)))
458 (arguments (org-match-string-no-properties 3))
459 (keywords (org-element-collect-affiliated-keywords))
460 (begin (car keywords
))
461 (contents-begin (progn (forward-line) (point)))
462 (hidden (org-truely-invisible-p))
463 (contents-end (progn (re-search-forward org-dblock-end-re nil t
)
465 (pos-before-blank (progn (forward-line) (point)))
466 (end (progn (org-skip-whitespace)
467 (if (eobp) (point) (point-at-bol)))))
472 :arguments
,arguments
474 :contents-begin
,contents-begin
475 :contents-end
,contents-end
476 :post-blank
,(count-lines pos-before-blank end
)
477 ,@(cadr keywords
))))))
479 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
480 "Interpret DYNAMIC-BLOCK element as Org syntax.
481 CONTENTS is the contents of the element."
482 (format "#+BEGIN: %s%s\n%s#+END:"
483 (org-element-property :block-name dynamic-block
)
484 (let ((args (org-element-property :arguments dynamic-block
)))
485 (and args
(concat " " args
)))
489 ;;;; Footnote Definition
491 (defun org-element-footnote-definition-parser ()
492 "Parse a footnote definition.
494 Return a list whose CAR is `footnote-definition' and CDR is
495 a plist containing `:label', `:begin' `:end', `:contents-begin',
496 `:contents-end' and `:post-blank' keywords.
498 Assume point is at the beginning of the footnote definition."
500 (looking-at org-footnote-definition-re
)
501 (let* ((label (org-match-string-no-properties 1))
502 (keywords (org-element-collect-affiliated-keywords))
503 (begin (car keywords
))
504 (contents-begin (progn (search-forward "]")
505 (org-skip-whitespace)
507 (contents-end (if (progn
510 (concat org-outline-regexp-bol
"\\|"
511 org-footnote-definition-re
"\\|"
512 "^[ \t]*$") nil
'move
))
515 (end (progn (org-skip-whitespace)
516 (if (eobp) (point) (point-at-bol)))))
517 `(footnote-definition
521 :contents-begin
,contents-begin
522 :contents-end
,contents-end
523 :post-blank
,(count-lines contents-end end
)
524 ,@(cadr keywords
))))))
526 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
527 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
528 CONTENTS is the contents of the footnote-definition."
529 (concat (format "[%s]" (org-element-property :label footnote-definition
))
536 (defun org-element-headline-parser (&optional raw-secondary-p
)
539 Return a list whose CAR is `headline' and CDR is a plist
540 containing `:raw-value', `:title', `:begin', `:end',
541 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
542 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
543 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
544 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
547 The plist also contains any property set in the property drawer,
548 with its name in lowercase, the underscores replaced with hyphens
549 and colons at the beginning (i.e. `:custom-id').
551 When RAW-SECONDARY-P is non-nil, headline's title will not be
552 parsed as a secondary string, but as a plain string instead.
554 Assume point is at beginning of the headline."
556 (let* ((components (org-heading-components))
557 (level (nth 1 components
))
558 (todo (nth 2 components
))
560 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
561 (tags (let ((raw-tags (nth 5 components
)))
562 (and raw-tags
(org-split-string raw-tags
":"))))
563 (raw-value (nth 4 components
))
565 (let ((case-fold-search nil
))
566 (string-match (format "^%s +" org-quote-string
) raw-value
)))
568 (let ((case-fold-search nil
))
569 (string-match (format "^%s +" org-comment-string
) raw-value
)))
570 (archivedp (member org-archive-tag tags
))
571 (footnote-section-p (and org-footnote-section
572 (string= org-footnote-section raw-value
)))
573 (standard-props (let (plist)
576 (let ((p-name (downcase (car p
))))
577 (while (string-match "_" p-name
)
579 (replace-match "-" nil nil p-name
)))
580 (setq p-name
(intern (concat ":" p-name
)))
582 (plist-put plist p-name
(cdr p
)))))
583 (org-entry-properties nil
'standard
))
585 (time-props (org-entry-properties nil
'special
"CLOCK"))
586 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
587 (deadline (cdr (assoc "DEADLINE" time-props
)))
588 (clock (cdr (assoc "CLOCK" time-props
)))
589 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
591 (pos-after-head (save-excursion (forward-line) (point)))
592 (contents-begin (save-excursion (forward-line)
593 (org-skip-whitespace)
594 (if (eobp) (point) (point-at-bol))))
595 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
596 (end (progn (goto-char (org-end-of-subtree t t
))))
597 (contents-end (progn (skip-chars-backward " \r\t\n")
601 ;; Clean RAW-VALUE from any quote or comment string.
602 (when (or quotedp commentedp
)
604 (replace-regexp-in-string
605 (concat "\\(" org-quote-string
"\\|" org-comment-string
"\\) +")
608 ;; Clean TAGS from archive tag, if any.
609 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
612 (if raw-secondary-p raw-value
613 (org-element-parse-secondary-string
614 raw-value
(org-element-restriction 'headline
))))
616 (:raw-value
,raw-value
620 :pre-blank
,(count-lines pos-after-head contents-begin
)
622 :contents-begin
,contents-begin
623 :contents-end
,contents-end
625 :priority
,(nth 3 components
)
628 :todo-type
,todo-type
629 :scheduled
,scheduled
631 :timestamp
,timestamp
633 :post-blank
,(count-lines contents-end end
)
634 :footnote-section-p
,footnote-section-p
635 :archivedp
,archivedp
636 :commentedp
,commentedp
638 ,@standard-props
)))))
640 (defun org-element-headline-interpreter (headline contents
)
641 "Interpret HEADLINE element as Org syntax.
642 CONTENTS is the contents of the element."
643 (let* ((level (org-element-property :level headline
))
644 (todo (org-element-property :todo-keyword headline
))
645 (priority (org-element-property :priority headline
))
646 (title (org-element-interpret-data
647 (org-element-property :title headline
)))
648 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
649 (cons org-archive-tag
650 (org-element-property :tags headline
))
651 (org-element-property :tags headline
))))
653 (format ":%s:" (mapconcat 'identity tag-list
":")))))
654 (commentedp (org-element-property :commentedp headline
))
655 (quotedp (org-element-property :quotedp headline
))
656 (pre-blank (or (org-element-property :pre-blank headline
) 0))
657 (heading (concat (make-string level ?
*)
658 (and todo
(concat " " todo
))
659 (and quotedp
(concat " " org-quote-string
))
660 (and commentedp
(concat " " org-comment-string
))
662 (format " [#%s]" (char-to-string priority
)))
663 (cond ((and org-footnote-section
664 (org-element-property
665 :footnote-section-p headline
))
666 (concat " " org-footnote-section
))
667 (title (concat " " title
))))))
672 ((zerop org-tags-column
) (format " %s" tags
))
673 ((< org-tags-column
0)
676 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
681 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
683 (make-string (1+ pre-blank
) 10)
689 (defun org-element-inlinetask-parser (&optional raw-secondary-p
)
690 "Parse an inline task.
692 Return a list whose CAR is `inlinetask' and CDR is a plist
693 containing `:title', `:begin', `:end', `:hiddenp',
694 `:contents-begin' and `:contents-end', `:level', `:priority',
695 `:tags', `:todo-keyword', `:todo-type', `:scheduled',
696 `:deadline', `:timestamp', `:clock' and `:post-blank' keywords.
698 The plist also contains any property set in the property drawer,
699 with its name in lowercase, the underscores replaced with hyphens
700 and colons at the beginning (i.e. `:custom-id').
702 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
703 title will not be parsed as a secondary string, but as a plain
706 Assume point is at beginning of the inline task."
708 (let* ((keywords (org-element-collect-affiliated-keywords))
709 (begin (car keywords
))
710 (components (org-heading-components))
711 (todo (nth 2 components
))
713 (if (member todo org-done-keywords
) 'done
'todo
)))
714 (tags (let ((raw-tags (nth 5 components
)))
715 (and raw-tags
(org-split-string raw-tags
":"))))
716 (title (if raw-secondary-p
(nth 4 components
)
717 (org-element-parse-secondary-string
719 (org-element-restriction 'inlinetask
))))
720 (standard-props (let (plist)
723 (let ((p-name (downcase (car p
))))
724 (while (string-match "_" p-name
)
726 (replace-match "-" nil nil p-name
)))
727 (setq p-name
(intern (concat ":" p-name
)))
729 (plist-put plist p-name
(cdr p
)))))
730 (org-entry-properties nil
'standard
))
732 (time-props (org-entry-properties nil
'special
"CLOCK"))
733 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
734 (deadline (cdr (assoc "DEADLINE" time-props
)))
735 (clock (cdr (assoc "CLOCK" time-props
)))
736 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
737 (contents-begin (save-excursion (forward-line) (point)))
738 (hidden (org-truely-invisible-p))
739 (pos-before-blank (org-inlinetask-goto-end))
740 ;; In the case of a single line task, CONTENTS-BEGIN and
741 ;; CONTENTS-END might overlap.
742 (contents-end (max contents-begin
743 (if (not (bolp)) (point-at-bol)
744 (save-excursion (forward-line -
1) (point)))))
745 (end (progn (org-skip-whitespace)
746 (if (eobp) (point) (point-at-bol)))))
751 :hiddenp
,(and (> contents-end contents-begin
) hidden
)
752 :contents-begin
,contents-begin
753 :contents-end
,contents-end
754 :level
,(nth 1 components
)
755 :priority
,(nth 3 components
)
758 :todo-type
,todo-type
759 :scheduled
,scheduled
761 :timestamp
,timestamp
763 :post-blank
,(count-lines pos-before-blank end
)
765 ,@(cadr keywords
))))))
767 (defun org-element-inlinetask-interpreter (inlinetask contents
)
768 "Interpret INLINETASK element as Org syntax.
769 CONTENTS is the contents of inlinetask."
770 (let* ((level (org-element-property :level inlinetask
))
771 (todo (org-element-property :todo-keyword inlinetask
))
772 (priority (org-element-property :priority inlinetask
))
773 (title (org-element-interpret-data
774 (org-element-property :title inlinetask
)))
775 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
777 (format ":%s:" (mapconcat 'identity tag-list
":")))))
778 (task (concat (make-string level ?
*)
779 (and todo
(concat " " todo
))
781 (format " [#%s]" (char-to-string priority
)))
782 (and title
(concat " " title
)))))
787 ((zerop org-tags-column
) (format " %s" tags
))
788 ((< org-tags-column
0)
791 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
796 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
798 ;; Prefer degenerate inlinetasks when there are no
803 (make-string level ?
*) " END")))))
808 (defun org-element-item-parser (struct &optional raw-secondary-p
)
811 STRUCT is the structure of the plain list.
813 Return a list whose CAR is `item' and CDR is a plist containing
814 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
815 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
816 `:post-blank' keywords.
818 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
819 any, will not be parsed as a secondary string, but as a plain
822 Assume point is at the beginning of the item."
825 (let* ((begin (point))
826 (bullet (org-list-get-bullet (point) struct
))
827 (checkbox (let ((box (org-list-get-checkbox begin struct
)))
828 (cond ((equal "[ ]" box
) 'off
)
829 ((equal "[X]" box
) 'on
)
830 ((equal "[-]" box
) 'trans
))))
831 (counter (let ((c (org-list-get-counter begin struct
)))
834 ((string-match "[A-Za-z]" c
)
835 (- (string-to-char (upcase (match-string 0 c
)))
837 ((string-match "[0-9]+" c
)
838 (string-to-number (match-string 0 c
))))))
840 (let ((raw-tag (org-list-get-tag begin struct
)))
842 (if raw-secondary-p raw-tag
843 (org-element-parse-secondary-string
844 raw-tag
(org-element-restriction 'item
))))))
845 (end (org-list-get-item-end begin struct
))
846 (contents-begin (progn (looking-at org-list-full-item-re
)
847 (goto-char (match-end 0))
848 (org-skip-whitespace)
849 ;; If first line isn't empty,
850 ;; contents really start at the text
851 ;; after item's meta-data.
852 (if (= (point-at-bol) begin
) (point)
854 (hidden (progn (forward-line)
855 (and (not (= (point) end
))
856 (org-truely-invisible-p))))
857 (contents-end (progn (goto-char end
)
858 (skip-chars-backward " \r\t\n")
865 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
866 ;; up in the case of an empty item separated
867 ;; from the next by a blank line. Thus, ensure
868 ;; the former is always the smallest of two.
869 :contents-begin
,(min contents-begin contents-end
)
870 :contents-end
,(max contents-begin contents-end
)
876 :post-blank
,(count-lines contents-end end
))))))
878 (defun org-element-item-interpreter (item contents
)
879 "Interpret ITEM element as Org syntax.
880 CONTENTS is the contents of the element."
882 (let* ((beg (org-element-property :begin item
))
883 (struct (org-element-property :structure item
))
884 (pre (org-list-prevs-alist struct
))
885 (bul (org-element-property :bullet item
)))
886 (org-list-bullet-string
887 (if (not (eq (org-list-get-list-type beg struct pre
) 'ordered
)) "-"
891 (org-list-get-item-number
892 beg struct pre
(org-list-parents-alist struct
))))))
895 (if (eq org-plain-list-ordered-item-terminator ?\
)) ")"
897 (checkbox (org-element-property :checkbox item
))
898 (counter (org-element-property :counter item
))
899 (tag (let ((tag (org-element-property :tag item
)))
900 (and tag
(org-element-interpret-data tag
))))
901 ;; Compute indentation.
902 (ind (make-string (length bullet
) 32))
903 (item-starts-with-par-p
904 (eq (org-element-type (car (org-element-contents item
)))
909 (and counter
(format "[@%d] " counter
))
911 ((eq checkbox
'on
) "[X] ")
912 ((eq checkbox
'off
) "[ ] ")
913 ((eq checkbox
'trans
) "[-] "))
914 (and tag
(format "%s :: " tag
))
915 (let ((contents (replace-regexp-in-string
916 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))
917 (if item-starts-with-par-p
(org-trim contents
)
918 (concat "\n" contents
))))))
923 (defun org-element-plain-list-parser (&optional structure
)
926 Optional argument STRUCTURE, when non-nil, is the structure of
927 the plain list being parsed.
929 Return a list whose CAR is `plain-list' and CDR is a plist
930 containing `:type', `:begin', `:end', `:contents-begin' and
931 `:contents-end', `:structure' and `:post-blank' keywords.
933 Assume point is at the beginning of the list."
935 (let* ((struct (or structure
(org-list-struct)))
936 (prevs (org-list-prevs-alist struct
))
937 (parents (org-list-parents-alist struct
))
938 (type (org-list-get-list-type (point) struct prevs
))
939 (contents-begin (point))
940 (keywords (org-element-collect-affiliated-keywords))
941 (begin (car keywords
))
943 (goto-char (org-list-get-list-end (point) struct prevs
)))
944 (end (save-excursion (org-skip-whitespace)
945 (if (eobp) (point) (point-at-bol)))))
946 ;; Blank lines below list belong to the top-level list only.
947 (unless (= (org-list-get-top-point struct
) contents-begin
)
948 (setq end
(min (org-list-get-bottom-point struct
)
949 (progn (org-skip-whitespace)
950 (if (eobp) (point) (point-at-bol))))))
956 :contents-begin
,contents-begin
957 :contents-end
,contents-end
959 :post-blank
,(count-lines contents-end end
)
960 ,@(cadr keywords
))))))
962 (defun org-element-plain-list-interpreter (plain-list contents
)
963 "Interpret PLAIN-LIST element as Org syntax.
964 CONTENTS is the contents of the element."
970 (defun org-element-quote-block-parser ()
971 "Parse a quote block.
973 Return a list whose CAR is `quote-block' and CDR is a plist
974 containing `:begin', `:end', `:hiddenp', `:contents-begin',
975 `:contents-end' and `:post-blank' keywords.
977 Assume point is at the beginning of the block."
979 (let* ((case-fold-search t
)
980 (keywords (org-element-collect-affiliated-keywords))
981 (begin (car keywords
))
982 (contents-begin (progn (forward-line) (point)))
983 (hidden (org-truely-invisible-p))
984 (contents-end (progn (re-search-forward "^[ \t]*#\\+END_QUOTE" nil t
)
986 (pos-before-blank (progn (forward-line) (point)))
987 (end (progn (org-skip-whitespace)
988 (if (eobp) (point) (point-at-bol)))))
993 :contents-begin
,contents-begin
994 :contents-end
,contents-end
995 :post-blank
,(count-lines pos-before-blank end
)
996 ,@(cadr keywords
))))))
998 (defun org-element-quote-block-interpreter (quote-block contents
)
999 "Interpret QUOTE-BLOCK element as Org syntax.
1000 CONTENTS is the contents of the element."
1001 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
1006 (defun org-element-section-parser ()
1009 Return a list whose CAR is `section' and CDR is a plist
1010 containing `:begin', `:end', `:contents-begin', `contents-end'
1011 and `:post-blank' keywords."
1013 ;; Beginning of section is the beginning of the first non-blank
1014 ;; line after previous headline.
1015 (org-with-limited-levels
1018 (outline-previous-heading)
1019 (if (not (org-at-heading-p)) (point)
1020 (forward-line) (org-skip-whitespace) (point-at-bol))))
1021 (end (progn (outline-next-heading) (point)))
1022 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1028 :contents-begin
,begin
1029 :contents-end
,pos-before-blank
1030 :post-blank
,(count-lines pos-before-blank end
)))))))
1032 (defun org-element-section-interpreter (section contents
)
1033 "Interpret SECTION element as Org syntax.
1034 CONTENTS is the contents of the element."
1040 (defun org-element-special-block-parser ()
1041 "Parse a special block.
1043 Return a list whose CAR is `special-block' and CDR is a plist
1044 containing `:type', `:begin', `:end', `:hiddenp',
1045 `:contents-begin', `:contents-end' and `:post-blank' keywords.
1047 Assume point is at the beginning of the block."
1049 (let* ((case-fold-search t
)
1050 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\([-A-Za-z0-9]+\\)")
1051 (org-match-string-no-properties 1)))
1052 (keywords (org-element-collect-affiliated-keywords))
1053 (begin (car keywords
))
1054 (contents-begin (progn (forward-line) (point)))
1055 (hidden (org-truely-invisible-p))
1057 (progn (re-search-forward (concat "^[ \t]*#\\+END_" type
) nil t
)
1059 (pos-before-blank (progn (forward-line) (point)))
1060 (end (progn (org-skip-whitespace)
1061 (if (eobp) (point) (point-at-bol)))))
1067 :contents-begin
,contents-begin
1068 :contents-end
,contents-end
1069 :post-blank
,(count-lines pos-before-blank end
)
1070 ,@(cadr keywords
))))))
1072 (defun org-element-special-block-interpreter (special-block contents
)
1073 "Interpret SPECIAL-BLOCK element as Org syntax.
1074 CONTENTS is the contents of the element."
1075 (let ((block-type (org-element-property :type special-block
)))
1076 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
1082 ;; For each element, a parser and an interpreter are also defined.
1083 ;; Both follow the same naming convention used for greater elements.
1085 ;; Also, as for greater elements, adding a new element type is done
1086 ;; through the following steps: implement a parser and an interpreter,
1087 ;; tweak `org-element-current-element' so that it recognizes the new
1088 ;; type and add that new type to `org-element-all-elements'.
1090 ;; As a special case, when the newly defined type is a block type,
1091 ;; `org-element-block-name-alist' has to be modified accordingly.
1096 (defun org-element-babel-call-parser ()
1097 "Parse a babel call.
1099 Return a list whose CAR is `babel-call' and CDR is a plist
1100 containing `:begin', `:end', `:info' and `:post-blank' as
1103 (let ((case-fold-search t
)
1104 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
1105 (org-babel-lob-get-info)))
1106 (begin (point-at-bol))
1107 (pos-before-blank (progn (forward-line) (point)))
1108 (end (progn (org-skip-whitespace)
1109 (if (eobp) (point) (point-at-bol)))))
1114 :post-blank
,(count-lines pos-before-blank end
))))))
1116 (defun org-element-babel-call-interpreter (babel-call contents
)
1117 "Interpret BABEL-CALL element as Org syntax.
1119 (let* ((babel-info (org-element-property :info babel-call
))
1120 (main (car babel-info
))
1121 (post-options (nth 1 babel-info
)))
1123 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
1124 ;; Remove redundant square brackets.
1125 (replace-match (match-string 1 main
) nil nil main
))
1126 (and post-options
(format "[%s]" post-options
)))))
1131 (defun org-element-clock-parser ()
1134 Return a list whose CAR is `clock' and CDR is a plist containing
1135 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
1138 (let* ((case-fold-search nil
)
1140 (value (progn (search-forward org-clock-string
(line-end-position) t
)
1141 (org-skip-whitespace)
1142 (looking-at "\\[.*\\]")
1143 (org-match-string-no-properties 0)))
1144 (time (and (progn (goto-char (match-end 0))
1145 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
1146 (org-match-string-no-properties 1)))
1147 (status (if time
'closed
'running
))
1148 (post-blank (let ((before-blank (progn (forward-line) (point))))
1149 (org-skip-whitespace)
1150 (unless (eobp) (beginning-of-line))
1151 (count-lines before-blank
(point))))
1153 `(clock (:status
,status
1158 :post-blank
,post-blank
)))))
1160 (defun org-element-clock-interpreter (clock contents
)
1161 "Interpret CLOCK element as Org syntax.
1163 (concat org-clock-string
" "
1164 (org-element-property :value clock
)
1165 (let ((time (org-element-property :time clock
)))
1170 (org-split-string time
":")))))))
1175 (defun org-element-comment-parser ()
1178 Return a list whose CAR is `comment' and CDR is a plist
1179 containing `:begin', `:end', `:value' and `:post-blank'
1182 Assume point is at comment beginning."
1184 (let* ((keywords (org-element-collect-affiliated-keywords))
1185 (begin (car keywords
))
1186 ;; Match first line with a loose regexp since it might as
1187 ;; well be an ill-defined keyword.
1189 (looking-at "#\\+? ?")
1190 (buffer-substring-no-properties
1191 (match-end 0) (progn (forward-line) (point)))))
1193 ;; Get comments ending. This may not be accurate if
1194 ;; commented lines within an item are followed by
1195 ;; commented lines outside of a list. Though, parser will
1196 ;; always get it right as it already knows surrounding
1197 ;; element and has narrowed buffer to its contents.
1199 (while (looking-at "\\(\\(# ?\\)[^+]\\|[ \t]*#\\+\\( \\|$\\)\\)")
1200 ;; Accumulate lines without leading hash and plus sign
1201 ;; if any. First whitespace is also ignored.
1204 (buffer-substring-no-properties
1205 (or (match-end 2) (match-end 3))
1206 (progn (forward-line) (point))))))
1208 (end (progn (goto-char com-end
)
1209 (org-skip-whitespace)
1210 (if (eobp) (point) (point-at-bol)))))
1215 :post-blank
,(count-lines com-end end
)
1216 ,@(cadr keywords
))))))
1218 (defun org-element-comment-interpreter (comment contents
)
1219 "Interpret COMMENT element as Org syntax.
1221 (replace-regexp-in-string "^" "#+ " (org-element-property :value comment
)))
1226 (defun org-element-comment-block-parser ()
1227 "Parse an export block.
1229 Return a list whose CAR is `comment-block' and CDR is a plist
1230 containing `:begin', `:end', `:hiddenp', `:value' and
1231 `:post-blank' keywords.
1233 Assume point is at comment block beginning."
1235 (let* ((case-fold-search t
)
1236 (keywords (org-element-collect-affiliated-keywords))
1237 (begin (car keywords
))
1238 (contents-begin (progn (forward-line) (point)))
1239 (hidden (org-truely-invisible-p))
1241 (progn (re-search-forward "^[ \t]*#\\+END_COMMENT" nil t
)
1243 (pos-before-blank (progn (forward-line) (point)))
1244 (end (progn (org-skip-whitespace)
1245 (if (eobp) (point) (point-at-bol))))
1246 (value (buffer-substring-no-properties contents-begin contents-end
)))
1252 :post-blank
,(count-lines pos-before-blank end
)
1253 ,@(cadr keywords
))))))
1255 (defun org-element-comment-block-interpreter (comment-block contents
)
1256 "Interpret COMMENT-BLOCK element as Org syntax.
1258 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1259 (org-remove-indentation (org-element-property :value comment-block
))))
1264 (defun org-element-example-block-parser ()
1265 "Parse an example block.
1267 Return a list whose CAR is `example-block' and CDR is a plist
1268 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1269 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1270 `:switches', `:value' and `:post-blank' keywords."
1272 (let* ((case-fold-search t
)
1274 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1275 (org-match-string-no-properties 1)))
1276 ;; Switches analysis
1277 (number-lines (cond ((not switches
) nil
)
1278 ((string-match "-n\\>" switches
) 'new
)
1279 ((string-match "+n\\>" switches
) 'continued
)))
1280 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1281 ;; Should labels be retained in (or stripped from) example
1285 (not (string-match "-r\\>" switches
))
1286 (and number-lines
(string-match "-k\\>" switches
))))
1287 ;; What should code-references use - labels or
1291 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1292 (label-fmt (and switches
1293 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1294 (match-string 1 switches
)))
1295 ;; Standard block parsing.
1296 (keywords (org-element-collect-affiliated-keywords))
1297 (begin (car keywords
))
1298 (contents-begin (progn (forward-line) (point)))
1299 (hidden (org-truely-invisible-p))
1301 (progn (re-search-forward "^[ \t]*#\\+END_EXAMPLE" nil t
)
1303 (value (buffer-substring-no-properties contents-begin contents-end
))
1304 (pos-before-blank (progn (forward-line) (point)))
1305 (end (progn (org-skip-whitespace)
1306 (if (eobp) (point) (point-at-bol)))))
1312 :number-lines
,number-lines
1313 :preserve-indent
,preserve-indent
1314 :retain-labels
,retain-labels
1315 :use-labels
,use-labels
1316 :label-fmt
,label-fmt
1318 :post-blank
,(count-lines pos-before-blank end
)
1319 ,@(cadr keywords
))))))
1321 (defun org-element-example-block-interpreter (example-block contents
)
1322 "Interpret EXAMPLE-BLOCK element as Org syntax.
1324 (let ((switches (org-element-property :switches example-block
)))
1325 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1326 (org-remove-indentation
1327 (org-element-property :value example-block
))
1333 (defun org-element-export-block-parser ()
1334 "Parse an export block.
1336 Return a list whose CAR is `export-block' and CDR is a plist
1337 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1338 `:post-blank' keywords.
1340 Assume point is at export-block beginning."
1342 (let* ((case-fold-search t
)
1343 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\([A-Za-z0-9]+\\)")
1344 (upcase (org-match-string-no-properties 1))))
1345 (keywords (org-element-collect-affiliated-keywords))
1346 (begin (car keywords
))
1347 (contents-begin (progn (forward-line) (point)))
1348 (hidden (org-truely-invisible-p))
1350 (progn (re-search-forward (concat "^[ \t]*#\\+END_" type
) nil t
)
1352 (pos-before-blank (progn (forward-line) (point)))
1353 (end (progn (org-skip-whitespace)
1354 (if (eobp) (point) (point-at-bol))))
1355 (value (buffer-substring-no-properties contents-begin contents-end
)))
1362 :post-blank
,(count-lines pos-before-blank end
)
1363 ,@(cadr keywords
))))))
1365 (defun org-element-export-block-interpreter (export-block contents
)
1366 "Interpret EXPORT-BLOCK element as Org syntax.
1368 (let ((type (org-element-property :type export-block
)))
1369 (concat (format "#+BEGIN_%s\n" type
)
1370 (org-element-property :value export-block
)
1371 (format "#+END_%s" type
))))
1376 (defun org-element-fixed-width-parser ()
1377 "Parse a fixed-width section.
1379 Return a list whose CAR is `fixed-width' and CDR is a plist
1380 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1382 Assume point is at the beginning of the fixed-width area."
1384 (let* ((keywords (org-element-collect-affiliated-keywords))
1385 (begin (car keywords
))
1388 ;; Ending position may not be accurate if fixed-width
1389 ;; lines within an item are followed by fixed-width lines
1390 ;; outside of a list. Though, parser will always get it
1391 ;; right as it already knows surrounding element and has
1392 ;; narrowed buffer to its contents.
1394 (while (looking-at "[ \t]*:\\( \\|$\\)")
1395 ;, Accumulate text without starting colons.
1398 (buffer-substring-no-properties
1399 (match-end 0) (point-at-eol))
1403 (end (progn (org-skip-whitespace)
1404 (if (eobp) (point) (point-at-bol)))))
1409 :post-blank
,(count-lines end-area end
)
1410 ,@(cadr keywords
))))))
1412 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1413 "Interpret FIXED-WIDTH element as Org syntax.
1415 (replace-regexp-in-string
1416 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1419 ;;;; Horizontal Rule
1421 (defun org-element-horizontal-rule-parser ()
1422 "Parse an horizontal rule.
1424 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1425 containing `:begin', `:end' and `:post-blank' keywords."
1427 (let* ((keywords (org-element-collect-affiliated-keywords))
1428 (begin (car keywords
))
1429 (post-hr (progn (forward-line) (point)))
1430 (end (progn (org-skip-whitespace)
1431 (if (eobp) (point) (point-at-bol)))))
1435 :post-blank
,(count-lines post-hr end
)
1436 ,@(cadr keywords
))))))
1438 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1439 "Interpret HORIZONTAL-RULE element as Org syntax.
1446 (defun org-element-keyword-parser ()
1447 "Parse a keyword at point.
1449 Return a list whose CAR is `keyword' and CDR is a plist
1450 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1453 (let* ((case-fold-search t
)
1455 (key (progn (looking-at
1456 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1457 (upcase (org-match-string-no-properties 1))))
1458 (value (org-trim (buffer-substring-no-properties
1459 (match-end 0) (point-at-eol))))
1460 (pos-before-blank (progn (forward-line) (point)))
1461 (end (progn (org-skip-whitespace)
1462 (if (eobp) (point) (point-at-bol)))))
1468 :post-blank
,(count-lines pos-before-blank end
))))))
1470 (defun org-element-keyword-interpreter (keyword contents
)
1471 "Interpret KEYWORD element as Org syntax.
1474 (org-element-property :key keyword
)
1475 (org-element-property :value keyword
)))
1478 ;;;; Latex Environment
1480 (defun org-element-latex-environment-parser ()
1481 "Parse a LaTeX environment.
1483 Return a list whose CAR is `latex-environment' and CDR is a plist
1484 containing `:begin', `:end', `:value' and `:post-blank'
1487 Assume point is at the beginning of the latex environment."
1489 (let* ((case-fold-search t
)
1490 (code-begin (point))
1491 (keywords (org-element-collect-affiliated-keywords))
1492 (begin (car keywords
))
1493 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
1494 (regexp-quote (match-string 1))))
1496 (progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env
))
1499 (value (buffer-substring-no-properties code-begin code-end
))
1500 (end (progn (org-skip-whitespace)
1501 (if (eobp) (point) (point-at-bol)))))
1506 :post-blank
,(count-lines code-end end
)
1507 ,@(cadr keywords
))))))
1509 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1510 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1512 (org-element-property :value latex-environment
))
1517 (defun org-element-paragraph-parser ()
1520 Return a list whose CAR is `paragraph' and CDR is a plist
1521 containing `:begin', `:end', `:contents-begin' and
1522 `:contents-end' and `:post-blank' keywords.
1524 Assume point is at the beginning of the paragraph."
1526 (let* ((contents-begin (point))
1527 (keywords (org-element-collect-affiliated-keywords))
1528 (begin (car keywords
))
1530 (progn (end-of-line)
1531 (if (re-search-forward org-element-paragraph-separate nil
'm
)
1532 (progn (forward-line -
1) (end-of-line) (point))
1534 (pos-before-blank (progn (forward-line) (point)))
1535 (end (progn (org-skip-whitespace)
1536 (if (eobp) (point) (point-at-bol)))))
1540 :contents-begin
,contents-begin
1541 :contents-end
,contents-end
1542 :post-blank
,(count-lines pos-before-blank end
)
1543 ,@(cadr keywords
))))))
1545 (defun org-element-paragraph-interpreter (paragraph contents
)
1546 "Interpret PARAGRAPH element as Org syntax.
1547 CONTENTS is the contents of the element."
1553 (defun org-element-planning-parser ()
1556 Return a list whose CAR is `planning' and CDR is a plist
1557 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1558 and `:post-blank' keywords."
1560 (let* ((case-fold-search nil
)
1562 (post-blank (let ((before-blank (progn (forward-line) (point))))
1563 (org-skip-whitespace)
1564 (unless (eobp) (beginning-of-line))
1565 (count-lines before-blank
(point))))
1567 closed deadline scheduled
)
1569 (while (re-search-forward org-keyword-time-not-clock-regexp
1570 (line-end-position) t
)
1571 (goto-char (match-end 1))
1572 (org-skip-whitespace)
1573 (let ((time (buffer-substring-no-properties (point) (match-end 0)))
1574 (keyword (match-string 1)))
1575 (cond ((equal keyword org-closed-string
) (setq closed time
))
1576 ((equal keyword org-deadline-string
) (setq deadline time
))
1577 (t (setq scheduled time
)))))
1581 :scheduled
,scheduled
1584 :post-blank
,post-blank
)))))
1586 (defun org-element-planning-interpreter (planning contents
)
1587 "Interpret PLANNING element as Org syntax.
1592 (list (let ((closed (org-element-property :closed planning
)))
1593 (when closed
(concat org-closed-string
" " closed
)))
1594 (let ((deadline (org-element-property :deadline planning
)))
1595 (when deadline
(concat org-deadline-string
" " deadline
)))
1596 (let ((scheduled (org-element-property :scheduled planning
)))
1597 (when scheduled
(concat org-scheduled-string
" " scheduled
)))))
1601 ;;;; Property Drawer
1603 (defun org-element-property-drawer-parser ()
1604 "Parse a property drawer.
1606 Return a list whose CAR is `property-drawer' and CDR is a plist
1607 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1608 `:contents-end', `:properties' and `:post-blank' keywords.
1610 Assume point is at the beginning of the property drawer."
1612 (let ((case-fold-search t
)
1614 (prop-begin (progn (forward-line) (point)))
1615 (hidden (org-truely-invisible-p))
1618 (while (not (looking-at "^[ \t]*:END:"))
1619 (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
1620 (push (cons (org-match-string-no-properties 1)
1622 (buffer-substring-no-properties
1623 (match-end 0) (point-at-eol))))
1627 (prop-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
1629 (pos-before-blank (progn (forward-line) (point)))
1630 (end (progn (org-skip-whitespace)
1631 (if (eobp) (point) (point-at-bol)))))
1636 :properties
,properties
1637 :post-blank
,(count-lines pos-before-blank end
))))))
1639 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1640 "Interpret PROPERTY-DRAWER element as Org syntax.
1642 (let ((props (org-element-property :properties property-drawer
)))
1645 (mapconcat (lambda (p)
1646 (format org-property-format
(format ":%s:" (car p
)) (cdr p
)))
1647 (nreverse props
) "\n")
1653 (defun org-element-quote-section-parser ()
1654 "Parse a quote section.
1656 Return a list whose CAR is `quote-section' and CDR is a plist
1657 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1659 Assume point is at beginning of the section."
1661 (let* ((begin (point))
1662 (end (progn (org-with-limited-levels (outline-next-heading))
1664 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1667 (value (buffer-substring-no-properties begin pos-before-blank
)))
1672 :post-blank
,(count-lines pos-before-blank end
))))))
1674 (defun org-element-quote-section-interpreter (quote-section contents
)
1675 "Interpret QUOTE-SECTION element as Org syntax.
1677 (org-element-property :value quote-section
))
1682 (defun org-element-src-block-parser ()
1685 Return a list whose CAR is `src-block' and CDR is a plist
1686 containing `:language', `:switches', `:parameters', `:begin',
1687 `:end', `:hiddenp', `:number-lines', `:retain-labels',
1688 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
1689 `:post-blank' keywords.
1691 Assume point is at the beginning of the block."
1693 (let* ((case-fold-search t
)
1694 (contents-begin (point))
1695 ;; Get affiliated keywords.
1696 (keywords (org-element-collect-affiliated-keywords))
1697 ;; Get beginning position.
1698 (begin (car keywords
))
1699 ;; Get language as a string.
1703 (concat "^[ \t]*#\\+BEGIN_SRC"
1704 "\\(?: +\\(\\S-+\\)\\)?"
1705 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
1707 (org-match-string-no-properties 1)))
1709 (switches (org-match-string-no-properties 2))
1711 (parameters (org-match-string-no-properties 3))
1712 ;; Switches analysis
1713 (number-lines (cond ((not switches
) nil
)
1714 ((string-match "-n\\>" switches
) 'new
)
1715 ((string-match "+n\\>" switches
) 'continued
)))
1716 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1717 (label-fmt (and switches
1718 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1719 (match-string 1 switches
)))
1720 ;; Should labels be retained in (or stripped from) src
1724 (not (string-match "-r\\>" switches
))
1725 (and number-lines
(string-match "-k\\>" switches
))))
1726 ;; What should code-references use - labels or
1730 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1731 ;; Get position at end of block.
1732 (contents-end (progn (re-search-forward "^[ \t]*#\\+END_SRC" nil t
)
1736 (value (buffer-substring-no-properties
1737 (save-excursion (goto-char contents-begin
)
1740 (match-beginning 0)))
1741 ;; Get position after ending blank lines.
1742 (end (progn (org-skip-whitespace)
1743 (if (eobp) (point) (point-at-bol))))
1744 ;; Get visibility status.
1745 (hidden (progn (goto-char contents-begin
)
1747 (org-truely-invisible-p))))
1749 (:language
,language
1750 :switches
,(and (org-string-nw-p switches
)
1751 (org-trim switches
))
1752 :parameters
,(and (org-string-nw-p parameters
)
1753 (org-trim parameters
))
1756 :number-lines
,number-lines
1757 :preserve-indent
,preserve-indent
1758 :retain-labels
,retain-labels
1759 :use-labels
,use-labels
1760 :label-fmt
,label-fmt
1763 :post-blank
,(count-lines contents-end end
)
1764 ,@(cadr keywords
))))))
1766 (defun org-element-src-block-interpreter (src-block contents
)
1767 "Interpret SRC-BLOCK element as Org syntax.
1769 (let ((lang (org-element-property :language src-block
))
1770 (switches (org-element-property :switches src-block
))
1771 (params (org-element-property :parameters src-block
))
1772 (value (let ((val (org-element-property :value src-block
)))
1775 (org-src-preserve-indentation val
)
1776 ((zerop org-edit-src-content-indentation
)
1777 (org-remove-indentation val
))
1779 (let ((ind (make-string
1780 org-edit-src-content-indentation
32)))
1781 (replace-regexp-in-string
1782 "\\(^\\)[ \t]*\\S-" ind
1783 (org-remove-indentation val
) nil nil
1)))))))
1784 (concat (format "#+BEGIN_SRC%s\n"
1785 (concat (and lang
(concat " " lang
))
1786 (and switches
(concat " " switches
))
1787 (and params
(concat " " params
))))
1794 (defun org-element-table-parser ()
1795 "Parse a table at point.
1797 Return a list whose CAR is `table' and CDR is a plist containing
1798 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
1799 `:contents-end', `:value' and `:post-blank' keywords.
1801 Assume point is at the beginning of the table."
1803 (let* ((case-fold-search t
)
1804 (table-begin (point))
1805 (type (if (org-at-table.el-p
) 'table.el
'org
))
1806 (keywords (org-element-collect-affiliated-keywords))
1807 (begin (car keywords
))
1808 (table-end (goto-char (marker-position (org-table-end t
))))
1810 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
1811 (push (org-match-string-no-properties 1) acc
)
1814 (pos-before-blank (point))
1815 (end (progn (org-skip-whitespace)
1816 (if (eobp) (point) (point-at-bol)))))
1822 ;; Only `org' tables have contents. `table.el' tables
1823 ;; use a `:value' property to store raw table as
1825 :contents-begin
,(and (eq type
'org
) table-begin
)
1826 :contents-end
,(and (eq type
'org
) table-end
)
1827 :value
,(and (eq type
'table.el
)
1828 (buffer-substring-no-properties
1829 table-begin table-end
))
1830 :post-blank
,(count-lines pos-before-blank end
)
1831 ,@(cadr keywords
))))))
1833 (defun org-element-table-interpreter (table contents
)
1834 "Interpret TABLE element as Org syntax.
1836 (if (eq (org-element-property :type table
) 'table.el
)
1837 (org-remove-indentation (org-element-property :value table
))
1838 (concat (with-temp-buffer (insert contents
)
1841 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
1842 (reverse (org-element-property :tblfm table
))
1848 (defun org-element-table-row-parser ()
1849 "Parse table row at point.
1851 Return a list whose CAR is `table-row' and CDR is a plist
1852 containing `:begin', `:end', `:contents-begin', `:contents-end',
1853 `:type' and `:post-blank' keywords."
1855 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
1857 ;; A table rule has no contents. In that case, ensure
1858 ;; CONTENTS-BEGIN matches CONTENTS-END.
1859 (contents-begin (if (eq type
'standard
)
1860 (progn (search-forward "|") (point))
1862 (skip-chars-backward " \r\t\n")
1864 (contents-end (progn (end-of-line)
1865 (skip-chars-backward " \r\t\n")
1867 (end (progn (forward-line) (point))))
1872 :contents-begin
,contents-begin
1873 :contents-end
,contents-end
1876 (defun org-element-table-row-interpreter (table-row contents
)
1877 "Interpret TABLE-ROW element as Org syntax.
1878 CONTENTS is the contents of the table row."
1879 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
1880 (concat "| " contents
)))
1885 (defun org-element-verse-block-parser ()
1886 "Parse a verse block.
1888 Return a list whose CAR is `verse-block' and CDR is a plist
1889 containing `:begin', `:end', `:contents-begin', `:contents-end',
1890 `:hiddenp' and `:post-blank' keywords.
1892 Assume point is at beginning of the block."
1894 (let* ((case-fold-search t
)
1895 (keywords (org-element-collect-affiliated-keywords))
1896 (begin (car keywords
))
1897 (hidden (progn (forward-line) (org-truely-invisible-p)))
1898 (contents-begin (point))
1901 (re-search-forward (concat "^[ \t]*#\\+END_VERSE") nil t
)
1903 (pos-before-blank (progn (forward-line) (point)))
1904 (end (progn (org-skip-whitespace)
1905 (if (eobp) (point) (point-at-bol)))))
1909 :contents-begin
,contents-begin
1910 :contents-end
,contents-end
1912 :post-blank
,(count-lines pos-before-blank end
)
1913 ,@(cadr keywords
))))))
1915 (defun org-element-verse-block-interpreter (verse-block contents
)
1916 "Interpret VERSE-BLOCK element as Org syntax.
1917 CONTENTS is verse block contents."
1918 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
1924 ;; Unlike to elements, interstices can be found between objects.
1925 ;; That's why, along with the parser, successor functions are provided
1926 ;; for each object. Some objects share the same successor (i.e. `code'
1927 ;; and `verbatim' objects).
1929 ;; A successor must accept a single argument bounding the search. It
1930 ;; will return either a cons cell whose CAR is the object's type, as
1931 ;; a symbol, and CDR the position of its next occurrence, or nil.
1933 ;; Successors follow the naming convention:
1934 ;; org-element-NAME-successor, where NAME is the name of the
1935 ;; successor, as defined in `org-element-all-successors'.
1937 ;; Some object types (i.e. `emphasis') are recursive. Restrictions on
1938 ;; object types they can contain will be specified in
1939 ;; `org-element-object-restrictions'.
1941 ;; Adding a new type of object is simple. Implement a successor,
1942 ;; a parser, and an interpreter for it, all following the naming
1943 ;; convention. Register type in `org-element-all-objects' and
1944 ;; successor in `org-element-all-successors'. Maybe tweak
1945 ;; restrictions about it, and that's it.
1950 (defun org-element-bold-parser ()
1951 "Parse bold object at point.
1953 Return a list whose CAR is `bold' and CDR is a plist with
1954 `:begin', `:end', `:contents-begin' and `:contents-end' and
1955 `:post-blank' keywords.
1957 Assume point is at the first star marker."
1959 (unless (bolp) (backward-char 1))
1960 (looking-at org-emph-re
)
1961 (let ((begin (match-beginning 2))
1962 (contents-begin (match-beginning 4))
1963 (contents-end (match-end 4))
1964 (post-blank (progn (goto-char (match-end 2))
1965 (skip-chars-forward " \t")))
1970 :contents-begin
,contents-begin
1971 :contents-end
,contents-end
1972 :post-blank
,post-blank
)))))
1974 (defun org-element-bold-interpreter (bold contents
)
1975 "Interpret BOLD object as Org syntax.
1976 CONTENTS is the contents of the object."
1977 (format "*%s*" contents
))
1979 (defun org-element-text-markup-successor (limit)
1980 "Search for the next text-markup object.
1982 LIMIT bounds the search.
1984 Return value is a cons cell whose CAR is a symbol among `bold',
1985 `italic', `underline', `strike-through', `code' and `verbatim'
1986 and CDR is beginning position."
1988 (unless (bolp) (backward-char))
1989 (when (re-search-forward org-emph-re limit t
)
1990 (let ((marker (match-string 3)))
1992 ((equal marker
"*") 'bold
)
1993 ((equal marker
"/") 'italic
)
1994 ((equal marker
"_") 'underline
)
1995 ((equal marker
"+") 'strike-through
)
1996 ((equal marker
"~") 'code
)
1997 ((equal marker
"=") 'verbatim
)
1998 (t (error "Unknown marker at %d" (match-beginning 3))))
1999 (match-beginning 2))))))
2004 (defun org-element-code-parser ()
2005 "Parse code object at point.
2007 Return a list whose CAR is `code' and CDR is a plist with
2008 `:value', `:begin', `:end' and `:post-blank' keywords.
2010 Assume point is at the first tilde marker."
2012 (unless (bolp) (backward-char 1))
2013 (looking-at org-emph-re
)
2014 (let ((begin (match-beginning 2))
2015 (value (org-match-string-no-properties 4))
2016 (post-blank (progn (goto-char (match-end 2))
2017 (skip-chars-forward " \t")))
2023 :post-blank
,post-blank
)))))
2025 (defun org-element-code-interpreter (code contents
)
2026 "Interpret CODE object as Org syntax.
2028 (format "~%s~" (org-element-property :value code
)))
2033 (defun org-element-entity-parser ()
2034 "Parse entity at point.
2036 Return a list whose CAR is `entity' and CDR a plist with
2037 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
2038 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
2041 Assume point is at the beginning of the entity."
2043 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
2044 (let* ((value (org-entity-get (match-string 1)))
2045 (begin (match-beginning 0))
2046 (bracketsp (string= (match-string 2) "{}"))
2047 (post-blank (progn (goto-char (match-end 1))
2048 (when bracketsp
(forward-char 2))
2049 (skip-chars-forward " \t")))
2053 :latex
,(nth 1 value
)
2054 :latex-math-p
,(nth 2 value
)
2055 :html
,(nth 3 value
)
2056 :ascii
,(nth 4 value
)
2057 :latin1
,(nth 5 value
)
2058 :utf-8
,(nth 6 value
)
2061 :use-brackets-p
,bracketsp
2062 :post-blank
,post-blank
)))))
2064 (defun org-element-entity-interpreter (entity contents
)
2065 "Interpret ENTITY object as Org syntax.
2068 (org-element-property :name entity
)
2069 (when (org-element-property :use-brackets-p entity
) "{}")))
2071 (defun org-element-latex-or-entity-successor (limit)
2072 "Search for the next latex-fragment or entity object.
2074 LIMIT bounds the search.
2076 Return value is a cons cell whose CAR is `entity' or
2077 `latex-fragment' and CDR is beginning position."
2079 (let ((matchers (plist-get org-format-latex-options
:matchers
))
2080 ;; ENTITY-RE matches both LaTeX commands and Org entities.
2082 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
2083 (when (re-search-forward
2084 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
2088 (goto-char (match-beginning 0))
2089 (if (looking-at entity-re
)
2090 ;; Determine if it's a real entity or a LaTeX command.
2091 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
2092 (match-beginning 0))
2093 ;; No entity nor command: point is at a LaTeX fragment.
2094 ;; Determine its type to get the correct beginning position.
2095 (cons 'latex-fragment
2098 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
2101 (nth 2 (assoc e org-latex-regexps
))))))
2108 (defun org-element-export-snippet-parser ()
2109 "Parse export snippet at point.
2111 Return a list whose CAR is `export-snippet' and CDR a plist with
2112 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
2115 Assume point is at the beginning of the snippet."
2117 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t
)
2118 (let* ((begin (match-beginning 0))
2119 (back-end (org-match-string-no-properties 1))
2120 (value (buffer-substring-no-properties
2122 (progn (re-search-forward "@@" nil t
) (match-beginning 0))))
2123 (post-blank (skip-chars-forward " \t"))
2126 (:back-end
,back-end
2130 :post-blank
,post-blank
)))))
2132 (defun org-element-export-snippet-interpreter (export-snippet contents
)
2133 "Interpret EXPORT-SNIPPET object as Org syntax.
2136 (org-element-property :back-end export-snippet
)
2137 (org-element-property :value export-snippet
)))
2139 (defun org-element-export-snippet-successor (limit)
2140 "Search for the next export-snippet object.
2142 LIMIT bounds the search.
2144 Return value is a cons cell whose CAR is `export-snippet' and CDR
2145 its beginning position."
2148 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t
)
2149 (setq beg
(match-beginning 0))
2150 (re-search-forward "@@" limit t
))
2151 (cons 'export-snippet beg
)))))
2154 ;;;; Footnote Reference
2156 (defun org-element-footnote-reference-parser ()
2157 "Parse footnote reference at point.
2159 Return a list whose CAR is `footnote-reference' and CDR a plist
2160 with `:label', `:type', `:inline-definition', `:begin', `:end'
2161 and `:post-blank' as keywords."
2163 (looking-at org-footnote-re
)
2164 (let* ((begin (point))
2165 (label (or (org-match-string-no-properties 2)
2166 (org-match-string-no-properties 3)
2167 (and (match-string 1)
2168 (concat "fn:" (org-match-string-no-properties 1)))))
2169 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
2170 (inner-begin (match-end 0))
2174 (while (and (> count
0) (re-search-forward "[][]" nil t
))
2175 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
2177 (post-blank (progn (goto-char (1+ inner-end
))
2178 (skip-chars-forward " \t")))
2181 (and (eq type
'inline
)
2182 (org-element-parse-secondary-string
2183 (buffer-substring inner-begin inner-end
)
2184 (org-element-restriction 'footnote-reference
)))))
2185 `(footnote-reference
2188 :inline-definition
,inline-definition
2191 :post-blank
,post-blank
)))))
2193 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2194 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2196 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2199 (org-element-property :inline-definition footnote-reference
)))
2200 (if (not inline-def
) ""
2201 (concat ":" (org-element-interpret-data inline-def
))))))
2202 (format "[%s]" (concat label def
))))
2204 (defun org-element-footnote-reference-successor (limit)
2205 "Search for the next footnote-reference object.
2207 LIMIT bounds the search.
2209 Return value is a cons cell whose CAR is `footnote-reference' and
2210 CDR is beginning position."
2213 (while (re-search-forward org-footnote-re limit t
)
2215 (let ((beg (match-beginning 0))
2218 (while (re-search-forward "[][]" limit t
)
2219 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2221 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2224 ;;;; Inline Babel Call
2226 (defun org-element-inline-babel-call-parser ()
2227 "Parse inline babel call at point.
2229 Return a list whose CAR is `inline-babel-call' and CDR a plist
2230 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2232 Assume point is at the beginning of the babel call."
2234 (unless (bolp) (backward-char))
2235 (looking-at org-babel-inline-lob-one-liner-regexp
)
2236 (let ((info (save-match-data (org-babel-lob-get-info)))
2237 (begin (match-end 1))
2238 (post-blank (progn (goto-char (match-end 0))
2239 (skip-chars-forward " \t")))
2245 :post-blank
,post-blank
)))))
2247 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2248 "Interpret INLINE-BABEL-CALL object as Org syntax.
2250 (let* ((babel-info (org-element-property :info inline-babel-call
))
2251 (main-source (car babel-info
))
2252 (post-options (nth 1 babel-info
)))
2254 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2255 ;; Remove redundant square brackets.
2257 (match-string 1 main-source
) nil nil main-source
)
2259 (and post-options
(format "[%s]" post-options
)))))
2261 (defun org-element-inline-babel-call-successor (limit)
2262 "Search for the next inline-babel-call object.
2264 LIMIT bounds the search.
2266 Return value is a cons cell whose CAR is `inline-babel-call' and
2267 CDR is beginning position."
2269 ;; Use a simplified version of
2270 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
2271 (when (re-search-forward
2272 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
2274 (cons 'inline-babel-call
(match-beginning 0)))))
2277 ;;;; Inline Src Block
2279 (defun org-element-inline-src-block-parser ()
2280 "Parse inline source block at point.
2282 Return a list whose CAR is `inline-src-block' and CDR a plist
2283 with `:begin', `:end', `:language', `:value', `:parameters' and
2284 `:post-blank' as keywords.
2286 Assume point is at the beginning of the inline src block."
2288 (unless (bolp) (backward-char))
2289 (looking-at org-babel-inline-src-block-regexp
)
2290 (let ((begin (match-beginning 1))
2291 (language (org-match-string-no-properties 2))
2292 (parameters (org-match-string-no-properties 4))
2293 (value (org-match-string-no-properties 5))
2294 (post-blank (progn (goto-char (match-end 0))
2295 (skip-chars-forward " \t")))
2298 (:language
,language
2300 :parameters
,parameters
2303 :post-blank
,post-blank
)))))
2305 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2306 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2308 (let ((language (org-element-property :language inline-src-block
))
2309 (arguments (org-element-property :parameters inline-src-block
))
2310 (body (org-element-property :value inline-src-block
)))
2311 (format "src_%s%s{%s}"
2313 (if arguments
(format "[%s]" arguments
) "")
2316 (defun org-element-inline-src-block-successor (limit)
2317 "Search for the next inline-babel-call element.
2319 LIMIT bounds the search.
2321 Return value is a cons cell whose CAR is `inline-babel-call' and
2322 CDR is beginning position."
2324 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2325 (cons 'inline-src-block
(match-beginning 1)))))
2329 (defun org-element-italic-parser ()
2330 "Parse italic object at point.
2332 Return a list whose CAR is `italic' and CDR is a plist with
2333 `:begin', `:end', `:contents-begin' and `:contents-end' and
2334 `:post-blank' keywords.
2336 Assume point is at the first slash marker."
2338 (unless (bolp) (backward-char 1))
2339 (looking-at org-emph-re
)
2340 (let ((begin (match-beginning 2))
2341 (contents-begin (match-beginning 4))
2342 (contents-end (match-end 4))
2343 (post-blank (progn (goto-char (match-end 2))
2344 (skip-chars-forward " \t")))
2349 :contents-begin
,contents-begin
2350 :contents-end
,contents-end
2351 :post-blank
,post-blank
)))))
2353 (defun org-element-italic-interpreter (italic contents
)
2354 "Interpret ITALIC object as Org syntax.
2355 CONTENTS is the contents of the object."
2356 (format "/%s/" contents
))
2361 (defun org-element-latex-fragment-parser ()
2362 "Parse latex fragment at point.
2364 Return a list whose CAR is `latex-fragment' and CDR a plist with
2365 `:value', `:begin', `:end', and `:post-blank' as keywords.
2367 Assume point is at the beginning of the latex fragment."
2369 (let* ((begin (point))
2373 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2374 (when (or (looking-at latex-regexp
)
2378 (looking-at latex-regexp
))))
2379 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2380 (plist-get org-format-latex-options
:matchers
))
2381 ;; None found: it's a macro.
2382 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2384 (value (match-string-no-properties substring-match
))
2385 (post-blank (progn (goto-char (match-end substring-match
))
2386 (skip-chars-forward " \t")))
2392 :post-blank
,post-blank
)))))
2394 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2395 "Interpret LATEX-FRAGMENT object as Org syntax.
2397 (org-element-property :value latex-fragment
))
2401 (defun org-element-line-break-parser ()
2402 "Parse line break at point.
2404 Return a list whose CAR is `line-break', and CDR a plist with
2405 `:begin', `:end' and `:post-blank' keywords.
2407 Assume point is at the beginning of the line break."
2408 (let ((begin (point))
2409 (end (save-excursion (forward-line) (point))))
2410 `(line-break (:begin
,begin
:end
,end
:post-blank
0))))
2412 (defun org-element-line-break-interpreter (line-break contents
)
2413 "Interpret LINE-BREAK object as Org syntax.
2417 (defun org-element-line-break-successor (limit)
2418 "Search for the next line-break object.
2420 LIMIT bounds the search.
2422 Return value is a cons cell whose CAR is `line-break' and CDR is
2423 beginning position."
2425 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2426 (goto-char (match-beginning 1)))))
2427 ;; A line break can only happen on a non-empty line.
2428 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2429 (cons 'line-break beg
)))))
2434 (defun org-element-link-parser ()
2435 "Parse link at point.
2437 Return a list whose CAR is `link' and CDR a plist with `:type',
2438 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2439 `:contents-end' and `:post-blank' as keywords.
2441 Assume point is at the beginning of the link."
2443 (let ((begin (point))
2444 end contents-begin contents-end link-end post-blank path type
2447 ;; Type 1: Text targeted from a radio target.
2448 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2450 link-end
(match-end 0)
2451 path
(org-match-string-no-properties 0)))
2452 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2453 ((looking-at org-bracket-link-regexp
)
2454 (setq contents-begin
(match-beginning 3)
2455 contents-end
(match-end 3)
2456 link-end
(match-end 0)
2457 ;; RAW-LINK is the original link.
2458 raw-link
(org-match-string-no-properties 1)
2459 link
(org-translate-link
2460 (org-link-expand-abbrev
2461 (org-link-unescape raw-link
))))
2462 ;; Determine TYPE of link and set PATH accordingly.
2465 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2466 (setq type
"file" path link
))
2467 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2468 ((string-match org-link-re-with-space3 link
)
2469 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2470 ;; Id type: PATH is the id.
2471 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2472 (setq type
"id" path
(match-string 1 link
)))
2473 ;; Code-ref type: PATH is the name of the reference.
2474 ((string-match "^(\\(.*\\))$" link
)
2475 (setq type
"coderef" path
(match-string 1 link
)))
2476 ;; Custom-id type: PATH is the name of the custom id.
2477 ((= (aref link
0) ?
#)
2478 (setq type
"custom-id" path
(substring link
1)))
2479 ;; Fuzzy type: Internal link either matches a target, an
2480 ;; headline name or nothing. PATH is the target or
2482 (t (setq type
"fuzzy" path link
))))
2483 ;; Type 3: Plain link, i.e. http://orgmode.org
2484 ((looking-at org-plain-link-re
)
2485 (setq raw-link
(org-match-string-no-properties 0)
2486 type
(org-match-string-no-properties 1)
2487 path
(org-match-string-no-properties 2)
2488 link-end
(match-end 0)))
2489 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2490 ((looking-at org-angle-link-re
)
2491 (setq raw-link
(buffer-substring-no-properties
2492 (match-beginning 1) (match-end 2))
2493 type
(org-match-string-no-properties 1)
2494 path
(org-match-string-no-properties 2)
2495 link-end
(match-end 0))))
2496 ;; In any case, deduce end point after trailing white space from
2497 ;; LINK-END variable.
2498 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2503 :raw-link
,(or raw-link path
)
2506 :contents-begin
,contents-begin
2507 :contents-end
,contents-end
2508 :post-blank
,post-blank
)))))
2510 (defun org-element-link-interpreter (link contents
)
2511 "Interpret LINK object as Org syntax.
2512 CONTENTS is the contents of the object, or nil."
2513 (let ((type (org-element-property :type link
))
2514 (raw-link (org-element-property :raw-link link
)))
2515 (if (string= type
"radio") raw-link
2518 (if contents
(format "[%s]" contents
) "")))))
2520 (defun org-element-link-successor (limit)
2521 "Search for the next link object.
2523 LIMIT bounds the search.
2525 Return value is a cons cell whose CAR is `link' and CDR is
2526 beginning position."
2529 (if (not org-target-link-regexp
) org-any-link-re
2530 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2531 (when (re-search-forward link-regexp limit t
)
2532 (cons 'link
(match-beginning 0))))))
2537 (defun org-element-macro-parser ()
2538 "Parse macro at point.
2540 Return a list whose CAR is `macro' and CDR a plist with `:key',
2541 `:args', `:begin', `:end', `:value' and `:post-blank' as
2544 Assume point is at the macro."
2546 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2547 (let ((begin (point))
2548 (key (downcase (org-match-string-no-properties 1)))
2549 (value (org-match-string-no-properties 0))
2550 (post-blank (progn (goto-char (match-end 0))
2551 (skip-chars-forward " \t")))
2553 (args (let ((args (org-match-string-no-properties 3)) args2
)
2555 (setq args
(org-split-string args
","))
2557 (while (string-match "\\\\\\'" (car args
))
2558 ;; Repair bad splits.
2559 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2562 (push (pop args
) args2
))
2563 (mapcar 'org-trim
(nreverse args2
))))))
2570 :post-blank
,post-blank
)))))
2572 (defun org-element-macro-interpreter (macro contents
)
2573 "Interpret MACRO object as Org syntax.
2575 (org-element-property :value macro
))
2577 (defun org-element-macro-successor (limit)
2578 "Search for the next macro object.
2580 LIMIT bounds the search.
2582 Return value is cons cell whose CAR is `macro' and CDR is
2583 beginning position."
2585 (when (re-search-forward
2586 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2588 (cons 'macro
(match-beginning 0)))))
2593 (defun org-element-radio-target-parser ()
2594 "Parse radio target at point.
2596 Return a list whose CAR is `radio-target' and CDR a plist with
2597 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2598 and `:post-blank' as keywords.
2600 Assume point is at the radio target."
2602 (looking-at org-radio-target-regexp
)
2603 (let ((begin (point))
2604 (contents-begin (match-beginning 1))
2605 (contents-end (match-end 1))
2606 (value (org-match-string-no-properties 1))
2607 (post-blank (progn (goto-char (match-end 0))
2608 (skip-chars-forward " \t")))
2613 :contents-begin
,contents-begin
2614 :contents-end
,contents-end
2615 :post-blank
,post-blank
2618 (defun org-element-radio-target-interpreter (target contents
)
2619 "Interpret TARGET object as Org syntax.
2620 CONTENTS is the contents of the object."
2621 (concat "<<<" contents
">>>"))
2623 (defun org-element-radio-target-successor (limit)
2624 "Search for the next radio-target object.
2626 LIMIT bounds the search.
2628 Return value is a cons cell whose CAR is `radio-target' and CDR
2629 is beginning position."
2631 (when (re-search-forward org-radio-target-regexp limit t
)
2632 (cons 'radio-target
(match-beginning 0)))))
2635 ;;;; Statistics Cookie
2637 (defun org-element-statistics-cookie-parser ()
2638 "Parse statistics cookie at point.
2640 Return a list whose CAR is `statistics-cookie', and CDR a plist
2641 with `:begin', `:end', `:value' and `:post-blank' keywords.
2643 Assume point is at the beginning of the statistics-cookie."
2645 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2646 (let* ((begin (point))
2647 (value (buffer-substring-no-properties
2648 (match-beginning 0) (match-end 0)))
2649 (post-blank (progn (goto-char (match-end 0))
2650 (skip-chars-forward " \t")))
2656 :post-blank
,post-blank
)))))
2658 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
2659 "Interpret STATISTICS-COOKIE object as Org syntax.
2661 (org-element-property :value statistics-cookie
))
2663 (defun org-element-statistics-cookie-successor (limit)
2664 "Search for the next statistics cookie object.
2666 LIMIT bounds the search.
2668 Return value is a cons cell whose CAR is `statistics-cookie' and
2669 CDR is beginning position."
2671 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
2672 (cons 'statistics-cookie
(match-beginning 0)))))
2677 (defun org-element-strike-through-parser ()
2678 "Parse strike-through object at point.
2680 Return a list whose CAR is `strike-through' and CDR is a plist
2681 with `:begin', `:end', `:contents-begin' and `:contents-end' and
2682 `:post-blank' keywords.
2684 Assume point is at the first plus sign marker."
2686 (unless (bolp) (backward-char 1))
2687 (looking-at org-emph-re
)
2688 (let ((begin (match-beginning 2))
2689 (contents-begin (match-beginning 4))
2690 (contents-end (match-end 4))
2691 (post-blank (progn (goto-char (match-end 2))
2692 (skip-chars-forward " \t")))
2697 :contents-begin
,contents-begin
2698 :contents-end
,contents-end
2699 :post-blank
,post-blank
)))))
2701 (defun org-element-strike-through-interpreter (strike-through contents
)
2702 "Interpret STRIKE-THROUGH object as Org syntax.
2703 CONTENTS is the contents of the object."
2704 (format "+%s+" contents
))
2709 (defun org-element-subscript-parser ()
2710 "Parse subscript at point.
2712 Return a list whose CAR is `subscript' and CDR a plist with
2713 `:begin', `:end', `:contents-begin', `:contents-end',
2714 `:use-brackets-p' and `:post-blank' as keywords.
2716 Assume point is at the underscore."
2718 (unless (bolp) (backward-char))
2719 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2721 (not (looking-at org-match-substring-regexp
))))
2722 (begin (match-beginning 2))
2723 (contents-begin (or (match-beginning 5)
2724 (match-beginning 3)))
2725 (contents-end (or (match-end 5) (match-end 3)))
2726 (post-blank (progn (goto-char (match-end 0))
2727 (skip-chars-forward " \t")))
2732 :use-brackets-p
,bracketsp
2733 :contents-begin
,contents-begin
2734 :contents-end
,contents-end
2735 :post-blank
,post-blank
)))))
2737 (defun org-element-subscript-interpreter (subscript contents
)
2738 "Interpret SUBSCRIPT object as Org syntax.
2739 CONTENTS is the contents of the object."
2741 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
2744 (defun org-element-sub/superscript-successor
(limit)
2745 "Search for the next sub/superscript object.
2747 LIMIT bounds the search.
2749 Return value is a cons cell whose CAR is either `subscript' or
2750 `superscript' and CDR is beginning position."
2752 (when (re-search-forward org-match-substring-regexp limit t
)
2753 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
2754 (match-beginning 2)))))
2759 (defun org-element-superscript-parser ()
2760 "Parse superscript at point.
2762 Return a list whose CAR is `superscript' and CDR a plist with
2763 `:begin', `:end', `:contents-begin', `:contents-end',
2764 `:use-brackets-p' and `:post-blank' as keywords.
2766 Assume point is at the caret."
2768 (unless (bolp) (backward-char))
2769 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
2770 (not (looking-at org-match-substring-regexp
))))
2771 (begin (match-beginning 2))
2772 (contents-begin (or (match-beginning 5)
2773 (match-beginning 3)))
2774 (contents-end (or (match-end 5) (match-end 3)))
2775 (post-blank (progn (goto-char (match-end 0))
2776 (skip-chars-forward " \t")))
2781 :use-brackets-p
,bracketsp
2782 :contents-begin
,contents-begin
2783 :contents-end
,contents-end
2784 :post-blank
,post-blank
)))))
2786 (defun org-element-superscript-interpreter (superscript contents
)
2787 "Interpret SUPERSCRIPT object as Org syntax.
2788 CONTENTS is the contents of the object."
2790 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
2796 (defun org-element-table-cell-parser ()
2797 "Parse table cell at point.
2799 Return a list whose CAR is `table-cell' and CDR is a plist
2800 containing `:begin', `:end', `:contents-begin', `:contents-end'
2801 and `:post-blank' keywords."
2802 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
2803 (let* ((begin (match-beginning 0))
2805 (contents-begin (match-beginning 1))
2806 (contents-end (match-end 1)))
2810 :contents-begin
,contents-begin
2811 :contents-end
,contents-end
2814 (defun org-element-table-cell-interpreter (table-cell contents
)
2815 "Interpret TABLE-CELL element as Org syntax.
2816 CONTENTS is the contents of the cell, or nil."
2817 (concat " " contents
" |"))
2819 (defun org-element-table-cell-successor (limit)
2820 "Search for the next table-cell object.
2822 LIMIT bounds the search.
2824 Return value is a cons cell whose CAR is `table-cell' and CDR is
2825 beginning position."
2826 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
2831 (defun org-element-target-parser ()
2832 "Parse target at point.
2834 Return a list whose CAR is `target' and CDR a plist with
2835 `:begin', `:end', `:value' and `:post-blank' as keywords.
2837 Assume point is at the target."
2839 (looking-at org-target-regexp
)
2840 (let ((begin (point))
2841 (value (org-match-string-no-properties 1))
2842 (post-blank (progn (goto-char (match-end 0))
2843 (skip-chars-forward " \t")))
2849 :post-blank
,post-blank
)))))
2851 (defun org-element-target-interpreter (target contents
)
2852 "Interpret TARGET object as Org syntax.
2854 (format "<<%s>>" (org-element-property :value target
)))
2856 (defun org-element-target-successor (limit)
2857 "Search for the next target object.
2859 LIMIT bounds the search.
2861 Return value is a cons cell whose CAR is `target' and CDR is
2862 beginning position."
2864 (when (re-search-forward org-target-regexp limit t
)
2865 (cons 'target
(match-beginning 0)))))
2870 (defun org-element-timestamp-parser ()
2871 "Parse time stamp at point.
2873 Return a list whose CAR is `timestamp', and CDR a plist with
2874 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
2876 Assume point is at the beginning of the timestamp."
2878 (let* ((begin (point))
2880 ((looking-at org-tsr-regexp
)
2881 (if (match-string 2) 'active-range
'active
))
2882 ((looking-at org-tsr-regexp-both
)
2883 (if (match-string 2) 'inactive-range
'inactive
))
2886 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2888 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2890 (value (org-match-string-no-properties 0))
2891 (post-blank (progn (goto-char (match-end 0))
2892 (skip-chars-forward " \t")))
2899 :post-blank
,post-blank
)))))
2901 (defun org-element-timestamp-interpreter (timestamp contents
)
2902 "Interpret TIMESTAMP object as Org syntax.
2904 (org-element-property :value timestamp
))
2906 (defun org-element-timestamp-successor (limit)
2907 "Search for the next timestamp object.
2909 LIMIT bounds the search.
2911 Return value is a cons cell whose CAR is `timestamp' and CDR is
2912 beginning position."
2914 (when (re-search-forward
2915 (concat org-ts-regexp-both
2917 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2919 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2921 (cons 'timestamp
(match-beginning 0)))))
2926 (defun org-element-underline-parser ()
2927 "Parse underline object at point.
2929 Return a list whose CAR is `underline' and CDR is a plist with
2930 `:begin', `:end', `:contents-begin' and `:contents-end' and
2931 `:post-blank' keywords.
2933 Assume point is at the first underscore marker."
2935 (unless (bolp) (backward-char 1))
2936 (looking-at org-emph-re
)
2937 (let ((begin (match-beginning 2))
2938 (contents-begin (match-beginning 4))
2939 (contents-end (match-end 4))
2940 (post-blank (progn (goto-char (match-end 2))
2941 (skip-chars-forward " \t")))
2946 :contents-begin
,contents-begin
2947 :contents-end
,contents-end
2948 :post-blank
,post-blank
)))))
2950 (defun org-element-underline-interpreter (underline contents
)
2951 "Interpret UNDERLINE object as Org syntax.
2952 CONTENTS is the contents of the object."
2953 (format "_%s_" contents
))
2958 (defun org-element-verbatim-parser ()
2959 "Parse verbatim object at point.
2961 Return a list whose CAR is `verbatim' and CDR is a plist with
2962 `:value', `:begin', `:end' and `:post-blank' keywords.
2964 Assume point is at the first equal sign marker."
2966 (unless (bolp) (backward-char 1))
2967 (looking-at org-emph-re
)
2968 (let ((begin (match-beginning 2))
2969 (value (org-match-string-no-properties 4))
2970 (post-blank (progn (goto-char (match-end 2))
2971 (skip-chars-forward " \t")))
2977 :post-blank
,post-blank
)))))
2979 (defun org-element-verbatim-interpreter (verbatim contents
)
2980 "Interpret VERBATIM object as Org syntax.
2982 (format "=%s=" (org-element-property :value verbatim
)))
2986 ;;; Accessors and Setters
2988 ;; Provide four accessors: `org-element-type', `org-element-property'
2989 ;; `org-element-contents' and `org-element-restriction'.
2991 ;; Setter functions allow to modify elements by side effect. There is
2992 ;; `org-element-put-property', `org-element-set-contents',
2993 ;; `org-element-set-element' and `org-element-adopt-element'. Note
2994 ;; that `org-element-set-element' and `org-element-adopt-element' are
2995 ;; higher level functions since also update `:parent' property.
2997 (defun org-element-type (element)
2998 "Return type of ELEMENT.
3000 The function returns the type of the element or object provided.
3001 It can also return the following special value:
3002 `plain-text' for a string
3003 `org-data' for a complete document
3004 nil in any other case."
3006 ((not (consp element
)) (and (stringp element
) 'plain-text
))
3007 ((symbolp (car element
)) (car element
))))
3009 (defun org-element-property (property element
)
3010 "Extract the value from the PROPERTY of an ELEMENT."
3011 (plist-get (nth 1 element
) property
))
3013 (defun org-element-contents (element)
3014 "Extract contents from an ELEMENT."
3015 (and (consp element
) (nthcdr 2 element
)))
3017 (defun org-element-restriction (element)
3018 "Return restriction associated to ELEMENT.
3019 ELEMENT can be an element, an object or a symbol representing an
3020 element or object type."
3021 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
3022 org-element-object-restrictions
)))
3024 (defsubst org-element-put-property
(element property value
)
3025 "In ELEMENT set PROPERTY to VALUE.
3026 Return modified element."
3027 (setcar (cdr element
) (plist-put (nth 1 element
) property value
))
3030 (defsubst org-element-set-contents
(element &rest contents
)
3031 "Set ELEMENT contents to CONTENTS.
3032 Return modified element."
3033 (setcdr (cdr element
) contents
))
3035 (defsubst org-element-set-element
(old new
)
3036 "Replace element or object OLD with element or object NEW.
3037 The function takes care of setting `:parent' property for NEW."
3038 ;; OLD can belong to the contents of PARENT or to its secondary
3040 (let* ((parent (org-element-property :parent old
))
3041 (sec-loc (cdr (assq (org-element-type parent
)
3042 org-element-secondary-value-alist
)))
3043 (sec-value (and sec-loc
(org-element-property sec-loc parent
)))
3044 (place (or (member old sec-value
) (member old parent
))))
3045 ;; Make sure NEW has correct `:parent' property.
3046 (org-element-put-property new
:parent parent
)
3047 ;; Replace OLD with NEW in PARENT.
3048 (setcar place new
)))
3050 (defsubst org-element-adopt-element
(parent child
&optional append
)
3051 "Add an element to the contents of another element.
3053 PARENT is an element or object. CHILD is an element, an object,
3056 CHILD is added at the beginning of PARENT contents, unless the
3057 optional argument APPEND is non-nil, in which case CHILD is added
3060 The function takes care of setting `:parent' property for CHILD.
3061 Return parent element."
3062 (let ((contents (org-element-contents parent
)))
3063 (apply 'org-element-set-contents
3065 (if append
(append contents
(list child
)) (cons child contents
))))
3066 ;; Link the child element with parent.
3067 (when (consp child
) (org-element-put-property child
:parent parent
))
3068 ;; Return the parent element.
3073 ;;; Parsing Element Starting At Point
3075 ;; `org-element-current-element' is the core function of this section.
3076 ;; It returns the Lisp representation of the element starting at
3079 ;; `org-element-current-element' makes use of special modes. They are
3080 ;; activated for fixed element chaining (i.e. `plain-list' > `item')
3081 ;; or fixed conditional element chaining (i.e. `headline' >
3082 ;; `section'). Special modes are: `section', `quote-section', `item'
3085 (defun org-element-current-element (&optional granularity special structure
)
3086 "Parse the element starting at point.
3088 Return value is a list like (TYPE PROPS) where TYPE is the type
3089 of the element and PROPS a plist of properties associated to the
3092 Possible types are defined in `org-element-all-elements'.
3094 Optional argument GRANULARITY determines the depth of the
3095 recursion. Allowed values are `headline', `greater-element',
3096 `element', `object' or nil. When it is broader than `object' (or
3097 nil), secondary values will not be parsed, since they only
3100 Optional argument SPECIAL, when non-nil, can be either `section',
3101 `quote-section', `table-row' and `item'.
3103 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3106 This function assumes point is always at the beginning of the
3107 element it has to parse."
3109 ;; If point is at an affiliated keyword, try moving to the
3110 ;; beginning of the associated element. If none is found, the
3111 ;; keyword is orphaned and will be treated as plain text.
3112 (when (looking-at org-element--affiliated-re
)
3113 (let ((opoint (point)))
3114 (while (looking-at org-element--affiliated-re
) (forward-line))
3115 (when (looking-at "[ \t]*$") (goto-char opoint
))))
3116 (let ((case-fold-search t
)
3117 ;; Determine if parsing depth allows for secondary strings
3118 ;; parsing. It only applies to elements referenced in
3119 ;; `org-element-secondary-value-alist'.
3120 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3124 (org-element-item-parser (or structure
(org-list-struct))
3127 ((eq special
'quote-section
) (org-element-quote-section-parser))
3129 ((eq special
'table-row
) (org-element-table-row-parser))
3131 ((org-with-limited-levels (org-at-heading-p))
3132 (org-element-headline-parser raw-secondary-p
))
3133 ;; Section (must be checked after headline).
3134 ((eq special
'section
) (org-element-section-parser))
3135 ;; Planning and Clock.
3136 ((and (looking-at org-planning-or-clock-line-re
))
3137 (if (equal (match-string 1) org-clock-string
)
3138 (org-element-clock-parser)
3139 (org-element-planning-parser)))
3141 ((when (looking-at "[ \t]*#\\+BEGIN_\\([-A-Za-z0-9]+\\)\\(?: \\|$\\)")
3142 (let ((name (upcase (match-string 1))) parser
)
3144 ((not (save-excursion
3146 (format "^[ \t]*#\\+END_%s\\(?: \\|$\\)" name
) nil t
)))
3147 (org-element-paragraph-parser))
3148 ((setq parser
(assoc name org-element-block-name-alist
))
3149 (funcall (cdr parser
)))
3150 (t (org-element-special-block-parser))))))
3152 ((org-at-heading-p) (org-element-inlinetask-parser raw-secondary-p
))
3153 ;; LaTeX Environment.
3154 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
3157 (format "[ \t]*\\\\end{%s}[ \t]*"
3158 (regexp-quote (match-string 1)))
3160 (org-element-latex-environment-parser)
3161 (org-element-paragraph-parser)))
3162 ;; Drawer and Property Drawer.
3163 ((looking-at org-drawer-regexp
)
3164 (let ((name (match-string 1)))
3166 ((not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
)))
3167 (org-element-paragraph-parser))
3168 ((equal "PROPERTIES" name
) (org-element-property-drawer-parser))
3169 (t (org-element-drawer-parser)))))
3171 ((looking-at "[ \t]*:\\( \\|$\\)") (org-element-fixed-width-parser))
3172 ;; Babel Call, Dynamic Block and Keyword.
3173 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
3174 (let ((key (upcase (match-string 1))))
3176 ((equal key
"CALL") (org-element-babel-call-parser))
3177 ((and (equal key
"BEGIN")
3179 (re-search-forward "^[ \t]*#\\+END:\\(?: \\|$\\)" nil t
)))
3180 (org-element-dynamic-block-parser))
3181 ((and (not (equal key
"TBLFM"))
3182 (not (member key org-element-affiliated-keywords
)))
3183 (org-element-keyword-parser))
3184 (t (org-element-paragraph-parser)))))
3185 ;; Footnote Definition.
3186 ((looking-at org-footnote-definition-re
)
3187 (org-element-footnote-definition-parser))
3189 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)")
3190 (org-element-comment-parser))
3192 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3193 (org-element-horizontal-rule-parser))
3195 ((org-at-table-p t
) (org-element-table-parser))
3197 ((looking-at (org-item-re))
3198 (org-element-plain-list-parser (or structure
(org-list-struct))))
3199 ;; Default element: Paragraph.
3200 (t (org-element-paragraph-parser))))))
3203 ;; Most elements can have affiliated keywords. When looking for an
3204 ;; element beginning, we want to move before them, as they belong to
3205 ;; that element, and, in the meantime, collect information they give
3206 ;; into appropriate properties. Hence the following function.
3208 ;; Usage of optional arguments may not be obvious at first glance:
3210 ;; - TRANS-LIST is used to polish keywords names that have evolved
3211 ;; during Org history. In example, even though =result= and
3212 ;; =results= coexist, we want to have them under the same =result=
3213 ;; property. It's also true for "srcname" and "name", where the
3214 ;; latter seems to be preferred nowadays (thus the "name" property).
3216 ;; - CONSED allows to regroup multi-lines keywords under the same
3217 ;; property, while preserving their own identity. This is mostly
3218 ;; used for "attr_latex" and al.
3220 ;; - PARSED prepares a keyword value for export. This is useful for
3221 ;; "caption". Objects restrictions for such keywords are defined in
3222 ;; `org-element-object-restrictions'.
3224 ;; - DUALS is used to take care of keywords accepting a main and an
3225 ;; optional secondary values. For example "results" has its
3226 ;; source's name as the main value, and may have an hash string in
3227 ;; optional square brackets as the secondary one.
3229 ;; A keyword may belong to more than one category.
3231 (defun org-element-collect-affiliated-keywords
3232 (&optional key-re trans-list consed parsed duals
)
3233 "Collect affiliated keywords before point.
3235 Optional argument KEY-RE is a regexp matching keywords, which
3236 puts matched keyword in group 1. It defaults to
3237 `org-element--affiliated-re'.
3239 TRANS-LIST is an alist where key is the keyword and value the
3240 property name it should be translated to, without the colons. It
3241 defaults to `org-element-keyword-translation-alist'.
3243 CONSED is a list of strings. Any keyword belonging to that list
3244 will have its value consed. The check is done after keyword
3245 translation. It defaults to `org-element-multiple-keywords'.
3247 PARSED is a list of strings. Any keyword member of this list
3248 will have its value parsed. The check is done after keyword
3249 translation. If a keyword is a member of both CONSED and PARSED,
3250 it's value will be a list of parsed strings. It defaults to
3251 `org-element-parsed-keywords'.
3253 DUALS is a list of strings. Any keyword member of this list can
3254 have two parts: one mandatory and one optional. Its value is
3255 a cons cell whose CAR is the former, and the CDR the latter. If
3256 a keyword is a member of both PARSED and DUALS, both values will
3257 be parsed. It defaults to `org-element-dual-keywords'.
3259 Return a list whose CAR is the position at the first of them and
3260 CDR a plist of keywords and values."
3262 (let ((case-fold-search t
)
3263 (key-re (or key-re org-element--affiliated-re
))
3264 (trans-list (or trans-list org-element-keyword-translation-alist
))
3265 (consed (or consed org-element-multiple-keywords
))
3266 (parsed (or parsed org-element-parsed-keywords
))
3267 (duals (or duals org-element-dual-keywords
))
3268 ;; RESTRICT is the list of objects allowed in parsed
3270 (restrict (org-element-restriction 'keyword
))
3273 (while (and (not (bobp)) (progn (forward-line -
1) (looking-at key-re
)))
3274 (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
3275 ;; Apply translation to RAW-KWD. From there, KWD is
3276 ;; the official keyword.
3277 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
3278 ;; Find main value for any keyword.
3282 (buffer-substring-no-properties
3283 (match-end 0) (point-at-eol)))))
3284 ;; If KWD is a dual keyword, find its secondary
3285 ;; value. Maybe parse it.
3287 (and (member kwd duals
)
3288 (let ((sec (org-match-string-no-properties 3)))
3289 (if (or (not sec
) (not (member kwd parsed
))) sec
3290 (org-element-parse-secondary-string sec restrict
)))))
3291 ;; Attribute a property name to KWD.
3292 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3293 ;; Now set final shape for VALUE.
3294 (when (member kwd parsed
)
3295 (setq value
(org-element-parse-secondary-string value restrict
)))
3296 (when (member kwd duals
)
3297 ;; VALUE is mandatory. Set it to nil if there is none.
3298 (setq value
(and value
(cons value dual-value
))))
3299 ;; Attributes are always consed.
3300 (when (or (member kwd consed
) (string-match "^ATTR_" kwd
))
3301 (setq value
(cons value
(plist-get output kwd-sym
))))
3302 ;; Eventually store the new value in OUTPUT.
3303 (setq output
(plist-put output kwd-sym value
))))
3304 (unless (looking-at key-re
) (forward-line 1)))
3305 (list (point) output
))))
3311 ;; The two major functions here are `org-element-parse-buffer', which
3312 ;; parses Org syntax inside the current buffer, taking into account
3313 ;; region, narrowing, or even visibility if specified, and
3314 ;; `org-element-parse-secondary-string', which parses objects within
3317 ;; The (almost) almighty `org-element-map' allows to apply a function
3318 ;; on elements or objects matching some type, and accumulate the
3319 ;; resulting values. In an export situation, it also skips unneeded
3320 ;; parts of the parse tree.
3322 (defun org-element-parse-buffer (&optional granularity visible-only
)
3323 "Recursively parse the buffer and return structure.
3324 If narrowing is in effect, only parse the visible part of the
3327 Optional argument GRANULARITY determines the depth of the
3328 recursion. It can be set to the following symbols:
3330 `headline' Only parse headlines.
3331 `greater-element' Don't recurse into greater elements excepted
3332 headlines and sections. Thus, elements
3333 parsed are the top-level ones.
3334 `element' Parse everything but objects and plain text.
3335 `object' Parse the complete buffer (default).
3337 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3340 Assume buffer is in Org mode."
3342 (goto-char (point-min))
3343 (org-skip-whitespace)
3344 (org-element-parse-elements
3345 (point-at-bol) (point-max)
3346 ;; Start in `section' mode so text before the first
3347 ;; headline belongs to a section.
3348 'section nil granularity visible-only
(list 'org-data nil
))))
3350 (defun org-element-parse-secondary-string (string restriction
)
3351 "Recursively parse objects in STRING and return structure.
3353 RESTRICTION, when non-nil, is a symbol limiting the object types
3354 that will be looked after."
3357 (org-element-parse-objects (point-min) (point-max) nil restriction
)))
3359 (defun org-element-map (data types fun
&optional info first-match no-recursion
)
3360 "Map a function on selected elements or objects.
3362 DATA is the parsed tree, as returned by, i.e,
3363 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3364 of elements or objects types. FUN is the function called on the
3365 matching element or object. It must accept one arguments: the
3366 element or object itself.
3368 When optional argument INFO is non-nil, it should be a plist
3369 holding export options. In that case, parts of the parse tree
3370 not exportable according to that property list will be skipped.
3372 When optional argument FIRST-MATCH is non-nil, stop at the first
3373 match for which FUN doesn't return nil, and return that value.
3375 Optional argument NO-RECURSION is a symbol or a list of symbols
3376 representing elements or objects types. `org-element-map' won't
3377 enter any recursive element or object whose type belongs to that
3378 list. Though, FUN can still be applied on them.
3380 Nil values returned from FUN do not appear in the results."
3381 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3382 (unless (listp types
) (setq types
(list types
)))
3383 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3384 ;; Recursion depth is determined by --CATEGORY.
3387 (let ((category 'greater-elements
))
3388 (mapc (lambda (type)
3389 (cond ((or (memq type org-element-all-objects
)
3390 (eq type
'plain-text
))
3391 ;; If one object is found, the function
3392 ;; has to recurse into every object.
3393 (throw 'found
'objects
))
3394 ((not (memq type org-element-greater-elements
))
3395 ;; If one regular element is found, the
3396 ;; function has to recurse, at lest, into
3397 ;; every element it encounters.
3398 (and (not (eq category
'elements
))
3399 (setq category
'elements
)))))
3407 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3408 ;; holding contextual information.
3409 (let ((--type (org-element-type --data
)))
3412 ;; Ignored element in an export context.
3413 ((and info
(member --data
(plist-get info
:ignore-list
))))
3414 ;; Secondary string: only objects can be found there.
3416 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
3417 ;; Unconditionally enter parse trees.
3418 ((eq --type
'org-data
)
3419 (mapc --walk-tree
(org-element-contents --data
)))
3421 ;; Check if TYPE is matching among TYPES. If so,
3422 ;; apply FUN to --DATA and accumulate return value
3423 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3424 (when (memq --type types
)
3425 (let ((result (funcall fun --data
)))
3426 (cond ((not result
))
3427 (first-match (throw 'first-match result
))
3428 (t (push result --acc
)))))
3429 ;; If --DATA has a secondary string that can contain
3430 ;; objects with their type among TYPES, look into it.
3431 (when (eq --category
'objects
)
3433 (assq --type org-element-secondary-value-alist
)))
3435 (funcall --walk-tree
3436 (org-element-property (cdr sec-prop
) --data
)))))
3437 ;; Determine if a recursion into --DATA is possible.
3439 ;; --TYPE is explicitly removed from recursion.
3440 ((memq --type no-recursion
))
3441 ;; --DATA has no contents.
3442 ((not (org-element-contents --data
)))
3443 ;; Looking for greater elements but --DATA is simply
3444 ;; an element or an object.
3445 ((and (eq --category
'greater-elements
)
3446 (not (memq --type org-element-greater-elements
))))
3447 ;; Looking for elements but --DATA is an object.
3448 ((and (eq --category
'elements
)
3449 (memq --type org-element-all-objects
)))
3450 ;; In any other case, map contents.
3451 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
3453 (funcall --walk-tree data
)
3454 ;; Return value in a proper order.
3457 ;; The following functions are internal parts of the parser.
3459 ;; The first one, `org-element-parse-elements' acts at the element's
3462 ;; The second one, `org-element-parse-objects' applies on all objects
3463 ;; of a paragraph or a secondary string. It uses
3464 ;; `org-element-get-candidates' to optimize the search of the next
3465 ;; object in the buffer.
3467 ;; More precisely, that function looks for every allowed object type
3468 ;; first. Then, it discards failed searches, keeps further matches,
3469 ;; and searches again types matched behind point, for subsequent
3470 ;; calls. Thus, searching for a given type fails only once, and every
3471 ;; object is searched only once at top level (but sometimes more for
3474 (defun org-element-parse-elements
3475 (beg end special structure granularity visible-only acc
)
3476 "Parse elements between BEG and END positions.
3478 SPECIAL prioritize some elements over the others. It can be set
3479 to `quote-section', `section' `item' or `table-row'.
3481 When value is `item', STRUCTURE will be used as the current list
3484 GRANULARITY determines the depth of the recursion. See
3485 `org-element-parse-buffer' for more information.
3487 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3490 Elements are accumulated into ACC."
3493 (narrow-to-region beg end
)
3495 ;; When parsing only headlines, skip any text before first one.
3496 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3497 (org-with-limited-levels (outline-next-heading)))
3500 ;; Find current element's type and parse it accordingly to
3502 (let* ((element (org-element-current-element
3503 granularity special structure
))
3504 (type (org-element-type element
))
3505 (cbeg (org-element-property :contents-begin element
)))
3506 ;; Set ACC as parent of current element. It will be
3507 ;; completed by side-effect. If the element contains any
3508 ;; secondary string, also set `:parent' property of every
3509 ;; object within it as current element.
3510 (plist-put (nth 1 element
) :parent acc
)
3511 (let ((sec-loc (assq type org-element-secondary-value-alist
)))
3513 (let ((sec-value (org-element-property (cdr sec-loc
) element
)))
3514 (unless (stringp sec-value
)
3516 (unless (stringp obj
)
3517 (plist-put (nth 1 obj
) :parent element
)))
3519 (goto-char (org-element-property :end element
))
3524 ;; Case 1. Simply accumulate element if VISIBLE-ONLY is
3525 ;; true and element is hidden or if it has no contents
3527 ((or (and visible-only
(org-element-property :hiddenp element
))
3528 (not cbeg
)) element
)
3529 ;; Case 2. Greater element: parse it between
3530 ;; `contents-begin' and `contents-end'. Make sure
3531 ;; GRANULARITY allows the recursion, or ELEMENT is an
3532 ;; headline, in which case going inside is mandatory, in
3533 ;; order to get sub-level headings.
3534 ((and (memq type org-element-greater-elements
)
3535 (or (memq granularity
'(element object nil
))
3536 (and (eq granularity
'greater-element
)
3538 (eq type
'headline
)))
3539 (org-element-parse-elements
3540 cbeg
(org-element-property :contents-end element
)
3541 ;; Possibly switch to a special mode.
3544 (if (org-element-property :quotedp element
) 'quote-section
3548 (org-element-property :structure element
)
3549 granularity visible-only element
))
3550 ;; Case 3. ELEMENT has contents. Parse objects inside,
3551 ;; if GRANULARITY allows it.
3552 ((and cbeg
(memq granularity
'(object nil
)))
3553 (org-element-parse-objects
3554 cbeg
(org-element-property :contents-end element
)
3555 element
(org-element-restriction type
)))
3556 ;; Case 4. Else, just accumulate ELEMENT.
3561 (defun org-element-parse-objects (beg end acc restriction
)
3562 "Parse objects between BEG and END and return recursive structure.
3564 Objects are accumulated in ACC.
3566 RESTRICTION is a list of object types which are allowed in the
3568 (let ((get-next-object
3571 ;; Return the parsing function associated to the nearest
3572 ;; object among list of candidates CAND.
3573 (let ((pos (apply 'min
(mapcar 'cdr cand
))))
3578 (format "org-element-%s-parser" (car (rassq pos cand
))))))))))
3579 next-object candidates
)
3582 (while (setq candidates
(org-element-get-next-object-candidates
3583 end restriction candidates
))
3584 (setq next-object
(funcall get-next-object candidates
))
3585 ;; Set ACC as parent of current element. It will be completed
3587 (plist-put (nth 1 next-object
) :parent acc
)
3588 ;; 1. Text before any object. Untabify it.
3589 (let ((obj-beg (org-element-property :begin next-object
)))
3590 (unless (= (point) obj-beg
)
3593 (replace-regexp-in-string
3594 "\t" (make-string tab-width ?
)
3595 (buffer-substring-no-properties (point) obj-beg
)))))
3596 (if acc
(nconc acc beg-text
) (setq acc beg-text
)))))
3598 (let* ((obj-end (org-element-property :end next-object
))
3599 (cont-beg (org-element-property :contents-begin next-object
))
3600 (complete-next-object
3601 (if (and (memq (car next-object
) org-element-recursive-objects
)
3603 ;; ... recursive. The CONT-BEG check is for
3604 ;; links, as some of them might not be recursive
3605 ;; (i.e. plain links).
3609 (org-element-property :contents-end next-object
))
3610 (org-element-parse-objects
3611 (point-min) (point-max) next-object
3612 ;; Restrict allowed objects.
3613 (org-element-restriction next-object
)))
3615 (if acc
(nconc acc
(list complete-next-object
))
3616 (setq acc
(list complete-next-object
)))
3617 ;; If the object contains any secondary string, also set
3618 ;; `:parent' property of every object within it as current
3620 (let ((sec-loc (assq (org-element-type next-object
)
3621 org-element-secondary-value-alist
)))
3624 (org-element-property (cdr sec-loc
) next-object
)))
3625 (unless (stringp sec-value
)
3627 (unless (stringp obj
)
3628 (plist-put (nth 1 obj
)
3630 complete-next-object
)))
3632 (goto-char obj-end
)))
3633 ;; 3. Text after last object. Untabify it.
3634 (unless (= (point) end
)
3637 (replace-regexp-in-string
3638 "\t" (make-string tab-width ?
)
3639 (buffer-substring-no-properties (point) end
)))))
3640 (if acc
(nconc acc end-text
) (setq acc end-text
))))
3644 (defun org-element-get-next-object-candidates (limit restriction objects
)
3645 "Return an alist of candidates for the next object.
3647 LIMIT bounds the search, and RESTRICTION narrows candidates to
3650 Return value is an alist whose CAR is position and CDR the object
3653 OBJECTS is the previous candidates alist."
3654 (let (next-candidates types-to-search
)
3655 ;; If no previous result, search every object type in RESTRICTION.
3656 ;; Otherwise, keep potential candidates (old objects located after
3657 ;; point) and ask to search again those which had matched before.
3658 (if (not objects
) (setq types-to-search restriction
)
3660 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3661 (push obj next-candidates
)))
3663 ;; Call the appropriate successor function for each type to search
3664 ;; and accumulate matches.
3667 (let* ((successor-fun
3669 (format "org-element-%s-successor"
3670 (or (cdr (assq type org-element-object-successor-alist
))
3672 (obj (funcall successor-fun limit
)))
3673 (and obj
(push obj next-candidates
))))
3680 ;;; Towards A Bijective Process
3682 ;; The parse tree obtained with `org-element-parse-buffer' is really
3683 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3684 ;; interpreted and expanded into a string with canonical Org syntax.
3685 ;; Hence `org-element-interpret-data'.
3687 ;; The function relies internally on
3688 ;; `org-element-interpret--affiliated-keywords'.
3690 (defun org-element-interpret-data (data &optional parent
)
3691 "Interpret DATA as Org syntax.
3693 DATA is a parse tree, an element, an object or a secondary string
3696 Optional argument PARENT is used for recursive calls. It contains
3697 the element or object containing data, or nil.
3699 Return Org syntax as a string."
3700 (let* ((type (org-element-type data
))
3703 ;; Secondary string.
3706 (lambda (obj) (org-element-interpret-data obj parent
))
3708 ;; Full Org document.
3709 ((eq type
'org-data
)
3711 (lambda (obj) (org-element-interpret-data obj parent
))
3712 (org-element-contents data
) ""))
3714 ((stringp data
) data
)
3715 ;; Element/Object without contents.
3716 ((not (org-element-contents data
))
3717 (funcall (intern (format "org-element-%s-interpreter" type
))
3719 ;; Element/Object with contents.
3721 (let* ((greaterp (memq type org-element-greater-elements
))
3722 (objectp (and (not greaterp
)
3723 (memq type org-element-recursive-objects
)))
3726 (lambda (obj) (org-element-interpret-data obj data
))
3727 (org-element-contents
3728 (if (or greaterp objectp
) data
3729 ;; Elements directly containing objects must
3730 ;; have their indentation normalized first.
3731 (org-element-normalize-contents
3733 ;; When normalizing first paragraph of an
3734 ;; item or a footnote-definition, ignore
3735 ;; first line's indentation.
3736 (and (eq type
'paragraph
)
3737 (equal data
(car (org-element-contents parent
)))
3738 (memq (org-element-type parent
)
3739 '(footnote-definiton item
))))))
3741 (funcall (intern (format "org-element-%s-interpreter" type
))
3743 (if greaterp
(org-element-normalize-contents contents
)
3745 (if (memq type
'(org-data plain-text nil
)) results
3746 ;; Build white spaces. If no `:post-blank' property is
3747 ;; specified, assume its value is 0.
3748 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
3749 (if (memq type org-element-all-objects
)
3750 (concat results
(make-string post-blank
32))
3752 (org-element-interpret--affiliated-keywords data
)
3753 (org-element-normalize-string results
)
3754 (make-string post-blank
10)))))))
3756 (defun org-element-interpret--affiliated-keywords (element)
3757 "Return ELEMENT's affiliated keywords as Org syntax.
3758 If there is no affiliated keyword, return the empty string."
3759 (let ((keyword-to-org
3763 (when (member key org-element-dual-keywords
)
3764 (setq dual
(cdr value
) value
(car value
)))
3767 (format "[%s]" (org-element-interpret-data dual
)))
3769 (if (member key org-element-parsed-keywords
)
3770 (org-element-interpret-data value
)
3775 (let ((value (org-element-property prop element
))
3776 (keyword (upcase (substring (symbol-name prop
) 1))))
3778 (if (or (member keyword org-element-multiple-keywords
)
3779 ;; All attribute keywords can have multiple lines.
3780 (string-match "^ATTR_" keyword
))
3781 (mapconcat (lambda (line) (funcall keyword-to-org keyword line
))
3784 (funcall keyword-to-org keyword value
)))))
3785 ;; List all ELEMENT's properties matching an attribute line or an
3786 ;; affiliated keyword, but ignore translated keywords since they
3787 ;; cannot belong to the property list.
3788 (loop for prop in
(nth 1 element
) by
'cddr
3789 when
(let ((keyword (upcase (substring (symbol-name prop
) 1))))
3790 (or (string-match "^ATTR_" keyword
)
3792 (member keyword org-element-affiliated-keywords
)
3794 org-element-keyword-translation-alist
)))))
3798 ;; Because interpretation of the parse tree must return the same
3799 ;; number of blank lines between elements and the same number of white
3800 ;; space after objects, some special care must be given to white
3803 ;; The first function, `org-element-normalize-string', ensures any
3804 ;; string different from the empty string will end with a single
3805 ;; newline character.
3807 ;; The second function, `org-element-normalize-contents', removes
3808 ;; global indentation from the contents of the current element.
3810 (defun org-element-normalize-string (s)
3811 "Ensure string S ends with a single newline character.
3813 If S isn't a string return it unchanged. If S is the empty
3814 string, return it. Otherwise, return a new string with a single
3815 newline character at its end."
3817 ((not (stringp s
)) s
)
3819 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3820 (replace-match "\n" nil nil s
)))))
3822 (defun org-element-normalize-contents (element &optional ignore-first
)
3823 "Normalize plain text in ELEMENT's contents.
3825 ELEMENT must only contain plain text and objects.
3827 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3828 indentation to compute maximal common indentation.
3830 Return the normalized element that is element with global
3831 indentation removed from its contents. The function assumes that
3832 indentation is not done with TAB characters."
3833 (let* (ind-list ; for byte-compiler
3834 collect-inds
; for byte-compiler
3837 ;; Return list of indentations within BLOB. This is done by
3838 ;; walking recursively BLOB and updating IND-LIST along the
3839 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3840 ;; been seen yet. It is required as this string is the only
3841 ;; one whose indentation doesn't happen after a newline
3843 (lambda (blob first-flag
)
3846 (when (and first-flag
(stringp object
))
3847 (setq first-flag nil
)
3848 (string-match "\\`\\( *\\)" object
)
3849 (let ((len (length (match-string 1 object
))))
3850 ;; An indentation of zero means no string will be
3851 ;; modified. Quit the process.
3852 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
3853 (push len ind-list
))))
3857 ;; Avoid matching blank or empty lines.
3858 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
3859 (not (equal (match-string 2 object
) " ")))
3860 (setq start
(match-end 0))
3861 (push (length (match-string 1 object
)) ind-list
))))
3862 ((memq (org-element-type object
) org-element-recursive-objects
)
3863 (funcall collect-inds object first-flag
))))
3864 (org-element-contents blob
))))))
3865 ;; Collect indentation list in ELEMENT. Possibly remove first
3866 ;; value if IGNORE-FIRST is non-nil.
3867 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
3868 (if (not ind-list
) element
3869 ;; Build ELEMENT back, replacing each string with the same
3870 ;; string minus common indentation.
3871 (let* (build ; for byte compiler
3874 (lambda (blob mci first-flag
)
3875 ;; Return BLOB with all its strings indentation
3876 ;; shortened from MCI white spaces. FIRST-FLAG is
3877 ;; non-nil when the first string hasn't been seen
3880 (list (org-element-type blob
) (nth 1 blob
))
3883 (when (and first-flag
(stringp object
))
3884 (setq first-flag nil
)
3886 (replace-regexp-in-string
3887 (format "\\` \\{%d\\}" mci
) "" object
)))
3890 (replace-regexp-in-string
3891 (format "\n \\{%d\\}" mci
) "\n" object
))
3892 ((memq (org-element-type object
)
3893 org-element-recursive-objects
)
3894 (funcall build object mci first-flag
))
3896 (org-element-contents blob
)))))))
3897 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
3903 ;; The first move is to implement a way to obtain the smallest element
3904 ;; containing point. This is the job of `org-element-at-point'. It
3905 ;; basically jumps back to the beginning of section containing point
3906 ;; and moves, element after element, with
3907 ;; `org-element-current-element' until the container is found.
3909 ;; At a deeper level, `org-element-context' lists all elements and
3910 ;; objects containing point.
3912 ;; Note: When using `org-element-at-point', secondary values are never
3913 ;; parsed since the function focuses on elements, not on objects.
3915 (defun org-element-at-point (&optional keep-trail
)
3916 "Determine closest element around point.
3918 Return value is a list like (TYPE PROPS) where TYPE is the type
3919 of the element and PROPS a plist of properties associated to the
3920 element. Possible types are defined in
3921 `org-element-all-elements'.
3923 As a special case, if point is at the very beginning of a list or
3924 sub-list, returned element will be that list instead of the first
3925 item. In the same way, if point is at the beginning of the first
3926 row of a table, returned element will be the table instead of the
3929 If optional argument KEEP-TRAIL is non-nil, the function returns
3930 a list of of elements leading to element at point. The list's
3931 CAR is always the element at point. Following positions contain
3932 element's siblings, then parents, siblings of parents, until the
3933 first element of current section."
3934 (org-with-wide-buffer
3935 ;; If at an headline, parse it. It is the sole element that
3936 ;; doesn't require to know about context. Be sure to disallow
3937 ;; secondary string parsing, though.
3938 (if (org-with-limited-levels (org-at-heading-p))
3941 (if (not keep-trail
) (org-element-headline-parser t
)
3942 (list (org-element-headline-parser t
))))
3943 ;; Otherwise move at the beginning of the section containing
3945 (let ((origin (point)) element type special-flag trail struct prevs
)
3946 (org-with-limited-levels
3947 (if (org-before-first-heading-p) (goto-char (point-min))
3948 (org-back-to-heading)
3950 (org-skip-whitespace)
3952 ;; Parse successively each element, skipping those ending
3953 ;; before original position.
3956 (setq element
(org-element-current-element
3957 'element special-flag struct
)
3959 (push element trail
)
3961 ;; 1. Skip any element ending before point or at point.
3962 ((let ((end (org-element-property :end element
)))
3963 (when (<= end origin
)
3964 (if (> (point-max) end
) (goto-char end
)
3965 (throw 'exit
(if keep-trail trail element
))))))
3966 ;; 2. An element containing point is always the element at
3968 ((not (memq type org-element-greater-elements
))
3969 (throw 'exit
(if keep-trail trail element
)))
3970 ;; 3. At any other greater element type, if point is
3971 ;; within contents, move into it. Otherwise, return
3974 (let ((beg (org-element-property :contents-begin element
))
3975 (end (org-element-property :contents-end element
)))
3976 (if (or (not beg
) (not end
) (> beg origin
) (<= end origin
)
3977 (and (= beg origin
) (memq type
'(plain-list table
))))
3978 (throw 'exit
(if keep-trail trail element
))
3981 (setq special-flag
'item
3982 struct
(org-element-property :structure element
)))
3983 (table (setq special-flag
'table-row
))
3984 (otherwise (setq special-flag nil
)))
3985 (narrow-to-region beg end
)
3986 (goto-char beg
)))))))))))
3988 (defun org-element-context ()
3989 "Return list of all elements and objects around point.
3991 Return value is a list like (TYPE PROPS) where TYPE is the type
3992 of the element or object and PROPS a plist of properties
3993 associated to it. Possible types are defined in
3994 `org-element-all-elements' and `org-element-all-objects'.
3996 All elements and objects returned belong to the current section
3997 and are ordered from closest to farthest."
3998 (org-with-wide-buffer
3999 (let* ((origin (point))
4000 ;; Remove elements not containing point from trail.
4001 (elements (org-remove-if
4003 (or (> (org-element-property :begin el
) origin
)
4004 (< (org-element-property :end el
) origin
)))
4005 (org-element-at-point 'keep-trail
)))
4006 (element (car elements
))
4007 (type (car element
)) end
)
4008 ;; Check if point is inside an element containing objects or at
4009 ;; a secondary string. In that case, move to beginning of the
4010 ;; element or secondary string and set END to the other side.
4011 (if (not (or (and (eq type
'item
)
4012 (let ((tag (org-element-property :tag element
)))
4016 (search-forward tag
(point-at-eol))
4017 (goto-char (match-beginning 0))
4018 (and (>= origin
(point))
4020 ;; `1+' is required so some
4021 ;; successors can match
4022 ;; properly their object.
4023 (setq end
(1+ (match-end 0)))))))))
4024 (and (memq type
'(headline inlinetask
))
4025 (progn (beginning-of-line)
4026 (skip-chars-forward "* ")
4027 (setq end
(point-at-eol))))
4028 (and (memq (car element
) '(paragraph table-cell verse-block
))
4029 (let ((cbeg (org-element-property
4030 :contents-begin element
))
4031 (cend (org-element-property
4032 :contents-end element
)))
4033 (and (>= origin cbeg
)
4035 (progn (goto-char cbeg
) (setq end cend
)))))))
4037 (let ((restriction (org-element-restriction element
)) candidates
)
4039 (while (setq candidates
(org-element-get-next-object-candidates
4040 end restriction candidates
))
4041 (let ((closest-cand (rassq (apply 'min
(mapcar 'cdr candidates
))
4043 ;; If ORIGIN is before next object in element, there's
4044 ;; no point in looking further.
4045 (if (> (cdr closest-cand
) origin
) (throw 'exit elements
)
4047 (progn (goto-char (cdr closest-cand
))
4048 (funcall (intern (format "org-element-%s-parser"
4049 (car closest-cand
))))))
4050 (cbeg (org-element-property :contents-begin object
))
4051 (cend (org-element-property :contents-end object
)))
4053 ;; ORIGIN is after OBJECT, so skip it.
4054 ((< (org-element-property :end object
) origin
)
4055 (goto-char (org-element-property :end object
)))
4056 ;; ORIGIN is within a non-recursive object or at an
4057 ;; object boundaries: Return that object.
4058 ((or (not cbeg
) (> cbeg origin
) (< cend origin
))
4059 (throw 'exit
(cons object elements
)))
4060 ;; Otherwise, move within current object and restrict
4061 ;; search to the end of its contents.
4064 (push object elements
)))))))
4068 ;; Once the local structure around point is well understood, it's easy
4069 ;; to implement some replacements for `forward-paragraph'
4070 ;; `backward-paragraph', namely `org-element-forward' and
4071 ;; `org-element-backward'.
4073 ;; Also, `org-transpose-elements' mimics the behaviour of
4074 ;; `transpose-words', at the element's level, whereas
4075 ;; `org-element-drag-forward', `org-element-drag-backward', and
4076 ;; `org-element-up' generalize, respectively, functions
4077 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
4079 ;; `org-element-unindent-buffer' will, as its name almost suggests,
4080 ;; smartly remove global indentation from buffer, making it possible
4081 ;; to use Org indent mode on a file created with hard indentation.
4083 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
4084 ;; internally by some of the previously cited tools.
4086 (defsubst org-element-nested-p
(elem-A elem-B
)
4087 "Non-nil when elements ELEM-A and ELEM-B are nested."
4088 (let ((beg-A (org-element-property :begin elem-A
))
4089 (beg-B (org-element-property :begin elem-B
))
4090 (end-A (org-element-property :end elem-A
))
4091 (end-B (org-element-property :end elem-B
)))
4092 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4093 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4095 (defun org-element-swap-A-B (elem-A elem-B
)
4096 "Swap elements ELEM-A and ELEM-B.
4097 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
4099 (goto-char (org-element-property :begin elem-A
))
4100 ;; There are two special cases when an element doesn't start at bol:
4101 ;; the first paragraph in an item or in a footnote definition.
4102 (let ((specialp (not (bolp))))
4103 ;; Only a paragraph without any affiliated keyword can be moved at
4104 ;; ELEM-A position in such a situation. Note that the case of
4105 ;; a footnote definition is impossible: it cannot contain two
4106 ;; paragraphs in a row because it cannot contain a blank line.
4108 (or (not (eq (org-element-type elem-B
) 'paragraph
))
4109 (/= (org-element-property :begin elem-B
)
4110 (org-element-property :contents-begin elem-B
))))
4111 (error "Cannot swap elements"))
4112 ;; In a special situation, ELEM-A will have no indentation. We'll
4113 ;; give it ELEM-B's (which will in, in turn, have no indentation).
4114 (let* ((ind-B (when specialp
4115 (goto-char (org-element-property :begin elem-B
))
4116 (org-get-indentation)))
4117 (beg-A (org-element-property :begin elem-A
))
4118 (end-A (save-excursion
4119 (goto-char (org-element-property :end elem-A
))
4120 (skip-chars-backward " \r\t\n")
4122 (beg-B (org-element-property :begin elem-B
))
4123 (end-B (save-excursion
4124 (goto-char (org-element-property :end elem-B
))
4125 (skip-chars-backward " \r\t\n")
4127 ;; Store overlays responsible for visibility status. We
4128 ;; also need to store their boundaries as they will be
4129 ;; removed from buffer.
4132 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4133 (overlays-in beg-A end-A
))
4134 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
4135 (overlays-in beg-B end-B
))))
4137 (body-A (buffer-substring beg-A end-A
))
4138 (body-B (delete-and-extract-region beg-B end-B
)))
4141 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
4142 (org-indent-to-column ind-B
))
4144 ;; Restore ex ELEM-A overlays.
4148 (+ (nth 1 ov
) (- beg-B beg-A
))
4149 (+ (nth 2 ov
) (- beg-B beg-A
))))
4152 (delete-region beg-A end-A
)
4154 ;; Restore ex ELEM-B overlays.
4156 (move-overlay (car ov
)
4157 (+ (nth 1 ov
) (- beg-A beg-B
))
4158 (+ (nth 2 ov
) (- beg-A beg-B
))))
4160 (goto-char (org-element-property :end elem-B
)))))
4162 (defun org-element-forward ()
4163 "Move forward by one element.
4164 Move to the next element at the same level, when possible."
4166 (if (org-with-limited-levels (org-at-heading-p))
4167 (let ((origin (point)))
4168 (org-forward-same-level 1)
4169 (unless (org-with-limited-levels (org-at-heading-p))
4171 (error "Cannot move further down")))
4172 (let* ((trail (org-element-at-point 'keep-trail
))
4174 (end (org-element-property :end elem
))
4175 (parent (loop for prev in trail
4176 when
(>= (org-element-property :end prev
) end
)
4179 ((eobp) (error "Cannot move further down"))
4180 ((and parent
(= (org-element-property :contents-end parent
) end
))
4181 (goto-char (org-element-property :end parent
)))
4182 (t (goto-char end
))))))
4184 (defun org-element-backward ()
4185 "Move backward by one element.
4186 Move to the previous element at the same level, when possible."
4188 (if (org-with-limited-levels (org-at-heading-p))
4189 ;; At an headline, move to the previous one, if any, or stay
4191 (let ((origin (point)))
4192 (org-backward-same-level 1)
4193 (unless (org-with-limited-levels (org-at-heading-p))
4195 (error "Cannot move further up")))
4196 (let* ((trail (org-element-at-point 'keep-trail
))
4198 (prev-elem (nth 1 trail
))
4199 (beg (org-element-property :begin elem
)))
4201 ;; Move to beginning of current element if point isn't there
4203 ((/= (point) beg
) (goto-char beg
))
4204 ((not prev-elem
) (error "Cannot move further up"))
4205 (t (goto-char (org-element-property :begin prev-elem
)))))))
4207 (defun org-element-up ()
4208 "Move to upper element."
4210 (if (org-with-limited-levels (org-at-heading-p))
4211 (unless (org-up-heading-safe)
4212 (error "No surrounding element"))
4213 (let* ((trail (org-element-at-point 'keep-trail
))
4215 (end (org-element-property :end elem
))
4216 (parent (loop for prev in trail
4217 when
(>= (org-element-property :end prev
) end
)
4220 (parent (goto-char (org-element-property :begin parent
)))
4221 ((org-before-first-heading-p) (error "No surrounding element"))
4222 (t (org-back-to-heading))))))
4224 (defun org-element-down ()
4225 "Move to inner element."
4227 (let ((element (org-element-at-point)))
4229 ((memq (org-element-type element
) '(plain-list table
))
4230 (goto-char (org-element-property :contents-begin element
))
4232 ((memq (org-element-type element
) org-element-greater-elements
)
4233 ;; If contents are hidden, first disclose them.
4234 (when (org-element-property :hiddenp element
) (org-cycle))
4235 (goto-char (org-element-property :contents-begin element
)))
4236 (t (error "No inner element")))))
4238 (defun org-element-drag-backward ()
4239 "Move backward element at point."
4241 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
4242 (let* ((trail (org-element-at-point 'keep-trail
))
4244 (prev-elem (nth 1 trail
)))
4245 ;; Error out if no previous element or previous element is
4246 ;; a parent of the current one.
4247 (if (or (not prev-elem
) (org-element-nested-p elem prev-elem
))
4248 (error "Cannot drag element backward")
4249 (let ((pos (point)))
4250 (org-element-swap-A-B prev-elem elem
)
4251 (goto-char (+ (org-element-property :begin prev-elem
)
4252 (- pos
(org-element-property :begin elem
)))))))))
4254 (defun org-element-drag-forward ()
4255 "Move forward element at point."
4257 (let* ((pos (point))
4258 (elem (org-element-at-point)))
4259 (when (= (point-max) (org-element-property :end elem
))
4260 (error "Cannot drag element forward"))
4261 (goto-char (org-element-property :end elem
))
4262 (let ((next-elem (org-element-at-point)))
4263 (when (or (org-element-nested-p elem next-elem
)
4264 (and (eq (org-element-type next-elem
) 'headline
)
4265 (not (eq (org-element-type elem
) 'headline
))))
4267 (error "Cannot drag element forward"))
4268 ;; Compute new position of point: it's shifted by NEXT-ELEM
4269 ;; body's length (without final blanks) and by the length of
4270 ;; blanks between ELEM and NEXT-ELEM.
4271 (let ((size-next (- (save-excursion
4272 (goto-char (org-element-property :end next-elem
))
4273 (skip-chars-backward " \r\t\n")
4275 ;; Small correction if buffer doesn't end
4276 ;; with a newline character.
4277 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
4278 (org-element-property :begin next-elem
)))
4279 (size-blank (- (org-element-property :end elem
)
4281 (goto-char (org-element-property :end elem
))
4282 (skip-chars-backward " \r\t\n")
4285 (org-element-swap-A-B elem next-elem
)
4286 (goto-char (+ pos size-next size-blank
))))))
4288 (defun org-element-mark-element ()
4289 "Put point at beginning of this element, mark at end.
4291 Interactively, if this command is repeated or (in Transient Mark
4292 mode) if the mark is active, it marks the next element after the
4293 ones already marked."
4295 (let (deactivate-mark)
4296 (if (or (and (eq last-command this-command
) (mark t
))
4297 (and transient-mark-mode mark-active
))
4301 (goto-char (org-element-property :end
(org-element-at-point)))))
4302 (let ((element (org-element-at-point)))
4304 (push-mark (org-element-property :end element
) t t
)
4305 (goto-char (org-element-property :begin element
))))))
4307 (defun org-narrow-to-element ()
4308 "Narrow buffer to current element."
4310 (let ((elem (org-element-at-point)))
4312 ((eq (car elem
) 'headline
)
4314 (org-element-property :begin elem
)
4315 (org-element-property :end elem
)))
4316 ((memq (car elem
) org-element-greater-elements
)
4318 (org-element-property :contents-begin elem
)
4319 (org-element-property :contents-end elem
)))
4322 (org-element-property :begin elem
)
4323 (org-element-property :end elem
))))))
4325 (defun org-element-transpose ()
4326 "Transpose current and previous elements, keeping blank lines between.
4327 Point is moved after both elements."
4329 (org-skip-whitespace)
4330 (let ((end (org-element-property :end
(org-element-at-point))))
4331 (org-element-drag-backward)
4334 (defun org-element-unindent-buffer ()
4335 "Un-indent the visible part of the buffer.
4336 Relative indentation (between items, inside blocks, etc.) isn't
4339 (unless (eq major-mode
'org-mode
)
4340 (error "Cannot un-indent a buffer not in Org mode"))
4341 (let* ((parse-tree (org-element-parse-buffer 'greater-element
))
4342 unindent-tree
; For byte-compiler.
4348 (if (memq (org-element-type element
) '(headline section
))
4349 (funcall unindent-tree
(org-element-contents element
))
4353 (org-element-property :begin element
)
4354 (org-element-property :end element
))
4355 (org-do-remove-indentation)))))
4356 (reverse contents
))))))
4357 (funcall unindent-tree
(org-element-contents parse-tree
))))
4359 (defun org-element-fill-paragraph (&optional justify
)
4360 "Fill element at point, when applicable.
4362 This function only applies to paragraph, comment blocks, example
4363 blocks and fixed-width areas. Also, as a special case, re-align
4364 table when point is at one.
4366 If JUSTIFY is non-nil (interactively, with prefix argument),
4367 justify as well. If `sentence-end-double-space' is non-nil, then
4368 period followed by one space does not end a sentence, so don't
4369 break a line there. The variable `fill-column' controls the
4371 (let ((element (org-element-at-point)))
4372 (case (org-element-type element
)
4373 ;; Align Org tables, leave table.el tables as-is.
4374 (table-row (org-table-align) t
)
4376 (when (eq (org-element-property :type element
) 'org
) (org-table-align))
4378 ;; Elements that may contain `line-break' type objects.
4379 ((paragraph verse-block
)
4380 (let ((beg (org-element-property :contents-begin element
))
4381 (end (org-element-property :contents-end element
)))
4382 ;; Do nothing if point is at an affiliated keyword or at
4383 ;; verse block markers.
4384 (if (or (< (point) beg
) (>= (point) end
)) t
4385 ;; At a verse block, first narrow to current "paragraph"
4386 ;; and set current element to that paragraph.
4388 (when (eq (org-element-type element
) 'verse-block
)
4389 (narrow-to-region beg end
)
4392 (let ((bol-pos (point-at-bol)))
4393 (re-search-backward org-element-paragraph-separate nil
'move
)
4394 (unless (or (bobp) (= (point-at-bol) bol-pos
))
4396 (setq element
(org-element-paragraph-parser)
4397 beg
(org-element-property :contents-begin element
)
4398 end
(org-element-property :contents-end element
)))))
4399 ;; Fill paragraph, taking line breaks into consideration.
4400 ;; For that, slice the paragraph using line breaks as
4401 ;; separators, and fill the parts in reverse order to
4402 ;; avoid messing with markers.
4407 (fill-region-as-paragraph pos
(point) justify
)
4409 ;; Find the list of ending positions for line breaks
4410 ;; in the current paragraph. Add paragraph beginning
4411 ;; to include first slice.
4415 (org-element-parse-objects
4416 beg end nil org-element-all-objects
)
4418 (lambda (lb) (org-element-property :end lb
)))))))) t
)))
4419 ;; Elements whose contents should be filled as plain text.
4420 ((comment-block example-block
)
4424 (goto-char (org-element-property :begin element
))
4425 (while (looking-at org-element--affiliated-re
) (forward-line))
4429 (goto-char (org-element-property :end element
))
4430 (if (bolp) (forward-line -
1) (beginning-of-line))
4432 (fill-paragraph justify
) t
))
4433 ;; Ignore every other element.
4437 (provide 'org-element
)
4438 ;;; org-element.el ends here