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-row' and `verse-block'. Among them, `paragraph'
50 ;; and `verse-block' types can contain Org objects and plain text.
52 ;; Objects are related to document's contents. Some of them are
53 ;; recursive. Associated types are of the following: `bold', `code',
54 ;; `entity', `export-snippet', `footnote-reference',
55 ;; `inline-babel-call', `inline-src-block', `italic',
56 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
57 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
58 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
60 ;; Some elements also have special properties whose value can hold
61 ;; objects themselves (i.e. an item tag or an headline name). Such
62 ;; values are called "secondary strings". Any object belongs to
63 ;; either an element or a secondary string.
65 ;; Notwithstanding affiliated keywords, each greater element, element
66 ;; and object has a fixed set of properties attached to it. Among
67 ;; them, three are shared by all types: `:begin' and `:end', which
68 ;; refer to the beginning and ending buffer positions of the
69 ;; considered element or object, and `:post-blank', which holds the
70 ;; number of blank lines, or white spaces, at its end. Greater
71 ;; elements and elements containing objects will also have
72 ;; `:contents-begin' and `:contents-end' properties to delimit
75 ;; Lisp-wise, an element or an object can be represented as a list.
76 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
77 ;; TYPE is a symbol describing the Org element or object.
78 ;; PROPERTIES is the property list attached to it. See docstring of
79 ;; appropriate parsing function to get an exhaustive
81 ;; CONTENTS is a list of elements, objects or raw strings contained
82 ;; in the current element or object, when applicable.
84 ;; An Org buffer is a nested list of such elements and objects, whose
85 ;; type is `org-data' and properties is nil.
87 ;; The first part of this file implements a parser and an interpreter
88 ;; for each type of Org syntax.
90 ;; The next two parts introduce four accessors and a function
91 ;; retrieving the element starting at point (respectively
92 ;; `org-element-type', `org-element-property', `org-element-contents',
93 ;; `org-element-restriction' and `org-element-current-element').
95 ;; The following part creates a fully recursive buffer parser. It
96 ;; also provides a tool to map a function to elements or objects
97 ;; matching some criteria in the parse tree. Functions of interest
98 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
99 ;; extent, `org-element-parse-secondary-string'.
101 ;; The penultimate part is the cradle of an interpreter for the
102 ;; obtained parse tree: `org-element-interpret-data'.
104 ;; The library ends by furnishing a set of interactive tools for
105 ;; element's navigation and manipulation, mostly based on
106 ;; `org-element-at-point' function.
111 (eval-when-compile (require 'cl
))
113 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
118 ;; For each greater element type, we define a parser and an
121 ;; A parser returns the element or object as the list described above.
122 ;; Most of them accepts no argument. Though, exceptions exist. Hence
123 ;; every element containing a secondary string (see
124 ;; `org-element-secondary-value-alist') will accept an optional
125 ;; argument to toggle parsing of that secondary string. Moreover,
126 ;; `item' parser requires current list's structure as its first
129 ;; An interpreter accepts two arguments: the list representation of
130 ;; the element or object, and its contents. The latter may be nil,
131 ;; depending on the element or object considered. It returns the
132 ;; appropriate Org syntax, as a string.
134 ;; Parsing functions must follow the naming convention:
135 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
136 ;; defined in `org-element-greater-elements'.
138 ;; Similarly, interpreting functions must follow the naming
139 ;; convention: org-element-TYPE-interpreter.
141 ;; With the exception of `headline' and `item' types, greater elements
142 ;; cannot contain other greater elements of their own type.
144 ;; Beside implementing a parser and an interpreter, adding a new
145 ;; greater element requires to tweak `org-element-current-element'.
146 ;; Moreover, the newly defined type must be added to both
147 ;; `org-element-all-elements' and `org-element-greater-elements'.
152 (defun org-element-center-block-parser ()
153 "Parse a center block.
155 Return a list whose CAR is `center-block' and CDR is a plist
156 containing `:begin', `:end', `:hiddenp', `:contents-begin',
157 `:contents-end' and `:post-blank' keywords.
159 Assume point is at the beginning of the block."
161 (let* ((case-fold-search t
)
162 (keywords (org-element-collect-affiliated-keywords))
163 (begin (car keywords
))
164 (contents-begin (progn (forward-line) (point)))
165 (hidden (org-truely-invisible-p))
167 (progn (re-search-forward "^[ \t]*#\\+END_CENTER" nil t
)
169 (pos-before-blank (progn (forward-line) (point)))
170 (end (progn (org-skip-whitespace)
171 (if (eobp) (point) (point-at-bol)))))
176 :contents-begin
,contents-begin
177 :contents-end
,contents-end
178 :post-blank
,(count-lines pos-before-blank end
)
179 ,@(cadr keywords
))))))
181 (defun org-element-center-block-interpreter (center-block contents
)
182 "Interpret CENTER-BLOCK element as Org syntax.
183 CONTENTS is the contents of the element."
184 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents
))
189 (defun org-element-drawer-parser ()
192 Return a list whose CAR is `drawer' and CDR is a plist containing
193 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
194 `:contents-end' and `:post-blank' keywords.
196 Assume point is at beginning of drawer."
198 (let* ((case-fold-search t
)
199 (name (progn (looking-at org-drawer-regexp
)
200 (org-match-string-no-properties 1)))
201 (keywords (org-element-collect-affiliated-keywords))
202 (begin (car keywords
))
203 (contents-begin (progn (forward-line) (point)))
204 (hidden (org-truely-invisible-p))
205 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
207 (pos-before-blank (progn (forward-line) (point)))
208 (end (progn (org-skip-whitespace)
209 (if (eobp) (point) (point-at-bol)))))
215 :contents-begin
,contents-begin
216 :contents-end
,contents-end
217 :post-blank
,(count-lines pos-before-blank end
)
218 ,@(cadr keywords
))))))
220 (defun org-element-drawer-interpreter (drawer contents
)
221 "Interpret DRAWER element as Org syntax.
222 CONTENTS is the contents of the element."
223 (format ":%s:\n%s:END:"
224 (org-element-property :drawer-name drawer
)
230 (defun org-element-dynamic-block-parser ()
231 "Parse a dynamic block.
233 Return a list whose CAR is `dynamic-block' and CDR is a plist
234 containing `:block-name', `:begin', `:end', `:hiddenp',
235 `:contents-begin', `:contents-end', `:arguments' and
236 `:post-blank' keywords.
238 Assume point is at beginning of dynamic block."
240 (let* ((case-fold-search t
)
241 (name (progn (looking-at org-dblock-start-re
)
242 (org-match-string-no-properties 1)))
243 (arguments (org-match-string-no-properties 3))
244 (keywords (org-element-collect-affiliated-keywords))
245 (begin (car keywords
))
246 (contents-begin (progn (forward-line) (point)))
247 (hidden (org-truely-invisible-p))
248 (contents-end (progn (re-search-forward org-dblock-end-re nil t
)
250 (pos-before-blank (progn (forward-line) (point)))
251 (end (progn (org-skip-whitespace)
252 (if (eobp) (point) (point-at-bol)))))
257 :arguments
,arguments
259 :contents-begin
,contents-begin
260 :contents-end
,contents-end
261 :post-blank
,(count-lines pos-before-blank end
)
262 ,@(cadr keywords
))))))
264 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
265 "Interpret DYNAMIC-BLOCK element as Org syntax.
266 CONTENTS is the contents of the element."
267 (format "#+BEGIN: %s%s\n%s#+END:"
268 (org-element-property :block-name dynamic-block
)
269 (let ((args (org-element-property :arguments dynamic-block
)))
270 (and args
(concat " " args
)))
274 ;;;; Footnote Definition
276 (defun org-element-footnote-definition-parser ()
277 "Parse a footnote definition.
279 Return a list whose CAR is `footnote-definition' and CDR is
280 a plist containing `:label', `:begin' `:end', `:contents-begin',
281 `:contents-end' and `:post-blank' keywords.
283 Assume point is at the beginning of the footnote definition."
285 (looking-at org-footnote-definition-re
)
286 (let* ((label (org-match-string-no-properties 1))
287 (keywords (org-element-collect-affiliated-keywords))
288 (begin (car keywords
))
289 (contents-begin (progn (search-forward "]")
290 (org-skip-whitespace)
292 (contents-end (if (progn
295 (concat org-outline-regexp-bol
"\\|"
296 org-footnote-definition-re
"\\|"
297 "^[ \t]*$") nil
'move
))
300 (end (progn (org-skip-whitespace)
301 (if (eobp) (point) (point-at-bol)))))
302 `(footnote-definition
306 :contents-begin
,contents-begin
307 :contents-end
,contents-end
308 :post-blank
,(count-lines contents-end end
)
309 ,@(cadr keywords
))))))
311 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
312 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
313 CONTENTS is the contents of the footnote-definition."
314 (concat (format "[%s]" (org-element-property :label footnote-definition
))
321 (defun org-element-headline-parser (&optional raw-secondary-p
)
324 Return a list whose CAR is `headline' and CDR is a plist
325 containing `:raw-value', `:title', `:begin', `:end',
326 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
327 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
328 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
329 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
332 The plist also contains any property set in the property drawer,
333 with its name in lowercase, the underscores replaced with hyphens
334 and colons at the beginning (i.e. `:custom-id').
336 When RAW-SECONDARY-P is non-nil, headline's title will not be
337 parsed as a secondary string, but as a plain string instead.
339 Assume point is at beginning of the headline."
341 (let* ((components (org-heading-components))
342 (level (nth 1 components
))
343 (todo (nth 2 components
))
345 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
346 (tags (let ((raw-tags (nth 5 components
)))
347 (and raw-tags
(org-split-string raw-tags
":"))))
348 (raw-value (nth 4 components
))
350 (let ((case-fold-search nil
))
351 (string-match (format "^%s +" org-quote-string
) raw-value
)))
353 (let ((case-fold-search nil
))
354 (string-match (format "^%s +" org-comment-string
) raw-value
)))
355 (archivedp (member org-archive-tag tags
))
356 (footnote-section-p (and org-footnote-section
357 (string= org-footnote-section raw-value
)))
358 (standard-props (let (plist)
361 (let ((p-name (downcase (car p
))))
362 (while (string-match "_" p-name
)
364 (replace-match "-" nil nil p-name
)))
365 (setq p-name
(intern (concat ":" p-name
)))
367 (plist-put plist p-name
(cdr p
)))))
368 (org-entry-properties nil
'standard
))
370 (time-props (org-entry-properties nil
'special
"CLOCK"))
371 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
372 (deadline (cdr (assoc "DEADLINE" time-props
)))
373 (clock (cdr (assoc "CLOCK" time-props
)))
374 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
376 (pos-after-head (save-excursion (forward-line) (point)))
377 (contents-begin (save-excursion (forward-line)
378 (org-skip-whitespace)
379 (if (eobp) (point) (point-at-bol))))
380 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
381 (end (progn (goto-char (org-end-of-subtree t t
))))
382 (contents-end (progn (skip-chars-backward " \r\t\n")
386 ;; Clean RAW-VALUE from any quote or comment string.
387 (when (or quotedp commentedp
)
389 (replace-regexp-in-string
390 (concat "\\(" org-quote-string
"\\|" org-comment-string
"\\) +")
393 ;; Clean TAGS from archive tag, if any.
394 (when archivedp
(setq tags
(delete org-archive-tag tags
)))
397 (if raw-secondary-p raw-value
398 (org-element-parse-secondary-string
399 raw-value
(org-element-restriction 'headline
))))
401 (:raw-value
,raw-value
405 :pre-blank
,(count-lines pos-after-head contents-begin
)
407 :contents-begin
,contents-begin
408 :contents-end
,contents-end
410 :priority
,(nth 3 components
)
413 :todo-type
,todo-type
414 :scheduled
,scheduled
416 :timestamp
,timestamp
418 :post-blank
,(count-lines contents-end end
)
419 :footnote-section-p
,footnote-section-p
420 :archivedp
,archivedp
421 :commentedp
,commentedp
423 ,@standard-props
)))))
425 (defun org-element-headline-interpreter (headline contents
)
426 "Interpret HEADLINE element as Org syntax.
427 CONTENTS is the contents of the element."
428 (let* ((level (org-element-property :level headline
))
429 (todo (org-element-property :todo-keyword headline
))
430 (priority (org-element-property :priority headline
))
431 (title (org-element-interpret-data
432 (org-element-property :title headline
)))
433 (tags (let ((tag-list (if (org-element-property :archivedp headline
)
434 (cons org-archive-tag
435 (org-element-property :tags headline
))
436 (org-element-property :tags headline
))))
438 (format ":%s:" (mapconcat 'identity tag-list
":")))))
439 (commentedp (org-element-property :commentedp headline
))
440 (quotedp (org-element-property :quotedp headline
))
441 (pre-blank (or (org-element-property :pre-blank headline
) 0))
442 (heading (concat (make-string level ?
*)
443 (and todo
(concat " " todo
))
444 (and quotedp
(concat " " org-quote-string
))
445 (and commentedp
(concat " " org-comment-string
))
447 (format " [#%s]" (char-to-string priority
)))
448 (cond ((and org-footnote-section
449 (org-element-property
450 :footnote-section-p headline
))
451 (concat " " org-footnote-section
))
452 (title (concat " " title
))))))
457 ((zerop org-tags-column
) (format " %s" tags
))
458 ((< org-tags-column
0)
461 (max (- (+ org-tags-column
(length heading
) (length tags
))) 1)
466 (make-string (max (- org-tags-column
(length heading
)) 1) ?
)
468 (make-string (1+ pre-blank
) 10)
474 (defun org-element-inlinetask-parser (&optional raw-secondary-p
)
475 "Parse an inline task.
477 Return a list whose CAR is `inlinetask' and CDR is a plist
478 containing `:title', `:begin', `:end', `:hiddenp',
479 `:contents-begin' and `:contents-end', `:level', `:priority',
480 `:tags', `:todo-keyword', `:todo-type', `:scheduled',
481 `:deadline', `:timestamp', `:clock' and `:post-blank' keywords.
483 The plist also contains any property set in the property drawer,
484 with its name in lowercase, the underscores replaced with hyphens
485 and colons at the beginning (i.e. `:custom-id').
487 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
488 title will not be parsed as a secondary string, but as a plain
491 Assume point is at beginning of the inline task."
493 (let* ((keywords (org-element-collect-affiliated-keywords))
494 (begin (car keywords
))
495 (components (org-heading-components))
496 (todo (nth 2 components
))
498 (if (member todo org-done-keywords
) 'done
'todo
)))
499 (tags (let ((raw-tags (nth 5 components
)))
500 (and raw-tags
(org-split-string raw-tags
":"))))
501 (title (if raw-secondary-p
(nth 4 components
)
502 (org-element-parse-secondary-string
504 (org-element-restriction 'inlinetask
))))
505 (standard-props (let (plist)
508 (let ((p-name (downcase (car p
))))
509 (while (string-match "_" p-name
)
511 (replace-match "-" nil nil p-name
)))
512 (setq p-name
(intern (concat ":" p-name
)))
514 (plist-put plist p-name
(cdr p
)))))
515 (org-entry-properties nil
'standard
))
517 (time-props (org-entry-properties nil
'special
"CLOCK"))
518 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
519 (deadline (cdr (assoc "DEADLINE" time-props
)))
520 (clock (cdr (assoc "CLOCK" time-props
)))
521 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
522 (contents-begin (save-excursion (forward-line) (point)))
523 (hidden (org-truely-invisible-p))
524 (pos-before-blank (org-inlinetask-goto-end))
525 ;; In the case of a single line task, CONTENTS-BEGIN and
526 ;; CONTENTS-END might overlap.
527 (contents-end (max contents-begin
528 (if (not (bolp)) (point-at-bol)
529 (save-excursion (forward-line -
1) (point)))))
530 (end (progn (org-skip-whitespace)
531 (if (eobp) (point) (point-at-bol)))))
536 :hiddenp
,(and (> contents-end contents-begin
) hidden
)
537 :contents-begin
,contents-begin
538 :contents-end
,contents-end
539 :level
,(nth 1 components
)
540 :priority
,(nth 3 components
)
543 :todo-type
,todo-type
544 :scheduled
,scheduled
546 :timestamp
,timestamp
548 :post-blank
,(count-lines pos-before-blank end
)
550 ,@(cadr keywords
))))))
552 (defun org-element-inlinetask-interpreter (inlinetask contents
)
553 "Interpret INLINETASK element as Org syntax.
554 CONTENTS is the contents of inlinetask."
555 (let* ((level (org-element-property :level inlinetask
))
556 (todo (org-element-property :todo-keyword inlinetask
))
557 (priority (org-element-property :priority inlinetask
))
558 (title (org-element-interpret-data
559 (org-element-property :title inlinetask
)))
560 (tags (let ((tag-list (org-element-property :tags inlinetask
)))
562 (format ":%s:" (mapconcat 'identity tag-list
":")))))
563 (task (concat (make-string level ?
*)
564 (and todo
(concat " " todo
))
566 (format " [#%s]" (char-to-string priority
)))
567 (and title
(concat " " title
)))))
572 ((zerop org-tags-column
) (format " %s" tags
))
573 ((< org-tags-column
0)
576 (max (- (+ org-tags-column
(length task
) (length tags
))) 1)
581 (make-string (max (- org-tags-column
(length task
)) 1) ?
)
583 ;; Prefer degenerate inlinetasks when there are no
588 (make-string level ?
*) " END")))))
593 (defun org-element-item-parser (struct &optional raw-secondary-p
)
596 STRUCT is the structure of the plain list.
598 Return a list whose CAR is `item' and CDR is a plist containing
599 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
600 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
601 `:post-blank' keywords.
603 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
604 any, will not be parsed as a secondary string, but as a plain
607 Assume point is at the beginning of the item."
610 (let* ((begin (point))
611 (bullet (org-list-get-bullet (point) struct
))
612 (checkbox (let ((box (org-list-get-checkbox begin struct
)))
613 (cond ((equal "[ ]" box
) 'off
)
614 ((equal "[X]" box
) 'on
)
615 ((equal "[-]" box
) 'trans
))))
616 (counter (let ((c (org-list-get-counter begin struct
)))
619 ((string-match "[A-Za-z]" c
)
620 (- (string-to-char (upcase (match-string 0 c
)))
622 ((string-match "[0-9]+" c
)
623 (string-to-number (match-string 0 c
))))))
625 (let ((raw-tag (org-list-get-tag begin struct
)))
627 (if raw-secondary-p raw-tag
628 (org-element-parse-secondary-string
629 raw-tag
(org-element-restriction 'item
))))))
630 (end (org-list-get-item-end begin struct
))
631 (contents-begin (progn (looking-at org-list-full-item-re
)
632 (goto-char (match-end 0))
633 (org-skip-whitespace)
634 ;; If first line isn't empty,
635 ;; contents really start at the text
636 ;; after item's meta-data.
637 (if (= (point-at-bol) begin
) (point)
639 (hidden (progn (forward-line)
640 (and (not (= (point) end
))
641 (org-truely-invisible-p))))
642 (contents-end (progn (goto-char end
)
643 (skip-chars-backward " \r\t\n")
650 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
651 ;; up in the case of an empty item separated
652 ;; from the next by a blank line. Thus, ensure
653 ;; the former is always the smallest of two.
654 :contents-begin
,(min contents-begin contents-end
)
655 :contents-end
,(max contents-begin contents-end
)
661 :post-blank
,(count-lines contents-end end
))))))
663 (defun org-element-item-interpreter (item contents
)
664 "Interpret ITEM element as Org syntax.
665 CONTENTS is the contents of the element."
667 (let* ((beg (org-element-property :begin item
))
668 (struct (org-element-property :structure item
))
669 (pre (org-list-prevs-alist struct
))
670 (bul (org-element-property :bullet item
)))
671 (org-list-bullet-string
672 (if (not (eq (org-list-get-list-type beg struct pre
) 'ordered
)) "-"
676 (org-list-get-item-number
677 beg struct pre
(org-list-parents-alist struct
))))))
680 (if (eq org-plain-list-ordered-item-terminator ?\
)) ")"
682 (checkbox (org-element-property :checkbox item
))
683 (counter (org-element-property :counter item
))
684 (tag (let ((tag (org-element-property :tag item
)))
685 (and tag
(org-element-interpret-data tag
))))
686 ;; Compute indentation.
687 (ind (make-string (length bullet
) 32)))
691 (and counter
(format "[@%d] " counter
))
693 ((eq checkbox
'on
) "[X] ")
694 ((eq checkbox
'off
) "[ ] ")
695 ((eq checkbox
'trans
) "[-] "))
696 (and tag
(format "%s :: " tag
))
698 (replace-regexp-in-string "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))))
703 (defun org-element-plain-list-parser (&optional structure
)
706 Optional argument STRUCTURE, when non-nil, is the structure of
707 the plain list being parsed.
709 Return a list whose CAR is `plain-list' and CDR is a plist
710 containing `:type', `:begin', `:end', `:contents-begin' and
711 `:contents-end', `:structure' and `:post-blank' keywords.
713 Assume point is at the beginning of the list."
715 (let* ((struct (or structure
(org-list-struct)))
716 (prevs (org-list-prevs-alist struct
))
717 (parents (org-list-parents-alist struct
))
718 (type (org-list-get-list-type (point) struct prevs
))
719 (contents-begin (point))
720 (keywords (org-element-collect-affiliated-keywords))
721 (begin (car keywords
))
723 (goto-char (org-list-get-list-end (point) struct prevs
)))
724 (end (save-excursion (org-skip-whitespace)
725 (if (eobp) (point) (point-at-bol)))))
726 ;; Blank lines below list belong to the top-level list only.
727 (unless (= (org-list-get-top-point struct
) contents-begin
)
728 (setq end
(min (org-list-get-bottom-point struct
)
729 (progn (org-skip-whitespace)
730 (if (eobp) (point) (point-at-bol))))))
736 :contents-begin
,contents-begin
737 :contents-end
,contents-end
739 :post-blank
,(count-lines contents-end end
)
740 ,@(cadr keywords
))))))
742 (defun org-element-plain-list-interpreter (plain-list contents
)
743 "Interpret PLAIN-LIST element as Org syntax.
744 CONTENTS is the contents of the element."
750 (defun org-element-quote-block-parser ()
751 "Parse a quote block.
753 Return a list whose CAR is `quote-block' and CDR is a plist
754 containing `:begin', `:end', `:hiddenp', `:contents-begin',
755 `:contents-end' and `:post-blank' keywords.
757 Assume point is at the beginning of the block."
759 (let* ((case-fold-search t
)
760 (keywords (org-element-collect-affiliated-keywords))
761 (begin (car keywords
))
762 (contents-begin (progn (forward-line) (point)))
763 (hidden (org-truely-invisible-p))
764 (contents-end (progn (re-search-forward "^[ \t]*#\\+END_QUOTE" nil t
)
766 (pos-before-blank (progn (forward-line) (point)))
767 (end (progn (org-skip-whitespace)
768 (if (eobp) (point) (point-at-bol)))))
773 :contents-begin
,contents-begin
774 :contents-end
,contents-end
775 :post-blank
,(count-lines pos-before-blank end
)
776 ,@(cadr keywords
))))))
778 (defun org-element-quote-block-interpreter (quote-block contents
)
779 "Interpret QUOTE-BLOCK element as Org syntax.
780 CONTENTS is the contents of the element."
781 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
786 (defun org-element-section-parser ()
789 Return a list whose CAR is `section' and CDR is a plist
790 containing `:begin', `:end', `:contents-begin', `contents-end'
791 and `:post-blank' keywords."
793 ;; Beginning of section is the beginning of the first non-blank
794 ;; line after previous headline.
795 (org-with-limited-levels
798 (outline-previous-heading)
799 (if (not (org-at-heading-p)) (point)
800 (forward-line) (org-skip-whitespace) (point-at-bol))))
801 (end (progn (outline-next-heading) (point)))
802 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
808 :contents-begin
,begin
809 :contents-end
,pos-before-blank
810 :post-blank
,(count-lines pos-before-blank end
)))))))
812 (defun org-element-section-interpreter (section contents
)
813 "Interpret SECTION element as Org syntax.
814 CONTENTS is the contents of the element."
820 (defun org-element-special-block-parser ()
821 "Parse a special block.
823 Return a list whose CAR is `special-block' and CDR is a plist
824 containing `:type', `:begin', `:end', `:hiddenp',
825 `:contents-begin', `:contents-end' and `:post-blank' keywords.
827 Assume point is at the beginning of the block."
829 (let* ((case-fold-search t
)
830 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\([-A-Za-z0-9]+\\)")
831 (org-match-string-no-properties 1)))
832 (keywords (org-element-collect-affiliated-keywords))
833 (begin (car keywords
))
834 (contents-begin (progn (forward-line) (point)))
835 (hidden (org-truely-invisible-p))
837 (progn (re-search-forward (concat "^[ \t]*#\\+END_" type
) nil t
)
839 (pos-before-blank (progn (forward-line) (point)))
840 (end (progn (org-skip-whitespace)
841 (if (eobp) (point) (point-at-bol)))))
847 :contents-begin
,contents-begin
848 :contents-end
,contents-end
849 :post-blank
,(count-lines pos-before-blank end
)
850 ,@(cadr keywords
))))))
852 (defun org-element-special-block-interpreter (special-block contents
)
853 "Interpret SPECIAL-BLOCK element as Org syntax.
854 CONTENTS is the contents of the element."
855 (let ((block-type (org-element-property :type special-block
)))
856 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
862 ;; For each element, a parser and an interpreter are also defined.
863 ;; Both follow the same naming convention used for greater elements.
865 ;; Also, as for greater elements, adding a new element type is done
866 ;; through the following steps: implement a parser and an interpreter,
867 ;; tweak `org-element-current-element' so that it recognizes the new
868 ;; type and add that new type to `org-element-all-elements'.
870 ;; As a special case, when the newly defined type is a block type,
871 ;; `org-element-block-name-alist' has to be modified accordingly.
876 (defun org-element-babel-call-parser ()
879 Return a list whose CAR is `babel-call' and CDR is a plist
880 containing `:begin', `:end', `:info' and `:post-blank' as
883 (let ((case-fold-search t
)
884 (info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
885 (org-babel-lob-get-info)))
886 (begin (point-at-bol))
887 (pos-before-blank (progn (forward-line) (point)))
888 (end (progn (org-skip-whitespace)
889 (if (eobp) (point) (point-at-bol)))))
894 :post-blank
,(count-lines pos-before-blank end
))))))
896 (defun org-element-babel-call-interpreter (babel-call contents
)
897 "Interpret BABEL-CALL element as Org syntax.
899 (let* ((babel-info (org-element-property :info babel-call
))
900 (main (car babel-info
))
901 (post-options (nth 1 babel-info
)))
903 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main
)) main
904 ;; Remove redundant square brackets.
905 (replace-match (match-string 1 main
) nil nil main
))
906 (and post-options
(format "[%s]" post-options
)))))
911 (defun org-element-clock-parser ()
914 Return a list whose CAR is `clock' and CDR is a plist containing
915 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
918 (let* ((case-fold-search nil
)
920 (value (progn (search-forward org-clock-string
(line-end-position) t
)
921 (org-skip-whitespace)
922 (looking-at "\\[.*\\]")
923 (org-match-string-no-properties 0)))
924 (time (and (progn (goto-char (match-end 0))
925 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
926 (org-match-string-no-properties 1)))
927 (status (if time
'closed
'running
))
928 (post-blank (let ((before-blank (progn (forward-line) (point))))
929 (org-skip-whitespace)
930 (unless (eobp) (beginning-of-line))
931 (count-lines before-blank
(point))))
933 `(clock (:status
,status
938 :post-blank
,post-blank
)))))
940 (defun org-element-clock-interpreter (clock contents
)
941 "Interpret CLOCK element as Org syntax.
943 (concat org-clock-string
" "
944 (org-element-property :value clock
)
945 (let ((time (org-element-property :time clock
)))
950 (org-split-string time
":")))))))
955 (defun org-element-comment-parser ()
958 Return a list whose CAR is `comment' and CDR is a plist
959 containing `:begin', `:end', `:value' and `:post-blank'
962 Assume point is at comment beginning."
964 (let* ((keywords (org-element-collect-affiliated-keywords))
965 (begin (car keywords
))
966 ;; Match first line with a loose regexp since it might as
967 ;; well be an ill-defined keyword.
969 (looking-at "#\\+? ?")
970 (buffer-substring-no-properties
971 (match-end 0) (progn (forward-line) (point)))))
973 ;; Get comments ending. This may not be accurate if
974 ;; commented lines within an item are followed by
975 ;; commented lines outside of a list. Though, parser will
976 ;; always get it right as it already knows surrounding
977 ;; element and has narrowed buffer to its contents.
979 (while (looking-at "\\(\\(# ?\\)[^+]\\|[ \t]*#\\+\\( \\|$\\)\\)")
980 ;; Accumulate lines without leading hash and plus sign
981 ;; if any. First whitespace is also ignored.
984 (buffer-substring-no-properties
985 (or (match-end 2) (match-end 3))
986 (progn (forward-line) (point))))))
988 (end (progn (goto-char com-end
)
989 (org-skip-whitespace)
990 (if (eobp) (point) (point-at-bol)))))
995 :post-blank
,(count-lines com-end end
)
996 ,@(cadr keywords
))))))
998 (defun org-element-comment-interpreter (comment contents
)
999 "Interpret COMMENT element as Org syntax.
1001 (replace-regexp-in-string "^" "#+ " (org-element-property :value comment
)))
1006 (defun org-element-comment-block-parser ()
1007 "Parse an export block.
1009 Return a list whose CAR is `comment-block' and CDR is a plist
1010 containing `:begin', `:end', `:hiddenp', `:value' and
1011 `:post-blank' keywords.
1013 Assume point is at comment block beginning."
1015 (let* ((case-fold-search t
)
1016 (keywords (org-element-collect-affiliated-keywords))
1017 (begin (car keywords
))
1018 (contents-begin (progn (forward-line) (point)))
1019 (hidden (org-truely-invisible-p))
1021 (progn (re-search-forward "^[ \t]*#\\+END_COMMENT" nil t
)
1023 (pos-before-blank (progn (forward-line) (point)))
1024 (end (progn (org-skip-whitespace)
1025 (if (eobp) (point) (point-at-bol))))
1026 (value (buffer-substring-no-properties contents-begin contents-end
)))
1032 :post-blank
,(count-lines pos-before-blank end
)
1033 ,@(cadr keywords
))))))
1035 (defun org-element-comment-block-interpreter (comment-block contents
)
1036 "Interpret COMMENT-BLOCK element as Org syntax.
1038 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1039 (org-remove-indentation (org-element-property :value comment-block
))))
1044 (defun org-element-example-block-parser ()
1045 "Parse an example block.
1047 Return a list whose CAR is `example-block' and CDR is a plist
1048 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1049 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1050 `:switches', `:value' and `:post-blank' keywords."
1052 (let* ((case-fold-search t
)
1054 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1055 (org-match-string-no-properties 1)))
1056 ;; Switches analysis
1057 (number-lines (cond ((not switches
) nil
)
1058 ((string-match "-n\\>" switches
) 'new
)
1059 ((string-match "+n\\>" switches
) 'continued
)))
1060 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1061 ;; Should labels be retained in (or stripped from) example
1065 (not (string-match "-r\\>" switches
))
1066 (and number-lines
(string-match "-k\\>" switches
))))
1067 ;; What should code-references use - labels or
1071 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1072 (label-fmt (and switches
1073 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1074 (match-string 1 switches
)))
1075 ;; Standard block parsing.
1076 (keywords (org-element-collect-affiliated-keywords))
1077 (begin (car keywords
))
1078 (contents-begin (progn (forward-line) (point)))
1079 (hidden (org-truely-invisible-p))
1081 (progn (re-search-forward "^[ \t]*#\\+END_EXAMPLE" nil t
)
1083 (value (buffer-substring-no-properties contents-begin contents-end
))
1084 (pos-before-blank (progn (forward-line) (point)))
1085 (end (progn (org-skip-whitespace)
1086 (if (eobp) (point) (point-at-bol)))))
1092 :number-lines
,number-lines
1093 :preserve-indent
,preserve-indent
1094 :retain-labels
,retain-labels
1095 :use-labels
,use-labels
1096 :label-fmt
,label-fmt
1098 :post-blank
,(count-lines pos-before-blank end
)
1099 ,@(cadr keywords
))))))
1101 (defun org-element-example-block-interpreter (example-block contents
)
1102 "Interpret EXAMPLE-BLOCK element as Org syntax.
1104 (let ((switches (org-element-property :switches example-block
)))
1105 (concat "#+BEGIN_EXAMPLE" (and switches
(concat " " switches
)) "\n"
1106 (org-remove-indentation
1107 (org-element-property :value example-block
))
1113 (defun org-element-export-block-parser ()
1114 "Parse an export block.
1116 Return a list whose CAR is `export-block' and CDR is a plist
1117 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1118 `:post-blank' keywords.
1120 Assume point is at export-block beginning."
1122 (let* ((case-fold-search t
)
1123 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\([A-Za-z0-9]+\\)")
1124 (upcase (org-match-string-no-properties 1))))
1125 (keywords (org-element-collect-affiliated-keywords))
1126 (begin (car keywords
))
1127 (contents-begin (progn (forward-line) (point)))
1128 (hidden (org-truely-invisible-p))
1130 (progn (re-search-forward (concat "^[ \t]*#\\+END_" type
) nil t
)
1132 (pos-before-blank (progn (forward-line) (point)))
1133 (end (progn (org-skip-whitespace)
1134 (if (eobp) (point) (point-at-bol))))
1135 (value (buffer-substring-no-properties contents-begin contents-end
)))
1142 :post-blank
,(count-lines pos-before-blank end
)
1143 ,@(cadr keywords
))))))
1145 (defun org-element-export-block-interpreter (export-block contents
)
1146 "Interpret EXPORT-BLOCK element as Org syntax.
1148 (let ((type (org-element-property :type export-block
)))
1149 (concat (format "#+BEGIN_%s\n" type
)
1150 (org-element-property :value export-block
)
1151 (format "#+END_%s" type
))))
1156 (defun org-element-fixed-width-parser ()
1157 "Parse a fixed-width section.
1159 Return a list whose CAR is `fixed-width' and CDR is a plist
1160 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1162 Assume point is at the beginning of the fixed-width area."
1164 (let* ((keywords (org-element-collect-affiliated-keywords))
1165 (begin (car keywords
))
1168 ;; Ending position may not be accurate if fixed-width
1169 ;; lines within an item are followed by fixed-width lines
1170 ;; outside of a list. Though, parser will always get it
1171 ;; right as it already knows surrounding element and has
1172 ;; narrowed buffer to its contents.
1174 (while (looking-at "[ \t]*:\\( \\|$\\)")
1175 ;, Accumulate text without starting colons.
1178 (buffer-substring-no-properties
1179 (match-end 0) (point-at-eol))
1183 (end (progn (org-skip-whitespace)
1184 (if (eobp) (point) (point-at-bol)))))
1189 :post-blank
,(count-lines end-area end
)
1190 ,@(cadr keywords
))))))
1192 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1193 "Interpret FIXED-WIDTH element as Org syntax.
1195 (replace-regexp-in-string
1196 "^" ": " (substring (org-element-property :value fixed-width
) 0 -
1)))
1199 ;;;; Horizontal Rule
1201 (defun org-element-horizontal-rule-parser ()
1202 "Parse an horizontal rule.
1204 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1205 containing `:begin', `:end' and `:post-blank' keywords."
1207 (let* ((keywords (org-element-collect-affiliated-keywords))
1208 (begin (car keywords
))
1209 (post-hr (progn (forward-line) (point)))
1210 (end (progn (org-skip-whitespace)
1211 (if (eobp) (point) (point-at-bol)))))
1215 :post-blank
,(count-lines post-hr end
)
1216 ,@(cadr keywords
))))))
1218 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1219 "Interpret HORIZONTAL-RULE element as Org syntax.
1226 (defun org-element-keyword-parser ()
1227 "Parse a keyword at point.
1229 Return a list whose CAR is `keyword' and CDR is a plist
1230 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1233 (let* ((case-fold-search t
)
1235 (key (progn (looking-at
1236 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1237 (upcase (org-match-string-no-properties 1))))
1238 (value (org-trim (buffer-substring-no-properties
1239 (match-end 0) (point-at-eol))))
1240 (pos-before-blank (progn (forward-line) (point)))
1241 (end (progn (org-skip-whitespace)
1242 (if (eobp) (point) (point-at-bol)))))
1248 :post-blank
,(count-lines pos-before-blank end
))))))
1250 (defun org-element-keyword-interpreter (keyword contents
)
1251 "Interpret KEYWORD element as Org syntax.
1254 (org-element-property :key keyword
)
1255 (org-element-property :value keyword
)))
1258 ;;;; Latex Environment
1260 (defun org-element-latex-environment-parser ()
1261 "Parse a LaTeX environment.
1263 Return a list whose CAR is `latex-environment' and CDR is a plist
1264 containing `:begin', `:end', `:value' and `:post-blank'
1267 Assume point is at the beginning of the latex environment."
1269 (let* ((case-fold-search t
)
1270 (code-begin (point))
1271 (keywords (org-element-collect-affiliated-keywords))
1272 (begin (car keywords
))
1273 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
1274 (regexp-quote (match-string 1))))
1276 (progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env
))
1279 (value (buffer-substring-no-properties code-begin code-end
))
1280 (end (progn (org-skip-whitespace)
1281 (if (eobp) (point) (point-at-bol)))))
1286 :post-blank
,(count-lines code-end end
)
1287 ,@(cadr keywords
))))))
1289 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1290 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1292 (org-element-property :value latex-environment
))
1297 (defun org-element-paragraph-parser ()
1300 Return a list whose CAR is `paragraph' and CDR is a plist
1301 containing `:begin', `:end', `:contents-begin' and
1302 `:contents-end' and `:post-blank' keywords.
1304 Assume point is at the beginning of the paragraph."
1306 (let* ((contents-begin (point))
1307 (keywords (org-element-collect-affiliated-keywords))
1308 (begin (car keywords
))
1310 (progn (end-of-line)
1311 (if (re-search-forward org-element-paragraph-separate nil
'm
)
1312 (progn (forward-line -
1) (end-of-line) (point))
1314 (pos-before-blank (progn (forward-line) (point)))
1315 (end (progn (org-skip-whitespace)
1316 (if (eobp) (point) (point-at-bol)))))
1320 :contents-begin
,contents-begin
1321 :contents-end
,contents-end
1322 :post-blank
,(count-lines pos-before-blank end
)
1323 ,@(cadr keywords
))))))
1325 (defun org-element-paragraph-interpreter (paragraph contents
)
1326 "Interpret PARAGRAPH element as Org syntax.
1327 CONTENTS is the contents of the element."
1333 (defun org-element-planning-parser ()
1336 Return a list whose CAR is `planning' and CDR is a plist
1337 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1338 and `:post-blank' keywords."
1340 (let* ((case-fold-search nil
)
1342 (post-blank (let ((before-blank (progn (forward-line) (point))))
1343 (org-skip-whitespace)
1344 (unless (eobp) (beginning-of-line))
1345 (count-lines before-blank
(point))))
1347 closed deadline scheduled
)
1349 (while (re-search-forward org-keyword-time-not-clock-regexp
1350 (line-end-position) t
)
1351 (goto-char (match-end 1))
1352 (org-skip-whitespace)
1353 (let ((time (buffer-substring-no-properties (point) (match-end 0)))
1354 (keyword (match-string 1)))
1355 (cond ((equal keyword org-closed-string
) (setq closed time
))
1356 ((equal keyword org-deadline-string
) (setq deadline time
))
1357 (t (setq scheduled time
)))))
1361 :scheduled
,scheduled
1364 :post-blank
,post-blank
)))))
1366 (defun org-element-planning-interpreter (planning contents
)
1367 "Interpret PLANNING element as Org syntax.
1372 (list (let ((closed (org-element-property :closed planning
)))
1373 (when closed
(concat org-closed-string
" " closed
)))
1374 (let ((deadline (org-element-property :deadline planning
)))
1375 (when deadline
(concat org-deadline-string
" " deadline
)))
1376 (let ((scheduled (org-element-property :scheduled planning
)))
1377 (when scheduled
(concat org-scheduled-string
" " scheduled
)))))
1381 ;;;; Property Drawer
1383 (defun org-element-property-drawer-parser ()
1384 "Parse a property drawer.
1386 Return a list whose CAR is `property-drawer' and CDR is a plist
1387 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1388 `:contents-end', `:properties' and `:post-blank' keywords.
1390 Assume point is at the beginning of the property drawer."
1392 (let ((case-fold-search t
)
1394 (prop-begin (progn (forward-line) (point)))
1395 (hidden (org-truely-invisible-p))
1398 (while (not (looking-at "^[ \t]*:END:"))
1399 (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
1400 (push (cons (org-match-string-no-properties 1)
1402 (buffer-substring-no-properties
1403 (match-end 0) (point-at-eol))))
1407 (prop-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
1409 (pos-before-blank (progn (forward-line) (point)))
1410 (end (progn (org-skip-whitespace)
1411 (if (eobp) (point) (point-at-bol)))))
1416 :properties
,properties
1417 :post-blank
,(count-lines pos-before-blank end
))))))
1419 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1420 "Interpret PROPERTY-DRAWER element as Org syntax.
1422 (let ((props (org-element-property :properties property-drawer
)))
1425 (mapconcat (lambda (p)
1426 (format org-property-format
(format ":%s:" (car p
)) (cdr p
)))
1427 (nreverse props
) "\n")
1433 (defun org-element-quote-section-parser ()
1434 "Parse a quote section.
1436 Return a list whose CAR is `quote-section' and CDR is a plist
1437 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1439 Assume point is at beginning of the section."
1441 (let* ((begin (point))
1442 (end (progn (org-with-limited-levels (outline-next-heading))
1444 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1447 (value (buffer-substring-no-properties begin pos-before-blank
)))
1452 :post-blank
,(count-lines pos-before-blank end
))))))
1454 (defun org-element-quote-section-interpreter (quote-section contents
)
1455 "Interpret QUOTE-SECTION element as Org syntax.
1457 (org-element-property :value quote-section
))
1462 (defun org-element-src-block-parser ()
1465 Return a list whose CAR is `src-block' and CDR is a plist
1466 containing `:language', `:switches', `:parameters', `:begin',
1467 `:end', `:hiddenp', `:number-lines', `:retain-labels',
1468 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
1469 `:post-blank' keywords.
1471 Assume point is at the beginning of the block."
1473 (let* ((case-fold-search t
)
1474 (contents-begin (point))
1475 ;; Get affiliated keywords.
1476 (keywords (org-element-collect-affiliated-keywords))
1477 ;; Get beginning position.
1478 (begin (car keywords
))
1479 ;; Get language as a string.
1483 (concat "^[ \t]*#\\+BEGIN_SRC"
1484 "\\(?: +\\(\\S-+\\)\\)?"
1485 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
1487 (org-match-string-no-properties 1)))
1489 (switches (org-match-string-no-properties 2))
1491 (parameters (org-match-string-no-properties 3))
1492 ;; Switches analysis
1493 (number-lines (cond ((not switches
) nil
)
1494 ((string-match "-n\\>" switches
) 'new
)
1495 ((string-match "+n\\>" switches
) 'continued
)))
1496 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1497 (label-fmt (and switches
1498 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1499 (match-string 1 switches
)))
1500 ;; Should labels be retained in (or stripped from) src
1504 (not (string-match "-r\\>" switches
))
1505 (and number-lines
(string-match "-k\\>" switches
))))
1506 ;; What should code-references use - labels or
1510 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1511 ;; Get position at end of block.
1512 (contents-end (progn (re-search-forward "^[ \t]*#\\+END_SRC" nil t
)
1516 (value (buffer-substring-no-properties
1517 (save-excursion (goto-char contents-begin
)
1520 (match-beginning 0)))
1521 ;; Get position after ending blank lines.
1522 (end (progn (org-skip-whitespace)
1523 (if (eobp) (point) (point-at-bol))))
1524 ;; Get visibility status.
1525 (hidden (progn (goto-char contents-begin
)
1527 (org-truely-invisible-p))))
1529 (:language
,language
1530 :switches
,(and (org-string-nw-p switches
)
1531 (org-trim switches
))
1532 :parameters
,(and (org-string-nw-p parameters
)
1533 (org-trim parameters
))
1536 :number-lines
,number-lines
1537 :preserve-indent
,preserve-indent
1538 :retain-labels
,retain-labels
1539 :use-labels
,use-labels
1540 :label-fmt
,label-fmt
1543 :post-blank
,(count-lines contents-end end
)
1544 ,@(cadr keywords
))))))
1546 (defun org-element-src-block-interpreter (src-block contents
)
1547 "Interpret SRC-BLOCK element as Org syntax.
1549 (let ((lang (org-element-property :language src-block
))
1550 (switches (org-element-property :switches src-block
))
1551 (params (org-element-property :parameters src-block
))
1552 (value (let ((val (org-element-property :value src-block
)))
1555 (org-src-preserve-indentation val
)
1556 ((zerop org-edit-src-content-indentation
)
1557 (org-remove-indentation val
))
1559 (let ((ind (make-string
1560 org-edit-src-content-indentation
32)))
1561 (replace-regexp-in-string
1562 "\\(^\\)[ \t]*\\S-" ind
1563 (org-remove-indentation val
) nil nil
1)))))))
1564 (concat (format "#+BEGIN_SRC%s\n"
1565 (concat (and lang
(concat " " lang
))
1566 (and switches
(concat " " switches
))
1567 (and params
(concat " " params
))))
1574 (defun org-element-table-parser ()
1575 "Parse a table at point.
1577 Return a list whose CAR is `table' and CDR is a plist containing
1578 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
1579 `:contents-end', `:value' and `:post-blank' keywords.
1581 Assume point is at the beginning of the table."
1583 (let* ((case-fold-search t
)
1584 (table-begin (point))
1585 (type (if (org-at-table.el-p
) 'table.el
'org
))
1586 (keywords (org-element-collect-affiliated-keywords))
1587 (begin (car keywords
))
1588 (table-end (goto-char (marker-position (org-table-end t
))))
1590 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
1591 (push (org-match-string-no-properties 1) acc
)
1594 (pos-before-blank (point))
1595 (end (progn (org-skip-whitespace)
1596 (if (eobp) (point) (point-at-bol)))))
1602 ;; Only `org' tables have contents. `table.el' tables
1603 ;; use a `:value' property to store raw table as
1605 :contents-begin
,(and (eq type
'org
) table-begin
)
1606 :contents-end
,(and (eq type
'org
) table-end
)
1607 :value
,(and (eq type
'table.el
)
1608 (buffer-substring-no-properties
1609 table-begin table-end
))
1610 :post-blank
,(count-lines pos-before-blank end
)
1611 ,@(cadr keywords
))))))
1613 (defun org-element-table-interpreter (table contents
)
1614 "Interpret TABLE element as Org syntax.
1616 (if (eq (org-element-property :type table
) 'table.el
)
1617 (org-remove-indentation (org-element-property :value table
))
1618 (concat (with-temp-buffer (insert contents
)
1621 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm
))
1622 (reverse (org-element-property :tblfm table
))
1628 (defun org-element-table-row-parser ()
1629 "Parse table row at point.
1631 Return a list whose CAR is `table-row' and CDR is a plist
1632 containing `:begin', `:end', `:contents-begin', `:contents-end',
1633 `:type' and `:post-blank' keywords."
1635 (let* ((type (if (looking-at "^[ \t]*|-") 'rule
'standard
))
1637 ;; A table rule has no contents. In that case, ensure
1638 ;; CONTENTS-BEGIN matches CONTENTS-END.
1639 (contents-begin (if (eq type
'standard
)
1640 (progn (search-forward "|") (point))
1642 (skip-chars-backward " \r\t\n")
1644 (contents-end (progn (end-of-line)
1645 (skip-chars-backward " \r\t\n")
1647 (end (progn (forward-line) (point))))
1652 :contents-begin
,contents-begin
1653 :contents-end
,contents-end
1656 (defun org-element-table-row-interpreter (table-row contents
)
1657 "Interpret TABLE-ROW element as Org syntax.
1658 CONTENTS is the contents of the table row."
1659 (if (eq (org-element-property :type table-row
) 'rule
) "|-"
1660 (concat "| " contents
)))
1665 (defun org-element-verse-block-parser ()
1666 "Parse a verse block.
1668 Return a list whose CAR is `verse-block' and CDR is a plist
1669 containing `:begin', `:end', `:contents-begin', `:contents-end',
1670 `:hiddenp' and `:post-blank' keywords.
1672 Assume point is at beginning of the block."
1674 (let* ((case-fold-search t
)
1675 (keywords (org-element-collect-affiliated-keywords))
1676 (begin (car keywords
))
1677 (hidden (progn (forward-line) (org-truely-invisible-p)))
1678 (contents-begin (point))
1681 (re-search-forward (concat "^[ \t]*#\\+END_VERSE") nil t
)
1683 (pos-before-blank (progn (forward-line) (point)))
1684 (end (progn (org-skip-whitespace)
1685 (if (eobp) (point) (point-at-bol)))))
1689 :contents-begin
,contents-begin
1690 :contents-end
,contents-end
1692 :post-blank
,(count-lines pos-before-blank end
)
1693 ,@(cadr keywords
))))))
1695 (defun org-element-verse-block-interpreter (verse-block contents
)
1696 "Interpret VERSE-BLOCK element as Org syntax.
1697 CONTENTS is verse block contents."
1698 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents
))
1704 ;; Unlike to elements, interstices can be found between objects.
1705 ;; That's why, along with the parser, successor functions are provided
1706 ;; for each object. Some objects share the same successor (i.e. `code'
1707 ;; and `verbatim' objects).
1709 ;; A successor must accept a single argument bounding the search. It
1710 ;; will return either a cons cell whose CAR is the object's type, as
1711 ;; a symbol, and CDR the position of its next occurrence, or nil.
1713 ;; Successors follow the naming convention:
1714 ;; org-element-NAME-successor, where NAME is the name of the
1715 ;; successor, as defined in `org-element-all-successors'.
1717 ;; Some object types (i.e. `emphasis') are recursive. Restrictions on
1718 ;; object types they can contain will be specified in
1719 ;; `org-element-object-restrictions'.
1721 ;; Adding a new type of object is simple. Implement a successor,
1722 ;; a parser, and an interpreter for it, all following the naming
1723 ;; convention. Register type in `org-element-all-objects' and
1724 ;; successor in `org-element-all-successors'. Maybe tweak
1725 ;; restrictions about it, and that's it.
1730 (defun org-element-bold-parser ()
1731 "Parse bold object at point.
1733 Return a list whose CAR is `bold' and CDR is a plist with
1734 `:begin', `:end', `:contents-begin' and `:contents-end' and
1735 `:post-blank' keywords.
1737 Assume point is at the first star marker."
1739 (unless (bolp) (backward-char 1))
1740 (looking-at org-emph-re
)
1741 (let ((begin (match-beginning 2))
1742 (contents-begin (match-beginning 4))
1743 (contents-end (match-end 4))
1744 (post-blank (progn (goto-char (match-end 2))
1745 (skip-chars-forward " \t")))
1750 :contents-begin
,contents-begin
1751 :contents-end
,contents-end
1752 :post-blank
,post-blank
)))))
1754 (defun org-element-bold-interpreter (bold contents
)
1755 "Interpret BOLD object as Org syntax.
1756 CONTENTS is the contents of the object."
1757 (format "*%s*" contents
))
1759 (defun org-element-text-markup-successor (limit)
1760 "Search for the next text-markup object.
1762 LIMIT bounds the search.
1764 Return value is a cons cell whose CAR is a symbol among `bold',
1765 `italic', `underline', `strike-through', `code' and `verbatim'
1766 and CDR is beginning position."
1768 (unless (bolp) (backward-char))
1769 (when (re-search-forward org-emph-re limit t
)
1770 (let ((marker (match-string 3)))
1772 ((equal marker
"*") 'bold
)
1773 ((equal marker
"/") 'italic
)
1774 ((equal marker
"_") 'underline
)
1775 ((equal marker
"+") 'strike-through
)
1776 ((equal marker
"~") 'code
)
1777 ((equal marker
"=") 'verbatim
)
1778 (t (error "Unknown marker at %d" (match-beginning 3))))
1779 (match-beginning 2))))))
1784 (defun org-element-code-parser ()
1785 "Parse code object at point.
1787 Return a list whose CAR is `code' and CDR is a plist with
1788 `:value', `:begin', `:end' and `:post-blank' keywords.
1790 Assume point is at the first tilde marker."
1792 (unless (bolp) (backward-char 1))
1793 (looking-at org-emph-re
)
1794 (let ((begin (match-beginning 2))
1795 (value (org-match-string-no-properties 4))
1796 (post-blank (progn (goto-char (match-end 2))
1797 (skip-chars-forward " \t")))
1803 :post-blank
,post-blank
)))))
1805 (defun org-element-code-interpreter (code contents
)
1806 "Interpret CODE object as Org syntax.
1808 (format "~%s~" (org-element-property :value code
)))
1813 (defun org-element-entity-parser ()
1814 "Parse entity at point.
1816 Return a list whose CAR is `entity' and CDR a plist with
1817 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1818 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1821 Assume point is at the beginning of the entity."
1823 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1824 (let* ((value (org-entity-get (match-string 1)))
1825 (begin (match-beginning 0))
1826 (bracketsp (string= (match-string 2) "{}"))
1827 (post-blank (progn (goto-char (match-end 1))
1828 (when bracketsp
(forward-char 2))
1829 (skip-chars-forward " \t")))
1833 :latex
,(nth 1 value
)
1834 :latex-math-p
,(nth 2 value
)
1835 :html
,(nth 3 value
)
1836 :ascii
,(nth 4 value
)
1837 :latin1
,(nth 5 value
)
1838 :utf-8
,(nth 6 value
)
1841 :use-brackets-p
,bracketsp
1842 :post-blank
,post-blank
)))))
1844 (defun org-element-entity-interpreter (entity contents
)
1845 "Interpret ENTITY object as Org syntax.
1848 (org-element-property :name entity
)
1849 (when (org-element-property :use-brackets-p entity
) "{}")))
1851 (defun org-element-latex-or-entity-successor (limit)
1852 "Search for the next latex-fragment or entity object.
1854 LIMIT bounds the search.
1856 Return value is a cons cell whose CAR is `entity' or
1857 `latex-fragment' and CDR is beginning position."
1859 (let ((matchers (plist-get org-format-latex-options
:matchers
))
1860 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1862 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
1863 (when (re-search-forward
1864 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
1868 (goto-char (match-beginning 0))
1869 (if (looking-at entity-re
)
1870 ;; Determine if it's a real entity or a LaTeX command.
1871 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
1872 (match-beginning 0))
1873 ;; No entity nor command: point is at a LaTeX fragment.
1874 ;; Determine its type to get the correct beginning position.
1875 (cons 'latex-fragment
1878 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
1881 (nth 2 (assoc e org-latex-regexps
))))))
1888 (defun org-element-export-snippet-parser ()
1889 "Parse export snippet at point.
1891 Return a list whose CAR is `export-snippet' and CDR a plist with
1892 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1895 Assume point is at the beginning of the snippet."
1897 (looking-at "<\\([-A-Za-z0-9]+\\)@")
1898 (let* ((begin (point))
1899 (back-end (org-match-string-no-properties 1))
1900 (inner-begin (match-end 0))
1903 (goto-char inner-begin
)
1904 (while (and (> count
0) (re-search-forward "[<>]" nil t
))
1905 (if (equal (match-string 0) "<") (incf count
) (decf count
)))
1907 (value (buffer-substring-no-properties inner-begin inner-end
))
1908 (post-blank (skip-chars-forward " \t"))
1911 (:back-end
,back-end
1915 :post-blank
,post-blank
)))))
1917 (defun org-element-export-snippet-interpreter (export-snippet contents
)
1918 "Interpret EXPORT-SNIPPET object as Org syntax.
1921 (org-element-property :back-end export-snippet
)
1922 (org-element-property :value export-snippet
)))
1924 (defun org-element-export-snippet-successor (limit)
1925 "Search for the next export-snippet object.
1927 LIMIT bounds the search.
1929 Return value is a cons cell whose CAR is `export-snippet' CDR is
1930 its beginning position."
1933 (while (re-search-forward "<[-A-Za-z0-9]+@" limit t
)
1935 (let ((beg (match-beginning 0))
1937 (while (re-search-forward "[<>]" limit t
)
1938 (if (equal (match-string 0) "<") (incf count
) (decf count
))
1940 (throw 'exit
(cons 'export-snippet beg
))))))))))
1943 ;;;; Footnote Reference
1945 (defun org-element-footnote-reference-parser ()
1946 "Parse footnote reference at point.
1948 Return a list whose CAR is `footnote-reference' and CDR a plist
1949 with `:label', `:type', `:inline-definition', `:begin', `:end'
1950 and `:post-blank' as keywords."
1952 (looking-at org-footnote-re
)
1953 (let* ((begin (point))
1954 (label (or (org-match-string-no-properties 2)
1955 (org-match-string-no-properties 3)
1956 (and (match-string 1)
1957 (concat "fn:" (org-match-string-no-properties 1)))))
1958 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
1959 (inner-begin (match-end 0))
1963 (while (and (> count
0) (re-search-forward "[][]" nil t
))
1964 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
1966 (post-blank (progn (goto-char (1+ inner-end
))
1967 (skip-chars-forward " \t")))
1970 (and (eq type
'inline
)
1971 (org-element-parse-secondary-string
1972 (buffer-substring inner-begin inner-end
)
1973 (org-element-restriction 'footnote-reference
)))))
1974 `(footnote-reference
1977 :inline-definition
,inline-definition
1980 :post-blank
,post-blank
)))))
1982 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
1983 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1985 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
1988 (org-element-property :inline-definition footnote-reference
)))
1989 (if (not inline-def
) ""
1990 (concat ":" (org-element-interpret-data inline-def
))))))
1991 (format "[%s]" (concat label def
))))
1993 (defun org-element-footnote-reference-successor (limit)
1994 "Search for the next footnote-reference object.
1996 LIMIT bounds the search.
1998 Return value is a cons cell whose CAR is `footnote-reference' and
1999 CDR is beginning position."
2002 (while (re-search-forward org-footnote-re limit t
)
2004 (let ((beg (match-beginning 0))
2007 (while (re-search-forward "[][]" limit t
)
2008 (if (equal (match-string 0) "[") (incf count
) (decf count
))
2010 (throw 'exit
(cons 'footnote-reference beg
))))))))))
2013 ;;;; Inline Babel Call
2015 (defun org-element-inline-babel-call-parser ()
2016 "Parse inline babel call at point.
2018 Return a list whose CAR is `inline-babel-call' and CDR a plist
2019 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2021 Assume point is at the beginning of the babel call."
2023 (unless (bolp) (backward-char))
2024 (looking-at org-babel-inline-lob-one-liner-regexp
)
2025 (let ((info (save-match-data (org-babel-lob-get-info)))
2026 (begin (match-end 1))
2027 (post-blank (progn (goto-char (match-end 0))
2028 (skip-chars-forward " \t")))
2034 :post-blank
,post-blank
)))))
2036 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
2037 "Interpret INLINE-BABEL-CALL object as Org syntax.
2039 (let* ((babel-info (org-element-property :info inline-babel-call
))
2040 (main-source (car babel-info
))
2041 (post-options (nth 1 babel-info
)))
2043 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
2044 ;; Remove redundant square brackets.
2046 (match-string 1 main-source
) nil nil main-source
)
2048 (and post-options
(format "[%s]" post-options
)))))
2050 (defun org-element-inline-babel-call-successor (limit)
2051 "Search for the next inline-babel-call object.
2053 LIMIT bounds the search.
2055 Return value is a cons cell whose CAR is `inline-babel-call' and
2056 CDR is beginning position."
2058 ;; Use a simplified version of
2059 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
2060 (when (re-search-forward
2061 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
2063 (cons 'inline-babel-call
(match-beginning 0)))))
2066 ;;;; Inline Src Block
2068 (defun org-element-inline-src-block-parser ()
2069 "Parse inline source block at point.
2071 Return a list whose CAR is `inline-src-block' and CDR a plist
2072 with `:begin', `:end', `:language', `:value', `:parameters' and
2073 `:post-blank' as keywords.
2075 Assume point is at the beginning of the inline src block."
2077 (unless (bolp) (backward-char))
2078 (looking-at org-babel-inline-src-block-regexp
)
2079 (let ((begin (match-beginning 1))
2080 (language (org-match-string-no-properties 2))
2081 (parameters (org-match-string-no-properties 4))
2082 (value (org-match-string-no-properties 5))
2083 (post-blank (progn (goto-char (match-end 0))
2084 (skip-chars-forward " \t")))
2087 (:language
,language
2089 :parameters
,parameters
2092 :post-blank
,post-blank
)))))
2094 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
2095 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2097 (let ((language (org-element-property :language inline-src-block
))
2098 (arguments (org-element-property :parameters inline-src-block
))
2099 (body (org-element-property :value inline-src-block
)))
2100 (format "src_%s%s{%s}"
2102 (if arguments
(format "[%s]" arguments
) "")
2105 (defun org-element-inline-src-block-successor (limit)
2106 "Search for the next inline-babel-call element.
2108 LIMIT bounds the search.
2110 Return value is a cons cell whose CAR is `inline-babel-call' and
2111 CDR is beginning position."
2113 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
2114 (cons 'inline-src-block
(match-beginning 1)))))
2118 (defun org-element-italic-parser ()
2119 "Parse italic object at point.
2121 Return a list whose CAR is `italic' and CDR is a plist with
2122 `:begin', `:end', `:contents-begin' and `:contents-end' and
2123 `:post-blank' keywords.
2125 Assume point is at the first slash marker."
2127 (unless (bolp) (backward-char 1))
2128 (looking-at org-emph-re
)
2129 (let ((begin (match-beginning 2))
2130 (contents-begin (match-beginning 4))
2131 (contents-end (match-end 4))
2132 (post-blank (progn (goto-char (match-end 2))
2133 (skip-chars-forward " \t")))
2138 :contents-begin
,contents-begin
2139 :contents-end
,contents-end
2140 :post-blank
,post-blank
)))))
2142 (defun org-element-italic-interpreter (italic contents
)
2143 "Interpret ITALIC object as Org syntax.
2144 CONTENTS is the contents of the object."
2145 (format "/%s/" contents
))
2150 (defun org-element-latex-fragment-parser ()
2151 "Parse latex fragment at point.
2153 Return a list whose CAR is `latex-fragment' and CDR a plist with
2154 `:value', `:begin', `:end', and `:post-blank' as keywords.
2156 Assume point is at the beginning of the latex fragment."
2158 (let* ((begin (point))
2162 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
2163 (when (or (looking-at latex-regexp
)
2167 (looking-at latex-regexp
))))
2168 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
2169 (plist-get org-format-latex-options
:matchers
))
2170 ;; None found: it's a macro.
2171 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2173 (value (match-string-no-properties substring-match
))
2174 (post-blank (progn (goto-char (match-end substring-match
))
2175 (skip-chars-forward " \t")))
2181 :post-blank
,post-blank
)))))
2183 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
2184 "Interpret LATEX-FRAGMENT object as Org syntax.
2186 (org-element-property :value latex-fragment
))
2190 (defun org-element-line-break-parser ()
2191 "Parse line break at point.
2193 Return a list whose CAR is `line-break', and CDR a plist with
2194 `:begin', `:end' and `:post-blank' keywords.
2196 Assume point is at the beginning of the line break."
2197 (let ((begin (point))
2198 (end (save-excursion (forward-line) (point))))
2199 `(line-break (:begin
,begin
:end
,end
:post-blank
0))))
2201 (defun org-element-line-break-interpreter (line-break contents
)
2202 "Interpret LINE-BREAK object as Org syntax.
2206 (defun org-element-line-break-successor (limit)
2207 "Search for the next line-break object.
2209 LIMIT bounds the search.
2211 Return value is a cons cell whose CAR is `line-break' and CDR is
2212 beginning position."
2214 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2215 (goto-char (match-beginning 1)))))
2216 ;; A line break can only happen on a non-empty line.
2217 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2218 (cons 'line-break beg
)))))
2223 (defun org-element-link-parser ()
2224 "Parse link at point.
2226 Return a list whose CAR is `link' and CDR a plist with `:type',
2227 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2228 `:contents-end' and `:post-blank' as keywords.
2230 Assume point is at the beginning of the link."
2232 (let ((begin (point))
2233 end contents-begin contents-end link-end post-blank path type
2236 ;; Type 1: Text targeted from a radio target.
2237 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2239 link-end
(match-end 0)
2240 path
(org-match-string-no-properties 0)))
2241 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2242 ((looking-at org-bracket-link-regexp
)
2243 (setq contents-begin
(match-beginning 3)
2244 contents-end
(match-end 3)
2245 link-end
(match-end 0)
2246 ;; RAW-LINK is the original link.
2247 raw-link
(org-match-string-no-properties 1)
2248 link
(org-translate-link
2249 (org-link-expand-abbrev
2250 (org-link-unescape raw-link
))))
2251 ;; Determine TYPE of link and set PATH accordingly.
2254 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2255 (setq type
"file" path link
))
2256 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2257 ((string-match org-link-re-with-space3 link
)
2258 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2259 ;; Id type: PATH is the id.
2260 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2261 (setq type
"id" path
(match-string 1 link
)))
2262 ;; Code-ref type: PATH is the name of the reference.
2263 ((string-match "^(\\(.*\\))$" link
)
2264 (setq type
"coderef" path
(match-string 1 link
)))
2265 ;; Custom-id type: PATH is the name of the custom id.
2266 ((= (aref link
0) ?
#)
2267 (setq type
"custom-id" path
(substring link
1)))
2268 ;; Fuzzy type: Internal link either matches a target, an
2269 ;; headline name or nothing. PATH is the target or
2271 (t (setq type
"fuzzy" path link
))))
2272 ;; Type 3: Plain link, i.e. http://orgmode.org
2273 ((looking-at org-plain-link-re
)
2274 (setq raw-link
(org-match-string-no-properties 0)
2275 type
(org-match-string-no-properties 1)
2276 path
(org-match-string-no-properties 2)
2277 link-end
(match-end 0)))
2278 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2279 ((looking-at org-angle-link-re
)
2280 (setq raw-link
(buffer-substring-no-properties
2281 (match-beginning 1) (match-end 2))
2282 type
(org-match-string-no-properties 1)
2283 path
(org-match-string-no-properties 2)
2284 link-end
(match-end 0))))
2285 ;; In any case, deduce end point after trailing white space from
2286 ;; LINK-END variable.
2287 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2292 :raw-link
,(or raw-link path
)
2295 :contents-begin
,contents-begin
2296 :contents-end
,contents-end
2297 :post-blank
,post-blank
)))))
2299 (defun org-element-link-interpreter (link contents
)
2300 "Interpret LINK object as Org syntax.
2301 CONTENTS is the contents of the object, or nil."
2302 (let ((type (org-element-property :type link
))
2303 (raw-link (org-element-property :raw-link link
)))
2304 (if (string= type
"radio") raw-link
2307 (if contents
(format "[%s]" contents
) "")))))
2309 (defun org-element-link-successor (limit)
2310 "Search for the next link object.
2312 LIMIT bounds the search.
2314 Return value is a cons cell whose CAR is `link' and CDR is
2315 beginning position."
2318 (if (not org-target-link-regexp
) org-any-link-re
2319 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2320 (when (re-search-forward link-regexp limit t
)
2321 (cons 'link
(match-beginning 0))))))
2326 (defun org-element-macro-parser ()
2327 "Parse macro at point.
2329 Return a list whose CAR is `macro' and CDR a plist with `:key',
2330 `:args', `:begin', `:end', `:value' and `:post-blank' as
2333 Assume point is at the macro."
2335 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2336 (let ((begin (point))
2337 (key (downcase (org-match-string-no-properties 1)))
2338 (value (org-match-string-no-properties 0))
2339 (post-blank (progn (goto-char (match-end 0))
2340 (skip-chars-forward " \t")))
2342 (args (let ((args (org-match-string-no-properties 3)) args2
)
2344 (setq args
(org-split-string args
","))
2346 (while (string-match "\\\\\\'" (car args
))
2347 ;; Repair bad splits.
2348 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2351 (push (pop args
) args2
))
2352 (mapcar 'org-trim
(nreverse args2
))))))
2359 :post-blank
,post-blank
)))))
2361 (defun org-element-macro-interpreter (macro contents
)
2362 "Interpret MACRO object as Org syntax.
2364 (org-element-property :value macro
))
2366 (defun org-element-macro-successor (limit)
2367 "Search for the next macro object.
2369 LIMIT bounds the search.
2371 Return value is cons cell whose CAR is `macro' and CDR is
2372 beginning position."
2374 (when (re-search-forward
2375 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2377 (cons 'macro
(match-beginning 0)))))
2382 (defun org-element-radio-target-parser ()
2383 "Parse radio target at point.
2385 Return a list whose CAR is `radio-target' and CDR a plist with
2386 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2387 and `:post-blank' as keywords.
2389 Assume point is at the radio target."
2391 (looking-at org-radio-target-regexp
)
2392 (let ((begin (point))
2393 (contents-begin (match-beginning 1))
2394 (contents-end (match-end 1))
2395 (value (org-match-string-no-properties 1))
2396 (post-blank (progn (goto-char (match-end 0))
2397 (skip-chars-forward " \t")))
2402 :contents-begin
,contents-begin
2403 :contents-end
,contents-end
2404 :post-blank
,post-blank
2407 (defun org-element-radio-target-interpreter (target contents
)
2408 "Interpret TARGET object as Org syntax.
2409 CONTENTS is the contents of the object."
2410 (concat "<<<" contents
">>>"))
2412 (defun org-element-radio-target-successor (limit)
2413 "Search for the next radio-target object.
2415 LIMIT bounds the search.
2417 Return value is a cons cell whose CAR is `radio-target' and CDR
2418 is beginning position."
2420 (when (re-search-forward org-radio-target-regexp limit t
)
2421 (cons 'radio-target
(match-beginning 0)))))
2424 ;;;; Statistics Cookie
2426 (defun org-element-statistics-cookie-parser ()
2427 "Parse statistics cookie at point.
2429 Return a list whose CAR is `statistics-cookie', and CDR a plist
2430 with `:begin', `:end', `:value' and `:post-blank' keywords.
2432 Assume point is at the beginning of the statistics-cookie."
2434 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2435 (let* ((begin (point))
2436 (value (buffer-substring-no-properties
2437 (match-beginning 0) (match-end 0)))
2438 (post-blank (progn (goto-char (match-end 0))
2439 (skip-chars-forward " \t")))
2445 :post-blank
,post-blank
)))))
2447 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
2448 "Interpret STATISTICS-COOKIE object as Org syntax.
2450 (org-element-property :value statistics-cookie
))
2452 (defun org-element-statistics-cookie-successor (limit)
2453 "Search for the next statistics cookie object.
2455 LIMIT bounds the search.
2457 Return value is a cons cell whose CAR is `statistics-cookie' and
2458 CDR is beginning position."
2460 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
2461 (cons 'statistics-cookie
(match-beginning 0)))))
2466 (defun org-element-strike-through-parser ()
2467 "Parse strike-through object at point.
2469 Return a list whose CAR is `strike-through' and CDR is a plist
2470 with `:begin', `:end', `:contents-begin' and `:contents-end' and
2471 `:post-blank' keywords.
2473 Assume point is at the first plus sign marker."
2475 (unless (bolp) (backward-char 1))
2476 (looking-at org-emph-re
)
2477 (let ((begin (match-beginning 2))
2478 (contents-begin (match-beginning 4))
2479 (contents-end (match-end 4))
2480 (post-blank (progn (goto-char (match-end 2))
2481 (skip-chars-forward " \t")))
2486 :contents-begin
,contents-begin
2487 :contents-end
,contents-end
2488 :post-blank
,post-blank
)))))
2490 (defun org-element-strike-through-interpreter (strike-through contents
)
2491 "Interpret STRIKE-THROUGH object as Org syntax.
2492 CONTENTS is the contents of the object."
2493 (format "+%s+" contents
))
2498 (defun org-element-subscript-parser ()
2499 "Parse subscript at point.
2501 Return a list whose CAR is `subscript' and CDR a plist with
2502 `:begin', `:end', `:contents-begin', `:contents-end',
2503 `:use-brackets-p' and `:post-blank' as keywords.
2505 Assume point is at the underscore."
2507 (unless (bolp) (backward-char))
2508 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2510 (not (looking-at org-match-substring-regexp
))))
2511 (begin (match-beginning 2))
2512 (contents-begin (or (match-beginning 5)
2513 (match-beginning 3)))
2514 (contents-end (or (match-end 5) (match-end 3)))
2515 (post-blank (progn (goto-char (match-end 0))
2516 (skip-chars-forward " \t")))
2521 :use-brackets-p
,bracketsp
2522 :contents-begin
,contents-begin
2523 :contents-end
,contents-end
2524 :post-blank
,post-blank
)))))
2526 (defun org-element-subscript-interpreter (subscript contents
)
2527 "Interpret SUBSCRIPT object as Org syntax.
2528 CONTENTS is the contents of the object."
2530 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
2533 (defun org-element-sub/superscript-successor
(limit)
2534 "Search for the next sub/superscript object.
2536 LIMIT bounds the search.
2538 Return value is a cons cell whose CAR is either `subscript' or
2539 `superscript' and CDR is beginning position."
2541 (when (re-search-forward org-match-substring-regexp limit t
)
2542 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
2543 (match-beginning 2)))))
2548 (defun org-element-superscript-parser ()
2549 "Parse superscript at point.
2551 Return a list whose CAR is `superscript' and CDR a plist with
2552 `:begin', `:end', `:contents-begin', `:contents-end',
2553 `:use-brackets-p' and `:post-blank' as keywords.
2555 Assume point is at the caret."
2557 (unless (bolp) (backward-char))
2558 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
) t
2559 (not (looking-at org-match-substring-regexp
))))
2560 (begin (match-beginning 2))
2561 (contents-begin (or (match-beginning 5)
2562 (match-beginning 3)))
2563 (contents-end (or (match-end 5) (match-end 3)))
2564 (post-blank (progn (goto-char (match-end 0))
2565 (skip-chars-forward " \t")))
2570 :use-brackets-p
,bracketsp
2571 :contents-begin
,contents-begin
2572 :contents-end
,contents-end
2573 :post-blank
,post-blank
)))))
2575 (defun org-element-superscript-interpreter (superscript contents
)
2576 "Interpret SUPERSCRIPT object as Org syntax.
2577 CONTENTS is the contents of the object."
2579 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
2585 (defun org-element-table-cell-parser ()
2586 "Parse table cell at point.
2588 Return a list whose CAR is `table-cell' and CDR is a plist
2589 containing `:begin', `:end', `:contents-begin', `:contents-end'
2590 and `:post-blank' keywords."
2591 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
2592 (let* ((begin (match-beginning 0))
2594 (contents-begin (match-beginning 1))
2595 (contents-end (match-end 1)))
2599 :contents-begin
,contents-begin
2600 :contents-end
,contents-end
2603 (defun org-element-table-cell-interpreter (table-cell contents
)
2604 "Interpret TABLE-CELL element as Org syntax.
2605 CONTENTS is the contents of the cell, or nil."
2606 (concat " " contents
" |"))
2608 (defun org-element-table-cell-successor (limit)
2609 "Search for the next table-cell object.
2611 LIMIT bounds the search.
2613 Return value is a cons cell whose CAR is `table-cell' and CDR is
2614 beginning position."
2615 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell
(point))))
2620 (defun org-element-target-parser ()
2621 "Parse target at point.
2623 Return a list whose CAR is `target' and CDR a plist with
2624 `:begin', `:end', `:value' and `:post-blank' as keywords.
2626 Assume point is at the target."
2628 (looking-at org-target-regexp
)
2629 (let ((begin (point))
2630 (value (org-match-string-no-properties 1))
2631 (post-blank (progn (goto-char (match-end 0))
2632 (skip-chars-forward " \t")))
2638 :post-blank
,post-blank
)))))
2640 (defun org-element-target-interpreter (target contents
)
2641 "Interpret TARGET object as Org syntax.
2643 (format "<<%s>>" (org-element-property :value target
)))
2645 (defun org-element-target-successor (limit)
2646 "Search for the next target object.
2648 LIMIT bounds the search.
2650 Return value is a cons cell whose CAR is `target' and CDR is
2651 beginning position."
2653 (when (re-search-forward org-target-regexp limit t
)
2654 (cons 'target
(match-beginning 0)))))
2659 (defun org-element-timestamp-parser ()
2660 "Parse time stamp at point.
2662 Return a list whose CAR is `timestamp', and CDR a plist with
2663 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
2665 Assume point is at the beginning of the timestamp."
2667 (let* ((begin (point))
2669 ((looking-at org-tsr-regexp
)
2670 (if (match-string 2) 'active-range
'active
))
2671 ((looking-at org-tsr-regexp-both
)
2672 (if (match-string 2) 'inactive-range
'inactive
))
2675 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2677 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2679 (value (org-match-string-no-properties 0))
2680 (post-blank (progn (goto-char (match-end 0))
2681 (skip-chars-forward " \t")))
2688 :post-blank
,post-blank
)))))
2690 (defun org-element-timestamp-interpreter (timestamp contents
)
2691 "Interpret TIMESTAMP object as Org syntax.
2693 (org-element-property :value timestamp
))
2695 (defun org-element-timestamp-successor (limit)
2696 "Search for the next timestamp object.
2698 LIMIT bounds the search.
2700 Return value is a cons cell whose CAR is `timestamp' and CDR is
2701 beginning position."
2703 (when (re-search-forward
2704 (concat org-ts-regexp-both
2706 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2708 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2710 (cons 'timestamp
(match-beginning 0)))))
2715 (defun org-element-underline-parser ()
2716 "Parse underline object at point.
2718 Return a list whose CAR is `underline' and CDR is a plist with
2719 `:begin', `:end', `:contents-begin' and `:contents-end' and
2720 `:post-blank' keywords.
2722 Assume point is at the first underscore marker."
2724 (unless (bolp) (backward-char 1))
2725 (looking-at org-emph-re
)
2726 (let ((begin (match-beginning 2))
2727 (contents-begin (match-beginning 4))
2728 (contents-end (match-end 4))
2729 (post-blank (progn (goto-char (match-end 2))
2730 (skip-chars-forward " \t")))
2735 :contents-begin
,contents-begin
2736 :contents-end
,contents-end
2737 :post-blank
,post-blank
)))))
2739 (defun org-element-underline-interpreter (underline contents
)
2740 "Interpret UNDERLINE object as Org syntax.
2741 CONTENTS is the contents of the object."
2742 (format "_%s_" contents
))
2747 (defun org-element-verbatim-parser ()
2748 "Parse verbatim object at point.
2750 Return a list whose CAR is `verbatim' and CDR is a plist with
2751 `:value', `:begin', `:end' and `:post-blank' keywords.
2753 Assume point is at the first equal sign marker."
2755 (unless (bolp) (backward-char 1))
2756 (looking-at org-emph-re
)
2757 (let ((begin (match-beginning 2))
2758 (value (org-match-string-no-properties 4))
2759 (post-blank (progn (goto-char (match-end 2))
2760 (skip-chars-forward " \t")))
2766 :post-blank
,post-blank
)))))
2768 (defun org-element-verbatim-interpreter (verbatim contents
)
2769 "Interpret VERBATIM object as Org syntax.
2771 (format "=%s=" (org-element-property :value verbatim
)))
2775 ;;; Definitions And Rules
2777 ;; Define elements, greater elements and specify recursive objects,
2778 ;; along with the affiliated keywords recognized. Also set up
2779 ;; restrictions on recursive objects combinations.
2781 ;; These variables really act as a control center for the parsing
2783 (defconst org-element-paragraph-separate
2784 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2785 ;; Headlines and inlinetasks.
2786 org-outline-regexp-bol
"\\|"
2787 ;; Comments, blocks (any type), keywords and babel calls.
2788 "^[ \t]*#\\+" "\\|" "^#\\(?: \\|$\\)" "\\|"
2790 (org-item-beginning-re) "\\|"
2791 ;; Fixed-width, drawers (any type) and tables.
2793 ;; Footnote definitions.
2794 org-footnote-definition-re
"\\|"
2795 ;; Horizontal rules.
2796 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2797 ;; LaTeX environments.
2798 "^[ \t]*\\\\\\(begin\\|end\\)" "\\|"
2799 ;; Planning and Clock lines.
2801 org-clock-string
"\\|"
2802 org-closed-string
"\\|"
2803 org-deadline-string
"\\|"
2804 org-scheduled-string
"\\)")
2805 "Regexp to separate paragraphs in an Org buffer.")
2807 (defconst org-element-all-elements
2808 '(center-block clock comment comment-block drawer dynamic-block example-block
2809 export-block fixed-width footnote-definition headline
2810 horizontal-rule inlinetask item keyword latex-environment
2811 babel-call paragraph plain-list planning property-drawer
2812 quote-block quote-section section special-block src-block table
2813 table-row verse-block
)
2814 "Complete list of element types.")
2816 (defconst org-element-greater-elements
2817 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2818 item plain-list quote-block section special-block table
)
2819 "List of recursive element types aka Greater Elements.")
2821 (defconst org-element-all-successors
2822 '(export-snippet footnote-reference inline-babel-call inline-src-block
2823 latex-or-entity line-break link macro radio-target
2824 statistics-cookie sub
/superscript table-cell target
2825 text-markup timestamp
)
2826 "Complete list of successors.")
2828 (defconst org-element-object-successor-alist
2829 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
2830 (bold . text-markup
) (code . text-markup
) (italic . text-markup
)
2831 (strike-through . text-markup
) (underline . text-markup
)
2832 (verbatim . text-markup
) (entity . latex-or-entity
)
2833 (latex-fragment . latex-or-entity
))
2834 "Alist of translations between object type and successor name.
2836 Sharing the same successor comes handy when, for example, the
2837 regexp matching one object can also match the other object.")
2839 (defconst org-element-all-objects
2840 '(bold code entity export-snippet footnote-reference inline-babel-call
2841 inline-src-block italic line-break latex-fragment link macro
2842 radio-target statistics-cookie strike-through subscript superscript
2843 table-cell target timestamp underline verbatim
)
2844 "Complete list of object types.")
2846 (defconst org-element-recursive-objects
2847 '(bold italic link macro subscript radio-target strike-through superscript
2848 table-cell underline
)
2849 "List of recursive object types.")
2851 (defconst org-element-block-name-alist
2852 '(("ASCII" . org-element-export-block-parser
)
2853 ("CENTER" . org-element-center-block-parser
)
2854 ("COMMENT" . org-element-comment-block-parser
)
2855 ("DOCBOOK" . org-element-export-block-parser
)
2856 ("EXAMPLE" . org-element-example-block-parser
)
2857 ("HTML" . org-element-export-block-parser
)
2858 ("LATEX" . org-element-export-block-parser
)
2859 ("ODT" . org-element-export-block-parser
)
2860 ("QUOTE" . org-element-quote-block-parser
)
2861 ("SRC" . org-element-src-block-parser
)
2862 ("VERSE" . org-element-verse-block-parser
))
2863 "Alist between block names and the associated parsing function.
2864 Names must be uppercase. Any block whose name has no association
2865 is parsed with `org-element-special-block-parser'.")
2867 (defconst org-element-affiliated-keywords
2868 '("ATTR_ASCII" "ATTR_DOCBOOK" "ATTR_HTML" "ATTR_LATEX" "ATTR_ODT" "CAPTION"
2869 "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT" "RESULTS"
2870 "SOURCE" "SRCNAME" "TBLNAME")
2871 "List of affiliated keywords as strings.")
2873 (defconst org-element-keyword-translation-alist
2874 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
2875 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
2876 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
2877 "Alist of usual translations for keywords.
2878 The key is the old name and the value the new one. The property
2879 holding their value will be named after the translated name.")
2881 (defconst org-element-multiple-keywords
2882 '("ATTR_ASCII" "ATTR_DOCBOOK" "ATTR_HTML" "ATTR_LATEX" "ATTR_ODT" "HEADER")
2883 "List of affiliated keywords that can occur more that once in an element.
2885 Their value will be consed into a list of strings, which will be
2886 returned as the value of the property.
2888 This list is checked after translations have been applied. See
2889 `org-element-keyword-translation-alist'.")
2891 (defconst org-element-parsed-keywords
'("AUTHOR" "CAPTION" "TITLE")
2892 "List of keywords whose value can be parsed.
2894 Their value will be stored as a secondary string: a list of
2895 strings and objects.
2897 This list is checked after translations have been applied. See
2898 `org-element-keyword-translation-alist'.")
2900 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
2901 "List of keywords which can have a secondary value.
2903 In Org syntax, they can be written with optional square brackets
2904 before the colons. For example, results keyword can be
2905 associated to a hash value with the following:
2907 #+RESULTS[hash-string]: some-source
2909 This list is checked after translations have been applied. See
2910 `org-element-keyword-translation-alist'.")
2912 (defconst org-element-object-restrictions
2913 `((bold entity export-snippet inline-babel-call inline-src-block link
2914 radio-target sub
/superscript target text-markup timestamp
)
2915 (footnote-reference entity export-snippet footnote-reference
2916 inline-babel-call inline-src-block latex-fragment
2917 line-break link macro radio-target sub
/superscript
2918 target text-markup timestamp
)
2919 (headline entity inline-babel-call inline-src-block latex-fragment link
2920 macro radio-target statistics-cookie sub
/superscript target
2921 text-markup timestamp
)
2922 (inlinetask entity inline-babel-call inline-src-block latex-fragment link
2923 macro radio-target sub
/superscript target text-markup timestamp
)
2924 (italic entity export-snippet inline-babel-call inline-src-block link
2925 radio-target sub
/superscript target text-markup timestamp
)
2926 (item entity inline-babel-call latex-fragment macro radio-target
2927 sub
/superscript target text-markup
)
2928 (keyword entity latex-fragment macro sub
/superscript text-markup
)
2929 (link entity export-snippet inline-babel-call inline-src-block
2930 latex-fragment link sub
/superscript text-markup
)
2932 (paragraph ,@org-element-all-successors
)
2933 (radio-target entity export-snippet latex-fragment sub
/superscript
)
2934 (strike-through entity export-snippet inline-babel-call inline-src-block
2935 link radio-target sub
/superscript target text-markup
2937 (subscript entity export-snippet inline-babel-call inline-src-block
2938 latex-fragment sub
/superscript target text-markup
)
2939 (superscript entity export-snippet inline-babel-call inline-src-block
2940 latex-fragment sub
/superscript target text-markup
)
2941 (table-cell entity export-snippet latex-fragment link macro radio-target
2942 sub
/superscript target text-markup timestamp
)
2943 (table-row table-cell
)
2944 (underline entity export-snippet inline-babel-call inline-src-block link
2945 radio-target sub
/superscript target text-markup timestamp
)
2946 (verse-block entity footnote-reference inline-babel-call inline-src-block
2947 latex-fragment line-break link macro radio-target
2948 sub
/superscript target text-markup timestamp
))
2949 "Alist of objects restrictions.
2951 CAR is an element or object type containing objects and CDR is
2952 a list of successors that will be called within an element or
2953 object of such type.
2955 For example, in a `radio-target' object, one can only find
2956 entities, export snippets, latex-fragments, subscript and
2959 This alist also applies to secondary string. For example, an
2960 `headline' type element doesn't directly contain objects, but
2961 still has an entry since one of its properties (`:title') does.")
2963 (defconst org-element-secondary-value-alist
2964 '((headline .
:title
)
2965 (inlinetask .
:title
)
2967 (footnote-reference .
:inline-definition
))
2968 "Alist between element types and location of secondary value.")
2974 ;; Provide four accessors: `org-element-type', `org-element-property'
2975 ;; `org-element-contents' and `org-element-restriction'.
2977 (defun org-element-type (element)
2978 "Return type of element ELEMENT.
2980 The function returns the type of the element or object provided.
2981 It can also return the following special value:
2982 `plain-text' for a string
2983 `org-data' for a complete document
2984 nil in any other case."
2986 ((not (consp element
)) (and (stringp element
) 'plain-text
))
2987 ((symbolp (car element
)) (car element
))))
2989 (defun org-element-property (property element
)
2990 "Extract the value from the PROPERTY of an ELEMENT."
2991 (plist-get (nth 1 element
) property
))
2993 (defun org-element-contents (element)
2994 "Extract contents from an ELEMENT."
2995 (and (consp element
) (nthcdr 2 element
)))
2997 (defun org-element-restriction (element)
2998 "Return restriction associated to ELEMENT.
2999 ELEMENT can be an element, an object or a symbol representing an
3000 element or object type."
3001 (cdr (assq (if (symbolp element
) element
(org-element-type element
))
3002 org-element-object-restrictions
)))
3006 ;;; Parsing Element Starting At Point
3008 ;; `org-element-current-element' is the core function of this section.
3009 ;; It returns the Lisp representation of the element starting at
3012 ;; `org-element-current-element' makes use of special modes. They are
3013 ;; activated for fixed element chaining (i.e. `plain-list' > `item')
3014 ;; or fixed conditional element chaining (i.e. `headline' >
3015 ;; `section'). Special modes are: `section', `quote-section', `item'
3018 (defun org-element-current-element (&optional granularity special structure
)
3019 "Parse the element starting at point.
3021 Return value is a list like (TYPE PROPS) where TYPE is the type
3022 of the element and PROPS a plist of properties associated to the
3025 Possible types are defined in `org-element-all-elements'.
3027 Optional argument GRANULARITY determines the depth of the
3028 recursion. Allowed values are `headline', `greater-element',
3029 `element', `object' or nil. When it is broader than `object' (or
3030 nil), secondary values will not be parsed, since they only
3033 Optional argument SPECIAL, when non-nil, can be either `section',
3034 `quote-section', `table-row' and `item'.
3036 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3039 This function assumes point is always at the beginning of the
3040 element it has to parse."
3042 ;; If point is at an affiliated keyword, try moving to the
3043 ;; beginning of the associated element. If none is found, the
3044 ;; keyword is orphaned and will be treated as plain text.
3045 (when (looking-at org-element--affiliated-re
)
3046 (let ((opoint (point)))
3047 (while (looking-at org-element--affiliated-re
) (forward-line))
3048 (when (looking-at "[ \t]*$") (goto-char opoint
))))
3049 (let ((case-fold-search t
)
3050 ;; Determine if parsing depth allows for secondary strings
3051 ;; parsing. It only applies to elements referenced in
3052 ;; `org-element-secondary-value-alist'.
3053 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
3057 (org-element-item-parser (or structure
(org-list-struct))
3060 ((eq special
'quote-section
) (org-element-quote-section-parser))
3062 ((eq special
'table-row
) (org-element-table-row-parser))
3064 ((org-with-limited-levels (org-at-heading-p))
3065 (org-element-headline-parser raw-secondary-p
))
3066 ;; Section (must be checked after headline).
3067 ((eq special
'section
) (org-element-section-parser))
3068 ;; Planning and Clock.
3069 ((and (looking-at org-planning-or-clock-line-re
))
3070 (if (equal (match-string 1) org-clock-string
)
3071 (org-element-clock-parser)
3072 (org-element-planning-parser)))
3074 ((when (looking-at "[ \t]*#\\+BEGIN_\\([-A-Za-z0-9]+\\)\\(?: \\|$\\)")
3075 (let ((name (upcase (match-string 1))) parser
)
3077 ((not (save-excursion
3079 (format "^[ \t]*#\\+END_%s\\(?: \\|$\\)" name
) nil t
)))
3080 (org-element-paragraph-parser))
3081 ((setq parser
(assoc name org-element-block-name-alist
))
3082 (funcall (cdr parser
)))
3083 (t (org-element-special-block-parser))))))
3085 ((org-at-heading-p) (org-element-inlinetask-parser raw-secondary-p
))
3086 ;; LaTeX Environment.
3087 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
3090 (format "[ \t]*\\\\end{%s}[ \t]*"
3091 (regexp-quote (match-string 1)))
3093 (org-element-latex-environment-parser)
3094 (org-element-paragraph-parser)))
3095 ;; Drawer and Property Drawer.
3096 ((looking-at org-drawer-regexp
)
3097 (let ((name (match-string 1)))
3099 ((not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
)))
3100 (org-element-paragraph-parser))
3101 ((equal "PROPERTIES" name
) (org-element-property-drawer-parser))
3102 (t (org-element-drawer-parser)))))
3104 ((looking-at "[ \t]*:\\( \\|$\\)") (org-element-fixed-width-parser))
3105 ;; Babel Call, Dynamic Block and Keyword.
3106 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
3107 (let ((key (upcase (match-string 1))))
3109 ((equal key
"CALL") (org-element-babel-call-parser))
3110 ((and (equal key
"BEGIN")
3112 (re-search-forward "^[ \t]*#\\+END:\\(?: \\|$\\)" nil t
)))
3113 (org-element-dynamic-block-parser))
3114 ((and (not (equal key
"TBLFM"))
3115 (not (member key org-element-affiliated-keywords
)))
3116 (org-element-keyword-parser))
3117 (t (org-element-paragraph-parser)))))
3118 ;; Footnote Definition.
3119 ((looking-at org-footnote-definition-re
)
3120 (org-element-footnote-definition-parser))
3122 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)")
3123 (org-element-comment-parser))
3125 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3126 (org-element-horizontal-rule-parser))
3128 ((org-at-table-p t
) (org-element-table-parser))
3130 ((looking-at (org-item-re))
3131 (org-element-plain-list-parser (or structure
(org-list-struct))))
3132 ;; Default element: Paragraph.
3133 (t (org-element-paragraph-parser))))))
3136 ;; Most elements can have affiliated keywords. When looking for an
3137 ;; element beginning, we want to move before them, as they belong to
3138 ;; that element, and, in the meantime, collect information they give
3139 ;; into appropriate properties. Hence the following function.
3141 ;; Usage of optional arguments may not be obvious at first glance:
3143 ;; - TRANS-LIST is used to polish keywords names that have evolved
3144 ;; during Org history. In example, even though =result= and
3145 ;; =results= coexist, we want to have them under the same =result=
3146 ;; property. It's also true for "srcname" and "name", where the
3147 ;; latter seems to be preferred nowadays (thus the "name" property).
3149 ;; - CONSED allows to regroup multi-lines keywords under the same
3150 ;; property, while preserving their own identity. This is mostly
3151 ;; used for "attr_latex" and al.
3153 ;; - PARSED prepares a keyword value for export. This is useful for
3154 ;; "caption". Objects restrictions for such keywords are defined in
3155 ;; `org-element-object-restrictions'.
3157 ;; - DUALS is used to take care of keywords accepting a main and an
3158 ;; optional secondary values. For example "results" has its
3159 ;; source's name as the main value, and may have an hash string in
3160 ;; optional square brackets as the secondary one.
3162 ;; A keyword may belong to more than one category.
3164 (defconst org-element--affiliated-re
3165 (format "[ \t]*#\\+\\(%s\\):"
3168 (if (member keyword org-element-dual-keywords
)
3169 (format "\\(%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
3170 (regexp-quote keyword
))
3171 (regexp-quote keyword
)))
3172 org-element-affiliated-keywords
"\\|"))
3173 "Regexp matching any affiliated keyword.
3175 Keyword name is put in match group 1. Moreover, if keyword
3176 belongs to `org-element-dual-keywords', put the dual value in
3179 Don't modify it, set `org-element-affiliated-keywords' instead.")
3181 (defun org-element-collect-affiliated-keywords
3182 (&optional key-re trans-list consed parsed duals
)
3183 "Collect affiliated keywords before point.
3185 Optional argument KEY-RE is a regexp matching keywords, which
3186 puts matched keyword in group 1. It defaults to
3187 `org-element--affiliated-re'.
3189 TRANS-LIST is an alist where key is the keyword and value the
3190 property name it should be translated to, without the colons. It
3191 defaults to `org-element-keyword-translation-alist'.
3193 CONSED is a list of strings. Any keyword belonging to that list
3194 will have its value consed. The check is done after keyword
3195 translation. It defaults to `org-element-multiple-keywords'.
3197 PARSED is a list of strings. Any keyword member of this list
3198 will have its value parsed. The check is done after keyword
3199 translation. If a keyword is a member of both CONSED and PARSED,
3200 it's value will be a list of parsed strings. It defaults to
3201 `org-element-parsed-keywords'.
3203 DUALS is a list of strings. Any keyword member of this list can
3204 have two parts: one mandatory and one optional. Its value is
3205 a cons cell whose CAR is the former, and the CDR the latter. If
3206 a keyword is a member of both PARSED and DUALS, both values will
3207 be parsed. It defaults to `org-element-dual-keywords'.
3209 Return a list whose CAR is the position at the first of them and
3210 CDR a plist of keywords and values."
3212 (let ((case-fold-search t
)
3213 (key-re (or key-re org-element--affiliated-re
))
3214 (trans-list (or trans-list org-element-keyword-translation-alist
))
3215 (consed (or consed org-element-multiple-keywords
))
3216 (parsed (or parsed org-element-parsed-keywords
))
3217 (duals (or duals org-element-dual-keywords
))
3218 ;; RESTRICT is the list of objects allowed in parsed
3220 (restrict (org-element-restriction 'keyword
))
3223 (while (and (not (bobp))
3224 (progn (forward-line -
1) (looking-at key-re
)))
3225 (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
3226 ;; Apply translation to RAW-KWD. From there, KWD is
3227 ;; the official keyword.
3228 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
3229 ;; Find main value for any keyword.
3233 (buffer-substring-no-properties
3234 (match-end 0) (point-at-eol)))))
3235 ;; If KWD is a dual keyword, find its secondary
3236 ;; value. Maybe parse it.
3238 (and (member kwd duals
)
3239 (let ((sec (org-match-string-no-properties 3)))
3240 (if (or (not sec
) (not (member kwd parsed
))) sec
3241 (org-element-parse-secondary-string sec restrict
)))))
3242 ;; Attribute a property name to KWD.
3243 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
3244 ;; Now set final shape for VALUE.
3245 (when (member kwd parsed
)
3246 (setq value
(org-element-parse-secondary-string value restrict
)))
3247 (when (member kwd duals
)
3248 ;; VALUE is mandatory. Set it to nil if there is none.
3249 (setq value
(and value
(cons value dual-value
))))
3250 (when (member kwd consed
)
3251 (setq value
(cons value
(plist-get output kwd-sym
))))
3252 ;; Eventually store the new value in OUTPUT.
3253 (setq output
(plist-put output kwd-sym value
))))
3254 (unless (looking-at key-re
) (forward-line 1)))
3255 (list (point) output
))))
3261 ;; The two major functions here are `org-element-parse-buffer', which
3262 ;; parses Org syntax inside the current buffer, taking into account
3263 ;; region, narrowing, or even visibility if specified, and
3264 ;; `org-element-parse-secondary-string', which parses objects within
3267 ;; The (almost) almighty `org-element-map' allows to apply a function
3268 ;; on elements or objects matching some type, and accumulate the
3269 ;; resulting values. In an export situation, it also skips unneeded
3270 ;; parts of the parse tree.
3272 (defun org-element-parse-buffer (&optional granularity visible-only
)
3273 "Recursively parse the buffer and return structure.
3274 If narrowing is in effect, only parse the visible part of the
3277 Optional argument GRANULARITY determines the depth of the
3278 recursion. It can be set to the following symbols:
3280 `headline' Only parse headlines.
3281 `greater-element' Don't recurse into greater elements excepted
3282 headlines and sections. Thus, elements
3283 parsed are the top-level ones.
3284 `element' Parse everything but objects and plain text.
3285 `object' Parse the complete buffer (default).
3287 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3290 Assume buffer is in Org mode."
3292 (goto-char (point-min))
3293 (org-skip-whitespace)
3294 (nconc (list 'org-data nil
)
3295 (org-element-parse-elements
3296 (point-at-bol) (point-max)
3297 ;; Start in `section' mode so text before the first
3298 ;; headline belongs to a section.
3299 'section nil granularity visible-only nil
))))
3301 (defun org-element-parse-secondary-string (string restriction
)
3302 "Recursively parse objects in STRING and return structure.
3304 RESTRICTION, when non-nil, is a symbol limiting the object types
3305 that will be looked after."
3308 (org-element-parse-objects (point-min) (point-max) nil restriction
)))
3310 (defun org-element-map (data types fun
&optional info first-match no-recursion
)
3311 "Map a function on selected elements or objects.
3313 DATA is the parsed tree, as returned by, i.e,
3314 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3315 of elements or objects types. FUN is the function called on the
3316 matching element or object. It must accept one arguments: the
3317 element or object itself.
3319 When optional argument INFO is non-nil, it should be a plist
3320 holding export options. In that case, parts of the parse tree
3321 not exportable according to that property list will be skipped.
3323 When optional argument FIRST-MATCH is non-nil, stop at the first
3324 match for which FUN doesn't return nil, and return that value.
3326 Optional argument NO-RECURSION is a symbol or a list of symbols
3327 representing elements or objects types. `org-element-map' won't
3328 enter any recursive element or object whose type belongs to that
3329 list. Though, FUN can still be applied on them.
3331 Nil values returned from FUN do not appear in the results."
3332 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3333 (unless (listp types
) (setq types
(list types
)))
3334 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3335 ;; Recursion depth is determined by --CATEGORY.
3338 ((every (lambda (el) (memq el org-element-greater-elements
)) types
)
3340 ((every (lambda (el) (memq el org-element-all-elements
)) types
)
3348 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3349 ;; holding contextual information.
3350 (let ((--type (org-element-type --data
)))
3353 ;; Ignored element in an export context.
3354 ((and info
(member --data
(plist-get info
:ignore-list
))))
3355 ;; Secondary string: only objects can be found there.
3357 (when (eq --category
'objects
) (mapc --walk-tree --data
)))
3358 ;; Unconditionally enter parse trees.
3359 ((eq --type
'org-data
)
3360 (mapc --walk-tree
(org-element-contents --data
)))
3362 ;; Check if TYPE is matching among TYPES. If so,
3363 ;; apply FUN to --DATA and accumulate return value
3364 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3365 (when (memq --type types
)
3366 (let ((result (funcall fun --data
)))
3367 (cond ((not result
))
3368 (first-match (throw 'first-match result
))
3369 (t (push result --acc
)))))
3370 ;; If --DATA has a secondary string that can contain
3371 ;; objects with their type among TYPES, look into it.
3372 (when (eq --category
'objects
)
3374 (assq --type org-element-secondary-value-alist
)))
3376 (funcall --walk-tree
3377 (org-element-property (cdr sec-prop
) --data
)))))
3378 ;; Determine if a recursion into --DATA is possible.
3380 ;; --TYPE is explicitly removed from recursion.
3381 ((memq --type no-recursion
))
3382 ;; --DATA has no contents.
3383 ((not (org-element-contents --data
)))
3384 ;; Looking for greater elements but --DATA is simply
3385 ;; an element or an object.
3386 ((and (eq --category
'greater-elements
)
3387 (not (memq --type org-element-greater-elements
))))
3388 ;; Looking for elements but --DATA is an object.
3389 ((and (eq --category
'elements
)
3390 (memq --type org-element-all-objects
)))
3391 ;; In any other case, map contents.
3392 (t (mapc --walk-tree
(org-element-contents --data
)))))))))))
3394 (funcall --walk-tree data
)
3395 ;; Return value in a proper order.
3398 ;; The following functions are internal parts of the parser.
3400 ;; The first one, `org-element-parse-elements' acts at the element's
3403 ;; The second one, `org-element-parse-objects' applies on all objects
3404 ;; of a paragraph or a secondary string. It uses
3405 ;; `org-element-get-candidates' to optimize the search of the next
3406 ;; object in the buffer.
3408 ;; More precisely, that function looks for every allowed object type
3409 ;; first. Then, it discards failed searches, keeps further matches,
3410 ;; and searches again types matched behind point, for subsequent
3411 ;; calls. Thus, searching for a given type fails only once, and every
3412 ;; object is searched only once at top level (but sometimes more for
3415 (defun org-element-parse-elements
3416 (beg end special structure granularity visible-only acc
)
3417 "Parse elements between BEG and END positions.
3419 SPECIAL prioritize some elements over the others. It can be set
3420 to `quote-section', `section' `item' or `table-row'.
3422 When value is `item', STRUCTURE will be used as the current list
3425 GRANULARITY determines the depth of the recursion. See
3426 `org-element-parse-buffer' for more information.
3428 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3431 Elements are accumulated into ACC."
3434 (narrow-to-region beg end
)
3436 ;; When parsing only headlines, skip any text before first one.
3437 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3438 (org-with-limited-levels (outline-next-heading)))
3442 ;; Find current element's type and parse it accordingly to
3444 (let* ((element (org-element-current-element
3445 granularity special structure
))
3446 (type (org-element-type element
))
3447 (cbeg (org-element-property :contents-begin element
)))
3448 (goto-char (org-element-property :end element
))
3450 ;; Case 1. Simply accumulate element if VISIBLE-ONLY is
3451 ;; true and element is hidden or if it has no contents
3453 ((or (and visible-only
(org-element-property :hiddenp element
))
3454 (not cbeg
)) element
)
3455 ;; Case 2. Greater element: parse it between
3456 ;; `contents-begin' and `contents-end'. Make sure
3457 ;; GRANULARITY allows the recursion, or ELEMENT is an
3458 ;; headline, in which case going inside is mandatory, in
3459 ;; order to get sub-level headings.
3460 ((and (memq type org-element-greater-elements
)
3461 (or (memq granularity
'(element object nil
))
3462 (and (eq granularity
'greater-element
)
3464 (eq type
'headline
)))
3465 (org-element-parse-elements
3466 cbeg
(org-element-property :contents-end element
)
3467 ;; Possibly switch to a special mode.
3470 (if (org-element-property :quotedp element
) 'quote-section
3474 (org-element-property :structure element
)
3475 granularity visible-only
(nreverse element
)))
3476 ;; Case 3. ELEMENT has contents. Parse objects inside,
3477 ;; if GRANULARITY allows it.
3478 ((and cbeg
(memq granularity
'(object nil
)))
3479 (org-element-parse-objects
3480 cbeg
(org-element-property :contents-end element
)
3481 (nreverse element
) (org-element-restriction type
)))
3482 ;; Case 4. Else, just accumulate ELEMENT.
3488 (defun org-element-parse-objects (beg end acc restriction
)
3489 "Parse objects between BEG and END and return recursive structure.
3491 Objects are accumulated in ACC.
3493 RESTRICTION is a list of object types which are allowed in the
3495 (let ((get-next-object
3498 ;; Return the parsing function associated to the nearest
3499 ;; object among list of candidates CAND.
3500 (let ((pos (apply 'min
(mapcar 'cdr cand
))))
3505 (format "org-element-%s-parser" (car (rassq pos cand
))))))))))
3506 next-object candidates
)
3509 (while (setq candidates
(org-element-get-next-object-candidates
3510 end restriction candidates
))
3511 (setq next-object
(funcall get-next-object candidates
))
3512 ;; 1. Text before any object. Untabify it.
3513 (let ((obj-beg (org-element-property :begin next-object
)))
3514 (unless (= (point) obj-beg
)
3515 (push (replace-regexp-in-string
3516 "\t" (make-string tab-width ?
)
3517 (buffer-substring-no-properties (point) obj-beg
))
3520 (let ((obj-end (org-element-property :end next-object
))
3521 (cont-beg (org-element-property :contents-begin next-object
)))
3522 (push (if (and (memq (car next-object
) org-element-recursive-objects
)
3524 ;; ... recursive. The CONT-BEG check is for
3525 ;; links, as some of them might not be recursive
3526 ;; (i.e. plain links).
3530 (org-element-property :contents-end next-object
))
3531 (org-element-parse-objects
3532 (point-min) (point-max)
3533 (nreverse next-object
)
3534 ;; Restrict allowed objects.
3535 (org-element-restriction next-object
)))
3536 ;; ... not recursive. Accumulate the object.
3539 (goto-char obj-end
)))
3540 ;; 3. Text after last object. Untabify it.
3541 (unless (= (point) end
)
3542 (push (replace-regexp-in-string
3543 "\t" (make-string tab-width ?
)
3544 (buffer-substring-no-properties (point) end
))
3549 (defun org-element-get-next-object-candidates (limit restriction objects
)
3550 "Return an alist of candidates for the next object.
3552 LIMIT bounds the search, and RESTRICTION narrows candidates to
3555 Return value is an alist whose CAR is position and CDR the object
3558 OBJECTS is the previous candidates alist."
3559 (let (next-candidates types-to-search
)
3560 ;; If no previous result, search every object type in RESTRICTION.
3561 ;; Otherwise, keep potential candidates (old objects located after
3562 ;; point) and ask to search again those which had matched before.
3563 (if (not objects
) (setq types-to-search restriction
)
3565 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3566 (push obj next-candidates
)))
3568 ;; Call the appropriate successor function for each type to search
3569 ;; and accumulate matches.
3572 (let* ((successor-fun
3574 (format "org-element-%s-successor"
3575 (or (cdr (assq type org-element-object-successor-alist
))
3577 (obj (funcall successor-fun limit
)))
3578 (and obj
(push obj next-candidates
))))
3585 ;;; Towards A Bijective Process
3587 ;; The parse tree obtained with `org-element-parse-buffer' is really
3588 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3589 ;; interpreted and expanded into a string with canonical Org syntax.
3590 ;; Hence `org-element-interpret-data'.
3592 ;; The function relies internally on
3593 ;; `org-element-interpret--affiliated-keywords'.
3595 (defun org-element-interpret-data (data &optional parent
)
3596 "Interpret DATA as Org syntax.
3598 DATA is a parse tree, an element, an object or a secondary string
3601 Optional argument PARENT is used for recursive calls. It contains
3602 the element or object containing data, or nil.
3604 Return Org syntax as a string."
3605 (let* ((type (org-element-type data
))
3608 ;; Secondary string.
3611 (lambda (obj) (org-element-interpret-data obj parent
))
3613 ;; Full Org document.
3614 ((eq type
'org-data
)
3616 (lambda (obj) (org-element-interpret-data obj parent
))
3617 (org-element-contents data
) ""))
3619 ((stringp data
) data
)
3620 ;; Element/Object without contents.
3621 ((not (org-element-contents data
))
3622 (funcall (intern (format "org-element-%s-interpreter" type
))
3624 ;; Element/Object with contents.
3626 (let* ((greaterp (memq type org-element-greater-elements
))
3627 (objectp (and (not greaterp
)
3628 (memq type org-element-recursive-objects
)))
3631 (lambda (obj) (org-element-interpret-data obj data
))
3632 (org-element-contents
3633 (if (or greaterp objectp
) data
3634 ;; Elements directly containing objects must
3635 ;; have their indentation normalized first.
3636 (org-element-normalize-contents
3638 ;; When normalizing first paragraph of an
3639 ;; item or a footnote-definition, ignore
3640 ;; first line's indentation.
3641 (and (eq type
'paragraph
)
3642 (equal data
(car (org-element-contents parent
)))
3643 (memq (org-element-type parent
)
3644 '(footnote-definiton item
))))))
3646 (funcall (intern (format "org-element-%s-interpreter" type
))
3648 (if greaterp
(org-element-normalize-contents contents
)
3650 (if (memq type
'(org-data plain-text nil
)) results
3651 ;; Build white spaces. If no `:post-blank' property is
3652 ;; specified, assume its value is 0.
3653 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
3654 (if (memq type org-element-all-objects
)
3655 (concat results
(make-string post-blank
32))
3657 (org-element-interpret--affiliated-keywords data
)
3658 (org-element-normalize-string results
)
3659 (make-string post-blank
10)))))))
3661 (defun org-element-interpret--affiliated-keywords (element)
3662 "Return ELEMENT's affiliated keywords as Org syntax.
3663 If there is no affiliated keyword, return the empty string."
3664 (let ((keyword-to-org
3668 (when (member key org-element-dual-keywords
)
3669 (setq dual
(cdr value
) value
(car value
)))
3672 (format "[%s]" (org-element-interpret-data dual
)))
3674 (if (member key org-element-parsed-keywords
)
3675 (org-element-interpret-data value
)
3680 (let ((value (org-element-property (intern (concat ":" (downcase key
)))
3683 (if (member key org-element-multiple-keywords
)
3684 (mapconcat (lambda (line)
3685 (funcall keyword-to-org key line
))
3687 (funcall keyword-to-org key value
)))))
3688 ;; Remove translated keywords.
3692 (and (not (assoc key org-element-keyword-translation-alist
)) key
))
3693 org-element-affiliated-keywords
))
3696 ;; Because interpretation of the parse tree must return the same
3697 ;; number of blank lines between elements and the same number of white
3698 ;; space after objects, some special care must be given to white
3701 ;; The first function, `org-element-normalize-string', ensures any
3702 ;; string different from the empty string will end with a single
3703 ;; newline character.
3705 ;; The second function, `org-element-normalize-contents', removes
3706 ;; global indentation from the contents of the current element.
3708 (defun org-element-normalize-string (s)
3709 "Ensure string S ends with a single newline character.
3711 If S isn't a string return it unchanged. If S is the empty
3712 string, return it. Otherwise, return a new string with a single
3713 newline character at its end."
3715 ((not (stringp s
)) s
)
3717 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3718 (replace-match "\n" nil nil s
)))))
3720 (defun org-element-normalize-contents (element &optional ignore-first
)
3721 "Normalize plain text in ELEMENT's contents.
3723 ELEMENT must only contain plain text and objects.
3725 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3726 indentation to compute maximal common indentation.
3728 Return the normalized element that is element with global
3729 indentation removed from its contents. The function assumes that
3730 indentation is not done with TAB characters."
3731 (let* (ind-list ; for byte-compiler
3732 collect-inds
; for byte-compiler
3735 ;; Return list of indentations within BLOB. This is done by
3736 ;; walking recursively BLOB and updating IND-LIST along the
3737 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3738 ;; been seen yet. It is required as this string is the only
3739 ;; one whose indentation doesn't happen after a newline
3741 (lambda (blob first-flag
)
3744 (when (and first-flag
(stringp object
))
3745 (setq first-flag nil
)
3746 (string-match "\\`\\( *\\)" object
)
3747 (let ((len (length (match-string 1 object
))))
3748 ;; An indentation of zero means no string will be
3749 ;; modified. Quit the process.
3750 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
3751 (push len ind-list
))))
3755 ;; Avoid matching blank or empty lines.
3756 (while (and (string-match "\n\\( *\\)\\(.\\)" object start
)
3757 (not (equal (match-string 2 object
) " ")))
3758 (setq start
(match-end 0))
3759 (push (length (match-string 1 object
)) ind-list
))))
3760 ((memq (org-element-type object
) org-element-recursive-objects
)
3761 (funcall collect-inds object first-flag
))))
3762 (org-element-contents blob
))))))
3763 ;; Collect indentation list in ELEMENT. Possibly remove first
3764 ;; value if IGNORE-FIRST is non-nil.
3765 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
3766 (if (not ind-list
) element
3767 ;; Build ELEMENT back, replacing each string with the same
3768 ;; string minus common indentation.
3769 (let* (build ; for byte compiler
3772 (lambda (blob mci first-flag
)
3773 ;; Return BLOB with all its strings indentation
3774 ;; shortened from MCI white spaces. FIRST-FLAG is
3775 ;; non-nil when the first string hasn't been seen
3778 (list (org-element-type blob
) (nth 1 blob
))
3781 (when (and first-flag
(stringp object
))
3782 (setq first-flag nil
)
3784 (replace-regexp-in-string
3785 (format "\\` \\{%d\\}" mci
) "" object
)))
3788 (replace-regexp-in-string
3789 (format "\n \\{%d\\}" mci
) "\n" object
))
3790 ((memq (org-element-type object
)
3791 org-element-recursive-objects
)
3792 (funcall build object mci first-flag
))
3794 (org-element-contents blob
)))))))
3795 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
3801 ;; The first move is to implement a way to obtain the smallest element
3802 ;; containing point. This is the job of `org-element-at-point'. It
3803 ;; basically jumps back to the beginning of section containing point
3804 ;; and moves, element after element, with
3805 ;; `org-element-current-element' until the container is found.
3807 ;; Note: When using `org-element-at-point', secondary values are never
3808 ;; parsed since the function focuses on elements, not on objects.
3810 (defun org-element-at-point (&optional keep-trail
)
3811 "Determine closest element around point.
3813 Return value is a list like (TYPE PROPS) where TYPE is the type
3814 of the element and PROPS a plist of properties associated to the
3815 element. Possible types are defined in
3816 `org-element-all-elements'.
3818 As a special case, if point is at the very beginning of a list or
3819 sub-list, returned element will be that list instead of the first
3820 item. In the same way, if point is at the beginning of the first
3821 row of a table, returned element will be the table instead of the
3824 If optional argument KEEP-TRAIL is non-nil, the function returns
3825 a list of of elements leading to element at point. The list's
3826 CAR is always the element at point. Following positions contain
3827 element's siblings, then parents, siblings of parents, until the
3828 first element of current section."
3829 (org-with-wide-buffer
3830 ;; If at an headline, parse it. It is the sole element that
3831 ;; doesn't require to know about context. Be sure to disallow
3832 ;; secondary string parsing, though.
3833 (if (org-with-limited-levels (org-at-heading-p))
3836 (if (not keep-trail
) (org-element-headline-parser t
)
3837 (list (org-element-headline-parser t
))))
3838 ;; Otherwise move at the beginning of the section containing
3840 (let ((origin (point)) element type special-flag trail struct prevs
)
3841 (org-with-limited-levels
3842 (if (org-before-first-heading-p) (goto-char (point-min))
3843 (org-back-to-heading)
3845 (org-skip-whitespace)
3847 ;; Parse successively each element, skipping those ending
3848 ;; before original position.
3851 (setq element
(org-element-current-element
3852 'element special-flag struct
)
3854 (push element trail
)
3856 ;; 1. Skip any element ending before point or at point.
3857 ((let ((end (org-element-property :end element
)))
3858 (when (<= end origin
)
3859 (if (> (point-max) end
) (goto-char end
)
3860 (throw 'exit
(if keep-trail trail element
))))))
3861 ;; 2. An element containing point is always the element at
3863 ((not (memq type org-element-greater-elements
))
3864 (throw 'exit
(if keep-trail trail element
)))
3865 ;; 3. At any other greater element type, if point is
3866 ;; within contents, move into it. Otherwise, return
3869 (let ((beg (org-element-property :contents-begin element
))
3870 (end (org-element-property :contents-end element
)))
3871 (if (or (not beg
) (not end
) (> beg origin
) (<= end origin
)
3872 (and (= beg origin
) (memq type
'(plain-list table
))))
3873 (throw 'exit
(if keep-trail trail element
))
3876 (setq special-flag
'item
3877 struct
(org-element-property :structure element
)))
3878 (table (setq special-flag
'table-row
))
3879 (otherwise (setq special-flag nil
)))
3880 (narrow-to-region beg end
)
3881 (goto-char beg
)))))))))))
3884 ;; Once the local structure around point is well understood, it's easy
3885 ;; to implement some replacements for `forward-paragraph'
3886 ;; `backward-paragraph', namely `org-element-forward' and
3887 ;; `org-element-backward'.
3889 ;; Also, `org-transpose-elements' mimics the behaviour of
3890 ;; `transpose-words', at the element's level, whereas
3891 ;; `org-element-drag-forward', `org-element-drag-backward', and
3892 ;; `org-element-up' generalize, respectively, functions
3893 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3895 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3896 ;; smartly remove global indentation from buffer, making it possible
3897 ;; to use Org indent mode on a file created with hard indentation.
3899 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3900 ;; internally by some of the previously cited tools.
3902 (defsubst org-element-nested-p
(elem-A elem-B
)
3903 "Non-nil when elements ELEM-A and ELEM-B are nested."
3904 (let ((beg-A (org-element-property :begin elem-A
))
3905 (beg-B (org-element-property :begin elem-B
))
3906 (end-A (org-element-property :end elem-A
))
3907 (end-B (org-element-property :end elem-B
)))
3908 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
3909 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
3911 (defun org-element-swap-A-B (elem-A elem-B
)
3912 "Swap elements ELEM-A and ELEM-B.
3913 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
3915 (goto-char (org-element-property :begin elem-A
))
3916 ;; There are two special cases when an element doesn't start at bol:
3917 ;; the first paragraph in an item or in a footnote definition.
3918 (let ((specialp (not (bolp))))
3919 ;; Only a paragraph without any affiliated keyword can be moved at
3920 ;; ELEM-A position in such a situation. Note that the case of
3921 ;; a footnote definition is impossible: it cannot contain two
3922 ;; paragraphs in a row because it cannot contain a blank line.
3924 (or (not (eq (org-element-type elem-B
) 'paragraph
))
3925 (/= (org-element-property :begin elem-B
)
3926 (org-element-property :contents-begin elem-B
))))
3927 (error "Cannot swap elements"))
3928 ;; In a special situation, ELEM-A will have no indentation. We'll
3929 ;; give it ELEM-B's (which will in, in turn, have no indentation).
3930 (let* ((ind-B (when specialp
3931 (goto-char (org-element-property :begin elem-B
))
3932 (org-get-indentation)))
3933 (beg-A (org-element-property :begin elem-A
))
3934 (end-A (save-excursion
3935 (goto-char (org-element-property :end elem-A
))
3936 (skip-chars-backward " \r\t\n")
3938 (beg-B (org-element-property :begin elem-B
))
3939 (end-B (save-excursion
3940 (goto-char (org-element-property :end elem-B
))
3941 (skip-chars-backward " \r\t\n")
3943 ;; Store overlays responsible for visibility status. We
3944 ;; also need to store their boundaries as they will be
3945 ;; removed from buffer.
3948 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
3949 (overlays-in beg-A end-A
))
3950 (mapcar (lambda (ov) (list ov
(overlay-start ov
) (overlay-end ov
)))
3951 (overlays-in beg-B end-B
))))
3953 (body-A (buffer-substring beg-A end-A
))
3954 (body-B (delete-and-extract-region beg-B end-B
)))
3957 (setq body-B
(replace-regexp-in-string "\\`[ \t]*" "" body-B
))
3958 (org-indent-to-column ind-B
))
3960 ;; Restore ex ELEM-A overlays.
3964 (+ (nth 1 ov
) (- beg-B beg-A
))
3965 (+ (nth 2 ov
) (- beg-B beg-A
))))
3968 (delete-region beg-A end-A
)
3970 ;; Restore ex ELEM-B overlays.
3972 (move-overlay (car ov
)
3973 (+ (nth 1 ov
) (- beg-A beg-B
))
3974 (+ (nth 2 ov
) (- beg-A beg-B
))))
3976 (goto-char (org-element-property :end elem-B
)))))
3978 (defun org-element-forward ()
3979 "Move forward by one element.
3980 Move to the next element at the same level, when possible."
3982 (if (org-with-limited-levels (org-at-heading-p))
3983 (let ((origin (point)))
3984 (org-forward-same-level 1)
3985 (unless (org-with-limited-levels (org-at-heading-p))
3987 (error "Cannot move further down")))
3988 (let* ((trail (org-element-at-point 'keep-trail
))
3990 (end (org-element-property :end elem
))
3991 (parent (loop for prev in trail
3992 when
(>= (org-element-property :end prev
) end
)
3995 ((eobp) (error "Cannot move further down"))
3996 ((and parent
(= (org-element-property :contents-end parent
) end
))
3997 (goto-char (org-element-property :end parent
)))
3998 (t (goto-char end
))))))
4000 (defun org-element-backward ()
4001 "Move backward by one element.
4002 Move to the previous element at the same level, when possible."
4004 (if (org-with-limited-levels (org-at-heading-p))
4005 ;; At an headline, move to the previous one, if any, or stay
4007 (let ((origin (point)))
4008 (org-backward-same-level 1)
4009 (unless (org-with-limited-levels (org-at-heading-p))
4011 (error "Cannot move further up")))
4012 (let* ((trail (org-element-at-point 'keep-trail
))
4014 (prev-elem (nth 1 trail
))
4015 (beg (org-element-property :begin elem
)))
4017 ;; Move to beginning of current element if point isn't there
4019 ((/= (point) beg
) (goto-char beg
))
4020 ((not prev-elem
) (error "Cannot move further up"))
4021 (t (goto-char (org-element-property :begin prev-elem
)))))))
4023 (defun org-element-up ()
4024 "Move to upper element."
4026 (if (org-with-limited-levels (org-at-heading-p))
4027 (unless (org-up-heading-safe)
4028 (error "No surrounding element"))
4029 (let* ((trail (org-element-at-point 'keep-trail
))
4031 (end (org-element-property :end elem
))
4032 (parent (loop for prev in trail
4033 when
(>= (org-element-property :end prev
) end
)
4036 (parent (goto-char (org-element-property :begin parent
)))
4037 ((org-before-first-heading-p) (error "No surrounding element"))
4038 (t (org-back-to-heading))))))
4040 (defun org-element-down ()
4041 "Move to inner element."
4043 (let ((element (org-element-at-point)))
4045 ((memq (org-element-type element
) '(plain-list table
))
4046 (goto-char (org-element-property :contents-begin element
))
4048 ((memq (org-element-type element
) org-element-greater-elements
)
4049 ;; If contents are hidden, first disclose them.
4050 (when (org-element-property :hiddenp element
) (org-cycle))
4051 (goto-char (org-element-property :contents-begin element
)))
4052 (t (error "No inner element")))))
4054 (defun org-element-drag-backward ()
4055 "Move backward element at point."
4057 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
4058 (let* ((trail (org-element-at-point 'keep-trail
))
4060 (prev-elem (nth 1 trail
)))
4061 ;; Error out if no previous element or previous element is
4062 ;; a parent of the current one.
4063 (if (or (not prev-elem
) (org-element-nested-p elem prev-elem
))
4064 (error "Cannot drag element backward")
4065 (let ((pos (point)))
4066 (org-element-swap-A-B prev-elem elem
)
4067 (goto-char (+ (org-element-property :begin prev-elem
)
4068 (- pos
(org-element-property :begin elem
)))))))))
4070 (defun org-element-drag-forward ()
4071 "Move forward element at point."
4073 (let* ((pos (point))
4074 (elem (org-element-at-point)))
4075 (when (= (point-max) (org-element-property :end elem
))
4076 (error "Cannot drag element forward"))
4077 (goto-char (org-element-property :end elem
))
4078 (let ((next-elem (org-element-at-point)))
4079 (when (or (org-element-nested-p elem next-elem
)
4080 (and (eq (org-element-type next-elem
) 'headline
)
4081 (not (eq (org-element-type elem
) 'headline
))))
4083 (error "Cannot drag element forward"))
4084 ;; Compute new position of point: it's shifted by NEXT-ELEM
4085 ;; body's length (without final blanks) and by the length of
4086 ;; blanks between ELEM and NEXT-ELEM.
4087 (let ((size-next (- (save-excursion
4088 (goto-char (org-element-property :end next-elem
))
4089 (skip-chars-backward " \r\t\n")
4091 ;; Small correction if buffer doesn't end
4092 ;; with a newline character.
4093 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
4094 (org-element-property :begin next-elem
)))
4095 (size-blank (- (org-element-property :end elem
)
4097 (goto-char (org-element-property :end elem
))
4098 (skip-chars-backward " \r\t\n")
4101 (org-element-swap-A-B elem next-elem
)
4102 (goto-char (+ pos size-next size-blank
))))))
4104 (defun org-element-mark-element ()
4105 "Put point at beginning of this element, mark at end.
4107 Interactively, if this command is repeated or (in Transient Mark
4108 mode) if the mark is active, it marks the next element after the
4109 ones already marked."
4111 (let (deactivate-mark)
4112 (if (or (and (eq last-command this-command
) (mark t
))
4113 (and transient-mark-mode mark-active
))
4117 (goto-char (org-element-property :end
(org-element-at-point)))))
4118 (let ((element (org-element-at-point)))
4120 (push-mark (org-element-property :end element
) t t
)
4121 (goto-char (org-element-property :begin element
))))))
4123 (defun org-narrow-to-element ()
4124 "Narrow buffer to current element."
4126 (let ((elem (org-element-at-point)))
4128 ((eq (car elem
) 'headline
)
4130 (org-element-property :begin elem
)
4131 (org-element-property :end elem
)))
4132 ((memq (car elem
) org-element-greater-elements
)
4134 (org-element-property :contents-begin elem
)
4135 (org-element-property :contents-end elem
)))
4138 (org-element-property :begin elem
)
4139 (org-element-property :end elem
))))))
4141 (defun org-element-transpose ()
4142 "Transpose current and previous elements, keeping blank lines between.
4143 Point is moved after both elements."
4145 (org-skip-whitespace)
4146 (let ((end (org-element-property :end
(org-element-at-point))))
4147 (org-element-drag-backward)
4150 (defun org-element-unindent-buffer ()
4151 "Un-indent the visible part of the buffer.
4152 Relative indentation (between items, inside blocks, etc.) isn't
4155 (unless (eq major-mode
'org-mode
)
4156 (error "Cannot un-indent a buffer not in Org mode"))
4157 (let* ((parse-tree (org-element-parse-buffer 'greater-element
))
4158 unindent-tree
; For byte-compiler.
4164 (if (memq (org-element-type element
) '(headline section
))
4165 (funcall unindent-tree
(org-element-contents element
))
4169 (org-element-property :begin element
)
4170 (org-element-property :end element
))
4171 (org-do-remove-indentation)))))
4172 (reverse contents
))))))
4173 (funcall unindent-tree
(org-element-contents parse-tree
))))
4175 (defun org-element-fill-paragraph (&optional justify
)
4176 "Fill element at point, when applicable.
4178 This function only applies to paragraph, comment blocks, example
4179 blocks and fixed-width areas. Also, as a special case, re-align
4180 table when point is at one.
4182 If JUSTIFY is non-nil (interactively, with prefix argument),
4183 justify as well. If `sentence-end-double-space' is non-nil, then
4184 period followed by one space does not end a sentence, so don't
4185 break a line there. The variable `fill-column' controls the
4187 (let ((element (org-element-at-point)))
4188 (case (org-element-type element
)
4189 ;; Align Org tables, leave table.el tables as-is.
4190 (table-row (org-table-align) t
)
4192 (when (eq (org-element-property :type element
) 'org
) (org-table-align))
4194 ;; Elements that may contain `line-break' type objects.
4195 ((paragraph verse-block
)
4196 (let ((beg (org-element-property :contents-begin element
))
4197 (end (org-element-property :contents-end element
)))
4198 ;; Do nothing if point is at an affiliated keyword or at
4199 ;; verse block markers.
4200 (if (or (< (point) beg
) (>= (point) end
)) t
4201 ;; At a verse block, first narrow to current "paragraph"
4202 ;; and set current element to that paragraph.
4204 (when (eq (org-element-type element
) 'verse-block
)
4205 (narrow-to-region beg end
)
4208 (let ((bol-pos (point-at-bol)))
4209 (re-search-backward org-element-paragraph-separate nil
'move
)
4210 (unless (or (bobp) (= (point-at-bol) bol-pos
))
4212 (setq element
(org-element-paragraph-parser)
4213 beg
(org-element-property :contents-begin element
)
4214 end
(org-element-property :contents-end element
)))))
4215 ;; Fill paragraph, taking line breaks into consideration.
4216 ;; For that, slice the paragraph using line breaks as
4217 ;; separators, and fill the parts in reverse order to
4218 ;; avoid messing with markers.
4223 (fill-region-as-paragraph pos
(point) justify
)
4225 ;; Find the list of ending positions for line breaks
4226 ;; in the current paragraph. Add paragraph beginning
4227 ;; to include first slice.
4231 (org-element-parse-objects
4232 beg end nil org-element-all-objects
)
4234 (lambda (lb) (org-element-property :end lb
)))))))) t
)))
4235 ;; Elements whose contents should be filled as plain text.
4236 ((comment-block example-block
)
4240 (goto-char (org-element-property :begin element
))
4241 (while (looking-at org-element--affiliated-re
) (forward-line))
4245 (goto-char (org-element-property :end element
))
4246 (if (bolp) (forward-line -
1) (beginning-of-line))
4248 (fill-paragraph justify
) t
))
4249 ;; Ignore every other element.
4253 (provide 'org-element
)
4254 ;;; org-element.el ends here