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. All affiliated keywords
38 ;; are referenced in `org-element-affiliated-keywords'.
40 ;; Element containing other elements (and only elements) are called
41 ;; greater elements. Concerned types are: `center-block', `drawer',
42 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
43 ;; `item', `plain-list', `quote-block', `section' and `special-block'.
45 ;; Other element types are: `babel-call', `clock', `comment',
46 ;; `comment-block', `example-block', `export-block', `fixed-width',
47 ;; `horizontal-rule', `keyword', `latex-environment', `paragraph',
48 ;; `planning', `property-drawer', `quote-section', `src-block',
49 ;; `table', `table-cell', `table-row' and `verse-block'. Among them,
50 ;; `paragraph', `table-cell' and `verse-block' types can contain Org
51 ;; 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, three 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, and `:post-blank', which holds the
71 ;; number of blank lines, or white spaces, at its end. Greater
72 ;; elements and elements containing objects will also have
73 ;; `:contents-begin' and `:contents-end' properties to delimit
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' (and its
104 ;; relative, `org-element-interpret-secondary').
106 ;; The library ends by furnishing a set of interactive tools for
107 ;; element's navigation and manipulation, mostly based on
108 ;; `org-element-at-point' function.
113 (eval-when-compile (require 'cl
))
115 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
120 ;; For each greater element type, we define a parser and an
123 ;; A parser returns the element or object as the list described above.
124 ;; Most of them accepts no argument. Though, exceptions exist. Hence
125 ;; every element containing a secondary string (see
126 ;; `org-element-secondary-value-alist') will accept an optional
127 ;; argument to toggle parsing of that secondary string. Moreover,
128 ;; `item' parser requires current list's structure as its first
131 ;; An interpreter accepts two arguments: the list representation of
132 ;; the element or object, and its contents. The latter may be nil,
133 ;; depending on the element or object considered. It returns the
134 ;; appropriate Org syntax, as a string.
136 ;; Parsing functions must follow the naming convention:
137 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
138 ;; defined in `org-element-greater-elements'.
140 ;; Similarly, interpreting functions must follow the naming
141 ;; convention: org-element-TYPE-interpreter.
143 ;; With the exception of `headline' and `item' types, greater elements
144 ;; cannot contain other greater elements of their own type.
146 ;; Beside implementing a parser and an interpreter, adding a new
147 ;; greater element requires to tweak `org-element-current-element'.
148 ;; Moreover, the newly defined type must be added to both
149 ;; `org-element-all-elements' and `org-element-greater-elements'.
154 (defun org-element-center-block-parser ()
155 "Parse a center block.
157 Return a list whose CAR is `center-block' and CDR is a plist
158 containing `:begin', `:end', `:hiddenp', `:contents-begin',
159 `:contents-end' and `:post-blank' keywords.
161 Assume point is at beginning or end of the block."
163 (let* ((case-fold-search t
)
167 (concat "^[ \t]*#\\+BEGIN_CENTER") nil t
)
168 (org-element-collect-affiliated-keywords)))
169 (begin (car keywords
))
170 (contents-begin (progn (forward-line) (point)))
171 (hidden (org-truely-invisible-p))
172 (contents-end (progn (re-search-forward
173 (concat "^[ \t]*#\\+END_CENTER") nil t
)
175 (pos-before-blank (progn (forward-line) (point)))
176 (end (progn (org-skip-whitespace)
177 (if (eobp) (point) (point-at-bol)))))
182 :contents-begin
,contents-begin
183 :contents-end
,contents-end
184 :post-blank
,(count-lines pos-before-blank end
)
185 ,@(cadr keywords
))))))
187 (defun org-element-center-block-interpreter (center-block contents
)
188 "Interpret CENTER-BLOCK element as Org syntax.
189 CONTENTS is the contents of the element."
190 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
195 (defun org-element-drawer-parser ()
198 Return a list whose CAR is `drawer' and CDR is a plist containing
199 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
200 `:contents-end' and `:post-blank' keywords.
202 Assume point is at beginning of drawer."
204 (let* ((case-fold-search t
)
205 (name (progn (looking-at org-drawer-regexp
)
206 (org-match-string-no-properties 1)))
207 (keywords (org-element-collect-affiliated-keywords))
208 (begin (car keywords
))
209 (contents-begin (progn (forward-line) (point)))
210 (hidden (org-truely-invisible-p))
211 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
213 (pos-before-blank (progn (forward-line) (point)))
214 (end (progn (org-skip-whitespace)
215 (if (eobp) (point) (point-at-bol)))))
221 :contents-begin
,contents-begin
222 :contents-end
,contents-end
223 :post-blank
,(count-lines pos-before-blank end
)
224 ,@(cadr keywords
))))))
226 (defun org-element-drawer-interpreter (drawer contents
)
227 "Interpret DRAWER element as Org syntax.
228 CONTENTS is the contents of the element."
229 (format ":%s:\n%s:END:"
230 (org-element-property :drawer-name drawer
)
236 (defun org-element-dynamic-block-parser ()
237 "Parse a dynamic block.
239 Return a list whose CAR is `dynamic-block' and CDR is a plist
240 containing `:block-name', `:begin', `:end', `:hiddenp',
241 `:contents-begin', `:contents-end', `:arguments' and
242 `:post-blank' keywords.
244 Assume point is at beginning of dynamic block."
246 (let* ((case-fold-search t
)
247 (name (progn (looking-at org-dblock-start-re
)
248 (org-match-string-no-properties 1)))
249 (arguments (org-match-string-no-properties 3))
250 (keywords (org-element-collect-affiliated-keywords))
251 (begin (car keywords
))
252 (contents-begin (progn (forward-line) (point)))
253 (hidden (org-truely-invisible-p))
254 (contents-end (progn (re-search-forward org-dblock-end-re nil t
)
256 (pos-before-blank (progn (forward-line) (point)))
257 (end (progn (org-skip-whitespace)
258 (if (eobp) (point) (point-at-bol)))))
263 :arguments
,arguments
265 :contents-begin
,contents-begin
266 :contents-end
,contents-end
267 :post-blank
,(count-lines pos-before-blank end
)
268 ,@(cadr keywords
))))))
270 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
271 "Interpret DYNAMIC-BLOCK element as Org syntax.
272 CONTENTS is the contents of the element."
273 (format "#+BEGIN: %s%s\n%s#+END:"
274 (org-element-property :block-name dynamic-block
)
275 (let ((args (org-element-property :arguments dynamic-block
)))
276 (and args
(concat " " args
)))
280 ;;;; Footnote Definition
282 (defun org-element-footnote-definition-parser ()
283 "Parse a footnote definition.
285 Return a list whose CAR is `footnote-definition' and CDR is
286 a plist containing `:label', `:begin' `:end', `:contents-begin',
287 `:contents-end' and `:post-blank' keywords.
289 Assume point is at the beginning of the footnote definition."
291 (looking-at org-footnote-definition-re
)
292 (let* ((label (org-match-string-no-properties 1))
293 (keywords (org-element-collect-affiliated-keywords))
294 (begin (car keywords
))
295 (contents-begin (progn (search-forward "]")
296 (org-skip-whitespace)
298 (contents-end (if (progn
301 (concat org-outline-regexp-bol
"\\|"
302 org-footnote-definition-re
"\\|"
306 (end (progn (org-skip-whitespace)
307 (if (eobp) (point) (point-at-bol)))))
308 `(footnote-definition
312 :contents-begin
,contents-begin
313 :contents-end
,contents-end
314 :post-blank
,(count-lines contents-end end
)
315 ,@(cadr keywords
))))))
317 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
318 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
319 CONTENTS is the contents of the footnote-definition."
320 (concat (format "[%s]" (org-element-property :label footnote-definition
))
327 (defun org-element-headline-parser (&optional raw-secondary-p
)
330 Return a list whose CAR is `headline' and CDR is a plist
331 containing `:raw-value', `:title', `:begin', `:end',
332 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
333 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
334 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
335 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
338 The plist also contains any property set in the property drawer,
339 with its name in lowercase, the underscores replaced with hyphens
340 and colons at the beginning (i.e. `:custom-id').
342 When RAW-SECONDARY-P is non-nil, headline's title will not be
343 parsed as a secondary string, but as a plain string instead.
345 Assume point is at beginning of the headline."
347 (let* ((components (org-heading-components))
348 (level (nth 1 components
))
349 (todo (nth 2 components
))
351 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
352 (tags (nth 5 components
))
353 (raw-value (nth 4 components
))
355 (let ((case-fold-search nil
))
356 (string-match (format "^%s +" org-quote-string
) raw-value
)))
358 (let ((case-fold-search nil
))
359 (string-match (format "^%s +" org-comment-string
) raw-value
)))
362 (let ((case-fold-search nil
))
363 (string-match (format ":%s:" org-archive-tag
) tags
))))
364 (footnote-section-p (and org-footnote-section
365 (string= org-footnote-section raw-value
)))
366 (standard-props (let (plist)
369 (let ((p-name (downcase (car p
))))
370 (while (string-match "_" p-name
)
372 (replace-match "-" nil nil p-name
)))
373 (setq p-name
(intern (concat ":" p-name
)))
375 (plist-put plist p-name
(cdr p
)))))
376 (org-entry-properties nil
'standard
))
378 (time-props (org-entry-properties nil
'special
"CLOCK"))
379 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
380 (deadline (cdr (assoc "DEADLINE" time-props
)))
381 (clock (cdr (assoc "CLOCK" time-props
)))
382 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
384 (pos-after-head (save-excursion (forward-line) (point)))
385 (contents-begin (save-excursion (forward-line)
386 (org-skip-whitespace)
387 (if (eobp) (point) (point-at-bol))))
388 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
389 (end (progn (goto-char (org-end-of-subtree t t
))))
390 (contents-end (progn (skip-chars-backward " \r\t\n")
394 ;; Clean RAW-VALUE from any quote or comment string.
395 (when (or quotedp commentedp
)
397 (replace-regexp-in-string
398 (concat "\\(" org-quote-string
"\\|" org-comment-string
"\\) +")
401 ;; Clean TAGS from archive tag, if any.
404 (and (not (string= tags
(format ":%s:" org-archive-tag
)))
405 (replace-regexp-in-string
406 (concat org-archive-tag
":") "" tags
)))
407 (when (string= tags
":") (setq tags nil
)))
410 (if raw-secondary-p raw-value
411 (org-element-parse-secondary-string
412 raw-value
(org-element-restriction 'headline
))))
414 (:raw-value
,raw-value
418 :pre-blank
,(count-lines pos-after-head contents-begin
)
420 :contents-begin
,contents-begin
421 :contents-end
,contents-end
423 :priority
,(nth 3 components
)
426 :todo-type
,todo-type
427 :scheduled
,scheduled
429 :timestamp
,timestamp
431 :post-blank
,(count-lines contents-end end
)
432 :footnote-section-p
,footnote-section-p
433 :archivedp
,archivedp
434 :commentedp
,commentedp
436 ,@standard-props
)))))
438 (defun org-element-headline-interpreter (headline contents
)
439 "Interpret HEADLINE element as Org syntax.
440 CONTENTS is the contents of the element."
441 (let* ((level (org-element-property :level headline
))
442 (todo (org-element-property :todo-keyword headline
))
443 (priority (org-element-property :priority headline
))
444 (title (org-element-interpret-secondary
445 (org-element-property :title headline
)))
446 (tags (let ((tag-string (org-element-property :tags headline
))
447 (archivedp (org-element-property :archivedp headline
)))
449 ((and (not tag-string
) archivedp
)
450 (format ":%s:" org-archive-tag
))
451 (archivedp (concat ":" org-archive-tag tag-string
))
453 (commentedp (org-element-property :commentedp headline
))
454 (quotedp (org-element-property :quotedp headline
))
455 (pre-blank (org-element-property :pre-blank headline
))
456 (heading (concat (make-string level ?
*)
457 (and todo
(concat " " todo
))
458 (and quotedp
(concat " " org-quote-string
))
459 (and commentedp
(concat " " org-comment-string
))
461 (format " [#%s]" (char-to-string priority
)))
462 (cond ((and org-footnote-section
463 (org-element-property
464 :footnote-section-p headline
))
465 (concat " " org-footnote-section
))
466 (title (concat " " title
))))))
471 ((zerop org-tags-column
) (format " %s" tags
))
472 ((< org-tags-column
0)
475 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
480 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
482 (make-string (1+ pre-blank
) 10)
488 (defun org-element-inlinetask-parser (&optional raw-secondary-p
)
489 "Parse an inline task.
491 Return a list whose CAR is `inlinetask' and CDR is a plist
492 containing `:title', `:begin', `:end', `:hiddenp',
493 `:contents-begin' and `:contents-end', `:level', `:priority',
494 `:tags', `:todo-keyword', `:todo-type', `:scheduled',
495 `:deadline', `:timestamp', `:clock' and `:post-blank' keywords.
497 The plist also contains any property set in the property drawer,
498 with its name in lowercase, the underscores replaced with hyphens
499 and colons at the beginning (i.e. `:custom-id').
501 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
502 title will not be parsed as a secondary string, but as a plain
505 Assume point is at beginning of the inline task."
507 (let* ((keywords (org-element-collect-affiliated-keywords))
508 (begin (car keywords
))
509 (components (org-heading-components))
510 (todo (nth 2 components
))
512 (if (member todo org-done-keywords
) 'done
'todo
)))
513 (title (if raw-secondary-p
(nth 4 components
)
514 (org-element-parse-secondary-string
516 (org-element-restriction 'inlinetask
))))
517 (standard-props (let (plist)
520 (let ((p-name (downcase (car p
))))
521 (while (string-match "_" p-name
)
523 (replace-match "-" nil nil p-name
)))
524 (setq p-name
(intern (concat ":" p-name
)))
526 (plist-put plist p-name
(cdr p
)))))
527 (org-entry-properties nil
'standard
))
529 (time-props (org-entry-properties nil
'special
"CLOCK"))
530 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
531 (deadline (cdr (assoc "DEADLINE" time-props
)))
532 (clock (cdr (assoc "CLOCK" time-props
)))
533 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
534 (contents-begin (save-excursion (forward-line) (point)))
535 (hidden (org-truely-invisible-p))
536 (pos-before-blank (org-inlinetask-goto-end))
537 ;; In the case of a single line task, CONTENTS-BEGIN and
538 ;; CONTENTS-END might overlap.
539 (contents-end (max contents-begin
540 (if (not (bolp)) (point-at-bol)
541 (save-excursion (forward-line -
1) (point)))))
542 (end (progn (org-skip-whitespace)
543 (if (eobp) (point) (point-at-bol)))))
548 :hiddenp
,(and (> contents-end contents-begin
) hidden
)
549 :contents-begin
,contents-begin
550 :contents-end
,contents-end
551 :level
,(nth 1 components
)
552 :priority
,(nth 3 components
)
553 :tags
,(nth 5 components
)
555 :todo-type
,todo-type
556 :scheduled
,scheduled
558 :timestamp
,timestamp
560 :post-blank
,(count-lines pos-before-blank end
)
562 ,@(cadr keywords
))))))
564 (defun org-element-inlinetask-interpreter (inlinetask contents
)
565 "Interpret INLINETASK element as Org syntax.
566 CONTENTS is the contents of inlinetask."
567 (let* ((level (org-element-property :level inlinetask
))
568 (todo (org-element-property :todo-keyword inlinetask
))
569 (priority (org-element-property :priority inlinetask
))
570 (title (org-element-interpret-secondary
571 (org-element-property :title inlinetask
)))
572 (tags (org-element-property :tags inlinetask
))
573 (task (concat (make-string level ?
*)
574 (and todo
(concat " " todo
))
576 (format " [#%s]" (char-to-string priority
)))
577 (and title
(concat " " title
)))))
582 ((zerop org-tags-column
) (format " %s" tags
))
583 ((< org-tags-column
0)
586 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
591 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
593 ;; Prefer degenerate inlinetasks when there are no
598 (make-string level ?
*) " END")))))
603 (defun org-element-item-parser (struct &optional raw-secondary-p
)
606 STRUCT is the structure of the plain list.
608 Return a list whose CAR is `item' and CDR is a plist containing
609 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
610 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
611 `:post-blank' keywords.
613 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
614 any, will not be parsed as a secondary string, but as a plain
617 Assume point is at the beginning of the item."
620 (let* ((begin (point))
621 (bullet (org-list-get-bullet (point) struct
))
622 (checkbox (let ((box (org-list-get-checkbox begin struct
)))
623 (cond ((equal "[ ]" box
) 'off
)
624 ((equal "[X]" box
) 'on
)
625 ((equal "[-]" box
) 'trans
))))
626 (counter (let ((c (org-list-get-counter begin struct
)))
629 ((string-match "[A-Za-z]" c
)
630 (- (string-to-char (upcase (match-string 0 c
)))
632 ((string-match "[0-9]+" c
)
633 (string-to-number (match-string 0 c
))))))
635 (let ((raw-tag (org-list-get-tag begin struct
)))
637 (if raw-secondary-p raw-tag
638 (org-element-parse-secondary-string
639 raw-tag
(org-element-restriction 'item
))))))
640 (end (org-list-get-item-end begin struct
))
641 (contents-begin (progn (looking-at org-list-full-item-re
)
642 (goto-char (match-end 0))
643 (org-skip-whitespace)
644 ;; If first line isn't empty,
645 ;; contents really start at the text
646 ;; after item's meta-data.
647 (if (= (point-at-bol) begin
) (point)
649 (hidden (progn (forward-line)
650 (and (not (= (point) end
))
651 (org-truely-invisible-p))))
652 (contents-end (progn (goto-char end
)
653 (skip-chars-backward " \r\t\n")
660 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
661 ;; up in the case of an empty item separated
662 ;; from the next by a blank line. Thus, ensure
663 ;; the former is always the smallest of two.
664 :contents-begin
,(min contents-begin contents-end
)
665 :contents-end
,(max contents-begin contents-end
)
671 :post-blank
,(count-lines contents-end end
))))))
673 (defun org-element-item-interpreter (item contents
)
674 "Interpret ITEM element as Org syntax.
675 CONTENTS is the contents of the element."
677 (let* ((beg (org-element-property :begin item
))
678 (struct (org-element-property :structure item
))
679 (pre (org-list-prevs-alist struct
))
680 (bul (org-element-property :bullet item
)))
681 (org-list-bullet-string
682 (if (not (eq (org-list-get-list-type beg struct pre
) 'ordered
)) "-"
686 (org-list-get-item-number
687 beg struct pre
(org-list-parents-alist struct
))))))
690 (if (eq org-plain-list-ordered-item-terminator ?\
)) ")"
692 (checkbox (org-element-property :checkbox item
))
693 (counter (org-element-property :counter item
))
694 (tag (let ((tag (org-element-property :tag item
)))
695 (and tag
(org-element-interpret-secondary tag
))))
696 ;; Compute indentation.
697 (ind (make-string (length bullet
) 32)))
701 (and counter
(format "[@%d] " counter
))
703 ((eq checkbox
'on
) "[X] ")
704 ((eq checkbox
'off
) "[ ] ")
705 ((eq checkbox
'trans
) "[-] "))
706 (and tag
(format "%s :: " tag
))
708 (replace-regexp-in-string "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))))
713 (defun org-element-plain-list-parser (&optional structure
)
716 Optional argument STRUCTURE, when non-nil, is the structure of
717 the plain list being parsed.
719 Return a list whose CAR is `plain-list' and CDR is a plist
720 containing `:type', `:begin', `:end', `:contents-begin' and
721 `:contents-end', `:level', `:structure' and `:post-blank'
724 Assume point is at one of the list items."
726 (let* ((struct (or structure
(org-list-struct)))
727 (prevs (org-list-prevs-alist struct
))
728 (parents (org-list-parents-alist struct
))
729 (type (org-list-get-list-type (point) struct prevs
))
730 (contents-begin (goto-char
731 (org-list-get-list-begin (point) struct prevs
)))
732 (keywords (org-element-collect-affiliated-keywords))
733 (begin (car keywords
))
734 (contents-end (goto-char
735 (org-list-get-list-end (point) struct prevs
)))
736 (end (save-excursion (org-skip-whitespace)
737 (if (eobp) (point) (point-at-bol))))
740 (let ((item contents-begin
))
743 (org-list-get-list-begin item struct prevs
)
746 ;; Blank lines below list belong to the top-level list only.
748 (setq end
(min (org-list-get-bottom-point struct
)
749 (progn (org-skip-whitespace)
750 (if (eobp) (point) (point-at-bol))))))
756 :contents-begin
,contents-begin
757 :contents-end
,contents-end
760 :post-blank
,(count-lines contents-end end
)
761 ,@(cadr keywords
))))))
763 (defun org-element-plain-list-interpreter (plain-list contents
)
764 "Interpret PLAIN-LIST element as Org syntax.
765 CONTENTS is the contents of the element."
771 (defun org-element-quote-block-parser ()
772 "Parse a quote block.
774 Return a list whose CAR is `quote-block' and CDR is a plist
775 containing `:begin', `:end', `:hiddenp', `:contents-begin',
776 `:contents-end' and `:post-blank' keywords.
778 Assume point is at beginning or end of the block."
780 (let* ((case-fold-search t
)
784 (concat "^[ \t]*#\\+BEGIN_QUOTE") nil t
)
785 (org-element-collect-affiliated-keywords)))
786 (begin (car keywords
))
787 (contents-begin (progn (forward-line) (point)))
788 (hidden (org-truely-invisible-p))
789 (contents-end (progn (re-search-forward
790 (concat "^[ \t]*#\\+END_QUOTE") nil t
)
792 (pos-before-blank (progn (forward-line) (point)))
793 (end (progn (org-skip-whitespace)
794 (if (eobp) (point) (point-at-bol)))))
799 :contents-begin
,contents-begin
800 :contents-end
,contents-end
801 :post-blank
,(count-lines pos-before-blank end
)
802 ,@(cadr keywords
))))))
804 (defun org-element-quote-block-interpreter (quote-block contents
)
805 "Interpret QUOTE-BLOCK element as Org syntax.
806 CONTENTS is the contents of the element."
807 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
812 (defun org-element-section-parser ()
815 Return a list whose CAR is `section' and CDR is a plist
816 containing `:begin', `:end', `:contents-begin', `contents-end'
817 and `:post-blank' keywords."
819 ;; Beginning of section is the beginning of the first non-blank
820 ;; line after previous headline.
821 (org-with-limited-levels
824 (outline-previous-heading)
825 (if (not (org-at-heading-p)) (point)
826 (forward-line) (org-skip-whitespace) (point-at-bol))))
827 (end (progn (outline-next-heading) (point)))
828 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
834 :contents-begin
,begin
835 :contents-end
,pos-before-blank
836 :post-blank
,(count-lines pos-before-blank end
)))))))
838 (defun org-element-section-interpreter (section contents
)
839 "Interpret SECTION element as Org syntax.
840 CONTENTS is the contents of the element."
846 (defun org-element-special-block-parser ()
847 "Parse a special block.
849 Return a list whose CAR is `special-block' and CDR is a plist
850 containing `:type', `:begin', `:end', `:hiddenp',
851 `:contents-begin', `:contents-end' and `:post-blank' keywords.
853 Assume point is at beginning or end of the block."
855 (let* ((case-fold-search t
)
856 (type (progn (looking-at
857 "[ \t]*#\\+\\(?:BEGIN\\|END\\)_\\([-A-Za-z0-9]+\\)")
858 (org-match-string-no-properties 1)))
862 (concat "^[ \t]*#\\+BEGIN_" type
) nil t
)
863 (org-element-collect-affiliated-keywords)))
864 (begin (car keywords
))
865 (contents-begin (progn (forward-line) (point)))
866 (hidden (org-truely-invisible-p))
867 (contents-end (progn (re-search-forward
868 (concat "^[ \t]*#\\+END_" type
) nil t
)
870 (pos-before-blank (progn (forward-line) (point)))
871 (end (progn (org-skip-whitespace)
872 (if (eobp) (point) (point-at-bol)))))
878 :contents-begin
,contents-begin
879 :contents-end
,contents-end
880 :post-blank
,(count-lines pos-before-blank end
)
881 ,@(cadr keywords
))))))
883 (defun org-element-special-block-interpreter (special-block contents
)
884 "Interpret SPECIAL-BLOCK element as Org syntax.
885 CONTENTS is the contents of the element."
886 (let ((block-type (org-element-property :type special-block
)))
887 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
893 ;; For each element, a parser and an interpreter are also defined.
894 ;; Both follow the same naming convention used for greater elements.
896 ;; Also, as for greater elements, adding a new element type is done
897 ;; through the following steps: implement a parser and an interpreter,
898 ;; tweak `org-element-current-element' so that it recognizes the new
899 ;; type and add that new type to `org-element-all-elements'.
901 ;; As a special case, when the newly defined type is a block type,
902 ;; `org-element-non-recursive-block-alist' has to be modified
908 (defun org-element-babel-call-parser ()
911 Return a list whose CAR is `babel-call' and CDR is a plist
912 containing `:begin', `:end', `:info' and `:post-blank' as
915 (let ((info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
916 (org-babel-lob-get-info)))
917 (begin (point-at-bol))
918 (pos-before-blank (progn (forward-line) (point)))
919 (end (progn (org-skip-whitespace)
920 (if (eobp) (point) (point-at-bol)))))
925 :post-blank
,(count-lines pos-before-blank end
))))))
927 (defun org-element-babel-call-interpreter (babel-call contents
)
928 "Interpret BABEL-CALL element as Org syntax.
930 (let* ((babel-info (org-element-property :info babel-call
))
931 (main (car babel-info
))
932 (post-options (nth 1 babel-info
)))
934 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
935 ;; Remove redundant square brackets.
936 (replace-match (match-string 1 main
) nil nil main
))
937 (and post-options
(format "[%s]" post-options
)))))
942 (defun org-element-clock-parser ()
945 Return a list whose CAR is `clock' and CDR is a plist containing
946 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
949 (let* ((case-fold-search nil
)
951 (value (progn (search-forward org-clock-string
(line-end-position) t
)
952 (org-skip-whitespace)
953 (looking-at "\\[.*\\]")
954 (org-match-string-no-properties 0)))
955 (time (and (progn (goto-char (match-end 0))
956 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
957 (org-match-string-no-properties 1)))
958 (status (if time
'closed
'running
))
959 (post-blank (let ((before-blank (progn (forward-line) (point))))
960 (org-skip-whitespace)
961 (unless (eobp) (beginning-of-line))
962 (count-lines before-blank
(point))))
964 `(clock (:status
,status
969 :post-blank
,post-blank
)))))
971 (defun org-element-clock-interpreter (clock contents
)
972 "Interpret CLOCK element as Org syntax.
974 (concat org-clock-string
" "
975 (org-element-property :value clock
)
976 (let ((time (org-element-property :time clock
)))
981 (org-split-string time
":")))))))
986 (defun org-element-comment-parser ()
989 Return a list whose CAR is `comment' and CDR is a plist
990 containing `:begin', `:end', `:value' and `:post-blank'
992 (let (beg-coms begin end end-coms keywords
)
995 ;; First type of comment: comments at column 0.
996 (let ((comment-re "^\\([^#]\\|#\\+[a-z]\\)"))
998 (re-search-backward comment-re nil
'move
)
999 (if (bobp) (setq keywords nil beg-coms
(point))
1001 (setq keywords
(org-element-collect-affiliated-keywords)
1003 (re-search-forward comment-re nil
'move
)
1004 (setq end-coms
(if (eobp) (point) (match-beginning 0))))
1005 ;; Second type of comment: indented comments.
1006 (let ((comment-re "[ \t]*#\\+\\(?: \\|$\\)"))
1008 (while (and (not (bobp)) (looking-at comment-re
))
1010 (unless (looking-at comment-re
) (forward-line)))
1011 (setq beg-coms
(point))
1012 (setq keywords
(org-element-collect-affiliated-keywords))
1013 ;; Get comments ending. This may not be accurate if
1014 ;; commented lines within an item are followed by commented
1015 ;; lines outside of the list. Though, parser will always
1016 ;; get it right as it already knows surrounding element and
1017 ;; has narrowed buffer to its contents.
1018 (while (looking-at comment-re
) (forward-line))
1019 (setq end-coms
(point))))
1020 ;; Find position after blank.
1021 (goto-char end-coms
)
1022 (org-skip-whitespace)
1023 (setq end
(if (eobp) (point) (point-at-bol))))
1025 (:begin
,(or (car keywords
) beg-coms
)
1027 :value
,(buffer-substring-no-properties beg-coms end-coms
)
1028 :post-blank
,(count-lines end-coms end
)
1029 ,@(cadr keywords
)))))
1031 (defun org-element-comment-interpreter (comment contents
)
1032 "Interpret COMMENT element as Org syntax.
1034 (org-element-property :value comment
))
1039 (defun org-element-comment-block-parser ()
1040 "Parse an export block.
1042 Return a list whose CAR is `comment-block' and CDR is a plist
1043 containing `:begin', `:end', `:hiddenp', `:value' and
1044 `:post-blank' keywords."
1047 (let* ((case-fold-search t
)
1049 (re-search-backward "^[ \t]*#\\+BEGIN_COMMENT" nil t
)
1050 (org-element-collect-affiliated-keywords)))
1051 (begin (car keywords
))
1052 (contents-begin (progn (forward-line) (point)))
1053 (hidden (org-truely-invisible-p))
1054 (contents-end (progn (re-search-forward
1055 "^[ \t]*#\\+END_COMMENT" nil t
)
1057 (pos-before-blank (progn (forward-line) (point)))
1058 (end (progn (org-skip-whitespace)
1059 (if (eobp) (point) (point-at-bol))))
1060 (value (buffer-substring-no-properties contents-begin contents-end
)))
1066 :post-blank
,(count-lines pos-before-blank end
)
1067 ,@(cadr keywords
))))))
1069 (defun org-element-comment-block-interpreter (comment-block contents
)
1070 "Interpret COMMENT-BLOCK element as Org syntax.
1072 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1073 (org-remove-indentation (org-element-property :value comment-block
))))
1078 (defun org-element-example-block-parser ()
1079 "Parse an example block.
1081 Return a list whose CAR is `example-block' and CDR is a plist
1082 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1083 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1084 `:switches', `:value' and `:post-blank' keywords."
1087 (let* ((case-fold-search t
)
1090 "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?" nil t
)
1091 (org-match-string-no-properties 1)))
1092 ;; Switches analysis
1093 (number-lines (cond ((not switches
) nil
)
1094 ((string-match "-n\\>" switches
) 'new
)
1095 ((string-match "+n\\>" switches
) 'continued
)))
1096 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1097 ;; Should labels be retained in (or stripped from) example
1101 (not (string-match "-r\\>" switches
))
1102 (and number-lines
(string-match "-k\\>" switches
))))
1103 ;; What should code-references use - labels or
1107 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1108 (label-fmt (and switches
1109 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1110 (match-string 1 switches
)))
1111 ;; Standard block parsing.
1112 (keywords (org-element-collect-affiliated-keywords))
1113 (begin (car keywords
))
1114 (contents-begin (progn (forward-line) (point)))
1115 (hidden (org-truely-invisible-p))
1116 (contents-end (progn
1117 (re-search-forward "^[ \t]*#\\+END_EXAMPLE" nil t
)
1119 (value (buffer-substring-no-properties contents-begin contents-end
))
1120 (pos-before-blank (progn (forward-line) (point)))
1121 (end (progn (org-skip-whitespace)
1122 (if (eobp) (point) (point-at-bol)))))
1128 :number-lines
,number-lines
1129 :preserve-indent
,preserve-indent
1130 :retain-labels
,retain-labels
1131 :use-labels
,use-labels
1132 :label-fmt
,label-fmt
1134 :post-blank
,(count-lines pos-before-blank end
)
1135 ,@(cadr keywords
))))))
1137 (defun org-element-example-block-interpreter (example-block contents
)
1138 "Interpret EXAMPLE-BLOCK element as Org syntax.
1140 (let ((options (org-element-property :options example-block
)))
1141 (concat "#+BEGIN_EXAMPLE" (and options
(concat " " options
)) "\n"
1142 (org-remove-indentation
1143 (org-element-property :value example-block
))
1149 (defun org-element-export-block-parser ()
1150 "Parse an export block.
1152 Return a list whose CAR is `export-block' and CDR is a plist
1153 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1154 `:post-blank' keywords."
1157 (let* ((case-fold-search t
)
1159 (type (progn (re-search-backward
1160 (concat "[ \t]*#\\+BEGIN_"
1161 (org-re "\\([[:alnum:]]+\\)")))
1162 (upcase (org-match-string-no-properties 1))))
1163 (keywords (org-element-collect-affiliated-keywords))
1164 (begin (car keywords
))
1165 (contents-begin (progn (forward-line) (point)))
1166 (hidden (org-truely-invisible-p))
1167 (contents-end (progn (re-search-forward
1168 (concat "^[ \t]*#\\+END_" type
) nil t
)
1170 (pos-before-blank (progn (forward-line) (point)))
1171 (end (progn (org-skip-whitespace)
1172 (if (eobp) (point) (point-at-bol))))
1173 (value (buffer-substring-no-properties contents-begin contents-end
)))
1180 :post-blank
,(count-lines pos-before-blank end
)
1181 ,@(cadr keywords
))))))
1183 (defun org-element-export-block-interpreter (export-block contents
)
1184 "Interpret EXPORT-BLOCK element as Org syntax.
1186 (let ((type (org-element-property :type export-block
)))
1187 (concat (format "#+BEGIN_%s\n" type
)
1188 (org-element-property :value export-block
)
1189 (format "#+END_%s" type
))))
1194 (defun org-element-fixed-width-parser ()
1195 "Parse a fixed-width section.
1197 Return a list whose CAR is `fixed-width' and CDR is a plist
1198 containing `:begin', `:end', `:value' and `:post-blank'
1200 (let ((fixed-re "[ \t]*:\\( \\|$\\)")
1201 beg-area begin end value pos-before-blank keywords
)
1203 ;; Move to the beginning of the fixed-width area.
1205 (while (and (not (bobp)) (looking-at fixed-re
))
1207 (unless (looking-at fixed-re
) (forward-line 1)))
1208 (setq beg-area
(point))
1209 ;; Get affiliated keywords, if any.
1210 (setq keywords
(org-element-collect-affiliated-keywords))
1211 ;; Store true beginning of element.
1212 (setq begin
(car keywords
))
1213 ;; Get ending of fixed-width area. If point is in a list,
1214 ;; ensure to not get outside of it.
1215 (let* ((itemp (org-in-item-p))
1217 (org-list-get-bottom-point
1218 (save-excursion (goto-char itemp
) (org-list-struct)))
1220 (while (and (looking-at fixed-re
) (< (point) max-pos
))
1222 (setq pos-before-blank
(point))
1223 ;; Find position after blank
1224 (org-skip-whitespace)
1225 (setq end
(if (eobp) (point) (point-at-bol)))
1227 (setq value
(buffer-substring-no-properties beg-area pos-before-blank
)))
1232 :post-blank
,(count-lines pos-before-blank end
)
1233 ,@(cadr keywords
)))))
1235 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1236 "Interpret FIXED-WIDTH element as Org syntax.
1238 (org-remove-indentation (org-element-property :value fixed-width
)))
1241 ;;;; Horizontal Rule
1243 (defun org-element-horizontal-rule-parser ()
1244 "Parse an horizontal rule.
1246 Return a list whose CAR is `horizontal-rule' and CDR is
1247 a plist containing `:begin', `:end' and `:post-blank'
1250 (let* ((keywords (org-element-collect-affiliated-keywords))
1251 (begin (car keywords
))
1252 (post-hr (progn (forward-line) (point)))
1253 (end (progn (org-skip-whitespace)
1254 (if (eobp) (point) (point-at-bol)))))
1258 :post-blank
,(count-lines post-hr end
)
1259 ,@(cadr keywords
))))))
1261 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1262 "Interpret HORIZONTAL-RULE element as Org syntax.
1269 (defun org-element-keyword-parser ()
1270 "Parse a keyword at point.
1272 Return a list whose CAR is `keyword' and CDR is a plist
1273 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1276 (let* ((begin (point))
1277 (key (progn (looking-at
1278 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1279 (upcase (org-match-string-no-properties 1))))
1280 (value (org-trim (buffer-substring-no-properties
1281 (match-end 0) (point-at-eol))))
1282 (pos-before-blank (progn (forward-line) (point)))
1283 (end (progn (org-skip-whitespace)
1284 (if (eobp) (point) (point-at-bol)))))
1290 :post-blank
,(count-lines pos-before-blank end
))))))
1292 (defun org-element-keyword-interpreter (keyword contents
)
1293 "Interpret KEYWORD element as Org syntax.
1296 (org-element-property :key keyword
)
1297 (org-element-property :value keyword
)))
1300 ;;;; Latex Environment
1302 (defun org-element-latex-environment-parser ()
1303 "Parse a LaTeX environment.
1305 Return a list whose CAR is `latex-environment' and CDR is a plist
1306 containing `:begin', `:end', `:value' and `:post-blank'
1310 (let* ((case-fold-search t
)
1311 (contents-begin (re-search-backward "^[ \t]*\\\\begin" nil t
))
1312 (keywords (org-element-collect-affiliated-keywords))
1313 (begin (car keywords
))
1314 (contents-end (progn (re-search-forward "^[ \t]*\\\\end")
1317 (value (buffer-substring-no-properties contents-begin contents-end
))
1318 (end (progn (org-skip-whitespace)
1319 (if (eobp) (point) (point-at-bol)))))
1324 :post-blank
,(count-lines contents-end end
)
1325 ,@(cadr keywords
))))))
1327 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1328 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1330 (org-element-property :value latex-environment
))
1335 (defun org-element-paragraph-parser ()
1338 Return a list whose CAR is `paragraph' and CDR is a plist
1339 containing `:begin', `:end', `:contents-begin' and
1340 `:contents-end' and `:post-blank' keywords.
1342 Assume point is at the beginning of the paragraph."
1344 (let* ((contents-begin (point))
1345 (keywords (org-element-collect-affiliated-keywords))
1346 (begin (car keywords
))
1347 (contents-end (progn
1349 (if (re-search-forward
1350 org-element-paragraph-separate nil
'm
)
1351 (progn (forward-line -
1) (end-of-line) (point))
1353 (pos-before-blank (progn (forward-line) (point)))
1354 (end (progn (org-skip-whitespace)
1355 (if (eobp) (point) (point-at-bol)))))
1359 :contents-begin
,contents-begin
1360 :contents-end
,contents-end
1361 :post-blank
,(count-lines pos-before-blank end
)
1362 ,@(cadr keywords
))))))
1364 (defun org-element-paragraph-interpreter (paragraph contents
)
1365 "Interpret PARAGRAPH element as Org syntax.
1366 CONTENTS is the contents of the element."
1372 (defun org-element-planning-parser ()
1375 Return a list whose CAR is `planning' and CDR is a plist
1376 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1377 and `:post-blank' keywords."
1379 (let* ((case-fold-search nil
)
1381 (post-blank (let ((before-blank (progn (forward-line) (point))))
1382 (org-skip-whitespace)
1383 (unless (eobp) (beginning-of-line))
1384 (count-lines before-blank
(point))))
1386 closed deadline scheduled
)
1388 (while (re-search-forward org-keyword-time-not-clock-regexp
1389 (line-end-position) t
)
1390 (goto-char (match-end 1))
1391 (org-skip-whitespace)
1392 (let ((time (buffer-substring-no-properties (point) (match-end 0)))
1393 (keyword (match-string 1)))
1394 (cond ((equal keyword org-closed-string
) (setq closed time
))
1395 ((equal keyword org-deadline-string
) (setq deadline time
))
1396 (t (setq scheduled time
)))))
1400 :scheduled
,scheduled
1403 :post-blank
,post-blank
)))))
1405 (defun org-element-planning-interpreter (planning contents
)
1406 "Interpret PLANNING element as Org syntax.
1411 (list (let ((closed (org-element-property :closed planning
)))
1412 (when closed
(concat org-closed-string
" " closed
)))
1413 (let ((deadline (org-element-property :deadline planning
)))
1414 (when deadline
(concat org-deadline-string
" " deadline
)))
1415 (let ((scheduled (org-element-property :scheduled planning
)))
1416 (when scheduled
(concat org-scheduled-string
" " scheduled
)))))
1420 ;;;; Property Drawer
1422 (defun org-element-property-drawer-parser ()
1423 "Parse a property drawer.
1425 Return a list whose CAR is `property-drawer' and CDR is a plist
1426 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1427 `:contents-end', `:properties' and `:post-blank' keywords."
1429 (let ((case-fold-search t
)
1430 (begin (progn (end-of-line)
1431 (re-search-backward org-property-start-re
)
1432 (match-beginning 0)))
1433 (contents-begin (progn (forward-line) (point)))
1434 (hidden (org-truely-invisible-p))
1435 (properties (let (val)
1436 (while (not (looking-at "^[ \t]*:END:"))
1439 "[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):"))
1440 (push (cons (match-string 1)
1443 (match-end 0) (point-at-eol))))
1447 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
1449 (pos-before-blank (progn (forward-line) (point)))
1450 (end (progn (org-skip-whitespace)
1451 (if (eobp) (point) (point-at-bol)))))
1456 :properties
,properties
1457 :post-blank
,(count-lines pos-before-blank end
))))))
1459 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1460 "Interpret PROPERTY-DRAWER element as Org syntax.
1462 (let ((props (org-element-property :properties property-drawer
)))
1465 (mapconcat (lambda (p)
1466 (format org-property-format
(format ":%s:" (car p
)) (cdr p
)))
1467 (nreverse props
) "\n")
1473 (defun org-element-quote-section-parser ()
1474 "Parse a quote section.
1476 Return a list whose CAR is `quote-section' and CDR is a plist
1477 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1479 Assume point is at beginning of the section."
1481 (let* ((begin (point))
1482 (end (progn (org-with-limited-levels (outline-next-heading))
1484 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1487 (value (buffer-substring-no-properties begin pos-before-blank
)))
1492 :post-blank
,(count-lines pos-before-blank end
))))))
1494 (defun org-element-quote-section-interpreter (quote-section contents
)
1495 "Interpret QUOTE-SECTION element as Org syntax.
1497 (org-element-property :value quote-section
))
1502 (defun org-element-src-block-parser ()
1505 Return a list whose CAR is `src-block' and CDR is a plist
1506 containing `:language', `:switches', `:parameters', `:begin',
1507 `:end', `:hiddenp', `:contents-begin', `:contents-end',
1508 `:number-lines', `:retain-labels', `:use-labels', `:label-fmt',
1509 `:preserve-indent', `:value' and `:post-blank' keywords."
1512 (let* ((case-fold-search t
)
1513 ;; Get position at beginning of block.
1517 "^[ \t]*#\\+BEGIN_SRC"
1518 "\\(?: +\\(\\S-+\\)\\)?" ; language
1519 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)*\\)" ; switches
1520 "\\(.*\\)[ \t]*$") ; parameters
1522 ;; Get language as a string.
1523 (language (org-match-string-no-properties 1))
1525 (parameters (org-trim (org-match-string-no-properties 3)))
1527 (switches (org-match-string-no-properties 2))
1528 ;; Switches analysis
1529 (number-lines (cond ((not switches
) nil
)
1530 ((string-match "-n\\>" switches
) 'new
)
1531 ((string-match "+n\\>" switches
) 'continued
)))
1532 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1533 (label-fmt (and switches
1534 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1535 (match-string 1 switches
)))
1536 ;; Should labels be retained in (or stripped from) src
1540 (not (string-match "-r\\>" switches
))
1541 (and number-lines
(string-match "-k\\>" switches
))))
1542 ;; What should code-references use - labels or
1546 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1547 ;; Get affiliated keywords.
1548 (keywords (org-element-collect-affiliated-keywords))
1549 ;; Get beginning position.
1550 (begin (car keywords
))
1551 ;; Get position at end of block.
1552 (contents-end (progn (re-search-forward "^[ \t]*#\\+END_SRC" nil t
)
1556 (value (buffer-substring-no-properties
1557 (save-excursion (goto-char contents-begin
)
1560 (match-beginning 0)))
1561 ;; Get position after ending blank lines.
1562 (end (progn (org-skip-whitespace)
1563 (if (eobp) (point) (point-at-bol))))
1564 ;; Get visibility status.
1565 (hidden (progn (goto-char contents-begin
)
1567 (org-truely-invisible-p))))
1569 (:language
,language
1571 :parameters
,parameters
1574 :number-lines
,number-lines
1575 :preserve-indent
,preserve-indent
1576 :retain-labels
,retain-labels
1577 :use-labels
,use-labels
1578 :label-fmt
,label-fmt
1581 :post-blank
,(count-lines contents-end end
)
1582 ,@(cadr keywords
))))))
1584 (defun org-element-src-block-interpreter (src-block contents
)
1585 "Interpret SRC-BLOCK element as Org syntax.
1587 (let ((lang (org-element-property :language src-block
))
1588 (switches (org-element-property :switches src-block
))
1589 (params (org-element-property :parameters src-block
))
1590 (value (let ((val (org-element-property :value src-block
)))
1593 (org-src-preserve-indentation val
)
1594 ((zerop org-edit-src-content-indentation
)
1595 (org-remove-indentation val
))
1597 (let ((ind (make-string
1598 org-edit-src-content-indentation
32)))
1599 (replace-regexp-in-string
1600 "\\(^\\)[ \t]*\\S-" ind
1601 (org-remove-indentation val
) nil nil
1)))))))
1602 (concat (format "#+BEGIN_SRC%s\n"
1603 (concat (and lang
(concat " " lang
))
1604 (and switches
(concat " " switches
))
1605 (and params
(concat " " params
))))
1612 (defun org-element-table-parser ()
1613 "Parse a table at point.
1615 Return a list whose CAR is `table' and CDR is a plist containing
1616 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
1617 `:contents-end', `:value' and `:post-blank' keywords."
1619 (let* ((case-fold-search t
)
1620 (table-begin (goto-char (org-table-begin t
)))
1621 (type (if (org-at-table.el-p
) 'table.el
'org
))
1622 (keywords (org-element-collect-affiliated-keywords))
1623 (begin (car keywords
))
1624 (table-end (goto-char (marker-position (org-table-end t
))))
1625 (tblfm (when (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
1626 (prog1 (org-match-string-no-properties 1)
1628 (pos-before-blank (point))
1629 (end (progn (org-skip-whitespace)
1630 (if (eobp) (point) (point-at-bol)))))
1636 ;; Only `org' tables have contents. `table.el'
1637 ;; tables use a `:value' property to store raw
1638 ;; table as a string.
1639 :contents-begin
,(and (eq type
'org
) table-begin
)
1640 :contents-end
,(and (eq type
'org
) table-end
)
1641 :value
,(and (eq type
'table.el
)
1642 (buffer-substring-no-properties
1643 table-begin table-end
))
1644 :post-blank
,(count-lines pos-before-blank end
)
1645 ,@(cadr keywords
))))))
1647 (defun org-element-table-interpreter (table contents
)
1648 "Interpret TABLE element as Org syntax.
1650 (if (eq (org-element-property :type table
) 'table.el
)
1651 (org-remove-indentation (org-element-property :value table
))
1652 (concat (with-temp-buffer (insert contents
)
1655 (when (org-element-property :tblfm table
)
1656 (format "#+TBLFM: " (org-element-property :tblfm table
))))))
1661 (defun org-element-table-row-parser ()
1662 "Parse table row at point.
1664 Return a list whose CAR is `table-row' and CDR is a plist
1665 containing `:begin', `:end', `:contents-begin', `:contents-end',
1666 `:type' and `:post-blank' keywords."
1668 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
1670 ;; A table rule has no contents. In that case, ensure
1671 ;; CONTENTS-BEGIN matches CONTENTS-END.
1672 (contents-begin (if (eq type
'standard
)
1673 (progn (search-forward "|") (point))
1675 (skip-chars-backward " \r\t\n")
1677 (contents-end (progn (end-of-line)
1678 (skip-chars-backward " \r\t\n")
1680 (end (progn (forward-line) (point))))
1685 :contents-begin
,contents-begin
1686 :contents-end
,contents-end
1689 (defun org-element-table-row-interpreter (table-row contents
)
1690 "Interpret TABLE-ROW element as Org syntax.
1691 CONTENTS is the contents of the table row."
1692 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
1693 (concat "| " contents
)))
1698 (defun org-element-verse-block-parser ()
1699 "Parse a verse block.
1701 Return a list whose CAR is `verse-block' and CDR is a plist
1702 containing `:begin', `:end', `:contents-begin', `:contents-end',
1703 `:hiddenp' and `:post-blank' keywords.
1705 Assume point is at beginning of the block."
1707 (let* ((case-fold-search t
)
1708 (keywords (org-element-collect-affiliated-keywords))
1709 (begin (car keywords
))
1710 (hidden (progn (forward-line) (org-truely-invisible-p)))
1711 (contents-begin (point))
1714 (re-search-forward (concat "^[ \t]*#\\+END_VERSE") nil t
)
1716 (pos-before-blank (progn (forward-line) (point)))
1717 (end (progn (org-skip-whitespace)
1718 (if (eobp) (point) (point-at-bol)))))
1722 :contents-begin
,contents-begin
1723 :contents-end
,contents-end
1725 :post-blank
,(count-lines pos-before-blank end
)
1726 ,@(cadr keywords
))))))
1728 (defun org-element-verse-block-interpreter (verse-block contents
)
1729 "Interpret VERSE-BLOCK element as Org syntax.
1730 CONTENTS is verse block contents."
1731 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
1737 ;; Unlike to elements, interstices can be found between objects.
1738 ;; That's why, along with the parser, successor functions are provided
1739 ;; for each object. Some objects share the same successor (i.e. `code'
1740 ;; and `verbatim' objects).
1742 ;; A successor must accept a single argument bounding the search. It
1743 ;; will return either a cons cell whose CAR is the object's type, as
1744 ;; a symbol, and CDR the position of its next occurrence, or nil.
1746 ;; Successors follow the naming convention:
1747 ;; org-element-NAME-successor, where NAME is the name of the
1748 ;; successor, as defined in `org-element-all-successors'.
1750 ;; Some object types (i.e. `emphasis') are recursive. Restrictions on
1751 ;; object types they can contain will be specified in
1752 ;; `org-element-object-restrictions'.
1754 ;; Adding a new type of object is simple. Implement a successor,
1755 ;; a parser, and an interpreter for it, all following the naming
1756 ;; convention. Register type in `org-element-all-objects' and
1757 ;; successor in `org-element-all-successors'. Maybe tweak
1758 ;; restrictions about it, and that's it.
1763 (defun org-element-bold-parser ()
1764 "Parse bold object at point.
1766 Return a list whose CAR is `bold' and CDR is a plist with
1767 `:begin', `:end', `:contents-begin' and `:contents-end' and
1768 `:post-blank' keywords.
1770 Assume point is at the first star marker."
1772 (unless (bolp) (backward-char 1))
1773 (looking-at org-emph-re
)
1774 (let ((begin (match-beginning 2))
1775 (contents-begin (match-beginning 4))
1776 (contents-end (match-end 4))
1777 (post-blank (progn (goto-char (match-end 2))
1778 (skip-chars-forward " \t")))
1783 :contents-begin
,contents-begin
1784 :contents-end
,contents-end
1785 :post-blank
,post-blank
)))))
1787 (defun org-element-bold-interpreter (bold contents
)
1788 "Interpret BOLD object as Org syntax.
1789 CONTENTS is the contents of the object."
1790 (format "*%s*" contents
))
1792 (defun org-element-text-markup-successor (limit)
1793 "Search for the next text-markup object.
1795 LIMIT bounds the search.
1797 Return value is a cons cell whose CAR is a symbol among `bold',
1798 `italic', `underline', `strike-through', `code' and `verbatim'
1799 and CDR is beginning position."
1801 (unless (bolp) (backward-char))
1802 (when (re-search-forward org-emph-re limit t
)
1803 (let ((marker (match-string 3)))
1805 ((equal marker
"*") 'bold
)
1806 ((equal marker
"/") 'italic
)
1807 ((equal marker
"_") 'underline
)
1808 ((equal marker
"+") 'strike-through
)
1809 ((equal marker
"~") 'code
)
1810 ((equal marker
"=") 'verbatim
)
1811 (t (error "Unknown marker at %d" (match-beginning 3))))
1812 (match-beginning 2))))))
1817 (defun org-element-code-parser ()
1818 "Parse code object at point.
1820 Return a list whose CAR is `code' and CDR is a plist with
1821 `:value', `:begin', `:end' and `:post-blank' keywords.
1823 Assume point is at the first tilde marker."
1825 (unless (bolp) (backward-char 1))
1826 (looking-at org-emph-re
)
1827 (let ((begin (match-beginning 2))
1828 (value (org-match-string-no-properties 4))
1829 (post-blank (progn (goto-char (match-end 2))
1830 (skip-chars-forward " \t")))
1836 :post-blank
,post-blank
)))))
1838 (defun org-element-code-interpreter (code contents
)
1839 "Interpret CODE object as Org syntax.
1841 (format "~%s~" (org-element-property :value code
)))
1846 (defun org-element-entity-parser ()
1847 "Parse entity at point.
1849 Return a list whose CAR is `entity' and CDR a plist with
1850 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1851 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1854 Assume point is at the beginning of the entity."
1856 (looking-at "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1857 (let* ((value (org-entity-get (match-string 1)))
1858 (begin (match-beginning 0))
1859 (bracketsp (string= (match-string 2) "{}"))
1860 (post-blank (progn (goto-char (match-end 1))
1861 (when bracketsp
(forward-char 2))
1862 (skip-chars-forward " \t")))
1866 :latex
,(nth 1 value
)
1867 :latex-math-p
,(nth 2 value
)
1868 :html
,(nth 3 value
)
1869 :ascii
,(nth 4 value
)
1870 :latin1
,(nth 5 value
)
1871 :utf-8
,(nth 6 value
)
1874 :use-brackets-p
,bracketsp
1875 :post-blank
,post-blank
)))))
1877 (defun org-element-entity-interpreter (entity contents
)
1878 "Interpret ENTITY object as Org syntax.
1881 (org-element-property :name entity
)
1882 (when (org-element-property :use-brackets-p entity
) "{}")))
1884 (defun org-element-latex-or-entity-successor (limit)
1885 "Search for the next latex-fragment or entity object.
1887 LIMIT bounds the search.
1889 Return value is a cons cell whose CAR is `entity' or
1890 `latex-fragment' and CDR is beginning position."
1892 (let ((matchers (plist-get org-format-latex-options
:matchers
))
1893 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1895 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"))
1896 (when (re-search-forward
1897 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
1901 (goto-char (match-beginning 0))
1902 (if (looking-at entity-re
)
1903 ;; Determine if it's a real entity or a LaTeX command.
1904 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
1905 (match-beginning 0))
1906 ;; No entity nor command: point is at a LaTeX fragment.
1907 ;; Determine its type to get the correct beginning position.
1908 (cons 'latex-fragment
1911 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
1914 (nth 2 (assoc e org-latex-regexps
))))))
1921 (defun org-element-export-snippet-parser ()
1922 "Parse export snippet at point.
1924 Return a list whose CAR is `export-snippet' and CDR a plist with
1925 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1928 Assume point is at the beginning of the snippet."
1930 (looking-at "<\\([-A-Za-z0-9]+\\)@")
1931 (let* ((begin (point))
1932 (back-end (org-match-string-no-properties 1))
1933 (inner-begin (match-end 0))
1936 (while (and (> count
0) (re-search-forward "[<>]" nil t
))
1937 (if (equal (match-string 0) "<") (incf count
) (decf count
)))
1939 (value (buffer-substring-no-properties inner-begin inner-end
))
1940 (post-blank (skip-chars-forward " \t"))
1943 (:back-end
,back-end
1947 :post-blank
,post-blank
)))))
1949 (defun org-element-export-snippet-interpreter (export-snippet contents
)
1950 "Interpret EXPORT-SNIPPET object as Org syntax.
1953 (org-element-property :back-end export-snippet
)
1954 (org-element-property :value export-snippet
)))
1956 (defun org-element-export-snippet-successor (limit)
1957 "Search for the next export-snippet object.
1959 LIMIT bounds the search.
1961 Return value is a cons cell whose CAR is `export-snippet' CDR is
1962 its beginning position."
1965 (while (re-search-forward "<[-A-Za-z0-9]+@" limit t
)
1967 (let ((beg (match-beginning 0))
1969 (while (re-search-forward "[<>]" limit t
)
1970 (if (equal (match-string 0) "<") (incf count
) (decf count
))
1972 (throw 'exit
(cons 'export-snippet beg
))))))))))
1975 ;;;; Footnote Reference
1977 (defun org-element-footnote-reference-parser ()
1978 "Parse footnote reference at point.
1980 Return a list whose CAR is `footnote-reference' and CDR a plist
1981 with `:label', `:type', `:inline-definition', `:begin', `:end'
1982 and `:post-blank' as keywords."
1984 (looking-at org-footnote-re
)
1985 (let* ((begin (point))
1986 (label (or (org-match-string-no-properties 2)
1987 (org-match-string-no-properties 3)
1988 (and (match-string 1)
1989 (concat "fn:" (org-match-string-no-properties 1)))))
1990 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
1991 (inner-begin (match-end 0))
1995 (while (and (> count
0) (re-search-forward "[][]" nil t
))
1996 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
1998 (post-blank (progn (goto-char (1+ inner-end
))
1999 (skip-chars-forward " \t")))
2002 (and (eq type
'inline
)
2003 (org-element-parse-secondary-string
2004 (buffer-substring inner-begin inner-end
)
2005 (org-element-restriction 'footnote-reference
)))))
2006 `(footnote-reference
2009 :inline-definition
,inline-definition
2012 :post-blank
,post-blank
)))))
2014 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
2015 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
2017 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
2020 (org-element-property :inline-definition footnote-reference
)))
2021 (if (not inline-def
) ""
2022 (concat ":" (org-element-interpret-secondary inline-def
))))))
2023 (format "[%s]" (concat label def
))))
2025 (defun org-element-footnote-reference-successor (limit)
2026 "Search for the next footnote-reference object.
2028 LIMIT bounds the search.
2030 Return value is a cons cell whose CAR is `footnote-reference' and
2031 CDR is beginning position."
2034 (while (re-search-forward org-footnote-re limit t
)
2036 (let ((beg (match-beginning 0))
2039 (while (re-search-forward "[][]" limit t
)
2040 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2042 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2045 ;;;; Inline Babel Call
2047 (defun org-element-inline-babel-call-parser ()
2048 "Parse inline babel call at point.
2050 Return a list whose CAR is `inline-babel-call' and CDR a plist
2051 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2053 Assume point is at the beginning of the babel call."
2055 (unless (bolp) (backward-char))
2056 (looking-at org-babel-inline-lob-one-liner-regexp
)
2057 (let ((info (save-match-data (org-babel-lob-get-info)))
2058 (begin (match-end 1))
2059 (post-blank (progn (goto-char (match-end 0))
2060 (skip-chars-forward " \t")))
2066 :post-blank
,post-blank
)))))
2068 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2069 "Interpret INLINE-BABEL-CALL object as Org syntax.
2071 (let* ((babel-info (org-element-property :info inline-babel-call
))
2072 (main-source (car babel-info
))
2073 (post-options (nth 1 babel-info
)))
2075 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2076 ;; Remove redundant square brackets.
2078 (match-string 1 main-source
) nil nil main-source
)
2080 (and post-options
(format "[%s]" post-options
)))))
2082 (defun org-element-inline-babel-call-successor (limit)
2083 "Search for the next inline-babel-call object.
2085 LIMIT bounds the search.
2087 Return value is a cons cell whose CAR is `inline-babel-call' and
2088 CDR is beginning position."
2090 ;; Use a simplified version of
2091 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
2092 (when (re-search-forward
2093 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
2095 (cons 'inline-babel-call
(match-beginning 0)))))
2098 ;;;; Inline Src Block
2100 (defun org-element-inline-src-block-parser ()
2101 "Parse inline source block at point.
2103 Return a list whose CAR is `inline-src-block' and CDR a plist
2104 with `:begin', `:end', `:language', `:value', `:parameters' and
2105 `:post-blank' as keywords.
2107 Assume point is at the beginning of the inline src block."
2109 (unless (bolp) (backward-char))
2110 (looking-at org-babel-inline-src-block-regexp
)
2111 (let ((begin (match-beginning 1))
2112 (language (org-match-string-no-properties 2))
2113 (parameters (org-match-string-no-properties 4))
2114 (value (org-match-string-no-properties 5))
2115 (post-blank (progn (goto-char (match-end 0))
2116 (skip-chars-forward " \t")))
2119 (:language
,language
2121 :parameters
,parameters
2124 :post-blank
,post-blank
)))))
2126 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2127 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2129 (let ((language (org-element-property :language inline-src-block
))
2130 (arguments (org-element-property :parameters inline-src-block
))
2131 (body (org-element-property :value inline-src-block
)))
2132 (format "src_%s%s{%s}"
2134 (if arguments
(format "[%s]" arguments
) "")
2137 (defun org-element-inline-src-block-successor (limit)
2138 "Search for the next inline-babel-call element.
2140 LIMIT bounds the search.
2142 Return value is a cons cell whose CAR is `inline-babel-call' and
2143 CDR is beginning position."
2145 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2146 (cons 'inline-src-block
(match-beginning 1)))))
2150 (defun org-element-italic-parser ()
2151 "Parse italic object at point.
2153 Return a list whose CAR is `italic' and CDR is a plist with
2154 `:begin', `:end', `:contents-begin' and `:contents-end' and
2155 `:post-blank' keywords.
2157 Assume point is at the first slash marker."
2159 (unless (bolp) (backward-char 1))
2160 (looking-at org-emph-re
)
2161 (let ((begin (match-beginning 2))
2162 (contents-begin (match-beginning 4))
2163 (contents-end (match-end 4))
2164 (post-blank (progn (goto-char (match-end 2))
2165 (skip-chars-forward " \t")))
2170 :contents-begin
,contents-begin
2171 :contents-end
,contents-end
2172 :post-blank
,post-blank
)))))
2174 (defun org-element-italic-interpreter (italic contents
)
2175 "Interpret ITALIC object as Org syntax.
2176 CONTENTS is the contents of the object."
2177 (format "/%s/" contents
))
2182 (defun org-element-latex-fragment-parser ()
2183 "Parse latex fragment at point.
2185 Return a list whose CAR is `latex-fragment' and CDR a plist with
2186 `:value', `:begin', `:end', and `:post-blank' as keywords.
2188 Assume point is at the beginning of the latex fragment."
2190 (let* ((begin (point))
2194 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2195 (when (or (looking-at latex-regexp
)
2199 (looking-at latex-regexp
))))
2200 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2201 (plist-get org-format-latex-options
:matchers
))
2202 ;; None found: it's a macro.
2203 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2205 (value (match-string-no-properties substring-match
))
2206 (post-blank (progn (goto-char (match-end substring-match
))
2207 (skip-chars-forward " \t")))
2213 :post-blank
,post-blank
)))))
2215 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2216 "Interpret LATEX-FRAGMENT object as Org syntax.
2218 (org-element-property :value latex-fragment
))
2222 (defun org-element-line-break-parser ()
2223 "Parse line break at point.
2225 Return a list whose CAR is `line-break', and CDR a plist with
2226 `:begin', `:end' and `:post-blank' keywords.
2228 Assume point is at the beginning of the line break."
2229 (let ((begin (point))
2230 (end (save-excursion (forward-line) (point))))
2231 `(line-break (:begin
,begin
:end
,end
:post-blank
0))))
2233 (defun org-element-line-break-interpreter (line-break contents
)
2234 "Interpret LINE-BREAK object as Org syntax.
2238 (defun org-element-line-break-successor (limit)
2239 "Search for the next line-break object.
2241 LIMIT bounds the search.
2243 Return value is a cons cell whose CAR is `line-break' and CDR is
2244 beginning position."
2246 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2247 (goto-char (match-beginning 1)))))
2248 ;; A line break can only happen on a non-empty line.
2249 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2250 (cons 'line-break beg
)))))
2255 (defun org-element-link-parser ()
2256 "Parse link at point.
2258 Return a list whose CAR is `link' and CDR a plist with `:type',
2259 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2260 `:contents-end' and `:post-blank' as keywords.
2262 Assume point is at the beginning of the link."
2264 (let ((begin (point))
2265 end contents-begin contents-end link-end post-blank path type
2268 ;; Type 1: Text targeted from a radio target.
2269 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2271 link-end
(match-end 0)
2272 path
(org-match-string-no-properties 0)))
2273 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2274 ((looking-at org-bracket-link-regexp
)
2275 (setq contents-begin
(match-beginning 3)
2276 contents-end
(match-end 3)
2277 link-end
(match-end 0)
2278 ;; RAW-LINK is the original link.
2279 raw-link
(org-match-string-no-properties 1)
2280 link
(org-link-expand-abbrev
2281 (replace-regexp-in-string
2282 " *\n *" " " (org-link-unescape raw-link
) t t
)))
2283 ;; Determine TYPE of link and set PATH accordingly.
2286 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2287 (setq type
"file" path link
))
2288 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2289 ((string-match org-link-re-with-space3 link
)
2290 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2291 ;; Id type: PATH is the id.
2292 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2293 (setq type
"id" path
(match-string 1 link
)))
2294 ;; Code-ref type: PATH is the name of the reference.
2295 ((string-match "^(\\(.*\\))$" link
)
2296 (setq type
"coderef" path
(match-string 1 link
)))
2297 ;; Custom-id type: PATH is the name of the custom id.
2298 ((= (aref link
0) ?
#)
2299 (setq type
"custom-id" path
(substring link
1)))
2300 ;; Fuzzy type: Internal link either matches a target, an
2301 ;; headline name or nothing. PATH is the target or headline's
2303 (t (setq type
"fuzzy" path link
))))
2304 ;; Type 3: Plain link, i.e. http://orgmode.org
2305 ((looking-at org-plain-link-re
)
2306 (setq raw-link
(org-match-string-no-properties 0)
2307 type
(org-match-string-no-properties 1)
2308 path
(org-match-string-no-properties 2)
2309 link-end
(match-end 0)))
2310 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2311 ((looking-at org-angle-link-re
)
2312 (setq raw-link
(buffer-substring-no-properties
2313 (match-beginning 1) (match-end 2))
2314 type
(org-match-string-no-properties 1)
2315 path
(org-match-string-no-properties 2)
2316 link-end
(match-end 0))))
2317 ;; In any case, deduce end point after trailing white space from
2318 ;; LINK-END variable.
2319 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2324 :raw-link
,(or raw-link path
)
2327 :contents-begin
,contents-begin
2328 :contents-end
,contents-end
2329 :post-blank
,post-blank
)))))
2331 (defun org-element-link-interpreter (link contents
)
2332 "Interpret LINK object as Org syntax.
2333 CONTENTS is the contents of the object, or nil."
2334 (let ((type (org-element-property :type link
))
2335 (raw-link (org-element-property :raw-link link
)))
2336 (if (string= type
"radio") raw-link
2339 (if contents
(format "[%s]" contents
) "")))))
2341 (defun org-element-link-successor (limit)
2342 "Search for the next link object.
2344 LIMIT bounds the search.
2346 Return value is a cons cell whose CAR is `link' and CDR is
2347 beginning position."
2350 (if (not org-target-link-regexp
) org-any-link-re
2351 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2352 (when (re-search-forward link-regexp limit t
)
2353 (cons 'link
(match-beginning 0))))))
2358 (defun org-element-macro-parser ()
2359 "Parse macro at point.
2361 Return a list whose CAR is `macro' and CDR a plist with `:key',
2362 `:args', `:begin', `:end', `:value' and `:post-blank' as
2365 Assume point is at the macro."
2367 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2368 (let ((begin (point))
2369 (key (downcase (org-match-string-no-properties 1)))
2370 (value (org-match-string-no-properties 0))
2371 (post-blank (progn (goto-char (match-end 0))
2372 (skip-chars-forward " \t")))
2374 (args (let ((args (org-match-string-no-properties 3)) args2
)
2376 (setq args
(org-split-string args
","))
2378 (while (string-match "\\\\\\'" (car args
))
2379 ;; Repair bad splits.
2380 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2383 (push (pop args
) args2
))
2384 (mapcar 'org-trim
(nreverse args2
))))))
2391 :post-blank
,post-blank
)))))
2393 (defun org-element-macro-interpreter (macro contents
)
2394 "Interpret MACRO object as Org syntax.
2396 (org-element-property :value macro
))
2398 (defun org-element-macro-successor (limit)
2399 "Search for the next macro object.
2401 LIMIT bounds the search.
2403 Return value is cons cell whose CAR is `macro' and CDR is
2404 beginning position."
2406 (when (re-search-forward
2407 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2409 (cons 'macro
(match-beginning 0)))))
2414 (defun org-element-radio-target-parser ()
2415 "Parse radio target at point.
2417 Return a list whose CAR is `radio-target' and CDR a plist with
2418 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2419 and `:post-blank' as keywords.
2421 Assume point is at the radio target."
2423 (looking-at org-radio-target-regexp
)
2424 (let ((begin (point))
2425 (contents-begin (match-beginning 1))
2426 (contents-end (match-end 1))
2427 (value (org-match-string-no-properties 1))
2428 (post-blank (progn (goto-char (match-end 0))
2429 (skip-chars-forward " \t")))
2434 :contents-begin
,contents-begin
2435 :contents-end
,contents-end
2436 :post-blank
,post-blank
2439 (defun org-element-radio-target-interpreter (target contents
)
2440 "Interpret TARGET object as Org syntax.
2441 CONTENTS is the contents of the object."
2442 (concat "<<<" contents
">>>"))
2444 (defun org-element-radio-target-successor (limit)
2445 "Search for the next radio-target object.
2447 LIMIT bounds the search.
2449 Return value is a cons cell whose CAR is `radio-target' and CDR
2450 is beginning position."
2452 (when (re-search-forward org-radio-target-regexp limit t
)
2453 (cons 'radio-target
(match-beginning 0)))))
2456 ;;;; Statistics Cookie
2458 (defun org-element-statistics-cookie-parser ()
2459 "Parse statistics cookie at point.
2461 Return a list whose CAR is `statistics-cookie', and CDR a plist
2462 with `:begin', `:end', `:value' and `:post-blank' keywords.
2464 Assume point is at the beginning of the statistics-cookie."
2466 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2467 (let* ((begin (point))
2468 (value (buffer-substring-no-properties
2469 (match-beginning 0) (match-end 0)))
2470 (post-blank (progn (goto-char (match-end 0))
2471 (skip-chars-forward " \t")))
2477 :post-blank
,post-blank
)))))
2479 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
2480 "Interpret STATISTICS-COOKIE object as Org syntax.
2482 (org-element-property :value statistics-cookie
))
2484 (defun org-element-statistics-cookie-successor (limit)
2485 "Search for the next statistics cookie object.
2487 LIMIT bounds the search.
2489 Return value is a cons cell whose CAR is `statistics-cookie' and
2490 CDR is beginning position."
2492 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
2493 (cons 'statistics-cookie
(match-beginning 0)))))
2498 (defun org-element-strike-through-parser ()
2499 "Parse strike-through object at point.
2501 Return a list whose CAR is `strike-through' and CDR is a plist
2502 with `:begin', `:end', `:contents-begin' and `:contents-end' and
2503 `:post-blank' keywords.
2505 Assume point is at the first plus sign marker."
2507 (unless (bolp) (backward-char 1))
2508 (looking-at org-emph-re
)
2509 (let ((begin (match-beginning 2))
2510 (contents-begin (match-beginning 4))
2511 (contents-end (match-end 4))
2512 (post-blank (progn (goto-char (match-end 2))
2513 (skip-chars-forward " \t")))
2518 :contents-begin
,contents-begin
2519 :contents-end
,contents-end
2520 :post-blank
,post-blank
)))))
2522 (defun org-element-strike-through-interpreter (strike-through contents
)
2523 "Interpret STRIKE-THROUGH object as Org syntax.
2524 CONTENTS is the contents of the object."
2525 (format "+%s+" contents
))
2530 (defun org-element-subscript-parser ()
2531 "Parse subscript at point.
2533 Return a list whose CAR is `subscript' and CDR a plist with
2534 `:begin', `:end', `:contents-begin', `:contents-end',
2535 `:use-brackets-p' and `:post-blank' as keywords.
2537 Assume point is at the underscore."
2539 (unless (bolp) (backward-char))
2540 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2542 (not (looking-at org-match-substring-regexp
))))
2543 (begin (match-beginning 2))
2544 (contents-begin (or (match-beginning 5)
2545 (match-beginning 3)))
2546 (contents-end (or (match-end 5) (match-end 3)))
2547 (post-blank (progn (goto-char (match-end 0))
2548 (skip-chars-forward " \t")))
2553 :use-brackets-p
,bracketsp
2554 :contents-begin
,contents-begin
2555 :contents-end
,contents-end
2556 :post-blank
,post-blank
)))))
2558 (defun org-element-subscript-interpreter (subscript contents
)
2559 "Interpret SUBSCRIPT object as Org syntax.
2560 CONTENTS is the contents of the object."
2562 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
2565 (defun org-element-sub/superscript-successor
(limit)
2566 "Search for the next sub/superscript object.
2568 LIMIT bounds the search.
2570 Return value is a cons cell whose CAR is either `subscript' or
2571 `superscript' and CDR is beginning position."
2573 (when (re-search-forward org-match-substring-regexp limit t
)
2574 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
2575 (match-beginning 2)))))
2580 (defun org-element-superscript-parser ()
2581 "Parse superscript at point.
2583 Return a list whose CAR is `superscript' and CDR a plist with
2584 `:begin', `:end', `:contents-begin', `:contents-end',
2585 `:use-brackets-p' and `:post-blank' as keywords.
2587 Assume point is at the caret."
2589 (unless (bolp) (backward-char))
2590 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
2591 (not (looking-at org-match-substring-regexp
))))
2592 (begin (match-beginning 2))
2593 (contents-begin (or (match-beginning 5)
2594 (match-beginning 3)))
2595 (contents-end (or (match-end 5) (match-end 3)))
2596 (post-blank (progn (goto-char (match-end 0))
2597 (skip-chars-forward " \t")))
2602 :use-brackets-p
,bracketsp
2603 :contents-begin
,contents-begin
2604 :contents-end
,contents-end
2605 :post-blank
,post-blank
)))))
2607 (defun org-element-superscript-interpreter (superscript contents
)
2608 "Interpret SUPERSCRIPT object as Org syntax.
2609 CONTENTS is the contents of the object."
2611 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
2617 (defun org-element-table-cell-parser ()
2618 "Parse table cell at point.
2620 Return a list whose CAR is `table-cell' and CDR is a plist
2621 containing `:begin', `:end', `:contents-begin', `:contents-end'
2622 and `:post-blank' keywords."
2623 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
2624 (let* ((begin (match-beginning 0))
2626 (contents-begin (match-beginning 1))
2627 (contents-end (match-end 1)))
2631 :contents-begin
,contents-begin
2632 :contents-end
,contents-end
2635 (defun org-element-table-cell-interpreter (table-cell contents
)
2636 "Interpret TABLE-CELL element as Org syntax.
2637 CONTENTS is the contents of the cell, or nil."
2638 (concat " " contents
" |"))
2640 (defun org-element-table-cell-successor (limit)
2641 "Search for the next table-cell object.
2643 LIMIT bounds the search.
2645 Return value is a cons cell whose CAR is `table-cell' and CDR is
2646 beginning position."
2647 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
2652 (defun org-element-target-parser ()
2653 "Parse target at point.
2655 Return a list whose CAR is `target' and CDR a plist with
2656 `:begin', `:end', `:value' and `:post-blank' as keywords.
2658 Assume point is at the target."
2660 (looking-at org-target-regexp
)
2661 (let ((begin (point))
2662 (value (org-match-string-no-properties 1))
2663 (post-blank (progn (goto-char (match-end 0))
2664 (skip-chars-forward " \t")))
2670 :post-blank
,post-blank
)))))
2672 (defun org-element-target-interpreter (target contents
)
2673 "Interpret TARGET object as Org syntax.
2675 (format "<<%s>>" (org-element-property :value target
)))
2677 (defun org-element-target-successor (limit)
2678 "Search for the next target object.
2680 LIMIT bounds the search.
2682 Return value is a cons cell whose CAR is `target' and CDR is
2683 beginning position."
2685 (when (re-search-forward org-target-regexp limit t
)
2686 (cons 'target
(match-beginning 0)))))
2691 (defun org-element-timestamp-parser ()
2692 "Parse time stamp at point.
2694 Return a list whose CAR is `timestamp', and CDR a plist with
2695 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
2697 Assume point is at the beginning of the timestamp."
2699 (let* ((begin (point))
2701 ((looking-at org-tsr-regexp
)
2702 (if (match-string 2) 'active-range
'active
))
2703 ((looking-at org-tsr-regexp-both
)
2704 (if (match-string 2) 'inactive-range
'inactive
))
2707 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2709 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2711 (value (org-match-string-no-properties 0))
2712 (post-blank (progn (goto-char (match-end 0))
2713 (skip-chars-forward " \t")))
2720 :post-blank
,post-blank
)))))
2722 (defun org-element-timestamp-interpreter (timestamp contents
)
2723 "Interpret TIMESTAMP object as Org syntax.
2725 (org-element-property :value timestamp
))
2727 (defun org-element-timestamp-successor (limit)
2728 "Search for the next timestamp object.
2730 LIMIT bounds the search.
2732 Return value is a cons cell whose CAR is `timestamp' and CDR is
2733 beginning position."
2735 (when (re-search-forward
2736 (concat org-ts-regexp-both
2738 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2740 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2742 (cons 'timestamp
(match-beginning 0)))))
2747 (defun org-element-underline-parser ()
2748 "Parse underline object at point.
2750 Return a list whose CAR is `underline' and CDR is a plist with
2751 `:begin', `:end', `:contents-begin' and `:contents-end' and
2752 `:post-blank' keywords.
2754 Assume point is at the first underscore marker."
2756 (unless (bolp) (backward-char 1))
2757 (looking-at org-emph-re
)
2758 (let ((begin (match-beginning 2))
2759 (contents-begin (match-beginning 4))
2760 (contents-end (match-end 4))
2761 (post-blank (progn (goto-char (match-end 2))
2762 (skip-chars-forward " \t")))
2767 :contents-begin
,contents-begin
2768 :contents-end
,contents-end
2769 :post-blank
,post-blank
)))))
2771 (defun org-element-underline-interpreter (underline contents
)
2772 "Interpret UNDERLINE object as Org syntax.
2773 CONTENTS is the contents of the object."
2774 (format "_%s_" contents
))
2779 (defun org-element-verbatim-parser ()
2780 "Parse verbatim object at point.
2782 Return a list whose CAR is `verbatim' and CDR is a plist with
2783 `:value', `:begin', `:end' and `:post-blank' keywords.
2785 Assume point is at the first equal sign marker."
2787 (unless (bolp) (backward-char 1))
2788 (looking-at org-emph-re
)
2789 (let ((begin (match-beginning 2))
2790 (value (org-match-string-no-properties 4))
2791 (post-blank (progn (goto-char (match-end 2))
2792 (skip-chars-forward " \t")))
2798 :post-blank
,post-blank
)))))
2800 (defun org-element-verbatim-interpreter (verbatim contents
)
2801 "Interpret VERBATIM object as Org syntax.
2803 (format "=%s=" (org-element-property :value verbatim
)))
2807 ;;; Definitions And Rules
2809 ;; Define elements, greater elements and specify recursive objects,
2810 ;; along with the affiliated keywords recognized. Also set up
2811 ;; restrictions on recursive objects combinations.
2813 ;; These variables really act as a control center for the parsing
2815 (defconst org-element-paragraph-separate
2816 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2817 ;; Headlines and inlinetasks.
2818 org-outline-regexp-bol
"\\|"
2819 ;; Comments, blocks (any type), keywords and babel calls.
2820 "^[ \t]*#\\+" "\\|" "^#\\(?: \\|$\\)" "\\|"
2822 (org-item-beginning-re) "\\|"
2823 ;; Fixed-width, drawers (any type) and tables.
2825 ;; Footnote definitions.
2826 org-footnote-definition-re
"\\|"
2827 ;; Horizontal rules.
2828 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2829 ;; LaTeX environments.
2830 "^[ \t]*\\\\\\(begin\\|end\\)"
2831 ;; Planning and Clock lines.
2833 org-clock-string
"\\|"
2834 org-closed-string
"\\|"
2835 org-deadline-string
"\\|"
2836 org-scheduled-string
"\\)")
2837 "Regexp to separate paragraphs in an Org buffer.")
2839 (defconst org-element-all-elements
2840 '(center-block clock comment comment-block drawer dynamic-block example-block
2841 export-block fixed-width footnote-definition headline
2842 horizontal-rule inlinetask item keyword latex-environment
2843 babel-call paragraph plain-list planning property-drawer
2844 quote-block quote-section section special-block src-block table
2845 table-row verse-block
)
2846 "Complete list of element types.")
2848 (defconst org-element-greater-elements
2849 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2850 item plain-list quote-block section special-block table
)
2851 "List of recursive element types aka Greater Elements.")
2853 (defconst org-element-all-successors
2854 '(export-snippet footnote-reference inline-babel-call inline-src-block
2855 latex-or-entity line-break link macro radio-target
2856 statistics-cookie sub
/superscript table-cell target
2857 text-markup timestamp
)
2858 "Complete list of successors.")
2860 (defconst org-element-object-successor-alist
2861 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
2862 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
2863 (strike-through . text-markup
) (underline . text-markup
)
2864 (verbatim . text-markup
) (entity . latex-or-entity
)
2865 (latex-fragment . latex-or-entity
))
2866 "Alist of translations between object type and successor name.
2868 Sharing the same successor comes handy when, for example, the
2869 regexp matching one object can also match the other object.")
2871 (defconst org-element-all-objects
2872 '(bold code entity export-snippet footnote-reference inline-babel-call
2873 inline-src-block italic line-break latex-fragment link macro
2874 radio-target statistics-cookie strike-through subscript superscript
2875 table-cell target timestamp underline verbatim
)
2876 "Complete list of object types.")
2878 (defconst org-element-recursive-objects
2879 '(bold italic link macro subscript radio-target strike-through superscript
2880 table-cell underline
)
2881 "List of recursive object types.")
2883 (defconst org-element-non-recursive-block-alist
2884 '(("ASCII" . export-block
)
2885 ("COMMENT" . comment-block
)
2886 ("DOCBOOK" . export-block
)
2887 ("EXAMPLE" . example-block
)
2888 ("HTML" . export-block
)
2889 ("LATEX" . export-block
)
2890 ("ODT" . export-block
)
2892 ("VERSE" . verse-block
))
2893 "Alist between non-recursive block name and their element type.")
2895 (defconst org-element-affiliated-keywords
2896 '("ATTR_ASCII" "ATTR_DOCBOOK" "ATTR_HTML" "ATTR_LATEX" "ATTR_ODT" "CAPTION"
2897 "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT" "RESULTS"
2898 "SOURCE" "SRCNAME" "TBLNAME")
2899 "List of affiliated keywords as strings.")
2901 (defconst org-element-keyword-translation-alist
2902 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
2903 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
2904 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
2905 "Alist of usual translations for keywords.
2906 The key is the old name and the value the new one. The property
2907 holding their value will be named after the translated name.")
2909 (defconst org-element-multiple-keywords
2910 '("ATTR_ASCII" "ATTR_DOCBOOK" "ATTR_HTML" "ATTR_LATEX" "ATTR_ODT" "HEADER")
2911 "List of affiliated keywords that can occur more that once in an element.
2913 Their value will be consed into a list of strings, which will be
2914 returned as the value of the property.
2916 This list is checked after translations have been applied. See
2917 `org-element-keyword-translation-alist'.")
2919 (defconst org-element-parsed-keywords
'("AUTHOR" "CAPTION" "TITLE")
2920 "List of keywords whose value can be parsed.
2922 Their value will be stored as a secondary string: a list of
2923 strings and objects.
2925 This list is checked after translations have been applied. See
2926 `org-element-keyword-translation-alist'.")
2928 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
2929 "List of keywords which can have a secondary value.
2931 In Org syntax, they can be written with optional square brackets
2932 before the colons. For example, results keyword can be
2933 associated to a hash value with the following:
2935 #+RESULTS[hash-string]: some-source
2937 This list is checked after translations have been applied. See
2938 `org-element-keyword-translation-alist'.")
2940 (defconst org-element-object-restrictions
2941 `((bold entity export-snippet inline-babel-call inline-src-block link
2942 radio-target sub
/superscript target text-markup timestamp
)
2943 (footnote-reference entity export-snippet footnote-reference
2944 inline-babel-call inline-src-block latex-fragment
2945 line-break link macro radio-target sub
/superscript
2946 target text-markup timestamp
)
2947 (headline entity inline-babel-call inline-src-block latex-fragment link
2948 macro radio-target statistics-cookie sub
/superscript text-markup
2950 (inlinetask entity inline-babel-call inline-src-block latex-fragment link
2951 macro radio-target sub
/superscript text-markup timestamp
)
2952 (italic entity export-snippet inline-babel-call inline-src-block link
2953 radio-target sub
/superscript target text-markup timestamp
)
2954 (item entity inline-babel-call latex-fragment macro radio-target
2955 sub
/superscript target text-markup
)
2956 (keyword entity latex-fragment macro sub
/superscript text-markup
)
2957 (link entity export-snippet inline-babel-call inline-src-block
2958 latex-fragment link sub
/superscript text-markup
)
2960 (paragraph ,@org-element-all-successors
)
2961 (radio-target entity export-snippet latex-fragment sub
/superscript
)
2962 (strike-through entity export-snippet inline-babel-call inline-src-block
2963 link radio-target sub
/superscript target text-markup
2965 (subscript entity export-snippet inline-babel-call inline-src-block
2966 latex-fragment sub
/superscript text-markup
)
2967 (superscript entity export-snippet inline-babel-call inline-src-block
2968 latex-fragment sub
/superscript text-markup
)
2969 (table-cell entity export-snippet latex-fragment link macro radio-target
2970 sub
/superscript target text-markup timestamp
)
2971 (table-row table-cell
)
2972 (underline entity export-snippet inline-babel-call inline-src-block link
2973 radio-target sub
/superscript target text-markup timestamp
)
2974 (verse-block entity footnote-reference inline-babel-call inline-src-block
2975 latex-fragment line-break link macro radio-target
2976 sub
/superscript target text-markup timestamp
))
2977 "Alist of objects restrictions.
2979 CAR is an element or object type containing objects and CDR is
2980 a list of successors that will be called within an element or
2981 object of such type.
2983 For example, in a `radio-target' object, one can only find
2984 entities, export snippets, latex-fragments, subscript and
2987 This alist also applies to secondary string. For example, an
2988 `headline' type element doesn't directly contain objects, but
2989 still has an entry since one of its properties (`:title') does.")
2991 (defconst org-element-secondary-value-alist
2992 '((headline .
:title
)
2993 (inlinetask .
:title
)
2995 (footnote-reference .
:inline-definition
))
2996 "Alist between element types and location of secondary value.")
3002 ;; Provide four accessors: `org-element-type', `org-element-property'
3003 ;; `org-element-contents' and `org-element-restriction'.
3005 (defun org-element-type (element)
3006 "Return type of element ELEMENT.
3008 The function returns the type of the element or object provided.
3009 It can also return the following special value:
3010 `plain-text' for a string
3011 `org-data' for a complete document
3012 nil in any other case."
3014 ((not (consp element
)) (and (stringp element
) 'plain-text
))
3015 ((symbolp (car element
)) (car element
))))
3017 (defun org-element-property (property element
)
3018 "Extract the value from the PROPERTY of an ELEMENT."
3019 (plist-get (nth 1 element
) property
))
3021 (defun org-element-contents (element)
3022 "Extract contents from an ELEMENT."
3023 (and (consp element
) (nthcdr 2 element
)))
3025 (defun org-element-restriction (element)
3026 "Return restriction associated to ELEMENT.
3027 ELEMENT can be an element, an object or a symbol representing an
3028 element or object type."
3029 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
3030 org-element-object-restrictions
)))
3034 ;;; Parsing Element Starting At Point
3036 ;; `org-element-current-element' is the core function of this section.
3037 ;; It returns the Lisp representation of the element starting at
3038 ;; point. It uses `org-element--element-block-re' for quick access to
3041 ;; `org-element-current-element' makes use of special modes. They are
3042 ;; activated for fixed element chaining (i.e. `plain-list' > `item')
3043 ;; or fixed conditional element chaining (i.e. `headline' >
3044 ;; `section'). Special modes are: `section', `quote-section', `item'
3047 (defconst org-element--element-block-re
3048 (format "[ \t]*#\\+BEGIN_\\(%s\\)\\(?: \\|$\\)"
3051 (mapcar 'car org-element-non-recursive-block-alist
) "\\|"))
3052 "Regexp matching the beginning of a non-recursive block type.
3053 Used internally by `org-element-current-element'.")
3055 (defun org-element-current-element (&optional granularity special structure
)
3056 "Parse the element starting at point.
3058 Return value is a list like (TYPE PROPS) where TYPE is the type
3059 of the element and PROPS a plist of properties associated to the
3062 Possible types are defined in `org-element-all-elements'.
3064 Optional argument GRANULARITY determines the depth of the
3065 recursion. Allowed values are `headline', `greater-element',
3066 `element', `object' or nil. When it is broader than `object' (or
3067 nil), secondary values will not be parsed, since they only
3070 Optional argument SPECIAL, when non-nil, can be either `section',
3071 `quote-section', `table-row' and `item'.
3073 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3076 Unlike to `org-element-at-point', this function assumes point is
3077 always at the beginning of the element it has to parse. As such,
3078 it is quicker than its counterpart, albeit more restrictive."
3080 ;; If point is at an affiliated keyword, try moving to the
3081 ;; beginning of the associated element. If none is found, the
3082 ;; keyword is orphaned and will be treated as plain text.
3083 (when (looking-at org-element--affiliated-re
)
3084 (let ((opoint (point)))
3085 (while (looking-at org-element--affiliated-re
) (forward-line))
3086 (when (looking-at "[ \t]*$") (goto-char opoint
))))
3087 (let ((case-fold-search t
)
3088 ;; Determine if parsing depth allows for secondary strings
3089 ;; parsing. It only applies to elements referenced in
3090 ;; `org-element-secondary-value-alist'.
3091 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3095 (org-element-item-parser (or structure
(org-list-struct))
3098 ((eq special
'quote-section
) (org-element-quote-section-parser))
3100 ((eq special
'table-row
) (org-element-table-row-parser))
3102 ((org-with-limited-levels (org-at-heading-p))
3103 (org-element-headline-parser raw-secondary-p
))
3104 ;; Section (must be checked after headline).
3105 ((eq special
'section
) (org-element-section-parser))
3106 ;; Planning and Clock.
3107 ((and (looking-at org-planning-or-clock-line-re
))
3108 (if (equal (match-string 1) org-clock-string
)
3109 (org-element-clock-parser)
3110 (org-element-planning-parser)))
3111 ;; Non-recursive block.
3112 ((when (looking-at org-element--element-block-re
)
3113 (let ((type (upcase (match-string 1))))
3116 (format "^[ \t]*#\\+END_%s\\(?: \\|$\\)" type
) nil t
))
3120 "org-element-%s-parser"
3121 (cdr (assoc type org-element-non-recursive-block-alist
)))))
3122 (org-element-paragraph-parser)))))
3124 ((org-at-heading-p) (org-element-inlinetask-parser raw-secondary-p
))
3125 ;; LaTeX Environment or Paragraph if incomplete.
3126 ((looking-at "[ \t]*\\\\begin{")
3128 (re-search-forward "[ \t]*\\\\end{[^}]*}[ \t]*" nil t
))
3129 (org-element-latex-environment-parser)
3130 (org-element-paragraph-parser)))
3132 ((looking-at org-property-start-re
)
3133 (if (save-excursion (re-search-forward org-property-end-re nil t
))
3134 (org-element-property-drawer-parser)
3135 (org-element-paragraph-parser)))
3136 ;; Recursive Block, or Paragraph if incomplete.
3137 ((looking-at "[ \t]*#\\+BEGIN_\\([-A-Za-z0-9]+\\)\\(?: \\|$\\)")
3138 (let ((type (upcase (match-string 1))))
3140 ((not (save-excursion
3142 (format "^[ \t]*#\\+END_%s\\(?: \\|$\\)" type
) nil t
)))
3143 (org-element-paragraph-parser))
3144 ((string= type
"CENTER") (org-element-center-block-parser))
3145 ((string= type
"QUOTE") (org-element-quote-block-parser))
3146 (t (org-element-special-block-parser)))))
3148 ((looking-at org-drawer-regexp
)
3149 (if (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
))
3150 (org-element-drawer-parser)
3151 (org-element-paragraph-parser)))
3152 ((looking-at "[ \t]*:\\( \\|$\\)") (org-element-fixed-width-parser))
3154 ((looking-at org-babel-block-lob-one-liner-regexp
)
3155 (org-element-babel-call-parser))
3156 ;; Dynamic Block or Paragraph if incomplete. This must be
3157 ;; checked before regular keywords since their regexp matches
3158 ;; dynamic blocks too.
3159 ((looking-at "[ \t]*#\\+BEGIN:\\(?: \\|$\\)")
3161 (re-search-forward "^[ \t]*#\\+END:\\(?: \\|$\\)" nil t
))
3162 (org-element-dynamic-block-parser)
3163 (org-element-paragraph-parser)))
3164 ;; Keyword, or Paragraph if at an orphaned affiliated keyword.
3165 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
3166 (let ((key (upcase (match-string 1))))
3167 (if (or (string= key
"TBLFM")
3168 (member key org-element-affiliated-keywords
))
3169 (org-element-paragraph-parser)
3170 (org-element-keyword-parser))))
3171 ;; Footnote definition.
3172 ((looking-at org-footnote-definition-re
)
3173 (org-element-footnote-definition-parser))
3175 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)")
3176 (org-element-comment-parser))
3178 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3179 (org-element-horizontal-rule-parser))
3181 ((org-at-table-p t
) (org-element-table-parser))
3183 ((looking-at (org-item-re))
3184 (org-element-plain-list-parser (or structure
(org-list-struct))))
3185 ;; Default element: Paragraph.
3186 (t (org-element-paragraph-parser))))))
3189 ;; Most elements can have affiliated keywords. When looking for an
3190 ;; element beginning, we want to move before them, as they belong to
3191 ;; that element, and, in the meantime, collect information they give
3192 ;; into appropriate properties. Hence the following function.
3194 ;; Usage of optional arguments may not be obvious at first glance:
3196 ;; - TRANS-LIST is used to polish keywords names that have evolved
3197 ;; during Org history. In example, even though =result= and
3198 ;; =results= coexist, we want to have them under the same =result=
3199 ;; property. It's also true for "srcname" and "name", where the
3200 ;; latter seems to be preferred nowadays (thus the "name" property).
3202 ;; - CONSED allows to regroup multi-lines keywords under the same
3203 ;; property, while preserving their own identity. This is mostly
3204 ;; used for "attr_latex" and al.
3206 ;; - PARSED prepares a keyword value for export. This is useful for
3207 ;; "caption". Objects restrictions for such keywords are defined in
3208 ;; `org-element-object-restrictions'.
3210 ;; - DUALS is used to take care of keywords accepting a main and an
3211 ;; optional secondary values. For example "results" has its
3212 ;; source's name as the main value, and may have an hash string in
3213 ;; optional square brackets as the secondary one.
3215 ;; A keyword may belong to more than one category.
3217 (defconst org-element--affiliated-re
3218 (format "[ \t]*#\\+\\(%s\\):"
3221 (if (member keyword org-element-dual-keywords
)
3222 (format "\\(%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
3223 (regexp-quote keyword
))
3224 (regexp-quote keyword
)))
3225 org-element-affiliated-keywords
"\\|"))
3226 "Regexp matching any affiliated keyword.
3228 Keyword name is put in match group 1. Moreover, if keyword
3229 belongs to `org-element-dual-keywords', put the dual value in
3232 Don't modify it, set `org-element-affiliated-keywords' instead.")
3234 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
3235 consed parsed duals
)
3236 "Collect affiliated keywords before point.
3238 Optional argument KEY-RE is a regexp matching keywords, which
3239 puts matched keyword in group 1. It defaults to
3240 `org-element--affiliated-re'.
3242 TRANS-LIST is an alist where key is the keyword and value the
3243 property name it should be translated to, without the colons. It
3244 defaults to `org-element-keyword-translation-alist'.
3246 CONSED is a list of strings. Any keyword belonging to that list
3247 will have its value consed. The check is done after keyword
3248 translation. It defaults to `org-element-multiple-keywords'.
3250 PARSED is a list of strings. Any keyword member of this list
3251 will have its value parsed. The check is done after keyword
3252 translation. If a keyword is a member of both CONSED and PARSED,
3253 it's value will be a list of parsed strings. It defaults to
3254 `org-element-parsed-keywords'.
3256 DUALS is a list of strings. Any keyword member of this list can
3257 have two parts: one mandatory and one optional. Its value is
3258 a cons cell whose car is the former, and the cdr the latter. If
3259 a keyword is a member of both PARSED and DUALS, both values will
3260 be parsed. It defaults to `org-element-dual-keywords'.
3262 Return a list whose car is the position at the first of them and
3263 cdr a plist of keywords and values."
3265 (let ((case-fold-search t
)
3266 (key-re (or key-re org-element--affiliated-re
))
3267 (trans-list (or trans-list org-element-keyword-translation-alist
))
3268 (consed (or consed org-element-multiple-keywords
))
3269 (parsed (or parsed org-element-parsed-keywords
))
3270 (duals (or duals org-element-dual-keywords
))
3271 ;; RESTRICT is the list of objects allowed in parsed
3273 (restrict (org-element-restriction 'keyword
))
3276 (while (and (not (bobp))
3277 (progn (forward-line -
1) (looking-at key-re
)))
3278 (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
3279 ;; Apply translation to RAW-KWD. From there, KWD is
3280 ;; the official keyword.
3281 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
3282 ;; Find main value for any keyword.
3286 (buffer-substring-no-properties
3287 (match-end 0) (point-at-eol)))))
3288 ;; If KWD is a dual keyword, find its secondary
3289 ;; value. Maybe parse it.
3291 (and (member kwd duals
)
3292 (let ((sec (org-match-string-no-properties 3)))
3293 (if (or (not sec
) (not (member kwd parsed
))) sec
3294 (org-element-parse-secondary-string sec restrict
)))))
3295 ;; Attribute a property name to KWD.
3296 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3297 ;; Now set final shape for VALUE.
3298 (when (member kwd parsed
)
3299 (setq value
(org-element-parse-secondary-string value restrict
)))
3300 (when (member kwd duals
)
3301 ;; VALUE is mandatory. Set it to nil if there is none.
3302 (setq value
(and value
(cons value dual-value
))))
3303 (when (member kwd consed
)
3304 (setq value
(cons value
(plist-get output kwd-sym
))))
3305 ;; Eventually store the new value in OUTPUT.
3306 (setq output
(plist-put output kwd-sym value
))))
3307 (unless (looking-at key-re
) (forward-line 1)))
3308 (list (point) output
))))
3314 ;; The two major functions here are `org-element-parse-buffer', which
3315 ;; parses Org syntax inside the current buffer, taking into account
3316 ;; region, narrowing, or even visibility if specified, and
3317 ;; `org-element-parse-secondary-string', which parses objects within
3320 ;; The (almost) almighty `org-element-map' allows to apply a function
3321 ;; on elements or objects matching some type, and accumulate the
3322 ;; resulting values. In an export situation, it also skips unneeded
3323 ;; parts of the parse tree.
3325 (defun org-element-parse-buffer (&optional granularity visible-only
)
3326 "Recursively parse the buffer and return structure.
3327 If narrowing is in effect, only parse the visible part of the
3330 Optional argument GRANULARITY determines the depth of the
3331 recursion. It can be set to the following symbols:
3333 `headline' Only parse headlines.
3334 `greater-element' Don't recurse into greater elements excepted
3335 headlines and sections. Thus, elements
3336 parsed are the top-level ones.
3337 `element' Parse everything but objects and plain text.
3338 `object' Parse the complete buffer (default).
3340 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3343 Assume buffer is in Org mode."
3345 (goto-char (point-min))
3346 (org-skip-whitespace)
3347 (nconc (list 'org-data nil
)
3348 (org-element-parse-elements
3349 (point-at-bol) (point-max)
3350 ;; Start in `section' mode so text before the first
3351 ;; headline belongs to a section.
3352 'section nil granularity visible-only nil
))))
3354 (defun org-element-parse-secondary-string (string restriction
)
3355 "Recursively parse objects in STRING and return structure.
3357 RESTRICTION, when non-nil, is a symbol limiting the object types
3358 that will be looked after."
3361 (org-element-parse-objects (point-min) (point-max) nil restriction
)))
3363 (defun org-element-map (data types fun
&optional info first-match no-recursion
)
3364 "Map a function on selected elements or objects.
3366 DATA is the parsed tree, as returned by, i.e,
3367 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3368 of elements or objects types. FUN is the function called on the
3369 matching element or object. It must accept one arguments: the
3370 element or object itself.
3372 When optional argument INFO is non-nil, it should be a plist
3373 holding export options. In that case, parts of the parse tree
3374 not exportable according to that property list will be skipped.
3376 When optional argument FIRST-MATCH is non-nil, stop at the first
3377 match for which FUN doesn't return nil, and return that value.
3379 Optional argument NO-RECURSION is a symbol or a list of symbols
3380 representing elements or objects types. `org-element-map' won't
3381 enter any recursive element or object whose type belongs to that
3382 list. Though, FUN can still be applied on them.
3384 Nil values returned from FUN do not appear in the results."
3385 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3386 (unless (listp types
) (setq types
(list types
)))
3387 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3388 ;; Recursion depth is determined by --CATEGORY.
3391 ((loop for type in types
3392 always
(memq type org-element-greater-elements
))
3394 ((loop for type in types
3395 always
(memq type org-element-all-elements
))
3398 ;; --RESTRICTS is a list of element types whose secondary
3399 ;; string could possibly contain an object with a type among
3402 (and (eq --category
'objects
)
3403 (loop for el in org-element-secondary-value-alist
3405 (loop for o in types
3406 thereis
(memq o
(org-element-restriction (car el
))))
3412 ;; Recursively walk DATA. INFO, if non-nil, is
3413 ;; a plist holding contextual information.
3416 (unless (and info
(member --blob
(plist-get info
:ignore-list
)))
3417 (let ((--type (org-element-type --blob
)))
3418 ;; Check if TYPE is matching among TYPES. If so,
3419 ;; apply FUN to --BLOB and accumulate return value
3420 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3421 (when (memq --type types
)
3422 (let ((result (funcall fun --blob
)))
3423 (cond ((not result
))
3424 (first-match (throw 'first-match result
))
3425 (t (push result --acc
)))))
3426 ;; If --BLOB has a secondary string that can
3427 ;; contain objects with their type among TYPES,
3428 ;; look into that string.
3429 (when (memq --type --restricts
)
3434 ,@(org-element-property
3435 (cdr (assq --type org-element-secondary-value-alist
))
3437 ;; Now determine if a recursion into --BLOB is
3438 ;; possible. If so, do it.
3439 (unless (memq --type no-recursion
)
3440 (when (or (and (memq --type org-element-greater-elements
)
3441 (not (eq --category
'greater-elements
)))
3442 (and (memq --type org-element-all-elements
)
3443 (not (eq --category
'elements
)))
3444 (org-element-contents --blob
))
3445 (funcall --walk-tree --blob
))))))
3446 (org-element-contents --data
))))))
3448 (funcall --walk-tree data
)
3449 ;; Return value in a proper order.
3452 ;; The following functions are internal parts of the parser.
3454 ;; The first one, `org-element-parse-elements' acts at the element's
3457 ;; The second one, `org-element-parse-objects' applies on all objects
3458 ;; of a paragraph or a secondary string. It uses
3459 ;; `org-element-get-candidates' to optimize the search of the next
3460 ;; object in the buffer.
3462 ;; More precisely, that function looks for every allowed object type
3463 ;; first. Then, it discards failed searches, keeps further matches,
3464 ;; and searches again types matched behind point, for subsequent
3465 ;; calls. Thus, searching for a given type fails only once, and every
3466 ;; object is searched only once at top level (but sometimes more for
3469 (defun org-element-parse-elements
3470 (beg end special structure granularity visible-only acc
)
3471 "Parse elements between BEG and END positions.
3473 SPECIAL prioritize some elements over the others. It can be set
3474 to `quote-section', `section' `item' or `table-row'.
3476 When value is `item', STRUCTURE will be used as the current list
3479 GRANULARITY determines the depth of the recursion. See
3480 `org-element-parse-buffer' for more information.
3482 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3485 Elements are accumulated into ACC."
3488 (narrow-to-region beg end
)
3490 ;; When parsing only headlines, skip any text before first one.
3491 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3492 (org-with-limited-levels (outline-next-heading)))
3496 ;; Find current element's type and parse it accordingly to
3498 (let* ((element (org-element-current-element
3499 granularity special structure
))
3500 (type (org-element-type element
))
3501 (cbeg (org-element-property :contents-begin element
)))
3502 (goto-char (org-element-property :end element
))
3504 ;; Case 1. Simply accumulate element if VISIBLE-ONLY is
3505 ;; true and element is hidden or if it has no contents
3507 ((or (and visible-only
(org-element-property :hiddenp element
))
3508 (not cbeg
)) element
)
3509 ;; Case 2. Greater element: parse it between
3510 ;; `contents-begin' and `contents-end'. Make sure
3511 ;; GRANULARITY allows the recursion, or ELEMENT is an
3512 ;; headline, in which case going inside is mandatory, in
3513 ;; order to get sub-level headings.
3514 ((and (memq type org-element-greater-elements
)
3515 (or (memq granularity
'(element object nil
))
3516 (and (eq granularity
'greater-element
)
3518 (eq type
'headline
)))
3519 (org-element-parse-elements
3520 cbeg
(org-element-property :contents-end element
)
3521 ;; Possibly switch to a special mode.
3524 (if (org-element-property :quotedp element
) 'quote-section
3528 (org-element-property :structure element
)
3529 granularity visible-only
(nreverse element
)))
3530 ;; Case 3. ELEMENT has contents. Parse objects inside,
3531 ;; if GRANULARITY allows it.
3532 ((and cbeg
(memq granularity
'(object nil
)))
3533 (org-element-parse-objects
3534 cbeg
(org-element-property :contents-end element
)
3535 (nreverse element
) (org-element-restriction type
)))
3536 ;; Case 4. Else, just accumulate ELEMENT.
3542 (defun org-element-parse-objects (beg end acc restriction
)
3543 "Parse objects between BEG and END and return recursive structure.
3545 Objects are accumulated in ACC.
3547 RESTRICTION is a list of object types which are allowed in the
3549 (let ((get-next-object
3552 ;; Return the parsing function associated to the nearest
3553 ;; object among list of candidates CAND.
3554 (let ((pos (apply 'min
(mapcar 'cdr cand
))))
3559 (format "org-element-%s-parser" (car (rassq pos cand
))))))))))
3560 next-object candidates
)
3563 (while (setq candidates
(org-element-get-next-object-candidates
3564 end restriction candidates
))
3565 (setq next-object
(funcall get-next-object candidates
))
3566 ;; 1. Text before any object. Untabify it.
3567 (let ((obj-beg (org-element-property :begin next-object
)))
3568 (unless (= (point) obj-beg
)
3569 (push (replace-regexp-in-string
3570 "\t" (make-string tab-width ?
)
3571 (buffer-substring-no-properties (point) obj-beg
))
3574 (let ((obj-end (org-element-property :end next-object
))
3575 (cont-beg (org-element-property :contents-begin next-object
)))
3576 (push (if (and (memq (car next-object
) org-element-recursive-objects
)
3578 ;; ... recursive. The CONT-BEG check is for
3579 ;; links, as some of them might not be recursive
3580 ;; (i.e. plain links).
3584 (org-element-property :contents-end next-object
))
3585 (org-element-parse-objects
3586 (point-min) (point-max)
3587 (nreverse next-object
)
3588 ;; Restrict allowed objects.
3589 (org-element-restriction next-object
)))
3590 ;; ... not recursive. Accumulate the object.
3593 (goto-char obj-end
)))
3594 ;; 3. Text after last object. Untabify it.
3595 (unless (= (point) end
)
3596 (push (replace-regexp-in-string
3597 "\t" (make-string tab-width ?
)
3598 (buffer-substring-no-properties (point) end
))
3603 (defun org-element-get-next-object-candidates (limit restriction objects
)
3604 "Return an alist of candidates for the next object.
3606 LIMIT bounds the search, and RESTRICTION narrows candidates to
3609 Return value is an alist whose CAR is position and CDR the object
3612 OBJECTS is the previous candidates alist."
3613 (let (next-candidates types-to-search
)
3614 ;; If no previous result, search every object type in RESTRICTION.
3615 ;; Otherwise, keep potential candidates (old objects located after
3616 ;; point) and ask to search again those which had matched before.
3617 (if (not objects
) (setq types-to-search restriction
)
3619 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3620 (push obj next-candidates
)))
3622 ;; Call the appropriate successor function for each type to search
3623 ;; and accumulate matches.
3626 (let* ((successor-fun
3628 (format "org-element-%s-successor"
3629 (or (cdr (assq type org-element-object-successor-alist
))
3631 (obj (funcall successor-fun limit
)))
3632 (and obj
(push obj next-candidates
))))
3639 ;;; Towards A Bijective Process
3641 ;; The parse tree obtained with `org-element-parse-buffer' is really
3642 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3643 ;; interpreted and expanded into a string with canonical Org
3644 ;; syntax. Hence `org-element-interpret-data'.
3646 ;; Data parsed from secondary strings, whose shape is slightly
3647 ;; different than the standard parse tree, is expanded with the
3648 ;; equivalent function `org-element-interpret-secondary'.
3650 ;; Both functions rely internally on
3651 ;; `org-element-interpret--affiliated-keywords'.
3653 (defun org-element-interpret-data (data &optional genealogy previous
)
3654 "Interpret a parse tree representing Org data.
3656 DATA is the parse tree to interpret.
3658 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3660 GENEALOGY is the list of its parents types.
3661 PREVIOUS is the type of the element or object at the same level
3664 Return Org syntax as a string."
3667 ;; BLOB can be an element, an object, a string, or nil.
3670 ((equal blob
"") nil
)
3671 ((stringp blob
) blob
)
3673 (let* ((type (org-element-type blob
))
3675 (if (eq type
'org-data
) 'identity
3676 (intern (format "org-element-%s-interpreter" type
))))
3679 ;; Elements or objects without contents.
3680 ((not (org-element-contents blob
)) nil
)
3681 ;; Full Org document.
3682 ((eq type
'org-data
)
3683 (org-element-interpret-data blob genealogy previous
))
3684 ;; Greater elements.
3685 ((memq type org-element-greater-elements
)
3686 (org-element-interpret-data blob
(cons type genealogy
) nil
))
3688 (org-element-interpret-data
3689 (org-element-normalize-contents
3691 ;; When normalizing first paragraph of an item or
3692 ;; a footnote-definition, ignore first line's
3694 (and (eq type
'paragraph
)
3696 (memq (car genealogy
) '(footnote-definiton item
))))
3697 (cons type genealogy
) nil
))))
3698 (results (funcall interpreter blob contents
)))
3700 (setq previous type
)
3701 ;; Build white spaces. If no `:post-blank' property is
3702 ;; specified, assume its value is 0.
3703 (let ((post-blank (or (org-element-property :post-blank blob
) 0)))
3705 ((eq type
'org-data
) results
)
3706 ((memq type org-element-all-elements
)
3708 (org-element-interpret--affiliated-keywords blob
)
3709 (org-element-normalize-string results
)
3710 (make-string post-blank
10)))
3711 (t (concat results
(make-string post-blank
32)))))))))
3712 (org-element-contents data
) ""))
3714 (defun org-element-interpret-secondary (secondary)
3715 "Interpret SECONDARY string as Org syntax.
3717 SECONDARY-STRING is a nested list as returned by
3718 `org-element-parse-secondary-string'.
3720 Return interpreted string."
3721 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3722 (let ((s (if (listp secondary
) secondary
(list secondary
))))
3723 (org-element-interpret-data `(org-data nil
,@s
) nil nil
)))
3725 ;; Both functions internally use `org-element--affiliated-keywords'.
3727 (defun org-element-interpret--affiliated-keywords (element)
3728 "Return ELEMENT's affiliated keywords as Org syntax.
3729 If there is no affiliated keyword, return the empty string."
3730 (let ((keyword-to-org
3734 (when (member key org-element-dual-keywords
)
3735 (setq dual
(cdr value
) value
(car value
)))
3739 (org-element-interpret-secondary dual
)))
3741 (if (member key org-element-parsed-keywords
)
3742 (org-element-interpret-secondary value
)
3747 (let ((value (org-element-property (intern (concat ":" (downcase key
)))
3750 (if (member key org-element-multiple-keywords
)
3751 (mapconcat (lambda (line)
3752 (funcall keyword-to-org key line
))
3754 (funcall keyword-to-org key value
)))))
3755 ;; Remove translated keywords.
3759 (and (not (assoc key org-element-keyword-translation-alist
)) key
))
3760 org-element-affiliated-keywords
))
3763 ;; Because interpretation of the parse tree must return the same
3764 ;; number of blank lines between elements and the same number of white
3765 ;; space after objects, some special care must be given to white
3768 ;; The first function, `org-element-normalize-string', ensures any
3769 ;; string different from the empty string will end with a single
3770 ;; newline character.
3772 ;; The second function, `org-element-normalize-contents', removes
3773 ;; global indentation from the contents of the current element.
3775 (defun org-element-normalize-string (s)
3776 "Ensure string S ends with a single newline character.
3778 If S isn't a string return it unchanged. If S is the empty
3779 string, return it. Otherwise, return a new string with a single
3780 newline character at its end."
3782 ((not (stringp s
)) s
)
3784 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3785 (replace-match "\n" nil nil s
)))))
3787 (defun org-element-normalize-contents (element &optional ignore-first
)
3788 "Normalize plain text in ELEMENT's contents.
3790 ELEMENT must only contain plain text and objects.
3792 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3793 indentation to compute maximal common indentation.
3795 Return the normalized element that is element with global
3796 indentation removed from its contents. The function assumes that
3797 indentation is not done with TAB characters."
3801 ;; Return list of indentations within BLOB. This is done by
3802 ;; walking recursively BLOB and updating IND-LIST along the
3803 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3804 ;; been seen yet. It is required as this string is the only
3805 ;; one whose indentation doesn't happen after a newline
3807 (lambda (blob first-flag
)
3810 (when (and first-flag
(stringp object
))
3811 (setq first-flag nil
)
3812 (string-match "\\`\\( *\\)" object
)
3813 (let ((len (length (match-string 1 object
))))
3814 ;; An indentation of zero means no string will be
3815 ;; modified. Quit the process.
3816 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
3817 (push len ind-list
))))
3821 ;; Avoid matching blank or empty lines.
3822 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
3823 (not (equal (match-string 2 object
) " ")))
3824 (setq start
(match-end 0))
3825 (push (length (match-string 1 object
)) ind-list
))))
3826 ((memq (org-element-type object
) org-element-recursive-objects
)
3827 (funcall collect-inds object first-flag
))))
3828 (org-element-contents blob
))))))
3829 ;; Collect indentation list in ELEMENT. Possibly remove first
3830 ;; value if IGNORE-FIRST is non-nil.
3831 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
3832 (if (not ind-list
) element
3833 ;; Build ELEMENT back, replacing each string with the same
3834 ;; string minus common indentation.
3837 (lambda (blob mci first-flag
)
3838 ;; Return BLOB with all its strings indentation
3839 ;; shortened from MCI white spaces. FIRST-FLAG is
3840 ;; non-nil when the first string hasn't been seen
3843 (list (org-element-type blob
) (nth 1 blob
))
3846 (when (and first-flag
(stringp object
))
3847 (setq first-flag nil
)
3849 (replace-regexp-in-string
3850 (format "\\` \\{%d\\}" mci
) "" object
)))
3853 (replace-regexp-in-string
3854 (format "\n \\{%d\\}" mci
) "\n" object
))
3855 ((memq (org-element-type object
)
3856 org-element-recursive-objects
)
3857 (funcall build object mci first-flag
))
3859 (org-element-contents blob
)))))))
3860 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
3866 ;; The first move is to implement a way to obtain the smallest element
3867 ;; containing point. This is the job of `org-element-at-point'. It
3868 ;; basically jumps back to the beginning of section containing point
3869 ;; and moves, element after element, with
3870 ;; `org-element-current-element' until the container is found.
3872 ;; Note: When using `org-element-at-point', secondary values are never
3873 ;; parsed since the function focuses on elements, not on objects.
3875 (defun org-element-at-point (&optional keep-trail
)
3876 "Determine closest element around point.
3878 Return value is a list like (TYPE PROPS) where TYPE is the type
3879 of the element and PROPS a plist of properties associated to the
3880 element. Possible types are defined in
3881 `org-element-all-elements'.
3883 As a special case, if point is at the very beginning of a list or
3884 sub-list, returned element will be that list instead of the first
3885 item. In the same way, if point is at the beginning of the first
3886 row of a table, returned element will be the table instead of the
3889 If optional argument KEEP-TRAIL is non-nil, the function returns
3890 a list of of elements leading to element at point. The list's
3891 CAR is always the element at point. Its last item will be the
3892 element's parent, unless element was either the first in its
3893 section (in which case the last item in the list is the first
3894 element of section) or an headline (in which case the list
3895 contains that headline as its single element). Elements
3896 in-between, if any, are siblings of the element at point."
3897 (org-with-wide-buffer
3898 ;; If at an headline, parse it. It is the sole element that
3899 ;; doesn't require to know about context. Be sure to disallow
3900 ;; secondary string parsing, though.
3901 (if (org-with-limited-levels (org-at-heading-p))
3902 (if (not keep-trail
) (org-element-headline-parser t
)
3903 (list (org-element-headline-parser t
)))
3904 ;; Otherwise move at the beginning of the section containing
3906 (let ((origin (point)) element type special-flag trail struct prevs
)
3907 (org-with-limited-levels
3908 (if (org-before-first-heading-p) (goto-char (point-min))
3909 (org-back-to-heading)
3911 (org-skip-whitespace)
3913 ;; Starting parsing successively each element with
3914 ;; `org-element-current-element'. Skip those ending before
3915 ;; original position.
3918 (setq element
(org-element-current-element
3919 'element special-flag struct
)
3921 (when keep-trail
(push element trail
))
3923 ;; 1. Skip any element ending before point or at point.
3924 ((let ((end (org-element-property :end element
)))
3925 (when (<= end origin
)
3926 (if (> (point-max) end
) (goto-char end
)
3927 (throw 'exit
(or trail element
))))))
3928 ;; 2. An element containing point is always the element at
3930 ((not (memq type org-element-greater-elements
))
3931 (throw 'exit
(if keep-trail trail element
)))
3932 ;; 3. At a plain list.
3933 ((eq type
'plain-list
)
3934 (setq struct
(org-element-property :structure element
)
3935 prevs
(or prevs
(org-list-prevs-alist struct
)))
3936 (let ((beg (org-element-property :contents-begin element
)))
3937 (if (<= origin beg
) (throw 'exit
(or trail element
))
3938 ;; Find the item at this level containing ORIGIN.
3939 (let ((items (org-list-get-all-items beg struct prevs
))
3945 ;; Item ends before point: skip it.
3946 ((<= (org-list-get-item-end pos struct
) origin
))
3947 ;; Item contains point: store is in PARENT.
3948 ((<= pos origin
) (setq parent pos
))
3949 ;; We went too far: return PARENT.
3950 (t (throw 'local nil
)))) items
))
3951 ;; No parent: no item contained point, though the
3952 ;; plain list does. Point is in the blank lines
3953 ;; after the list: return plain list.
3954 (if (not parent
) (throw 'exit
(or trail element
))
3955 (setq special-flag
'item
)
3956 (goto-char parent
))))))
3959 (if (eq (org-element-property :type element
) 'table.el
)
3960 (throw 'exit
(or trail element
))
3961 (let ((beg (org-element-property :contents-begin element
))
3962 (end (org-element-property :contents-end element
)))
3963 (if (or (<= origin beg
) (>= origin end
))
3964 (throw 'exit
(or trail element
))
3965 (when keep-trail
(setq trail
(list element
)))
3966 (setq special-flag
'table-row
)
3967 (narrow-to-region beg end
)))))
3968 ;; 4. At any other greater element type, if point is
3969 ;; within contents, move into it. Otherwise, return
3972 (when (eq type
'item
) (setq special-flag nil
))
3973 (let ((beg (org-element-property :contents-begin element
))
3974 (end (org-element-property :contents-end element
)))
3975 (if (or (not beg
) (not end
) (> beg origin
) (< end origin
))
3976 (throw 'exit
(or trail element
))
3977 ;; Reset trail, since we found a parent.
3978 (when keep-trail
(setq trail
(list element
)))
3979 (narrow-to-region beg end
)
3980 (goto-char beg
)))))))))))
3983 ;; Once the local structure around point is well understood, it's easy
3984 ;; to implement some replacements for `forward-paragraph'
3985 ;; `backward-paragraph', namely `org-element-forward' and
3986 ;; `org-element-backward'.
3988 ;; Also, `org-transpose-elements' mimics the behaviour of
3989 ;; `transpose-words', at the element's level, whereas
3990 ;; `org-element-drag-forward', `org-element-drag-backward', and
3991 ;; `org-element-up' generalize, respectively, functions
3992 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3994 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3995 ;; smartly remove global indentation from buffer, making it possible
3996 ;; to use Org indent mode on a file created with hard indentation.
3998 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3999 ;; internally by some of the previously cited tools.
4001 (defsubst org-element-nested-p
(elem-A elem-B
)
4002 "Non-nil when elements ELEM-A and ELEM-B are nested."
4003 (let ((beg-A (org-element-property :begin elem-A
))
4004 (beg-B (org-element-property :begin elem-B
))
4005 (end-A (org-element-property :end elem-A
))
4006 (end-B (org-element-property :end elem-B
)))
4007 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
4008 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
4010 (defun org-element-swap-A-B (elem-A elem-B
)
4011 "Swap elements ELEM-A and ELEM-B.
4013 Leave point at the end of ELEM-A."
4014 (goto-char (org-element-property :begin elem-A
))
4015 (let* ((beg-A (org-element-property :begin elem-A
))
4016 (end-A (save-excursion
4017 (goto-char (org-element-property :end elem-A
))
4018 (skip-chars-backward " \r\t\n")
4020 (beg-B (org-element-property :begin elem-B
))
4021 (end-B (save-excursion
4022 (goto-char (org-element-property :end elem-B
))
4023 (skip-chars-backward " \r\t\n")
4025 (body-A (buffer-substring beg-A end-A
))
4026 (body-B (delete-and-extract-region beg-B end-B
)))
4030 (delete-region beg-A end-A
)
4032 (goto-char (org-element-property :end elem-B
))))
4034 (defun org-element-backward ()
4035 "Move backward by one element.
4036 Move to the previous element at the same level, when possible."
4038 (if (save-excursion (skip-chars-backward " \r\t\n") (bobp))
4039 (error "Cannot move further up")
4040 (let* ((trail (org-element-at-point 'keep-trail
))
4041 (element (car trail
))
4042 (beg (org-element-property :begin element
)))
4043 ;; Move to beginning of current element if point isn't there.
4044 (if (/= (point) beg
) (goto-char beg
)
4045 (let ((type (org-element-type element
)))
4047 ;; At an headline: move to previous headline at the same
4048 ;; level, a parent, or BOB.
4049 ((eq type
'headline
)
4050 (let ((dest (save-excursion (org-backward-same-level 1) (point))))
4051 (if (= (point-min) dest
) (error "Cannot move further up")
4053 ;; At an item: try to move to the previous item, if any.
4054 ((and (eq type
'item
)
4055 (let* ((struct (org-element-property :structure element
))
4056 (prev (org-list-get-prev-item
4057 beg struct
(org-list-prevs-alist struct
))))
4058 (when prev
(goto-char prev
)))))
4059 ;; In any other case, find the previous element in the
4060 ;; trail and move to its beginning. If no previous element
4061 ;; can be found, move to headline.
4062 (t (let ((prev (nth 1 trail
)))
4063 (if prev
(goto-char (org-element-property :begin prev
))
4064 (org-back-to-heading))))))))))
4066 (defun org-element-drag-backward ()
4067 "Drag backward element at point."
4069 (let* ((pos (point))
4070 (elem (org-element-at-point)))
4071 (when (= (progn (goto-char (point-min))
4072 (org-skip-whitespace)
4074 (org-element-property :end elem
))
4075 (error "Cannot drag element backward"))
4076 (goto-char (org-element-property :begin elem
))
4077 (org-element-backward)
4078 (let ((prev-elem (org-element-at-point)))
4079 (when (or (org-element-nested-p elem prev-elem
)
4080 (and (eq (org-element-type elem
) 'headline
)
4081 (not (eq (org-element-type prev-elem
) 'headline
))))
4083 (error "Cannot drag element backward"))
4084 ;; Compute new position of point: it's shifted by PREV-ELEM
4086 (let ((size-prev (- (org-element-property :end prev-elem
)
4087 (org-element-property :begin prev-elem
))))
4088 (org-element-swap-A-B prev-elem elem
)
4089 (goto-char (- pos size-prev
))))))
4091 (defun org-element-drag-forward ()
4092 "Move forward element at point."
4094 (let* ((pos (point))
4095 (elem (org-element-at-point)))
4096 (when (= (point-max) (org-element-property :end elem
))
4097 (error "Cannot drag element forward"))
4098 (goto-char (org-element-property :end elem
))
4099 (let ((next-elem (org-element-at-point)))
4100 (when (or (org-element-nested-p elem next-elem
)
4101 (and (eq (org-element-type next-elem
) 'headline
)
4102 (not (eq (org-element-type elem
) 'headline
))))
4104 (error "Cannot drag element forward"))
4105 ;; Compute new position of point: it's shifted by NEXT-ELEM
4106 ;; body's length (without final blanks) and by the length of
4107 ;; blanks between ELEM and NEXT-ELEM.
4108 (let ((size-next (- (save-excursion
4109 (goto-char (org-element-property :end next-elem
))
4110 (skip-chars-backward " \r\t\n")
4113 (org-element-property :begin next-elem
)))
4114 (size-blank (- (org-element-property :end elem
)
4116 (goto-char (org-element-property :end elem
))
4117 (skip-chars-backward " \r\t\n")
4120 (org-element-swap-A-B elem next-elem
)
4121 (goto-char (+ pos size-next size-blank
))))))
4123 (defun org-element-forward ()
4124 "Move forward by one element.
4125 Move to the next element at the same level, when possible."
4127 (if (eobp) (error "Cannot move further down")
4128 (let* ((trail (org-element-at-point 'keep-trail
))
4129 (element (car trail
))
4130 (type (org-element-type element
))
4131 (end (org-element-property :end element
)))
4133 ;; At an headline, move to next headline at the same level.
4134 ((eq type
'headline
) (goto-char end
))
4135 ;; At an item. Move to the next item, if possible.
4136 ((and (eq type
'item
)
4137 (let* ((struct (org-element-property :structure element
))
4138 (prevs (org-list-prevs-alist struct
))
4139 (beg (org-element-property :begin element
))
4140 (next-item (org-list-get-next-item beg struct prevs
)))
4141 (when next-item
(goto-char next-item
)))))
4142 ;; In any other case, move to element's end, unless this
4143 ;; position is also the end of its parent's contents, in which
4144 ;; case, directly jump to parent's end.
4147 ;; Determine if TRAIL contains the real parent of ELEMENT.
4148 (and (> (length trail
) 1)
4149 (let* ((parent-candidate (car (last trail
))))
4150 (and (memq (org-element-type parent-candidate
)
4151 org-element-greater-elements
)
4152 (>= (org-element-property
4153 :contents-end parent-candidate
) end
)
4154 parent-candidate
)))))
4155 (cond ((not parent
) (goto-char end
))
4156 ((= (org-element-property :contents-end parent
) end
)
4157 (goto-char (org-element-property :end parent
)))
4158 (t (goto-char end
)))))))))
4160 (defun org-element-mark-element ()
4161 "Put point at beginning of this element, mark at end.
4163 Interactively, if this command is repeated or (in Transient Mark
4164 mode) if the mark is active, it marks the next element after the
4165 ones already marked."
4167 (let (deactivate-mark)
4168 (if (or (and (eq last-command this-command
) (mark t
))
4169 (and transient-mark-mode mark-active
))
4173 (goto-char (org-element-property :end
(org-element-at-point)))))
4174 (let ((element (org-element-at-point)))
4176 (push-mark (org-element-property :end element
) t t
)
4177 (goto-char (org-element-property :begin element
))))))
4179 (defun org-narrow-to-element ()
4180 "Narrow buffer to current element."
4182 (let ((elem (org-element-at-point)))
4184 ((eq (car elem
) 'headline
)
4186 (org-element-property :begin elem
)
4187 (org-element-property :end elem
)))
4188 ((memq (car elem
) org-element-greater-elements
)
4190 (org-element-property :contents-begin elem
)
4191 (org-element-property :contents-end elem
)))
4194 (org-element-property :begin elem
)
4195 (org-element-property :end elem
))))))
4197 (defun org-transpose-elements ()
4198 "Transpose current and previous elements, keeping blank lines between.
4199 Point is moved after both elements."
4201 (org-skip-whitespace)
4203 (cur (org-element-at-point)))
4204 (when (= (save-excursion (goto-char (point-min))
4205 (org-skip-whitespace)
4207 (org-element-property :begin cur
))
4208 (error "No previous element"))
4209 (goto-char (org-element-property :begin cur
))
4211 (let ((prev (org-element-at-point)))
4212 (when (org-element-nested-p cur prev
)
4214 (error "Cannot transpose nested elements"))
4215 (org-element-swap-A-B prev cur
))))
4217 (defun org-element-unindent-buffer ()
4218 "Un-indent the visible part of the buffer.
4219 Relative indentation \(between items, inside blocks, etc.\) isn't
4222 (unless (eq major-mode
'org-mode
)
4223 (error "Cannot un-indent a buffer not in Org mode"))
4224 (let* ((parse-tree (org-element-parse-buffer 'greater-element
))
4225 unindent-tree
; For byte-compiler.
4229 (mapc (lambda (element)
4230 (if (eq (org-element-type element
) 'headline
)
4231 (funcall unindent-tree
4232 (org-element-contents element
))
4236 (org-element-property :begin element
)
4237 (org-element-property :end element
))
4238 (org-do-remove-indentation)))))
4239 (reverse contents
))))))
4240 (funcall unindent-tree
(org-element-contents parse-tree
))))
4242 (defun org-element-up ()
4243 "Move to upper element."
4246 ((bobp) (error "No surrounding element"))
4247 ((org-with-limited-levels (org-at-heading-p))
4248 (or (org-up-heading-safe) (error "No surronding element")))
4250 (let* ((trail (org-element-at-point 'keep-trail
))
4251 (element (car trail
))
4252 (type (org-element-type element
)))
4254 ;; At an item, with a parent in the list: move to that parent.
4255 ((and (eq type
'item
)
4256 (let* ((beg (org-element-property :begin element
))
4257 (struct (org-element-property :structure element
))
4258 (parents (org-list-parents-alist struct
))
4259 (parentp (org-list-get-parent beg struct parents
)))
4260 (and parentp
(goto-char parentp
)))))
4261 ;; Determine parent in the trail.
4264 (and (> (length trail
) 1)
4265 (let ((parentp (car (last trail
))))
4266 (and (memq (org-element-type parentp
)
4267 org-element-greater-elements
)
4268 (>= (org-element-property :contents-end parentp
)
4269 (org-element-property :end element
))
4272 ;; When parent is found move to its beginning.
4273 (parent (goto-char (org-element-property :begin parent
)))
4274 ;; If no parent was found, move to headline above, if any
4275 ;; or return an error.
4276 ((org-before-first-heading-p) (error "No surrounding element"))
4277 (t (org-back-to-heading))))))))))
4279 (defun org-element-down ()
4280 "Move to inner element."
4282 (let ((element (org-element-at-point)))
4284 ((memq (org-element-type element
) '(plain-list table
))
4285 (goto-char (org-element-property :contents-begin element
))
4287 ((memq (org-element-type element
) org-element-greater-elements
)
4288 ;; If contents are hidden, first disclose them.
4289 (when (org-element-property :hiddenp element
) (org-cycle))
4290 (goto-char (org-element-property :contents-begin element
)))
4291 (t (error "No inner element")))))
4294 (provide 'org-element
)
4295 ;;; org-element.el ends here