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'.
137 (defun org-element-center-block-parser ()
138 "Parse a center block.
140 Return a list whose car is `center-block' and cdr is a plist
141 containing `:begin', `:end', `:hiddenp', `:contents-begin',
142 `:contents-end' and `:post-blank' keywords.
144 Assume point is at beginning or end of the block."
146 (let* ((case-fold-search t
)
150 (concat "^[ \t]*#\\+begin_center") nil t
)
151 (org-element-collect-affiliated-keywords)))
152 (begin (car keywords
))
153 (contents-begin (progn (forward-line) (point)))
154 (hidden (org-truely-invisible-p))
155 (contents-end (progn (re-search-forward
156 (concat "^[ \t]*#\\+end_center") nil t
)
158 (pos-before-blank (progn (forward-line) (point)))
159 (end (progn (org-skip-whitespace)
160 (if (eobp) (point) (point-at-bol)))))
165 :contents-begin
,contents-begin
166 :contents-end
,contents-end
167 :post-blank
,(count-lines pos-before-blank end
)
168 ,@(cadr keywords
))))))
170 (defun org-element-center-block-interpreter (center-block contents
)
171 "Interpret CENTER-BLOCK element as Org syntax.
172 CONTENTS is the contents of the element."
173 (format "#+begin_center\n%s#+end_center" contents
))
176 (defun org-element-drawer-parser ()
179 Return a list whose car is `drawer' and cdr is a plist containing
180 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
181 `:contents-end' and `:post-blank' keywords.
183 Assume point is at beginning of drawer."
185 (let* ((case-fold-search t
)
186 (name (progn (looking-at org-drawer-regexp
)
187 (org-match-string-no-properties 1)))
188 (keywords (org-element-collect-affiliated-keywords))
189 (begin (car keywords
))
190 (contents-begin (progn (forward-line) (point)))
191 (hidden (org-truely-invisible-p))
192 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
194 (pos-before-blank (progn (forward-line) (point)))
195 (end (progn (org-skip-whitespace)
196 (if (eobp) (point) (point-at-bol)))))
202 :contents-begin
,contents-begin
203 :contents-end
,contents-end
204 :post-blank
,(count-lines pos-before-blank end
)
205 ,@(cadr keywords
))))))
207 (defun org-element-drawer-interpreter (drawer contents
)
208 "Interpret DRAWER element as Org syntax.
209 CONTENTS is the contents of the element."
210 (format ":%s:\n%s:END:"
211 (org-element-get-property :drawer-name drawer
)
215 (defun org-element-dynamic-block-parser ()
216 "Parse a dynamic block.
218 Return a list whose car is `dynamic-block' and cdr is a plist
219 containing `:block-name', `:begin', `:end', `:hiddenp',
220 `:contents-begin', `:contents-end', `:arguments' and
221 `:post-blank' keywords.
223 Assume point is at beginning of dynamic block."
225 (let* ((case-fold-search t
)
226 (name (progn (looking-at org-dblock-start-re
)
227 (org-match-string-no-properties 1)))
228 (arguments (org-match-string-no-properties 3))
229 (keywords (org-element-collect-affiliated-keywords))
230 (begin (car keywords
))
231 (contents-begin (progn (forward-line) (point)))
232 (hidden (org-truely-invisible-p))
233 (contents-end (progn (re-search-forward org-dblock-end-re nil t
)
235 (pos-before-blank (progn (forward-line) (point)))
236 (end (progn (org-skip-whitespace)
237 (if (eobp) (point) (point-at-bol)))))
242 :arguments
,arguments
244 :contents-begin
,contents-begin
245 :contents-end
,contents-end
246 :post-blank
,(count-lines pos-before-blank end
)
247 ,@(cadr keywords
))))))
249 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
250 "Interpret DYNAMIC-BLOCK element as Org syntax.
251 CONTENTS is the contents of the element."
252 (format "#+BEGIN: %s%s\n%s#+END:"
253 (org-element-get-property :block-name dynamic-block
)
254 (let ((args (org-element-get-property :arguments dynamic-block
)))
255 (and arg
(concat " " args
)))
258 ;;;; Footnote Definition
260 (defun org-element-footnote-definition-parser ()
261 "Parse a footnote definition.
263 Return a list whose car is `footnote-definition' and cdr is
264 a plist containing `:label', `:begin' `:end', `:contents-begin',
265 `:contents-end' and `:post-blank' keywords."
267 (let* ((f-def (org-footnote-at-definition-p))
269 (keywords (progn (goto-char (nth 1 f-def
))
270 (org-element-collect-affiliated-keywords)))
271 (begin (car keywords
))
272 (contents-begin (progn (looking-at (concat "\\[" label
"\\]"))
273 (goto-char (match-end 0))
274 (org-skip-whitespace)
276 (end (goto-char (nth 2 f-def
)))
277 (contents-end (progn (skip-chars-backward " \r\t\n")
280 (list 'footnote-definition
284 :contents-begin
,contents-begin
285 :contents-end
,contents-end
286 :post-blank
,(count-lines contents-end end
)
287 ,@(cadr keywords
))))))
289 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
290 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
291 CONTENTS is the contents of the footnote-definition."
292 (concat (format "[%s]" (org-element-get-property :label footnote-definition
))
298 (defun org-element-headline-parser ()
301 Return a list whose car is `headline' and cdr is a plist
302 containing `:raw-value', `:title', `:begin', `:end',
303 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
304 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
305 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
306 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
309 The plist also contains any property set in the property drawer,
310 with its name in lowercase, the underscores replaced with hyphens
311 and colons at the beginning (i.e. `:custom-id').
313 Assume point is at beginning of the headline."
315 (let* ((components (org-heading-components))
316 (level (nth 1 components
))
317 (todo (nth 2 components
))
319 (if (member todo org-done-keywords
) 'done
'todo
)))
320 (tags (nth 5 components
))
321 (raw-value (nth 4 components
))
322 (quotedp (string-match (format "^%s +" org-quote-string
) raw-value
))
323 (commentedp (string-match
324 (format "^%s +" org-comment-string
) raw-value
))
326 (string-match (format ":%s:" org-archive-tag
) tags
)))
327 (footnote-section-p (and org-footnote-section
328 (string= org-footnote-section raw-value
)))
329 (standard-props (let (plist)
332 (let ((p-name (downcase (car p
))))
333 (while (string-match "_" p-name
)
335 (replace-match "-" nil nil p-name
)))
336 (setq p-name
(intern (concat ":" p-name
)))
338 (plist-put plist p-name
(cdr p
)))))
339 (org-entry-properties nil
'standard
))
341 (time-props (org-entry-properties nil
'special
"CLOCK"))
342 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
343 (deadline (cdr (assoc "DEADLINE" time-props
)))
344 (clock (cdr (assoc "CLOCK" time-props
)))
345 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
347 (pos-after-head (save-excursion (forward-line) (point)))
348 (contents-begin (save-excursion (forward-line)
349 (org-skip-whitespace)
350 (if (eobp) (point) (point-at-bol))))
351 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
352 (end (progn (goto-char (org-end-of-subtree t t
))))
353 (contents-end (progn (skip-chars-backward " \r\t\n")
357 ;; Clean RAW-VALUE from any quote or comment string.
358 (when (or quotedp commentedp
)
360 (replace-regexp-in-string
361 (concat "\\(" org-quote-string
"\\|" org-comment-string
"\\) +")
364 ;; Clean TAGS from archive tag, if any.
367 (and (not (string= tags
(format ":%s:" org-archive-tag
)))
368 (replace-regexp-in-string
369 (concat org-archive-tag
":") "" tags
)))
370 (when (string= tags
":") (setq tags nil
)))
372 (setq title
(org-element-parse-secondary-string
374 (cdr (assq 'headline org-element-string-restrictions
))))
376 `(:raw-value
,raw-value
380 :pre-blank
,(count-lines pos-after-head contents-begin
)
382 :contents-begin
,contents-begin
383 :contents-end
,contents-end
385 :priority
,(nth 3 components
)
388 :todo-type
,todo-type
389 :scheduled
,scheduled
391 :timestamp
,timestamp
393 :post-blank
,(count-lines contents-end end
)
394 :footnote-section-p
,footnote-section-p
395 :archivedp
,archivedp
396 :commentedp
,commentedp
398 ,@standard-props
)))))
400 (defun org-element-headline-interpreter (headline contents
)
401 "Interpret HEADLINE element as Org syntax.
402 CONTENTS is the contents of the element."
403 (let* ((level (org-element-get-property :level headline
))
404 (todo (org-element-get-property :todo-keyword headline
))
405 (priority (org-element-get-property :priority headline
))
406 (title (org-element-get-property :raw-value headline
))
407 (tags (let ((tag-string (org-element-get-property :tags headline
))
408 (archivedp (org-element-get-property :archivedp headline
)))
410 ((and (not tag-string
) archivedp
)
411 (format ":%s:" org-archive-tag
))
412 (archivedp (concat ":" org-archive-tag tag-string
))
414 (commentedp (org-element-get-property :commentedp headline
))
415 (quotedp (org-element-get-property :quotedp headline
))
416 (pre-blank (org-element-get-property :pre-blank headline
))
417 (heading (concat (make-string level ?
*)
418 (and todo
(concat " " todo
))
419 (and quotedp
(concat " " org-quote-string
))
420 (and commentedp
(concat " " org-comment-string
))
421 (and priority
(concat " " priority
))
422 (cond ((and org-footnote-section
423 (org-element-get-property
424 :footnote-section-p headline
))
425 (concat " " org-footnote-section
))
426 (title (concat " " title
)))))
429 (let ((tags-len (length tags
)))
432 ((zerop org-tags-column
) (1+ tags-len
))
433 ((< org-tags-column
0)
434 (max (- (+ org-tags-column
(length heading
)))
436 (t (max (+ (- org-tags-column
(length heading
))
438 (1+ tags-len
)))))))))
439 (concat heading
(and tags
(format tags-fmt tags
))
440 (make-string (1+ pre-blank
) 10)
444 (defun org-element-inlinetask-parser ()
445 "Parse an inline task.
447 Return a list whose car is `inlinetask' and cdr is a plist
448 containing `:raw-value', `:title', `:begin', `:end', `:hiddenp',
449 `:contents-begin' and `:contents-end', `:level', `:priority',
450 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
451 `:scheduled', `:deadline', `:timestamp', `:clock' and
452 `:post-blank' keywords.
454 The plist also contains any property set in the property drawer,
455 with its name in lowercase, the underscores replaced with hyphens
456 and colons at the beginning (i.e. `:custom-id').
458 Assume point is at beginning of the inline task."
460 (let* ((keywords (org-element-collect-affiliated-keywords))
461 (begin (car keywords
))
462 (components (org-heading-components))
463 (todo (nth 2 components
))
465 (if (member todo org-done-keywords
) 'done
'todo
)))
466 (raw-value (nth 4 components
))
467 (standard-props (let (plist)
470 (let ((p-name (downcase (car p
))))
471 (while (string-match "_" p-name
)
473 (replace-match "-" nil nil p-name
)))
474 (setq p-name
(intern (concat ":" p-name
)))
476 (plist-put plist p-name
(cdr p
)))))
477 (org-entry-properties nil
'standard
))
479 (time-props (org-entry-properties nil
'special
"CLOCK"))
480 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
481 (deadline (cdr (assoc "DEADLINE" time-props
)))
482 (clock (cdr (assoc "CLOCK" time-props
)))
483 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
484 (title (org-element-parse-secondary-string
486 (cdr (assq 'inlinetask org-element-string-restrictions
))))
487 (contents-begin (save-excursion (forward-line) (point)))
488 (hidden (org-truely-invisible-p))
489 (pos-before-blank (org-inlinetask-goto-end))
490 ;; In the case of a single line task, CONTENTS-BEGIN and
491 ;; CONTENTS-END might overlap.
492 (contents-end (max contents-begin
493 (save-excursion (forward-line -
1) (point))))
494 (end (progn (org-skip-whitespace)
495 (if (eobp) (point) (point-at-bol)))))
497 `(:raw-value
,raw-value
501 :hiddenp
,(and (> contents-end contents-begin
) hidden
)
502 :contents-begin
,contents-begin
503 :contents-end
,contents-end
504 :level
,(nth 1 components
)
505 :priority
,(nth 3 components
)
506 :tags
,(nth 5 components
)
508 :todo-type
,todo-type
509 :scheduled
,scheduled
511 :timestamp
,timestamp
513 :post-blank
,(count-lines pos-before-blank end
)
515 ,@(cadr keywords
))))))
517 (defun org-element-inlinetask-interpreter (inlinetask contents
)
518 "Interpret INLINETASK element as Org syntax.
519 CONTENTS is the contents of inlinetask."
520 (let* ((level (org-element-get-property :level inlinetask
))
521 (todo (org-element-get-property :todo-keyword inlinetask
))
522 (priority (org-element-get-property :priority inlinetask
))
523 (title (org-element-get-property :raw-value inlinetask
))
524 (tags (org-element-get-property :tags inlinetask
))
525 (task (concat (make-string level ?
*)
526 (and todo
(concat " " todo
))
527 (and priority
(concat " " priority
))
528 (and title
(concat " " title
))))
533 ((zerop org-tags-column
) 1)
534 ((< 0 org-tags-column
)
535 (max (+ org-tags-column
539 (t (max (- org-tags-column
(length inlinetask
))
541 (concat inlinetask
(and tags
(format tags-fmt tags
) "\n" contents
))))
544 (defun org-element-item-parser (struct)
547 STRUCT is the structure of the plain list.
549 Return a list whose car is `item' and cdr is a plist containing
550 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
551 `:checkbox', `:counter', `:tag', `:raw-tag', `:structure',
552 `:hiddenp' and `:post-blank' keywords.
554 Assume point is at the beginning of the item."
557 (let* ((begin (point))
558 (bullet (org-list-get-bullet (point) struct
))
559 (checkbox (let ((box (org-list-get-checkbox begin struct
)))
560 (cond ((equal "[ ]" box
) 'off
)
561 ((equal "[X]" box
) 'on
)
562 ((equal "[-]" box
) 'trans
))))
563 (counter (let ((c (org-list-get-counter begin struct
)))
566 ((string-match "[A-Za-z]" c
)
567 (- (string-to-char (upcase (match-string 0 c
)))
569 ((string-match "[0-9]+" c
)
570 (string-to-number (match-string 0 c
))))))
571 (raw-tag (org-list-get-tag begin struct
))
573 (org-element-parse-secondary-string
575 (cdr (assq 'item org-element-string-restrictions
)))))
576 (end (org-list-get-item-end begin struct
))
577 (contents-begin (progn (looking-at org-list-full-item-re
)
578 (goto-char (match-end 0))
579 (org-skip-whitespace)
583 (hidden (progn (forward-line)
584 (and (not (= (point) end
))
585 (org-truely-invisible-p))))
586 (contents-end (progn (goto-char end
)
587 (skip-chars-backward " \r\t\n")
594 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
595 ;; up in the case of an empty item separated
596 ;; from the next by a blank line. Thus, ensure
597 ;; the former is always the smallest of two.
598 :contents-begin
,(min contents-begin contents-end
)
599 :contents-end
,(max contents-begin contents-end
)
606 :post-blank
,(count-lines contents-end end
))))))
608 (defun org-element-item-interpreter (item contents
)
609 "Interpret ITEM element as Org syntax.
610 CONTENTS is the contents of the element."
611 (let* ((bullet (org-element-get-property :bullet item
))
612 (checkbox (org-element-get-property :checkbox item
))
613 (counter (org-element-get-property :counter item
))
614 (tag (org-element-get-property :raw-tag item
))
615 ;; Compute indentation.
616 (ind (make-string (length bullet
) 32)))
620 (when (and org-list-two-spaces-after-bullet-regexp
621 (string-match org-list-two-spaces-after-bullet-regexp bullet
))
623 (and counter
(format "[@%d] " counter
))
625 ((eq checkbox
'on
) "[X] ")
626 ((eq checkbox
'off
) "[ ] ")
627 ((eq checkbox
'trans
) "[-] "))
628 (and tag
(format "%s :: " tag
))
630 (replace-regexp-in-string
631 "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))))
634 (defun org-element-plain-list-parser (&optional structure
)
637 Return a list whose car is `plain-list' and cdr is a plist
638 containing `:type', `:begin', `:end', `:contents-begin' and
639 `:contents-end', `:level', `:structure' and `:post-blank'
642 Assume point is at one of the list items."
644 (let* ((struct (or structure
(org-list-struct)))
645 (prevs (org-list-prevs-alist struct
))
646 (parents (org-list-parents-alist struct
))
647 (type (org-list-get-list-type (point) struct prevs
))
648 (contents-begin (goto-char
649 (org-list-get-list-begin (point) struct prevs
)))
650 (keywords (org-element-collect-affiliated-keywords))
651 (begin (car keywords
))
652 (contents-end (goto-char
653 (org-list-get-list-end (point) struct prevs
)))
654 (end (save-excursion (org-skip-whitespace)
655 (if (eobp) (point) (point-at-bol))))
658 (let ((item contents-begin
))
661 (org-list-get-list-begin item struct prevs
)
664 ;; Blank lines below list belong to the top-level list only.
666 (setq end
(min (org-list-get-bottom-point struct
)
667 (progn (org-skip-whitespace)
668 (if (eobp) (point) (point-at-bol))))))
674 :contents-begin
,contents-begin
675 :contents-end
,contents-end
678 :post-blank
,(count-lines contents-end end
)
679 ,@(cadr keywords
))))))
681 (defun org-element-plain-list-interpreter (plain-list contents
)
682 "Interpret PLAIN-LIST element as Org syntax.
683 CONTENTS is the contents of the element."
687 (defun org-element-quote-block-parser ()
688 "Parse a quote block.
690 Return a list whose car is `quote-block' and cdr is a plist
691 containing `:begin', `:end', `:hiddenp', `:contents-begin',
692 `:contents-end' and `:post-blank' keywords.
694 Assume point is at beginning or end of the block."
696 (let* ((case-fold-search t
)
700 (concat "^[ \t]*#\\+begin_quote") nil t
)
701 (org-element-collect-affiliated-keywords)))
702 (begin (car keywords
))
703 (contents-begin (progn (forward-line) (point)))
704 (hidden (org-truely-invisible-p))
705 (contents-end (progn (re-search-forward
706 (concat "^[ \t]*#\\+end_quote") nil t
)
708 (pos-before-blank (progn (forward-line) (point)))
709 (end (progn (org-skip-whitespace)
710 (if (eobp) (point) (point-at-bol)))))
715 :contents-begin
,contents-begin
716 :contents-end
,contents-end
717 :post-blank
,(count-lines pos-before-blank end
)
718 ,@(cadr keywords
))))))
720 (defun org-element-quote-block-interpreter (quote-block contents
)
721 "Interpret QUOTE-BLOCK element as Org syntax.
722 CONTENTS is the contents of the element."
723 (format "#+begin_quote\n%s#+end_quote" contents
))
728 (defun org-element-section-parser ()
731 Return a list whose car is `section' and cdr is a plist
732 containing `:begin', `:end', `:contents-begin', `contents-end'
733 and `:post-blank' keywords."
735 ;; Beginning of section is the beginning of the first non-blank
736 ;; line after previous headline.
737 (let ((begin (save-excursion
738 (org-with-limited-levels (outline-previous-heading))
740 (forward-line) (org-skip-whitespace) (point-at-bol))))
741 (end (progn (org-with-limited-levels (outline-next-heading))
743 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
749 :contents-begin
,begin
750 :contents-end
,pos-before-blank
751 :post-blank
,(count-lines pos-before-blank end
))))))
753 (defun org-element-section-interpreter (section contents
)
754 "Interpret SECTION element as Org syntax.
755 CONTENTS is the contents of the element."
760 (defun org-element-special-block-parser ()
761 "Parse a special block.
763 Return a list whose car is `special-block' and cdr is a plist
764 containing `:type', `:begin', `:end', `:hiddenp',
765 `:contents-begin', `:contents-end' and `:post-blank' keywords.
767 Assume point is at beginning or end of the block."
769 (let* ((case-fold-search t
)
770 (type (progn (looking-at
771 "[ \t]*#\\+\\(?:begin\\|end\\)_\\([-A-Za-z0-9]+\\)")
772 (org-match-string-no-properties 1)))
776 (concat "^[ \t]*#\\+begin_" type
) nil t
)
777 (org-element-collect-affiliated-keywords)))
778 (begin (car keywords
))
779 (contents-begin (progn (forward-line) (point)))
780 (hidden (org-truely-invisible-p))
781 (contents-end (progn (re-search-forward
782 (concat "^[ \t]*#\\+end_" type
) nil t
)
784 (pos-before-blank (progn (forward-line) (point)))
785 (end (progn (org-skip-whitespace)
786 (if (eobp) (point) (point-at-bol)))))
792 :contents-begin
,contents-begin
793 :contents-end
,contents-end
794 :post-blank
,(count-lines pos-before-blank end
)
795 ,@(cadr keywords
))))))
797 (defun org-element-special-block-interpreter (special-block contents
)
798 "Interpret SPECIAL-BLOCK element as Org syntax.
799 CONTENTS is the contents of the element."
800 (let ((block-type (org-element-get-property :type special-block
)))
801 (format "#+begin_%s\n%s#+end_%s" block-type contents block-type
)))
807 ;; For each element, a parser and an interpreter are also defined.
808 ;; Both follow the same naming convention used for greater elements.
810 ;; Also, as for greater elements, adding a new element type is done
811 ;; through the following steps: implement a parser and an interpreter,
812 ;; tweak `org-element-guess-type' so that it recognizes the new type
813 ;; and add that new type to `org-element-all-elements'.
815 ;; As a special case, when the newly defined type is a block type,
816 ;; `org-element-non-recursive-block-alist' has to be modified
821 (defun org-element-babel-call-parser ()
824 Return a list whose car is `babel-call' and cdr is a plist
825 containing `:begin', `:end', `:info' and `:post-blank' as
828 (let ((info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
829 (org-babel-lob-get-info)))
831 (pos-before-blank (progn (forward-line) (point)))
832 (end (progn (org-skip-whitespace)
833 (if (eobp) (point) (point-at-bol)))))
838 :post-blank
,(count-lines pos-before-blank end
))))))
840 (defun org-element-babel-call-interpreter (inline-babel-call contents
)
841 "Interpret INLINE-BABEL-CALL object as Org syntax.
843 (let* ((babel-info (org-element-get-property :info inline-babel-call
))
844 (main-source (car babel-info
))
845 (post-options (nth 1 babel-info
)))
847 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
848 ;; Remove redundant square brackets.
850 (match-string 1 main-source
) nil nil main-source
)
852 (and post-options
(format "[%s]" post-options
)))))
855 (defun org-element-comment-parser ()
858 Return a list whose car is `comment' and cdr is a plist
859 containing `:begin', `:end', `:value' and `:post-blank'
861 (let ((comment-re "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)")
862 beg-coms begin end value pos-before-blank keywords
)
864 ;; Move to the beginning of comments.
866 (while (and (not (bobp)) (looking-at comment-re
))
868 (unless (looking-at comment-re
) (forward-line 1)))
869 (setq beg-coms
(point))
870 ;; Get affiliated keywords, if any.
871 (setq keywords
(org-element-collect-affiliated-keywords))
872 ;; Store true beginning of element.
873 (setq begin
(car keywords
))
874 ;; Get ending of comments. If point is in a list, ensure to not
875 ;; get outside of it.
876 (let* ((itemp (org-in-item-p))
878 (org-list-get-bottom-point
879 (save-excursion (goto-char itemp
) (org-list-struct)))
881 (while (and (looking-at comment-re
) (< (point) max-pos
))
883 (setq pos-before-blank
(point))
884 ;; Find position after blank.
885 (org-skip-whitespace)
886 (setq end
(if (eobp) (point) (point-at-bol)))
888 (setq value
(buffer-substring-no-properties beg-coms pos-before-blank
)))
893 :post-blank
,(count-lines pos-before-blank end
)
894 ,@(cadr keywords
)))))
896 (defun org-element-comment-interpreter (comment contents
)
897 "Interpret COMMENT element as Org syntax.
899 (org-element-get-property :value comment
))
902 (defun org-element-comment-block-parser ()
903 "Parse an export block.
905 Return a list whose car is `comment-block' and cdr is a plist
906 containing `:begin', `:end', `:hiddenp', `:value' and
907 `:post-blank' keywords."
910 (let* ((case-fold-search t
)
912 (re-search-backward "^[ \t]*#\\+begin_comment" nil t
)
913 (org-element-collect-affiliated-keywords)))
914 (begin (car keywords
))
915 (contents-begin (progn (forward-line) (point)))
916 (hidden (org-truely-invisible-p))
917 (contents-end (progn (re-search-forward
918 "^[ \t]*#\\+end_comment" nil t
)
920 (pos-before-blank (progn (forward-line) (point)))
921 (end (progn (org-skip-whitespace)
922 (if (eobp) (point) (point-at-bol))))
923 (value (buffer-substring-no-properties contents-begin contents-end
)))
929 :post-blank
,(count-lines pos-before-blank end
)
930 ,@(cadr keywords
))))))
932 (defun org-element-comment-block-interpreter (comment-block contents
)
933 "Interpret COMMENT-BLOCK element as Org syntax.
935 (concat "#+begin_comment\n"
936 (org-remove-indentation
937 (org-element-get-property :value comment-block
))
941 (defun org-element-example-block-parser ()
942 "Parse an example block.
944 Return a list whose car is `example' and cdr is a plist
945 containing `:begin', `:end', `:options', `:hiddenp', `:value' and
946 `:post-blank' keywords."
949 (let* ((case-fold-search t
)
952 "^[ \t]*#\\+begin_example\\(?: +\\(.*\\)\\)?" nil t
)
953 (org-match-string-no-properties 1)))
954 (keywords (org-element-collect-affiliated-keywords))
955 (begin (car keywords
))
956 (contents-begin (progn (forward-line) (point)))
957 (hidden (org-truely-invisible-p))
959 (re-search-forward "^[ \t]*#\\+end_example" nil t
)
961 (value (buffer-substring-no-properties contents-begin contents-end
))
962 (pos-before-blank (progn (forward-line) (point)))
963 (end (progn (org-skip-whitespace)
964 (if (eobp) (point) (point-at-bol)))))
971 :post-blank
,(count-lines pos-before-blank end
)
972 ,@(cadr keywords
))))))
974 (defun org-element-example-block-interpreter (example-block contents
)
975 "Interpret EXAMPLE-BLOCK element as Org syntax.
977 (let ((options (org-element-get-property :options example-block
)))
978 (concat "#+begin_example" (and options
(concat " " options
)) "\n"
979 (org-remove-indentation
980 (org-element-get-property :value example-block
))
984 (defun org-element-export-block-parser ()
985 "Parse an export block.
987 Return a list whose car is `export-block' and cdr is a plist
988 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
989 `:post-blank' keywords."
992 (let* ((case-fold-search t
)
994 (type (progn (re-search-backward
995 (concat "[ \t]*#\\+begin_"
996 (org-re "\\([[:alnum:]]+\\)")))
997 (downcase (org-match-string-no-properties 1))))
998 (keywords (org-element-collect-affiliated-keywords))
999 (begin (car keywords
))
1000 (contents-begin (progn (forward-line) (point)))
1001 (hidden (org-truely-invisible-p))
1002 (contents-end (progn (re-search-forward
1003 (concat "^[ \t]*#\\+end_" type
) nil t
)
1005 (pos-before-blank (progn (forward-line) (point)))
1006 (end (progn (org-skip-whitespace)
1007 (if (eobp) (point) (point-at-bol))))
1008 (value (buffer-substring-no-properties contents-begin contents-end
)))
1015 :post-blank
,(count-lines pos-before-blank end
)
1016 ,@(cadr keywords
))))))
1018 (defun org-element-export-block-interpreter (export-block contents
)
1019 "Interpret EXPORT-BLOCK element as Org syntax.
1021 (let ((type (org-element-get-property :type export-block
)))
1022 (concat (format "#+begin_%s\n" type
)
1023 (org-element-get-property :value export-block
)
1024 (format "#+end_%s" type
))))
1027 (defun org-element-fixed-width-parser ()
1028 "Parse a fixed-width section.
1030 Return a list whose car is `fixed-width' and cdr is a plist
1031 containing `:begin', `:end', `:value' and `:post-blank'
1033 (let ((fixed-re "[ \t]*:\\( \\|$\\)")
1034 beg-area begin end value pos-before-blank keywords
)
1036 ;; Move to the beginning of the fixed-width area.
1038 (while (and (not (bobp)) (looking-at fixed-re
))
1040 (unless (looking-at fixed-re
) (forward-line 1)))
1041 (setq beg-area
(point))
1042 ;; Get affiliated keywords, if any.
1043 (setq keywords
(org-element-collect-affiliated-keywords))
1044 ;; Store true beginning of element.
1045 (setq begin
(car keywords
))
1046 ;; Get ending of fixed-width area. If point is in a list,
1047 ;; ensure to not get outside of it.
1048 (let* ((itemp (org-in-item-p))
1050 (org-list-get-bottom-point
1051 (save-excursion (goto-char itemp
) (org-list-struct)))
1053 (while (and (looking-at fixed-re
) (< (point) max-pos
))
1055 (setq pos-before-blank
(point))
1056 ;; Find position after blank
1057 (org-skip-whitespace)
1058 (setq end
(if (eobp) (point) (point-at-bol)))
1060 (setq value
(buffer-substring-no-properties beg-area pos-before-blank
)))
1065 :post-blank
,(count-lines pos-before-blank end
)
1066 ,@(cadr keywords
)))))
1068 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1069 "Interpret FIXED-WIDTH element as Org syntax.
1071 (org-remove-indentation (org-element-get-property :value fixed-width
)))
1073 ;;;; Horizontal Rule
1074 (defun org-element-horizontal-rule-parser ()
1075 "Parse an horizontal rule.
1077 Return a list whose car is `horizontal-rule' and cdr is
1078 a plist containing `:begin', `:end' and `:post-blank'
1081 (let* ((keywords (org-element-collect-affiliated-keywords))
1082 (begin (car keywords
))
1083 (post-hr (progn (forward-line) (point)))
1084 (end (progn (org-skip-whitespace)
1085 (if (eobp) (point) (point-at-bol)))))
1086 (list 'horizontal-rule
1089 :post-blank
,(count-lines post-hr end
)
1090 ,@(cadr keywords
))))))
1092 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1093 "Interpret HORIZONTAL-RULE element as Org syntax.
1098 (defun org-element-keyword-parser ()
1099 "Parse a keyword at point.
1101 Return a list whose car is `keyword' and cdr is a plist
1102 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1105 (let* ((begin (point))
1106 (key (progn (looking-at
1107 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1108 (org-match-string-no-properties 1)))
1109 (value (org-trim (buffer-substring-no-properties
1110 (match-end 0) (point-at-eol))))
1111 (pos-before-blank (progn (forward-line) (point)))
1112 (end (progn (org-skip-whitespace)
1113 (if (eobp) (point) (point-at-bol)))))
1119 :post-blank
,(count-lines pos-before-blank end
))))))
1121 (defun org-element-keyword-interpreter (keyword contents
)
1122 "Interpret KEYWORD element as Org syntax.
1125 (org-element-get-property :key keyword
)
1126 (org-element-get-property :value keyword
)))
1128 ;;;; Latex Environment
1129 (defun org-element-latex-environment-parser ()
1130 "Parse a LaTeX environment.
1132 Return a list whose car is `latex-environment' and cdr is a plist
1133 containing `:begin', `:end', `:value' and `:post-blank' keywords."
1136 (let* ((case-fold-search t
)
1137 (contents-begin (re-search-backward "^[ \t]*\\\\begin" nil t
))
1138 (keywords (org-element-collect-affiliated-keywords))
1139 (begin (car keywords
))
1140 (contents-end (progn (re-search-forward "^[ \t]*\\\\end")
1143 (value (buffer-substring-no-properties contents-begin contents-end
))
1144 (end (progn (org-skip-whitespace)
1145 (if (eobp) (point) (point-at-bol)))))
1146 (list 'latex-environment
1150 :post-blank
,(count-lines contents-end end
)
1151 ,@(cadr keywords
))))))
1153 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1154 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1156 (org-element-get-property :value latex-environment
))
1159 (defun org-element-paragraph-parser ()
1162 Return a list whose car is `paragraph' and cdr is a plist
1163 containing `:begin', `:end', `:contents-begin' and
1164 `:contents-end' and `:post-blank' keywords.
1166 Assume point is at the beginning of the paragraph."
1168 (let* ((contents-begin (point))
1169 (keywords (org-element-collect-affiliated-keywords))
1170 (begin (car keywords
))
1171 (contents-end (progn
1173 (if (re-search-forward
1174 org-element-paragraph-separate nil
'm
)
1175 (progn (forward-line -
1) (end-of-line) (point))
1177 (pos-before-blank (progn (forward-line) (point)))
1178 (end (progn (org-skip-whitespace)
1179 (if (eobp) (point) (point-at-bol)))))
1183 :contents-begin
,contents-begin
1184 :contents-end
,contents-end
1185 :post-blank
,(count-lines pos-before-blank end
)
1186 ,@(cadr keywords
))))))
1188 (defun org-element-paragraph-interpreter (paragraph contents
)
1189 "Interpret PARAGRAPH element as Org syntax.
1190 CONTENTS is the contents of the element."
1193 ;;;; Property Drawer
1194 (defun org-element-property-drawer-parser ()
1195 "Parse a property drawer.
1197 Return a list whose car is `property-drawer' and cdr is a plist
1198 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1199 `:contents-end', `:properties' and `:post-blank' keywords."
1201 (let ((case-fold-search t
)
1202 (begin (progn (end-of-line)
1203 (re-search-backward org-property-start-re
)
1204 (match-beginning 0)))
1205 (contents-begin (progn (forward-line) (point)))
1206 (hidden (org-truely-invisible-p))
1207 (properties (let (val)
1208 (while (not (looking-at "^[ \t]*:END:"))
1211 "[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):"))
1212 (push (cons (match-string 1)
1215 (match-end 0) (point-at-eol))))
1219 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
1221 (pos-before-blank (progn (forward-line) (point)))
1222 (end (progn (org-skip-whitespace)
1223 (if (eobp) (point) (point-at-bol)))))
1224 (list 'property-drawer
1228 :properties
,properties
1229 :post-blank
,(count-lines pos-before-blank end
))))))
1231 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1232 "Interpret PROPERTY-DRAWER element as Org syntax.
1234 (let ((props (org-element-get-property :properties property-drawer
)))
1237 (mapconcat (lambda (p)
1238 (format org-property-format
(format ":%s:" (car p
)) (cdr p
)))
1239 (nreverse props
) "\n")
1243 (defun org-element-quote-section-parser ()
1244 "Parse a quote section.
1246 Return a list whose car is `quote-section' and cdr is a plist
1247 containing `:begin', `:end', `:value' and `:post-blank'
1250 (let* ((begin (progn (org-back-to-heading t
)
1252 (org-skip-whitespace)
1254 (end (progn (org-with-limited-levels (outline-next-heading))
1256 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1259 (value (unless (= begin end
)
1260 (buffer-substring-no-properties begin pos-before-blank
))))
1261 (list 'quote-section
1265 :post-blank
,(if value
1266 (count-lines pos-before-blank end
)
1269 (defun org-element-quote-section-interpreter (quote-section contents
)
1270 "Interpret QUOTE-SECTION element as Org syntax.
1272 (org-element-get-property :value quote-section
))
1275 (defun org-element-src-block-parser ()
1278 Return a list whose car is `src-block' and cdr is a plist
1279 containing `:language', `:switches', `:parameters', `:begin',
1280 `:end', `:hiddenp', `:contents-begin', `:contents-end', `:value'
1281 and `:post-blank' keywords."
1284 (let* ((case-fold-search t
)
1285 ;; Get position at beginning of block.
1288 (concat "^[ \t]*#\\+begin_src"
1289 "\\(?: +\\(\\S-+\\)\\)?" ; language
1290 "\\(\\(?: +[-+][A-Za-z]\\)*\\)" ; switches
1291 "\\(.*\\)[ \t]*$") ; arguments
1293 ;; Get language as a string.
1294 (language (org-match-string-no-properties 1))
1296 (switches (org-match-string-no-properties 2))
1298 (parameters (org-trim (org-match-string-no-properties 3)))
1299 ;; Get affiliated keywords.
1300 (keywords (org-element-collect-affiliated-keywords))
1301 ;; Get beginning position.
1302 (begin (car keywords
))
1303 ;; Get position at end of block.
1304 (contents-end (progn (re-search-forward "^[ \t]*#\\+end_src" nil t
)
1308 (value (buffer-substring-no-properties
1309 (save-excursion (goto-char contents-begin
)
1312 (match-beginning 0)))
1313 ;; Get position after ending blank lines.
1314 (end (progn (org-skip-whitespace)
1315 (if (eobp) (point) (point-at-bol))))
1316 ;; Get visibility status.
1317 (hidden (progn (goto-char contents-begin
)
1319 (org-truely-invisible-p))))
1321 `(:language
,language
1323 :parameters
,parameters
1328 :post-blank
,(count-lines contents-end end
)
1329 ,@(cadr keywords
))))))
1331 (defun org-element-src-block-interpreter (src-block contents
)
1332 "Interpret SRC-BLOCK element as Org syntax.
1334 (let ((lang (org-element-get-property :language src-block
))
1335 (switches (org-element-get-property :switches src-block
))
1336 (params (org-element-get-property :parameters src-block
))
1337 (value (let ((val (org-element-get-property :value src-block
)))
1339 (org-src-preserve-indentation val
)
1340 ((zerop org-edit-src-content-indentation
)
1341 (org-remove-indentation val
))
1343 (let ((ind (make-string
1344 org-edit-src-content-indentation
32)))
1345 (replace-regexp-in-string
1346 "\\(^\\)[ \t]*\\S-" ind
1347 (org-remove-indentation val
) nil nil
1)))))))
1348 (concat (format "#+begin_src%s\n"
1349 (concat (and lang
(concat " " lang
))
1350 (and switches
(concat " " switches
))
1351 (and params
(concat " " params
))))
1356 (defun org-element-table-parser ()
1357 "Parse a table at point.
1359 Return a list whose car is `table' and cdr is a plist containing
1360 `:begin', `:end', `:contents-begin', `:contents-end', `:tblfm',
1361 `:type', `:raw-table' and `:post-blank' keywords."
1363 (let* ((table-begin (goto-char (org-table-begin t
)))
1364 (type (if (org-at-table.el-p
) 'table.el
'org
))
1365 (keywords (org-element-collect-affiliated-keywords))
1366 (begin (car keywords
))
1367 (table-end (goto-char (marker-position (org-table-end t
))))
1368 (tblfm (when (looking-at "[ \t]*#\\+tblfm: +\\(.*\\)[ \t]*")
1369 (prog1 (org-match-string-no-properties 1)
1371 (pos-before-blank (point))
1372 (end (progn (org-skip-whitespace)
1373 (if (eobp) (point) (point-at-bol))))
1374 (raw-table (org-remove-indentation
1375 (buffer-substring-no-properties table-begin table-end
))))
1380 :raw-table
,raw-table
1382 :post-blank
,(count-lines pos-before-blank end
)
1383 ,@(cadr keywords
))))))
1385 (defun org-element-table-interpreter (table contents
)
1386 "Interpret TABLE element as Org syntax.
1388 (org-element-get-property :raw-table table
))
1391 (defun org-element-verse-block-parser ()
1392 "Parse a verse block.
1394 Return a list whose car is `verse-block' and cdr is a plist
1395 containing `:begin', `:end', `:hiddenp', `:raw-value', `:value'
1396 and `:post-blank' keywords.
1398 Assume point is at beginning or end of the block."
1400 (let* ((case-fold-search t
)
1404 (concat "^[ \t]*#\\+begin_verse") nil t
)
1405 (org-element-collect-affiliated-keywords)))
1406 (begin (car keywords
))
1407 (hidden (progn (forward-line) (org-truely-invisible-p)))
1408 (raw-val (buffer-substring-no-properties
1411 (re-search-forward (concat "^[ \t]*#\\+end_verse") nil t
)
1413 (pos-before-blank (progn (forward-line) (point)))
1414 (end (progn (org-skip-whitespace)
1415 (if (eobp) (point) (point-at-bol))))
1416 (value (org-element-parse-secondary-string
1417 (org-remove-indentation raw-val
)
1418 (cdr (assq 'verse org-element-string-restrictions
)))))
1425 :post-blank
,(count-lines pos-before-blank end
)
1426 ,@(cadr keywords
))))))
1429 (defun org-element-verse-block-interpreter (verse-block contents
)
1430 "Interpret VERSE-BLOCK element as Org syntax.
1432 (format "#+begin_verse\n%s#+end_verse"
1433 (org-remove-indentation
1434 (org-element-get-property :raw-value verse-block
))))
1440 ;; Unlike to elements, interstices can be found between objects.
1441 ;; That's why, along with the parser, successor functions are provided
1442 ;; for each object. Some objects share the same successor
1443 ;; (i.e. `emphasis' and `verbatim' objects).
1445 ;; A successor must accept a single argument bounding the search. It
1446 ;; will return either a cons cell whose car is the object's type, as
1447 ;; a symbol, and cdr the position of its next occurrence, or nil.
1449 ;; Successors follow the naming convention:
1450 ;; org-element-NAME-successor, where NAME is the name of the
1451 ;; successor, as defined in `org-element-all-successors'.
1453 ;; Some object types (i.e `emphasis') are recursive. Restrictions on
1454 ;; object types they can contain will be specified in
1455 ;; `org-element-object-restrictions'.
1457 ;; Adding a new type of object is simple. Implement a successor,
1458 ;; a parser, and an interpreter for it, all following the naming
1459 ;; convention. Register successor in `org-element-all-successors',
1460 ;; maybe tweak restrictions about it, and that's it.
1463 (defun org-element-emphasis-parser ()
1464 "Parse text markup object at point.
1466 Return a list whose car is `emphasis' and cdr is a plist with
1467 `:marker', `:begin', `:end', `:contents-begin' and
1468 `:contents-end' and `:post-blank' keywords.
1470 Assume point is at the first emphasis marker."
1472 (unless (bolp) (backward-char 1))
1473 (looking-at org-emph-re
)
1474 (let ((begin (match-beginning 2))
1475 (marker (org-match-string-no-properties 3))
1476 (contents-begin (match-beginning 4))
1477 (contents-end (match-end 4))
1478 (post-blank (progn (goto-char (match-end 2))
1479 (skip-chars-forward " \t")))
1485 :contents-begin
,contents-begin
1486 :contents-end
,contents-end
1487 :post-blank
,post-blank
)))))
1489 (defun org-element-emphasis-interpreter (emphasis contents
)
1490 "Interpret EMPHASIS object as Org syntax.
1491 CONTENTS is the contents of the object."
1492 (let ((marker (org-element-get-property :marker emphasis
)))
1493 (concat marker contents marker
)))
1495 (defun org-element-text-markup-successor (limit)
1496 "Search for the next emphasis or verbatim and return position.
1498 LIMIT bounds the search.
1500 Return value is a cons cell whose car is `emphasis' or
1501 `verbatim' and cdr is beginning position."
1503 (unless (bolp) (backward-char))
1504 (when (re-search-forward org-emph-re limit t
)
1505 (cons (if (nth 4 (assoc (match-string 3) org-emphasis-alist
))
1508 (match-beginning 2)))))
1511 (defun org-element-entity-parser ()
1512 "Parse entity at point.
1514 Return a list whose car is `entity' and cdr a plist with
1515 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1516 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1519 Assume point is at the beginning of the entity."
1521 (looking-at "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1522 (let* ((value (org-entity-get (match-string 1)))
1523 (begin (match-beginning 0))
1524 (bracketsp (string= (match-string 2) "{}"))
1525 (post-blank (progn (goto-char (match-end 1))
1526 (when bracketsp
(forward-char 2))
1527 (skip-chars-forward " \t")))
1530 `(:name
,(car value
)
1531 :latex
,(nth 1 value
)
1532 :latex-math-p
,(nth 2 value
)
1533 :html
,(nth 3 value
)
1534 :ascii
,(nth 4 value
)
1535 :latin1
,(nth 5 value
)
1536 :utf-8
,(nth 6 value
)
1539 :use-brackets-p
,bracketsp
1540 :post-blank
,post-blank
)))))
1542 (defun org-element-entity-interpreter (entity contents
)
1543 "Interpret ENTITY object as Org syntax.
1546 (org-element-get-property :name entity
)
1547 (when (org-element-get-property :use-brackets-p entity
) "{}")))
1549 (defun org-element-latex-or-entity-successor (limit)
1550 "Search for the next latex-fragment or entity object.
1552 LIMIT bounds the search.
1554 Return value is a cons cell whose car is `entity' or
1555 `latex-fragment' and cdr is beginning position."
1557 (let ((matchers (plist-get org-format-latex-options
:matchers
))
1558 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1560 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"))
1561 (when (re-search-forward
1562 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
1566 (goto-char (match-beginning 0))
1567 (if (looking-at entity-re
)
1568 ;; Determine if it's a real entity or a LaTeX command.
1569 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
1570 (match-beginning 0))
1571 ;; No entity nor command: point is at a LaTeX fragment.
1572 ;; Determine its type to get the correct beginning position.
1573 (cons 'latex-fragment
1576 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
1579 (nth 2 (assoc e org-latex-regexps
))))))
1584 (defun org-element-export-snippet-parser ()
1585 "Parse export snippet at point.
1587 Return a list whose car is `export-snippet' and cdr a plist with
1588 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1591 Assume point is at the beginning of the snippet."
1593 (looking-at "@\\([-A-Za-z0-9]+\\){")
1594 (let* ((begin (point))
1595 (back-end (org-match-string-no-properties 1))
1596 (before-blank (progn (goto-char (scan-sexps (1- (match-end 0)) 1))))
1597 (value (buffer-substring-no-properties
1598 (match-end 0) (1- before-blank
)))
1599 (post-blank (skip-chars-forward " \t"))
1601 (list 'export-snippet
1602 `(:back-end
,back-end
1606 :post-blank
,post-blank
)))))
1608 (defun org-element-export-snippet-interpreter (export-snippet contents
)
1609 "Interpret EXPORT-SNIPPET object as Org syntax.
1612 (org-element-get-property :back-end export-snippet
)
1613 (org-element-get-property :value export-snippet
)))
1615 (defun org-element-export-snippet-successor (limit)
1616 "Search for the next export-snippet object.
1618 LIMIT bounds the search.
1620 Return value is a cons cell whose car is `export-snippet' cdr is
1621 its beginning position."
1624 (while (re-search-forward "@[-A-Za-z0-9]+{" limit t
)
1625 (when (let ((end (ignore-errors (scan-sexps (1- (point)) 1))))
1626 (and end
(eq (char-before end
) ?
})))
1627 (throw 'exit
(cons 'export-snippet
(match-beginning 0))))))))
1629 ;;;; Footnote Reference
1631 (defun org-element-footnote-reference-parser ()
1632 "Parse footnote reference at point.
1634 Return a list whose car is `footnote-reference' and cdr a plist
1635 with `:label', `:type', `:definition', `:begin', `:end' and
1636 `:post-blank' as keywords."
1638 (let* ((ref (org-footnote-at-reference-p))
1640 (raw-def (nth 3 ref
))
1641 (inline-def (and raw-def
1642 (org-element-parse-secondary-string raw-def nil
)))
1643 (type (if (nth 3 ref
) 'inline
'standard
))
1645 (post-blank (progn (goto-char (nth 2 ref
))
1646 (skip-chars-forward " \t")))
1648 (list 'footnote-reference
1651 :inline-definition
,inline-def
1654 :post-blank
,post-blank
1655 :raw-definition
,raw-def
)))))
1657 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
1658 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1660 (let ((label (or (org-element-get-property :label footnote-reference
)
1662 (def (let ((raw (org-element-get-property
1663 :raw-definition footnote-reference
)))
1664 (if raw
(concat ":" raw
) ""))))
1665 (format "[%s]" (concat label def
))))
1667 (defun org-element-footnote-reference-successor (limit)
1668 "Search for the next footnote-reference and return beginning
1671 LIMIT bounds the search.
1673 Return value is a cons cell whose car is `footnote-reference' and
1674 cdr is beginning position."
1676 (when (setq fn-ref
(org-footnote-get-next-reference nil nil limit
))
1677 (cons 'footnote-reference
(nth 1 fn-ref
)))))
1680 ;;;; Inline Babel Call
1681 (defun org-element-inline-babel-call-parser ()
1682 "Parse inline babel call at point.
1684 Return a list whose car is `inline-babel-call' and cdr a plist with
1685 `:begin', `:end', `:info' and `:post-blank' as keywords.
1687 Assume point is at the beginning of the babel call."
1689 (unless (bolp) (backward-char))
1690 (looking-at org-babel-inline-lob-one-liner-regexp
)
1691 (let ((info (save-match-data (org-babel-lob-get-info)))
1692 (begin (match-end 1))
1693 (post-blank (progn (goto-char (match-end 0))
1694 (skip-chars-forward " \t")))
1696 (list 'inline-babel-call
1700 :post-blank
,post-blank
)))))
1702 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
1703 "Interpret INLINE-BABEL-CALL object as Org syntax.
1705 (let* ((babel-info (org-element-get-property :info inline-babel-call
))
1706 (main-source (car babel-info
))
1707 (post-options (nth 1 babel-info
)))
1709 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
1710 ;; Remove redundant square brackets.
1712 (match-string 1 main-source
) nil nil main-source
)
1714 (and post-options
(format "[%s]" post-options
)))))
1716 (defun org-element-inline-babel-call-successor (limit)
1717 "Search for the next inline-babel-call and return beginning
1720 LIMIT bounds the search.
1722 Return value is a cons cell whose car is `inline-babel-call' and
1723 cdr is beginning position."
1725 ;; Use a simplified version of
1726 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
1727 (when (re-search-forward
1728 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
1730 (cons 'inline-babel-call
(match-beginning 0)))))
1732 ;;;; Inline Src Block
1733 (defun org-element-inline-src-block-parser ()
1734 "Parse inline source block at point.
1736 Return a list whose car is `inline-src-block' and cdr a plist
1737 with `:begin', `:end', `:language', `:value', `:parameters' and
1738 `:post-blank' as keywords.
1740 Assume point is at the beginning of the inline src block."
1742 (unless (bolp) (backward-char))
1743 (looking-at org-babel-inline-src-block-regexp
)
1744 (let ((begin (match-beginning 1))
1745 (language (org-match-string-no-properties 2))
1746 (parameters (org-match-string-no-properties 4))
1747 (value (org-match-string-no-properties 5))
1748 (post-blank (progn (goto-char (match-end 0))
1749 (skip-chars-forward " \t")))
1751 (list 'inline-src-block
1752 `(:language
,language
1754 :parameters
,parameters
1757 :post-blank
,post-blank
)))))
1761 (defun org-element-inline-src-block-successor (limit)
1762 "Search for the next inline-babel-call and return beginning position.
1764 LIMIT bounds the search.
1766 Return value is a cons cell whose car is `inline-babel-call' and
1767 cdr is beginning position."
1769 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
1770 (cons 'inline-src-block
(match-beginning 1)))))
1773 (defun org-element-latex-fragment-parser ()
1774 "Parse latex fragment at point.
1776 Return a list whose car is `latex-fragment' and cdr a plist with
1777 `:value', `:begin', `:end', and `:post-blank' as keywords.
1779 Assume point is at the beginning of the latex fragment."
1781 (let* ((begin (point))
1785 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
1786 (when (or (looking-at latex-regexp
)
1790 (looking-at latex-regexp
))))
1791 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
1792 (plist-get org-format-latex-options
:matchers
))
1793 ;; None found: it's a macro.
1794 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
1796 (value (match-string-no-properties substring-match
))
1797 (post-blank (progn (goto-char (match-end substring-match
))
1798 (skip-chars-forward " \t")))
1800 (list 'latex-fragment
1804 :post-blank
,post-blank
)))))
1806 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
1807 "Interpret LATEX-FRAGMENT object as Org syntax.
1809 (org-element-get-property :value latex-fragment
))
1812 (defun org-element-line-break-parser ()
1813 "Parse line break at point.
1815 Return a list whose car is `line-break', and cdr a plist with
1816 `:begin', `:end' and `:post-blank' keywords.
1818 Assume point is at the beginning of the line break."
1820 (let* ((begin (point))
1821 (end (progn (end-of-line) (point)))
1822 (post-blank (- (skip-chars-backward " \t")))
1827 :post-blank
,post-blank
)))))
1829 (defun org-element-line-break-interpreter (line-break contents
)
1830 "Interpret LINE-BREAK object as Org syntax.
1832 (org-element-get-property :value line-break
))
1834 (defun org-element-line-break-successor (limit)
1835 "Search for the next statistics cookie and return position.
1837 LIMIT bounds the search.
1839 Return value is a cons cell whose car is `line-break' and cdr is
1840 beginning position."
1842 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
1843 (goto-char (match-beginning 1)))))
1844 ;; A line break can only happen on a non-empty line.
1845 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
1846 (cons 'line-break beg
)))))
1849 (defun org-element-link-parser ()
1850 "Parse link at point.
1852 Return a list whose car is `link' and cdr a plist with `:type',
1853 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
1854 `:contents-end' and `:post-blank' as keywords.
1856 Assume point is at the beginning of the link."
1858 (let ((begin (point))
1859 end contents-begin contents-end link-end post-blank path type
1862 ;; Type 1: text targeted from a radio target.
1863 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
1865 path
(org-match-string-no-properties 0)
1866 contents-begin
(match-beginning 0)
1867 contents-end
(match-end 0)
1868 link-end
(match-end 0)))
1869 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
1870 ((looking-at org-bracket-link-regexp
)
1871 (setq contents-begin
(match-beginning 3)
1872 contents-end
(match-end 3)
1873 link-end
(match-end 0)
1874 ;; RAW-LINK is the original link.
1875 raw-link
(org-match-string-no-properties 1)
1876 link
(org-link-expand-abbrev
1877 (replace-regexp-in-string
1878 " *\n *" " " (org-link-unescape raw-link
) t t
)))
1879 ;; Determine TYPE of link and set PATH accordingly.
1882 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
1883 (setq type
"file" path link
))
1884 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
1885 ((string-match org-link-re-with-space3 link
)
1886 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
1887 ;; Id type: PATH is the id.
1888 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
1889 (setq type
"id" path
(match-string 1 link
)))
1890 ;; Code-ref type: PATH is the name of the reference.
1891 ((string-match "^(\\(.*\\))$" link
)
1892 (setq type
"coderef" path
(match-string 1 link
)))
1893 ;; Custom-id type: PATH is the name of the custom id.
1894 ((= (aref link
0) ?
#)
1895 (setq type
"custom-id" path
(substring link
1)))
1896 ;; Fuzzy type: Internal link either matches a target, an
1897 ;; headline name or nothing. PATH is the target or headline's
1899 (t (setq type
"fuzzy" path link
))))
1900 ;; Type 3: Plain link, i.e. http://orgmode.org
1901 ((looking-at org-plain-link-re
)
1902 (setq raw-link
(org-match-string-no-properties 0)
1903 type
(org-match-string-no-properties 1)
1904 path
(org-match-string-no-properties 2)
1905 link-end
(match-end 0)))
1906 ;; Type 4: Angular link, i.e. <http://orgmode.org>
1907 ((looking-at org-angle-link-re
)
1908 (setq raw-link
(buffer-substring-no-properties
1909 (match-beginning 1) (match-end 2))
1910 type
(org-match-string-no-properties 1)
1911 path
(org-match-string-no-properties 2)
1912 link-end
(match-end 0))))
1913 ;; In any case, deduce end point after trailing white space from
1914 ;; LINK-END variable.
1915 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
1920 :raw-link
,(or raw-link path
)
1923 :contents-begin
,contents-begin
1924 :contents-end
,contents-end
1925 :post-blank
,post-blank
)))))
1927 (defun org-element-link-interpreter (link contents
)
1928 "Interpret LINK object as Org syntax.
1929 CONTENTS is the contents of the object."
1930 (let ((type (org-element-get-property :type link
))
1931 (raw-link (org-element-get-property :raw-link link
)))
1933 ((string= type
"radio") raw-link
)
1934 (t (format "[[%s]%s]"
1936 (if (string= contents
"") "" (format "[%s]" contents
)))))))
1938 (defun org-element-link-successor (limit)
1939 "Search for the next link and return position.
1941 LIMIT bounds the search.
1943 Return value is a cons cell whose car is `link' and cdr is
1944 beginning position."
1947 (if org-target-link-regexp
1948 (concat org-any-link-re
"\\|" org-target-link-regexp
)
1950 (when (re-search-forward link-regexp limit t
)
1951 (cons 'link
(match-beginning 0))))))
1954 (defun org-element-macro-parser ()
1955 "Parse macro at point.
1957 Return a list whose car is `macro' and cdr a plist with `:key',
1958 `:args', `:begin', `:end', `:value' and `:post-blank' as
1961 Assume point is at the macro."
1963 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
1964 (let ((begin (point))
1965 (key (downcase (org-match-string-no-properties 1)))
1966 (value (org-match-string-no-properties 0))
1967 (post-blank (progn (goto-char (match-end 0))
1968 (skip-chars-forward " \t")))
1970 (args (let ((args (org-match-string-no-properties 3)) args2
)
1972 (setq args
(org-split-string args
","))
1974 (while (string-match "\\\\\\'" (car args
))
1975 ;; Repair bad splits.
1976 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
1979 (push (pop args
) args2
))
1980 (mapcar 'org-trim
(nreverse args2
))))))
1987 :post-blank
,post-blank
)))))
1989 (defun org-element-macro-interpreter (macro contents
)
1990 "Interpret MACRO object as Org syntax.
1992 (org-element-get-property :value macro
))
1994 (defun org-element-macro-successor (limit)
1995 "Search for the next macro and return position.
1997 LIMIT bounds the search.
1999 Return value is cons cell whose car is `macro' and cdr is
2000 beginning position."
2002 (when (re-search-forward
2003 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2005 (cons 'macro
(match-beginning 0)))))
2008 (defun org-element-radio-target-parser ()
2009 "Parse radio target at point.
2011 Return a list whose car is `radio-target' and cdr a plist with
2012 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2013 and `:post-blank' as keywords.
2015 Assume point is at the radio target."
2017 (looking-at org-radio-target-regexp
)
2018 (let ((begin (point))
2019 (contents-begin (match-beginning 1))
2020 (contents-end (match-end 1))
2021 (raw-value (org-match-string-no-properties 1))
2022 (post-blank (progn (goto-char (match-end 0))
2023 (skip-chars-forward " \t")))
2028 :contents-begin
,contents-begin
2029 :contents-end
,contents-end
2030 :raw-value
,raw-value
2031 :post-blank
,post-blank
)))))
2033 (defun org-element-radio-target-interpreter (target contents
)
2034 "Interpret TARGET object as Org syntax.
2035 CONTENTS is the contents of the object."
2038 (defun org-element-radio-target-successor (limit)
2039 "Search for the next radio-target and return position.
2041 LIMIT bounds the search.
2043 Return value is a cons cell whose car is `radio-target' and cdr
2044 is beginning position."
2046 (when (re-search-forward org-radio-target-regexp limit t
)
2047 (cons 'radio-target
(match-beginning 0)))))
2049 ;;;; Statistics Cookie
2050 (defun org-element-statistics-cookie-parser ()
2051 "Parse statistics cookie at point.
2053 Return a list whose car is `statistics-cookie', and cdr a plist
2054 with `:begin', `:end', `:value' and `:post-blank' keywords.
2056 Assume point is at the beginning of the statistics-cookie."
2058 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2059 (let* ((begin (point))
2060 (value (buffer-substring-no-properties
2061 (match-beginning 0) (match-end 0)))
2062 (post-blank (progn (goto-char (match-end 0))
2063 (skip-chars-forward " \t")))
2065 (list 'statistics-cookie
2069 :post-blank
,post-blank
)))))
2071 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
2072 "Interpret STATISTICS-COOKIE object as Org syntax.
2074 (org-element-get-property :value statistics-cookie
))
2076 (defun org-element-statistics-cookie-successor (limit)
2077 "Search for the next statistics cookie and return position.
2079 LIMIT bounds the search.
2081 Return value is a cons cell whose car is `statistics-cookie' and
2082 cdr is beginning position."
2084 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
2085 (cons 'statistics-cookie
(match-beginning 0)))))
2088 (defun org-element-subscript-parser ()
2089 "Parse subscript at point.
2091 Return a list whose car is `subscript' and cdr a plist with
2092 `:begin', `:end', `:contents-begin', `:contents-end',
2093 `:use-brackets-p' and `:post-blank' as keywords.
2095 Assume point is at the underscore."
2097 (unless (bolp) (backward-char))
2098 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2100 (not (looking-at org-match-substring-regexp
))))
2101 (begin (match-beginning 2))
2102 (contents-begin (or (match-beginning 5)
2103 (match-beginning 3)))
2104 (contents-end (or (match-end 5) (match-end 3)))
2105 (post-blank (progn (goto-char (match-end 0))
2106 (skip-chars-forward " \t")))
2111 :use-brackets-p
,bracketsp
2112 :contents-begin
,contents-begin
2113 :contents-end
,contents-end
2114 :post-blank
,post-blank
)))))
2116 (defun org-element-subscript-interpreter (subscript contents
)
2117 "Interpret SUBSCRIPT object as Org syntax.
2118 CONTENTS is the contents of the object."
2120 (if (org-element-get-property :use-brackets-p subscript
) "_{%s}" "_%s")
2123 (defun org-element-sub/superscript-successor
(limit)
2124 "Search for the next sub/superscript and return beginning
2127 LIMIT bounds the search.
2129 Return value is a cons cell whose car is either `subscript' or
2130 `superscript' and cdr is beginning position."
2132 (when (re-search-forward org-match-substring-regexp limit t
)
2133 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
2134 (match-beginning 2)))))
2137 (defun org-element-superscript-parser ()
2138 "Parse superscript at point.
2140 Return a list whose car is `superscript' and cdr a plist with
2141 `:begin', `:end', `:contents-begin', `:contents-end',
2142 `:use-brackets-p' and `:post-blank' as keywords.
2144 Assume point is at the caret."
2146 (unless (bolp) (backward-char))
2147 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2149 (not (looking-at org-match-substring-regexp
))))
2150 (begin (match-beginning 2))
2151 (contents-begin (or (match-beginning 5)
2152 (match-beginning 3)))
2153 (contents-end (or (match-end 5) (match-end 3)))
2154 (post-blank (progn (goto-char (match-end 0))
2155 (skip-chars-forward " \t")))
2160 :use-brackets-p
,bracketsp
2161 :contents-begin
,contents-begin
2162 :contents-end
,contents-end
2163 :post-blank
,post-blank
)))))
2165 (defun org-element-superscript-interpreter (superscript contents
)
2166 "Interpret SUPERSCRIPT object as Org syntax.
2167 CONTENTS is the contents of the object."
2169 (if (org-element-get-property :use-brackets-p superscript
) "^{%s}" "^%s")
2173 (defun org-element-target-parser ()
2174 "Parse target at point.
2176 Return a list whose car is `target' and cdr a plist with
2177 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2178 and `:post-blank' as keywords.
2180 Assume point is at the target."
2182 (looking-at org-target-regexp
)
2183 (let ((begin (point))
2184 (contents-begin (match-beginning 1))
2185 (contents-end (match-end 1))
2186 (raw-value (org-match-string-no-properties 1))
2187 (post-blank (progn (goto-char (match-end 0))
2188 (skip-chars-forward " \t")))
2193 :contents-begin
,contents-begin
2194 :contents-end
,contents-end
2195 :raw-value
,raw-value
2196 :post-blank
,post-blank
)))))
2198 (defun org-element-target-interpreter (target contents
)
2199 "Interpret TARGET object as Org syntax.
2200 CONTENTS is the contents of target."
2203 (defun org-element-target-successor (limit)
2204 "Search for the next target and return position.
2206 LIMIT bounds the search.
2208 Return value is a cons cell whose car is `target' and cdr is
2209 beginning position."
2211 (when (re-search-forward org-target-regexp limit t
)
2212 (cons 'target
(match-beginning 0)))))
2215 (defun org-element-time-stamp-parser ()
2216 "Parse time stamp at point.
2218 Return a list whose car is `time-stamp', and cdr a plist with
2219 `:appt-type', `:type', `:begin', `:end', `:value' and
2220 `:post-blank' keywords.
2222 Assume point is at the beginning of the time-stamp."
2224 (let* ((appt-type (cond
2225 ((looking-at (concat org-deadline-string
" +"))
2226 (goto-char (match-end 0))
2228 ((looking-at (concat org-scheduled-string
" +"))
2229 (goto-char (match-end 0))
2231 ((looking-at (concat org-closed-string
" +"))
2232 (goto-char (match-end 0))
2234 (begin (and appt-type
(match-beginning 0)))
2236 ((looking-at org-tsr-regexp
)
2237 (if (match-string 2) 'active-range
'active
))
2238 ((looking-at org-tsr-regexp-both
)
2239 (if (match-string 2) 'inactive-range
'inactive
))
2240 ((looking-at (concat
2241 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2243 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2245 (begin (or begin
(match-beginning 0)))
2246 (value (buffer-substring-no-properties
2247 (match-beginning 0) (match-end 0)))
2248 (post-blank (progn (goto-char (match-end 0))
2249 (skip-chars-forward " \t")))
2252 `(:appt-type
,appt-type
2257 :post-blank
,post-blank
)))))
2259 (defun org-element-time-stamp-interpreter (time-stamp contents
)
2260 "Interpret TIME-STAMP object as Org syntax.
2263 (case (org-element-get-property :appt-type time-stamp
)
2264 (closed (concat org-closed-string
" "))
2265 (deadline (concat org-deadline-string
" "))
2266 (scheduled (concat org-scheduled-string
" ")))
2267 (org-element-get-property :value time-stamp
)))
2269 (defun org-element-time-stamp-successor (limit)
2270 "Search for the next time-stamp and return position.
2272 LIMIT bounds the search.
2274 Return value is a cons cell whose car is `time-stamp' and cdr is
2275 beginning position."
2277 (when (re-search-forward
2278 (concat "\\(?:" org-scheduled-string
" +\\|"
2279 org-deadline-string
" +\\|" org-closed-string
" +\\)?"
2282 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2284 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2286 (cons 'time-stamp
(match-beginning 0)))))
2289 (defun org-element-verbatim-parser ()
2290 "Parse verbatim object at point.
2292 Return a list whose car is `verbatim' and cdr is a plist with
2293 `:marker', `:begin', `:end' and `:post-blank' keywords.
2295 Assume point is at the first verbatim marker."
2297 (unless (bolp) (backward-char 1))
2298 (looking-at org-emph-re
)
2299 (let ((begin (match-beginning 2))
2300 (marker (org-match-string-no-properties 3))
2301 (value (org-match-string-no-properties 4))
2302 (post-blank (progn (goto-char (match-end 2))
2303 (skip-chars-forward " \t")))
2310 :post-blank
,post-blank
)))))
2312 (defun org-element-verbatim-interpreter (verbatim contents
)
2313 "Interpret VERBATIM object as Org syntax.
2315 (let ((marker (org-element-get-property :marker verbatim
))
2316 (value (org-element-get-property :value verbatim
)))
2317 (concat marker value marker
)))
2321 ;;; Definitions And Rules
2323 ;; Define elements, greater elements and specify recursive objects,
2324 ;; along with the affiliated keywords recognized. Also set up
2325 ;; restrictions on recursive objects combinations.
2327 ;; These variables really act as a control center for the parsing
2329 (defconst org-element-paragraph-separate
2330 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2331 ;; Headlines and inlinetasks.
2332 org-outline-regexp-bol
"\\|"
2333 ;; Comments, blocks (any type), keywords and babel calls.
2334 "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
2336 (org-item-beginning-re) "\\|"
2337 ;; Fixed-width, drawers (any type) and tables.
2339 ;; Footnote definitions.
2340 org-footnote-definition-re
"\\|"
2341 ;; Horizontal rules.
2342 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2343 ;; LaTeX environments.
2344 "^[ \t]*\\\\\\(begin\\|end\\)")
2345 "Regexp to separate paragraphs in an Org buffer.")
2347 (defconst org-element-all-elements
2348 '(center-block comment comment-block drawer dynamic-block example-block
2349 export-block fixed-width footnote-definition headline
2350 horizontal-rule inlinetask item keyword latex-environment
2351 babel-call paragraph plain-list property-drawer quote-block
2352 quote-section section special-block src-block table
2354 "Complete list of elements.")
2356 (defconst org-element-greater-elements
2357 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2358 item plain-list quote-block section special-block
)
2359 "List of recursive element types aka Greater Elements.")
2361 (defconst org-element-all-successors
2362 '(export-snippet footnote-reference inline-babel-call inline-src-block
2363 latex-or-entity line-break link macro radio-target
2364 statistics-cookie sub
/superscript target text-markup
2366 "Complete list of successors.")
2368 (defconst org-element-object-successor-alist
2369 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
2370 (emphasis . text-markup
) (verbatim . text-markup
)
2371 (entity . latex-or-entity
) (latex-fragment . latex-or-entity
))
2372 "Alist of translations between object type and successor name.
2374 Sharing the same successor comes handy when, for example, the
2375 regexp matching one object can also match the other object.")
2377 (defconst org-element-recursive-objects
2378 '(emphasis link subscript superscript target radio-target
)
2379 "List of recursive object types.")
2381 (defconst org-element-non-recursive-block-alist
2382 '(("ascii" . export-block
)
2383 ("comment" . comment-block
)
2384 ("docbook" . export-block
)
2385 ("example" . example-block
)
2386 ("html" . export-block
)
2387 ("latex" . latex-block
)
2388 ("odt" . export-block
)
2390 ("verse" . verse-block
))
2391 "Alist between non-recursive block name and their element type.")
2393 (defconst org-element-affiliated-keywords
2394 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "caption"
2395 "data" "header" "headers" "label" "name" "plot" "resname" "result" "results"
2396 "source" "srcname" "tblname")
2397 "List of affiliated keywords as strings.")
2399 (defconst org-element-keyword-translation-alist
2400 '(("data" .
"name") ("label" .
"name") ("resname" .
"name")
2401 ("source" .
"name") ("srcname" .
"name") ("tblname" .
"name")
2402 ("result" .
"results") ("headers" .
"header"))
2403 "Alist of usual translations for keywords.
2404 The key is the old name and the value the new one. The property
2405 holding their value will be named after the translated name.")
2407 (defconst org-element-multiple-keywords
2408 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "header")
2409 "List of affiliated keywords that can occur more that once in an element.
2411 Their value will be consed into a list of strings, which will be
2412 returned as the value of the property.
2414 This list is checked after translations have been applied. See
2415 `org-element-keyword-translation-alist'.")
2417 (defconst org-element-parsed-keywords
'("author" "caption" "title")
2418 "List of keywords whose value can be parsed.
2420 Their value will be stored as a secondary string: a list of
2421 strings and objects.
2423 This list is checked after translations have been applied. See
2424 `org-element-keyword-translation-alist'.")
2426 (defconst org-element-dual-keywords
'("results")
2427 "List of keywords which can have a secondary value.
2429 In Org syntax, they can be written with optional square brackets
2430 before the colons. For example, results keyword can be
2431 associated to a hash value with the following:
2433 #+results[hash-string]: some-source
2435 This list is checked after translations have been applied. See
2436 `org-element-keyword-translation-alist'.")
2438 (defconst org-element-object-restrictions
2439 '((emphasis entity export-snippet inline-babel-call inline-src-block
2440 radio-target sub
/superscript target text-markup time-stamp
)
2441 (link entity export-snippet inline-babel-call inline-src-block
2442 latex-fragment sub
/superscript text-markup
)
2443 (radio-target entity export-snippet latex-fragment sub
/superscript
)
2444 (subscript entity export-snippet inline-babel-call inline-src-block
2445 latex-fragment sub
/superscript text-markup
)
2446 (superscript entity export-snippet inline-babel-call inline-src-block
2447 latex-fragment sub
/superscript text-markup
)
2448 (target entity export-snippet latex-fragment sub
/superscript text-markup
))
2449 "Alist of recursive objects restrictions.
2451 Car is a recursive object type and cdr is a list of successors
2452 that will be called within an object of such type.
2454 For example, in a `radio-target' object, one can only find
2455 entities, export snippets, latex-fragments, subscript and
2458 (defconst org-element-string-restrictions
2459 '((headline entity inline-babel-call latex-fragment link macro radio-target
2460 statistics-cookie sub
/superscript text-markup time-stamp
)
2461 (inlinetask entity inline-babel-call latex-fragment link macro radio-target
2462 sub
/superscript text-markup time-stamp
)
2463 (item entity inline-babel-call latex-fragment macro radio-target
2464 sub
/superscript target verbatim
)
2465 (keyword entity latex-fragment macro sub
/superscript text-markup
)
2466 (table entity latex-fragment macro text-markup
)
2467 (verse entity footnote-reference inline-babel-call inline-src-block
2468 latex-fragment line-break link macro radio-target sub
/superscript
2469 target text-markup time-stamp
))
2470 "Alist of secondary strings restrictions.
2472 When parsed, some elements have a secondary string which could
2473 contain various objects (i.e. headline's name, or table's cells).
2474 For association, the car is the element type, and the cdr a list
2475 of successors that will be called in that secondary string.
2477 Note: `keyword' secondary string type only applies to keywords
2478 matching `org-element-parsed-keywords'.")
2484 ;; Provide two accessors: `org-element-get-property' and
2485 ;; `org-element-get-contents'.
2486 (defun org-element-get-property (property element
)
2487 "Extract the value from the PROPERTY of an ELEMENT."
2488 (plist-get (nth 1 element
) property
))
2490 (defun org-element-get-contents (element)
2491 "Extract contents from an ELEMENT."
2496 ;; Obtaining The Smallest Element Containing Point
2498 ;; `org-element-at-point' is the core function of this section. It
2499 ;; returns the Lisp representation of the element at point. It uses
2500 ;; `org-element-guess-type' and `org-element-skip-keywords' as helper
2503 ;; When point is at an item, there is no automatic way to determine if
2504 ;; the function should return the `plain-list' element, or the
2505 ;; corresponding `item' element. By default, `org-element-at-point'
2506 ;; works at the `plain-list' level. But, by providing an optional
2507 ;; argument, one can make it switch to the `item' level.
2508 (defconst org-element--affiliated-re
2509 (format "[ \t]*#\\+\\(%s\\):"
2512 (if (member keyword org-element-dual-keywords
)
2513 (format "\\(%s\\)\\(?:\\[\\(.*?\\)\\]\\)?"
2514 (regexp-quote keyword
))
2515 (regexp-quote keyword
)))
2516 org-element-affiliated-keywords
"\\|"))
2517 "Regexp matching any affiliated keyword.
2519 Keyword name is put in match group 1. Moreover, if keyword
2520 belongs to `org-element-dual-keywords', put the dual value in
2523 Don't modify it, set `org-element--affiliated-keywords' instead.")
2525 (defun org-element-at-point (&optional special structure
)
2526 "Determine closest element around point.
2528 Return value is a list \(TYPE PROPS\) where TYPE is the type of
2529 the element and PROPS a plist of properties associated to the
2532 Possible types are defined in `org-element-all-elements'.
2534 Optional argument SPECIAL, when non-nil, can be either `item' or
2535 `section'. The former allows to parse item wise instead of
2536 plain-list wise, using STRUCTURE as the current list structure.
2537 The latter will try to parse a section before anything else.
2539 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
2543 ;; Move before any blank line.
2544 (when (looking-at "[ \t]*$")
2545 (skip-chars-backward " \r\t\n")
2546 (beginning-of-line))
2547 (let ((case-fold-search t
))
2548 ;; Check if point is at an affiliated keyword. In that case,
2549 ;; try moving to the beginning of the associated element. If
2550 ;; the keyword is orphaned, treat it as plain text.
2551 (when (looking-at org-element--affiliated-re
)
2552 (let ((opoint (point)))
2553 (while (looking-at org-element--affiliated-re
) (forward-line))
2554 (when (looking-at "[ \t]*$") (goto-char opoint
))))
2555 (let ((type (org-element-guess-type (eq special
'section
))))
2557 ;; Guessing element type on the current line is impossible:
2558 ;; try to find the beginning of the current element to get
2559 ;; more information.
2561 (let ((search-origin (point))
2562 (opoint-in-item-p (org-in-item-p))
2566 (re-search-backward org-element-paragraph-separate nil
'm
))))
2568 ;; Unable to find a paragraph delimiter above: we're at
2569 ;; bob and looking at a paragraph.
2570 ((not par-found-p
) (org-element-paragraph-parser))
2571 ;; Trying to find element's beginning set point back to
2572 ;; its original position. There's something peculiar on
2573 ;; this line that prevents parsing, probably an
2574 ;; ill-formed keyword or an undefined drawer name. Parse
2575 ;; it as plain text anyway.
2576 ((< search-origin
(point-at-eol)) (org-element-paragraph-parser))
2577 ;; Original point wasn't in a list but previous paragraph
2578 ;; is. It means that either point was inside some block,
2579 ;; or current list was ended without using a blank line.
2580 ;; In the last case, paragraph really starts at list end.
2582 (and (not opoint-in-item-p
)
2583 (not (looking-at "[ \t]*#\\+begin"))
2584 (setq item
(org-in-item-p))
2585 (let ((struct (save-excursion (goto-char item
)
2586 (org-list-struct))))
2587 (goto-char (org-list-get-bottom-point struct
))
2588 (org-skip-whitespace)
2590 (org-element-paragraph-parser)))))
2591 ((org-footnote-at-definition-p)
2592 (org-element-footnote-definition-parser))
2593 ((and opoint-in-item-p
(org-at-item-p) (= opoint-in-item-p
(point)))
2594 (if (eq special
'item
)
2595 (org-element-item-parser (or structure
(org-list-struct)))
2596 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2597 ;; In any other case, the paragraph started the line
2599 (t (forward-line) (org-element-paragraph-parser)))))
2600 ((eq type
'plain-list
)
2601 (if (eq special
'item
)
2602 (org-element-item-parser (or structure
(org-list-struct)))
2603 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2604 ;; Straightforward case: call the appropriate parser.
2605 (t (funcall (intern (format "org-element-%s-parser" type
)))))))))
2608 ;; It is obvious to tell if point is in most elements, either by
2609 ;; looking for a specific regexp in the current line, or by using
2610 ;; already implemented functions. This is the goal of
2611 ;; `org-element-guess-type'.
2612 (defconst org-element--element-block-types
2613 (mapcar 'car org-element-non-recursive-block-alist
)
2614 "List of non-recursive block types, as strings.
2615 Used internally by `org-element-guess-type'. Do not modify it
2616 directly, set `org-element-non-recursive-block-alist' instead.")
2618 (defun org-element-guess-type (&optional section-mode
)
2619 "Return the type of element at point, or nil if undetermined.
2621 This function may move point to an appropriate position for
2622 parsing. Used internally by `org-element-at-point'.
2624 When optional argument SECTION-MODE is non-nil, try to find if
2625 point is in a section in priority."
2626 ;; Beware: Order matters for some cases in that function.
2628 (let ((case-fold-search t
))
2630 ((org-with-limited-levels (org-at-heading-p)) 'headline
)
2631 ((let ((headline (ignore-errors (nth 4 (org-heading-components)))))
2633 (let (case-fold-search)
2634 (string-match (format "^%s\\(?: \\|$\\)" org-quote-string
)
2637 ;; Any buffer position not at an headline or in a quote section
2638 ;; is inside a section, provided function is actively looking for
2640 (section-mode 'section
)
2641 ;; Non-recursive block.
2642 ((let ((type (org-in-block-p org-element--element-block-types
)))
2643 (and type
(cdr (assoc type org-element-non-recursive-block-alist
)))))
2644 ((org-at-heading-p) 'inlinetask
)
2645 ((org-between-regexps-p
2646 "^[ \t]*\\\\begin{" "^[ \t]*\\\\end{[^}]*}[ \t]*") 'latex-environment
)
2647 ;; Property drawer. Almost `org-at-property-p', but allow drawer
2649 ((org-with-wide-buffer
2650 (and (not (org-before-first-heading-p))
2651 (let ((pblock (org-get-property-block)))
2653 (<= (point) (cdr pblock
))
2654 (>= (point-at-eol) (1- (car pblock
)))))))
2656 ;; Recursive block. If the block isn't complete, parse the
2657 ;; current part as a paragraph.
2658 ((looking-at "[ \t]*#\\+\\(begin\\|end\\)_\\([-A-Za-z0-9]+\\)\\(?:$\\|\\s-\\)")
2659 (let ((type (downcase (match-string 2))))
2661 ((not (org-in-block-p (list type
))) 'paragraph
)
2662 ((string= type
"center") 'center-block
)
2663 ((string= type
"quote") 'quote-block
)
2664 (t 'special-block
))))
2665 ;; Regular drawers must be tested after property drawer as both
2666 ;; elements share the same ending regexp.
2667 ((or (looking-at org-drawer-regexp
) (looking-at "[ \t]*:END:[ \t]*$"))
2668 (let ((completep (org-between-regexps-p
2669 org-drawer-regexp
"^[ \t]*:END:[ \t]*$")))
2672 (goto-char (car completep
)) 'drawer
)))
2673 ((looking-at "[ \t]*:\\( \\|$\\)") 'fixed-width
)
2674 ;; Babel calls must be tested before general keywords as they are
2675 ;; a subset of them.
2676 ((looking-at org-babel-block-lob-one-liner-regexp
) 'babel-call
)
2677 ((looking-at org-footnote-definition-re
) 'footnote-definition
)
2678 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
2679 (if (member (downcase (match-string 1)) org-element-affiliated-keywords
)
2682 ;; Dynamic block: simplify regexp used for match. If it isn't
2683 ;; complete, parse the current part as a paragraph.
2684 ((looking-at "[ \t]*#\\+\\(begin\\end\\):\\(?:\\s-\\|$\\)")
2685 (let ((completep (org-between-regexps-p
2686 "^[ \t]*#\\+begin:\\(?:\\s-\\|$\\)"
2687 "^[ \t]*#\\+end:\\(?:\\s-\\|$\\)")))
2690 (goto-char (car completep
)) 'dynamic-block
)))
2691 ((looking-at "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)") 'comment
)
2692 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$") 'horizontal-rule
)
2693 ((org-at-table-p t
) 'table
)
2694 ((looking-at "[ \t]*#\\+tblfm:")
2696 ;; A TBLFM line separated from any table is just plain text.
2697 (if (org-at-table-p)
2699 (forward-line) 'paragraph
))
2700 ((looking-at (org-item-re)) 'plain-list
))))
2702 ;; Most elements can have affiliated keywords. When looking for an
2703 ;; element beginning, we want to move before them, as they belong to
2704 ;; that element, and, in the meantime, collect information they give
2705 ;; into appropriate properties. Hence the following function.
2707 ;; Usage of optional arguments may not be obvious at first glance:
2709 ;; - TRANS-LIST is used to polish keywords names that have evolved
2710 ;; during Org history. In example, even though =result= and
2711 ;; =results= coexist, we want to have them under the same =result=
2712 ;; property. It's also true for "srcname" and "name", where the
2713 ;; latter seems to be preferred nowadays (thus the "name" property).
2715 ;; - CONSED allows to regroup multi-lines keywords under the same
2716 ;; property, while preserving their own identity. This is mostly
2717 ;; used for "attr_latex" and al.
2719 ;; - PARSED prepares a keyword value for export. This is useful for
2720 ;; "caption". Objects restrictions for such keywords are defined in
2721 ;; `org-element-string-restrictions'.
2723 ;; - DUALS is used to take care of keywords accepting a main and an
2724 ;; optional secondary values. For example "results" has its
2725 ;; source's name as the main value, and may have an hash string in
2726 ;; optional square brackets as the secondary one.
2728 ;; A keyword may belong to more than one category.
2729 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
2730 consed parsed duals
)
2731 "Collect affiliated keywords before point.
2733 Optional argument KEY-RE is a regexp matching keywords, which
2734 puts matched keyword in group 1. It defaults to
2735 `org-element--affiliated-re'.
2737 TRANS-LIST is an alist where key is the keyword and value the
2738 property name it should be translated to, without the colons. It
2739 defaults to `org-element-keyword-translation-alist'.
2741 CONSED is a list of strings. Any keyword belonging to that list
2742 will have its value consed. The check is done after keyword
2743 translation. It defaults to `org-element-multiple-keywords'.
2745 PARSED is a list of strings. Any keyword member of this list
2746 will have its value parsed. The check is done after keyword
2747 translation. If a keyword is a member of both CONSED and PARSED,
2748 it's value will be a list of parsed strings. It defaults to
2749 `org-element-parsed-keywords'.
2751 DUALS is a list of strings. Any keyword member of this list can
2752 have two parts: one mandatory and one optional. Its value is
2753 a cons cell whose car is the former, and the cdr the latter. If
2754 a keyword is a member of both PARSED and DUALS, only the primary
2755 part will be parsed. It defaults to `org-element-dual-keywords'.
2757 Return a list whose car is the position at the first of them and
2758 cdr a plist of keywords and values."
2760 (let ((case-fold-search t
)
2761 (key-re (or key-re org-element--affiliated-re
))
2762 (trans-list (or trans-list org-element-keyword-translation-alist
))
2763 (consed (or consed org-element-multiple-keywords
))
2764 (parsed (or parsed org-element-parsed-keywords
))
2765 (duals (or duals org-element-dual-keywords
))
2768 (while (and (not (bobp))
2769 (progn (forward-line -
1) (looking-at key-re
)))
2770 (let* ((raw-kwd (downcase (or (match-string 2) (match-string 1))))
2771 ;; Apply translation to RAW-KWD. From there, KWD is
2772 ;; the official keyword.
2773 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
2774 ;; If KWD is a dual keyword, find it secondary value.
2775 (dual-value (and (member kwd duals
)
2776 (org-match-string-no-properties 3)))
2777 ;; Find main value for any keyword.
2778 (value (org-trim (buffer-substring-no-properties
2779 (match-end 0) (point-at-eol))))
2780 ;; Attribute a property name to KWD.
2781 (kwd-sym (and kwd
(intern (concat ":" kwd
)))))
2782 ;; Now set final shape for VALUE.
2783 (when (member kwd parsed
)
2785 (org-element-parse-secondary-string
2787 (cdr (assq 'keyword org-element-string-restrictions
)))))
2788 (when (member kwd duals
) (setq value
(cons value dual-value
)))
2789 (when (member kwd consed
)
2790 (setq value
(cons value
(plist-get output kwd-sym
))))
2791 ;; Eventually store the new value in OUTPUT.
2792 (setq output
(plist-put output kwd-sym value
))))
2793 (unless (looking-at key-re
) (forward-line 1)))
2794 (list (point) output
))))
2800 ;; The two major functions here are `org-element-parse-buffer', which
2801 ;; parses Org syntax inside the current buffer, taking into account
2802 ;; region, narrowing, or even visibility if specified, and
2803 ;; `org-element-parse-secondary-string', which parses objects within
2806 ;; The (almost) almighty `org-element-map' allows to apply a function
2807 ;; on elements or objects matching some type, and accumulate the
2808 ;; resulting values. In an export situation, it also skips unneeded
2809 ;; parts of the parse tree, transparently walks into included files,
2810 ;; and maintain a list of local properties (i.e. those inherited from
2811 ;; parent headlines) for function's consumption.
2812 (defun org-element-parse-buffer (&optional granularity visible-only
)
2813 "Recursively parse the buffer and return structure.
2814 If narrowing is in effect, only parse the visible part of the
2817 Optional argument GRANULARITY determines the depth of the
2818 recursion. It can be set to the following symbols:
2820 `headline' Only parse headlines.
2821 `greater-element' Don't recurse into greater elements. Thus,
2822 elements parsed are the top-level ones.
2823 `element' Parse everything but objects and plain text.
2824 `object' Parse the complete buffer (default).
2826 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
2829 Assume buffer is in Org mode."
2831 (goto-char (point-min))
2832 (org-skip-whitespace)
2833 (nconc (list 'org-data nil
)
2834 (org-element-parse-elements
2835 (point-at-bol) (point-max)
2836 ;; Start is section mode so text before the first headline
2837 ;; belongs to a section.
2838 'section nil granularity visible-only nil
))))
2840 (defun org-element-parse-secondary-string (string restriction
&optional buffer
)
2841 "Recursively parse objects in STRING and return structure.
2843 RESTRICTION, when non-nil, is a symbol limiting the object types
2844 that will be looked after.
2846 Optional argument BUFFER indicates the buffer from where the
2847 secondary string was extracted. It is used to determine where to
2848 get extraneous information for an object \(i.e. when resolving
2849 a link or looking for a footnote definition\). It defaults to
2850 the current buffer."
2853 (org-element-parse-objects (point-min) (point-max) nil restriction
)))
2855 (defun org-element-map (data types fun
&optional info first-match
)
2856 "Map a function on selected elements or objects.
2858 DATA is the parsed tree, as returned by, i.e,
2859 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
2860 of elements or objects types. FUN is the function called on the
2861 matching element or object. It must accept two arguments: the
2862 element or object itself and a plist holding contextual
2865 When optional argument INFO is non-nil, it should be a plist
2866 holding export options. In that case, parts of the parse tree
2867 not exportable according to that property list will be skipped
2868 and files included through a keyword will be visited.
2870 When optional argument FIRST-MATCH is non-nil, stop at the first
2871 match for which FUN doesn't return nil, and return that value.
2873 Nil values returned from FUN are ignored in the result."
2874 ;; Ensure TYPES is a list, even of one element.
2875 (unless (listp types
) (setq types
(list types
)))
2876 ;; Recursion depth is determined by --CATEGORY.
2879 ((loop for type in types
2880 always
(memq type org-element-greater-elements
))
2882 ((loop for type in types
2883 always
(memq type org-element-all-elements
))
2886 walk-tree
; For byte-compiler
2890 (lambda (--type types fun --blob --local
)
2891 ;; Check if TYPE is matching among TYPES. If so, apply
2892 ;; FUN to --BLOB and accumulate return value
2893 ;; into --ACC. --LOCAL is the communication channel.
2894 (when (memq --type types
)
2895 (let ((result (funcall fun --blob --local
)))
2896 (cond ((not result
))
2897 (first-match (throw 'first-match result
))
2898 (t (push result --acc
))))))))
2901 (lambda (--data --local
)
2902 ;; Recursively walk DATA. --LOCAL, if non-nil, is
2903 ;; a plist holding contextual information.
2906 (let ((--type (if (stringp --blob
) 'plain-text
(car --blob
))))
2907 ;; Determine if a recursion into --BLOB is
2908 ;; possible and allowed.
2910 ;; Element or object not exportable.
2911 ((and info
(org-export-skip-p --blob info
)))
2912 ;; Archived headline: Maybe apply fun on it, but
2915 (eq --type
'headline
)
2916 (eq (plist-get info
:with-archived-trees
) 'headline
)
2917 (org-element-get-property :archivedp --blob
))
2918 (funcall accumulate-maybe --type types fun --blob --local
))
2919 ;; At an include keyword: apply mapping to its
2922 (eq --type
'keyword
)
2924 (downcase (org-element-get-property :key --blob
))
2926 (funcall accumulate-maybe --type types fun --blob --local
)
2928 (org-export-parse-included-file --blob --local
))
2929 (--value (org-element-get-property :value --blob
))
2931 (and (string-match "^\"\\(\\S-+\\)\"" --value
)
2932 (match-string 1 --value
))))
2937 ;; Store full path of already included files
2938 ;; to avoid recursive file inclusion.
2940 ,(cons (expand-file-name --file
)
2941 (plist-get --local
:included-files
))
2942 ;; Ensure that a top-level headline in the
2943 ;; included file becomes a direct child of
2944 ;; the current headline in the buffer.
2947 (plist-get --local
:inherited-properties
)
2949 (or (plist-get --local
:headline-offset
) 0))
2950 (1- (org-export-get-min-level
2951 --data --local
))))))))
2952 ;; Limiting recursion to greater elements, and --BLOB
2954 ((and (eq --category
'greater-elements
)
2955 (not (memq --type org-element-greater-elements
)))
2956 (funcall accumulate-maybe --type types fun --blob --local
))
2957 ;; Limiting recursion to elements, and --BLOB only
2958 ;; contains objects.
2959 ((and (eq --category
'elements
) (eq --type
'paragraph
)))
2960 ;; No limitation on recursion, but --BLOB hasn't
2961 ;; got a recursive type.
2962 ((and (eq --category
'objects
)
2963 (not (or (eq --type
'paragraph
)
2964 (memq --type org-element-greater-elements
)
2965 (memq --type org-element-recursive-objects
))))
2966 (funcall accumulate-maybe --type types fun --blob --local
))
2967 ;; Recursion is possible and allowed: Update local
2968 ;; information and move into --BLOB.
2969 (t (funcall accumulate-maybe --type types fun --blob --local
)
2972 (and info
(org-export-update-info --blob --local t
)))))))
2973 (org-element-get-contents --data
))))))
2975 (funcall walk-tree data info
)
2976 ;; Return value in a proper order.
2979 ;; The following functions are internal parts of the parser. The
2980 ;; first one, `org-element-parse-elements' acts at the element's
2981 ;; level. The second one, `org-element-parse-objects' applies on all
2982 ;; objects of a paragraph or a secondary string. It uses
2983 ;; `org-element-get-candidates' to optimize the search of the next
2984 ;; object in the buffer.
2986 ;; More precisely, that function looks for every allowed object type
2987 ;; first. Then, it discards failed searches, keeps further matches,
2988 ;; and searches again types matched behind point, for subsequent
2989 ;; calls. Thus, searching for a given type fails only once, and every
2990 ;; object is searched only once at top level (but sometimes more for
2992 (defun org-element-parse-elements
2993 (beg end special structure granularity visible-only acc
)
2994 "Parse ELEMENT with point at its beginning.
2996 SPECIAL prioritize some elements over the others. It can set to
2997 either `section' or `item', which will focus search,
2998 respectively, on sections and items. Moreover, when value is
2999 `item', STRUCTURE will be used as the current list structure.
3001 GRANULARITY determines the depth of the recursion. It can be set
3002 to the following symbols:
3004 `headline' Only parse headlines.
3005 `greater-element' Don't recurse into greater elements. Thus,
3006 elements parsed are the top-level ones.
3007 `element' Parse everything but objects and plain text.
3008 `object' or nil Parse the complete buffer.
3010 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3013 Elements are accumulated into ACC."
3016 ;; Shortcut when parsing only headlines.
3017 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3018 (org-with-limited-levels (outline-next-heading)))
3020 (while (and (< (point) end
) (not (eobp)))
3022 ;; 1. Item mode is active: point is at an item. Knowing that,
3023 ;; there's no need to go through `org-element-at-point'.
3024 (if (eq special
'item
)
3025 (let* ((element (org-element-item-parser structure
))
3026 (cbeg (org-element-get-property :contents-begin element
))
3027 (cend (org-element-get-property :contents-end element
)))
3028 (goto-char (org-element-get-property :end element
))
3029 ;; Narrow region to contents, so that item bullet don't
3030 ;; interfere with paragraph parsing.
3032 (narrow-to-region cbeg cend
)
3033 (org-element-parse-elements
3034 cbeg cend nil structure granularity visible-only
3035 (reverse element
))))
3036 ;; 2. When ITEM is nil, find current element's type and parse
3037 ;; it accordingly to its category.
3038 (let ((element (org-element-at-point special structure
)))
3039 (goto-char (org-element-get-property :end element
))
3041 ;; Case 1. ELEMENT is a footnote-definition. If
3042 ;; GRANURALITY allows parsing, use narrowing so that
3043 ;; footnote label don't interfere with paragraph
3045 ((and (eq (car element
) 'footnote-definition
)
3046 (not (memq granularity
'(headline greater-element
))))
3047 (let ((cbeg (org-element-get-property :contents-begin element
))
3048 (cend (org-element-get-property :contents-end element
)))
3050 (narrow-to-region cbeg cend
)
3051 (org-element-parse-elements
3052 cbeg cend nil structure granularity visible-only
3053 (reverse element
)))))
3054 ;; Case 2. ELEMENT is a paragraph. Parse objects inside,
3055 ;; if GRANULARITY allows it.
3056 ((and (eq (car element
) 'paragraph
)
3057 (or (not granularity
) (eq granularity
'object
)))
3058 (org-element-parse-objects
3059 (org-element-get-property :contents-begin element
)
3060 (org-element-get-property :contents-end element
)
3063 ;; Case 3. ELEMENT is recursive: parse it between
3064 ;; `contents-begin' and `contents-end'. Make sure
3065 ;; GRANULARITY allows the recursion, or ELEMENT is an
3066 ;; headline, in which case going inside is mandatory, in
3067 ;; order to get sub-level headings. If VISIBLE-ONLY is
3068 ;; true and element is hidden, do not recurse into it.
3069 ((and (memq (car element
) org-element-greater-elements
)
3070 (or (not granularity
)
3071 (memq granularity
'(element object
))
3072 (eq (car element
) 'headline
))
3073 (not (and visible-only
3074 (org-element-get-property :hiddenp element
))))
3075 (org-element-parse-elements
3076 (org-element-get-property :contents-begin element
)
3077 (org-element-get-property :contents-end element
)
3078 ;; At a plain list, switch to item mode. At an
3079 ;; headline, switch to section mode. Any other element
3080 ;; turns off special modes.
3081 (case (car element
) (plain-list 'item
) (headline 'section
))
3082 (org-element-get-property :structure element
)
3086 ;; Case 4. Else, just accumulate ELEMENT.
3089 (org-skip-whitespace))
3093 (defun org-element-parse-objects (beg end acc restriction
)
3094 "Parse objects between BEG and END and return recursive structure.
3096 Objects are accumulated in ACC.
3098 RESTRICTION, when non-nil, is a list of object types which are
3099 allowed in the current object."
3100 (let ((get-next-object
3103 ;; Return the parsing function associated to the nearest
3104 ;; object among list of candidates CAND.
3105 (let ((pos (apply #'min
(mapcar #'cdr cand
))))
3110 (format "org-element-%s-parser" (car (rassq pos cand
))))))))))
3111 next-object candidates
)
3114 (while (setq candidates
(org-element-get-next-object-candidates
3115 end restriction candidates
))
3116 (setq next-object
(funcall get-next-object candidates
))
3117 ;; 1. Text before any object.
3118 (let ((obj-beg (org-element-get-property :begin next-object
)))
3119 (unless (= beg obj-beg
)
3120 (push (buffer-substring-no-properties (point) obj-beg
) acc
)))
3122 (let ((obj-end (org-element-get-property :end next-object
))
3123 (cont-beg (org-element-get-property :contents-begin next-object
)))
3124 (push (if (and (memq (car next-object
) org-element-recursive-objects
)
3126 ;; ... recursive. The CONT-BEG check is for
3127 ;; links, as some of them might not be recursive
3128 ;; (i.e. plain links).
3132 (org-element-get-property :contents-end next-object
))
3133 (org-element-parse-objects
3134 (point-min) (point-max) (reverse next-object
)
3135 ;; Restrict allowed objects. This is the
3136 ;; intersection of current restriction and next
3137 ;; object's restriction.
3139 (cdr (assq (car next-object
)
3140 org-element-object-restrictions
))))
3141 (if (not restriction
)
3145 (and (memq e restriction
) e
))
3147 ;; ... not recursive.
3150 (goto-char obj-end
)))
3151 ;; 3. Text after last object.
3152 (unless (= (point) end
)
3153 (push (buffer-substring-no-properties (point) end
) acc
))
3157 (defun org-element-get-next-object-candidates (limit restriction objects
)
3158 "Return an alist of candidates for the next object.
3160 LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
3161 the possible object types.
3163 Return value is an alist whose car is position and cdr the object
3164 type, as a string. There is an association for the closest
3165 object of each type within RESTRICTION when non-nil, or for every
3168 OBJECTS is the previous candidates alist."
3169 (let ((restriction (or restriction org-element-all-successors
))
3170 next-candidates types-to-search
)
3171 ;; If no previous result, search every object type in RESTRICTION.
3172 ;; Otherwise, keep potential candidates (old objects located after
3173 ;; point) and ask to search again those which had matched before.
3176 (if (< (cdr obj
) (point))
3177 (push (car obj
) types-to-search
)
3178 (push obj next-candidates
)))
3180 (setq types-to-search restriction
))
3181 ;; Call the appropriate "get-next" function for each type to
3182 ;; search and accumulate matches.
3185 (let* ((successor-fun
3187 (format "org-element-%s-successor"
3188 (or (cdr (assq type org-element-object-successor-alist
))
3190 (obj (funcall successor-fun limit
)))
3191 (and obj
(push obj next-candidates
))))
3198 ;;; Towards A Bijective Process
3200 ;; The parse tree obtained with `org-element-parse-buffer' is really
3201 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3202 ;; interpreted and expanded into a string with canonical Org
3203 ;; syntax. Hence `org-element-interpret-data'.
3205 ;; Data parsed from secondary strings, whose shape is slightly
3206 ;; different than the standard parse tree, is expanded with the
3207 ;; equivalent function `org-element-interpret-secondary'.
3209 ;; Both functions rely internally on
3210 ;; `org-element-interpret--affiliated-keywords'.
3211 (defun org-element-interpret-data (data &optional genealogy previous
)
3212 "Interpret a parse tree representing Org data.
3214 DATA is the parse tree to interpret.
3216 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3218 GENEALOGY is the list of its parents types.
3219 PREVIOUS is the type of the element or object at the same level
3222 Return Org syntax as a string."
3225 ;; BLOB can be an element, an object, a string, or nil.
3228 ((equal blob
"") nil
)
3229 ((stringp blob
) blob
)
3231 (let* ((type (car blob
))
3233 (if (eq type
'org-data
)
3235 (intern (format "org-element-%s-interpreter" type
))))
3238 ;; Full Org document.
3239 ((eq type
'org-data
)
3240 (org-element-interpret-data blob genealogy previous
))
3241 ;; Recursive objects.
3242 ((memq type org-element-recursive-objects
)
3243 (org-element-interpret-data
3244 blob
(cons type genealogy
) nil
))
3245 ;; Recursive elements.
3246 ((memq type org-element-greater-elements
)
3247 (org-element-normalize-string
3248 (org-element-interpret-data
3249 blob
(cons type genealogy
) nil
)))
3251 ((eq type
'paragraph
)
3253 (org-element-normalize-contents
3255 ;; When normalizing contents of an item,
3256 ;; ignore first line's indentation.
3258 (memq (car genealogy
)
3259 '(footnote-definiton item
))))))
3260 (org-element-interpret-data
3261 paragraph
(cons type genealogy
) nil
)))))
3262 (results (funcall interpreter blob contents
)))
3264 (setq previous type
)
3265 ;; Build white spaces.
3267 ((eq type
'org-data
) results
)
3268 ((memq type org-element-all-elements
)
3270 (org-element-interpret--affiliated-keywords blob
)
3271 (org-element-normalize-string results
)
3272 (make-string (org-element-get-property :post-blank blob
) 10)))
3276 (org-element-get-property :post-blank blob
) 32))))))))
3277 (org-element-get-contents data
) ""))
3279 (defun org-element-interpret-secondary (secondary)
3280 "Interpret SECONDARY string as Org syntax.
3282 SECONDARY-STRING is a nested list as returned by
3283 `org-element-parse-secondary-string'.
3285 Return interpreted string."
3286 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3287 (let ((s (if (listp secondary
) secondary
(list secondary
))))
3288 (org-element-interpret-data `(org-data nil
,@s
) nil nil
)))
3290 ;; Both functions internally use `org-element--affiliated-keywords'.
3292 (defun org-element-interpret--affiliated-keywords (element)
3293 "Return ELEMENT's affiliated keywords as Org syntax.
3294 If there is no affiliated keyword, return the empty string."
3295 (let ((keyword-to-org
3299 (when (member key org-element-dual-keywords
)
3300 (setq dual
(cdr value
) value
(car value
)))
3301 (concat "#+" key
(and dual
(format "[%s]" dual
)) ": "
3302 (if (member key org-element-parsed-keywords
)
3303 (org-element-interpret-secondary value
)
3308 (let ((value (org-element-get-property (intern (concat ":" key
)) element
)))
3310 (if (member key org-element-multiple-keywords
)
3311 (mapconcat (lambda (line)
3312 (funcall keyword-to-org key line
))
3314 (funcall keyword-to-org key value
)))))
3315 ;; Remove translated keywords.
3319 (and (not (assoc key org-element-keyword-translation-alist
)) key
))
3320 org-element-affiliated-keywords
))
3323 ;; Because interpretation of the parse tree must return the same
3324 ;; number of blank lines between elements and the same number of white
3325 ;; space after objects, some special care must be given to white
3328 ;; The first function, `org-element-normalize-string', ensures any
3329 ;; string different from the empty string will end with a single
3330 ;; newline character.
3332 ;; The second function, `org-element-normalize-contents', removes
3333 ;; global indentation from the contents of the current element.
3334 (defun org-element-normalize-string (s)
3335 "Ensure string S ends with a single newline character.
3337 If S isn't a string return it unchanged. If S is the empty
3338 string, return it. Otherwise, return a new string with a single
3339 newline character at its end."
3341 ((not (stringp s
)) s
)
3343 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3344 (replace-match "\n" nil nil s
)))))
3346 (defun org-element-normalize-contents (element &optional ignore-first
)
3347 "Normalize plain text in ELEMENT's contents.
3349 ELEMENT must only contain plain text and objects.
3351 The following changes are applied to plain text:
3352 - Remove global indentation, preserving relative one.
3355 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3356 indentation to compute maximal common indentation.
3358 Return the normalized element."
3360 (list (car element
) (nth 1 element
))
3361 (let ((contents (org-element-get-contents element
)))
3363 ((and (not ignore-first
) (not (stringp (car contents
)))) contents
)
3366 ;; 1. Remove tabs from each string in CONTENTS. Get maximal
3367 ;; common indentation (MCI) along the way.
3368 (let* ((ind-list (unless ignore-first
3369 (list (org-get-string-indentation (car contents
)))))
3371 (mapcar (lambda (object)
3372 (if (not (stringp object
))
3375 (object (org-remove-tabs object
)))
3376 (while (string-match "\n\\( *\\)" object start
)
3377 (setq start
(match-end 0))
3378 (push (length (match-string 1 object
))
3383 (apply 'min ind-list
)
3384 (throw 'exit contents
))))
3385 ;; 2. Remove that indentation from CONTENTS. First string
3386 ;; must be treated differently because it's the only one
3387 ;; whose indentation doesn't happen after a newline
3389 (let ((first-obj (car contents
)))
3390 (unless (or (not (stringp first-obj
)) ignore-first
)
3392 (cons (replace-regexp-in-string
3393 (format "\\` \\{%d\\}" mci
) "" first-obj
)
3395 (mapcar (lambda (object)
3396 (if (not (stringp object
))
3398 (replace-regexp-in-string
3399 (format "\n \\{%d\\}" mci
) "\n" object
)))
3406 ;; Once the structure of an Org file is well understood, it's easy to
3407 ;; implement some replacements for `forward-paragraph'
3408 ;; `backward-paragraph', namely `org-element-forward' and
3409 ;; `org-element-backward'.
3411 ;; Also, `org-transpose-elements' mimics the behaviour of
3412 ;; `transpose-words', at the element's level, whereas
3413 ;; `org-element-drag-forward', `org-element-drag-backward', and
3414 ;; `org-element-up' generalize, respectively, functions
3415 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3417 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3418 ;; smartly remove global indentation from buffer, making it possible
3419 ;; to use Org indent mode on a file created with hard indentation.
3421 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3422 ;; internally by some of the previously cited tools.
3423 (defsubst org-element-nested-p
(elem-A elem-B
)
3424 "Non-nil when elements ELEM-A and ELEM-B are nested."
3425 (let ((beg-A (org-element-get-property :begin elem-A
))
3426 (beg-B (org-element-get-property :begin elem-B
))
3427 (end-A (org-element-get-property :end elem-A
))
3428 (end-B (org-element-get-property :end elem-B
)))
3429 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
3430 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
3432 (defun org-element-swap-A-B (elem-A elem-B
)
3433 "Swap elements ELEM-A and ELEM-B.
3435 Leave point at the end of ELEM-A.
3437 Assume ELEM-A is before ELEM-B and that they are not nested."
3438 (goto-char (org-element-get-property :begin elem-A
))
3439 (let* ((beg-B (org-element-get-property :begin elem-B
))
3440 (end-B-no-blank (save-excursion
3441 (goto-char (org-element-get-property :end elem-B
))
3442 (skip-chars-backward " \r\t\n")
3445 (beg-A (org-element-get-property :begin elem-A
))
3446 (end-A-no-blank (save-excursion
3447 (goto-char (org-element-get-property :end elem-A
))
3448 (skip-chars-backward " \r\t\n")
3451 (body-A (buffer-substring beg-A end-A-no-blank
))
3452 (body-B (buffer-substring beg-B end-B-no-blank
))
3453 (between-A-B (buffer-substring end-A-no-blank beg-B
)))
3454 (delete-region beg-A end-B-no-blank
)
3455 (insert body-B between-A-B body-A
)
3456 (goto-char (org-element-get-property :end elem-B
))))
3458 (defun org-element-backward ()
3459 "Move backward by one element."
3461 (let* ((opoint (point))
3462 (element (org-element-at-point))
3463 (start-el-beg (org-element-get-property :begin element
)))
3464 ;; At an headline. The previous element is the previous sibling,
3465 ;; or the parent if any.
3467 ;; Already at the beginning of the current element: move to the
3468 ;; beginning of the previous one.
3469 ((= opoint start-el-beg
)
3471 (skip-chars-backward " \r\t\n")
3472 (let* ((prev-element (org-element-at-point))
3473 (itemp (org-in-item-p))
3475 (save-excursion (goto-char itemp
)
3476 (org-list-struct)))))
3477 ;; When moving into a new list, go directly at the
3478 ;; beginning of the top list structure.
3479 (if (and itemp
(<= (org-list-get-bottom-point struct
) opoint
))
3481 (goto-char (org-list-get-top-point struct
))
3482 (goto-char (org-element-get-property
3483 :begin
(org-element-at-point))))
3484 (goto-char (org-element-get-property :begin prev-element
))))
3485 (while (org-truely-invisible-p) (org-element-up)))
3486 ;; Else, move at the element beginning. One exception: if point
3487 ;; was in the blank lines after the end of a list, move directly
3491 (if (and (setq itemp
(org-in-item-p))
3492 (<= (org-list-get-bottom-point
3493 (save-excursion (goto-char itemp
)
3494 (setq struct
(org-list-struct))))
3496 (progn (goto-char (org-list-get-top-point struct
))
3497 (goto-char (org-element-get-property
3498 :begin
(org-element-at-point))))
3499 (goto-char start-el-beg
)))))))
3501 (defun org-element-drag-backward ()
3502 "Drag backward element at point."
3504 (let* ((pos (point))
3505 (elem (org-element-at-point)))
3506 (when (= (progn (goto-char (point-min))
3507 (org-skip-whitespace)
3509 (org-element-get-property :end elem
))
3510 (error "Cannot drag element backward"))
3511 (goto-char (org-element-get-property :begin elem
))
3512 (org-element-backward)
3513 (let ((prev-elem (org-element-at-point)))
3514 (when (or (org-element-nested-p elem prev-elem
)
3515 (and (eq (car elem
) 'headline
)
3516 (not (eq (car prev-elem
) 'headline
))))
3518 (error "Cannot drag element backward"))
3519 ;; Compute new position of point: it's shifted by PREV-ELEM
3521 (let ((size-prev (- (org-element-get-property :end prev-elem
)
3522 (org-element-get-property :begin prev-elem
))))
3523 (org-element-swap-A-B prev-elem elem
)
3524 (goto-char (- pos size-prev
))))))
3526 (defun org-element-drag-forward ()
3527 "Move forward element at point."
3529 (let* ((pos (point))
3530 (elem (org-element-at-point)))
3531 (when (= (point-max) (org-element-get-property :end elem
))
3532 (error "Cannot drag element forward"))
3533 (goto-char (org-element-get-property :end elem
))
3534 (let ((next-elem (org-element-at-point)))
3535 (when (or (org-element-nested-p elem next-elem
)
3536 (and (eq (car next-elem
) 'headline
)
3537 (not (eq (car elem
) 'headline
))))
3539 (error "Cannot drag element forward"))
3540 ;; Compute new position of point: it's shifted by NEXT-ELEM
3541 ;; body's length (without final blanks) and by the length of
3542 ;; blanks between ELEM and NEXT-ELEM.
3543 (let ((size-next (- (save-excursion
3544 (goto-char (org-element-get-property :end next-elem
))
3545 (skip-chars-backward " \r\t\n")
3548 (org-element-get-property :begin next-elem
)))
3549 (size-blank (- (org-element-get-property :end elem
)
3551 (goto-char (org-element-get-property :end elem
))
3552 (skip-chars-backward " \r\t\n")
3555 (org-element-swap-A-B elem next-elem
)
3556 (goto-char (+ pos size-next size-blank
))))))
3558 (defun org-element-forward ()
3559 "Move forward by one element."
3562 (cond ((eobp) (error "Cannot move further down"))
3563 ((looking-at "[ \t]*$")
3564 (org-skip-whitespace)
3565 (goto-char (if (eobp) (point) (point-at-bol))))
3567 (let ((element (org-element-at-point t
))
3570 ;; At an item: Either move to the next element inside, or
3571 ;; to its end if it's hidden.
3572 ((eq (car element
) 'item
)
3573 (if (org-element-get-property :hiddenp element
)
3574 (goto-char (org-element-get-property :end element
))
3576 (re-search-forward org-element-paragraph-separate nil t
)
3577 (org-skip-whitespace)
3578 (beginning-of-line)))
3579 ;; At a recursive element: Either move inside, or if it's
3580 ;; hidden, move to its end.
3581 ((memq (car element
) org-element-greater-elements
)
3582 (let ((cbeg (org-element-get-property :contents-begin element
)))
3584 (if (or (org-element-get-property :hiddenp element
)
3586 (org-element-get-property :end element
)
3588 ;; Else: move to the current element's end.
3589 (t (goto-char (org-element-get-property :end element
))))))))
3591 (defun org-element-mark-element ()
3592 "Put point at beginning of this element, mark at end.
3594 Interactively, if this command is repeated or (in Transient Mark
3595 mode) if the mark is active, it marks the next element after the
3596 ones already marked."
3598 (let (deactivate-mark)
3599 (if (or (and (eq last-command this-command
) (mark t
))
3600 (and transient-mark-mode mark-active
))
3604 (goto-char (org-element-get-property :end
(org-element-at-point)))))
3605 (let ((element (org-element-at-point)))
3607 (push-mark (org-element-get-property :end element
) t t
)
3608 (goto-char (org-element-get-property :begin element
))))))
3610 (defun org-narrow-to-element ()
3611 "Narrow buffer to current element."
3613 (let ((elem (org-element-at-point)))
3615 ((eq (car elem
) 'headline
)
3617 (org-element-get-property :begin elem
)
3618 (org-element-get-property :end elem
)))
3619 ((memq (car elem
) org-element-greater-elements
)
3621 (org-element-get-property :contents-begin elem
)
3622 (org-element-get-property :contents-end elem
)))
3625 (org-element-get-property :begin elem
)
3626 (org-element-get-property :end elem
))))))
3628 (defun org-transpose-elements ()
3629 "Transpose current and previous elements, keeping blank lines between.
3630 Point is moved after both elements."
3632 (org-skip-whitespace)
3634 (cur (org-element-at-point)))
3635 (when (= (save-excursion (goto-char (point-min))
3636 (org-skip-whitespace)
3638 (org-element-get-property :begin cur
))
3639 (error "No previous element"))
3640 (goto-char (org-element-get-property :begin cur
))
3642 (let ((prev (org-element-at-point)))
3643 (when (org-element-nested-p cur prev
)
3645 (error "Cannot transpose nested elements"))
3646 (org-element-swap-A-B prev cur
))))
3648 (defun org-element-unindent-buffer ()
3649 "Un-indent the visible part of the buffer.
3650 Relative indentation \(between items, inside blocks, etc.\) isn't
3653 (unless (eq major-mode
'org-mode
)
3654 (error "Cannot un-indent a buffer not in Org mode"))
3655 (let* ((parse-tree (org-element-parse-buffer 'greater-element
))
3656 unindent-tree
; For byte-compiler.
3660 (mapc (lambda (element)
3661 (if (eq (car element
) 'headline
)
3662 (funcall unindent-tree
3663 (org-element-get-contents element
))
3667 (org-element-get-property :begin element
)
3668 (org-element-get-property :end element
))
3669 (org-do-remove-indentation)))))
3670 (reverse contents
))))))
3671 (funcall unindent-tree
(org-element-get-contents parse-tree
))))
3673 (defun org-element-up ()
3674 "Move to upper element.
3675 Return position at the beginning of the upper element."
3677 (let ((opoint (point)) elem
)
3679 ((bobp) (error "No surrounding element"))
3680 ((org-with-limited-levels (org-at-heading-p))
3681 (or (org-up-heading-safe) (error "No surronding element")))
3682 ((and (org-at-item-p)
3683 (setq elem
(org-element-at-point))
3684 (let* ((top-list-p (zerop (org-element-get-property :level elem
))))
3686 ;; If parent is bound to be in the same list as the
3687 ;; original point, move to that parent.
3688 (let ((struct (org-element-get-property :structure elem
)))
3690 (org-list-get-parent
3691 (point-at-bol) struct
(org-list-parents-alist struct
))))))))
3693 (let* ((elem (or elem
(org-element-at-point)))
3694 (end (save-excursion
3695 (goto-char (org-element-get-property :end elem
))
3696 (skip-chars-backward " \r\t\n")
3700 (goto-char (org-element-get-property :begin elem
))
3702 (while (and (< (org-element-get-property
3703 :end
(setq prev-elem
(org-element-at-point)))
3706 (goto-char (org-element-get-property :begin prev-elem
))
3708 (if (and (bobp) (< (org-element-get-property :end prev-elem
) end
))
3709 (progn (goto-char opoint
)
3710 (error "No surrounding element"))
3711 (goto-char (org-element-get-property :begin prev-elem
))))))))
3714 (provide 'org-element
)
3715 ;;; org-element.el ends here