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', `value' and `:post-blank' as keywords.
2375 Assume point is at the target."
2377 (looking-at org-target-regexp
)
2378 (let ((begin (point))
2379 (value (org-match-string-no-properties 1))
2380 (post-blank (progn (goto-char (match-end 0))
2381 (skip-chars-forward " \t")))
2387 :post-blank
,post-blank
)))))
2389 (defun org-element-target-interpreter (target contents
)
2390 "Interpret TARGET object as Org syntax.
2392 (format "<<%s>>" (org-element-property :value target
)))
2394 (defun org-element-target-successor (limit)
2395 "Search for the next target object.
2397 LIMIT bounds the search.
2399 Return value is a cons cell whose car is `target' and cdr is
2400 beginning position."
2402 (when (re-search-forward org-target-regexp limit t
)
2403 (cons 'target
(match-beginning 0)))))
2408 (defun org-element-time-stamp-parser ()
2409 "Parse time stamp at point.
2411 Return a list whose car is `time-stamp', and cdr a plist with
2412 `:appt-type', `:type', `:begin', `:end', `:value' and
2413 `:post-blank' keywords.
2415 Assume point is at the beginning of the time-stamp."
2417 (let* ((appt-type (cond
2418 ((looking-at (concat org-deadline-string
" +"))
2419 (goto-char (match-end 0))
2421 ((looking-at (concat org-scheduled-string
" +"))
2422 (goto-char (match-end 0))
2424 ((looking-at (concat org-closed-string
" +"))
2425 (goto-char (match-end 0))
2427 (begin (and appt-type
(match-beginning 0)))
2429 ((looking-at org-tsr-regexp
)
2430 (if (match-string 2) 'active-range
'active
))
2431 ((looking-at org-tsr-regexp-both
)
2432 (if (match-string 2) 'inactive-range
'inactive
))
2433 ((looking-at (concat
2434 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2436 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2438 (begin (or begin
(match-beginning 0)))
2439 (value (buffer-substring-no-properties
2440 (match-beginning 0) (match-end 0)))
2441 (post-blank (progn (goto-char (match-end 0))
2442 (skip-chars-forward " \t")))
2445 (:appt-type
,appt-type
2450 :post-blank
,post-blank
)))))
2452 (defun org-element-time-stamp-interpreter (time-stamp contents
)
2453 "Interpret TIME-STAMP object as Org syntax.
2456 (case (org-element-property :appt-type time-stamp
)
2457 (closed (concat org-closed-string
" "))
2458 (deadline (concat org-deadline-string
" "))
2459 (scheduled (concat org-scheduled-string
" ")))
2460 (org-element-property :value time-stamp
)))
2462 (defun org-element-time-stamp-successor (limit)
2463 "Search for the next time-stamp object.
2465 LIMIT bounds the search.
2467 Return value is a cons cell whose car is `time-stamp' and cdr is
2468 beginning position."
2470 (when (re-search-forward
2471 (concat "\\(?:" org-scheduled-string
" +\\|"
2472 org-deadline-string
" +\\|" org-closed-string
" +\\)?"
2475 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2477 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2479 (cons 'time-stamp
(match-beginning 0)))))
2484 (defun org-element-verbatim-parser ()
2485 "Parse verbatim object at point.
2487 Return a list whose car is `verbatim' and cdr is a plist with
2488 `:marker', `:begin', `:end' and `:post-blank' keywords.
2490 Assume point is at the first verbatim marker."
2492 (unless (bolp) (backward-char 1))
2493 (looking-at org-emph-re
)
2494 (let ((begin (match-beginning 2))
2495 (marker (org-match-string-no-properties 3))
2496 (value (org-match-string-no-properties 4))
2497 (post-blank (progn (goto-char (match-end 2))
2498 (skip-chars-forward " \t")))
2505 :post-blank
,post-blank
)))))
2507 (defun org-element-verbatim-interpreter (verbatim contents
)
2508 "Interpret VERBATIM object as Org syntax.
2510 (let ((marker (org-element-property :marker verbatim
))
2511 (value (org-element-property :value verbatim
)))
2512 (concat marker value marker
)))
2516 ;;; Definitions And Rules
2518 ;; Define elements, greater elements and specify recursive objects,
2519 ;; along with the affiliated keywords recognized. Also set up
2520 ;; restrictions on recursive objects combinations.
2522 ;; These variables really act as a control center for the parsing
2524 (defconst org-element-paragraph-separate
2525 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2526 ;; Headlines and inlinetasks.
2527 org-outline-regexp-bol
"\\|"
2528 ;; Comments, blocks (any type), keywords and babel calls.
2529 "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
2531 (org-item-beginning-re) "\\|"
2532 ;; Fixed-width, drawers (any type) and tables.
2534 ;; Footnote definitions.
2535 org-footnote-definition-re
"\\|"
2536 ;; Horizontal rules.
2537 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2538 ;; LaTeX environments.
2539 "^[ \t]*\\\\\\(begin\\|end\\)")
2540 "Regexp to separate paragraphs in an Org buffer.")
2542 (defconst org-element-all-elements
2543 '(center-block comment comment-block drawer dynamic-block example-block
2544 export-block fixed-width footnote-definition headline
2545 horizontal-rule inlinetask item keyword latex-environment
2546 babel-call paragraph plain-list property-drawer quote-block
2547 quote-section section special-block src-block table
2549 "Complete list of element types.")
2551 (defconst org-element-greater-elements
2552 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2553 item plain-list quote-block section special-block
)
2554 "List of recursive element types aka Greater Elements.")
2556 (defconst org-element-all-successors
2557 '(export-snippet footnote-reference inline-babel-call inline-src-block
2558 latex-or-entity line-break link macro radio-target
2559 statistics-cookie sub
/superscript target text-markup
2561 "Complete list of successors.")
2563 (defconst org-element-object-successor-alist
2564 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
2565 (emphasis . text-markup
) (verbatim . text-markup
)
2566 (entity . latex-or-entity
) (latex-fragment . latex-or-entity
))
2567 "Alist of translations between object type and successor name.
2569 Sharing the same successor comes handy when, for example, the
2570 regexp matching one object can also match the other object.")
2572 (defconst org-element-all-objects
2573 '(emphasis entity export-snippet footnote-reference inline-babel-call
2574 inline-src-block line-break latex-fragment link macro radio-target
2575 statistics-cookie subscript superscript target time-stamp
2577 "Complete list of object types.")
2579 (defconst org-element-recursive-objects
2580 '(emphasis link macro subscript superscript radio-target
)
2581 "List of recursive object types.")
2583 (defconst org-element-non-recursive-block-alist
2584 '(("ASCII" . export-block
)
2585 ("COMMENT" . comment-block
)
2586 ("DOCBOOK" . export-block
)
2587 ("EXAMPLE" . example-block
)
2588 ("HTML" . export-block
)
2589 ("LATEX" . export-block
)
2590 ("ODT" . export-block
)
2592 ("VERSE" . verse-block
))
2593 "Alist between non-recursive block name and their element type.")
2595 (defconst org-element-affiliated-keywords
2596 '("ATTR_ASCII" "ATTR_DOCBOOK" "ATTR_HTML" "ATTR_LATEX" "ATTR_ODT" "CAPTION"
2597 "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT" "RESULTS"
2598 "SOURCE" "SRCNAME" "TBLNAME")
2599 "List of affiliated keywords as strings.")
2601 (defconst org-element-keyword-translation-alist
2602 '(("DATA" .
"NAME") ("LABEL" .
"NAME") ("RESNAME" .
"NAME")
2603 ("SOURCE" .
"NAME") ("SRCNAME" .
"NAME") ("TBLNAME" .
"NAME")
2604 ("RESULT" .
"RESULTS") ("HEADERS" .
"HEADER"))
2605 "Alist of usual translations for keywords.
2606 The key is the old name and the value the new one. The property
2607 holding their value will be named after the translated name.")
2609 (defconst org-element-multiple-keywords
2610 '("ATTR_ASCII" "ATTR_DOCBOOK" "ATTR_HTML" "ATTR_LATEX" "ATTR_ODT" "HEADER")
2611 "List of affiliated keywords that can occur more that once in an element.
2613 Their value will be consed into a list of strings, which will be
2614 returned as the value of the property.
2616 This list is checked after translations have been applied. See
2617 `org-element-keyword-translation-alist'.")
2619 (defconst org-element-parsed-keywords
'("AUTHOR" "CAPTION" "TITLE")
2620 "List of keywords whose value can be parsed.
2622 Their value will be stored as a secondary string: a list of
2623 strings and objects.
2625 This list is checked after translations have been applied. See
2626 `org-element-keyword-translation-alist'.")
2628 (defconst org-element-dual-keywords
'("CAPTION" "RESULTS")
2629 "List of keywords which can have a secondary value.
2631 In Org syntax, they can be written with optional square brackets
2632 before the colons. For example, results keyword can be
2633 associated to a hash value with the following:
2635 #+RESULTS[hash-string]: some-source
2637 This list is checked after translations have been applied. See
2638 `org-element-keyword-translation-alist'.")
2640 (defconst org-element-object-restrictions
2641 '((emphasis entity export-snippet inline-babel-call inline-src-block link
2642 radio-target sub
/superscript target text-markup time-stamp
)
2643 (link entity export-snippet inline-babel-call inline-src-block
2644 latex-fragment link sub
/superscript text-markup
)
2646 (radio-target entity export-snippet latex-fragment sub
/superscript
)
2647 (subscript entity export-snippet inline-babel-call inline-src-block
2648 latex-fragment sub
/superscript text-markup
)
2649 (superscript entity export-snippet inline-babel-call inline-src-block
2650 latex-fragment sub
/superscript text-markup
))
2651 "Alist of recursive objects restrictions.
2653 CAR is a recursive object type and CDR is a list of successors
2654 that will be called within an object of such type.
2656 For example, in a `radio-target' object, one can only find
2657 entities, export snippets, latex-fragments, subscript and
2660 (defconst org-element-string-restrictions
2661 '((footnote-reference entity export-snippet footnote-reference
2662 inline-babel-call inline-src-block latex-fragment
2663 line-break link macro radio-target sub
/superscript
2664 target text-markup time-stamp
)
2665 (headline entity inline-babel-call inline-src-block latex-fragment link
2666 macro radio-target statistics-cookie sub
/superscript text-markup
2668 (inlinetask entity inline-babel-call inline-src-block latex-fragment link
2669 macro radio-target sub
/superscript text-markup time-stamp
)
2670 (item entity inline-babel-call latex-fragment macro radio-target
2671 sub
/superscript target text-markup
)
2672 (keyword entity latex-fragment macro sub
/superscript text-markup
)
2673 (table entity latex-fragment macro target text-markup
)
2674 (verse-block entity footnote-reference inline-babel-call inline-src-block
2675 latex-fragment line-break link macro radio-target
2676 sub
/superscript target text-markup time-stamp
))
2677 "Alist of secondary strings restrictions.
2679 When parsed, some elements have a secondary string which could
2680 contain various objects (i.e. headline's name, or table's cells).
2681 For association, CAR is the element type, and CDR a list of
2682 successors that will be called in that secondary string.
2684 Note: `keyword' secondary string type only applies to keywords
2685 matching `org-element-parsed-keywords'.")
2687 (defconst org-element-secondary-value-alist
2688 '((headline .
:title
)
2689 (inlinetask .
:title
)
2691 (footnote-reference .
:inline-definition
)
2692 (verse-block .
:value
))
2693 "Alist between element types and location of secondary value.")
2699 ;; Provide three accessors: `org-element-type', `org-element-property'
2700 ;; and `org-element-contents'.
2702 (defun org-element-type (element)
2703 "Return type of element ELEMENT.
2705 The function returns the type of the element or object provided.
2706 It can also return the following special value:
2707 `plain-text' for a string
2708 `org-data' for a complete document
2709 nil in any other case."
2711 ((not (consp element
)) (and (stringp element
) 'plain-text
))
2712 ((symbolp (car element
)) (car element
))))
2714 (defun org-element-property (property element
)
2715 "Extract the value from the PROPERTY of an ELEMENT."
2716 (plist-get (nth 1 element
) property
))
2718 (defun org-element-contents (element)
2719 "Extract contents from an ELEMENT."
2724 ;;; Parsing Element Starting At Point
2726 ;; `org-element-current-element' is the core function of this section.
2727 ;; It returns the Lisp representation of the element starting at
2728 ;; point. It uses `org-element--element-block-re' for quick access to
2731 (defconst org-element--element-block-re
2732 (format "[ \t]*#\\+BEGIN_\\(%s\\)\\(?: \\|$\\)"
2735 (mapcar 'car org-element-non-recursive-block-alist
) "\\|"))
2736 "Regexp matching the beginning of a non-recursive block type.
2737 Used internally by `org-element-current-element'. Do not modify
2738 it directly, set `org-element-recursive-block-alist' instead.")
2740 (defun org-element-current-element (&optional granularity special structure
)
2741 "Parse the element starting at point.
2743 Return value is a list like (TYPE PROPS) where TYPE is the type
2744 of the element and PROPS a plist of properties associated to the
2747 Possible types are defined in `org-element-all-elements'.
2749 Optional argument GRANULARITY determines the depth of the
2750 recursion. Allowed values are `headline', `greater-element',
2751 `element', `object' or nil. When it is bigger than `object' (or
2752 nil), secondary values will not be parsed, since they only
2755 Optional argument SPECIAL, when non-nil, can be either `item',
2756 `section' or `quote-section'. `item' allows to parse item wise
2757 instead of plain-list wise, using STRUCTURE as the current list
2758 structure. `section' (resp. `quote-section') will try to parse
2759 a section (resp. a quote section) before anything else.
2761 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
2764 Unlike to `org-element-at-point', this function assumes point is
2765 always at the beginning of the element it has to parse. As such,
2766 it is quicker than its counterpart, albeit more restrictive."
2769 ;; If point is at an affiliated keyword, try moving to the
2770 ;; beginning of the associated element. If none is found, the
2771 ;; keyword is orphaned and will be treated as plain text.
2772 (when (looking-at org-element--affiliated-re
)
2773 (let ((opoint (point)))
2774 (while (looking-at org-element--affiliated-re
) (forward-line))
2775 (when (looking-at "[ \t]*$") (goto-char opoint
))))
2776 (let ((case-fold-search t
)
2777 ;; Determine if parsing depth allows for secondary strings
2778 ;; parsing. It only applies to elements referenced in
2779 ;; `org-element-secondary-value-alist'.
2780 (raw-secondary-p (and granularity
(not (eq granularity
'object
)))))
2783 ((org-with-limited-levels (org-at-heading-p))
2784 (org-element-headline-parser raw-secondary-p
))
2786 ((eq special
'quote-section
) (org-element-quote-section-parser))
2788 ((eq special
'section
) (org-element-section-parser))
2789 ;; Non-recursive block.
2790 ((when (looking-at org-element--element-block-re
)
2791 (let ((type (upcase (match-string 1))))
2794 (format "[ \t]*#\\+END_%s\\(?: \\|$\\)" type
) nil t
))
2795 ;; Build appropriate parser. `verse-block' type
2796 ;; elements require an additional argument, so they
2797 ;; must be treated separately.
2798 (if (string= "VERSE" type
)
2799 (org-element-verse-block-parser raw-secondary-p
)
2803 "org-element-%s-parser"
2805 org-element-non-recursive-block-alist
))))))
2806 (org-element-paragraph-parser)))))
2808 ((org-at-heading-p) (org-element-inlinetask-parser raw-secondary-p
))
2809 ;; LaTeX Environment or paragraph if incomplete.
2810 ((looking-at "^[ \t]*\\\\begin{")
2812 (re-search-forward "^[ \t]*\\\\end{[^}]*}[ \t]*" nil t
))
2813 (org-element-latex-environment-parser)
2814 (org-element-paragraph-parser)))
2816 ((looking-at org-property-start-re
)
2817 (if (save-excursion (re-search-forward org-property-end-re nil t
))
2818 (org-element-property-drawer-parser)
2819 (org-element-paragraph-parser)))
2820 ;; Recursive block, or paragraph if incomplete.
2821 ((looking-at "[ \t]*#\\+BEGIN_\\([-A-Za-z0-9]+\\)\\(?: \\|$\\)")
2822 (let ((type (upcase (match-string 1))))
2824 ((not (save-excursion
2826 (format "[ \t]*#\\+END_%s\\(?: \\|$\\)" type
) nil t
)))
2827 (org-element-paragraph-parser))
2828 ((string= type
"CENTER") (org-element-center-block-parser))
2829 ((string= type
"QUOTE") (org-element-quote-block-parser))
2830 (t (org-element-special-block-parser)))))
2832 ((looking-at org-drawer-regexp
)
2833 (if (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
))
2834 (org-element-drawer-parser)
2835 (org-element-paragraph-parser)))
2836 ((looking-at "[ \t]*:\\( \\|$\\)") (org-element-fixed-width-parser))
2838 ((looking-at org-babel-block-lob-one-liner-regexp
)
2839 (org-element-babel-call-parser))
2840 ;; Keyword, or paragraph if at an affiliated keyword.
2841 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
2842 (let ((key (upcase (match-string 1))))
2843 (if (or (string= key
"TBLFM")
2844 (member key org-element-affiliated-keywords
))
2845 (org-element-paragraph-parser)
2846 (org-element-keyword-parser))))
2847 ;; Footnote definition.
2848 ((looking-at org-footnote-definition-re
)
2849 (org-element-footnote-definition-parser))
2850 ;; Dynamic block or paragraph if incomplete.
2851 ((looking-at "[ \t]*#\\+BEGIN:\\(?: \\|$\\)")
2853 (re-search-forward "^[ \t]*#\\+END:\\(?: \\|$\\)" nil t
))
2854 (org-element-dynamic-block-parser)
2855 (org-element-paragraph-parser)))
2857 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)")
2858 (org-element-comment-parser))
2860 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
2861 (org-element-horizontal-rule-parser))
2863 ((org-at-table-p t
) (org-element-table-parser))
2865 ((looking-at (org-item-re))
2866 (if (eq special
'item
)
2867 (org-element-item-parser
2868 (or structure
(org-list-struct))
2870 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2871 ;; Default element: Paragraph.
2872 (t (org-element-paragraph-parser))))))
2875 ;; Most elements can have affiliated keywords. When looking for an
2876 ;; element beginning, we want to move before them, as they belong to
2877 ;; that element, and, in the meantime, collect information they give
2878 ;; into appropriate properties. Hence the following function.
2880 ;; Usage of optional arguments may not be obvious at first glance:
2882 ;; - TRANS-LIST is used to polish keywords names that have evolved
2883 ;; during Org history. In example, even though =result= and
2884 ;; =results= coexist, we want to have them under the same =result=
2885 ;; property. It's also true for "srcname" and "name", where the
2886 ;; latter seems to be preferred nowadays (thus the "name" property).
2888 ;; - CONSED allows to regroup multi-lines keywords under the same
2889 ;; property, while preserving their own identity. This is mostly
2890 ;; used for "attr_latex" and al.
2892 ;; - PARSED prepares a keyword value for export. This is useful for
2893 ;; "caption". Objects restrictions for such keywords are defined in
2894 ;; `org-element-string-restrictions'.
2896 ;; - DUALS is used to take care of keywords accepting a main and an
2897 ;; optional secondary values. For example "results" has its
2898 ;; source's name as the main value, and may have an hash string in
2899 ;; optional square brackets as the secondary one.
2901 ;; A keyword may belong to more than one category.
2903 (defconst org-element--affiliated-re
2904 (format "[ \t]*#\\+\\(%s\\):"
2907 (if (member keyword org-element-dual-keywords
)
2908 (format "\\(%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
2909 (regexp-quote keyword
))
2910 (regexp-quote keyword
)))
2911 org-element-affiliated-keywords
"\\|"))
2912 "Regexp matching any affiliated keyword.
2914 Keyword name is put in match group 1. Moreover, if keyword
2915 belongs to `org-element-dual-keywords', put the dual value in
2918 Don't modify it, set `org-element-affiliated-keywords' instead.")
2920 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
2921 consed parsed duals
)
2922 "Collect affiliated keywords before point.
2924 Optional argument KEY-RE is a regexp matching keywords, which
2925 puts matched keyword in group 1. It defaults to
2926 `org-element--affiliated-re'.
2928 TRANS-LIST is an alist where key is the keyword and value the
2929 property name it should be translated to, without the colons. It
2930 defaults to `org-element-keyword-translation-alist'.
2932 CONSED is a list of strings. Any keyword belonging to that list
2933 will have its value consed. The check is done after keyword
2934 translation. It defaults to `org-element-multiple-keywords'.
2936 PARSED is a list of strings. Any keyword member of this list
2937 will have its value parsed. The check is done after keyword
2938 translation. If a keyword is a member of both CONSED and PARSED,
2939 it's value will be a list of parsed strings. It defaults to
2940 `org-element-parsed-keywords'.
2942 DUALS is a list of strings. Any keyword member of this list can
2943 have two parts: one mandatory and one optional. Its value is
2944 a cons cell whose car is the former, and the cdr the latter. If
2945 a keyword is a member of both PARSED and DUALS, both values will
2946 be parsed. It defaults to `org-element-dual-keywords'.
2948 Return a list whose car is the position at the first of them and
2949 cdr a plist of keywords and values."
2951 (let ((case-fold-search t
)
2952 (key-re (or key-re org-element--affiliated-re
))
2953 (trans-list (or trans-list org-element-keyword-translation-alist
))
2954 (consed (or consed org-element-multiple-keywords
))
2955 (parsed (or parsed org-element-parsed-keywords
))
2956 (duals (or duals org-element-dual-keywords
))
2957 ;; RESTRICT is the list of objects allowed in parsed
2959 (restrict (cdr (assq 'keyword org-element-string-restrictions
)))
2962 (while (and (not (bobp))
2963 (progn (forward-line -
1) (looking-at key-re
)))
2964 (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
2965 ;; Apply translation to RAW-KWD. From there, KWD is
2966 ;; the official keyword.
2967 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
2968 ;; Find main value for any keyword.
2972 (buffer-substring-no-properties
2973 (match-end 0) (point-at-eol)))))
2974 ;; If KWD is a dual keyword, find its secondary
2975 ;; value. Maybe parse it.
2977 (and (member kwd duals
)
2978 (let ((sec (org-match-string-no-properties 3)))
2979 (if (or (not sec
) (not (member kwd parsed
))) sec
2980 (org-element-parse-secondary-string sec restrict
)))))
2981 ;; Attribute a property name to KWD.
2982 (kwd-sym (and kwd
(intern (concat ":" (downcase kwd
))))))
2983 ;; Now set final shape for VALUE.
2984 (when (member kwd parsed
)
2985 (setq value
(org-element-parse-secondary-string value restrict
)))
2986 (when (member kwd duals
)
2987 ;; VALUE is mandatory. Set it to nil if there is none.
2988 (setq value
(and value
(cons value dual-value
))))
2989 (when (member kwd consed
)
2990 (setq value
(cons value
(plist-get output kwd-sym
))))
2991 ;; Eventually store the new value in OUTPUT.
2992 (setq output
(plist-put output kwd-sym value
))))
2993 (unless (looking-at key-re
) (forward-line 1)))
2994 (list (point) output
))))
3000 ;; The two major functions here are `org-element-parse-buffer', which
3001 ;; parses Org syntax inside the current buffer, taking into account
3002 ;; region, narrowing, or even visibility if specified, and
3003 ;; `org-element-parse-secondary-string', which parses objects within
3006 ;; The (almost) almighty `org-element-map' allows to apply a function
3007 ;; on elements or objects matching some type, and accumulate the
3008 ;; resulting values. In an export situation, it also skips unneeded
3009 ;; parts of the parse tree.
3011 (defun org-element-parse-buffer (&optional granularity visible-only
)
3012 "Recursively parse the buffer and return structure.
3013 If narrowing is in effect, only parse the visible part of the
3016 Optional argument GRANULARITY determines the depth of the
3017 recursion. It can be set to the following symbols:
3019 `headline' Only parse headlines.
3020 `greater-element' Don't recurse into greater elements excepted
3021 headlines and sections. Thus, elements
3022 parsed are the top-level ones.
3023 `element' Parse everything but objects and plain text.
3024 `object' Parse the complete buffer (default).
3026 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3029 Assume buffer is in Org mode."
3031 (goto-char (point-min))
3032 (org-skip-whitespace)
3033 (nconc (list 'org-data nil
)
3034 (org-element-parse-elements
3035 (point-at-bol) (point-max)
3036 ;; Start is section mode so text before the first headline
3037 ;; belongs to a section.
3038 'section nil granularity visible-only nil
))))
3040 (defun org-element-parse-secondary-string (string restriction
)
3041 "Recursively parse objects in STRING and return structure.
3043 RESTRICTION, when non-nil, is a symbol limiting the object types
3044 that will be looked after."
3047 (org-element-parse-objects (point-min) (point-max) nil restriction
)))
3049 (defun org-element-map (data types fun
&optional info first-match no-recursion
)
3050 "Map a function on selected elements or objects.
3052 DATA is the parsed tree, as returned by, i.e,
3053 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3054 of elements or objects types. FUN is the function called on the
3055 matching element or object. It must accept one arguments: the
3056 element or object itself.
3058 When optional argument INFO is non-nil, it should be a plist
3059 holding export options. In that case, parts of the parse tree
3060 not exportable according to that property list will be skipped.
3062 When optional argument FIRST-MATCH is non-nil, stop at the first
3063 match for which FUN doesn't return nil, and return that value.
3065 Optional argument NO-RECURSION is a symbol or a list of symbols
3066 representing elements or objects types. `org-element-map' won't
3067 enter any recursive element or object whose type belongs to that
3068 list. Though, FUN can still be applied on them.
3070 Nil values returned from FUN do not appear in the results."
3071 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3072 (unless (listp types
) (setq types
(list types
)))
3073 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
3074 ;; Recursion depth is determined by --CATEGORY.
3077 ((loop for type in types
3078 always
(memq type org-element-greater-elements
))
3080 ((loop for type in types
3081 always
(memq type org-element-all-elements
))
3084 ;; --RESTRICTS is a list of element types whose secondary
3085 ;; string could possibly contain an object with a type among
3088 (and (eq --category
'objects
)
3089 (loop for el in org-element-secondary-value-alist
3091 (loop for o in types
3095 org-element-string-restrictions
))))
3101 ;; Recursively walk DATA. INFO, if non-nil, is
3102 ;; a plist holding contextual information.
3105 (unless (and info
(member --blob
(plist-get info
:ignore-list
)))
3106 (let ((--type (org-element-type --blob
)))
3107 ;; Check if TYPE is matching among TYPES. If so,
3108 ;; apply FUN to --BLOB and accumulate return value
3109 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3110 (when (memq --type types
)
3111 (let ((result (funcall fun --blob
)))
3112 (cond ((not result
))
3113 (first-match (throw 'first-match result
))
3114 (t (push result --acc
)))))
3115 ;; If --BLOB has a secondary string that can
3116 ;; contain objects with their type among TYPES,
3117 ;; look into that string.
3118 (when (memq --type --restricts
)
3123 ,@(org-element-property
3124 (cdr (assq --type org-element-secondary-value-alist
))
3126 ;; Now determine if a recursion into --BLOB is
3127 ;; possible. If so, do it.
3128 (unless (memq --type no-recursion
)
3129 (when (or (and (memq --type org-element-greater-elements
)
3130 (not (eq --category
'greater-elements
)))
3131 (and (memq --type org-element-all-elements
)
3132 (not (eq --category
'elements
)))
3133 (memq --type org-element-recursive-objects
))
3134 (funcall --walk-tree --blob
))))))
3135 (org-element-contents --data
))))))
3137 (funcall --walk-tree data
)
3138 ;; Return value in a proper order.
3141 ;; The following functions are internal parts of the parser.
3143 ;; The first one, `org-element-parse-elements' acts at the element's
3146 ;; The second one, `org-element-parse-objects' applies on all objects
3147 ;; of a paragraph or a secondary string. It uses
3148 ;; `org-element-get-candidates' to optimize the search of the next
3149 ;; object in the buffer.
3151 ;; More precisely, that function looks for every allowed object type
3152 ;; first. Then, it discards failed searches, keeps further matches,
3153 ;; and searches again types matched behind point, for subsequent
3154 ;; calls. Thus, searching for a given type fails only once, and every
3155 ;; object is searched only once at top level (but sometimes more for
3158 (defun org-element-parse-elements
3159 (beg end special structure granularity visible-only acc
)
3160 "Parse elements between BEG and END positions.
3162 SPECIAL prioritize some elements over the others. It can set to
3163 `quote-section', `section' or `item', which will focus search,
3164 respectively, on quote sections, sections and items. Moreover,
3165 when value is `item', STRUCTURE will be used as the current list
3168 GRANULARITY determines the depth of the recursion. See
3169 `org-element-parse-buffer' for more information.
3171 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3174 Elements are accumulated into ACC."
3177 (narrow-to-region beg end
)
3179 ;; When parsing only headlines, skip any text before first one.
3180 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3181 (org-with-limited-levels (outline-next-heading)))
3185 ;; 1. Item mode is active: point must be at an item. Parse it
3186 ;; directly, skipping `org-element-current-element'.
3187 (if (eq special
'item
)
3189 (org-element-item-parser
3191 (and granularity
(not (eq granularity
'object
))))))
3192 (goto-char (org-element-property :end element
))
3193 (org-element-parse-elements
3194 (org-element-property :contents-begin element
)
3195 (org-element-property :contents-end element
)
3196 nil structure granularity visible-only
(reverse element
)))
3197 ;; 2. When ITEM is nil, find current element's type and parse
3198 ;; it accordingly to its category.
3199 (let* ((element (org-element-current-element
3200 granularity special structure
))
3201 (type (org-element-type element
)))
3202 (goto-char (org-element-property :end element
))
3204 ;; Case 1. ELEMENT is a paragraph. Parse objects inside,
3205 ;; if GRANULARITY allows it.
3206 ((and (eq type
'paragraph
)
3207 (or (not granularity
) (eq granularity
'object
)))
3208 (org-element-parse-objects
3209 (org-element-property :contents-begin element
)
3210 (org-element-property :contents-end element
)
3211 (reverse element
) nil
))
3212 ;; Case 2. ELEMENT is recursive: parse it between
3213 ;; `contents-begin' and `contents-end'. Make sure
3214 ;; GRANULARITY allows the recursion, or ELEMENT is an
3215 ;; headline, in which case going inside is mandatory, in
3216 ;; order to get sub-level headings. If VISIBLE-ONLY is
3217 ;; true and element is hidden, do not recurse into it.
3218 ((and (memq type org-element-greater-elements
)
3219 (or (not granularity
)
3220 (memq granularity
'(element object
))
3221 (and (eq granularity
'greater-element
) (eq type
'section
))
3222 (eq type
'headline
))
3223 (not (and visible-only
3224 (org-element-property :hiddenp element
))))
3225 (org-element-parse-elements
3226 (org-element-property :contents-begin element
)
3227 (org-element-property :contents-end element
)
3228 ;; At a plain list, switch to item mode. At an
3229 ;; headline, switch to section mode. Any other
3230 ;; element turns off special modes.
3233 (headline (if (org-element-property :quotedp element
)
3236 (org-element-property :structure element
)
3237 granularity visible-only
(reverse element
)))
3238 ;; Case 3. Else, just accumulate ELEMENT.
3244 (defun org-element-parse-objects (beg end acc restriction
)
3245 "Parse objects between BEG and END and return recursive structure.
3247 Objects are accumulated in ACC.
3249 RESTRICTION, when non-nil, is a list of object types which are
3250 allowed in the current object."
3251 (let ((get-next-object
3254 ;; Return the parsing function associated to the nearest
3255 ;; object among list of candidates CAND.
3256 (let ((pos (apply #'min
(mapcar #'cdr cand
))))
3261 (format "org-element-%s-parser" (car (rassq pos cand
))))))))))
3262 next-object candidates
)
3265 (while (setq candidates
(org-element-get-next-object-candidates
3266 end restriction candidates
))
3267 (setq next-object
(funcall get-next-object candidates
))
3268 ;; 1. Text before any object. Untabify it.
3269 (let ((obj-beg (org-element-property :begin next-object
)))
3270 (unless (= (point) obj-beg
)
3271 (push (replace-regexp-in-string
3272 "\t" (make-string tab-width ?
)
3273 (buffer-substring-no-properties (point) obj-beg
))
3276 (let ((obj-end (org-element-property :end next-object
))
3277 (cont-beg (org-element-property :contents-begin next-object
)))
3278 (push (if (and (memq (car next-object
) org-element-recursive-objects
)
3280 ;; ... recursive. The CONT-BEG check is for
3281 ;; links, as some of them might not be recursive
3282 ;; (i.e. plain links).
3286 (org-element-property :contents-end next-object
))
3287 (org-element-parse-objects
3288 (point-min) (point-max) (reverse next-object
)
3289 ;; Restrict allowed objects. This is the
3290 ;; intersection of current restriction and next
3291 ;; object's restriction.
3293 (cdr (assq (car next-object
)
3294 org-element-object-restrictions
))))
3295 (if (not restriction
) new-restr
3297 (lambda (e) (and (memq e restriction
) e
))
3299 ;; ... not recursive.
3302 (goto-char obj-end
)))
3303 ;; 3. Text after last object. Untabify it.
3304 (unless (= (point) end
)
3305 (push (replace-regexp-in-string
3306 "\t" (make-string tab-width ?
)
3307 (buffer-substring-no-properties (point) end
))
3312 (defun org-element-get-next-object-candidates (limit restriction objects
)
3313 "Return an alist of candidates for the next object.
3315 LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
3316 the possible object types.
3318 Return value is an alist whose car is position and cdr the object
3319 type, as a string. There is an association for the closest
3320 object of each type within RESTRICTION when non-nil, or for every
3323 OBJECTS is the previous candidates alist."
3324 (let ((restriction (or restriction org-element-all-successors
))
3325 next-candidates types-to-search
)
3326 ;; If no previous result, search every object type in RESTRICTION.
3327 ;; Otherwise, keep potential candidates (old objects located after
3328 ;; point) and ask to search again those which had matched before.
3329 (if (not objects
) (setq types-to-search restriction
)
3331 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3332 (push obj next-candidates
)))
3334 ;; Call the appropriate "get-next" function for each type to
3335 ;; search and accumulate matches.
3338 (let* ((successor-fun
3340 (format "org-element-%s-successor"
3341 (or (cdr (assq type org-element-object-successor-alist
))
3343 (obj (funcall successor-fun limit
)))
3344 (and obj
(push obj next-candidates
))))
3351 ;;; Towards A Bijective Process
3353 ;; The parse tree obtained with `org-element-parse-buffer' is really
3354 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3355 ;; interpreted and expanded into a string with canonical Org
3356 ;; syntax. Hence `org-element-interpret-data'.
3358 ;; Data parsed from secondary strings, whose shape is slightly
3359 ;; different than the standard parse tree, is expanded with the
3360 ;; equivalent function `org-element-interpret-secondary'.
3362 ;; Both functions rely internally on
3363 ;; `org-element-interpret--affiliated-keywords'.
3365 (defun org-element-interpret-data (data &optional genealogy previous
)
3366 "Interpret a parse tree representing Org data.
3368 DATA is the parse tree to interpret.
3370 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3372 GENEALOGY is the list of its parents types.
3373 PREVIOUS is the type of the element or object at the same level
3376 Return Org syntax as a string."
3379 ;; BLOB can be an element, an object, a string, or nil.
3382 ((equal blob
"") nil
)
3383 ((stringp blob
) blob
)
3385 (let* ((type (org-element-type blob
))
3387 (if (eq type
'org-data
) 'identity
3388 (intern (format "org-element-%s-interpreter" type
))))
3391 ;; Full Org document.
3392 ((eq type
'org-data
)
3393 (org-element-interpret-data blob genealogy previous
))
3394 ;; Recursive objects.
3395 ((memq type org-element-recursive-objects
)
3396 (org-element-interpret-data
3397 blob
(cons type genealogy
) nil
))
3398 ;; Recursive elements.
3399 ((memq type org-element-greater-elements
)
3400 (org-element-normalize-string
3401 (org-element-interpret-data
3402 blob
(cons type genealogy
) nil
)))
3404 ((eq type
'paragraph
)
3406 (org-element-normalize-contents
3408 ;; When normalizing contents of an item,
3409 ;; ignore first line's indentation.
3411 (memq (car genealogy
)
3412 '(footnote-definiton item
))))))
3413 (org-element-interpret-data
3414 paragraph
(cons type genealogy
) nil
)))))
3415 (results (funcall interpreter blob contents
)))
3417 (setq previous type
)
3418 ;; Build white spaces. If no `:post-blank' property is
3419 ;; specified, assume its value is 0.
3420 (let ((post-blank (or (org-element-property :post-blank blob
) 0)))
3422 ((eq type
'org-data
) results
)
3423 ((memq type org-element-all-elements
)
3425 (org-element-interpret--affiliated-keywords blob
)
3426 (org-element-normalize-string results
)
3427 (make-string post-blank
10)))
3428 (t (concat results
(make-string post-blank
32)))))))))
3429 (org-element-contents data
) ""))
3431 (defun org-element-interpret-secondary (secondary)
3432 "Interpret SECONDARY string as Org syntax.
3434 SECONDARY-STRING is a nested list as returned by
3435 `org-element-parse-secondary-string'.
3437 Return interpreted string."
3438 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3439 (let ((s (if (listp secondary
) secondary
(list secondary
))))
3440 (org-element-interpret-data `(org-data nil
,@s
) nil nil
)))
3442 ;; Both functions internally use `org-element--affiliated-keywords'.
3444 (defun org-element-interpret--affiliated-keywords (element)
3445 "Return ELEMENT's affiliated keywords as Org syntax.
3446 If there is no affiliated keyword, return the empty string."
3447 (let ((keyword-to-org
3451 (when (member key org-element-dual-keywords
)
3452 (setq dual
(cdr value
) value
(car value
)))
3456 (org-element-interpret-secondary dual
)))
3458 (if (member key org-element-parsed-keywords
)
3459 (org-element-interpret-secondary value
)
3464 (let ((value (org-element-property (intern (concat ":" (downcase key
)))
3467 (if (member key org-element-multiple-keywords
)
3468 (mapconcat (lambda (line)
3469 (funcall keyword-to-org key line
))
3471 (funcall keyword-to-org key value
)))))
3472 ;; Remove translated keywords.
3476 (and (not (assoc key org-element-keyword-translation-alist
)) key
))
3477 org-element-affiliated-keywords
))
3480 ;; Because interpretation of the parse tree must return the same
3481 ;; number of blank lines between elements and the same number of white
3482 ;; space after objects, some special care must be given to white
3485 ;; The first function, `org-element-normalize-string', ensures any
3486 ;; string different from the empty string will end with a single
3487 ;; newline character.
3489 ;; The second function, `org-element-normalize-contents', removes
3490 ;; global indentation from the contents of the current element.
3492 (defun org-element-normalize-string (s)
3493 "Ensure string S ends with a single newline character.
3495 If S isn't a string return it unchanged. If S is the empty
3496 string, return it. Otherwise, return a new string with a single
3497 newline character at its end."
3499 ((not (stringp s
)) s
)
3501 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3502 (replace-match "\n" nil nil s
)))))
3504 (defun org-element-normalize-contents (element &optional ignore-first
)
3505 "Normalize plain text in ELEMENT's contents.
3507 ELEMENT must only contain plain text and objects.
3509 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3510 indentation to compute maximal common indentation.
3512 Return the normalized element that is element with global
3513 indentation removed from its contents. The function assumes that
3514 indentation is not done with TAB characters."
3518 ;; Return list of indentations within BLOB. This is done by
3519 ;; walking recursively BLOB and updating IND-LIST along the
3520 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3521 ;; been seen yet. It is required as this string is the only
3522 ;; one whose indentation doesn't happen after a newline
3524 (lambda (blob first-flag
)
3527 (when (and first-flag
(stringp object
))
3528 (setq first-flag nil
)
3529 (string-match "\\`\\( *\\)" object
)
3530 (let ((len (length (match-string 1 object
))))
3531 ;; An indentation of zero means no string will be
3532 ;; modified. Quit the process.
3533 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
3534 (push len ind-list
))))
3538 (while (string-match "\n\\( *\\)" object start
)
3539 (setq start
(match-end 0))
3540 (push (length (match-string 1 object
)) ind-list
))))
3541 ((memq (org-element-type object
) org-element-recursive-objects
)
3542 (funcall collect-inds object first-flag
))))
3543 (org-element-contents blob
))))))
3544 ;; Collect indentation list in ELEMENT. Possibly remove first
3545 ;; value if IGNORE-FIRST is non-nil.
3546 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
3547 (if (not ind-list
) element
3548 ;; Build ELEMENT back, replacing each string with the same
3549 ;; string minus common indentation.
3552 (lambda (blob mci first-flag
)
3553 ;; Return BLOB with all its strings indentation
3554 ;; shortened from MCI white spaces. FIRST-FLAG is
3555 ;; non-nil when the first string hasn't been seen
3558 (list (org-element-type blob
) (nth 1 blob
))
3561 (when (and first-flag
(stringp object
))
3562 (setq first-flag nil
)
3564 (replace-regexp-in-string
3565 (format "\\` \\{%d\\}" mci
) "" object
)))
3568 (replace-regexp-in-string
3569 (format "\n \\{%d\\}" mci
) "\n" object
))
3570 ((memq (org-element-type object
) org-element-recursive-objects
)
3571 (funcall build object mci first-flag
))
3573 (org-element-contents blob
)))))))
3574 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
3580 ;; The first move is to implement a way to obtain the smallest element
3581 ;; containing point. This is the job of `org-element-at-point'. It
3582 ;; basically jumps back to the beginning of section containing point
3583 ;; and moves, element after element, with
3584 ;; `org-element-current-element' until the container is found.
3586 ;; Note: When using `org-element-at-point', secondary values are never
3587 ;; parsed since the function focuses on elements, not on objects.
3589 (defun org-element-at-point (&optional keep-trail
)
3590 "Determine closest element around point.
3592 Return value is a list like (TYPE PROPS) where TYPE is the type
3593 of the element and PROPS a plist of properties associated to the
3594 element. Possible types are defined in
3595 `org-element-all-elements'.
3597 As a special case, if point is at the very beginning of a list or
3598 sub-list, element returned will be that list instead of the first
3601 If optional argument KEEP-TRAIL is non-nil, the function returns
3602 a list of of elements leading to element at point. The list's
3603 CAR is always the element at point. Its last item will be the
3604 element's parent, unless element was either the first in its
3605 section (in which case the last item in the list is the first
3606 element of section) or an headline (in which case the list
3607 contains that headline as its single element). Elements
3608 in-between, if any, are siblings of the element at point."
3609 (org-with-wide-buffer
3610 ;; If at an headline, parse it. It is the sole element that
3611 ;; doesn't require to know about context. Be sure to disallow
3612 ;; secondary string parsing, though.
3613 (if (org-with-limited-levels (org-at-heading-p))
3614 (if (not keep-trail
) (org-element-headline-parser t
)
3615 (list (org-element-headline-parser t
)))
3616 ;; Otherwise move at the beginning of the section containing
3618 (let ((origin (point)) element type item-flag trail struct prevs
)
3619 (org-with-limited-levels
3620 (if (org-before-first-heading-p) (goto-char (point-min))
3621 (org-back-to-heading)
3623 (org-skip-whitespace)
3625 ;; Starting parsing successively each element with
3626 ;; `org-element-current-element'. Skip those ending before
3627 ;; original position.
3630 (setq element
(org-element-current-element 'element item-flag struct
)
3632 (when keep-trail
(push element trail
))
3634 ;; 1. Skip any element ending before point or at point.
3635 ((let ((end (org-element-property :end element
)))
3636 (when (<= end origin
)
3637 (if (> (point-max) end
) (goto-char end
)
3638 (throw 'exit
(or trail element
))))))
3639 ;; 2. An element containing point is always the element at
3641 ((not (memq type org-element-greater-elements
))
3642 (throw 'exit
(if keep-trail trail element
)))
3643 ;; 3. At a plain list.
3644 ((eq type
'plain-list
)
3645 (setq struct
(org-element-property :structure element
)
3646 prevs
(or prevs
(org-list-prevs-alist struct
)))
3647 (let ((beg (org-element-property :contents-begin element
)))
3648 (if (= beg origin
) (throw 'exit
(or trail element
))
3649 ;; Find the item at this level containing ORIGIN.
3650 (let ((items (org-list-get-all-items beg struct prevs
)))
3656 ;; Item ends before point: skip it.
3657 ((<= (org-list-get-item-end pos struct
) origin
))
3658 ;; Item contains point: store is in PARENT.
3659 ((<= pos origin
) (setq parent pos
))
3660 ;; We went too far: return PARENT.
3661 (t (throw 'local nil
)))) items
))
3662 ;; No parent: no item contained point, though
3663 ;; the plain list does. Point is in the blank
3664 ;; lines after the list: return plain list.
3665 (if (not parent
) (throw 'exit
(or trail element
))
3666 (setq item-flag
'item
)
3667 (goto-char parent
)))))))
3668 ;; 4. At any other greater element type, if point is
3669 ;; within contents, move into it. Otherwise, return
3672 (when (eq type
'item
) (setq item-flag nil
))
3673 (let ((beg (org-element-property :contents-begin element
))
3674 (end (org-element-property :contents-end element
)))
3675 (if (or (> beg origin
) (< end origin
))
3676 (throw 'exit
(or trail element
))
3677 ;; Reset trail, since we found a parent.
3678 (when keep-trail
(setq trail
(list element
)))
3679 (narrow-to-region beg end
)
3680 (goto-char beg
)))))))))))
3683 ;; Once the local structure around point is well understood, it's easy
3684 ;; to implement some replacements for `forward-paragraph'
3685 ;; `backward-paragraph', namely `org-element-forward' and
3686 ;; `org-element-backward'.
3688 ;; Also, `org-transpose-elements' mimics the behaviour of
3689 ;; `transpose-words', at the element's level, whereas
3690 ;; `org-element-drag-forward', `org-element-drag-backward', and
3691 ;; `org-element-up' generalize, respectively, functions
3692 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3694 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3695 ;; smartly remove global indentation from buffer, making it possible
3696 ;; to use Org indent mode on a file created with hard indentation.
3698 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3699 ;; internally by some of the previously cited tools.
3701 (defsubst org-element-nested-p
(elem-A elem-B
)
3702 "Non-nil when elements ELEM-A and ELEM-B are nested."
3703 (let ((beg-A (org-element-property :begin elem-A
))
3704 (beg-B (org-element-property :begin elem-B
))
3705 (end-A (org-element-property :end elem-A
))
3706 (end-B (org-element-property :end elem-B
)))
3707 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
3708 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
3710 (defun org-element-swap-A-B (elem-A elem-B
)
3711 "Swap elements ELEM-A and ELEM-B.
3713 Leave point at the end of ELEM-A."
3714 (goto-char (org-element-property :begin elem-A
))
3715 (let* ((beg-A (org-element-property :begin elem-A
))
3716 (end-A (save-excursion
3717 (goto-char (org-element-property :end elem-A
))
3718 (skip-chars-backward " \r\t\n")
3720 (beg-B (org-element-property :begin elem-B
))
3721 (end-B (save-excursion
3722 (goto-char (org-element-property :end elem-B
))
3723 (skip-chars-backward " \r\t\n")
3725 (body-A (buffer-substring beg-A end-A
))
3726 (body-B (delete-and-extract-region beg-B end-B
)))
3730 (delete-region beg-A end-A
)
3732 (goto-char (org-element-property :end elem-B
))))
3734 (defun org-element-backward ()
3735 "Move backward by one element.
3736 Move to the previous element at the same level, when possible."
3738 (if (save-excursion (skip-chars-backward " \r\t\n") (bobp))
3739 (error "Cannot move further up")
3740 (let* ((trail (org-element-at-point 'keep-trail
))
3741 (element (car trail
))
3742 (beg (org-element-property :begin element
)))
3743 ;; Move to beginning of current element if point isn't there.
3744 (if (/= (point) beg
) (goto-char beg
)
3745 (let ((type (org-element-type element
)))
3747 ;; At an headline: move to previous headline at the same
3748 ;; level, a parent, or BOB.
3749 ((eq type
'headline
)
3750 (let ((dest (save-excursion (org-backward-same-level 1) (point))))
3751 (if (= (point-min) dest
) (error "Cannot move further up")
3753 ;; At an item: try to move to the previous item, if any.
3754 ((and (eq type
'item
)
3755 (let* ((struct (org-element-property :structure element
))
3756 (prev (org-list-get-prev-item
3757 beg struct
(org-list-prevs-alist struct
))))
3758 (when prev
(goto-char prev
)))))
3759 ;; In any other case, find the previous element in the
3760 ;; trail and move to its beginning. If no previous element
3761 ;; can be found, move to headline.
3762 (t (let ((prev (nth 1 trail
)))
3763 (if prev
(goto-char (org-element-property :begin prev
))
3764 (org-back-to-heading))))))))))
3766 (defun org-element-drag-backward ()
3767 "Drag backward element at point."
3769 (let* ((pos (point))
3770 (elem (org-element-at-point)))
3771 (when (= (progn (goto-char (point-min))
3772 (org-skip-whitespace)
3774 (org-element-property :end elem
))
3775 (error "Cannot drag element backward"))
3776 (goto-char (org-element-property :begin elem
))
3777 (org-element-backward)
3778 (let ((prev-elem (org-element-at-point)))
3779 (when (or (org-element-nested-p elem prev-elem
)
3780 (and (eq (org-element-type elem
) 'headline
)
3781 (not (eq (org-element-type prev-elem
) 'headline
))))
3783 (error "Cannot drag element backward"))
3784 ;; Compute new position of point: it's shifted by PREV-ELEM
3786 (let ((size-prev (- (org-element-property :end prev-elem
)
3787 (org-element-property :begin prev-elem
))))
3788 (org-element-swap-A-B prev-elem elem
)
3789 (goto-char (- pos size-prev
))))))
3791 (defun org-element-drag-forward ()
3792 "Move forward element at point."
3794 (let* ((pos (point))
3795 (elem (org-element-at-point)))
3796 (when (= (point-max) (org-element-property :end elem
))
3797 (error "Cannot drag element forward"))
3798 (goto-char (org-element-property :end elem
))
3799 (let ((next-elem (org-element-at-point)))
3800 (when (or (org-element-nested-p elem next-elem
)
3801 (and (eq (org-element-type next-elem
) 'headline
)
3802 (not (eq (org-element-type elem
) 'headline
))))
3804 (error "Cannot drag element forward"))
3805 ;; Compute new position of point: it's shifted by NEXT-ELEM
3806 ;; body's length (without final blanks) and by the length of
3807 ;; blanks between ELEM and NEXT-ELEM.
3808 (let ((size-next (- (save-excursion
3809 (goto-char (org-element-property :end next-elem
))
3810 (skip-chars-backward " \r\t\n")
3813 (org-element-property :begin next-elem
)))
3814 (size-blank (- (org-element-property :end elem
)
3816 (goto-char (org-element-property :end elem
))
3817 (skip-chars-backward " \r\t\n")
3820 (org-element-swap-A-B elem next-elem
)
3821 (goto-char (+ pos size-next size-blank
))))))
3823 (defun org-element-forward ()
3824 "Move forward by one element.
3825 Move to the next element at the same level, when possible."
3827 (if (eobp) (error "Cannot move further down")
3828 (let* ((trail (org-element-at-point 'keep-trail
))
3829 (element (car trail
))
3830 (type (org-element-type element
))
3831 (end (org-element-property :end element
)))
3833 ;; At an headline, move to next headline at the same level.
3834 ((eq type
'headline
) (goto-char end
))
3835 ;; At an item. Move to the next item, if possible.
3836 ((and (eq type
'item
)
3837 (let* ((struct (org-element-property :structure element
))
3838 (prevs (org-list-prevs-alist struct
))
3839 (beg (org-element-property :begin element
))
3840 (next-item (org-list-get-next-item beg struct prevs
)))
3841 (when next-item
(goto-char next-item
)))))
3842 ;; In any other case, move to element's end, unless this
3843 ;; position is also the end of its parent's contents, in which
3844 ;; case, directly jump to parent's end.
3847 ;; Determine if TRAIL contains the real parent of ELEMENT.
3848 (and (> (length trail
) 1)
3849 (let* ((parent-candidate (car (last trail
))))
3850 (and (memq (org-element-type parent-candidate
)
3851 org-element-greater-elements
)
3852 (>= (org-element-property
3853 :contents-end parent-candidate
) end
)
3854 parent-candidate
)))))
3855 (cond ((not parent
) (goto-char end
))
3856 ((= (org-element-property :contents-end parent
) end
)
3857 (goto-char (org-element-property :end parent
)))
3858 (t (goto-char end
)))))))))
3860 (defun org-element-mark-element ()
3861 "Put point at beginning of this element, mark at end.
3863 Interactively, if this command is repeated or (in Transient Mark
3864 mode) if the mark is active, it marks the next element after the
3865 ones already marked."
3867 (let (deactivate-mark)
3868 (if (or (and (eq last-command this-command
) (mark t
))
3869 (and transient-mark-mode mark-active
))
3873 (goto-char (org-element-property :end
(org-element-at-point)))))
3874 (let ((element (org-element-at-point)))
3876 (push-mark (org-element-property :end element
) t t
)
3877 (goto-char (org-element-property :begin element
))))))
3879 (defun org-narrow-to-element ()
3880 "Narrow buffer to current element."
3882 (let ((elem (org-element-at-point)))
3884 ((eq (car elem
) 'headline
)
3886 (org-element-property :begin elem
)
3887 (org-element-property :end elem
)))
3888 ((memq (car elem
) org-element-greater-elements
)
3890 (org-element-property :contents-begin elem
)
3891 (org-element-property :contents-end elem
)))
3894 (org-element-property :begin elem
)
3895 (org-element-property :end elem
))))))
3897 (defun org-transpose-elements ()
3898 "Transpose current and previous elements, keeping blank lines between.
3899 Point is moved after both elements."
3901 (org-skip-whitespace)
3903 (cur (org-element-at-point)))
3904 (when (= (save-excursion (goto-char (point-min))
3905 (org-skip-whitespace)
3907 (org-element-property :begin cur
))
3908 (error "No previous element"))
3909 (goto-char (org-element-property :begin cur
))
3911 (let ((prev (org-element-at-point)))
3912 (when (org-element-nested-p cur prev
)
3914 (error "Cannot transpose nested elements"))
3915 (org-element-swap-A-B prev cur
))))
3917 (defun org-element-unindent-buffer ()
3918 "Un-indent the visible part of the buffer.
3919 Relative indentation \(between items, inside blocks, etc.\) isn't
3922 (unless (eq major-mode
'org-mode
)
3923 (error "Cannot un-indent a buffer not in Org mode"))
3924 (let* ((parse-tree (org-element-parse-buffer 'greater-element
))
3925 unindent-tree
; For byte-compiler.
3929 (mapc (lambda (element)
3930 (if (eq (org-element-type element
) 'headline
)
3931 (funcall unindent-tree
3932 (org-element-contents element
))
3936 (org-element-property :begin element
)
3937 (org-element-property :end element
))
3938 (org-do-remove-indentation)))))
3939 (reverse contents
))))))
3940 (funcall unindent-tree
(org-element-contents parse-tree
))))
3942 (defun org-element-up ()
3943 "Move to upper element."
3946 ((bobp) (error "No surrounding element"))
3947 ((org-with-limited-levels (org-at-heading-p))
3948 (or (org-up-heading-safe) (error "No surronding element")))
3950 (let* ((trail (org-element-at-point 'keep-trail
))
3951 (element (car trail
))
3952 (type (org-element-type element
)))
3954 ;; At an item, with a parent in the list: move to that parent.
3955 ((and (eq type
'item
)
3956 (let* ((beg (org-element-property :begin element
))
3957 (struct (org-element-property :structure element
))
3958 (parents (org-list-parents-alist struct
))
3959 (parentp (org-list-get-parent beg struct parents
)))
3960 (and parentp
(goto-char parentp
)))))
3961 ;; Determine parent in the trail.
3964 (and (> (length trail
) 1)
3965 (let ((parentp (car (last trail
))))
3966 (and (memq (org-element-type parentp
)
3967 org-element-greater-elements
)
3968 (>= (org-element-property :contents-end parentp
)
3969 (org-element-property :end element
))
3972 ;; When parent is found move to its beginning.
3973 (parent (goto-char (org-element-property :begin parent
)))
3974 ;; If no parent was found, move to headline above, if any
3975 ;; or return an error.
3976 ((org-before-first-heading-p) (error "No surrounding element"))
3977 (t (org-back-to-heading))))))))))
3979 (defun org-element-down ()
3980 "Move to inner element."
3982 (let ((element (org-element-at-point)))
3984 ((eq (org-element-type element
) 'plain-list
)
3986 ((memq (org-element-type element
) org-element-greater-elements
)
3987 ;; If contents are hidden, first disclose them.
3988 (when (org-element-property :hiddenp element
) (org-cycle))
3989 (goto-char (org-element-property :contents-begin element
)))
3990 (t (error "No inner element")))))
3993 (provide 'org-element
)
3994 ;;; org-element.el ends here