org-element: Every keyword starting with ATTR_ is an affiliated keyword
[org-mode.git] / contrib / lisp / org-element.el
blob0d0f4511d575afd1cd917e8ec72ea949c18ed138
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/>.
23 ;;; Commentary:
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; Elements are related to the structure of the document. Indeed, all
29 ;; elements are a cover for the document: each position within belongs
30 ;; to at least one element.
32 ;; An element always starts and ends at the beginning of a line. With
33 ;; a few exceptions (namely `babel-call', `clock', `headline', `item',
34 ;; `keyword', `planning', `property-drawer' and `section' types), it
35 ;; can also accept a fixed set of keywords as attributes. Those are
36 ;; called "affiliated keywords" to distinguish them from other
37 ;; keywords, which are full-fledged elements. Almost all affiliated
38 ;; keywords are referenced in `org-element-affiliated-keywords'; the
39 ;; others are export attributes and start with "ATTR_" prefix.
41 ;; Element containing other elements (and only elements) are called
42 ;; greater elements. Concerned types are: `center-block', `drawer',
43 ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
44 ;; `item', `plain-list', `quote-block', `section' and `special-block'.
46 ;; Other element types are: `babel-call', `clock', `comment',
47 ;; `comment-block', `example-block', `export-block', `fixed-width',
48 ;; `horizontal-rule', `keyword', `latex-environment', `paragraph',
49 ;; `planning', `property-drawer', `quote-section', `src-block',
50 ;; `table', `table-row' and `verse-block'. Among them, `paragraph'
51 ;; and `verse-block' types can contain Org objects and plain text.
53 ;; Objects are related to document's contents. Some of them are
54 ;; recursive. Associated types are of the following: `bold', `code',
55 ;; `entity', `export-snippet', `footnote-reference',
56 ;; `inline-babel-call', `inline-src-block', `italic',
57 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
58 ;; `statistics-cookie', `strike-through', `subscript', `superscript',
59 ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
61 ;; Some elements also have special properties whose value can hold
62 ;; objects themselves (i.e. an item tag or an headline name). Such
63 ;; values are called "secondary strings". Any object belongs to
64 ;; either an element or a secondary string.
66 ;; Notwithstanding affiliated keywords, each greater element, element
67 ;; and object has a fixed set of properties attached to it. Among
68 ;; them, four are shared by all types: `:begin' and `:end', which
69 ;; refer to the beginning and ending buffer positions of the
70 ;; considered element or object, `:post-blank', which holds the number
71 ;; of blank lines, or white spaces, at its end and `:parent' which
72 ;; refers to the element or object containing it. Greater elements
73 ;; and elements containing objects will also have `:contents-begin'
74 ;; and `:contents-end' properties to delimit contents.
76 ;; Lisp-wise, an element or an object can be represented as a list.
77 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
78 ;; TYPE is a symbol describing the Org element or object.
79 ;; PROPERTIES is the property list attached to it. See docstring of
80 ;; appropriate parsing function to get an exhaustive
81 ;; list.
82 ;; CONTENTS is a list of elements, objects or raw strings contained
83 ;; in the current element or object, when applicable.
85 ;; An Org buffer is a nested list of such elements and objects, whose
86 ;; type is `org-data' and properties is nil.
88 ;; The first part of this file implements a parser and an interpreter
89 ;; for each type of Org syntax.
91 ;; The next two parts introduce four accessors and a function
92 ;; retrieving the element starting at point (respectively
93 ;; `org-element-type', `org-element-property', `org-element-contents',
94 ;; `org-element-restriction' and `org-element-current-element').
96 ;; The following part creates a fully recursive buffer parser. It
97 ;; also provides a tool to map a function to elements or objects
98 ;; matching some criteria in the parse tree. Functions of interest
99 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
100 ;; extent, `org-element-parse-secondary-string'.
102 ;; The penultimate part is the cradle of an interpreter for the
103 ;; obtained parse tree: `org-element-interpret-data'.
105 ;; The library ends by furnishing a set of interactive tools for
106 ;; element's navigation and manipulation, mostly based on
107 ;; `org-element-at-point' function.
110 ;;; Code:
112 (eval-when-compile (require 'cl))
113 (require 'org)
114 (declare-function org-inlinetask-goto-end "org-inlinetask" ())
117 ;;; Greater elements
119 ;; For each greater element type, we define a parser and an
120 ;; interpreter.
122 ;; A parser returns the element or object as the list described above.
123 ;; Most of them accepts no argument. Though, exceptions exist. Hence
124 ;; every element containing a secondary string (see
125 ;; `org-element-secondary-value-alist') will accept an optional
126 ;; argument to toggle parsing of that secondary string. Moreover,
127 ;; `item' parser requires current list's structure as its first
128 ;; element.
130 ;; An interpreter accepts two arguments: the list representation of
131 ;; the element or object, and its contents. The latter may be nil,
132 ;; depending on the element or object considered. It returns the
133 ;; appropriate Org syntax, as a string.
135 ;; Parsing functions must follow the naming convention:
136 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
137 ;; defined in `org-element-greater-elements'.
139 ;; Similarly, interpreting functions must follow the naming
140 ;; convention: org-element-TYPE-interpreter.
142 ;; With the exception of `headline' and `item' types, greater elements
143 ;; cannot contain other greater elements of their own type.
145 ;; Beside implementing a parser and an interpreter, adding a new
146 ;; greater element requires to tweak `org-element-current-element'.
147 ;; Moreover, the newly defined type must be added to both
148 ;; `org-element-all-elements' and `org-element-greater-elements'.
151 ;;;; Center Block
153 (defun org-element-center-block-parser ()
154 "Parse a center block.
156 Return a list whose CAR is `center-block' and CDR is a plist
157 containing `:begin', `:end', `:hiddenp', `:contents-begin',
158 `:contents-end' and `:post-blank' keywords.
160 Assume point is at the beginning of the block."
161 (save-excursion
162 (let* ((case-fold-search t)
163 (keywords (org-element-collect-affiliated-keywords))
164 (begin (car keywords))
165 (contents-begin (progn (forward-line) (point)))
166 (hidden (org-truely-invisible-p))
167 (contents-end
168 (progn (re-search-forward "^[ \t]*#\\+END_CENTER" nil t)
169 (point-at-bol)))
170 (pos-before-blank (progn (forward-line) (point)))
171 (end (progn (org-skip-whitespace)
172 (if (eobp) (point) (point-at-bol)))))
173 `(center-block
174 (:begin ,begin
175 :end ,end
176 :hiddenp ,hidden
177 :contents-begin ,contents-begin
178 :contents-end ,contents-end
179 :post-blank ,(count-lines pos-before-blank end)
180 ,@(cadr keywords))))))
182 (defun org-element-center-block-interpreter (center-block contents)
183 "Interpret CENTER-BLOCK element as Org syntax.
184 CONTENTS is the contents of the element."
185 (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
188 ;;;; Drawer
190 (defun org-element-drawer-parser ()
191 "Parse a drawer.
193 Return a list whose CAR is `drawer' and CDR is a plist containing
194 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
195 `:contents-end' and `:post-blank' keywords.
197 Assume point is at beginning of drawer."
198 (save-excursion
199 (let* ((case-fold-search t)
200 (name (progn (looking-at org-drawer-regexp)
201 (org-match-string-no-properties 1)))
202 (keywords (org-element-collect-affiliated-keywords))
203 (begin (car keywords))
204 (contents-begin (progn (forward-line) (point)))
205 (hidden (org-truely-invisible-p))
206 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t)
207 (point-at-bol)))
208 (pos-before-blank (progn (forward-line) (point)))
209 (end (progn (org-skip-whitespace)
210 (if (eobp) (point) (point-at-bol)))))
211 `(drawer
212 (:begin ,begin
213 :end ,end
214 :drawer-name ,name
215 :hiddenp ,hidden
216 :contents-begin ,contents-begin
217 :contents-end ,contents-end
218 :post-blank ,(count-lines pos-before-blank end)
219 ,@(cadr keywords))))))
221 (defun org-element-drawer-interpreter (drawer contents)
222 "Interpret DRAWER element as Org syntax.
223 CONTENTS is the contents of the element."
224 (format ":%s:\n%s:END:"
225 (org-element-property :drawer-name drawer)
226 contents))
229 ;;;; Dynamic Block
231 (defun org-element-dynamic-block-parser ()
232 "Parse a dynamic block.
234 Return a list whose CAR is `dynamic-block' and CDR is a plist
235 containing `:block-name', `:begin', `:end', `:hiddenp',
236 `:contents-begin', `:contents-end', `:arguments' and
237 `:post-blank' keywords.
239 Assume point is at beginning of dynamic block."
240 (save-excursion
241 (let* ((case-fold-search t)
242 (name (progn (looking-at org-dblock-start-re)
243 (org-match-string-no-properties 1)))
244 (arguments (org-match-string-no-properties 3))
245 (keywords (org-element-collect-affiliated-keywords))
246 (begin (car keywords))
247 (contents-begin (progn (forward-line) (point)))
248 (hidden (org-truely-invisible-p))
249 (contents-end (progn (re-search-forward org-dblock-end-re nil t)
250 (point-at-bol)))
251 (pos-before-blank (progn (forward-line) (point)))
252 (end (progn (org-skip-whitespace)
253 (if (eobp) (point) (point-at-bol)))))
254 (list 'dynamic-block
255 `(:begin ,begin
256 :end ,end
257 :block-name ,name
258 :arguments ,arguments
259 :hiddenp ,hidden
260 :contents-begin ,contents-begin
261 :contents-end ,contents-end
262 :post-blank ,(count-lines pos-before-blank end)
263 ,@(cadr keywords))))))
265 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
266 "Interpret DYNAMIC-BLOCK element as Org syntax.
267 CONTENTS is the contents of the element."
268 (format "#+BEGIN: %s%s\n%s#+END:"
269 (org-element-property :block-name dynamic-block)
270 (let ((args (org-element-property :arguments dynamic-block)))
271 (and args (concat " " args)))
272 contents))
275 ;;;; Footnote Definition
277 (defun org-element-footnote-definition-parser ()
278 "Parse a footnote definition.
280 Return a list whose CAR is `footnote-definition' and CDR is
281 a plist containing `:label', `:begin' `:end', `:contents-begin',
282 `:contents-end' and `:post-blank' keywords.
284 Assume point is at the beginning of the footnote definition."
285 (save-excursion
286 (looking-at org-footnote-definition-re)
287 (let* ((label (org-match-string-no-properties 1))
288 (keywords (org-element-collect-affiliated-keywords))
289 (begin (car keywords))
290 (contents-begin (progn (search-forward "]")
291 (org-skip-whitespace)
292 (point)))
293 (contents-end (if (progn
294 (end-of-line)
295 (re-search-forward
296 (concat org-outline-regexp-bol "\\|"
297 org-footnote-definition-re "\\|"
298 "^[ \t]*$") nil 'move))
299 (match-beginning 0)
300 (point)))
301 (end (progn (org-skip-whitespace)
302 (if (eobp) (point) (point-at-bol)))))
303 `(footnote-definition
304 (:label ,label
305 :begin ,begin
306 :end ,end
307 :contents-begin ,contents-begin
308 :contents-end ,contents-end
309 :post-blank ,(count-lines contents-end end)
310 ,@(cadr keywords))))))
312 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
313 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
314 CONTENTS is the contents of the footnote-definition."
315 (concat (format "[%s]" (org-element-property :label footnote-definition))
317 contents))
320 ;;;; Headline
322 (defun org-element-headline-parser (&optional raw-secondary-p)
323 "Parse an headline.
325 Return a list whose CAR is `headline' and CDR is a plist
326 containing `:raw-value', `:title', `:begin', `:end',
327 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
328 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
329 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
330 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
331 keywords.
333 The plist also contains any property set in the property drawer,
334 with its name in lowercase, the underscores replaced with hyphens
335 and colons at the beginning (i.e. `:custom-id').
337 When RAW-SECONDARY-P is non-nil, headline's title will not be
338 parsed as a secondary string, but as a plain string instead.
340 Assume point is at beginning of the headline."
341 (save-excursion
342 (let* ((components (org-heading-components))
343 (level (nth 1 components))
344 (todo (nth 2 components))
345 (todo-type
346 (and todo (if (member todo org-done-keywords) 'done 'todo)))
347 (tags (let ((raw-tags (nth 5 components)))
348 (and raw-tags (org-split-string raw-tags ":"))))
349 (raw-value (nth 4 components))
350 (quotedp
351 (let ((case-fold-search nil))
352 (string-match (format "^%s +" org-quote-string) raw-value)))
353 (commentedp
354 (let ((case-fold-search nil))
355 (string-match (format "^%s +" org-comment-string) raw-value)))
356 (archivedp (member org-archive-tag tags))
357 (footnote-section-p (and org-footnote-section
358 (string= org-footnote-section raw-value)))
359 (standard-props (let (plist)
360 (mapc
361 (lambda (p)
362 (let ((p-name (downcase (car p))))
363 (while (string-match "_" p-name)
364 (setq p-name
365 (replace-match "-" nil nil p-name)))
366 (setq p-name (intern (concat ":" p-name)))
367 (setq plist
368 (plist-put plist p-name (cdr p)))))
369 (org-entry-properties nil 'standard))
370 plist))
371 (time-props (org-entry-properties nil 'special "CLOCK"))
372 (scheduled (cdr (assoc "SCHEDULED" time-props)))
373 (deadline (cdr (assoc "DEADLINE" time-props)))
374 (clock (cdr (assoc "CLOCK" time-props)))
375 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
376 (begin (point))
377 (pos-after-head (save-excursion (forward-line) (point)))
378 (contents-begin (save-excursion (forward-line)
379 (org-skip-whitespace)
380 (if (eobp) (point) (point-at-bol))))
381 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
382 (end (progn (goto-char (org-end-of-subtree t t))))
383 (contents-end (progn (skip-chars-backward " \r\t\n")
384 (forward-line)
385 (point)))
386 title)
387 ;; Clean RAW-VALUE from any quote or comment string.
388 (when (or quotedp commentedp)
389 (setq raw-value
390 (replace-regexp-in-string
391 (concat "\\(" org-quote-string "\\|" org-comment-string "\\) +")
393 raw-value)))
394 ;; Clean TAGS from archive tag, if any.
395 (when archivedp (setq tags (delete org-archive-tag tags)))
396 ;; Then get TITLE.
397 (setq title
398 (if raw-secondary-p raw-value
399 (org-element-parse-secondary-string
400 raw-value (org-element-restriction 'headline))))
401 `(headline
402 (:raw-value ,raw-value
403 :title ,title
404 :begin ,begin
405 :end ,end
406 :pre-blank ,(count-lines pos-after-head contents-begin)
407 :hiddenp ,hidden
408 :contents-begin ,contents-begin
409 :contents-end ,contents-end
410 :level ,level
411 :priority ,(nth 3 components)
412 :tags ,tags
413 :todo-keyword ,todo
414 :todo-type ,todo-type
415 :scheduled ,scheduled
416 :deadline ,deadline
417 :timestamp ,timestamp
418 :clock ,clock
419 :post-blank ,(count-lines contents-end end)
420 :footnote-section-p ,footnote-section-p
421 :archivedp ,archivedp
422 :commentedp ,commentedp
423 :quotedp ,quotedp
424 ,@standard-props)))))
426 (defun org-element-headline-interpreter (headline contents)
427 "Interpret HEADLINE element as Org syntax.
428 CONTENTS is the contents of the element."
429 (let* ((level (org-element-property :level headline))
430 (todo (org-element-property :todo-keyword headline))
431 (priority (org-element-property :priority headline))
432 (title (org-element-interpret-data
433 (org-element-property :title headline)))
434 (tags (let ((tag-list (if (org-element-property :archivedp headline)
435 (cons org-archive-tag
436 (org-element-property :tags headline))
437 (org-element-property :tags headline))))
438 (and tag-list
439 (format ":%s:" (mapconcat 'identity tag-list ":")))))
440 (commentedp (org-element-property :commentedp headline))
441 (quotedp (org-element-property :quotedp headline))
442 (pre-blank (or (org-element-property :pre-blank headline) 0))
443 (heading (concat (make-string level ?*)
444 (and todo (concat " " todo))
445 (and quotedp (concat " " org-quote-string))
446 (and commentedp (concat " " org-comment-string))
447 (and priority
448 (format " [#%s]" (char-to-string priority)))
449 (cond ((and org-footnote-section
450 (org-element-property
451 :footnote-section-p headline))
452 (concat " " org-footnote-section))
453 (title (concat " " title))))))
454 (concat heading
455 ;; Align tags.
456 (when tags
457 (cond
458 ((zerop org-tags-column) (format " %s" tags))
459 ((< org-tags-column 0)
460 (concat
461 (make-string
462 (max (- (+ org-tags-column (length heading) (length tags))) 1)
464 tags))
466 (concat
467 (make-string (max (- org-tags-column (length heading)) 1) ? )
468 tags))))
469 (make-string (1+ pre-blank) 10)
470 contents)))
473 ;;;; Inlinetask
475 (defun org-element-inlinetask-parser (&optional raw-secondary-p)
476 "Parse an inline task.
478 Return a list whose CAR is `inlinetask' and CDR is a plist
479 containing `:title', `:begin', `:end', `:hiddenp',
480 `:contents-begin' and `:contents-end', `:level', `:priority',
481 `:tags', `:todo-keyword', `:todo-type', `:scheduled',
482 `:deadline', `:timestamp', `:clock' and `:post-blank' keywords.
484 The plist also contains any property set in the property drawer,
485 with its name in lowercase, the underscores replaced with hyphens
486 and colons at the beginning (i.e. `:custom-id').
488 When optional argument RAW-SECONDARY-P is non-nil, inline-task's
489 title will not be parsed as a secondary string, but as a plain
490 string instead.
492 Assume point is at beginning of the inline task."
493 (save-excursion
494 (let* ((keywords (org-element-collect-affiliated-keywords))
495 (begin (car keywords))
496 (components (org-heading-components))
497 (todo (nth 2 components))
498 (todo-type (and todo
499 (if (member todo org-done-keywords) 'done 'todo)))
500 (tags (let ((raw-tags (nth 5 components)))
501 (and raw-tags (org-split-string raw-tags ":"))))
502 (title (if raw-secondary-p (nth 4 components)
503 (org-element-parse-secondary-string
504 (nth 4 components)
505 (org-element-restriction 'inlinetask))))
506 (standard-props (let (plist)
507 (mapc
508 (lambda (p)
509 (let ((p-name (downcase (car p))))
510 (while (string-match "_" p-name)
511 (setq p-name
512 (replace-match "-" nil nil p-name)))
513 (setq p-name (intern (concat ":" p-name)))
514 (setq plist
515 (plist-put plist p-name (cdr p)))))
516 (org-entry-properties nil 'standard))
517 plist))
518 (time-props (org-entry-properties nil 'special "CLOCK"))
519 (scheduled (cdr (assoc "SCHEDULED" time-props)))
520 (deadline (cdr (assoc "DEADLINE" time-props)))
521 (clock (cdr (assoc "CLOCK" time-props)))
522 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
523 (contents-begin (save-excursion (forward-line) (point)))
524 (hidden (org-truely-invisible-p))
525 (pos-before-blank (org-inlinetask-goto-end))
526 ;; In the case of a single line task, CONTENTS-BEGIN and
527 ;; CONTENTS-END might overlap.
528 (contents-end (max contents-begin
529 (if (not (bolp)) (point-at-bol)
530 (save-excursion (forward-line -1) (point)))))
531 (end (progn (org-skip-whitespace)
532 (if (eobp) (point) (point-at-bol)))))
533 `(inlinetask
534 (:title ,title
535 :begin ,begin
536 :end ,end
537 :hiddenp ,(and (> contents-end contents-begin) hidden)
538 :contents-begin ,contents-begin
539 :contents-end ,contents-end
540 :level ,(nth 1 components)
541 :priority ,(nth 3 components)
542 :tags ,tags
543 :todo-keyword ,todo
544 :todo-type ,todo-type
545 :scheduled ,scheduled
546 :deadline ,deadline
547 :timestamp ,timestamp
548 :clock ,clock
549 :post-blank ,(count-lines pos-before-blank end)
550 ,@standard-props
551 ,@(cadr keywords))))))
553 (defun org-element-inlinetask-interpreter (inlinetask contents)
554 "Interpret INLINETASK element as Org syntax.
555 CONTENTS is the contents of inlinetask."
556 (let* ((level (org-element-property :level inlinetask))
557 (todo (org-element-property :todo-keyword inlinetask))
558 (priority (org-element-property :priority inlinetask))
559 (title (org-element-interpret-data
560 (org-element-property :title inlinetask)))
561 (tags (let ((tag-list (org-element-property :tags inlinetask)))
562 (and tag-list
563 (format ":%s:" (mapconcat 'identity tag-list ":")))))
564 (task (concat (make-string level ?*)
565 (and todo (concat " " todo))
566 (and priority
567 (format " [#%s]" (char-to-string priority)))
568 (and title (concat " " title)))))
569 (concat task
570 ;; Align tags.
571 (when tags
572 (cond
573 ((zerop org-tags-column) (format " %s" tags))
574 ((< org-tags-column 0)
575 (concat
576 (make-string
577 (max (- (+ org-tags-column (length task) (length tags))) 1)
579 tags))
581 (concat
582 (make-string (max (- org-tags-column (length task)) 1) ? )
583 tags))))
584 ;; Prefer degenerate inlinetasks when there are no
585 ;; contents.
586 (when contents
587 (concat "\n"
588 contents
589 (make-string level ?*) " END")))))
592 ;;;; Item
594 (defun org-element-item-parser (struct &optional raw-secondary-p)
595 "Parse an item.
597 STRUCT is the structure of the plain list.
599 Return a list whose CAR is `item' and CDR is a plist containing
600 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
601 `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
602 `:post-blank' keywords.
604 When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
605 any, will not be parsed as a secondary string, but as a plain
606 string instead.
608 Assume point is at the beginning of the item."
609 (save-excursion
610 (beginning-of-line)
611 (let* ((begin (point))
612 (bullet (org-list-get-bullet (point) struct))
613 (checkbox (let ((box (org-list-get-checkbox begin struct)))
614 (cond ((equal "[ ]" box) 'off)
615 ((equal "[X]" box) 'on)
616 ((equal "[-]" box) 'trans))))
617 (counter (let ((c (org-list-get-counter begin struct)))
618 (cond
619 ((not c) nil)
620 ((string-match "[A-Za-z]" c)
621 (- (string-to-char (upcase (match-string 0 c)))
622 64))
623 ((string-match "[0-9]+" c)
624 (string-to-number (match-string 0 c))))))
625 (tag
626 (let ((raw-tag (org-list-get-tag begin struct)))
627 (and raw-tag
628 (if raw-secondary-p raw-tag
629 (org-element-parse-secondary-string
630 raw-tag (org-element-restriction 'item))))))
631 (end (org-list-get-item-end begin struct))
632 (contents-begin (progn (looking-at org-list-full-item-re)
633 (goto-char (match-end 0))
634 (org-skip-whitespace)
635 ;; If first line isn't empty,
636 ;; contents really start at the text
637 ;; after item's meta-data.
638 (if (= (point-at-bol) begin) (point)
639 (point-at-bol))))
640 (hidden (progn (forward-line)
641 (and (not (= (point) end))
642 (org-truely-invisible-p))))
643 (contents-end (progn (goto-char end)
644 (skip-chars-backward " \r\t\n")
645 (forward-line)
646 (point))))
647 `(item
648 (:bullet ,bullet
649 :begin ,begin
650 :end ,end
651 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
652 ;; up in the case of an empty item separated
653 ;; from the next by a blank line. Thus, ensure
654 ;; the former is always the smallest of two.
655 :contents-begin ,(min contents-begin contents-end)
656 :contents-end ,(max contents-begin contents-end)
657 :checkbox ,checkbox
658 :counter ,counter
659 :tag ,tag
660 :hiddenp ,hidden
661 :structure ,struct
662 :post-blank ,(count-lines contents-end end))))))
664 (defun org-element-item-interpreter (item contents)
665 "Interpret ITEM element as Org syntax.
666 CONTENTS is the contents of the element."
667 (let* ((bullet
668 (let* ((beg (org-element-property :begin item))
669 (struct (org-element-property :structure item))
670 (pre (org-list-prevs-alist struct))
671 (bul (org-element-property :bullet item)))
672 (org-list-bullet-string
673 (if (not (eq (org-list-get-list-type beg struct pre) 'ordered)) "-"
674 (let ((num
675 (car
676 (last
677 (org-list-get-item-number
678 beg struct pre (org-list-parents-alist struct))))))
679 (format "%d%s"
681 (if (eq org-plain-list-ordered-item-terminator ?\)) ")"
682 ".")))))))
683 (checkbox (org-element-property :checkbox item))
684 (counter (org-element-property :counter item))
685 (tag (let ((tag (org-element-property :tag item)))
686 (and tag (org-element-interpret-data tag))))
687 ;; Compute indentation.
688 (ind (make-string (length bullet) 32))
689 (item-starts-with-par-p
690 (eq (org-element-type (car (org-element-contents item)))
691 'paragraph)))
692 ;; Indent contents.
693 (concat
694 bullet
695 (and counter (format "[@%d] " counter))
696 (cond
697 ((eq checkbox 'on) "[X] ")
698 ((eq checkbox 'off) "[ ] ")
699 ((eq checkbox 'trans) "[-] "))
700 (and tag (format "%s :: " tag))
701 (let ((contents (replace-regexp-in-string
702 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
703 (if item-starts-with-par-p (org-trim contents)
704 (concat "\n" contents))))))
707 ;;;; Plain List
709 (defun org-element-plain-list-parser (&optional structure)
710 "Parse a plain list.
712 Optional argument STRUCTURE, when non-nil, is the structure of
713 the plain list being parsed.
715 Return a list whose CAR is `plain-list' and CDR is a plist
716 containing `:type', `:begin', `:end', `:contents-begin' and
717 `:contents-end', `:structure' and `:post-blank' keywords.
719 Assume point is at the beginning of the list."
720 (save-excursion
721 (let* ((struct (or structure (org-list-struct)))
722 (prevs (org-list-prevs-alist struct))
723 (parents (org-list-parents-alist struct))
724 (type (org-list-get-list-type (point) struct prevs))
725 (contents-begin (point))
726 (keywords (org-element-collect-affiliated-keywords))
727 (begin (car keywords))
728 (contents-end
729 (goto-char (org-list-get-list-end (point) struct prevs)))
730 (end (save-excursion (org-skip-whitespace)
731 (if (eobp) (point) (point-at-bol)))))
732 ;; Blank lines below list belong to the top-level list only.
733 (unless (= (org-list-get-top-point struct) contents-begin)
734 (setq end (min (org-list-get-bottom-point struct)
735 (progn (org-skip-whitespace)
736 (if (eobp) (point) (point-at-bol))))))
737 ;; Return value.
738 `(plain-list
739 (:type ,type
740 :begin ,begin
741 :end ,end
742 :contents-begin ,contents-begin
743 :contents-end ,contents-end
744 :structure ,struct
745 :post-blank ,(count-lines contents-end end)
746 ,@(cadr keywords))))))
748 (defun org-element-plain-list-interpreter (plain-list contents)
749 "Interpret PLAIN-LIST element as Org syntax.
750 CONTENTS is the contents of the element."
751 contents)
754 ;;;; Quote Block
756 (defun org-element-quote-block-parser ()
757 "Parse a quote block.
759 Return a list whose CAR is `quote-block' and CDR is a plist
760 containing `:begin', `:end', `:hiddenp', `:contents-begin',
761 `:contents-end' and `:post-blank' keywords.
763 Assume point is at the beginning of the block."
764 (save-excursion
765 (let* ((case-fold-search t)
766 (keywords (org-element-collect-affiliated-keywords))
767 (begin (car keywords))
768 (contents-begin (progn (forward-line) (point)))
769 (hidden (org-truely-invisible-p))
770 (contents-end (progn (re-search-forward "^[ \t]*#\\+END_QUOTE" nil t)
771 (point-at-bol)))
772 (pos-before-blank (progn (forward-line) (point)))
773 (end (progn (org-skip-whitespace)
774 (if (eobp) (point) (point-at-bol)))))
775 `(quote-block
776 (:begin ,begin
777 :end ,end
778 :hiddenp ,hidden
779 :contents-begin ,contents-begin
780 :contents-end ,contents-end
781 :post-blank ,(count-lines pos-before-blank end)
782 ,@(cadr keywords))))))
784 (defun org-element-quote-block-interpreter (quote-block contents)
785 "Interpret QUOTE-BLOCK element as Org syntax.
786 CONTENTS is the contents of the element."
787 (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
790 ;;;; Section
792 (defun org-element-section-parser ()
793 "Parse a section.
795 Return a list whose CAR is `section' and CDR is a plist
796 containing `:begin', `:end', `:contents-begin', `contents-end'
797 and `:post-blank' keywords."
798 (save-excursion
799 ;; Beginning of section is the beginning of the first non-blank
800 ;; line after previous headline.
801 (org-with-limited-levels
802 (let ((begin
803 (save-excursion
804 (outline-previous-heading)
805 (if (not (org-at-heading-p)) (point)
806 (forward-line) (org-skip-whitespace) (point-at-bol))))
807 (end (progn (outline-next-heading) (point)))
808 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
809 (forward-line)
810 (point))))
811 `(section
812 (:begin ,begin
813 :end ,end
814 :contents-begin ,begin
815 :contents-end ,pos-before-blank
816 :post-blank ,(count-lines pos-before-blank end)))))))
818 (defun org-element-section-interpreter (section contents)
819 "Interpret SECTION element as Org syntax.
820 CONTENTS is the contents of the element."
821 contents)
824 ;;;; Special Block
826 (defun org-element-special-block-parser ()
827 "Parse a special block.
829 Return a list whose CAR is `special-block' and CDR is a plist
830 containing `:type', `:begin', `:end', `:hiddenp',
831 `:contents-begin', `:contents-end' and `:post-blank' keywords.
833 Assume point is at the beginning of the block."
834 (save-excursion
835 (let* ((case-fold-search t)
836 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\([-A-Za-z0-9]+\\)")
837 (org-match-string-no-properties 1)))
838 (keywords (org-element-collect-affiliated-keywords))
839 (begin (car keywords))
840 (contents-begin (progn (forward-line) (point)))
841 (hidden (org-truely-invisible-p))
842 (contents-end
843 (progn (re-search-forward (concat "^[ \t]*#\\+END_" type) nil t)
844 (point-at-bol)))
845 (pos-before-blank (progn (forward-line) (point)))
846 (end (progn (org-skip-whitespace)
847 (if (eobp) (point) (point-at-bol)))))
848 `(special-block
849 (:type ,type
850 :begin ,begin
851 :end ,end
852 :hiddenp ,hidden
853 :contents-begin ,contents-begin
854 :contents-end ,contents-end
855 :post-blank ,(count-lines pos-before-blank end)
856 ,@(cadr keywords))))))
858 (defun org-element-special-block-interpreter (special-block contents)
859 "Interpret SPECIAL-BLOCK element as Org syntax.
860 CONTENTS is the contents of the element."
861 (let ((block-type (org-element-property :type special-block)))
862 (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
866 ;;; Elements
868 ;; For each element, a parser and an interpreter are also defined.
869 ;; Both follow the same naming convention used for greater elements.
871 ;; Also, as for greater elements, adding a new element type is done
872 ;; through the following steps: implement a parser and an interpreter,
873 ;; tweak `org-element-current-element' so that it recognizes the new
874 ;; type and add that new type to `org-element-all-elements'.
876 ;; As a special case, when the newly defined type is a block type,
877 ;; `org-element-block-name-alist' has to be modified accordingly.
880 ;;;; Babel Call
882 (defun org-element-babel-call-parser ()
883 "Parse a babel call.
885 Return a list whose CAR is `babel-call' and CDR is a plist
886 containing `:begin', `:end', `:info' and `:post-blank' as
887 keywords."
888 (save-excursion
889 (let ((case-fold-search t)
890 (info (progn (looking-at org-babel-block-lob-one-liner-regexp)
891 (org-babel-lob-get-info)))
892 (begin (point-at-bol))
893 (pos-before-blank (progn (forward-line) (point)))
894 (end (progn (org-skip-whitespace)
895 (if (eobp) (point) (point-at-bol)))))
896 `(babel-call
897 (:begin ,begin
898 :end ,end
899 :info ,info
900 :post-blank ,(count-lines pos-before-blank end))))))
902 (defun org-element-babel-call-interpreter (babel-call contents)
903 "Interpret BABEL-CALL element as Org syntax.
904 CONTENTS is nil."
905 (let* ((babel-info (org-element-property :info babel-call))
906 (main (car babel-info))
907 (post-options (nth 1 babel-info)))
908 (concat "#+CALL: "
909 (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
910 ;; Remove redundant square brackets.
911 (replace-match (match-string 1 main) nil nil main))
912 (and post-options (format "[%s]" post-options)))))
915 ;;;; Clock
917 (defun org-element-clock-parser ()
918 "Parse a clock.
920 Return a list whose CAR is `clock' and CDR is a plist containing
921 `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
922 as keywords."
923 (save-excursion
924 (let* ((case-fold-search nil)
925 (begin (point))
926 (value (progn (search-forward org-clock-string (line-end-position) t)
927 (org-skip-whitespace)
928 (looking-at "\\[.*\\]")
929 (org-match-string-no-properties 0)))
930 (time (and (progn (goto-char (match-end 0))
931 (looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
932 (org-match-string-no-properties 1)))
933 (status (if time 'closed 'running))
934 (post-blank (let ((before-blank (progn (forward-line) (point))))
935 (org-skip-whitespace)
936 (unless (eobp) (beginning-of-line))
937 (count-lines before-blank (point))))
938 (end (point)))
939 `(clock (:status ,status
940 :value ,value
941 :time ,time
942 :begin ,begin
943 :end ,end
944 :post-blank ,post-blank)))))
946 (defun org-element-clock-interpreter (clock contents)
947 "Interpret CLOCK element as Org syntax.
948 CONTENTS is nil."
949 (concat org-clock-string " "
950 (org-element-property :value clock)
951 (let ((time (org-element-property :time clock)))
952 (and time
953 (concat " => "
954 (apply 'format
955 "%2s:%02s"
956 (org-split-string time ":")))))))
959 ;;;; Comment
961 (defun org-element-comment-parser ()
962 "Parse a comment.
964 Return a list whose CAR is `comment' and CDR is a plist
965 containing `:begin', `:end', `:value' and `:post-blank'
966 keywords.
968 Assume point is at comment beginning."
969 (save-excursion
970 (let* ((keywords (org-element-collect-affiliated-keywords))
971 (begin (car keywords))
972 ;; Match first line with a loose regexp since it might as
973 ;; well be an ill-defined keyword.
974 (value (progn
975 (looking-at "#\\+? ?")
976 (buffer-substring-no-properties
977 (match-end 0) (progn (forward-line) (point)))))
978 (com-end
979 ;; Get comments ending. This may not be accurate if
980 ;; commented lines within an item are followed by
981 ;; commented lines outside of a list. Though, parser will
982 ;; always get it right as it already knows surrounding
983 ;; element and has narrowed buffer to its contents.
984 (progn
985 (while (looking-at "\\(\\(# ?\\)[^+]\\|[ \t]*#\\+\\( \\|$\\)\\)")
986 ;; Accumulate lines without leading hash and plus sign
987 ;; if any. First whitespace is also ignored.
988 (setq value
989 (concat value
990 (buffer-substring-no-properties
991 (or (match-end 2) (match-end 3))
992 (progn (forward-line) (point))))))
993 (point)))
994 (end (progn (goto-char com-end)
995 (org-skip-whitespace)
996 (if (eobp) (point) (point-at-bol)))))
997 `(comment
998 (:begin ,begin
999 :end ,end
1000 :value ,value
1001 :post-blank ,(count-lines com-end end)
1002 ,@(cadr keywords))))))
1004 (defun org-element-comment-interpreter (comment contents)
1005 "Interpret COMMENT element as Org syntax.
1006 CONTENTS is nil."
1007 (replace-regexp-in-string "^" "#+ " (org-element-property :value comment)))
1010 ;;;; Comment Block
1012 (defun org-element-comment-block-parser ()
1013 "Parse an export block.
1015 Return a list whose CAR is `comment-block' and CDR is a plist
1016 containing `:begin', `:end', `:hiddenp', `:value' and
1017 `:post-blank' keywords.
1019 Assume point is at comment block beginning."
1020 (save-excursion
1021 (let* ((case-fold-search t)
1022 (keywords (org-element-collect-affiliated-keywords))
1023 (begin (car keywords))
1024 (contents-begin (progn (forward-line) (point)))
1025 (hidden (org-truely-invisible-p))
1026 (contents-end
1027 (progn (re-search-forward "^[ \t]*#\\+END_COMMENT" nil t)
1028 (point-at-bol)))
1029 (pos-before-blank (progn (forward-line) (point)))
1030 (end (progn (org-skip-whitespace)
1031 (if (eobp) (point) (point-at-bol))))
1032 (value (buffer-substring-no-properties contents-begin contents-end)))
1033 `(comment-block
1034 (:begin ,begin
1035 :end ,end
1036 :value ,value
1037 :hiddenp ,hidden
1038 :post-blank ,(count-lines pos-before-blank end)
1039 ,@(cadr keywords))))))
1041 (defun org-element-comment-block-interpreter (comment-block contents)
1042 "Interpret COMMENT-BLOCK element as Org syntax.
1043 CONTENTS is nil."
1044 (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
1045 (org-remove-indentation (org-element-property :value comment-block))))
1048 ;;;; Example Block
1050 (defun org-element-example-block-parser ()
1051 "Parse an example block.
1053 Return a list whose CAR is `example-block' and CDR is a plist
1054 containing `:begin', `:end', `:number-lines', `:preserve-indent',
1055 `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
1056 `:switches', `:value' and `:post-blank' keywords."
1057 (save-excursion
1058 (let* ((case-fold-search t)
1059 (switches
1060 (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
1061 (org-match-string-no-properties 1)))
1062 ;; Switches analysis
1063 (number-lines (cond ((not switches) nil)
1064 ((string-match "-n\\>" switches) 'new)
1065 ((string-match "+n\\>" switches) 'continued)))
1066 (preserve-indent (and switches (string-match "-i\\>" switches)))
1067 ;; Should labels be retained in (or stripped from) example
1068 ;; blocks?
1069 (retain-labels
1070 (or (not switches)
1071 (not (string-match "-r\\>" switches))
1072 (and number-lines (string-match "-k\\>" switches))))
1073 ;; What should code-references use - labels or
1074 ;; line-numbers?
1075 (use-labels
1076 (or (not switches)
1077 (and retain-labels (not (string-match "-k\\>" switches)))))
1078 (label-fmt (and switches
1079 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1080 (match-string 1 switches)))
1081 ;; Standard block parsing.
1082 (keywords (org-element-collect-affiliated-keywords))
1083 (begin (car keywords))
1084 (contents-begin (progn (forward-line) (point)))
1085 (hidden (org-truely-invisible-p))
1086 (contents-end
1087 (progn (re-search-forward "^[ \t]*#\\+END_EXAMPLE" nil t)
1088 (point-at-bol)))
1089 (value (buffer-substring-no-properties contents-begin contents-end))
1090 (pos-before-blank (progn (forward-line) (point)))
1091 (end (progn (org-skip-whitespace)
1092 (if (eobp) (point) (point-at-bol)))))
1093 `(example-block
1094 (:begin ,begin
1095 :end ,end
1096 :value ,value
1097 :switches ,switches
1098 :number-lines ,number-lines
1099 :preserve-indent ,preserve-indent
1100 :retain-labels ,retain-labels
1101 :use-labels ,use-labels
1102 :label-fmt ,label-fmt
1103 :hiddenp ,hidden
1104 :post-blank ,(count-lines pos-before-blank end)
1105 ,@(cadr keywords))))))
1107 (defun org-element-example-block-interpreter (example-block contents)
1108 "Interpret EXAMPLE-BLOCK element as Org syntax.
1109 CONTENTS is nil."
1110 (let ((switches (org-element-property :switches example-block)))
1111 (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
1112 (org-remove-indentation
1113 (org-element-property :value example-block))
1114 "#+END_EXAMPLE")))
1117 ;;;; Export Block
1119 (defun org-element-export-block-parser ()
1120 "Parse an export block.
1122 Return a list whose CAR is `export-block' and CDR is a plist
1123 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1124 `:post-blank' keywords.
1126 Assume point is at export-block beginning."
1127 (save-excursion
1128 (let* ((case-fold-search t)
1129 (type (progn (looking-at "[ \t]*#\\+BEGIN_\\([A-Za-z0-9]+\\)")
1130 (upcase (org-match-string-no-properties 1))))
1131 (keywords (org-element-collect-affiliated-keywords))
1132 (begin (car keywords))
1133 (contents-begin (progn (forward-line) (point)))
1134 (hidden (org-truely-invisible-p))
1135 (contents-end
1136 (progn (re-search-forward (concat "^[ \t]*#\\+END_" type) nil t)
1137 (point-at-bol)))
1138 (pos-before-blank (progn (forward-line) (point)))
1139 (end (progn (org-skip-whitespace)
1140 (if (eobp) (point) (point-at-bol))))
1141 (value (buffer-substring-no-properties contents-begin contents-end)))
1142 `(export-block
1143 (:begin ,begin
1144 :end ,end
1145 :type ,type
1146 :value ,value
1147 :hiddenp ,hidden
1148 :post-blank ,(count-lines pos-before-blank end)
1149 ,@(cadr keywords))))))
1151 (defun org-element-export-block-interpreter (export-block contents)
1152 "Interpret EXPORT-BLOCK element as Org syntax.
1153 CONTENTS is nil."
1154 (let ((type (org-element-property :type export-block)))
1155 (concat (format "#+BEGIN_%s\n" type)
1156 (org-element-property :value export-block)
1157 (format "#+END_%s" type))))
1160 ;;;; Fixed-width
1162 (defun org-element-fixed-width-parser ()
1163 "Parse a fixed-width section.
1165 Return a list whose CAR is `fixed-width' and CDR is a plist
1166 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1168 Assume point is at the beginning of the fixed-width area."
1169 (save-excursion
1170 (let* ((keywords (org-element-collect-affiliated-keywords))
1171 (begin (car keywords))
1172 value
1173 (end-area
1174 ;; Ending position may not be accurate if fixed-width
1175 ;; lines within an item are followed by fixed-width lines
1176 ;; outside of a list. Though, parser will always get it
1177 ;; right as it already knows surrounding element and has
1178 ;; narrowed buffer to its contents.
1179 (progn
1180 (while (looking-at "[ \t]*:\\( \\|$\\)")
1181 ;, Accumulate text without starting colons.
1182 (setq value
1183 (concat value
1184 (buffer-substring-no-properties
1185 (match-end 0) (point-at-eol))
1186 "\n"))
1187 (forward-line))
1188 (point)))
1189 (end (progn (org-skip-whitespace)
1190 (if (eobp) (point) (point-at-bol)))))
1191 `(fixed-width
1192 (:begin ,begin
1193 :end ,end
1194 :value ,value
1195 :post-blank ,(count-lines end-area end)
1196 ,@(cadr keywords))))))
1198 (defun org-element-fixed-width-interpreter (fixed-width contents)
1199 "Interpret FIXED-WIDTH element as Org syntax.
1200 CONTENTS is nil."
1201 (replace-regexp-in-string
1202 "^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
1205 ;;;; Horizontal Rule
1207 (defun org-element-horizontal-rule-parser ()
1208 "Parse an horizontal rule.
1210 Return a list whose CAR is `horizontal-rule' and CDR is a plist
1211 containing `:begin', `:end' and `:post-blank' keywords."
1212 (save-excursion
1213 (let* ((keywords (org-element-collect-affiliated-keywords))
1214 (begin (car keywords))
1215 (post-hr (progn (forward-line) (point)))
1216 (end (progn (org-skip-whitespace)
1217 (if (eobp) (point) (point-at-bol)))))
1218 `(horizontal-rule
1219 (:begin ,begin
1220 :end ,end
1221 :post-blank ,(count-lines post-hr end)
1222 ,@(cadr keywords))))))
1224 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1225 "Interpret HORIZONTAL-RULE element as Org syntax.
1226 CONTENTS is nil."
1227 "-----")
1230 ;;;; Keyword
1232 (defun org-element-keyword-parser ()
1233 "Parse a keyword at point.
1235 Return a list whose CAR is `keyword' and CDR is a plist
1236 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1237 keywords."
1238 (save-excursion
1239 (let* ((case-fold-search t)
1240 (begin (point))
1241 (key (progn (looking-at
1242 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1243 (upcase (org-match-string-no-properties 1))))
1244 (value (org-trim (buffer-substring-no-properties
1245 (match-end 0) (point-at-eol))))
1246 (pos-before-blank (progn (forward-line) (point)))
1247 (end (progn (org-skip-whitespace)
1248 (if (eobp) (point) (point-at-bol)))))
1249 `(keyword
1250 (:key ,key
1251 :value ,value
1252 :begin ,begin
1253 :end ,end
1254 :post-blank ,(count-lines pos-before-blank end))))))
1256 (defun org-element-keyword-interpreter (keyword contents)
1257 "Interpret KEYWORD element as Org syntax.
1258 CONTENTS is nil."
1259 (format "#+%s: %s"
1260 (org-element-property :key keyword)
1261 (org-element-property :value keyword)))
1264 ;;;; Latex Environment
1266 (defun org-element-latex-environment-parser ()
1267 "Parse a LaTeX environment.
1269 Return a list whose CAR is `latex-environment' and CDR is a plist
1270 containing `:begin', `:end', `:value' and `:post-blank'
1271 keywords.
1273 Assume point is at the beginning of the latex environment."
1274 (save-excursion
1275 (let* ((case-fold-search t)
1276 (code-begin (point))
1277 (keywords (org-element-collect-affiliated-keywords))
1278 (begin (car keywords))
1279 (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
1280 (regexp-quote (match-string 1))))
1281 (code-end
1282 (progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env))
1283 (forward-line)
1284 (point)))
1285 (value (buffer-substring-no-properties code-begin code-end))
1286 (end (progn (org-skip-whitespace)
1287 (if (eobp) (point) (point-at-bol)))))
1288 `(latex-environment
1289 (:begin ,begin
1290 :end ,end
1291 :value ,value
1292 :post-blank ,(count-lines code-end end)
1293 ,@(cadr keywords))))))
1295 (defun org-element-latex-environment-interpreter (latex-environment contents)
1296 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1297 CONTENTS is nil."
1298 (org-element-property :value latex-environment))
1301 ;;;; Paragraph
1303 (defun org-element-paragraph-parser ()
1304 "Parse a paragraph.
1306 Return a list whose CAR is `paragraph' and CDR is a plist
1307 containing `:begin', `:end', `:contents-begin' and
1308 `:contents-end' and `:post-blank' keywords.
1310 Assume point is at the beginning of the paragraph."
1311 (save-excursion
1312 (let* ((contents-begin (point))
1313 (keywords (org-element-collect-affiliated-keywords))
1314 (begin (car keywords))
1315 (contents-end
1316 (progn (end-of-line)
1317 (if (re-search-forward org-element-paragraph-separate nil 'm)
1318 (progn (forward-line -1) (end-of-line) (point))
1319 (point))))
1320 (pos-before-blank (progn (forward-line) (point)))
1321 (end (progn (org-skip-whitespace)
1322 (if (eobp) (point) (point-at-bol)))))
1323 `(paragraph
1324 (:begin ,begin
1325 :end ,end
1326 :contents-begin ,contents-begin
1327 :contents-end ,contents-end
1328 :post-blank ,(count-lines pos-before-blank end)
1329 ,@(cadr keywords))))))
1331 (defun org-element-paragraph-interpreter (paragraph contents)
1332 "Interpret PARAGRAPH element as Org syntax.
1333 CONTENTS is the contents of the element."
1334 contents)
1337 ;;;; Planning
1339 (defun org-element-planning-parser ()
1340 "Parse a planning.
1342 Return a list whose CAR is `planning' and CDR is a plist
1343 containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
1344 and `:post-blank' keywords."
1345 (save-excursion
1346 (let* ((case-fold-search nil)
1347 (begin (point))
1348 (post-blank (let ((before-blank (progn (forward-line) (point))))
1349 (org-skip-whitespace)
1350 (unless (eobp) (beginning-of-line))
1351 (count-lines before-blank (point))))
1352 (end (point))
1353 closed deadline scheduled)
1354 (goto-char begin)
1355 (while (re-search-forward org-keyword-time-not-clock-regexp
1356 (line-end-position) t)
1357 (goto-char (match-end 1))
1358 (org-skip-whitespace)
1359 (let ((time (buffer-substring-no-properties (point) (match-end 0)))
1360 (keyword (match-string 1)))
1361 (cond ((equal keyword org-closed-string) (setq closed time))
1362 ((equal keyword org-deadline-string) (setq deadline time))
1363 (t (setq scheduled time)))))
1364 `(planning
1365 (:closed ,closed
1366 :deadline ,deadline
1367 :scheduled ,scheduled
1368 :begin ,begin
1369 :end ,end
1370 :post-blank ,post-blank)))))
1372 (defun org-element-planning-interpreter (planning contents)
1373 "Interpret PLANNING element as Org syntax.
1374 CONTENTS is nil."
1375 (mapconcat
1376 'identity
1377 (delq nil
1378 (list (let ((closed (org-element-property :closed planning)))
1379 (when closed (concat org-closed-string " " closed)))
1380 (let ((deadline (org-element-property :deadline planning)))
1381 (when deadline (concat org-deadline-string " " deadline)))
1382 (let ((scheduled (org-element-property :scheduled planning)))
1383 (when scheduled (concat org-scheduled-string " " scheduled)))))
1384 " "))
1387 ;;;; Property Drawer
1389 (defun org-element-property-drawer-parser ()
1390 "Parse a property drawer.
1392 Return a list whose CAR is `property-drawer' and CDR is a plist
1393 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1394 `:contents-end', `:properties' and `:post-blank' keywords.
1396 Assume point is at the beginning of the property drawer."
1397 (save-excursion
1398 (let ((case-fold-search t)
1399 (begin (point))
1400 (prop-begin (progn (forward-line) (point)))
1401 (hidden (org-truely-invisible-p))
1402 (properties
1403 (let (val)
1404 (while (not (looking-at "^[ \t]*:END:"))
1405 (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
1406 (push (cons (org-match-string-no-properties 1)
1407 (org-trim
1408 (buffer-substring-no-properties
1409 (match-end 0) (point-at-eol))))
1410 val))
1411 (forward-line))
1412 val))
1413 (prop-end (progn (re-search-forward "^[ \t]*:END:" nil t)
1414 (point-at-bol)))
1415 (pos-before-blank (progn (forward-line) (point)))
1416 (end (progn (org-skip-whitespace)
1417 (if (eobp) (point) (point-at-bol)))))
1418 `(property-drawer
1419 (:begin ,begin
1420 :end ,end
1421 :hiddenp ,hidden
1422 :properties ,properties
1423 :post-blank ,(count-lines pos-before-blank end))))))
1425 (defun org-element-property-drawer-interpreter (property-drawer contents)
1426 "Interpret PROPERTY-DRAWER element as Org syntax.
1427 CONTENTS is nil."
1428 (let ((props (org-element-property :properties property-drawer)))
1429 (concat
1430 ":PROPERTIES:\n"
1431 (mapconcat (lambda (p)
1432 (format org-property-format (format ":%s:" (car p)) (cdr p)))
1433 (nreverse props) "\n")
1434 "\n:END:")))
1437 ;;;; Quote Section
1439 (defun org-element-quote-section-parser ()
1440 "Parse a quote section.
1442 Return a list whose CAR is `quote-section' and CDR is a plist
1443 containing `:begin', `:end', `:value' and `:post-blank' keywords.
1445 Assume point is at beginning of the section."
1446 (save-excursion
1447 (let* ((begin (point))
1448 (end (progn (org-with-limited-levels (outline-next-heading))
1449 (point)))
1450 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1451 (forward-line)
1452 (point)))
1453 (value (buffer-substring-no-properties begin pos-before-blank)))
1454 `(quote-section
1455 (:begin ,begin
1456 :end ,end
1457 :value ,value
1458 :post-blank ,(count-lines pos-before-blank end))))))
1460 (defun org-element-quote-section-interpreter (quote-section contents)
1461 "Interpret QUOTE-SECTION element as Org syntax.
1462 CONTENTS is nil."
1463 (org-element-property :value quote-section))
1466 ;;;; Src Block
1468 (defun org-element-src-block-parser ()
1469 "Parse a src block.
1471 Return a list whose CAR is `src-block' and CDR is a plist
1472 containing `:language', `:switches', `:parameters', `:begin',
1473 `:end', `:hiddenp', `:number-lines', `:retain-labels',
1474 `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
1475 `:post-blank' keywords.
1477 Assume point is at the beginning of the block."
1478 (save-excursion
1479 (let* ((case-fold-search t)
1480 (contents-begin (point))
1481 ;; Get affiliated keywords.
1482 (keywords (org-element-collect-affiliated-keywords))
1483 ;; Get beginning position.
1484 (begin (car keywords))
1485 ;; Get language as a string.
1486 (language
1487 (progn
1488 (looking-at
1489 (concat "^[ \t]*#\\+BEGIN_SRC"
1490 "\\(?: +\\(\\S-+\\)\\)?"
1491 "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
1492 "\\(.*\\)[ \t]*$"))
1493 (org-match-string-no-properties 1)))
1494 ;; Get switches.
1495 (switches (org-match-string-no-properties 2))
1496 ;; Get parameters.
1497 (parameters (org-match-string-no-properties 3))
1498 ;; Switches analysis
1499 (number-lines (cond ((not switches) nil)
1500 ((string-match "-n\\>" switches) 'new)
1501 ((string-match "+n\\>" switches) 'continued)))
1502 (preserve-indent (and switches (string-match "-i\\>" switches)))
1503 (label-fmt (and switches
1504 (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
1505 (match-string 1 switches)))
1506 ;; Should labels be retained in (or stripped from) src
1507 ;; blocks?
1508 (retain-labels
1509 (or (not switches)
1510 (not (string-match "-r\\>" switches))
1511 (and number-lines (string-match "-k\\>" switches))))
1512 ;; What should code-references use - labels or
1513 ;; line-numbers?
1514 (use-labels
1515 (or (not switches)
1516 (and retain-labels (not (string-match "-k\\>" switches)))))
1517 ;; Get position at end of block.
1518 (contents-end (progn (re-search-forward "^[ \t]*#\\+END_SRC" nil t)
1519 (forward-line)
1520 (point)))
1521 ;; Retrieve code.
1522 (value (buffer-substring-no-properties
1523 (save-excursion (goto-char contents-begin)
1524 (forward-line)
1525 (point))
1526 (match-beginning 0)))
1527 ;; Get position after ending blank lines.
1528 (end (progn (org-skip-whitespace)
1529 (if (eobp) (point) (point-at-bol))))
1530 ;; Get visibility status.
1531 (hidden (progn (goto-char contents-begin)
1532 (forward-line)
1533 (org-truely-invisible-p))))
1534 `(src-block
1535 (:language ,language
1536 :switches ,(and (org-string-nw-p switches)
1537 (org-trim switches))
1538 :parameters ,(and (org-string-nw-p parameters)
1539 (org-trim parameters))
1540 :begin ,begin
1541 :end ,end
1542 :number-lines ,number-lines
1543 :preserve-indent ,preserve-indent
1544 :retain-labels ,retain-labels
1545 :use-labels ,use-labels
1546 :label-fmt ,label-fmt
1547 :hiddenp ,hidden
1548 :value ,value
1549 :post-blank ,(count-lines contents-end end)
1550 ,@(cadr keywords))))))
1552 (defun org-element-src-block-interpreter (src-block contents)
1553 "Interpret SRC-BLOCK element as Org syntax.
1554 CONTENTS is nil."
1555 (let ((lang (org-element-property :language src-block))
1556 (switches (org-element-property :switches src-block))
1557 (params (org-element-property :parameters src-block))
1558 (value (let ((val (org-element-property :value src-block)))
1559 (cond
1561 (org-src-preserve-indentation val)
1562 ((zerop org-edit-src-content-indentation)
1563 (org-remove-indentation val))
1565 (let ((ind (make-string
1566 org-edit-src-content-indentation 32)))
1567 (replace-regexp-in-string
1568 "\\(^\\)[ \t]*\\S-" ind
1569 (org-remove-indentation val) nil nil 1)))))))
1570 (concat (format "#+BEGIN_SRC%s\n"
1571 (concat (and lang (concat " " lang))
1572 (and switches (concat " " switches))
1573 (and params (concat " " params))))
1574 value
1575 "#+END_SRC")))
1578 ;;;; Table
1580 (defun org-element-table-parser ()
1581 "Parse a table at point.
1583 Return a list whose CAR is `table' and CDR is a plist containing
1584 `:begin', `:end', `:tblfm', `:type', `:contents-begin',
1585 `:contents-end', `:value' and `:post-blank' keywords.
1587 Assume point is at the beginning of the table."
1588 (save-excursion
1589 (let* ((case-fold-search t)
1590 (table-begin (point))
1591 (type (if (org-at-table.el-p) 'table.el 'org))
1592 (keywords (org-element-collect-affiliated-keywords))
1593 (begin (car keywords))
1594 (table-end (goto-char (marker-position (org-table-end t))))
1595 (tblfm (let (acc)
1596 (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
1597 (push (org-match-string-no-properties 1) acc)
1598 (forward-line))
1599 acc))
1600 (pos-before-blank (point))
1601 (end (progn (org-skip-whitespace)
1602 (if (eobp) (point) (point-at-bol)))))
1603 `(table
1604 (:begin ,begin
1605 :end ,end
1606 :type ,type
1607 :tblfm ,tblfm
1608 ;; Only `org' tables have contents. `table.el' tables
1609 ;; use a `:value' property to store raw table as
1610 ;; a string.
1611 :contents-begin ,(and (eq type 'org) table-begin)
1612 :contents-end ,(and (eq type 'org) table-end)
1613 :value ,(and (eq type 'table.el)
1614 (buffer-substring-no-properties
1615 table-begin table-end))
1616 :post-blank ,(count-lines pos-before-blank end)
1617 ,@(cadr keywords))))))
1619 (defun org-element-table-interpreter (table contents)
1620 "Interpret TABLE element as Org syntax.
1621 CONTENTS is nil."
1622 (if (eq (org-element-property :type table) 'table.el)
1623 (org-remove-indentation (org-element-property :value table))
1624 (concat (with-temp-buffer (insert contents)
1625 (org-table-align)
1626 (buffer-string))
1627 (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
1628 (reverse (org-element-property :tblfm table))
1629 "\n"))))
1632 ;;;; Table Row
1634 (defun org-element-table-row-parser ()
1635 "Parse table row at point.
1637 Return a list whose CAR is `table-row' and CDR is a plist
1638 containing `:begin', `:end', `:contents-begin', `:contents-end',
1639 `:type' and `:post-blank' keywords."
1640 (save-excursion
1641 (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
1642 (begin (point))
1643 ;; A table rule has no contents. In that case, ensure
1644 ;; CONTENTS-BEGIN matches CONTENTS-END.
1645 (contents-begin (if (eq type 'standard)
1646 (progn (search-forward "|") (point))
1647 (end-of-line)
1648 (skip-chars-backward " \r\t\n")
1649 (point)))
1650 (contents-end (progn (end-of-line)
1651 (skip-chars-backward " \r\t\n")
1652 (point)))
1653 (end (progn (forward-line) (point))))
1654 `(table-row
1655 (:type ,type
1656 :begin ,begin
1657 :end ,end
1658 :contents-begin ,contents-begin
1659 :contents-end ,contents-end
1660 :post-blank 0)))))
1662 (defun org-element-table-row-interpreter (table-row contents)
1663 "Interpret TABLE-ROW element as Org syntax.
1664 CONTENTS is the contents of the table row."
1665 (if (eq (org-element-property :type table-row) 'rule) "|-"
1666 (concat "| " contents)))
1669 ;;;; Verse Block
1671 (defun org-element-verse-block-parser ()
1672 "Parse a verse block.
1674 Return a list whose CAR is `verse-block' and CDR is a plist
1675 containing `:begin', `:end', `:contents-begin', `:contents-end',
1676 `:hiddenp' and `:post-blank' keywords.
1678 Assume point is at beginning of the block."
1679 (save-excursion
1680 (let* ((case-fold-search t)
1681 (keywords (org-element-collect-affiliated-keywords))
1682 (begin (car keywords))
1683 (hidden (progn (forward-line) (org-truely-invisible-p)))
1684 (contents-begin (point))
1685 (contents-end
1686 (progn
1687 (re-search-forward (concat "^[ \t]*#\\+END_VERSE") nil t)
1688 (point-at-bol)))
1689 (pos-before-blank (progn (forward-line) (point)))
1690 (end (progn (org-skip-whitespace)
1691 (if (eobp) (point) (point-at-bol)))))
1692 `(verse-block
1693 (:begin ,begin
1694 :end ,end
1695 :contents-begin ,contents-begin
1696 :contents-end ,contents-end
1697 :hiddenp ,hidden
1698 :post-blank ,(count-lines pos-before-blank end)
1699 ,@(cadr keywords))))))
1701 (defun org-element-verse-block-interpreter (verse-block contents)
1702 "Interpret VERSE-BLOCK element as Org syntax.
1703 CONTENTS is verse block contents."
1704 (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
1708 ;;; Objects
1710 ;; Unlike to elements, interstices can be found between objects.
1711 ;; That's why, along with the parser, successor functions are provided
1712 ;; for each object. Some objects share the same successor (i.e. `code'
1713 ;; and `verbatim' objects).
1715 ;; A successor must accept a single argument bounding the search. It
1716 ;; will return either a cons cell whose CAR is the object's type, as
1717 ;; a symbol, and CDR the position of its next occurrence, or nil.
1719 ;; Successors follow the naming convention:
1720 ;; org-element-NAME-successor, where NAME is the name of the
1721 ;; successor, as defined in `org-element-all-successors'.
1723 ;; Some object types (i.e. `emphasis') are recursive. Restrictions on
1724 ;; object types they can contain will be specified in
1725 ;; `org-element-object-restrictions'.
1727 ;; Adding a new type of object is simple. Implement a successor,
1728 ;; a parser, and an interpreter for it, all following the naming
1729 ;; convention. Register type in `org-element-all-objects' and
1730 ;; successor in `org-element-all-successors'. Maybe tweak
1731 ;; restrictions about it, and that's it.
1734 ;;;; Bold
1736 (defun org-element-bold-parser ()
1737 "Parse bold object at point.
1739 Return a list whose CAR is `bold' and CDR is a plist with
1740 `:begin', `:end', `:contents-begin' and `:contents-end' and
1741 `:post-blank' keywords.
1743 Assume point is at the first star marker."
1744 (save-excursion
1745 (unless (bolp) (backward-char 1))
1746 (looking-at org-emph-re)
1747 (let ((begin (match-beginning 2))
1748 (contents-begin (match-beginning 4))
1749 (contents-end (match-end 4))
1750 (post-blank (progn (goto-char (match-end 2))
1751 (skip-chars-forward " \t")))
1752 (end (point)))
1753 `(bold
1754 (:begin ,begin
1755 :end ,end
1756 :contents-begin ,contents-begin
1757 :contents-end ,contents-end
1758 :post-blank ,post-blank)))))
1760 (defun org-element-bold-interpreter (bold contents)
1761 "Interpret BOLD object as Org syntax.
1762 CONTENTS is the contents of the object."
1763 (format "*%s*" contents))
1765 (defun org-element-text-markup-successor (limit)
1766 "Search for the next text-markup object.
1768 LIMIT bounds the search.
1770 Return value is a cons cell whose CAR is a symbol among `bold',
1771 `italic', `underline', `strike-through', `code' and `verbatim'
1772 and CDR is beginning position."
1773 (save-excursion
1774 (unless (bolp) (backward-char))
1775 (when (re-search-forward org-emph-re limit t)
1776 (let ((marker (match-string 3)))
1777 (cons (cond
1778 ((equal marker "*") 'bold)
1779 ((equal marker "/") 'italic)
1780 ((equal marker "_") 'underline)
1781 ((equal marker "+") 'strike-through)
1782 ((equal marker "~") 'code)
1783 ((equal marker "=") 'verbatim)
1784 (t (error "Unknown marker at %d" (match-beginning 3))))
1785 (match-beginning 2))))))
1788 ;;;; Code
1790 (defun org-element-code-parser ()
1791 "Parse code object at point.
1793 Return a list whose CAR is `code' and CDR is a plist with
1794 `:value', `:begin', `:end' and `:post-blank' keywords.
1796 Assume point is at the first tilde marker."
1797 (save-excursion
1798 (unless (bolp) (backward-char 1))
1799 (looking-at org-emph-re)
1800 (let ((begin (match-beginning 2))
1801 (value (org-match-string-no-properties 4))
1802 (post-blank (progn (goto-char (match-end 2))
1803 (skip-chars-forward " \t")))
1804 (end (point)))
1805 `(code
1806 (:value ,value
1807 :begin ,begin
1808 :end ,end
1809 :post-blank ,post-blank)))))
1811 (defun org-element-code-interpreter (code contents)
1812 "Interpret CODE object as Org syntax.
1813 CONTENTS is nil."
1814 (format "~%s~" (org-element-property :value code)))
1817 ;;;; Entity
1819 (defun org-element-entity-parser ()
1820 "Parse entity at point.
1822 Return a list whose CAR is `entity' and CDR a plist with
1823 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1824 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1825 keywords.
1827 Assume point is at the beginning of the entity."
1828 (save-excursion
1829 (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1830 (let* ((value (org-entity-get (match-string 1)))
1831 (begin (match-beginning 0))
1832 (bracketsp (string= (match-string 2) "{}"))
1833 (post-blank (progn (goto-char (match-end 1))
1834 (when bracketsp (forward-char 2))
1835 (skip-chars-forward " \t")))
1836 (end (point)))
1837 `(entity
1838 (:name ,(car value)
1839 :latex ,(nth 1 value)
1840 :latex-math-p ,(nth 2 value)
1841 :html ,(nth 3 value)
1842 :ascii ,(nth 4 value)
1843 :latin1 ,(nth 5 value)
1844 :utf-8 ,(nth 6 value)
1845 :begin ,begin
1846 :end ,end
1847 :use-brackets-p ,bracketsp
1848 :post-blank ,post-blank)))))
1850 (defun org-element-entity-interpreter (entity contents)
1851 "Interpret ENTITY object as Org syntax.
1852 CONTENTS is nil."
1853 (concat "\\"
1854 (org-element-property :name entity)
1855 (when (org-element-property :use-brackets-p entity) "{}")))
1857 (defun org-element-latex-or-entity-successor (limit)
1858 "Search for the next latex-fragment or entity object.
1860 LIMIT bounds the search.
1862 Return value is a cons cell whose CAR is `entity' or
1863 `latex-fragment' and CDR is beginning position."
1864 (save-excursion
1865 (let ((matchers (plist-get org-format-latex-options :matchers))
1866 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1867 (entity-re
1868 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
1869 (when (re-search-forward
1870 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
1871 matchers "\\|")
1872 "\\|" entity-re)
1873 limit t)
1874 (goto-char (match-beginning 0))
1875 (if (looking-at entity-re)
1876 ;; Determine if it's a real entity or a LaTeX command.
1877 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
1878 (match-beginning 0))
1879 ;; No entity nor command: point is at a LaTeX fragment.
1880 ;; Determine its type to get the correct beginning position.
1881 (cons 'latex-fragment
1882 (catch 'return
1883 (mapc (lambda (e)
1884 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
1885 (throw 'return
1886 (match-beginning
1887 (nth 2 (assoc e org-latex-regexps))))))
1888 matchers)
1889 (point))))))))
1892 ;;;; Export Snippet
1894 (defun org-element-export-snippet-parser ()
1895 "Parse export snippet at point.
1897 Return a list whose CAR is `export-snippet' and CDR a plist with
1898 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1899 keywords.
1901 Assume point is at the beginning of the snippet."
1902 (save-excursion
1903 (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
1904 (let* ((begin (match-beginning 0))
1905 (back-end (org-match-string-no-properties 1))
1906 (value (buffer-substring-no-properties
1907 (point)
1908 (progn (re-search-forward "@@" nil t) (match-beginning 0))))
1909 (post-blank (skip-chars-forward " \t"))
1910 (end (point)))
1911 `(export-snippet
1912 (:back-end ,back-end
1913 :value ,value
1914 :begin ,begin
1915 :end ,end
1916 :post-blank ,post-blank)))))
1918 (defun org-element-export-snippet-interpreter (export-snippet contents)
1919 "Interpret EXPORT-SNIPPET object as Org syntax.
1920 CONTENTS is nil."
1921 (format "@@%s:%s@@"
1922 (org-element-property :back-end export-snippet)
1923 (org-element-property :value export-snippet)))
1925 (defun org-element-export-snippet-successor (limit)
1926 "Search for the next export-snippet object.
1928 LIMIT bounds the search.
1930 Return value is a cons cell whose CAR is `export-snippet' and CDR
1931 its beginning position."
1932 (save-excursion
1933 (let (beg)
1934 (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
1935 (setq beg (match-beginning 0))
1936 (re-search-forward "@@" limit t))
1937 (cons 'export-snippet beg)))))
1940 ;;;; Footnote Reference
1942 (defun org-element-footnote-reference-parser ()
1943 "Parse footnote reference at point.
1945 Return a list whose CAR is `footnote-reference' and CDR a plist
1946 with `:label', `:type', `:inline-definition', `:begin', `:end'
1947 and `:post-blank' as keywords."
1948 (save-excursion
1949 (looking-at org-footnote-re)
1950 (let* ((begin (point))
1951 (label (or (org-match-string-no-properties 2)
1952 (org-match-string-no-properties 3)
1953 (and (match-string 1)
1954 (concat "fn:" (org-match-string-no-properties 1)))))
1955 (type (if (or (not label) (match-string 1)) 'inline 'standard))
1956 (inner-begin (match-end 0))
1957 (inner-end
1958 (let ((count 1))
1959 (forward-char)
1960 (while (and (> count 0) (re-search-forward "[][]" nil t))
1961 (if (equal (match-string 0) "[") (incf count) (decf count)))
1962 (1- (point))))
1963 (post-blank (progn (goto-char (1+ inner-end))
1964 (skip-chars-forward " \t")))
1965 (end (point))
1966 (inline-definition
1967 (and (eq type 'inline)
1968 (org-element-parse-secondary-string
1969 (buffer-substring inner-begin inner-end)
1970 (org-element-restriction 'footnote-reference)))))
1971 `(footnote-reference
1972 (:label ,label
1973 :type ,type
1974 :inline-definition ,inline-definition
1975 :begin ,begin
1976 :end ,end
1977 :post-blank ,post-blank)))))
1979 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
1980 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1981 CONTENTS is nil."
1982 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
1983 (def
1984 (let ((inline-def
1985 (org-element-property :inline-definition footnote-reference)))
1986 (if (not inline-def) ""
1987 (concat ":" (org-element-interpret-data inline-def))))))
1988 (format "[%s]" (concat label def))))
1990 (defun org-element-footnote-reference-successor (limit)
1991 "Search for the next footnote-reference object.
1993 LIMIT bounds the search.
1995 Return value is a cons cell whose CAR is `footnote-reference' and
1996 CDR is beginning position."
1997 (save-excursion
1998 (catch 'exit
1999 (while (re-search-forward org-footnote-re limit t)
2000 (save-excursion
2001 (let ((beg (match-beginning 0))
2002 (count 1))
2003 (backward-char)
2004 (while (re-search-forward "[][]" limit t)
2005 (if (equal (match-string 0) "[") (incf count) (decf count))
2006 (when (zerop count)
2007 (throw 'exit (cons 'footnote-reference beg))))))))))
2010 ;;;; Inline Babel Call
2012 (defun org-element-inline-babel-call-parser ()
2013 "Parse inline babel call at point.
2015 Return a list whose CAR is `inline-babel-call' and CDR a plist
2016 with `:begin', `:end', `:info' and `:post-blank' as keywords.
2018 Assume point is at the beginning of the babel call."
2019 (save-excursion
2020 (unless (bolp) (backward-char))
2021 (looking-at org-babel-inline-lob-one-liner-regexp)
2022 (let ((info (save-match-data (org-babel-lob-get-info)))
2023 (begin (match-end 1))
2024 (post-blank (progn (goto-char (match-end 0))
2025 (skip-chars-forward " \t")))
2026 (end (point)))
2027 `(inline-babel-call
2028 (:begin ,begin
2029 :end ,end
2030 :info ,info
2031 :post-blank ,post-blank)))))
2033 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
2034 "Interpret INLINE-BABEL-CALL object as Org syntax.
2035 CONTENTS is nil."
2036 (let* ((babel-info (org-element-property :info inline-babel-call))
2037 (main-source (car babel-info))
2038 (post-options (nth 1 babel-info)))
2039 (concat "call_"
2040 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
2041 ;; Remove redundant square brackets.
2042 (replace-match
2043 (match-string 1 main-source) nil nil main-source)
2044 main-source)
2045 (and post-options (format "[%s]" post-options)))))
2047 (defun org-element-inline-babel-call-successor (limit)
2048 "Search for the next inline-babel-call object.
2050 LIMIT bounds the search.
2052 Return value is a cons cell whose CAR is `inline-babel-call' and
2053 CDR is beginning position."
2054 (save-excursion
2055 ;; Use a simplified version of
2056 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
2057 (when (re-search-forward
2058 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
2059 limit t)
2060 (cons 'inline-babel-call (match-beginning 0)))))
2063 ;;;; Inline Src Block
2065 (defun org-element-inline-src-block-parser ()
2066 "Parse inline source block at point.
2068 Return a list whose CAR is `inline-src-block' and CDR a plist
2069 with `:begin', `:end', `:language', `:value', `:parameters' and
2070 `:post-blank' as keywords.
2072 Assume point is at the beginning of the inline src block."
2073 (save-excursion
2074 (unless (bolp) (backward-char))
2075 (looking-at org-babel-inline-src-block-regexp)
2076 (let ((begin (match-beginning 1))
2077 (language (org-match-string-no-properties 2))
2078 (parameters (org-match-string-no-properties 4))
2079 (value (org-match-string-no-properties 5))
2080 (post-blank (progn (goto-char (match-end 0))
2081 (skip-chars-forward " \t")))
2082 (end (point)))
2083 `(inline-src-block
2084 (:language ,language
2085 :value ,value
2086 :parameters ,parameters
2087 :begin ,begin
2088 :end ,end
2089 :post-blank ,post-blank)))))
2091 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
2092 "Interpret INLINE-SRC-BLOCK object as Org syntax.
2093 CONTENTS is nil."
2094 (let ((language (org-element-property :language inline-src-block))
2095 (arguments (org-element-property :parameters inline-src-block))
2096 (body (org-element-property :value inline-src-block)))
2097 (format "src_%s%s{%s}"
2098 language
2099 (if arguments (format "[%s]" arguments) "")
2100 body)))
2102 (defun org-element-inline-src-block-successor (limit)
2103 "Search for the next inline-babel-call element.
2105 LIMIT bounds the search.
2107 Return value is a cons cell whose CAR is `inline-babel-call' and
2108 CDR is beginning position."
2109 (save-excursion
2110 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
2111 (cons 'inline-src-block (match-beginning 1)))))
2113 ;;;; Italic
2115 (defun org-element-italic-parser ()
2116 "Parse italic object at point.
2118 Return a list whose CAR is `italic' and CDR is a plist with
2119 `:begin', `:end', `:contents-begin' and `:contents-end' and
2120 `:post-blank' keywords.
2122 Assume point is at the first slash marker."
2123 (save-excursion
2124 (unless (bolp) (backward-char 1))
2125 (looking-at org-emph-re)
2126 (let ((begin (match-beginning 2))
2127 (contents-begin (match-beginning 4))
2128 (contents-end (match-end 4))
2129 (post-blank (progn (goto-char (match-end 2))
2130 (skip-chars-forward " \t")))
2131 (end (point)))
2132 `(italic
2133 (:begin ,begin
2134 :end ,end
2135 :contents-begin ,contents-begin
2136 :contents-end ,contents-end
2137 :post-blank ,post-blank)))))
2139 (defun org-element-italic-interpreter (italic contents)
2140 "Interpret ITALIC object as Org syntax.
2141 CONTENTS is the contents of the object."
2142 (format "/%s/" contents))
2145 ;;;; Latex Fragment
2147 (defun org-element-latex-fragment-parser ()
2148 "Parse latex fragment at point.
2150 Return a list whose CAR is `latex-fragment' and CDR a plist with
2151 `:value', `:begin', `:end', and `:post-blank' as keywords.
2153 Assume point is at the beginning of the latex fragment."
2154 (save-excursion
2155 (let* ((begin (point))
2156 (substring-match
2157 (catch 'exit
2158 (mapc (lambda (e)
2159 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
2160 (when (or (looking-at latex-regexp)
2161 (and (not (bobp))
2162 (save-excursion
2163 (backward-char)
2164 (looking-at latex-regexp))))
2165 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
2166 (plist-get org-format-latex-options :matchers))
2167 ;; None found: it's a macro.
2168 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
2170 (value (match-string-no-properties substring-match))
2171 (post-blank (progn (goto-char (match-end substring-match))
2172 (skip-chars-forward " \t")))
2173 (end (point)))
2174 `(latex-fragment
2175 (:value ,value
2176 :begin ,begin
2177 :end ,end
2178 :post-blank ,post-blank)))))
2180 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
2181 "Interpret LATEX-FRAGMENT object as Org syntax.
2182 CONTENTS is nil."
2183 (org-element-property :value latex-fragment))
2185 ;;;; Line Break
2187 (defun org-element-line-break-parser ()
2188 "Parse line break at point.
2190 Return a list whose CAR is `line-break', and CDR a plist with
2191 `:begin', `:end' and `:post-blank' keywords.
2193 Assume point is at the beginning of the line break."
2194 (let ((begin (point))
2195 (end (save-excursion (forward-line) (point))))
2196 `(line-break (:begin ,begin :end ,end :post-blank 0))))
2198 (defun org-element-line-break-interpreter (line-break contents)
2199 "Interpret LINE-BREAK object as Org syntax.
2200 CONTENTS is nil."
2201 "\\\\\n")
2203 (defun org-element-line-break-successor (limit)
2204 "Search for the next line-break object.
2206 LIMIT bounds the search.
2208 Return value is a cons cell whose CAR is `line-break' and CDR is
2209 beginning position."
2210 (save-excursion
2211 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
2212 (goto-char (match-beginning 1)))))
2213 ;; A line break can only happen on a non-empty line.
2214 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
2215 (cons 'line-break beg)))))
2218 ;;;; Link
2220 (defun org-element-link-parser ()
2221 "Parse link at point.
2223 Return a list whose CAR is `link' and CDR a plist with `:type',
2224 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
2225 `:contents-end' and `:post-blank' as keywords.
2227 Assume point is at the beginning of the link."
2228 (save-excursion
2229 (let ((begin (point))
2230 end contents-begin contents-end link-end post-blank path type
2231 raw-link link)
2232 (cond
2233 ;; Type 1: Text targeted from a radio target.
2234 ((and org-target-link-regexp (looking-at org-target-link-regexp))
2235 (setq type "radio"
2236 link-end (match-end 0)
2237 path (org-match-string-no-properties 0)))
2238 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
2239 ((looking-at org-bracket-link-regexp)
2240 (setq contents-begin (match-beginning 3)
2241 contents-end (match-end 3)
2242 link-end (match-end 0)
2243 ;; RAW-LINK is the original link.
2244 raw-link (org-match-string-no-properties 1)
2245 link (org-translate-link
2246 (org-link-expand-abbrev
2247 (org-link-unescape raw-link))))
2248 ;; Determine TYPE of link and set PATH accordingly.
2249 (cond
2250 ;; File type.
2251 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
2252 (setq type "file" path link))
2253 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
2254 ((string-match org-link-re-with-space3 link)
2255 (setq type (match-string 1 link) path (match-string 2 link)))
2256 ;; Id type: PATH is the id.
2257 ((string-match "^id:\\([-a-f0-9]+\\)" link)
2258 (setq type "id" path (match-string 1 link)))
2259 ;; Code-ref type: PATH is the name of the reference.
2260 ((string-match "^(\\(.*\\))$" link)
2261 (setq type "coderef" path (match-string 1 link)))
2262 ;; Custom-id type: PATH is the name of the custom id.
2263 ((= (aref link 0) ?#)
2264 (setq type "custom-id" path (substring link 1)))
2265 ;; Fuzzy type: Internal link either matches a target, an
2266 ;; headline name or nothing. PATH is the target or
2267 ;; headline's name.
2268 (t (setq type "fuzzy" path link))))
2269 ;; Type 3: Plain link, i.e. http://orgmode.org
2270 ((looking-at org-plain-link-re)
2271 (setq raw-link (org-match-string-no-properties 0)
2272 type (org-match-string-no-properties 1)
2273 path (org-match-string-no-properties 2)
2274 link-end (match-end 0)))
2275 ;; Type 4: Angular link, i.e. <http://orgmode.org>
2276 ((looking-at org-angle-link-re)
2277 (setq raw-link (buffer-substring-no-properties
2278 (match-beginning 1) (match-end 2))
2279 type (org-match-string-no-properties 1)
2280 path (org-match-string-no-properties 2)
2281 link-end (match-end 0))))
2282 ;; In any case, deduce end point after trailing white space from
2283 ;; LINK-END variable.
2284 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
2285 end (point))
2286 `(link
2287 (:type ,type
2288 :path ,path
2289 :raw-link ,(or raw-link path)
2290 :begin ,begin
2291 :end ,end
2292 :contents-begin ,contents-begin
2293 :contents-end ,contents-end
2294 :post-blank ,post-blank)))))
2296 (defun org-element-link-interpreter (link contents)
2297 "Interpret LINK object as Org syntax.
2298 CONTENTS is the contents of the object, or nil."
2299 (let ((type (org-element-property :type link))
2300 (raw-link (org-element-property :raw-link link)))
2301 (if (string= type "radio") raw-link
2302 (format "[[%s]%s]"
2303 raw-link
2304 (if contents (format "[%s]" contents) "")))))
2306 (defun org-element-link-successor (limit)
2307 "Search for the next link object.
2309 LIMIT bounds the search.
2311 Return value is a cons cell whose CAR is `link' and CDR is
2312 beginning position."
2313 (save-excursion
2314 (let ((link-regexp
2315 (if (not org-target-link-regexp) org-any-link-re
2316 (concat org-any-link-re "\\|" org-target-link-regexp))))
2317 (when (re-search-forward link-regexp limit t)
2318 (cons 'link (match-beginning 0))))))
2321 ;;;; Macro
2323 (defun org-element-macro-parser ()
2324 "Parse macro at point.
2326 Return a list whose CAR is `macro' and CDR a plist with `:key',
2327 `:args', `:begin', `:end', `:value' and `:post-blank' as
2328 keywords.
2330 Assume point is at the macro."
2331 (save-excursion
2332 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2333 (let ((begin (point))
2334 (key (downcase (org-match-string-no-properties 1)))
2335 (value (org-match-string-no-properties 0))
2336 (post-blank (progn (goto-char (match-end 0))
2337 (skip-chars-forward " \t")))
2338 (end (point))
2339 (args (let ((args (org-match-string-no-properties 3)) args2)
2340 (when args
2341 (setq args (org-split-string args ","))
2342 (while args
2343 (while (string-match "\\\\\\'" (car args))
2344 ;; Repair bad splits.
2345 (setcar (cdr args) (concat (substring (car args) 0 -1)
2346 "," (nth 1 args)))
2347 (pop args))
2348 (push (pop args) args2))
2349 (mapcar 'org-trim (nreverse args2))))))
2350 `(macro
2351 (:key ,key
2352 :value ,value
2353 :args ,args
2354 :begin ,begin
2355 :end ,end
2356 :post-blank ,post-blank)))))
2358 (defun org-element-macro-interpreter (macro contents)
2359 "Interpret MACRO object as Org syntax.
2360 CONTENTS is nil."
2361 (org-element-property :value macro))
2363 (defun org-element-macro-successor (limit)
2364 "Search for the next macro object.
2366 LIMIT bounds the search.
2368 Return value is cons cell whose CAR is `macro' and CDR is
2369 beginning position."
2370 (save-excursion
2371 (when (re-search-forward
2372 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2373 limit t)
2374 (cons 'macro (match-beginning 0)))))
2377 ;;;; Radio-target
2379 (defun org-element-radio-target-parser ()
2380 "Parse radio target at point.
2382 Return a list whose CAR is `radio-target' and CDR a plist with
2383 `:begin', `:end', `:contents-begin', `:contents-end', `:value'
2384 and `:post-blank' as keywords.
2386 Assume point is at the radio target."
2387 (save-excursion
2388 (looking-at org-radio-target-regexp)
2389 (let ((begin (point))
2390 (contents-begin (match-beginning 1))
2391 (contents-end (match-end 1))
2392 (value (org-match-string-no-properties 1))
2393 (post-blank (progn (goto-char (match-end 0))
2394 (skip-chars-forward " \t")))
2395 (end (point)))
2396 `(radio-target
2397 (:begin ,begin
2398 :end ,end
2399 :contents-begin ,contents-begin
2400 :contents-end ,contents-end
2401 :post-blank ,post-blank
2402 :value ,value)))))
2404 (defun org-element-radio-target-interpreter (target contents)
2405 "Interpret TARGET object as Org syntax.
2406 CONTENTS is the contents of the object."
2407 (concat "<<<" contents ">>>"))
2409 (defun org-element-radio-target-successor (limit)
2410 "Search for the next radio-target object.
2412 LIMIT bounds the search.
2414 Return value is a cons cell whose CAR is `radio-target' and CDR
2415 is beginning position."
2416 (save-excursion
2417 (when (re-search-forward org-radio-target-regexp limit t)
2418 (cons 'radio-target (match-beginning 0)))))
2421 ;;;; Statistics Cookie
2423 (defun org-element-statistics-cookie-parser ()
2424 "Parse statistics cookie at point.
2426 Return a list whose CAR is `statistics-cookie', and CDR a plist
2427 with `:begin', `:end', `:value' and `:post-blank' keywords.
2429 Assume point is at the beginning of the statistics-cookie."
2430 (save-excursion
2431 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2432 (let* ((begin (point))
2433 (value (buffer-substring-no-properties
2434 (match-beginning 0) (match-end 0)))
2435 (post-blank (progn (goto-char (match-end 0))
2436 (skip-chars-forward " \t")))
2437 (end (point)))
2438 `(statistics-cookie
2439 (:begin ,begin
2440 :end ,end
2441 :value ,value
2442 :post-blank ,post-blank)))))
2444 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
2445 "Interpret STATISTICS-COOKIE object as Org syntax.
2446 CONTENTS is nil."
2447 (org-element-property :value statistics-cookie))
2449 (defun org-element-statistics-cookie-successor (limit)
2450 "Search for the next statistics cookie object.
2452 LIMIT bounds the search.
2454 Return value is a cons cell whose CAR is `statistics-cookie' and
2455 CDR is beginning position."
2456 (save-excursion
2457 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
2458 (cons 'statistics-cookie (match-beginning 0)))))
2461 ;;;; Strike-Through
2463 (defun org-element-strike-through-parser ()
2464 "Parse strike-through object at point.
2466 Return a list whose CAR is `strike-through' and CDR is a plist
2467 with `:begin', `:end', `:contents-begin' and `:contents-end' and
2468 `:post-blank' keywords.
2470 Assume point is at the first plus sign marker."
2471 (save-excursion
2472 (unless (bolp) (backward-char 1))
2473 (looking-at org-emph-re)
2474 (let ((begin (match-beginning 2))
2475 (contents-begin (match-beginning 4))
2476 (contents-end (match-end 4))
2477 (post-blank (progn (goto-char (match-end 2))
2478 (skip-chars-forward " \t")))
2479 (end (point)))
2480 `(strike-through
2481 (:begin ,begin
2482 :end ,end
2483 :contents-begin ,contents-begin
2484 :contents-end ,contents-end
2485 :post-blank ,post-blank)))))
2487 (defun org-element-strike-through-interpreter (strike-through contents)
2488 "Interpret STRIKE-THROUGH object as Org syntax.
2489 CONTENTS is the contents of the object."
2490 (format "+%s+" contents))
2493 ;;;; Subscript
2495 (defun org-element-subscript-parser ()
2496 "Parse subscript at point.
2498 Return a list whose CAR is `subscript' and CDR a plist with
2499 `:begin', `:end', `:contents-begin', `:contents-end',
2500 `:use-brackets-p' and `:post-blank' as keywords.
2502 Assume point is at the underscore."
2503 (save-excursion
2504 (unless (bolp) (backward-char))
2505 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
2507 (not (looking-at org-match-substring-regexp))))
2508 (begin (match-beginning 2))
2509 (contents-begin (or (match-beginning 5)
2510 (match-beginning 3)))
2511 (contents-end (or (match-end 5) (match-end 3)))
2512 (post-blank (progn (goto-char (match-end 0))
2513 (skip-chars-forward " \t")))
2514 (end (point)))
2515 `(subscript
2516 (:begin ,begin
2517 :end ,end
2518 :use-brackets-p ,bracketsp
2519 :contents-begin ,contents-begin
2520 :contents-end ,contents-end
2521 :post-blank ,post-blank)))))
2523 (defun org-element-subscript-interpreter (subscript contents)
2524 "Interpret SUBSCRIPT object as Org syntax.
2525 CONTENTS is the contents of the object."
2526 (format
2527 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
2528 contents))
2530 (defun org-element-sub/superscript-successor (limit)
2531 "Search for the next sub/superscript object.
2533 LIMIT bounds the search.
2535 Return value is a cons cell whose CAR is either `subscript' or
2536 `superscript' and CDR is beginning position."
2537 (save-excursion
2538 (when (re-search-forward org-match-substring-regexp limit t)
2539 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
2540 (match-beginning 2)))))
2543 ;;;; Superscript
2545 (defun org-element-superscript-parser ()
2546 "Parse superscript at point.
2548 Return a list whose CAR is `superscript' and CDR a plist with
2549 `:begin', `:end', `:contents-begin', `:contents-end',
2550 `:use-brackets-p' and `:post-blank' as keywords.
2552 Assume point is at the caret."
2553 (save-excursion
2554 (unless (bolp) (backward-char))
2555 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
2556 (not (looking-at org-match-substring-regexp))))
2557 (begin (match-beginning 2))
2558 (contents-begin (or (match-beginning 5)
2559 (match-beginning 3)))
2560 (contents-end (or (match-end 5) (match-end 3)))
2561 (post-blank (progn (goto-char (match-end 0))
2562 (skip-chars-forward " \t")))
2563 (end (point)))
2564 `(superscript
2565 (:begin ,begin
2566 :end ,end
2567 :use-brackets-p ,bracketsp
2568 :contents-begin ,contents-begin
2569 :contents-end ,contents-end
2570 :post-blank ,post-blank)))))
2572 (defun org-element-superscript-interpreter (superscript contents)
2573 "Interpret SUPERSCRIPT object as Org syntax.
2574 CONTENTS is the contents of the object."
2575 (format
2576 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
2577 contents))
2580 ;;;; Table Cell
2582 (defun org-element-table-cell-parser ()
2583 "Parse table cell at point.
2585 Return a list whose CAR is `table-cell' and CDR is a plist
2586 containing `:begin', `:end', `:contents-begin', `:contents-end'
2587 and `:post-blank' keywords."
2588 (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
2589 (let* ((begin (match-beginning 0))
2590 (end (match-end 0))
2591 (contents-begin (match-beginning 1))
2592 (contents-end (match-end 1)))
2593 `(table-cell
2594 (:begin ,begin
2595 :end ,end
2596 :contents-begin ,contents-begin
2597 :contents-end ,contents-end
2598 :post-blank 0))))
2600 (defun org-element-table-cell-interpreter (table-cell contents)
2601 "Interpret TABLE-CELL element as Org syntax.
2602 CONTENTS is the contents of the cell, or nil."
2603 (concat " " contents " |"))
2605 (defun org-element-table-cell-successor (limit)
2606 "Search for the next table-cell object.
2608 LIMIT bounds the search.
2610 Return value is a cons cell whose CAR is `table-cell' and CDR is
2611 beginning position."
2612 (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
2615 ;;;; Target
2617 (defun org-element-target-parser ()
2618 "Parse target at point.
2620 Return a list whose CAR is `target' and CDR a plist with
2621 `:begin', `:end', `:value' and `:post-blank' as keywords.
2623 Assume point is at the target."
2624 (save-excursion
2625 (looking-at org-target-regexp)
2626 (let ((begin (point))
2627 (value (org-match-string-no-properties 1))
2628 (post-blank (progn (goto-char (match-end 0))
2629 (skip-chars-forward " \t")))
2630 (end (point)))
2631 `(target
2632 (:begin ,begin
2633 :end ,end
2634 :value ,value
2635 :post-blank ,post-blank)))))
2637 (defun org-element-target-interpreter (target contents)
2638 "Interpret TARGET object as Org syntax.
2639 CONTENTS is nil."
2640 (format "<<%s>>" (org-element-property :value target)))
2642 (defun org-element-target-successor (limit)
2643 "Search for the next target object.
2645 LIMIT bounds the search.
2647 Return value is a cons cell whose CAR is `target' and CDR is
2648 beginning position."
2649 (save-excursion
2650 (when (re-search-forward org-target-regexp limit t)
2651 (cons 'target (match-beginning 0)))))
2654 ;;;; Timestamp
2656 (defun org-element-timestamp-parser ()
2657 "Parse time stamp at point.
2659 Return a list whose CAR is `timestamp', and CDR a plist with
2660 `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
2662 Assume point is at the beginning of the timestamp."
2663 (save-excursion
2664 (let* ((begin (point))
2665 (type (cond
2666 ((looking-at org-tsr-regexp)
2667 (if (match-string 2) 'active-range 'active))
2668 ((looking-at org-tsr-regexp-both)
2669 (if (match-string 2) 'inactive-range 'inactive))
2670 ((looking-at
2671 (concat
2672 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2673 "\\|"
2674 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2675 'diary)))
2676 (value (org-match-string-no-properties 0))
2677 (post-blank (progn (goto-char (match-end 0))
2678 (skip-chars-forward " \t")))
2679 (end (point)))
2680 `(timestamp
2681 (:type ,type
2682 :value ,value
2683 :begin ,begin
2684 :end ,end
2685 :post-blank ,post-blank)))))
2687 (defun org-element-timestamp-interpreter (timestamp contents)
2688 "Interpret TIMESTAMP object as Org syntax.
2689 CONTENTS is nil."
2690 (org-element-property :value timestamp))
2692 (defun org-element-timestamp-successor (limit)
2693 "Search for the next timestamp object.
2695 LIMIT bounds the search.
2697 Return value is a cons cell whose CAR is `timestamp' and CDR is
2698 beginning position."
2699 (save-excursion
2700 (when (re-search-forward
2701 (concat org-ts-regexp-both
2702 "\\|"
2703 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2704 "\\|"
2705 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2706 limit t)
2707 (cons 'timestamp (match-beginning 0)))))
2710 ;;;; Underline
2712 (defun org-element-underline-parser ()
2713 "Parse underline object at point.
2715 Return a list whose CAR is `underline' and CDR is a plist with
2716 `:begin', `:end', `:contents-begin' and `:contents-end' and
2717 `:post-blank' keywords.
2719 Assume point is at the first underscore marker."
2720 (save-excursion
2721 (unless (bolp) (backward-char 1))
2722 (looking-at org-emph-re)
2723 (let ((begin (match-beginning 2))
2724 (contents-begin (match-beginning 4))
2725 (contents-end (match-end 4))
2726 (post-blank (progn (goto-char (match-end 2))
2727 (skip-chars-forward " \t")))
2728 (end (point)))
2729 `(underline
2730 (:begin ,begin
2731 :end ,end
2732 :contents-begin ,contents-begin
2733 :contents-end ,contents-end
2734 :post-blank ,post-blank)))))
2736 (defun org-element-underline-interpreter (underline contents)
2737 "Interpret UNDERLINE object as Org syntax.
2738 CONTENTS is the contents of the object."
2739 (format "_%s_" contents))
2742 ;;;; Verbatim
2744 (defun org-element-verbatim-parser ()
2745 "Parse verbatim object at point.
2747 Return a list whose CAR is `verbatim' and CDR is a plist with
2748 `:value', `:begin', `:end' and `:post-blank' keywords.
2750 Assume point is at the first equal sign marker."
2751 (save-excursion
2752 (unless (bolp) (backward-char 1))
2753 (looking-at org-emph-re)
2754 (let ((begin (match-beginning 2))
2755 (value (org-match-string-no-properties 4))
2756 (post-blank (progn (goto-char (match-end 2))
2757 (skip-chars-forward " \t")))
2758 (end (point)))
2759 `(verbatim
2760 (:value ,value
2761 :begin ,begin
2762 :end ,end
2763 :post-blank ,post-blank)))))
2765 (defun org-element-verbatim-interpreter (verbatim contents)
2766 "Interpret VERBATIM object as Org syntax.
2767 CONTENTS is nil."
2768 (format "=%s=" (org-element-property :value verbatim)))
2772 ;;; Definitions And Rules
2774 ;; Define elements, greater elements and specify recursive objects,
2775 ;; along with the affiliated keywords recognized. Also set up
2776 ;; restrictions on recursive objects combinations.
2778 ;; These variables really act as a control center for the parsing
2779 ;; process.
2780 (defconst org-element-paragraph-separate
2781 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2782 ;; Headlines and inlinetasks.
2783 org-outline-regexp-bol "\\|"
2784 ;; Comments, blocks (any type), keywords and babel calls.
2785 "^[ \t]*#\\+" "\\|" "^#\\(?: \\|$\\)" "\\|"
2786 ;; Lists.
2787 (org-item-beginning-re) "\\|"
2788 ;; Fixed-width, drawers (any type) and tables.
2789 "^[ \t]*[:|]" "\\|"
2790 ;; Footnote definitions.
2791 org-footnote-definition-re "\\|"
2792 ;; Horizontal rules.
2793 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2794 ;; LaTeX environments.
2795 "^[ \t]*\\\\\\(begin\\|end\\)" "\\|"
2796 ;; Planning and Clock lines.
2797 "^[ \t]*\\(?:"
2798 org-clock-string "\\|"
2799 org-closed-string "\\|"
2800 org-deadline-string "\\|"
2801 org-scheduled-string "\\)")
2802 "Regexp to separate paragraphs in an Org buffer.")
2804 (defconst org-element-all-elements
2805 '(center-block clock comment comment-block drawer dynamic-block example-block
2806 export-block fixed-width footnote-definition headline
2807 horizontal-rule inlinetask item keyword latex-environment
2808 babel-call paragraph plain-list planning property-drawer
2809 quote-block quote-section section special-block src-block table
2810 table-row verse-block)
2811 "Complete list of element types.")
2813 (defconst org-element-greater-elements
2814 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2815 item plain-list quote-block section special-block table)
2816 "List of recursive element types aka Greater Elements.")
2818 (defconst org-element-all-successors
2819 '(export-snippet footnote-reference inline-babel-call inline-src-block
2820 latex-or-entity line-break link macro radio-target
2821 statistics-cookie sub/superscript table-cell target
2822 text-markup timestamp)
2823 "Complete list of successors.")
2825 (defconst org-element-object-successor-alist
2826 '((subscript . sub/superscript) (superscript . sub/superscript)
2827 (bold . text-markup) (code . text-markup) (italic . text-markup)
2828 (strike-through . text-markup) (underline . text-markup)
2829 (verbatim . text-markup) (entity . latex-or-entity)
2830 (latex-fragment . latex-or-entity))
2831 "Alist of translations between object type and successor name.
2833 Sharing the same successor comes handy when, for example, the
2834 regexp matching one object can also match the other object.")
2836 (defconst org-element-all-objects
2837 '(bold code entity export-snippet footnote-reference inline-babel-call
2838 inline-src-block italic line-break latex-fragment link macro
2839 radio-target statistics-cookie strike-through subscript superscript
2840 table-cell target timestamp underline verbatim)
2841 "Complete list of object types.")
2843 (defconst org-element-recursive-objects
2844 '(bold italic link macro subscript radio-target strike-through superscript
2845 table-cell underline)
2846 "List of recursive object types.")
2848 (defconst org-element-block-name-alist
2849 '(("ASCII" . org-element-export-block-parser)
2850 ("CENTER" . org-element-center-block-parser)
2851 ("COMMENT" . org-element-comment-block-parser)
2852 ("DOCBOOK" . org-element-export-block-parser)
2853 ("EXAMPLE" . org-element-example-block-parser)
2854 ("HTML" . org-element-export-block-parser)
2855 ("LATEX" . org-element-export-block-parser)
2856 ("ODT" . org-element-export-block-parser)
2857 ("QUOTE" . org-element-quote-block-parser)
2858 ("SRC" . org-element-src-block-parser)
2859 ("VERSE" . org-element-verse-block-parser))
2860 "Alist between block names and the associated parsing function.
2861 Names must be uppercase. Any block whose name has no association
2862 is parsed with `org-element-special-block-parser'.")
2864 (defconst org-element-affiliated-keywords
2865 '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
2866 "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
2867 "List of affiliated keywords as strings.
2868 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
2869 are affiliated keywords and need not to be in this list.")
2871 (defconst org-element-keyword-translation-alist
2872 '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
2873 ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
2874 ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
2875 "Alist of usual translations for keywords.
2876 The key is the old name and the value the new one. The property
2877 holding their value will be named after the translated name.")
2879 (defconst org-element-multiple-keywords '("HEADER")
2880 "List of affiliated keywords that can occur more that once in an element.
2882 Their value will be consed into a list of strings, which will be
2883 returned as the value of the property.
2885 This list is checked after translations have been applied. See
2886 `org-element-keyword-translation-alist'.
2888 By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
2889 allow multiple occurrences and need not to be in this list.")
2891 (defconst org-element-parsed-keywords '("AUTHOR" "CAPTION" "DATE" "TITLE")
2892 "List of keywords whose value can be parsed.
2894 Their value will be stored as a secondary string: a list of
2895 strings and objects.
2897 This list is checked after translations have been applied. See
2898 `org-element-keyword-translation-alist'.")
2900 (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
2901 "List of keywords which can have a secondary value.
2903 In Org syntax, they can be written with optional square brackets
2904 before the colons. For example, results keyword can be
2905 associated to a hash value with the following:
2907 #+RESULTS[hash-string]: some-source
2909 This list is checked after translations have been applied. See
2910 `org-element-keyword-translation-alist'.")
2912 (defconst org-element-object-restrictions
2913 `((bold entity export-snippet inline-babel-call inline-src-block link
2914 radio-target sub/superscript target text-markup timestamp)
2915 (footnote-reference entity export-snippet footnote-reference
2916 inline-babel-call inline-src-block latex-fragment
2917 line-break link macro radio-target sub/superscript
2918 target text-markup timestamp)
2919 (headline entity inline-babel-call inline-src-block latex-fragment link
2920 macro radio-target statistics-cookie sub/superscript target
2921 text-markup timestamp)
2922 (inlinetask entity inline-babel-call inline-src-block latex-fragment link
2923 macro radio-target sub/superscript target text-markup timestamp)
2924 (italic entity export-snippet inline-babel-call inline-src-block link
2925 radio-target sub/superscript target text-markup timestamp)
2926 (item entity footnote-reference inline-babel-call latex-fragment macro
2927 radio-target sub/superscript target text-markup)
2928 (keyword entity latex-fragment macro sub/superscript text-markup)
2929 (link entity export-snippet inline-babel-call inline-src-block
2930 latex-fragment link sub/superscript text-markup)
2931 (macro macro)
2932 (paragraph ,@org-element-all-successors)
2933 (radio-target entity export-snippet latex-fragment sub/superscript)
2934 (strike-through entity export-snippet inline-babel-call inline-src-block
2935 link radio-target sub/superscript target text-markup
2936 timestamp)
2937 (subscript entity export-snippet inline-babel-call inline-src-block
2938 latex-fragment sub/superscript target text-markup)
2939 (superscript entity export-snippet inline-babel-call inline-src-block
2940 latex-fragment sub/superscript target text-markup)
2941 (table-cell entity export-snippet latex-fragment link macro radio-target
2942 sub/superscript target text-markup timestamp)
2943 (table-row table-cell)
2944 (underline entity export-snippet inline-babel-call inline-src-block link
2945 radio-target sub/superscript target text-markup timestamp)
2946 (verse-block entity footnote-reference inline-babel-call inline-src-block
2947 latex-fragment line-break link macro radio-target
2948 sub/superscript target text-markup timestamp))
2949 "Alist of objects restrictions.
2951 CAR is an element or object type containing objects and CDR is
2952 a list of successors that will be called within an element or
2953 object of such type.
2955 For example, in a `radio-target' object, one can only find
2956 entities, export snippets, latex-fragments, subscript and
2957 superscript.
2959 This alist also applies to secondary string. For example, an
2960 `headline' type element doesn't directly contain objects, but
2961 still has an entry since one of its properties (`:title') does.")
2963 (defconst org-element-secondary-value-alist
2964 '((headline . :title)
2965 (inlinetask . :title)
2966 (item . :tag)
2967 (footnote-reference . :inline-definition))
2968 "Alist between element types and location of secondary value.")
2972 ;;; Accessors
2974 ;; Provide four accessors: `org-element-type', `org-element-property'
2975 ;; `org-element-contents' and `org-element-restriction'.
2977 (defun org-element-type (element)
2978 "Return type of element ELEMENT.
2980 The function returns the type of the element or object provided.
2981 It can also return the following special value:
2982 `plain-text' for a string
2983 `org-data' for a complete document
2984 nil in any other case."
2985 (cond
2986 ((not (consp element)) (and (stringp element) 'plain-text))
2987 ((symbolp (car element)) (car element))))
2989 (defun org-element-property (property element)
2990 "Extract the value from the PROPERTY of an ELEMENT."
2991 (plist-get (nth 1 element) property))
2993 (defun org-element-contents (element)
2994 "Extract contents from an ELEMENT."
2995 (and (consp element) (nthcdr 2 element)))
2997 (defun org-element-restriction (element)
2998 "Return restriction associated to ELEMENT.
2999 ELEMENT can be an element, an object or a symbol representing an
3000 element or object type."
3001 (cdr (assq (if (symbolp element) element (org-element-type element))
3002 org-element-object-restrictions)))
3006 ;;; Parsing Element Starting At Point
3008 ;; `org-element-current-element' is the core function of this section.
3009 ;; It returns the Lisp representation of the element starting at
3010 ;; point.
3012 ;; `org-element-current-element' makes use of special modes. They are
3013 ;; activated for fixed element chaining (i.e. `plain-list' > `item')
3014 ;; or fixed conditional element chaining (i.e. `headline' >
3015 ;; `section'). Special modes are: `section', `quote-section', `item'
3016 ;; and `table-row'.
3018 (defun org-element-current-element (&optional granularity special structure)
3019 "Parse the element starting at point.
3021 Return value is a list like (TYPE PROPS) where TYPE is the type
3022 of the element and PROPS a plist of properties associated to the
3023 element.
3025 Possible types are defined in `org-element-all-elements'.
3027 Optional argument GRANULARITY determines the depth of the
3028 recursion. Allowed values are `headline', `greater-element',
3029 `element', `object' or nil. When it is broader than `object' (or
3030 nil), secondary values will not be parsed, since they only
3031 contain objects.
3033 Optional argument SPECIAL, when non-nil, can be either `section',
3034 `quote-section', `table-row' and `item'.
3036 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3037 be computed.
3039 This function assumes point is always at the beginning of the
3040 element it has to parse."
3041 (save-excursion
3042 ;; If point is at an affiliated keyword, try moving to the
3043 ;; beginning of the associated element. If none is found, the
3044 ;; keyword is orphaned and will be treated as plain text.
3045 (when (looking-at org-element--affiliated-re)
3046 (let ((opoint (point)))
3047 (while (looking-at org-element--affiliated-re) (forward-line))
3048 (when (looking-at "[ \t]*$") (goto-char opoint))))
3049 (let ((case-fold-search t)
3050 ;; Determine if parsing depth allows for secondary strings
3051 ;; parsing. It only applies to elements referenced in
3052 ;; `org-element-secondary-value-alist'.
3053 (raw-secondary-p (and granularity (not (eq granularity 'object)))))
3054 (cond
3055 ;; Item.
3056 ((eq special 'item)
3057 (org-element-item-parser (or structure (org-list-struct))
3058 raw-secondary-p))
3059 ;; Quote Section.
3060 ((eq special 'quote-section) (org-element-quote-section-parser))
3061 ;; Table Row.
3062 ((eq special 'table-row) (org-element-table-row-parser))
3063 ;; Headline.
3064 ((org-with-limited-levels (org-at-heading-p))
3065 (org-element-headline-parser raw-secondary-p))
3066 ;; Section (must be checked after headline).
3067 ((eq special 'section) (org-element-section-parser))
3068 ;; Planning and Clock.
3069 ((and (looking-at org-planning-or-clock-line-re))
3070 (if (equal (match-string 1) org-clock-string)
3071 (org-element-clock-parser)
3072 (org-element-planning-parser)))
3073 ;; Blocks.
3074 ((when (looking-at "[ \t]*#\\+BEGIN_\\([-A-Za-z0-9]+\\)\\(?: \\|$\\)")
3075 (let ((name (upcase (match-string 1))) parser)
3076 (cond
3077 ((not (save-excursion
3078 (re-search-forward
3079 (format "^[ \t]*#\\+END_%s\\(?: \\|$\\)" name) nil t)))
3080 (org-element-paragraph-parser))
3081 ((setq parser (assoc name org-element-block-name-alist))
3082 (funcall (cdr parser)))
3083 (t (org-element-special-block-parser))))))
3084 ;; Inlinetask.
3085 ((org-at-heading-p) (org-element-inlinetask-parser raw-secondary-p))
3086 ;; LaTeX Environment.
3087 ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
3088 (if (save-excursion
3089 (re-search-forward
3090 (format "[ \t]*\\\\end{%s}[ \t]*"
3091 (regexp-quote (match-string 1)))
3092 nil t))
3093 (org-element-latex-environment-parser)
3094 (org-element-paragraph-parser)))
3095 ;; Drawer and Property Drawer.
3096 ((looking-at org-drawer-regexp)
3097 (let ((name (match-string 1)))
3098 (cond
3099 ((not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
3100 (org-element-paragraph-parser))
3101 ((equal "PROPERTIES" name) (org-element-property-drawer-parser))
3102 (t (org-element-drawer-parser)))))
3103 ;; Fixed Width
3104 ((looking-at "[ \t]*:\\( \\|$\\)") (org-element-fixed-width-parser))
3105 ;; Babel Call, Dynamic Block and Keyword.
3106 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
3107 (let ((key (upcase (match-string 1))))
3108 (cond
3109 ((equal key "CALL") (org-element-babel-call-parser))
3110 ((and (equal key "BEGIN")
3111 (save-excursion
3112 (re-search-forward "^[ \t]*#\\+END:\\(?: \\|$\\)" nil t)))
3113 (org-element-dynamic-block-parser))
3114 ((and (not (equal key "TBLFM"))
3115 (not (member key org-element-affiliated-keywords)))
3116 (org-element-keyword-parser))
3117 (t (org-element-paragraph-parser)))))
3118 ;; Footnote Definition.
3119 ((looking-at org-footnote-definition-re)
3120 (org-element-footnote-definition-parser))
3121 ;; Comment.
3122 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)")
3123 (org-element-comment-parser))
3124 ;; Horizontal Rule.
3125 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3126 (org-element-horizontal-rule-parser))
3127 ;; Table.
3128 ((org-at-table-p t) (org-element-table-parser))
3129 ;; List.
3130 ((looking-at (org-item-re))
3131 (org-element-plain-list-parser (or structure (org-list-struct))))
3132 ;; Default element: Paragraph.
3133 (t (org-element-paragraph-parser))))))
3136 ;; Most elements can have affiliated keywords. When looking for an
3137 ;; element beginning, we want to move before them, as they belong to
3138 ;; that element, and, in the meantime, collect information they give
3139 ;; into appropriate properties. Hence the following function.
3141 ;; Usage of optional arguments may not be obvious at first glance:
3143 ;; - TRANS-LIST is used to polish keywords names that have evolved
3144 ;; during Org history. In example, even though =result= and
3145 ;; =results= coexist, we want to have them under the same =result=
3146 ;; property. It's also true for "srcname" and "name", where the
3147 ;; latter seems to be preferred nowadays (thus the "name" property).
3149 ;; - CONSED allows to regroup multi-lines keywords under the same
3150 ;; property, while preserving their own identity. This is mostly
3151 ;; used for "attr_latex" and al.
3153 ;; - PARSED prepares a keyword value for export. This is useful for
3154 ;; "caption". Objects restrictions for such keywords are defined in
3155 ;; `org-element-object-restrictions'.
3157 ;; - DUALS is used to take care of keywords accepting a main and an
3158 ;; optional secondary values. For example "results" has its
3159 ;; source's name as the main value, and may have an hash string in
3160 ;; optional square brackets as the secondary one.
3162 ;; A keyword may belong to more than one category.
3164 (defconst org-element--affiliated-re
3165 (format "[ \t]*#\\+%s:"
3166 ;; Regular affiliated keywords.
3167 (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
3168 (regexp-opt org-element-affiliated-keywords)))
3169 "Regexp matching any affiliated keyword.
3171 Keyword name is put in match group 1. Moreover, if keyword
3172 belongs to `org-element-dual-keywords', put the dual value in
3173 match group 2.
3175 Don't modify it, set `org-element-affiliated-keywords' instead.")
3177 (defun org-element-collect-affiliated-keywords
3178 (&optional key-re trans-list consed parsed duals)
3179 "Collect affiliated keywords before point.
3181 Optional argument KEY-RE is a regexp matching keywords, which
3182 puts matched keyword in group 1. It defaults to
3183 `org-element--affiliated-re'.
3185 TRANS-LIST is an alist where key is the keyword and value the
3186 property name it should be translated to, without the colons. It
3187 defaults to `org-element-keyword-translation-alist'.
3189 CONSED is a list of strings. Any keyword belonging to that list
3190 will have its value consed. The check is done after keyword
3191 translation. It defaults to `org-element-multiple-keywords'.
3193 PARSED is a list of strings. Any keyword member of this list
3194 will have its value parsed. The check is done after keyword
3195 translation. If a keyword is a member of both CONSED and PARSED,
3196 it's value will be a list of parsed strings. It defaults to
3197 `org-element-parsed-keywords'.
3199 DUALS is a list of strings. Any keyword member of this list can
3200 have two parts: one mandatory and one optional. Its value is
3201 a cons cell whose CAR is the former, and the CDR the latter. If
3202 a keyword is a member of both PARSED and DUALS, both values will
3203 be parsed. It defaults to `org-element-dual-keywords'.
3205 Return a list whose CAR is the position at the first of them and
3206 CDR a plist of keywords and values."
3207 (save-excursion
3208 (let ((case-fold-search t)
3209 (key-re (or key-re org-element--affiliated-re))
3210 (trans-list (or trans-list org-element-keyword-translation-alist))
3211 (consed (or consed org-element-multiple-keywords))
3212 (parsed (or parsed org-element-parsed-keywords))
3213 (duals (or duals org-element-dual-keywords))
3214 ;; RESTRICT is the list of objects allowed in parsed
3215 ;; keywords value.
3216 (restrict (org-element-restriction 'keyword))
3217 output)
3218 (unless (bobp)
3219 (while (and (not (bobp)) (progn (forward-line -1) (looking-at key-re)))
3220 (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
3221 ;; Apply translation to RAW-KWD. From there, KWD is
3222 ;; the official keyword.
3223 (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
3224 ;; Find main value for any keyword.
3225 (value
3226 (save-match-data
3227 (org-trim
3228 (buffer-substring-no-properties
3229 (match-end 0) (point-at-eol)))))
3230 ;; If KWD is a dual keyword, find its secondary
3231 ;; value. Maybe parse it.
3232 (dual-value
3233 (and (member kwd duals)
3234 (let ((sec (org-match-string-no-properties 3)))
3235 (if (or (not sec) (not (member kwd parsed))) sec
3236 (org-element-parse-secondary-string sec restrict)))))
3237 ;; Attribute a property name to KWD.
3238 (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
3239 ;; Now set final shape for VALUE.
3240 (when (member kwd parsed)
3241 (setq value (org-element-parse-secondary-string value restrict)))
3242 (when (member kwd duals)
3243 ;; VALUE is mandatory. Set it to nil if there is none.
3244 (setq value (and value (cons value dual-value))))
3245 ;; Attributes are always consed.
3246 (when (or (member kwd consed) (string-match "^ATTR_" kwd))
3247 (setq value (cons value (plist-get output kwd-sym))))
3248 ;; Eventually store the new value in OUTPUT.
3249 (setq output (plist-put output kwd-sym value))))
3250 (unless (looking-at key-re) (forward-line 1)))
3251 (list (point) output))))
3255 ;;; The Org Parser
3257 ;; The two major functions here are `org-element-parse-buffer', which
3258 ;; parses Org syntax inside the current buffer, taking into account
3259 ;; region, narrowing, or even visibility if specified, and
3260 ;; `org-element-parse-secondary-string', which parses objects within
3261 ;; a given string.
3263 ;; The (almost) almighty `org-element-map' allows to apply a function
3264 ;; on elements or objects matching some type, and accumulate the
3265 ;; resulting values. In an export situation, it also skips unneeded
3266 ;; parts of the parse tree.
3268 (defun org-element-parse-buffer (&optional granularity visible-only)
3269 "Recursively parse the buffer and return structure.
3270 If narrowing is in effect, only parse the visible part of the
3271 buffer.
3273 Optional argument GRANULARITY determines the depth of the
3274 recursion. It can be set to the following symbols:
3276 `headline' Only parse headlines.
3277 `greater-element' Don't recurse into greater elements excepted
3278 headlines and sections. Thus, elements
3279 parsed are the top-level ones.
3280 `element' Parse everything but objects and plain text.
3281 `object' Parse the complete buffer (default).
3283 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3284 elements.
3286 Assume buffer is in Org mode."
3287 (save-excursion
3288 (goto-char (point-min))
3289 (org-skip-whitespace)
3290 (org-element-parse-elements
3291 (point-at-bol) (point-max)
3292 ;; Start in `section' mode so text before the first
3293 ;; headline belongs to a section.
3294 'section nil granularity visible-only (list 'org-data nil))))
3296 (defun org-element-parse-secondary-string (string restriction)
3297 "Recursively parse objects in STRING and return structure.
3299 RESTRICTION, when non-nil, is a symbol limiting the object types
3300 that will be looked after."
3301 (with-temp-buffer
3302 (insert string)
3303 (org-element-parse-objects (point-min) (point-max) nil restriction)))
3305 (defun org-element-map (data types fun &optional info first-match no-recursion)
3306 "Map a function on selected elements or objects.
3308 DATA is the parsed tree, as returned by, i.e,
3309 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3310 of elements or objects types. FUN is the function called on the
3311 matching element or object. It must accept one arguments: the
3312 element or object itself.
3314 When optional argument INFO is non-nil, it should be a plist
3315 holding export options. In that case, parts of the parse tree
3316 not exportable according to that property list will be skipped.
3318 When optional argument FIRST-MATCH is non-nil, stop at the first
3319 match for which FUN doesn't return nil, and return that value.
3321 Optional argument NO-RECURSION is a symbol or a list of symbols
3322 representing elements or objects types. `org-element-map' won't
3323 enter any recursive element or object whose type belongs to that
3324 list. Though, FUN can still be applied on them.
3326 Nil values returned from FUN do not appear in the results."
3327 ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
3328 (unless (listp types) (setq types (list types)))
3329 (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
3330 ;; Recursion depth is determined by --CATEGORY.
3331 (let* ((--category
3332 (catch 'found
3333 (let ((category 'greater-elements))
3334 (mapc (lambda (type)
3335 (cond ((or (memq type org-element-all-objects)
3336 (eq type 'plain-text))
3337 ;; If one object is found, the function
3338 ;; has to recurse into every object.
3339 (throw 'found 'objects))
3340 ((not (memq type org-element-greater-elements))
3341 ;; If one regular element is found, the
3342 ;; function has to recurse, at lest, into
3343 ;; every element it encounters.
3344 (and (not (eq category 'elements))
3345 (setq category 'elements)))))
3346 types)
3347 category)))
3348 --acc
3349 --walk-tree
3350 (--walk-tree
3351 (function
3352 (lambda (--data)
3353 ;; Recursively walk DATA. INFO, if non-nil, is a plist
3354 ;; holding contextual information.
3355 (let ((--type (org-element-type --data)))
3356 (cond
3357 ((not --data))
3358 ;; Ignored element in an export context.
3359 ((and info (member --data (plist-get info :ignore-list))))
3360 ;; Secondary string: only objects can be found there.
3361 ((not --type)
3362 (when (eq --category 'objects) (mapc --walk-tree --data)))
3363 ;; Unconditionally enter parse trees.
3364 ((eq --type 'org-data)
3365 (mapc --walk-tree (org-element-contents --data)))
3367 ;; Check if TYPE is matching among TYPES. If so,
3368 ;; apply FUN to --DATA and accumulate return value
3369 ;; into --ACC (or exit if FIRST-MATCH is non-nil).
3370 (when (memq --type types)
3371 (let ((result (funcall fun --data)))
3372 (cond ((not result))
3373 (first-match (throw 'first-match result))
3374 (t (push result --acc)))))
3375 ;; If --DATA has a secondary string that can contain
3376 ;; objects with their type among TYPES, look into it.
3377 (when (eq --category 'objects)
3378 (let ((sec-prop
3379 (assq --type org-element-secondary-value-alist)))
3380 (when sec-prop
3381 (funcall --walk-tree
3382 (org-element-property (cdr sec-prop) --data)))))
3383 ;; Determine if a recursion into --DATA is possible.
3384 (cond
3385 ;; --TYPE is explicitly removed from recursion.
3386 ((memq --type no-recursion))
3387 ;; --DATA has no contents.
3388 ((not (org-element-contents --data)))
3389 ;; Looking for greater elements but --DATA is simply
3390 ;; an element or an object.
3391 ((and (eq --category 'greater-elements)
3392 (not (memq --type org-element-greater-elements))))
3393 ;; Looking for elements but --DATA is an object.
3394 ((and (eq --category 'elements)
3395 (memq --type org-element-all-objects)))
3396 ;; In any other case, map contents.
3397 (t (mapc --walk-tree (org-element-contents --data)))))))))))
3398 (catch 'first-match
3399 (funcall --walk-tree data)
3400 ;; Return value in a proper order.
3401 (nreverse --acc))))
3403 ;; The following functions are internal parts of the parser.
3405 ;; The first one, `org-element-parse-elements' acts at the element's
3406 ;; level.
3408 ;; The second one, `org-element-parse-objects' applies on all objects
3409 ;; of a paragraph or a secondary string. It uses
3410 ;; `org-element-get-candidates' to optimize the search of the next
3411 ;; object in the buffer.
3413 ;; More precisely, that function looks for every allowed object type
3414 ;; first. Then, it discards failed searches, keeps further matches,
3415 ;; and searches again types matched behind point, for subsequent
3416 ;; calls. Thus, searching for a given type fails only once, and every
3417 ;; object is searched only once at top level (but sometimes more for
3418 ;; nested types).
3420 (defun org-element-parse-elements
3421 (beg end special structure granularity visible-only acc)
3422 "Parse elements between BEG and END positions.
3424 SPECIAL prioritize some elements over the others. It can be set
3425 to `quote-section', `section' `item' or `table-row'.
3427 When value is `item', STRUCTURE will be used as the current list
3428 structure.
3430 GRANULARITY determines the depth of the recursion. See
3431 `org-element-parse-buffer' for more information.
3433 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3434 elements.
3436 Elements are accumulated into ACC."
3437 (save-excursion
3438 (save-restriction
3439 (narrow-to-region beg end)
3440 (goto-char beg)
3441 ;; When parsing only headlines, skip any text before first one.
3442 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
3443 (org-with-limited-levels (outline-next-heading)))
3444 ;; Main loop start.
3445 (while (not (eobp))
3446 ;; Find current element's type and parse it accordingly to
3447 ;; its category.
3448 (let* ((element (org-element-current-element
3449 granularity special structure))
3450 (type (org-element-type element))
3451 (cbeg (org-element-property :contents-begin element)))
3452 ;; Set ACC as parent of current element. It will be
3453 ;; completed by side-effect. If the element contains any
3454 ;; secondary string, also set `:parent' property of every
3455 ;; object within it as current element.
3456 (plist-put (nth 1 element) :parent acc)
3457 (let ((sec-loc (assq type org-element-secondary-value-alist)))
3458 (when sec-loc
3459 (let ((sec-value (org-element-property (cdr sec-loc) element)))
3460 (unless (stringp sec-value)
3461 (mapc (lambda (obj)
3462 (unless (stringp obj)
3463 (plist-put (nth 1 obj) :parent element)))
3464 sec-value)))))
3465 (goto-char (org-element-property :end element))
3466 (nconc
3468 (list
3469 (cond
3470 ;; Case 1. Simply accumulate element if VISIBLE-ONLY is
3471 ;; true and element is hidden or if it has no contents
3472 ;; anyway.
3473 ((or (and visible-only (org-element-property :hiddenp element))
3474 (not cbeg)) element)
3475 ;; Case 2. Greater element: parse it between
3476 ;; `contents-begin' and `contents-end'. Make sure
3477 ;; GRANULARITY allows the recursion, or ELEMENT is an
3478 ;; headline, in which case going inside is mandatory, in
3479 ;; order to get sub-level headings.
3480 ((and (memq type org-element-greater-elements)
3481 (or (memq granularity '(element object nil))
3482 (and (eq granularity 'greater-element)
3483 (eq type 'section))
3484 (eq type 'headline)))
3485 (org-element-parse-elements
3486 cbeg (org-element-property :contents-end element)
3487 ;; Possibly switch to a special mode.
3488 (case type
3489 (headline
3490 (if (org-element-property :quotedp element) 'quote-section
3491 'section))
3492 (plain-list 'item)
3493 (table 'table-row))
3494 (org-element-property :structure element)
3495 granularity visible-only element))
3496 ;; Case 3. ELEMENT has contents. Parse objects inside,
3497 ;; if GRANULARITY allows it.
3498 ((and cbeg (memq granularity '(object nil)))
3499 (org-element-parse-objects
3500 cbeg (org-element-property :contents-end element)
3501 element (org-element-restriction type)))
3502 ;; Case 4. Else, just accumulate ELEMENT.
3503 (t element)))))))
3504 ;; Return result.
3505 acc))
3507 (defun org-element-parse-objects (beg end acc restriction)
3508 "Parse objects between BEG and END and return recursive structure.
3510 Objects are accumulated in ACC.
3512 RESTRICTION is a list of object types which are allowed in the
3513 current object."
3514 (let ((get-next-object
3515 (function
3516 (lambda (cand)
3517 ;; Return the parsing function associated to the nearest
3518 ;; object among list of candidates CAND.
3519 (let ((pos (apply 'min (mapcar 'cdr cand))))
3520 (save-excursion
3521 (goto-char pos)
3522 (funcall
3523 (intern
3524 (format "org-element-%s-parser" (car (rassq pos cand))))))))))
3525 next-object candidates)
3526 (save-excursion
3527 (goto-char beg)
3528 (while (setq candidates (org-element-get-next-object-candidates
3529 end restriction candidates))
3530 (setq next-object (funcall get-next-object candidates))
3531 ;; Set ACC as parent of current element. It will be completed
3532 ;; by side-effect.
3533 (plist-put (nth 1 next-object) :parent acc)
3534 ;; 1. Text before any object. Untabify it.
3535 (let ((obj-beg (org-element-property :begin next-object)))
3536 (unless (= (point) obj-beg)
3537 (let ((beg-text
3538 (list
3539 (replace-regexp-in-string
3540 "\t" (make-string tab-width ? )
3541 (buffer-substring-no-properties (point) obj-beg)))))
3542 (if acc (nconc acc beg-text) (setq acc beg-text)))))
3543 ;; 2. Object...
3544 (let* ((obj-end (org-element-property :end next-object))
3545 (cont-beg (org-element-property :contents-begin next-object))
3546 (complete-next-object
3547 (if (and (memq (car next-object) org-element-recursive-objects)
3548 cont-beg)
3549 ;; ... recursive. The CONT-BEG check is for
3550 ;; links, as some of them might not be recursive
3551 ;; (i.e. plain links).
3552 (save-restriction
3553 (narrow-to-region
3554 cont-beg
3555 (org-element-property :contents-end next-object))
3556 (org-element-parse-objects
3557 (point-min) (point-max) next-object
3558 ;; Restrict allowed objects.
3559 (org-element-restriction next-object)))
3560 next-object)))
3561 (if acc (nconc acc (list complete-next-object))
3562 (setq acc (list complete-next-object)))
3563 ;; If the object contains any secondary string, also set
3564 ;; `:parent' property of every object within it as current
3565 ;; object.
3566 (let ((sec-loc (assq (org-element-type next-object)
3567 org-element-secondary-value-alist)))
3568 (when sec-loc
3569 (let ((sec-value
3570 (org-element-property (cdr sec-loc) next-object)))
3571 (unless (stringp sec-value)
3572 (mapc (lambda (obj)
3573 (unless (stringp obj)
3574 (plist-put (nth 1 obj)
3575 :parent
3576 complete-next-object)))
3577 sec-value)))))
3578 (goto-char obj-end)))
3579 ;; 3. Text after last object. Untabify it.
3580 (unless (= (point) end)
3581 (let ((end-text
3582 (list
3583 (replace-regexp-in-string
3584 "\t" (make-string tab-width ? )
3585 (buffer-substring-no-properties (point) end)))))
3586 (if acc (nconc acc end-text) (setq acc end-text))))
3587 ;; Result.
3588 acc)))
3590 (defun org-element-get-next-object-candidates (limit restriction objects)
3591 "Return an alist of candidates for the next object.
3593 LIMIT bounds the search, and RESTRICTION narrows candidates to
3594 some object types.
3596 Return value is an alist whose CAR is position and CDR the object
3597 type, as a symbol.
3599 OBJECTS is the previous candidates alist."
3600 (let (next-candidates types-to-search)
3601 ;; If no previous result, search every object type in RESTRICTION.
3602 ;; Otherwise, keep potential candidates (old objects located after
3603 ;; point) and ask to search again those which had matched before.
3604 (if (not objects) (setq types-to-search restriction)
3605 (mapc (lambda (obj)
3606 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
3607 (push obj next-candidates)))
3608 objects))
3609 ;; Call the appropriate successor function for each type to search
3610 ;; and accumulate matches.
3611 (mapc
3612 (lambda (type)
3613 (let* ((successor-fun
3614 (intern
3615 (format "org-element-%s-successor"
3616 (or (cdr (assq type org-element-object-successor-alist))
3617 type))))
3618 (obj (funcall successor-fun limit)))
3619 (and obj (push obj next-candidates))))
3620 types-to-search)
3621 ;; Return alist.
3622 next-candidates))
3626 ;;; Towards A Bijective Process
3628 ;; The parse tree obtained with `org-element-parse-buffer' is really
3629 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3630 ;; interpreted and expanded into a string with canonical Org syntax.
3631 ;; Hence `org-element-interpret-data'.
3633 ;; The function relies internally on
3634 ;; `org-element-interpret--affiliated-keywords'.
3636 (defun org-element-interpret-data (data &optional parent)
3637 "Interpret DATA as Org syntax.
3639 DATA is a parse tree, an element, an object or a secondary string
3640 to interpret.
3642 Optional argument PARENT is used for recursive calls. It contains
3643 the element or object containing data, or nil.
3645 Return Org syntax as a string."
3646 (let* ((type (org-element-type data))
3647 (results
3648 (cond
3649 ;; Secondary string.
3650 ((not type)
3651 (mapconcat
3652 (lambda (obj) (org-element-interpret-data obj parent))
3653 data ""))
3654 ;; Full Org document.
3655 ((eq type 'org-data)
3656 (mapconcat
3657 (lambda (obj) (org-element-interpret-data obj parent))
3658 (org-element-contents data) ""))
3659 ;; Plain text.
3660 ((stringp data) data)
3661 ;; Element/Object without contents.
3662 ((not (org-element-contents data))
3663 (funcall (intern (format "org-element-%s-interpreter" type))
3664 data nil))
3665 ;; Element/Object with contents.
3667 (let* ((greaterp (memq type org-element-greater-elements))
3668 (objectp (and (not greaterp)
3669 (memq type org-element-recursive-objects)))
3670 (contents
3671 (mapconcat
3672 (lambda (obj) (org-element-interpret-data obj data))
3673 (org-element-contents
3674 (if (or greaterp objectp) data
3675 ;; Elements directly containing objects must
3676 ;; have their indentation normalized first.
3677 (org-element-normalize-contents
3678 data
3679 ;; When normalizing first paragraph of an
3680 ;; item or a footnote-definition, ignore
3681 ;; first line's indentation.
3682 (and (eq type 'paragraph)
3683 (equal data (car (org-element-contents parent)))
3684 (memq (org-element-type parent)
3685 '(footnote-definiton item))))))
3686 "")))
3687 (funcall (intern (format "org-element-%s-interpreter" type))
3688 data
3689 (if greaterp (org-element-normalize-contents contents)
3690 contents)))))))
3691 (if (memq type '(org-data plain-text nil)) results
3692 ;; Build white spaces. If no `:post-blank' property is
3693 ;; specified, assume its value is 0.
3694 (let ((post-blank (or (org-element-property :post-blank data) 0)))
3695 (if (memq type org-element-all-objects)
3696 (concat results (make-string post-blank 32))
3697 (concat
3698 (org-element-interpret--affiliated-keywords data)
3699 (org-element-normalize-string results)
3700 (make-string post-blank 10)))))))
3702 (defun org-element-interpret--affiliated-keywords (element)
3703 "Return ELEMENT's affiliated keywords as Org syntax.
3704 If there is no affiliated keyword, return the empty string."
3705 (let ((keyword-to-org
3706 (function
3707 (lambda (key value)
3708 (let (dual)
3709 (when (member key org-element-dual-keywords)
3710 (setq dual (cdr value) value (car value)))
3711 (concat "#+" key
3712 (and dual
3713 (format "[%s]" (org-element-interpret-data dual)))
3714 ": "
3715 (if (member key org-element-parsed-keywords)
3716 (org-element-interpret-data value)
3717 value)
3718 "\n"))))))
3719 (mapconcat
3720 (lambda (prop)
3721 (let ((value (org-element-property prop element))
3722 (keyword (upcase (substring (symbol-name prop) 1))))
3723 (when value
3724 (if (or (member keyword org-element-multiple-keywords)
3725 ;; All attribute keywords can have multiple lines.
3726 (string-match "^ATTR_" keyword))
3727 (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
3728 value
3730 (funcall keyword-to-org keyword value)))))
3731 ;; List all ELEMENT's properties matching an attribute line or an
3732 ;; affiliated keyword, but ignore translated keywords since they
3733 ;; cannot belong to the property list.
3734 (loop for prop in (nth 1 element) by 'cddr
3735 when (let ((keyword (upcase (substring (symbol-name prop) 1))))
3736 (or (string-match "^ATTR_" keyword)
3737 (and
3738 (member keyword org-element-affiliated-keywords)
3739 (not (assoc keyword
3740 org-element-keyword-translation-alist)))))
3741 collect prop)
3742 "")))
3744 ;; Because interpretation of the parse tree must return the same
3745 ;; number of blank lines between elements and the same number of white
3746 ;; space after objects, some special care must be given to white
3747 ;; spaces.
3749 ;; The first function, `org-element-normalize-string', ensures any
3750 ;; string different from the empty string will end with a single
3751 ;; newline character.
3753 ;; The second function, `org-element-normalize-contents', removes
3754 ;; global indentation from the contents of the current element.
3756 (defun org-element-normalize-string (s)
3757 "Ensure string S ends with a single newline character.
3759 If S isn't a string return it unchanged. If S is the empty
3760 string, return it. Otherwise, return a new string with a single
3761 newline character at its end."
3762 (cond
3763 ((not (stringp s)) s)
3764 ((string= "" s) "")
3765 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
3766 (replace-match "\n" nil nil s)))))
3768 (defun org-element-normalize-contents (element &optional ignore-first)
3769 "Normalize plain text in ELEMENT's contents.
3771 ELEMENT must only contain plain text and objects.
3773 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3774 indentation to compute maximal common indentation.
3776 Return the normalized element that is element with global
3777 indentation removed from its contents. The function assumes that
3778 indentation is not done with TAB characters."
3779 (let* (ind-list ; for byte-compiler
3780 collect-inds ; for byte-compiler
3781 (collect-inds
3782 (function
3783 ;; Return list of indentations within BLOB. This is done by
3784 ;; walking recursively BLOB and updating IND-LIST along the
3785 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3786 ;; been seen yet. It is required as this string is the only
3787 ;; one whose indentation doesn't happen after a newline
3788 ;; character.
3789 (lambda (blob first-flag)
3790 (mapc
3791 (lambda (object)
3792 (when (and first-flag (stringp object))
3793 (setq first-flag nil)
3794 (string-match "\\`\\( *\\)" object)
3795 (let ((len (length (match-string 1 object))))
3796 ;; An indentation of zero means no string will be
3797 ;; modified. Quit the process.
3798 (if (zerop len) (throw 'zero (setq ind-list nil))
3799 (push len ind-list))))
3800 (cond
3801 ((stringp object)
3802 (let ((start 0))
3803 ;; Avoid matching blank or empty lines.
3804 (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
3805 (not (equal (match-string 2 object) " ")))
3806 (setq start (match-end 0))
3807 (push (length (match-string 1 object)) ind-list))))
3808 ((memq (org-element-type object) org-element-recursive-objects)
3809 (funcall collect-inds object first-flag))))
3810 (org-element-contents blob))))))
3811 ;; Collect indentation list in ELEMENT. Possibly remove first
3812 ;; value if IGNORE-FIRST is non-nil.
3813 (catch 'zero (funcall collect-inds element (not ignore-first)))
3814 (if (not ind-list) element
3815 ;; Build ELEMENT back, replacing each string with the same
3816 ;; string minus common indentation.
3817 (let* (build ; for byte compiler
3818 (build
3819 (function
3820 (lambda (blob mci first-flag)
3821 ;; Return BLOB with all its strings indentation
3822 ;; shortened from MCI white spaces. FIRST-FLAG is
3823 ;; non-nil when the first string hasn't been seen
3824 ;; yet.
3825 (nconc
3826 (list (org-element-type blob) (nth 1 blob))
3827 (mapcar
3828 (lambda (object)
3829 (when (and first-flag (stringp object))
3830 (setq first-flag nil)
3831 (setq object
3832 (replace-regexp-in-string
3833 (format "\\` \\{%d\\}" mci) "" object)))
3834 (cond
3835 ((stringp object)
3836 (replace-regexp-in-string
3837 (format "\n \\{%d\\}" mci) "\n" object))
3838 ((memq (org-element-type object)
3839 org-element-recursive-objects)
3840 (funcall build object mci first-flag))
3841 (t object)))
3842 (org-element-contents blob)))))))
3843 (funcall build element (apply 'min ind-list) (not ignore-first))))))
3847 ;;; The Toolbox
3849 ;; The first move is to implement a way to obtain the smallest element
3850 ;; containing point. This is the job of `org-element-at-point'. It
3851 ;; basically jumps back to the beginning of section containing point
3852 ;; and moves, element after element, with
3853 ;; `org-element-current-element' until the container is found.
3855 ;; Note: When using `org-element-at-point', secondary values are never
3856 ;; parsed since the function focuses on elements, not on objects.
3858 (defun org-element-at-point (&optional keep-trail)
3859 "Determine closest element around point.
3861 Return value is a list like (TYPE PROPS) where TYPE is the type
3862 of the element and PROPS a plist of properties associated to the
3863 element. Possible types are defined in
3864 `org-element-all-elements'.
3866 As a special case, if point is at the very beginning of a list or
3867 sub-list, returned element will be that list instead of the first
3868 item. In the same way, if point is at the beginning of the first
3869 row of a table, returned element will be the table instead of the
3870 first row.
3872 If optional argument KEEP-TRAIL is non-nil, the function returns
3873 a list of of elements leading to element at point. The list's
3874 CAR is always the element at point. Following positions contain
3875 element's siblings, then parents, siblings of parents, until the
3876 first element of current section."
3877 (org-with-wide-buffer
3878 ;; If at an headline, parse it. It is the sole element that
3879 ;; doesn't require to know about context. Be sure to disallow
3880 ;; secondary string parsing, though.
3881 (if (org-with-limited-levels (org-at-heading-p))
3882 (progn
3883 (beginning-of-line)
3884 (if (not keep-trail) (org-element-headline-parser t)
3885 (list (org-element-headline-parser t))))
3886 ;; Otherwise move at the beginning of the section containing
3887 ;; point.
3888 (let ((origin (point)) element type special-flag trail struct prevs)
3889 (org-with-limited-levels
3890 (if (org-before-first-heading-p) (goto-char (point-min))
3891 (org-back-to-heading)
3892 (forward-line)))
3893 (org-skip-whitespace)
3894 (beginning-of-line)
3895 ;; Parse successively each element, skipping those ending
3896 ;; before original position.
3897 (catch 'exit
3898 (while t
3899 (setq element (org-element-current-element
3900 'element special-flag struct)
3901 type (car element))
3902 (push element trail)
3903 (cond
3904 ;; 1. Skip any element ending before point or at point.
3905 ((let ((end (org-element-property :end element)))
3906 (when (<= end origin)
3907 (if (> (point-max) end) (goto-char end)
3908 (throw 'exit (if keep-trail trail element))))))
3909 ;; 2. An element containing point is always the element at
3910 ;; point.
3911 ((not (memq type org-element-greater-elements))
3912 (throw 'exit (if keep-trail trail element)))
3913 ;; 3. At any other greater element type, if point is
3914 ;; within contents, move into it. Otherwise, return
3915 ;; that element.
3917 (let ((beg (org-element-property :contents-begin element))
3918 (end (org-element-property :contents-end element)))
3919 (if (or (not beg) (not end) (> beg origin) (<= end origin)
3920 (and (= beg origin) (memq type '(plain-list table))))
3921 (throw 'exit (if keep-trail trail element))
3922 (case type
3923 (plain-list
3924 (setq special-flag 'item
3925 struct (org-element-property :structure element)))
3926 (table (setq special-flag 'table-row))
3927 (otherwise (setq special-flag nil)))
3928 (narrow-to-region beg end)
3929 (goto-char beg)))))))))))
3932 ;; Once the local structure around point is well understood, it's easy
3933 ;; to implement some replacements for `forward-paragraph'
3934 ;; `backward-paragraph', namely `org-element-forward' and
3935 ;; `org-element-backward'.
3937 ;; Also, `org-transpose-elements' mimics the behaviour of
3938 ;; `transpose-words', at the element's level, whereas
3939 ;; `org-element-drag-forward', `org-element-drag-backward', and
3940 ;; `org-element-up' generalize, respectively, functions
3941 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3943 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3944 ;; smartly remove global indentation from buffer, making it possible
3945 ;; to use Org indent mode on a file created with hard indentation.
3947 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3948 ;; internally by some of the previously cited tools.
3950 (defsubst org-element-nested-p (elem-A elem-B)
3951 "Non-nil when elements ELEM-A and ELEM-B are nested."
3952 (let ((beg-A (org-element-property :begin elem-A))
3953 (beg-B (org-element-property :begin elem-B))
3954 (end-A (org-element-property :end elem-A))
3955 (end-B (org-element-property :end elem-B)))
3956 (or (and (>= beg-A beg-B) (<= end-A end-B))
3957 (and (>= beg-B beg-A) (<= end-B end-A)))))
3959 (defun org-element-swap-A-B (elem-A elem-B)
3960 "Swap elements ELEM-A and ELEM-B.
3961 Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
3962 end of ELEM-A."
3963 (goto-char (org-element-property :begin elem-A))
3964 ;; There are two special cases when an element doesn't start at bol:
3965 ;; the first paragraph in an item or in a footnote definition.
3966 (let ((specialp (not (bolp))))
3967 ;; Only a paragraph without any affiliated keyword can be moved at
3968 ;; ELEM-A position in such a situation. Note that the case of
3969 ;; a footnote definition is impossible: it cannot contain two
3970 ;; paragraphs in a row because it cannot contain a blank line.
3971 (if (and specialp
3972 (or (not (eq (org-element-type elem-B) 'paragraph))
3973 (/= (org-element-property :begin elem-B)
3974 (org-element-property :contents-begin elem-B))))
3975 (error "Cannot swap elements"))
3976 ;; In a special situation, ELEM-A will have no indentation. We'll
3977 ;; give it ELEM-B's (which will in, in turn, have no indentation).
3978 (let* ((ind-B (when specialp
3979 (goto-char (org-element-property :begin elem-B))
3980 (org-get-indentation)))
3981 (beg-A (org-element-property :begin elem-A))
3982 (end-A (save-excursion
3983 (goto-char (org-element-property :end elem-A))
3984 (skip-chars-backward " \r\t\n")
3985 (point-at-eol)))
3986 (beg-B (org-element-property :begin elem-B))
3987 (end-B (save-excursion
3988 (goto-char (org-element-property :end elem-B))
3989 (skip-chars-backward " \r\t\n")
3990 (point-at-eol)))
3991 ;; Store overlays responsible for visibility status. We
3992 ;; also need to store their boundaries as they will be
3993 ;; removed from buffer.
3994 (overlays
3995 (cons
3996 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
3997 (overlays-in beg-A end-A))
3998 (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
3999 (overlays-in beg-B end-B))))
4000 ;; Get contents.
4001 (body-A (buffer-substring beg-A end-A))
4002 (body-B (delete-and-extract-region beg-B end-B)))
4003 (goto-char beg-B)
4004 (when specialp
4005 (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
4006 (org-indent-to-column ind-B))
4007 (insert body-A)
4008 ;; Restore ex ELEM-A overlays.
4009 (mapc (lambda (ov)
4010 (move-overlay
4011 (car ov)
4012 (+ (nth 1 ov) (- beg-B beg-A))
4013 (+ (nth 2 ov) (- beg-B beg-A))))
4014 (car overlays))
4015 (goto-char beg-A)
4016 (delete-region beg-A end-A)
4017 (insert body-B)
4018 ;; Restore ex ELEM-B overlays.
4019 (mapc (lambda (ov)
4020 (move-overlay (car ov)
4021 (+ (nth 1 ov) (- beg-A beg-B))
4022 (+ (nth 2 ov) (- beg-A beg-B))))
4023 (cdr overlays))
4024 (goto-char (org-element-property :end elem-B)))))
4026 (defun org-element-forward ()
4027 "Move forward by one element.
4028 Move to the next element at the same level, when possible."
4029 (interactive)
4030 (if (org-with-limited-levels (org-at-heading-p))
4031 (let ((origin (point)))
4032 (org-forward-same-level 1)
4033 (unless (org-with-limited-levels (org-at-heading-p))
4034 (goto-char origin)
4035 (error "Cannot move further down")))
4036 (let* ((trail (org-element-at-point 'keep-trail))
4037 (elem (pop trail))
4038 (end (org-element-property :end elem))
4039 (parent (loop for prev in trail
4040 when (>= (org-element-property :end prev) end)
4041 return prev)))
4042 (cond
4043 ((eobp) (error "Cannot move further down"))
4044 ((and parent (= (org-element-property :contents-end parent) end))
4045 (goto-char (org-element-property :end parent)))
4046 (t (goto-char end))))))
4048 (defun org-element-backward ()
4049 "Move backward by one element.
4050 Move to the previous element at the same level, when possible."
4051 (interactive)
4052 (if (org-with-limited-levels (org-at-heading-p))
4053 ;; At an headline, move to the previous one, if any, or stay
4054 ;; here.
4055 (let ((origin (point)))
4056 (org-backward-same-level 1)
4057 (unless (org-with-limited-levels (org-at-heading-p))
4058 (goto-char origin)
4059 (error "Cannot move further up")))
4060 (let* ((trail (org-element-at-point 'keep-trail))
4061 (elem (car trail))
4062 (prev-elem (nth 1 trail))
4063 (beg (org-element-property :begin elem)))
4064 (cond
4065 ;; Move to beginning of current element if point isn't there
4066 ;; already.
4067 ((/= (point) beg) (goto-char beg))
4068 ((not prev-elem) (error "Cannot move further up"))
4069 (t (goto-char (org-element-property :begin prev-elem)))))))
4071 (defun org-element-up ()
4072 "Move to upper element."
4073 (interactive)
4074 (if (org-with-limited-levels (org-at-heading-p))
4075 (unless (org-up-heading-safe)
4076 (error "No surrounding element"))
4077 (let* ((trail (org-element-at-point 'keep-trail))
4078 (elem (pop trail))
4079 (end (org-element-property :end elem))
4080 (parent (loop for prev in trail
4081 when (>= (org-element-property :end prev) end)
4082 return prev)))
4083 (cond
4084 (parent (goto-char (org-element-property :begin parent)))
4085 ((org-before-first-heading-p) (error "No surrounding element"))
4086 (t (org-back-to-heading))))))
4088 (defun org-element-down ()
4089 "Move to inner element."
4090 (interactive)
4091 (let ((element (org-element-at-point)))
4092 (cond
4093 ((memq (org-element-type element) '(plain-list table))
4094 (goto-char (org-element-property :contents-begin element))
4095 (forward-char))
4096 ((memq (org-element-type element) org-element-greater-elements)
4097 ;; If contents are hidden, first disclose them.
4098 (when (org-element-property :hiddenp element) (org-cycle))
4099 (goto-char (org-element-property :contents-begin element)))
4100 (t (error "No inner element")))))
4102 (defun org-element-drag-backward ()
4103 "Move backward element at point."
4104 (interactive)
4105 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
4106 (let* ((trail (org-element-at-point 'keep-trail))
4107 (elem (car trail))
4108 (prev-elem (nth 1 trail)))
4109 ;; Error out if no previous element or previous element is
4110 ;; a parent of the current one.
4111 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
4112 (error "Cannot drag element backward")
4113 (let ((pos (point)))
4114 (org-element-swap-A-B prev-elem elem)
4115 (goto-char (+ (org-element-property :begin prev-elem)
4116 (- pos (org-element-property :begin elem)))))))))
4118 (defun org-element-drag-forward ()
4119 "Move forward element at point."
4120 (interactive)
4121 (let* ((pos (point))
4122 (elem (org-element-at-point)))
4123 (when (= (point-max) (org-element-property :end elem))
4124 (error "Cannot drag element forward"))
4125 (goto-char (org-element-property :end elem))
4126 (let ((next-elem (org-element-at-point)))
4127 (when (or (org-element-nested-p elem next-elem)
4128 (and (eq (org-element-type next-elem) 'headline)
4129 (not (eq (org-element-type elem) 'headline))))
4130 (goto-char pos)
4131 (error "Cannot drag element forward"))
4132 ;; Compute new position of point: it's shifted by NEXT-ELEM
4133 ;; body's length (without final blanks) and by the length of
4134 ;; blanks between ELEM and NEXT-ELEM.
4135 (let ((size-next (- (save-excursion
4136 (goto-char (org-element-property :end next-elem))
4137 (skip-chars-backward " \r\t\n")
4138 (forward-line)
4139 ;; Small correction if buffer doesn't end
4140 ;; with a newline character.
4141 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
4142 (org-element-property :begin next-elem)))
4143 (size-blank (- (org-element-property :end elem)
4144 (save-excursion
4145 (goto-char (org-element-property :end elem))
4146 (skip-chars-backward " \r\t\n")
4147 (forward-line)
4148 (point)))))
4149 (org-element-swap-A-B elem next-elem)
4150 (goto-char (+ pos size-next size-blank))))))
4152 (defun org-element-mark-element ()
4153 "Put point at beginning of this element, mark at end.
4155 Interactively, if this command is repeated or (in Transient Mark
4156 mode) if the mark is active, it marks the next element after the
4157 ones already marked."
4158 (interactive)
4159 (let (deactivate-mark)
4160 (if (or (and (eq last-command this-command) (mark t))
4161 (and transient-mark-mode mark-active))
4162 (set-mark
4163 (save-excursion
4164 (goto-char (mark))
4165 (goto-char (org-element-property :end (org-element-at-point)))))
4166 (let ((element (org-element-at-point)))
4167 (end-of-line)
4168 (push-mark (org-element-property :end element) t t)
4169 (goto-char (org-element-property :begin element))))))
4171 (defun org-narrow-to-element ()
4172 "Narrow buffer to current element."
4173 (interactive)
4174 (let ((elem (org-element-at-point)))
4175 (cond
4176 ((eq (car elem) 'headline)
4177 (narrow-to-region
4178 (org-element-property :begin elem)
4179 (org-element-property :end elem)))
4180 ((memq (car elem) org-element-greater-elements)
4181 (narrow-to-region
4182 (org-element-property :contents-begin elem)
4183 (org-element-property :contents-end elem)))
4185 (narrow-to-region
4186 (org-element-property :begin elem)
4187 (org-element-property :end elem))))))
4189 (defun org-element-transpose ()
4190 "Transpose current and previous elements, keeping blank lines between.
4191 Point is moved after both elements."
4192 (interactive)
4193 (org-skip-whitespace)
4194 (let ((end (org-element-property :end (org-element-at-point))))
4195 (org-element-drag-backward)
4196 (goto-char end)))
4198 (defun org-element-unindent-buffer ()
4199 "Un-indent the visible part of the buffer.
4200 Relative indentation (between items, inside blocks, etc.) isn't
4201 modified."
4202 (interactive)
4203 (unless (eq major-mode 'org-mode)
4204 (error "Cannot un-indent a buffer not in Org mode"))
4205 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
4206 unindent-tree ; For byte-compiler.
4207 (unindent-tree
4208 (function
4209 (lambda (contents)
4210 (mapc
4211 (lambda (element)
4212 (if (memq (org-element-type element) '(headline section))
4213 (funcall unindent-tree (org-element-contents element))
4214 (save-excursion
4215 (save-restriction
4216 (narrow-to-region
4217 (org-element-property :begin element)
4218 (org-element-property :end element))
4219 (org-do-remove-indentation)))))
4220 (reverse contents))))))
4221 (funcall unindent-tree (org-element-contents parse-tree))))
4223 (defun org-element-fill-paragraph (&optional justify)
4224 "Fill element at point, when applicable.
4226 This function only applies to paragraph, comment blocks, example
4227 blocks and fixed-width areas. Also, as a special case, re-align
4228 table when point is at one.
4230 If JUSTIFY is non-nil (interactively, with prefix argument),
4231 justify as well. If `sentence-end-double-space' is non-nil, then
4232 period followed by one space does not end a sentence, so don't
4233 break a line there. The variable `fill-column' controls the
4234 width for filling."
4235 (let ((element (org-element-at-point)))
4236 (case (org-element-type element)
4237 ;; Align Org tables, leave table.el tables as-is.
4238 (table-row (org-table-align) t)
4239 (table
4240 (when (eq (org-element-property :type element) 'org) (org-table-align))
4242 ;; Elements that may contain `line-break' type objects.
4243 ((paragraph verse-block)
4244 (let ((beg (org-element-property :contents-begin element))
4245 (end (org-element-property :contents-end element)))
4246 ;; Do nothing if point is at an affiliated keyword or at
4247 ;; verse block markers.
4248 (if (or (< (point) beg) (>= (point) end)) t
4249 ;; At a verse block, first narrow to current "paragraph"
4250 ;; and set current element to that paragraph.
4251 (save-restriction
4252 (when (eq (org-element-type element) 'verse-block)
4253 (narrow-to-region beg end)
4254 (save-excursion
4255 (end-of-line)
4256 (let ((bol-pos (point-at-bol)))
4257 (re-search-backward org-element-paragraph-separate nil 'move)
4258 (unless (or (bobp) (= (point-at-bol) bol-pos))
4259 (forward-line))
4260 (setq element (org-element-paragraph-parser)
4261 beg (org-element-property :contents-begin element)
4262 end (org-element-property :contents-end element)))))
4263 ;; Fill paragraph, taking line breaks into consideration.
4264 ;; For that, slice the paragraph using line breaks as
4265 ;; separators, and fill the parts in reverse order to
4266 ;; avoid messing with markers.
4267 (save-excursion
4268 (goto-char end)
4269 (mapc
4270 (lambda (pos)
4271 (fill-region-as-paragraph pos (point) justify)
4272 (goto-char pos))
4273 ;; Find the list of ending positions for line breaks
4274 ;; in the current paragraph. Add paragraph beginning
4275 ;; to include first slice.
4276 (nreverse
4277 (cons beg
4278 (org-element-map
4279 (org-element-parse-objects
4280 beg end nil org-element-all-objects)
4281 'line-break
4282 (lambda (lb) (org-element-property :end lb)))))))) t)))
4283 ;; Elements whose contents should be filled as plain text.
4284 ((comment-block example-block)
4285 (save-restriction
4286 (narrow-to-region
4287 (save-excursion
4288 (goto-char (org-element-property :begin element))
4289 (while (looking-at org-element--affiliated-re) (forward-line))
4290 (forward-line)
4291 (point))
4292 (save-excursion
4293 (goto-char (org-element-property :end element))
4294 (if (bolp) (forward-line -1) (beginning-of-line))
4295 (point)))
4296 (fill-paragraph justify) t))
4297 ;; Ignore every other element.
4298 (otherwise t))))
4301 (provide 'org-element)
4302 ;;; org-element.el ends here