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 (`item''s excepted) accepts no argument and represents the
115 ;; element or object as the list described above. An interpreter
116 ;; accepts two arguments: the list representation of the element or
117 ;; object, and its contents. The latter may be nil, depending on the
118 ;; element or object considered. It returns the appropriate Org
119 ;; syntax, as a string.
121 ;; Parsing functions must follow the naming convention:
122 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
123 ;; defined in `org-element-greater-elements'.
125 ;; Similarly, interpreting functions must follow the naming
126 ;; convention: org-element-TYPE-interpreter.
128 ;; With the exception of `headline' and `item' types, greater elements
129 ;; cannot contain other greater elements of their own type.
131 ;; Beside implementing a parser and an interpreter, adding a new
132 ;; greater element requires to tweak `org-element-current-element'.
133 ;; Moreover, the newly defined type must be added to both
134 ;; `org-element-all-elements' and `org-element-greater-elements'.
139 (defun org-element-center-block-parser ()
140 "Parse a center block.
142 Return a list whose car is `center-block' and cdr is a plist
143 containing `:begin', `:end', `:hiddenp', `:contents-begin',
144 `:contents-end' and `:post-blank' keywords.
146 Assume point is at beginning or end of the block."
148 (let* ((case-fold-search t
)
152 (concat "^[ \t]*#\\+begin_center") nil t
)
153 (org-element-collect-affiliated-keywords)))
154 (begin (car keywords
))
155 (contents-begin (progn (forward-line) (point)))
156 (hidden (org-truely-invisible-p))
157 (contents-end (progn (re-search-forward
158 (concat "^[ \t]*#\\+end_center") nil t
)
160 (pos-before-blank (progn (forward-line) (point)))
161 (end (progn (org-skip-whitespace)
162 (if (eobp) (point) (point-at-bol)))))
167 :contents-begin
,contents-begin
168 :contents-end
,contents-end
169 :post-blank
,(count-lines pos-before-blank end
)
170 ,@(cadr keywords
))))))
172 (defun org-element-center-block-interpreter (center-block contents
)
173 "Interpret CENTER-BLOCK element as Org syntax.
174 CONTENTS is the contents of the element."
175 (format "#+begin_center\n%s#+end_center" contents
))
180 (defun org-element-drawer-parser ()
183 Return a list whose car is `drawer' and cdr is a plist containing
184 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
185 `:contents-end' and `:post-blank' keywords.
187 Assume point is at beginning of drawer."
189 (let* ((case-fold-search t
)
190 (name (progn (looking-at org-drawer-regexp
)
191 (org-match-string-no-properties 1)))
192 (keywords (org-element-collect-affiliated-keywords))
193 (begin (car keywords
))
194 (contents-begin (progn (forward-line) (point)))
195 (hidden (org-truely-invisible-p))
196 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
198 (pos-before-blank (progn (forward-line) (point)))
199 (end (progn (org-skip-whitespace)
200 (if (eobp) (point) (point-at-bol)))))
206 :contents-begin
,contents-begin
207 :contents-end
,contents-end
208 :post-blank
,(count-lines pos-before-blank end
)
209 ,@(cadr keywords
))))))
211 (defun org-element-drawer-interpreter (drawer contents
)
212 "Interpret DRAWER element as Org syntax.
213 CONTENTS is the contents of the element."
214 (format ":%s:\n%s:END:"
215 (org-element-property :drawer-name drawer
)
221 (defun org-element-dynamic-block-parser ()
222 "Parse a dynamic block.
224 Return a list whose car is `dynamic-block' and cdr is a plist
225 containing `:block-name', `:begin', `:end', `:hiddenp',
226 `:contents-begin', `:contents-end', `:arguments' and
227 `:post-blank' keywords.
229 Assume point is at beginning of dynamic block."
231 (let* ((case-fold-search t
)
232 (name (progn (looking-at org-dblock-start-re
)
233 (org-match-string-no-properties 1)))
234 (arguments (org-match-string-no-properties 3))
235 (keywords (org-element-collect-affiliated-keywords))
236 (begin (car keywords
))
237 (contents-begin (progn (forward-line) (point)))
238 (hidden (org-truely-invisible-p))
239 (contents-end (progn (re-search-forward org-dblock-end-re nil t
)
241 (pos-before-blank (progn (forward-line) (point)))
242 (end (progn (org-skip-whitespace)
243 (if (eobp) (point) (point-at-bol)))))
248 :arguments
,arguments
250 :contents-begin
,contents-begin
251 :contents-end
,contents-end
252 :post-blank
,(count-lines pos-before-blank end
)
253 ,@(cadr keywords
))))))
255 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
256 "Interpret DYNAMIC-BLOCK element as Org syntax.
257 CONTENTS is the contents of the element."
258 (format "#+BEGIN: %s%s\n%s#+END:"
259 (org-element-property :block-name dynamic-block
)
260 (let ((args (org-element-property :arguments dynamic-block
)))
261 (and arg
(concat " " args
)))
265 ;;;; Footnote Definition
267 (defun org-element-footnote-definition-parser ()
268 "Parse a footnote definition.
270 Return a list whose car is `footnote-definition' and cdr is
271 a plist containing `:label', `:begin' `:end', `:contents-begin',
272 `:contents-end' and `:post-blank' keywords."
274 (let* ((f-def (org-footnote-at-definition-p))
276 (keywords (progn (goto-char (nth 1 f-def
))
277 (org-element-collect-affiliated-keywords)))
278 (begin (car keywords
))
279 (contents-begin (progn (looking-at (concat "\\[" label
"\\]"))
280 (goto-char (match-end 0))
281 (org-skip-whitespace)
283 (end (goto-char (nth 2 f-def
)))
284 (contents-end (progn (skip-chars-backward " \r\t\n")
287 `(footnote-definition
291 :contents-begin
,contents-begin
292 :contents-end
,contents-end
293 :post-blank
,(count-lines contents-end end
)
294 ,@(cadr keywords
))))))
296 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
297 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
298 CONTENTS is the contents of the footnote-definition."
299 (concat (format "[%s]" (org-element-property :label footnote-definition
))
306 (defun org-element-headline-parser ()
309 Return a list whose car is `headline' and cdr is a plist
310 containing `:raw-value', `:title', `:begin', `:end',
311 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
312 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
313 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
314 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
317 The plist also contains any property set in the property drawer,
318 with its name in lowercase, the underscores replaced with hyphens
319 and colons at the beginning (i.e. `:custom-id').
321 Assume point is at beginning of the headline."
323 (let* ((components (org-heading-components))
324 (level (nth 1 components
))
325 (todo (nth 2 components
))
327 (and todo
(if (member todo org-done-keywords
) 'done
'todo
)))
328 (tags (nth 5 components
))
329 (raw-value (nth 4 components
))
331 (let ((case-fold-search nil
))
332 (string-match (format "^%s +" org-quote-string
) raw-value
)))
334 (let ((case-fold-search nil
))
335 (string-match (format "^%s +" org-comment-string
) raw-value
)))
338 (let ((case-fold-search nil
))
339 (string-match (format ":%s:" org-archive-tag
) tags
))))
340 (footnote-section-p (and org-footnote-section
341 (string= org-footnote-section raw-value
)))
342 (standard-props (let (plist)
345 (let ((p-name (downcase (car p
))))
346 (while (string-match "_" p-name
)
348 (replace-match "-" nil nil p-name
)))
349 (setq p-name
(intern (concat ":" p-name
)))
351 (plist-put plist p-name
(cdr p
)))))
352 (org-entry-properties nil
'standard
))
354 (time-props (org-entry-properties nil
'special
"CLOCK"))
355 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
356 (deadline (cdr (assoc "DEADLINE" time-props
)))
357 (clock (cdr (assoc "CLOCK" time-props
)))
358 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
360 (pos-after-head (save-excursion (forward-line) (point)))
361 (contents-begin (save-excursion (forward-line)
362 (org-skip-whitespace)
363 (if (eobp) (point) (point-at-bol))))
364 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
365 (end (progn (goto-char (org-end-of-subtree t t
))))
366 (contents-end (progn (skip-chars-backward " \r\t\n")
370 ;; Clean RAW-VALUE from any quote or comment string.
371 (when (or quotedp commentedp
)
373 (replace-regexp-in-string
374 (concat "\\(" org-quote-string
"\\|" org-comment-string
"\\) +")
377 ;; Clean TAGS from archive tag, if any.
380 (and (not (string= tags
(format ":%s:" org-archive-tag
)))
381 (replace-regexp-in-string
382 (concat org-archive-tag
":") "" tags
)))
383 (when (string= tags
":") (setq tags nil
)))
385 (setq title
(org-element-parse-secondary-string
387 (cdr (assq 'headline org-element-string-restrictions
))))
389 (:raw-value
,raw-value
393 :pre-blank
,(count-lines pos-after-head contents-begin
)
395 :contents-begin
,contents-begin
396 :contents-end
,contents-end
398 :priority
,(nth 3 components
)
401 :todo-type
,todo-type
402 :scheduled
,scheduled
404 :timestamp
,timestamp
406 :post-blank
,(count-lines contents-end end
)
407 :footnote-section-p
,footnote-section-p
408 :archivedp
,archivedp
409 :commentedp
,commentedp
411 ,@standard-props
)))))
413 (defun org-element-headline-interpreter (headline contents
)
414 "Interpret HEADLINE element as Org syntax.
415 CONTENTS is the contents of the element."
416 (let* ((level (org-element-property :level headline
))
417 (todo (org-element-property :todo-keyword headline
))
418 (priority (org-element-property :priority headline
))
419 (title (org-element-property :raw-value headline
))
420 (tags (let ((tag-string (org-element-property :tags headline
))
421 (archivedp (org-element-property :archivedp headline
)))
423 ((and (not tag-string
) archivedp
)
424 (format ":%s:" org-archive-tag
))
425 (archivedp (concat ":" org-archive-tag tag-string
))
427 (commentedp (org-element-property :commentedp headline
))
428 (quotedp (org-element-property :quotedp headline
))
429 (pre-blank (org-element-property :pre-blank headline
))
430 (heading (concat (make-string level ?
*)
431 (and todo
(concat " " todo
))
432 (and quotedp
(concat " " org-quote-string
))
433 (and commentedp
(concat " " org-comment-string
))
434 (and priority
(concat " " priority
))
435 (cond ((and org-footnote-section
436 (org-element-property
437 :footnote-section-p headline
))
438 (concat " " org-footnote-section
))
439 (title (concat " " title
)))))
442 (let ((tags-len (length tags
)))
445 ((zerop org-tags-column
) (1+ tags-len
))
446 ((< org-tags-column
0)
447 (max (- (+ org-tags-column
(length heading
)))
449 (t (max (+ (- org-tags-column
(length heading
))
451 (1+ tags-len
)))))))))
452 (concat heading
(and tags
(format tags-fmt tags
))
453 (make-string (1+ pre-blank
) 10)
459 (defun org-element-inlinetask-parser ()
460 "Parse an inline task.
462 Return a list whose car is `inlinetask' and cdr is a plist
463 containing `:raw-value', `:title', `:begin', `:end', `:hiddenp',
464 `:contents-begin' and `:contents-end', `:level', `:priority',
465 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
466 `:scheduled', `:deadline', `:timestamp', `:clock' and
467 `:post-blank' keywords.
469 The plist also contains any property set in the property drawer,
470 with its name in lowercase, the underscores replaced with hyphens
471 and colons at the beginning (i.e. `:custom-id').
473 Assume point is at beginning of the inline task."
475 (let* ((keywords (org-element-collect-affiliated-keywords))
476 (begin (car keywords
))
477 (components (org-heading-components))
478 (todo (nth 2 components
))
480 (if (member todo org-done-keywords
) 'done
'todo
)))
481 (raw-value (nth 4 components
))
482 (standard-props (let (plist)
485 (let ((p-name (downcase (car p
))))
486 (while (string-match "_" p-name
)
488 (replace-match "-" nil nil p-name
)))
489 (setq p-name
(intern (concat ":" p-name
)))
491 (plist-put plist p-name
(cdr p
)))))
492 (org-entry-properties nil
'standard
))
494 (time-props (org-entry-properties nil
'special
"CLOCK"))
495 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
496 (deadline (cdr (assoc "DEADLINE" time-props
)))
497 (clock (cdr (assoc "CLOCK" time-props
)))
498 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
499 (title (org-element-parse-secondary-string
501 (cdr (assq 'inlinetask org-element-string-restrictions
))))
502 (contents-begin (save-excursion (forward-line) (point)))
503 (hidden (org-truely-invisible-p))
504 (pos-before-blank (org-inlinetask-goto-end))
505 ;; In the case of a single line task, CONTENTS-BEGIN and
506 ;; CONTENTS-END might overlap.
507 (contents-end (max contents-begin
508 (save-excursion (forward-line -
1) (point))))
509 (end (progn (org-skip-whitespace)
510 (if (eobp) (point) (point-at-bol)))))
512 (:raw-value
,raw-value
516 :hiddenp
,(and (> contents-end contents-begin
) hidden
)
517 :contents-begin
,contents-begin
518 :contents-end
,contents-end
519 :level
,(nth 1 components
)
520 :priority
,(nth 3 components
)
521 :tags
,(nth 5 components
)
523 :todo-type
,todo-type
524 :scheduled
,scheduled
526 :timestamp
,timestamp
528 :post-blank
,(count-lines pos-before-blank end
)
530 ,@(cadr keywords
))))))
532 (defun org-element-inlinetask-interpreter (inlinetask contents
)
533 "Interpret INLINETASK element as Org syntax.
534 CONTENTS is the contents of inlinetask."
535 (let* ((level (org-element-property :level inlinetask
))
536 (todo (org-element-property :todo-keyword inlinetask
))
537 (priority (org-element-property :priority inlinetask
))
538 (title (org-element-property :raw-value inlinetask
))
539 (tags (org-element-property :tags inlinetask
))
540 (task (concat (make-string level ?
*)
541 (and todo
(concat " " todo
))
542 (and priority
(concat " " priority
))
543 (and title
(concat " " title
))))
548 ((zerop org-tags-column
) 1)
549 ((< 0 org-tags-column
)
550 (max (+ org-tags-column
554 (t (max (- org-tags-column
(length inlinetask
))
556 (concat inlinetask
(and tags
(format tags-fmt tags
) "\n" contents
))))
561 (defun org-element-item-parser (struct)
564 STRUCT is the structure of the plain list.
566 Return a list whose car is `item' and cdr is a plist containing
567 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
568 `:checkbox', `:counter', `:tag', `:raw-tag', `:structure',
569 `:hiddenp' and `:post-blank' keywords.
571 Assume point is at the beginning of the item."
574 (let* ((begin (point))
575 (bullet (org-list-get-bullet (point) struct
))
576 (checkbox (let ((box (org-list-get-checkbox begin struct
)))
577 (cond ((equal "[ ]" box
) 'off
)
578 ((equal "[X]" box
) 'on
)
579 ((equal "[-]" box
) 'trans
))))
580 (counter (let ((c (org-list-get-counter begin struct
)))
583 ((string-match "[A-Za-z]" c
)
584 (- (string-to-char (upcase (match-string 0 c
)))
586 ((string-match "[0-9]+" c
)
587 (string-to-number (match-string 0 c
))))))
588 (raw-tag (org-list-get-tag begin struct
))
590 (org-element-parse-secondary-string
592 (cdr (assq 'item org-element-string-restrictions
)))))
593 (end (org-list-get-item-end begin struct
))
594 (contents-begin (progn (looking-at org-list-full-item-re
)
595 (goto-char (match-end 0))
596 (org-skip-whitespace)
597 ;; If first line isn't empty,
598 ;; contents really start at the text
599 ;; after item's meta-data.
600 (if (= (point-at-bol) begin
) (point)
602 (hidden (progn (forward-line)
603 (and (not (= (point) end
))
604 (org-truely-invisible-p))))
605 (contents-end (progn (goto-char end
)
606 (skip-chars-backward " \r\t\n")
613 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
614 ;; up in the case of an empty item separated
615 ;; from the next by a blank line. Thus, ensure
616 ;; the former is always the smallest of two.
617 :contents-begin
,(min contents-begin contents-end
)
618 :contents-end
,(max contents-begin contents-end
)
625 :post-blank
,(count-lines contents-end end
))))))
627 (defun org-element-item-interpreter (item contents
)
628 "Interpret ITEM element as Org syntax.
629 CONTENTS is the contents of the element."
631 (let* ((beg (org-element-property :begin item
))
632 (struct (org-element-property :structure item
))
633 (pre (org-list-prevs-alist struct
))
634 (bul (org-element-property :bullet item
)))
635 (org-list-bullet-string
636 (if (not (eq (org-list-get-list-type beg struct pre
) 'ordered
)) "-"
640 (org-list-get-item-number
641 beg struct pre
(org-list-parents-alist struct
))))))
644 (if (eq org-plain-list-ordered-item-terminator ?\
)) ")"
646 (checkbox (org-element-property :checkbox item
))
647 (counter (org-element-property :counter item
))
648 (tag (org-element-property :raw-tag item
))
649 ;; Compute indentation.
650 (ind (make-string (length bullet
) 32)))
654 (and counter
(format "[@%d] " counter
))
656 ((eq checkbox
'on
) "[X] ")
657 ((eq checkbox
'off
) "[ ] ")
658 ((eq checkbox
'trans
) "[-] "))
659 (and tag
(format "%s :: " tag
))
661 (replace-regexp-in-string "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))))
666 (defun org-element-plain-list-parser (&optional structure
)
669 Optional argument STRUCTURE, when non-nil, is the structure of
670 the plain list being parsed.
672 Return a list whose car is `plain-list' and cdr is a plist
673 containing `:type', `:begin', `:end', `:contents-begin' and
674 `:contents-end', `:level', `:structure' and `:post-blank'
677 Assume point is at one of the list items."
679 (let* ((struct (or structure
(org-list-struct)))
680 (prevs (org-list-prevs-alist struct
))
681 (parents (org-list-parents-alist struct
))
682 (type (org-list-get-list-type (point) struct prevs
))
683 (contents-begin (goto-char
684 (org-list-get-list-begin (point) struct prevs
)))
685 (keywords (org-element-collect-affiliated-keywords))
686 (begin (car keywords
))
687 (contents-end (goto-char
688 (org-list-get-list-end (point) struct prevs
)))
689 (end (save-excursion (org-skip-whitespace)
690 (if (eobp) (point) (point-at-bol))))
693 (let ((item contents-begin
))
696 (org-list-get-list-begin item struct prevs
)
699 ;; Blank lines below list belong to the top-level list only.
701 (setq end
(min (org-list-get-bottom-point struct
)
702 (progn (org-skip-whitespace)
703 (if (eobp) (point) (point-at-bol))))))
709 :contents-begin
,contents-begin
710 :contents-end
,contents-end
713 :post-blank
,(count-lines contents-end end
)
714 ,@(cadr keywords
))))))
716 (defun org-element-plain-list-interpreter (plain-list contents
)
717 "Interpret PLAIN-LIST element as Org syntax.
718 CONTENTS is the contents of the element."
724 (defun org-element-quote-block-parser ()
725 "Parse a quote block.
727 Return a list whose car is `quote-block' and cdr is a plist
728 containing `:begin', `:end', `:hiddenp', `:contents-begin',
729 `:contents-end' and `:post-blank' keywords.
731 Assume point is at beginning or end of the block."
733 (let* ((case-fold-search t
)
737 (concat "^[ \t]*#\\+begin_quote") nil t
)
738 (org-element-collect-affiliated-keywords)))
739 (begin (car keywords
))
740 (contents-begin (progn (forward-line) (point)))
741 (hidden (org-truely-invisible-p))
742 (contents-end (progn (re-search-forward
743 (concat "^[ \t]*#\\+end_quote") nil t
)
745 (pos-before-blank (progn (forward-line) (point)))
746 (end (progn (org-skip-whitespace)
747 (if (eobp) (point) (point-at-bol)))))
752 :contents-begin
,contents-begin
753 :contents-end
,contents-end
754 :post-blank
,(count-lines pos-before-blank end
)
755 ,@(cadr keywords
))))))
757 (defun org-element-quote-block-interpreter (quote-block contents
)
758 "Interpret QUOTE-BLOCK element as Org syntax.
759 CONTENTS is the contents of the element."
760 (format "#+begin_quote\n%s#+end_quote" contents
))
765 (defun org-element-section-parser ()
768 Return a list whose car is `section' and cdr is a plist
769 containing `:begin', `:end', `:contents-begin', `contents-end'
770 and `:post-blank' keywords."
772 ;; Beginning of section is the beginning of the first non-blank
773 ;; line after previous headline.
774 (org-with-limited-levels
777 (outline-previous-heading)
778 (if (not (org-at-heading-p)) (point)
779 (forward-line) (org-skip-whitespace) (point-at-bol))))
780 (end (progn (outline-next-heading) (point)))
781 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
787 :contents-begin
,begin
788 :contents-end
,pos-before-blank
789 :post-blank
,(count-lines pos-before-blank end
)))))))
791 (defun org-element-section-interpreter (section contents
)
792 "Interpret SECTION element as Org syntax.
793 CONTENTS is the contents of the element."
799 (defun org-element-special-block-parser ()
800 "Parse a special block.
802 Return a list whose car is `special-block' and cdr is a plist
803 containing `:type', `:begin', `:end', `:hiddenp',
804 `:contents-begin', `:contents-end' and `:post-blank' keywords.
806 Assume point is at beginning or end of the block."
808 (let* ((case-fold-search t
)
809 (type (progn (looking-at
810 "[ \t]*#\\+\\(?:begin\\|end\\)_\\([-A-Za-z0-9]+\\)")
811 (org-match-string-no-properties 1)))
815 (concat "^[ \t]*#\\+begin_" type
) nil t
)
816 (org-element-collect-affiliated-keywords)))
817 (begin (car keywords
))
818 (contents-begin (progn (forward-line) (point)))
819 (hidden (org-truely-invisible-p))
820 (contents-end (progn (re-search-forward
821 (concat "^[ \t]*#\\+end_" type
) nil t
)
823 (pos-before-blank (progn (forward-line) (point)))
824 (end (progn (org-skip-whitespace)
825 (if (eobp) (point) (point-at-bol)))))
831 :contents-begin
,contents-begin
832 :contents-end
,contents-end
833 :post-blank
,(count-lines pos-before-blank end
)
834 ,@(cadr keywords
))))))
836 (defun org-element-special-block-interpreter (special-block contents
)
837 "Interpret SPECIAL-BLOCK element as Org syntax.
838 CONTENTS is the contents of the element."
839 (let ((block-type (org-element-property :type special-block
)))
840 (format "#+begin_%s\n%s#+end_%s" block-type contents block-type
)))
846 ;; For each element, a parser and an interpreter are also defined.
847 ;; Both follow the same naming convention used for greater elements.
849 ;; Also, as for greater elements, adding a new element type is done
850 ;; through the following steps: implement a parser and an interpreter,
851 ;; tweak `org-element-current-element' so that it recognizes the new
852 ;; type and add that new type to `org-element-all-elements'.
854 ;; As a special case, when the newly defined type is a block type,
855 ;; `org-element-non-recursive-block-alist' has to be modified
861 (defun org-element-babel-call-parser ()
864 Return a list whose car is `babel-call' and cdr is a plist
865 containing `:begin', `:end', `:info' and `:post-blank' as
868 (let ((info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
869 (org-babel-lob-get-info)))
871 (pos-before-blank (progn (forward-line) (point)))
872 (end (progn (org-skip-whitespace)
873 (if (eobp) (point) (point-at-bol)))))
878 :post-blank
,(count-lines pos-before-blank end
))))))
880 (defun org-element-babel-call-interpreter (inline-babel-call contents
)
881 "Interpret INLINE-BABEL-CALL object as Org syntax.
883 (let* ((babel-info (org-element-property :info inline-babel-call
))
884 (main-source (car babel-info
))
885 (post-options (nth 1 babel-info
)))
887 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
888 ;; Remove redundant square brackets.
890 (match-string 1 main-source
) nil nil main-source
)
892 (and post-options
(format "[%s]" post-options
)))))
897 (defun org-element-comment-parser ()
900 Return a list whose car is `comment' and cdr is a plist
901 containing `:begin', `:end', `:value' and `:post-blank'
903 (let (beg-coms begin end end-coms keywords
)
906 ;; First type of comment: comments at column 0.
907 (let ((comment-re "^\\([^#]\\|#\\+[a-z]\\)"))
909 (re-search-backward comment-re nil
'move
)
910 (if (bobp) (setq keywords nil beg-coms
(point))
912 (setq keywords
(org-element-collect-affiliated-keywords)
914 (re-search-forward comment-re nil
'move
)
915 (setq end-coms
(if (eobp) (point) (match-beginning 0))))
916 ;; Second type of comment: indented comments.
917 (let ((comment-re "[ \t]*#\\+\\(?: \\|$\\)"))
919 (while (and (not (bobp)) (looking-at comment-re
))
921 (unless (looking-at comment-re
) (forward-line)))
922 (setq beg-coms
(point))
923 (setq keywords
(org-element-collect-affiliated-keywords))
924 ;; Get comments ending. This may not be accurate if
925 ;; commented lines within an item are followed by commented
926 ;; lines outside of the list. Though, parser will always
927 ;; get it right as it already knows surrounding element and
928 ;; has narrowed buffer to its contents.
929 (while (looking-at comment-re
) (forward-line))
930 (setq end-coms
(point))))
931 ;; Find position after blank.
933 (org-skip-whitespace)
934 (setq end
(if (eobp) (point) (point-at-bol))))
936 (:begin
,(or (car keywords
) beg-coms
)
938 :value
,(buffer-substring-no-properties beg-coms end-coms
)
939 :post-blank
,(count-lines end-coms end
)
940 ,@(cadr keywords
)))))
942 (defun org-element-comment-interpreter (comment contents
)
943 "Interpret COMMENT element as Org syntax.
945 (org-element-property :value comment
))
950 (defun org-element-comment-block-parser ()
951 "Parse an export block.
953 Return a list whose car is `comment-block' and cdr is a plist
954 containing `:begin', `:end', `:hiddenp', `:value' and
955 `:post-blank' keywords."
958 (let* ((case-fold-search t
)
960 (re-search-backward "^[ \t]*#\\+begin_comment" nil t
)
961 (org-element-collect-affiliated-keywords)))
962 (begin (car keywords
))
963 (contents-begin (progn (forward-line) (point)))
964 (hidden (org-truely-invisible-p))
965 (contents-end (progn (re-search-forward
966 "^[ \t]*#\\+end_comment" nil t
)
968 (pos-before-blank (progn (forward-line) (point)))
969 (end (progn (org-skip-whitespace)
970 (if (eobp) (point) (point-at-bol))))
971 (value (buffer-substring-no-properties contents-begin contents-end
)))
977 :post-blank
,(count-lines pos-before-blank end
)
978 ,@(cadr keywords
))))))
980 (defun org-element-comment-block-interpreter (comment-block contents
)
981 "Interpret COMMENT-BLOCK element as Org syntax.
983 (concat "#+begin_comment\n"
984 (org-remove-indentation
985 (org-element-property :value comment-block
))
991 (defun org-element-example-block-parser ()
992 "Parse an example block.
994 Return a list whose car is `example' and cdr is a plist
995 containing `:begin', `:end', `:options', `:hiddenp', `:value' and
996 `:post-blank' keywords."
999 (let* ((case-fold-search t
)
1002 "^[ \t]*#\\+begin_example\\(?: +\\(.*\\)\\)?" nil t
)
1003 (org-match-string-no-properties 1)))
1004 (keywords (org-element-collect-affiliated-keywords))
1005 (begin (car keywords
))
1006 (contents-begin (progn (forward-line) (point)))
1007 (hidden (org-truely-invisible-p))
1008 (contents-end (progn
1009 (re-search-forward "^[ \t]*#\\+end_example" nil t
)
1011 (value (buffer-substring-no-properties contents-begin contents-end
))
1012 (pos-before-blank (progn (forward-line) (point)))
1013 (end (progn (org-skip-whitespace)
1014 (if (eobp) (point) (point-at-bol)))))
1021 :post-blank
,(count-lines pos-before-blank end
)
1022 ,@(cadr keywords
))))))
1024 (defun org-element-example-block-interpreter (example-block contents
)
1025 "Interpret EXAMPLE-BLOCK element as Org syntax.
1027 (let ((options (org-element-property :options example-block
)))
1028 (concat "#+begin_example" (and options
(concat " " options
)) "\n"
1029 (org-remove-indentation
1030 (org-element-property :value example-block
))
1036 (defun org-element-export-block-parser ()
1037 "Parse an export block.
1039 Return a list whose car is `export-block' and cdr is a plist
1040 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1041 `:post-blank' keywords."
1044 (let* ((case-fold-search t
)
1046 (type (progn (re-search-backward
1047 (concat "[ \t]*#\\+begin_"
1048 (org-re "\\([[:alnum:]]+\\)")))
1049 (downcase (org-match-string-no-properties 1))))
1050 (keywords (org-element-collect-affiliated-keywords))
1051 (begin (car keywords
))
1052 (contents-begin (progn (forward-line) (point)))
1053 (hidden (org-truely-invisible-p))
1054 (contents-end (progn (re-search-forward
1055 (concat "^[ \t]*#\\+end_" type
) nil t
)
1057 (pos-before-blank (progn (forward-line) (point)))
1058 (end (progn (org-skip-whitespace)
1059 (if (eobp) (point) (point-at-bol))))
1060 (value (buffer-substring-no-properties contents-begin contents-end
)))
1067 :post-blank
,(count-lines pos-before-blank end
)
1068 ,@(cadr keywords
))))))
1070 (defun org-element-export-block-interpreter (export-block contents
)
1071 "Interpret EXPORT-BLOCK element as Org syntax.
1073 (let ((type (org-element-property :type export-block
)))
1074 (concat (format "#+begin_%s\n" type
)
1075 (org-element-property :value export-block
)
1076 (format "#+end_%s" type
))))
1081 (defun org-element-fixed-width-parser ()
1082 "Parse a fixed-width section.
1084 Return a list whose car is `fixed-width' and cdr is a plist
1085 containing `:begin', `:end', `:value' and `:post-blank'
1087 (let ((fixed-re "[ \t]*:\\( \\|$\\)")
1088 beg-area begin end value pos-before-blank keywords
)
1090 ;; Move to the beginning of the fixed-width area.
1092 (while (and (not (bobp)) (looking-at fixed-re
))
1094 (unless (looking-at fixed-re
) (forward-line 1)))
1095 (setq beg-area
(point))
1096 ;; Get affiliated keywords, if any.
1097 (setq keywords
(org-element-collect-affiliated-keywords))
1098 ;; Store true beginning of element.
1099 (setq begin
(car keywords
))
1100 ;; Get ending of fixed-width area. If point is in a list,
1101 ;; ensure to not get outside of it.
1102 (let* ((itemp (org-in-item-p))
1104 (org-list-get-bottom-point
1105 (save-excursion (goto-char itemp
) (org-list-struct)))
1107 (while (and (looking-at fixed-re
) (< (point) max-pos
))
1109 (setq pos-before-blank
(point))
1110 ;; Find position after blank
1111 (org-skip-whitespace)
1112 (setq end
(if (eobp) (point) (point-at-bol)))
1114 (setq value
(buffer-substring-no-properties beg-area pos-before-blank
)))
1119 :post-blank
,(count-lines pos-before-blank end
)
1120 ,@(cadr keywords
)))))
1122 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1123 "Interpret FIXED-WIDTH element as Org syntax.
1125 (org-remove-indentation (org-element-property :value fixed-width
)))
1128 ;;;; Horizontal Rule
1130 (defun org-element-horizontal-rule-parser ()
1131 "Parse an horizontal rule.
1133 Return a list whose car is `horizontal-rule' and cdr is
1134 a plist containing `:begin', `:end' and `:post-blank'
1137 (let* ((keywords (org-element-collect-affiliated-keywords))
1138 (begin (car keywords
))
1139 (post-hr (progn (forward-line) (point)))
1140 (end (progn (org-skip-whitespace)
1141 (if (eobp) (point) (point-at-bol)))))
1145 :post-blank
,(count-lines post-hr end
)
1146 ,@(cadr keywords
))))))
1148 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1149 "Interpret HORIZONTAL-RULE element as Org syntax.
1156 (defun org-element-keyword-parser ()
1157 "Parse a keyword at point.
1159 Return a list whose car is `keyword' and cdr is a plist
1160 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1163 (let* ((begin (point))
1164 (key (progn (looking-at
1165 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1166 (org-match-string-no-properties 1)))
1167 (value (org-trim (buffer-substring-no-properties
1168 (match-end 0) (point-at-eol))))
1169 (pos-before-blank (progn (forward-line) (point)))
1170 (end (progn (org-skip-whitespace)
1171 (if (eobp) (point) (point-at-bol)))))
1177 :post-blank
,(count-lines pos-before-blank end
))))))
1179 (defun org-element-keyword-interpreter (keyword contents
)
1180 "Interpret KEYWORD element as Org syntax.
1183 (org-element-property :key keyword
)
1184 (org-element-property :value keyword
)))
1187 ;;;; Latex Environment
1189 (defun org-element-latex-environment-parser ()
1190 "Parse a LaTeX environment.
1192 Return a list whose car is `latex-environment' and cdr is a plist
1193 containing `:begin', `:end', `:value' and `:post-blank' keywords."
1196 (let* ((case-fold-search t
)
1197 (contents-begin (re-search-backward "^[ \t]*\\\\begin" nil t
))
1198 (keywords (org-element-collect-affiliated-keywords))
1199 (begin (car keywords
))
1200 (contents-end (progn (re-search-forward "^[ \t]*\\\\end")
1203 (value (buffer-substring-no-properties contents-begin contents-end
))
1204 (end (progn (org-skip-whitespace)
1205 (if (eobp) (point) (point-at-bol)))))
1210 :post-blank
,(count-lines contents-end end
)
1211 ,@(cadr keywords
))))))
1213 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1214 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1216 (org-element-property :value latex-environment
))
1221 (defun org-element-paragraph-parser ()
1224 Return a list whose car is `paragraph' and cdr is a plist
1225 containing `:begin', `:end', `:contents-begin' and
1226 `:contents-end' and `:post-blank' keywords.
1228 Assume point is at the beginning of the paragraph."
1230 (let* ((contents-begin (point))
1231 (keywords (org-element-collect-affiliated-keywords))
1232 (begin (car keywords
))
1233 (contents-end (progn
1235 (if (re-search-forward
1236 org-element-paragraph-separate nil
'm
)
1237 (progn (forward-line -
1) (end-of-line) (point))
1239 (pos-before-blank (progn (forward-line) (point)))
1240 (end (progn (org-skip-whitespace)
1241 (if (eobp) (point) (point-at-bol)))))
1245 :contents-begin
,contents-begin
1246 :contents-end
,contents-end
1247 :post-blank
,(count-lines pos-before-blank end
)
1248 ,@(cadr keywords
))))))
1250 (defun org-element-paragraph-interpreter (paragraph contents
)
1251 "Interpret PARAGRAPH element as Org syntax.
1252 CONTENTS is the contents of the element."
1256 ;;;; Property Drawer
1258 (defun org-element-property-drawer-parser ()
1259 "Parse a property drawer.
1261 Return a list whose car is `property-drawer' and cdr is a plist
1262 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1263 `:contents-end', `:properties' and `:post-blank' keywords."
1265 (let ((case-fold-search t
)
1266 (begin (progn (end-of-line)
1267 (re-search-backward org-property-start-re
)
1268 (match-beginning 0)))
1269 (contents-begin (progn (forward-line) (point)))
1270 (hidden (org-truely-invisible-p))
1271 (properties (let (val)
1272 (while (not (looking-at "^[ \t]*:END:"))
1275 "[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):"))
1276 (push (cons (match-string 1)
1279 (match-end 0) (point-at-eol))))
1283 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
1285 (pos-before-blank (progn (forward-line) (point)))
1286 (end (progn (org-skip-whitespace)
1287 (if (eobp) (point) (point-at-bol)))))
1292 :properties
,properties
1293 :post-blank
,(count-lines pos-before-blank end
))))))
1295 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1296 "Interpret PROPERTY-DRAWER element as Org syntax.
1298 (let ((props (org-element-property :properties property-drawer
)))
1301 (mapconcat (lambda (p)
1302 (format org-property-format
(format ":%s:" (car p
)) (cdr p
)))
1303 (nreverse props
) "\n")
1309 (defun org-element-quote-section-parser ()
1310 "Parse a quote section.
1312 Return a list whose car is `quote-section' and cdr is a plist
1313 containing `:begin', `:end', `:value' and `:post-blank'
1316 Assume point is at beginning of the section."
1318 (let* ((begin (point))
1319 (end (progn (org-with-limited-levels (outline-next-heading))
1321 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1324 (value (buffer-substring-no-properties begin pos-before-blank
)))
1329 :post-blank
,(count-lines pos-before-blank end
))))))
1331 (defun org-element-quote-section-interpreter (quote-section contents
)
1332 "Interpret QUOTE-SECTION element as Org syntax.
1334 (org-element-property :value quote-section
))
1339 (defun org-element-src-block-parser ()
1342 Return a list whose car is `src-block' and cdr is a plist
1343 containing `:language', `:switches', `:parameters', `:begin',
1344 `:end', `:hiddenp', `:contents-begin', `:contents-end', `:value'
1345 and `:post-blank' keywords."
1348 (let* ((case-fold-search t
)
1349 ;; Get position at beginning of block.
1352 (concat "^[ \t]*#\\+begin_src"
1353 "\\(?: +\\(\\S-+\\)\\)?" ; language
1354 "\\(\\(?: +[-+][A-Za-z]\\)*\\)" ; switches
1355 "\\(.*\\)[ \t]*$") ; arguments
1357 ;; Get language as a string.
1358 (language (org-match-string-no-properties 1))
1360 (switches (org-match-string-no-properties 2))
1362 (parameters (org-trim (org-match-string-no-properties 3)))
1363 ;; Get affiliated keywords.
1364 (keywords (org-element-collect-affiliated-keywords))
1365 ;; Get beginning position.
1366 (begin (car keywords
))
1367 ;; Get position at end of block.
1368 (contents-end (progn (re-search-forward "^[ \t]*#\\+end_src" nil t
)
1372 (value (buffer-substring-no-properties
1373 (save-excursion (goto-char contents-begin
)
1376 (match-beginning 0)))
1377 ;; Get position after ending blank lines.
1378 (end (progn (org-skip-whitespace)
1379 (if (eobp) (point) (point-at-bol))))
1380 ;; Get visibility status.
1381 (hidden (progn (goto-char contents-begin
)
1383 (org-truely-invisible-p))))
1385 (:language
,language
1387 :parameters
,parameters
1392 :post-blank
,(count-lines contents-end end
)
1393 ,@(cadr keywords
))))))
1395 (defun org-element-src-block-interpreter (src-block contents
)
1396 "Interpret SRC-BLOCK element as Org syntax.
1398 (let ((lang (org-element-property :language src-block
))
1399 (switches (org-element-property :switches src-block
))
1400 (params (org-element-property :parameters src-block
))
1401 (value (let ((val (org-element-property :value src-block
)))
1403 (org-src-preserve-indentation val
)
1404 ((zerop org-edit-src-content-indentation
)
1405 (org-remove-indentation val
))
1407 (let ((ind (make-string
1408 org-edit-src-content-indentation
32)))
1409 (replace-regexp-in-string
1410 "\\(^\\)[ \t]*\\S-" ind
1411 (org-remove-indentation val
) nil nil
1)))))))
1412 (concat (format "#+begin_src%s\n"
1413 (concat (and lang
(concat " " lang
))
1414 (and switches
(concat " " switches
))
1415 (and params
(concat " " params
))))
1422 (defun org-element-table-parser ()
1423 "Parse a table at point.
1425 Return a list whose car is `table' and cdr is a plist containing
1426 `:begin', `:end', `:contents-begin', `:contents-end', `:tblfm',
1427 `:type', `:raw-table' and `:post-blank' keywords."
1429 (let* ((table-begin (goto-char (org-table-begin t
)))
1430 (type (if (org-at-table.el-p
) 'table.el
'org
))
1431 (keywords (org-element-collect-affiliated-keywords))
1432 (begin (car keywords
))
1433 (table-end (goto-char (marker-position (org-table-end t
))))
1434 (tblfm (when (looking-at "[ \t]*#\\+tblfm: +\\(.*\\)[ \t]*")
1435 (prog1 (org-match-string-no-properties 1)
1437 (pos-before-blank (point))
1438 (end (progn (org-skip-whitespace)
1439 (if (eobp) (point) (point-at-bol))))
1440 (raw-table (org-remove-indentation
1441 (buffer-substring-no-properties table-begin table-end
))))
1446 :raw-table
,raw-table
1448 :post-blank
,(count-lines pos-before-blank end
)
1449 ,@(cadr keywords
))))))
1451 (defun org-element-table-interpreter (table contents
)
1452 "Interpret TABLE element as Org syntax.
1454 (org-element-property :raw-table table
))
1459 (defun org-element-verse-block-parser ()
1460 "Parse a verse block.
1462 Return a list whose car is `verse-block' and cdr is a plist
1463 containing `:begin', `:end', `:hiddenp', `:raw-value', `:value'
1464 and `:post-blank' keywords.
1466 Assume point is at beginning or end of the block."
1468 (let* ((case-fold-search t
)
1472 (concat "^[ \t]*#\\+begin_verse") nil t
)
1473 (org-element-collect-affiliated-keywords)))
1474 (begin (car keywords
))
1475 (hidden (progn (forward-line) (org-truely-invisible-p)))
1476 (raw-val (buffer-substring-no-properties
1479 (re-search-forward (concat "^[ \t]*#\\+end_verse") nil t
)
1481 (pos-before-blank (progn (forward-line) (point)))
1482 (end (progn (org-skip-whitespace)
1483 (if (eobp) (point) (point-at-bol))))
1484 (value (org-element-parse-secondary-string
1485 (org-remove-indentation raw-val
)
1486 (cdr (assq 'verse-block org-element-string-restrictions
)))))
1493 :post-blank
,(count-lines pos-before-blank end
)
1494 ,@(cadr keywords
))))))
1496 (defun org-element-verse-block-interpreter (verse-block contents
)
1497 "Interpret VERSE-BLOCK element as Org syntax.
1499 (format "#+begin_verse\n%s#+end_verse"
1500 (org-remove-indentation
1501 (org-element-property :raw-value verse-block
))))
1507 ;; Unlike to elements, interstices can be found between objects.
1508 ;; That's why, along with the parser, successor functions are provided
1509 ;; for each object. Some objects share the same successor
1510 ;; (i.e. `emphasis' and `verbatim' objects).
1512 ;; A successor must accept a single argument bounding the search. It
1513 ;; will return either a cons cell whose car is the object's type, as
1514 ;; a symbol, and cdr the position of its next occurrence, or nil.
1516 ;; Successors follow the naming convention:
1517 ;; org-element-NAME-successor, where NAME is the name of the
1518 ;; successor, as defined in `org-element-all-successors'.
1520 ;; Some object types (i.e. `emphasis') are recursive. Restrictions on
1521 ;; object types they can contain will be specified in
1522 ;; `org-element-object-restrictions'.
1524 ;; Adding a new type of object is simple. Implement a successor,
1525 ;; a parser, and an interpreter for it, all following the naming
1526 ;; convention. Register type in `org-element-all-objects' and
1527 ;; successor in `org-element-all-successors'. Maybe tweak
1528 ;; restrictions about it, and that's it.
1532 (defun org-element-emphasis-parser ()
1533 "Parse text markup object at point.
1535 Return a list whose car is `emphasis' and cdr is a plist with
1536 `:marker', `:begin', `:end', `:contents-begin' and
1537 `:contents-end' and `:post-blank' keywords.
1539 Assume point is at the first emphasis marker."
1541 (unless (bolp) (backward-char 1))
1542 (looking-at org-emph-re
)
1543 (let ((begin (match-beginning 2))
1544 (marker (org-match-string-no-properties 3))
1545 (contents-begin (match-beginning 4))
1546 (contents-end (match-end 4))
1547 (post-blank (progn (goto-char (match-end 2))
1548 (skip-chars-forward " \t")))
1554 :contents-begin
,contents-begin
1555 :contents-end
,contents-end
1556 :post-blank
,post-blank
)))))
1558 (defun org-element-emphasis-interpreter (emphasis contents
)
1559 "Interpret EMPHASIS object as Org syntax.
1560 CONTENTS is the contents of the object."
1561 (let ((marker (org-element-property :marker emphasis
)))
1562 (concat marker contents marker
)))
1564 (defun org-element-text-markup-successor (limit)
1565 "Search for the next emphasis or verbatim object.
1567 LIMIT bounds the search.
1569 Return value is a cons cell whose car is `emphasis' or
1570 `verbatim' and cdr is beginning position."
1572 (unless (bolp) (backward-char))
1573 (when (re-search-forward org-emph-re limit t
)
1574 (cons (if (nth 4 (assoc (match-string 3) org-emphasis-alist
))
1577 (match-beginning 2)))))
1581 (defun org-element-entity-parser ()
1582 "Parse entity at point.
1584 Return a list whose car is `entity' and cdr a plist with
1585 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1586 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1589 Assume point is at the beginning of the entity."
1591 (looking-at "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1592 (let* ((value (org-entity-get (match-string 1)))
1593 (begin (match-beginning 0))
1594 (bracketsp (string= (match-string 2) "{}"))
1595 (post-blank (progn (goto-char (match-end 1))
1596 (when bracketsp
(forward-char 2))
1597 (skip-chars-forward " \t")))
1601 :latex
,(nth 1 value
)
1602 :latex-math-p
,(nth 2 value
)
1603 :html
,(nth 3 value
)
1604 :ascii
,(nth 4 value
)
1605 :latin1
,(nth 5 value
)
1606 :utf-8
,(nth 6 value
)
1609 :use-brackets-p
,bracketsp
1610 :post-blank
,post-blank
)))))
1612 (defun org-element-entity-interpreter (entity contents
)
1613 "Interpret ENTITY object as Org syntax.
1616 (org-element-property :name entity
)
1617 (when (org-element-property :use-brackets-p entity
) "{}")))
1619 (defun org-element-latex-or-entity-successor (limit)
1620 "Search for the next latex-fragment or entity object.
1622 LIMIT bounds the search.
1624 Return value is a cons cell whose car is `entity' or
1625 `latex-fragment' and cdr is beginning position."
1627 (let ((matchers (plist-get org-format-latex-options
:matchers
))
1628 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1630 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"))
1631 (when (re-search-forward
1632 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
1636 (goto-char (match-beginning 0))
1637 (if (looking-at entity-re
)
1638 ;; Determine if it's a real entity or a LaTeX command.
1639 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
1640 (match-beginning 0))
1641 ;; No entity nor command: point is at a LaTeX fragment.
1642 ;; Determine its type to get the correct beginning position.
1643 (cons 'latex-fragment
1646 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
1649 (nth 2 (assoc e org-latex-regexps
))))))
1656 (defun org-element-export-snippet-parser ()
1657 "Parse export snippet at point.
1659 Return a list whose car is `export-snippet' and cdr a plist with
1660 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1663 Assume point is at the beginning of the snippet."
1665 (looking-at "@\\([-A-Za-z0-9]+\\){")
1666 (let* ((begin (point))
1667 (back-end (org-match-string-no-properties 1))
1668 (before-blank (progn (goto-char (scan-sexps (1- (match-end 0)) 1))))
1669 (value (buffer-substring-no-properties
1670 (match-end 0) (1- before-blank
)))
1671 (post-blank (skip-chars-forward " \t"))
1674 (:back-end
,back-end
1678 :post-blank
,post-blank
)))))
1680 (defun org-element-export-snippet-interpreter (export-snippet contents
)
1681 "Interpret EXPORT-SNIPPET object as Org syntax.
1684 (org-element-property :back-end export-snippet
)
1685 (org-element-property :value export-snippet
)))
1687 (defun org-element-export-snippet-successor (limit)
1688 "Search for the next export-snippet object.
1690 LIMIT bounds the search.
1692 Return value is a cons cell whose car is `export-snippet' cdr is
1693 its beginning position."
1696 (while (re-search-forward "@[-A-Za-z0-9]+{" limit t
)
1697 (when (let ((end (ignore-errors (scan-sexps (1- (point)) 1))))
1698 (and end
(eq (char-before end
) ?
})))
1699 (throw 'exit
(cons 'export-snippet
(match-beginning 0))))))))
1702 ;;;; Footnote Reference
1704 (defun org-element-footnote-reference-parser ()
1705 "Parse footnote reference at point.
1707 Return a list whose car is `footnote-reference' and cdr a plist
1708 with `:label', `:type', `:definition', `:begin', `:end' and
1709 `:post-blank' as keywords."
1711 (let* ((ref (org-footnote-at-reference-p))
1713 (raw-def (nth 3 ref
))
1716 (org-element-parse-secondary-string
1718 (cdr (assq 'footnote-reference
1719 org-element-string-restrictions
)))))
1720 (type (if (nth 3 ref
) 'inline
'standard
))
1722 (post-blank (progn (goto-char (nth 2 ref
))
1723 (skip-chars-forward " \t")))
1725 `(footnote-reference
1728 :inline-definition
,inline-def
1731 :post-blank
,post-blank
1732 :raw-definition
,raw-def
)))))
1734 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
1735 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1737 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
1739 (let ((raw (org-element-property :raw-definition footnote-reference
)))
1740 (if raw
(concat ":" raw
) ""))))
1741 (format "[%s]" (concat label def
))))
1743 (defun org-element-footnote-reference-successor (limit)
1744 "Search for the next footnote-reference object.
1746 LIMIT bounds the search.
1748 Return value is a cons cell whose car is `footnote-reference' and
1749 cdr is beginning position."
1751 (when (setq fn-ref
(org-footnote-get-next-reference nil nil limit
))
1752 (cons 'footnote-reference
(nth 1 fn-ref
)))))
1755 ;;;; Inline Babel Call
1757 (defun org-element-inline-babel-call-parser ()
1758 "Parse inline babel call at point.
1760 Return a list whose car is `inline-babel-call' and cdr a plist with
1761 `:begin', `:end', `:info' and `:post-blank' as keywords.
1763 Assume point is at the beginning of the babel call."
1765 (unless (bolp) (backward-char))
1766 (looking-at org-babel-inline-lob-one-liner-regexp
)
1767 (let ((info (save-match-data (org-babel-lob-get-info)))
1768 (begin (match-end 1))
1769 (post-blank (progn (goto-char (match-end 0))
1770 (skip-chars-forward " \t")))
1776 :post-blank
,post-blank
)))))
1778 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
1779 "Interpret INLINE-BABEL-CALL object as Org syntax.
1781 (let* ((babel-info (org-element-property :info inline-babel-call
))
1782 (main-source (car babel-info
))
1783 (post-options (nth 1 babel-info
)))
1785 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
1786 ;; Remove redundant square brackets.
1788 (match-string 1 main-source
) nil nil main-source
)
1790 (and post-options
(format "[%s]" post-options
)))))
1792 (defun org-element-inline-babel-call-successor (limit)
1793 "Search for the next inline-babel-call object.
1795 LIMIT bounds the search.
1797 Return value is a cons cell whose car is `inline-babel-call' and
1798 cdr is beginning position."
1800 ;; Use a simplified version of
1801 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
1802 (when (re-search-forward
1803 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
1805 (cons 'inline-babel-call
(match-beginning 0)))))
1808 ;;;; Inline Src Block
1810 (defun org-element-inline-src-block-parser ()
1811 "Parse inline source block at point.
1813 Return a list whose car is `inline-src-block' and cdr a plist
1814 with `:begin', `:end', `:language', `:value', `:parameters' and
1815 `:post-blank' as keywords.
1817 Assume point is at the beginning of the inline src block."
1819 (unless (bolp) (backward-char))
1820 (looking-at org-babel-inline-src-block-regexp
)
1821 (let ((begin (match-beginning 1))
1822 (language (org-match-string-no-properties 2))
1823 (parameters (org-match-string-no-properties 4))
1824 (value (org-match-string-no-properties 5))
1825 (post-blank (progn (goto-char (match-end 0))
1826 (skip-chars-forward " \t")))
1829 (:language
,language
1831 :parameters
,parameters
1834 :post-blank
,post-blank
)))))
1836 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
1837 "Interpret INLINE-SRC-BLOCK object as Org syntax.
1839 (let ((language (org-element-property :language inline-src-block
))
1840 (arguments (org-element-property :parameters inline-src-block
))
1841 (body (org-element-property :value inline-src-block
)))
1842 (format "src_%s%s{%s}"
1844 (if arguments
(format "[%s]" arguments
) "")
1847 (defun org-element-inline-src-block-successor (limit)
1848 "Search for the next inline-babel-call element.
1850 LIMIT bounds the search.
1852 Return value is a cons cell whose car is `inline-babel-call' and
1853 cdr is beginning position."
1855 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
1856 (cons 'inline-src-block
(match-beginning 1)))))
1861 (defun org-element-latex-fragment-parser ()
1862 "Parse latex fragment at point.
1864 Return a list whose car is `latex-fragment' and cdr a plist with
1865 `:value', `:begin', `:end', and `:post-blank' as keywords.
1867 Assume point is at the beginning of the latex fragment."
1869 (let* ((begin (point))
1873 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
1874 (when (or (looking-at latex-regexp
)
1878 (looking-at latex-regexp
))))
1879 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
1880 (plist-get org-format-latex-options
:matchers
))
1881 ;; None found: it's a macro.
1882 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
1884 (value (match-string-no-properties substring-match
))
1885 (post-blank (progn (goto-char (match-end substring-match
))
1886 (skip-chars-forward " \t")))
1892 :post-blank
,post-blank
)))))
1894 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
1895 "Interpret LATEX-FRAGMENT object as Org syntax.
1897 (org-element-property :value latex-fragment
))
1901 (defun org-element-line-break-parser ()
1902 "Parse line break at point.
1904 Return a list whose car is `line-break', and cdr a plist with
1905 `:begin', `:end' and `:post-blank' keywords.
1907 Assume point is at the beginning of the line break."
1908 (let ((begin (point))
1909 (end (save-excursion (forward-line) (point))))
1910 `(line-break (:begin
,begin
:end
,end
:post-blank
0))))
1912 (defun org-element-line-break-interpreter (line-break contents
)
1913 "Interpret LINE-BREAK object as Org syntax.
1917 (defun org-element-line-break-successor (limit)
1918 "Search for the next line-break object.
1920 LIMIT bounds the search.
1922 Return value is a cons cell whose car is `line-break' and cdr is
1923 beginning position."
1925 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
1926 (goto-char (match-beginning 1)))))
1927 ;; A line break can only happen on a non-empty line.
1928 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
1929 (cons 'line-break beg
)))))
1934 (defun org-element-link-parser ()
1935 "Parse link at point.
1937 Return a list whose car is `link' and cdr a plist with `:type',
1938 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
1939 `:contents-end' and `:post-blank' as keywords.
1941 Assume point is at the beginning of the link."
1943 (let ((begin (point))
1944 end contents-begin contents-end link-end post-blank path type
1947 ;; Type 1: Text targeted from a radio target.
1948 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
1950 link-end
(match-end 0)
1951 path
(org-match-string-no-properties 0)))
1952 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
1953 ((looking-at org-bracket-link-regexp
)
1954 (setq contents-begin
(match-beginning 3)
1955 contents-end
(match-end 3)
1956 link-end
(match-end 0)
1957 ;; RAW-LINK is the original link.
1958 raw-link
(org-match-string-no-properties 1)
1959 link
(org-link-expand-abbrev
1960 (replace-regexp-in-string
1961 " *\n *" " " (org-link-unescape raw-link
) t t
)))
1962 ;; Determine TYPE of link and set PATH accordingly.
1965 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
1966 (setq type
"file" path link
))
1967 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
1968 ((string-match org-link-re-with-space3 link
)
1969 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
1970 ;; Id type: PATH is the id.
1971 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
1972 (setq type
"id" path
(match-string 1 link
)))
1973 ;; Code-ref type: PATH is the name of the reference.
1974 ((string-match "^(\\(.*\\))$" link
)
1975 (setq type
"coderef" path
(match-string 1 link
)))
1976 ;; Custom-id type: PATH is the name of the custom id.
1977 ((= (aref link
0) ?
#)
1978 (setq type
"custom-id" path
(substring link
1)))
1979 ;; Fuzzy type: Internal link either matches a target, an
1980 ;; headline name or nothing. PATH is the target or headline's
1982 (t (setq type
"fuzzy" path link
))))
1983 ;; Type 3: Plain link, i.e. http://orgmode.org
1984 ((looking-at org-plain-link-re
)
1985 (setq raw-link
(org-match-string-no-properties 0)
1986 type
(org-match-string-no-properties 1)
1987 path
(org-match-string-no-properties 2)
1988 link-end
(match-end 0)))
1989 ;; Type 4: Angular link, i.e. <http://orgmode.org>
1990 ((looking-at org-angle-link-re
)
1991 (setq raw-link
(buffer-substring-no-properties
1992 (match-beginning 1) (match-end 2))
1993 type
(org-match-string-no-properties 1)
1994 path
(org-match-string-no-properties 2)
1995 link-end
(match-end 0))))
1996 ;; In any case, deduce end point after trailing white space from
1997 ;; LINK-END variable.
1998 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
2003 :raw-link
,(or raw-link path
)
2006 :contents-begin
,contents-begin
2007 :contents-end
,contents-end
2008 :post-blank
,post-blank
)))))
2010 (defun org-element-link-interpreter (link contents
)
2011 "Interpret LINK object as Org syntax.
2012 CONTENTS is the contents of the object."
2013 (let ((type (org-element-property :type link
))
2014 (raw-link (org-element-property :raw-link link
)))
2015 (if (string= type
"radio") raw-link
2018 (if (string= contents
"") "" (format "[%s]" contents
))))))
2020 (defun org-element-link-successor (limit)
2021 "Search for the next link object.
2023 LIMIT bounds the search.
2025 Return value is a cons cell whose car is `link' and cdr is
2026 beginning position."
2029 (if (not org-target-link-regexp
) org-any-link-re
2030 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2031 (when (re-search-forward link-regexp limit t
)
2032 (cons 'link
(match-beginning 0))))))
2037 (defun org-element-macro-parser ()
2038 "Parse macro at point.
2040 Return a list whose car is `macro' and cdr a plist with `:key',
2041 `:args', `:begin', `:end', `:value' and `:post-blank' as
2044 Assume point is at the macro."
2046 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2047 (let ((begin (point))
2048 (key (downcase (org-match-string-no-properties 1)))
2049 (value (org-match-string-no-properties 0))
2050 (post-blank (progn (goto-char (match-end 0))
2051 (skip-chars-forward " \t")))
2053 (args (let ((args (org-match-string-no-properties 3)) args2
)
2055 (setq args
(org-split-string args
","))
2057 (while (string-match "\\\\\\'" (car args
))
2058 ;; Repair bad splits.
2059 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2062 (push (pop args
) args2
))
2063 (mapcar 'org-trim
(nreverse args2
))))))
2070 :post-blank
,post-blank
)))))
2072 (defun org-element-macro-interpreter (macro contents
)
2073 "Interpret MACRO object as Org syntax.
2075 (org-element-property :value macro
))
2077 (defun org-element-macro-successor (limit)
2078 "Search for the next macro object.
2080 LIMIT bounds the search.
2082 Return value is cons cell whose car is `macro' and cdr is
2083 beginning position."
2085 (when (re-search-forward
2086 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2088 (cons 'macro
(match-beginning 0)))))
2093 (defun org-element-radio-target-parser ()
2094 "Parse radio target at point.
2096 Return a list whose car is `radio-target' and cdr a plist with
2097 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2098 and `:post-blank' as keywords.
2100 Assume point is at the radio target."
2102 (looking-at org-radio-target-regexp
)
2103 (let ((begin (point))
2104 (contents-begin (match-beginning 1))
2105 (contents-end (match-end 1))
2106 (raw-value (org-match-string-no-properties 1))
2107 (post-blank (progn (goto-char (match-end 0))
2108 (skip-chars-forward " \t")))
2113 :contents-begin
,contents-begin
2114 :contents-end
,contents-end
2115 :raw-value
,raw-value
2116 :post-blank
,post-blank
)))))
2118 (defun org-element-radio-target-interpreter (target contents
)
2119 "Interpret TARGET object as Org syntax.
2120 CONTENTS is the contents of the object."
2121 (concat "<<<" contents
">>>"))
2123 (defun org-element-radio-target-successor (limit)
2124 "Search for the next radio-target object.
2126 LIMIT bounds the search.
2128 Return value is a cons cell whose car is `radio-target' and cdr
2129 is beginning position."
2131 (when (re-search-forward org-radio-target-regexp limit t
)
2132 (cons 'radio-target
(match-beginning 0)))))
2135 ;;;; Statistics Cookie
2137 (defun org-element-statistics-cookie-parser ()
2138 "Parse statistics cookie at point.
2140 Return a list whose car is `statistics-cookie', and cdr a plist
2141 with `:begin', `:end', `:value' and `:post-blank' keywords.
2143 Assume point is at the beginning of the statistics-cookie."
2145 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2146 (let* ((begin (point))
2147 (value (buffer-substring-no-properties
2148 (match-beginning 0) (match-end 0)))
2149 (post-blank (progn (goto-char (match-end 0))
2150 (skip-chars-forward " \t")))
2156 :post-blank
,post-blank
)))))
2158 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
2159 "Interpret STATISTICS-COOKIE object as Org syntax.
2161 (org-element-property :value statistics-cookie
))
2163 (defun org-element-statistics-cookie-successor (limit)
2164 "Search for the next statistics cookie object.
2166 LIMIT bounds the search.
2168 Return value is a cons cell whose car is `statistics-cookie' and
2169 cdr is beginning position."
2171 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
2172 (cons 'statistics-cookie
(match-beginning 0)))))
2177 (defun org-element-subscript-parser ()
2178 "Parse subscript at point.
2180 Return a list whose car is `subscript' and cdr a plist with
2181 `:begin', `:end', `:contents-begin', `:contents-end',
2182 `:use-brackets-p' and `:post-blank' as keywords.
2184 Assume point is at the underscore."
2186 (unless (bolp) (backward-char))
2187 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2189 (not (looking-at org-match-substring-regexp
))))
2190 (begin (match-beginning 2))
2191 (contents-begin (or (match-beginning 5)
2192 (match-beginning 3)))
2193 (contents-end (or (match-end 5) (match-end 3)))
2194 (post-blank (progn (goto-char (match-end 0))
2195 (skip-chars-forward " \t")))
2200 :use-brackets-p
,bracketsp
2201 :contents-begin
,contents-begin
2202 :contents-end
,contents-end
2203 :post-blank
,post-blank
)))))
2205 (defun org-element-subscript-interpreter (subscript contents
)
2206 "Interpret SUBSCRIPT object as Org syntax.
2207 CONTENTS is the contents of the object."
2209 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
2212 (defun org-element-sub/superscript-successor
(limit)
2213 "Search for the next sub/superscript object.
2215 LIMIT bounds the search.
2217 Return value is a cons cell whose car is either `subscript' or
2218 `superscript' and cdr is beginning position."
2220 (when (re-search-forward org-match-substring-regexp limit t
)
2221 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
2222 (match-beginning 2)))))
2227 (defun org-element-superscript-parser ()
2228 "Parse superscript at point.
2230 Return a list whose car is `superscript' and cdr a plist with
2231 `:begin', `:end', `:contents-begin', `:contents-end',
2232 `:use-brackets-p' and `:post-blank' as keywords.
2234 Assume point is at the caret."
2236 (unless (bolp) (backward-char))
2237 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2239 (not (looking-at org-match-substring-regexp
))))
2240 (begin (match-beginning 2))
2241 (contents-begin (or (match-beginning 5)
2242 (match-beginning 3)))
2243 (contents-end (or (match-end 5) (match-end 3)))
2244 (post-blank (progn (goto-char (match-end 0))
2245 (skip-chars-forward " \t")))
2250 :use-brackets-p
,bracketsp
2251 :contents-begin
,contents-begin
2252 :contents-end
,contents-end
2253 :post-blank
,post-blank
)))))
2255 (defun org-element-superscript-interpreter (superscript contents
)
2256 "Interpret SUPERSCRIPT object as Org syntax.
2257 CONTENTS is the contents of the object."
2259 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
2265 (defun org-element-target-parser ()
2266 "Parse target at point.
2268 Return a list whose car is `target' and cdr a plist with
2269 `:begin', `:end', `:contents-begin', `:contents-end', `value' and
2270 `:post-blank' as keywords.
2272 Assume point is at the target."
2274 (looking-at org-target-regexp
)
2275 (let ((begin (point))
2276 (value (org-match-string-no-properties 1))
2277 (post-blank (progn (goto-char (match-end 0))
2278 (skip-chars-forward " \t")))
2284 :post-blank
,post-blank
)))))
2286 (defun org-element-target-interpreter (target contents
)
2287 "Interpret TARGET object as Org syntax.
2288 CONTENTS is the contents of target."
2291 (defun org-element-target-successor (limit)
2292 "Search for the next target object.
2294 LIMIT bounds the search.
2296 Return value is a cons cell whose car is `target' and cdr is
2297 beginning position."
2299 (when (re-search-forward org-target-regexp limit t
)
2300 (cons 'target
(match-beginning 0)))))
2305 (defun org-element-time-stamp-parser ()
2306 "Parse time stamp at point.
2308 Return a list whose car is `time-stamp', and cdr a plist with
2309 `:appt-type', `:type', `:begin', `:end', `:value' and
2310 `:post-blank' keywords.
2312 Assume point is at the beginning of the time-stamp."
2314 (let* ((appt-type (cond
2315 ((looking-at (concat org-deadline-string
" +"))
2316 (goto-char (match-end 0))
2318 ((looking-at (concat org-scheduled-string
" +"))
2319 (goto-char (match-end 0))
2321 ((looking-at (concat org-closed-string
" +"))
2322 (goto-char (match-end 0))
2324 (begin (and appt-type
(match-beginning 0)))
2326 ((looking-at org-tsr-regexp
)
2327 (if (match-string 2) 'active-range
'active
))
2328 ((looking-at org-tsr-regexp-both
)
2329 (if (match-string 2) 'inactive-range
'inactive
))
2330 ((looking-at (concat
2331 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2333 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2335 (begin (or begin
(match-beginning 0)))
2336 (value (buffer-substring-no-properties
2337 (match-beginning 0) (match-end 0)))
2338 (post-blank (progn (goto-char (match-end 0))
2339 (skip-chars-forward " \t")))
2342 (:appt-type
,appt-type
2347 :post-blank
,post-blank
)))))
2349 (defun org-element-time-stamp-interpreter (time-stamp contents
)
2350 "Interpret TIME-STAMP object as Org syntax.
2353 (case (org-element-property :appt-type time-stamp
)
2354 (closed (concat org-closed-string
" "))
2355 (deadline (concat org-deadline-string
" "))
2356 (scheduled (concat org-scheduled-string
" ")))
2357 (org-element-property :value time-stamp
)))
2359 (defun org-element-time-stamp-successor (limit)
2360 "Search for the next time-stamp object.
2362 LIMIT bounds the search.
2364 Return value is a cons cell whose car is `time-stamp' and cdr is
2365 beginning position."
2367 (when (re-search-forward
2368 (concat "\\(?:" org-scheduled-string
" +\\|"
2369 org-deadline-string
" +\\|" org-closed-string
" +\\)?"
2372 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2374 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2376 (cons 'time-stamp
(match-beginning 0)))))
2381 (defun org-element-verbatim-parser ()
2382 "Parse verbatim object at point.
2384 Return a list whose car is `verbatim' and cdr is a plist with
2385 `:marker', `:begin', `:end' and `:post-blank' keywords.
2387 Assume point is at the first verbatim marker."
2389 (unless (bolp) (backward-char 1))
2390 (looking-at org-emph-re
)
2391 (let ((begin (match-beginning 2))
2392 (marker (org-match-string-no-properties 3))
2393 (value (org-match-string-no-properties 4))
2394 (post-blank (progn (goto-char (match-end 2))
2395 (skip-chars-forward " \t")))
2402 :post-blank
,post-blank
)))))
2404 (defun org-element-verbatim-interpreter (verbatim contents
)
2405 "Interpret VERBATIM object as Org syntax.
2407 (let ((marker (org-element-property :marker verbatim
))
2408 (value (org-element-property :value verbatim
)))
2409 (concat marker value marker
)))
2413 ;;; Definitions And Rules
2415 ;; Define elements, greater elements and specify recursive objects,
2416 ;; along with the affiliated keywords recognized. Also set up
2417 ;; restrictions on recursive objects combinations.
2419 ;; These variables really act as a control center for the parsing
2421 (defconst org-element-paragraph-separate
2422 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2423 ;; Headlines and inlinetasks.
2424 org-outline-regexp-bol
"\\|"
2425 ;; Comments, blocks (any type), keywords and babel calls.
2426 "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
2428 (org-item-beginning-re) "\\|"
2429 ;; Fixed-width, drawers (any type) and tables.
2431 ;; Footnote definitions.
2432 org-footnote-definition-re
"\\|"
2433 ;; Horizontal rules.
2434 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2435 ;; LaTeX environments.
2436 "^[ \t]*\\\\\\(begin\\|end\\)")
2437 "Regexp to separate paragraphs in an Org buffer.")
2439 (defconst org-element-all-elements
2440 '(center-block comment comment-block drawer dynamic-block example-block
2441 export-block fixed-width footnote-definition headline
2442 horizontal-rule inlinetask item keyword latex-environment
2443 babel-call paragraph plain-list property-drawer quote-block
2444 quote-section section special-block src-block table
2446 "Complete list of element types.")
2448 (defconst org-element-greater-elements
2449 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2450 item plain-list quote-block section special-block
)
2451 "List of recursive element types aka Greater Elements.")
2453 (defconst org-element-all-successors
2454 '(export-snippet footnote-reference inline-babel-call inline-src-block
2455 latex-or-entity line-break link macro radio-target
2456 statistics-cookie sub
/superscript target text-markup
2458 "Complete list of successors.")
2460 (defconst org-element-object-successor-alist
2461 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
2462 (emphasis . text-markup
) (verbatim . text-markup
)
2463 (entity . latex-or-entity
) (latex-fragment . latex-or-entity
))
2464 "Alist of translations between object type and successor name.
2466 Sharing the same successor comes handy when, for example, the
2467 regexp matching one object can also match the other object.")
2469 (defconst org-element-all-objects
2470 '(emphasis entity export-snippet footnote-reference inline-babel-call
2471 inline-src-block line-break latex-fragment link macro radio-target
2472 statistics-cookie subscript superscript target time-stamp
2474 "Complete list of object types.")
2476 (defconst org-element-recursive-objects
2477 '(emphasis link macro subscript superscript radio-target
)
2478 "List of recursive object types.")
2480 (defconst org-element-non-recursive-block-alist
2481 '(("ascii" . export-block
)
2482 ("comment" . comment-block
)
2483 ("docbook" . export-block
)
2484 ("example" . example-block
)
2485 ("html" . export-block
)
2486 ("latex" . export-block
)
2487 ("odt" . export-block
)
2489 ("verse" . verse-block
))
2490 "Alist between non-recursive block name and their element type.")
2492 (defconst org-element-affiliated-keywords
2493 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "caption"
2494 "data" "header" "headers" "label" "name" "plot" "resname" "result" "results"
2495 "source" "srcname" "tblname")
2496 "List of affiliated keywords as strings.")
2498 (defconst org-element-keyword-translation-alist
2499 '(("data" .
"name") ("label" .
"name") ("resname" .
"name")
2500 ("source" .
"name") ("srcname" .
"name") ("tblname" .
"name")
2501 ("result" .
"results") ("headers" .
"header"))
2502 "Alist of usual translations for keywords.
2503 The key is the old name and the value the new one. The property
2504 holding their value will be named after the translated name.")
2506 (defconst org-element-multiple-keywords
2507 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "header")
2508 "List of affiliated keywords that can occur more that once in an element.
2510 Their value will be consed into a list of strings, which will be
2511 returned as the value of the property.
2513 This list is checked after translations have been applied. See
2514 `org-element-keyword-translation-alist'.")
2516 (defconst org-element-parsed-keywords
'("author" "caption" "title")
2517 "List of keywords whose value can be parsed.
2519 Their value will be stored as a secondary string: a list of
2520 strings and objects.
2522 This list is checked after translations have been applied. See
2523 `org-element-keyword-translation-alist'.")
2525 (defconst org-element-dual-keywords
'("caption" "results")
2526 "List of keywords which can have a secondary value.
2528 In Org syntax, they can be written with optional square brackets
2529 before the colons. For example, results keyword can be
2530 associated to a hash value with the following:
2532 #+results[hash-string]: some-source
2534 This list is checked after translations have been applied. See
2535 `org-element-keyword-translation-alist'.")
2537 (defconst org-element-object-restrictions
2538 '((emphasis entity export-snippet inline-babel-call inline-src-block link
2539 radio-target sub
/superscript target text-markup time-stamp
)
2540 (link entity export-snippet inline-babel-call inline-src-block
2541 latex-fragment link sub
/superscript text-markup
)
2543 (radio-target entity export-snippet latex-fragment sub
/superscript
)
2544 (subscript entity export-snippet inline-babel-call inline-src-block
2545 latex-fragment sub
/superscript text-markup
)
2546 (superscript entity export-snippet inline-babel-call inline-src-block
2547 latex-fragment sub
/superscript text-markup
))
2548 "Alist of recursive objects restrictions.
2550 CAR is a recursive object type and CDR is a list of successors
2551 that will be called within an object of such type.
2553 For example, in a `radio-target' object, one can only find
2554 entities, export snippets, latex-fragments, subscript and
2557 (defconst org-element-string-restrictions
2558 '((footnote-reference entity export-snippet footnote-reference
2559 inline-babel-call inline-src-block latex-fragment
2560 line-break link macro radio-target sub
/superscript
2561 target text-markup time-stamp
)
2562 (headline entity inline-babel-call inline-src-block latex-fragment link
2563 macro radio-target statistics-cookie sub
/superscript text-markup
2565 (inlinetask entity inline-babel-call inline-src-block latex-fragment link
2566 macro radio-target sub
/superscript text-markup time-stamp
)
2567 (item entity inline-babel-call latex-fragment macro radio-target
2568 sub
/superscript target text-markup
)
2569 (keyword entity latex-fragment macro sub
/superscript text-markup
)
2570 (table entity latex-fragment macro target text-markup
)
2571 (verse-block entity footnote-reference inline-babel-call inline-src-block
2572 latex-fragment line-break link macro radio-target
2573 sub
/superscript target text-markup time-stamp
))
2574 "Alist of secondary strings restrictions.
2576 When parsed, some elements have a secondary string which could
2577 contain various objects (i.e. headline's name, or table's cells).
2578 For association, CAR is the element type, and CDR a list of
2579 successors that will be called in that secondary string.
2581 Note: `keyword' secondary string type only applies to keywords
2582 matching `org-element-parsed-keywords'.")
2584 (defconst org-element-secondary-value-alist
2585 '((headline .
:title
)
2586 (inlinetask .
:title
)
2588 (footnote-reference .
:inline-definition
)
2589 (verse-block .
:value
))
2590 "Alist between element types and location of secondary value.
2591 Only elements with a secondary value available at parse time are
2592 considered here. This is used internally by `org-element-map',
2593 which will look into the secondary strings of an element only if
2594 its type is listed here.")
2600 ;; Provide three accessors: `org-element-type', `org-element-property'
2601 ;; and `org-element-contents'.
2603 (defun org-element-type (element)
2604 "Return type of element ELEMENT.
2606 The function returns the type of the element or object provided.
2607 It can also return the following special value:
2608 `plain-text' for a string
2609 `org-data' for a complete document
2610 nil in any other case."
2612 ((not (consp element
)) (and (stringp element
) 'plain-text
))
2613 ((symbolp (car element
)) (car element
))))
2615 (defun org-element-property (property element
)
2616 "Extract the value from the PROPERTY of an ELEMENT."
2617 (plist-get (nth 1 element
) property
))
2619 (defun org-element-contents (element)
2620 "Extract contents from an ELEMENT."
2625 ;;; Parsing Element Starting At Point
2627 ;; `org-element-current-element' is the core function of this section.
2628 ;; It returns the Lisp representation of the element starting at
2629 ;; point. It uses `org-element--element-block-re' for quick access to
2632 (defconst org-element--element-block-re
2633 (format "[ \t]*#\\+begin_\\(%s\\)\\(?: \\|$\\)"
2636 (mapcar 'car org-element-non-recursive-block-alist
) "\\|"))
2637 "Regexp matching the beginning of a non-recursive block type.
2638 Used internally by `org-element-current-element'. Do not modify
2639 it directly, set `org-element-recursive-block-alist' instead.")
2641 (defun org-element-current-element (&optional special structure
)
2642 "Parse the element starting at point.
2644 Return value is a list like (TYPE PROPS) where TYPE is the type
2645 of the element and PROPS a plist of properties associated to the
2648 Possible types are defined in `org-element-all-elements'.
2650 Optional argument SPECIAL, when non-nil, can be either `item',
2651 `section' or `quote-section'. `item' allows to parse item wise
2652 instead of plain-list wise, using STRUCTURE as the current list
2653 structure. `section' (resp. `quote-section') will try to parse
2654 a section (resp. a quote section) before anything else.
2656 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
2659 Unlike to `org-element-at-point', this function assumes point is
2660 always at the beginning of the element it has to parse. As such,
2661 it is quicker than its counterpart, albeit more restrictive."
2664 ;; If point is at an affiliated keyword, try moving to the
2665 ;; beginning of the associated element. If none is found, the
2666 ;; keyword is orphaned and will be treated as plain text.
2667 (when (looking-at org-element--affiliated-re
)
2668 (let ((opoint (point)))
2669 (while (looking-at org-element--affiliated-re
) (forward-line))
2670 (when (looking-at "[ \t]*$") (goto-char opoint
))))
2671 (let ((case-fold-search t
))
2674 ((org-with-limited-levels (org-at-heading-p))
2675 (org-element-headline-parser))
2677 ((eq special
'quote-section
) (org-element-quote-section-parser))
2679 ((eq special
'section
) (org-element-section-parser))
2680 ;; Non-recursive block.
2681 ((when (looking-at org-element--element-block-re
)
2682 (let ((type (downcase (match-string 1))))
2685 (format "[ \t]*#\\+end_%s\\(?: \\|$\\)" type
) nil t
))
2686 ;; Build appropriate parser.
2689 (format "org-element-%s-parser"
2691 org-element-non-recursive-block-alist
)))))
2692 (org-element-paragraph-parser)))))
2694 ((org-at-heading-p) (org-element-inlinetask-parser))
2695 ;; LaTeX Environment or paragraph if incomplete.
2696 ((looking-at "^[ \t]*\\\\begin{")
2698 (re-search-forward "^[ \t]*\\\\end{[^}]*}[ \t]*" nil t
))
2699 (org-element-latex-environment-parser)
2700 (org-element-paragraph-parser)))
2702 ((looking-at org-property-start-re
)
2703 (if (save-excursion (re-search-forward org-property-end-re nil t
))
2704 (org-element-property-drawer-parser)
2705 (org-element-paragraph-parser)))
2706 ;; Recursive block, or paragraph if incomplete.
2707 ((looking-at "[ \t]*#\\+begin_\\([-A-Za-z0-9]+\\)\\(?: \\|$\\)")
2708 (let ((type (downcase (match-string 1))))
2710 ((not (save-excursion
2712 (format "[ \t]*#\\+end_%s\\(?: \\|$\\)" type
) nil t
)))
2713 (org-element-paragraph-parser))
2714 ((string= type
"center") (org-element-center-block-parser))
2715 ((string= type
"quote") (org-element-quote-block-parser))
2716 (t (org-element-special-block-parser)))))
2718 ((looking-at org-drawer-regexp
)
2719 (if (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
))
2720 (org-element-drawer-parser)
2721 (org-element-paragraph-parser)))
2722 ((looking-at "[ \t]*:\\( \\|$\\)") (org-element-fixed-width-parser))
2724 ((looking-at org-babel-block-lob-one-liner-regexp
)
2725 (org-element-babel-call-parser))
2726 ;; Keyword, or paragraph if at an affiliated keyword.
2727 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
2728 (let ((key (downcase (match-string 1))))
2729 (if (or (string= key
"tblfm")
2730 (member key org-element-affiliated-keywords
))
2731 (org-element-paragraph-parser)
2732 (org-element-keyword-parser))))
2733 ;; Footnote definition.
2734 ((looking-at org-footnote-definition-re
)
2735 (org-element-footnote-definition-parser))
2736 ;; Dynamic block or paragraph if incomplete.
2737 ((looking-at "[ \t]*#\\+begin:\\(?: \\|$\\)")
2739 (re-search-forward "^[ \t]*#\\+end:\\(?: \\|$\\)" nil t
))
2740 (org-element-dynamic-block-parser)
2741 (org-element-paragraph-parser)))
2743 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)")
2744 (org-element-comment-parser))
2746 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
2747 (org-element-horizontal-rule-parser))
2749 ((org-at-table-p t
) (org-element-table-parser))
2751 ((looking-at (org-item-re))
2752 (if (eq special
'item
)
2753 (org-element-item-parser (or structure
(org-list-struct)))
2754 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2755 ;; Default element: Paragraph.
2756 (t (org-element-paragraph-parser))))))
2759 ;; Most elements can have affiliated keywords. When looking for an
2760 ;; element beginning, we want to move before them, as they belong to
2761 ;; that element, and, in the meantime, collect information they give
2762 ;; into appropriate properties. Hence the following function.
2764 ;; Usage of optional arguments may not be obvious at first glance:
2766 ;; - TRANS-LIST is used to polish keywords names that have evolved
2767 ;; during Org history. In example, even though =result= and
2768 ;; =results= coexist, we want to have them under the same =result=
2769 ;; property. It's also true for "srcname" and "name", where the
2770 ;; latter seems to be preferred nowadays (thus the "name" property).
2772 ;; - CONSED allows to regroup multi-lines keywords under the same
2773 ;; property, while preserving their own identity. This is mostly
2774 ;; used for "attr_latex" and al.
2776 ;; - PARSED prepares a keyword value for export. This is useful for
2777 ;; "caption". Objects restrictions for such keywords are defined in
2778 ;; `org-element-string-restrictions'.
2780 ;; - DUALS is used to take care of keywords accepting a main and an
2781 ;; optional secondary values. For example "results" has its
2782 ;; source's name as the main value, and may have an hash string in
2783 ;; optional square brackets as the secondary one.
2785 ;; A keyword may belong to more than one category.
2787 (defconst org-element--affiliated-re
2788 (format "[ \t]*#\\+\\(%s\\):"
2791 (if (member keyword org-element-dual-keywords
)
2792 (format "\\(%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
2793 (regexp-quote keyword
))
2794 (regexp-quote keyword
)))
2795 org-element-affiliated-keywords
"\\|"))
2796 "Regexp matching any affiliated keyword.
2798 Keyword name is put in match group 1. Moreover, if keyword
2799 belongs to `org-element-dual-keywords', put the dual value in
2802 Don't modify it, set `org-element-affiliated-keywords' instead.")
2804 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
2805 consed parsed duals
)
2806 "Collect affiliated keywords before point.
2808 Optional argument KEY-RE is a regexp matching keywords, which
2809 puts matched keyword in group 1. It defaults to
2810 `org-element--affiliated-re'.
2812 TRANS-LIST is an alist where key is the keyword and value the
2813 property name it should be translated to, without the colons. It
2814 defaults to `org-element-keyword-translation-alist'.
2816 CONSED is a list of strings. Any keyword belonging to that list
2817 will have its value consed. The check is done after keyword
2818 translation. It defaults to `org-element-multiple-keywords'.
2820 PARSED is a list of strings. Any keyword member of this list
2821 will have its value parsed. The check is done after keyword
2822 translation. If a keyword is a member of both CONSED and PARSED,
2823 it's value will be a list of parsed strings. It defaults to
2824 `org-element-parsed-keywords'.
2826 DUALS is a list of strings. Any keyword member of this list can
2827 have two parts: one mandatory and one optional. Its value is
2828 a cons cell whose car is the former, and the cdr the latter. If
2829 a keyword is a member of both PARSED and DUALS, both values will
2830 be parsed. It defaults to `org-element-dual-keywords'.
2832 Return a list whose car is the position at the first of them and
2833 cdr a plist of keywords and values."
2835 (let ((case-fold-search t
)
2836 (key-re (or key-re org-element--affiliated-re
))
2837 (trans-list (or trans-list org-element-keyword-translation-alist
))
2838 (consed (or consed org-element-multiple-keywords
))
2839 (parsed (or parsed org-element-parsed-keywords
))
2840 (duals (or duals org-element-dual-keywords
))
2841 ;; RESTRICT is the list of objects allowed in parsed
2843 (restrict (cdr (assq 'keyword org-element-string-restrictions
)))
2846 (while (and (not (bobp))
2847 (progn (forward-line -
1) (looking-at key-re
)))
2848 (let* ((raw-kwd (downcase (or (match-string 2) (match-string 1))))
2849 ;; Apply translation to RAW-KWD. From there, KWD is
2850 ;; the official keyword.
2851 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
2852 ;; Find main value for any keyword.
2856 (buffer-substring-no-properties
2857 (match-end 0) (point-at-eol)))))
2858 ;; If KWD is a dual keyword, find its secondary
2859 ;; value. Maybe parse it.
2861 (and (member kwd duals
)
2862 (let ((sec (org-match-string-no-properties 3)))
2863 (if (or (not sec
) (not (member kwd parsed
))) sec
2864 (org-element-parse-secondary-string sec restrict
)))))
2865 ;; Attribute a property name to KWD.
2866 (kwd-sym (and kwd
(intern (concat ":" kwd
)))))
2867 ;; Now set final shape for VALUE.
2868 (when (member kwd parsed
)
2869 (setq value
(org-element-parse-secondary-string value restrict
)))
2870 (when (member kwd duals
)
2871 ;; VALUE is mandatory. Set it to nil if there is none.
2872 (setq value
(and value
(cons value dual-value
))))
2873 (when (member kwd consed
)
2874 (setq value
(cons value
(plist-get output kwd-sym
))))
2875 ;; Eventually store the new value in OUTPUT.
2876 (setq output
(plist-put output kwd-sym value
))))
2877 (unless (looking-at key-re
) (forward-line 1)))
2878 (list (point) output
))))
2884 ;; The two major functions here are `org-element-parse-buffer', which
2885 ;; parses Org syntax inside the current buffer, taking into account
2886 ;; region, narrowing, or even visibility if specified, and
2887 ;; `org-element-parse-secondary-string', which parses objects within
2890 ;; The (almost) almighty `org-element-map' allows to apply a function
2891 ;; on elements or objects matching some type, and accumulate the
2892 ;; resulting values. In an export situation, it also skips unneeded
2893 ;; parts of the parse tree.
2895 (defun org-element-parse-buffer (&optional granularity visible-only
)
2896 "Recursively parse the buffer and return structure.
2897 If narrowing is in effect, only parse the visible part of the
2900 Optional argument GRANULARITY determines the depth of the
2901 recursion. It can be set to the following symbols:
2903 `headline' Only parse headlines.
2904 `greater-element' Don't recurse into greater elements. Thus,
2905 elements parsed are the top-level ones.
2906 `element' Parse everything but objects and plain text.
2907 `object' Parse the complete buffer (default).
2909 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
2912 Assume buffer is in Org mode."
2914 (goto-char (point-min))
2915 (org-skip-whitespace)
2916 (nconc (list 'org-data nil
)
2917 (org-element-parse-elements
2918 (point-at-bol) (point-max)
2919 ;; Start is section mode so text before the first headline
2920 ;; belongs to a section.
2921 'section nil granularity visible-only nil
))))
2923 (defun org-element-parse-secondary-string (string restriction
&optional buffer
)
2924 "Recursively parse objects in STRING and return structure.
2926 RESTRICTION, when non-nil, is a symbol limiting the object types
2927 that will be looked after.
2929 Optional argument BUFFER indicates the buffer from where the
2930 secondary string was extracted. It is used to determine where to
2931 get extraneous information for an object \(i.e. when resolving
2932 a link or looking for a footnote definition\). It defaults to
2933 the current buffer."
2936 (org-element-parse-objects (point-min) (point-max) nil restriction
)))
2938 (defun org-element-map (data types fun
&optional info first-match no-recursion
)
2939 "Map a function on selected elements or objects.
2941 DATA is the parsed tree, as returned by, i.e,
2942 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
2943 of elements or objects types. FUN is the function called on the
2944 matching element or object. It must accept one arguments: the
2945 element or object itself.
2947 When optional argument INFO is non-nil, it should be a plist
2948 holding export options. In that case, parts of the parse tree
2949 not exportable according to that property list will be skipped.
2951 When optional argument FIRST-MATCH is non-nil, stop at the first
2952 match for which FUN doesn't return nil, and return that value.
2954 Optional argument NO-RECURSION is a symbol or a list of symbols
2955 representing elements or objects types. `org-element-map' won't
2956 enter any recursive element or object whose type belongs to that
2957 list. Though, FUN can still be applied on them.
2959 Nil values returned from FUN do not appear in the results."
2960 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
2961 (unless (listp types
) (setq types
(list types
)))
2962 (unless (listp no-recursion
) (setq no-recursion
(list no-recursion
)))
2963 ;; Recursion depth is determined by --CATEGORY.
2966 ((loop for type in types
2967 always
(memq type org-element-greater-elements
))
2969 ((loop for type in types
2970 always
(memq type org-element-all-elements
))
2973 ;; --RESTRICTS is a list of element types whose secondary
2974 ;; string could possibly contain an object with a type among
2977 (and (eq --category
'objects
)
2978 (loop for el in org-element-secondary-value-alist
2980 (loop for o in types
2984 org-element-string-restrictions
))))
2990 ;; Recursively walk DATA. INFO, if non-nil, is
2991 ;; a plist holding contextual information.
2994 (unless (and info
(member --blob
(plist-get info
:ignore-list
)))
2995 (let ((--type (org-element-type --blob
)))
2996 ;; Check if TYPE is matching among TYPES. If so,
2997 ;; apply FUN to --BLOB and accumulate return value
2998 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
2999 (when (memq --type types
)
3000 (let ((result (funcall fun --blob
)))
3001 (cond ((not result
))
3002 (first-match (throw 'first-match result
))
3003 (t (push result --acc
)))))
3004 ;; If --BLOB has a secondary string that can
3005 ;; contain objects with their type among TYPES,
3006 ;; look into that string.
3007 (when (memq --type --restricts
)
3012 ,@(org-element-property
3013 (cdr (assq --type org-element-secondary-value-alist
))
3015 ;; Now determine if a recursion into --BLOB is
3016 ;; possible. If so, do it.
3017 (unless (memq --type no-recursion
)
3018 (when (or (and (memq --type org-element-greater-elements
)
3019 (not (eq --category
'greater-elements
)))
3020 (and (memq --type org-element-all-elements
)
3021 (not (eq --category
'elements
)))
3022 (memq --type org-element-recursive-objects
))
3023 (funcall --walk-tree --blob
))))))
3024 (org-element-contents --data
))))))
3026 (funcall --walk-tree data
)
3027 ;; Return value in a proper order.
3030 ;; The following functions are internal parts of the parser.
3032 ;; The first one, `org-element-parse-elements' acts at the element's
3035 ;; The second one, `org-element-parse-objects' applies on all objects
3036 ;; of a paragraph or a secondary string. It uses
3037 ;; `org-element-get-candidates' to optimize the search of the next
3038 ;; object in the buffer.
3040 ;; More precisely, that function looks for every allowed object type
3041 ;; first. Then, it discards failed searches, keeps further matches,
3042 ;; and searches again types matched behind point, for subsequent
3043 ;; calls. Thus, searching for a given type fails only once, and every
3044 ;; object is searched only once at top level (but sometimes more for
3047 (defun org-element-parse-elements
3048 (beg end special structure granularity visible-only acc
)
3049 "Parse elements between BEG and END positions.
3051 SPECIAL prioritize some elements over the others. It can set to
3052 `quote-section', `section' or `item', which will focus search,
3053 respectively, on quote sections, sections and items. Moreover,
3054 when value is `item', STRUCTURE will be used as the current list
3057 GRANULARITY determines the depth of the recursion. It can be set
3058 to the following symbols:
3060 `headline' Only parse headlines.
3061 `greater-element' Don't recurse into greater elements. Thus,
3062 elements parsed are the top-level ones.
3063 `element' Parse everything but objects and plain text.
3064 `object' or nil Parse the complete buffer.
3066 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3069 Elements are accumulated into ACC."
3072 (narrow-to-region beg end
)
3074 ;; When parsing only headlines, skip any text before first one.
3075 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3076 (org-with-limited-levels (outline-next-heading)))
3080 ;; 1. Item mode is active: point must be at an item. Parse it
3081 ;; directly, skipping `org-element-current-element'.
3082 (if (eq special
'item
)
3083 (let ((element (org-element-item-parser structure
)))
3084 (goto-char (org-element-property :end element
))
3085 (org-element-parse-elements
3086 (org-element-property :contents-begin element
)
3087 (org-element-property :contents-end element
)
3088 nil structure granularity visible-only
(reverse element
)))
3089 ;; 2. When ITEM is nil, find current element's type and parse
3090 ;; it accordingly to its category.
3091 (let* ((element (org-element-current-element special structure
))
3092 (type (org-element-type element
)))
3093 (goto-char (org-element-property :end element
))
3095 ;; Case 1. ELEMENT is a paragraph. Parse objects inside,
3096 ;; if GRANULARITY allows it.
3097 ((and (eq type
'paragraph
)
3098 (or (not granularity
) (eq granularity
'object
)))
3099 (org-element-parse-objects
3100 (org-element-property :contents-begin element
)
3101 (org-element-property :contents-end element
)
3102 (reverse element
) nil
))
3103 ;; Case 2. ELEMENT is recursive: parse it between
3104 ;; `contents-begin' and `contents-end'. Make sure
3105 ;; GRANULARITY allows the recursion, or ELEMENT is an
3106 ;; headline, in which case going inside is mandatory, in
3107 ;; order to get sub-level headings. If VISIBLE-ONLY is
3108 ;; true and element is hidden, do not recurse into it.
3109 ((and (memq type org-element-greater-elements
)
3110 (or (not granularity
)
3111 (memq granularity
'(element object
))
3112 (eq type
'headline
))
3113 (not (and visible-only
3114 (org-element-property :hiddenp element
))))
3115 (org-element-parse-elements
3116 (org-element-property :contents-begin element
)
3117 (org-element-property :contents-end element
)
3118 ;; At a plain list, switch to item mode. At an
3119 ;; headline, switch to section mode. Any other
3120 ;; element turns off special modes.
3123 (headline (if (org-element-property :quotedp element
)
3126 (org-element-property :structure element
)
3127 granularity visible-only
(reverse element
)))
3128 ;; Case 3. Else, just accumulate ELEMENT.
3134 (defun org-element-parse-objects (beg end acc restriction
)
3135 "Parse objects between BEG and END and return recursive structure.
3137 Objects are accumulated in ACC.
3139 RESTRICTION, when non-nil, is a list of object types which are
3140 allowed in the current object."
3141 (let ((get-next-object
3144 ;; Return the parsing function associated to the nearest
3145 ;; object among list of candidates CAND.
3146 (let ((pos (apply #'min
(mapcar #'cdr cand
))))
3151 (format "org-element-%s-parser" (car (rassq pos cand
))))))))))
3152 next-object candidates
)
3155 (while (setq candidates
(org-element-get-next-object-candidates
3156 end restriction candidates
))
3157 (setq next-object
(funcall get-next-object candidates
))
3158 ;; 1. Text before any object. Untabify it.
3159 (let ((obj-beg (org-element-property :begin next-object
)))
3160 (unless (= (point) obj-beg
)
3161 (push (replace-regexp-in-string
3162 "\t" (make-string tab-width ?
)
3163 (buffer-substring-no-properties (point) obj-beg
))
3166 (let ((obj-end (org-element-property :end next-object
))
3167 (cont-beg (org-element-property :contents-begin next-object
)))
3168 (push (if (and (memq (car next-object
) org-element-recursive-objects
)
3170 ;; ... recursive. The CONT-BEG check is for
3171 ;; links, as some of them might not be recursive
3172 ;; (i.e. plain links).
3176 (org-element-property :contents-end next-object
))
3177 (org-element-parse-objects
3178 (point-min) (point-max) (reverse next-object
)
3179 ;; Restrict allowed objects. This is the
3180 ;; intersection of current restriction and next
3181 ;; object's restriction.
3183 (cdr (assq (car next-object
)
3184 org-element-object-restrictions
))))
3185 (if (not restriction
) new-restr
3187 (lambda (e) (and (memq e restriction
) e
))
3189 ;; ... not recursive.
3192 (goto-char obj-end
)))
3193 ;; 3. Text after last object. Untabify it.
3194 (unless (= (point) end
)
3195 (push (replace-regexp-in-string
3196 "\t" (make-string tab-width ?
)
3197 (buffer-substring-no-properties (point) end
))
3202 (defun org-element-get-next-object-candidates (limit restriction objects
)
3203 "Return an alist of candidates for the next object.
3205 LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
3206 the possible object types.
3208 Return value is an alist whose car is position and cdr the object
3209 type, as a string. There is an association for the closest
3210 object of each type within RESTRICTION when non-nil, or for every
3213 OBJECTS is the previous candidates alist."
3214 (let ((restriction (or restriction org-element-all-successors
))
3215 next-candidates types-to-search
)
3216 ;; If no previous result, search every object type in RESTRICTION.
3217 ;; Otherwise, keep potential candidates (old objects located after
3218 ;; point) and ask to search again those which had matched before.
3219 (if (not objects
) (setq types-to-search restriction
)
3221 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3222 (push obj next-candidates
)))
3224 ;; Call the appropriate "get-next" function for each type to
3225 ;; search and accumulate matches.
3228 (let* ((successor-fun
3230 (format "org-element-%s-successor"
3231 (or (cdr (assq type org-element-object-successor-alist
))
3233 (obj (funcall successor-fun limit
)))
3234 (and obj
(push obj next-candidates
))))
3241 ;;; Towards A Bijective Process
3243 ;; The parse tree obtained with `org-element-parse-buffer' is really
3244 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3245 ;; interpreted and expanded into a string with canonical Org
3246 ;; syntax. Hence `org-element-interpret-data'.
3248 ;; Data parsed from secondary strings, whose shape is slightly
3249 ;; different than the standard parse tree, is expanded with the
3250 ;; equivalent function `org-element-interpret-secondary'.
3252 ;; Both functions rely internally on
3253 ;; `org-element-interpret--affiliated-keywords'.
3255 (defun org-element-interpret-data (data &optional genealogy previous
)
3256 "Interpret a parse tree representing Org data.
3258 DATA is the parse tree to interpret.
3260 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3262 GENEALOGY is the list of its parents types.
3263 PREVIOUS is the type of the element or object at the same level
3266 Return Org syntax as a string."
3269 ;; BLOB can be an element, an object, a string, or nil.
3272 ((equal blob
"") nil
)
3273 ((stringp blob
) blob
)
3275 (let* ((type (org-element-type blob
))
3277 (if (eq type
'org-data
) 'identity
3278 (intern (format "org-element-%s-interpreter" type
))))
3281 ;; Full Org document.
3282 ((eq type
'org-data
)
3283 (org-element-interpret-data blob genealogy previous
))
3284 ;; Recursive objects.
3285 ((memq type org-element-recursive-objects
)
3286 (org-element-interpret-data
3287 blob
(cons type genealogy
) nil
))
3288 ;; Recursive elements.
3289 ((memq type org-element-greater-elements
)
3290 (org-element-normalize-string
3291 (org-element-interpret-data
3292 blob
(cons type genealogy
) nil
)))
3294 ((eq type
'paragraph
)
3296 (org-element-normalize-contents
3298 ;; When normalizing contents of an item,
3299 ;; ignore first line's indentation.
3301 (memq (car genealogy
)
3302 '(footnote-definiton item
))))))
3303 (org-element-interpret-data
3304 paragraph
(cons type genealogy
) nil
)))))
3305 (results (funcall interpreter blob contents
)))
3307 (setq previous type
)
3308 ;; Build white spaces.
3310 ((eq type
'org-data
) results
)
3311 ((memq type org-element-all-elements
)
3313 (org-element-interpret--affiliated-keywords blob
)
3314 (org-element-normalize-string results
)
3315 (make-string (org-element-property :post-blank blob
) 10)))
3318 (make-string (org-element-property :post-blank blob
) 32))))))))
3319 (org-element-contents data
) ""))
3321 (defun org-element-interpret-secondary (secondary)
3322 "Interpret SECONDARY string as Org syntax.
3324 SECONDARY-STRING is a nested list as returned by
3325 `org-element-parse-secondary-string'.
3327 Return interpreted string."
3328 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3329 (let ((s (if (listp secondary
) secondary
(list secondary
))))
3330 (org-element-interpret-data `(org-data nil
,@s
) nil nil
)))
3332 ;; Both functions internally use `org-element--affiliated-keywords'.
3334 (defun org-element-interpret--affiliated-keywords (element)
3335 "Return ELEMENT's affiliated keywords as Org syntax.
3336 If there is no affiliated keyword, return the empty string."
3337 (let ((keyword-to-org
3341 (when (member key org-element-dual-keywords
)
3342 (setq dual
(cdr value
) value
(car value
)))
3343 (concat "#+" key
(and dual
(format "[%s]" dual
)) ": "
3344 (if (member key org-element-parsed-keywords
)
3345 (org-element-interpret-secondary value
)
3350 (let ((value (org-element-property (intern (concat ":" key
)) element
)))
3352 (if (member key org-element-multiple-keywords
)
3353 (mapconcat (lambda (line)
3354 (funcall keyword-to-org key line
))
3356 (funcall keyword-to-org key value
)))))
3357 ;; Remove translated keywords.
3361 (and (not (assoc key org-element-keyword-translation-alist
)) key
))
3362 org-element-affiliated-keywords
))
3365 ;; Because interpretation of the parse tree must return the same
3366 ;; number of blank lines between elements and the same number of white
3367 ;; space after objects, some special care must be given to white
3370 ;; The first function, `org-element-normalize-string', ensures any
3371 ;; string different from the empty string will end with a single
3372 ;; newline character.
3374 ;; The second function, `org-element-normalize-contents', removes
3375 ;; global indentation from the contents of the current element.
3377 (defun org-element-normalize-string (s)
3378 "Ensure string S ends with a single newline character.
3380 If S isn't a string return it unchanged. If S is the empty
3381 string, return it. Otherwise, return a new string with a single
3382 newline character at its end."
3384 ((not (stringp s
)) s
)
3386 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3387 (replace-match "\n" nil nil s
)))))
3389 (defun org-element-normalize-contents (element &optional ignore-first
)
3390 "Normalize plain text in ELEMENT's contents.
3392 ELEMENT must only contain plain text and objects.
3394 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3395 indentation to compute maximal common indentation.
3397 Return the normalized element that is element with global
3398 indentation removed from its contents. The function assumes that
3399 indentation is not done with TAB characters."
3403 ;; Return list of indentations within BLOB. This is done by
3404 ;; walking recursively BLOB and updating IND-LIST along the
3405 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3406 ;; been seen yet. It is required as this string is the only
3407 ;; one whose indentation doesn't happen after a newline
3409 (lambda (blob first-flag
)
3412 (when (and first-flag
(stringp object
))
3413 (setq first-flag nil
)
3414 (string-match "\\`\\( *\\)" object
)
3415 (let ((len (length (match-string 1 object
))))
3416 ;; An indentation of zero means no string will be
3417 ;; modified. Quit the process.
3418 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
3419 (push len ind-list
))))
3423 (while (string-match "\n\\( *\\)" object start
)
3424 (setq start
(match-end 0))
3425 (push (length (match-string 1 object
)) ind-list
))))
3426 ((memq (org-element-type object
) org-element-recursive-objects
)
3427 (funcall collect-inds object first-flag
))))
3428 (org-element-contents blob
))))))
3429 ;; Collect indentation list in ELEMENT. Possibly remove first
3430 ;; value if IGNORE-FIRST is non-nil.
3431 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
3432 (if (not ind-list
) element
3433 ;; Build ELEMENT back, replacing each string with the same
3434 ;; string minus common indentation.
3437 (lambda (blob mci first-flag
)
3438 ;; Return BLOB with all its strings indentation
3439 ;; shortened from MCI white spaces. FIRST-FLAG is
3440 ;; non-nil when the first string hasn't been seen
3443 (list (org-element-type blob
) (nth 1 blob
))
3446 (when (and first-flag
(stringp object
))
3447 (setq first-flag nil
)
3449 (replace-regexp-in-string
3450 (format "\\` \\{%d\\}" mci
) "" object
)))
3453 (replace-regexp-in-string
3454 (format "\n \\{%d\\}" mci
) "\n" object
))
3455 ((memq (org-element-type object
) org-element-recursive-objects
)
3456 (funcall build object mci first-flag
))
3458 (org-element-contents blob
)))))))
3459 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
3465 ;; The first move is to implement a way to obtain the smallest element
3466 ;; containing point. This is the job of `org-element-at-point'. It
3467 ;; basically jumps back to the beginning of section containing point
3468 ;; and moves, element after element, with
3469 ;; `org-element-current-element' until the container is found.
3471 (defun org-element-at-point (&optional keep-trail
)
3472 "Determine closest element around point.
3474 Return value is a list like (TYPE PROPS) where TYPE is the type
3475 of the element and PROPS a plist of properties associated to the
3476 element. Possible types are defined in
3477 `org-element-all-elements'.
3479 As a special case, if point is at the very beginning of a list or
3480 sub-list, element returned will be that list instead of the first
3483 If optional argument KEEP-TRAIL is non-nil, the function returns
3484 a list of of elements leading to element at point. The list's
3485 CAR is always the element at point. Its last item will be the
3486 element's parent, unless element was either the first in its
3487 section (in which case the last item in the list is the first
3488 element of section) or an headline (in which case the list
3489 contains that headline as its single element). Elements
3490 in-between, if any, are siblings of the element at point."
3491 (org-with-wide-buffer
3492 ;; If at an headline, parse it. It is the sole element that
3493 ;; doesn't require to know about context.
3494 (if (org-with-limited-levels (org-at-heading-p))
3495 (if (not keep-trail
) (org-element-headline-parser)
3496 (list (org-element-headline-parser)))
3497 ;; Otherwise move at the beginning of the section containing
3499 (let ((origin (point)) element type item-flag trail struct prevs
)
3500 (org-with-limited-levels
3501 (if (org-before-first-heading-p) (goto-char (point-min))
3502 (org-back-to-heading)
3504 (org-skip-whitespace)
3506 ;; Starting parsing successively each element with
3507 ;; `org-element-current-element'. Skip those ending before
3508 ;; original position.
3511 (setq element
(org-element-current-element item-flag struct
)
3513 (when keep-trail
(push element trail
))
3515 ;; 1. Skip any element ending before point or at point.
3516 ((let ((end (org-element-property :end element
)))
3517 (when (<= end origin
)
3518 (if (> (point-max) end
) (goto-char end
)
3519 (throw 'exit
(or trail element
))))))
3520 ;; 2. An element containing point is always the element at
3522 ((not (memq type org-element-greater-elements
))
3523 (throw 'exit
(if keep-trail trail element
)))
3524 ;; 3. At a plain list.
3525 ((eq type
'plain-list
)
3526 (setq struct
(org-element-property :structure element
)
3527 prevs
(or prevs
(org-list-prevs-alist struct
)))
3528 (let ((beg (org-element-property :contents-begin element
)))
3529 (if (= beg origin
) (throw 'exit
(or trail element
))
3530 ;; Find the item at this level containing ORIGIN.
3531 (let ((items (org-list-get-all-items beg struct prevs
)))
3537 ;; Item ends before point: skip it.
3538 ((<= (org-list-get-item-end pos struct
) origin
))
3539 ;; Item contains point: store is in PARENT.
3540 ((<= pos origin
) (setq parent pos
))
3541 ;; We went too far: return PARENT.
3542 (t (throw 'local nil
)))) items
))
3543 ;; No parent: no item contained point, though
3544 ;; the plain list does. Point is in the blank
3545 ;; lines after the list: return plain list.
3546 (if (not parent
) (throw 'exit
(or trail element
))
3547 (setq item-flag
'item
)
3548 (goto-char parent
)))))))
3549 ;; 4. At any other greater element type, if point is
3550 ;; within contents, move into it. Otherwise, return
3553 (when (eq type
'item
) (setq item-flag nil
))
3554 (let ((beg (org-element-property :contents-begin element
))
3555 (end (org-element-property :contents-end element
)))
3556 (if (or (> beg origin
) (< end origin
))
3557 (throw 'exit
(or trail element
))
3558 ;; Reset trail, since we found a parent.
3559 (when keep-trail
(setq trail
(list element
)))
3560 (narrow-to-region beg end
)
3561 (goto-char beg
)))))))))))
3564 ;; Once the local structure around point is well understood, it's easy
3565 ;; to implement some replacements for `forward-paragraph'
3566 ;; `backward-paragraph', namely `org-element-forward' and
3567 ;; `org-element-backward'.
3569 ;; Also, `org-transpose-elements' mimics the behaviour of
3570 ;; `transpose-words', at the element's level, whereas
3571 ;; `org-element-drag-forward', `org-element-drag-backward', and
3572 ;; `org-element-up' generalize, respectively, functions
3573 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3575 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3576 ;; smartly remove global indentation from buffer, making it possible
3577 ;; to use Org indent mode on a file created with hard indentation.
3579 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3580 ;; internally by some of the previously cited tools.
3582 (defsubst org-element-nested-p
(elem-A elem-B
)
3583 "Non-nil when elements ELEM-A and ELEM-B are nested."
3584 (let ((beg-A (org-element-property :begin elem-A
))
3585 (beg-B (org-element-property :begin elem-B
))
3586 (end-A (org-element-property :end elem-A
))
3587 (end-B (org-element-property :end elem-B
)))
3588 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
3589 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
3591 (defun org-element-swap-A-B (elem-A elem-B
)
3592 "Swap elements ELEM-A and ELEM-B.
3594 Leave point at the end of ELEM-A.
3596 Assume ELEM-A is before ELEM-B and that they are not nested."
3597 (goto-char (org-element-property :begin elem-A
))
3598 (let* ((beg-B (org-element-property :begin elem-B
))
3599 (end-B-no-blank (save-excursion
3600 (goto-char (org-element-property :end elem-B
))
3601 (skip-chars-backward " \r\t\n")
3604 (beg-A (org-element-property :begin elem-A
))
3605 (end-A-no-blank (save-excursion
3606 (goto-char (org-element-property :end elem-A
))
3607 (skip-chars-backward " \r\t\n")
3610 (body-A (buffer-substring beg-A end-A-no-blank
))
3611 (body-B (buffer-substring beg-B end-B-no-blank
))
3612 (between-A-B (buffer-substring end-A-no-blank beg-B
)))
3613 (delete-region beg-A end-B-no-blank
)
3614 (insert body-B between-A-B body-A
)
3615 (goto-char (org-element-property :end elem-B
))))
3617 (defun org-element-backward ()
3618 "Move backward by one element.
3619 Move to the previous element at the same level, when possible."
3621 (if (save-excursion (skip-chars-backward " \r\t\n") (bobp))
3622 (error "Cannot move further up")
3623 (let* ((trail (org-element-at-point 'keep-trail
))
3624 (element (car trail
))
3625 (beg (org-element-property :begin element
)))
3626 ;; Move to beginning of current element if point isn't there.
3627 (if (/= (point) beg
) (goto-char beg
)
3628 (let ((type (org-element-type element
)))
3630 ;; At an headline: move to previous headline at the same
3631 ;; level, a parent, or BOB.
3632 ((eq type
'headline
)
3633 (let ((dest (save-excursion (org-backward-same-level 1) (point))))
3634 (if (= (point-min) dest
) (error "Cannot move further up")
3636 ;; At an item: try to move to the previous item, if any.
3637 ((and (eq type
'item
)
3638 (let* ((struct (org-element-property :structure element
))
3639 (prev (org-list-get-prev-item
3640 beg struct
(org-list-prevs-alist struct
))))
3641 (when prev
(goto-char prev
)))))
3642 ;; In any other case, find the previous element in the
3643 ;; trail and move to its beginning. If no previous element
3644 ;; can be found, move to headline.
3645 (t (let ((prev (nth 1 trail
)))
3646 (if prev
(goto-char (org-element-property :begin prev
))
3647 (org-back-to-heading))))))))))
3649 (defun org-element-drag-backward ()
3650 "Drag backward element at point."
3652 (let* ((pos (point))
3653 (elem (org-element-at-point)))
3654 (when (= (progn (goto-char (point-min))
3655 (org-skip-whitespace)
3657 (org-element-property :end elem
))
3658 (error "Cannot drag element backward"))
3659 (goto-char (org-element-property :begin elem
))
3660 (org-element-backward)
3661 (let ((prev-elem (org-element-at-point)))
3662 (when (or (org-element-nested-p elem prev-elem
)
3663 (and (eq (car elem
) 'headline
)
3664 (not (eq (car prev-elem
) 'headline
))))
3666 (error "Cannot drag element backward"))
3667 ;; Compute new position of point: it's shifted by PREV-ELEM
3669 (let ((size-prev (- (org-element-property :end prev-elem
)
3670 (org-element-property :begin prev-elem
))))
3671 (org-element-swap-A-B prev-elem elem
)
3672 (goto-char (- pos size-prev
))))))
3674 (defun org-element-drag-forward ()
3675 "Move forward element at point."
3677 (let* ((pos (point))
3678 (elem (org-element-at-point)))
3679 (when (= (point-max) (org-element-property :end elem
))
3680 (error "Cannot drag element forward"))
3681 (goto-char (org-element-property :end elem
))
3682 (let ((next-elem (org-element-at-point)))
3683 (when (or (org-element-nested-p elem next-elem
)
3684 (and (eq (car next-elem
) 'headline
)
3685 (not (eq (car elem
) 'headline
))))
3687 (error "Cannot drag element forward"))
3688 ;; Compute new position of point: it's shifted by NEXT-ELEM
3689 ;; body's length (without final blanks) and by the length of
3690 ;; blanks between ELEM and NEXT-ELEM.
3691 (let ((size-next (- (save-excursion
3692 (goto-char (org-element-property :end next-elem
))
3693 (skip-chars-backward " \r\t\n")
3696 (org-element-property :begin next-elem
)))
3697 (size-blank (- (org-element-property :end elem
)
3699 (goto-char (org-element-property :end elem
))
3700 (skip-chars-backward " \r\t\n")
3703 (org-element-swap-A-B elem next-elem
)
3704 (goto-char (+ pos size-next size-blank
))))))
3706 (defun org-element-forward ()
3707 "Move forward by one element.
3708 Move to the next element at the same level, when possible."
3710 (if (eobp) (error "Cannot move further down")
3711 (let* ((trail (org-element-at-point 'keep-trail
))
3712 (element (car trail
))
3713 (type (org-element-type element
))
3714 (end (org-element-property :end element
)))
3716 ;; At an headline, move to next headline at the same level.
3717 ((eq type
'headline
) (goto-char end
))
3718 ;; At an item. Move to the next item, if possible.
3719 ((and (eq type
'item
)
3720 (let* ((struct (org-element-property :structure element
))
3721 (prevs (org-list-prevs-alist struct
))
3722 (beg (org-element-property :begin element
))
3723 (next-item (org-list-get-next-item beg struct prevs
)))
3724 (when next-item
(goto-char next-item
)))))
3725 ;; In any other case, move to element's end, unless this
3726 ;; position is also the end of its parent's contents, in which
3727 ;; case, directly jump to parent's end.
3730 ;; Determine if TRAIL contains the real parent of ELEMENT.
3731 (and (> (length trail
) 1)
3732 (let* ((parent-candidate (car (last trail
))))
3733 (and (memq (org-element-type parent-candidate
)
3734 org-element-greater-elements
)
3735 (>= (org-element-property
3736 :contents-end parent-candidate
) end
)
3737 parent-candidate
)))))
3738 (cond ((not parent
) (goto-char end
))
3739 ((= (org-element-property :contents-end parent
) end
)
3740 (goto-char (org-element-property :end parent
)))
3741 (t (goto-char end
)))))))))
3743 (defun org-element-mark-element ()
3744 "Put point at beginning of this element, mark at end.
3746 Interactively, if this command is repeated or (in Transient Mark
3747 mode) if the mark is active, it marks the next element after the
3748 ones already marked."
3750 (let (deactivate-mark)
3751 (if (or (and (eq last-command this-command
) (mark t
))
3752 (and transient-mark-mode mark-active
))
3756 (goto-char (org-element-property :end
(org-element-at-point)))))
3757 (let ((element (org-element-at-point)))
3759 (push-mark (org-element-property :end element
) t t
)
3760 (goto-char (org-element-property :begin element
))))))
3762 (defun org-narrow-to-element ()
3763 "Narrow buffer to current element."
3765 (let ((elem (org-element-at-point)))
3767 ((eq (car elem
) 'headline
)
3769 (org-element-property :begin elem
)
3770 (org-element-property :end elem
)))
3771 ((memq (car elem
) org-element-greater-elements
)
3773 (org-element-property :contents-begin elem
)
3774 (org-element-property :contents-end elem
)))
3777 (org-element-property :begin elem
)
3778 (org-element-property :end elem
))))))
3780 (defun org-transpose-elements ()
3781 "Transpose current and previous elements, keeping blank lines between.
3782 Point is moved after both elements."
3784 (org-skip-whitespace)
3786 (cur (org-element-at-point)))
3787 (when (= (save-excursion (goto-char (point-min))
3788 (org-skip-whitespace)
3790 (org-element-property :begin cur
))
3791 (error "No previous element"))
3792 (goto-char (org-element-property :begin cur
))
3794 (let ((prev (org-element-at-point)))
3795 (when (org-element-nested-p cur prev
)
3797 (error "Cannot transpose nested elements"))
3798 (org-element-swap-A-B prev cur
))))
3800 (defun org-element-unindent-buffer ()
3801 "Un-indent the visible part of the buffer.
3802 Relative indentation \(between items, inside blocks, etc.\) isn't
3805 (unless (eq major-mode
'org-mode
)
3806 (error "Cannot un-indent a buffer not in Org mode"))
3807 (let* ((parse-tree (org-element-parse-buffer 'greater-element
))
3808 unindent-tree
; For byte-compiler.
3812 (mapc (lambda (element)
3813 (if (eq (org-element-type element
) 'headline
)
3814 (funcall unindent-tree
3815 (org-element-contents element
))
3819 (org-element-property :begin element
)
3820 (org-element-property :end element
))
3821 (org-do-remove-indentation)))))
3822 (reverse contents
))))))
3823 (funcall unindent-tree
(org-element-contents parse-tree
))))
3825 (defun org-element-up ()
3826 "Move to upper element."
3829 ((bobp) (error "No surrounding element"))
3830 ((org-with-limited-levels (org-at-heading-p))
3831 (or (org-up-heading-safe) (error "No surronding element")))
3833 (let* ((trail (org-element-at-point 'keep-trail
))
3834 (element (car trail
))
3835 (type (org-element-type element
)))
3837 ;; At an item, with a parent in the list: move to that parent.
3838 ((and (eq type
'item
)
3839 (let* ((beg (org-element-property :begin element
))
3840 (struct (org-element-property :structure element
))
3841 (parents (org-list-parents-alist struct
))
3842 (parentp (org-list-get-parent beg struct parents
)))
3843 (and parentp
(goto-char parentp
)))))
3844 ;; Determine parent in the trail.
3847 (and (> (length trail
) 1)
3848 (let ((parentp (car (last trail
))))
3849 (and (memq (org-element-type parentp
)
3850 org-element-greater-elements
)
3851 (>= (org-element-property :contents-end parentp
)
3852 (org-element-property :end element
))
3855 ;; When parent is found move to its beginning.
3856 (parent (goto-char (org-element-property :begin parent
)))
3857 ;; If no parent was found, move to headline above, if any
3858 ;; or return an error.
3859 ((org-before-first-heading-p) (error "No surrounding element"))
3860 (t (org-back-to-heading))))))))))
3863 (provide 'org-element
)
3864 ;;; org-element.el ends here