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 ;; An object can be defined anywhere on a line. It may span over more
29 ;; than a line but never contains a blank one. Objects belong to the
30 ;; following types: `emphasis', `entity', `export-snippet',
31 ;; `footnote-reference', `inline-babel-call', `inline-src-block',
32 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
33 ;; `statistics-cookie', `subscript', `superscript', `target',
34 ;; `time-stamp' and `verbatim'.
36 ;; An element always starts and ends at the beginning of a line. The
37 ;; only element's type containing objects is called a `paragraph'.
38 ;; Other types are: `comment', `comment-block', `example-block',
39 ;; `export-block', `fixed-width', `horizontal-rule', `keyword',
40 ;; `latex-environment', `babel-call', `property-drawer',
41 ;; `quote-section', `src-block', `table' and `verse-block'.
43 ;; Elements containing paragraphs are called greater elements.
44 ;; Concerned types are: `center-block', `drawer', `dynamic-block',
45 ;; `footnote-definition', `headline', `inlinetask', `item',
46 ;; `plain-list', `quote-block', `section' and `special-block'.
48 ;; Greater elements (excepted `headline', `item' and `section' types)
49 ;; and elements (excepted `keyword', `babel-call', and
50 ;; `property-drawer' types) can have a fixed set of keywords as
51 ;; attributes. Those are called "affiliated keywords", to distinguish
52 ;; them from others keywords, which are full-fledged elements. In
53 ;; particular, the "name" affiliated keyword allows to label almost
54 ;; any element in an Org buffer.
56 ;; Notwithstanding affiliated keywords, each greater element, element
57 ;; and object has a fixed set of properties attached to it. Among
58 ;; them, three are shared by all types: `:begin' and `:end', which
59 ;; refer to the beginning and ending buffer positions of the
60 ;; considered element or object, and `:post-blank', which holds the
61 ;; number of blank lines, or white spaces, at its end.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (i.e. an item tag, an headline name, a table
65 ;; cell). Such values are called "secondary strings".
67 ;; Lisp-wise, an element or an object can be represented as a list.
68 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
69 ;; TYPE is a symbol describing the Org element or object.
70 ;; PROPERTIES is the property list attached to it. See docstring of
71 ;; appropriate parsing function to get an exhaustive
73 ;; CONTENTS is a list of elements, objects or raw strings contained
74 ;; in the current element or object, when applicable.
76 ;; An Org buffer is a nested list of such elements and objects, whose
77 ;; type is `org-data' and properties is nil.
79 ;; The first part of this file implements a parser and an interpreter
80 ;; for each type of Org syntax.
82 ;; The next two parts introduce three accessors and a function
83 ;; retrieving the smallest element starting at point (respectively
84 ;; `org-element-type', `org-element-property', `org-element-contents'
85 ;; and `org-element-current-element').
87 ;; The following part creates a fully recursive buffer parser. It
88 ;; also provides a tool to map a function to elements or objects
89 ;; matching some criteria in the parse tree. Functions of interest
90 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
91 ;; extent, `org-element-parse-secondary-string'.
93 ;; The penultimate part is the cradle of an interpreter for the
94 ;; obtained parse tree: `org-element-interpret-data' (and its
95 ;; relative, `org-element-interpret-secondary').
97 ;; The library ends by furnishing a set of interactive tools for
98 ;; element's navigation and manipulation, mostly based on
99 ;; `org-element-at-point' function.
104 (eval-when-compile (require 'cl
))
106 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
111 ;; For each greater element type, we define a parser and an
114 ;; A parser returns the element or object as the list described above.
115 ;; Most of them accepts no argument. Though, exceptions exist. Hence
116 ;; every element containing a secondary string (see
117 ;; `org-element-secondary-value-alist') will accept an optional
118 ;; argument to toggle parsing of that secondary string. Moreover,
119 ;; `item' parser requires current list's structure as its first
122 ;; An interpreter accepts two arguments: the list representation of
123 ;; the element or object, and its contents. The latter may be nil,
124 ;; depending on the element or object considered. It returns the
125 ;; appropriate Org syntax, as a string.
127 ;; Parsing functions must follow the naming convention:
128 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
129 ;; defined in `org-element-greater-elements'.
131 ;; Similarly, interpreting functions must follow the naming
132 ;; convention: org-element-TYPE-interpreter.
134 ;; With the exception of `headline' and `item' types, greater elements
135 ;; cannot contain other greater elements of their own type.
137 ;; Beside implementing a parser and an interpreter, adding a new
138 ;; greater element requires to tweak `org-element-current-element'.
139 ;; Moreover, the newly defined type must be added to both
140 ;; `org-element-all-elements' and `org-element-greater-elements'.
145 (defun org-element-center-block-parser ()
146 "Parse a center block.
148 Return a list whose car is `center-block' and cdr is a plist
149 containing `:begin', `:end', `:hiddenp', `:contents-begin',
150 `:contents-end' and `:post-blank' keywords.
152 Assume point is at beginning or end of the block."
154 (let* ((case-fold-search t
)
158 (concat "^[ \t]*#\\+BEGIN_CENTER") nil t
)
159 (org-element-collect-affiliated-keywords)))
160 (begin (car keywords
))
161 (contents-begin (progn (forward-line) (point)))
162 (hidden (org-truely-invisible-p))
163 (contents-end (progn (re-search-forward
164 (concat "^[ \t]*#\\+END_CENTER") nil t
)
166 (pos-before-blank (progn (forward-line) (point)))
167 (end (progn (org-skip-whitespace)
168 (if (eobp) (point) (point-at-bol)))))
173 :contents-begin
,contents-begin
174 :contents-end
,contents-end
175 :post-blank
,(count-lines pos-before-blank end
)
176 ,@(cadr keywords
))))))
178 (defun org-element-center-block-interpreter (center-block contents
)
179 "Interpret CENTER-BLOCK element as Org syntax.
180 CONTENTS is the contents of the element."
181 (format "#+begin_center\n%s#+end_center" contents
))
186 (defun org-element-drawer-parser ()
189 Return a list whose car is `drawer' and cdr is a plist containing
190 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
191 `:contents-end' and `:post-blank' keywords.
193 Assume point is at beginning of drawer."
195 (let* ((case-fold-search t
)
196 (name (progn (looking-at org-drawer-regexp
)
197 (org-match-string-no-properties 1)))
198 (keywords (org-element-collect-affiliated-keywords))
199 (begin (car keywords
))
200 (contents-begin (progn (forward-line) (point)))
201 (hidden (org-truely-invisible-p))
202 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
204 (pos-before-blank (progn (forward-line) (point)))
205 (end (progn (org-skip-whitespace)
206 (if (eobp) (point) (point-at-bol)))))
212 :contents-begin
,contents-begin
213 :contents-end
,contents-end
214 :post-blank
,(count-lines pos-before-blank end
)
215 ,@(cadr keywords
))))))
217 (defun org-element-drawer-interpreter (drawer contents
)
218 "Interpret DRAWER element as Org syntax.
219 CONTENTS is the contents of the element."
220 (format ":%s:\n%s:END:"
221 (org-element-property :drawer-name drawer
)
227 (defun org-element-dynamic-block-parser ()
228 "Parse a dynamic block.
230 Return a list whose car is `dynamic-block' and cdr is a plist
231 containing `:block-name', `:begin', `:end', `:hiddenp',
232 `:contents-begin', `:contents-end', `:arguments' and
233 `:post-blank' keywords.
235 Assume point is at beginning of dynamic block."
237 (let* ((case-fold-search t
)
238 (name (progn (looking-at org-dblock-start-re
)
239 (org-match-string-no-properties 1)))
240 (arguments (org-match-string-no-properties 3))
241 (keywords (org-element-collect-affiliated-keywords))
242 (begin (car keywords
))
243 (contents-begin (progn (forward-line) (point)))
244 (hidden (org-truely-invisible-p))
245 (contents-end (progn (re-search-forward org-dblock-end-re nil t
)
247 (pos-before-blank (progn (forward-line) (point)))
248 (end (progn (org-skip-whitespace)
249 (if (eobp) (point) (point-at-bol)))))
254 :arguments
,arguments
256 :contents-begin
,contents-begin
257 :contents-end
,contents-end
258 :post-blank
,(count-lines pos-before-blank end
)
259 ,@(cadr keywords
))))))
261 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
262 "Interpret DYNAMIC-BLOCK element as Org syntax.
263 CONTENTS is the contents of the element."
264 (format "#+BEGIN: %s%s\n%s#+END:"
265 (org-element-property :block-name dynamic-block
)
266 (let ((args (org-element-property :arguments dynamic-block
)))
267 (and arg
(concat " " args
)))
271 ;;;; Footnote Definition
273 (defun org-element-footnote-definition-parser ()
274 "Parse a footnote definition.
276 Return a list whose CAR is `footnote-definition' and CDR is
277 a plist containing `:label', `:begin' `:end', `:contents-begin',
278 `:contents-end' and `:post-blank' keywords.
280 Assume point is at the beginning of the footnote definition."
282 (looking-at org-footnote-definition-re
)
283 (let* ((label (org-match-string-no-properties 1))
284 (keywords (org-element-collect-affiliated-keywords))
285 (begin (car keywords
))
286 (contents-begin (progn (search-forward "]")
287 (org-skip-whitespace)
289 (contents-end (if (progn
292 (concat org-outline-regexp-bol
"\\|"
293 org-footnote-definition-re
"\\|"
297 (end (progn (org-skip-whitespace)
298 (if (eobp) (point) (point-at-bol)))))
299 `(footnote-definition
303 :contents-begin
,contents-begin
304 :contents-end
,contents-end
305 :post-blank
,(count-lines contents-end end
)
306 ,@(cadr keywords
))))))
308 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
309 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
310 CONTENTS is the contents of the footnote-definition."
311 (concat (format "[%s]" (org-element-property :label footnote-definition
))
318 (defun org-element-headline-parser (&optional raw-secondary-p
)
321 Return a list whose car is `headline' and cdr is a plist
322 containing `:raw-value', `:title', `:begin', `:end',
323 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
324 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
325 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
326 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
329 The plist also contains any property set in the property drawer,
330 with its name in lowercase, the underscores replaced with hyphens
331 and colons at the beginning (i.e. `:custom-id').
333 When RAW-SECONDARY-P is non-nil, headline's title will not be
334 parsed as a secondary string, but as a plain string instead.
336 Assume point is at beginning of the headline."
338 (let* ((components (org-heading-components))
339 (level (nth 1 components
))
340 (todo (nth 2 components
))
342 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
343 (tags (nth 5 components
))
344 (raw-value (nth 4 components
))
346 (let ((case-fold-search nil
))
347 (string-match (format "^%s +" org-quote-string
) raw-value
)))
349 (let ((case-fold-search nil
))
350 (string-match (format "^%s +" org-comment-string
) raw-value
)))
353 (let ((case-fold-search nil
))
354 (string-match (format ":%s:" org-archive-tag
) tags
))))
355 (footnote-section-p (and org-footnote-section
356 (string= org-footnote-section raw-value
)))
357 (standard-props (let (plist)
360 (let ((p-name (downcase (car p
))))
361 (while (string-match "_" p-name
)
363 (replace-match "-" nil nil p-name
)))
364 (setq p-name
(intern (concat ":" p-name
)))
366 (plist-put plist p-name
(cdr p
)))))
367 (org-entry-properties nil
'standard
))
369 (time-props (org-entry-properties nil
'special
"CLOCK"))
370 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
371 (deadline (cdr (assoc "DEADLINE" time-props
)))
372 (clock (cdr (assoc "CLOCK" time-props
)))
373 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
375 (pos-after-head (save-excursion (forward-line) (point)))
376 (contents-begin (save-excursion (forward-line)
377 (org-skip-whitespace)
378 (if (eobp) (point) (point-at-bol))))
379 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
380 (end (progn (goto-char (org-end-of-subtree t t
))))
381 (contents-end (progn (skip-chars-backward " \r\t\n")
385 ;; Clean RAW-VALUE from any quote or comment string.
386 (when (or quotedp commentedp
)
388 (replace-regexp-in-string
389 (concat "\\(" org-quote-string
"\\|" org-comment-string
"\\) +")
392 ;; Clean TAGS from archive tag, if any.
395 (and (not (string= tags
(format ":%s:" org-archive-tag
)))
396 (replace-regexp-in-string
397 (concat org-archive-tag
":") "" tags
)))
398 (when (string= tags
":") (setq tags nil
)))
401 (if raw-secondary-p raw-value
402 (org-element-parse-secondary-string
404 (cdr (assq 'headline org-element-string-restrictions
)))))
406 (:raw-value
,raw-value
410 :pre-blank
,(count-lines pos-after-head contents-begin
)
412 :contents-begin
,contents-begin
413 :contents-end
,contents-end
415 :priority
,(nth 3 components
)
418 :todo-type
,todo-type
419 :scheduled
,scheduled
421 :timestamp
,timestamp
423 :post-blank
,(count-lines contents-end end
)
424 :footnote-section-p
,footnote-section-p
425 :archivedp
,archivedp
426 :commentedp
,commentedp
428 ,@standard-props
)))))
430 (defun org-element-headline-interpreter (headline contents
)
431 "Interpret HEADLINE element as Org syntax.
432 CONTENTS is the contents of the element."
433 (let* ((level (org-element-property :level headline
))
434 (todo (org-element-property :todo-keyword headline
))
435 (priority (org-element-property :priority headline
))
436 (title (org-element-interpret-secondary
437 (org-element-property :title headline
)))
438 (tags (let ((tag-string (org-element-property :tags headline
))
439 (archivedp (org-element-property :archivedp headline
)))
441 ((and (not tag-string
) archivedp
)
442 (format ":%s:" org-archive-tag
))
443 (archivedp (concat ":" org-archive-tag tag-string
))
445 (commentedp (org-element-property :commentedp headline
))
446 (quotedp (org-element-property :quotedp headline
))
447 (pre-blank (org-element-property :pre-blank headline
))
448 (heading (concat (make-string level ?
*)
449 (and todo
(concat " " todo
))
450 (and quotedp
(concat " " org-quote-string
))
451 (and commentedp
(concat " " org-comment-string
))
452 (and priority
(concat " " priority
))
453 (cond ((and org-footnote-section
454 (org-element-property
455 :footnote-section-p headline
))
456 (concat " " org-footnote-section
))
457 (title (concat " " title
)))))
460 (let ((tags-len (length tags
)))
463 ((zerop org-tags-column
) (1+ tags-len
))
464 ((< org-tags-column
0)
465 (max (- (+ org-tags-column
(length heading
)))
467 (t (max (+ (- org-tags-column
(length heading
))
469 (1+ tags-len
)))))))))
470 (concat heading
(and tags
(format tags-fmt tags
))
471 (make-string (1+ pre-blank
) 10)
477 (defun org-element-inlinetask-parser (&optional raw-secondary-p
)
478 "Parse an inline task.
480 Return a list whose car is `inlinetask' and cdr is a plist
481 containing `:title', `:begin', `:end', `:hiddenp',
482 `:contents-begin' and `:contents-end', `:level', `:priority',
483 `:tags', `:todo-keyword', `:todo-type', `:scheduled',
484 `:deadline', `:timestamp', `:clock' and `:post-blank' keywords.
486 The plist also contains any property set in the property drawer,
487 with its name in lowercase, the underscores replaced with hyphens
488 and colons at the beginning (i.e. `:custom-id').
490 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
491 title will not be parsed as a secondary string, but as a plain
494 Assume point is at beginning of the inline task."
496 (let* ((keywords (org-element-collect-affiliated-keywords))
497 (begin (car keywords
))
498 (components (org-heading-components))
499 (todo (nth 2 components
))
501 (if (member todo org-done-keywords
) 'done
'todo
)))
502 (title (if raw-secondary-p
(nth 4 components
)
503 (org-element-parse-secondary-string
505 (cdr (assq 'inlinetask org-element-string-restrictions
)))))
506 (standard-props (let (plist)
509 (let ((p-name (downcase (car p
))))
510 (while (string-match "_" p-name
)
512 (replace-match "-" nil nil p-name
)))
513 (setq p-name
(intern (concat ":" p-name
)))
515 (plist-put plist p-name
(cdr p
)))))
516 (org-entry-properties nil
'standard
))
518 (time-props (org-entry-properties nil
'special
"CLOCK"))
519 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
520 (deadline (cdr (assoc "DEADLINE" time-props
)))
521 (clock (cdr (assoc "CLOCK" time-props
)))
522 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
523 (contents-begin (save-excursion (forward-line) (point)))
524 (hidden (org-truely-invisible-p))
525 (pos-before-blank (org-inlinetask-goto-end))
526 ;; In the case of a single line task, CONTENTS-BEGIN and
527 ;; CONTENTS-END might overlap.
528 (contents-end (max contents-begin
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
)
541 :tags
,(nth 5 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-secondary
559 (org-element-property :title inlinetask
)))
560 (tags (org-element-property :tags inlinetask
))
561 (task (concat (make-string level ?
*)
562 (and todo
(concat " " todo
))
563 (and priority
(concat " " priority
))
564 (and title
(concat " " title
))))
569 ((zerop org-tags-column
) 1)
570 ((< 0 org-tags-column
)
571 (max (+ org-tags-column
575 (t (max (- org-tags-column
(length inlinetask
))
577 (concat inlinetask
(and tags
(format tags-fmt tags
) "\n" contents
))))
582 (defun org-element-item-parser (struct &optional raw-secondary-p
)
585 STRUCT is the structure of the plain list.
587 Return a list whose car is `item' and cdr is a plist containing
588 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
589 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
590 `:post-blank' keywords.
592 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
593 any, will not be parsed as a secondary string, but as a plain
596 Assume point is at the beginning of the item."
599 (let* ((begin (point))
600 (bullet (org-list-get-bullet (point) struct
))
601 (checkbox (let ((box (org-list-get-checkbox begin struct
)))
602 (cond ((equal "[ ]" box
) 'off
)
603 ((equal "[X]" box
) 'on
)
604 ((equal "[-]" box
) 'trans
))))
605 (counter (let ((c (org-list-get-counter begin struct
)))
608 ((string-match "[A-Za-z]" c
)
609 (- (string-to-char (upcase (match-string 0 c
)))
611 ((string-match "[0-9]+" c
)
612 (string-to-number (match-string 0 c
))))))
614 (let ((raw-tag (org-list-get-tag begin struct
)))
616 (if raw-secondary-p raw-tag
617 (org-element-parse-secondary-string
619 (cdr (assq 'item org-element-string-restrictions
)))))))
620 (end (org-list-get-item-end begin struct
))
621 (contents-begin (progn (looking-at org-list-full-item-re
)
622 (goto-char (match-end 0))
623 (org-skip-whitespace)
624 ;; If first line isn't empty,
625 ;; contents really start at the text
626 ;; after item's meta-data.
627 (if (= (point-at-bol) begin
) (point)
629 (hidden (progn (forward-line)
630 (and (not (= (point) end
))
631 (org-truely-invisible-p))))
632 (contents-end (progn (goto-char end
)
633 (skip-chars-backward " \r\t\n")
640 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
641 ;; up in the case of an empty item separated
642 ;; from the next by a blank line. Thus, ensure
643 ;; the former is always the smallest of two.
644 :contents-begin
,(min contents-begin contents-end
)
645 :contents-end
,(max contents-begin contents-end
)
651 :post-blank
,(count-lines contents-end end
))))))
653 (defun org-element-item-interpreter (item contents
)
654 "Interpret ITEM element as Org syntax.
655 CONTENTS is the contents of the element."
657 (let* ((beg (org-element-property :begin item
))
658 (struct (org-element-property :structure item
))
659 (pre (org-list-prevs-alist struct
))
660 (bul (org-element-property :bullet item
)))
661 (org-list-bullet-string
662 (if (not (eq (org-list-get-list-type beg struct pre
) 'ordered
)) "-"
666 (org-list-get-item-number
667 beg struct pre
(org-list-parents-alist struct
))))))
670 (if (eq org-plain-list-ordered-item-terminator ?\
)) ")"
672 (checkbox (org-element-property :checkbox item
))
673 (counter (org-element-property :counter item
))
674 (tag (let ((tag (org-element-property :tag item
)))
675 (and tag
(org-element-interpret-secondary tag
))))
676 ;; Compute indentation.
677 (ind (make-string (length bullet
) 32)))
681 (and counter
(format "[@%d] " counter
))
683 ((eq checkbox
'on
) "[X] ")
684 ((eq checkbox
'off
) "[ ] ")
685 ((eq checkbox
'trans
) "[-] "))
686 (and tag
(format "%s :: " tag
))
688 (replace-regexp-in-string "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))))
693 (defun org-element-plain-list-parser (&optional structure
)
696 Optional argument STRUCTURE, when non-nil, is the structure of
697 the plain list being parsed.
699 Return a list whose car is `plain-list' and cdr is a plist
700 containing `:type', `:begin', `:end', `:contents-begin' and
701 `:contents-end', `:level', `:structure' and `:post-blank'
704 Assume point is at one of the list items."
706 (let* ((struct (or structure
(org-list-struct)))
707 (prevs (org-list-prevs-alist struct
))
708 (parents (org-list-parents-alist struct
))
709 (type (org-list-get-list-type (point) struct prevs
))
710 (contents-begin (goto-char
711 (org-list-get-list-begin (point) struct prevs
)))
712 (keywords (org-element-collect-affiliated-keywords))
713 (begin (car keywords
))
714 (contents-end (goto-char
715 (org-list-get-list-end (point) struct prevs
)))
716 (end (save-excursion (org-skip-whitespace)
717 (if (eobp) (point) (point-at-bol))))
720 (let ((item contents-begin
))
723 (org-list-get-list-begin item struct prevs
)
726 ;; Blank lines below list belong to the top-level list only.
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
740 :post-blank
,(count-lines contents-end end
)
741 ,@(cadr keywords
))))))
743 (defun org-element-plain-list-interpreter (plain-list contents
)
744 "Interpret PLAIN-LIST element as Org syntax.
745 CONTENTS is the contents of the element."
751 (defun org-element-quote-block-parser ()
752 "Parse a quote block.
754 Return a list whose car is `quote-block' and cdr is a plist
755 containing `:begin', `:end', `:hiddenp', `:contents-begin',
756 `:contents-end' and `:post-blank' keywords.
758 Assume point is at beginning or end of the block."
760 (let* ((case-fold-search t
)
764 (concat "^[ \t]*#\\+BEGIN_QUOTE") nil t
)
765 (org-element-collect-affiliated-keywords)))
766 (begin (car keywords
))
767 (contents-begin (progn (forward-line) (point)))
768 (hidden (org-truely-invisible-p))
769 (contents-end (progn (re-search-forward
770 (concat "^[ \t]*#\\+END_QUOTE") nil t
)
772 (pos-before-blank (progn (forward-line) (point)))
773 (end (progn (org-skip-whitespace)
774 (if (eobp) (point) (point-at-bol)))))
779 :contents-begin
,contents-begin
780 :contents-end
,contents-end
781 :post-blank
,(count-lines pos-before-blank end
)
782 ,@(cadr keywords
))))))
784 (defun org-element-quote-block-interpreter (quote-block contents
)
785 "Interpret QUOTE-BLOCK element as Org syntax.
786 CONTENTS is the contents of the element."
787 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents
))
792 (defun org-element-section-parser ()
795 Return a list whose car is `section' and cdr is a plist
796 containing `:begin', `:end', `:contents-begin', `contents-end'
797 and `:post-blank' keywords."
799 ;; Beginning of section is the beginning of the first non-blank
800 ;; line after previous headline.
801 (org-with-limited-levels
804 (outline-previous-heading)
805 (if (not (org-at-heading-p)) (point)
806 (forward-line) (org-skip-whitespace) (point-at-bol))))
807 (end (progn (outline-next-heading) (point)))
808 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
814 :contents-begin
,begin
815 :contents-end
,pos-before-blank
816 :post-blank
,(count-lines pos-before-blank end
)))))))
818 (defun org-element-section-interpreter (section contents
)
819 "Interpret SECTION element as Org syntax.
820 CONTENTS is the contents of the element."
826 (defun org-element-special-block-parser ()
827 "Parse a special block.
829 Return a list whose car is `special-block' and cdr is a plist
830 containing `:type', `:begin', `:end', `:hiddenp',
831 `:contents-begin', `:contents-end' and `:post-blank' keywords.
833 Assume point is at beginning or end of the block."
835 (let* ((case-fold-search t
)
836 (type (progn (looking-at
837 "[ \t]*#\\+\\(?:BEGIN\\|END\\)_\\([-A-Za-z0-9]+\\)")
838 (org-match-string-no-properties 1)))
842 (concat "^[ \t]*#\\+BEGIN_" type
) nil t
)
843 (org-element-collect-affiliated-keywords)))
844 (begin (car keywords
))
845 (contents-begin (progn (forward-line) (point)))
846 (hidden (org-truely-invisible-p))
847 (contents-end (progn (re-search-forward
848 (concat "^[ \t]*#\\+END_" type
) nil t
)
850 (pos-before-blank (progn (forward-line) (point)))
851 (end (progn (org-skip-whitespace)
852 (if (eobp) (point) (point-at-bol)))))
858 :contents-begin
,contents-begin
859 :contents-end
,contents-end
860 :post-blank
,(count-lines pos-before-blank end
)
861 ,@(cadr keywords
))))))
863 (defun org-element-special-block-interpreter (special-block contents
)
864 "Interpret SPECIAL-BLOCK element as Org syntax.
865 CONTENTS is the contents of the element."
866 (let ((block-type (org-element-property :type special-block
)))
867 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type
)))
873 ;; For each element, a parser and an interpreter are also defined.
874 ;; Both follow the same naming convention used for greater elements.
876 ;; Also, as for greater elements, adding a new element type is done
877 ;; through the following steps: implement a parser and an interpreter,
878 ;; tweak `org-element-current-element' so that it recognizes the new
879 ;; type and add that new type to `org-element-all-elements'.
881 ;; As a special case, when the newly defined type is a block type,
882 ;; `org-element-non-recursive-block-alist' has to be modified
888 (defun org-element-babel-call-parser ()
891 Return a list whose car is `babel-call' and cdr is a plist
892 containing `:begin', `:end', `:info' and `:post-blank' as
895 (let ((info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
896 (org-babel-lob-get-info)))
898 (pos-before-blank (progn (forward-line) (point)))
899 (end (progn (org-skip-whitespace)
900 (if (eobp) (point) (point-at-bol)))))
905 :post-blank
,(count-lines pos-before-blank end
))))))
907 (defun org-element-babel-call-interpreter (inline-babel-call contents
)
908 "Interpret INLINE-BABEL-CALL object as Org syntax.
910 (let* ((babel-info (org-element-property :info inline-babel-call
))
911 (main-source (car babel-info
))
912 (post-options (nth 1 babel-info
)))
914 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
915 ;; Remove redundant square brackets.
917 (match-string 1 main-source
) nil nil main-source
)
919 (and post-options
(format "[%s]" post-options
)))))
924 (defun org-element-comment-parser ()
927 Return a list whose car is `comment' and cdr is a plist
928 containing `:begin', `:end', `:value' and `:post-blank'
930 (let (beg-coms begin end end-coms keywords
)
933 ;; First type of comment: comments at column 0.
934 (let ((comment-re "^\\([^#]\\|#\\+[a-z]\\)"))
936 (re-search-backward comment-re nil
'move
)
937 (if (bobp) (setq keywords nil beg-coms
(point))
939 (setq keywords
(org-element-collect-affiliated-keywords)
941 (re-search-forward comment-re nil
'move
)
942 (setq end-coms
(if (eobp) (point) (match-beginning 0))))
943 ;; Second type of comment: indented comments.
944 (let ((comment-re "[ \t]*#\\+\\(?: \\|$\\)"))
946 (while (and (not (bobp)) (looking-at comment-re
))
948 (unless (looking-at comment-re
) (forward-line)))
949 (setq beg-coms
(point))
950 (setq keywords
(org-element-collect-affiliated-keywords))
951 ;; Get comments ending. This may not be accurate if
952 ;; commented lines within an item are followed by commented
953 ;; lines outside of the list. Though, parser will always
954 ;; get it right as it already knows surrounding element and
955 ;; has narrowed buffer to its contents.
956 (while (looking-at comment-re
) (forward-line))
957 (setq end-coms
(point))))
958 ;; Find position after blank.
960 (org-skip-whitespace)
961 (setq end
(if (eobp) (point) (point-at-bol))))
963 (:begin
,(or (car keywords
) beg-coms
)
965 :value
,(buffer-substring-no-properties beg-coms end-coms
)
966 :post-blank
,(count-lines end-coms end
)
967 ,@(cadr keywords
)))))
969 (defun org-element-comment-interpreter (comment contents
)
970 "Interpret COMMENT element as Org syntax.
972 (org-element-property :value comment
))
977 (defun org-element-comment-block-parser ()
978 "Parse an export block.
980 Return a list whose car is `comment-block' and cdr is a plist
981 containing `:begin', `:end', `:hiddenp', `:value' and
982 `:post-blank' keywords."
985 (let* ((case-fold-search t
)
987 (re-search-backward "^[ \t]*#\\+BEGIN_COMMENT" nil t
)
988 (org-element-collect-affiliated-keywords)))
989 (begin (car keywords
))
990 (contents-begin (progn (forward-line) (point)))
991 (hidden (org-truely-invisible-p))
992 (contents-end (progn (re-search-forward
993 "^[ \t]*#\\+END_COMMENT" nil t
)
995 (pos-before-blank (progn (forward-line) (point)))
996 (end (progn (org-skip-whitespace)
997 (if (eobp) (point) (point-at-bol))))
998 (value (buffer-substring-no-properties contents-begin contents-end
)))
1004 :post-blank
,(count-lines pos-before-blank end
)
1005 ,@(cadr keywords
))))))
1007 (defun org-element-comment-block-interpreter (comment-block contents
)
1008 "Interpret COMMENT-BLOCK element as Org syntax.
1010 (concat "#+begin_comment\n"
1011 (org-remove-indentation
1012 (org-element-property :value comment-block
))
1018 (defun org-element-example-block-parser ()
1019 "Parse an example block.
1021 Return a list whose car is `example-block' and cdr is a plist
1022 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1023 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1024 `:switches', `:value' and `:post-blank' keywords."
1027 (let* ((case-fold-search t
)
1030 "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?" nil t
)
1031 (org-match-string-no-properties 1)))
1032 ;; Switches analysis
1033 (number-lines (cond ((not switches
) nil
)
1034 ((string-match "-n\\>" switches
) 'new
)
1035 ((string-match "+n\\>" switches
) 'continued
)))
1036 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1037 ;; Should labels be retained in (or stripped from) example
1041 (not (string-match "-r\\>" switches
))
1042 (and number-lines
(string-match "-k\\>" switches
))))
1043 ;; What should code-references use - labels or
1047 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1048 (label-fmt (and switches
1049 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1050 (match-string 1 switches
)))
1051 ;; Standard block parsing.
1052 (keywords (org-element-collect-affiliated-keywords))
1053 (begin (car keywords
))
1054 (contents-begin (progn (forward-line) (point)))
1055 (hidden (org-truely-invisible-p))
1056 (contents-end (progn
1057 (re-search-forward "^[ \t]*#\\+END_EXAMPLE" nil t
)
1059 (value (buffer-substring-no-properties contents-begin contents-end
))
1060 (pos-before-blank (progn (forward-line) (point)))
1061 (end (progn (org-skip-whitespace)
1062 (if (eobp) (point) (point-at-bol)))))
1068 :number-lines
,number-lines
1069 :preserve-indent
,preserve-indent
1070 :retain-labels
,retain-labels
1071 :use-labels
,use-labels
1072 :label-fmt
,label-fmt
1074 :post-blank
,(count-lines pos-before-blank end
)
1075 ,@(cadr keywords
))))))
1077 (defun org-element-example-block-interpreter (example-block contents
)
1078 "Interpret EXAMPLE-BLOCK element as Org syntax.
1080 (let ((options (org-element-property :options example-block
)))
1081 (concat "#+BEGIN_EXAMPLE" (and options
(concat " " options
)) "\n"
1082 (org-remove-indentation
1083 (org-element-property :value example-block
))
1089 (defun org-element-export-block-parser ()
1090 "Parse an export block.
1092 Return a list whose car is `export-block' and cdr is a plist
1093 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1094 `:post-blank' keywords."
1097 (let* ((case-fold-search t
)
1099 (type (progn (re-search-backward
1100 (concat "[ \t]*#\\+BEGIN_"
1101 (org-re "\\([[:alnum:]]+\\)")))
1102 (upcase (org-match-string-no-properties 1))))
1103 (keywords (org-element-collect-affiliated-keywords))
1104 (begin (car keywords
))
1105 (contents-begin (progn (forward-line) (point)))
1106 (hidden (org-truely-invisible-p))
1107 (contents-end (progn (re-search-forward
1108 (concat "^[ \t]*#\\+END_" type
) nil t
)
1110 (pos-before-blank (progn (forward-line) (point)))
1111 (end (progn (org-skip-whitespace)
1112 (if (eobp) (point) (point-at-bol))))
1113 (value (buffer-substring-no-properties contents-begin contents-end
)))
1120 :post-blank
,(count-lines pos-before-blank end
)
1121 ,@(cadr keywords
))))))
1123 (defun org-element-export-block-interpreter (export-block contents
)
1124 "Interpret EXPORT-BLOCK element as Org syntax.
1126 (let ((type (org-element-property :type export-block
)))
1127 (concat (format "#+BEGIN_%s\n" type
)
1128 (org-element-property :value export-block
)
1129 (format "#+END_%s" type
))))
1134 (defun org-element-fixed-width-parser ()
1135 "Parse a fixed-width section.
1137 Return a list whose car is `fixed-width' and cdr is a plist
1138 containing `:begin', `:end', `:value' and `:post-blank'
1140 (let ((fixed-re "[ \t]*:\\( \\|$\\)")
1141 beg-area begin end value pos-before-blank keywords
)
1143 ;; Move to the beginning of the fixed-width area.
1145 (while (and (not (bobp)) (looking-at fixed-re
))
1147 (unless (looking-at fixed-re
) (forward-line 1)))
1148 (setq beg-area
(point))
1149 ;; Get affiliated keywords, if any.
1150 (setq keywords
(org-element-collect-affiliated-keywords))
1151 ;; Store true beginning of element.
1152 (setq begin
(car keywords
))
1153 ;; Get ending of fixed-width area. If point is in a list,
1154 ;; ensure to not get outside of it.
1155 (let* ((itemp (org-in-item-p))
1157 (org-list-get-bottom-point
1158 (save-excursion (goto-char itemp
) (org-list-struct)))
1160 (while (and (looking-at fixed-re
) (< (point) max-pos
))
1162 (setq pos-before-blank
(point))
1163 ;; Find position after blank
1164 (org-skip-whitespace)
1165 (setq end
(if (eobp) (point) (point-at-bol)))
1167 (setq value
(buffer-substring-no-properties beg-area pos-before-blank
)))
1172 :post-blank
,(count-lines pos-before-blank end
)
1173 ,@(cadr keywords
)))))
1175 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1176 "Interpret FIXED-WIDTH element as Org syntax.
1178 (org-remove-indentation (org-element-property :value fixed-width
)))
1181 ;;;; Horizontal Rule
1183 (defun org-element-horizontal-rule-parser ()
1184 "Parse an horizontal rule.
1186 Return a list whose car is `horizontal-rule' and cdr is
1187 a plist containing `:begin', `:end' and `:post-blank'
1190 (let* ((keywords (org-element-collect-affiliated-keywords))
1191 (begin (car keywords
))
1192 (post-hr (progn (forward-line) (point)))
1193 (end (progn (org-skip-whitespace)
1194 (if (eobp) (point) (point-at-bol)))))
1198 :post-blank
,(count-lines post-hr end
)
1199 ,@(cadr keywords
))))))
1201 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1202 "Interpret HORIZONTAL-RULE element as Org syntax.
1209 (defun org-element-keyword-parser ()
1210 "Parse a keyword at point.
1212 Return a list whose car is `keyword' and cdr is a plist
1213 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1216 (let* ((begin (point))
1217 (key (progn (looking-at
1218 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1219 (upcase (org-match-string-no-properties 1))))
1220 (value (org-trim (buffer-substring-no-properties
1221 (match-end 0) (point-at-eol))))
1222 (pos-before-blank (progn (forward-line) (point)))
1223 (end (progn (org-skip-whitespace)
1224 (if (eobp) (point) (point-at-bol)))))
1230 :post-blank
,(count-lines pos-before-blank end
))))))
1232 (defun org-element-keyword-interpreter (keyword contents
)
1233 "Interpret KEYWORD element as Org syntax.
1236 (org-element-property :key keyword
)
1237 (org-element-property :value keyword
)))
1240 ;;;; Latex Environment
1242 (defun org-element-latex-environment-parser ()
1243 "Parse a LaTeX environment.
1245 Return a list whose car is `latex-environment' and cdr is a plist
1246 containing `:begin', `:end', `:value' and `:post-blank' keywords."
1249 (let* ((case-fold-search t
)
1250 (contents-begin (re-search-backward "^[ \t]*\\\\begin" nil t
))
1251 (keywords (org-element-collect-affiliated-keywords))
1252 (begin (car keywords
))
1253 (contents-end (progn (re-search-forward "^[ \t]*\\\\end")
1256 (value (buffer-substring-no-properties contents-begin contents-end
))
1257 (end (progn (org-skip-whitespace)
1258 (if (eobp) (point) (point-at-bol)))))
1263 :post-blank
,(count-lines contents-end end
)
1264 ,@(cadr keywords
))))))
1266 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1267 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1269 (org-element-property :value latex-environment
))
1274 (defun org-element-paragraph-parser ()
1277 Return a list whose car is `paragraph' and cdr is a plist
1278 containing `:begin', `:end', `:contents-begin' and
1279 `:contents-end' and `:post-blank' keywords.
1281 Assume point is at the beginning of the paragraph."
1283 (let* ((contents-begin (point))
1284 (keywords (org-element-collect-affiliated-keywords))
1285 (begin (car keywords
))
1286 (contents-end (progn
1288 (if (re-search-forward
1289 org-element-paragraph-separate nil
'm
)
1290 (progn (forward-line -
1) (end-of-line) (point))
1292 (pos-before-blank (progn (forward-line) (point)))
1293 (end (progn (org-skip-whitespace)
1294 (if (eobp) (point) (point-at-bol)))))
1298 :contents-begin
,contents-begin
1299 :contents-end
,contents-end
1300 :post-blank
,(count-lines pos-before-blank end
)
1301 ,@(cadr keywords
))))))
1303 (defun org-element-paragraph-interpreter (paragraph contents
)
1304 "Interpret PARAGRAPH element as Org syntax.
1305 CONTENTS is the contents of the element."
1309 ;;;; Property Drawer
1311 (defun org-element-property-drawer-parser ()
1312 "Parse a property drawer.
1314 Return a list whose car is `property-drawer' and cdr is a plist
1315 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1316 `:contents-end', `:properties' and `:post-blank' keywords."
1318 (let ((case-fold-search t
)
1319 (begin (progn (end-of-line)
1320 (re-search-backward org-property-start-re
)
1321 (match-beginning 0)))
1322 (contents-begin (progn (forward-line) (point)))
1323 (hidden (org-truely-invisible-p))
1324 (properties (let (val)
1325 (while (not (looking-at "^[ \t]*:END:"))
1328 "[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):"))
1329 (push (cons (match-string 1)
1332 (match-end 0) (point-at-eol))))
1336 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
1338 (pos-before-blank (progn (forward-line) (point)))
1339 (end (progn (org-skip-whitespace)
1340 (if (eobp) (point) (point-at-bol)))))
1345 :properties
,properties
1346 :post-blank
,(count-lines pos-before-blank end
))))))
1348 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1349 "Interpret PROPERTY-DRAWER element as Org syntax.
1351 (let ((props (org-element-property :properties property-drawer
)))
1354 (mapconcat (lambda (p)
1355 (format org-property-format
(format ":%s:" (car p
)) (cdr p
)))
1356 (nreverse props
) "\n")
1362 (defun org-element-quote-section-parser ()
1363 "Parse a quote section.
1365 Return a list whose car is `quote-section' and cdr is a plist
1366 containing `:begin', `:end', `:value' and `:post-blank'
1369 Assume point is at beginning of the section."
1371 (let* ((begin (point))
1372 (end (progn (org-with-limited-levels (outline-next-heading))
1374 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1377 (value (buffer-substring-no-properties begin pos-before-blank
)))
1382 :post-blank
,(count-lines pos-before-blank end
))))))
1384 (defun org-element-quote-section-interpreter (quote-section contents
)
1385 "Interpret QUOTE-SECTION element as Org syntax.
1387 (org-element-property :value quote-section
))
1392 (defun org-element-src-block-parser ()
1395 Return a list whose car is `src-block' and cdr is a plist
1396 containing `:language', `:switches', `:parameters', `:begin',
1397 `:end', `:hiddenp', `:contents-begin', `:contents-end',
1398 `:number-lines', `:retain-labels', `:use-labels', `:label-fmt',
1399 `:preserve-indent', `:value' and `:post-blank' keywords."
1402 (let* ((case-fold-search t
)
1403 ;; Get position at beginning of block.
1407 "^[ \t]*#\\+BEGIN_SRC"
1408 "\\(?: +\\(\\S-+\\)\\)?" ; language
1409 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)*\\)" ; switches
1410 "\\(.*\\)[ \t]*$") ; parameters
1412 ;; Get language as a string.
1413 (language (org-match-string-no-properties 1))
1415 (parameters (org-trim (org-match-string-no-properties 3)))
1417 (switches (org-match-string-no-properties 2))
1418 ;; Switches analysis
1419 (number-lines (cond ((not switches
) nil
)
1420 ((string-match "-n\\>" switches
) 'new
)
1421 ((string-match "+n\\>" switches
) 'continued
)))
1422 (preserve-indent (and switches
(string-match "-i\\>" switches
)))
1423 (label-fmt (and switches
1424 (string-match "-l +\"\\([^\"\n]+\\)\"" switches
)
1425 (match-string 1 switches
)))
1426 ;; Should labels be retained in (or stripped from) src
1430 (not (string-match "-r\\>" switches
))
1431 (and number-lines
(string-match "-k\\>" switches
))))
1432 ;; What should code-references use - labels or
1436 (and retain-labels
(not (string-match "-k\\>" switches
)))))
1437 ;; Get affiliated keywords.
1438 (keywords (org-element-collect-affiliated-keywords))
1439 ;; Get beginning position.
1440 (begin (car keywords
))
1441 ;; Get position at end of block.
1442 (contents-end (progn (re-search-forward "^[ \t]*#\\+END_SRC" nil t
)
1446 (value (buffer-substring-no-properties
1447 (save-excursion (goto-char contents-begin
)
1450 (match-beginning 0)))
1451 ;; Get position after ending blank lines.
1452 (end (progn (org-skip-whitespace)
1453 (if (eobp) (point) (point-at-bol))))
1454 ;; Get visibility status.
1455 (hidden (progn (goto-char contents-begin
)
1457 (org-truely-invisible-p))))
1459 (:language
,language
1461 :parameters
,parameters
1464 :number-lines
,number-lines
1465 :preserve-indent
,preserve-indent
1466 :retain-labels
,retain-labels
1467 :use-labels
,use-labels
1468 :label-fmt
,label-fmt
1471 :post-blank
,(count-lines contents-end end
)
1472 ,@(cadr keywords
))))))
1474 (defun org-element-src-block-interpreter (src-block contents
)
1475 "Interpret SRC-BLOCK element as Org syntax.
1477 (let ((lang (org-element-property :language src-block
))
1478 (switches (org-element-property :switches src-block
))
1479 (params (org-element-property :parameters src-block
))
1480 (value (let ((val (org-element-property :value src-block
)))
1482 (org-src-preserve-indentation val
)
1483 ((zerop org-edit-src-content-indentation
)
1484 (org-remove-indentation val
))
1486 (let ((ind (make-string
1487 org-edit-src-content-indentation
32)))
1488 (replace-regexp-in-string
1489 "\\(^\\)[ \t]*\\S-" ind
1490 (org-remove-indentation val
) nil nil
1)))))))
1491 (concat (format "#+BEGIN_SRC%s\n"
1492 (concat (and lang
(concat " " lang
))
1493 (and switches
(concat " " switches
))
1494 (and params
(concat " " params
))))
1501 (defun org-element-table-parser ()
1502 "Parse a table at point.
1504 Return a list whose car is `table' and cdr is a plist containing
1505 `:begin', `:end', `:contents-begin', `:contents-end', `:tblfm',
1506 `:type', `:raw-table' and `:post-blank' keywords."
1508 (let* ((table-begin (goto-char (org-table-begin t
)))
1509 (type (if (org-at-table.el-p
) 'table.el
'org
))
1510 (keywords (org-element-collect-affiliated-keywords))
1511 (begin (car keywords
))
1512 (table-end (goto-char (marker-position (org-table-end t
))))
1513 (tblfm (when (looking-at "[ \t]*#\\+tblfm: +\\(.*\\)[ \t]*")
1514 (prog1 (org-match-string-no-properties 1)
1516 (pos-before-blank (point))
1517 (end (progn (org-skip-whitespace)
1518 (if (eobp) (point) (point-at-bol))))
1519 (raw-table (org-remove-indentation
1520 (buffer-substring-no-properties table-begin table-end
))))
1525 :raw-table
,raw-table
1527 :post-blank
,(count-lines pos-before-blank end
)
1528 ,@(cadr keywords
))))))
1530 (defun org-element-table-interpreter (table contents
)
1531 "Interpret TABLE element as Org syntax.
1533 (org-element-property :raw-table table
))
1538 (defun org-element-verse-block-parser (&optional raw-secondary-p
)
1539 "Parse a verse block.
1541 Return a list whose car is `verse-block' and cdr is a plist
1542 containing `:begin', `:end', `:hiddenp', `:value' and
1543 `:post-blank' keywords.
1545 When optional argument RAW-SECONDARY-P is non-nil, verse-block's
1546 value will not be parsed as a secondary string, but as a plain
1549 Assume point is at beginning or end of the block."
1551 (let* ((case-fold-search t
)
1555 (concat "^[ \t]*#\\+BEGIN_VERSE") nil t
)
1556 (org-element-collect-affiliated-keywords)))
1557 (begin (car keywords
))
1558 (hidden (progn (forward-line) (org-truely-invisible-p)))
1559 (value-begin (point))
1562 (re-search-forward (concat "^[ \t]*#\\+END_VERSE") nil t
)
1564 (pos-before-blank (progn (forward-line) (point)))
1565 (end (progn (org-skip-whitespace)
1566 (if (eobp) (point) (point-at-bol))))
1569 (buffer-substring-no-properties value-begin value-end
)
1570 (org-element-parse-secondary-string
1571 (buffer-substring-no-properties value-begin value-end
)
1572 (cdr (assq 'verse-block org-element-string-restrictions
))))))
1578 :post-blank
,(count-lines pos-before-blank end
)
1579 ,@(cadr keywords
))))))
1581 (defun org-element-verse-block-interpreter (verse-block contents
)
1582 "Interpret VERSE-BLOCK element as Org syntax.
1584 (format "#+BEGIN_VERSE\n%s#+END_VERSE"
1585 (org-remove-indentation
1586 (org-element-interpret-secondary
1587 (org-element-property :value verse-block
)))))
1593 ;; Unlike to elements, interstices can be found between objects.
1594 ;; That's why, along with the parser, successor functions are provided
1595 ;; for each object. Some objects share the same successor
1596 ;; (i.e. `emphasis' and `verbatim' objects).
1598 ;; A successor must accept a single argument bounding the search. It
1599 ;; will return either a cons cell whose car is the object's type, as
1600 ;; a symbol, and cdr the position of its next occurrence, or nil.
1602 ;; Successors follow the naming convention:
1603 ;; org-element-NAME-successor, where NAME is the name of the
1604 ;; successor, as defined in `org-element-all-successors'.
1606 ;; Some object types (i.e. `emphasis') are recursive. Restrictions on
1607 ;; object types they can contain will be specified in
1608 ;; `org-element-object-restrictions'.
1610 ;; Adding a new type of object is simple. Implement a successor,
1611 ;; a parser, and an interpreter for it, all following the naming
1612 ;; convention. Register type in `org-element-all-objects' and
1613 ;; successor in `org-element-all-successors'. Maybe tweak
1614 ;; restrictions about it, and that's it.
1618 (defun org-element-emphasis-parser ()
1619 "Parse text markup object at point.
1621 Return a list whose car is `emphasis' and cdr is a plist with
1622 `:marker', `:begin', `:end', `:contents-begin' and
1623 `:contents-end' and `:post-blank' keywords.
1625 Assume point is at the first emphasis marker."
1627 (unless (bolp) (backward-char 1))
1628 (looking-at org-emph-re
)
1629 (let ((begin (match-beginning 2))
1630 (marker (org-match-string-no-properties 3))
1631 (contents-begin (match-beginning 4))
1632 (contents-end (match-end 4))
1633 (post-blank (progn (goto-char (match-end 2))
1634 (skip-chars-forward " \t")))
1640 :contents-begin
,contents-begin
1641 :contents-end
,contents-end
1642 :post-blank
,post-blank
)))))
1644 (defun org-element-emphasis-interpreter (emphasis contents
)
1645 "Interpret EMPHASIS object as Org syntax.
1646 CONTENTS is the contents of the object."
1647 (let ((marker (org-element-property :marker emphasis
)))
1648 (concat marker contents marker
)))
1650 (defun org-element-text-markup-successor (limit)
1651 "Search for the next emphasis or verbatim object.
1653 LIMIT bounds the search.
1655 Return value is a cons cell whose car is `emphasis' or
1656 `verbatim' and cdr is beginning position."
1658 (unless (bolp) (backward-char))
1659 (when (re-search-forward org-emph-re limit t
)
1660 (cons (if (nth 4 (assoc (match-string 3) org-emphasis-alist
))
1663 (match-beginning 2)))))
1667 (defun org-element-entity-parser ()
1668 "Parse entity at point.
1670 Return a list whose car is `entity' and cdr a plist with
1671 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1672 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1675 Assume point is at the beginning of the entity."
1677 (looking-at "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1678 (let* ((value (org-entity-get (match-string 1)))
1679 (begin (match-beginning 0))
1680 (bracketsp (string= (match-string 2) "{}"))
1681 (post-blank (progn (goto-char (match-end 1))
1682 (when bracketsp
(forward-char 2))
1683 (skip-chars-forward " \t")))
1687 :latex
,(nth 1 value
)
1688 :latex-math-p
,(nth 2 value
)
1689 :html
,(nth 3 value
)
1690 :ascii
,(nth 4 value
)
1691 :latin1
,(nth 5 value
)
1692 :utf-8
,(nth 6 value
)
1695 :use-brackets-p
,bracketsp
1696 :post-blank
,post-blank
)))))
1698 (defun org-element-entity-interpreter (entity contents
)
1699 "Interpret ENTITY object as Org syntax.
1702 (org-element-property :name entity
)
1703 (when (org-element-property :use-brackets-p entity
) "{}")))
1705 (defun org-element-latex-or-entity-successor (limit)
1706 "Search for the next latex-fragment or entity object.
1708 LIMIT bounds the search.
1710 Return value is a cons cell whose car is `entity' or
1711 `latex-fragment' and cdr is beginning position."
1713 (let ((matchers (plist-get org-format-latex-options
:matchers
))
1714 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1716 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"))
1717 (when (re-search-forward
1718 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
1722 (goto-char (match-beginning 0))
1723 (if (looking-at entity-re
)
1724 ;; Determine if it's a real entity or a LaTeX command.
1725 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
1726 (match-beginning 0))
1727 ;; No entity nor command: point is at a LaTeX fragment.
1728 ;; Determine its type to get the correct beginning position.
1729 (cons 'latex-fragment
1732 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
1735 (nth 2 (assoc e org-latex-regexps
))))))
1742 (defun org-element-export-snippet-parser ()
1743 "Parse export snippet at point.
1745 Return a list whose car is `export-snippet' and cdr a plist with
1746 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1749 Assume point is at the beginning of the snippet."
1751 (looking-at "@\\([-A-Za-z0-9]+\\){")
1752 (let* ((begin (point))
1753 (back-end (org-match-string-no-properties 1))
1754 (before-blank (progn (goto-char (scan-sexps (1- (match-end 0)) 1))))
1755 (value (buffer-substring-no-properties
1756 (match-end 0) (1- before-blank
)))
1757 (post-blank (skip-chars-forward " \t"))
1760 (:back-end
,back-end
1764 :post-blank
,post-blank
)))))
1766 (defun org-element-export-snippet-interpreter (export-snippet contents
)
1767 "Interpret EXPORT-SNIPPET object as Org syntax.
1770 (org-element-property :back-end export-snippet
)
1771 (org-element-property :value export-snippet
)))
1773 (defun org-element-export-snippet-successor (limit)
1774 "Search for the next export-snippet object.
1776 LIMIT bounds the search.
1778 Return value is a cons cell whose car is `export-snippet' cdr is
1779 its beginning position."
1782 (while (re-search-forward "@[-A-Za-z0-9]+{" limit t
)
1783 (when (let ((end (ignore-errors (scan-sexps (1- (point)) 1))))
1784 (and end
(eq (char-before end
) ?
})))
1785 (throw 'exit
(cons 'export-snippet
(match-beginning 0))))))))
1788 ;;;; Footnote Reference
1790 (defun org-element-footnote-reference-parser ()
1791 "Parse footnote reference at point.
1793 Return a list whose CAR is `footnote-reference' and CDR a plist
1794 with `:label', `:type', `:inline-definition', `:begin', `:end'
1795 and `:post-blank' as keywords."
1797 (looking-at org-footnote-re
)
1798 (let* ((begin (point))
1799 (label (or (org-match-string-no-properties 2)
1800 (org-match-string-no-properties 3)
1801 (and (match-string 1)
1802 (concat "fn:" (org-match-string-no-properties 1)))))
1803 (type (if (or (not label
) (match-string 1)) 'inline
'standard
))
1804 (inner-begin (match-end 0))
1808 (while (and (> count
0) (re-search-forward "[][]" nil t
))
1809 (if (equal (match-string 0) "[") (incf count
) (decf count
)))
1811 (post-blank (progn (goto-char (1+ inner-end
))
1812 (skip-chars-forward " \t")))
1815 (and (eq type
'inline
)
1816 (org-element-parse-secondary-string
1817 (buffer-substring inner-begin inner-end
)
1818 (cdr (assq 'footnote-reference
1819 org-element-string-restrictions
))))))
1820 `(footnote-reference
1823 :inline-definition
,inline-definition
1826 :post-blank
,post-blank
)))))
1828 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
1829 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1831 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
1834 (org-element-property :inline-definition footnote-reference
)))
1835 (if (not inline-def
) ""
1836 (concat ":" (org-element-interpret-secondary inline-def
))))))
1837 (format "[%s]" (concat label def
))))
1839 (defun org-element-footnote-reference-successor (limit)
1840 "Search for the next footnote-reference object.
1842 LIMIT bounds the search.
1844 Return value is a cons cell whose CAR is `footnote-reference' and
1845 CDR is beginning position."
1848 (while (re-search-forward org-footnote-re limit t
)
1850 (let ((beg (match-beginning 0))
1853 (while (re-search-forward "[][]" limit t
)
1854 (if (equal (match-string 0) "[") (incf count
) (decf count
))
1856 (throw 'exit
(cons 'footnote-reference beg
))))))))))
1859 ;;;; Inline Babel Call
1861 (defun org-element-inline-babel-call-parser ()
1862 "Parse inline babel call at point.
1864 Return a list whose car is `inline-babel-call' and cdr a plist with
1865 `:begin', `:end', `:info' and `:post-blank' as keywords.
1867 Assume point is at the beginning of the babel call."
1869 (unless (bolp) (backward-char))
1870 (looking-at org-babel-inline-lob-one-liner-regexp
)
1871 (let ((info (save-match-data (org-babel-lob-get-info)))
1872 (begin (match-end 1))
1873 (post-blank (progn (goto-char (match-end 0))
1874 (skip-chars-forward " \t")))
1880 :post-blank
,post-blank
)))))
1882 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
1883 "Interpret INLINE-BABEL-CALL object as Org syntax.
1885 (let* ((babel-info (org-element-property :info inline-babel-call
))
1886 (main-source (car babel-info
))
1887 (post-options (nth 1 babel-info
)))
1889 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
1890 ;; Remove redundant square brackets.
1892 (match-string 1 main-source
) nil nil main-source
)
1894 (and post-options
(format "[%s]" post-options
)))))
1896 (defun org-element-inline-babel-call-successor (limit)
1897 "Search for the next inline-babel-call object.
1899 LIMIT bounds the search.
1901 Return value is a cons cell whose car is `inline-babel-call' and
1902 cdr is beginning position."
1904 ;; Use a simplified version of
1905 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
1906 (when (re-search-forward
1907 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
1909 (cons 'inline-babel-call
(match-beginning 0)))))
1912 ;;;; Inline Src Block
1914 (defun org-element-inline-src-block-parser ()
1915 "Parse inline source block at point.
1917 Return a list whose car is `inline-src-block' and cdr a plist
1918 with `:begin', `:end', `:language', `:value', `:parameters' and
1919 `:post-blank' as keywords.
1921 Assume point is at the beginning of the inline src block."
1923 (unless (bolp) (backward-char))
1924 (looking-at org-babel-inline-src-block-regexp
)
1925 (let ((begin (match-beginning 1))
1926 (language (org-match-string-no-properties 2))
1927 (parameters (org-match-string-no-properties 4))
1928 (value (org-match-string-no-properties 5))
1929 (post-blank (progn (goto-char (match-end 0))
1930 (skip-chars-forward " \t")))
1933 (:language
,language
1935 :parameters
,parameters
1938 :post-blank
,post-blank
)))))
1940 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
1941 "Interpret INLINE-SRC-BLOCK object as Org syntax.
1943 (let ((language (org-element-property :language inline-src-block
))
1944 (arguments (org-element-property :parameters inline-src-block
))
1945 (body (org-element-property :value inline-src-block
)))
1946 (format "src_%s%s{%s}"
1948 (if arguments
(format "[%s]" arguments
) "")
1951 (defun org-element-inline-src-block-successor (limit)
1952 "Search for the next inline-babel-call element.
1954 LIMIT bounds the search.
1956 Return value is a cons cell whose car is `inline-babel-call' and
1957 cdr is beginning position."
1959 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
1960 (cons 'inline-src-block
(match-beginning 1)))))
1965 (defun org-element-latex-fragment-parser ()
1966 "Parse latex fragment at point.
1968 Return a list whose car is `latex-fragment' and cdr a plist with
1969 `:value', `:begin', `:end', and `:post-blank' as keywords.
1971 Assume point is at the beginning of the latex fragment."
1973 (let* ((begin (point))
1977 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
1978 (when (or (looking-at latex-regexp
)
1982 (looking-at latex-regexp
))))
1983 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
1984 (plist-get org-format-latex-options
:matchers
))
1985 ;; None found: it's a macro.
1986 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
1988 (value (match-string-no-properties substring-match
))
1989 (post-blank (progn (goto-char (match-end substring-match
))
1990 (skip-chars-forward " \t")))
1996 :post-blank
,post-blank
)))))
1998 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
1999 "Interpret LATEX-FRAGMENT object as Org syntax.
2001 (org-element-property :value latex-fragment
))
2005 (defun org-element-line-break-parser ()
2006 "Parse line break at point.
2008 Return a list whose car is `line-break', and cdr a plist with
2009 `:begin', `:end' and `:post-blank' keywords.
2011 Assume point is at the beginning of the line break."
2012 (let ((begin (point))
2013 (end (save-excursion (forward-line) (point))))
2014 `(line-break (:begin
,begin
:end
,end
:post-blank
0))))
2016 (defun org-element-line-break-interpreter (line-break contents
)
2017 "Interpret LINE-BREAK object as Org syntax.
2021 (defun org-element-line-break-successor (limit)
2022 "Search for the next line-break object.
2024 LIMIT bounds the search.
2026 Return value is a cons cell whose car is `line-break' and cdr is
2027 beginning position."
2029 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
2030 (goto-char (match-beginning 1)))))
2031 ;; A line break can only happen on a non-empty line.
2032 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
2033 (cons 'line-break beg
)))))
2038 (defun org-element-link-parser ()
2039 "Parse link at point.
2041 Return a list whose car is `link' and cdr a plist with `:type',
2042 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2043 `:contents-end' and `:post-blank' as keywords.
2045 Assume point is at the beginning of the link."
2047 (let ((begin (point))
2048 end contents-begin contents-end link-end post-blank path type
2051 ;; Type 1: Text targeted from a radio target.
2052 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
2054 link-end
(match-end 0)
2055 path
(org-match-string-no-properties 0)))
2056 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2057 ((looking-at org-bracket-link-regexp
)
2058 (setq contents-begin
(match-beginning 3)
2059 contents-end
(match-end 3)
2060 link-end
(match-end 0)
2061 ;; RAW-LINK is the original link.
2062 raw-link
(org-match-string-no-properties 1)
2063 link
(org-link-expand-abbrev
2064 (replace-regexp-in-string
2065 " *\n *" " " (org-link-unescape raw-link
) t t
)))
2066 ;; Determine TYPE of link and set PATH accordingly.
2069 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
2070 (setq type
"file" path link
))
2071 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2072 ((string-match org-link-re-with-space3 link
)
2073 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
2074 ;; Id type: PATH is the id.
2075 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
2076 (setq type
"id" path
(match-string 1 link
)))
2077 ;; Code-ref type: PATH is the name of the reference.
2078 ((string-match "^(\\(.*\\))$" link
)
2079 (setq type
"coderef" path
(match-string 1 link
)))
2080 ;; Custom-id type: PATH is the name of the custom id.
2081 ((= (aref link
0) ?
#)
2082 (setq type
"custom-id" path
(substring link
1)))
2083 ;; Fuzzy type: Internal link either matches a target, an
2084 ;; headline name or nothing. PATH is the target or headline's
2086 (t (setq type
"fuzzy" path link
))))
2087 ;; Type 3: Plain link, i.e. http://orgmode.org
2088 ((looking-at org-plain-link-re
)
2089 (setq raw-link
(org-match-string-no-properties 0)
2090 type
(org-match-string-no-properties 1)
2091 path
(org-match-string-no-properties 2)
2092 link-end
(match-end 0)))
2093 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2094 ((looking-at org-angle-link-re
)
2095 (setq raw-link
(buffer-substring-no-properties
2096 (match-beginning 1) (match-end 2))
2097 type
(org-match-string-no-properties 1)
2098 path
(org-match-string-no-properties 2)
2099 link-end
(match-end 0))))
2100 ;; In any case, deduce end point after trailing white space from
2101 ;; LINK-END variable.
2102 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2107 :raw-link
,(or raw-link path
)
2110 :contents-begin
,contents-begin
2111 :contents-end
,contents-end
2112 :post-blank
,post-blank
)))))
2114 (defun org-element-link-interpreter (link contents
)
2115 "Interpret LINK object as Org syntax.
2116 CONTENTS is the contents of the object."
2117 (let ((type (org-element-property :type link
))
2118 (raw-link (org-element-property :raw-link link
)))
2119 (if (string= type
"radio") raw-link
2122 (if (string= contents
"") "" (format "[%s]" contents
))))))
2124 (defun org-element-link-successor (limit)
2125 "Search for the next link object.
2127 LIMIT bounds the search.
2129 Return value is a cons cell whose car is `link' and cdr is
2130 beginning position."
2133 (if (not org-target-link-regexp
) org-any-link-re
2134 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2135 (when (re-search-forward link-regexp limit t
)
2136 (cons 'link
(match-beginning 0))))))
2141 (defun org-element-macro-parser ()
2142 "Parse macro at point.
2144 Return a list whose car is `macro' and cdr a plist with `:key',
2145 `:args', `:begin', `:end', `:value' and `:post-blank' as
2148 Assume point is at the macro."
2150 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2151 (let ((begin (point))
2152 (key (downcase (org-match-string-no-properties 1)))
2153 (value (org-match-string-no-properties 0))
2154 (post-blank (progn (goto-char (match-end 0))
2155 (skip-chars-forward " \t")))
2157 (args (let ((args (org-match-string-no-properties 3)) args2
)
2159 (setq args
(org-split-string args
","))
2161 (while (string-match "\\\\\\'" (car args
))
2162 ;; Repair bad splits.
2163 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2166 (push (pop args
) args2
))
2167 (mapcar 'org-trim
(nreverse args2
))))))
2174 :post-blank
,post-blank
)))))
2176 (defun org-element-macro-interpreter (macro contents
)
2177 "Interpret MACRO object as Org syntax.
2179 (org-element-property :value macro
))
2181 (defun org-element-macro-successor (limit)
2182 "Search for the next macro object.
2184 LIMIT bounds the search.
2186 Return value is cons cell whose car is `macro' and cdr is
2187 beginning position."
2189 (when (re-search-forward
2190 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2192 (cons 'macro
(match-beginning 0)))))
2197 (defun org-element-radio-target-parser ()
2198 "Parse radio target at point.
2200 Return a list whose car is `radio-target' and cdr a plist with
2201 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2202 and `:post-blank' as keywords.
2204 Assume point is at the radio target."
2206 (looking-at org-radio-target-regexp
)
2207 (let ((begin (point))
2208 (contents-begin (match-beginning 1))
2209 (contents-end (match-end 1))
2210 (value (org-match-string-no-properties 1))
2211 (post-blank (progn (goto-char (match-end 0))
2212 (skip-chars-forward " \t")))
2217 :contents-begin
,contents-begin
2218 :contents-end
,contents-end
2219 :post-blank
,post-blank
2222 (defun org-element-radio-target-interpreter (target contents
)
2223 "Interpret TARGET object as Org syntax.
2224 CONTENTS is the contents of the object."
2225 (concat "<<<" contents
">>>"))
2227 (defun org-element-radio-target-successor (limit)
2228 "Search for the next radio-target object.
2230 LIMIT bounds the search.
2232 Return value is a cons cell whose car is `radio-target' and cdr
2233 is beginning position."
2235 (when (re-search-forward org-radio-target-regexp limit t
)
2236 (cons 'radio-target
(match-beginning 0)))))
2239 ;;;; Statistics Cookie
2241 (defun org-element-statistics-cookie-parser ()
2242 "Parse statistics cookie at point.
2244 Return a list whose car is `statistics-cookie', and cdr a plist
2245 with `:begin', `:end', `:value' and `:post-blank' keywords.
2247 Assume point is at the beginning of the statistics-cookie."
2249 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2250 (let* ((begin (point))
2251 (value (buffer-substring-no-properties
2252 (match-beginning 0) (match-end 0)))
2253 (post-blank (progn (goto-char (match-end 0))
2254 (skip-chars-forward " \t")))
2260 :post-blank
,post-blank
)))))
2262 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
2263 "Interpret STATISTICS-COOKIE object as Org syntax.
2265 (org-element-property :value statistics-cookie
))
2267 (defun org-element-statistics-cookie-successor (limit)
2268 "Search for the next statistics cookie object.
2270 LIMIT bounds the search.
2272 Return value is a cons cell whose car is `statistics-cookie' and
2273 cdr is beginning position."
2275 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
2276 (cons 'statistics-cookie
(match-beginning 0)))))
2281 (defun org-element-subscript-parser ()
2282 "Parse subscript at point.
2284 Return a list whose car is `subscript' and cdr a plist with
2285 `:begin', `:end', `:contents-begin', `:contents-end',
2286 `:use-brackets-p' and `:post-blank' as keywords.
2288 Assume point is at the underscore."
2290 (unless (bolp) (backward-char))
2291 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2293 (not (looking-at org-match-substring-regexp
))))
2294 (begin (match-beginning 2))
2295 (contents-begin (or (match-beginning 5)
2296 (match-beginning 3)))
2297 (contents-end (or (match-end 5) (match-end 3)))
2298 (post-blank (progn (goto-char (match-end 0))
2299 (skip-chars-forward " \t")))
2304 :use-brackets-p
,bracketsp
2305 :contents-begin
,contents-begin
2306 :contents-end
,contents-end
2307 :post-blank
,post-blank
)))))
2309 (defun org-element-subscript-interpreter (subscript contents
)
2310 "Interpret SUBSCRIPT object as Org syntax.
2311 CONTENTS is the contents of the object."
2313 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
2316 (defun org-element-sub/superscript-successor
(limit)
2317 "Search for the next sub/superscript object.
2319 LIMIT bounds the search.
2321 Return value is a cons cell whose car is either `subscript' or
2322 `superscript' and cdr is beginning position."
2324 (when (re-search-forward org-match-substring-regexp limit t
)
2325 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
2326 (match-beginning 2)))))
2331 (defun org-element-superscript-parser ()
2332 "Parse superscript at point.
2334 Return a list whose car is `superscript' and cdr a plist with
2335 `:begin', `:end', `:contents-begin', `:contents-end',
2336 `:use-brackets-p' and `:post-blank' as keywords.
2338 Assume point is at the caret."
2340 (unless (bolp) (backward-char))
2341 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2343 (not (looking-at org-match-substring-regexp
))))
2344 (begin (match-beginning 2))
2345 (contents-begin (or (match-beginning 5)
2346 (match-beginning 3)))
2347 (contents-end (or (match-end 5) (match-end 3)))
2348 (post-blank (progn (goto-char (match-end 0))
2349 (skip-chars-forward " \t")))
2354 :use-brackets-p
,bracketsp
2355 :contents-begin
,contents-begin
2356 :contents-end
,contents-end
2357 :post-blank
,post-blank
)))))
2359 (defun org-element-superscript-interpreter (superscript contents
)
2360 "Interpret SUPERSCRIPT object as Org syntax.
2361 CONTENTS is the contents of the object."
2363 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
2369 (defun org-element-target-parser ()
2370 "Parse target at point.
2372 Return a list whose car is `target' and cdr a plist with
2373 `:begin', `:end', `:contents-begin', `:contents-end', `value' and
2374 `:post-blank' as keywords.
2376 Assume point is at the target."
2378 (looking-at org-target-regexp
)
2379 (let ((begin (point))
2380 (value (org-match-string-no-properties 1))
2381 (post-blank (progn (goto-char (match-end 0))
2382 (skip-chars-forward " \t")))
2388 :post-blank
,post-blank
)))))
2390 (defun org-element-target-interpreter (target contents
)
2391 "Interpret TARGET object as Org syntax.
2392 CONTENTS is the contents of target."
2395 (defun org-element-target-successor (limit)
2396 "Search for the next target object.
2398 LIMIT bounds the search.
2400 Return value is a cons cell whose car is `target' and cdr is
2401 beginning position."
2403 (when (re-search-forward org-target-regexp limit t
)
2404 (cons 'target
(match-beginning 0)))))
2409 (defun org-element-time-stamp-parser ()
2410 "Parse time stamp at point.
2412 Return a list whose car is `time-stamp', and cdr a plist with
2413 `:appt-type', `:type', `:begin', `:end', `:value' and
2414 `:post-blank' keywords.
2416 Assume point is at the beginning of the time-stamp."
2418 (let* ((appt-type (cond
2419 ((looking-at (concat org-deadline-string
" +"))
2420 (goto-char (match-end 0))
2422 ((looking-at (concat org-scheduled-string
" +"))
2423 (goto-char (match-end 0))
2425 ((looking-at (concat org-closed-string
" +"))
2426 (goto-char (match-end 0))
2428 (begin (and appt-type
(match-beginning 0)))
2430 ((looking-at org-tsr-regexp
)
2431 (if (match-string 2) 'active-range
'active
))
2432 ((looking-at org-tsr-regexp-both
)
2433 (if (match-string 2) 'inactive-range
'inactive
))
2434 ((looking-at (concat
2435 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2437 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2439 (begin (or begin
(match-beginning 0)))
2440 (value (buffer-substring-no-properties
2441 (match-beginning 0) (match-end 0)))
2442 (post-blank (progn (goto-char (match-end 0))
2443 (skip-chars-forward " \t")))
2446 (:appt-type
,appt-type
2451 :post-blank
,post-blank
)))))
2453 (defun org-element-time-stamp-interpreter (time-stamp contents
)
2454 "Interpret TIME-STAMP object as Org syntax.
2457 (case (org-element-property :appt-type time-stamp
)
2458 (closed (concat org-closed-string
" "))
2459 (deadline (concat org-deadline-string
" "))
2460 (scheduled (concat org-scheduled-string
" ")))
2461 (org-element-property :value time-stamp
)))
2463 (defun org-element-time-stamp-successor (limit)
2464 "Search for the next time-stamp object.
2466 LIMIT bounds the search.
2468 Return value is a cons cell whose car is `time-stamp' and cdr is
2469 beginning position."
2471 (when (re-search-forward
2472 (concat "\\(?:" org-scheduled-string
" +\\|"
2473 org-deadline-string
" +\\|" org-closed-string
" +\\)?"
2476 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2478 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2480 (cons 'time-stamp
(match-beginning 0)))))
2485 (defun org-element-verbatim-parser ()
2486 "Parse verbatim object at point.
2488 Return a list whose car is `verbatim' and cdr is a plist with
2489 `:marker', `:begin', `:end' and `:post-blank' keywords.
2491 Assume point is at the first verbatim marker."
2493 (unless (bolp) (backward-char 1))
2494 (looking-at org-emph-re
)
2495 (let ((begin (match-beginning 2))
2496 (marker (org-match-string-no-properties 3))
2497 (value (org-match-string-no-properties 4))
2498 (post-blank (progn (goto-char (match-end 2))
2499 (skip-chars-forward " \t")))
2506 :post-blank
,post-blank
)))))
2508 (defun org-element-verbatim-interpreter (verbatim contents
)
2509 "Interpret VERBATIM object as Org syntax.
2511 (let ((marker (org-element-property :marker verbatim
))
2512 (value (org-element-property :value verbatim
)))
2513 (concat marker value marker
)))
2517 ;;; Definitions And Rules
2519 ;; Define elements, greater elements and specify recursive objects,
2520 ;; along with the affiliated keywords recognized. Also set up
2521 ;; restrictions on recursive objects combinations.
2523 ;; These variables really act as a control center for the parsing
2525 (defconst org-element-paragraph-separate
2526 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2527 ;; Headlines and inlinetasks.
2528 org-outline-regexp-bol
"\\|"
2529 ;; Comments, blocks (any type), keywords and babel calls.
2530 "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
2532 (org-item-beginning-re) "\\|"
2533 ;; Fixed-width, drawers (any type) and tables.
2535 ;; Footnote definitions.
2536 org-footnote-definition-re
"\\|"
2537 ;; Horizontal rules.
2538 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2539 ;; LaTeX environments.
2540 "^[ \t]*\\\\\\(begin\\|end\\)")
2541 "Regexp to separate paragraphs in an Org buffer.")
2543 (defconst org-element-all-elements
2544 '(center-block comment comment-block drawer dynamic-block example-block
2545 export-block fixed-width footnote-definition headline
2546 horizontal-rule inlinetask item keyword latex-environment
2547 babel-call paragraph plain-list property-drawer quote-block
2548 quote-section section special-block src-block table
2550 "Complete list of element types.")
2552 (defconst org-element-greater-elements
2553 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2554 item plain-list quote-block section special-block
)
2555 "List of recursive element types aka Greater Elements.")
2557 (defconst org-element-all-successors
2558 '(export-snippet footnote-reference inline-babel-call inline-src-block
2559 latex-or-entity line-break link macro radio-target
2560 statistics-cookie sub
/superscript target text-markup
2562 "Complete list of successors.")
2564 (defconst org-element-object-successor-alist
2565 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
2566 (emphasis . text-markup
) (verbatim . text-markup
)
2567 (entity . latex-or-entity
) (latex-fragment . latex-or-entity
))
2568 "Alist of translations between object type and successor name.
2570 Sharing the same successor comes handy when, for example, the
2571 regexp matching one object can also match the other object.")
2573 (defconst org-element-all-objects
2574 '(emphasis entity export-snippet footnote-reference inline-babel-call
2575 inline-src-block line-break latex-fragment link macro radio-target
2576 statistics-cookie subscript superscript target time-stamp
2578 "Complete list of object types.")
2580 (defconst org-element-recursive-objects
2581 '(emphasis link macro subscript superscript radio-target
)
2582 "List of recursive object types.")
2584 (defconst org-element-non-recursive-block-alist
2585 '(("ASCII" . export-block
)
2586 ("COMMENT" . comment-block
)
2587 ("DOCBOOK" . export-block
)
2588 ("EXAMPLE" . example-block
)
2589 ("HTML" . export-block
)
2590 ("LATEX" . export-block
)
2591 ("ODT" . export-block
)
2593 ("VERSE" . verse-block
))
2594 "Alist between non-recursive block name and their element type.")
2596 (defconst org-element-affiliated-keywords
2597 '("ATTR_ASCII" "ATTR_DOCBOOK" "ATTR_HTML" "ATTR_LATEX" "ATTR_ODT" "CAPTION"
2598 "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT" "RESULTS"
2599 "SOURCE" "SRCNAME" "TBLNAME")
2600 "List of affiliated keywords as strings.")
2602 (defconst org-element-keyword-translation-alist
2603 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
2604 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
2605 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
2606 "Alist of usual translations for keywords.
2607 The key is the old name and the value the new one. The property
2608 holding their value will be named after the translated name.")
2610 (defconst org-element-multiple-keywords
2611 '("ATTR_ASCII" "ATTR_DOCBOOK" "ATTR_HTML" "ATTR_LATEX" "ATTR_ODT" "HEADER")
2612 "List of affiliated keywords that can occur more that once in an element.
2614 Their value will be consed into a list of strings, which will be
2615 returned as the value of the property.
2617 This list is checked after translations have been applied. See
2618 `org-element-keyword-translation-alist'.")
2620 (defconst org-element-parsed-keywords
'("AUTHOR" "CAPTION" "TITLE")
2621 "List of keywords whose value can be parsed.
2623 Their value will be stored as a secondary string: a list of
2624 strings and objects.
2626 This list is checked after translations have been applied. See
2627 `org-element-keyword-translation-alist'.")
2629 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
2630 "List of keywords which can have a secondary value.
2632 In Org syntax, they can be written with optional square brackets
2633 before the colons. For example, results keyword can be
2634 associated to a hash value with the following:
2636 #+RESULTS[hash-string]: some-source
2638 This list is checked after translations have been applied. See
2639 `org-element-keyword-translation-alist'.")
2641 (defconst org-element-object-restrictions
2642 '((emphasis entity export-snippet inline-babel-call inline-src-block link
2643 radio-target sub
/superscript target text-markup time-stamp
)
2644 (link entity export-snippet inline-babel-call inline-src-block
2645 latex-fragment link sub
/superscript text-markup
)
2647 (radio-target entity export-snippet latex-fragment sub
/superscript
)
2648 (subscript entity export-snippet inline-babel-call inline-src-block
2649 latex-fragment sub
/superscript text-markup
)
2650 (superscript entity export-snippet inline-babel-call inline-src-block
2651 latex-fragment sub
/superscript text-markup
))
2652 "Alist of recursive objects restrictions.
2654 CAR is a recursive object type and CDR is a list of successors
2655 that will be called within an object of such type.
2657 For example, in a `radio-target' object, one can only find
2658 entities, export snippets, latex-fragments, subscript and
2661 (defconst org-element-string-restrictions
2662 '((footnote-reference entity export-snippet footnote-reference
2663 inline-babel-call inline-src-block latex-fragment
2664 line-break link macro radio-target sub
/superscript
2665 target text-markup time-stamp
)
2666 (headline entity inline-babel-call inline-src-block latex-fragment link
2667 macro radio-target statistics-cookie sub
/superscript text-markup
2669 (inlinetask entity inline-babel-call inline-src-block latex-fragment link
2670 macro radio-target sub
/superscript text-markup time-stamp
)
2671 (item entity inline-babel-call latex-fragment macro radio-target
2672 sub
/superscript target text-markup
)
2673 (keyword entity latex-fragment macro sub
/superscript text-markup
)
2674 (table entity latex-fragment macro target text-markup
)
2675 (verse-block entity footnote-reference inline-babel-call inline-src-block
2676 latex-fragment line-break link macro radio-target
2677 sub
/superscript target text-markup time-stamp
))
2678 "Alist of secondary strings restrictions.
2680 When parsed, some elements have a secondary string which could
2681 contain various objects (i.e. headline's name, or table's cells).
2682 For association, CAR is the element type, and CDR a list of
2683 successors that will be called in that secondary string.
2685 Note: `keyword' secondary string type only applies to keywords
2686 matching `org-element-parsed-keywords'.")
2688 (defconst org-element-secondary-value-alist
2689 '((headline .
:title
)
2690 (inlinetask .
:title
)
2692 (footnote-reference .
:inline-definition
)
2693 (verse-block .
:value
))
2694 "Alist between element types and location of secondary value.")
2700 ;; Provide three accessors: `org-element-type', `org-element-property'
2701 ;; and `org-element-contents'.
2703 (defun org-element-type (element)
2704 "Return type of element ELEMENT.
2706 The function returns the type of the element or object provided.
2707 It can also return the following special value:
2708 `plain-text' for a string
2709 `org-data' for a complete document
2710 nil in any other case."
2712 ((not (consp element
)) (and (stringp element
) 'plain-text
))
2713 ((symbolp (car element
)) (car element
))))
2715 (defun org-element-property (property element
)
2716 "Extract the value from the PROPERTY of an ELEMENT."
2717 (plist-get (nth 1 element
) property
))
2719 (defun org-element-contents (element)
2720 "Extract contents from an ELEMENT."
2725 ;;; Parsing Element Starting At Point
2727 ;; `org-element-current-element' is the core function of this section.
2728 ;; It returns the Lisp representation of the element starting at
2729 ;; point. It uses `org-element--element-block-re' for quick access to
2732 (defconst org-element--element-block-re
2733 (format "[ \t]*#\\+BEGIN_\\(%s\\)\\(?: \\|$\\)"
2736 (mapcar 'car org-element-non-recursive-block-alist
) "\\|"))
2737 "Regexp matching the beginning of a non-recursive block type.
2738 Used internally by `org-element-current-element'. Do not modify
2739 it directly, set `org-element-recursive-block-alist' instead.")
2741 (defun org-element-current-element (&optional granularity special structure
)
2742 "Parse the element starting at point.
2744 Return value is a list like (TYPE PROPS) where TYPE is the type
2745 of the element and PROPS a plist of properties associated to the
2748 Possible types are defined in `org-element-all-elements'.
2750 Optional argument GRANULARITY determines the depth of the
2751 recursion. Allowed values are `headline', `greater-element',
2752 `element', `object' or nil. When it is bigger than `object' (or
2753 nil), secondary values will not be parsed, since they only
2756 Optional argument SPECIAL, when non-nil, can be either `item',
2757 `section' or `quote-section'. `item' allows to parse item wise
2758 instead of plain-list wise, using STRUCTURE as the current list
2759 structure. `section' (resp. `quote-section') will try to parse
2760 a section (resp. a quote section) before anything else.
2762 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
2765 Unlike to `org-element-at-point', this function assumes point is
2766 always at the beginning of the element it has to parse. As such,
2767 it is quicker than its counterpart, albeit more restrictive."
2770 ;; If point is at an affiliated keyword, try moving to the
2771 ;; beginning of the associated element. If none is found, the
2772 ;; keyword is orphaned and will be treated as plain text.
2773 (when (looking-at org-element--affiliated-re
)
2774 (let ((opoint (point)))
2775 (while (looking-at org-element--affiliated-re
) (forward-line))
2776 (when (looking-at "[ \t]*$") (goto-char opoint
))))
2777 (let ((case-fold-search t
)
2778 ;; Determine if parsing depth allows for secondary strings
2779 ;; parsing. It only applies to elements referenced in
2780 ;; `org-element-secondary-value-alist'.
2781 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
2784 ((org-with-limited-levels (org-at-heading-p))
2785 (org-element-headline-parser raw-secondary-p
))
2787 ((eq special
'quote-section
) (org-element-quote-section-parser))
2789 ((eq special
'section
) (org-element-section-parser))
2790 ;; Non-recursive block.
2791 ((when (looking-at org-element--element-block-re
)
2792 (let ((type (upcase (match-string 1))))
2795 (format "[ \t]*#\\+END_%s\\(?: \\|$\\)" type
) nil t
))
2796 ;; Build appropriate parser. `verse-block' type
2797 ;; elements require an additional argument, so they
2798 ;; must be treated separately.
2799 (if (string= "VERSE" type
)
2800 (org-element-verse-block-parser raw-secondary-p
)
2804 "org-element-%s-parser"
2806 org-element-non-recursive-block-alist
))))))
2807 (org-element-paragraph-parser)))))
2809 ((org-at-heading-p) (org-element-inlinetask-parser raw-secondary-p
))
2810 ;; LaTeX Environment or paragraph if incomplete.
2811 ((looking-at "^[ \t]*\\\\begin{")
2813 (re-search-forward "^[ \t]*\\\\end{[^}]*}[ \t]*" nil t
))
2814 (org-element-latex-environment-parser)
2815 (org-element-paragraph-parser)))
2817 ((looking-at org-property-start-re
)
2818 (if (save-excursion (re-search-forward org-property-end-re nil t
))
2819 (org-element-property-drawer-parser)
2820 (org-element-paragraph-parser)))
2821 ;; Recursive block, or paragraph if incomplete.
2822 ((looking-at "[ \t]*#\\+BEGIN_\\([-A-Za-z0-9]+\\)\\(?: \\|$\\)")
2823 (let ((type (upcase (match-string 1))))
2825 ((not (save-excursion
2827 (format "[ \t]*#\\+END_%s\\(?: \\|$\\)" type
) nil t
)))
2828 (org-element-paragraph-parser))
2829 ((string= type
"CENTER") (org-element-center-block-parser))
2830 ((string= type
"QUOTE") (org-element-quote-block-parser))
2831 (t (org-element-special-block-parser)))))
2833 ((looking-at org-drawer-regexp
)
2834 (if (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
))
2835 (org-element-drawer-parser)
2836 (org-element-paragraph-parser)))
2837 ((looking-at "[ \t]*:\\( \\|$\\)") (org-element-fixed-width-parser))
2839 ((looking-at org-babel-block-lob-one-liner-regexp
)
2840 (org-element-babel-call-parser))
2841 ;; Keyword, or paragraph if at an affiliated keyword.
2842 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
2843 (let ((key (upcase (match-string 1))))
2844 (if (or (string= key
"TBLFM")
2845 (member key org-element-affiliated-keywords
))
2846 (org-element-paragraph-parser)
2847 (org-element-keyword-parser))))
2848 ;; Footnote definition.
2849 ((looking-at org-footnote-definition-re
)
2850 (org-element-footnote-definition-parser))
2851 ;; Dynamic block or paragraph if incomplete.
2852 ((looking-at "[ \t]*#\\+BEGIN:\\(?: \\|$\\)")
2854 (re-search-forward "^[ \t]*#\\+END:\\(?: \\|$\\)" nil t
))
2855 (org-element-dynamic-block-parser)
2856 (org-element-paragraph-parser)))
2858 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)")
2859 (org-element-comment-parser))
2861 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
2862 (org-element-horizontal-rule-parser))
2864 ((org-at-table-p t
) (org-element-table-parser))
2866 ((looking-at (org-item-re))
2867 (if (eq special
'item
)
2868 (org-element-item-parser
2869 (or structure
(org-list-struct))
2871 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2872 ;; Default element: Paragraph.
2873 (t (org-element-paragraph-parser))))))
2876 ;; Most elements can have affiliated keywords. When looking for an
2877 ;; element beginning, we want to move before them, as they belong to
2878 ;; that element, and, in the meantime, collect information they give
2879 ;; into appropriate properties. Hence the following function.
2881 ;; Usage of optional arguments may not be obvious at first glance:
2883 ;; - TRANS-LIST is used to polish keywords names that have evolved
2884 ;; during Org history. In example, even though =result= and
2885 ;; =results= coexist, we want to have them under the same =result=
2886 ;; property. It's also true for "srcname" and "name", where the
2887 ;; latter seems to be preferred nowadays (thus the "name" property).
2889 ;; - CONSED allows to regroup multi-lines keywords under the same
2890 ;; property, while preserving their own identity. This is mostly
2891 ;; used for "attr_latex" and al.
2893 ;; - PARSED prepares a keyword value for export. This is useful for
2894 ;; "caption". Objects restrictions for such keywords are defined in
2895 ;; `org-element-string-restrictions'.
2897 ;; - DUALS is used to take care of keywords accepting a main and an
2898 ;; optional secondary values. For example "results" has its
2899 ;; source's name as the main value, and may have an hash string in
2900 ;; optional square brackets as the secondary one.
2902 ;; A keyword may belong to more than one category.
2904 (defconst org-element--affiliated-re
2905 (format "[ \t]*#\\+\\(%s\\):"
2908 (if (member keyword org-element-dual-keywords
)
2909 (format "\\(%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
2910 (regexp-quote keyword
))
2911 (regexp-quote keyword
)))
2912 org-element-affiliated-keywords
"\\|"))
2913 "Regexp matching any affiliated keyword.
2915 Keyword name is put in match group 1. Moreover, if keyword
2916 belongs to `org-element-dual-keywords', put the dual value in
2919 Don't modify it, set `org-element-affiliated-keywords' instead.")
2921 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
2922 consed parsed duals
)
2923 "Collect affiliated keywords before point.
2925 Optional argument KEY-RE is a regexp matching keywords, which
2926 puts matched keyword in group 1. It defaults to
2927 `org-element--affiliated-re'.
2929 TRANS-LIST is an alist where key is the keyword and value the
2930 property name it should be translated to, without the colons. It
2931 defaults to `org-element-keyword-translation-alist'.
2933 CONSED is a list of strings. Any keyword belonging to that list
2934 will have its value consed. The check is done after keyword
2935 translation. It defaults to `org-element-multiple-keywords'.
2937 PARSED is a list of strings. Any keyword member of this list
2938 will have its value parsed. The check is done after keyword
2939 translation. If a keyword is a member of both CONSED and PARSED,
2940 it's value will be a list of parsed strings. It defaults to
2941 `org-element-parsed-keywords'.
2943 DUALS is a list of strings. Any keyword member of this list can
2944 have two parts: one mandatory and one optional. Its value is
2945 a cons cell whose car is the former, and the cdr the latter. If
2946 a keyword is a member of both PARSED and DUALS, both values will
2947 be parsed. It defaults to `org-element-dual-keywords'.
2949 Return a list whose car is the position at the first of them and
2950 cdr a plist of keywords and values."
2952 (let ((case-fold-search t
)
2953 (key-re (or key-re org-element--affiliated-re
))
2954 (trans-list (or trans-list org-element-keyword-translation-alist
))
2955 (consed (or consed org-element-multiple-keywords
))
2956 (parsed (or parsed org-element-parsed-keywords
))
2957 (duals (or duals org-element-dual-keywords
))
2958 ;; RESTRICT is the list of objects allowed in parsed
2960 (restrict (cdr (assq 'keyword org-element-string-restrictions
)))
2963 (while (and (not (bobp))
2964 (progn (forward-line -
1) (looking-at key-re
)))
2965 (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
2966 ;; Apply translation to RAW-KWD. From there, KWD is
2967 ;; the official keyword.
2968 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
2969 ;; Find main value for any keyword.
2973 (buffer-substring-no-properties
2974 (match-end 0) (point-at-eol)))))
2975 ;; If KWD is a dual keyword, find its secondary
2976 ;; value. Maybe parse it.
2978 (and (member kwd duals
)
2979 (let ((sec (org-match-string-no-properties 3)))
2980 (if (or (not sec
) (not (member kwd parsed
))) sec
2981 (org-element-parse-secondary-string sec restrict
)))))
2982 ;; Attribute a property name to KWD.
2983 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
2984 ;; Now set final shape for VALUE.
2985 (when (member kwd parsed
)
2986 (setq value
(org-element-parse-secondary-string value restrict
)))
2987 (when (member kwd duals
)
2988 ;; VALUE is mandatory. Set it to nil if there is none.
2989 (setq value
(and value
(cons value dual-value
))))
2990 (when (member kwd consed
)
2991 (setq value
(cons value
(plist-get output kwd-sym
))))
2992 ;; Eventually store the new value in OUTPUT.
2993 (setq output
(plist-put output kwd-sym value
))))
2994 (unless (looking-at key-re
) (forward-line 1)))
2995 (list (point) output
))))
3001 ;; The two major functions here are `org-element-parse-buffer', which
3002 ;; parses Org syntax inside the current buffer, taking into account
3003 ;; region, narrowing, or even visibility if specified, and
3004 ;; `org-element-parse-secondary-string', which parses objects within
3007 ;; The (almost) almighty `org-element-map' allows to apply a function
3008 ;; on elements or objects matching some type, and accumulate the
3009 ;; resulting values. In an export situation, it also skips unneeded
3010 ;; parts of the parse tree.
3012 (defun org-element-parse-buffer (&optional granularity visible-only
)
3013 "Recursively parse the buffer and return structure.
3014 If narrowing is in effect, only parse the visible part of the
3017 Optional argument GRANULARITY determines the depth of the
3018 recursion. It can be set to the following symbols:
3020 `headline' Only parse headlines.
3021 `greater-element' Don't recurse into greater elements excepted
3022 headlines and sections. Thus, elements
3023 parsed are the top-level ones.
3024 `element' Parse everything but objects and plain text.
3025 `object' Parse the complete buffer (default).
3027 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3030 Assume buffer is in Org mode."
3032 (goto-char (point-min))
3033 (org-skip-whitespace)
3034 (nconc (list 'org-data nil
)
3035 (org-element-parse-elements
3036 (point-at-bol) (point-max)
3037 ;; Start is section mode so text before the first headline
3038 ;; belongs to a section.
3039 'section nil granularity visible-only nil
))))
3041 (defun org-element-parse-secondary-string (string restriction
)
3042 "Recursively parse objects in STRING and return structure.
3044 RESTRICTION, when non-nil, is a symbol limiting the object types
3045 that will be looked after."
3048 (org-element-parse-objects (point-min) (point-max) nil restriction
)))
3050 (defun org-element-map (data types fun
&optional info first-match no-recursion
)
3051 "Map a function on selected elements or objects.
3053 DATA is the parsed tree, as returned by, i.e,
3054 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3055 of elements or objects types. FUN is the function called on the
3056 matching element or object. It must accept one arguments: the
3057 element or object itself.
3059 When optional argument INFO is non-nil, it should be a plist
3060 holding export options. In that case, parts of the parse tree
3061 not exportable according to that property list will be skipped.
3063 When optional argument FIRST-MATCH is non-nil, stop at the first
3064 match for which FUN doesn't return nil, and return that value.
3066 Optional argument NO-RECURSION is a symbol or a list of symbols
3067 representing elements or objects types. `org-element-map' won't
3068 enter any recursive element or object whose type belongs to that
3069 list. Though, FUN can still be applied on them.
3071 Nil values returned from FUN do not appear in the results."
3072 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3073 (unless (listp types
) (setq types
(list types
)))
3074 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3075 ;; Recursion depth is determined by --CATEGORY.
3078 ((loop for type in types
3079 always
(memq type org-element-greater-elements
))
3081 ((loop for type in types
3082 always
(memq type org-element-all-elements
))
3085 ;; --RESTRICTS is a list of element types whose secondary
3086 ;; string could possibly contain an object with a type among
3089 (and (eq --category
'objects
)
3090 (loop for el in org-element-secondary-value-alist
3092 (loop for o in types
3096 org-element-string-restrictions
))))
3102 ;; Recursively walk DATA. INFO, if non-nil, is
3103 ;; a plist holding contextual information.
3106 (unless (and info
(member --blob
(plist-get info
:ignore-list
)))
3107 (let ((--type (org-element-type --blob
)))
3108 ;; Check if TYPE is matching among TYPES. If so,
3109 ;; apply FUN to --BLOB and accumulate return value
3110 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3111 (when (memq --type types
)
3112 (let ((result (funcall fun --blob
)))
3113 (cond ((not result
))
3114 (first-match (throw 'first-match result
))
3115 (t (push result --acc
)))))
3116 ;; If --BLOB has a secondary string that can
3117 ;; contain objects with their type among TYPES,
3118 ;; look into that string.
3119 (when (memq --type --restricts
)
3124 ,@(org-element-property
3125 (cdr (assq --type org-element-secondary-value-alist
))
3127 ;; Now determine if a recursion into --BLOB is
3128 ;; possible. If so, do it.
3129 (unless (memq --type no-recursion
)
3130 (when (or (and (memq --type org-element-greater-elements
)
3131 (not (eq --category
'greater-elements
)))
3132 (and (memq --type org-element-all-elements
)
3133 (not (eq --category
'elements
)))
3134 (memq --type org-element-recursive-objects
))
3135 (funcall --walk-tree --blob
))))))
3136 (org-element-contents --data
))))))
3138 (funcall --walk-tree data
)
3139 ;; Return value in a proper order.
3142 ;; The following functions are internal parts of the parser.
3144 ;; The first one, `org-element-parse-elements' acts at the element's
3147 ;; The second one, `org-element-parse-objects' applies on all objects
3148 ;; of a paragraph or a secondary string. It uses
3149 ;; `org-element-get-candidates' to optimize the search of the next
3150 ;; object in the buffer.
3152 ;; More precisely, that function looks for every allowed object type
3153 ;; first. Then, it discards failed searches, keeps further matches,
3154 ;; and searches again types matched behind point, for subsequent
3155 ;; calls. Thus, searching for a given type fails only once, and every
3156 ;; object is searched only once at top level (but sometimes more for
3159 (defun org-element-parse-elements
3160 (beg end special structure granularity visible-only acc
)
3161 "Parse elements between BEG and END positions.
3163 SPECIAL prioritize some elements over the others. It can set to
3164 `quote-section', `section' or `item', which will focus search,
3165 respectively, on quote sections, sections and items. Moreover,
3166 when value is `item', STRUCTURE will be used as the current list
3169 GRANULARITY determines the depth of the recursion. See
3170 `org-element-parse-buffer' for more information.
3172 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3175 Elements are accumulated into ACC."
3178 (narrow-to-region beg end
)
3180 ;; When parsing only headlines, skip any text before first one.
3181 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3182 (org-with-limited-levels (outline-next-heading)))
3186 ;; 1. Item mode is active: point must be at an item. Parse it
3187 ;; directly, skipping `org-element-current-element'.
3188 (if (eq special
'item
)
3190 (org-element-item-parser
3192 (and granularity
(not (eq granularity
'object
))))))
3193 (goto-char (org-element-property :end element
))
3194 (org-element-parse-elements
3195 (org-element-property :contents-begin element
)
3196 (org-element-property :contents-end element
)
3197 nil structure granularity visible-only
(reverse element
)))
3198 ;; 2. When ITEM is nil, find current element's type and parse
3199 ;; it accordingly to its category.
3200 (let* ((element (org-element-current-element
3201 granularity special structure
))
3202 (type (org-element-type element
)))
3203 (goto-char (org-element-property :end element
))
3205 ;; Case 1. ELEMENT is a paragraph. Parse objects inside,
3206 ;; if GRANULARITY allows it.
3207 ((and (eq type
'paragraph
)
3208 (or (not granularity
) (eq granularity
'object
)))
3209 (org-element-parse-objects
3210 (org-element-property :contents-begin element
)
3211 (org-element-property :contents-end element
)
3212 (reverse element
) nil
))
3213 ;; Case 2. ELEMENT is recursive: parse it between
3214 ;; `contents-begin' and `contents-end'. Make sure
3215 ;; GRANULARITY allows the recursion, or ELEMENT is an
3216 ;; headline, in which case going inside is mandatory, in
3217 ;; order to get sub-level headings. If VISIBLE-ONLY is
3218 ;; true and element is hidden, do not recurse into it.
3219 ((and (memq type org-element-greater-elements
)
3220 (or (not granularity
)
3221 (memq granularity
'(element object
))
3222 (and (eq granularity
'greater-element
) (eq type
'section
))
3223 (eq type
'headline
))
3224 (not (and visible-only
3225 (org-element-property :hiddenp element
))))
3226 (org-element-parse-elements
3227 (org-element-property :contents-begin element
)
3228 (org-element-property :contents-end element
)
3229 ;; At a plain list, switch to item mode. At an
3230 ;; headline, switch to section mode. Any other
3231 ;; element turns off special modes.
3234 (headline (if (org-element-property :quotedp element
)
3237 (org-element-property :structure element
)
3238 granularity visible-only
(reverse element
)))
3239 ;; Case 3. Else, just accumulate ELEMENT.
3245 (defun org-element-parse-objects (beg end acc restriction
)
3246 "Parse objects between BEG and END and return recursive structure.
3248 Objects are accumulated in ACC.
3250 RESTRICTION, when non-nil, is a list of object types which are
3251 allowed in the current object."
3252 (let ((get-next-object
3255 ;; Return the parsing function associated to the nearest
3256 ;; object among list of candidates CAND.
3257 (let ((pos (apply #'min
(mapcar #'cdr cand
))))
3262 (format "org-element-%s-parser" (car (rassq pos cand
))))))))))
3263 next-object candidates
)
3266 (while (setq candidates
(org-element-get-next-object-candidates
3267 end restriction candidates
))
3268 (setq next-object
(funcall get-next-object candidates
))
3269 ;; 1. Text before any object. Untabify it.
3270 (let ((obj-beg (org-element-property :begin next-object
)))
3271 (unless (= (point) obj-beg
)
3272 (push (replace-regexp-in-string
3273 "\t" (make-string tab-width ?
)
3274 (buffer-substring-no-properties (point) obj-beg
))
3277 (let ((obj-end (org-element-property :end next-object
))
3278 (cont-beg (org-element-property :contents-begin next-object
)))
3279 (push (if (and (memq (car next-object
) org-element-recursive-objects
)
3281 ;; ... recursive. The CONT-BEG check is for
3282 ;; links, as some of them might not be recursive
3283 ;; (i.e. plain links).
3287 (org-element-property :contents-end next-object
))
3288 (org-element-parse-objects
3289 (point-min) (point-max) (reverse next-object
)
3290 ;; Restrict allowed objects. This is the
3291 ;; intersection of current restriction and next
3292 ;; object's restriction.
3294 (cdr (assq (car next-object
)
3295 org-element-object-restrictions
))))
3296 (if (not restriction
) new-restr
3298 (lambda (e) (and (memq e restriction
) e
))
3300 ;; ... not recursive.
3303 (goto-char obj-end
)))
3304 ;; 3. Text after last object. Untabify it.
3305 (unless (= (point) end
)
3306 (push (replace-regexp-in-string
3307 "\t" (make-string tab-width ?
)
3308 (buffer-substring-no-properties (point) end
))
3313 (defun org-element-get-next-object-candidates (limit restriction objects
)
3314 "Return an alist of candidates for the next object.
3316 LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
3317 the possible object types.
3319 Return value is an alist whose car is position and cdr the object
3320 type, as a string. There is an association for the closest
3321 object of each type within RESTRICTION when non-nil, or for every
3324 OBJECTS is the previous candidates alist."
3325 (let ((restriction (or restriction org-element-all-successors
))
3326 next-candidates types-to-search
)
3327 ;; If no previous result, search every object type in RESTRICTION.
3328 ;; Otherwise, keep potential candidates (old objects located after
3329 ;; point) and ask to search again those which had matched before.
3330 (if (not objects
) (setq types-to-search restriction
)
3332 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3333 (push obj next-candidates
)))
3335 ;; Call the appropriate "get-next" function for each type to
3336 ;; search and accumulate matches.
3339 (let* ((successor-fun
3341 (format "org-element-%s-successor"
3342 (or (cdr (assq type org-element-object-successor-alist
))
3344 (obj (funcall successor-fun limit
)))
3345 (and obj
(push obj next-candidates
))))
3352 ;;; Towards A Bijective Process
3354 ;; The parse tree obtained with `org-element-parse-buffer' is really
3355 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3356 ;; interpreted and expanded into a string with canonical Org
3357 ;; syntax. Hence `org-element-interpret-data'.
3359 ;; Data parsed from secondary strings, whose shape is slightly
3360 ;; different than the standard parse tree, is expanded with the
3361 ;; equivalent function `org-element-interpret-secondary'.
3363 ;; Both functions rely internally on
3364 ;; `org-element-interpret--affiliated-keywords'.
3366 (defun org-element-interpret-data (data &optional genealogy previous
)
3367 "Interpret a parse tree representing Org data.
3369 DATA is the parse tree to interpret.
3371 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3373 GENEALOGY is the list of its parents types.
3374 PREVIOUS is the type of the element or object at the same level
3377 Return Org syntax as a string."
3380 ;; BLOB can be an element, an object, a string, or nil.
3383 ((equal blob
"") nil
)
3384 ((stringp blob
) blob
)
3386 (let* ((type (org-element-type blob
))
3388 (if (eq type
'org-data
) 'identity
3389 (intern (format "org-element-%s-interpreter" type
))))
3392 ;; Full Org document.
3393 ((eq type
'org-data
)
3394 (org-element-interpret-data blob genealogy previous
))
3395 ;; Recursive objects.
3396 ((memq type org-element-recursive-objects
)
3397 (org-element-interpret-data
3398 blob
(cons type genealogy
) nil
))
3399 ;; Recursive elements.
3400 ((memq type org-element-greater-elements
)
3401 (org-element-normalize-string
3402 (org-element-interpret-data
3403 blob
(cons type genealogy
) nil
)))
3405 ((eq type
'paragraph
)
3407 (org-element-normalize-contents
3409 ;; When normalizing contents of an item,
3410 ;; ignore first line's indentation.
3412 (memq (car genealogy
)
3413 '(footnote-definiton item
))))))
3414 (org-element-interpret-data
3415 paragraph
(cons type genealogy
) nil
)))))
3416 (results (funcall interpreter blob contents
)))
3418 (setq previous type
)
3419 ;; Build white spaces.
3421 ((eq type
'org-data
) results
)
3422 ((memq type org-element-all-elements
)
3424 (org-element-interpret--affiliated-keywords blob
)
3425 (org-element-normalize-string results
)
3426 (make-string (org-element-property :post-blank blob
) 10)))
3429 (make-string (org-element-property :post-blank blob
) 32))))))))
3430 (org-element-contents data
) ""))
3432 (defun org-element-interpret-secondary (secondary)
3433 "Interpret SECONDARY string as Org syntax.
3435 SECONDARY-STRING is a nested list as returned by
3436 `org-element-parse-secondary-string'.
3438 Return interpreted string."
3439 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3440 (let ((s (if (listp secondary
) secondary
(list secondary
))))
3441 (org-element-interpret-data `(org-data nil
,@s
) nil nil
)))
3443 ;; Both functions internally use `org-element--affiliated-keywords'.
3445 (defun org-element-interpret--affiliated-keywords (element)
3446 "Return ELEMENT's affiliated keywords as Org syntax.
3447 If there is no affiliated keyword, return the empty string."
3448 (let ((keyword-to-org
3452 (when (member key org-element-dual-keywords
)
3453 (setq dual
(cdr value
) value
(car value
)))
3454 (concat "#+" key
(and dual
(format "[%s]" dual
)) ": "
3455 (if (member key org-element-parsed-keywords
)
3456 (org-element-interpret-secondary value
)
3461 (let ((value (org-element-property (intern (concat ":" key
)) element
)))
3463 (if (member key org-element-multiple-keywords
)
3464 (mapconcat (lambda (line)
3465 (funcall keyword-to-org key line
))
3467 (funcall keyword-to-org key value
)))))
3468 ;; Remove translated keywords.
3472 (and (not (assoc key org-element-keyword-translation-alist
)) key
))
3473 org-element-affiliated-keywords
))
3476 ;; Because interpretation of the parse tree must return the same
3477 ;; number of blank lines between elements and the same number of white
3478 ;; space after objects, some special care must be given to white
3481 ;; The first function, `org-element-normalize-string', ensures any
3482 ;; string different from the empty string will end with a single
3483 ;; newline character.
3485 ;; The second function, `org-element-normalize-contents', removes
3486 ;; global indentation from the contents of the current element.
3488 (defun org-element-normalize-string (s)
3489 "Ensure string S ends with a single newline character.
3491 If S isn't a string return it unchanged. If S is the empty
3492 string, return it. Otherwise, return a new string with a single
3493 newline character at its end."
3495 ((not (stringp s
)) s
)
3497 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3498 (replace-match "\n" nil nil s
)))))
3500 (defun org-element-normalize-contents (element &optional ignore-first
)
3501 "Normalize plain text in ELEMENT's contents.
3503 ELEMENT must only contain plain text and objects.
3505 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3506 indentation to compute maximal common indentation.
3508 Return the normalized element that is element with global
3509 indentation removed from its contents. The function assumes that
3510 indentation is not done with TAB characters."
3514 ;; Return list of indentations within BLOB. This is done by
3515 ;; walking recursively BLOB and updating IND-LIST along the
3516 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3517 ;; been seen yet. It is required as this string is the only
3518 ;; one whose indentation doesn't happen after a newline
3520 (lambda (blob first-flag
)
3523 (when (and first-flag
(stringp object
))
3524 (setq first-flag nil
)
3525 (string-match "\\`\\( *\\)" object
)
3526 (let ((len (length (match-string 1 object
))))
3527 ;; An indentation of zero means no string will be
3528 ;; modified. Quit the process.
3529 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
3530 (push len ind-list
))))
3534 (while (string-match "\n\\( *\\)" object start
)
3535 (setq start
(match-end 0))
3536 (push (length (match-string 1 object
)) ind-list
))))
3537 ((memq (org-element-type object
) org-element-recursive-objects
)
3538 (funcall collect-inds object first-flag
))))
3539 (org-element-contents blob
))))))
3540 ;; Collect indentation list in ELEMENT. Possibly remove first
3541 ;; value if IGNORE-FIRST is non-nil.
3542 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
3543 (if (not ind-list
) element
3544 ;; Build ELEMENT back, replacing each string with the same
3545 ;; string minus common indentation.
3548 (lambda (blob mci first-flag
)
3549 ;; Return BLOB with all its strings indentation
3550 ;; shortened from MCI white spaces. FIRST-FLAG is
3551 ;; non-nil when the first string hasn't been seen
3554 (list (org-element-type blob
) (nth 1 blob
))
3557 (when (and first-flag
(stringp object
))
3558 (setq first-flag nil
)
3560 (replace-regexp-in-string
3561 (format "\\` \\{%d\\}" mci
) "" object
)))
3564 (replace-regexp-in-string
3565 (format "\n \\{%d\\}" mci
) "\n" object
))
3566 ((memq (org-element-type object
) org-element-recursive-objects
)
3567 (funcall build object mci first-flag
))
3569 (org-element-contents blob
)))))))
3570 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
3576 ;; The first move is to implement a way to obtain the smallest element
3577 ;; containing point. This is the job of `org-element-at-point'. It
3578 ;; basically jumps back to the beginning of section containing point
3579 ;; and moves, element after element, with
3580 ;; `org-element-current-element' until the container is found.
3582 ;; Note: When using `org-element-at-point', secondary values are never
3583 ;; parsed since the function focuses on elements, not on objects.
3585 (defun org-element-at-point (&optional keep-trail
)
3586 "Determine closest element around point.
3588 Return value is a list like (TYPE PROPS) where TYPE is the type
3589 of the element and PROPS a plist of properties associated to the
3590 element. Possible types are defined in
3591 `org-element-all-elements'.
3593 As a special case, if point is at the very beginning of a list or
3594 sub-list, element returned will be that list instead of the first
3597 If optional argument KEEP-TRAIL is non-nil, the function returns
3598 a list of of elements leading to element at point. The list's
3599 CAR is always the element at point. Its last item will be the
3600 element's parent, unless element was either the first in its
3601 section (in which case the last item in the list is the first
3602 element of section) or an headline (in which case the list
3603 contains that headline as its single element). Elements
3604 in-between, if any, are siblings of the element at point."
3605 (org-with-wide-buffer
3606 ;; If at an headline, parse it. It is the sole element that
3607 ;; doesn't require to know about context. Be sure to disallow
3608 ;; secondary string parsing, though.
3609 (if (org-with-limited-levels (org-at-heading-p))
3610 (if (not keep-trail
) (org-element-headline-parser t
)
3611 (list (org-element-headline-parser t
)))
3612 ;; Otherwise move at the beginning of the section containing
3614 (let ((origin (point)) element type item-flag trail struct prevs
)
3615 (org-with-limited-levels
3616 (if (org-before-first-heading-p) (goto-char (point-min))
3617 (org-back-to-heading)
3619 (org-skip-whitespace)
3621 ;; Starting parsing successively each element with
3622 ;; `org-element-current-element'. Skip those ending before
3623 ;; original position.
3626 (setq element
(org-element-current-element 'element item-flag struct
)
3628 (when keep-trail
(push element trail
))
3630 ;; 1. Skip any element ending before point or at point.
3631 ((let ((end (org-element-property :end element
)))
3632 (when (<= end origin
)
3633 (if (> (point-max) end
) (goto-char end
)
3634 (throw 'exit
(or trail element
))))))
3635 ;; 2. An element containing point is always the element at
3637 ((not (memq type org-element-greater-elements
))
3638 (throw 'exit
(if keep-trail trail element
)))
3639 ;; 3. At a plain list.
3640 ((eq type
'plain-list
)
3641 (setq struct
(org-element-property :structure element
)
3642 prevs
(or prevs
(org-list-prevs-alist struct
)))
3643 (let ((beg (org-element-property :contents-begin element
)))
3644 (if (= beg origin
) (throw 'exit
(or trail element
))
3645 ;; Find the item at this level containing ORIGIN.
3646 (let ((items (org-list-get-all-items beg struct prevs
)))
3652 ;; Item ends before point: skip it.
3653 ((<= (org-list-get-item-end pos struct
) origin
))
3654 ;; Item contains point: store is in PARENT.
3655 ((<= pos origin
) (setq parent pos
))
3656 ;; We went too far: return PARENT.
3657 (t (throw 'local nil
)))) items
))
3658 ;; No parent: no item contained point, though
3659 ;; the plain list does. Point is in the blank
3660 ;; lines after the list: return plain list.
3661 (if (not parent
) (throw 'exit
(or trail element
))
3662 (setq item-flag
'item
)
3663 (goto-char parent
)))))))
3664 ;; 4. At any other greater element type, if point is
3665 ;; within contents, move into it. Otherwise, return
3668 (when (eq type
'item
) (setq item-flag nil
))
3669 (let ((beg (org-element-property :contents-begin element
))
3670 (end (org-element-property :contents-end element
)))
3671 (if (or (> beg origin
) (< end origin
))
3672 (throw 'exit
(or trail element
))
3673 ;; Reset trail, since we found a parent.
3674 (when keep-trail
(setq trail
(list element
)))
3675 (narrow-to-region beg end
)
3676 (goto-char beg
)))))))))))
3679 ;; Once the local structure around point is well understood, it's easy
3680 ;; to implement some replacements for `forward-paragraph'
3681 ;; `backward-paragraph', namely `org-element-forward' and
3682 ;; `org-element-backward'.
3684 ;; Also, `org-transpose-elements' mimics the behaviour of
3685 ;; `transpose-words', at the element's level, whereas
3686 ;; `org-element-drag-forward', `org-element-drag-backward', and
3687 ;; `org-element-up' generalize, respectively, functions
3688 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3690 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3691 ;; smartly remove global indentation from buffer, making it possible
3692 ;; to use Org indent mode on a file created with hard indentation.
3694 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3695 ;; internally by some of the previously cited tools.
3697 (defsubst org-element-nested-p
(elem-A elem-B
)
3698 "Non-nil when elements ELEM-A and ELEM-B are nested."
3699 (let ((beg-A (org-element-property :begin elem-A
))
3700 (beg-B (org-element-property :begin elem-B
))
3701 (end-A (org-element-property :end elem-A
))
3702 (end-B (org-element-property :end elem-B
)))
3703 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
3704 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
3706 (defun org-element-swap-A-B (elem-A elem-B
)
3707 "Swap elements ELEM-A and ELEM-B.
3709 Leave point at the end of ELEM-A."
3710 (goto-char (org-element-property :begin elem-A
))
3711 (let* ((beg-A (org-element-property :begin elem-A
))
3712 (end-A (save-excursion
3713 (goto-char (org-element-property :end elem-A
))
3714 (skip-chars-backward " \r\t\n")
3716 (beg-B (org-element-property :begin elem-B
))
3717 (end-B (save-excursion
3718 (goto-char (org-element-property :end elem-B
))
3719 (skip-chars-backward " \r\t\n")
3721 (body-A (buffer-substring beg-A end-A
))
3722 (body-B (delete-and-extract-region beg-B end-B
)))
3726 (delete-region beg-A end-A
)
3728 (goto-char (org-element-property :end elem-B
))))
3730 (defun org-element-backward ()
3731 "Move backward by one element.
3732 Move to the previous element at the same level, when possible."
3734 (if (save-excursion (skip-chars-backward " \r\t\n") (bobp))
3735 (error "Cannot move further up")
3736 (let* ((trail (org-element-at-point 'keep-trail
))
3737 (element (car trail
))
3738 (beg (org-element-property :begin element
)))
3739 ;; Move to beginning of current element if point isn't there.
3740 (if (/= (point) beg
) (goto-char beg
)
3741 (let ((type (org-element-type element
)))
3743 ;; At an headline: move to previous headline at the same
3744 ;; level, a parent, or BOB.
3745 ((eq type
'headline
)
3746 (let ((dest (save-excursion (org-backward-same-level 1) (point))))
3747 (if (= (point-min) dest
) (error "Cannot move further up")
3749 ;; At an item: try to move to the previous item, if any.
3750 ((and (eq type
'item
)
3751 (let* ((struct (org-element-property :structure element
))
3752 (prev (org-list-get-prev-item
3753 beg struct
(org-list-prevs-alist struct
))))
3754 (when prev
(goto-char prev
)))))
3755 ;; In any other case, find the previous element in the
3756 ;; trail and move to its beginning. If no previous element
3757 ;; can be found, move to headline.
3758 (t (let ((prev (nth 1 trail
)))
3759 (if prev
(goto-char (org-element-property :begin prev
))
3760 (org-back-to-heading))))))))))
3762 (defun org-element-drag-backward ()
3763 "Drag backward element at point."
3765 (let* ((pos (point))
3766 (elem (org-element-at-point)))
3767 (when (= (progn (goto-char (point-min))
3768 (org-skip-whitespace)
3770 (org-element-property :end elem
))
3771 (error "Cannot drag element backward"))
3772 (goto-char (org-element-property :begin elem
))
3773 (org-element-backward)
3774 (let ((prev-elem (org-element-at-point)))
3775 (when (or (org-element-nested-p elem prev-elem
)
3776 (and (eq (org-element-type elem
) 'headline
)
3777 (not (eq (org-element-type prev-elem
) 'headline
))))
3779 (error "Cannot drag element backward"))
3780 ;; Compute new position of point: it's shifted by PREV-ELEM
3782 (let ((size-prev (- (org-element-property :end prev-elem
)
3783 (org-element-property :begin prev-elem
))))
3784 (org-element-swap-A-B prev-elem elem
)
3785 (goto-char (- pos size-prev
))))))
3787 (defun org-element-drag-forward ()
3788 "Move forward element at point."
3790 (let* ((pos (point))
3791 (elem (org-element-at-point)))
3792 (when (= (point-max) (org-element-property :end elem
))
3793 (error "Cannot drag element forward"))
3794 (goto-char (org-element-property :end elem
))
3795 (let ((next-elem (org-element-at-point)))
3796 (when (or (org-element-nested-p elem next-elem
)
3797 (and (eq (org-element-type next-elem
) 'headline
)
3798 (not (eq (org-element-type elem
) 'headline
))))
3800 (error "Cannot drag element forward"))
3801 ;; Compute new position of point: it's shifted by NEXT-ELEM
3802 ;; body's length (without final blanks) and by the length of
3803 ;; blanks between ELEM and NEXT-ELEM.
3804 (let ((size-next (- (save-excursion
3805 (goto-char (org-element-property :end next-elem
))
3806 (skip-chars-backward " \r\t\n")
3809 (org-element-property :begin next-elem
)))
3810 (size-blank (- (org-element-property :end elem
)
3812 (goto-char (org-element-property :end elem
))
3813 (skip-chars-backward " \r\t\n")
3816 (org-element-swap-A-B elem next-elem
)
3817 (goto-char (+ pos size-next size-blank
))))))
3819 (defun org-element-forward ()
3820 "Move forward by one element.
3821 Move to the next element at the same level, when possible."
3823 (if (eobp) (error "Cannot move further down")
3824 (let* ((trail (org-element-at-point 'keep-trail
))
3825 (element (car trail
))
3826 (type (org-element-type element
))
3827 (end (org-element-property :end element
)))
3829 ;; At an headline, move to next headline at the same level.
3830 ((eq type
'headline
) (goto-char end
))
3831 ;; At an item. Move to the next item, if possible.
3832 ((and (eq type
'item
)
3833 (let* ((struct (org-element-property :structure element
))
3834 (prevs (org-list-prevs-alist struct
))
3835 (beg (org-element-property :begin element
))
3836 (next-item (org-list-get-next-item beg struct prevs
)))
3837 (when next-item
(goto-char next-item
)))))
3838 ;; In any other case, move to element's end, unless this
3839 ;; position is also the end of its parent's contents, in which
3840 ;; case, directly jump to parent's end.
3843 ;; Determine if TRAIL contains the real parent of ELEMENT.
3844 (and (> (length trail
) 1)
3845 (let* ((parent-candidate (car (last trail
))))
3846 (and (memq (org-element-type parent-candidate
)
3847 org-element-greater-elements
)
3848 (>= (org-element-property
3849 :contents-end parent-candidate
) end
)
3850 parent-candidate
)))))
3851 (cond ((not parent
) (goto-char end
))
3852 ((= (org-element-property :contents-end parent
) end
)
3853 (goto-char (org-element-property :end parent
)))
3854 (t (goto-char end
)))))))))
3856 (defun org-element-mark-element ()
3857 "Put point at beginning of this element, mark at end.
3859 Interactively, if this command is repeated or (in Transient Mark
3860 mode) if the mark is active, it marks the next element after the
3861 ones already marked."
3863 (let (deactivate-mark)
3864 (if (or (and (eq last-command this-command
) (mark t
))
3865 (and transient-mark-mode mark-active
))
3869 (goto-char (org-element-property :end
(org-element-at-point)))))
3870 (let ((element (org-element-at-point)))
3872 (push-mark (org-element-property :end element
) t t
)
3873 (goto-char (org-element-property :begin element
))))))
3875 (defun org-narrow-to-element ()
3876 "Narrow buffer to current element."
3878 (let ((elem (org-element-at-point)))
3880 ((eq (car elem
) 'headline
)
3882 (org-element-property :begin elem
)
3883 (org-element-property :end elem
)))
3884 ((memq (car elem
) org-element-greater-elements
)
3886 (org-element-property :contents-begin elem
)
3887 (org-element-property :contents-end elem
)))
3890 (org-element-property :begin elem
)
3891 (org-element-property :end elem
))))))
3893 (defun org-transpose-elements ()
3894 "Transpose current and previous elements, keeping blank lines between.
3895 Point is moved after both elements."
3897 (org-skip-whitespace)
3899 (cur (org-element-at-point)))
3900 (when (= (save-excursion (goto-char (point-min))
3901 (org-skip-whitespace)
3903 (org-element-property :begin cur
))
3904 (error "No previous element"))
3905 (goto-char (org-element-property :begin cur
))
3907 (let ((prev (org-element-at-point)))
3908 (when (org-element-nested-p cur prev
)
3910 (error "Cannot transpose nested elements"))
3911 (org-element-swap-A-B prev cur
))))
3913 (defun org-element-unindent-buffer ()
3914 "Un-indent the visible part of the buffer.
3915 Relative indentation \(between items, inside blocks, etc.\) isn't
3918 (unless (eq major-mode
'org-mode
)
3919 (error "Cannot un-indent a buffer not in Org mode"))
3920 (let* ((parse-tree (org-element-parse-buffer 'greater-element
))
3921 unindent-tree
; For byte-compiler.
3925 (mapc (lambda (element)
3926 (if (eq (org-element-type element
) 'headline
)
3927 (funcall unindent-tree
3928 (org-element-contents element
))
3932 (org-element-property :begin element
)
3933 (org-element-property :end element
))
3934 (org-do-remove-indentation)))))
3935 (reverse contents
))))))
3936 (funcall unindent-tree
(org-element-contents parse-tree
))))
3938 (defun org-element-up ()
3939 "Move to upper element."
3942 ((bobp) (error "No surrounding element"))
3943 ((org-with-limited-levels (org-at-heading-p))
3944 (or (org-up-heading-safe) (error "No surronding element")))
3946 (let* ((trail (org-element-at-point 'keep-trail
))
3947 (element (car trail
))
3948 (type (org-element-type element
)))
3950 ;; At an item, with a parent in the list: move to that parent.
3951 ((and (eq type
'item
)
3952 (let* ((beg (org-element-property :begin element
))
3953 (struct (org-element-property :structure element
))
3954 (parents (org-list-parents-alist struct
))
3955 (parentp (org-list-get-parent beg struct parents
)))
3956 (and parentp
(goto-char parentp
)))))
3957 ;; Determine parent in the trail.
3960 (and (> (length trail
) 1)
3961 (let ((parentp (car (last trail
))))
3962 (and (memq (org-element-type parentp
)
3963 org-element-greater-elements
)
3964 (>= (org-element-property :contents-end parentp
)
3965 (org-element-property :end element
))
3968 ;; When parent is found move to its beginning.
3969 (parent (goto-char (org-element-property :begin parent
)))
3970 ;; If no parent was found, move to headline above, if any
3971 ;; or return an error.
3972 ((org-before-first-heading-p) (error "No surrounding element"))
3973 (t (org-back-to-heading))))))))))
3975 (defun org-element-down ()
3976 "Move to inner element."
3978 (let ((element (org-element-at-point)))
3980 ((eq (org-element-type element
) 'plain-list
)
3982 ((memq (org-element-type element
) org-element-greater-elements
)
3983 ;; If contents are hidden, first disclose them.
3984 (when (org-element-property :hiddenp element
) (org-cycle))
3985 (goto-char (org-element-property :contents-begin element
)))
3986 (t (error "No inner element")))))
3989 (provide 'org-element
)
3990 ;;; org-element.el ends here