1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; This file is not part of GNU Emacs.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; An object can be defined anywhere on a line. It may span over more
29 ;; than a line but never contains a blank one. Objects belong to the
30 ;; following types: `emphasis', `entity', `export-snippet',
31 ;; `footnote-reference', `inline-babel-call', `inline-src-block',
32 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
33 ;; `statistics-cookie', `subscript', `superscript', `target',
34 ;; `time-stamp' and `verbatim'.
36 ;; An element always starts and ends at the beginning of a line. The
37 ;; only element's type containing objects is called a `paragraph'.
38 ;; Other types are: `comment', `comment-block', `example-block',
39 ;; `export-block', `fixed-width', `horizontal-rule', `keyword',
40 ;; `latex-environment', `babel-call', `property-drawer',
41 ;; `quote-section', `src-block', `table' and `verse-block'.
43 ;; Elements containing paragraphs are called greater elements.
44 ;; Concerned types are: `center-block', `drawer', `dynamic-block',
45 ;; `footnote-definition', `headline', `inlinetask', `item',
46 ;; `plain-list', `quote-block', `section' and `special-block'.
48 ;; Greater elements (excepted `headline', `item' and `section' types)
49 ;; and elements (excepted `keyword', `babel-call', and
50 ;; `property-drawer' types) can have a fixed set of keywords as
51 ;; attributes. Those are called "affiliated keywords", to distinguish
52 ;; them from others keywords, which are full-fledged elements. In
53 ;; particular, the "name" affiliated keyword allows to label almost
54 ;; any element in an Org buffer.
56 ;; Notwithstanding affiliated keywords, each greater element, element
57 ;; and object has a fixed set of properties attached to it. Among
58 ;; them, three are shared by all types: `:begin' and `:end', which
59 ;; refer to the beginning and ending buffer positions of the
60 ;; considered element or object, and `:post-blank', which holds the
61 ;; number of blank lines, or white spaces, at its end.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (i.e. an item tag, an headline name, a table
65 ;; cell). Such values are called "secondary strings".
67 ;; Lisp-wise, an element or an object can be represented as a list.
68 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
69 ;; TYPE is a symbol describing the Org element or object.
70 ;; PROPERTIES is the property list attached to it. See docstring of
71 ;; appropriate parsing function to get an exhaustive
73 ;; CONTENTS is a list of elements, objects or raw strings contained
74 ;; in the current element or object, when applicable.
76 ;; An Org buffer is a nested list of such elements and objects, whose
77 ;; type is `org-data' and properties is nil.
79 ;; The first part of this file implements a parser and an interpreter
80 ;; for each type of Org syntax.
82 ;; The next two parts introduce two accessors and a function
83 ;; retrieving the smallest element containing point (respectively
84 ;; `org-element-get-property', `org-element-get-contents' and
85 ;; `org-element-at-point').
87 ;; The following part creates a fully recursive buffer parser. It
88 ;; also provides a tool to map a function to elements or objects
89 ;; matching some criteria in the parse tree. Functions of interest
90 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
91 ;; extent, `org-element-parse-secondary-string'.
93 ;; The penultimate part is the cradle of an interpreter for the
94 ;; obtained parse tree: `org-element-interpret-data' (and its
95 ;; relative, `org-element-interpret-secondary').
97 ;; The library ends by furnishing a set of interactive tools for
98 ;; element's navigation and manipulation.
103 (eval-when-compile (require 'cl
))
105 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
110 ;; For each greater element type, we define a parser and an
113 ;; A parser (`item''s excepted) accepts no argument and represents the
114 ;; element or object as the list described above. An interpreter
115 ;; accepts two arguments: the list representation of the element or
116 ;; object, and its contents. The latter may be nil, depending on the
117 ;; element or object considered. It returns the appropriate Org
118 ;; syntax, as a string.
120 ;; Parsing functions must follow the naming convention:
121 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
122 ;; defined in `org-element-greater-elements'.
124 ;; Similarly, interpreting functions must follow the naming
125 ;; convention: org-element-TYPE-interpreter.
127 ;; With the exception of `headline' and `item' types, greater elements
128 ;; cannot contain other greater elements of their own type.
130 ;; Beside implementing a parser and an interpreter, adding a new
131 ;; greater element requires to tweak `org-element-guess-type'.
132 ;; Moreover, the newly defined type must be added to both
133 ;; `org-element-all-elements' and `org-element-greater-elements'.
138 (defun org-element-center-block-parser ()
139 "Parse a center block.
141 Return a list whose car is `center-block' and cdr is a plist
142 containing `:begin', `:end', `:hiddenp', `:contents-begin',
143 `:contents-end' and `:post-blank' keywords.
145 Assume point is at beginning or end of the block."
147 (let* ((case-fold-search t
)
151 (concat "^[ \t]*#\\+begin_center") nil t
)
152 (org-element-collect-affiliated-keywords)))
153 (begin (car keywords
))
154 (contents-begin (progn (forward-line) (point)))
155 (hidden (org-truely-invisible-p))
156 (contents-end (progn (re-search-forward
157 (concat "^[ \t]*#\\+end_center") nil t
)
159 (pos-before-blank (progn (forward-line) (point)))
160 (end (progn (org-skip-whitespace)
161 (if (eobp) (point) (point-at-bol)))))
166 :contents-begin
,contents-begin
167 :contents-end
,contents-end
168 :post-blank
,(count-lines pos-before-blank end
)
169 ,@(cadr keywords
))))))
171 (defun org-element-center-block-interpreter (center-block contents
)
172 "Interpret CENTER-BLOCK element as Org syntax.
173 CONTENTS is the contents of the element."
174 (format "#+begin_center\n%s#+end_center" contents
))
179 (defun org-element-drawer-parser ()
182 Return a list whose car is `drawer' and cdr is a plist containing
183 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
184 `:contents-end' and `:post-blank' keywords.
186 Assume point is at beginning of drawer."
188 (let* ((case-fold-search t
)
189 (name (progn (looking-at org-drawer-regexp
)
190 (org-match-string-no-properties 1)))
191 (keywords (org-element-collect-affiliated-keywords))
192 (begin (car keywords
))
193 (contents-begin (progn (forward-line) (point)))
194 (hidden (org-truely-invisible-p))
195 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
197 (pos-before-blank (progn (forward-line) (point)))
198 (end (progn (org-skip-whitespace)
199 (if (eobp) (point) (point-at-bol)))))
205 :contents-begin
,contents-begin
206 :contents-end
,contents-end
207 :post-blank
,(count-lines pos-before-blank end
)
208 ,@(cadr keywords
))))))
210 (defun org-element-drawer-interpreter (drawer contents
)
211 "Interpret DRAWER element as Org syntax.
212 CONTENTS is the contents of the element."
213 (format ":%s:\n%s:END:"
214 (org-element-get-property :drawer-name drawer
)
220 (defun org-element-dynamic-block-parser ()
221 "Parse a dynamic block.
223 Return a list whose car is `dynamic-block' and cdr is a plist
224 containing `:block-name', `:begin', `:end', `:hiddenp',
225 `:contents-begin', `:contents-end', `:arguments' and
226 `:post-blank' keywords.
228 Assume point is at beginning of dynamic block."
230 (let* ((case-fold-search t
)
231 (name (progn (looking-at org-dblock-start-re
)
232 (org-match-string-no-properties 1)))
233 (arguments (org-match-string-no-properties 3))
234 (keywords (org-element-collect-affiliated-keywords))
235 (begin (car keywords
))
236 (contents-begin (progn (forward-line) (point)))
237 (hidden (org-truely-invisible-p))
238 (contents-end (progn (re-search-forward org-dblock-end-re nil t
)
240 (pos-before-blank (progn (forward-line) (point)))
241 (end (progn (org-skip-whitespace)
242 (if (eobp) (point) (point-at-bol)))))
247 :arguments
,arguments
249 :contents-begin
,contents-begin
250 :contents-end
,contents-end
251 :post-blank
,(count-lines pos-before-blank end
)
252 ,@(cadr keywords
))))))
254 (defun org-element-dynamic-block-interpreter (dynamic-block contents
)
255 "Interpret DYNAMIC-BLOCK element as Org syntax.
256 CONTENTS is the contents of the element."
257 (format "#+BEGIN: %s%s\n%s#+END:"
258 (org-element-get-property :block-name dynamic-block
)
259 (let ((args (org-element-get-property :arguments dynamic-block
)))
260 (and arg
(concat " " args
)))
264 ;;;; Footnote Definition
266 (defun org-element-footnote-definition-parser ()
267 "Parse a footnote definition.
269 Return a list whose car is `footnote-definition' and cdr is
270 a plist containing `:label', `:begin' `:end', `:contents-begin',
271 `:contents-end' and `:post-blank' keywords."
273 (let* ((f-def (org-footnote-at-definition-p))
275 (keywords (progn (goto-char (nth 1 f-def
))
276 (org-element-collect-affiliated-keywords)))
277 (begin (car keywords
))
278 (contents-begin (progn (looking-at (concat "\\[" label
"\\]"))
279 (goto-char (match-end 0))
280 (org-skip-whitespace)
282 (end (goto-char (nth 2 f-def
)))
283 (contents-end (progn (skip-chars-backward " \r\t\n")
286 `(footnote-definition
290 :contents-begin
,contents-begin
291 :contents-end
,contents-end
292 :post-blank
,(count-lines contents-end end
)
293 ,@(cadr keywords
))))))
295 (defun org-element-footnote-definition-interpreter (footnote-definition contents
)
296 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
297 CONTENTS is the contents of the footnote-definition."
298 (concat (format "[%s]" (org-element-get-property :label footnote-definition
))
305 (defun org-element-headline-parser ()
308 Return a list whose car is `headline' and cdr is a plist
309 containing `:raw-value', `:title', `:begin', `:end',
310 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
311 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
312 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
313 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
316 The plist also contains any property set in the property drawer,
317 with its name in lowercase, the underscores replaced with hyphens
318 and colons at the beginning (i.e. `:custom-id').
320 Assume point is at beginning of the headline."
322 (let* ((components (org-heading-components))
323 (level (nth 1 components
))
324 (todo (nth 2 components
))
326 (if (member todo org-done-keywords
) 'done
'todo
)))
327 (tags (nth 5 components
))
328 (raw-value (nth 4 components
))
329 (quotedp (string-match (format "^%s +" org-quote-string
) raw-value
))
330 (commentedp (string-match
331 (format "^%s +" org-comment-string
) raw-value
))
333 (string-match (format ":%s:" org-archive-tag
) tags
)))
334 (footnote-section-p (and org-footnote-section
335 (string= org-footnote-section raw-value
)))
336 (standard-props (let (plist)
339 (let ((p-name (downcase (car p
))))
340 (while (string-match "_" p-name
)
342 (replace-match "-" nil nil p-name
)))
343 (setq p-name
(intern (concat ":" p-name
)))
345 (plist-put plist p-name
(cdr p
)))))
346 (org-entry-properties nil
'standard
))
348 (time-props (org-entry-properties nil
'special
"CLOCK"))
349 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
350 (deadline (cdr (assoc "DEADLINE" time-props
)))
351 (clock (cdr (assoc "CLOCK" time-props
)))
352 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
354 (pos-after-head (save-excursion (forward-line) (point)))
355 (contents-begin (save-excursion (forward-line)
356 (org-skip-whitespace)
357 (if (eobp) (point) (point-at-bol))))
358 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
359 (end (progn (goto-char (org-end-of-subtree t t
))))
360 (contents-end (progn (skip-chars-backward " \r\t\n")
364 ;; Clean RAW-VALUE from any quote or comment string.
365 (when (or quotedp commentedp
)
367 (replace-regexp-in-string
368 (concat "\\(" org-quote-string
"\\|" org-comment-string
"\\) +")
371 ;; Clean TAGS from archive tag, if any.
374 (and (not (string= tags
(format ":%s:" org-archive-tag
)))
375 (replace-regexp-in-string
376 (concat org-archive-tag
":") "" tags
)))
377 (when (string= tags
":") (setq tags nil
)))
379 (setq title
(org-element-parse-secondary-string
381 (cdr (assq 'headline org-element-string-restrictions
))))
383 (:raw-value
,raw-value
387 :pre-blank
,(count-lines pos-after-head contents-begin
)
389 :contents-begin
,contents-begin
390 :contents-end
,contents-end
392 :priority
,(nth 3 components
)
395 :todo-type
,todo-type
396 :scheduled
,scheduled
398 :timestamp
,timestamp
400 :post-blank
,(count-lines contents-end end
)
401 :footnote-section-p
,footnote-section-p
402 :archivedp
,archivedp
403 :commentedp
,commentedp
405 ,@standard-props
)))))
407 (defun org-element-headline-interpreter (headline contents
)
408 "Interpret HEADLINE element as Org syntax.
409 CONTENTS is the contents of the element."
410 (let* ((level (org-element-get-property :level headline
))
411 (todo (org-element-get-property :todo-keyword headline
))
412 (priority (org-element-get-property :priority headline
))
413 (title (org-element-get-property :raw-value headline
))
414 (tags (let ((tag-string (org-element-get-property :tags headline
))
415 (archivedp (org-element-get-property :archivedp headline
)))
417 ((and (not tag-string
) archivedp
)
418 (format ":%s:" org-archive-tag
))
419 (archivedp (concat ":" org-archive-tag tag-string
))
421 (commentedp (org-element-get-property :commentedp headline
))
422 (quotedp (org-element-get-property :quotedp headline
))
423 (pre-blank (org-element-get-property :pre-blank headline
))
424 (heading (concat (make-string level ?
*)
425 (and todo
(concat " " todo
))
426 (and quotedp
(concat " " org-quote-string
))
427 (and commentedp
(concat " " org-comment-string
))
428 (and priority
(concat " " priority
))
429 (cond ((and org-footnote-section
430 (org-element-get-property
431 :footnote-section-p headline
))
432 (concat " " org-footnote-section
))
433 (title (concat " " title
)))))
436 (let ((tags-len (length tags
)))
439 ((zerop org-tags-column
) (1+ tags-len
))
440 ((< org-tags-column
0)
441 (max (- (+ org-tags-column
(length heading
)))
443 (t (max (+ (- org-tags-column
(length heading
))
445 (1+ tags-len
)))))))))
446 (concat heading
(and tags
(format tags-fmt tags
))
447 (make-string (1+ pre-blank
) 10)
453 (defun org-element-inlinetask-parser ()
454 "Parse an inline task.
456 Return a list whose car is `inlinetask' and cdr is a plist
457 containing `:raw-value', `:title', `:begin', `:end', `:hiddenp',
458 `:contents-begin' and `:contents-end', `:level', `:priority',
459 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
460 `:scheduled', `:deadline', `:timestamp', `:clock' and
461 `:post-blank' keywords.
463 The plist also contains any property set in the property drawer,
464 with its name in lowercase, the underscores replaced with hyphens
465 and colons at the beginning (i.e. `:custom-id').
467 Assume point is at beginning of the inline task."
469 (let* ((keywords (org-element-collect-affiliated-keywords))
470 (begin (car keywords
))
471 (components (org-heading-components))
472 (todo (nth 2 components
))
474 (if (member todo org-done-keywords
) 'done
'todo
)))
475 (raw-value (nth 4 components
))
476 (standard-props (let (plist)
479 (let ((p-name (downcase (car p
))))
480 (while (string-match "_" p-name
)
482 (replace-match "-" nil nil p-name
)))
483 (setq p-name
(intern (concat ":" p-name
)))
485 (plist-put plist p-name
(cdr p
)))))
486 (org-entry-properties nil
'standard
))
488 (time-props (org-entry-properties nil
'special
"CLOCK"))
489 (scheduled (cdr (assoc "SCHEDULED" time-props
)))
490 (deadline (cdr (assoc "DEADLINE" time-props
)))
491 (clock (cdr (assoc "CLOCK" time-props
)))
492 (timestamp (cdr (assoc "TIMESTAMP" time-props
)))
493 (title (org-element-parse-secondary-string
495 (cdr (assq 'inlinetask org-element-string-restrictions
))))
496 (contents-begin (save-excursion (forward-line) (point)))
497 (hidden (org-truely-invisible-p))
498 (pos-before-blank (org-inlinetask-goto-end))
499 ;; In the case of a single line task, CONTENTS-BEGIN and
500 ;; CONTENTS-END might overlap.
501 (contents-end (max contents-begin
502 (save-excursion (forward-line -
1) (point))))
503 (end (progn (org-skip-whitespace)
504 (if (eobp) (point) (point-at-bol)))))
506 (:raw-value
,raw-value
510 :hiddenp
,(and (> contents-end contents-begin
) hidden
)
511 :contents-begin
,contents-begin
512 :contents-end
,contents-end
513 :level
,(nth 1 components
)
514 :priority
,(nth 3 components
)
515 :tags
,(nth 5 components
)
517 :todo-type
,todo-type
518 :scheduled
,scheduled
520 :timestamp
,timestamp
522 :post-blank
,(count-lines pos-before-blank end
)
524 ,@(cadr keywords
))))))
526 (defun org-element-inlinetask-interpreter (inlinetask contents
)
527 "Interpret INLINETASK element as Org syntax.
528 CONTENTS is the contents of inlinetask."
529 (let* ((level (org-element-get-property :level inlinetask
))
530 (todo (org-element-get-property :todo-keyword inlinetask
))
531 (priority (org-element-get-property :priority inlinetask
))
532 (title (org-element-get-property :raw-value inlinetask
))
533 (tags (org-element-get-property :tags inlinetask
))
534 (task (concat (make-string level ?
*)
535 (and todo
(concat " " todo
))
536 (and priority
(concat " " priority
))
537 (and title
(concat " " title
))))
542 ((zerop org-tags-column
) 1)
543 ((< 0 org-tags-column
)
544 (max (+ org-tags-column
548 (t (max (- org-tags-column
(length inlinetask
))
550 (concat inlinetask
(and tags
(format tags-fmt tags
) "\n" contents
))))
555 (defun org-element-item-parser (struct)
558 STRUCT is the structure of the plain list.
560 Return a list whose car is `item' and cdr is a plist containing
561 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
562 `:checkbox', `:counter', `:tag', `:raw-tag', `:structure',
563 `:hiddenp' and `:post-blank' keywords.
565 Assume point is at the beginning of the item."
568 (let* ((begin (point))
569 (bullet (org-list-get-bullet (point) struct
))
570 (checkbox (let ((box (org-list-get-checkbox begin struct
)))
571 (cond ((equal "[ ]" box
) 'off
)
572 ((equal "[X]" box
) 'on
)
573 ((equal "[-]" box
) 'trans
))))
574 (counter (let ((c (org-list-get-counter begin struct
)))
577 ((string-match "[A-Za-z]" c
)
578 (- (string-to-char (upcase (match-string 0 c
)))
580 ((string-match "[0-9]+" c
)
581 (string-to-number (match-string 0 c
))))))
582 (raw-tag (org-list-get-tag begin struct
))
584 (org-element-parse-secondary-string
586 (cdr (assq 'item org-element-string-restrictions
)))))
587 (end (org-list-get-item-end begin struct
))
588 (contents-begin (progn (looking-at org-list-full-item-re
)
589 (goto-char (match-end 0))
590 (org-skip-whitespace)
591 ;; If first line isn't empty,
592 ;; contents really start at the text
593 ;; after item's meta-data.
594 (if (= (point-at-bol) begin
) (point)
596 (hidden (progn (forward-line)
597 (and (not (= (point) end
))
598 (org-truely-invisible-p))))
599 (contents-end (progn (goto-char end
)
600 (skip-chars-backward " \r\t\n")
607 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
608 ;; up in the case of an empty item separated
609 ;; from the next by a blank line. Thus, ensure
610 ;; the former is always the smallest of two.
611 :contents-begin
,(min contents-begin contents-end
)
612 :contents-end
,(max contents-begin contents-end
)
619 :post-blank
,(count-lines contents-end end
))))))
621 (defun org-element-item-interpreter (item contents
)
622 "Interpret ITEM element as Org syntax.
623 CONTENTS is the contents of the element."
625 (let* ((beg (org-element-get-property :begin item
))
626 (struct (org-element-get-property :structure item
))
627 (pre (org-list-prevs-alist struct
))
628 (bul (org-element-get-property :bullet item
)))
629 (org-list-bullet-string
630 (if (not (eq (org-list-get-list-type beg struct pre
) 'ordered
)) "-"
634 (org-list-get-item-number
635 beg struct pre
(org-list-parents-alist struct
))))))
638 (if (eq org-plain-list-ordered-item-terminator ?\
)) ")"
640 (checkbox (org-element-get-property :checkbox item
))
641 (counter (org-element-get-property :counter item
))
642 (tag (org-element-get-property :raw-tag item
))
643 ;; Compute indentation.
644 (ind (make-string (length bullet
) 32)))
648 (and counter
(format "[@%d] " counter
))
650 ((eq checkbox
'on
) "[X] ")
651 ((eq checkbox
'off
) "[ ] ")
652 ((eq checkbox
'trans
) "[-] "))
653 (and tag
(format "%s :: " tag
))
655 (replace-regexp-in-string "\\(^\\)[ \t]*\\S-" ind contents nil nil
1)))))
660 (defun org-element-plain-list-parser (&optional structure
)
663 Optional argument STRUCTURE, when non-nil, is the structure of
664 the plain list being parsed.
666 Return a list whose car is `plain-list' and cdr is a plist
667 containing `:type', `:begin', `:end', `:contents-begin' and
668 `:contents-end', `:level', `:structure' and `:post-blank'
671 Assume point is at one of the list items."
673 (let* ((struct (or structure
(org-list-struct)))
674 (prevs (org-list-prevs-alist struct
))
675 (parents (org-list-parents-alist struct
))
676 (type (org-list-get-list-type (point) struct prevs
))
677 (contents-begin (goto-char
678 (org-list-get-list-begin (point) struct prevs
)))
679 (keywords (org-element-collect-affiliated-keywords))
680 (begin (car keywords
))
681 (contents-end (goto-char
682 (org-list-get-list-end (point) struct prevs
)))
683 (end (save-excursion (org-skip-whitespace)
684 (if (eobp) (point) (point-at-bol))))
687 (let ((item contents-begin
))
690 (org-list-get-list-begin item struct prevs
)
693 ;; Blank lines below list belong to the top-level list only.
695 (setq end
(min (org-list-get-bottom-point struct
)
696 (progn (org-skip-whitespace)
697 (if (eobp) (point) (point-at-bol))))))
703 :contents-begin
,contents-begin
704 :contents-end
,contents-end
707 :post-blank
,(count-lines contents-end end
)
708 ,@(cadr keywords
))))))
710 (defun org-element-plain-list-interpreter (plain-list contents
)
711 "Interpret PLAIN-LIST element as Org syntax.
712 CONTENTS is the contents of the element."
718 (defun org-element-quote-block-parser ()
719 "Parse a quote block.
721 Return a list whose car is `quote-block' and cdr is a plist
722 containing `:begin', `:end', `:hiddenp', `:contents-begin',
723 `:contents-end' and `:post-blank' keywords.
725 Assume point is at beginning or end of the block."
727 (let* ((case-fold-search t
)
731 (concat "^[ \t]*#\\+begin_quote") nil t
)
732 (org-element-collect-affiliated-keywords)))
733 (begin (car keywords
))
734 (contents-begin (progn (forward-line) (point)))
735 (hidden (org-truely-invisible-p))
736 (contents-end (progn (re-search-forward
737 (concat "^[ \t]*#\\+end_quote") nil t
)
739 (pos-before-blank (progn (forward-line) (point)))
740 (end (progn (org-skip-whitespace)
741 (if (eobp) (point) (point-at-bol)))))
746 :contents-begin
,contents-begin
747 :contents-end
,contents-end
748 :post-blank
,(count-lines pos-before-blank end
)
749 ,@(cadr keywords
))))))
751 (defun org-element-quote-block-interpreter (quote-block contents
)
752 "Interpret QUOTE-BLOCK element as Org syntax.
753 CONTENTS is the contents of the element."
754 (format "#+begin_quote\n%s#+end_quote" contents
))
759 (defun org-element-section-parser ()
762 Return a list whose car is `section' and cdr is a plist
763 containing `:begin', `:end', `:contents-begin', `contents-end'
764 and `:post-blank' keywords."
766 ;; Beginning of section is the beginning of the first non-blank
767 ;; line after previous headline.
768 (org-with-limited-levels
771 (outline-previous-heading)
772 (if (not (org-at-heading-p)) (point)
773 (forward-line) (org-skip-whitespace) (point-at-bol))))
774 (end (progn (outline-next-heading) (point)))
775 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
781 :contents-begin
,begin
782 :contents-end
,pos-before-blank
783 :post-blank
,(count-lines pos-before-blank end
)))))))
785 (defun org-element-section-interpreter (section contents
)
786 "Interpret SECTION element as Org syntax.
787 CONTENTS is the contents of the element."
793 (defun org-element-special-block-parser ()
794 "Parse a special block.
796 Return a list whose car is `special-block' and cdr is a plist
797 containing `:type', `:begin', `:end', `:hiddenp',
798 `:contents-begin', `:contents-end' and `:post-blank' keywords.
800 Assume point is at beginning or end of the block."
802 (let* ((case-fold-search t
)
803 (type (progn (looking-at
804 "[ \t]*#\\+\\(?:begin\\|end\\)_\\([-A-Za-z0-9]+\\)")
805 (org-match-string-no-properties 1)))
809 (concat "^[ \t]*#\\+begin_" type
) nil t
)
810 (org-element-collect-affiliated-keywords)))
811 (begin (car keywords
))
812 (contents-begin (progn (forward-line) (point)))
813 (hidden (org-truely-invisible-p))
814 (contents-end (progn (re-search-forward
815 (concat "^[ \t]*#\\+end_" type
) nil t
)
817 (pos-before-blank (progn (forward-line) (point)))
818 (end (progn (org-skip-whitespace)
819 (if (eobp) (point) (point-at-bol)))))
825 :contents-begin
,contents-begin
826 :contents-end
,contents-end
827 :post-blank
,(count-lines pos-before-blank end
)
828 ,@(cadr keywords
))))))
830 (defun org-element-special-block-interpreter (special-block contents
)
831 "Interpret SPECIAL-BLOCK element as Org syntax.
832 CONTENTS is the contents of the element."
833 (let ((block-type (org-element-get-property :type special-block
)))
834 (format "#+begin_%s\n%s#+end_%s" block-type contents block-type
)))
840 ;; For each element, a parser and an interpreter are also defined.
841 ;; Both follow the same naming convention used for greater elements.
843 ;; Also, as for greater elements, adding a new element type is done
844 ;; through the following steps: implement a parser and an interpreter,
845 ;; tweak `org-element-guess-type' so that it recognizes the new type
846 ;; and add that new type to `org-element-all-elements'.
848 ;; As a special case, when the newly defined type is a block type,
849 ;; `org-element-non-recursive-block-alist' has to be modified
855 (defun org-element-babel-call-parser ()
858 Return a list whose car is `babel-call' and cdr is a plist
859 containing `:begin', `:end', `:info' and `:post-blank' as
862 (let ((info (progn (looking-at org-babel-block-lob-one-liner-regexp
)
863 (org-babel-lob-get-info)))
865 (pos-before-blank (progn (forward-line) (point)))
866 (end (progn (org-skip-whitespace)
867 (if (eobp) (point) (point-at-bol)))))
872 :post-blank
,(count-lines pos-before-blank end
))))))
874 (defun org-element-babel-call-interpreter (inline-babel-call contents
)
875 "Interpret INLINE-BABEL-CALL object as Org syntax.
877 (let* ((babel-info (org-element-get-property :info inline-babel-call
))
878 (main-source (car babel-info
))
879 (post-options (nth 1 babel-info
)))
881 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
882 ;; Remove redundant square brackets.
884 (match-string 1 main-source
) nil nil main-source
)
886 (and post-options
(format "[%s]" post-options
)))))
891 (defun org-element-comment-parser ()
894 Return a list whose car is `comment' and cdr is a plist
895 containing `:begin', `:end', `:value' and `:post-blank'
897 (let (beg-coms begin end end-coms keywords
)
900 ;; First type of comment: comments at column 0.
901 (let ((comment-re "^\\([^#]\\|#\\+[a-z]\\)"))
903 (re-search-backward comment-re nil
'move
)
904 (if (bobp) (setq keywords nil beg-coms
(point))
906 (setq keywords
(org-element-collect-affiliated-keywords)
908 (re-search-forward comment-re nil
'move
)
909 (setq end-coms
(if (eobp) (point) (match-beginning 0))))
910 ;; Second type of comment: indented comments.
911 (let ((comment-re "[ \t]*#\\+\\(?: \\|$\\)"))
913 (while (and (not (bobp)) (looking-at comment-re
))
915 (unless (looking-at comment-re
) (forward-line)))
916 (setq beg-coms
(point))
917 (setq keywords
(org-element-collect-affiliated-keywords))
918 ;; Get comments ending. This may not be accurate if
919 ;; commented lines within an item are followed by commented
920 ;; lines outside of the list. Though, parser will always
921 ;; get it right as it already knows surrounding element and
922 ;; has narrowed buffer to its contents.
923 (while (looking-at comment-re
) (forward-line))
924 (setq end-coms
(point))))
925 ;; Find position after blank.
927 (org-skip-whitespace)
928 (setq end
(if (eobp) (point) (point-at-bol))))
930 (:begin
,(or (car keywords
) beg-coms
)
932 :value
,(buffer-substring-no-properties beg-coms end-coms
)
933 :post-blank
,(count-lines end-coms end
)
934 ,@(cadr keywords
)))))
936 (defun org-element-comment-interpreter (comment contents
)
937 "Interpret COMMENT element as Org syntax.
939 (org-element-get-property :value comment
))
944 (defun org-element-comment-block-parser ()
945 "Parse an export block.
947 Return a list whose car is `comment-block' and cdr is a plist
948 containing `:begin', `:end', `:hiddenp', `:value' and
949 `:post-blank' keywords."
952 (let* ((case-fold-search t
)
954 (re-search-backward "^[ \t]*#\\+begin_comment" nil t
)
955 (org-element-collect-affiliated-keywords)))
956 (begin (car keywords
))
957 (contents-begin (progn (forward-line) (point)))
958 (hidden (org-truely-invisible-p))
959 (contents-end (progn (re-search-forward
960 "^[ \t]*#\\+end_comment" nil t
)
962 (pos-before-blank (progn (forward-line) (point)))
963 (end (progn (org-skip-whitespace)
964 (if (eobp) (point) (point-at-bol))))
965 (value (buffer-substring-no-properties contents-begin contents-end
)))
971 :post-blank
,(count-lines pos-before-blank end
)
972 ,@(cadr keywords
))))))
974 (defun org-element-comment-block-interpreter (comment-block contents
)
975 "Interpret COMMENT-BLOCK element as Org syntax.
977 (concat "#+begin_comment\n"
978 (org-remove-indentation
979 (org-element-get-property :value comment-block
))
985 (defun org-element-example-block-parser ()
986 "Parse an example block.
988 Return a list whose car is `example' and cdr is a plist
989 containing `:begin', `:end', `:options', `:hiddenp', `:value' and
990 `:post-blank' keywords."
993 (let* ((case-fold-search t
)
996 "^[ \t]*#\\+begin_example\\(?: +\\(.*\\)\\)?" nil t
)
997 (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
1003 (re-search-forward "^[ \t]*#\\+end_example" nil t
)
1005 (value (buffer-substring-no-properties contents-begin contents-end
))
1006 (pos-before-blank (progn (forward-line) (point)))
1007 (end (progn (org-skip-whitespace)
1008 (if (eobp) (point) (point-at-bol)))))
1015 :post-blank
,(count-lines pos-before-blank end
)
1016 ,@(cadr keywords
))))))
1018 (defun org-element-example-block-interpreter (example-block contents
)
1019 "Interpret EXAMPLE-BLOCK element as Org syntax.
1021 (let ((options (org-element-get-property :options example-block
)))
1022 (concat "#+begin_example" (and options
(concat " " options
)) "\n"
1023 (org-remove-indentation
1024 (org-element-get-property :value example-block
))
1030 (defun org-element-export-block-parser ()
1031 "Parse an export block.
1033 Return a list whose car is `export-block' and cdr is a plist
1034 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1035 `:post-blank' keywords."
1038 (let* ((case-fold-search t
)
1040 (type (progn (re-search-backward
1041 (concat "[ \t]*#\\+begin_"
1042 (org-re "\\([[:alnum:]]+\\)")))
1043 (downcase (org-match-string-no-properties 1))))
1044 (keywords (org-element-collect-affiliated-keywords))
1045 (begin (car keywords
))
1046 (contents-begin (progn (forward-line) (point)))
1047 (hidden (org-truely-invisible-p))
1048 (contents-end (progn (re-search-forward
1049 (concat "^[ \t]*#\\+end_" type
) nil t
)
1051 (pos-before-blank (progn (forward-line) (point)))
1052 (end (progn (org-skip-whitespace)
1053 (if (eobp) (point) (point-at-bol))))
1054 (value (buffer-substring-no-properties contents-begin contents-end
)))
1061 :post-blank
,(count-lines pos-before-blank end
)
1062 ,@(cadr keywords
))))))
1064 (defun org-element-export-block-interpreter (export-block contents
)
1065 "Interpret EXPORT-BLOCK element as Org syntax.
1067 (let ((type (org-element-get-property :type export-block
)))
1068 (concat (format "#+begin_%s\n" type
)
1069 (org-element-get-property :value export-block
)
1070 (format "#+end_%s" type
))))
1075 (defun org-element-fixed-width-parser ()
1076 "Parse a fixed-width section.
1078 Return a list whose car is `fixed-width' and cdr is a plist
1079 containing `:begin', `:end', `:value' and `:post-blank'
1081 (let ((fixed-re "[ \t]*:\\( \\|$\\)")
1082 beg-area begin end value pos-before-blank keywords
)
1084 ;; Move to the beginning of the fixed-width area.
1086 (while (and (not (bobp)) (looking-at fixed-re
))
1088 (unless (looking-at fixed-re
) (forward-line 1)))
1089 (setq beg-area
(point))
1090 ;; Get affiliated keywords, if any.
1091 (setq keywords
(org-element-collect-affiliated-keywords))
1092 ;; Store true beginning of element.
1093 (setq begin
(car keywords
))
1094 ;; Get ending of fixed-width area. If point is in a list,
1095 ;; ensure to not get outside of it.
1096 (let* ((itemp (org-in-item-p))
1098 (org-list-get-bottom-point
1099 (save-excursion (goto-char itemp
) (org-list-struct)))
1101 (while (and (looking-at fixed-re
) (< (point) max-pos
))
1103 (setq pos-before-blank
(point))
1104 ;; Find position after blank
1105 (org-skip-whitespace)
1106 (setq end
(if (eobp) (point) (point-at-bol)))
1108 (setq value
(buffer-substring-no-properties beg-area pos-before-blank
)))
1113 :post-blank
,(count-lines pos-before-blank end
)
1114 ,@(cadr keywords
)))))
1116 (defun org-element-fixed-width-interpreter (fixed-width contents
)
1117 "Interpret FIXED-WIDTH element as Org syntax.
1119 (org-remove-indentation (org-element-get-property :value fixed-width
)))
1122 ;;;; Horizontal Rule
1124 (defun org-element-horizontal-rule-parser ()
1125 "Parse an horizontal rule.
1127 Return a list whose car is `horizontal-rule' and cdr is
1128 a plist containing `:begin', `:end' and `:post-blank'
1131 (let* ((keywords (org-element-collect-affiliated-keywords))
1132 (begin (car keywords
))
1133 (post-hr (progn (forward-line) (point)))
1134 (end (progn (org-skip-whitespace)
1135 (if (eobp) (point) (point-at-bol)))))
1139 :post-blank
,(count-lines post-hr end
)
1140 ,@(cadr keywords
))))))
1142 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents
)
1143 "Interpret HORIZONTAL-RULE element as Org syntax.
1150 (defun org-element-keyword-parser ()
1151 "Parse a keyword at point.
1153 Return a list whose car is `keyword' and cdr is a plist
1154 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1157 (let* ((begin (point))
1158 (key (progn (looking-at
1159 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1160 (org-match-string-no-properties 1)))
1161 (value (org-trim (buffer-substring-no-properties
1162 (match-end 0) (point-at-eol))))
1163 (pos-before-blank (progn (forward-line) (point)))
1164 (end (progn (org-skip-whitespace)
1165 (if (eobp) (point) (point-at-bol)))))
1171 :post-blank
,(count-lines pos-before-blank end
))))))
1173 (defun org-element-keyword-interpreter (keyword contents
)
1174 "Interpret KEYWORD element as Org syntax.
1177 (org-element-get-property :key keyword
)
1178 (org-element-get-property :value keyword
)))
1181 ;;;; Latex Environment
1183 (defun org-element-latex-environment-parser ()
1184 "Parse a LaTeX environment.
1186 Return a list whose car is `latex-environment' and cdr is a plist
1187 containing `:begin', `:end', `:value' and `:post-blank' keywords."
1190 (let* ((case-fold-search t
)
1191 (contents-begin (re-search-backward "^[ \t]*\\\\begin" nil t
))
1192 (keywords (org-element-collect-affiliated-keywords))
1193 (begin (car keywords
))
1194 (contents-end (progn (re-search-forward "^[ \t]*\\\\end")
1197 (value (buffer-substring-no-properties contents-begin contents-end
))
1198 (end (progn (org-skip-whitespace)
1199 (if (eobp) (point) (point-at-bol)))))
1204 :post-blank
,(count-lines contents-end end
)
1205 ,@(cadr keywords
))))))
1207 (defun org-element-latex-environment-interpreter (latex-environment contents
)
1208 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1210 (org-element-get-property :value latex-environment
))
1215 (defun org-element-paragraph-parser ()
1218 Return a list whose car is `paragraph' and cdr is a plist
1219 containing `:begin', `:end', `:contents-begin' and
1220 `:contents-end' and `:post-blank' keywords.
1222 Assume point is at the beginning of the paragraph."
1224 (let* ((contents-begin (point))
1225 (keywords (org-element-collect-affiliated-keywords))
1226 (begin (car keywords
))
1227 (contents-end (progn
1229 (if (re-search-forward
1230 org-element-paragraph-separate nil
'm
)
1231 (progn (forward-line -
1) (end-of-line) (point))
1233 (pos-before-blank (progn (forward-line) (point)))
1234 (end (progn (org-skip-whitespace)
1235 (if (eobp) (point) (point-at-bol)))))
1239 :contents-begin
,contents-begin
1240 :contents-end
,contents-end
1241 :post-blank
,(count-lines pos-before-blank end
)
1242 ,@(cadr keywords
))))))
1244 (defun org-element-paragraph-interpreter (paragraph contents
)
1245 "Interpret PARAGRAPH element as Org syntax.
1246 CONTENTS is the contents of the element."
1250 ;;;; Property Drawer
1252 (defun org-element-property-drawer-parser ()
1253 "Parse a property drawer.
1255 Return a list whose car is `property-drawer' and cdr is a plist
1256 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1257 `:contents-end', `:properties' and `:post-blank' keywords."
1259 (let ((case-fold-search t
)
1260 (begin (progn (end-of-line)
1261 (re-search-backward org-property-start-re
)
1262 (match-beginning 0)))
1263 (contents-begin (progn (forward-line) (point)))
1264 (hidden (org-truely-invisible-p))
1265 (properties (let (val)
1266 (while (not (looking-at "^[ \t]*:END:"))
1269 "[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):"))
1270 (push (cons (match-string 1)
1273 (match-end 0) (point-at-eol))))
1277 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t
)
1279 (pos-before-blank (progn (forward-line) (point)))
1280 (end (progn (org-skip-whitespace)
1281 (if (eobp) (point) (point-at-bol)))))
1286 :properties
,properties
1287 :post-blank
,(count-lines pos-before-blank end
))))))
1289 (defun org-element-property-drawer-interpreter (property-drawer contents
)
1290 "Interpret PROPERTY-DRAWER element as Org syntax.
1292 (let ((props (org-element-get-property :properties property-drawer
)))
1295 (mapconcat (lambda (p)
1296 (format org-property-format
(format ":%s:" (car p
)) (cdr p
)))
1297 (nreverse props
) "\n")
1303 (defun org-element-quote-section-parser ()
1304 "Parse a quote section.
1306 Return a list whose car is `quote-section' and cdr is a plist
1307 containing `:begin', `:end', `:value' and `:post-blank'
1310 Assume point is at beginning of the section."
1312 (let* ((begin (point))
1313 (end (progn (org-with-limited-levels (outline-next-heading))
1315 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1318 (value (buffer-substring-no-properties begin pos-before-blank
)))
1323 :post-blank
,(count-lines pos-before-blank end
))))))
1325 (defun org-element-quote-section-interpreter (quote-section contents
)
1326 "Interpret QUOTE-SECTION element as Org syntax.
1328 (org-element-get-property :value quote-section
))
1333 (defun org-element-src-block-parser ()
1336 Return a list whose car is `src-block' and cdr is a plist
1337 containing `:language', `:switches', `:parameters', `:begin',
1338 `:end', `:hiddenp', `:contents-begin', `:contents-end', `:value'
1339 and `:post-blank' keywords."
1342 (let* ((case-fold-search t
)
1343 ;; Get position at beginning of block.
1346 (concat "^[ \t]*#\\+begin_src"
1347 "\\(?: +\\(\\S-+\\)\\)?" ; language
1348 "\\(\\(?: +[-+][A-Za-z]\\)*\\)" ; switches
1349 "\\(.*\\)[ \t]*$") ; arguments
1351 ;; Get language as a string.
1352 (language (org-match-string-no-properties 1))
1354 (switches (org-match-string-no-properties 2))
1356 (parameters (org-trim (org-match-string-no-properties 3)))
1357 ;; Get affiliated keywords.
1358 (keywords (org-element-collect-affiliated-keywords))
1359 ;; Get beginning position.
1360 (begin (car keywords
))
1361 ;; Get position at end of block.
1362 (contents-end (progn (re-search-forward "^[ \t]*#\\+end_src" nil t
)
1366 (value (buffer-substring-no-properties
1367 (save-excursion (goto-char contents-begin
)
1370 (match-beginning 0)))
1371 ;; Get position after ending blank lines.
1372 (end (progn (org-skip-whitespace)
1373 (if (eobp) (point) (point-at-bol))))
1374 ;; Get visibility status.
1375 (hidden (progn (goto-char contents-begin
)
1377 (org-truely-invisible-p))))
1379 (:language
,language
1381 :parameters
,parameters
1386 :post-blank
,(count-lines contents-end end
)
1387 ,@(cadr keywords
))))))
1389 (defun org-element-src-block-interpreter (src-block contents
)
1390 "Interpret SRC-BLOCK element as Org syntax.
1392 (let ((lang (org-element-get-property :language src-block
))
1393 (switches (org-element-get-property :switches src-block
))
1394 (params (org-element-get-property :parameters src-block
))
1395 (value (let ((val (org-element-get-property :value src-block
)))
1397 (org-src-preserve-indentation val
)
1398 ((zerop org-edit-src-content-indentation
)
1399 (org-remove-indentation val
))
1401 (let ((ind (make-string
1402 org-edit-src-content-indentation
32)))
1403 (replace-regexp-in-string
1404 "\\(^\\)[ \t]*\\S-" ind
1405 (org-remove-indentation val
) nil nil
1)))))))
1406 (concat (format "#+begin_src%s\n"
1407 (concat (and lang
(concat " " lang
))
1408 (and switches
(concat " " switches
))
1409 (and params
(concat " " params
))))
1416 (defun org-element-table-parser ()
1417 "Parse a table at point.
1419 Return a list whose car is `table' and cdr is a plist containing
1420 `:begin', `:end', `:contents-begin', `:contents-end', `:tblfm',
1421 `:type', `:raw-table' and `:post-blank' keywords."
1423 (let* ((table-begin (goto-char (org-table-begin t
)))
1424 (type (if (org-at-table.el-p
) 'table.el
'org
))
1425 (keywords (org-element-collect-affiliated-keywords))
1426 (begin (car keywords
))
1427 (table-end (goto-char (marker-position (org-table-end t
))))
1428 (tblfm (when (looking-at "[ \t]*#\\+tblfm: +\\(.*\\)[ \t]*")
1429 (prog1 (org-match-string-no-properties 1)
1431 (pos-before-blank (point))
1432 (end (progn (org-skip-whitespace)
1433 (if (eobp) (point) (point-at-bol))))
1434 (raw-table (org-remove-indentation
1435 (buffer-substring-no-properties table-begin table-end
))))
1440 :raw-table
,raw-table
1442 :post-blank
,(count-lines pos-before-blank end
)
1443 ,@(cadr keywords
))))))
1445 (defun org-element-table-interpreter (table contents
)
1446 "Interpret TABLE element as Org syntax.
1448 (org-element-get-property :raw-table table
))
1453 (defun org-element-verse-block-parser ()
1454 "Parse a verse block.
1456 Return a list whose car is `verse-block' and cdr is a plist
1457 containing `:begin', `:end', `:hiddenp', `:raw-value', `:value'
1458 and `:post-blank' keywords.
1460 Assume point is at beginning or end of the block."
1462 (let* ((case-fold-search t
)
1466 (concat "^[ \t]*#\\+begin_verse") nil t
)
1467 (org-element-collect-affiliated-keywords)))
1468 (begin (car keywords
))
1469 (hidden (progn (forward-line) (org-truely-invisible-p)))
1470 (raw-val (buffer-substring-no-properties
1473 (re-search-forward (concat "^[ \t]*#\\+end_verse") nil t
)
1475 (pos-before-blank (progn (forward-line) (point)))
1476 (end (progn (org-skip-whitespace)
1477 (if (eobp) (point) (point-at-bol))))
1478 (value (org-element-parse-secondary-string
1479 (org-remove-indentation raw-val
)
1480 (cdr (assq 'verse-block org-element-string-restrictions
)))))
1487 :post-blank
,(count-lines pos-before-blank end
)
1488 ,@(cadr keywords
))))))
1490 (defun org-element-verse-block-interpreter (verse-block contents
)
1491 "Interpret VERSE-BLOCK element as Org syntax.
1493 (format "#+begin_verse\n%s#+end_verse"
1494 (org-remove-indentation
1495 (org-element-get-property :raw-value verse-block
))))
1501 ;; Unlike to elements, interstices can be found between objects.
1502 ;; That's why, along with the parser, successor functions are provided
1503 ;; for each object. Some objects share the same successor
1504 ;; (i.e. `emphasis' and `verbatim' objects).
1506 ;; A successor must accept a single argument bounding the search. It
1507 ;; will return either a cons cell whose car is the object's type, as
1508 ;; a symbol, and cdr the position of its next occurrence, or nil.
1510 ;; Successors follow the naming convention:
1511 ;; org-element-NAME-successor, where NAME is the name of the
1512 ;; successor, as defined in `org-element-all-successors'.
1514 ;; Some object types (i.e. `emphasis') are recursive. Restrictions on
1515 ;; object types they can contain will be specified in
1516 ;; `org-element-object-restrictions'.
1518 ;; Adding a new type of object is simple. Implement a successor,
1519 ;; a parser, and an interpreter for it, all following the naming
1520 ;; convention. Register successor in `org-element-all-successors',
1521 ;; maybe tweak restrictions about it, and that's it.
1525 (defun org-element-emphasis-parser ()
1526 "Parse text markup object at point.
1528 Return a list whose car is `emphasis' and cdr is a plist with
1529 `:marker', `:begin', `:end', `:contents-begin' and
1530 `:contents-end' and `:post-blank' keywords.
1532 Assume point is at the first emphasis marker."
1534 (unless (bolp) (backward-char 1))
1535 (looking-at org-emph-re
)
1536 (let ((begin (match-beginning 2))
1537 (marker (org-match-string-no-properties 3))
1538 (contents-begin (match-beginning 4))
1539 (contents-end (match-end 4))
1540 (post-blank (progn (goto-char (match-end 2))
1541 (skip-chars-forward " \t")))
1547 :contents-begin
,contents-begin
1548 :contents-end
,contents-end
1549 :post-blank
,post-blank
)))))
1551 (defun org-element-emphasis-interpreter (emphasis contents
)
1552 "Interpret EMPHASIS object as Org syntax.
1553 CONTENTS is the contents of the object."
1554 (let ((marker (org-element-get-property :marker emphasis
)))
1555 (concat marker contents marker
)))
1557 (defun org-element-text-markup-successor (limit)
1558 "Search for the next emphasis or verbatim object.
1560 LIMIT bounds the search.
1562 Return value is a cons cell whose car is `emphasis' or
1563 `verbatim' and cdr is beginning position."
1565 (unless (bolp) (backward-char))
1566 (when (re-search-forward org-emph-re limit t
)
1567 (cons (if (nth 4 (assoc (match-string 3) org-emphasis-alist
))
1570 (match-beginning 2)))))
1574 (defun org-element-entity-parser ()
1575 "Parse entity at point.
1577 Return a list whose car is `entity' and cdr a plist with
1578 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1579 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1582 Assume point is at the beginning of the entity."
1584 (looking-at "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1585 (let* ((value (org-entity-get (match-string 1)))
1586 (begin (match-beginning 0))
1587 (bracketsp (string= (match-string 2) "{}"))
1588 (post-blank (progn (goto-char (match-end 1))
1589 (when bracketsp
(forward-char 2))
1590 (skip-chars-forward " \t")))
1594 :latex
,(nth 1 value
)
1595 :latex-math-p
,(nth 2 value
)
1596 :html
,(nth 3 value
)
1597 :ascii
,(nth 4 value
)
1598 :latin1
,(nth 5 value
)
1599 :utf-8
,(nth 6 value
)
1602 :use-brackets-p
,bracketsp
1603 :post-blank
,post-blank
)))))
1605 (defun org-element-entity-interpreter (entity contents
)
1606 "Interpret ENTITY object as Org syntax.
1609 (org-element-get-property :name entity
)
1610 (when (org-element-get-property :use-brackets-p entity
) "{}")))
1612 (defun org-element-latex-or-entity-successor (limit)
1613 "Search for the next latex-fragment or entity object.
1615 LIMIT bounds the search.
1617 Return value is a cons cell whose car is `entity' or
1618 `latex-fragment' and cdr is beginning position."
1620 (let ((matchers (plist-get org-format-latex-options
:matchers
))
1621 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1623 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"))
1624 (when (re-search-forward
1625 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps
)))
1629 (goto-char (match-beginning 0))
1630 (if (looking-at entity-re
)
1631 ;; Determine if it's a real entity or a LaTeX command.
1632 (cons (if (org-entity-get (match-string 1)) 'entity
'latex-fragment
)
1633 (match-beginning 0))
1634 ;; No entity nor command: point is at a LaTeX fragment.
1635 ;; Determine its type to get the correct beginning position.
1636 (cons 'latex-fragment
1639 (when (looking-at (nth 1 (assoc e org-latex-regexps
)))
1642 (nth 2 (assoc e org-latex-regexps
))))))
1649 (defun org-element-export-snippet-parser ()
1650 "Parse export snippet at point.
1652 Return a list whose car is `export-snippet' and cdr a plist with
1653 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1656 Assume point is at the beginning of the snippet."
1658 (looking-at "@\\([-A-Za-z0-9]+\\){")
1659 (let* ((begin (point))
1660 (back-end (org-match-string-no-properties 1))
1661 (before-blank (progn (goto-char (scan-sexps (1- (match-end 0)) 1))))
1662 (value (buffer-substring-no-properties
1663 (match-end 0) (1- before-blank
)))
1664 (post-blank (skip-chars-forward " \t"))
1667 (:back-end
,back-end
1671 :post-blank
,post-blank
)))))
1673 (defun org-element-export-snippet-interpreter (export-snippet contents
)
1674 "Interpret EXPORT-SNIPPET object as Org syntax.
1677 (org-element-get-property :back-end export-snippet
)
1678 (org-element-get-property :value export-snippet
)))
1680 (defun org-element-export-snippet-successor (limit)
1681 "Search for the next export-snippet object.
1683 LIMIT bounds the search.
1685 Return value is a cons cell whose car is `export-snippet' cdr is
1686 its beginning position."
1689 (while (re-search-forward "@[-A-Za-z0-9]+{" limit t
)
1690 (when (let ((end (ignore-errors (scan-sexps (1- (point)) 1))))
1691 (and end
(eq (char-before end
) ?
})))
1692 (throw 'exit
(cons 'export-snippet
(match-beginning 0))))))))
1695 ;;;; Footnote Reference
1697 (defun org-element-footnote-reference-parser ()
1698 "Parse footnote reference at point.
1700 Return a list whose car is `footnote-reference' and cdr a plist
1701 with `:label', `:type', `:definition', `:begin', `:end' and
1702 `:post-blank' as keywords."
1704 (let* ((ref (org-footnote-at-reference-p))
1706 (raw-def (nth 3 ref
))
1707 (inline-def (and raw-def
1708 (org-element-parse-secondary-string raw-def nil
)))
1709 (type (if (nth 3 ref
) 'inline
'standard
))
1711 (post-blank (progn (goto-char (nth 2 ref
))
1712 (skip-chars-forward " \t")))
1714 `(footnote-reference
1717 :inline-definition
,inline-def
1720 :post-blank
,post-blank
1721 :raw-definition
,raw-def
)))))
1723 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
1724 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1726 (let ((label (or (org-element-get-property :label footnote-reference
)
1728 (def (let ((raw (org-element-get-property
1729 :raw-definition footnote-reference
)))
1730 (if raw
(concat ":" raw
) ""))))
1731 (format "[%s]" (concat label def
))))
1733 (defun org-element-footnote-reference-successor (limit)
1734 "Search for the next footnote-reference object.
1736 LIMIT bounds the search.
1738 Return value is a cons cell whose car is `footnote-reference' and
1739 cdr is beginning position."
1741 (when (setq fn-ref
(org-footnote-get-next-reference nil nil limit
))
1742 (cons 'footnote-reference
(nth 1 fn-ref
)))))
1745 ;;;; Inline Babel Call
1747 (defun org-element-inline-babel-call-parser ()
1748 "Parse inline babel call at point.
1750 Return a list whose car is `inline-babel-call' and cdr a plist with
1751 `:begin', `:end', `:info' and `:post-blank' as keywords.
1753 Assume point is at the beginning of the babel call."
1755 (unless (bolp) (backward-char))
1756 (looking-at org-babel-inline-lob-one-liner-regexp
)
1757 (let ((info (save-match-data (org-babel-lob-get-info)))
1758 (begin (match-end 1))
1759 (post-blank (progn (goto-char (match-end 0))
1760 (skip-chars-forward " \t")))
1766 :post-blank
,post-blank
)))))
1768 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
1769 "Interpret INLINE-BABEL-CALL object as Org syntax.
1771 (let* ((babel-info (org-element-get-property :info inline-babel-call
))
1772 (main-source (car babel-info
))
1773 (post-options (nth 1 babel-info
)))
1775 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
1776 ;; Remove redundant square brackets.
1778 (match-string 1 main-source
) nil nil main-source
)
1780 (and post-options
(format "[%s]" post-options
)))))
1782 (defun org-element-inline-babel-call-successor (limit)
1783 "Search for the next inline-babel-call object.
1785 LIMIT bounds the search.
1787 Return value is a cons cell whose car is `inline-babel-call' and
1788 cdr is beginning position."
1790 ;; Use a simplified version of
1791 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
1792 (when (re-search-forward
1793 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
1795 (cons 'inline-babel-call
(match-beginning 0)))))
1798 ;;;; Inline Src Block
1800 (defun org-element-inline-src-block-parser ()
1801 "Parse inline source block at point.
1803 Return a list whose car is `inline-src-block' and cdr a plist
1804 with `:begin', `:end', `:language', `:value', `:parameters' and
1805 `:post-blank' as keywords.
1807 Assume point is at the beginning of the inline src block."
1809 (unless (bolp) (backward-char))
1810 (looking-at org-babel-inline-src-block-regexp
)
1811 (let ((begin (match-beginning 1))
1812 (language (org-match-string-no-properties 2))
1813 (parameters (org-match-string-no-properties 4))
1814 (value (org-match-string-no-properties 5))
1815 (post-blank (progn (goto-char (match-end 0))
1816 (skip-chars-forward " \t")))
1819 (:language
,language
1821 :parameters
,parameters
1824 :post-blank
,post-blank
)))))
1826 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
1827 "Interpret INLINE-SRC-BLOCK object as Org syntax.
1829 (let ((language (org-element-get-property :language inline-src-block
))
1830 (arguments (org-element-get-property :parameters inline-src-block
))
1831 (body (org-element-get-property :value inline-src-block
)))
1832 (format "src_%s%s{%s}"
1834 (if arguments
(format "[%s]" arguments
) "")
1837 (defun org-element-inline-src-block-successor (limit)
1838 "Search for the next inline-babel-call element.
1840 LIMIT bounds the search.
1842 Return value is a cons cell whose car is `inline-babel-call' and
1843 cdr is beginning position."
1845 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
1846 (cons 'inline-src-block
(match-beginning 1)))))
1851 (defun org-element-latex-fragment-parser ()
1852 "Parse latex fragment at point.
1854 Return a list whose car is `latex-fragment' and cdr a plist with
1855 `:value', `:begin', `:end', and `:post-blank' as keywords.
1857 Assume point is at the beginning of the latex fragment."
1859 (let* ((begin (point))
1863 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
1864 (when (or (looking-at latex-regexp
)
1868 (looking-at latex-regexp
))))
1869 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
1870 (plist-get org-format-latex-options
:matchers
))
1871 ;; None found: it's a macro.
1872 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
1874 (value (match-string-no-properties substring-match
))
1875 (post-blank (progn (goto-char (match-end substring-match
))
1876 (skip-chars-forward " \t")))
1882 :post-blank
,post-blank
)))))
1884 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
1885 "Interpret LATEX-FRAGMENT object as Org syntax.
1887 (org-element-get-property :value latex-fragment
))
1891 (defun org-element-line-break-parser ()
1892 "Parse line break at point.
1894 Return a list whose car is `line-break', and cdr a plist with
1895 `:begin', `:end' and `:post-blank' keywords.
1897 Assume point is at the beginning of the line break."
1898 (let ((begin (point))
1899 (end (save-excursion (forward-line) (point))))
1900 `(line-break (:begin
,begin
:end
,end
:post-blank
0))))
1902 (defun org-element-line-break-interpreter (line-break contents
)
1903 "Interpret LINE-BREAK object as Org syntax.
1907 (defun org-element-line-break-successor (limit)
1908 "Search for the next line-break object.
1910 LIMIT bounds the search.
1912 Return value is a cons cell whose car is `line-break' and cdr is
1913 beginning position."
1915 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
1916 (goto-char (match-beginning 1)))))
1917 ;; A line break can only happen on a non-empty line.
1918 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
1919 (cons 'line-break beg
)))))
1924 (defun org-element-link-parser ()
1925 "Parse link at point.
1927 Return a list whose car is `link' and cdr a plist with `:type',
1928 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
1929 `:contents-end' and `:post-blank' as keywords.
1931 Assume point is at the beginning of the link."
1933 (let ((begin (point))
1934 end contents-begin contents-end link-end post-blank path type
1937 ;; Type 1: Text targeted from a radio target.
1938 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
1940 link-end
(match-end 0)
1941 path
(org-match-string-no-properties 0)))
1942 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
1943 ((looking-at org-bracket-link-regexp
)
1944 (setq contents-begin
(match-beginning 3)
1945 contents-end
(match-end 3)
1946 link-end
(match-end 0)
1947 ;; RAW-LINK is the original link.
1948 raw-link
(org-match-string-no-properties 1)
1949 link
(org-link-expand-abbrev
1950 (replace-regexp-in-string
1951 " *\n *" " " (org-link-unescape raw-link
) t t
)))
1952 ;; Determine TYPE of link and set PATH accordingly.
1955 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
1956 (setq type
"file" path link
))
1957 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
1958 ((string-match org-link-re-with-space3 link
)
1959 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
1960 ;; Ref type: PATH is the name of the target element.
1961 ((string-match "^ref:\\(.*\\)" link
)
1962 (setq type
"ref" path
(org-trim (match-string 1 link
))))
1963 ;; Id type: PATH is the id.
1964 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
1965 (setq type
"id" path
(match-string 1 link
)))
1966 ;; Code-ref type: PATH is the name of the reference.
1967 ((string-match "^(\\(.*\\))$" link
)
1968 (setq type
"coderef" path
(match-string 1 link
)))
1969 ;; Custom-id type: PATH is the name of the custom id.
1970 ((= (aref link
0) ?
#)
1971 (setq type
"custom-id" path
(substring link
1)))
1972 ;; Fuzzy type: Internal link either matches a target, an
1973 ;; headline name or nothing. PATH is the target or headline's
1975 (t (setq type
"fuzzy" path link
))))
1976 ;; Type 3: Plain link, i.e. http://orgmode.org
1977 ((looking-at org-plain-link-re
)
1978 (setq raw-link
(org-match-string-no-properties 0)
1979 type
(org-match-string-no-properties 1)
1980 path
(org-match-string-no-properties 2)
1981 link-end
(match-end 0)))
1982 ;; Type 4: Angular link, i.e. <http://orgmode.org>
1983 ((looking-at org-angle-link-re
)
1984 (setq raw-link
(buffer-substring-no-properties
1985 (match-beginning 1) (match-end 2))
1986 type
(org-match-string-no-properties 1)
1987 path
(org-match-string-no-properties 2)
1988 link-end
(match-end 0))))
1989 ;; In any case, deduce end point after trailing white space from
1990 ;; LINK-END variable.
1991 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
1996 :raw-link
,(or raw-link path
)
1999 :contents-begin
,contents-begin
2000 :contents-end
,contents-end
2001 :post-blank
,post-blank
)))))
2003 (defun org-element-link-interpreter (link contents
)
2004 "Interpret LINK object as Org syntax.
2005 CONTENTS is the contents of the object."
2006 (let ((type (org-element-get-property :type link
))
2007 (raw-link (org-element-get-property :raw-link link
)))
2009 ((string= type
"radio") raw-link
)
2010 (t (format "[[%s]%s]"
2012 (if (string= contents
"") "" (format "[%s]" contents
)))))))
2014 (defun org-element-link-successor (limit)
2015 "Search for the next link object.
2017 LIMIT bounds the search.
2019 Return value is a cons cell whose car is `link' and cdr is
2020 beginning position."
2023 (if org-target-link-regexp
2024 (concat org-any-link-re
"\\|" org-target-link-regexp
)
2026 (when (re-search-forward link-regexp limit t
)
2027 (cons 'link
(match-beginning 0))))))
2032 (defun org-element-macro-parser ()
2033 "Parse macro at point.
2035 Return a list whose car is `macro' and cdr a plist with `:key',
2036 `:args', `:begin', `:end', `:value' and `:post-blank' as
2039 Assume point is at the macro."
2041 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2042 (let ((begin (point))
2043 (key (downcase (org-match-string-no-properties 1)))
2044 (value (org-match-string-no-properties 0))
2045 (post-blank (progn (goto-char (match-end 0))
2046 (skip-chars-forward " \t")))
2048 (args (let ((args (org-match-string-no-properties 3)) args2
)
2050 (setq args
(org-split-string args
","))
2052 (while (string-match "\\\\\\'" (car args
))
2053 ;; Repair bad splits.
2054 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2057 (push (pop args
) args2
))
2058 (mapcar 'org-trim
(nreverse args2
))))))
2065 :post-blank
,post-blank
)))))
2067 (defun org-element-macro-interpreter (macro contents
)
2068 "Interpret MACRO object as Org syntax.
2070 (org-element-get-property :value macro
))
2072 (defun org-element-macro-successor (limit)
2073 "Search for the next macro object.
2075 LIMIT bounds the search.
2077 Return value is cons cell whose car is `macro' and cdr is
2078 beginning position."
2080 (when (re-search-forward
2081 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2083 (cons 'macro
(match-beginning 0)))))
2088 (defun org-element-radio-target-parser ()
2089 "Parse radio target at point.
2091 Return a list whose car is `radio-target' and cdr a plist with
2092 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2093 and `:post-blank' as keywords.
2095 Assume point is at the radio target."
2097 (looking-at org-radio-target-regexp
)
2098 (let ((begin (point))
2099 (contents-begin (match-beginning 1))
2100 (contents-end (match-end 1))
2101 (raw-value (org-match-string-no-properties 1))
2102 (post-blank (progn (goto-char (match-end 0))
2103 (skip-chars-forward " \t")))
2108 :contents-begin
,contents-begin
2109 :contents-end
,contents-end
2110 :raw-value
,raw-value
2111 :post-blank
,post-blank
)))))
2113 (defun org-element-radio-target-interpreter (target contents
)
2114 "Interpret TARGET object as Org syntax.
2115 CONTENTS is the contents of the object."
2116 (concat "<<<" contents
">>>"))
2118 (defun org-element-radio-target-successor (limit)
2119 "Search for the next radio-target object.
2121 LIMIT bounds the search.
2123 Return value is a cons cell whose car is `radio-target' and cdr
2124 is beginning position."
2126 (when (re-search-forward org-radio-target-regexp limit t
)
2127 (cons 'radio-target
(match-beginning 0)))))
2130 ;;;; Statistics Cookie
2132 (defun org-element-statistics-cookie-parser ()
2133 "Parse statistics cookie at point.
2135 Return a list whose car is `statistics-cookie', and cdr a plist
2136 with `:begin', `:end', `:value' and `:post-blank' keywords.
2138 Assume point is at the beginning of the statistics-cookie."
2140 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2141 (let* ((begin (point))
2142 (value (buffer-substring-no-properties
2143 (match-beginning 0) (match-end 0)))
2144 (post-blank (progn (goto-char (match-end 0))
2145 (skip-chars-forward " \t")))
2151 :post-blank
,post-blank
)))))
2153 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
2154 "Interpret STATISTICS-COOKIE object as Org syntax.
2156 (org-element-get-property :value statistics-cookie
))
2158 (defun org-element-statistics-cookie-successor (limit)
2159 "Search for the next statistics cookie object.
2161 LIMIT bounds the search.
2163 Return value is a cons cell whose car is `statistics-cookie' and
2164 cdr is beginning position."
2166 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
2167 (cons 'statistics-cookie
(match-beginning 0)))))
2172 (defun org-element-subscript-parser ()
2173 "Parse subscript at point.
2175 Return a list whose car is `subscript' and cdr a plist with
2176 `:begin', `:end', `:contents-begin', `:contents-end',
2177 `:use-brackets-p' and `:post-blank' as keywords.
2179 Assume point is at the underscore."
2181 (unless (bolp) (backward-char))
2182 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2184 (not (looking-at org-match-substring-regexp
))))
2185 (begin (match-beginning 2))
2186 (contents-begin (or (match-beginning 5)
2187 (match-beginning 3)))
2188 (contents-end (or (match-end 5) (match-end 3)))
2189 (post-blank (progn (goto-char (match-end 0))
2190 (skip-chars-forward " \t")))
2195 :use-brackets-p
,bracketsp
2196 :contents-begin
,contents-begin
2197 :contents-end
,contents-end
2198 :post-blank
,post-blank
)))))
2200 (defun org-element-subscript-interpreter (subscript contents
)
2201 "Interpret SUBSCRIPT object as Org syntax.
2202 CONTENTS is the contents of the object."
2204 (if (org-element-get-property :use-brackets-p subscript
) "_{%s}" "_%s")
2207 (defun org-element-sub/superscript-successor
(limit)
2208 "Search for the next sub/superscript object.
2210 LIMIT bounds the search.
2212 Return value is a cons cell whose car is either `subscript' or
2213 `superscript' and cdr is beginning position."
2215 (when (re-search-forward org-match-substring-regexp limit t
)
2216 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
2217 (match-beginning 2)))))
2222 (defun org-element-superscript-parser ()
2223 "Parse superscript at point.
2225 Return a list whose car is `superscript' and cdr a plist with
2226 `:begin', `:end', `:contents-begin', `:contents-end',
2227 `:use-brackets-p' and `:post-blank' as keywords.
2229 Assume point is at the caret."
2231 (unless (bolp) (backward-char))
2232 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2234 (not (looking-at org-match-substring-regexp
))))
2235 (begin (match-beginning 2))
2236 (contents-begin (or (match-beginning 5)
2237 (match-beginning 3)))
2238 (contents-end (or (match-end 5) (match-end 3)))
2239 (post-blank (progn (goto-char (match-end 0))
2240 (skip-chars-forward " \t")))
2245 :use-brackets-p
,bracketsp
2246 :contents-begin
,contents-begin
2247 :contents-end
,contents-end
2248 :post-blank
,post-blank
)))))
2250 (defun org-element-superscript-interpreter (superscript contents
)
2251 "Interpret SUPERSCRIPT object as Org syntax.
2252 CONTENTS is the contents of the object."
2254 (if (org-element-get-property :use-brackets-p superscript
) "^{%s}" "^%s")
2260 (defun org-element-target-parser ()
2261 "Parse target at point.
2263 Return a list whose car is `target' and cdr a plist with
2264 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2265 and `:post-blank' as keywords.
2267 Assume point is at the target."
2269 (looking-at org-target-regexp
)
2270 (let ((begin (point))
2271 (contents-begin (match-beginning 1))
2272 (contents-end (match-end 1))
2273 (raw-value (org-match-string-no-properties 1))
2274 (post-blank (progn (goto-char (match-end 0))
2275 (skip-chars-forward " \t")))
2280 :contents-begin
,contents-begin
2281 :contents-end
,contents-end
2282 :raw-value
,raw-value
2283 :post-blank
,post-blank
)))))
2285 (defun org-element-target-interpreter (target contents
)
2286 "Interpret TARGET object as Org syntax.
2287 CONTENTS is the contents of target."
2290 (defun org-element-target-successor (limit)
2291 "Search for the next target object.
2293 LIMIT bounds the search.
2295 Return value is a cons cell whose car is `target' and cdr is
2296 beginning position."
2298 (when (re-search-forward org-target-regexp limit t
)
2299 (cons 'target
(match-beginning 0)))))
2304 (defun org-element-time-stamp-parser ()
2305 "Parse time stamp at point.
2307 Return a list whose car is `time-stamp', and cdr a plist with
2308 `:appt-type', `:type', `:begin', `:end', `:value' and
2309 `:post-blank' keywords.
2311 Assume point is at the beginning of the time-stamp."
2313 (let* ((appt-type (cond
2314 ((looking-at (concat org-deadline-string
" +"))
2315 (goto-char (match-end 0))
2317 ((looking-at (concat org-scheduled-string
" +"))
2318 (goto-char (match-end 0))
2320 ((looking-at (concat org-closed-string
" +"))
2321 (goto-char (match-end 0))
2323 (begin (and appt-type
(match-beginning 0)))
2325 ((looking-at org-tsr-regexp
)
2326 (if (match-string 2) 'active-range
'active
))
2327 ((looking-at org-tsr-regexp-both
)
2328 (if (match-string 2) 'inactive-range
'inactive
))
2329 ((looking-at (concat
2330 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2332 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2334 (begin (or begin
(match-beginning 0)))
2335 (value (buffer-substring-no-properties
2336 (match-beginning 0) (match-end 0)))
2337 (post-blank (progn (goto-char (match-end 0))
2338 (skip-chars-forward " \t")))
2341 (:appt-type
,appt-type
2346 :post-blank
,post-blank
)))))
2348 (defun org-element-time-stamp-interpreter (time-stamp contents
)
2349 "Interpret TIME-STAMP object as Org syntax.
2352 (case (org-element-get-property :appt-type time-stamp
)
2353 (closed (concat org-closed-string
" "))
2354 (deadline (concat org-deadline-string
" "))
2355 (scheduled (concat org-scheduled-string
" ")))
2356 (org-element-get-property :value time-stamp
)))
2358 (defun org-element-time-stamp-successor (limit)
2359 "Search for the next time-stamp object.
2361 LIMIT bounds the search.
2363 Return value is a cons cell whose car is `time-stamp' and cdr is
2364 beginning position."
2366 (when (re-search-forward
2367 (concat "\\(?:" org-scheduled-string
" +\\|"
2368 org-deadline-string
" +\\|" org-closed-string
" +\\)?"
2371 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2373 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2375 (cons 'time-stamp
(match-beginning 0)))))
2380 (defun org-element-verbatim-parser ()
2381 "Parse verbatim object at point.
2383 Return a list whose car is `verbatim' and cdr is a plist with
2384 `:marker', `:begin', `:end' and `:post-blank' keywords.
2386 Assume point is at the first verbatim marker."
2388 (unless (bolp) (backward-char 1))
2389 (looking-at org-emph-re
)
2390 (let ((begin (match-beginning 2))
2391 (marker (org-match-string-no-properties 3))
2392 (value (org-match-string-no-properties 4))
2393 (post-blank (progn (goto-char (match-end 2))
2394 (skip-chars-forward " \t")))
2401 :post-blank
,post-blank
)))))
2403 (defun org-element-verbatim-interpreter (verbatim contents
)
2404 "Interpret VERBATIM object as Org syntax.
2406 (let ((marker (org-element-get-property :marker verbatim
))
2407 (value (org-element-get-property :value verbatim
)))
2408 (concat marker value marker
)))
2412 ;;; Definitions And Rules
2414 ;; Define elements, greater elements and specify recursive objects,
2415 ;; along with the affiliated keywords recognized. Also set up
2416 ;; restrictions on recursive objects combinations.
2418 ;; These variables really act as a control center for the parsing
2420 (defconst org-element-paragraph-separate
2421 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2422 ;; Headlines and inlinetasks.
2423 org-outline-regexp-bol
"\\|"
2424 ;; Comments, blocks (any type), keywords and babel calls.
2425 "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
2427 (org-item-beginning-re) "\\|"
2428 ;; Fixed-width, drawers (any type) and tables.
2430 ;; Footnote definitions.
2431 org-footnote-definition-re
"\\|"
2432 ;; Horizontal rules.
2433 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2434 ;; LaTeX environments.
2435 "^[ \t]*\\\\\\(begin\\|end\\)")
2436 "Regexp to separate paragraphs in an Org buffer.")
2438 (defconst org-element-all-elements
2439 '(center-block comment comment-block drawer dynamic-block example-block
2440 export-block fixed-width footnote-definition headline
2441 horizontal-rule inlinetask item keyword latex-environment
2442 babel-call paragraph plain-list property-drawer quote-block
2443 quote-section section special-block src-block table
2445 "Complete list of elements.")
2447 (defconst org-element-greater-elements
2448 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2449 item plain-list quote-block section special-block
)
2450 "List of recursive element types aka Greater Elements.")
2452 (defconst org-element-all-successors
2453 '(export-snippet footnote-reference inline-babel-call inline-src-block
2454 latex-or-entity line-break link macro radio-target
2455 statistics-cookie sub
/superscript target text-markup
2457 "Complete list of successors.")
2459 (defconst org-element-object-successor-alist
2460 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
2461 (emphasis . text-markup
) (verbatim . text-markup
)
2462 (entity . latex-or-entity
) (latex-fragment . latex-or-entity
))
2463 "Alist of translations between object type and successor name.
2465 Sharing the same successor comes handy when, for example, the
2466 regexp matching one object can also match the other object.")
2468 (defconst org-element-recursive-objects
2469 '(emphasis link macro subscript superscript target radio-target
)
2470 "List of recursive object types.")
2472 (defconst org-element-non-recursive-block-alist
2473 '(("ascii" . export-block
)
2474 ("comment" . comment-block
)
2475 ("docbook" . export-block
)
2476 ("example" . example-block
)
2477 ("html" . export-block
)
2478 ("latex" . export-block
)
2479 ("odt" . export-block
)
2481 ("verse" . verse-block
))
2482 "Alist between non-recursive block name and their element type.")
2484 (defconst org-element-affiliated-keywords
2485 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "caption"
2486 "data" "header" "headers" "label" "name" "plot" "resname" "result" "results"
2487 "source" "srcname" "tblname")
2488 "List of affiliated keywords as strings.")
2490 (defconst org-element-keyword-translation-alist
2491 '(("data" .
"name") ("label" .
"name") ("resname" .
"name")
2492 ("source" .
"name") ("srcname" .
"name") ("tblname" .
"name")
2493 ("result" .
"results") ("headers" .
"header"))
2494 "Alist of usual translations for keywords.
2495 The key is the old name and the value the new one. The property
2496 holding their value will be named after the translated name.")
2498 (defconst org-element-multiple-keywords
2499 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "header")
2500 "List of affiliated keywords that can occur more that once in an element.
2502 Their value will be consed into a list of strings, which will be
2503 returned as the value of the property.
2505 This list is checked after translations have been applied. See
2506 `org-element-keyword-translation-alist'.")
2508 (defconst org-element-parsed-keywords
'("author" "caption" "title")
2509 "List of keywords whose value can be parsed.
2511 Their value will be stored as a secondary string: a list of
2512 strings and objects.
2514 This list is checked after translations have been applied. See
2515 `org-element-keyword-translation-alist'.")
2517 (defconst org-element-dual-keywords
'("caption" "results")
2518 "List of keywords which can have a secondary value.
2520 In Org syntax, they can be written with optional square brackets
2521 before the colons. For example, results keyword can be
2522 associated to a hash value with the following:
2524 #+results[hash-string]: some-source
2526 This list is checked after translations have been applied. See
2527 `org-element-keyword-translation-alist'.")
2529 (defconst org-element-object-restrictions
2530 '((emphasis entity export-snippet inline-babel-call inline-src-block
2531 radio-target sub
/superscript target text-markup time-stamp
)
2532 (link entity export-snippet inline-babel-call inline-src-block
2533 latex-fragment link sub
/superscript text-markup
)
2535 (radio-target entity export-snippet latex-fragment sub
/superscript
)
2536 (subscript entity export-snippet inline-babel-call inline-src-block
2537 latex-fragment sub
/superscript text-markup
)
2538 (superscript entity export-snippet inline-babel-call inline-src-block
2539 latex-fragment sub
/superscript text-markup
)
2540 (target entity export-snippet latex-fragment sub
/superscript text-markup
))
2541 "Alist of recursive objects restrictions.
2543 Car is a recursive object type and cdr is a list of successors
2544 that will be called within an object of such type.
2546 For example, in a `radio-target' object, one can only find
2547 entities, export snippets, latex-fragments, subscript and
2550 (defconst org-element-string-restrictions
2551 '((headline entity inline-babel-call latex-fragment link macro radio-target
2552 statistics-cookie sub
/superscript text-markup time-stamp
)
2553 (inlinetask entity inline-babel-call latex-fragment link macro radio-target
2554 sub
/superscript text-markup time-stamp
)
2555 (item entity inline-babel-call latex-fragment macro radio-target
2556 sub
/superscript target text-markup
)
2557 (keyword entity latex-fragment macro sub
/superscript text-markup
)
2558 (table entity latex-fragment macro target text-markup
)
2559 (verse-block entity footnote-reference inline-babel-call inline-src-block
2560 latex-fragment line-break link macro radio-target
2561 sub
/superscript target text-markup time-stamp
))
2562 "Alist of secondary strings restrictions.
2564 When parsed, some elements have a secondary string which could
2565 contain various objects (i.e. headline's name, or table's cells).
2566 For association, the car is the element type, and the cdr a list
2567 of successors that will be called in that secondary string.
2569 Note: `keyword' secondary string type only applies to keywords
2570 matching `org-element-parsed-keywords'.")
2576 ;; Provide two accessors: `org-element-get-property' and
2577 ;; `org-element-get-contents'.
2579 (defun org-element-get-property (property element
)
2580 "Extract the value from the PROPERTY of an ELEMENT."
2581 (plist-get (nth 1 element
) property
))
2583 (defun org-element-get-contents (element)
2584 "Extract contents from an ELEMENT."
2589 ;; Obtaining The Smallest Element Containing Point
2591 ;; `org-element-at-point' is the core function of this section. It
2592 ;; returns the Lisp representation of the element at point. It uses
2593 ;; `org-element-guess-type' and `org-element-skip-keywords' as helper
2596 ;; When point is at an item, there is no automatic way to determine if
2597 ;; the function should return the `plain-list' element, or the
2598 ;; corresponding `item' element. By default, `org-element-at-point'
2599 ;; works at the `plain-list' level. But, by providing an optional
2600 ;; argument, one can make it switch to the `item' level.
2602 (defconst org-element--affiliated-re
2603 (format "[ \t]*#\\+\\(%s\\):"
2606 (if (member keyword org-element-dual-keywords
)
2607 (format "\\(%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
2608 (regexp-quote keyword
))
2609 (regexp-quote keyword
)))
2610 org-element-affiliated-keywords
"\\|"))
2611 "Regexp matching any affiliated keyword.
2613 Keyword name is put in match group 1. Moreover, if keyword
2614 belongs to `org-element-dual-keywords', put the dual value in
2617 Don't modify it, set `org-element--affiliated-keywords' instead.")
2619 (defun org-element-at-point (&optional special structure
)
2620 "Determine closest element around point.
2622 Return value is a list \(TYPE PROPS\) where TYPE is the type of
2623 the element and PROPS a plist of properties associated to the
2626 Possible types are defined in `org-element-all-elements'.
2628 Optional argument SPECIAL, when non-nil, can be either `item' or
2629 `section'. The former allows to parse item wise instead of
2630 plain-list wise, using STRUCTURE as the current list structure.
2631 The latter will try to parse a section before anything else.
2633 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
2637 ;; Move before any blank line.
2638 (when (looking-at "[ \t]*$")
2639 (skip-chars-backward " \r\t\n")
2640 (beginning-of-line))
2641 (let ((case-fold-search t
))
2642 ;; Check if point is at an affiliated keyword. In that case,
2643 ;; try moving to the beginning of the associated element. If
2644 ;; the keyword is orphaned, treat it as plain text.
2645 (when (looking-at org-element--affiliated-re
)
2646 (let ((opoint (point)))
2647 (while (looking-at org-element--affiliated-re
) (forward-line))
2648 (when (looking-at "[ \t]*$") (goto-char opoint
))))
2649 (let ((type (org-element-guess-type (eq special
'section
))))
2651 ;; Guessing element type on the current line is impossible:
2652 ;; try to find the beginning of the current element to get
2653 ;; more information.
2655 (let ((search-origin (point))
2656 (opoint-in-item-p (org-in-item-p))
2660 (re-search-backward org-element-paragraph-separate nil
'm
))))
2662 ;; Unable to find a paragraph delimiter above: we're at
2663 ;; bob and looking at a paragraph.
2664 ((not par-found-p
) (org-element-paragraph-parser))
2665 ;; Trying to find element's beginning set point back to
2666 ;; its original position. There's something peculiar on
2667 ;; this line that prevents parsing, probably an
2668 ;; ill-formed keyword or an undefined drawer name. Parse
2669 ;; it as plain text anyway.
2670 ((< search-origin
(point-at-eol)) (org-element-paragraph-parser))
2671 ;; Original point wasn't in a list but previous paragraph
2672 ;; is. It means that either point was inside some block,
2673 ;; or current list was ended without using a blank line.
2674 ;; In the last case, paragraph really starts at list end.
2676 (and (not opoint-in-item-p
)
2677 (not (looking-at "[ \t]*#\\+begin"))
2678 (setq item
(org-in-item-p))
2679 (let ((struct (save-excursion (goto-char item
)
2680 (org-list-struct))))
2681 (goto-char (org-list-get-bottom-point struct
))
2682 (org-skip-whitespace)
2684 (org-element-paragraph-parser)))))
2685 ((org-footnote-at-definition-p)
2686 (org-element-footnote-definition-parser))
2687 ((and opoint-in-item-p
(org-at-item-p) (= opoint-in-item-p
(point)))
2688 (if (eq special
'item
)
2689 (org-element-item-parser (or structure
(org-list-struct)))
2690 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2691 ;; In any other case, the paragraph started the line
2693 (t (forward-line) (org-element-paragraph-parser)))))
2694 ((eq type
'plain-list
)
2695 (if (eq special
'item
)
2696 (org-element-item-parser (or structure
(org-list-struct)))
2697 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2698 ;; Straightforward case: call the appropriate parser.
2699 (t (funcall (intern (format "org-element-%s-parser" type
)))))))))
2702 ;; It is obvious to tell if point is in most elements, either by
2703 ;; looking for a specific regexp in the current line, or by using
2704 ;; already implemented functions. This is the goal of
2705 ;; `org-element-guess-type'.
2707 (defconst org-element--element-block-types
2708 (mapcar 'car org-element-non-recursive-block-alist
)
2709 "List of non-recursive block types, as strings.
2710 Used internally by `org-element-guess-type'. Do not modify it
2711 directly, set `org-element-non-recursive-block-alist' instead.")
2713 (defun org-element-guess-type (&optional section-mode
)
2714 "Return the type of element at point, or nil if undetermined.
2716 This function may move point to an appropriate position for
2717 parsing. Used internally by `org-element-at-point'.
2719 When optional argument SECTION-MODE is non-nil, try to find if
2720 point is in a section in priority."
2721 ;; Beware: Order matters for some cases in that function.
2723 (let ((case-fold-search t
))
2725 ((org-with-limited-levels (org-at-heading-p)) 'headline
)
2726 ((let ((headline (ignore-errors (nth 4 (org-heading-components)))))
2728 (let (case-fold-search)
2729 (string-match (format "^%s\\(?: \\|$\\)" org-quote-string
)
2731 ;; Move to section beginning.
2732 (org-back-to-heading t
)
2734 (org-skip-whitespace)
2737 ;; Any buffer position not at an headline or in a quote section
2738 ;; is inside a section, provided function is actively looking for
2740 (section-mode 'section
)
2741 ;; Non-recursive block.
2742 ((let ((type (org-in-block-p org-element--element-block-types
)))
2743 (and type
(cdr (assoc type org-element-non-recursive-block-alist
)))))
2744 ((org-at-heading-p) 'inlinetask
)
2745 ((org-between-regexps-p
2746 "^[ \t]*\\\\begin{" "^[ \t]*\\\\end{[^}]*}[ \t]*") 'latex-environment
)
2747 ;; Property drawer. Almost `org-at-property-p', but allow drawer
2749 ((org-with-wide-buffer
2750 (and (not (org-before-first-heading-p))
2751 (let ((pblock (org-get-property-block)))
2753 (<= (point) (cdr pblock
))
2754 (>= (point-at-eol) (1- (car pblock
)))))))
2756 ;; Recursive block. If the block isn't complete, parse the
2757 ;; current part as a paragraph.
2758 ((looking-at "[ \t]*#\\+\\(begin\\|end\\)_\\([-A-Za-z0-9]+\\)\\(?:$\\|\\s-\\)")
2759 (let ((type (downcase (match-string 2))))
2761 ((not (org-in-block-p (list type
))) 'paragraph
)
2762 ((string= type
"center") 'center-block
)
2763 ((string= type
"quote") 'quote-block
)
2764 (t 'special-block
))))
2765 ;; Regular drawers must be tested after property drawer as both
2766 ;; elements share the same ending regexp.
2767 ((or (looking-at org-drawer-regexp
) (looking-at "[ \t]*:END:[ \t]*$"))
2768 (let ((completep (org-between-regexps-p
2769 org-drawer-regexp
"^[ \t]*:END:[ \t]*$")))
2770 (if (not completep
) 'paragraph
2771 (goto-char (car completep
)) 'drawer
)))
2772 ((looking-at "[ \t]*:\\( \\|$\\)") 'fixed-width
)
2773 ;; Babel calls must be tested before general keywords as they are
2774 ;; a subset of them.
2775 ((looking-at org-babel-block-lob-one-liner-regexp
) 'babel-call
)
2776 ((looking-at org-footnote-definition-re
) 'footnote-definition
)
2777 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
2778 (if (member (downcase (match-string 1)) org-element-affiliated-keywords
)
2781 ;; Dynamic block: simplify regexp used for match. If it isn't
2782 ;; complete, parse the current part as a paragraph.
2783 ((looking-at "[ \t]*#\\+\\(begin\\end\\):\\(?:\\s-\\|$\\)")
2784 (let ((completep (org-between-regexps-p
2785 "^[ \t]*#\\+begin:\\(?:\\s-\\|$\\)"
2786 "^[ \t]*#\\+end:\\(?:\\s-\\|$\\)")))
2787 (if (not completep
) 'paragraph
2788 (goto-char (car completep
)) 'dynamic-block
)))
2789 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)") 'comment
)
2790 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$") 'horizontal-rule
)
2791 ((org-at-table-p t
) 'table
)
2792 ((looking-at "[ \t]*#\\+tblfm:")
2794 ;; A TBLFM line separated from any table is just plain text.
2795 (if (org-at-table-p) 'table
2796 (forward-line) 'paragraph
))
2797 ((looking-at (org-item-re)) 'plain-list
))))
2799 ;; Most elements can have affiliated keywords. When looking for an
2800 ;; element beginning, we want to move before them, as they belong to
2801 ;; that element, and, in the meantime, collect information they give
2802 ;; into appropriate properties. Hence the following function.
2804 ;; Usage of optional arguments may not be obvious at first glance:
2806 ;; - TRANS-LIST is used to polish keywords names that have evolved
2807 ;; during Org history. In example, even though =result= and
2808 ;; =results= coexist, we want to have them under the same =result=
2809 ;; property. It's also true for "srcname" and "name", where the
2810 ;; latter seems to be preferred nowadays (thus the "name" property).
2812 ;; - CONSED allows to regroup multi-lines keywords under the same
2813 ;; property, while preserving their own identity. This is mostly
2814 ;; used for "attr_latex" and al.
2816 ;; - PARSED prepares a keyword value for export. This is useful for
2817 ;; "caption". Objects restrictions for such keywords are defined in
2818 ;; `org-element-string-restrictions'.
2820 ;; - DUALS is used to take care of keywords accepting a main and an
2821 ;; optional secondary values. For example "results" has its
2822 ;; source's name as the main value, and may have an hash string in
2823 ;; optional square brackets as the secondary one.
2825 ;; A keyword may belong to more than one category.
2827 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
2828 consed parsed duals
)
2829 "Collect affiliated keywords before point.
2831 Optional argument KEY-RE is a regexp matching keywords, which
2832 puts matched keyword in group 1. It defaults to
2833 `org-element--affiliated-re'.
2835 TRANS-LIST is an alist where key is the keyword and value the
2836 property name it should be translated to, without the colons. It
2837 defaults to `org-element-keyword-translation-alist'.
2839 CONSED is a list of strings. Any keyword belonging to that list
2840 will have its value consed. The check is done after keyword
2841 translation. It defaults to `org-element-multiple-keywords'.
2843 PARSED is a list of strings. Any keyword member of this list
2844 will have its value parsed. The check is done after keyword
2845 translation. If a keyword is a member of both CONSED and PARSED,
2846 it's value will be a list of parsed strings. It defaults to
2847 `org-element-parsed-keywords'.
2849 DUALS is a list of strings. Any keyword member of this list can
2850 have two parts: one mandatory and one optional. Its value is
2851 a cons cell whose car is the former, and the cdr the latter. If
2852 a keyword is a member of both PARSED and DUALS, both values will
2853 be parsed. It defaults to `org-element-dual-keywords'.
2855 Return a list whose car is the position at the first of them and
2856 cdr a plist of keywords and values."
2858 (let ((case-fold-search t
)
2859 (key-re (or key-re org-element--affiliated-re
))
2860 (trans-list (or trans-list org-element-keyword-translation-alist
))
2861 (consed (or consed org-element-multiple-keywords
))
2862 (parsed (or parsed org-element-parsed-keywords
))
2863 (duals (or duals org-element-dual-keywords
))
2864 ;; RESTRICT is the list of objects allowed in parsed
2866 (restrict (cdr (assq 'keyword org-element-string-restrictions
)))
2869 (while (and (not (bobp))
2870 (progn (forward-line -
1) (looking-at key-re
)))
2871 (let* ((raw-kwd (downcase (or (match-string 2) (match-string 1))))
2872 ;; Apply translation to RAW-KWD. From there, KWD is
2873 ;; the official keyword.
2874 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
2875 ;; Find main value for any keyword.
2879 (buffer-substring-no-properties
2880 (match-end 0) (point-at-eol)))))
2881 ;; If KWD is a dual keyword, find its secondary
2882 ;; value. Maybe parse it.
2884 (and (member kwd duals
)
2885 (let ((sec (org-match-string-no-properties 3)))
2886 (if (or (not sec
) (not (member kwd parsed
))) sec
2887 (org-element-parse-secondary-string sec restrict
)))))
2888 ;; Attribute a property name to KWD.
2889 (kwd-sym (and kwd
(intern (concat ":" kwd
)))))
2890 ;; Now set final shape for VALUE.
2891 (when (member kwd parsed
)
2892 (setq value
(org-element-parse-secondary-string value restrict
)))
2893 (when (member kwd duals
)
2894 ;; VALUE is mandatory. Set it to nil if there is none.
2895 (setq value
(and value
(cons value dual-value
))))
2896 (when (member kwd consed
)
2897 (setq value
(cons value
(plist-get output kwd-sym
))))
2898 ;; Eventually store the new value in OUTPUT.
2899 (setq output
(plist-put output kwd-sym value
))))
2900 (unless (looking-at key-re
) (forward-line 1)))
2901 (list (point) output
))))
2907 ;; The two major functions here are `org-element-parse-buffer', which
2908 ;; parses Org syntax inside the current buffer, taking into account
2909 ;; region, narrowing, or even visibility if specified, and
2910 ;; `org-element-parse-secondary-string', which parses objects within
2913 ;; The (almost) almighty `org-element-map' allows to apply a function
2914 ;; on elements or objects matching some type, and accumulate the
2915 ;; resulting values. In an export situation, it also skips unneeded
2916 ;; parts of the parse tree, transparently walks into included files,
2917 ;; and maintain a list of local properties (i.e. those inherited from
2918 ;; parent headlines) for function's consumption.
2920 (defun org-element-parse-buffer (&optional granularity visible-only
)
2921 "Recursively parse the buffer and return structure.
2922 If narrowing is in effect, only parse the visible part of the
2925 Optional argument GRANULARITY determines the depth of the
2926 recursion. It can be set to the following symbols:
2928 `headline' Only parse headlines.
2929 `greater-element' Don't recurse into greater elements. Thus,
2930 elements parsed are the top-level ones.
2931 `element' Parse everything but objects and plain text.
2932 `object' Parse the complete buffer (default).
2934 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
2937 Assume buffer is in Org mode."
2939 (goto-char (point-min))
2940 (org-skip-whitespace)
2941 (nconc (list 'org-data nil
)
2942 (org-element-parse-elements
2943 (point-at-bol) (point-max)
2944 ;; Start is section mode so text before the first headline
2945 ;; belongs to a section.
2946 'section nil granularity visible-only nil
))))
2948 (defun org-element-parse-secondary-string (string restriction
&optional buffer
)
2949 "Recursively parse objects in STRING and return structure.
2951 RESTRICTION, when non-nil, is a symbol limiting the object types
2952 that will be looked after.
2954 Optional argument BUFFER indicates the buffer from where the
2955 secondary string was extracted. It is used to determine where to
2956 get extraneous information for an object \(i.e. when resolving
2957 a link or looking for a footnote definition\). It defaults to
2958 the current buffer."
2961 (org-element-parse-objects (point-min) (point-max) nil restriction
)))
2963 (defun org-element-map (data types fun
&optional info first-match
)
2964 "Map a function on selected elements or objects.
2966 DATA is the parsed tree, as returned by, i.e,
2967 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
2968 of elements or objects types. FUN is the function called on the
2969 matching element or object. It must accept two arguments: the
2970 element or object itself and a plist holding contextual
2973 When optional argument INFO is non-nil, it should be a plist
2974 holding export options. In that case, parts of the parse tree
2975 not exportable according to that property list will be skipped
2976 and files included through a keyword will be visited.
2978 When optional argument FIRST-MATCH is non-nil, stop at the first
2979 match for which FUN doesn't return nil, and return that value.
2981 Nil values returned from FUN are ignored in the result."
2982 ;; Ensure TYPES is a list, even of one element.
2983 (unless (listp types
) (setq types
(list types
)))
2984 ;; Recursion depth is determined by --CATEGORY.
2987 ((loop for type in types
2988 always
(memq type org-element-greater-elements
))
2990 ((loop for type in types
2991 always
(memq type org-element-all-elements
))
2994 walk-tree
; For byte-compiler
2998 (lambda (--type types fun --blob --local
)
2999 ;; Check if TYPE is matching among TYPES. If so, apply
3000 ;; FUN to --BLOB and accumulate return value
3001 ;; into --ACC. --LOCAL is the communication channel.
3002 (when (memq --type types
)
3003 (let ((result (funcall fun --blob --local
)))
3004 (cond ((not result
))
3005 (first-match (throw 'first-match result
))
3006 (t (push result --acc
))))))))
3009 (lambda (--data --local
)
3010 ;; Recursively walk DATA. --LOCAL, if non-nil, is
3011 ;; a plist holding contextual information.
3014 (let ((--type (if (stringp --blob
) 'plain-text
(car --blob
))))
3015 ;; Determine if a recursion into --BLOB is
3016 ;; possible and allowed.
3018 ;; Element or object not exportable.
3019 ((and info
(org-export-skip-p --blob info
)))
3020 ;; Archived headline: Maybe apply fun on it, but
3023 (eq --type
'headline
)
3024 (eq (plist-get info
:with-archived-trees
) 'headline
)
3025 (org-element-get-property :archivedp --blob
))
3026 (funcall accumulate-maybe --type types fun --blob --local
))
3027 ;; Limiting recursion to greater elements, and --BLOB
3029 ((and (eq --category
'greater-elements
)
3030 (not (memq --type org-element-greater-elements
)))
3031 (funcall accumulate-maybe --type types fun --blob --local
))
3032 ;; Limiting recursion to elements, and --BLOB only
3033 ;; contains objects.
3034 ((and (eq --category
'elements
) (eq --type
'paragraph
)))
3035 ;; No limitation on recursion, but --BLOB hasn't
3036 ;; got a recursive type.
3037 ((and (eq --category
'objects
)
3038 (not (or (eq --type
'paragraph
)
3039 (memq --type org-element-greater-elements
)
3040 (memq --type org-element-recursive-objects
))))
3041 (funcall accumulate-maybe --type types fun --blob --local
))
3042 ;; Recursion is possible and allowed: Update local
3043 ;; information and move into --BLOB.
3044 (t (funcall accumulate-maybe --type types fun --blob --local
)
3050 ,(cons --blob
(plist-get --local
:genealogy
)))))))))
3051 (org-element-get-contents --data
))))))
3053 (funcall walk-tree data info
)
3054 ;; Return value in a proper order.
3057 ;; The following functions are internal parts of the parser.
3059 ;; The first one, `org-element-parse-elements' acts at the element's
3060 ;; level. As point is always at the beginning of an element during
3061 ;; parsing, it doesn't have to rely on `org-element-at-point'.
3062 ;; Instead, it calls a more restrictive, though way quicker,
3063 ;; alternative: `org-element-current-element'. That function
3064 ;; internally uses `org-element--element-block-re' for quick access to
3067 ;; The second one, `org-element-parse-objects' applies on all objects
3068 ;; of a paragraph or a secondary string. It uses
3069 ;; `org-element-get-candidates' to optimize the search of the next
3070 ;; object in the buffer.
3072 ;; More precisely, that function looks for every allowed object type
3073 ;; first. Then, it discards failed searches, keeps further matches,
3074 ;; and searches again types matched behind point, for subsequent
3075 ;; calls. Thus, searching for a given type fails only once, and every
3076 ;; object is searched only once at top level (but sometimes more for
3079 (defun org-element-parse-elements
3080 (beg end special structure granularity visible-only acc
)
3081 "Parse elements between BEG and END positions.
3083 SPECIAL prioritize some elements over the others. It can set to
3084 `quote-section', `section' or `item', which will focus search,
3085 respectively, on quote sections, sections and items. Moreover,
3086 when value is `item', STRUCTURE will be used as the current list
3089 GRANULARITY determines the depth of the recursion. It can be set
3090 to the following symbols:
3092 `headline' Only parse headlines.
3093 `greater-element' Don't recurse into greater elements. Thus,
3094 elements parsed are the top-level ones.
3095 `element' Parse everything but objects and plain text.
3096 `object' or nil Parse the complete buffer.
3098 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3101 Elements are accumulated into ACC."
3104 (narrow-to-region beg end
)
3106 ;; When parsing only headlines, skip any text before first one.
3107 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3108 (org-with-limited-levels (outline-next-heading)))
3112 ;; 1. Item mode is active: point must be at an item. Parse it
3113 ;; directly, skipping `org-element-current-element'.
3114 (if (eq special
'item
)
3115 (let ((element (org-element-item-parser structure
)))
3116 (goto-char (org-element-get-property :end element
))
3117 (org-element-parse-elements
3118 (org-element-get-property :contents-begin element
)
3119 (org-element-get-property :contents-end element
)
3120 nil structure granularity visible-only
(reverse element
)))
3121 ;; 2. When ITEM is nil, find current element's type and parse
3122 ;; it accordingly to its category.
3123 (let ((element (org-element-current-element special structure
)))
3124 (goto-char (org-element-get-property :end element
))
3126 ;; Case 1. ELEMENT is a paragraph. Parse objects inside,
3127 ;; if GRANULARITY allows it.
3128 ((and (eq (car element
) 'paragraph
)
3129 (or (not granularity
) (eq granularity
'object
)))
3130 (org-element-parse-objects
3131 (org-element-get-property :contents-begin element
)
3132 (org-element-get-property :contents-end element
)
3133 (reverse element
) nil
))
3134 ;; Case 2. ELEMENT is recursive: parse it between
3135 ;; `contents-begin' and `contents-end'. Make sure
3136 ;; GRANULARITY allows the recursion, or ELEMENT is an
3137 ;; headline, in which case going inside is mandatory, in
3138 ;; order to get sub-level headings. If VISIBLE-ONLY is
3139 ;; true and element is hidden, do not recurse into it.
3140 ((and (memq (car element
) org-element-greater-elements
)
3141 (or (not granularity
)
3142 (memq granularity
'(element object
))
3143 (eq (car element
) 'headline
))
3144 (not (and visible-only
3145 (org-element-get-property :hiddenp element
))))
3146 (org-element-parse-elements
3147 (org-element-get-property :contents-begin element
)
3148 (org-element-get-property :contents-end element
)
3149 ;; At a plain list, switch to item mode. At an
3150 ;; headline, switch to section mode. Any other
3151 ;; element turns off special modes.
3154 (headline (if (org-element-get-property :quotedp element
)
3157 (org-element-get-property :structure element
)
3158 granularity visible-only
(reverse element
)))
3159 ;; Case 3. Else, just accumulate ELEMENT.
3165 (defconst org-element--element-block-re
3166 (format "[ \t]*#\\+begin_\\(%s\\)\\(?: \\|$\\)"
3169 (mapcar 'car org-element-non-recursive-block-alist
) "\\|"))
3170 "Regexp matching the beginning of a non-recursive block type.
3171 Used internally by `org-element-current-element'. Do not modify
3172 it directly, set `org-element-recursive-block-alist' instead.")
3174 (defun org-element-current-element (&optional special structure
)
3175 "Parse the element at point.
3177 Return value is a list \(TYPE PROPS\) where TYPE is the type of
3178 the element and PROPS a plist of properties associated to the
3181 Possible types are defined in `org-element-all-elements'.
3183 Optional argument SPECIAL, when non-nil, can be either `item',
3184 `section' or `quote-section'. `item' allows to parse item wise
3185 instead of plain-list wise, using STRUCTURE as the current list
3186 structure. `section' (resp. `quote-section') will try to parse
3187 a section (resp. a quote section) before anything else.
3189 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3192 Unlike to `org-element-at-point', this function assumes point is
3193 always at the beginning of the element it has to parse. As such,
3194 it is quicker than its counterpart and always accurate, albeit
3198 ;; If point is at an affiliated keyword, try moving to the
3199 ;; beginning of the associated element. If none is found, the
3200 ;; keyword is orphaned and will be treated as plain text.
3201 (when (looking-at org-element--affiliated-re
)
3202 (let ((opoint (point)))
3203 (while (looking-at org-element--affiliated-re
) (forward-line))
3204 (when (looking-at "[ \t]*$") (goto-char opoint
))))
3205 (let ((case-fold-search t
))
3208 ((org-with-limited-levels (org-at-heading-p))
3209 (org-element-headline-parser))
3211 ((eq special
'quote-section
) (org-element-quote-section-parser))
3213 ((eq special
'section
) (org-element-section-parser))
3214 ;; Non-recursive block.
3215 ((when (looking-at org-element--element-block-re
)
3216 (let ((type (downcase (match-string 1))))
3219 (format "[ \t]*#\\+end_%s\\(?: \\|$\\)" type
) nil t
))
3220 ;; Build appropriate parser.
3223 (format "org-element-%s-parser"
3225 org-element-non-recursive-block-alist
)))))
3226 (org-element-paragraph-parser)))))
3228 ((org-at-heading-p) (org-element-inlinetask-parser))
3229 ;; LaTeX Environment or paragraph if incomplete.
3230 ((looking-at "^[ \t]*\\\\begin{")
3232 (re-search-forward "^[ \t]*\\\\end{[^}]*}[ \t]*" nil t
))
3233 (org-element-latex-environment-parser)
3234 (org-element-paragraph-parser)))
3236 ((looking-at org-property-start-re
)
3237 (if (save-excursion (re-search-forward org-property-end-re nil t
))
3238 (org-element-property-drawer-parser)
3239 (org-element-paragraph-parser)))
3240 ;; Recursive block, or paragraph if incomplete.
3241 ((looking-at "[ \t]*#\\+begin_\\([-A-Za-z0-9]+\\)\\(?: \\|$\\)")
3242 (let ((type (downcase (match-string 1))))
3244 ((not (save-excursion
3246 (format "[ \t]*#\\+end_%s\\(?: \\|$\\)" type
) nil t
)))
3247 (org-element-paragraph-parser))
3248 ((string= type
"center") (org-element-center-block-parser))
3249 ((string= type
"quote") (org-element-quote-block-parser))
3250 (t (org-element-special-block-parser)))))
3252 ((looking-at org-drawer-regexp
)
3253 (if (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
))
3254 (org-element-drawer-parser)
3255 (org-element-paragraph-parser)))
3256 ((looking-at "[ \t]*:\\( \\|$\\)") (org-element-fixed-width-parser))
3258 ((looking-at org-babel-block-lob-one-liner-regexp
)
3259 (org-element-babel-call-parser))
3260 ;; Keyword, or paragraph if at an affiliated keyword.
3261 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
3262 (let ((key (downcase (match-string 1))))
3263 (if (or (string= key
"tblfm")
3264 (member key org-element-affiliated-keywords
))
3265 (org-element-paragraph-parser)
3266 (org-element-keyword-parser))))
3267 ;; Footnote definition.
3268 ((looking-at org-footnote-definition-re
)
3269 (org-element-footnote-definition-parser))
3270 ;; Dynamic block or paragraph if incomplete.
3271 ((looking-at "[ \t]*#\\+begin:\\(?: \\|$\\)")
3273 (re-search-forward "^[ \t]*#\\+end:\\(?: \\|$\\)" nil t
))
3274 (org-element-dynamic-block-parser)
3275 (org-element-paragraph-parser)))
3277 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)")
3278 (org-element-comment-parser))
3280 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3281 (org-element-horizontal-rule-parser))
3283 ((org-at-table-p t
) (org-element-table-parser))
3285 ((looking-at (org-item-re))
3286 (if (eq special
'item
)
3287 (org-element-item-parser (or structure
(org-list-struct)))
3288 (org-element-plain-list-parser (or structure
(org-list-struct)))))
3289 ;; Default element: Paragraph.
3290 (t (org-element-paragraph-parser))))))
3292 (defun org-element-parse-objects (beg end acc restriction
)
3293 "Parse objects between BEG and END and return recursive structure.
3295 Objects are accumulated in ACC.
3297 RESTRICTION, when non-nil, is a list of object types which are
3298 allowed in the current object."
3299 (let ((get-next-object
3302 ;; Return the parsing function associated to the nearest
3303 ;; object among list of candidates CAND.
3304 (let ((pos (apply #'min
(mapcar #'cdr cand
))))
3309 (format "org-element-%s-parser" (car (rassq pos cand
))))))))))
3310 next-object candidates
)
3313 (while (setq candidates
(org-element-get-next-object-candidates
3314 end restriction candidates
))
3315 (setq next-object
(funcall get-next-object candidates
))
3316 ;; 1. Text before any object. Untabify it.
3317 (let ((obj-beg (org-element-get-property :begin next-object
)))
3318 (unless (= (point) obj-beg
)
3319 (push (replace-regexp-in-string
3320 "\t" (make-string tab-width ?
)
3321 (buffer-substring-no-properties (point) obj-beg
))
3324 (let ((obj-end (org-element-get-property :end next-object
))
3325 (cont-beg (org-element-get-property :contents-begin next-object
)))
3326 (push (if (and (memq (car next-object
) org-element-recursive-objects
)
3328 ;; ... recursive. The CONT-BEG check is for
3329 ;; links, as some of them might not be recursive
3330 ;; (i.e. plain links).
3334 (org-element-get-property :contents-end next-object
))
3335 (org-element-parse-objects
3336 (point-min) (point-max) (reverse next-object
)
3337 ;; Restrict allowed objects. This is the
3338 ;; intersection of current restriction and next
3339 ;; object's restriction.
3341 (cdr (assq (car next-object
)
3342 org-element-object-restrictions
))))
3343 (if (not restriction
) new-restr
3345 (lambda (e) (and (memq e restriction
) e
))
3347 ;; ... not recursive.
3350 (goto-char obj-end
)))
3351 ;; 3. Text after last object. Untabify it.
3352 (unless (= (point) end
)
3353 (push (replace-regexp-in-string
3354 "\t" (make-string tab-width ?
)
3355 (buffer-substring-no-properties (point) end
))
3360 (defun org-element-get-next-object-candidates (limit restriction objects
)
3361 "Return an alist of candidates for the next object.
3363 LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
3364 the possible object types.
3366 Return value is an alist whose car is position and cdr the object
3367 type, as a string. There is an association for the closest
3368 object of each type within RESTRICTION when non-nil, or for every
3371 OBJECTS is the previous candidates alist."
3372 (let ((restriction (or restriction org-element-all-successors
))
3373 next-candidates types-to-search
)
3374 ;; If no previous result, search every object type in RESTRICTION.
3375 ;; Otherwise, keep potential candidates (old objects located after
3376 ;; point) and ask to search again those which had matched before.
3377 (if (not objects
) (setq types-to-search restriction
)
3379 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3380 (push obj next-candidates
)))
3382 ;; Call the appropriate "get-next" function for each type to
3383 ;; search and accumulate matches.
3386 (let* ((successor-fun
3388 (format "org-element-%s-successor"
3389 (or (cdr (assq type org-element-object-successor-alist
))
3391 (obj (funcall successor-fun limit
)))
3392 (and obj
(push obj next-candidates
))))
3399 ;;; Towards A Bijective Process
3401 ;; The parse tree obtained with `org-element-parse-buffer' is really
3402 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3403 ;; interpreted and expanded into a string with canonical Org
3404 ;; syntax. Hence `org-element-interpret-data'.
3406 ;; Data parsed from secondary strings, whose shape is slightly
3407 ;; different than the standard parse tree, is expanded with the
3408 ;; equivalent function `org-element-interpret-secondary'.
3410 ;; Both functions rely internally on
3411 ;; `org-element-interpret--affiliated-keywords'.
3413 (defun org-element-interpret-data (data &optional genealogy previous
)
3414 "Interpret a parse tree representing Org data.
3416 DATA is the parse tree to interpret.
3418 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3420 GENEALOGY is the list of its parents types.
3421 PREVIOUS is the type of the element or object at the same level
3424 Return Org syntax as a string."
3427 ;; BLOB can be an element, an object, a string, or nil.
3430 ((equal blob
"") nil
)
3431 ((stringp blob
) blob
)
3433 (let* ((type (car blob
))
3435 (if (eq type
'org-data
) 'identity
3436 (intern (format "org-element-%s-interpreter" type
))))
3439 ;; Full Org document.
3440 ((eq type
'org-data
)
3441 (org-element-interpret-data blob genealogy previous
))
3442 ;; Recursive objects.
3443 ((memq type org-element-recursive-objects
)
3444 (org-element-interpret-data
3445 blob
(cons type genealogy
) nil
))
3446 ;; Recursive elements.
3447 ((memq type org-element-greater-elements
)
3448 (org-element-normalize-string
3449 (org-element-interpret-data
3450 blob
(cons type genealogy
) nil
)))
3452 ((eq type
'paragraph
)
3454 (org-element-normalize-contents
3456 ;; When normalizing contents of an item,
3457 ;; ignore first line's indentation.
3459 (memq (car genealogy
)
3460 '(footnote-definiton item
))))))
3461 (org-element-interpret-data
3462 paragraph
(cons type genealogy
) nil
)))))
3463 (results (funcall interpreter blob contents
)))
3465 (setq previous type
)
3466 ;; Build white spaces.
3468 ((eq type
'org-data
) results
)
3469 ((memq type org-element-all-elements
)
3471 (org-element-interpret--affiliated-keywords blob
)
3472 (org-element-normalize-string results
)
3473 (make-string (org-element-get-property :post-blank blob
) 10)))
3477 (org-element-get-property :post-blank blob
) 32))))))))
3478 (org-element-get-contents data
) ""))
3480 (defun org-element-interpret-secondary (secondary)
3481 "Interpret SECONDARY string as Org syntax.
3483 SECONDARY-STRING is a nested list as returned by
3484 `org-element-parse-secondary-string'.
3486 Return interpreted string."
3487 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3488 (let ((s (if (listp secondary
) secondary
(list secondary
))))
3489 (org-element-interpret-data `(org-data nil
,@s
) nil nil
)))
3491 ;; Both functions internally use `org-element--affiliated-keywords'.
3493 (defun org-element-interpret--affiliated-keywords (element)
3494 "Return ELEMENT's affiliated keywords as Org syntax.
3495 If there is no affiliated keyword, return the empty string."
3496 (let ((keyword-to-org
3500 (when (member key org-element-dual-keywords
)
3501 (setq dual
(cdr value
) value
(car value
)))
3502 (concat "#+" key
(and dual
(format "[%s]" dual
)) ": "
3503 (if (member key org-element-parsed-keywords
)
3504 (org-element-interpret-secondary value
)
3509 (let ((value (org-element-get-property (intern (concat ":" key
)) element
)))
3511 (if (member key org-element-multiple-keywords
)
3512 (mapconcat (lambda (line)
3513 (funcall keyword-to-org key line
))
3515 (funcall keyword-to-org key value
)))))
3516 ;; Remove translated keywords.
3520 (and (not (assoc key org-element-keyword-translation-alist
)) key
))
3521 org-element-affiliated-keywords
))
3524 ;; Because interpretation of the parse tree must return the same
3525 ;; number of blank lines between elements and the same number of white
3526 ;; space after objects, some special care must be given to white
3529 ;; The first function, `org-element-normalize-string', ensures any
3530 ;; string different from the empty string will end with a single
3531 ;; newline character.
3533 ;; The second function, `org-element-normalize-contents', removes
3534 ;; global indentation from the contents of the current element.
3536 (defun org-element-normalize-string (s)
3537 "Ensure string S ends with a single newline character.
3539 If S isn't a string return it unchanged. If S is the empty
3540 string, return it. Otherwise, return a new string with a single
3541 newline character at its end."
3543 ((not (stringp s
)) s
)
3545 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3546 (replace-match "\n" nil nil s
)))))
3548 (defun org-element-normalize-contents (element &optional ignore-first
)
3549 "Normalize plain text in ELEMENT's contents.
3551 ELEMENT must only contain plain text and objects.
3553 The following changes are applied to plain text:
3554 - Remove global indentation, preserving relative one.
3557 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3558 indentation to compute maximal common indentation.
3560 Return the normalized element."
3562 (list (car element
) (nth 1 element
))
3563 (let ((contents (org-element-get-contents element
)))
3564 (if (not (or ignore-first
(stringp (car contents
)))) contents
3566 ;; 1. Get maximal common indentation (MCI) among each string
3568 (let* ((ind-list (unless ignore-first
3569 (list (org-get-string-indentation (car contents
)))))
3573 (if (not (stringp object
)) object
3575 (while (string-match "\n\\( *\\)" object start
)
3576 (setq start
(match-end 0))
3577 (push (length (match-string 1 object
)) ind-list
))
3580 (mci (if ind-list
(apply 'min ind-list
)
3581 (throw 'exit contents
))))
3582 ;; 2. Remove that indentation from CONTENTS. First string
3583 ;; must be treated differently because it's the only one
3584 ;; whose indentation doesn't happen after a newline
3586 (let ((first-obj (car contents
)))
3587 (unless (or (not (stringp first-obj
)) ignore-first
)
3589 (cons (replace-regexp-in-string
3590 (format "\\` \\{%d\\}" mci
) "" first-obj
)
3592 (mapcar (lambda (object)
3593 (if (not (stringp object
)) object
3594 (replace-regexp-in-string
3595 (format "\n \\{%d\\}" mci
) "\n" object
)))
3602 ;; Once the structure of an Org file is well understood, it's easy to
3603 ;; implement some replacements for `forward-paragraph'
3604 ;; `backward-paragraph', namely `org-element-forward' and
3605 ;; `org-element-backward'.
3607 ;; Also, `org-transpose-elements' mimics the behaviour of
3608 ;; `transpose-words', at the element's level, whereas
3609 ;; `org-element-drag-forward', `org-element-drag-backward', and
3610 ;; `org-element-up' generalize, respectively, functions
3611 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3613 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3614 ;; smartly remove global indentation from buffer, making it possible
3615 ;; to use Org indent mode on a file created with hard indentation.
3617 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3618 ;; internally by some of the previously cited tools.
3620 (defsubst org-element-nested-p
(elem-A elem-B
)
3621 "Non-nil when elements ELEM-A and ELEM-B are nested."
3622 (let ((beg-A (org-element-get-property :begin elem-A
))
3623 (beg-B (org-element-get-property :begin elem-B
))
3624 (end-A (org-element-get-property :end elem-A
))
3625 (end-B (org-element-get-property :end elem-B
)))
3626 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
3627 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
3629 (defun org-element-swap-A-B (elem-A elem-B
)
3630 "Swap elements ELEM-A and ELEM-B.
3632 Leave point at the end of ELEM-A.
3634 Assume ELEM-A is before ELEM-B and that they are not nested."
3635 (goto-char (org-element-get-property :begin elem-A
))
3636 (let* ((beg-B (org-element-get-property :begin elem-B
))
3637 (end-B-no-blank (save-excursion
3638 (goto-char (org-element-get-property :end elem-B
))
3639 (skip-chars-backward " \r\t\n")
3642 (beg-A (org-element-get-property :begin elem-A
))
3643 (end-A-no-blank (save-excursion
3644 (goto-char (org-element-get-property :end elem-A
))
3645 (skip-chars-backward " \r\t\n")
3648 (body-A (buffer-substring beg-A end-A-no-blank
))
3649 (body-B (buffer-substring beg-B end-B-no-blank
))
3650 (between-A-B (buffer-substring end-A-no-blank beg-B
)))
3651 (delete-region beg-A end-B-no-blank
)
3652 (insert body-B between-A-B body-A
)
3653 (goto-char (org-element-get-property :end elem-B
))))
3655 (defun org-element-backward ()
3656 "Move backward by one element."
3658 (let* ((opoint (point))
3659 (element (org-element-at-point))
3660 (start-el-beg (org-element-get-property :begin element
)))
3661 ;; At an headline. The previous element is the previous sibling,
3662 ;; or the parent if any.
3664 ;; Already at the beginning of the current element: move to the
3665 ;; beginning of the previous one.
3666 ((= opoint start-el-beg
)
3668 (skip-chars-backward " \r\t\n")
3669 (let* ((prev-element (org-element-at-point))
3670 (itemp (org-in-item-p))
3672 (save-excursion (goto-char itemp
)
3673 (org-list-struct)))))
3674 ;; When moving into a new list, go directly at the
3675 ;; beginning of the top list structure.
3676 (if (and itemp
(<= (org-list-get-bottom-point struct
) opoint
))
3678 (goto-char (org-list-get-top-point struct
))
3679 (goto-char (org-element-get-property
3680 :begin
(org-element-at-point))))
3681 (goto-char (org-element-get-property :begin prev-element
))))
3682 (while (org-truely-invisible-p) (org-element-up)))
3683 ;; Else, move at the element beginning. One exception: if point
3684 ;; was in the blank lines after the end of a list, move directly
3688 (if (and (setq itemp
(org-in-item-p))
3689 (<= (org-list-get-bottom-point
3690 (save-excursion (goto-char itemp
)
3691 (setq struct
(org-list-struct))))
3693 (progn (goto-char (org-list-get-top-point struct
))
3694 (goto-char (org-element-get-property
3695 :begin
(org-element-at-point))))
3696 (goto-char start-el-beg
)))))))
3698 (defun org-element-drag-backward ()
3699 "Drag backward element at point."
3701 (let* ((pos (point))
3702 (elem (org-element-at-point)))
3703 (when (= (progn (goto-char (point-min))
3704 (org-skip-whitespace)
3706 (org-element-get-property :end elem
))
3707 (error "Cannot drag element backward"))
3708 (goto-char (org-element-get-property :begin elem
))
3709 (org-element-backward)
3710 (let ((prev-elem (org-element-at-point)))
3711 (when (or (org-element-nested-p elem prev-elem
)
3712 (and (eq (car elem
) 'headline
)
3713 (not (eq (car prev-elem
) 'headline
))))
3715 (error "Cannot drag element backward"))
3716 ;; Compute new position of point: it's shifted by PREV-ELEM
3718 (let ((size-prev (- (org-element-get-property :end prev-elem
)
3719 (org-element-get-property :begin prev-elem
))))
3720 (org-element-swap-A-B prev-elem elem
)
3721 (goto-char (- pos size-prev
))))))
3723 (defun org-element-drag-forward ()
3724 "Move forward element at point."
3726 (let* ((pos (point))
3727 (elem (org-element-at-point)))
3728 (when (= (point-max) (org-element-get-property :end elem
))
3729 (error "Cannot drag element forward"))
3730 (goto-char (org-element-get-property :end elem
))
3731 (let ((next-elem (org-element-at-point)))
3732 (when (or (org-element-nested-p elem next-elem
)
3733 (and (eq (car next-elem
) 'headline
)
3734 (not (eq (car elem
) 'headline
))))
3736 (error "Cannot drag element forward"))
3737 ;; Compute new position of point: it's shifted by NEXT-ELEM
3738 ;; body's length (without final blanks) and by the length of
3739 ;; blanks between ELEM and NEXT-ELEM.
3740 (let ((size-next (- (save-excursion
3741 (goto-char (org-element-get-property :end next-elem
))
3742 (skip-chars-backward " \r\t\n")
3745 (org-element-get-property :begin next-elem
)))
3746 (size-blank (- (org-element-get-property :end elem
)
3748 (goto-char (org-element-get-property :end elem
))
3749 (skip-chars-backward " \r\t\n")
3752 (org-element-swap-A-B elem next-elem
)
3753 (goto-char (+ pos size-next size-blank
))))))
3755 (defun org-element-forward ()
3756 "Move forward by one element."
3759 (cond ((eobp) (error "Cannot move further down"))
3760 ((looking-at "[ \t]*$")
3761 (org-skip-whitespace)
3762 (goto-char (if (eobp) (point) (point-at-bol))))
3764 (let ((element (org-element-at-point t
))
3767 ;; At an item: Either move to the next element inside, or
3768 ;; to its end if it's hidden.
3769 ((eq (car element
) 'item
)
3770 (if (org-element-get-property :hiddenp element
)
3771 (goto-char (org-element-get-property :end element
))
3773 (re-search-forward org-element-paragraph-separate nil t
)
3774 (org-skip-whitespace)
3775 (beginning-of-line)))
3776 ;; At a recursive element: Either move inside, or if it's
3777 ;; hidden, move to its end.
3778 ((memq (car element
) org-element-greater-elements
)
3779 (let ((cbeg (org-element-get-property :contents-begin element
)))
3781 (if (or (org-element-get-property :hiddenp element
)
3783 (org-element-get-property :end element
)
3785 ;; Else: move to the current element's end.
3786 (t (goto-char (org-element-get-property :end element
))))))))
3788 (defun org-element-mark-element ()
3789 "Put point at beginning of this element, mark at end.
3791 Interactively, if this command is repeated or (in Transient Mark
3792 mode) if the mark is active, it marks the next element after the
3793 ones already marked."
3795 (let (deactivate-mark)
3796 (if (or (and (eq last-command this-command
) (mark t
))
3797 (and transient-mark-mode mark-active
))
3801 (goto-char (org-element-get-property :end
(org-element-at-point)))))
3802 (let ((element (org-element-at-point)))
3804 (push-mark (org-element-get-property :end element
) t t
)
3805 (goto-char (org-element-get-property :begin element
))))))
3807 (defun org-narrow-to-element ()
3808 "Narrow buffer to current element."
3810 (let ((elem (org-element-at-point)))
3812 ((eq (car elem
) 'headline
)
3814 (org-element-get-property :begin elem
)
3815 (org-element-get-property :end elem
)))
3816 ((memq (car elem
) org-element-greater-elements
)
3818 (org-element-get-property :contents-begin elem
)
3819 (org-element-get-property :contents-end elem
)))
3822 (org-element-get-property :begin elem
)
3823 (org-element-get-property :end elem
))))))
3825 (defun org-transpose-elements ()
3826 "Transpose current and previous elements, keeping blank lines between.
3827 Point is moved after both elements."
3829 (org-skip-whitespace)
3831 (cur (org-element-at-point)))
3832 (when (= (save-excursion (goto-char (point-min))
3833 (org-skip-whitespace)
3835 (org-element-get-property :begin cur
))
3836 (error "No previous element"))
3837 (goto-char (org-element-get-property :begin cur
))
3839 (let ((prev (org-element-at-point)))
3840 (when (org-element-nested-p cur prev
)
3842 (error "Cannot transpose nested elements"))
3843 (org-element-swap-A-B prev cur
))))
3845 (defun org-element-unindent-buffer ()
3846 "Un-indent the visible part of the buffer.
3847 Relative indentation \(between items, inside blocks, etc.\) isn't
3850 (unless (eq major-mode
'org-mode
)
3851 (error "Cannot un-indent a buffer not in Org mode"))
3852 (let* ((parse-tree (org-element-parse-buffer 'greater-element
))
3853 unindent-tree
; For byte-compiler.
3857 (mapc (lambda (element)
3858 (if (eq (car element
) 'headline
)
3859 (funcall unindent-tree
3860 (org-element-get-contents element
))
3864 (org-element-get-property :begin element
)
3865 (org-element-get-property :end element
))
3866 (org-do-remove-indentation)))))
3867 (reverse contents
))))))
3868 (funcall unindent-tree
(org-element-get-contents parse-tree
))))
3870 (defun org-element-up ()
3871 "Move to upper element.
3872 Return position at the beginning of the upper element."
3874 (let ((opoint (point)) elem
)
3876 ((bobp) (error "No surrounding element"))
3877 ((org-with-limited-levels (org-at-heading-p))
3878 (or (org-up-heading-safe) (error "No surronding element")))
3879 ((and (org-at-item-p)
3880 (setq elem
(org-element-at-point))
3881 (let* ((top-list-p (zerop (org-element-get-property :level elem
))))
3883 ;; If parent is bound to be in the same list as the
3884 ;; original point, move to that parent.
3885 (let ((struct (org-element-get-property :structure elem
)))
3887 (org-list-get-parent
3888 (point-at-bol) struct
(org-list-parents-alist struct
))))))))
3890 (let* ((elem (or elem
(org-element-at-point)))
3891 (end (save-excursion
3892 (goto-char (org-element-get-property :end elem
))
3893 (skip-chars-backward " \r\t\n")
3897 (goto-char (org-element-get-property :begin elem
))
3899 (while (and (< (org-element-get-property
3900 :end
(setq prev-elem
(org-element-at-point)))
3903 (goto-char (org-element-get-property :begin prev-elem
))
3905 (if (and (bobp) (< (org-element-get-property :end prev-elem
) end
))
3906 (progn (goto-char opoint
)
3907 (error "No surrounding element"))
3908 (goto-char (org-element-get-property :begin prev-elem
))))))))
3911 (provide 'org-element
)
3912 ;;; org-element.el ends here