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' and `special-block'.
48 ;; Greater elements (excepted `headline' and `item' types) and
49 ;; elements (excepted `keyword', `babel-call', and `property-drawer'
50 ;; types) can have a fixed set of keywords as attributes. Those are
51 ;; called "affiliated keywords", to distinguish them from others
52 ;; keywords, which are full-fledged elements. In particular, the
53 ;; "name" affiliated keyword allows to label almost any element in an
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 two accessors and a function
83 ;; retrieving the smallest element containing point (respectively
84 ;; `org-element-get-property', `org-element-get-contents' and
85 ;; `org-element-at-point').
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.
103 (eval-when-compile (require 'cl
))
105 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
110 ;; For each greater element type, we define a parser and an
113 ;; A parser (`item''s excepted) accepts no argument and represents the
114 ;; element or object as the list described above. An interpreter
115 ;; accepts two arguments: the list representation of the element or
116 ;; object, and its contents. The latter may be nil, depending on the
117 ;; element or object considered. It returns the appropriate Org
118 ;; syntax, as a string.
120 ;; Parsing functions must follow the naming convention:
121 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
122 ;; defined in `org-element-greater-elements'.
124 ;; Similarly, interpreting functions must follow the naming
125 ;; convention: org-element-TYPE-interpreter.
127 ;; With the exception of `headline' and `item' types, greater elements
128 ;; cannot contain other greater elements of their own type.
130 ;; Beside implementing a parser and an interpreter, adding a new
131 ;; greater element requires to tweak `org-element-guess-type'.
132 ;; Moreover, the newly defined type must be added to both
133 ;; `org-element-all-elements' and `org-element-greater-elements'.
137 (defun org-element-center-block-parser ()
138 "Parse a center block.
140 Return a list whose car is `center-block' and cdr is a plist
141 containing `:begin', `:end', `:hiddenp', `:contents-begin',
142 `:contents-end' and `:post-blank' keywords.
144 Assume point is at beginning or end of the block."
146 (let* ((case-fold-search t
)
150 (concat "^[ \t]*#\\+begin_center") nil t
)
151 (org-element-collect-affiliated-keywords)))
152 (begin (car keywords
))
153 (contents-begin (progn (forward-line) (point)))
154 (hidden (org-truely-invisible-p))
155 (contents-end (progn (re-search-forward
156 (concat "^[ \t]*#\\+end_center") nil t
)
158 (pos-before-blank (progn (forward-line) (point)))
159 (end (progn (org-skip-whitespace)
160 (if (eobp) (point) (point-at-bol)))))
165 :contents-begin
,contents-begin
166 :contents-end
,contents-end
167 :post-blank
,(count-lines pos-before-blank end
)
168 ,@(cadr keywords
))))))
170 (defun org-element-center-block-interpreter (center-block contents
)
171 "Interpret CENTER-BLOCK element as Org syntax.
172 CONTENTS is the contents of the element."
173 (format "#+begin_center\n%s#+end_center" contents
))
176 (defun org-element-drawer-parser ()
179 Return a list whose car is `drawer' and cdr is a plist containing
180 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
181 `:contents-end' and `:post-blank' keywords.
183 Assume point is at beginning of drawer."
185 (let* ((case-fold-search t
)
186 (name (progn (looking-at org-drawer-regexp
)
187 (org-match-string-no-properties 1)))
188 (keywords (org-element-collect-affiliated-keywords))
189 (begin (car keywords
))
190 (contents-begin (progn (forward-line) (point)))
191 (hidden (org-truely-invisible-p))
192 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
194 (pos-before-blank (progn (forward-line) (point)))
195 (end (progn (org-skip-whitespace)
196 (if (eobp) (point) (point-at-bol)))))
202 :contents-begin
,contents-begin
203 :contents-end
,contents-end
204 :post-blank
,(count-lines pos-before-blank end
)
205 ,@(cadr keywords
))))))
207 (defun org-element-drawer-interpreter (drawer contents
)
208 "Interpret DRAWER element as Org syntax.
209 CONTENTS is the contents of the element."
210 (format ":%s:\n%s:END:"
211 (org-element-get-property :drawer-name drawer
)
215 (defun org-element-dynamic-block-parser ()
216 "Parse a dynamic block.
218 Return a list whose car is `dynamic-block' and cdr is a plist
219 containing `:block-name', `:begin', `:end', `:hiddenp',
220 `:contents-begin', `:contents-end', `:arguments' and
221 `:post-blank' keywords.
223 Assume point is at beginning of dynamic block."
225 (let* ((case-fold-search t
)
226 (name (progn (looking-at org-dblock-start-re
)
227 (org-match-string-no-properties 1)))
228 (arguments (org-match-string-no-properties 3))
229 (keywords (org-element-collect-affiliated-keywords))
230 (begin (car keywords
))
231 (contents-begin (progn (forward-line) (point)))
232 (hidden (org-truely-invisible-p))
233 (contents-end (progn (re-search-forward org-dblock-end-re nil t
)
235 (pos-before-blank (progn (forward-line) (point)))
236 (end (progn (org-skip-whitespace)
237 (if (eobp) (point) (point-at-bol)))))
242 :arguments
,arguments
244 :contents-begin
,contents-begin
245 :contents-end
,contents-end
246 :post-blank
,(count-lines pos-before-blank end
)
247 ,@(cadr keywords
))))))
249 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
250 "Interpret DYNAMIC-BLOCK element as Org syntax.
251 CONTENTS is the contents of the element."
252 (format "#+BEGIN: %s%s\n%s#+END:"
253 (org-element-get-property :block-name dynamic-block
)
254 (let ((args (org-element-get-property :arguments dynamic-block
)))
255 (and arg
(concat " " args
)))
258 ;;;; Footnote Definition
260 (defun org-element-footnote-definition-parser ()
261 "Parse a footnote definition.
263 Return a list whose car is `footnote-definition' and cdr is
264 a plist containing `:label', `:begin' `:end', `:contents-begin',
265 `:contents-end' and `:post-blank' keywords."
267 (let* ((f-def (org-footnote-at-definition-p))
269 (keywords (progn (goto-char (nth 1 f-def
))
270 (org-element-collect-affiliated-keywords)))
271 (begin (car keywords
))
272 (contents-begin (progn (looking-at (concat "\\[" label
"\\]"))
273 (goto-char (match-end 0))
274 (org-skip-whitespace)
276 (end (goto-char (nth 2 f-def
)))
277 (contents-end (progn (skip-chars-backward " \r\t\n")
280 (list 'footnote-definition
284 :contents-begin
,contents-begin
285 :contents-end
,contents-end
286 :post-blank
,(count-lines contents-end end
)
287 ,@(cadr keywords
))))))
289 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
290 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
291 CONTENTS is the contents of the footnote-definition."
292 (concat (format "[%s]" (org-element-get-property :label footnote-definition
))
298 (defun org-element-headline-parser ()
301 Return a list whose car is `headline' and cdr is a plist
302 containing `:raw-value', `:title', `:begin', `:end',
303 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
304 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
305 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
306 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
309 The plist also contains any property set in the property drawer,
310 with its name in lowercase, the underscores replaced with hyphens
311 and colons at the beginning (i.e. `:custom-id').
313 Assume point is at beginning of the headline."
315 (let* ((components (org-heading-components))
316 (level (nth 1 components
))
317 (todo (nth 2 components
))
319 (if (member todo org-done-keywords
) 'done
'todo
)))
320 (tags (nth 5 components
))
321 (raw-value (nth 4 components
))
322 (quotedp (string-match (format "^%s +" org-quote-string
) raw-value
))
323 (commentedp (string-match
324 (format "^%s +" org-comment-string
) raw-value
))
326 (string-match (format ":%s:" org-archive-tag
) tags
)))
327 (footnote-section-p (and org-footnote-section
328 (string= org-footnote-section raw-value
)))
329 (standard-props (let (plist)
332 (let ((p-name (downcase (car p
))))
333 (while (string-match "_" p-name
)
335 (replace-match "-" nil nil p-name
)))
336 (setq p-name
(intern (concat ":" p-name
)))
338 (plist-put plist p-name
(cdr p
)))))
339 (org-entry-properties nil
'standard
))
341 (time-props (org-entry-properties nil
'special
"CLOCK"))
342 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
343 (deadline (cdr (assoc "DEADLINE" time-props
)))
344 (clock (cdr (assoc "CLOCK" time-props
)))
345 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
347 (pos-after-head (save-excursion (forward-line) (point)))
348 (contents-begin (save-excursion (forward-line)
349 (org-skip-whitespace)
350 (if (eobp) (point) (point-at-bol))))
351 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
352 (end (progn (goto-char (org-end-of-subtree t t
))))
353 (contents-end (progn (skip-chars-backward " \r\t\n")
357 ;; Clean RAW-VALUE from any quote or comment string.
358 (when (or quotedp commentedp
)
360 (replace-regexp-in-string
361 (concat "\\(" org-quote-string
"\\|" org-comment-string
"\\) +")
364 ;; Clean TAGS from archive tag, if any.
367 (and (not (string= tags
(format ":%s:" org-archive-tag
)))
368 (replace-regexp-in-string
369 (concat org-archive-tag
":") "" tags
)))
370 (when (string= tags
":") (setq tags nil
)))
372 (setq title
(org-element-parse-secondary-string
374 (cdr (assq 'headline org-element-string-restrictions
))))
376 `(:raw-value
,raw-value
380 :pre-blank
,(count-lines pos-after-head contents-begin
)
382 :contents-begin
,contents-begin
383 :contents-end
,contents-end
385 :priority
,(nth 3 components
)
388 :todo-type
,todo-type
389 :scheduled
,scheduled
391 :timestamp
,timestamp
393 :post-blank
,(count-lines contents-end end
)
394 :footnote-section-p
,footnote-section-p
395 :archivedp
,archivedp
396 :commentedp
,commentedp
398 ,@standard-props
)))))
400 (defun org-element-headline-interpreter (headline contents
)
401 "Interpret HEADLINE element as Org syntax.
402 CONTENTS is the contents of the element."
403 (let* ((level (org-element-get-property :level headline
))
404 (todo (org-element-get-property :todo-keyword headline
))
405 (priority (org-element-get-property :priority headline
))
406 (title (org-element-get-property :raw-value headline
))
407 (tags (let ((tag-string (org-element-get-property :tags headline
))
408 (archivedp (org-element-get-property :archivedp headline
)))
410 ((and (not tag-string
) archivedp
)
411 (format ":%s:" org-archive-tag
))
412 (archivedp (concat ":" org-archive-tag tag-string
))
414 (commentedp (org-element-get-property :commentedp headline
))
415 (quotedp (org-element-get-property :quotedp headline
))
416 (pre-blank (org-element-get-property :pre-blank headline
))
417 (heading (concat (make-string level ?
*)
418 (and todo
(concat " " todo
))
419 (and quotedp
(concat " " org-quote-string
))
420 (and commentedp
(concat " " org-comment-string
))
421 (and priority
(concat " " priority
))
422 (cond ((and org-footnote-section
423 (org-element-get-property
424 :footnote-section-p headline
))
425 (concat " " org-footnote-section
))
426 (title (concat " " title
)))))
429 (let ((tags-len (length tags
)))
432 ((zerop org-tags-column
) (1+ tags-len
))
433 ((< org-tags-column
0)
434 (max (- (+ org-tags-column
(length heading
)))
436 (t (max (+ (- org-tags-column
(length heading
))
438 (1+ tags-len
)))))))))
439 (concat heading
(and tags
(format tags-fmt tags
))
440 (make-string (1+ pre-blank
) 10)
444 (defun org-element-inlinetask-parser ()
445 "Parse an inline task.
447 Return a list whose car is `inlinetask' and cdr is a plist
448 containing `:raw-value', `:title', `:begin', `:end', `:hiddenp',
449 `:contents-begin' and `:contents-end', `:level', `:priority',
450 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
451 `:scheduled', `:deadline', `:timestamp', `:clock' and
452 `:post-blank' keywords.
454 The plist also contains any property set in the property drawer,
455 with its name in lowercase, the underscores replaced with hyphens
456 and colons at the beginning (i.e. `:custom-id').
458 Assume point is at beginning of the inline task."
460 (let* ((keywords (org-element-collect-affiliated-keywords))
461 (begin (car keywords
))
462 (components (org-heading-components))
463 (todo (nth 2 components
))
465 (if (member todo org-done-keywords
) 'done
'todo
)))
466 (raw-value (nth 4 components
))
467 (standard-props (let (plist)
470 (let ((p-name (downcase (car p
))))
471 (while (string-match "_" p-name
)
473 (replace-match "-" nil nil p-name
)))
474 (setq p-name
(intern (concat ":" p-name
)))
476 (plist-put plist p-name
(cdr p
)))))
477 (org-entry-properties nil
'standard
))
479 (time-props (org-entry-properties nil
'special
"CLOCK"))
480 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
481 (deadline (cdr (assoc "DEADLINE" time-props
)))
482 (clock (cdr (assoc "CLOCK" time-props
)))
483 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
484 (title (org-element-parse-secondary-string
486 (cdr (assq 'inlinetask org-element-string-restrictions
))))
487 (contents-begin (save-excursion (forward-line) (point)))
488 (hidden (org-truely-invisible-p))
489 (pos-before-blank (org-inlinetask-goto-end))
490 ;; In the case of a single line task, CONTENTS-BEGIN and
491 ;; CONTENTS-END might overlap.
492 (contents-end (max contents-begin
493 (save-excursion (forward-line -
1) (point))))
494 (end (progn (org-skip-whitespace)
495 (if (eobp) (point) (point-at-bol)))))
497 `(:raw-value
,raw-value
501 :hiddenp
,(and (> contents-end contents-begin
) hidden
)
502 :contents-begin
,contents-begin
503 :contents-end
,contents-end
504 :level
,(nth 1 components
)
505 :priority
,(nth 3 components
)
506 :tags
,(nth 5 components
)
508 :todo-type
,todo-type
509 :scheduled
,scheduled
511 :timestamp
,timestamp
513 :post-blank
,(count-lines pos-before-blank end
)
515 ,@(cadr keywords
))))))
517 (defun org-element-inlinetask-interpreter (inlinetask contents
)
518 "Interpret INLINETASK element as Org syntax.
519 CONTENTS is the contents of inlinetask."
520 (let* ((level (org-element-get-property :level inlinetask
))
521 (todo (org-element-get-property :todo-keyword inlinetask
))
522 (priority (org-element-get-property :priority inlinetask
))
523 (title (org-element-get-property :raw-value inlinetask
))
524 (tags (org-element-get-property :tags inlinetask
))
525 (task (concat (make-string level ?
*)
526 (and todo
(concat " " todo
))
527 (and priority
(concat " " priority
))
528 (and title
(concat " " title
))))
533 ((zerop org-tags-column
) 1)
534 ((< 0 org-tags-column
)
535 (max (+ org-tags-column
539 (t (max (- org-tags-column
(length inlinetask
))
541 (concat inlinetask
(and tags
(format tags-fmt tags
) "\n" contents
))))
544 (defun org-element-item-parser (struct)
547 STRUCT is the structure of the plain list.
549 Return a list whose car is `item' and cdr is a plist containing
550 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
551 `:checkbox', `:counter', `:tag', `:raw-tag', `:structure',
552 `:hiddenp' and `:post-blank' keywords.
554 Assume point is at the beginning of the item."
557 (let* ((begin (point))
558 (bullet (org-list-get-bullet (point) struct
))
559 (checkbox (let ((box (org-list-get-checkbox begin struct
)))
560 (cond ((equal "[ ]" box
) 'off
)
561 ((equal "[X]" box
) 'on
)
562 ((equal "[-]" box
) 'trans
))))
563 (counter (let ((c (org-list-get-counter begin struct
)))
566 ((string-match "[A-Za-z]" c
)
567 (- (string-to-char (upcase (match-string 0 c
)))
569 ((string-match "[0-9]+" c
)
570 (string-to-number (match-string 0 c
))))))
571 (raw-tag (org-list-get-tag begin struct
))
573 (org-element-parse-secondary-string
575 (cdr (assq 'item org-element-string-restrictions
)))))
576 (end (org-list-get-item-end begin struct
))
577 (contents-begin (progn (looking-at org-list-full-item-re
)
578 (goto-char (match-end 0))
579 (org-skip-whitespace)
583 (hidden (progn (forward-line)
584 (and (not (= (point) end
))
585 (org-truely-invisible-p))))
586 (contents-end (progn (goto-char end
)
587 (skip-chars-backward " \r\t\n")
594 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
595 ;; up in the case of an empty item separated
596 ;; from the next by a blank line. Thus, ensure
597 ;; the former is always the smallest of two.
598 :contents-begin
,(min contents-begin contents-end
)
599 :contents-end
,(max contents-begin contents-end
)
606 :post-blank
,(count-lines contents-end end
))))))
608 (defun org-element-item-interpreter (item contents
)
609 "Interpret ITEM element as Org syntax.
610 CONTENTS is the contents of the element."
611 (let* ((bullet (org-element-get-property :bullet item
))
612 (checkbox (org-element-get-property :checkbox item
))
613 (counter (org-element-get-property :counter item
))
614 (tag (org-element-get-property :raw-tag item
))
615 ;; Compute indentation.
616 (ind (make-string (length bullet
) 32)))
620 (when (and org-list-two-spaces-after-bullet-regexp
621 (string-match org-list-two-spaces-after-bullet-regexp bullet
))
623 (and counter
(format "[@%d] " counter
))
625 ((eq checkbox
'on
) "[X] ")
626 ((eq checkbox
'off
) "[ ] ")
627 ((eq checkbox
'trans
) "[-] "))
628 (and tag
(format "%s :: " tag
))
630 (replace-regexp-in-string
631 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))))
634 (defun org-element-plain-list-parser (&optional structure
)
637 Return a list whose car is `plain-list' and cdr is a plist
638 containing `:type', `:begin', `:end', `:contents-begin' and
639 `:contents-end', `:level', `:structure' and `:post-blank'
642 Assume point is at one of the list items."
644 (let* ((struct (or structure
(org-list-struct)))
645 (prevs (org-list-prevs-alist struct
))
646 (parents (org-list-parents-alist struct
))
647 (type (org-list-get-list-type (point) struct prevs
))
648 (contents-begin (goto-char
649 (org-list-get-list-begin (point) struct prevs
)))
650 (keywords (org-element-collect-affiliated-keywords))
651 (begin (car keywords
))
652 (contents-end (goto-char
653 (org-list-get-list-end (point) struct prevs
)))
654 (end (save-excursion (org-skip-whitespace)
655 (if (eobp) (point) (point-at-bol))))
658 (let ((item contents-begin
))
661 (org-list-get-list-begin item struct prevs
)
664 ;; Blank lines below list belong to the top-level list only.
666 (setq end
(min (org-list-get-bottom-point struct
)
667 (progn (org-skip-whitespace)
668 (if (eobp) (point) (point-at-bol))))))
674 :contents-begin
,contents-begin
675 :contents-end
,contents-end
678 :post-blank
,(count-lines contents-end end
)
679 ,@(cadr keywords
))))))
681 (defun org-element-plain-list-interpreter (plain-list contents
)
682 "Interpret PLAIN-LIST element as Org syntax.
683 CONTENTS is the contents of the element."
687 (defun org-element-quote-block-parser ()
688 "Parse a quote block.
690 Return a list whose car is `quote-block' and cdr is a plist
691 containing `:begin', `:end', `:hiddenp', `:contents-begin',
692 `:contents-end' and `:post-blank' keywords.
694 Assume point is at beginning or end of the block."
696 (let* ((case-fold-search t
)
700 (concat "^[ \t]*#\\+begin_quote") nil t
)
701 (org-element-collect-affiliated-keywords)))
702 (begin (car keywords
))
703 (contents-begin (progn (forward-line) (point)))
704 (hidden (org-truely-invisible-p))
705 (contents-end (progn (re-search-forward
706 (concat "^[ \t]*#\\+end_quote") nil t
)
708 (pos-before-blank (progn (forward-line) (point)))
709 (end (progn (org-skip-whitespace)
710 (if (eobp) (point) (point-at-bol)))))
715 :contents-begin
,contents-begin
716 :contents-end
,contents-end
717 :post-blank
,(count-lines pos-before-blank end
)
718 ,@(cadr keywords
))))))
721 (defun org-element-quote-block-interpreter (quote-block contents
)
722 "Interpret QUOTE-BLOCK element as Org syntax.
723 CONTENTS is the contents of the element."
724 (format "#+begin_quote\n%s#+end_quote" contents
))
727 (defun org-element-special-block-parser ()
728 "Parse a special block.
730 Return a list whose car is `special-block' and cdr is a plist
731 containing `:type', `:begin', `:end', `:hiddenp',
732 `:contents-begin', `:contents-end' and `:post-blank' keywords.
734 Assume point is at beginning or end of the block."
736 (let* ((case-fold-search t
)
737 (type (progn (looking-at
738 "[ \t]*#\\+\\(?:begin\\|end\\)_\\([-A-Za-z0-9]+\\)")
739 (org-match-string-no-properties 1)))
743 (concat "^[ \t]*#\\+begin_" type
) nil t
)
744 (org-element-collect-affiliated-keywords)))
745 (begin (car keywords
))
746 (contents-begin (progn (forward-line) (point)))
747 (hidden (org-truely-invisible-p))
748 (contents-end (progn (re-search-forward
749 (concat "^[ \t]*#\\+end_" type
) nil t
)
751 (pos-before-blank (progn (forward-line) (point)))
752 (end (progn (org-skip-whitespace)
753 (if (eobp) (point) (point-at-bol)))))
759 :contents-begin
,contents-begin
760 :contents-end
,contents-end
761 :post-blank
,(count-lines pos-before-blank end
)
762 ,@(cadr keywords
))))))
764 (defun org-element-special-block-interpreter (special-block contents
)
765 "Interpret SPECIAL-BLOCK element as Org syntax.
766 CONTENTS is the contents of the element."
767 (let ((block-type (org-element-get-property :type special-block
)))
768 (format "#+begin_%s\n%s#+end_%s" block-type contents block-type
)))
774 ;; For each element, a parser and an interpreter are also defined.
775 ;; Both follow the same naming convention used for greater elements.
777 ;; Also, as for greater elements, adding a new element type is done
778 ;; through the following steps: implement a parser and an interpreter,
779 ;; tweak `org-element-guess-type' so that it recognizes the new type
780 ;; and add that new type to `org-element-all-elements'.
782 ;; As a special case, when the newly defined type is a block type,
783 ;; `org-element-non-recursive-block-alist' has to be modified
788 (defun org-element-babel-call-parser ()
791 Return a list whose car is `babel-call' and cdr is a plist
792 containing `:begin', `:end', `:info' and `:post-blank' as
795 (let ((info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
796 (org-babel-lob-get-info)))
798 (pos-before-blank (progn (forward-line) (point)))
799 (end (progn (org-skip-whitespace)
800 (if (eobp) (point) (point-at-bol)))))
805 :post-blank
,(count-lines pos-before-blank end
))))))
807 (defun org-element-babel-call-interpreter (inline-babel-call contents
)
808 "Interpret INLINE-BABEL-CALL object as Org syntax.
810 (let* ((babel-info (org-element-get-property :info inline-babel-call
))
811 (main-source (car babel-info
))
812 (post-options (nth 1 babel-info
)))
814 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
815 ;; Remove redundant square brackets.
817 (match-string 1 main-source
) nil nil main-source
)
819 (and post-options
(format "[%s]" post-options
)))))
822 (defun org-element-comment-parser ()
825 Return a list whose car is `comment' and cdr is a plist
826 containing `:begin', `:end', `:value' and `:post-blank'
828 (let ((comment-re "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)")
829 beg-coms begin end value pos-before-blank keywords
)
831 ;; Move to the beginning of comments.
833 (while (and (not (bobp)) (looking-at comment-re
))
835 (unless (looking-at comment-re
) (forward-line 1)))
836 (setq beg-coms
(point))
837 ;; Get affiliated keywords, if any.
838 (setq keywords
(org-element-collect-affiliated-keywords))
839 ;; Store true beginning of element.
840 (setq begin
(car keywords
))
841 ;; Get ending of comments. If point is in a list, ensure to not
842 ;; get outside of it.
843 (let* ((itemp (org-in-item-p))
845 (org-list-get-bottom-point
846 (save-excursion (goto-char itemp
) (org-list-struct)))
848 (while (and (looking-at comment-re
) (< (point) max-pos
))
850 (setq pos-before-blank
(point))
851 ;; Find position after blank.
852 (org-skip-whitespace)
853 (setq end
(if (eobp) (point) (point-at-bol)))
855 (setq value
(buffer-substring-no-properties beg-coms pos-before-blank
)))
860 :post-blank
,(count-lines pos-before-blank end
)
861 ,@(cadr keywords
)))))
863 (defun org-element-comment-interpreter (comment contents
)
864 "Interpret COMMENT element as Org syntax.
866 (org-element-get-property :value comment
))
869 (defun org-element-comment-block-parser ()
870 "Parse an export block.
872 Return a list whose car is `comment-block' and cdr is a plist
873 containing `:begin', `:end', `:hiddenp', `:value' and
874 `:post-blank' keywords."
877 (let* ((case-fold-search t
)
879 (re-search-backward "^[ \t]*#\\+begin_comment" nil t
)
880 (org-element-collect-affiliated-keywords)))
881 (begin (car keywords
))
882 (contents-begin (progn (forward-line) (point)))
883 (hidden (org-truely-invisible-p))
884 (contents-end (progn (re-search-forward
885 "^[ \t]*#\\+end_comment" nil t
)
887 (pos-before-blank (progn (forward-line) (point)))
888 (end (progn (org-skip-whitespace)
889 (if (eobp) (point) (point-at-bol))))
890 (value (buffer-substring-no-properties contents-begin contents-end
)))
896 :post-blank
,(count-lines pos-before-blank end
)
897 ,@(cadr keywords
))))))
899 (defun org-element-comment-block-interpreter (comment-block contents
)
900 "Interpret COMMENT-BLOCK element as Org syntax.
902 (concat "#+begin_comment\n"
903 (org-remove-indentation
904 (org-element-get-property :value comment-block
))
908 (defun org-element-example-block-parser ()
909 "Parse an example block.
911 Return a list whose car is `example' and cdr is a plist
912 containing `:begin', `:end', `:options', `:hiddenp', `:value' and
913 `:post-blank' keywords."
916 (let* ((case-fold-search t
)
919 "^[ \t]*#\\+begin_example\\(?: +\\(.*\\)\\)?" nil t
)
920 (org-match-string-no-properties 1)))
921 (keywords (org-element-collect-affiliated-keywords))
922 (begin (car keywords
))
923 (contents-begin (progn (forward-line) (point)))
924 (hidden (org-truely-invisible-p))
926 (re-search-forward "^[ \t]*#\\+end_example" nil t
)
928 (value (buffer-substring-no-properties contents-begin contents-end
))
929 (pos-before-blank (progn (forward-line) (point)))
930 (end (progn (org-skip-whitespace)
931 (if (eobp) (point) (point-at-bol)))))
938 :post-blank
,(count-lines pos-before-blank end
)
939 ,@(cadr keywords
))))))
941 (defun org-element-example-block-interpreter (example-block contents
)
942 "Interpret EXAMPLE-BLOCK element as Org syntax.
944 (let ((options (org-element-get-property :options example-block
)))
945 (concat "#+begin_example" (and options
(concat " " options
)) "\n"
946 (org-remove-indentation
947 (org-element-get-property :value example-block
))
951 (defun org-element-export-block-parser ()
952 "Parse an export block.
954 Return a list whose car is `export-block' and cdr is a plist
955 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
956 `:post-blank' keywords."
959 (let* ((case-fold-search t
)
961 (type (progn (re-search-backward
962 (concat "[ \t]*#\\+begin_"
963 (org-re "\\([[:alnum:]]+\\)")))
964 (downcase (org-match-string-no-properties 1))))
965 (keywords (org-element-collect-affiliated-keywords))
966 (begin (car keywords
))
967 (contents-begin (progn (forward-line) (point)))
968 (hidden (org-truely-invisible-p))
969 (contents-end (progn (re-search-forward
970 (concat "^[ \t]*#\\+end_" type
) nil t
)
972 (pos-before-blank (progn (forward-line) (point)))
973 (end (progn (org-skip-whitespace)
974 (if (eobp) (point) (point-at-bol))))
975 (value (buffer-substring-no-properties contents-begin contents-end
)))
982 :post-blank
,(count-lines pos-before-blank end
)
983 ,@(cadr keywords
))))))
985 (defun org-element-export-block-interpreter (export-block contents
)
986 "Interpret EXPORT-BLOCK element as Org syntax.
988 (let ((type (org-element-get-property :type export-block
)))
989 (concat (format "#+begin_%s\n" type
)
990 (org-element-get-property :value export-block
)
991 (format "#+end_%s" type
))))
994 (defun org-element-fixed-width-parser ()
995 "Parse a fixed-width section.
997 Return a list whose car is `fixed-width' and cdr is a plist
998 containing `:begin', `:end', `:value' and `:post-blank'
1000 (let ((fixed-re "[ \t]*:\\( \\|$\\)")
1001 beg-area begin end value pos-before-blank keywords
)
1003 ;; Move to the beginning of the fixed-width area.
1005 (while (and (not (bobp)) (looking-at fixed-re
))
1007 (unless (looking-at fixed-re
) (forward-line 1)))
1008 (setq beg-area
(point))
1009 ;; Get affiliated keywords, if any.
1010 (setq keywords
(org-element-collect-affiliated-keywords))
1011 ;; Store true beginning of element.
1012 (setq begin
(car keywords
))
1013 ;; Get ending of fixed-width area. If point is in a list,
1014 ;; ensure to not get outside of it.
1015 (let* ((itemp (org-in-item-p))
1017 (org-list-get-bottom-point
1018 (save-excursion (goto-char itemp
) (org-list-struct)))
1020 (while (and (looking-at fixed-re
) (< (point) max-pos
))
1022 (setq pos-before-blank
(point))
1023 ;; Find position after blank
1024 (org-skip-whitespace)
1025 (setq end
(if (eobp) (point) (point-at-bol)))
1027 (setq value
(buffer-substring-no-properties beg-area pos-before-blank
)))
1032 :post-blank
,(count-lines pos-before-blank end
)
1033 ,@(cadr keywords
)))))
1035 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1036 "Interpret FIXED-WIDTH element as Org syntax.
1038 (org-remove-indentation (org-element-get-property :value fixed-width
)))
1040 ;;;; Horizontal Rule
1041 (defun org-element-horizontal-rule-parser ()
1042 "Parse an horizontal rule.
1044 Return a list whose car is `horizontal-rule' and cdr is
1045 a plist containing `:begin', `:end' and `:post-blank'
1048 (let* ((keywords (org-element-collect-affiliated-keywords))
1049 (begin (car keywords
))
1050 (post-hr (progn (forward-line) (point)))
1051 (end (progn (org-skip-whitespace)
1052 (if (eobp) (point) (point-at-bol)))))
1053 (list 'horizontal-rule
1056 :post-blank
,(count-lines post-hr end
)
1057 ,@(cadr keywords
))))))
1059 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1060 "Interpret HORIZONTAL-RULE element as Org syntax.
1065 (defun org-element-keyword-parser ()
1066 "Parse a keyword at point.
1068 Return a list whose car is `keyword' and cdr is a plist
1069 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1072 (let* ((begin (point))
1073 (key (progn (looking-at
1074 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1075 (org-match-string-no-properties 1)))
1076 (value (org-trim (buffer-substring-no-properties
1077 (match-end 0) (point-at-eol))))
1078 (pos-before-blank (progn (forward-line) (point)))
1079 (end (progn (org-skip-whitespace)
1080 (if (eobp) (point) (point-at-bol)))))
1086 :post-blank
,(count-lines pos-before-blank end
))))))
1088 (defun org-element-keyword-interpreter (keyword contents
)
1089 "Interpret KEYWORD element as Org syntax.
1092 (org-element-get-property :key keyword
)
1093 (org-element-get-property :value keyword
)))
1095 ;;;; Latex Environment
1096 (defun org-element-latex-environment-parser ()
1097 "Parse a LaTeX environment.
1099 Return a list whose car is `latex-environment' and cdr is a plist
1100 containing `:begin', `:end', `:value' and `:post-blank' keywords."
1103 (let* ((case-fold-search t
)
1104 (contents-begin (re-search-backward "^[ \t]*\\\\begin" nil t
))
1105 (keywords (org-element-collect-affiliated-keywords))
1106 (begin (car keywords
))
1107 (contents-end (progn (re-search-forward "^[ \t]*\\\\end")
1110 (value (buffer-substring-no-properties contents-begin contents-end
))
1111 (end (progn (org-skip-whitespace)
1112 (if (eobp) (point) (point-at-bol)))))
1113 (list 'latex-environment
1117 :post-blank
,(count-lines contents-end end
)
1118 ,@(cadr keywords
))))))
1120 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1121 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1123 (org-element-get-property :value latex-environment
))
1126 (defun org-element-paragraph-parser ()
1129 Return a list whose car is `paragraph' and cdr is a plist
1130 containing `:begin', `:end', `:contents-begin' and
1131 `:contents-end' and `:post-blank' keywords.
1133 Assume point is at the beginning of the paragraph."
1135 (let* ((contents-begin (point))
1136 (keywords (org-element-collect-affiliated-keywords))
1137 (begin (car keywords
))
1138 (contents-end (progn
1140 (if (re-search-forward
1141 org-element-paragraph-separate nil
'm
)
1142 (progn (forward-line -
1) (end-of-line) (point))
1144 (pos-before-blank (progn (forward-line) (point)))
1145 (end (progn (org-skip-whitespace)
1146 (if (eobp) (point) (point-at-bol)))))
1150 :contents-begin
,contents-begin
1151 :contents-end
,contents-end
1152 :post-blank
,(count-lines pos-before-blank end
)
1153 ,@(cadr keywords
))))))
1155 (defun org-element-paragraph-interpreter (paragraph contents
)
1156 "Interpret PARAGRAPH element as Org syntax.
1157 CONTENTS is the contents of the element."
1160 ;;;; Property Drawer
1161 (defun org-element-property-drawer-parser ()
1162 "Parse a property drawer.
1164 Return a list whose car is `property-drawer' and cdr is a plist
1165 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1166 `:contents-end', `:properties' and `:post-blank' keywords."
1168 (let ((case-fold-search t
)
1169 (begin (progn (end-of-line)
1170 (re-search-backward org-property-start-re
)
1171 (match-beginning 0)))
1172 (contents-begin (progn (forward-line) (point)))
1173 (hidden (org-truely-invisible-p))
1174 (properties (let (val)
1175 (while (not (looking-at "^[ \t]*:END:"))
1178 "[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):"))
1179 (push (cons (match-string 1)
1182 (match-end 0) (point-at-eol))))
1186 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
1188 (pos-before-blank (progn (forward-line) (point)))
1189 (end (progn (org-skip-whitespace)
1190 (if (eobp) (point) (point-at-bol)))))
1191 (list 'property-drawer
1195 :properties
,properties
1196 :post-blank
,(count-lines pos-before-blank end
))))))
1198 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1199 "Interpret PROPERTY-DRAWER element as Org syntax.
1201 (let ((props (org-element-get-property :properties property-drawer
)))
1204 (mapconcat (lambda (p)
1205 (format org-property-format
(format ":%s:" (car p
)) (cdr p
)))
1206 (nreverse props
) "\n")
1210 (defun org-element-quote-section-parser ()
1211 "Parse a quote section.
1213 Return a list whose car is `quote-section' and cdr is a plist
1214 containing `:begin', `:end', `:value' and `:post-blank'
1217 (let* ((begin (progn (org-back-to-heading t
)
1219 (org-skip-whitespace)
1221 (end (progn (org-with-limited-levels (outline-next-heading))
1223 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1226 (value (unless (= begin end
)
1227 (buffer-substring-no-properties begin pos-before-blank
))))
1228 (list 'quote-section
1232 :post-blank
,(if value
1233 (count-lines pos-before-blank end
)
1236 (defun org-element-quote-section-interpreter (quote-section contents
)
1237 "Interpret QUOTE-SECTION element as Org syntax.
1239 (org-element-get-property :value quote-section
))
1242 (defun org-element-src-block-parser ()
1245 Return a list whose car is `src-block' and cdr is a plist
1246 containing `:language', `:switches', `:parameters', `:begin',
1247 `:end', `:hiddenp', `:contents-begin', `:contents-end', `:value'
1248 and `:post-blank' keywords."
1251 (let* ((case-fold-search t
)
1252 ;; Get position at beginning of block.
1255 (concat "^[ \t]*#\\+begin_src"
1256 "\\(?: +\\(\\S-+\\)\\)?" ; language
1257 "\\(\\(?: +[-+][A-Za-z]\\)*\\)" ; switches
1258 "\\(.*\\)[ \t]*$") ; arguments
1260 ;; Get language as a string.
1261 (language (org-match-string-no-properties 1))
1263 (switches (org-match-string-no-properties 2))
1265 (parameters (org-trim (org-match-string-no-properties 3)))
1266 ;; Get affiliated keywords.
1267 (keywords (org-element-collect-affiliated-keywords))
1268 ;; Get beginning position.
1269 (begin (car keywords
))
1270 ;; Get position at end of block.
1271 (contents-end (progn (re-search-forward "^[ \t]*#\\+end_src" nil t
)
1275 (value (buffer-substring-no-properties
1276 (save-excursion (goto-char contents-begin
)
1279 (match-beginning 0)))
1280 ;; Get position after ending blank lines.
1281 (end (progn (org-skip-whitespace)
1282 (if (eobp) (point) (point-at-bol))))
1283 ;; Get visibility status.
1284 (hidden (progn (goto-char contents-begin
)
1286 (org-truely-invisible-p))))
1288 `(:language
,language
1290 :parameters
,parameters
1295 :post-blank
,(count-lines contents-end end
)
1296 ,@(cadr keywords
))))))
1298 (defun org-element-src-block-interpreter (src-block contents
)
1299 "Interpret SRC-BLOCK element as Org syntax.
1301 (let ((lang (org-element-get-property :language src-block
))
1302 (switches (org-element-get-property :switches src-block
))
1303 (params (org-element-get-property :parameters src-block
))
1304 (value (let ((val (org-element-get-property :value src-block
)))
1306 (org-src-preserve-indentation val
)
1307 ((zerop org-edit-src-content-indentation
)
1308 (org-remove-indentation val
))
1310 (let ((ind (make-string
1311 org-edit-src-content-indentation
32)))
1312 (replace-regexp-in-string
1313 "\\(^\\)[ \t]*\\S-" ind
1314 (org-remove-indentation val
) nil nil
1)))))))
1315 (concat (format "#+begin_src%s\n"
1316 (concat (and lang
(concat " " lang
))
1317 (and switches
(concat " " switches
))
1318 (and params
(concat " " params
))))
1323 (defun org-element-table-parser ()
1324 "Parse a table at point.
1326 Return a list whose car is `table' and cdr is a plist containing
1327 `:begin', `:end', `:contents-begin', `:contents-end', `:tblfm',
1328 `:type', `:raw-table' and `:post-blank' keywords."
1330 (let* ((table-begin (goto-char (org-table-begin t
)))
1331 (type (if (org-at-table.el-p
) 'table.el
'org
))
1332 (keywords (org-element-collect-affiliated-keywords))
1333 (begin (car keywords
))
1334 (table-end (goto-char (marker-position (org-table-end t
))))
1335 (tblfm (when (looking-at "[ \t]*#\\+tblfm: +\\(.*\\)[ \t]*")
1336 (prog1 (org-match-string-no-properties 1)
1338 (pos-before-blank (point))
1339 (end (progn (org-skip-whitespace)
1340 (if (eobp) (point) (point-at-bol))))
1341 (raw-table (org-remove-indentation
1342 (buffer-substring-no-properties table-begin table-end
))))
1347 :raw-table
,raw-table
1349 :post-blank
,(count-lines pos-before-blank end
)
1350 ,@(cadr keywords
))))))
1352 (defun org-element-table-interpreter (table contents
)
1353 "Interpret TABLE element as Org syntax.
1355 (org-element-get-property :raw-table table
))
1358 (defun org-element-verse-block-parser ()
1359 "Parse a verse block.
1361 Return a list whose car is `verse-block' and cdr is a plist
1362 containing `:begin', `:end', `:hiddenp', `:raw-value', `:value'
1363 and `:post-blank' keywords.
1365 Assume point is at beginning or end of the block."
1367 (let* ((case-fold-search t
)
1371 (concat "^[ \t]*#\\+begin_verse") nil t
)
1372 (org-element-collect-affiliated-keywords)))
1373 (begin (car keywords
))
1374 (hidden (progn (forward-line) (org-truely-invisible-p)))
1375 (raw-val (buffer-substring-no-properties
1378 (re-search-forward (concat "^[ \t]*#\\+end_verse") nil t
)
1380 (pos-before-blank (progn (forward-line) (point)))
1381 (end (progn (org-skip-whitespace)
1382 (if (eobp) (point) (point-at-bol))))
1383 (value (org-element-parse-secondary-string
1384 (org-remove-indentation raw-val
)
1385 (cdr (assq 'verse org-element-string-restrictions
)))))
1392 :post-blank
,(count-lines pos-before-blank end
)
1393 ,@(cadr keywords
))))))
1396 (defun org-element-verse-block-interpreter (verse-block contents
)
1397 "Interpret VERSE-BLOCK element as Org syntax.
1399 (format "#+begin_verse\n%s#+end_verse"
1400 (org-remove-indentation
1401 (org-element-get-property :raw-value verse-block
))))
1407 ;; Unlike to elements, interstices can be found between objects.
1408 ;; That's why, along with the parser, successor functions are provided
1409 ;; for each object. Some objects share the same successor
1410 ;; (i.e. `emphasis' and `verbatim' objects).
1412 ;; A successor must accept a single argument bounding the search. It
1413 ;; will return either a cons cell whose car is the object's type, as
1414 ;; a symbol, and cdr the position of its next occurrence, or nil.
1416 ;; Successors follow the naming convention:
1417 ;; org-element-NAME-successor, where NAME is the name of the
1418 ;; successor, as defined in `org-element-all-successors'.
1420 ;; Some object types (i.e `emphasis') are recursive. Restrictions on
1421 ;; object types they can contain will be specified in
1422 ;; `org-element-object-restrictions'.
1424 ;; Adding a new type of object is simple. Implement a successor,
1425 ;; a parser, and an interpreter for it, all following the naming
1426 ;; convention. Register successor in `org-element-all-successors',
1427 ;; maybe tweak restrictions about it, and that's it.
1430 (defun org-element-emphasis-parser ()
1431 "Parse text markup object at point.
1433 Return a list whose car is `emphasis' and cdr is a plist with
1434 `:marker', `:begin', `:end', `:contents-begin' and
1435 `:contents-end' and `:post-blank' keywords.
1437 Assume point is at the first emphasis marker."
1439 (unless (bolp) (backward-char 1))
1440 (looking-at org-emph-re
)
1441 (let ((begin (match-beginning 2))
1442 (marker (org-match-string-no-properties 3))
1443 (contents-begin (match-beginning 4))
1444 (contents-end (match-end 4))
1445 (post-blank (progn (goto-char (match-end 2))
1446 (skip-chars-forward " \t")))
1452 :contents-begin
,contents-begin
1453 :contents-end
,contents-end
1454 :post-blank
,post-blank
)))))
1456 (defun org-element-emphasis-interpreter (emphasis contents
)
1457 "Interpret EMPHASIS object as Org syntax.
1458 CONTENTS is the contents of the object."
1459 (let ((marker (org-element-get-property :marker emphasis
)))
1460 (concat marker contents marker
)))
1462 (defun org-element-text-markup-successor (limit)
1463 "Search for the next emphasis or verbatim and return position.
1465 LIMIT bounds the search.
1467 Return value is a cons cell whose car is `emphasis' or
1468 `verbatim' and cdr is beginning position."
1470 (unless (bolp) (backward-char))
1471 (when (re-search-forward org-emph-re limit t
)
1472 (cons (if (nth 4 (assoc (match-string 3) org-emphasis-alist
))
1475 (match-beginning 2)))))
1478 (defun org-element-entity-parser ()
1479 "Parse entity at point.
1481 Return a list whose car is `entity' and cdr a plist with
1482 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1483 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1486 Assume point is at the beginning of the entity."
1488 (looking-at "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1489 (let* ((value (org-entity-get (match-string 1)))
1490 (begin (match-beginning 0))
1491 (bracketsp (string= (match-string 2) "{}"))
1492 (post-blank (progn (goto-char (match-end 1))
1493 (when bracketsp
(forward-char 2))
1494 (skip-chars-forward " \t")))
1497 `(:name
,(car value
)
1498 :latex
,(nth 1 value
)
1499 :latex-math-p
,(nth 2 value
)
1500 :html
,(nth 3 value
)
1501 :ascii
,(nth 4 value
)
1502 :latin1
,(nth 5 value
)
1503 :utf-8
,(nth 6 value
)
1506 :use-brackets-p
,bracketsp
1507 :post-blank
,post-blank
)))))
1509 (defun org-element-entity-interpreter (entity contents
)
1510 "Interpret ENTITY object as Org syntax.
1513 (org-element-get-property :name entity
)
1514 (when (org-element-get-property :use-brackets-p entity
) "{}")))
1516 (defun org-element-latex-or-entity-successor (limit)
1517 "Search for the next latex-fragment or entity object.
1519 LIMIT bounds the search.
1521 Return value is a cons cell whose car is `entity' or
1522 `latex-fragment' and cdr is beginning position."
1524 (let ((matchers (plist-get org-format-latex-options
:matchers
))
1525 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1527 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"))
1528 (when (re-search-forward
1529 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
1533 (goto-char (match-beginning 0))
1534 (if (looking-at entity-re
)
1535 ;; Determine if it's a real entity or a LaTeX command.
1536 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
1537 (match-beginning 0))
1538 ;; No entity nor command: point is at a LaTeX fragment.
1539 ;; Determine its type to get the correct beginning position.
1540 (cons 'latex-fragment
1543 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
1546 (nth 2 (assoc e org-latex-regexps
))))))
1551 (defun org-element-export-snippet-parser ()
1552 "Parse export snippet at point.
1554 Return a list whose car is `export-snippet' and cdr a plist with
1555 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1558 Assume point is at the beginning of the snippet."
1560 (looking-at "@\\([-A-Za-z0-9]+\\){")
1561 (let* ((begin (point))
1562 (back-end (org-match-string-no-properties 1))
1563 (before-blank (progn (goto-char (scan-sexps (1- (match-end 0)) 1))))
1564 (value (buffer-substring-no-properties
1565 (match-end 0) (1- before-blank
)))
1566 (post-blank (skip-chars-forward " \t"))
1568 (list 'export-snippet
1569 `(:back-end
,back-end
1573 :post-blank
,post-blank
)))))
1575 (defun org-element-export-snippet-interpreter (export-snippet contents
)
1576 "Interpret EXPORT-SNIPPET object as Org syntax.
1579 (org-element-get-property :back-end export-snippet
)
1580 (org-element-get-property :value export-snippet
)))
1582 (defun org-element-export-snippet-successor (limit)
1583 "Search for the next export-snippet object.
1585 LIMIT bounds the search.
1587 Return value is a cons cell whose car is `export-snippet' cdr is
1588 its beginning position."
1591 (while (re-search-forward "@[-A-Za-z0-9]+{" limit t
)
1592 (when (let ((end (ignore-errors (scan-sexps (1- (point)) 1))))
1593 (and end
(eq (char-before end
) ?
})))
1594 (throw 'exit
(cons 'export-snippet
(match-beginning 0))))))))
1596 ;;;; Footnote Reference
1598 (defun org-element-footnote-reference-parser ()
1599 "Parse footnote reference at point.
1601 Return a list whose car is `footnote-reference' and cdr a plist
1602 with `:label', `:type', `:definition', `:begin', `:end' and
1603 `:post-blank' as keywords."
1605 (let* ((ref (org-footnote-at-reference-p))
1607 (raw-def (nth 3 ref
))
1608 (inline-def (and raw-def
1609 (org-element-parse-secondary-string raw-def nil
)))
1610 (type (if (nth 3 ref
) 'inline
'standard
))
1612 (post-blank (progn (goto-char (nth 2 ref
))
1613 (skip-chars-forward " \t")))
1615 (list 'footnote-reference
1618 :inline-definition
,inline-def
1621 :post-blank
,post-blank
1622 :raw-definition
,raw-def
)))))
1624 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
1625 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1627 (let ((label (or (org-element-get-property :label footnote-reference
)
1629 (def (let ((raw (org-element-get-property
1630 :raw-definition footnote-reference
)))
1631 (if raw
(concat ":" raw
) ""))))
1632 (format "[%s]" (concat label def
))))
1634 (defun org-element-footnote-reference-successor (limit)
1635 "Search for the next footnote-reference and return beginning
1638 LIMIT bounds the search.
1640 Return value is a cons cell whose car is `footnote-reference' and
1641 cdr is beginning position."
1643 (when (setq fn-ref
(org-footnote-get-next-reference nil nil limit
))
1644 (cons 'footnote-reference
(nth 1 fn-ref
)))))
1647 ;;;; Inline Babel Call
1648 (defun org-element-inline-babel-call-parser ()
1649 "Parse inline babel call at point.
1651 Return a list whose car is `inline-babel-call' and cdr a plist with
1652 `:begin', `:end', `:info' and `:post-blank' as keywords.
1654 Assume point is at the beginning of the babel call."
1656 (unless (bolp) (backward-char))
1657 (looking-at org-babel-inline-lob-one-liner-regexp
)
1658 (let ((info (save-match-data (org-babel-lob-get-info)))
1659 (begin (match-end 1))
1660 (post-blank (progn (goto-char (match-end 0))
1661 (skip-chars-forward " \t")))
1663 (list 'inline-babel-call
1667 :post-blank
,post-blank
)))))
1669 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
1670 "Interpret INLINE-BABEL-CALL object as Org syntax.
1672 (let* ((babel-info (org-element-get-property :info inline-babel-call
))
1673 (main-source (car babel-info
))
1674 (post-options (nth 1 babel-info
)))
1676 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
1677 ;; Remove redundant square brackets.
1679 (match-string 1 main-source
) nil nil main-source
)
1681 (and post-options
(format "[%s]" post-options
)))))
1683 (defun org-element-inline-babel-call-successor (limit)
1684 "Search for the next inline-babel-call and return beginning
1687 LIMIT bounds the search.
1689 Return value is a cons cell whose car is `inline-babel-call' and
1690 cdr is beginning position."
1692 ;; Use a simplified version of
1693 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
1694 (when (re-search-forward
1695 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
1697 (cons 'inline-babel-call
(match-beginning 0)))))
1699 ;;;; Inline Src Block
1700 (defun org-element-inline-src-block-parser ()
1701 "Parse inline source block at point.
1703 Return a list whose car is `inline-src-block' and cdr a plist
1704 with `:begin', `:end', `:language', `:value', `:parameters' and
1705 `:post-blank' as keywords.
1707 Assume point is at the beginning of the inline src block."
1709 (unless (bolp) (backward-char))
1710 (looking-at org-babel-inline-src-block-regexp
)
1711 (let ((begin (match-beginning 1))
1712 (language (org-match-string-no-properties 2))
1713 (parameters (org-match-string-no-properties 4))
1714 (value (org-match-string-no-properties 5))
1715 (post-blank (progn (goto-char (match-end 0))
1716 (skip-chars-forward " \t")))
1718 (list 'inline-src-block
1719 `(:language
,language
1721 :parameters
,parameters
1724 :post-blank
,post-blank
)))))
1728 (defun org-element-inline-src-block-successor (limit)
1729 "Search for the next inline-babel-call and return beginning position.
1731 LIMIT bounds the search.
1733 Return value is a cons cell whose car is `inline-babel-call' and
1734 cdr is beginning position."
1736 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
1737 (cons 'inline-src-block
(match-beginning 1)))))
1740 (defun org-element-latex-fragment-parser ()
1741 "Parse latex fragment at point.
1743 Return a list whose car is `latex-fragment' and cdr a plist with
1744 `:value', `:begin', `:end', and `:post-blank' as keywords.
1746 Assume point is at the beginning of the latex fragment."
1748 (let* ((begin (point))
1752 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
1753 (when (or (looking-at latex-regexp
)
1757 (looking-at latex-regexp
))))
1758 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
1759 (plist-get org-format-latex-options
:matchers
))
1760 ;; None found: it's a macro.
1761 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
1763 (value (match-string-no-properties substring-match
))
1764 (post-blank (progn (goto-char (match-end substring-match
))
1765 (skip-chars-forward " \t")))
1767 (list 'latex-fragment
1771 :post-blank
,post-blank
)))))
1773 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
1774 "Interpret LATEX-FRAGMENT object as Org syntax.
1776 (org-element-get-property :value latex-fragment
))
1779 (defun org-element-line-break-parser ()
1780 "Parse line break at point.
1782 Return a list whose car is `line-break', and cdr a plist with
1783 `:begin', `:end' and `:post-blank' keywords.
1785 Assume point is at the beginning of the line break."
1787 (let* ((begin (point))
1788 (end (progn (end-of-line) (point)))
1789 (post-blank (- (skip-chars-backward " \t")))
1794 :post-blank
,post-blank
)))))
1796 (defun org-element-line-break-interpreter (line-break contents
)
1797 "Interpret LINE-BREAK object as Org syntax.
1799 (org-element-get-property :value line-break
))
1801 (defun org-element-line-break-successor (limit)
1802 "Search for the next statistics cookie and return position.
1804 LIMIT bounds the search.
1806 Return value is a cons cell whose car is `line-break' and cdr is
1807 beginning position."
1809 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
1810 (goto-char (match-beginning 1)))))
1811 ;; A line break can only happen on a non-empty line.
1812 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
1813 (cons 'line-break beg
)))))
1816 (defun org-element-link-parser ()
1817 "Parse link at point.
1819 Return a list whose car is `link' and cdr a plist with `:type',
1820 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
1821 `:contents-end' and `:post-blank' as keywords.
1823 Assume point is at the beginning of the link."
1825 (let ((begin (point))
1826 end contents-begin contents-end link-end post-blank path type
1829 ;; Type 1: text targeted from a radio target.
1830 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
1832 path
(org-match-string-no-properties 0)
1833 contents-begin
(match-beginning 0)
1834 contents-end
(match-end 0)
1835 link-end
(match-end 0)))
1836 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
1837 ((looking-at org-bracket-link-regexp
)
1838 (setq contents-begin
(match-beginning 3)
1839 contents-end
(match-end 3)
1840 link-end
(match-end 0)
1841 ;; RAW-LINK is the original link.
1842 raw-link
(org-match-string-no-properties 1)
1843 link
(org-link-expand-abbrev
1844 (replace-regexp-in-string
1845 " *\n *" " " (org-link-unescape raw-link
) t t
)))
1846 ;; Determine TYPE of link and set PATH accordingly.
1849 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
1850 (setq type
"file" path link
))
1851 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
1852 ((string-match org-link-re-with-space3 link
)
1853 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
1854 ;; Id type: PATH is the id.
1855 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
1856 (setq type
"id" path
(match-string 1 link
)))
1857 ;; Code-ref type: PATH is the name of the reference.
1858 ((string-match "^(\\(.*\\))$" link
)
1859 (setq type
"coderef" path
(match-string 1 link
)))
1860 ;; Custom-id type: PATH is the name of the custom id.
1861 ((= (aref link
0) ?
#)
1862 (setq type
"custom-id" path
(substring link
1)))
1863 ;; Fuzzy type: Internal link either matches a target, an
1864 ;; headline name or nothing. PATH is the target or headline's
1866 (t (setq type
"fuzzy" path link
))))
1867 ;; Type 3: Plain link, i.e. http://orgmode.org
1868 ((looking-at org-plain-link-re
)
1869 (setq raw-link
(org-match-string-no-properties 0)
1870 type
(org-match-string-no-properties 1)
1871 path
(org-match-string-no-properties 2)
1872 link-end
(match-end 0)))
1873 ;; Type 4: Angular link, i.e. <http://orgmode.org>
1874 ((looking-at org-angle-link-re
)
1875 (setq raw-link
(buffer-substring-no-properties
1876 (match-beginning 1) (match-end 2))
1877 type
(org-match-string-no-properties 1)
1878 path
(org-match-string-no-properties 2)
1879 link-end
(match-end 0))))
1880 ;; In any case, deduce end point after trailing white space from
1881 ;; LINK-END variable.
1882 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
1887 :raw-link
,(or raw-link path
)
1890 :contents-begin
,contents-begin
1891 :contents-end
,contents-end
1892 :post-blank
,post-blank
)))))
1894 (defun org-element-link-interpreter (link contents
)
1895 "Interpret LINK object as Org syntax.
1896 CONTENTS is the contents of the object."
1897 (let ((type (org-element-get-property :type link
))
1898 (raw-link (org-element-get-property :raw-link link
)))
1900 ((string= type
"radio") raw-link
)
1901 (t (format "[[%s]%s]"
1903 (if (string= contents
"") "" (format "[%s]" contents
)))))))
1905 (defun org-element-link-successor (limit)
1906 "Search for the next link and return position.
1908 LIMIT bounds the search.
1910 Return value is a cons cell whose car is `link' and cdr is
1911 beginning position."
1914 (if org-target-link-regexp
1915 (concat org-any-link-re
"\\|" org-target-link-regexp
)
1917 (when (re-search-forward link-regexp limit t
)
1918 (cons 'link
(match-beginning 0))))))
1921 (defun org-element-macro-parser ()
1922 "Parse macro at point.
1924 Return a list whose car is `macro' and cdr a plist with `:key',
1925 `:args', `:begin', `:end', `:value' and `:post-blank' as
1928 Assume point is at the macro."
1930 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
1931 (let ((begin (point))
1932 (key (downcase (org-match-string-no-properties 1)))
1933 (value (org-match-string-no-properties 0))
1934 (post-blank (progn (goto-char (match-end 0))
1935 (skip-chars-forward " \t")))
1937 (args (let ((args (org-match-string-no-properties 3)) args2
)
1939 (setq args
(org-split-string args
","))
1941 (while (string-match "\\\\\\'" (car args
))
1942 ;; Repair bad splits.
1943 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
1946 (push (pop args
) args2
))
1947 (mapcar 'org-trim
(nreverse args2
))))))
1954 :post-blank
,post-blank
)))))
1956 (defun org-element-macro-interpreter (macro contents
)
1957 "Interpret MACRO object as Org syntax.
1959 (org-element-get-property :value macro
))
1961 (defun org-element-macro-successor (limit)
1962 "Search for the next macro and return position.
1964 LIMIT bounds the search.
1966 Return value is cons cell whose car is `macro' and cdr is
1967 beginning position."
1969 (when (re-search-forward
1970 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
1972 (cons 'macro
(match-beginning 0)))))
1975 (defun org-element-radio-target-parser ()
1976 "Parse radio target at point.
1978 Return a list whose car is `radio-target' and cdr a plist with
1979 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
1980 and `:post-blank' as keywords.
1982 Assume point is at the radio target."
1984 (looking-at org-radio-target-regexp
)
1985 (let ((begin (point))
1986 (contents-begin (match-beginning 1))
1987 (contents-end (match-end 1))
1988 (raw-value (org-match-string-no-properties 1))
1989 (post-blank (progn (goto-char (match-end 0))
1990 (skip-chars-forward " \t")))
1995 :contents-begin
,contents-begin
1996 :contents-end
,contents-end
1997 :raw-value
,raw-value
1998 :post-blank
,post-blank
)))))
2000 (defun org-element-radio-target-interpreter (target contents
)
2001 "Interpret TARGET object as Org syntax.
2002 CONTENTS is the contents of the object."
2005 (defun org-element-radio-target-successor (limit)
2006 "Search for the next radio-target and return position.
2008 LIMIT bounds the search.
2010 Return value is a cons cell whose car is `radio-target' and cdr
2011 is beginning position."
2013 (when (re-search-forward org-radio-target-regexp limit t
)
2014 (cons 'radio-target
(match-beginning 0)))))
2016 ;;;; Statistics Cookie
2017 (defun org-element-statistics-cookie-parser ()
2018 "Parse statistics cookie at point.
2020 Return a list whose car is `statistics-cookie', and cdr a plist
2021 with `:begin', `:end', `:value' and `:post-blank' keywords.
2023 Assume point is at the beginning of the statistics-cookie."
2025 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2026 (let* ((begin (point))
2027 (value (buffer-substring-no-properties
2028 (match-beginning 0) (match-end 0)))
2029 (post-blank (progn (goto-char (match-end 0))
2030 (skip-chars-forward " \t")))
2032 (list 'statistics-cookie
2036 :post-blank
,post-blank
)))))
2038 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
2039 "Interpret STATISTICS-COOKIE object as Org syntax.
2041 (org-element-get-property :value statistics-cookie
))
2043 (defun org-element-statistics-cookie-successor (limit)
2044 "Search for the next statistics cookie and return position.
2046 LIMIT bounds the search.
2048 Return value is a cons cell whose car is `statistics-cookie' and
2049 cdr is beginning position."
2051 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
2052 (cons 'statistics-cookie
(match-beginning 0)))))
2055 (defun org-element-subscript-parser ()
2056 "Parse subscript at point.
2058 Return a list whose car is `subscript' and cdr a plist with
2059 `:begin', `:end', `:contents-begin', `:contents-end',
2060 `:use-brackets-p' and `:post-blank' as keywords.
2062 Assume point is at the underscore."
2064 (unless (bolp) (backward-char))
2065 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2067 (not (looking-at org-match-substring-regexp
))))
2068 (begin (match-beginning 2))
2069 (contents-begin (or (match-beginning 5)
2070 (match-beginning 3)))
2071 (contents-end (or (match-end 5) (match-end 3)))
2072 (post-blank (progn (goto-char (match-end 0))
2073 (skip-chars-forward " \t")))
2078 :use-brackets-p
,bracketsp
2079 :contents-begin
,contents-begin
2080 :contents-end
,contents-end
2081 :post-blank
,post-blank
)))))
2083 (defun org-element-subscript-interpreter (subscript contents
)
2084 "Interpret SUBSCRIPT object as Org syntax.
2085 CONTENTS is the contents of the object."
2087 (if (org-element-get-property :use-brackets-p subscript
) "_{%s}" "_%s")
2090 (defun org-element-sub/superscript-successor
(limit)
2091 "Search for the next sub/superscript and return beginning
2094 LIMIT bounds the search.
2096 Return value is a cons cell whose car is either `subscript' or
2097 `superscript' and cdr is beginning position."
2099 (when (re-search-forward org-match-substring-regexp limit t
)
2100 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
2101 (match-beginning 2)))))
2104 (defun org-element-superscript-parser ()
2105 "Parse superscript at point.
2107 Return a list whose car is `superscript' and cdr a plist with
2108 `:begin', `:end', `:contents-begin', `:contents-end',
2109 `:use-brackets-p' and `:post-blank' as keywords.
2111 Assume point is at the caret."
2113 (unless (bolp) (backward-char))
2114 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2116 (not (looking-at org-match-substring-regexp
))))
2117 (begin (match-beginning 2))
2118 (contents-begin (or (match-beginning 5)
2119 (match-beginning 3)))
2120 (contents-end (or (match-end 5) (match-end 3)))
2121 (post-blank (progn (goto-char (match-end 0))
2122 (skip-chars-forward " \t")))
2127 :use-brackets-p
,bracketsp
2128 :contents-begin
,contents-begin
2129 :contents-end
,contents-end
2130 :post-blank
,post-blank
)))))
2132 (defun org-element-superscript-interpreter (superscript contents
)
2133 "Interpret SUPERSCRIPT object as Org syntax.
2134 CONTENTS is the contents of the object."
2136 (if (org-element-get-property :use-brackets-p superscript
) "^{%s}" "^%s")
2140 (defun org-element-target-parser ()
2141 "Parse target at point.
2143 Return a list whose car is `target' and cdr a plist with
2144 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2145 and `:post-blank' as keywords.
2147 Assume point is at the target."
2149 (looking-at org-target-regexp
)
2150 (let ((begin (point))
2151 (contents-begin (match-beginning 1))
2152 (contents-end (match-end 1))
2153 (raw-value (org-match-string-no-properties 1))
2154 (post-blank (progn (goto-char (match-end 0))
2155 (skip-chars-forward " \t")))
2160 :contents-begin
,contents-begin
2161 :contents-end
,contents-end
2162 :raw-value
,raw-value
2163 :post-blank
,post-blank
)))))
2165 (defun org-element-target-interpreter (target contents
)
2166 "Interpret TARGET object as Org syntax.
2167 CONTENTS is the contents of target."
2170 (defun org-element-target-successor (limit)
2171 "Search for the next target and return position.
2173 LIMIT bounds the search.
2175 Return value is a cons cell whose car is `target' and cdr is
2176 beginning position."
2178 (when (re-search-forward org-target-regexp limit t
)
2179 (cons 'target
(match-beginning 0)))))
2182 (defun org-element-time-stamp-parser ()
2183 "Parse time stamp at point.
2185 Return a list whose car is `time-stamp', and cdr a plist with
2186 `:appt-type', `:type', `:begin', `:end', `:value' and
2187 `:post-blank' keywords.
2189 Assume point is at the beginning of the time-stamp."
2191 (let* ((appt-type (cond
2192 ((looking-at (concat org-deadline-string
" +"))
2193 (goto-char (match-end 0))
2195 ((looking-at (concat org-scheduled-string
" +"))
2196 (goto-char (match-end 0))
2198 ((looking-at (concat org-closed-string
" +"))
2199 (goto-char (match-end 0))
2201 (begin (and appt-type
(match-beginning 0)))
2203 ((looking-at org-tsr-regexp
)
2204 (if (match-string 2) 'active-range
'active
))
2205 ((looking-at org-tsr-regexp-both
)
2206 (if (match-string 2) 'inactive-range
'inactive
))
2207 ((looking-at (concat
2208 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2210 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2212 (begin (or begin
(match-beginning 0)))
2213 (value (buffer-substring-no-properties
2214 (match-beginning 0) (match-end 0)))
2215 (post-blank (progn (goto-char (match-end 0))
2216 (skip-chars-forward " \t")))
2219 `(:appt-type
,appt-type
2224 :post-blank
,post-blank
)))))
2226 (defun org-element-time-stamp-interpreter (time-stamp contents
)
2227 "Interpret TIME-STAMP object as Org syntax.
2230 (case (org-element-get-property :appt-type time-stamp
)
2231 (closed (concat org-closed-string
" "))
2232 (deadline (concat org-deadline-string
" "))
2233 (scheduled (concat org-scheduled-string
" ")))
2234 (org-element-get-property :value time-stamp
)))
2236 (defun org-element-time-stamp-successor (limit)
2237 "Search for the next time-stamp and return position.
2239 LIMIT bounds the search.
2241 Return value is a cons cell whose car is `time-stamp' and cdr is
2242 beginning position."
2244 (when (re-search-forward
2245 (concat "\\(?:" org-scheduled-string
" +\\|"
2246 org-deadline-string
" +\\|" org-closed-string
" +\\)?"
2249 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2251 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2253 (cons 'time-stamp
(match-beginning 0)))))
2256 (defun org-element-verbatim-parser ()
2257 "Parse verbatim object at point.
2259 Return a list whose car is `verbatim' and cdr is a plist with
2260 `:marker', `:begin', `:end' and `:post-blank' keywords.
2262 Assume point is at the first verbatim marker."
2264 (unless (bolp) (backward-char 1))
2265 (looking-at org-emph-re
)
2266 (let ((begin (match-beginning 2))
2267 (marker (org-match-string-no-properties 3))
2268 (value (org-match-string-no-properties 4))
2269 (post-blank (progn (goto-char (match-end 2))
2270 (skip-chars-forward " \t")))
2277 :post-blank
,post-blank
)))))
2279 (defun org-element-verbatim-interpreter (verbatim contents
)
2280 "Interpret VERBATIM object as Org syntax.
2282 (let ((marker (org-element-get-property :marker verbatim
))
2283 (value (org-element-get-property :value verbatim
)))
2284 (concat marker value marker
)))
2288 ;;; Definitions And Rules
2290 ;; Define elements, greater elements and specify recursive objects,
2291 ;; along with the affiliated keywords recognized. Also set up
2292 ;; restrictions on recursive objects combinations.
2294 ;; These variables really act as a control center for the parsing
2296 (defconst org-element-paragraph-separate
2297 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2298 ;; Headlines and inlinetasks.
2299 org-outline-regexp-bol
"\\|"
2300 ;; Comments, blocks (any type), keywords and babel calls.
2301 "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
2303 (org-item-beginning-re) "\\|"
2304 ;; Fixed-width, drawers (any type) and tables.
2306 ;; Footnote definitions.
2307 org-footnote-definition-re
"\\|"
2308 ;; Horizontal rules.
2309 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2310 ;; LaTeX environments.
2311 "^[ \t]*\\\\\\(begin\\|end\\)")
2312 "Regexp to separate paragraphs in an Org buffer.")
2314 (defconst org-element-all-elements
2315 '(center-block comment comment-block drawer dynamic-block example-block
2316 export-block fixed-width footnote-definition headline
2317 horizontal-rule inlinetask item keyword latex-environment
2318 babel-call paragraph plain-list property-drawer quote-block
2319 quote-section special-block src-block table verse-block
)
2320 "Complete list of elements.")
2322 (defconst org-element-greater-elements
2323 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2324 item plain-list quote-block special-block
)
2325 "List of recursive element types aka Greater Elements.")
2327 (defconst org-element-all-successors
2328 '(export-snippet footnote-reference inline-babel-call inline-src-block
2329 latex-or-entity line-break link macro radio-target
2330 statistics-cookie sub
/superscript target text-markup
2332 "Complete list of successors.")
2334 (defconst org-element-object-successor-alist
2335 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
2336 (emphasis . text-markup
) (verbatim . text-markup
)
2337 (entity . latex-or-entity
) (latex-fragment . latex-or-entity
))
2338 "Alist of translations between object type and successor name.
2340 Sharing the same successor comes handy when, for example, the
2341 regexp matching one object can also match the other object.")
2343 (defconst org-element-recursive-objects
2344 '(emphasis link subscript superscript target radio-target
)
2345 "List of recursive object types.")
2347 (defconst org-element-non-recursive-block-alist
2348 '(("ascii" . export-block
)
2349 ("comment" . comment-block
)
2350 ("docbook" . export-block
)
2351 ("example" . example-block
)
2352 ("html" . export-block
)
2353 ("latex" . latex-block
)
2354 ("odt" . export-block
)
2356 ("verse" . verse-block
))
2357 "Alist between non-recursive block name and their element type.")
2359 (defconst org-element-affiliated-keywords
2360 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "caption"
2361 "data" "header" "headers" "label" "name" "plot" "resname" "result" "results"
2362 "source" "srcname" "tblname")
2363 "List of affiliated keywords as strings.")
2365 (defconst org-element-keyword-translation-alist
2366 '(("data" .
"name") ("label" .
"name") ("resname" .
"name")
2367 ("source" .
"name") ("srcname" .
"name") ("tblname" .
"name")
2368 ("result" .
"results") ("headers" .
"header"))
2369 "Alist of usual translations for keywords.
2370 The key is the old name and the value the new one. The property
2371 holding their value will be named after the translated name.")
2373 (defconst org-element-multiple-keywords
2374 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "header")
2375 "List of affiliated keywords that can occur more that once in an element.
2377 Their value will be consed into a list of strings, which will be
2378 returned as the value of the property.
2380 This list is checked after translations have been applied. See
2381 `org-element-keyword-translation-alist'.")
2383 (defconst org-element-parsed-keywords
'("author" "caption" "title")
2384 "List of keywords whose value can be parsed.
2386 Their value will be stored as a secondary string: a list of
2387 strings and objects.
2389 This list is checked after translations have been applied. See
2390 `org-element-keyword-translation-alist'.")
2392 (defconst org-element-dual-keywords
'("results")
2393 "List of keywords which can have a secondary value.
2395 In Org syntax, they can be written with optional square brackets
2396 before the colons. For example, results keyword can be
2397 associated to a hash value with the following:
2399 #+results[hash-string]: some-source
2401 This list is checked after translations have been applied. See
2402 `org-element-keyword-translation-alist'.")
2404 (defconst org-element-object-restrictions
2405 '((emphasis entity export-snippet inline-babel-call inline-src-block
2406 radio-target sub
/superscript target text-markup time-stamp
)
2407 (link entity export-snippet inline-babel-call inline-src-block
2408 latex-fragment sub
/superscript text-markup
)
2409 (radio-target entity export-snippet latex-fragment sub
/superscript
)
2410 (subscript entity export-snippet inline-babel-call inline-src-block
2411 latex-fragment sub
/superscript text-markup
)
2412 (superscript entity export-snippet inline-babel-call inline-src-block
2413 latex-fragment sub
/superscript text-markup
)
2414 (target entity export-snippet latex-fragment sub
/superscript text-markup
))
2415 "Alist of recursive objects restrictions.
2417 Car is a recursive object type and cdr is a list of successors
2418 that will be called within an object of such type.
2420 For example, in a `radio-target' object, one can only find
2421 entities, export snippets, latex-fragments, subscript and
2424 (defconst org-element-string-restrictions
2425 '((headline entity inline-babel-call latex-fragment link macro radio-target
2426 statistics-cookie sub
/superscript text-markup time-stamp
)
2427 (inlinetask entity inline-babel-call latex-fragment link macro radio-target
2428 sub
/superscript text-markup time-stamp
)
2429 (item entity inline-babel-call latex-fragment macro radio-target
2430 sub
/superscript target verbatim
)
2431 (keyword entity latex-fragment macro sub
/superscript text-markup
)
2432 (table entity latex-fragment macro text-markup
)
2433 (verse entity footnote-reference inline-babel-call inline-src-block
2434 latex-fragment line-break link macro radio-target sub
/superscript
2435 target text-markup time-stamp
))
2436 "Alist of secondary strings restrictions.
2438 When parsed, some elements have a secondary string which could
2439 contain various objects (i.e. headline's name, or table's cells).
2440 For association, the car is the element type, and the cdr a list
2441 of successors that will be called in that secondary string.
2443 Note: `keyword' secondary string type only applies to keywords
2444 matching `org-element-parsed-keywords'.")
2450 ;; Provide two accessors: `org-element-get-property' and
2451 ;; `org-element-get-contents'.
2452 (defun org-element-get-property (property element
)
2453 "Extract the value from the PROPERTY of an ELEMENT."
2454 (plist-get (nth 1 element
) property
))
2456 (defun org-element-get-contents (element)
2457 "Extract contents from an ELEMENT."
2462 ;; Obtaining The Smallest Element Containing Point
2464 ;; `org-element-at-point' is the core function of this section. It
2465 ;; returns the Lisp representation of the element at point. It uses
2466 ;; `org-element-guess-type' and `org-element-skip-keywords' as helper
2469 ;; When point is at an item, there is no automatic way to determine if
2470 ;; the function should return the `plain-list' element, or the
2471 ;; corresponding `item' element. By default, `org-element-at-point'
2472 ;; works at the `plain-list' level. But, by providing an optional
2473 ;; argument, one can make it switch to the `item' level.
2474 (defconst org-element--affiliated-re
2475 (format "[ \t]*#\\+\\(%s\\):"
2478 (if (member keyword org-element-dual-keywords
)
2479 (format "\\(%s\\)\\(?:\\[\\(.*?\\)\\]\\)?"
2480 (regexp-quote keyword
))
2481 (regexp-quote keyword
)))
2482 org-element-affiliated-keywords
"\\|"))
2483 "Regexp matching any affiliated keyword.
2485 Keyword name is put in match group 1. Moreover, if keyword
2486 belongs to `org-element-dual-keywords', put the dual value in
2489 Don't modify it, set `org-element--affiliated-keywords' instead.")
2491 (defun org-element-at-point (&optional toggle-item structure
)
2492 "Determine closest element around point.
2494 Return value is a list \(TYPE PROPS\) where TYPE is the type of
2495 the element and PROPS a plist of properties associated to the
2498 Possible types are defined in `org-element-all-elements'.
2500 If optional argument TOGGLE-ITEM is non-nil, parse item wise
2501 instead of plain-list wise, using STRUCTURE as the current list
2504 If STRUCTURE isn't provided but TOGGLE-ITEM is non-nil, it will
2508 ;; Move before any blank line.
2509 (when (looking-at "[ \t]*$")
2510 (skip-chars-backward " \r\t\n")
2511 (beginning-of-line))
2512 (let ((case-fold-search t
))
2513 ;; Check if point is at an affiliated keyword. In that case,
2514 ;; try moving to the beginning of the associated element. If
2515 ;; the keyword is orphaned, treat it as plain text.
2516 (when (looking-at org-element--affiliated-re
)
2517 (let ((opoint (point)))
2518 (while (looking-at org-element--affiliated-re
) (forward-line))
2519 (when (looking-at "[ \t]*$") (goto-char opoint
))))
2520 (let ((type (org-element-guess-type)))
2522 ;; Guessing element type on the current line is impossible:
2523 ;; try to find the beginning of the current element to get
2524 ;; more information.
2526 (let ((search-origin (point))
2527 (opoint-in-item-p (org-in-item-p))
2531 (re-search-backward org-element-paragraph-separate nil
'm
))))
2533 ;; Unable to find a paragraph delimiter above: we're at
2534 ;; bob and looking at a paragraph.
2535 ((not par-found-p
) (org-element-paragraph-parser))
2536 ;; Trying to find element's beginning set point back to
2537 ;; its original position. There's something peculiar on
2538 ;; this line that prevents parsing, probably an
2539 ;; ill-formed keyword or an undefined drawer name. Parse
2540 ;; it as plain text anyway.
2541 ((< search-origin
(point-at-eol)) (org-element-paragraph-parser))
2542 ;; Original point wasn't in a list but previous paragraph
2543 ;; is. It means that either point was inside some block,
2544 ;; or current list was ended without using a blank line.
2545 ;; In the last case, paragraph really starts at list end.
2547 (and (not opoint-in-item-p
)
2548 (not (looking-at "[ \t]*#\\+begin"))
2549 (setq item
(org-in-item-p))
2550 (let ((struct (save-excursion (goto-char item
)
2551 (org-list-struct))))
2552 (goto-char (org-list-get-bottom-point struct
))
2553 (org-skip-whitespace)
2555 (org-element-paragraph-parser)))))
2556 ((org-footnote-at-definition-p)
2557 (org-element-footnote-definition-parser))
2558 ((and opoint-in-item-p
(org-at-item-p) (= opoint-in-item-p
(point)))
2560 (org-element-item-parser (or structure
(org-list-struct)))
2561 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2562 ;; In any other case, the paragraph started the line
2564 (t (forward-line) (org-element-paragraph-parser)))))
2565 ((eq type
'plain-list
)
2567 (org-element-item-parser (or structure
(org-list-struct)))
2568 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2569 ;; Straightforward case: call the appropriate parser.
2570 (t (funcall (intern (format "org-element-%s-parser" type
)))))))))
2573 ;; It is obvious to tell if point is in most elements, either by
2574 ;; looking for a specific regexp in the current line, or by using
2575 ;; already implemented functions. This is the goal of
2576 ;; `org-element-guess-type'.
2577 (defconst org-element--element-block-types
2578 (mapcar 'car org-element-non-recursive-block-alist
)
2579 "List of non-recursive block types, as strings.
2580 Used internally by `org-element-guess-type'. Do not modify it
2581 directly, set `org-element-non-recursive-block-alist' instead.")
2583 (defun org-element-guess-type ()
2584 "Return the type of element at point, or nil if undetermined.
2585 This function may move point to an appropriate position for
2586 parsing. Used internally by `org-element-at-point'."
2587 ;; Beware: Order matters for some cases in that function.
2589 (let ((case-fold-search t
))
2591 ((org-with-limited-levels (org-at-heading-p)) 'headline
)
2592 ((let ((headline (ignore-errors (nth 4 (org-heading-components)))))
2594 (let (case-fold-search)
2595 (string-match (format "^%s\\(?: \\|$\\)" org-quote-string
)
2598 ;; Non-recursive block.
2599 ((let ((type (org-in-block-p org-element--element-block-types
)))
2600 (and type
(cdr (assoc type org-element-non-recursive-block-alist
)))))
2601 ((org-at-heading-p) 'inlinetask
)
2602 ((org-between-regexps-p
2603 "^[ \t]*\\\\begin{" "^[ \t]*\\\\end{[^}]*}[ \t]*") 'latex-environment
)
2604 ;; Property drawer. Almost `org-at-property-p', but allow drawer
2606 ((org-with-wide-buffer
2607 (and (not (org-before-first-heading-p))
2608 (let ((pblock (org-get-property-block)))
2610 (<= (point) (cdr pblock
))
2611 (>= (point-at-eol) (1- (car pblock
)))))))
2613 ;; Recursive block. If the block isn't complete, parse the
2614 ;; current part as a paragraph.
2615 ((looking-at "[ \t]*#\\+\\(begin\\|end\\)_\\([-A-Za-z0-9]+\\)\\(?:$\\|\\s-\\)")
2616 (let ((type (downcase (match-string 2))))
2618 ((not (org-in-block-p (list type
))) 'paragraph
)
2619 ((string= type
"center") 'center-block
)
2620 ((string= type
"quote") 'quote-block
)
2621 (t 'special-block
))))
2622 ;; Regular drawers must be tested after property drawer as both
2623 ;; elements share the same ending regexp.
2624 ((or (looking-at org-drawer-regexp
) (looking-at "[ \t]*:END:[ \t]*$"))
2625 (let ((completep (org-between-regexps-p
2626 org-drawer-regexp
"^[ \t]*:END:[ \t]*$")))
2629 (goto-char (car completep
)) 'drawer
)))
2630 ((looking-at "[ \t]*:\\( \\|$\\)") 'fixed-width
)
2631 ;; Babel calls must be tested before general keywords as they are
2632 ;; a subset of them.
2633 ((looking-at org-babel-block-lob-one-liner-regexp
) 'babel-call
)
2634 ((looking-at org-footnote-definition-re
) 'footnote-definition
)
2635 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
2636 (if (member (downcase (match-string 1)) org-element-affiliated-keywords
)
2639 ;; Dynamic block: simplify regexp used for match. If it isn't
2640 ;; complete, parse the current part as a paragraph.
2641 ((looking-at "[ \t]*#\\+\\(begin\\end\\):\\(?:\\s-\\|$\\)")
2642 (let ((completep (org-between-regexps-p
2643 "^[ \t]*#\\+begin:\\(?:\\s-\\|$\\)"
2644 "^[ \t]*#\\+end:\\(?:\\s-\\|$\\)")))
2647 (goto-char (car completep
)) 'dynamic-block
)))
2648 ((looking-at "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)") 'comment
)
2649 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$") 'horizontal-rule
)
2650 ((org-at-table-p t
) 'table
)
2651 ((looking-at "[ \t]*#\\+tblfm:")
2653 ;; A TBLFM line separated from any table is just plain text.
2654 (if (org-at-table-p)
2656 (forward-line) 'paragraph
))
2657 ((looking-at (org-item-re)) 'plain-list
))))
2659 ;; Most elements can have affiliated keywords. When looking for an
2660 ;; element beginning, we want to move before them, as they belong to
2661 ;; that element, and, in the meantime, collect information they give
2662 ;; into appropriate properties. Hence the following function.
2664 ;; Usage of optional arguments may not be obvious at first glance:
2666 ;; - TRANS-LIST is used to polish keywords names that have evolved
2667 ;; during Org history. In example, even though =result= and
2668 ;; =results= coexist, we want to have them under the same =result=
2669 ;; property. It's also true for "srcname" and "name", where the
2670 ;; latter seems to be preferred nowadays (thus the "name" property).
2672 ;; - CONSED allows to regroup multi-lines keywords under the same
2673 ;; property, while preserving their own identity. This is mostly
2674 ;; used for "attr_latex" and al.
2676 ;; - PARSED prepares a keyword value for export. This is useful for
2677 ;; "caption". Objects restrictions for such keywords are defined in
2678 ;; `org-element-string-restrictions'.
2680 ;; - DUALS is used to take care of keywords accepting a main and an
2681 ;; optional secondary values. For example "results" has its
2682 ;; source's name as the main value, and may have an hash string in
2683 ;; optional square brackets as the secondary one.
2685 ;; A keyword may belong to more than one category.
2686 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
2687 consed parsed duals
)
2688 "Collect affiliated keywords before point.
2690 Optional argument KEY-RE is a regexp matching keywords, which
2691 puts matched keyword in group 1. It defaults to
2692 `org-element--affiliated-re'.
2694 TRANS-LIST is an alist where key is the keyword and value the
2695 property name it should be translated to, without the colons. It
2696 defaults to `org-element-keyword-translation-alist'.
2698 CONSED is a list of strings. Any keyword belonging to that list
2699 will have its value consed. The check is done after keyword
2700 translation. It defaults to `org-element-multiple-keywords'.
2702 PARSED is a list of strings. Any keyword member of this list
2703 will have its value parsed. The check is done after keyword
2704 translation. If a keyword is a member of both CONSED and PARSED,
2705 it's value will be a list of parsed strings. It defaults to
2706 `org-element-parsed-keywords'.
2708 DUALS is a list of strings. Any keyword member of this list can
2709 have two parts: one mandatory and one optional. Its value is
2710 a cons cell whose car is the former, and the cdr the latter. If
2711 a keyword is a member of both PARSED and DUALS, only the primary
2712 part will be parsed. It defaults to `org-element-dual-keywords'.
2714 Return a list whose car is the position at the first of them and
2715 cdr a plist of keywords and values."
2717 (let ((case-fold-search t
)
2718 (key-re (or key-re org-element--affiliated-re
))
2719 (trans-list (or trans-list org-element-keyword-translation-alist
))
2720 (consed (or consed org-element-multiple-keywords
))
2721 (parsed (or parsed org-element-parsed-keywords
))
2722 (duals (or duals org-element-dual-keywords
))
2725 (while (and (not (bobp))
2726 (progn (forward-line -
1) (looking-at key-re
)))
2727 (let* ((raw-kwd (downcase (or (match-string 2) (match-string 1))))
2728 ;; Apply translation to RAW-KWD. From there, KWD is
2729 ;; the official keyword.
2730 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
2731 ;; If KWD is a dual keyword, find it secondary value.
2732 (dual-value (and (member kwd duals
)
2733 (org-match-string-no-properties 3)))
2734 ;; Find main value for any keyword.
2735 (value (org-trim (buffer-substring-no-properties
2736 (match-end 0) (point-at-eol))))
2737 ;; Attribute a property name to KWD.
2738 (kwd-sym (and kwd
(intern (concat ":" kwd
)))))
2739 ;; Now set final shape for VALUE.
2740 (when (member kwd parsed
)
2742 (org-element-parse-secondary-string
2744 (cdr (assq 'keyword org-element-string-restrictions
)))))
2745 (when (member kwd duals
) (setq value
(cons value dual-value
)))
2746 (when (member kwd consed
)
2747 (setq value
(cons value
(plist-get output kwd-sym
))))
2748 ;; Eventually store the new value in OUTPUT.
2749 (setq output
(plist-put output kwd-sym value
))))
2750 (unless (looking-at key-re
) (forward-line 1)))
2751 (list (point) output
))))
2757 ;; The two major functions here are `org-element-parse-buffer', which
2758 ;; parses Org syntax inside the current buffer, taking into account
2759 ;; region, narrowing, or even visibility if specified, and
2760 ;; `org-element-parse-secondary-string', which parses objects within
2763 ;; The (almost) almighty `org-element-map' allows to apply a function
2764 ;; on elements or objects matching some type, and accumulate the
2765 ;; resulting values. In an export situation, it also skips unneeded
2766 ;; parts of the parse tree, transparently walks into included files,
2767 ;; and maintain a list of local properties (i.e. those inherited from
2768 ;; parent headlines) for function's consumption.
2769 (defun org-element-parse-buffer (&optional granularity visible-only
)
2770 "Recursively parse the buffer and return structure.
2771 If narrowing is in effect, only parse the visible part of the
2774 Optional argument GRANULARITY determines the depth of the
2775 recursion. It can be set to the following symbols:
2777 `headline' Only parse headlines.
2778 `greater-element' Don't recurse into greater elements. Thus,
2779 elements parsed are the top-level ones.
2780 `element' Parse everything but objects and plain text.
2781 `object' Parse the complete buffer (default).
2783 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
2786 Assume buffer is in Org mode."
2788 (goto-char (point-min))
2789 (org-skip-whitespace)
2790 (nconc (list 'org-data nil
)
2791 (org-element-parse-elements
2792 (point-at-bol) (point-max)
2793 nil nil granularity visible-only nil
))))
2795 (defun org-element-parse-secondary-string (string restriction
&optional buffer
)
2796 "Recursively parse objects in STRING and return structure.
2798 RESTRICTION, when non-nil, is a symbol limiting the object types
2799 that will be looked after.
2801 Optional argument BUFFER indicates the buffer from where the
2802 secondary string was extracted. It is used to determine where to
2803 get extraneous information for an object \(i.e. when resolving
2804 a link or looking for a footnote definition\). It defaults to
2805 the current buffer."
2808 (org-element-parse-objects (point-min) (point-max) nil restriction
)))
2810 (defun org-element-map (data types fun
&optional info first-match
)
2811 "Map a function on selected elements or objects.
2813 DATA is the parsed tree, as returned by, i.e,
2814 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
2815 of elements or objects types. FUN is the function called on the
2816 matching element or object. It must accept two arguments: the
2817 element or object itself and a plist holding contextual
2820 When optional argument INFO is non-nil, it should be a plist
2821 holding export options. In that case, parts of the parse tree
2822 not exportable according to that property list will be skipped
2823 and files included through a keyword will be visited.
2825 When optional argument FIRST-MATCH is non-nil, stop at the first
2826 match for which FUN doesn't return nil, and return that value.
2828 Nil values returned from FUN are ignored in the result."
2829 ;; Ensure TYPES is a list, even of one element.
2830 (unless (listp types
) (setq types
(list types
)))
2831 ;; Recursion depth is determined by --CATEGORY.
2834 ((loop for type in types
2835 always
(memq type org-element-greater-elements
))
2837 ((loop for type in types
2838 always
(memq type org-element-all-elements
))
2841 walk-tree
; For byte-compiler
2845 (lambda (--type types fun --blob --local
)
2846 ;; Check if TYPE is matching among TYPES. If so, apply
2847 ;; FUN to --BLOB and accumulate return value
2848 ;; into --ACC. --LOCAL is the communication channel.
2849 (when (memq --type types
)
2850 (let ((result (funcall fun --blob --local
)))
2851 (cond ((not result
))
2852 (first-match (throw 'first-match result
))
2853 (t (push result --acc
))))))))
2856 (lambda (--data --local
)
2857 ;; Recursively walk DATA. --LOCAL, if non-nil, is
2858 ;; a plist holding contextual information.
2861 (let ((--type (if (stringp --blob
) 'plain-text
(car --blob
))))
2862 ;; Determine if a recursion into --BLOB is
2863 ;; possible and allowed.
2865 ;; Element or object not exportable.
2866 ((and info
(org-export-skip-p --blob info
)))
2867 ;; Archived headline: Maybe apply fun on it, but
2870 (eq --type
'headline
)
2871 (eq (plist-get info
:with-archived-trees
) 'headline
)
2872 (org-element-get-property :archivedp --blob
))
2873 (funcall accumulate-maybe --type types fun --blob --local
))
2874 ;; At an include keyword: apply mapping to its
2877 (eq --type
'keyword
)
2879 (downcase (org-element-get-property :key --blob
))
2881 (funcall accumulate-maybe --type types fun --blob --local
)
2883 (org-export-parse-included-file --blob --local
))
2884 (--value (org-element-get-property :value --blob
))
2886 (and (string-match "^\"\\(\\S-+\\)\"" --value
)
2887 (match-string 1 --value
))))
2892 ;; Store full path of already included files
2893 ;; to avoid recursive file inclusion.
2895 ,(cons (expand-file-name --file
)
2896 (plist-get --local
:included-files
))
2897 ;; Ensure that a top-level headline in the
2898 ;; included file becomes a direct child of
2899 ;; the current headline in the buffer.
2902 (plist-get --local
:inherited-properties
)
2904 (or (plist-get --local
:headline-offset
) 0))
2905 (1- (org-export-get-min-level
2906 --data --local
))))))))
2907 ;; Limiting recursion to greater elements, and --BLOB
2909 ((and (eq --category
'greater-elements
)
2910 (not (memq --type org-element-greater-elements
)))
2911 (funcall accumulate-maybe --type types fun --blob --local
))
2912 ;; Limiting recursion to elements, and --BLOB only
2913 ;; contains objects.
2914 ((and (eq --category
'elements
) (eq --type
'paragraph
)))
2915 ;; No limitation on recursion, but --BLOB hasn't
2916 ;; got a recursive type.
2917 ((and (eq --category
'objects
)
2918 (not (or (eq --type
'paragraph
)
2919 (memq --type org-element-greater-elements
)
2920 (memq --type org-element-recursive-objects
))))
2921 (funcall accumulate-maybe --type types fun --blob --local
))
2922 ;; Recursion is possible and allowed: Update local
2923 ;; information and move into --BLOB.
2924 (t (funcall accumulate-maybe --type types fun --blob --local
)
2927 (and info
(org-export-update-info --blob --local t
)))))))
2928 (org-element-get-contents --data
))))))
2930 (funcall walk-tree data info
)
2931 ;; Return value in a proper order.
2934 ;; The following functions are internal parts of the parser. The
2935 ;; first one, `org-element-parse-elements' acts at the element's
2936 ;; level. The second one, `org-element-parse-objects' applies on all
2937 ;; objects of a paragraph or a secondary string. It uses
2938 ;; `org-element-get-candidates' to optimize the search of the next
2939 ;; object in the buffer.
2941 ;; More precisely, that function looks for every allowed object type
2942 ;; first. Then, it discards failed searches, keeps further matches,
2943 ;; and searches again types matched behind point, for subsequent
2944 ;; calls. Thus, searching for a given type fails only once, and every
2945 ;; object is searched only once at top level (but sometimes more for
2947 (defun org-element-parse-elements (beg end item structure granularity visible-only acc
)
2948 "Parse ELEMENT with point at its beginning.
2950 If ITEM is non-nil, parse item wise instead of plain-list wise,
2951 using STRUCTURE as the current list structure.
2953 GRANULARITY determines the depth of the recursion. It can be set
2954 to the following symbols:
2956 `headline' Only parse headlines.
2957 `greater-element' Don't recurse into greater elements. Thus,
2958 elements parsed are the top-level ones.
2959 `element' Parse everything but objects and plain text.
2960 `object' or nil Parse the complete buffer.
2962 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
2965 Elements are accumulated into ACC."
2968 ;; Shortcut when parsing only headlines.
2969 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
2970 (org-with-limited-levels (outline-next-heading)))
2972 (while (and (< (point) end
) (not (eobp)))
2974 ;; 1. If ITEM is toggled, point is at an item. Knowing that,
2975 ;; there's no need to go through `org-element-at-point'.
2977 (let* ((element (org-element-item-parser structure
))
2978 (cbeg (org-element-get-property :contents-begin element
))
2979 (cend (org-element-get-property :contents-end element
)))
2980 (goto-char (org-element-get-property :end element
))
2981 ;; Narrow region to contents, so that item bullet don't
2982 ;; interfere with paragraph parsing.
2984 (narrow-to-region cbeg cend
)
2985 (org-element-parse-elements
2986 cbeg cend nil structure granularity visible-only
2987 (reverse element
))))
2988 ;; 2. When ITEM is nil, find current element's type and parse
2989 ;; it accordingly to its category.
2990 (let ((element (org-element-at-point nil structure
)))
2991 (goto-char (org-element-get-property :end element
))
2993 ;; Case 1: ELEMENT is a footnote-definition. If
2994 ;; GRANURALITY allows parsing, use narrowing so that
2995 ;; footnote label don't interfere with paragraph
2997 ((and (eq (car element
) 'footnote-definition
)
2998 (not (memq granularity
'(headline greater-element
))))
2999 (let ((cbeg (org-element-get-property :contents-begin element
))
3000 (cend (org-element-get-property :contents-end element
)))
3002 (narrow-to-region cbeg cend
)
3003 (org-element-parse-elements
3004 cbeg cend nil structure granularity visible-only
3005 (reverse element
)))))
3006 ;; Case 1: ELEMENT is a paragraph. Parse objects inside,
3007 ;; if GRANULARITY allows it.
3008 ((and (eq (car element
) 'paragraph
)
3009 (or (not granularity
) (eq granularity
'object
)))
3010 (org-element-parse-objects
3011 (org-element-get-property :contents-begin element
)
3012 (org-element-get-property :contents-end element
)
3015 ;; Case 2: ELEMENT is recursive: parse it between
3016 ;; `contents-begin' and `contents-end'. If it's
3017 ;; a plain list, also switch to item mode. Make
3018 ;; sure GRANULARITY allows the recursion, or
3019 ;; ELEMENT is an headline, in which case going
3020 ;; inside is mandatory, in order to get sub-level
3021 ;; headings. If VISIBLE-ONLY is true and element
3022 ;; is hidden, do not recurse into it.
3023 ((and (memq (car element
) org-element-greater-elements
)
3024 (or (not granularity
)
3025 (memq granularity
'(element object
))
3026 (eq (car element
) 'headline
))
3027 (not (and visible-only
3028 (org-element-get-property :hiddenp element
))))
3029 (org-element-parse-elements
3030 (org-element-get-property :contents-begin element
)
3031 (org-element-get-property :contents-end element
)
3032 (eq (car element
) 'plain-list
)
3033 (org-element-get-property :structure element
)
3037 ;; Case 3: Else, just accumulate ELEMENT, unless
3038 ;; GRANULARITY is set to `headline'.
3039 ((not (eq granularity
'headline
)) element
))))
3041 (org-skip-whitespace))
3045 (defun org-element-parse-objects (beg end acc restriction
)
3046 "Parse objects between BEG and END and return recursive structure.
3048 Objects are accumulated in ACC.
3050 RESTRICTION, when non-nil, is a list of object types which are
3051 allowed in the current object."
3052 (let ((get-next-object
3055 ;; Return the parsing function associated to the nearest
3056 ;; object among list of candidates CAND.
3057 (let ((pos (apply #'min
(mapcar #'cdr cand
))))
3062 (format "org-element-%s-parser" (car (rassq pos cand
))))))))))
3063 next-object candidates
)
3066 (while (setq candidates
(org-element-get-next-object-candidates
3067 end restriction candidates
))
3068 (setq next-object
(funcall get-next-object candidates
))
3069 ;; 1. Text before any object.
3070 (let ((obj-beg (org-element-get-property :begin next-object
)))
3071 (unless (= beg obj-beg
)
3072 (push (buffer-substring-no-properties (point) obj-beg
) acc
)))
3074 (let ((obj-end (org-element-get-property :end next-object
))
3075 (cont-beg (org-element-get-property :contents-begin next-object
)))
3076 (push (if (and (memq (car next-object
) org-element-recursive-objects
)
3078 ;; ... recursive. The CONT-BEG check is for
3079 ;; links, as some of them might not be recursive
3080 ;; (i.e. plain links).
3084 (org-element-get-property :contents-end next-object
))
3085 (org-element-parse-objects
3086 (point-min) (point-max) (reverse next-object
)
3087 ;; Restrict allowed objects. This is the
3088 ;; intersection of current restriction and next
3089 ;; object's restriction.
3091 (cdr (assq (car next-object
)
3092 org-element-object-restrictions
))))
3093 (if (not restriction
)
3097 (and (memq e restriction
) e
))
3099 ;; ... not recursive.
3102 (goto-char obj-end
)))
3103 ;; 3. Text after last object.
3104 (unless (= (point) end
)
3105 (push (buffer-substring-no-properties (point) end
) acc
))
3109 (defun org-element-get-next-object-candidates (limit restriction objects
)
3110 "Return an alist of candidates for the next object.
3112 LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
3113 the possible object types.
3115 Return value is an alist whose car is position and cdr the object
3116 type, as a string. There is an association for the closest
3117 object of each type within RESTRICTION when non-nil, or for every
3120 OBJECTS is the previous candidates alist."
3121 (let ((restriction (or restriction org-element-all-successors
))
3122 next-candidates types-to-search
)
3123 ;; If no previous result, search every object type in RESTRICTION.
3124 ;; Otherwise, keep potential candidates (old objects located after
3125 ;; point) and ask to search again those which had matched before.
3128 (if (< (cdr obj
) (point))
3129 (push (car obj
) types-to-search
)
3130 (push obj next-candidates
)))
3132 (setq types-to-search restriction
))
3133 ;; Call the appropriate "get-next" function for each type to
3134 ;; search and accumulate matches.
3137 (let* ((successor-fun
3139 (format "org-element-%s-successor"
3140 (or (cdr (assq type org-element-object-successor-alist
))
3142 (obj (funcall successor-fun limit
)))
3143 (and obj
(push obj next-candidates
))))
3150 ;;; Towards A Bijective Process
3152 ;; The parse tree obtained with `org-element-parse-buffer' is really
3153 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3154 ;; interpreted and expanded into a string with canonical Org
3155 ;; syntax. Hence `org-element-interpret-data'.
3157 ;; Data parsed from secondary strings, whose shape is slightly
3158 ;; different than the standard parse tree, is expanded with the
3159 ;; equivalent function `org-element-interpret-secondary'.
3161 ;; Both functions rely internally on
3162 ;; `org-element-interpret--affiliated-keywords'.
3163 (defun org-element-interpret-data (data &optional genealogy previous
)
3164 "Interpret a parse tree representing Org data.
3166 DATA is the parse tree to interpret.
3168 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3170 GENEALOGY is the list of its parents types.
3171 PREVIOUS is the type of the element or object at the same level
3174 Return Org syntax as a string."
3177 ;; BLOB can be an element, an object, a string, or nil.
3180 ((equal blob
"") nil
)
3181 ((stringp blob
) blob
)
3183 (let* ((type (car blob
))
3185 (if (eq type
'org-data
)
3187 (intern (format "org-element-%s-interpreter" type
))))
3190 ;; Full Org document.
3191 ((eq type
'org-data
)
3192 (org-element-interpret-data blob genealogy previous
))
3193 ;; Recursive objects.
3194 ((memq type org-element-recursive-objects
)
3195 (org-element-interpret-data
3196 blob
(cons type genealogy
) nil
))
3197 ;; Recursive elements.
3198 ((memq type org-element-greater-elements
)
3199 (org-element-normalize-string
3200 (org-element-interpret-data
3201 blob
(cons type genealogy
) nil
)))
3203 ((eq type
'paragraph
)
3205 (org-element-normalize-contents
3207 ;; When normalizing contents of an item,
3208 ;; ignore first line's indentation.
3210 (memq (car genealogy
)
3211 '(footnote-definiton item
))))))
3212 (org-element-interpret-data
3213 paragraph
(cons type genealogy
) nil
)))))
3214 (results (funcall interpreter blob contents
)))
3216 (setq previous type
)
3217 ;; Build white spaces.
3219 ((eq type
'org-data
) results
)
3220 ((memq type org-element-all-elements
)
3222 (org-element-interpret--affiliated-keywords blob
)
3223 (org-element-normalize-string results
)
3224 (make-string (org-element-get-property :post-blank blob
) 10)))
3228 (org-element-get-property :post-blank blob
) 32))))))))
3229 (org-element-get-contents data
) ""))
3231 (defun org-element-interpret-secondary (secondary)
3232 "Interpret SECONDARY string as Org syntax.
3234 SECONDARY-STRING is a nested list as returned by
3235 `org-element-parse-secondary-string'.
3237 Return interpreted string."
3238 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3239 (let ((s (if (listp secondary
) secondary
(list secondary
))))
3240 (org-element-interpret-data `(org-data nil
,@s
) nil nil
)))
3242 ;; Both functions internally use `org-element--affiliated-keywords'.
3244 (defun org-element-interpret--affiliated-keywords (element)
3245 "Return ELEMENT's affiliated keywords as Org syntax.
3246 If there is no affiliated keyword, return the empty string."
3247 (let ((keyword-to-org
3251 (when (member key org-element-dual-keywords
)
3252 (setq dual
(cdr value
) value
(car value
)))
3253 (concat "#+" key
(and dual
(format "[%s]" dual
)) ": "
3254 (if (member key org-element-parsed-keywords
)
3255 (org-element-interpret-secondary value
)
3260 (let ((value (org-element-get-property (intern (concat ":" key
)) element
)))
3262 (if (member key org-element-multiple-keywords
)
3263 (mapconcat (lambda (line)
3264 (funcall keyword-to-org key line
))
3266 (funcall keyword-to-org key value
)))))
3267 ;; Remove translated keywords.
3271 (and (not (assoc key org-element-keyword-translation-alist
)) key
))
3272 org-element-affiliated-keywords
))
3275 ;; Because interpretation of the parse tree must return the same
3276 ;; number of blank lines between elements and the same number of white
3277 ;; space after objects, some special care must be given to white
3280 ;; The first function, `org-element-normalize-string', ensures any
3281 ;; string different from the empty string will end with a single
3282 ;; newline character.
3284 ;; The second function, `org-element-normalize-contents', removes
3285 ;; global indentation from the contents of the current element.
3286 (defun org-element-normalize-string (s)
3287 "Ensure string S ends with a single newline character.
3289 If S isn't a string return it unchanged. If S is the empty
3290 string, return it. Otherwise, return a new string with a single
3291 newline character at its end."
3293 ((not (stringp s
)) s
)
3295 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3296 (replace-match "\n" nil nil s
)))))
3298 (defun org-element-normalize-contents (element &optional ignore-first
)
3299 "Normalize plain text in ELEMENT's contents.
3301 ELEMENT must only contain plain text and objects.
3303 The following changes are applied to plain text:
3304 - Remove global indentation, preserving relative one.
3307 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3308 indentation to compute maximal common indentation.
3310 Return the normalized element."
3312 (list (car element
) (nth 1 element
))
3313 (let ((contents (org-element-get-contents element
)))
3315 ((and (not ignore-first
) (not (stringp (car contents
)))) contents
)
3318 ;; 1. Remove tabs from each string in CONTENTS. Get maximal
3319 ;; common indentation (MCI) along the way.
3320 (let* ((ind-list (unless ignore-first
3321 (list (org-get-string-indentation (car contents
)))))
3323 (mapcar (lambda (object)
3324 (if (not (stringp object
))
3327 (object (org-remove-tabs object
)))
3328 (while (string-match "\n\\( *\\)" object start
)
3329 (setq start
(match-end 0))
3330 (push (length (match-string 1 object
))
3335 (apply 'min ind-list
)
3336 (throw 'exit contents
))))
3337 ;; 2. Remove that indentation from CONTENTS. First string
3338 ;; must be treated differently because it's the only one
3339 ;; whose indentation doesn't happen after a newline
3341 (let ((first-obj (car contents
)))
3342 (unless (or (not (stringp first-obj
)) ignore-first
)
3344 (cons (replace-regexp-in-string
3345 (format "\\` \\{%d\\}" mci
) "" first-obj
)
3347 (mapcar (lambda (object)
3348 (if (not (stringp object
))
3350 (replace-regexp-in-string
3351 (format "\n \\{%d\\}" mci
) "\n" object
)))
3358 ;; Once the structure of an Org file is well understood, it's easy to
3359 ;; implement some replacements for `forward-paragraph'
3360 ;; `backward-paragraph', namely `org-element-forward' and
3361 ;; `org-element-backward'.
3363 ;; Also, `org-transpose-elements' mimics the behaviour of
3364 ;; `transpose-words', at the element's level, whereas
3365 ;; `org-element-drag-forward', `org-element-drag-backward', and
3366 ;; `org-element-up' generalize, respectively, functions
3367 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3369 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3370 ;; smartly remove global indentation from buffer, making it possible
3371 ;; to use Org indent mode on a file created with hard indentation.
3373 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3374 ;; internally by some of the previously cited tools.
3375 (defsubst org-element-nested-p
(elem-A elem-B
)
3376 "Non-nil when elements ELEM-A and ELEM-B are nested."
3377 (let ((beg-A (org-element-get-property :begin elem-A
))
3378 (beg-B (org-element-get-property :begin elem-B
))
3379 (end-A (org-element-get-property :end elem-A
))
3380 (end-B (org-element-get-property :end elem-B
)))
3381 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
3382 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
3384 (defun org-element-swap-A-B (elem-A elem-B
)
3385 "Swap elements ELEM-A and ELEM-B.
3387 Leave point at the end of ELEM-A.
3389 Assume ELEM-A is before ELEM-B and that they are not nested."
3390 (goto-char (org-element-get-property :begin elem-A
))
3391 (let* ((beg-B (org-element-get-property :begin elem-B
))
3392 (end-B-no-blank (save-excursion
3393 (goto-char (org-element-get-property :end elem-B
))
3394 (skip-chars-backward " \r\t\n")
3397 (beg-A (org-element-get-property :begin elem-A
))
3398 (end-A-no-blank (save-excursion
3399 (goto-char (org-element-get-property :end elem-A
))
3400 (skip-chars-backward " \r\t\n")
3403 (body-A (buffer-substring beg-A end-A-no-blank
))
3404 (body-B (buffer-substring beg-B end-B-no-blank
))
3405 (between-A-B (buffer-substring end-A-no-blank beg-B
)))
3406 (delete-region beg-A end-B-no-blank
)
3407 (insert body-B between-A-B body-A
)
3408 (goto-char (org-element-get-property :end elem-B
))))
3410 (defun org-element-backward ()
3411 "Move backward by one element."
3413 (let* ((opoint (point))
3414 (element (org-element-at-point))
3415 (start-el-beg (org-element-get-property :begin element
)))
3416 ;; At an headline. The previous element is the previous sibling,
3417 ;; or the parent if any.
3419 ;; Already at the beginning of the current element: move to the
3420 ;; beginning of the previous one.
3421 ((= opoint start-el-beg
)
3423 (skip-chars-backward " \r\t\n")
3424 (let* ((prev-element (org-element-at-point))
3425 (itemp (org-in-item-p))
3427 (save-excursion (goto-char itemp
)
3428 (org-list-struct)))))
3429 ;; When moving into a new list, go directly at the
3430 ;; beginning of the top list structure.
3431 (if (and itemp
(<= (org-list-get-bottom-point struct
) opoint
))
3433 (goto-char (org-list-get-top-point struct
))
3434 (goto-char (org-element-get-property
3435 :begin
(org-element-at-point))))
3436 (goto-char (org-element-get-property :begin prev-element
))))
3437 (while (org-truely-invisible-p) (org-element-up)))
3438 ;; Else, move at the element beginning. One exception: if point
3439 ;; was in the blank lines after the end of a list, move directly
3443 (if (and (setq itemp
(org-in-item-p))
3444 (<= (org-list-get-bottom-point
3445 (save-excursion (goto-char itemp
)
3446 (setq struct
(org-list-struct))))
3448 (progn (goto-char (org-list-get-top-point struct
))
3449 (goto-char (org-element-get-property
3450 :begin
(org-element-at-point))))
3451 (goto-char start-el-beg
)))))))
3453 (defun org-element-drag-backward ()
3454 "Drag backward element at point."
3456 (let* ((pos (point))
3457 (elem (org-element-at-point)))
3458 (when (= (progn (goto-char (point-min))
3459 (org-skip-whitespace)
3461 (org-element-get-property :end elem
))
3462 (error "Cannot drag element backward"))
3463 (goto-char (org-element-get-property :begin elem
))
3464 (org-element-backward)
3465 (let ((prev-elem (org-element-at-point)))
3466 (when (or (org-element-nested-p elem prev-elem
)
3467 (and (eq (car elem
) 'headline
)
3468 (not (eq (car prev-elem
) 'headline
))))
3470 (error "Cannot drag element backward"))
3471 ;; Compute new position of point: it's shifted by PREV-ELEM
3473 (let ((size-prev (- (org-element-get-property :end prev-elem
)
3474 (org-element-get-property :begin prev-elem
))))
3475 (org-element-swap-A-B prev-elem elem
)
3476 (goto-char (- pos size-prev
))))))
3478 (defun org-element-drag-forward ()
3479 "Move forward element at point."
3481 (let* ((pos (point))
3482 (elem (org-element-at-point)))
3483 (when (= (point-max) (org-element-get-property :end elem
))
3484 (error "Cannot drag element forward"))
3485 (goto-char (org-element-get-property :end elem
))
3486 (let ((next-elem (org-element-at-point)))
3487 (when (or (org-element-nested-p elem next-elem
)
3488 (and (eq (car next-elem
) 'headline
)
3489 (not (eq (car elem
) 'headline
))))
3491 (error "Cannot drag element forward"))
3492 ;; Compute new position of point: it's shifted by NEXT-ELEM
3493 ;; body's length (without final blanks) and by the length of
3494 ;; blanks between ELEM and NEXT-ELEM.
3495 (let ((size-next (- (save-excursion
3496 (goto-char (org-element-get-property :end next-elem
))
3497 (skip-chars-backward " \r\t\n")
3500 (org-element-get-property :begin next-elem
)))
3501 (size-blank (- (org-element-get-property :end elem
)
3503 (goto-char (org-element-get-property :end elem
))
3504 (skip-chars-backward " \r\t\n")
3507 (org-element-swap-A-B elem next-elem
)
3508 (goto-char (+ pos size-next size-blank
))))))
3510 (defun org-element-forward ()
3511 "Move forward by one element."
3514 (cond ((eobp) (error "Cannot move further down"))
3515 ((looking-at "[ \t]*$")
3516 (org-skip-whitespace)
3517 (goto-char (if (eobp) (point) (point-at-bol))))
3519 (let ((element (org-element-at-point t
))
3522 ;; At an item: Either move to the next element inside, or
3523 ;; to its end if it's hidden.
3524 ((eq (car element
) 'item
)
3525 (if (org-element-get-property :hiddenp element
)
3526 (goto-char (org-element-get-property :end element
))
3528 (re-search-forward org-element-paragraph-separate nil t
)
3529 (org-skip-whitespace)
3530 (beginning-of-line)))
3531 ;; At a recursive element: Either move inside, or if it's
3532 ;; hidden, move to its end.
3533 ((memq (car element
) org-element-greater-elements
)
3534 (let ((cbeg (org-element-get-property :contents-begin element
)))
3536 (if (or (org-element-get-property :hiddenp element
)
3538 (org-element-get-property :end element
)
3540 ;; Else: move to the current element's end.
3541 (t (goto-char (org-element-get-property :end element
))))))))
3543 (defun org-element-mark-element ()
3544 "Put point at beginning of this element, mark at end.
3546 Interactively, if this command is repeated or (in Transient Mark
3547 mode) if the mark is active, it marks the next element after the
3548 ones already marked."
3550 (let (deactivate-mark)
3551 (if (or (and (eq last-command this-command
) (mark t
))
3552 (and transient-mark-mode mark-active
))
3556 (goto-char (org-element-get-property :end
(org-element-at-point)))))
3557 (let ((element (org-element-at-point)))
3559 (push-mark (org-element-get-property :end element
) t t
)
3560 (goto-char (org-element-get-property :begin element
))))))
3562 (defun org-narrow-to-element ()
3563 "Narrow buffer to current element."
3565 (let ((elem (org-element-at-point)))
3567 ((eq (car elem
) 'headline
)
3569 (org-element-get-property :begin elem
)
3570 (org-element-get-property :end elem
)))
3571 ((memq (car elem
) org-element-greater-elements
)
3573 (org-element-get-property :contents-begin elem
)
3574 (org-element-get-property :contents-end elem
)))
3577 (org-element-get-property :begin elem
)
3578 (org-element-get-property :end elem
))))))
3580 (defun org-transpose-elements ()
3581 "Transpose current and previous elements, keeping blank lines between.
3582 Point is moved after both elements."
3584 (org-skip-whitespace)
3586 (cur (org-element-at-point)))
3587 (when (= (save-excursion (goto-char (point-min))
3588 (org-skip-whitespace)
3590 (org-element-get-property :begin cur
))
3591 (error "No previous element"))
3592 (goto-char (org-element-get-property :begin cur
))
3594 (let ((prev (org-element-at-point)))
3595 (when (org-element-nested-p cur prev
)
3597 (error "Cannot transpose nested elements"))
3598 (org-element-swap-A-B prev cur
))))
3600 (defun org-element-unindent-buffer ()
3601 "Un-indent the visible part of the buffer.
3602 Relative indentation \(between items, inside blocks, etc.\) isn't
3605 (unless (eq major-mode
'org-mode
)
3606 (error "Cannot un-indent a buffer not in Org mode"))
3607 (let* ((parse-tree (org-element-parse-buffer 'greater-element
))
3608 unindent-tree
; For byte-compiler.
3612 (mapc (lambda (element)
3613 (if (eq (car element
) 'headline
)
3614 (funcall unindent-tree
3615 (org-element-get-contents element
))
3619 (org-element-get-property :begin element
)
3620 (org-element-get-property :end element
))
3621 (org-do-remove-indentation)))))
3622 (reverse contents
))))))
3623 (funcall unindent-tree
(org-element-get-contents parse-tree
))))
3625 (defun org-element-up ()
3626 "Move to upper element.
3627 Return position at the beginning of the upper element."
3629 (let ((opoint (point)) elem
)
3631 ((bobp) (error "No surrounding element"))
3632 ((org-with-limited-levels (org-at-heading-p))
3633 (or (org-up-heading-safe) (error "No surronding element")))
3634 ((and (org-at-item-p)
3635 (setq elem
(org-element-at-point))
3636 (let* ((top-list-p (zerop (org-element-get-property :level elem
))))
3638 ;; If parent is bound to be in the same list as the
3639 ;; original point, move to that parent.
3640 (let ((struct (org-element-get-property :structure elem
)))
3642 (org-list-get-parent
3643 (point-at-bol) struct
(org-list-parents-alist struct
))))))))
3645 (let* ((elem (or elem
(org-element-at-point)))
3646 (end (save-excursion
3647 (goto-char (org-element-get-property :end elem
))
3648 (skip-chars-backward " \r\t\n")
3652 (goto-char (org-element-get-property :begin elem
))
3654 (while (and (< (org-element-get-property
3655 :end
(setq prev-elem
(org-element-at-point)))
3658 (goto-char (org-element-get-property :begin prev-elem
))
3660 (if (and (bobp) (< (org-element-get-property :end prev-elem
) end
))
3661 (progn (goto-char opoint
)
3662 (error "No surrounding element"))
3663 (goto-char (org-element-get-property :begin prev-elem
))))))))
3666 (provide 'org-element
)
3667 ;;; org-element.el ends here