1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; This file is not part of GNU Emacs.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; An object can be defined anywhere on a line. It may span over more
29 ;; than a line but never contains a blank one. Objects belong to the
30 ;; following types: `emphasis', `entity', `export-snippet',
31 ;; `footnote-reference', `inline-babel-call', `inline-src-block',
32 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
33 ;; `statistics-cookie', `subscript', `superscript', `target',
34 ;; `time-stamp' and `verbatim'.
36 ;; An element always starts and ends at the beginning of a line. The
37 ;; only element's type containing objects is called a `paragraph'.
38 ;; Other types are: `comment', `comment-block', `example-block',
39 ;; `export-block', `fixed-width', `horizontal-rule', `keyword',
40 ;; `latex-environment', `babel-call', `property-drawer',
41 ;; `quote-section', `src-block', `table' and `verse-block'.
43 ;; Elements containing paragraphs are called greater elements.
44 ;; Concerned types are: `center-block', `drawer', `dynamic-block',
45 ;; `footnote-definition', `headline', `inlinetask', `item',
46 ;; `plain-list', `quote-block', `section' and `special-block'.
48 ;; Greater elements (excepted `headline', `item' and `section' types)
49 ;; and elements (excepted `keyword', `babel-call', and
50 ;; `property-drawer' types) can have a fixed set of keywords as
51 ;; attributes. Those are called "affiliated keywords", to distinguish
52 ;; them from others keywords, which are full-fledged elements. In
53 ;; particular, the "name" affiliated keyword allows to label almost
54 ;; any element in an Org buffer.
56 ;; Notwithstanding affiliated keywords, each greater element, element
57 ;; and object has a fixed set of properties attached to it. Among
58 ;; them, three are shared by all types: `:begin' and `:end', which
59 ;; refer to the beginning and ending buffer positions of the
60 ;; considered element or object, and `:post-blank', which holds the
61 ;; number of blank lines, or white spaces, at its end.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (i.e. an item tag, an headline name, a table
65 ;; cell). Such values are called "secondary strings".
67 ;; Lisp-wise, an element or an object can be represented as a list.
68 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
69 ;; TYPE is a symbol describing the Org element or object.
70 ;; PROPERTIES is the property list attached to it. See docstring of
71 ;; appropriate parsing function to get an exhaustive
73 ;; CONTENTS is a list of elements, objects or raw strings contained
74 ;; in the current element or object, when applicable.
76 ;; An Org buffer is a nested list of such elements and objects, whose
77 ;; type is `org-data' and properties is nil.
79 ;; The first part of this file implements a parser and an interpreter
80 ;; for each type of Org syntax.
82 ;; The next two parts introduce 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'.
138 (defun org-element-center-block-parser ()
139 "Parse a center block.
141 Return a list whose car is `center-block' and cdr is a plist
142 containing `:begin', `:end', `:hiddenp', `:contents-begin',
143 `:contents-end' and `:post-blank' keywords.
145 Assume point is at beginning or end of the block."
147 (let* ((case-fold-search t
)
151 (concat "^[ \t]*#\\+begin_center") nil t
)
152 (org-element-collect-affiliated-keywords)))
153 (begin (car keywords
))
154 (contents-begin (progn (forward-line) (point)))
155 (hidden (org-truely-invisible-p))
156 (contents-end (progn (re-search-forward
157 (concat "^[ \t]*#\\+end_center") nil t
)
159 (pos-before-blank (progn (forward-line) (point)))
160 (end (progn (org-skip-whitespace)
161 (if (eobp) (point) (point-at-bol)))))
166 :contents-begin
,contents-begin
167 :contents-end
,contents-end
168 :post-blank
,(count-lines pos-before-blank end
)
169 ,@(cadr keywords
))))))
171 (defun org-element-center-block-interpreter (center-block contents
)
172 "Interpret CENTER-BLOCK element as Org syntax.
173 CONTENTS is the contents of the element."
174 (format "#+begin_center\n%s#+end_center" contents
))
179 (defun org-element-drawer-parser ()
182 Return a list whose car is `drawer' and cdr is a plist containing
183 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
184 `:contents-end' and `:post-blank' keywords.
186 Assume point is at beginning of drawer."
188 (let* ((case-fold-search t
)
189 (name (progn (looking-at org-drawer-regexp
)
190 (org-match-string-no-properties 1)))
191 (keywords (org-element-collect-affiliated-keywords))
192 (begin (car keywords
))
193 (contents-begin (progn (forward-line) (point)))
194 (hidden (org-truely-invisible-p))
195 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
197 (pos-before-blank (progn (forward-line) (point)))
198 (end (progn (org-skip-whitespace)
199 (if (eobp) (point) (point-at-bol)))))
205 :contents-begin
,contents-begin
206 :contents-end
,contents-end
207 :post-blank
,(count-lines pos-before-blank end
)
208 ,@(cadr keywords
))))))
210 (defun org-element-drawer-interpreter (drawer contents
)
211 "Interpret DRAWER element as Org syntax.
212 CONTENTS is the contents of the element."
213 (format ":%s:\n%s:END:"
214 (org-element-get-property :drawer-name drawer
)
220 (defun org-element-dynamic-block-parser ()
221 "Parse a dynamic block.
223 Return a list whose car is `dynamic-block' and cdr is a plist
224 containing `:block-name', `:begin', `:end', `:hiddenp',
225 `:contents-begin', `:contents-end', `:arguments' and
226 `:post-blank' keywords.
228 Assume point is at beginning of dynamic block."
230 (let* ((case-fold-search t
)
231 (name (progn (looking-at org-dblock-start-re
)
232 (org-match-string-no-properties 1)))
233 (arguments (org-match-string-no-properties 3))
234 (keywords (org-element-collect-affiliated-keywords))
235 (begin (car keywords
))
236 (contents-begin (progn (forward-line) (point)))
237 (hidden (org-truely-invisible-p))
238 (contents-end (progn (re-search-forward org-dblock-end-re nil t
)
240 (pos-before-blank (progn (forward-line) (point)))
241 (end (progn (org-skip-whitespace)
242 (if (eobp) (point) (point-at-bol)))))
247 :arguments
,arguments
249 :contents-begin
,contents-begin
250 :contents-end
,contents-end
251 :post-blank
,(count-lines pos-before-blank end
)
252 ,@(cadr keywords
))))))
254 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
255 "Interpret DYNAMIC-BLOCK element as Org syntax.
256 CONTENTS is the contents of the element."
257 (format "#+BEGIN: %s%s\n%s#+END:"
258 (org-element-get-property :block-name dynamic-block
)
259 (let ((args (org-element-get-property :arguments dynamic-block
)))
260 (and arg
(concat " " args
)))
264 ;;;; Footnote Definition
266 (defun org-element-footnote-definition-parser ()
267 "Parse a footnote definition.
269 Return a list whose car is `footnote-definition' and cdr is
270 a plist containing `:label', `:begin' `:end', `:contents-begin',
271 `:contents-end' and `:post-blank' keywords."
273 (let* ((f-def (org-footnote-at-definition-p))
275 (keywords (progn (goto-char (nth 1 f-def
))
276 (org-element-collect-affiliated-keywords)))
277 (begin (car keywords
))
278 (contents-begin (progn (looking-at (concat "\\[" label
"\\]"))
279 (goto-char (match-end 0))
280 (org-skip-whitespace)
282 (end (goto-char (nth 2 f-def
)))
283 (contents-end (progn (skip-chars-backward " \r\t\n")
286 `(footnote-definition
290 :contents-begin
,contents-begin
291 :contents-end
,contents-end
292 :post-blank
,(count-lines contents-end end
)
293 ,@(cadr keywords
))))))
295 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
296 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
297 CONTENTS is the contents of the footnote-definition."
298 (concat (format "[%s]" (org-element-get-property :label footnote-definition
))
305 (defun org-element-headline-parser ()
308 Return a list whose car is `headline' and cdr is a plist
309 containing `:raw-value', `:title', `:begin', `:end',
310 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
311 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
312 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
313 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
316 The plist also contains any property set in the property drawer,
317 with its name in lowercase, the underscores replaced with hyphens
318 and colons at the beginning (i.e. `:custom-id').
320 Assume point is at beginning of the headline."
322 (let* ((components (org-heading-components))
323 (level (nth 1 components
))
324 (todo (nth 2 components
))
326 (if (member todo org-done-keywords
) 'done
'todo
)))
327 (tags (nth 5 components
))
328 (raw-value (nth 4 components
))
329 (quotedp (string-match (format "^%s +" org-quote-string
) raw-value
))
330 (commentedp (string-match
331 (format "^%s +" org-comment-string
) raw-value
))
333 (string-match (format ":%s:" org-archive-tag
) tags
)))
334 (footnote-section-p (and org-footnote-section
335 (string= org-footnote-section raw-value
)))
336 (standard-props (let (plist)
339 (let ((p-name (downcase (car p
))))
340 (while (string-match "_" p-name
)
342 (replace-match "-" nil nil p-name
)))
343 (setq p-name
(intern (concat ":" p-name
)))
345 (plist-put plist p-name
(cdr p
)))))
346 (org-entry-properties nil
'standard
))
348 (time-props (org-entry-properties nil
'special
"CLOCK"))
349 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
350 (deadline (cdr (assoc "DEADLINE" time-props
)))
351 (clock (cdr (assoc "CLOCK" time-props
)))
352 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
354 (pos-after-head (save-excursion (forward-line) (point)))
355 (contents-begin (save-excursion (forward-line)
356 (org-skip-whitespace)
357 (if (eobp) (point) (point-at-bol))))
358 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
359 (end (progn (goto-char (org-end-of-subtree t t
))))
360 (contents-end (progn (skip-chars-backward " \r\t\n")
364 ;; Clean RAW-VALUE from any quote or comment string.
365 (when (or quotedp commentedp
)
367 (replace-regexp-in-string
368 (concat "\\(" org-quote-string
"\\|" org-comment-string
"\\) +")
371 ;; Clean TAGS from archive tag, if any.
374 (and (not (string= tags
(format ":%s:" org-archive-tag
)))
375 (replace-regexp-in-string
376 (concat org-archive-tag
":") "" tags
)))
377 (when (string= tags
":") (setq tags nil
)))
379 (setq title
(org-element-parse-secondary-string
381 (cdr (assq 'headline org-element-string-restrictions
))))
383 (:raw-value
,raw-value
387 :pre-blank
,(count-lines pos-after-head contents-begin
)
389 :contents-begin
,contents-begin
390 :contents-end
,contents-end
392 :priority
,(nth 3 components
)
395 :todo-type
,todo-type
396 :scheduled
,scheduled
398 :timestamp
,timestamp
400 :post-blank
,(count-lines contents-end end
)
401 :footnote-section-p
,footnote-section-p
402 :archivedp
,archivedp
403 :commentedp
,commentedp
405 ,@standard-props
)))))
407 (defun org-element-headline-interpreter (headline contents
)
408 "Interpret HEADLINE element as Org syntax.
409 CONTENTS is the contents of the element."
410 (let* ((level (org-element-get-property :level headline
))
411 (todo (org-element-get-property :todo-keyword headline
))
412 (priority (org-element-get-property :priority headline
))
413 (title (org-element-get-property :raw-value headline
))
414 (tags (let ((tag-string (org-element-get-property :tags headline
))
415 (archivedp (org-element-get-property :archivedp headline
)))
417 ((and (not tag-string
) archivedp
)
418 (format ":%s:" org-archive-tag
))
419 (archivedp (concat ":" org-archive-tag tag-string
))
421 (commentedp (org-element-get-property :commentedp headline
))
422 (quotedp (org-element-get-property :quotedp headline
))
423 (pre-blank (org-element-get-property :pre-blank headline
))
424 (heading (concat (make-string level ?
*)
425 (and todo
(concat " " todo
))
426 (and quotedp
(concat " " org-quote-string
))
427 (and commentedp
(concat " " org-comment-string
))
428 (and priority
(concat " " priority
))
429 (cond ((and org-footnote-section
430 (org-element-get-property
431 :footnote-section-p headline
))
432 (concat " " org-footnote-section
))
433 (title (concat " " title
)))))
436 (let ((tags-len (length tags
)))
439 ((zerop org-tags-column
) (1+ tags-len
))
440 ((< org-tags-column
0)
441 (max (- (+ org-tags-column
(length heading
)))
443 (t (max (+ (- org-tags-column
(length heading
))
445 (1+ tags-len
)))))))))
446 (concat heading
(and tags
(format tags-fmt tags
))
447 (make-string (1+ pre-blank
) 10)
453 (defun org-element-inlinetask-parser ()
454 "Parse an inline task.
456 Return a list whose car is `inlinetask' and cdr is a plist
457 containing `:raw-value', `:title', `:begin', `:end', `:hiddenp',
458 `:contents-begin' and `:contents-end', `:level', `:priority',
459 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
460 `:scheduled', `:deadline', `:timestamp', `:clock' and
461 `:post-blank' keywords.
463 The plist also contains any property set in the property drawer,
464 with its name in lowercase, the underscores replaced with hyphens
465 and colons at the beginning (i.e. `:custom-id').
467 Assume point is at beginning of the inline task."
469 (let* ((keywords (org-element-collect-affiliated-keywords))
470 (begin (car keywords
))
471 (components (org-heading-components))
472 (todo (nth 2 components
))
474 (if (member todo org-done-keywords
) 'done
'todo
)))
475 (raw-value (nth 4 components
))
476 (standard-props (let (plist)
479 (let ((p-name (downcase (car p
))))
480 (while (string-match "_" p-name
)
482 (replace-match "-" nil nil p-name
)))
483 (setq p-name
(intern (concat ":" p-name
)))
485 (plist-put plist p-name
(cdr p
)))))
486 (org-entry-properties nil
'standard
))
488 (time-props (org-entry-properties nil
'special
"CLOCK"))
489 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
490 (deadline (cdr (assoc "DEADLINE" time-props
)))
491 (clock (cdr (assoc "CLOCK" time-props
)))
492 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
493 (title (org-element-parse-secondary-string
495 (cdr (assq 'inlinetask org-element-string-restrictions
))))
496 (contents-begin (save-excursion (forward-line) (point)))
497 (hidden (org-truely-invisible-p))
498 (pos-before-blank (org-inlinetask-goto-end))
499 ;; In the case of a single line task, CONTENTS-BEGIN and
500 ;; CONTENTS-END might overlap.
501 (contents-end (max contents-begin
502 (save-excursion (forward-line -
1) (point))))
503 (end (progn (org-skip-whitespace)
504 (if (eobp) (point) (point-at-bol)))))
506 (:raw-value
,raw-value
510 :hiddenp
,(and (> contents-end contents-begin
) hidden
)
511 :contents-begin
,contents-begin
512 :contents-end
,contents-end
513 :level
,(nth 1 components
)
514 :priority
,(nth 3 components
)
515 :tags
,(nth 5 components
)
517 :todo-type
,todo-type
518 :scheduled
,scheduled
520 :timestamp
,timestamp
522 :post-blank
,(count-lines pos-before-blank end
)
524 ,@(cadr keywords
))))))
526 (defun org-element-inlinetask-interpreter (inlinetask contents
)
527 "Interpret INLINETASK element as Org syntax.
528 CONTENTS is the contents of inlinetask."
529 (let* ((level (org-element-get-property :level inlinetask
))
530 (todo (org-element-get-property :todo-keyword inlinetask
))
531 (priority (org-element-get-property :priority inlinetask
))
532 (title (org-element-get-property :raw-value inlinetask
))
533 (tags (org-element-get-property :tags inlinetask
))
534 (task (concat (make-string level ?
*)
535 (and todo
(concat " " todo
))
536 (and priority
(concat " " priority
))
537 (and title
(concat " " title
))))
542 ((zerop org-tags-column
) 1)
543 ((< 0 org-tags-column
)
544 (max (+ org-tags-column
548 (t (max (- org-tags-column
(length inlinetask
))
550 (concat inlinetask
(and tags
(format tags-fmt tags
) "\n" contents
))))
555 (defun org-element-item-parser (struct)
558 STRUCT is the structure of the plain list.
560 Return a list whose car is `item' and cdr is a plist containing
561 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
562 `:checkbox', `:counter', `:tag', `:raw-tag', `:structure',
563 `:hiddenp' and `:post-blank' keywords.
565 Assume point is at the beginning of the item."
568 (let* ((begin (point))
569 (bullet (org-list-get-bullet (point) struct
))
570 (checkbox (let ((box (org-list-get-checkbox begin struct
)))
571 (cond ((equal "[ ]" box
) 'off
)
572 ((equal "[X]" box
) 'on
)
573 ((equal "[-]" box
) 'trans
))))
574 (counter (let ((c (org-list-get-counter begin struct
)))
577 ((string-match "[A-Za-z]" c
)
578 (- (string-to-char (upcase (match-string 0 c
)))
580 ((string-match "[0-9]+" c
)
581 (string-to-number (match-string 0 c
))))))
582 (raw-tag (org-list-get-tag begin struct
))
584 (org-element-parse-secondary-string
586 (cdr (assq 'item org-element-string-restrictions
)))))
587 (end (org-list-get-item-end begin struct
))
588 (contents-begin (progn (looking-at org-list-full-item-re
)
589 (goto-char (match-end 0))
590 (org-skip-whitespace)
594 (hidden (progn (forward-line)
595 (and (not (= (point) end
))
596 (org-truely-invisible-p))))
597 (contents-end (progn (goto-char end
)
598 (skip-chars-backward " \r\t\n")
605 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
606 ;; up in the case of an empty item separated
607 ;; from the next by a blank line. Thus, ensure
608 ;; the former is always the smallest of two.
609 :contents-begin
,(min contents-begin contents-end
)
610 :contents-end
,(max contents-begin contents-end
)
617 :post-blank
,(count-lines contents-end end
))))))
619 (defun org-element-item-interpreter (item contents
)
620 "Interpret ITEM element as Org syntax.
621 CONTENTS is the contents of the element."
622 (let* ((bullet (org-element-get-property :bullet item
))
623 (checkbox (org-element-get-property :checkbox item
))
624 (counter (org-element-get-property :counter item
))
625 (tag (org-element-get-property :raw-tag item
))
626 ;; Compute indentation.
627 (ind (make-string (length bullet
) 32)))
631 (when (and org-list-two-spaces-after-bullet-regexp
632 (string-match org-list-two-spaces-after-bullet-regexp bullet
))
634 (and counter
(format "[@%d] " counter
))
636 ((eq checkbox
'on
) "[X] ")
637 ((eq checkbox
'off
) "[ ] ")
638 ((eq checkbox
'trans
) "[-] "))
639 (and tag
(format "%s :: " tag
))
641 (replace-regexp-in-string
642 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))))
647 (defun org-element-plain-list-parser (&optional structure
)
650 Return a list whose car is `plain-list' and cdr is a plist
651 containing `:type', `:begin', `:end', `:contents-begin' and
652 `:contents-end', `:level', `:structure' and `:post-blank'
655 Assume point is at one of the list items."
657 (let* ((struct (or structure
(org-list-struct)))
658 (prevs (org-list-prevs-alist struct
))
659 (parents (org-list-parents-alist struct
))
660 (type (org-list-get-list-type (point) struct prevs
))
661 (contents-begin (goto-char
662 (org-list-get-list-begin (point) struct prevs
)))
663 (keywords (org-element-collect-affiliated-keywords))
664 (begin (car keywords
))
665 (contents-end (goto-char
666 (org-list-get-list-end (point) struct prevs
)))
667 (end (save-excursion (org-skip-whitespace)
668 (if (eobp) (point) (point-at-bol))))
671 (let ((item contents-begin
))
674 (org-list-get-list-begin item struct prevs
)
677 ;; Blank lines below list belong to the top-level list only.
679 (setq end
(min (org-list-get-bottom-point struct
)
680 (progn (org-skip-whitespace)
681 (if (eobp) (point) (point-at-bol))))))
687 :contents-begin
,contents-begin
688 :contents-end
,contents-end
691 :post-blank
,(count-lines contents-end end
)
692 ,@(cadr keywords
))))))
694 (defun org-element-plain-list-interpreter (plain-list contents
)
695 "Interpret PLAIN-LIST element as Org syntax.
696 CONTENTS is the contents of the element."
702 (defun org-element-quote-block-parser ()
703 "Parse a quote block.
705 Return a list whose car is `quote-block' and cdr is a plist
706 containing `:begin', `:end', `:hiddenp', `:contents-begin',
707 `:contents-end' and `:post-blank' keywords.
709 Assume point is at beginning or end of the block."
711 (let* ((case-fold-search t
)
715 (concat "^[ \t]*#\\+begin_quote") nil t
)
716 (org-element-collect-affiliated-keywords)))
717 (begin (car keywords
))
718 (contents-begin (progn (forward-line) (point)))
719 (hidden (org-truely-invisible-p))
720 (contents-end (progn (re-search-forward
721 (concat "^[ \t]*#\\+end_quote") nil t
)
723 (pos-before-blank (progn (forward-line) (point)))
724 (end (progn (org-skip-whitespace)
725 (if (eobp) (point) (point-at-bol)))))
730 :contents-begin
,contents-begin
731 :contents-end
,contents-end
732 :post-blank
,(count-lines pos-before-blank end
)
733 ,@(cadr keywords
))))))
735 (defun org-element-quote-block-interpreter (quote-block contents
)
736 "Interpret QUOTE-BLOCK element as Org syntax.
737 CONTENTS is the contents of the element."
738 (format "#+begin_quote\n%s#+end_quote" contents
))
743 (defun org-element-section-parser ()
746 Return a list whose car is `section' and cdr is a plist
747 containing `:begin', `:end', `:contents-begin', `contents-end'
748 and `:post-blank' keywords."
750 ;; Beginning of section is the beginning of the first non-blank
751 ;; line after previous headline.
752 (let ((begin (save-excursion
753 (org-with-limited-levels (outline-previous-heading))
754 (if (not (org-at-heading-p)) (point)
755 (forward-line) (org-skip-whitespace) (point-at-bol))))
756 (end (progn (org-with-limited-levels (outline-next-heading))
758 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
764 :contents-begin
,begin
765 :contents-end
,pos-before-blank
766 :post-blank
,(count-lines pos-before-blank end
))))))
768 (defun org-element-section-interpreter (section contents
)
769 "Interpret SECTION element as Org syntax.
770 CONTENTS is the contents of the element."
776 (defun org-element-special-block-parser ()
777 "Parse a special block.
779 Return a list whose car is `special-block' and cdr is a plist
780 containing `:type', `:begin', `:end', `:hiddenp',
781 `:contents-begin', `:contents-end' and `:post-blank' keywords.
783 Assume point is at beginning or end of the block."
785 (let* ((case-fold-search t
)
786 (type (progn (looking-at
787 "[ \t]*#\\+\\(?:begin\\|end\\)_\\([-A-Za-z0-9]+\\)")
788 (org-match-string-no-properties 1)))
792 (concat "^[ \t]*#\\+begin_" type
) nil t
)
793 (org-element-collect-affiliated-keywords)))
794 (begin (car keywords
))
795 (contents-begin (progn (forward-line) (point)))
796 (hidden (org-truely-invisible-p))
797 (contents-end (progn (re-search-forward
798 (concat "^[ \t]*#\\+end_" type
) nil t
)
800 (pos-before-blank (progn (forward-line) (point)))
801 (end (progn (org-skip-whitespace)
802 (if (eobp) (point) (point-at-bol)))))
808 :contents-begin
,contents-begin
809 :contents-end
,contents-end
810 :post-blank
,(count-lines pos-before-blank end
)
811 ,@(cadr keywords
))))))
813 (defun org-element-special-block-interpreter (special-block contents
)
814 "Interpret SPECIAL-BLOCK element as Org syntax.
815 CONTENTS is the contents of the element."
816 (let ((block-type (org-element-get-property :type special-block
)))
817 (format "#+begin_%s\n%s#+end_%s" block-type contents block-type
)))
823 ;; For each element, a parser and an interpreter are also defined.
824 ;; Both follow the same naming convention used for greater elements.
826 ;; Also, as for greater elements, adding a new element type is done
827 ;; through the following steps: implement a parser and an interpreter,
828 ;; tweak `org-element-guess-type' so that it recognizes the new type
829 ;; and add that new type to `org-element-all-elements'.
831 ;; As a special case, when the newly defined type is a block type,
832 ;; `org-element-non-recursive-block-alist' has to be modified
838 (defun org-element-babel-call-parser ()
841 Return a list whose car is `babel-call' and cdr is a plist
842 containing `:begin', `:end', `:info' and `:post-blank' as
845 (let ((info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
846 (org-babel-lob-get-info)))
848 (pos-before-blank (progn (forward-line) (point)))
849 (end (progn (org-skip-whitespace)
850 (if (eobp) (point) (point-at-bol)))))
855 :post-blank
,(count-lines pos-before-blank end
))))))
857 (defun org-element-babel-call-interpreter (inline-babel-call contents
)
858 "Interpret INLINE-BABEL-CALL object as Org syntax.
860 (let* ((babel-info (org-element-get-property :info inline-babel-call
))
861 (main-source (car babel-info
))
862 (post-options (nth 1 babel-info
)))
864 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
865 ;; Remove redundant square brackets.
867 (match-string 1 main-source
) nil nil main-source
)
869 (and post-options
(format "[%s]" post-options
)))))
874 (defun org-element-comment-parser ()
877 Return a list whose car is `comment' and cdr is a plist
878 containing `:begin', `:end', `:value' and `:post-blank'
880 (let ((comment-re "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)")
881 beg-coms begin end value pos-before-blank keywords
)
883 ;; Move to the beginning of comments.
885 (while (and (not (bobp)) (looking-at comment-re
))
887 (unless (looking-at comment-re
) (forward-line 1)))
888 (setq beg-coms
(point))
889 ;; Get affiliated keywords, if any.
890 (setq keywords
(org-element-collect-affiliated-keywords))
891 ;; Store true beginning of element.
892 (setq begin
(car keywords
))
893 ;; Get ending of comments. If point is in a list, ensure to not
894 ;; get outside of it.
895 (let* ((itemp (org-in-item-p))
897 (org-list-get-bottom-point
898 (save-excursion (goto-char itemp
) (org-list-struct)))
900 (while (and (looking-at comment-re
) (< (point) max-pos
))
902 (setq pos-before-blank
(point))
903 ;; Find position after blank.
904 (org-skip-whitespace)
905 (setq end
(if (eobp) (point) (point-at-bol)))
907 (setq value
(buffer-substring-no-properties beg-coms pos-before-blank
)))
912 :post-blank
,(count-lines pos-before-blank end
)
913 ,@(cadr keywords
)))))
915 (defun org-element-comment-interpreter (comment contents
)
916 "Interpret COMMENT element as Org syntax.
918 (org-element-get-property :value comment
))
923 (defun org-element-comment-block-parser ()
924 "Parse an export block.
926 Return a list whose car is `comment-block' and cdr is a plist
927 containing `:begin', `:end', `:hiddenp', `:value' and
928 `:post-blank' keywords."
931 (let* ((case-fold-search t
)
933 (re-search-backward "^[ \t]*#\\+begin_comment" nil t
)
934 (org-element-collect-affiliated-keywords)))
935 (begin (car keywords
))
936 (contents-begin (progn (forward-line) (point)))
937 (hidden (org-truely-invisible-p))
938 (contents-end (progn (re-search-forward
939 "^[ \t]*#\\+end_comment" nil t
)
941 (pos-before-blank (progn (forward-line) (point)))
942 (end (progn (org-skip-whitespace)
943 (if (eobp) (point) (point-at-bol))))
944 (value (buffer-substring-no-properties contents-begin contents-end
)))
950 :post-blank
,(count-lines pos-before-blank end
)
951 ,@(cadr keywords
))))))
953 (defun org-element-comment-block-interpreter (comment-block contents
)
954 "Interpret COMMENT-BLOCK element as Org syntax.
956 (concat "#+begin_comment\n"
957 (org-remove-indentation
958 (org-element-get-property :value comment-block
))
964 (defun org-element-example-block-parser ()
965 "Parse an example block.
967 Return a list whose car is `example' and cdr is a plist
968 containing `:begin', `:end', `:options', `:hiddenp', `:value' and
969 `:post-blank' keywords."
972 (let* ((case-fold-search t
)
975 "^[ \t]*#\\+begin_example\\(?: +\\(.*\\)\\)?" nil t
)
976 (org-match-string-no-properties 1)))
977 (keywords (org-element-collect-affiliated-keywords))
978 (begin (car keywords
))
979 (contents-begin (progn (forward-line) (point)))
980 (hidden (org-truely-invisible-p))
982 (re-search-forward "^[ \t]*#\\+end_example" nil t
)
984 (value (buffer-substring-no-properties contents-begin contents-end
))
985 (pos-before-blank (progn (forward-line) (point)))
986 (end (progn (org-skip-whitespace)
987 (if (eobp) (point) (point-at-bol)))))
994 :post-blank
,(count-lines pos-before-blank end
)
995 ,@(cadr keywords
))))))
997 (defun org-element-example-block-interpreter (example-block contents
)
998 "Interpret EXAMPLE-BLOCK element as Org syntax.
1000 (let ((options (org-element-get-property :options example-block
)))
1001 (concat "#+begin_example" (and options
(concat " " options
)) "\n"
1002 (org-remove-indentation
1003 (org-element-get-property :value example-block
))
1009 (defun org-element-export-block-parser ()
1010 "Parse an export block.
1012 Return a list whose car is `export-block' and cdr is a plist
1013 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1014 `:post-blank' keywords."
1017 (let* ((case-fold-search t
)
1019 (type (progn (re-search-backward
1020 (concat "[ \t]*#\\+begin_"
1021 (org-re "\\([[:alnum:]]+\\)")))
1022 (downcase (org-match-string-no-properties 1))))
1023 (keywords (org-element-collect-affiliated-keywords))
1024 (begin (car keywords
))
1025 (contents-begin (progn (forward-line) (point)))
1026 (hidden (org-truely-invisible-p))
1027 (contents-end (progn (re-search-forward
1028 (concat "^[ \t]*#\\+end_" type
) nil t
)
1030 (pos-before-blank (progn (forward-line) (point)))
1031 (end (progn (org-skip-whitespace)
1032 (if (eobp) (point) (point-at-bol))))
1033 (value (buffer-substring-no-properties contents-begin contents-end
)))
1040 :post-blank
,(count-lines pos-before-blank end
)
1041 ,@(cadr keywords
))))))
1043 (defun org-element-export-block-interpreter (export-block contents
)
1044 "Interpret EXPORT-BLOCK element as Org syntax.
1046 (let ((type (org-element-get-property :type export-block
)))
1047 (concat (format "#+begin_%s\n" type
)
1048 (org-element-get-property :value export-block
)
1049 (format "#+end_%s" type
))))
1054 (defun org-element-fixed-width-parser ()
1055 "Parse a fixed-width section.
1057 Return a list whose car is `fixed-width' and cdr is a plist
1058 containing `:begin', `:end', `:value' and `:post-blank'
1060 (let ((fixed-re "[ \t]*:\\( \\|$\\)")
1061 beg-area begin end value pos-before-blank keywords
)
1063 ;; Move to the beginning of the fixed-width area.
1065 (while (and (not (bobp)) (looking-at fixed-re
))
1067 (unless (looking-at fixed-re
) (forward-line 1)))
1068 (setq beg-area
(point))
1069 ;; Get affiliated keywords, if any.
1070 (setq keywords
(org-element-collect-affiliated-keywords))
1071 ;; Store true beginning of element.
1072 (setq begin
(car keywords
))
1073 ;; Get ending of fixed-width area. If point is in a list,
1074 ;; ensure to not get outside of it.
1075 (let* ((itemp (org-in-item-p))
1077 (org-list-get-bottom-point
1078 (save-excursion (goto-char itemp
) (org-list-struct)))
1080 (while (and (looking-at fixed-re
) (< (point) max-pos
))
1082 (setq pos-before-blank
(point))
1083 ;; Find position after blank
1084 (org-skip-whitespace)
1085 (setq end
(if (eobp) (point) (point-at-bol)))
1087 (setq value
(buffer-substring-no-properties beg-area pos-before-blank
)))
1092 :post-blank
,(count-lines pos-before-blank end
)
1093 ,@(cadr keywords
)))))
1095 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1096 "Interpret FIXED-WIDTH element as Org syntax.
1098 (org-remove-indentation (org-element-get-property :value fixed-width
)))
1101 ;;;; Horizontal Rule
1103 (defun org-element-horizontal-rule-parser ()
1104 "Parse an horizontal rule.
1106 Return a list whose car is `horizontal-rule' and cdr is
1107 a plist containing `:begin', `:end' and `:post-blank'
1110 (let* ((keywords (org-element-collect-affiliated-keywords))
1111 (begin (car keywords
))
1112 (post-hr (progn (forward-line) (point)))
1113 (end (progn (org-skip-whitespace)
1114 (if (eobp) (point) (point-at-bol)))))
1118 :post-blank
,(count-lines post-hr end
)
1119 ,@(cadr keywords
))))))
1121 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1122 "Interpret HORIZONTAL-RULE element as Org syntax.
1129 (defun org-element-keyword-parser ()
1130 "Parse a keyword at point.
1132 Return a list whose car is `keyword' and cdr is a plist
1133 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1136 (let* ((begin (point))
1137 (key (progn (looking-at
1138 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1139 (org-match-string-no-properties 1)))
1140 (value (org-trim (buffer-substring-no-properties
1141 (match-end 0) (point-at-eol))))
1142 (pos-before-blank (progn (forward-line) (point)))
1143 (end (progn (org-skip-whitespace)
1144 (if (eobp) (point) (point-at-bol)))))
1150 :post-blank
,(count-lines pos-before-blank end
))))))
1152 (defun org-element-keyword-interpreter (keyword contents
)
1153 "Interpret KEYWORD element as Org syntax.
1156 (org-element-get-property :key keyword
)
1157 (org-element-get-property :value keyword
)))
1160 ;;;; Latex Environment
1162 (defun org-element-latex-environment-parser ()
1163 "Parse a LaTeX environment.
1165 Return a list whose car is `latex-environment' and cdr is a plist
1166 containing `:begin', `:end', `:value' and `:post-blank' keywords."
1169 (let* ((case-fold-search t
)
1170 (contents-begin (re-search-backward "^[ \t]*\\\\begin" nil t
))
1171 (keywords (org-element-collect-affiliated-keywords))
1172 (begin (car keywords
))
1173 (contents-end (progn (re-search-forward "^[ \t]*\\\\end")
1176 (value (buffer-substring-no-properties contents-begin contents-end
))
1177 (end (progn (org-skip-whitespace)
1178 (if (eobp) (point) (point-at-bol)))))
1183 :post-blank
,(count-lines contents-end end
)
1184 ,@(cadr keywords
))))))
1186 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1187 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1189 (org-element-get-property :value latex-environment
))
1194 (defun org-element-paragraph-parser ()
1197 Return a list whose car is `paragraph' and cdr is a plist
1198 containing `:begin', `:end', `:contents-begin' and
1199 `:contents-end' and `:post-blank' keywords.
1201 Assume point is at the beginning of the paragraph."
1203 (let* ((contents-begin (point))
1204 (keywords (org-element-collect-affiliated-keywords))
1205 (begin (car keywords
))
1206 (contents-end (progn
1208 (if (re-search-forward
1209 org-element-paragraph-separate nil
'm
)
1210 (progn (forward-line -
1) (end-of-line) (point))
1212 (pos-before-blank (progn (forward-line) (point)))
1213 (end (progn (org-skip-whitespace)
1214 (if (eobp) (point) (point-at-bol)))))
1218 :contents-begin
,contents-begin
1219 :contents-end
,contents-end
1220 :post-blank
,(count-lines pos-before-blank end
)
1221 ,@(cadr keywords
))))))
1223 (defun org-element-paragraph-interpreter (paragraph contents
)
1224 "Interpret PARAGRAPH element as Org syntax.
1225 CONTENTS is the contents of the element."
1229 ;;;; Property Drawer
1231 (defun org-element-property-drawer-parser ()
1232 "Parse a property drawer.
1234 Return a list whose car is `property-drawer' and cdr is a plist
1235 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1236 `:contents-end', `:properties' and `:post-blank' keywords."
1238 (let ((case-fold-search t
)
1239 (begin (progn (end-of-line)
1240 (re-search-backward org-property-start-re
)
1241 (match-beginning 0)))
1242 (contents-begin (progn (forward-line) (point)))
1243 (hidden (org-truely-invisible-p))
1244 (properties (let (val)
1245 (while (not (looking-at "^[ \t]*:END:"))
1248 "[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):"))
1249 (push (cons (match-string 1)
1252 (match-end 0) (point-at-eol))))
1256 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
1258 (pos-before-blank (progn (forward-line) (point)))
1259 (end (progn (org-skip-whitespace)
1260 (if (eobp) (point) (point-at-bol)))))
1265 :properties
,properties
1266 :post-blank
,(count-lines pos-before-blank end
))))))
1268 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1269 "Interpret PROPERTY-DRAWER element as Org syntax.
1271 (let ((props (org-element-get-property :properties property-drawer
)))
1274 (mapconcat (lambda (p)
1275 (format org-property-format
(format ":%s:" (car p
)) (cdr p
)))
1276 (nreverse props
) "\n")
1282 (defun org-element-quote-section-parser ()
1283 "Parse a quote section.
1285 Return a list whose car is `quote-section' and cdr is a plist
1286 containing `:begin', `:end', `:value' and `:post-blank'
1289 (let* ((begin (progn (org-back-to-heading t
)
1291 (org-skip-whitespace)
1293 (end (progn (org-with-limited-levels (outline-next-heading))
1295 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1298 (value (unless (= begin end
)
1299 (buffer-substring-no-properties begin pos-before-blank
))))
1304 :post-blank
,(if value
1305 (count-lines pos-before-blank end
)
1308 (defun org-element-quote-section-interpreter (quote-section contents
)
1309 "Interpret QUOTE-SECTION element as Org syntax.
1311 (org-element-get-property :value quote-section
))
1316 (defun org-element-src-block-parser ()
1319 Return a list whose car is `src-block' and cdr is a plist
1320 containing `:language', `:switches', `:parameters', `:begin',
1321 `:end', `:hiddenp', `:contents-begin', `:contents-end', `:value'
1322 and `:post-blank' keywords."
1325 (let* ((case-fold-search t
)
1326 ;; Get position at beginning of block.
1329 (concat "^[ \t]*#\\+begin_src"
1330 "\\(?: +\\(\\S-+\\)\\)?" ; language
1331 "\\(\\(?: +[-+][A-Za-z]\\)*\\)" ; switches
1332 "\\(.*\\)[ \t]*$") ; arguments
1334 ;; Get language as a string.
1335 (language (org-match-string-no-properties 1))
1337 (switches (org-match-string-no-properties 2))
1339 (parameters (org-trim (org-match-string-no-properties 3)))
1340 ;; Get affiliated keywords.
1341 (keywords (org-element-collect-affiliated-keywords))
1342 ;; Get beginning position.
1343 (begin (car keywords
))
1344 ;; Get position at end of block.
1345 (contents-end (progn (re-search-forward "^[ \t]*#\\+end_src" nil t
)
1349 (value (buffer-substring-no-properties
1350 (save-excursion (goto-char contents-begin
)
1353 (match-beginning 0)))
1354 ;; Get position after ending blank lines.
1355 (end (progn (org-skip-whitespace)
1356 (if (eobp) (point) (point-at-bol))))
1357 ;; Get visibility status.
1358 (hidden (progn (goto-char contents-begin
)
1360 (org-truely-invisible-p))))
1362 (:language
,language
1364 :parameters
,parameters
1369 :post-blank
,(count-lines contents-end end
)
1370 ,@(cadr keywords
))))))
1372 (defun org-element-src-block-interpreter (src-block contents
)
1373 "Interpret SRC-BLOCK element as Org syntax.
1375 (let ((lang (org-element-get-property :language src-block
))
1376 (switches (org-element-get-property :switches src-block
))
1377 (params (org-element-get-property :parameters src-block
))
1378 (value (let ((val (org-element-get-property :value src-block
)))
1380 (org-src-preserve-indentation val
)
1381 ((zerop org-edit-src-content-indentation
)
1382 (org-remove-indentation val
))
1384 (let ((ind (make-string
1385 org-edit-src-content-indentation
32)))
1386 (replace-regexp-in-string
1387 "\\(^\\)[ \t]*\\S-" ind
1388 (org-remove-indentation val
) nil nil
1)))))))
1389 (concat (format "#+begin_src%s\n"
1390 (concat (and lang
(concat " " lang
))
1391 (and switches
(concat " " switches
))
1392 (and params
(concat " " params
))))
1399 (defun org-element-table-parser ()
1400 "Parse a table at point.
1402 Return a list whose car is `table' and cdr is a plist containing
1403 `:begin', `:end', `:contents-begin', `:contents-end', `:tblfm',
1404 `:type', `:raw-table' and `:post-blank' keywords."
1406 (let* ((table-begin (goto-char (org-table-begin t
)))
1407 (type (if (org-at-table.el-p
) 'table.el
'org
))
1408 (keywords (org-element-collect-affiliated-keywords))
1409 (begin (car keywords
))
1410 (table-end (goto-char (marker-position (org-table-end t
))))
1411 (tblfm (when (looking-at "[ \t]*#\\+tblfm: +\\(.*\\)[ \t]*")
1412 (prog1 (org-match-string-no-properties 1)
1414 (pos-before-blank (point))
1415 (end (progn (org-skip-whitespace)
1416 (if (eobp) (point) (point-at-bol))))
1417 (raw-table (org-remove-indentation
1418 (buffer-substring-no-properties table-begin table-end
))))
1423 :raw-table
,raw-table
1425 :post-blank
,(count-lines pos-before-blank end
)
1426 ,@(cadr keywords
))))))
1428 (defun org-element-table-interpreter (table contents
)
1429 "Interpret TABLE element as Org syntax.
1431 (org-element-get-property :raw-table table
))
1436 (defun org-element-verse-block-parser ()
1437 "Parse a verse block.
1439 Return a list whose car is `verse-block' and cdr is a plist
1440 containing `:begin', `:end', `:hiddenp', `:raw-value', `:value'
1441 and `:post-blank' keywords.
1443 Assume point is at beginning or end of the block."
1445 (let* ((case-fold-search t
)
1449 (concat "^[ \t]*#\\+begin_verse") nil t
)
1450 (org-element-collect-affiliated-keywords)))
1451 (begin (car keywords
))
1452 (hidden (progn (forward-line) (org-truely-invisible-p)))
1453 (raw-val (buffer-substring-no-properties
1456 (re-search-forward (concat "^[ \t]*#\\+end_verse") nil t
)
1458 (pos-before-blank (progn (forward-line) (point)))
1459 (end (progn (org-skip-whitespace)
1460 (if (eobp) (point) (point-at-bol))))
1461 (value (org-element-parse-secondary-string
1462 (org-remove-indentation raw-val
)
1463 (cdr (assq 'verse org-element-string-restrictions
)))))
1470 :post-blank
,(count-lines pos-before-blank end
)
1471 ,@(cadr keywords
))))))
1473 (defun org-element-verse-block-interpreter (verse-block contents
)
1474 "Interpret VERSE-BLOCK element as Org syntax.
1476 (format "#+begin_verse\n%s#+end_verse"
1477 (org-remove-indentation
1478 (org-element-get-property :raw-value verse-block
))))
1484 ;; Unlike to elements, interstices can be found between objects.
1485 ;; That's why, along with the parser, successor functions are provided
1486 ;; for each object. Some objects share the same successor
1487 ;; (i.e. `emphasis' and `verbatim' objects).
1489 ;; A successor must accept a single argument bounding the search. It
1490 ;; will return either a cons cell whose car is the object's type, as
1491 ;; a symbol, and cdr the position of its next occurrence, or nil.
1493 ;; Successors follow the naming convention:
1494 ;; org-element-NAME-successor, where NAME is the name of the
1495 ;; successor, as defined in `org-element-all-successors'.
1497 ;; Some object types (i.e `emphasis') are recursive. Restrictions on
1498 ;; object types they can contain will be specified in
1499 ;; `org-element-object-restrictions'.
1501 ;; Adding a new type of object is simple. Implement a successor,
1502 ;; a parser, and an interpreter for it, all following the naming
1503 ;; convention. Register successor in `org-element-all-successors',
1504 ;; maybe tweak restrictions about it, and that's it.
1508 (defun org-element-emphasis-parser ()
1509 "Parse text markup object at point.
1511 Return a list whose car is `emphasis' and cdr is a plist with
1512 `:marker', `:begin', `:end', `:contents-begin' and
1513 `:contents-end' and `:post-blank' keywords.
1515 Assume point is at the first emphasis marker."
1517 (unless (bolp) (backward-char 1))
1518 (looking-at org-emph-re
)
1519 (let ((begin (match-beginning 2))
1520 (marker (org-match-string-no-properties 3))
1521 (contents-begin (match-beginning 4))
1522 (contents-end (match-end 4))
1523 (post-blank (progn (goto-char (match-end 2))
1524 (skip-chars-forward " \t")))
1530 :contents-begin
,contents-begin
1531 :contents-end
,contents-end
1532 :post-blank
,post-blank
)))))
1534 (defun org-element-emphasis-interpreter (emphasis contents
)
1535 "Interpret EMPHASIS object as Org syntax.
1536 CONTENTS is the contents of the object."
1537 (let ((marker (org-element-get-property :marker emphasis
)))
1538 (concat marker contents marker
)))
1540 (defun org-element-text-markup-successor (limit)
1541 "Search for the next emphasis or verbatim and return position.
1543 LIMIT bounds the search.
1545 Return value is a cons cell whose car is `emphasis' or
1546 `verbatim' and cdr is beginning position."
1548 (unless (bolp) (backward-char))
1549 (when (re-search-forward org-emph-re limit t
)
1550 (cons (if (nth 4 (assoc (match-string 3) org-emphasis-alist
))
1553 (match-beginning 2)))))
1557 (defun org-element-entity-parser ()
1558 "Parse entity at point.
1560 Return a list whose car is `entity' and cdr a plist with
1561 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1562 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1565 Assume point is at the beginning of the entity."
1567 (looking-at "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1568 (let* ((value (org-entity-get (match-string 1)))
1569 (begin (match-beginning 0))
1570 (bracketsp (string= (match-string 2) "{}"))
1571 (post-blank (progn (goto-char (match-end 1))
1572 (when bracketsp
(forward-char 2))
1573 (skip-chars-forward " \t")))
1577 :latex
,(nth 1 value
)
1578 :latex-math-p
,(nth 2 value
)
1579 :html
,(nth 3 value
)
1580 :ascii
,(nth 4 value
)
1581 :latin1
,(nth 5 value
)
1582 :utf-8
,(nth 6 value
)
1585 :use-brackets-p
,bracketsp
1586 :post-blank
,post-blank
)))))
1588 (defun org-element-entity-interpreter (entity contents
)
1589 "Interpret ENTITY object as Org syntax.
1592 (org-element-get-property :name entity
)
1593 (when (org-element-get-property :use-brackets-p entity
) "{}")))
1595 (defun org-element-latex-or-entity-successor (limit)
1596 "Search for the next latex-fragment or entity object.
1598 LIMIT bounds the search.
1600 Return value is a cons cell whose car is `entity' or
1601 `latex-fragment' and cdr is beginning position."
1603 (let ((matchers (plist-get org-format-latex-options
:matchers
))
1604 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1606 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"))
1607 (when (re-search-forward
1608 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
1612 (goto-char (match-beginning 0))
1613 (if (looking-at entity-re
)
1614 ;; Determine if it's a real entity or a LaTeX command.
1615 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
1616 (match-beginning 0))
1617 ;; No entity nor command: point is at a LaTeX fragment.
1618 ;; Determine its type to get the correct beginning position.
1619 (cons 'latex-fragment
1622 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
1625 (nth 2 (assoc e org-latex-regexps
))))))
1632 (defun org-element-export-snippet-parser ()
1633 "Parse export snippet at point.
1635 Return a list whose car is `export-snippet' and cdr a plist with
1636 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1639 Assume point is at the beginning of the snippet."
1641 (looking-at "@\\([-A-Za-z0-9]+\\){")
1642 (let* ((begin (point))
1643 (back-end (org-match-string-no-properties 1))
1644 (before-blank (progn (goto-char (scan-sexps (1- (match-end 0)) 1))))
1645 (value (buffer-substring-no-properties
1646 (match-end 0) (1- before-blank
)))
1647 (post-blank (skip-chars-forward " \t"))
1650 (:back-end
,back-end
1654 :post-blank
,post-blank
)))))
1656 (defun org-element-export-snippet-interpreter (export-snippet contents
)
1657 "Interpret EXPORT-SNIPPET object as Org syntax.
1660 (org-element-get-property :back-end export-snippet
)
1661 (org-element-get-property :value export-snippet
)))
1663 (defun org-element-export-snippet-successor (limit)
1664 "Search for the next export-snippet object.
1666 LIMIT bounds the search.
1668 Return value is a cons cell whose car is `export-snippet' cdr is
1669 its beginning position."
1672 (while (re-search-forward "@[-A-Za-z0-9]+{" limit t
)
1673 (when (let ((end (ignore-errors (scan-sexps (1- (point)) 1))))
1674 (and end
(eq (char-before end
) ?
})))
1675 (throw 'exit
(cons 'export-snippet
(match-beginning 0))))))))
1678 ;;;; Footnote Reference
1680 (defun org-element-footnote-reference-parser ()
1681 "Parse footnote reference at point.
1683 Return a list whose car is `footnote-reference' and cdr a plist
1684 with `:label', `:type', `:definition', `:begin', `:end' and
1685 `:post-blank' as keywords."
1687 (let* ((ref (org-footnote-at-reference-p))
1689 (raw-def (nth 3 ref
))
1690 (inline-def (and raw-def
1691 (org-element-parse-secondary-string raw-def nil
)))
1692 (type (if (nth 3 ref
) 'inline
'standard
))
1694 (post-blank (progn (goto-char (nth 2 ref
))
1695 (skip-chars-forward " \t")))
1697 `(footnote-reference
1700 :inline-definition
,inline-def
1703 :post-blank
,post-blank
1704 :raw-definition
,raw-def
)))))
1706 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
1707 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1709 (let ((label (or (org-element-get-property :label footnote-reference
)
1711 (def (let ((raw (org-element-get-property
1712 :raw-definition footnote-reference
)))
1713 (if raw
(concat ":" raw
) ""))))
1714 (format "[%s]" (concat label def
))))
1716 (defun org-element-footnote-reference-successor (limit)
1717 "Search for the next footnote-reference and return beginning
1720 LIMIT bounds the search.
1722 Return value is a cons cell whose car is `footnote-reference' and
1723 cdr is beginning position."
1725 (when (setq fn-ref
(org-footnote-get-next-reference nil nil limit
))
1726 (cons 'footnote-reference
(nth 1 fn-ref
)))))
1729 ;;;; Inline Babel Call
1731 (defun org-element-inline-babel-call-parser ()
1732 "Parse inline babel call at point.
1734 Return a list whose car is `inline-babel-call' and cdr a plist with
1735 `:begin', `:end', `:info' and `:post-blank' as keywords.
1737 Assume point is at the beginning of the babel call."
1739 (unless (bolp) (backward-char))
1740 (looking-at org-babel-inline-lob-one-liner-regexp
)
1741 (let ((info (save-match-data (org-babel-lob-get-info)))
1742 (begin (match-end 1))
1743 (post-blank (progn (goto-char (match-end 0))
1744 (skip-chars-forward " \t")))
1750 :post-blank
,post-blank
)))))
1752 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
1753 "Interpret INLINE-BABEL-CALL object as Org syntax.
1755 (let* ((babel-info (org-element-get-property :info inline-babel-call
))
1756 (main-source (car babel-info
))
1757 (post-options (nth 1 babel-info
)))
1759 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
1760 ;; Remove redundant square brackets.
1762 (match-string 1 main-source
) nil nil main-source
)
1764 (and post-options
(format "[%s]" post-options
)))))
1766 (defun org-element-inline-babel-call-successor (limit)
1767 "Search for the next inline-babel-call and return beginning
1770 LIMIT bounds the search.
1772 Return value is a cons cell whose car is `inline-babel-call' and
1773 cdr is beginning position."
1775 ;; Use a simplified version of
1776 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
1777 (when (re-search-forward
1778 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
1780 (cons 'inline-babel-call
(match-beginning 0)))))
1783 ;;;; Inline Src Block
1785 (defun org-element-inline-src-block-parser ()
1786 "Parse inline source block at point.
1788 Return a list whose car is `inline-src-block' and cdr a plist
1789 with `:begin', `:end', `:language', `:value', `:parameters' and
1790 `:post-blank' as keywords.
1792 Assume point is at the beginning of the inline src block."
1794 (unless (bolp) (backward-char))
1795 (looking-at org-babel-inline-src-block-regexp
)
1796 (let ((begin (match-beginning 1))
1797 (language (org-match-string-no-properties 2))
1798 (parameters (org-match-string-no-properties 4))
1799 (value (org-match-string-no-properties 5))
1800 (post-blank (progn (goto-char (match-end 0))
1801 (skip-chars-forward " \t")))
1804 (:language
,language
1806 :parameters
,parameters
1809 :post-blank
,post-blank
)))))
1811 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
1812 "Interpret INLINE-SRC-BLOCK object as Org syntax.
1814 (let ((language (org-element-get-property :language inline-src-block
))
1815 (arguments (org-element-get-property :parameters inline-src-block
))
1816 (body (org-element-get-property :value inline-src-block
)))
1817 (format "src_%s%s{%s}"
1819 (if arguments
(format "[%s]" arguments
) "")
1822 (defun org-element-inline-src-block-successor (limit)
1823 "Search for the next inline-babel-call and return beginning position.
1825 LIMIT bounds the search.
1827 Return value is a cons cell whose car is `inline-babel-call' and
1828 cdr is beginning position."
1830 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
1831 (cons 'inline-src-block
(match-beginning 1)))))
1836 (defun org-element-latex-fragment-parser ()
1837 "Parse latex fragment at point.
1839 Return a list whose car is `latex-fragment' and cdr a plist with
1840 `:value', `:begin', `:end', and `:post-blank' as keywords.
1842 Assume point is at the beginning of the latex fragment."
1844 (let* ((begin (point))
1848 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
1849 (when (or (looking-at latex-regexp
)
1853 (looking-at latex-regexp
))))
1854 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
1855 (plist-get org-format-latex-options
:matchers
))
1856 ;; None found: it's a macro.
1857 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
1859 (value (match-string-no-properties substring-match
))
1860 (post-blank (progn (goto-char (match-end substring-match
))
1861 (skip-chars-forward " \t")))
1867 :post-blank
,post-blank
)))))
1869 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
1870 "Interpret LATEX-FRAGMENT object as Org syntax.
1872 (org-element-get-property :value latex-fragment
))
1876 (defun org-element-line-break-parser ()
1877 "Parse line break at point.
1879 Return a list whose car is `line-break', and cdr a plist with
1880 `:begin', `:end' and `:post-blank' keywords.
1882 Assume point is at the beginning of the line break."
1883 (let ((begin (point))
1884 (end (save-excursion (forward-line) (point))))
1885 `(line-break (:begin
,begin
:end
,end
:post-blank
0))))
1887 (defun org-element-line-break-interpreter (line-break contents
)
1888 "Interpret LINE-BREAK object as Org syntax.
1892 (defun org-element-line-break-successor (limit)
1893 "Search for the next statistics cookie and return position.
1895 LIMIT bounds the search.
1897 Return value is a cons cell whose car is `line-break' and cdr is
1898 beginning position."
1900 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
1901 (goto-char (match-beginning 1)))))
1902 ;; A line break can only happen on a non-empty line.
1903 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
1904 (cons 'line-break beg
)))))
1909 (defun org-element-link-parser ()
1910 "Parse link at point.
1912 Return a list whose car is `link' and cdr a plist with `:type',
1913 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
1914 `:contents-end' and `:post-blank' as keywords.
1916 Assume point is at the beginning of the link."
1918 (let ((begin (point))
1919 end contents-begin contents-end link-end post-blank path type
1922 ;; Type 1: text targeted from a radio target.
1923 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
1925 path
(org-match-string-no-properties 0)
1926 contents-begin
(match-beginning 0)
1927 contents-end
(match-end 0)
1928 link-end
(match-end 0)))
1929 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
1930 ((looking-at org-bracket-link-regexp
)
1931 (setq contents-begin
(match-beginning 3)
1932 contents-end
(match-end 3)
1933 link-end
(match-end 0)
1934 ;; RAW-LINK is the original link.
1935 raw-link
(org-match-string-no-properties 1)
1936 link
(org-link-expand-abbrev
1937 (replace-regexp-in-string
1938 " *\n *" " " (org-link-unescape raw-link
) t t
)))
1939 ;; Determine TYPE of link and set PATH accordingly.
1942 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
1943 (setq type
"file" path link
))
1944 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
1945 ((string-match org-link-re-with-space3 link
)
1946 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
1947 ;; Id type: PATH is the id.
1948 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
1949 (setq type
"id" path
(match-string 1 link
)))
1950 ;; Code-ref type: PATH is the name of the reference.
1951 ((string-match "^(\\(.*\\))$" link
)
1952 (setq type
"coderef" path
(match-string 1 link
)))
1953 ;; Custom-id type: PATH is the name of the custom id.
1954 ((= (aref link
0) ?
#)
1955 (setq type
"custom-id" path
(substring link
1)))
1956 ;; Fuzzy type: Internal link either matches a target, an
1957 ;; headline name or nothing. PATH is the target or headline's
1959 (t (setq type
"fuzzy" path link
))))
1960 ;; Type 3: Plain link, i.e. http://orgmode.org
1961 ((looking-at org-plain-link-re
)
1962 (setq raw-link
(org-match-string-no-properties 0)
1963 type
(org-match-string-no-properties 1)
1964 path
(org-match-string-no-properties 2)
1965 link-end
(match-end 0)))
1966 ;; Type 4: Angular link, i.e. <http://orgmode.org>
1967 ((looking-at org-angle-link-re
)
1968 (setq raw-link
(buffer-substring-no-properties
1969 (match-beginning 1) (match-end 2))
1970 type
(org-match-string-no-properties 1)
1971 path
(org-match-string-no-properties 2)
1972 link-end
(match-end 0))))
1973 ;; In any case, deduce end point after trailing white space from
1974 ;; LINK-END variable.
1975 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
1980 :raw-link
,(or raw-link path
)
1983 :contents-begin
,contents-begin
1984 :contents-end
,contents-end
1985 :post-blank
,post-blank
)))))
1987 (defun org-element-link-interpreter (link contents
)
1988 "Interpret LINK object as Org syntax.
1989 CONTENTS is the contents of the object."
1990 (let ((type (org-element-get-property :type link
))
1991 (raw-link (org-element-get-property :raw-link link
)))
1993 ((string= type
"radio") raw-link
)
1994 (t (format "[[%s]%s]"
1996 (if (string= contents
"") "" (format "[%s]" contents
)))))))
1998 (defun org-element-link-successor (limit)
1999 "Search for the next link and return position.
2001 LIMIT bounds the search.
2003 Return value is a cons cell whose car is `link' and cdr is
2004 beginning position."
2007 (if org-target-link-regexp
2008 (concat org-any-link-re
"\\|" org-target-link-regexp
)
2010 (when (re-search-forward link-regexp limit t
)
2011 (cons 'link
(match-beginning 0))))))
2016 (defun org-element-macro-parser ()
2017 "Parse macro at point.
2019 Return a list whose car is `macro' and cdr a plist with `:key',
2020 `:args', `:begin', `:end', `:value' and `:post-blank' as
2023 Assume point is at the macro."
2025 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2026 (let ((begin (point))
2027 (key (downcase (org-match-string-no-properties 1)))
2028 (value (org-match-string-no-properties 0))
2029 (post-blank (progn (goto-char (match-end 0))
2030 (skip-chars-forward " \t")))
2032 (args (let ((args (org-match-string-no-properties 3)) args2
)
2034 (setq args
(org-split-string args
","))
2036 (while (string-match "\\\\\\'" (car args
))
2037 ;; Repair bad splits.
2038 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2041 (push (pop args
) args2
))
2042 (mapcar 'org-trim
(nreverse args2
))))))
2049 :post-blank
,post-blank
)))))
2051 (defun org-element-macro-interpreter (macro contents
)
2052 "Interpret MACRO object as Org syntax.
2054 (org-element-get-property :value macro
))
2056 (defun org-element-macro-successor (limit)
2057 "Search for the next macro and return position.
2059 LIMIT bounds the search.
2061 Return value is cons cell whose car is `macro' and cdr is
2062 beginning position."
2064 (when (re-search-forward
2065 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2067 (cons 'macro
(match-beginning 0)))))
2072 (defun org-element-radio-target-parser ()
2073 "Parse radio target at point.
2075 Return a list whose car is `radio-target' and cdr a plist with
2076 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2077 and `:post-blank' as keywords.
2079 Assume point is at the radio target."
2081 (looking-at org-radio-target-regexp
)
2082 (let ((begin (point))
2083 (contents-begin (match-beginning 1))
2084 (contents-end (match-end 1))
2085 (raw-value (org-match-string-no-properties 1))
2086 (post-blank (progn (goto-char (match-end 0))
2087 (skip-chars-forward " \t")))
2092 :contents-begin
,contents-begin
2093 :contents-end
,contents-end
2094 :raw-value
,raw-value
2095 :post-blank
,post-blank
)))))
2097 (defun org-element-radio-target-interpreter (target contents
)
2098 "Interpret TARGET object as Org syntax.
2099 CONTENTS is the contents of the object."
2100 (concat "<<<" contents
">>>"))
2102 (defun org-element-radio-target-successor (limit)
2103 "Search for the next radio-target and return position.
2105 LIMIT bounds the search.
2107 Return value is a cons cell whose car is `radio-target' and cdr
2108 is beginning position."
2110 (when (re-search-forward org-radio-target-regexp limit t
)
2111 (cons 'radio-target
(match-beginning 0)))))
2114 ;;;; Statistics Cookie
2116 (defun org-element-statistics-cookie-parser ()
2117 "Parse statistics cookie at point.
2119 Return a list whose car is `statistics-cookie', and cdr a plist
2120 with `:begin', `:end', `:value' and `:post-blank' keywords.
2122 Assume point is at the beginning of the statistics-cookie."
2124 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2125 (let* ((begin (point))
2126 (value (buffer-substring-no-properties
2127 (match-beginning 0) (match-end 0)))
2128 (post-blank (progn (goto-char (match-end 0))
2129 (skip-chars-forward " \t")))
2135 :post-blank
,post-blank
)))))
2137 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
2138 "Interpret STATISTICS-COOKIE object as Org syntax.
2140 (org-element-get-property :value statistics-cookie
))
2142 (defun org-element-statistics-cookie-successor (limit)
2143 "Search for the next statistics cookie and return position.
2145 LIMIT bounds the search.
2147 Return value is a cons cell whose car is `statistics-cookie' and
2148 cdr is beginning position."
2150 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
2151 (cons 'statistics-cookie
(match-beginning 0)))))
2156 (defun org-element-subscript-parser ()
2157 "Parse subscript at point.
2159 Return a list whose car is `subscript' and cdr a plist with
2160 `:begin', `:end', `:contents-begin', `:contents-end',
2161 `:use-brackets-p' and `:post-blank' as keywords.
2163 Assume point is at the underscore."
2165 (unless (bolp) (backward-char))
2166 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2168 (not (looking-at org-match-substring-regexp
))))
2169 (begin (match-beginning 2))
2170 (contents-begin (or (match-beginning 5)
2171 (match-beginning 3)))
2172 (contents-end (or (match-end 5) (match-end 3)))
2173 (post-blank (progn (goto-char (match-end 0))
2174 (skip-chars-forward " \t")))
2179 :use-brackets-p
,bracketsp
2180 :contents-begin
,contents-begin
2181 :contents-end
,contents-end
2182 :post-blank
,post-blank
)))))
2184 (defun org-element-subscript-interpreter (subscript contents
)
2185 "Interpret SUBSCRIPT object as Org syntax.
2186 CONTENTS is the contents of the object."
2188 (if (org-element-get-property :use-brackets-p subscript
) "_{%s}" "_%s")
2191 (defun org-element-sub/superscript-successor
(limit)
2192 "Search for the next sub/superscript and return beginning
2195 LIMIT bounds the search.
2197 Return value is a cons cell whose car is either `subscript' or
2198 `superscript' and cdr is beginning position."
2200 (when (re-search-forward org-match-substring-regexp limit t
)
2201 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
2202 (match-beginning 2)))))
2207 (defun org-element-superscript-parser ()
2208 "Parse superscript at point.
2210 Return a list whose car is `superscript' and cdr a plist with
2211 `:begin', `:end', `:contents-begin', `:contents-end',
2212 `:use-brackets-p' and `:post-blank' as keywords.
2214 Assume point is at the caret."
2216 (unless (bolp) (backward-char))
2217 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2219 (not (looking-at org-match-substring-regexp
))))
2220 (begin (match-beginning 2))
2221 (contents-begin (or (match-beginning 5)
2222 (match-beginning 3)))
2223 (contents-end (or (match-end 5) (match-end 3)))
2224 (post-blank (progn (goto-char (match-end 0))
2225 (skip-chars-forward " \t")))
2230 :use-brackets-p
,bracketsp
2231 :contents-begin
,contents-begin
2232 :contents-end
,contents-end
2233 :post-blank
,post-blank
)))))
2235 (defun org-element-superscript-interpreter (superscript contents
)
2236 "Interpret SUPERSCRIPT object as Org syntax.
2237 CONTENTS is the contents of the object."
2239 (if (org-element-get-property :use-brackets-p superscript
) "^{%s}" "^%s")
2245 (defun org-element-target-parser ()
2246 "Parse target at point.
2248 Return a list whose car is `target' and cdr a plist with
2249 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2250 and `:post-blank' as keywords.
2252 Assume point is at the target."
2254 (looking-at org-target-regexp
)
2255 (let ((begin (point))
2256 (contents-begin (match-beginning 1))
2257 (contents-end (match-end 1))
2258 (raw-value (org-match-string-no-properties 1))
2259 (post-blank (progn (goto-char (match-end 0))
2260 (skip-chars-forward " \t")))
2265 :contents-begin
,contents-begin
2266 :contents-end
,contents-end
2267 :raw-value
,raw-value
2268 :post-blank
,post-blank
)))))
2270 (defun org-element-target-interpreter (target contents
)
2271 "Interpret TARGET object as Org syntax.
2272 CONTENTS is the contents of target."
2275 (defun org-element-target-successor (limit)
2276 "Search for the next target and return position.
2278 LIMIT bounds the search.
2280 Return value is a cons cell whose car is `target' and cdr is
2281 beginning position."
2283 (when (re-search-forward org-target-regexp limit t
)
2284 (cons 'target
(match-beginning 0)))))
2289 (defun org-element-time-stamp-parser ()
2290 "Parse time stamp at point.
2292 Return a list whose car is `time-stamp', and cdr a plist with
2293 `:appt-type', `:type', `:begin', `:end', `:value' and
2294 `:post-blank' keywords.
2296 Assume point is at the beginning of the time-stamp."
2298 (let* ((appt-type (cond
2299 ((looking-at (concat org-deadline-string
" +"))
2300 (goto-char (match-end 0))
2302 ((looking-at (concat org-scheduled-string
" +"))
2303 (goto-char (match-end 0))
2305 ((looking-at (concat org-closed-string
" +"))
2306 (goto-char (match-end 0))
2308 (begin (and appt-type
(match-beginning 0)))
2310 ((looking-at org-tsr-regexp
)
2311 (if (match-string 2) 'active-range
'active
))
2312 ((looking-at org-tsr-regexp-both
)
2313 (if (match-string 2) 'inactive-range
'inactive
))
2314 ((looking-at (concat
2315 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2317 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2319 (begin (or begin
(match-beginning 0)))
2320 (value (buffer-substring-no-properties
2321 (match-beginning 0) (match-end 0)))
2322 (post-blank (progn (goto-char (match-end 0))
2323 (skip-chars-forward " \t")))
2326 (:appt-type
,appt-type
2331 :post-blank
,post-blank
)))))
2333 (defun org-element-time-stamp-interpreter (time-stamp contents
)
2334 "Interpret TIME-STAMP object as Org syntax.
2337 (case (org-element-get-property :appt-type time-stamp
)
2338 (closed (concat org-closed-string
" "))
2339 (deadline (concat org-deadline-string
" "))
2340 (scheduled (concat org-scheduled-string
" ")))
2341 (org-element-get-property :value time-stamp
)))
2343 (defun org-element-time-stamp-successor (limit)
2344 "Search for the next time-stamp and return position.
2346 LIMIT bounds the search.
2348 Return value is a cons cell whose car is `time-stamp' and cdr is
2349 beginning position."
2351 (when (re-search-forward
2352 (concat "\\(?:" org-scheduled-string
" +\\|"
2353 org-deadline-string
" +\\|" org-closed-string
" +\\)?"
2356 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2358 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2360 (cons 'time-stamp
(match-beginning 0)))))
2365 (defun org-element-verbatim-parser ()
2366 "Parse verbatim object at point.
2368 Return a list whose car is `verbatim' and cdr is a plist with
2369 `:marker', `:begin', `:end' and `:post-blank' keywords.
2371 Assume point is at the first verbatim marker."
2373 (unless (bolp) (backward-char 1))
2374 (looking-at org-emph-re
)
2375 (let ((begin (match-beginning 2))
2376 (marker (org-match-string-no-properties 3))
2377 (value (org-match-string-no-properties 4))
2378 (post-blank (progn (goto-char (match-end 2))
2379 (skip-chars-forward " \t")))
2386 :post-blank
,post-blank
)))))
2388 (defun org-element-verbatim-interpreter (verbatim contents
)
2389 "Interpret VERBATIM object as Org syntax.
2391 (let ((marker (org-element-get-property :marker verbatim
))
2392 (value (org-element-get-property :value verbatim
)))
2393 (concat marker value marker
)))
2397 ;;; Definitions And Rules
2399 ;; Define elements, greater elements and specify recursive objects,
2400 ;; along with the affiliated keywords recognized. Also set up
2401 ;; restrictions on recursive objects combinations.
2403 ;; These variables really act as a control center for the parsing
2405 (defconst org-element-paragraph-separate
2406 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2407 ;; Headlines and inlinetasks.
2408 org-outline-regexp-bol
"\\|"
2409 ;; Comments, blocks (any type), keywords and babel calls.
2410 "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
2412 (org-item-beginning-re) "\\|"
2413 ;; Fixed-width, drawers (any type) and tables.
2415 ;; Footnote definitions.
2416 org-footnote-definition-re
"\\|"
2417 ;; Horizontal rules.
2418 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2419 ;; LaTeX environments.
2420 "^[ \t]*\\\\\\(begin\\|end\\)")
2421 "Regexp to separate paragraphs in an Org buffer.")
2423 (defconst org-element-all-elements
2424 '(center-block comment comment-block drawer dynamic-block example-block
2425 export-block fixed-width footnote-definition headline
2426 horizontal-rule inlinetask item keyword latex-environment
2427 babel-call paragraph plain-list property-drawer quote-block
2428 quote-section section special-block src-block table
2430 "Complete list of elements.")
2432 (defconst org-element-greater-elements
2433 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2434 item plain-list quote-block section special-block
)
2435 "List of recursive element types aka Greater Elements.")
2437 (defconst org-element-all-successors
2438 '(export-snippet footnote-reference inline-babel-call inline-src-block
2439 latex-or-entity line-break link macro radio-target
2440 statistics-cookie sub
/superscript target text-markup
2442 "Complete list of successors.")
2444 (defconst org-element-object-successor-alist
2445 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
2446 (emphasis . text-markup
) (verbatim . text-markup
)
2447 (entity . latex-or-entity
) (latex-fragment . latex-or-entity
))
2448 "Alist of translations between object type and successor name.
2450 Sharing the same successor comes handy when, for example, the
2451 regexp matching one object can also match the other object.")
2453 (defconst org-element-recursive-objects
2454 '(emphasis link subscript superscript target radio-target
)
2455 "List of recursive object types.")
2457 (defconst org-element-non-recursive-block-alist
2458 '(("ascii" . export-block
)
2459 ("comment" . comment-block
)
2460 ("docbook" . export-block
)
2461 ("example" . example-block
)
2462 ("html" . export-block
)
2463 ("latex" . latex-block
)
2464 ("odt" . export-block
)
2466 ("verse" . verse-block
))
2467 "Alist between non-recursive block name and their element type.")
2469 (defconst org-element-affiliated-keywords
2470 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "caption"
2471 "data" "header" "headers" "label" "name" "plot" "resname" "result" "results"
2472 "source" "srcname" "tblname")
2473 "List of affiliated keywords as strings.")
2475 (defconst org-element-keyword-translation-alist
2476 '(("data" .
"name") ("label" .
"name") ("resname" .
"name")
2477 ("source" .
"name") ("srcname" .
"name") ("tblname" .
"name")
2478 ("result" .
"results") ("headers" .
"header"))
2479 "Alist of usual translations for keywords.
2480 The key is the old name and the value the new one. The property
2481 holding their value will be named after the translated name.")
2483 (defconst org-element-multiple-keywords
2484 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "header")
2485 "List of affiliated keywords that can occur more that once in an element.
2487 Their value will be consed into a list of strings, which will be
2488 returned as the value of the property.
2490 This list is checked after translations have been applied. See
2491 `org-element-keyword-translation-alist'.")
2493 (defconst org-element-parsed-keywords
'("author" "caption" "title")
2494 "List of keywords whose value can be parsed.
2496 Their value will be stored as a secondary string: a list of
2497 strings and objects.
2499 This list is checked after translations have been applied. See
2500 `org-element-keyword-translation-alist'.")
2502 (defconst org-element-dual-keywords
'("results")
2503 "List of keywords which can have a secondary value.
2505 In Org syntax, they can be written with optional square brackets
2506 before the colons. For example, results keyword can be
2507 associated to a hash value with the following:
2509 #+results[hash-string]: some-source
2511 This list is checked after translations have been applied. See
2512 `org-element-keyword-translation-alist'.")
2514 (defconst org-element-object-restrictions
2515 '((emphasis entity export-snippet inline-babel-call inline-src-block
2516 radio-target sub
/superscript target text-markup time-stamp
)
2517 (link entity export-snippet inline-babel-call inline-src-block
2518 latex-fragment sub
/superscript text-markup
)
2519 (radio-target entity export-snippet latex-fragment sub
/superscript
)
2520 (subscript entity export-snippet inline-babel-call inline-src-block
2521 latex-fragment sub
/superscript text-markup
)
2522 (superscript entity export-snippet inline-babel-call inline-src-block
2523 latex-fragment sub
/superscript text-markup
)
2524 (target entity export-snippet latex-fragment sub
/superscript text-markup
))
2525 "Alist of recursive objects restrictions.
2527 Car is a recursive object type and cdr is a list of successors
2528 that will be called within an object of such type.
2530 For example, in a `radio-target' object, one can only find
2531 entities, export snippets, latex-fragments, subscript and
2534 (defconst org-element-string-restrictions
2535 '((headline entity inline-babel-call latex-fragment link macro radio-target
2536 statistics-cookie sub
/superscript text-markup time-stamp
)
2537 (inlinetask entity inline-babel-call latex-fragment link macro radio-target
2538 sub
/superscript text-markup time-stamp
)
2539 (item entity inline-babel-call latex-fragment macro radio-target
2540 sub
/superscript target verbatim
)
2541 (keyword entity latex-fragment macro sub
/superscript text-markup
)
2542 (table entity latex-fragment macro text-markup
)
2543 (verse entity footnote-reference inline-babel-call inline-src-block
2544 latex-fragment line-break link macro radio-target sub
/superscript
2545 target text-markup time-stamp
))
2546 "Alist of secondary strings restrictions.
2548 When parsed, some elements have a secondary string which could
2549 contain various objects (i.e. headline's name, or table's cells).
2550 For association, the car is the element type, and the cdr a list
2551 of successors that will be called in that secondary string.
2553 Note: `keyword' secondary string type only applies to keywords
2554 matching `org-element-parsed-keywords'.")
2560 ;; Provide two accessors: `org-element-get-property' and
2561 ;; `org-element-get-contents'.
2563 (defun org-element-get-property (property element
)
2564 "Extract the value from the PROPERTY of an ELEMENT."
2565 (plist-get (nth 1 element
) property
))
2567 (defun org-element-get-contents (element)
2568 "Extract contents from an ELEMENT."
2573 ;; Obtaining The Smallest Element Containing Point
2575 ;; `org-element-at-point' is the core function of this section. It
2576 ;; returns the Lisp representation of the element at point. It uses
2577 ;; `org-element-guess-type' and `org-element-skip-keywords' as helper
2580 ;; When point is at an item, there is no automatic way to determine if
2581 ;; the function should return the `plain-list' element, or the
2582 ;; corresponding `item' element. By default, `org-element-at-point'
2583 ;; works at the `plain-list' level. But, by providing an optional
2584 ;; argument, one can make it switch to the `item' level.
2586 (defconst org-element--affiliated-re
2587 (format "[ \t]*#\\+\\(%s\\):"
2590 (if (member keyword org-element-dual-keywords
)
2591 (format "\\(%s\\)\\(?:\\[\\(.*?\\)\\]\\)?"
2592 (regexp-quote keyword
))
2593 (regexp-quote keyword
)))
2594 org-element-affiliated-keywords
"\\|"))
2595 "Regexp matching any affiliated keyword.
2597 Keyword name is put in match group 1. Moreover, if keyword
2598 belongs to `org-element-dual-keywords', put the dual value in
2601 Don't modify it, set `org-element--affiliated-keywords' instead.")
2603 (defun org-element-at-point (&optional special structure
)
2604 "Determine closest element around point.
2606 Return value is a list \(TYPE PROPS\) where TYPE is the type of
2607 the element and PROPS a plist of properties associated to the
2610 Possible types are defined in `org-element-all-elements'.
2612 Optional argument SPECIAL, when non-nil, can be either `item' or
2613 `section'. The former allows to parse item wise instead of
2614 plain-list wise, using STRUCTURE as the current list structure.
2615 The latter will try to parse a section before anything else.
2617 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
2621 ;; Move before any blank line.
2622 (when (looking-at "[ \t]*$")
2623 (skip-chars-backward " \r\t\n")
2624 (beginning-of-line))
2625 (let ((case-fold-search t
))
2626 ;; Check if point is at an affiliated keyword. In that case,
2627 ;; try moving to the beginning of the associated element. If
2628 ;; the keyword is orphaned, treat it as plain text.
2629 (when (looking-at org-element--affiliated-re
)
2630 (let ((opoint (point)))
2631 (while (looking-at org-element--affiliated-re
) (forward-line))
2632 (when (looking-at "[ \t]*$") (goto-char opoint
))))
2633 (let ((type (org-element-guess-type (eq special
'section
))))
2635 ;; Guessing element type on the current line is impossible:
2636 ;; try to find the beginning of the current element to get
2637 ;; more information.
2639 (let ((search-origin (point))
2640 (opoint-in-item-p (org-in-item-p))
2644 (re-search-backward org-element-paragraph-separate nil
'm
))))
2646 ;; Unable to find a paragraph delimiter above: we're at
2647 ;; bob and looking at a paragraph.
2648 ((not par-found-p
) (org-element-paragraph-parser))
2649 ;; Trying to find element's beginning set point back to
2650 ;; its original position. There's something peculiar on
2651 ;; this line that prevents parsing, probably an
2652 ;; ill-formed keyword or an undefined drawer name. Parse
2653 ;; it as plain text anyway.
2654 ((< search-origin
(point-at-eol)) (org-element-paragraph-parser))
2655 ;; Original point wasn't in a list but previous paragraph
2656 ;; is. It means that either point was inside some block,
2657 ;; or current list was ended without using a blank line.
2658 ;; In the last case, paragraph really starts at list end.
2660 (and (not opoint-in-item-p
)
2661 (not (looking-at "[ \t]*#\\+begin"))
2662 (setq item
(org-in-item-p))
2663 (let ((struct (save-excursion (goto-char item
)
2664 (org-list-struct))))
2665 (goto-char (org-list-get-bottom-point struct
))
2666 (org-skip-whitespace)
2668 (org-element-paragraph-parser)))))
2669 ((org-footnote-at-definition-p)
2670 (org-element-footnote-definition-parser))
2671 ((and opoint-in-item-p
(org-at-item-p) (= opoint-in-item-p
(point)))
2672 (if (eq special
'item
)
2673 (org-element-item-parser (or structure
(org-list-struct)))
2674 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2675 ;; In any other case, the paragraph started the line
2677 (t (forward-line) (org-element-paragraph-parser)))))
2678 ((eq type
'plain-list
)
2679 (if (eq special
'item
)
2680 (org-element-item-parser (or structure
(org-list-struct)))
2681 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2682 ;; Straightforward case: call the appropriate parser.
2683 (t (funcall (intern (format "org-element-%s-parser" type
)))))))))
2686 ;; It is obvious to tell if point is in most elements, either by
2687 ;; looking for a specific regexp in the current line, or by using
2688 ;; already implemented functions. This is the goal of
2689 ;; `org-element-guess-type'.
2691 (defconst org-element--element-block-types
2692 (mapcar 'car org-element-non-recursive-block-alist
)
2693 "List of non-recursive block types, as strings.
2694 Used internally by `org-element-guess-type'. Do not modify it
2695 directly, set `org-element-non-recursive-block-alist' instead.")
2697 (defun org-element-guess-type (&optional section-mode
)
2698 "Return the type of element at point, or nil if undetermined.
2700 This function may move point to an appropriate position for
2701 parsing. Used internally by `org-element-at-point'.
2703 When optional argument SECTION-MODE is non-nil, try to find if
2704 point is in a section in priority."
2705 ;; Beware: Order matters for some cases in that function.
2707 (let ((case-fold-search t
))
2709 ((org-with-limited-levels (org-at-heading-p)) 'headline
)
2710 ((let ((headline (ignore-errors (nth 4 (org-heading-components)))))
2712 (let (case-fold-search)
2713 (string-match (format "^%s\\(?: \\|$\\)" org-quote-string
)
2716 ;; Any buffer position not at an headline or in a quote section
2717 ;; is inside a section, provided function is actively looking for
2719 (section-mode 'section
)
2720 ;; Non-recursive block.
2721 ((let ((type (org-in-block-p org-element--element-block-types
)))
2722 (and type
(cdr (assoc type org-element-non-recursive-block-alist
)))))
2723 ((org-at-heading-p) 'inlinetask
)
2724 ((org-between-regexps-p
2725 "^[ \t]*\\\\begin{" "^[ \t]*\\\\end{[^}]*}[ \t]*") 'latex-environment
)
2726 ;; Property drawer. Almost `org-at-property-p', but allow drawer
2728 ((org-with-wide-buffer
2729 (and (not (org-before-first-heading-p))
2730 (let ((pblock (org-get-property-block)))
2732 (<= (point) (cdr pblock
))
2733 (>= (point-at-eol) (1- (car pblock
)))))))
2735 ;; Recursive block. If the block isn't complete, parse the
2736 ;; current part as a paragraph.
2737 ((looking-at "[ \t]*#\\+\\(begin\\|end\\)_\\([-A-Za-z0-9]+\\)\\(?:$\\|\\s-\\)")
2738 (let ((type (downcase (match-string 2))))
2740 ((not (org-in-block-p (list type
))) 'paragraph
)
2741 ((string= type
"center") 'center-block
)
2742 ((string= type
"quote") 'quote-block
)
2743 (t 'special-block
))))
2744 ;; Regular drawers must be tested after property drawer as both
2745 ;; elements share the same ending regexp.
2746 ((or (looking-at org-drawer-regexp
) (looking-at "[ \t]*:END:[ \t]*$"))
2747 (let ((completep (org-between-regexps-p
2748 org-drawer-regexp
"^[ \t]*:END:[ \t]*$")))
2749 (if (not completep
) 'paragraph
2750 (goto-char (car completep
)) 'drawer
)))
2751 ((looking-at "[ \t]*:\\( \\|$\\)") 'fixed-width
)
2752 ;; Babel calls must be tested before general keywords as they are
2753 ;; a subset of them.
2754 ((looking-at org-babel-block-lob-one-liner-regexp
) 'babel-call
)
2755 ((looking-at org-footnote-definition-re
) 'footnote-definition
)
2756 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
2757 (if (member (downcase (match-string 1)) org-element-affiliated-keywords
)
2760 ;; Dynamic block: simplify regexp used for match. If it isn't
2761 ;; complete, parse the current part as a paragraph.
2762 ((looking-at "[ \t]*#\\+\\(begin\\end\\):\\(?:\\s-\\|$\\)")
2763 (let ((completep (org-between-regexps-p
2764 "^[ \t]*#\\+begin:\\(?:\\s-\\|$\\)"
2765 "^[ \t]*#\\+end:\\(?:\\s-\\|$\\)")))
2766 (if (not completep
) 'paragraph
2767 (goto-char (car completep
)) 'dynamic-block
)))
2768 ((looking-at "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)") 'comment
)
2769 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$") 'horizontal-rule
)
2770 ((org-at-table-p t
) 'table
)
2771 ((looking-at "[ \t]*#\\+tblfm:")
2773 ;; A TBLFM line separated from any table is just plain text.
2774 (if (org-at-table-p) 'table
2775 (forward-line) 'paragraph
))
2776 ((looking-at (org-item-re)) 'plain-list
))))
2778 ;; Most elements can have affiliated keywords. When looking for an
2779 ;; element beginning, we want to move before them, as they belong to
2780 ;; that element, and, in the meantime, collect information they give
2781 ;; into appropriate properties. Hence the following function.
2783 ;; Usage of optional arguments may not be obvious at first glance:
2785 ;; - TRANS-LIST is used to polish keywords names that have evolved
2786 ;; during Org history. In example, even though =result= and
2787 ;; =results= coexist, we want to have them under the same =result=
2788 ;; property. It's also true for "srcname" and "name", where the
2789 ;; latter seems to be preferred nowadays (thus the "name" property).
2791 ;; - CONSED allows to regroup multi-lines keywords under the same
2792 ;; property, while preserving their own identity. This is mostly
2793 ;; used for "attr_latex" and al.
2795 ;; - PARSED prepares a keyword value for export. This is useful for
2796 ;; "caption". Objects restrictions for such keywords are defined in
2797 ;; `org-element-string-restrictions'.
2799 ;; - DUALS is used to take care of keywords accepting a main and an
2800 ;; optional secondary values. For example "results" has its
2801 ;; source's name as the main value, and may have an hash string in
2802 ;; optional square brackets as the secondary one.
2804 ;; A keyword may belong to more than one category.
2806 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
2807 consed parsed duals
)
2808 "Collect affiliated keywords before point.
2810 Optional argument KEY-RE is a regexp matching keywords, which
2811 puts matched keyword in group 1. It defaults to
2812 `org-element--affiliated-re'.
2814 TRANS-LIST is an alist where key is the keyword and value the
2815 property name it should be translated to, without the colons. It
2816 defaults to `org-element-keyword-translation-alist'.
2818 CONSED is a list of strings. Any keyword belonging to that list
2819 will have its value consed. The check is done after keyword
2820 translation. It defaults to `org-element-multiple-keywords'.
2822 PARSED is a list of strings. Any keyword member of this list
2823 will have its value parsed. The check is done after keyword
2824 translation. If a keyword is a member of both CONSED and PARSED,
2825 it's value will be a list of parsed strings. It defaults to
2826 `org-element-parsed-keywords'.
2828 DUALS is a list of strings. Any keyword member of this list can
2829 have two parts: one mandatory and one optional. Its value is
2830 a cons cell whose car is the former, and the cdr the latter. If
2831 a keyword is a member of both PARSED and DUALS, only the primary
2832 part will be parsed. It defaults to `org-element-dual-keywords'.
2834 Return a list whose car is the position at the first of them and
2835 cdr a plist of keywords and values."
2837 (let ((case-fold-search t
)
2838 (key-re (or key-re org-element--affiliated-re
))
2839 (trans-list (or trans-list org-element-keyword-translation-alist
))
2840 (consed (or consed org-element-multiple-keywords
))
2841 (parsed (or parsed org-element-parsed-keywords
))
2842 (duals (or duals org-element-dual-keywords
))
2845 (while (and (not (bobp))
2846 (progn (forward-line -
1) (looking-at key-re
)))
2847 (let* ((raw-kwd (downcase (or (match-string 2) (match-string 1))))
2848 ;; Apply translation to RAW-KWD. From there, KWD is
2849 ;; the official keyword.
2850 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
2851 ;; If KWD is a dual keyword, find it secondary value.
2852 (dual-value (and (member kwd duals
)
2853 (org-match-string-no-properties 3)))
2854 ;; Find main value for any keyword.
2855 (value (org-trim (buffer-substring-no-properties
2856 (match-end 0) (point-at-eol))))
2857 ;; Attribute a property name to KWD.
2858 (kwd-sym (and kwd
(intern (concat ":" kwd
)))))
2859 ;; Now set final shape for VALUE.
2860 (when (member kwd parsed
)
2862 (org-element-parse-secondary-string
2864 (cdr (assq 'keyword org-element-string-restrictions
)))))
2865 (when (member kwd duals
) (setq value
(cons value dual-value
)))
2866 (when (member kwd consed
)
2867 (setq value
(cons value
(plist-get output kwd-sym
))))
2868 ;; Eventually store the new value in OUTPUT.
2869 (setq output
(plist-put output kwd-sym value
))))
2870 (unless (looking-at key-re
) (forward-line 1)))
2871 (list (point) output
))))
2877 ;; The two major functions here are `org-element-parse-buffer', which
2878 ;; parses Org syntax inside the current buffer, taking into account
2879 ;; region, narrowing, or even visibility if specified, and
2880 ;; `org-element-parse-secondary-string', which parses objects within
2883 ;; The (almost) almighty `org-element-map' allows to apply a function
2884 ;; on elements or objects matching some type, and accumulate the
2885 ;; resulting values. In an export situation, it also skips unneeded
2886 ;; parts of the parse tree, transparently walks into included files,
2887 ;; and maintain a list of local properties (i.e. those inherited from
2888 ;; parent headlines) for function's consumption.
2890 (defun org-element-parse-buffer (&optional granularity visible-only
)
2891 "Recursively parse the buffer and return structure.
2892 If narrowing is in effect, only parse the visible part of the
2895 Optional argument GRANULARITY determines the depth of the
2896 recursion. It can be set to the following symbols:
2898 `headline' Only parse headlines.
2899 `greater-element' Don't recurse into greater elements. Thus,
2900 elements parsed are the top-level ones.
2901 `element' Parse everything but objects and plain text.
2902 `object' Parse the complete buffer (default).
2904 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
2907 Assume buffer is in Org mode."
2909 (goto-char (point-min))
2910 (org-skip-whitespace)
2911 (nconc (list 'org-data nil
)
2912 (org-element-parse-elements
2913 (point-at-bol) (point-max)
2914 ;; Start is section mode so text before the first headline
2915 ;; belongs to a section.
2916 'section nil granularity visible-only nil
))))
2918 (defun org-element-parse-secondary-string (string restriction
&optional buffer
)
2919 "Recursively parse objects in STRING and return structure.
2921 RESTRICTION, when non-nil, is a symbol limiting the object types
2922 that will be looked after.
2924 Optional argument BUFFER indicates the buffer from where the
2925 secondary string was extracted. It is used to determine where to
2926 get extraneous information for an object \(i.e. when resolving
2927 a link or looking for a footnote definition\). It defaults to
2928 the current buffer."
2931 (org-element-parse-objects (point-min) (point-max) nil restriction
)))
2933 (defun org-element-map (data types fun
&optional info first-match
)
2934 "Map a function on selected elements or objects.
2936 DATA is the parsed tree, as returned by, i.e,
2937 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
2938 of elements or objects types. FUN is the function called on the
2939 matching element or object. It must accept two arguments: the
2940 element or object itself and a plist holding contextual
2943 When optional argument INFO is non-nil, it should be a plist
2944 holding export options. In that case, parts of the parse tree
2945 not exportable according to that property list will be skipped
2946 and files included through a keyword will be visited.
2948 When optional argument FIRST-MATCH is non-nil, stop at the first
2949 match for which FUN doesn't return nil, and return that value.
2951 Nil values returned from FUN are ignored in the result."
2952 ;; Ensure TYPES is a list, even of one element.
2953 (unless (listp types
) (setq types
(list types
)))
2954 ;; Recursion depth is determined by --CATEGORY.
2957 ((loop for type in types
2958 always
(memq type org-element-greater-elements
))
2960 ((loop for type in types
2961 always
(memq type org-element-all-elements
))
2964 walk-tree
; For byte-compiler
2968 (lambda (--type types fun --blob --local
)
2969 ;; Check if TYPE is matching among TYPES. If so, apply
2970 ;; FUN to --BLOB and accumulate return value
2971 ;; into --ACC. --LOCAL is the communication channel.
2972 (when (memq --type types
)
2973 (let ((result (funcall fun --blob --local
)))
2974 (cond ((not result
))
2975 (first-match (throw 'first-match result
))
2976 (t (push result --acc
))))))))
2979 (lambda (--data --local
)
2980 ;; Recursively walk DATA. --LOCAL, if non-nil, is
2981 ;; a plist holding contextual information.
2984 (let ((--type (if (stringp --blob
) 'plain-text
(car --blob
))))
2985 ;; Determine if a recursion into --BLOB is
2986 ;; possible and allowed.
2988 ;; Element or object not exportable.
2989 ((and info
(org-export-skip-p --blob info
)))
2990 ;; Archived headline: Maybe apply fun on it, but
2993 (eq --type
'headline
)
2994 (eq (plist-get info
:with-archived-trees
) 'headline
)
2995 (org-element-get-property :archivedp --blob
))
2996 (funcall accumulate-maybe --type types fun --blob --local
))
2997 ;; At an include keyword: apply mapping to its
3000 (eq --type
'keyword
)
3002 (downcase (org-element-get-property :key --blob
))
3004 (funcall accumulate-maybe --type types fun --blob --local
)
3006 (org-export-parse-included-file --blob --local
))
3007 (--value (org-element-get-property :value --blob
))
3009 (and (string-match "^\"\\(\\S-+\\)\"" --value
)
3010 (match-string 1 --value
))))
3015 ;; Store full path of already included files
3016 ;; to avoid recursive file inclusion.
3018 ,(cons (expand-file-name --file
)
3019 (plist-get --local
:included-files
))
3020 ;; Ensure that a top-level headline in the
3021 ;; included file becomes a direct child of
3022 ;; the current headline in the buffer.
3025 (plist-get --local
:inherited-properties
)
3027 (or (plist-get --local
:headline-offset
) 0))
3028 (1- (org-export-get-min-level
3029 --data --local
))))))))
3030 ;; Limiting recursion to greater elements, and --BLOB
3032 ((and (eq --category
'greater-elements
)
3033 (not (memq --type org-element-greater-elements
)))
3034 (funcall accumulate-maybe --type types fun --blob --local
))
3035 ;; Limiting recursion to elements, and --BLOB only
3036 ;; contains objects.
3037 ((and (eq --category
'elements
) (eq --type
'paragraph
)))
3038 ;; No limitation on recursion, but --BLOB hasn't
3039 ;; got a recursive type.
3040 ((and (eq --category
'objects
)
3041 (not (or (eq --type
'paragraph
)
3042 (memq --type org-element-greater-elements
)
3043 (memq --type org-element-recursive-objects
))))
3044 (funcall accumulate-maybe --type types fun --blob --local
))
3045 ;; Recursion is possible and allowed: Update local
3046 ;; information and move into --BLOB.
3047 (t (funcall accumulate-maybe --type types fun --blob --local
)
3052 ,(cons --blob
(plist-get info
:genealogy
)))))))))
3053 (org-element-get-contents --data
))))))
3055 (funcall walk-tree data info
)
3056 ;; Return value in a proper order.
3059 ;; The following functions are internal parts of the parser. The
3060 ;; first one, `org-element-parse-elements' acts at the element's
3061 ;; level. The second one, `org-element-parse-objects' applies on all
3062 ;; objects of a paragraph or a secondary string. It uses
3063 ;; `org-element-get-candidates' to optimize the search of the next
3064 ;; object in the buffer.
3066 ;; More precisely, that function looks for every allowed object type
3067 ;; first. Then, it discards failed searches, keeps further matches,
3068 ;; and searches again types matched behind point, for subsequent
3069 ;; calls. Thus, searching for a given type fails only once, and every
3070 ;; object is searched only once at top level (but sometimes more for
3073 (defun org-element-parse-elements
3074 (beg end special structure granularity visible-only acc
)
3075 "Parse ELEMENT with point at its beginning.
3077 SPECIAL prioritize some elements over the others. It can set to
3078 either `section' or `item', which will focus search,
3079 respectively, on sections and items. Moreover, when value is
3080 `item', STRUCTURE will be used as the current list structure.
3082 GRANULARITY determines the depth of the recursion. It can be set
3083 to the following symbols:
3085 `headline' Only parse headlines.
3086 `greater-element' Don't recurse into greater elements. Thus,
3087 elements parsed are the top-level ones.
3088 `element' Parse everything but objects and plain text.
3089 `object' or nil Parse the complete buffer.
3091 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3094 Elements are accumulated into ACC."
3097 ;; Shortcut when parsing only headlines.
3098 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3099 (org-with-limited-levels (outline-next-heading)))
3101 (while (and (< (point) end
) (not (eobp)))
3103 ;; 1. Item mode is active: point is at an item. Knowing that,
3104 ;; there's no need to go through `org-element-at-point'.
3105 (if (eq special
'item
)
3106 (let* ((element (org-element-item-parser structure
))
3107 (cbeg (org-element-get-property :contents-begin element
))
3108 (cend (org-element-get-property :contents-end element
)))
3109 (goto-char (org-element-get-property :end element
))
3110 ;; Narrow region to contents, so that item bullet don't
3111 ;; interfere with paragraph parsing.
3113 (narrow-to-region cbeg cend
)
3114 (org-element-parse-elements
3115 cbeg cend nil structure granularity visible-only
3116 (reverse element
))))
3117 ;; 2. When ITEM is nil, find current element's type and parse
3118 ;; it accordingly to its category.
3119 (let ((element (org-element-at-point special structure
)))
3120 (goto-char (org-element-get-property :end element
))
3122 ;; Case 1. ELEMENT is a footnote-definition. If
3123 ;; GRANURALITY allows parsing, use narrowing so that
3124 ;; footnote label don't interfere with paragraph
3126 ((and (eq (car element
) 'footnote-definition
)
3127 (not (memq granularity
'(headline greater-element
))))
3128 (let ((cbeg (org-element-get-property :contents-begin element
))
3129 (cend (org-element-get-property :contents-end element
)))
3131 (narrow-to-region cbeg cend
)
3132 (org-element-parse-elements
3133 cbeg cend nil structure granularity visible-only
3134 (reverse element
)))))
3135 ;; Case 2. ELEMENT is a paragraph. Parse objects inside,
3136 ;; if GRANULARITY allows it.
3137 ((and (eq (car element
) 'paragraph
)
3138 (or (not granularity
) (eq granularity
'object
)))
3139 (org-element-parse-objects
3140 (org-element-get-property :contents-begin element
)
3141 (org-element-get-property :contents-end element
)
3144 ;; Case 3. ELEMENT is recursive: parse it between
3145 ;; `contents-begin' and `contents-end'. Make sure
3146 ;; GRANULARITY allows the recursion, or ELEMENT is an
3147 ;; headline, in which case going inside is mandatory, in
3148 ;; order to get sub-level headings. If VISIBLE-ONLY is
3149 ;; true and element is hidden, do not recurse into it.
3150 ((and (memq (car element
) org-element-greater-elements
)
3151 (or (not granularity
)
3152 (memq granularity
'(element object
))
3153 (eq (car element
) 'headline
))
3154 (not (and visible-only
3155 (org-element-get-property :hiddenp element
))))
3156 (org-element-parse-elements
3157 (org-element-get-property :contents-begin element
)
3158 (org-element-get-property :contents-end element
)
3159 ;; At a plain list, switch to item mode. At an
3160 ;; headline, switch to section mode. Any other element
3161 ;; turns off special modes.
3162 (case (car element
) (plain-list 'item
) (headline 'section
))
3163 (org-element-get-property :structure element
)
3167 ;; Case 4. Else, just accumulate ELEMENT.
3170 (org-skip-whitespace))
3174 (defun org-element-parse-objects (beg end acc restriction
)
3175 "Parse objects between BEG and END and return recursive structure.
3177 Objects are accumulated in ACC.
3179 RESTRICTION, when non-nil, is a list of object types which are
3180 allowed in the current object."
3181 (let ((get-next-object
3184 ;; Return the parsing function associated to the nearest
3185 ;; object among list of candidates CAND.
3186 (let ((pos (apply #'min
(mapcar #'cdr cand
))))
3191 (format "org-element-%s-parser" (car (rassq pos cand
))))))))))
3192 next-object candidates
)
3195 (while (setq candidates
(org-element-get-next-object-candidates
3196 end restriction candidates
))
3197 (setq next-object
(funcall get-next-object candidates
))
3198 ;; 1. Text before any object. Untabify it.
3199 (let ((obj-beg (org-element-get-property :begin next-object
)))
3200 (unless (= (point) obj-beg
)
3201 (push (replace-regexp-in-string
3202 "\t" (make-string tab-width ?
)
3203 (buffer-substring-no-properties (point) obj-beg
))
3206 (let ((obj-end (org-element-get-property :end next-object
))
3207 (cont-beg (org-element-get-property :contents-begin next-object
)))
3208 (push (if (and (memq (car next-object
) org-element-recursive-objects
)
3210 ;; ... recursive. The CONT-BEG check is for
3211 ;; links, as some of them might not be recursive
3212 ;; (i.e. plain links).
3216 (org-element-get-property :contents-end next-object
))
3217 (org-element-parse-objects
3218 (point-min) (point-max) (reverse next-object
)
3219 ;; Restrict allowed objects. This is the
3220 ;; intersection of current restriction and next
3221 ;; object's restriction.
3223 (cdr (assq (car next-object
)
3224 org-element-object-restrictions
))))
3225 (if (not restriction
) new-restr
3227 (lambda (e) (and (memq e restriction
) e
))
3229 ;; ... not recursive.
3232 (goto-char obj-end
)))
3233 ;; 3. Text after last object. Untabify it.
3234 (unless (= (point) end
)
3235 (push (replace-regexp-in-string
3236 "\t" (make-string tab-width ?
)
3237 (buffer-substring-no-properties (point) end
))
3242 (defun org-element-get-next-object-candidates (limit restriction objects
)
3243 "Return an alist of candidates for the next object.
3245 LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
3246 the possible object types.
3248 Return value is an alist whose car is position and cdr the object
3249 type, as a string. There is an association for the closest
3250 object of each type within RESTRICTION when non-nil, or for every
3253 OBJECTS is the previous candidates alist."
3254 (let ((restriction (or restriction org-element-all-successors
))
3255 next-candidates types-to-search
)
3256 ;; If no previous result, search every object type in RESTRICTION.
3257 ;; Otherwise, keep potential candidates (old objects located after
3258 ;; point) and ask to search again those which had matched before.
3259 (if (not objects
) (setq types-to-search restriction
)
3261 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3262 (push obj next-candidates
)))
3264 ;; Call the appropriate "get-next" function for each type to
3265 ;; search and accumulate matches.
3268 (let* ((successor-fun
3270 (format "org-element-%s-successor"
3271 (or (cdr (assq type org-element-object-successor-alist
))
3273 (obj (funcall successor-fun limit
)))
3274 (and obj
(push obj next-candidates
))))
3281 ;;; Towards A Bijective Process
3283 ;; The parse tree obtained with `org-element-parse-buffer' is really
3284 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3285 ;; interpreted and expanded into a string with canonical Org
3286 ;; syntax. Hence `org-element-interpret-data'.
3288 ;; Data parsed from secondary strings, whose shape is slightly
3289 ;; different than the standard parse tree, is expanded with the
3290 ;; equivalent function `org-element-interpret-secondary'.
3292 ;; Both functions rely internally on
3293 ;; `org-element-interpret--affiliated-keywords'.
3295 (defun org-element-interpret-data (data &optional genealogy previous
)
3296 "Interpret a parse tree representing Org data.
3298 DATA is the parse tree to interpret.
3300 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3302 GENEALOGY is the list of its parents types.
3303 PREVIOUS is the type of the element or object at the same level
3306 Return Org syntax as a string."
3309 ;; BLOB can be an element, an object, a string, or nil.
3312 ((equal blob
"") nil
)
3313 ((stringp blob
) blob
)
3315 (let* ((type (car blob
))
3317 (if (eq type
'org-data
) 'identity
3318 (intern (format "org-element-%s-interpreter" type
))))
3321 ;; Full Org document.
3322 ((eq type
'org-data
)
3323 (org-element-interpret-data blob genealogy previous
))
3324 ;; Recursive objects.
3325 ((memq type org-element-recursive-objects
)
3326 (org-element-interpret-data
3327 blob
(cons type genealogy
) nil
))
3328 ;; Recursive elements.
3329 ((memq type org-element-greater-elements
)
3330 (org-element-normalize-string
3331 (org-element-interpret-data
3332 blob
(cons type genealogy
) nil
)))
3334 ((eq type
'paragraph
)
3336 (org-element-normalize-contents
3338 ;; When normalizing contents of an item,
3339 ;; ignore first line's indentation.
3341 (memq (car genealogy
)
3342 '(footnote-definiton item
))))))
3343 (org-element-interpret-data
3344 paragraph
(cons type genealogy
) nil
)))))
3345 (results (funcall interpreter blob contents
)))
3347 (setq previous type
)
3348 ;; Build white spaces.
3350 ((eq type
'org-data
) results
)
3351 ((memq type org-element-all-elements
)
3353 (org-element-interpret--affiliated-keywords blob
)
3354 (org-element-normalize-string results
)
3355 (make-string (org-element-get-property :post-blank blob
) 10)))
3359 (org-element-get-property :post-blank blob
) 32))))))))
3360 (org-element-get-contents data
) ""))
3362 (defun org-element-interpret-secondary (secondary)
3363 "Interpret SECONDARY string as Org syntax.
3365 SECONDARY-STRING is a nested list as returned by
3366 `org-element-parse-secondary-string'.
3368 Return interpreted string."
3369 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3370 (let ((s (if (listp secondary
) secondary
(list secondary
))))
3371 (org-element-interpret-data `(org-data nil
,@s
) nil nil
)))
3373 ;; Both functions internally use `org-element--affiliated-keywords'.
3375 (defun org-element-interpret--affiliated-keywords (element)
3376 "Return ELEMENT's affiliated keywords as Org syntax.
3377 If there is no affiliated keyword, return the empty string."
3378 (let ((keyword-to-org
3382 (when (member key org-element-dual-keywords
)
3383 (setq dual
(cdr value
) value
(car value
)))
3384 (concat "#+" key
(and dual
(format "[%s]" dual
)) ": "
3385 (if (member key org-element-parsed-keywords
)
3386 (org-element-interpret-secondary value
)
3391 (let ((value (org-element-get-property (intern (concat ":" key
)) element
)))
3393 (if (member key org-element-multiple-keywords
)
3394 (mapconcat (lambda (line)
3395 (funcall keyword-to-org key line
))
3397 (funcall keyword-to-org key value
)))))
3398 ;; Remove translated keywords.
3402 (and (not (assoc key org-element-keyword-translation-alist
)) key
))
3403 org-element-affiliated-keywords
))
3406 ;; Because interpretation of the parse tree must return the same
3407 ;; number of blank lines between elements and the same number of white
3408 ;; space after objects, some special care must be given to white
3411 ;; The first function, `org-element-normalize-string', ensures any
3412 ;; string different from the empty string will end with a single
3413 ;; newline character.
3415 ;; The second function, `org-element-normalize-contents', removes
3416 ;; global indentation from the contents of the current element.
3418 (defun org-element-normalize-string (s)
3419 "Ensure string S ends with a single newline character.
3421 If S isn't a string return it unchanged. If S is the empty
3422 string, return it. Otherwise, return a new string with a single
3423 newline character at its end."
3425 ((not (stringp s
)) s
)
3427 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3428 (replace-match "\n" nil nil s
)))))
3430 (defun org-element-normalize-contents (element &optional ignore-first
)
3431 "Normalize plain text in ELEMENT's contents.
3433 ELEMENT must only contain plain text and objects.
3435 The following changes are applied to plain text:
3436 - Remove global indentation, preserving relative one.
3439 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3440 indentation to compute maximal common indentation.
3442 Return the normalized element."
3444 (list (car element
) (nth 1 element
))
3445 (let ((contents (org-element-get-contents element
)))
3446 (if (not (or ignore-first
(stringp (car contents
)))) contents
3448 ;; 1. Get maximal common indentation (MCI) among each string
3450 (let* ((ind-list (unless ignore-first
3451 (list (org-get-string-indentation (car contents
)))))
3455 (if (not (stringp object
)) object
3457 (while (string-match "\n\\( *\\)" object start
)
3458 (setq start
(match-end 0))
3459 (push (length (match-string 1 object
)) ind-list
))
3462 (mci (if ind-list
(apply 'min ind-list
)
3463 (throw 'exit contents
))))
3464 ;; 2. Remove that indentation from CONTENTS. First string
3465 ;; must be treated differently because it's the only one
3466 ;; whose indentation doesn't happen after a newline
3468 (let ((first-obj (car contents
)))
3469 (unless (or (not (stringp first-obj
)) ignore-first
)
3471 (cons (replace-regexp-in-string
3472 (format "\\` \\{%d\\}" mci
) "" first-obj
)
3474 (mapcar (lambda (object)
3475 (if (not (stringp object
)) object
3476 (replace-regexp-in-string
3477 (format "\n \\{%d\\}" mci
) "\n" object
)))
3484 ;; Once the structure of an Org file is well understood, it's easy to
3485 ;; implement some replacements for `forward-paragraph'
3486 ;; `backward-paragraph', namely `org-element-forward' and
3487 ;; `org-element-backward'.
3489 ;; Also, `org-transpose-elements' mimics the behaviour of
3490 ;; `transpose-words', at the element's level, whereas
3491 ;; `org-element-drag-forward', `org-element-drag-backward', and
3492 ;; `org-element-up' generalize, respectively, functions
3493 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3495 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3496 ;; smartly remove global indentation from buffer, making it possible
3497 ;; to use Org indent mode on a file created with hard indentation.
3499 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3500 ;; internally by some of the previously cited tools.
3502 (defsubst org-element-nested-p
(elem-A elem-B
)
3503 "Non-nil when elements ELEM-A and ELEM-B are nested."
3504 (let ((beg-A (org-element-get-property :begin elem-A
))
3505 (beg-B (org-element-get-property :begin elem-B
))
3506 (end-A (org-element-get-property :end elem-A
))
3507 (end-B (org-element-get-property :end elem-B
)))
3508 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
3509 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
3511 (defun org-element-swap-A-B (elem-A elem-B
)
3512 "Swap elements ELEM-A and ELEM-B.
3514 Leave point at the end of ELEM-A.
3516 Assume ELEM-A is before ELEM-B and that they are not nested."
3517 (goto-char (org-element-get-property :begin elem-A
))
3518 (let* ((beg-B (org-element-get-property :begin elem-B
))
3519 (end-B-no-blank (save-excursion
3520 (goto-char (org-element-get-property :end elem-B
))
3521 (skip-chars-backward " \r\t\n")
3524 (beg-A (org-element-get-property :begin elem-A
))
3525 (end-A-no-blank (save-excursion
3526 (goto-char (org-element-get-property :end elem-A
))
3527 (skip-chars-backward " \r\t\n")
3530 (body-A (buffer-substring beg-A end-A-no-blank
))
3531 (body-B (buffer-substring beg-B end-B-no-blank
))
3532 (between-A-B (buffer-substring end-A-no-blank beg-B
)))
3533 (delete-region beg-A end-B-no-blank
)
3534 (insert body-B between-A-B body-A
)
3535 (goto-char (org-element-get-property :end elem-B
))))
3537 (defun org-element-backward ()
3538 "Move backward by one element."
3540 (let* ((opoint (point))
3541 (element (org-element-at-point))
3542 (start-el-beg (org-element-get-property :begin element
)))
3543 ;; At an headline. The previous element is the previous sibling,
3544 ;; or the parent if any.
3546 ;; Already at the beginning of the current element: move to the
3547 ;; beginning of the previous one.
3548 ((= opoint start-el-beg
)
3550 (skip-chars-backward " \r\t\n")
3551 (let* ((prev-element (org-element-at-point))
3552 (itemp (org-in-item-p))
3554 (save-excursion (goto-char itemp
)
3555 (org-list-struct)))))
3556 ;; When moving into a new list, go directly at the
3557 ;; beginning of the top list structure.
3558 (if (and itemp
(<= (org-list-get-bottom-point struct
) opoint
))
3560 (goto-char (org-list-get-top-point struct
))
3561 (goto-char (org-element-get-property
3562 :begin
(org-element-at-point))))
3563 (goto-char (org-element-get-property :begin prev-element
))))
3564 (while (org-truely-invisible-p) (org-element-up)))
3565 ;; Else, move at the element beginning. One exception: if point
3566 ;; was in the blank lines after the end of a list, move directly
3570 (if (and (setq itemp
(org-in-item-p))
3571 (<= (org-list-get-bottom-point
3572 (save-excursion (goto-char itemp
)
3573 (setq struct
(org-list-struct))))
3575 (progn (goto-char (org-list-get-top-point struct
))
3576 (goto-char (org-element-get-property
3577 :begin
(org-element-at-point))))
3578 (goto-char start-el-beg
)))))))
3580 (defun org-element-drag-backward ()
3581 "Drag backward element at point."
3583 (let* ((pos (point))
3584 (elem (org-element-at-point)))
3585 (when (= (progn (goto-char (point-min))
3586 (org-skip-whitespace)
3588 (org-element-get-property :end elem
))
3589 (error "Cannot drag element backward"))
3590 (goto-char (org-element-get-property :begin elem
))
3591 (org-element-backward)
3592 (let ((prev-elem (org-element-at-point)))
3593 (when (or (org-element-nested-p elem prev-elem
)
3594 (and (eq (car elem
) 'headline
)
3595 (not (eq (car prev-elem
) 'headline
))))
3597 (error "Cannot drag element backward"))
3598 ;; Compute new position of point: it's shifted by PREV-ELEM
3600 (let ((size-prev (- (org-element-get-property :end prev-elem
)
3601 (org-element-get-property :begin prev-elem
))))
3602 (org-element-swap-A-B prev-elem elem
)
3603 (goto-char (- pos size-prev
))))))
3605 (defun org-element-drag-forward ()
3606 "Move forward element at point."
3608 (let* ((pos (point))
3609 (elem (org-element-at-point)))
3610 (when (= (point-max) (org-element-get-property :end elem
))
3611 (error "Cannot drag element forward"))
3612 (goto-char (org-element-get-property :end elem
))
3613 (let ((next-elem (org-element-at-point)))
3614 (when (or (org-element-nested-p elem next-elem
)
3615 (and (eq (car next-elem
) 'headline
)
3616 (not (eq (car elem
) 'headline
))))
3618 (error "Cannot drag element forward"))
3619 ;; Compute new position of point: it's shifted by NEXT-ELEM
3620 ;; body's length (without final blanks) and by the length of
3621 ;; blanks between ELEM and NEXT-ELEM.
3622 (let ((size-next (- (save-excursion
3623 (goto-char (org-element-get-property :end next-elem
))
3624 (skip-chars-backward " \r\t\n")
3627 (org-element-get-property :begin next-elem
)))
3628 (size-blank (- (org-element-get-property :end elem
)
3630 (goto-char (org-element-get-property :end elem
))
3631 (skip-chars-backward " \r\t\n")
3634 (org-element-swap-A-B elem next-elem
)
3635 (goto-char (+ pos size-next size-blank
))))))
3637 (defun org-element-forward ()
3638 "Move forward by one element."
3641 (cond ((eobp) (error "Cannot move further down"))
3642 ((looking-at "[ \t]*$")
3643 (org-skip-whitespace)
3644 (goto-char (if (eobp) (point) (point-at-bol))))
3646 (let ((element (org-element-at-point t
))
3649 ;; At an item: Either move to the next element inside, or
3650 ;; to its end if it's hidden.
3651 ((eq (car element
) 'item
)
3652 (if (org-element-get-property :hiddenp element
)
3653 (goto-char (org-element-get-property :end element
))
3655 (re-search-forward org-element-paragraph-separate nil t
)
3656 (org-skip-whitespace)
3657 (beginning-of-line)))
3658 ;; At a recursive element: Either move inside, or if it's
3659 ;; hidden, move to its end.
3660 ((memq (car element
) org-element-greater-elements
)
3661 (let ((cbeg (org-element-get-property :contents-begin element
)))
3663 (if (or (org-element-get-property :hiddenp element
)
3665 (org-element-get-property :end element
)
3667 ;; Else: move to the current element's end.
3668 (t (goto-char (org-element-get-property :end element
))))))))
3670 (defun org-element-mark-element ()
3671 "Put point at beginning of this element, mark at end.
3673 Interactively, if this command is repeated or (in Transient Mark
3674 mode) if the mark is active, it marks the next element after the
3675 ones already marked."
3677 (let (deactivate-mark)
3678 (if (or (and (eq last-command this-command
) (mark t
))
3679 (and transient-mark-mode mark-active
))
3683 (goto-char (org-element-get-property :end
(org-element-at-point)))))
3684 (let ((element (org-element-at-point)))
3686 (push-mark (org-element-get-property :end element
) t t
)
3687 (goto-char (org-element-get-property :begin element
))))))
3689 (defun org-narrow-to-element ()
3690 "Narrow buffer to current element."
3692 (let ((elem (org-element-at-point)))
3694 ((eq (car elem
) 'headline
)
3696 (org-element-get-property :begin elem
)
3697 (org-element-get-property :end elem
)))
3698 ((memq (car elem
) org-element-greater-elements
)
3700 (org-element-get-property :contents-begin elem
)
3701 (org-element-get-property :contents-end elem
)))
3704 (org-element-get-property :begin elem
)
3705 (org-element-get-property :end elem
))))))
3707 (defun org-transpose-elements ()
3708 "Transpose current and previous elements, keeping blank lines between.
3709 Point is moved after both elements."
3711 (org-skip-whitespace)
3713 (cur (org-element-at-point)))
3714 (when (= (save-excursion (goto-char (point-min))
3715 (org-skip-whitespace)
3717 (org-element-get-property :begin cur
))
3718 (error "No previous element"))
3719 (goto-char (org-element-get-property :begin cur
))
3721 (let ((prev (org-element-at-point)))
3722 (when (org-element-nested-p cur prev
)
3724 (error "Cannot transpose nested elements"))
3725 (org-element-swap-A-B prev cur
))))
3727 (defun org-element-unindent-buffer ()
3728 "Un-indent the visible part of the buffer.
3729 Relative indentation \(between items, inside blocks, etc.\) isn't
3732 (unless (eq major-mode
'org-mode
)
3733 (error "Cannot un-indent a buffer not in Org mode"))
3734 (let* ((parse-tree (org-element-parse-buffer 'greater-element
))
3735 unindent-tree
; For byte-compiler.
3739 (mapc (lambda (element)
3740 (if (eq (car element
) 'headline
)
3741 (funcall unindent-tree
3742 (org-element-get-contents element
))
3746 (org-element-get-property :begin element
)
3747 (org-element-get-property :end element
))
3748 (org-do-remove-indentation)))))
3749 (reverse contents
))))))
3750 (funcall unindent-tree
(org-element-get-contents parse-tree
))))
3752 (defun org-element-up ()
3753 "Move to upper element.
3754 Return position at the beginning of the upper element."
3756 (let ((opoint (point)) elem
)
3758 ((bobp) (error "No surrounding element"))
3759 ((org-with-limited-levels (org-at-heading-p))
3760 (or (org-up-heading-safe) (error "No surronding element")))
3761 ((and (org-at-item-p)
3762 (setq elem
(org-element-at-point))
3763 (let* ((top-list-p (zerop (org-element-get-property :level elem
))))
3765 ;; If parent is bound to be in the same list as the
3766 ;; original point, move to that parent.
3767 (let ((struct (org-element-get-property :structure elem
)))
3769 (org-list-get-parent
3770 (point-at-bol) struct
(org-list-parents-alist struct
))))))))
3772 (let* ((elem (or elem
(org-element-at-point)))
3773 (end (save-excursion
3774 (goto-char (org-element-get-property :end elem
))
3775 (skip-chars-backward " \r\t\n")
3779 (goto-char (org-element-get-property :begin elem
))
3781 (while (and (< (org-element-get-property
3782 :end
(setq prev-elem
(org-element-at-point)))
3785 (goto-char (org-element-get-property :begin prev-elem
))
3787 (if (and (bobp) (< (org-element-get-property :end prev-elem
) end
))
3788 (progn (goto-char opoint
)
3789 (error "No surrounding element"))
3790 (goto-char (org-element-get-property :begin prev-elem
))))))))
3793 (provide 'org-element
)
3794 ;;; org-element.el ends here