1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; This file is not part of GNU Emacs.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; An object can be defined anywhere on a line. It may span over more
29 ;; than a line but never contains a blank one. Objects belong to the
30 ;; following types: `emphasis', `entity', `export-snippet',
31 ;; `footnote-reference', `inline-babel-call', `inline-src-block',
32 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
33 ;; `statistics-cookie', `subscript', `superscript', `target',
34 ;; `time-stamp' and `verbatim'.
36 ;; An element always starts and ends at the beginning of a line. The
37 ;; only element's type containing objects is called a `paragraph'.
38 ;; Other types are: `comment', `comment-block', `example-block',
39 ;; `export-block', `fixed-width', `horizontal-rule', `keyword',
40 ;; `latex-environment', `babel-call', `property-drawer',
41 ;; `quote-section', `src-block', `table' and `verse-block'.
43 ;; Elements containing paragraphs are called greater elements.
44 ;; Concerned types are: `center-block', `drawer', `dynamic-block',
45 ;; `footnote-definition', `headline', `inlinetask', `item',
46 ;; `plain-list', `quote-block', `section' and `special-block'.
48 ;; Greater elements (excepted `headline', `item' and `section' types)
49 ;; and elements (excepted `keyword', `babel-call', and
50 ;; `property-drawer' types) can have a fixed set of keywords as
51 ;; attributes. Those are called "affiliated keywords", to distinguish
52 ;; them from others keywords, which are full-fledged elements. In
53 ;; particular, the "name" affiliated keyword allows to label almost
54 ;; any element in an Org buffer.
56 ;; Notwithstanding affiliated keywords, each greater element, element
57 ;; and object has a fixed set of properties attached to it. Among
58 ;; them, three are shared by all types: `:begin' and `:end', which
59 ;; refer to the beginning and ending buffer positions of the
60 ;; considered element or object, and `:post-blank', which holds the
61 ;; number of blank lines, or white spaces, at its end.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (i.e. an item tag, an headline name, a table
65 ;; cell). Such values are called "secondary strings".
67 ;; Lisp-wise, an element or an object can be represented as a list.
68 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
69 ;; TYPE is a symbol describing the Org element or object.
70 ;; PROPERTIES is the property list attached to it. See docstring of
71 ;; appropriate parsing function to get an exhaustive
73 ;; CONTENTS is a list of elements, objects or raw strings contained
74 ;; in the current element or object, when applicable.
76 ;; An Org buffer is a nested list of such elements and objects, whose
77 ;; type is `org-data' and properties is nil.
79 ;; The first part of this file implements a parser and an interpreter
80 ;; for each type of Org syntax.
82 ;; The next two parts introduce three accessors and a function
83 ;; retrieving the smallest element containing point (respectively
84 ;; `org-element-type', `org-element-property', `org-element-contents'
85 ;; and `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-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-property :block-name dynamic-block
)
259 (let ((args (org-element-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-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-property :level headline
))
411 (todo (org-element-property :todo-keyword headline
))
412 (priority (org-element-property :priority headline
))
413 (title (org-element-property :raw-value headline
))
414 (tags (let ((tag-string (org-element-property :tags headline
))
415 (archivedp (org-element-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-property :commentedp headline
))
422 (quotedp (org-element-property :quotedp headline
))
423 (pre-blank (org-element-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-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-property :level inlinetask
))
530 (todo (org-element-property :todo-keyword inlinetask
))
531 (priority (org-element-property :priority inlinetask
))
532 (title (org-element-property :raw-value inlinetask
))
533 (tags (org-element-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-property :begin item
))
626 (struct (org-element-property :structure item
))
627 (pre (org-list-prevs-alist struct
))
628 (bul (org-element-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-property :checkbox item
))
641 (counter (org-element-property :counter item
))
642 (tag (org-element-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-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-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-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-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-property :options example-block
)))
1022 (concat "#+begin_example" (and options
(concat " " options
)) "\n"
1023 (org-remove-indentation
1024 (org-element-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-property :type export-block
)))
1068 (concat (format "#+begin_%s\n" type
)
1069 (org-element-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-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-property :key keyword
)
1178 (org-element-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-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-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-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-property :language src-block
))
1393 (switches (org-element-property :switches src-block
))
1394 (params (org-element-property :parameters src-block
))
1395 (value (let ((val (org-element-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-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-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-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-property :name entity
)
1610 (when (org-element-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-property :back-end export-snippet
)
1678 (org-element-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
))
1709 (org-element-parse-secondary-string
1711 (cdr (assq 'footnote-reference
1712 org-element-string-restrictions
)))))
1713 (type (if (nth 3 ref
) 'inline
'standard
))
1715 (post-blank (progn (goto-char (nth 2 ref
))
1716 (skip-chars-forward " \t")))
1718 `(footnote-reference
1721 :inline-definition
,inline-def
1724 :post-blank
,post-blank
1725 :raw-definition
,raw-def
)))))
1727 (defun org-element-footnote-reference-interpreter (footnote-reference contents
)
1728 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1730 (let ((label (or (org-element-property :label footnote-reference
) "fn:"))
1732 (let ((raw (org-element-property :raw-definition footnote-reference
)))
1733 (if raw
(concat ":" raw
) ""))))
1734 (format "[%s]" (concat label def
))))
1736 (defun org-element-footnote-reference-successor (limit)
1737 "Search for the next footnote-reference object.
1739 LIMIT bounds the search.
1741 Return value is a cons cell whose car is `footnote-reference' and
1742 cdr is beginning position."
1744 (when (setq fn-ref
(org-footnote-get-next-reference nil nil limit
))
1745 (cons 'footnote-reference
(nth 1 fn-ref
)))))
1748 ;;;; Inline Babel Call
1750 (defun org-element-inline-babel-call-parser ()
1751 "Parse inline babel call at point.
1753 Return a list whose car is `inline-babel-call' and cdr a plist with
1754 `:begin', `:end', `:info' and `:post-blank' as keywords.
1756 Assume point is at the beginning of the babel call."
1758 (unless (bolp) (backward-char))
1759 (looking-at org-babel-inline-lob-one-liner-regexp
)
1760 (let ((info (save-match-data (org-babel-lob-get-info)))
1761 (begin (match-end 1))
1762 (post-blank (progn (goto-char (match-end 0))
1763 (skip-chars-forward " \t")))
1769 :post-blank
,post-blank
)))))
1771 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents
)
1772 "Interpret INLINE-BABEL-CALL object as Org syntax.
1774 (let* ((babel-info (org-element-property :info inline-babel-call
))
1775 (main-source (car babel-info
))
1776 (post-options (nth 1 babel-info
)))
1778 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source
)
1779 ;; Remove redundant square brackets.
1781 (match-string 1 main-source
) nil nil main-source
)
1783 (and post-options
(format "[%s]" post-options
)))))
1785 (defun org-element-inline-babel-call-successor (limit)
1786 "Search for the next inline-babel-call object.
1788 LIMIT bounds the search.
1790 Return value is a cons cell whose car is `inline-babel-call' and
1791 cdr is beginning position."
1793 ;; Use a simplified version of
1794 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
1795 (when (re-search-forward
1796 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
1798 (cons 'inline-babel-call
(match-beginning 0)))))
1801 ;;;; Inline Src Block
1803 (defun org-element-inline-src-block-parser ()
1804 "Parse inline source block at point.
1806 Return a list whose car is `inline-src-block' and cdr a plist
1807 with `:begin', `:end', `:language', `:value', `:parameters' and
1808 `:post-blank' as keywords.
1810 Assume point is at the beginning of the inline src block."
1812 (unless (bolp) (backward-char))
1813 (looking-at org-babel-inline-src-block-regexp
)
1814 (let ((begin (match-beginning 1))
1815 (language (org-match-string-no-properties 2))
1816 (parameters (org-match-string-no-properties 4))
1817 (value (org-match-string-no-properties 5))
1818 (post-blank (progn (goto-char (match-end 0))
1819 (skip-chars-forward " \t")))
1822 (:language
,language
1824 :parameters
,parameters
1827 :post-blank
,post-blank
)))))
1829 (defun org-element-inline-src-block-interpreter (inline-src-block contents
)
1830 "Interpret INLINE-SRC-BLOCK object as Org syntax.
1832 (let ((language (org-element-property :language inline-src-block
))
1833 (arguments (org-element-property :parameters inline-src-block
))
1834 (body (org-element-property :value inline-src-block
)))
1835 (format "src_%s%s{%s}"
1837 (if arguments
(format "[%s]" arguments
) "")
1840 (defun org-element-inline-src-block-successor (limit)
1841 "Search for the next inline-babel-call element.
1843 LIMIT bounds the search.
1845 Return value is a cons cell whose car is `inline-babel-call' and
1846 cdr is beginning position."
1848 (when (re-search-forward org-babel-inline-src-block-regexp limit t
)
1849 (cons 'inline-src-block
(match-beginning 1)))))
1854 (defun org-element-latex-fragment-parser ()
1855 "Parse latex fragment at point.
1857 Return a list whose car is `latex-fragment' and cdr a plist with
1858 `:value', `:begin', `:end', and `:post-blank' as keywords.
1860 Assume point is at the beginning of the latex fragment."
1862 (let* ((begin (point))
1866 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps
))))
1867 (when (or (looking-at latex-regexp
)
1871 (looking-at latex-regexp
))))
1872 (throw 'exit
(nth 2 (assoc e org-latex-regexps
))))))
1873 (plist-get org-format-latex-options
:matchers
))
1874 ;; None found: it's a macro.
1875 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
1877 (value (match-string-no-properties substring-match
))
1878 (post-blank (progn (goto-char (match-end substring-match
))
1879 (skip-chars-forward " \t")))
1885 :post-blank
,post-blank
)))))
1887 (defun org-element-latex-fragment-interpreter (latex-fragment contents
)
1888 "Interpret LATEX-FRAGMENT object as Org syntax.
1890 (org-element-property :value latex-fragment
))
1894 (defun org-element-line-break-parser ()
1895 "Parse line break at point.
1897 Return a list whose car is `line-break', and cdr a plist with
1898 `:begin', `:end' and `:post-blank' keywords.
1900 Assume point is at the beginning of the line break."
1901 (let ((begin (point))
1902 (end (save-excursion (forward-line) (point))))
1903 `(line-break (:begin
,begin
:end
,end
:post-blank
0))))
1905 (defun org-element-line-break-interpreter (line-break contents
)
1906 "Interpret LINE-BREAK object as Org syntax.
1910 (defun org-element-line-break-successor (limit)
1911 "Search for the next line-break object.
1913 LIMIT bounds the search.
1915 Return value is a cons cell whose car is `line-break' and cdr is
1916 beginning position."
1918 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t
)
1919 (goto-char (match-beginning 1)))))
1920 ;; A line break can only happen on a non-empty line.
1921 (when (and beg
(re-search-backward "\\S-" (point-at-bol) t
))
1922 (cons 'line-break beg
)))))
1927 (defun org-element-link-parser ()
1928 "Parse link at point.
1930 Return a list whose car is `link' and cdr a plist with `:type',
1931 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
1932 `:contents-end' and `:post-blank' as keywords.
1934 Assume point is at the beginning of the link."
1936 (let ((begin (point))
1937 end contents-begin contents-end link-end post-blank path type
1940 ;; Type 1: Text targeted from a radio target.
1941 ((and org-target-link-regexp
(looking-at org-target-link-regexp
))
1943 link-end
(match-end 0)
1944 path
(org-match-string-no-properties 0)))
1945 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
1946 ((looking-at org-bracket-link-regexp
)
1947 (setq contents-begin
(match-beginning 3)
1948 contents-end
(match-end 3)
1949 link-end
(match-end 0)
1950 ;; RAW-LINK is the original link.
1951 raw-link
(org-match-string-no-properties 1)
1952 link
(org-link-expand-abbrev
1953 (replace-regexp-in-string
1954 " *\n *" " " (org-link-unescape raw-link
) t t
)))
1955 ;; Determine TYPE of link and set PATH accordingly.
1958 ((or (file-name-absolute-p link
) (string-match "^\\.\\.?/" link
))
1959 (setq type
"file" path link
))
1960 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
1961 ((string-match org-link-re-with-space3 link
)
1962 (setq type
(match-string 1 link
) path
(match-string 2 link
)))
1963 ;; Ref type: PATH is the name of the target element.
1964 ((string-match "^ref:\\(.*\\)" link
)
1965 (setq type
"ref" path
(org-trim (match-string 1 link
))))
1966 ;; Id type: PATH is the id.
1967 ((string-match "^id:\\([-a-f0-9]+\\)" link
)
1968 (setq type
"id" path
(match-string 1 link
)))
1969 ;; Code-ref type: PATH is the name of the reference.
1970 ((string-match "^(\\(.*\\))$" link
)
1971 (setq type
"coderef" path
(match-string 1 link
)))
1972 ;; Custom-id type: PATH is the name of the custom id.
1973 ((= (aref link
0) ?
#)
1974 (setq type
"custom-id" path
(substring link
1)))
1975 ;; Fuzzy type: Internal link either matches a target, an
1976 ;; headline name or nothing. PATH is the target or headline's
1978 (t (setq type
"fuzzy" path link
))))
1979 ;; Type 3: Plain link, i.e. http://orgmode.org
1980 ((looking-at org-plain-link-re
)
1981 (setq raw-link
(org-match-string-no-properties 0)
1982 type
(org-match-string-no-properties 1)
1983 path
(org-match-string-no-properties 2)
1984 link-end
(match-end 0)))
1985 ;; Type 4: Angular link, i.e. <http://orgmode.org>
1986 ((looking-at org-angle-link-re
)
1987 (setq raw-link
(buffer-substring-no-properties
1988 (match-beginning 1) (match-end 2))
1989 type
(org-match-string-no-properties 1)
1990 path
(org-match-string-no-properties 2)
1991 link-end
(match-end 0))))
1992 ;; In any case, deduce end point after trailing white space from
1993 ;; LINK-END variable.
1994 (setq post-blank
(progn (goto-char link-end
) (skip-chars-forward " \t"))
1999 :raw-link
,(or raw-link path
)
2002 :contents-begin
,contents-begin
2003 :contents-end
,contents-end
2004 :post-blank
,post-blank
)))))
2006 (defun org-element-link-interpreter (link contents
)
2007 "Interpret LINK object as Org syntax.
2008 CONTENTS is the contents of the object."
2009 (let ((type (org-element-property :type link
))
2010 (raw-link (org-element-property :raw-link link
)))
2011 (if (string= type
"radio") raw-link
2014 (if (string= contents
"") "" (format "[%s]" contents
))))))
2016 (defun org-element-link-successor (limit)
2017 "Search for the next link object.
2019 LIMIT bounds the search.
2021 Return value is a cons cell whose car is `link' and cdr is
2022 beginning position."
2025 (if (not org-target-link-regexp
) org-any-link-re
2026 (concat org-any-link-re
"\\|" org-target-link-regexp
))))
2027 (when (re-search-forward link-regexp limit t
)
2028 (cons 'link
(match-beginning 0))))))
2033 (defun org-element-macro-parser ()
2034 "Parse macro at point.
2036 Return a list whose car is `macro' and cdr a plist with `:key',
2037 `:args', `:begin', `:end', `:value' and `:post-blank' as
2040 Assume point is at the macro."
2042 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2043 (let ((begin (point))
2044 (key (downcase (org-match-string-no-properties 1)))
2045 (value (org-match-string-no-properties 0))
2046 (post-blank (progn (goto-char (match-end 0))
2047 (skip-chars-forward " \t")))
2049 (args (let ((args (org-match-string-no-properties 3)) args2
)
2051 (setq args
(org-split-string args
","))
2053 (while (string-match "\\\\\\'" (car args
))
2054 ;; Repair bad splits.
2055 (setcar (cdr args
) (concat (substring (car args
) 0 -
1)
2058 (push (pop args
) args2
))
2059 (mapcar 'org-trim
(nreverse args2
))))))
2066 :post-blank
,post-blank
)))))
2068 (defun org-element-macro-interpreter (macro contents
)
2069 "Interpret MACRO object as Org syntax.
2071 (org-element-property :value macro
))
2073 (defun org-element-macro-successor (limit)
2074 "Search for the next macro object.
2076 LIMIT bounds the search.
2078 Return value is cons cell whose car is `macro' and cdr is
2079 beginning position."
2081 (when (re-search-forward
2082 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2084 (cons 'macro
(match-beginning 0)))))
2089 (defun org-element-radio-target-parser ()
2090 "Parse radio target at point.
2092 Return a list whose car is `radio-target' and cdr a plist with
2093 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2094 and `:post-blank' as keywords.
2096 Assume point is at the radio target."
2098 (looking-at org-radio-target-regexp
)
2099 (let ((begin (point))
2100 (contents-begin (match-beginning 1))
2101 (contents-end (match-end 1))
2102 (raw-value (org-match-string-no-properties 1))
2103 (post-blank (progn (goto-char (match-end 0))
2104 (skip-chars-forward " \t")))
2109 :contents-begin
,contents-begin
2110 :contents-end
,contents-end
2111 :raw-value
,raw-value
2112 :post-blank
,post-blank
)))))
2114 (defun org-element-radio-target-interpreter (target contents
)
2115 "Interpret TARGET object as Org syntax.
2116 CONTENTS is the contents of the object."
2117 (concat "<<<" contents
">>>"))
2119 (defun org-element-radio-target-successor (limit)
2120 "Search for the next radio-target object.
2122 LIMIT bounds the search.
2124 Return value is a cons cell whose car is `radio-target' and cdr
2125 is beginning position."
2127 (when (re-search-forward org-radio-target-regexp limit t
)
2128 (cons 'radio-target
(match-beginning 0)))))
2131 ;;;; Statistics Cookie
2133 (defun org-element-statistics-cookie-parser ()
2134 "Parse statistics cookie at point.
2136 Return a list whose car is `statistics-cookie', and cdr a plist
2137 with `:begin', `:end', `:value' and `:post-blank' keywords.
2139 Assume point is at the beginning of the statistics-cookie."
2141 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2142 (let* ((begin (point))
2143 (value (buffer-substring-no-properties
2144 (match-beginning 0) (match-end 0)))
2145 (post-blank (progn (goto-char (match-end 0))
2146 (skip-chars-forward " \t")))
2152 :post-blank
,post-blank
)))))
2154 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents
)
2155 "Interpret STATISTICS-COOKIE object as Org syntax.
2157 (org-element-property :value statistics-cookie
))
2159 (defun org-element-statistics-cookie-successor (limit)
2160 "Search for the next statistics cookie object.
2162 LIMIT bounds the search.
2164 Return value is a cons cell whose car is `statistics-cookie' and
2165 cdr is beginning position."
2167 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t
)
2168 (cons 'statistics-cookie
(match-beginning 0)))))
2173 (defun org-element-subscript-parser ()
2174 "Parse subscript at point.
2176 Return a list whose car is `subscript' and cdr a plist with
2177 `:begin', `:end', `:contents-begin', `:contents-end',
2178 `:use-brackets-p' and `:post-blank' as keywords.
2180 Assume point is at the underscore."
2182 (unless (bolp) (backward-char))
2183 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2185 (not (looking-at org-match-substring-regexp
))))
2186 (begin (match-beginning 2))
2187 (contents-begin (or (match-beginning 5)
2188 (match-beginning 3)))
2189 (contents-end (or (match-end 5) (match-end 3)))
2190 (post-blank (progn (goto-char (match-end 0))
2191 (skip-chars-forward " \t")))
2196 :use-brackets-p
,bracketsp
2197 :contents-begin
,contents-begin
2198 :contents-end
,contents-end
2199 :post-blank
,post-blank
)))))
2201 (defun org-element-subscript-interpreter (subscript contents
)
2202 "Interpret SUBSCRIPT object as Org syntax.
2203 CONTENTS is the contents of the object."
2205 (if (org-element-property :use-brackets-p subscript
) "_{%s}" "_%s")
2208 (defun org-element-sub/superscript-successor
(limit)
2209 "Search for the next sub/superscript object.
2211 LIMIT bounds the search.
2213 Return value is a cons cell whose car is either `subscript' or
2214 `superscript' and cdr is beginning position."
2216 (when (re-search-forward org-match-substring-regexp limit t
)
2217 (cons (if (string= (match-string 2) "_") 'subscript
'superscript
)
2218 (match-beginning 2)))))
2223 (defun org-element-superscript-parser ()
2224 "Parse superscript at point.
2226 Return a list whose car is `superscript' and cdr a plist with
2227 `:begin', `:end', `:contents-begin', `:contents-end',
2228 `:use-brackets-p' and `:post-blank' as keywords.
2230 Assume point is at the caret."
2232 (unless (bolp) (backward-char))
2233 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp
)
2235 (not (looking-at org-match-substring-regexp
))))
2236 (begin (match-beginning 2))
2237 (contents-begin (or (match-beginning 5)
2238 (match-beginning 3)))
2239 (contents-end (or (match-end 5) (match-end 3)))
2240 (post-blank (progn (goto-char (match-end 0))
2241 (skip-chars-forward " \t")))
2246 :use-brackets-p
,bracketsp
2247 :contents-begin
,contents-begin
2248 :contents-end
,contents-end
2249 :post-blank
,post-blank
)))))
2251 (defun org-element-superscript-interpreter (superscript contents
)
2252 "Interpret SUPERSCRIPT object as Org syntax.
2253 CONTENTS is the contents of the object."
2255 (if (org-element-property :use-brackets-p superscript
) "^{%s}" "^%s")
2261 (defun org-element-target-parser ()
2262 "Parse target at point.
2264 Return a list whose car is `target' and cdr a plist with
2265 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2266 and `:post-blank' as keywords.
2268 Assume point is at the target."
2270 (looking-at org-target-regexp
)
2271 (let ((begin (point))
2272 (contents-begin (match-beginning 1))
2273 (contents-end (match-end 1))
2274 (raw-value (org-match-string-no-properties 1))
2275 (post-blank (progn (goto-char (match-end 0))
2276 (skip-chars-forward " \t")))
2281 :contents-begin
,contents-begin
2282 :contents-end
,contents-end
2283 :raw-value
,raw-value
2284 :post-blank
,post-blank
)))))
2286 (defun org-element-target-interpreter (target contents
)
2287 "Interpret TARGET object as Org syntax.
2288 CONTENTS is the contents of target."
2291 (defun org-element-target-successor (limit)
2292 "Search for the next target object.
2294 LIMIT bounds the search.
2296 Return value is a cons cell whose car is `target' and cdr is
2297 beginning position."
2299 (when (re-search-forward org-target-regexp limit t
)
2300 (cons 'target
(match-beginning 0)))))
2305 (defun org-element-time-stamp-parser ()
2306 "Parse time stamp at point.
2308 Return a list whose car is `time-stamp', and cdr a plist with
2309 `:appt-type', `:type', `:begin', `:end', `:value' and
2310 `:post-blank' keywords.
2312 Assume point is at the beginning of the time-stamp."
2314 (let* ((appt-type (cond
2315 ((looking-at (concat org-deadline-string
" +"))
2316 (goto-char (match-end 0))
2318 ((looking-at (concat org-scheduled-string
" +"))
2319 (goto-char (match-end 0))
2321 ((looking-at (concat org-closed-string
" +"))
2322 (goto-char (match-end 0))
2324 (begin (and appt-type
(match-beginning 0)))
2326 ((looking-at org-tsr-regexp
)
2327 (if (match-string 2) 'active-range
'active
))
2328 ((looking-at org-tsr-regexp-both
)
2329 (if (match-string 2) 'inactive-range
'inactive
))
2330 ((looking-at (concat
2331 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2333 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2335 (begin (or begin
(match-beginning 0)))
2336 (value (buffer-substring-no-properties
2337 (match-beginning 0) (match-end 0)))
2338 (post-blank (progn (goto-char (match-end 0))
2339 (skip-chars-forward " \t")))
2342 (:appt-type
,appt-type
2347 :post-blank
,post-blank
)))))
2349 (defun org-element-time-stamp-interpreter (time-stamp contents
)
2350 "Interpret TIME-STAMP object as Org syntax.
2353 (case (org-element-property :appt-type time-stamp
)
2354 (closed (concat org-closed-string
" "))
2355 (deadline (concat org-deadline-string
" "))
2356 (scheduled (concat org-scheduled-string
" ")))
2357 (org-element-property :value time-stamp
)))
2359 (defun org-element-time-stamp-successor (limit)
2360 "Search for the next time-stamp object.
2362 LIMIT bounds the search.
2364 Return value is a cons cell whose car is `time-stamp' and cdr is
2365 beginning position."
2367 (when (re-search-forward
2368 (concat "\\(?:" org-scheduled-string
" +\\|"
2369 org-deadline-string
" +\\|" org-closed-string
" +\\)?"
2372 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2374 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2376 (cons 'time-stamp
(match-beginning 0)))))
2381 (defun org-element-verbatim-parser ()
2382 "Parse verbatim object at point.
2384 Return a list whose car is `verbatim' and cdr is a plist with
2385 `:marker', `:begin', `:end' and `:post-blank' keywords.
2387 Assume point is at the first verbatim marker."
2389 (unless (bolp) (backward-char 1))
2390 (looking-at org-emph-re
)
2391 (let ((begin (match-beginning 2))
2392 (marker (org-match-string-no-properties 3))
2393 (value (org-match-string-no-properties 4))
2394 (post-blank (progn (goto-char (match-end 2))
2395 (skip-chars-forward " \t")))
2402 :post-blank
,post-blank
)))))
2404 (defun org-element-verbatim-interpreter (verbatim contents
)
2405 "Interpret VERBATIM object as Org syntax.
2407 (let ((marker (org-element-property :marker verbatim
))
2408 (value (org-element-property :value verbatim
)))
2409 (concat marker value marker
)))
2413 ;;; Definitions And Rules
2415 ;; Define elements, greater elements and specify recursive objects,
2416 ;; along with the affiliated keywords recognized. Also set up
2417 ;; restrictions on recursive objects combinations.
2419 ;; These variables really act as a control center for the parsing
2421 (defconst org-element-paragraph-separate
2422 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2423 ;; Headlines and inlinetasks.
2424 org-outline-regexp-bol
"\\|"
2425 ;; Comments, blocks (any type), keywords and babel calls.
2426 "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
2428 (org-item-beginning-re) "\\|"
2429 ;; Fixed-width, drawers (any type) and tables.
2431 ;; Footnote definitions.
2432 org-footnote-definition-re
"\\|"
2433 ;; Horizontal rules.
2434 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2435 ;; LaTeX environments.
2436 "^[ \t]*\\\\\\(begin\\|end\\)")
2437 "Regexp to separate paragraphs in an Org buffer.")
2439 (defconst org-element-all-elements
2440 '(center-block comment comment-block drawer dynamic-block example-block
2441 export-block fixed-width footnote-definition headline
2442 horizontal-rule inlinetask item keyword latex-environment
2443 babel-call paragraph plain-list property-drawer quote-block
2444 quote-section section special-block src-block table
2446 "Complete list of elements.")
2448 (defconst org-element-greater-elements
2449 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2450 item plain-list quote-block section special-block
)
2451 "List of recursive element types aka Greater Elements.")
2453 (defconst org-element-all-successors
2454 '(export-snippet footnote-reference inline-babel-call inline-src-block
2455 latex-or-entity line-break link macro radio-target
2456 statistics-cookie sub
/superscript target text-markup
2458 "Complete list of successors.")
2460 (defconst org-element-object-successor-alist
2461 '((subscript . sub
/superscript
) (superscript . sub
/superscript
)
2462 (emphasis . text-markup
) (verbatim . text-markup
)
2463 (entity . latex-or-entity
) (latex-fragment . latex-or-entity
))
2464 "Alist of translations between object type and successor name.
2466 Sharing the same successor comes handy when, for example, the
2467 regexp matching one object can also match the other object.")
2469 (defconst org-element-recursive-objects
2470 '(emphasis link macro subscript superscript target radio-target
)
2471 "List of recursive object types.")
2473 (defconst org-element-non-recursive-block-alist
2474 '(("ascii" . export-block
)
2475 ("comment" . comment-block
)
2476 ("docbook" . export-block
)
2477 ("example" . example-block
)
2478 ("html" . export-block
)
2479 ("latex" . export-block
)
2480 ("odt" . export-block
)
2482 ("verse" . verse-block
))
2483 "Alist between non-recursive block name and their element type.")
2485 (defconst org-element-affiliated-keywords
2486 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "caption"
2487 "data" "header" "headers" "label" "name" "plot" "resname" "result" "results"
2488 "source" "srcname" "tblname")
2489 "List of affiliated keywords as strings.")
2491 (defconst org-element-keyword-translation-alist
2492 '(("data" .
"name") ("label" .
"name") ("resname" .
"name")
2493 ("source" .
"name") ("srcname" .
"name") ("tblname" .
"name")
2494 ("result" .
"results") ("headers" .
"header"))
2495 "Alist of usual translations for keywords.
2496 The key is the old name and the value the new one. The property
2497 holding their value will be named after the translated name.")
2499 (defconst org-element-multiple-keywords
2500 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "header")
2501 "List of affiliated keywords that can occur more that once in an element.
2503 Their value will be consed into a list of strings, which will be
2504 returned as the value of the property.
2506 This list is checked after translations have been applied. See
2507 `org-element-keyword-translation-alist'.")
2509 (defconst org-element-parsed-keywords
'("author" "caption" "title")
2510 "List of keywords whose value can be parsed.
2512 Their value will be stored as a secondary string: a list of
2513 strings and objects.
2515 This list is checked after translations have been applied. See
2516 `org-element-keyword-translation-alist'.")
2518 (defconst org-element-dual-keywords
'("caption" "results")
2519 "List of keywords which can have a secondary value.
2521 In Org syntax, they can be written with optional square brackets
2522 before the colons. For example, results keyword can be
2523 associated to a hash value with the following:
2525 #+results[hash-string]: some-source
2527 This list is checked after translations have been applied. See
2528 `org-element-keyword-translation-alist'.")
2530 (defconst org-element-object-restrictions
2531 '((emphasis entity export-snippet inline-babel-call inline-src-block link
2532 radio-target sub
/superscript target text-markup time-stamp
)
2533 (link entity export-snippet inline-babel-call inline-src-block
2534 latex-fragment link sub
/superscript text-markup
)
2536 (radio-target entity export-snippet latex-fragment sub
/superscript
)
2537 (subscript entity export-snippet inline-babel-call inline-src-block
2538 latex-fragment sub
/superscript text-markup
)
2539 (superscript entity export-snippet inline-babel-call inline-src-block
2540 latex-fragment sub
/superscript text-markup
)
2541 (target entity export-snippet latex-fragment sub
/superscript text-markup
))
2542 "Alist of recursive objects restrictions.
2544 CAR is a recursive object type and CDR is a list of successors
2545 that will be called within an object of such type.
2547 For example, in a `radio-target' object, one can only find
2548 entities, export snippets, latex-fragments, subscript and
2551 (defconst org-element-string-restrictions
2552 '((footnote-reference entity export-snippet inline-babel-call inline-src-block
2553 latex-fragment line-break link macro radio-target
2554 sub
/superscript target text-markup time-stamp
)
2555 (headline entity inline-babel-call inline-src-block latex-fragment link
2556 macro radio-target statistics-cookie sub
/superscript text-markup
2558 (inlinetask entity inline-babel-call inline-src-block latex-fragment link
2559 macro radio-target sub
/superscript text-markup time-stamp
)
2560 (item entity inline-babel-call latex-fragment macro radio-target
2561 sub
/superscript target text-markup
)
2562 (keyword entity latex-fragment macro sub
/superscript text-markup
)
2563 (table entity latex-fragment macro target text-markup
)
2564 (verse-block entity footnote-reference inline-babel-call inline-src-block
2565 latex-fragment line-break link macro radio-target
2566 sub
/superscript target text-markup time-stamp
))
2567 "Alist of secondary strings restrictions.
2569 When parsed, some elements have a secondary string which could
2570 contain various objects (i.e. headline's name, or table's cells).
2571 For association, CAR is the element type, and CDR a list of
2572 successors that will be called in that secondary string.
2574 Note: `keyword' secondary string type only applies to keywords
2575 matching `org-element-parsed-keywords'.")
2577 (defconst org-element-secondary-value-alist
2578 '((headline .
:title
)
2579 (inlinetask .
:title
)
2581 (footnote-reference .
:inline-definition
)
2582 (verse-block .
:value
))
2583 "Alist between element types and location of secondary value.
2584 Only elements with a secondary value available at parse time are
2585 considered here. This is used internally by `org-element-map',
2586 which will look into the secondary strings of an element only if
2587 its type is listed here.")
2593 ;; Provide three accessors: `org-element-type', `org-element-property'
2594 ;; and `org-element-contents'.
2596 (defun org-element-type (element)
2597 "Return type of element ELEMENT.
2599 The function returns the type of the element or object provided.
2600 It can also return the following special value:
2601 `plain-text' for a string
2602 `org-data' for a complete document
2603 nil in any other case."
2605 ((not (consp element
)) (and (stringp element
) 'plain-text
))
2606 ((symbolp (car element
)) (car element
))))
2608 (defun org-element-property (property element
)
2609 "Extract the value from the PROPERTY of an ELEMENT."
2610 (plist-get (nth 1 element
) property
))
2612 (defun org-element-contents (element)
2613 "Extract contents from an ELEMENT."
2618 ;; Obtaining The Smallest Element Containing Point
2620 ;; `org-element-at-point' is the core function of this section. It
2621 ;; returns the Lisp representation of the element at point. It uses
2622 ;; `org-element-guess-type' and `org-element-skip-keywords' as helper
2625 ;; When point is at an item, there is no automatic way to determine if
2626 ;; the function should return the `plain-list' element, or the
2627 ;; corresponding `item' element. By default, `org-element-at-point'
2628 ;; works at the `plain-list' level. But, by providing an optional
2629 ;; argument, one can make it switch to the `item' level.
2631 (defconst org-element--affiliated-re
2632 (format "[ \t]*#\\+\\(%s\\):"
2635 (if (member keyword org-element-dual-keywords
)
2636 (format "\\(%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
2637 (regexp-quote keyword
))
2638 (regexp-quote keyword
)))
2639 org-element-affiliated-keywords
"\\|"))
2640 "Regexp matching any affiliated keyword.
2642 Keyword name is put in match group 1. Moreover, if keyword
2643 belongs to `org-element-dual-keywords', put the dual value in
2646 Don't modify it, set `org-element--affiliated-keywords' instead.")
2648 (defun org-element-at-point (&optional special structure
)
2649 "Determine closest element around point.
2651 Return value is a list \(TYPE PROPS\) where TYPE is the type of
2652 the element and PROPS a plist of properties associated to the
2655 Possible types are defined in `org-element-all-elements'.
2657 Optional argument SPECIAL, when non-nil, can be either `item' or
2658 `section'. The former allows to parse item wise instead of
2659 plain-list wise, using STRUCTURE as the current list structure.
2660 The latter will try to parse a section before anything else.
2662 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
2666 ;; Move before any blank line.
2667 (when (looking-at "[ \t]*$")
2668 (skip-chars-backward " \r\t\n")
2669 (beginning-of-line))
2670 (let ((case-fold-search t
))
2671 ;; Check if point is at an affiliated keyword. In that case,
2672 ;; try moving to the beginning of the associated element. If
2673 ;; the keyword is orphaned, treat it as plain text.
2674 (when (looking-at org-element--affiliated-re
)
2675 (let ((opoint (point)))
2676 (while (looking-at org-element--affiliated-re
) (forward-line))
2677 (when (looking-at "[ \t]*$") (goto-char opoint
))))
2678 (let ((type (org-element-guess-type (eq special
'section
))))
2680 ;; Guessing element type on the current line is impossible:
2681 ;; try to find the beginning of the current element to get
2682 ;; more information.
2684 (let ((search-origin (point))
2685 (opoint-in-item-p (org-in-item-p))
2689 (re-search-backward org-element-paragraph-separate nil
'm
))))
2691 ;; Unable to find a paragraph delimiter above: we're at
2692 ;; bob and looking at a paragraph.
2693 ((not par-found-p
) (org-element-paragraph-parser))
2694 ;; Trying to find element's beginning set point back to
2695 ;; its original position. There's something peculiar on
2696 ;; this line that prevents parsing, probably an
2697 ;; ill-formed keyword or an undefined drawer name. Parse
2698 ;; it as plain text anyway.
2699 ((< search-origin
(point-at-eol)) (org-element-paragraph-parser))
2700 ;; Original point wasn't in a list but previous paragraph
2701 ;; is. It means that either point was inside some block,
2702 ;; or current list was ended without using a blank line.
2703 ;; In the last case, paragraph really starts at list end.
2705 (and (not opoint-in-item-p
)
2706 (not (looking-at "[ \t]*#\\+begin"))
2707 (setq item
(org-in-item-p))
2708 (let ((struct (save-excursion (goto-char item
)
2709 (org-list-struct))))
2710 (goto-char (org-list-get-bottom-point struct
))
2711 (org-skip-whitespace)
2713 (org-element-paragraph-parser)))))
2714 ((org-footnote-at-definition-p)
2715 (org-element-footnote-definition-parser))
2716 ((and opoint-in-item-p
(org-at-item-p) (= opoint-in-item-p
(point)))
2717 (if (eq special
'item
)
2718 (org-element-item-parser (or structure
(org-list-struct)))
2719 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2720 ;; In any other case, the paragraph started the line
2722 (t (forward-line) (org-element-paragraph-parser)))))
2723 ((eq type
'plain-list
)
2724 (if (eq special
'item
)
2725 (org-element-item-parser (or structure
(org-list-struct)))
2726 (org-element-plain-list-parser (or structure
(org-list-struct)))))
2727 ;; Straightforward case: call the appropriate parser.
2728 (t (funcall (intern (format "org-element-%s-parser" type
)))))))))
2731 ;; It is obvious to tell if point is in most elements, either by
2732 ;; looking for a specific regexp in the current line, or by using
2733 ;; already implemented functions. This is the goal of
2734 ;; `org-element-guess-type'.
2736 (defconst org-element--element-block-types
2737 (mapcar 'car org-element-non-recursive-block-alist
)
2738 "List of non-recursive block types, as strings.
2739 Used internally by `org-element-guess-type'. Do not modify it
2740 directly, set `org-element-non-recursive-block-alist' instead.")
2742 (defun org-element-guess-type (&optional section-mode
)
2743 "Return the type of element at point, or nil if undetermined.
2745 This function may move point to an appropriate position for
2746 parsing. Used internally by `org-element-at-point'.
2748 When optional argument SECTION-MODE is non-nil, try to find if
2749 point is in a section in priority."
2750 ;; Beware: Order matters for some cases in that function.
2752 (let ((case-fold-search t
))
2754 ((org-with-limited-levels (org-at-heading-p)) 'headline
)
2755 ((let ((headline (ignore-errors (nth 4 (org-heading-components)))))
2757 (let (case-fold-search)
2758 (string-match (format "^%s\\(?: \\|$\\)" org-quote-string
)
2760 ;; Move to section beginning.
2761 (org-back-to-heading t
)
2763 (org-skip-whitespace)
2766 ;; Any buffer position not at an headline or in a quote section
2767 ;; is inside a section, provided function is actively looking for
2769 (section-mode 'section
)
2770 ;; Non-recursive block.
2771 ((let ((type (org-in-block-p org-element--element-block-types
)))
2772 (and type
(cdr (assoc type org-element-non-recursive-block-alist
)))))
2773 ((org-at-heading-p) 'inlinetask
)
2774 ((org-between-regexps-p
2775 "^[ \t]*\\\\begin{" "^[ \t]*\\\\end{[^}]*}[ \t]*") 'latex-environment
)
2776 ;; Property drawer. Almost `org-at-property-p', but allow drawer
2778 ((org-with-wide-buffer
2779 (and (not (org-before-first-heading-p))
2780 (let ((pblock (org-get-property-block)))
2782 (<= (point) (cdr pblock
))
2783 (>= (point-at-eol) (1- (car pblock
)))))))
2785 ;; Recursive block. If the block isn't complete, parse the
2786 ;; current part as a paragraph.
2787 ((looking-at "[ \t]*#\\+\\(begin\\|end\\)_\\([-A-Za-z0-9]+\\)\\(?:$\\|\\s-\\)")
2788 (let ((type (downcase (match-string 2))))
2790 ((not (org-in-block-p (list type
))) 'paragraph
)
2791 ((string= type
"center") 'center-block
)
2792 ((string= type
"quote") 'quote-block
)
2793 (t 'special-block
))))
2794 ;; Regular drawers must be tested after property drawer as both
2795 ;; elements share the same ending regexp.
2796 ((or (looking-at org-drawer-regexp
) (looking-at "[ \t]*:END:[ \t]*$"))
2797 (let ((completep (org-between-regexps-p
2798 org-drawer-regexp
"^[ \t]*:END:[ \t]*$")))
2799 (if (not completep
) 'paragraph
2800 (goto-char (car completep
)) 'drawer
)))
2801 ((looking-at "[ \t]*:\\( \\|$\\)") 'fixed-width
)
2802 ;; Babel calls must be tested before general keywords as they are
2803 ;; a subset of them.
2804 ((looking-at org-babel-block-lob-one-liner-regexp
) 'babel-call
)
2805 ((looking-at org-footnote-definition-re
) 'footnote-definition
)
2806 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
2807 (if (member (downcase (match-string 1)) org-element-affiliated-keywords
)
2810 ;; Dynamic block: simplify regexp used for match. If it isn't
2811 ;; complete, parse the current part as a paragraph.
2812 ((looking-at "[ \t]*#\\+\\(begin\\end\\):\\(?:\\s-\\|$\\)")
2813 (let ((completep (org-between-regexps-p
2814 "^[ \t]*#\\+begin:\\(?:\\s-\\|$\\)"
2815 "^[ \t]*#\\+end:\\(?:\\s-\\|$\\)")))
2816 (if (not completep
) 'paragraph
2817 (goto-char (car completep
)) 'dynamic-block
)))
2818 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)") 'comment
)
2819 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$") 'horizontal-rule
)
2820 ((org-at-table-p t
) 'table
)
2821 ((looking-at "[ \t]*#\\+tblfm:")
2823 ;; A TBLFM line separated from any table is just plain text.
2824 (if (org-at-table-p) 'table
2825 (forward-line) 'paragraph
))
2826 ((looking-at (org-item-re)) 'plain-list
))))
2828 ;; Most elements can have affiliated keywords. When looking for an
2829 ;; element beginning, we want to move before them, as they belong to
2830 ;; that element, and, in the meantime, collect information they give
2831 ;; into appropriate properties. Hence the following function.
2833 ;; Usage of optional arguments may not be obvious at first glance:
2835 ;; - TRANS-LIST is used to polish keywords names that have evolved
2836 ;; during Org history. In example, even though =result= and
2837 ;; =results= coexist, we want to have them under the same =result=
2838 ;; property. It's also true for "srcname" and "name", where the
2839 ;; latter seems to be preferred nowadays (thus the "name" property).
2841 ;; - CONSED allows to regroup multi-lines keywords under the same
2842 ;; property, while preserving their own identity. This is mostly
2843 ;; used for "attr_latex" and al.
2845 ;; - PARSED prepares a keyword value for export. This is useful for
2846 ;; "caption". Objects restrictions for such keywords are defined in
2847 ;; `org-element-string-restrictions'.
2849 ;; - DUALS is used to take care of keywords accepting a main and an
2850 ;; optional secondary values. For example "results" has its
2851 ;; source's name as the main value, and may have an hash string in
2852 ;; optional square brackets as the secondary one.
2854 ;; A keyword may belong to more than one category.
2856 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
2857 consed parsed duals
)
2858 "Collect affiliated keywords before point.
2860 Optional argument KEY-RE is a regexp matching keywords, which
2861 puts matched keyword in group 1. It defaults to
2862 `org-element--affiliated-re'.
2864 TRANS-LIST is an alist where key is the keyword and value the
2865 property name it should be translated to, without the colons. It
2866 defaults to `org-element-keyword-translation-alist'.
2868 CONSED is a list of strings. Any keyword belonging to that list
2869 will have its value consed. The check is done after keyword
2870 translation. It defaults to `org-element-multiple-keywords'.
2872 PARSED is a list of strings. Any keyword member of this list
2873 will have its value parsed. The check is done after keyword
2874 translation. If a keyword is a member of both CONSED and PARSED,
2875 it's value will be a list of parsed strings. It defaults to
2876 `org-element-parsed-keywords'.
2878 DUALS is a list of strings. Any keyword member of this list can
2879 have two parts: one mandatory and one optional. Its value is
2880 a cons cell whose car is the former, and the cdr the latter. If
2881 a keyword is a member of both PARSED and DUALS, both values will
2882 be parsed. It defaults to `org-element-dual-keywords'.
2884 Return a list whose car is the position at the first of them and
2885 cdr a plist of keywords and values."
2887 (let ((case-fold-search t
)
2888 (key-re (or key-re org-element--affiliated-re
))
2889 (trans-list (or trans-list org-element-keyword-translation-alist
))
2890 (consed (or consed org-element-multiple-keywords
))
2891 (parsed (or parsed org-element-parsed-keywords
))
2892 (duals (or duals org-element-dual-keywords
))
2893 ;; RESTRICT is the list of objects allowed in parsed
2895 (restrict (cdr (assq 'keyword org-element-string-restrictions
)))
2898 (while (and (not (bobp))
2899 (progn (forward-line -
1) (looking-at key-re
)))
2900 (let* ((raw-kwd (downcase (or (match-string 2) (match-string 1))))
2901 ;; Apply translation to RAW-KWD. From there, KWD is
2902 ;; the official keyword.
2903 (kwd (or (cdr (assoc raw-kwd trans-list
)) raw-kwd
))
2904 ;; Find main value for any keyword.
2908 (buffer-substring-no-properties
2909 (match-end 0) (point-at-eol)))))
2910 ;; If KWD is a dual keyword, find its secondary
2911 ;; value. Maybe parse it.
2913 (and (member kwd duals
)
2914 (let ((sec (org-match-string-no-properties 3)))
2915 (if (or (not sec
) (not (member kwd parsed
))) sec
2916 (org-element-parse-secondary-string sec restrict
)))))
2917 ;; Attribute a property name to KWD.
2918 (kwd-sym (and kwd
(intern (concat ":" kwd
)))))
2919 ;; Now set final shape for VALUE.
2920 (when (member kwd parsed
)
2921 (setq value
(org-element-parse-secondary-string value restrict
)))
2922 (when (member kwd duals
)
2923 ;; VALUE is mandatory. Set it to nil if there is none.
2924 (setq value
(and value
(cons value dual-value
))))
2925 (when (member kwd consed
)
2926 (setq value
(cons value
(plist-get output kwd-sym
))))
2927 ;; Eventually store the new value in OUTPUT.
2928 (setq output
(plist-put output kwd-sym value
))))
2929 (unless (looking-at key-re
) (forward-line 1)))
2930 (list (point) output
))))
2936 ;; The two major functions here are `org-element-parse-buffer', which
2937 ;; parses Org syntax inside the current buffer, taking into account
2938 ;; region, narrowing, or even visibility if specified, and
2939 ;; `org-element-parse-secondary-string', which parses objects within
2942 ;; The (almost) almighty `org-element-map' allows to apply a function
2943 ;; on elements or objects matching some type, and accumulate the
2944 ;; resulting values. In an export situation, it also skips unneeded
2945 ;; parts of the parse tree, transparently walks into included files,
2946 ;; and maintain a list of local properties (i.e. those inherited from
2947 ;; parent headlines) for function's consumption.
2949 (defun org-element-parse-buffer (&optional granularity visible-only
)
2950 "Recursively parse the buffer and return structure.
2951 If narrowing is in effect, only parse the visible part of the
2954 Optional argument GRANULARITY determines the depth of the
2955 recursion. It can be set to the following symbols:
2957 `headline' Only parse headlines.
2958 `greater-element' Don't recurse into greater elements. Thus,
2959 elements parsed are the top-level ones.
2960 `element' Parse everything but objects and plain text.
2961 `object' Parse the complete buffer (default).
2963 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
2966 Assume buffer is in Org mode."
2968 (goto-char (point-min))
2969 (org-skip-whitespace)
2970 (nconc (list 'org-data nil
)
2971 (org-element-parse-elements
2972 (point-at-bol) (point-max)
2973 ;; Start is section mode so text before the first headline
2974 ;; belongs to a section.
2975 'section nil granularity visible-only nil
))))
2977 (defun org-element-parse-secondary-string (string restriction
&optional buffer
)
2978 "Recursively parse objects in STRING and return structure.
2980 RESTRICTION, when non-nil, is a symbol limiting the object types
2981 that will be looked after.
2983 Optional argument BUFFER indicates the buffer from where the
2984 secondary string was extracted. It is used to determine where to
2985 get extraneous information for an object \(i.e. when resolving
2986 a link or looking for a footnote definition\). It defaults to
2987 the current buffer."
2990 (org-element-parse-objects (point-min) (point-max) nil restriction
)))
2992 (defun org-element-map (data types fun
&optional info first-match
)
2993 "Map a function on selected elements or objects.
2995 DATA is the parsed tree, as returned by, i.e,
2996 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
2997 of elements or objects types. FUN is the function called on the
2998 matching element or object. It must accept two arguments: the
2999 element or object itself and a plist holding contextual
3002 When optional argument INFO is non-nil, it should be a plist
3003 holding export options. In that case, parts of the parse tree
3004 not exportable according to that property list will be skipped
3005 and files included through a keyword will be visited.
3007 When optional argument FIRST-MATCH is non-nil, stop at the first
3008 match for which FUN doesn't return nil, and return that value.
3010 Nil values returned from FUN are ignored in the result."
3011 ;; Ensure TYPES is a list, even of one element.
3012 (unless (listp types
) (setq types
(list types
)))
3013 ;; Recursion depth is determined by --CATEGORY.
3016 ((loop for type in types
3017 always
(memq type org-element-greater-elements
))
3019 ((loop for type in types
3020 always
(memq type org-element-all-elements
))
3023 ;; --RESTRICTS is a list of element types whose secondary
3024 ;; string could possibly contain an object with a type among
3027 (and (eq --category
'objects
)
3028 (loop for el in org-element-secondary-value-alist
3030 (loop for o in types
3034 org-element-string-restrictions
))))
3036 --walk-tree
; For byte-compiler
3040 (lambda (--type types fun --blob info
)
3041 ;; Check if TYPE is matching among TYPES. If so, apply
3042 ;; FUN to --BLOB and accumulate return value into --ACC.
3043 ;; INFO is the communication channel. If --BLOB has
3044 ;; a secondary string that can contain objects with their
3045 ;; type amond TYPES, look into that string first.
3046 (when (memq --type --restricts
)
3051 ,@(org-element-property
3052 (cdr (assq --type org-element-secondary-value-alist
))
3055 (when (memq --type types
)
3056 (let ((result (funcall fun --blob info
)))
3057 (cond ((not result
))
3058 (first-match (throw 'first-match result
))
3059 (t (push result --acc
))))))))
3062 (lambda (--data info
)
3063 ;; Recursively walk DATA. INFO, if non-nil, is
3064 ;; a plist holding contextual information.
3067 (let ((--type (org-element-type --blob
)))
3068 ;; Determine if a recursion into --BLOB is
3069 ;; possible and allowed.
3071 ;; Element or object not exportable.
3072 ((member --blob
(plist-get info
:ignore-list
)))
3073 ;; Archived headline: Maybe apply FUN on it, but
3076 (eq --type
'headline
)
3077 (eq (plist-get info
:with-archived-trees
) 'headline
)
3078 (org-element-property :archivedp --blob
))
3079 (funcall --check-blob
3081 ;; Ensure --BLOB has no contents.
3082 (list --type
(nth 1 --blob
))
3084 ;; Limiting recursion to greater elements, and --BLOB
3086 ((and (eq --category
'greater-elements
)
3087 (not (memq --type org-element-greater-elements
)))
3088 (funcall --check-blob --type types fun --blob info
))
3089 ;; Limiting recursion to elements, and --BLOB only
3090 ;; contains objects.
3091 ((and (eq --category
'elements
) (eq --type
'paragraph
)))
3092 ;; No limitation on recursion, but --BLOB hasn't
3093 ;; got a recursive type.
3094 ((and (eq --category
'objects
)
3095 (not (or (eq --type
'paragraph
)
3096 (memq --type org-element-greater-elements
)
3097 (memq --type org-element-recursive-objects
))))
3098 (funcall --check-blob --type types fun --blob info
))
3099 ;; Recursion is possible and allowed: Maybe apply
3100 ;; FUN to --BLOB, then move into it.
3101 (t (funcall --check-blob --type types fun --blob info
)
3102 (funcall --walk-tree --blob info
)))))
3103 (org-element-contents --data
))))))
3105 (funcall --walk-tree data info
)
3106 ;; Return value in a proper order.
3109 ;; The following functions are internal parts of the parser.
3111 ;; The first one, `org-element-parse-elements' acts at the element's
3112 ;; level. As point is always at the beginning of an element during
3113 ;; parsing, it doesn't have to rely on `org-element-at-point'.
3114 ;; Instead, it calls a more restrictive, though way quicker,
3115 ;; alternative: `org-element-current-element'. That function
3116 ;; internally uses `org-element--element-block-re' for quick access to
3119 ;; The second one, `org-element-parse-objects' applies on all objects
3120 ;; of a paragraph or a secondary string. It uses
3121 ;; `org-element-get-candidates' to optimize the search of the next
3122 ;; object in the buffer.
3124 ;; More precisely, that function looks for every allowed object type
3125 ;; first. Then, it discards failed searches, keeps further matches,
3126 ;; and searches again types matched behind point, for subsequent
3127 ;; calls. Thus, searching for a given type fails only once, and every
3128 ;; object is searched only once at top level (but sometimes more for
3131 (defun org-element-parse-elements
3132 (beg end special structure granularity visible-only acc
)
3133 "Parse elements between BEG and END positions.
3135 SPECIAL prioritize some elements over the others. It can set to
3136 `quote-section', `section' or `item', which will focus search,
3137 respectively, on quote sections, sections and items. Moreover,
3138 when value is `item', STRUCTURE will be used as the current list
3141 GRANULARITY determines the depth of the recursion. It can be set
3142 to the following symbols:
3144 `headline' Only parse headlines.
3145 `greater-element' Don't recurse into greater elements. Thus,
3146 elements parsed are the top-level ones.
3147 `element' Parse everything but objects and plain text.
3148 `object' or nil Parse the complete buffer.
3150 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3153 Elements are accumulated into ACC."
3156 (narrow-to-region beg end
)
3158 ;; When parsing only headlines, skip any text before first one.
3159 (when (and (eq granularity
'headline
) (not (org-at-heading-p)))
3160 (org-with-limited-levels (outline-next-heading)))
3164 ;; 1. Item mode is active: point must be at an item. Parse it
3165 ;; directly, skipping `org-element-current-element'.
3166 (if (eq special
'item
)
3167 (let ((element (org-element-item-parser structure
)))
3168 (goto-char (org-element-property :end element
))
3169 (org-element-parse-elements
3170 (org-element-property :contents-begin element
)
3171 (org-element-property :contents-end element
)
3172 nil structure granularity visible-only
(reverse element
)))
3173 ;; 2. When ITEM is nil, find current element's type and parse
3174 ;; it accordingly to its category.
3175 (let ((element (org-element-current-element special structure
)))
3176 (goto-char (org-element-property :end element
))
3178 ;; Case 1. ELEMENT is a paragraph. Parse objects inside,
3179 ;; if GRANULARITY allows it.
3180 ((and (eq (org-element-type element
) 'paragraph
)
3181 (or (not granularity
) (eq granularity
'object
)))
3182 (org-element-parse-objects
3183 (org-element-property :contents-begin element
)
3184 (org-element-property :contents-end element
)
3185 (reverse element
) nil
))
3186 ;; Case 2. ELEMENT is recursive: parse it between
3187 ;; `contents-begin' and `contents-end'. Make sure
3188 ;; GRANULARITY allows the recursion, or ELEMENT is an
3189 ;; headline, in which case going inside is mandatory, in
3190 ;; order to get sub-level headings. If VISIBLE-ONLY is
3191 ;; true and element is hidden, do not recurse into it.
3192 ((and (memq (org-element-type element
) org-element-greater-elements
)
3193 (or (not granularity
)
3194 (memq granularity
'(element object
))
3195 (eq (org-element-type element
) 'headline
))
3196 (not (and visible-only
3197 (org-element-property :hiddenp element
))))
3198 (org-element-parse-elements
3199 (org-element-property :contents-begin element
)
3200 (org-element-property :contents-end element
)
3201 ;; At a plain list, switch to item mode. At an
3202 ;; headline, switch to section mode. Any other
3203 ;; element turns off special modes.
3204 (case (org-element-type element
)
3206 (headline (if (org-element-property :quotedp element
)
3209 (org-element-property :structure element
)
3210 granularity visible-only
(reverse element
)))
3211 ;; Case 3. Else, just accumulate ELEMENT.
3217 (defconst org-element--element-block-re
3218 (format "[ \t]*#\\+begin_\\(%s\\)\\(?: \\|$\\)"
3221 (mapcar 'car org-element-non-recursive-block-alist
) "\\|"))
3222 "Regexp matching the beginning of a non-recursive block type.
3223 Used internally by `org-element-current-element'. Do not modify
3224 it directly, set `org-element-recursive-block-alist' instead.")
3226 (defun org-element-current-element (&optional special structure
)
3227 "Parse the element at point.
3229 Return value is a list \(TYPE PROPS\) where TYPE is the type of
3230 the element and PROPS a plist of properties associated to the
3233 Possible types are defined in `org-element-all-elements'.
3235 Optional argument SPECIAL, when non-nil, can be either `item',
3236 `section' or `quote-section'. `item' allows to parse item wise
3237 instead of plain-list wise, using STRUCTURE as the current list
3238 structure. `section' (resp. `quote-section') will try to parse
3239 a section (resp. a quote section) before anything else.
3241 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3244 Unlike to `org-element-at-point', this function assumes point is
3245 always at the beginning of the element it has to parse. As such,
3246 it is quicker than its counterpart and always accurate, albeit
3250 ;; If point is at an affiliated keyword, try moving to the
3251 ;; beginning of the associated element. If none is found, the
3252 ;; keyword is orphaned and will be treated as plain text.
3253 (when (looking-at org-element--affiliated-re
)
3254 (let ((opoint (point)))
3255 (while (looking-at org-element--affiliated-re
) (forward-line))
3256 (when (looking-at "[ \t]*$") (goto-char opoint
))))
3257 (let ((case-fold-search t
))
3260 ((org-with-limited-levels (org-at-heading-p))
3261 (org-element-headline-parser))
3263 ((eq special
'quote-section
) (org-element-quote-section-parser))
3265 ((eq special
'section
) (org-element-section-parser))
3266 ;; Non-recursive block.
3267 ((when (looking-at org-element--element-block-re
)
3268 (let ((type (downcase (match-string 1))))
3271 (format "[ \t]*#\\+end_%s\\(?: \\|$\\)" type
) nil t
))
3272 ;; Build appropriate parser.
3275 (format "org-element-%s-parser"
3277 org-element-non-recursive-block-alist
)))))
3278 (org-element-paragraph-parser)))))
3280 ((org-at-heading-p) (org-element-inlinetask-parser))
3281 ;; LaTeX Environment or paragraph if incomplete.
3282 ((looking-at "^[ \t]*\\\\begin{")
3284 (re-search-forward "^[ \t]*\\\\end{[^}]*}[ \t]*" nil t
))
3285 (org-element-latex-environment-parser)
3286 (org-element-paragraph-parser)))
3288 ((looking-at org-property-start-re
)
3289 (if (save-excursion (re-search-forward org-property-end-re nil t
))
3290 (org-element-property-drawer-parser)
3291 (org-element-paragraph-parser)))
3292 ;; Recursive block, or paragraph if incomplete.
3293 ((looking-at "[ \t]*#\\+begin_\\([-A-Za-z0-9]+\\)\\(?: \\|$\\)")
3294 (let ((type (downcase (match-string 1))))
3296 ((not (save-excursion
3298 (format "[ \t]*#\\+end_%s\\(?: \\|$\\)" type
) nil t
)))
3299 (org-element-paragraph-parser))
3300 ((string= type
"center") (org-element-center-block-parser))
3301 ((string= type
"quote") (org-element-quote-block-parser))
3302 (t (org-element-special-block-parser)))))
3304 ((looking-at org-drawer-regexp
)
3305 (if (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" nil t
))
3306 (org-element-drawer-parser)
3307 (org-element-paragraph-parser)))
3308 ((looking-at "[ \t]*:\\( \\|$\\)") (org-element-fixed-width-parser))
3310 ((looking-at org-babel-block-lob-one-liner-regexp
)
3311 (org-element-babel-call-parser))
3312 ;; Keyword, or paragraph if at an affiliated keyword.
3313 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
3314 (let ((key (downcase (match-string 1))))
3315 (if (or (string= key
"tblfm")
3316 (member key org-element-affiliated-keywords
))
3317 (org-element-paragraph-parser)
3318 (org-element-keyword-parser))))
3319 ;; Footnote definition.
3320 ((looking-at org-footnote-definition-re
)
3321 (org-element-footnote-definition-parser))
3322 ;; Dynamic block or paragraph if incomplete.
3323 ((looking-at "[ \t]*#\\+begin:\\(?: \\|$\\)")
3325 (re-search-forward "^[ \t]*#\\+end:\\(?: \\|$\\)" nil t
))
3326 (org-element-dynamic-block-parser)
3327 (org-element-paragraph-parser)))
3329 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)")
3330 (org-element-comment-parser))
3332 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3333 (org-element-horizontal-rule-parser))
3335 ((org-at-table-p t
) (org-element-table-parser))
3337 ((looking-at (org-item-re))
3338 (if (eq special
'item
)
3339 (org-element-item-parser (or structure
(org-list-struct)))
3340 (org-element-plain-list-parser (or structure
(org-list-struct)))))
3341 ;; Default element: Paragraph.
3342 (t (org-element-paragraph-parser))))))
3344 (defun org-element-parse-objects (beg end acc restriction
)
3345 "Parse objects between BEG and END and return recursive structure.
3347 Objects are accumulated in ACC.
3349 RESTRICTION, when non-nil, is a list of object types which are
3350 allowed in the current object."
3351 (let ((get-next-object
3354 ;; Return the parsing function associated to the nearest
3355 ;; object among list of candidates CAND.
3356 (let ((pos (apply #'min
(mapcar #'cdr cand
))))
3361 (format "org-element-%s-parser" (car (rassq pos cand
))))))))))
3362 next-object candidates
)
3365 (while (setq candidates
(org-element-get-next-object-candidates
3366 end restriction candidates
))
3367 (setq next-object
(funcall get-next-object candidates
))
3368 ;; 1. Text before any object. Untabify it.
3369 (let ((obj-beg (org-element-property :begin next-object
)))
3370 (unless (= (point) obj-beg
)
3371 (push (replace-regexp-in-string
3372 "\t" (make-string tab-width ?
)
3373 (buffer-substring-no-properties (point) obj-beg
))
3376 (let ((obj-end (org-element-property :end next-object
))
3377 (cont-beg (org-element-property :contents-begin next-object
)))
3378 (push (if (and (memq (car next-object
) org-element-recursive-objects
)
3380 ;; ... recursive. The CONT-BEG check is for
3381 ;; links, as some of them might not be recursive
3382 ;; (i.e. plain links).
3386 (org-element-property :contents-end next-object
))
3387 (org-element-parse-objects
3388 (point-min) (point-max) (reverse next-object
)
3389 ;; Restrict allowed objects. This is the
3390 ;; intersection of current restriction and next
3391 ;; object's restriction.
3393 (cdr (assq (car next-object
)
3394 org-element-object-restrictions
))))
3395 (if (not restriction
) new-restr
3397 (lambda (e) (and (memq e restriction
) e
))
3399 ;; ... not recursive.
3402 (goto-char obj-end
)))
3403 ;; 3. Text after last object. Untabify it.
3404 (unless (= (point) end
)
3405 (push (replace-regexp-in-string
3406 "\t" (make-string tab-width ?
)
3407 (buffer-substring-no-properties (point) end
))
3412 (defun org-element-get-next-object-candidates (limit restriction objects
)
3413 "Return an alist of candidates for the next object.
3415 LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
3416 the possible object types.
3418 Return value is an alist whose car is position and cdr the object
3419 type, as a string. There is an association for the closest
3420 object of each type within RESTRICTION when non-nil, or for every
3423 OBJECTS is the previous candidates alist."
3424 (let ((restriction (or restriction org-element-all-successors
))
3425 next-candidates types-to-search
)
3426 ;; If no previous result, search every object type in RESTRICTION.
3427 ;; Otherwise, keep potential candidates (old objects located after
3428 ;; point) and ask to search again those which had matched before.
3429 (if (not objects
) (setq types-to-search restriction
)
3431 (if (< (cdr obj
) (point)) (push (car obj
) types-to-search
)
3432 (push obj next-candidates
)))
3434 ;; Call the appropriate "get-next" function for each type to
3435 ;; search and accumulate matches.
3438 (let* ((successor-fun
3440 (format "org-element-%s-successor"
3441 (or (cdr (assq type org-element-object-successor-alist
))
3443 (obj (funcall successor-fun limit
)))
3444 (and obj
(push obj next-candidates
))))
3451 ;;; Towards A Bijective Process
3453 ;; The parse tree obtained with `org-element-parse-buffer' is really
3454 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3455 ;; interpreted and expanded into a string with canonical Org
3456 ;; syntax. Hence `org-element-interpret-data'.
3458 ;; Data parsed from secondary strings, whose shape is slightly
3459 ;; different than the standard parse tree, is expanded with the
3460 ;; equivalent function `org-element-interpret-secondary'.
3462 ;; Both functions rely internally on
3463 ;; `org-element-interpret--affiliated-keywords'.
3465 (defun org-element-interpret-data (data &optional genealogy previous
)
3466 "Interpret a parse tree representing Org data.
3468 DATA is the parse tree to interpret.
3470 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3472 GENEALOGY is the list of its parents types.
3473 PREVIOUS is the type of the element or object at the same level
3476 Return Org syntax as a string."
3479 ;; BLOB can be an element, an object, a string, or nil.
3482 ((equal blob
"") nil
)
3483 ((stringp blob
) blob
)
3485 (let* ((type (org-element-type blob
))
3487 (if (eq type
'org-data
) 'identity
3488 (intern (format "org-element-%s-interpreter" type
))))
3491 ;; Full Org document.
3492 ((eq type
'org-data
)
3493 (org-element-interpret-data blob genealogy previous
))
3494 ;; Recursive objects.
3495 ((memq type org-element-recursive-objects
)
3496 (org-element-interpret-data
3497 blob
(cons type genealogy
) nil
))
3498 ;; Recursive elements.
3499 ((memq type org-element-greater-elements
)
3500 (org-element-normalize-string
3501 (org-element-interpret-data
3502 blob
(cons type genealogy
) nil
)))
3504 ((eq type
'paragraph
)
3506 (org-element-normalize-contents
3508 ;; When normalizing contents of an item,
3509 ;; ignore first line's indentation.
3511 (memq (car genealogy
)
3512 '(footnote-definiton item
))))))
3513 (org-element-interpret-data
3514 paragraph
(cons type genealogy
) nil
)))))
3515 (results (funcall interpreter blob contents
)))
3517 (setq previous type
)
3518 ;; Build white spaces.
3520 ((eq type
'org-data
) results
)
3521 ((memq type org-element-all-elements
)
3523 (org-element-interpret--affiliated-keywords blob
)
3524 (org-element-normalize-string results
)
3525 (make-string (org-element-property :post-blank blob
) 10)))
3528 (make-string (org-element-property :post-blank blob
) 32))))))))
3529 (org-element-contents data
) ""))
3531 (defun org-element-interpret-secondary (secondary)
3532 "Interpret SECONDARY string as Org syntax.
3534 SECONDARY-STRING is a nested list as returned by
3535 `org-element-parse-secondary-string'.
3537 Return interpreted string."
3538 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3539 (let ((s (if (listp secondary
) secondary
(list secondary
))))
3540 (org-element-interpret-data `(org-data nil
,@s
) nil nil
)))
3542 ;; Both functions internally use `org-element--affiliated-keywords'.
3544 (defun org-element-interpret--affiliated-keywords (element)
3545 "Return ELEMENT's affiliated keywords as Org syntax.
3546 If there is no affiliated keyword, return the empty string."
3547 (let ((keyword-to-org
3551 (when (member key org-element-dual-keywords
)
3552 (setq dual
(cdr value
) value
(car value
)))
3553 (concat "#+" key
(and dual
(format "[%s]" dual
)) ": "
3554 (if (member key org-element-parsed-keywords
)
3555 (org-element-interpret-secondary value
)
3560 (let ((value (org-element-property (intern (concat ":" key
)) element
)))
3562 (if (member key org-element-multiple-keywords
)
3563 (mapconcat (lambda (line)
3564 (funcall keyword-to-org key line
))
3566 (funcall keyword-to-org key value
)))))
3567 ;; Remove translated keywords.
3571 (and (not (assoc key org-element-keyword-translation-alist
)) key
))
3572 org-element-affiliated-keywords
))
3575 ;; Because interpretation of the parse tree must return the same
3576 ;; number of blank lines between elements and the same number of white
3577 ;; space after objects, some special care must be given to white
3580 ;; The first function, `org-element-normalize-string', ensures any
3581 ;; string different from the empty string will end with a single
3582 ;; newline character.
3584 ;; The second function, `org-element-normalize-contents', removes
3585 ;; global indentation from the contents of the current element.
3587 (defun org-element-normalize-string (s)
3588 "Ensure string S ends with a single newline character.
3590 If S isn't a string return it unchanged. If S is the empty
3591 string, return it. Otherwise, return a new string with a single
3592 newline character at its end."
3594 ((not (stringp s
)) s
)
3596 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s
)
3597 (replace-match "\n" nil nil s
)))))
3599 (defun org-element-normalize-contents (element &optional ignore-first
)
3600 "Normalize plain text in ELEMENT's contents.
3602 ELEMENT must only contain plain text and objects.
3604 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3605 indentation to compute maximal common indentation.
3607 Return the normalized element that is element with global
3608 indentation removed from its contents. The function assumes that
3609 indentation is not done with TAB characters."
3613 ;; Return list of indentations within BLOB. This is done by
3614 ;; walking recursively BLOB and updating IND-LIST along the
3615 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3616 ;; been seen yet. It is required as this string is the only
3617 ;; one whose indentation doesn't happen after a newline
3619 (lambda (blob first-flag
)
3622 (when (and first-flag
(stringp object
))
3623 (setq first-flag nil
)
3624 (string-match "\\`\\( *\\)" object
)
3625 (let ((len (length (match-string 1 object
))))
3626 ;; An indentation of zero means no string will be
3627 ;; modified. Quit the process.
3628 (if (zerop len
) (throw 'zero
(setq ind-list nil
))
3629 (push len ind-list
))))
3633 (while (string-match "\n\\( *\\)" object start
)
3634 (setq start
(match-end 0))
3635 (push (length (match-string 1 object
)) ind-list
))))
3636 ((memq (org-element-type object
) org-element-recursive-objects
)
3637 (funcall collect-inds object first-flag
))))
3638 (org-element-contents blob
))))))
3639 ;; Collect indentation list in ELEMENT. Possibly remove first
3640 ;; value if IGNORE-FIRST is non-nil.
3641 (catch 'zero
(funcall collect-inds element
(not ignore-first
)))
3642 (if (not ind-list
) element
3643 ;; Build ELEMENT back, replacing each string with the same
3644 ;; string minus common indentation.
3647 (lambda (blob mci first-flag
)
3648 ;; Return BLOB with all its strings indentation
3649 ;; shortened from MCI white spaces. FIRST-FLAG is
3650 ;; non-nil when the first string hasn't been seen
3653 (list (org-element-type blob
) (nth 1 blob
))
3656 (when (and first-flag
(stringp object
))
3657 (setq first-flag nil
)
3659 (replace-regexp-in-string
3660 (format "\\` \\{%d\\}" mci
) "" object
)))
3663 (replace-regexp-in-string
3664 (format "\n \\{%d\\}" mci
) "\n" object
))
3665 ((memq (org-element-type object
) org-element-recursive-objects
)
3666 (funcall build object mci first-flag
))
3668 (org-element-contents blob
)))))))
3669 (funcall build element
(apply 'min ind-list
) (not ignore-first
))))))
3675 ;; Once the structure of an Org file is well understood, it's easy to
3676 ;; implement some replacements for `forward-paragraph'
3677 ;; `backward-paragraph', namely `org-element-forward' and
3678 ;; `org-element-backward'.
3680 ;; Also, `org-transpose-elements' mimics the behaviour of
3681 ;; `transpose-words', at the element's level, whereas
3682 ;; `org-element-drag-forward', `org-element-drag-backward', and
3683 ;; `org-element-up' generalize, respectively, functions
3684 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3686 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3687 ;; smartly remove global indentation from buffer, making it possible
3688 ;; to use Org indent mode on a file created with hard indentation.
3690 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3691 ;; internally by some of the previously cited tools.
3693 (defsubst org-element-nested-p
(elem-A elem-B
)
3694 "Non-nil when elements ELEM-A and ELEM-B are nested."
3695 (let ((beg-A (org-element-property :begin elem-A
))
3696 (beg-B (org-element-property :begin elem-B
))
3697 (end-A (org-element-property :end elem-A
))
3698 (end-B (org-element-property :end elem-B
)))
3699 (or (and (>= beg-A beg-B
) (<= end-A end-B
))
3700 (and (>= beg-B beg-A
) (<= end-B end-A
)))))
3702 (defun org-element-swap-A-B (elem-A elem-B
)
3703 "Swap elements ELEM-A and ELEM-B.
3705 Leave point at the end of ELEM-A.
3707 Assume ELEM-A is before ELEM-B and that they are not nested."
3708 (goto-char (org-element-property :begin elem-A
))
3709 (let* ((beg-B (org-element-property :begin elem-B
))
3710 (end-B-no-blank (save-excursion
3711 (goto-char (org-element-property :end elem-B
))
3712 (skip-chars-backward " \r\t\n")
3715 (beg-A (org-element-property :begin elem-A
))
3716 (end-A-no-blank (save-excursion
3717 (goto-char (org-element-property :end elem-A
))
3718 (skip-chars-backward " \r\t\n")
3721 (body-A (buffer-substring beg-A end-A-no-blank
))
3722 (body-B (buffer-substring beg-B end-B-no-blank
))
3723 (between-A-B (buffer-substring end-A-no-blank beg-B
)))
3724 (delete-region beg-A end-B-no-blank
)
3725 (insert body-B between-A-B body-A
)
3726 (goto-char (org-element-property :end elem-B
))))
3728 (defun org-element-backward ()
3729 "Move backward by one element."
3731 (let* ((opoint (point))
3732 (element (org-element-at-point))
3733 (start-el-beg (org-element-property :begin element
)))
3734 ;; At an headline. The previous element is the previous sibling,
3735 ;; or the parent if any.
3737 ;; Already at the beginning of the current element: move to the
3738 ;; beginning of the previous one.
3739 ((= opoint start-el-beg
)
3741 (skip-chars-backward " \r\t\n")
3742 (let* ((prev-element (org-element-at-point))
3743 (itemp (org-in-item-p))
3745 (save-excursion (goto-char itemp
)
3746 (org-list-struct)))))
3747 ;; When moving into a new list, go directly at the
3748 ;; beginning of the top list structure.
3749 (if (and itemp
(<= (org-list-get-bottom-point struct
) opoint
))
3751 (goto-char (org-list-get-top-point struct
))
3752 (goto-char (org-element-property
3753 :begin
(org-element-at-point))))
3754 (goto-char (org-element-property :begin prev-element
))))
3755 (while (org-truely-invisible-p) (org-element-up)))
3756 ;; Else, move at the element beginning. One exception: if point
3757 ;; was in the blank lines after the end of a list, move directly
3761 (if (and (setq itemp
(org-in-item-p))
3762 (<= (org-list-get-bottom-point
3763 (save-excursion (goto-char itemp
)
3764 (setq struct
(org-list-struct))))
3767 (goto-char (org-list-get-top-point struct
))
3768 (goto-char (org-element-property :begin
(org-element-at-point))))
3769 (goto-char start-el-beg
)))))))
3771 (defun org-element-drag-backward ()
3772 "Drag backward element at point."
3774 (let* ((pos (point))
3775 (elem (org-element-at-point)))
3776 (when (= (progn (goto-char (point-min))
3777 (org-skip-whitespace)
3779 (org-element-property :end elem
))
3780 (error "Cannot drag element backward"))
3781 (goto-char (org-element-property :begin elem
))
3782 (org-element-backward)
3783 (let ((prev-elem (org-element-at-point)))
3784 (when (or (org-element-nested-p elem prev-elem
)
3785 (and (eq (car elem
) 'headline
)
3786 (not (eq (car prev-elem
) 'headline
))))
3788 (error "Cannot drag element backward"))
3789 ;; Compute new position of point: it's shifted by PREV-ELEM
3791 (let ((size-prev (- (org-element-property :end prev-elem
)
3792 (org-element-property :begin prev-elem
))))
3793 (org-element-swap-A-B prev-elem elem
)
3794 (goto-char (- pos size-prev
))))))
3796 (defun org-element-drag-forward ()
3797 "Move forward element at point."
3799 (let* ((pos (point))
3800 (elem (org-element-at-point)))
3801 (when (= (point-max) (org-element-property :end elem
))
3802 (error "Cannot drag element forward"))
3803 (goto-char (org-element-property :end elem
))
3804 (let ((next-elem (org-element-at-point)))
3805 (when (or (org-element-nested-p elem next-elem
)
3806 (and (eq (car next-elem
) 'headline
)
3807 (not (eq (car elem
) 'headline
))))
3809 (error "Cannot drag element forward"))
3810 ;; Compute new position of point: it's shifted by NEXT-ELEM
3811 ;; body's length (without final blanks) and by the length of
3812 ;; blanks between ELEM and NEXT-ELEM.
3813 (let ((size-next (- (save-excursion
3814 (goto-char (org-element-property :end next-elem
))
3815 (skip-chars-backward " \r\t\n")
3818 (org-element-property :begin next-elem
)))
3819 (size-blank (- (org-element-property :end elem
)
3821 (goto-char (org-element-property :end elem
))
3822 (skip-chars-backward " \r\t\n")
3825 (org-element-swap-A-B elem next-elem
)
3826 (goto-char (+ pos size-next size-blank
))))))
3828 (defun org-element-forward ()
3829 "Move forward by one element."
3832 (cond ((eobp) (error "Cannot move further down"))
3833 ((looking-at "[ \t]*$")
3834 (org-skip-whitespace)
3835 (goto-char (if (eobp) (point) (point-at-bol))))
3837 (let ((element (org-element-at-point t
))
3840 ;; At an item: Either move to the next element inside, or
3841 ;; to its end if it's hidden.
3842 ((eq (org-element-type element
) 'item
)
3843 (if (org-element-property :hiddenp element
)
3844 (goto-char (org-element-property :end element
))
3846 (re-search-forward org-element-paragraph-separate nil t
)
3847 (org-skip-whitespace)
3848 (beginning-of-line)))
3849 ;; At a recursive element: Either move inside, or if it's
3850 ;; hidden, move to its end.
3851 ((memq (org-element-type element
) org-element-greater-elements
)
3852 (let ((cbeg (org-element-property :contents-begin element
)))
3854 (if (or (org-element-property :hiddenp element
)
3856 (org-element-property :end element
)
3858 ;; Else: move to the current element's end.
3859 (t (goto-char (org-element-property :end element
))))))))
3861 (defun org-element-mark-element ()
3862 "Put point at beginning of this element, mark at end.
3864 Interactively, if this command is repeated or (in Transient Mark
3865 mode) if the mark is active, it marks the next element after the
3866 ones already marked."
3868 (let (deactivate-mark)
3869 (if (or (and (eq last-command this-command
) (mark t
))
3870 (and transient-mark-mode mark-active
))
3874 (goto-char (org-element-property :end
(org-element-at-point)))))
3875 (let ((element (org-element-at-point)))
3877 (push-mark (org-element-property :end element
) t t
)
3878 (goto-char (org-element-property :begin element
))))))
3880 (defun org-narrow-to-element ()
3881 "Narrow buffer to current element."
3883 (let ((elem (org-element-at-point)))
3885 ((eq (car elem
) 'headline
)
3887 (org-element-property :begin elem
)
3888 (org-element-property :end elem
)))
3889 ((memq (car elem
) org-element-greater-elements
)
3891 (org-element-property :contents-begin elem
)
3892 (org-element-property :contents-end elem
)))
3895 (org-element-property :begin elem
)
3896 (org-element-property :end elem
))))))
3898 (defun org-transpose-elements ()
3899 "Transpose current and previous elements, keeping blank lines between.
3900 Point is moved after both elements."
3902 (org-skip-whitespace)
3904 (cur (org-element-at-point)))
3905 (when (= (save-excursion (goto-char (point-min))
3906 (org-skip-whitespace)
3908 (org-element-property :begin cur
))
3909 (error "No previous element"))
3910 (goto-char (org-element-property :begin cur
))
3912 (let ((prev (org-element-at-point)))
3913 (when (org-element-nested-p cur prev
)
3915 (error "Cannot transpose nested elements"))
3916 (org-element-swap-A-B prev cur
))))
3918 (defun org-element-unindent-buffer ()
3919 "Un-indent the visible part of the buffer.
3920 Relative indentation \(between items, inside blocks, etc.\) isn't
3923 (unless (eq major-mode
'org-mode
)
3924 (error "Cannot un-indent a buffer not in Org mode"))
3925 (let* ((parse-tree (org-element-parse-buffer 'greater-element
))
3926 unindent-tree
; For byte-compiler.
3930 (mapc (lambda (element)
3931 (if (eq (org-element-type element
) 'headline
)
3932 (funcall unindent-tree
3933 (org-element-contents element
))
3937 (org-element-property :begin element
)
3938 (org-element-property :end element
))
3939 (org-do-remove-indentation)))))
3940 (reverse contents
))))))
3941 (funcall unindent-tree
(org-element-contents parse-tree
))))
3943 (defun org-element-up ()
3944 "Move to upper element.
3945 Return position at the beginning of the upper element."
3947 (let ((opoint (point)) elem
)
3949 ((bobp) (error "No surrounding element"))
3950 ((org-with-limited-levels (org-at-heading-p))
3951 (or (org-up-heading-safe) (error "No surronding element")))
3952 ((and (org-at-item-p)
3953 (setq elem
(org-element-at-point))
3954 (let* ((top-list-p (zerop (org-element-property :level elem
))))
3956 ;; If parent is bound to be in the same list as the
3957 ;; original point, move to that parent.
3958 (let ((struct (org-element-property :structure elem
)))
3960 (org-list-get-parent
3961 (point-at-bol) struct
(org-list-parents-alist struct
))))))))
3963 (let* ((elem (or elem
(org-element-at-point)))
3964 (end (save-excursion
3965 (goto-char (org-element-property :end elem
))
3966 (skip-chars-backward " \r\t\n")
3970 (goto-char (org-element-property :begin elem
))
3972 (while (and (< (org-element-property
3973 :end
(setq prev-elem
(org-element-at-point)))
3976 (goto-char (org-element-property :begin prev-elem
))
3978 (if (and (bobp) (< (org-element-property :end prev-elem
) end
))
3979 (progn (goto-char opoint
)
3980 (error "No surrounding element"))
3981 (goto-char (org-element-property :begin prev-elem
))))))))
3984 (provide 'org-element
)
3985 ;;; org-element.el ends here