org-element: COMMENT and QUOTE keywords, ARCHIVE tags are case sensitive
[org-mode/org-mode-NeilSmithlineMods.git] / contrib / lisp / org-element.el
blob93174a305b29411d7c4ca4ab101d9bb90f3781fe
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 ;; An object can be defined anywhere on a line. It may span over more
29 ;; than a line but never contains a blank one. Objects belong to the
30 ;; following types: `emphasis', `entity', `export-snippet',
31 ;; `footnote-reference', `inline-babel-call', `inline-src-block',
32 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
33 ;; `statistics-cookie', `subscript', `superscript', `target',
34 ;; `time-stamp' and `verbatim'.
36 ;; An element always starts and ends at the beginning of a line. The
37 ;; only element's type containing objects is called a `paragraph'.
38 ;; Other types are: `comment', `comment-block', `example-block',
39 ;; `export-block', `fixed-width', `horizontal-rule', `keyword',
40 ;; `latex-environment', `babel-call', `property-drawer',
41 ;; `quote-section', `src-block', `table' and `verse-block'.
43 ;; Elements containing paragraphs are called greater elements.
44 ;; Concerned types are: `center-block', `drawer', `dynamic-block',
45 ;; `footnote-definition', `headline', `inlinetask', `item',
46 ;; `plain-list', `quote-block', `section' and `special-block'.
48 ;; Greater elements (excepted `headline', `item' and `section' types)
49 ;; and elements (excepted `keyword', `babel-call', and
50 ;; `property-drawer' types) can have a fixed set of keywords as
51 ;; attributes. Those are called "affiliated keywords", to distinguish
52 ;; them from others keywords, which are full-fledged elements. In
53 ;; particular, the "name" affiliated keyword allows to label almost
54 ;; any element in an Org buffer.
56 ;; Notwithstanding affiliated keywords, each greater element, element
57 ;; and object has a fixed set of properties attached to it. Among
58 ;; them, three are shared by all types: `:begin' and `:end', which
59 ;; refer to the beginning and ending buffer positions of the
60 ;; considered element or object, and `:post-blank', which holds the
61 ;; number of blank lines, or white spaces, at its end.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (i.e. an item tag, an headline name, a table
65 ;; cell). Such values are called "secondary strings".
67 ;; Lisp-wise, an element or an object can be represented as a list.
68 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
69 ;; TYPE is a symbol describing the Org element or object.
70 ;; PROPERTIES is the property list attached to it. See docstring of
71 ;; appropriate parsing function to get an exhaustive
72 ;; list.
73 ;; CONTENTS is a list of elements, objects or raw strings contained
74 ;; in the current element or object, when applicable.
76 ;; An Org buffer is a nested list of such elements and objects, whose
77 ;; type is `org-data' and properties is nil.
79 ;; The first part of this file implements a parser and an interpreter
80 ;; for each type of Org syntax.
82 ;; The next two parts introduce three accessors and a function
83 ;; retrieving the smallest element containing point (respectively
84 ;; `org-element-type', `org-element-property', `org-element-contents'
85 ;; and `org-element-at-point').
87 ;; The following part creates a fully recursive buffer parser. It
88 ;; also provides a tool to map a function to elements or objects
89 ;; matching some criteria in the parse tree. Functions of interest
90 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
91 ;; extent, `org-element-parse-secondary-string'.
93 ;; The penultimate part is the cradle of an interpreter for the
94 ;; obtained parse tree: `org-element-interpret-data' (and its
95 ;; relative, `org-element-interpret-secondary').
97 ;; The library ends by furnishing a set of interactive tools for
98 ;; element's navigation and manipulation.
101 ;;; Code:
103 (eval-when-compile (require 'cl))
104 (require 'org)
105 (declare-function org-inlinetask-goto-end "org-inlinetask" ())
108 ;;; Greater elements
110 ;; For each greater element type, we define a parser and an
111 ;; interpreter.
113 ;; A parser (`item''s excepted) accepts no argument and represents the
114 ;; element or object as the list described above. An interpreter
115 ;; accepts two arguments: the list representation of the element or
116 ;; object, and its contents. The latter may be nil, depending on the
117 ;; element or object considered. It returns the appropriate Org
118 ;; syntax, as a string.
120 ;; Parsing functions must follow the naming convention:
121 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
122 ;; defined in `org-element-greater-elements'.
124 ;; Similarly, interpreting functions must follow the naming
125 ;; convention: org-element-TYPE-interpreter.
127 ;; With the exception of `headline' and `item' types, greater elements
128 ;; cannot contain other greater elements of their own type.
130 ;; Beside implementing a parser and an interpreter, adding a new
131 ;; greater element requires to tweak `org-element-guess-type'.
132 ;; Moreover, the newly defined type must be added to both
133 ;; `org-element-all-elements' and `org-element-greater-elements'.
136 ;;;; Center Block
138 (defun org-element-center-block-parser ()
139 "Parse a center block.
141 Return a list whose car is `center-block' and cdr is a plist
142 containing `:begin', `:end', `:hiddenp', `:contents-begin',
143 `:contents-end' and `:post-blank' keywords.
145 Assume point is at beginning or end of the block."
146 (save-excursion
147 (let* ((case-fold-search t)
148 (keywords (progn
149 (end-of-line)
150 (re-search-backward
151 (concat "^[ \t]*#\\+begin_center") nil t)
152 (org-element-collect-affiliated-keywords)))
153 (begin (car keywords))
154 (contents-begin (progn (forward-line) (point)))
155 (hidden (org-truely-invisible-p))
156 (contents-end (progn (re-search-forward
157 (concat "^[ \t]*#\\+end_center") nil t)
158 (point-at-bol)))
159 (pos-before-blank (progn (forward-line) (point)))
160 (end (progn (org-skip-whitespace)
161 (if (eobp) (point) (point-at-bol)))))
162 `(center-block
163 (:begin ,begin
164 :end ,end
165 :hiddenp ,hidden
166 :contents-begin ,contents-begin
167 :contents-end ,contents-end
168 :post-blank ,(count-lines pos-before-blank end)
169 ,@(cadr keywords))))))
171 (defun org-element-center-block-interpreter (center-block contents)
172 "Interpret CENTER-BLOCK element as Org syntax.
173 CONTENTS is the contents of the element."
174 (format "#+begin_center\n%s#+end_center" contents))
177 ;;;; Drawer
179 (defun org-element-drawer-parser ()
180 "Parse a drawer.
182 Return a list whose car is `drawer' and cdr is a plist containing
183 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
184 `:contents-end' and `:post-blank' keywords.
186 Assume point is at beginning of drawer."
187 (save-excursion
188 (let* ((case-fold-search t)
189 (name (progn (looking-at org-drawer-regexp)
190 (org-match-string-no-properties 1)))
191 (keywords (org-element-collect-affiliated-keywords))
192 (begin (car keywords))
193 (contents-begin (progn (forward-line) (point)))
194 (hidden (org-truely-invisible-p))
195 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t)
196 (point-at-bol)))
197 (pos-before-blank (progn (forward-line) (point)))
198 (end (progn (org-skip-whitespace)
199 (if (eobp) (point) (point-at-bol)))))
200 `(drawer
201 (:begin ,begin
202 :end ,end
203 :drawer-name ,name
204 :hiddenp ,hidden
205 :contents-begin ,contents-begin
206 :contents-end ,contents-end
207 :post-blank ,(count-lines pos-before-blank end)
208 ,@(cadr keywords))))))
210 (defun org-element-drawer-interpreter (drawer contents)
211 "Interpret DRAWER element as Org syntax.
212 CONTENTS is the contents of the element."
213 (format ":%s:\n%s:END:"
214 (org-element-property :drawer-name drawer)
215 contents))
218 ;;;; Dynamic Block
220 (defun org-element-dynamic-block-parser ()
221 "Parse a dynamic block.
223 Return a list whose car is `dynamic-block' and cdr is a plist
224 containing `:block-name', `:begin', `:end', `:hiddenp',
225 `:contents-begin', `:contents-end', `:arguments' and
226 `:post-blank' keywords.
228 Assume point is at beginning of dynamic block."
229 (save-excursion
230 (let* ((case-fold-search t)
231 (name (progn (looking-at org-dblock-start-re)
232 (org-match-string-no-properties 1)))
233 (arguments (org-match-string-no-properties 3))
234 (keywords (org-element-collect-affiliated-keywords))
235 (begin (car keywords))
236 (contents-begin (progn (forward-line) (point)))
237 (hidden (org-truely-invisible-p))
238 (contents-end (progn (re-search-forward org-dblock-end-re nil t)
239 (point-at-bol)))
240 (pos-before-blank (progn (forward-line) (point)))
241 (end (progn (org-skip-whitespace)
242 (if (eobp) (point) (point-at-bol)))))
243 (list 'dynamic-block
244 `(:begin ,begin
245 :end ,end
246 :block-name ,name
247 :arguments ,arguments
248 :hiddenp ,hidden
249 :contents-begin ,contents-begin
250 :contents-end ,contents-end
251 :post-blank ,(count-lines pos-before-blank end)
252 ,@(cadr keywords))))))
254 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
255 "Interpret DYNAMIC-BLOCK element as Org syntax.
256 CONTENTS is the contents of the element."
257 (format "#+BEGIN: %s%s\n%s#+END:"
258 (org-element-property :block-name dynamic-block)
259 (let ((args (org-element-property :arguments dynamic-block)))
260 (and arg (concat " " args)))
261 contents))
264 ;;;; Footnote Definition
266 (defun org-element-footnote-definition-parser ()
267 "Parse a footnote definition.
269 Return a list whose car is `footnote-definition' and cdr is
270 a plist containing `:label', `:begin' `:end', `:contents-begin',
271 `:contents-end' and `:post-blank' keywords."
272 (save-excursion
273 (let* ((f-def (org-footnote-at-definition-p))
274 (label (car f-def))
275 (keywords (progn (goto-char (nth 1 f-def))
276 (org-element-collect-affiliated-keywords)))
277 (begin (car keywords))
278 (contents-begin (progn (looking-at (concat "\\[" label "\\]"))
279 (goto-char (match-end 0))
280 (org-skip-whitespace)
281 (point)))
282 (end (goto-char (nth 2 f-def)))
283 (contents-end (progn (skip-chars-backward " \r\t\n")
284 (forward-line)
285 (point))))
286 `(footnote-definition
287 (:label ,label
288 :begin ,begin
289 :end ,end
290 :contents-begin ,contents-begin
291 :contents-end ,contents-end
292 :post-blank ,(count-lines contents-end end)
293 ,@(cadr keywords))))))
295 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
296 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
297 CONTENTS is the contents of the footnote-definition."
298 (concat (format "[%s]" (org-element-property :label footnote-definition))
300 contents))
303 ;;;; Headline
305 (defun org-element-headline-parser ()
306 "Parse an headline.
308 Return a list whose car is `headline' and cdr is a plist
309 containing `:raw-value', `:title', `:begin', `:end',
310 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
311 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
312 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
313 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
314 keywords.
316 The plist also contains any property set in the property drawer,
317 with its name in lowercase, the underscores replaced with hyphens
318 and colons at the beginning (i.e. `:custom-id').
320 Assume point is at beginning of the headline."
321 (save-excursion
322 (let* ((components (org-heading-components))
323 (level (nth 1 components))
324 (todo (nth 2 components))
325 (todo-type
326 (and todo (if (member todo org-done-keywords) 'done 'todo)))
327 (tags (nth 5 components))
328 (raw-value (nth 4 components))
329 (quotedp
330 (let ((case-fold-search nil))
331 (string-match (format "^%s +" org-quote-string) raw-value)))
332 (commentedp
333 (let ((case-fold-search nil))
334 (string-match (format "^%s +" org-comment-string) raw-value)))
335 (archivedp
336 (and tags
337 (let ((case-fold-search nil))
338 (string-match (format ":%s:" org-archive-tag) tags))))
339 (footnote-section-p (and org-footnote-section
340 (string= org-footnote-section raw-value)))
341 (standard-props (let (plist)
342 (mapc
343 (lambda (p)
344 (let ((p-name (downcase (car p))))
345 (while (string-match "_" p-name)
346 (setq p-name
347 (replace-match "-" nil nil p-name)))
348 (setq p-name (intern (concat ":" p-name)))
349 (setq plist
350 (plist-put plist p-name (cdr p)))))
351 (org-entry-properties nil 'standard))
352 plist))
353 (time-props (org-entry-properties nil 'special "CLOCK"))
354 (scheduled (cdr (assoc "SCHEDULED" time-props)))
355 (deadline (cdr (assoc "DEADLINE" time-props)))
356 (clock (cdr (assoc "CLOCK" time-props)))
357 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
358 (begin (point))
359 (pos-after-head (save-excursion (forward-line) (point)))
360 (contents-begin (save-excursion (forward-line)
361 (org-skip-whitespace)
362 (if (eobp) (point) (point-at-bol))))
363 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
364 (end (progn (goto-char (org-end-of-subtree t t))))
365 (contents-end (progn (skip-chars-backward " \r\t\n")
366 (forward-line)
367 (point)))
368 title)
369 ;; Clean RAW-VALUE from any quote or comment string.
370 (when (or quotedp commentedp)
371 (setq raw-value
372 (replace-regexp-in-string
373 (concat "\\(" org-quote-string "\\|" org-comment-string "\\) +")
375 raw-value)))
376 ;; Clean TAGS from archive tag, if any.
377 (when archivedp
378 (setq tags
379 (and (not (string= tags (format ":%s:" org-archive-tag)))
380 (replace-regexp-in-string
381 (concat org-archive-tag ":") "" tags)))
382 (when (string= tags ":") (setq tags nil)))
383 ;; Then get TITLE.
384 (setq title (org-element-parse-secondary-string
385 raw-value
386 (cdr (assq 'headline org-element-string-restrictions))))
387 `(headline
388 (:raw-value ,raw-value
389 :title ,title
390 :begin ,begin
391 :end ,end
392 :pre-blank ,(count-lines pos-after-head contents-begin)
393 :hiddenp ,hidden
394 :contents-begin ,contents-begin
395 :contents-end ,contents-end
396 :level ,level
397 :priority ,(nth 3 components)
398 :tags ,tags
399 :todo-keyword ,todo
400 :todo-type ,todo-type
401 :scheduled ,scheduled
402 :deadline ,deadline
403 :timestamp ,timestamp
404 :clock ,clock
405 :post-blank ,(count-lines contents-end end)
406 :footnote-section-p ,footnote-section-p
407 :archivedp ,archivedp
408 :commentedp ,commentedp
409 :quotedp ,quotedp
410 ,@standard-props)))))
412 (defun org-element-headline-interpreter (headline contents)
413 "Interpret HEADLINE element as Org syntax.
414 CONTENTS is the contents of the element."
415 (let* ((level (org-element-property :level headline))
416 (todo (org-element-property :todo-keyword headline))
417 (priority (org-element-property :priority headline))
418 (title (org-element-property :raw-value headline))
419 (tags (let ((tag-string (org-element-property :tags headline))
420 (archivedp (org-element-property :archivedp headline)))
421 (cond
422 ((and (not tag-string) archivedp)
423 (format ":%s:" org-archive-tag))
424 (archivedp (concat ":" org-archive-tag tag-string))
425 (t tag-string))))
426 (commentedp (org-element-property :commentedp headline))
427 (quotedp (org-element-property :quotedp headline))
428 (pre-blank (org-element-property :pre-blank headline))
429 (heading (concat (make-string level ?*)
430 (and todo (concat " " todo))
431 (and quotedp (concat " " org-quote-string))
432 (and commentedp (concat " " org-comment-string))
433 (and priority (concat " " priority))
434 (cond ((and org-footnote-section
435 (org-element-property
436 :footnote-section-p headline))
437 (concat " " org-footnote-section))
438 (title (concat " " title)))))
439 ;; Align tags.
440 (tags-fmt (when tags
441 (let ((tags-len (length tags)))
442 (format "%% %ds"
443 (cond
444 ((zerop org-tags-column) (1+ tags-len))
445 ((< org-tags-column 0)
446 (max (- (+ org-tags-column (length heading)))
447 (1+ tags-len)))
448 (t (max (+ (- org-tags-column (length heading))
449 tags-len)
450 (1+ tags-len)))))))))
451 (concat heading (and tags (format tags-fmt tags))
452 (make-string (1+ pre-blank) 10)
453 contents)))
456 ;;;; Inlinetask
458 (defun org-element-inlinetask-parser ()
459 "Parse an inline task.
461 Return a list whose car is `inlinetask' and cdr is a plist
462 containing `:raw-value', `:title', `:begin', `:end', `:hiddenp',
463 `:contents-begin' and `:contents-end', `:level', `:priority',
464 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
465 `:scheduled', `:deadline', `:timestamp', `:clock' and
466 `:post-blank' keywords.
468 The plist also contains any property set in the property drawer,
469 with its name in lowercase, the underscores replaced with hyphens
470 and colons at the beginning (i.e. `:custom-id').
472 Assume point is at beginning of the inline task."
473 (save-excursion
474 (let* ((keywords (org-element-collect-affiliated-keywords))
475 (begin (car keywords))
476 (components (org-heading-components))
477 (todo (nth 2 components))
478 (todo-type (and todo
479 (if (member todo org-done-keywords) 'done 'todo)))
480 (raw-value (nth 4 components))
481 (standard-props (let (plist)
482 (mapc
483 (lambda (p)
484 (let ((p-name (downcase (car p))))
485 (while (string-match "_" p-name)
486 (setq p-name
487 (replace-match "-" nil nil p-name)))
488 (setq p-name (intern (concat ":" p-name)))
489 (setq plist
490 (plist-put plist p-name (cdr p)))))
491 (org-entry-properties nil 'standard))
492 plist))
493 (time-props (org-entry-properties nil 'special "CLOCK"))
494 (scheduled (cdr (assoc "SCHEDULED" time-props)))
495 (deadline (cdr (assoc "DEADLINE" time-props)))
496 (clock (cdr (assoc "CLOCK" time-props)))
497 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
498 (title (org-element-parse-secondary-string
499 raw-value
500 (cdr (assq 'inlinetask org-element-string-restrictions))))
501 (contents-begin (save-excursion (forward-line) (point)))
502 (hidden (org-truely-invisible-p))
503 (pos-before-blank (org-inlinetask-goto-end))
504 ;; In the case of a single line task, CONTENTS-BEGIN and
505 ;; CONTENTS-END might overlap.
506 (contents-end (max contents-begin
507 (save-excursion (forward-line -1) (point))))
508 (end (progn (org-skip-whitespace)
509 (if (eobp) (point) (point-at-bol)))))
510 `(inlinetask
511 (:raw-value ,raw-value
512 :title ,title
513 :begin ,begin
514 :end ,end
515 :hiddenp ,(and (> contents-end contents-begin) hidden)
516 :contents-begin ,contents-begin
517 :contents-end ,contents-end
518 :level ,(nth 1 components)
519 :priority ,(nth 3 components)
520 :tags ,(nth 5 components)
521 :todo-keyword ,todo
522 :todo-type ,todo-type
523 :scheduled ,scheduled
524 :deadline ,deadline
525 :timestamp ,timestamp
526 :clock ,clock
527 :post-blank ,(count-lines pos-before-blank end)
528 ,@standard-props
529 ,@(cadr keywords))))))
531 (defun org-element-inlinetask-interpreter (inlinetask contents)
532 "Interpret INLINETASK element as Org syntax.
533 CONTENTS is the contents of inlinetask."
534 (let* ((level (org-element-property :level inlinetask))
535 (todo (org-element-property :todo-keyword inlinetask))
536 (priority (org-element-property :priority inlinetask))
537 (title (org-element-property :raw-value inlinetask))
538 (tags (org-element-property :tags inlinetask))
539 (task (concat (make-string level ?*)
540 (and todo (concat " " todo))
541 (and priority (concat " " priority))
542 (and title (concat " " title))))
543 ;; Align tags.
544 (tags-fmt (when tags
545 (format "%% %ds"
546 (cond
547 ((zerop org-tags-column) 1)
548 ((< 0 org-tags-column)
549 (max (+ org-tags-column
550 (length inlinetask)
551 (length tags))
553 (t (max (- org-tags-column (length inlinetask))
554 1)))))))
555 (concat inlinetask (and tags (format tags-fmt tags) "\n" contents))))
558 ;;;; Item
560 (defun org-element-item-parser (struct)
561 "Parse an item.
563 STRUCT is the structure of the plain list.
565 Return a list whose car is `item' and cdr is a plist containing
566 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
567 `:checkbox', `:counter', `:tag', `:raw-tag', `:structure',
568 `:hiddenp' and `:post-blank' keywords.
570 Assume point is at the beginning of the item."
571 (save-excursion
572 (beginning-of-line)
573 (let* ((begin (point))
574 (bullet (org-list-get-bullet (point) struct))
575 (checkbox (let ((box (org-list-get-checkbox begin struct)))
576 (cond ((equal "[ ]" box) 'off)
577 ((equal "[X]" box) 'on)
578 ((equal "[-]" box) 'trans))))
579 (counter (let ((c (org-list-get-counter begin struct)))
580 (cond
581 ((not c) nil)
582 ((string-match "[A-Za-z]" c)
583 (- (string-to-char (upcase (match-string 0 c)))
584 64))
585 ((string-match "[0-9]+" c)
586 (string-to-number (match-string 0 c))))))
587 (raw-tag (org-list-get-tag begin struct))
588 (tag (and raw-tag
589 (org-element-parse-secondary-string
590 raw-tag
591 (cdr (assq 'item org-element-string-restrictions)))))
592 (end (org-list-get-item-end begin struct))
593 (contents-begin (progn (looking-at org-list-full-item-re)
594 (goto-char (match-end 0))
595 (org-skip-whitespace)
596 ;; If first line isn't empty,
597 ;; contents really start at the text
598 ;; after item's meta-data.
599 (if (= (point-at-bol) begin) (point)
600 (point-at-bol))))
601 (hidden (progn (forward-line)
602 (and (not (= (point) end))
603 (org-truely-invisible-p))))
604 (contents-end (progn (goto-char end)
605 (skip-chars-backward " \r\t\n")
606 (forward-line)
607 (point))))
608 `(item
609 (:bullet ,bullet
610 :begin ,begin
611 :end ,end
612 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
613 ;; up in the case of an empty item separated
614 ;; from the next by a blank line. Thus, ensure
615 ;; the former is always the smallest of two.
616 :contents-begin ,(min contents-begin contents-end)
617 :contents-end ,(max contents-begin contents-end)
618 :checkbox ,checkbox
619 :counter ,counter
620 :raw-tag ,raw-tag
621 :tag ,tag
622 :hiddenp ,hidden
623 :structure ,struct
624 :post-blank ,(count-lines contents-end end))))))
626 (defun org-element-item-interpreter (item contents)
627 "Interpret ITEM element as Org syntax.
628 CONTENTS is the contents of the element."
629 (let* ((bullet
630 (let* ((beg (org-element-property :begin item))
631 (struct (org-element-property :structure item))
632 (pre (org-list-prevs-alist struct))
633 (bul (org-element-property :bullet item)))
634 (org-list-bullet-string
635 (if (not (eq (org-list-get-list-type beg struct pre) 'ordered)) "-"
636 (let ((num
637 (car
638 (last
639 (org-list-get-item-number
640 beg struct pre (org-list-parents-alist struct))))))
641 (format "%d%s"
643 (if (eq org-plain-list-ordered-item-terminator ?\)) ")"
644 ".")))))))
645 (checkbox (org-element-property :checkbox item))
646 (counter (org-element-property :counter item))
647 (tag (org-element-property :raw-tag item))
648 ;; Compute indentation.
649 (ind (make-string (length bullet) 32)))
650 ;; Indent contents.
651 (concat
652 bullet
653 (and counter (format "[@%d] " counter))
654 (cond
655 ((eq checkbox 'on) "[X] ")
656 ((eq checkbox 'off) "[ ] ")
657 ((eq checkbox 'trans) "[-] "))
658 (and tag (format "%s :: " tag))
659 (org-trim
660 (replace-regexp-in-string "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))))
663 ;;;; Plain List
665 (defun org-element-plain-list-parser (&optional structure)
666 "Parse a plain list.
668 Optional argument STRUCTURE, when non-nil, is the structure of
669 the plain list being parsed.
671 Return a list whose car is `plain-list' and cdr is a plist
672 containing `:type', `:begin', `:end', `:contents-begin' and
673 `:contents-end', `:level', `:structure' and `:post-blank'
674 keywords.
676 Assume point is at one of the list items."
677 (save-excursion
678 (let* ((struct (or structure (org-list-struct)))
679 (prevs (org-list-prevs-alist struct))
680 (parents (org-list-parents-alist struct))
681 (type (org-list-get-list-type (point) struct prevs))
682 (contents-begin (goto-char
683 (org-list-get-list-begin (point) struct prevs)))
684 (keywords (org-element-collect-affiliated-keywords))
685 (begin (car keywords))
686 (contents-end (goto-char
687 (org-list-get-list-end (point) struct prevs)))
688 (end (save-excursion (org-skip-whitespace)
689 (if (eobp) (point) (point-at-bol))))
690 (level 0))
691 ;; Get list level.
692 (let ((item contents-begin))
693 (while (setq item
694 (org-list-get-parent
695 (org-list-get-list-begin item struct prevs)
696 struct parents))
697 (incf level)))
698 ;; Blank lines below list belong to the top-level list only.
699 (when (> level 0)
700 (setq end (min (org-list-get-bottom-point struct)
701 (progn (org-skip-whitespace)
702 (if (eobp) (point) (point-at-bol))))))
703 ;; Return value.
704 `(plain-list
705 (:type ,type
706 :begin ,begin
707 :end ,end
708 :contents-begin ,contents-begin
709 :contents-end ,contents-end
710 :level ,level
711 :structure ,struct
712 :post-blank ,(count-lines contents-end end)
713 ,@(cadr keywords))))))
715 (defun org-element-plain-list-interpreter (plain-list contents)
716 "Interpret PLAIN-LIST element as Org syntax.
717 CONTENTS is the contents of the element."
718 contents)
721 ;;;; Quote Block
723 (defun org-element-quote-block-parser ()
724 "Parse a quote block.
726 Return a list whose car is `quote-block' and cdr is a plist
727 containing `:begin', `:end', `:hiddenp', `:contents-begin',
728 `:contents-end' and `:post-blank' keywords.
730 Assume point is at beginning or end of the block."
731 (save-excursion
732 (let* ((case-fold-search t)
733 (keywords (progn
734 (end-of-line)
735 (re-search-backward
736 (concat "^[ \t]*#\\+begin_quote") nil t)
737 (org-element-collect-affiliated-keywords)))
738 (begin (car keywords))
739 (contents-begin (progn (forward-line) (point)))
740 (hidden (org-truely-invisible-p))
741 (contents-end (progn (re-search-forward
742 (concat "^[ \t]*#\\+end_quote") nil t)
743 (point-at-bol)))
744 (pos-before-blank (progn (forward-line) (point)))
745 (end (progn (org-skip-whitespace)
746 (if (eobp) (point) (point-at-bol)))))
747 `(quote-block
748 (:begin ,begin
749 :end ,end
750 :hiddenp ,hidden
751 :contents-begin ,contents-begin
752 :contents-end ,contents-end
753 :post-blank ,(count-lines pos-before-blank end)
754 ,@(cadr keywords))))))
756 (defun org-element-quote-block-interpreter (quote-block contents)
757 "Interpret QUOTE-BLOCK element as Org syntax.
758 CONTENTS is the contents of the element."
759 (format "#+begin_quote\n%s#+end_quote" contents))
762 ;;;; Section
764 (defun org-element-section-parser ()
765 "Parse a section.
767 Return a list whose car is `section' and cdr is a plist
768 containing `:begin', `:end', `:contents-begin', `contents-end'
769 and `:post-blank' keywords."
770 (save-excursion
771 ;; Beginning of section is the beginning of the first non-blank
772 ;; line after previous headline.
773 (org-with-limited-levels
774 (let ((begin
775 (save-excursion
776 (outline-previous-heading)
777 (if (not (org-at-heading-p)) (point)
778 (forward-line) (org-skip-whitespace) (point-at-bol))))
779 (end (progn (outline-next-heading) (point)))
780 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
781 (forward-line)
782 (point))))
783 `(section
784 (:begin ,begin
785 :end ,end
786 :contents-begin ,begin
787 :contents-end ,pos-before-blank
788 :post-blank ,(count-lines pos-before-blank end)))))))
790 (defun org-element-section-interpreter (section contents)
791 "Interpret SECTION element as Org syntax.
792 CONTENTS is the contents of the element."
793 contents)
796 ;;;; Special Block
798 (defun org-element-special-block-parser ()
799 "Parse a special block.
801 Return a list whose car is `special-block' and cdr is a plist
802 containing `:type', `:begin', `:end', `:hiddenp',
803 `:contents-begin', `:contents-end' and `:post-blank' keywords.
805 Assume point is at beginning or end of the block."
806 (save-excursion
807 (let* ((case-fold-search t)
808 (type (progn (looking-at
809 "[ \t]*#\\+\\(?:begin\\|end\\)_\\([-A-Za-z0-9]+\\)")
810 (org-match-string-no-properties 1)))
811 (keywords (progn
812 (end-of-line)
813 (re-search-backward
814 (concat "^[ \t]*#\\+begin_" type) nil t)
815 (org-element-collect-affiliated-keywords)))
816 (begin (car keywords))
817 (contents-begin (progn (forward-line) (point)))
818 (hidden (org-truely-invisible-p))
819 (contents-end (progn (re-search-forward
820 (concat "^[ \t]*#\\+end_" type) nil t)
821 (point-at-bol)))
822 (pos-before-blank (progn (forward-line) (point)))
823 (end (progn (org-skip-whitespace)
824 (if (eobp) (point) (point-at-bol)))))
825 `(special-block
826 (:type ,type
827 :begin ,begin
828 :end ,end
829 :hiddenp ,hidden
830 :contents-begin ,contents-begin
831 :contents-end ,contents-end
832 :post-blank ,(count-lines pos-before-blank end)
833 ,@(cadr keywords))))))
835 (defun org-element-special-block-interpreter (special-block contents)
836 "Interpret SPECIAL-BLOCK element as Org syntax.
837 CONTENTS is the contents of the element."
838 (let ((block-type (org-element-property :type special-block)))
839 (format "#+begin_%s\n%s#+end_%s" block-type contents block-type)))
843 ;;; Elements
845 ;; For each element, a parser and an interpreter are also defined.
846 ;; Both follow the same naming convention used for greater elements.
848 ;; Also, as for greater elements, adding a new element type is done
849 ;; through the following steps: implement a parser and an interpreter,
850 ;; tweak `org-element-guess-type' so that it recognizes the new type
851 ;; and add that new type to `org-element-all-elements'.
853 ;; As a special case, when the newly defined type is a block type,
854 ;; `org-element-non-recursive-block-alist' has to be modified
855 ;; accordingly.
858 ;;;; Babel Call
860 (defun org-element-babel-call-parser ()
861 "Parse a babel call.
863 Return a list whose car is `babel-call' and cdr is a plist
864 containing `:begin', `:end', `:info' and `:post-blank' as
865 keywords."
866 (save-excursion
867 (let ((info (progn (looking-at org-babel-block-lob-one-liner-regexp)
868 (org-babel-lob-get-info)))
869 (beg (point-at-bol))
870 (pos-before-blank (progn (forward-line) (point)))
871 (end (progn (org-skip-whitespace)
872 (if (eobp) (point) (point-at-bol)))))
873 `(babel-call
874 (:beg ,beg
875 :end ,end
876 :info ,info
877 :post-blank ,(count-lines pos-before-blank end))))))
879 (defun org-element-babel-call-interpreter (inline-babel-call contents)
880 "Interpret INLINE-BABEL-CALL object as Org syntax.
881 CONTENTS is nil."
882 (let* ((babel-info (org-element-property :info inline-babel-call))
883 (main-source (car babel-info))
884 (post-options (nth 1 babel-info)))
885 (concat "#+call: "
886 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
887 ;; Remove redundant square brackets.
888 (replace-match
889 (match-string 1 main-source) nil nil main-source)
890 main-source)
891 (and post-options (format "[%s]" post-options)))))
894 ;;;; Comment
896 (defun org-element-comment-parser ()
897 "Parse a comment.
899 Return a list whose car is `comment' and cdr is a plist
900 containing `:begin', `:end', `:value' and `:post-blank'
901 keywords."
902 (let (beg-coms begin end end-coms keywords)
903 (save-excursion
904 (if (looking-at "#")
905 ;; First type of comment: comments at column 0.
906 (let ((comment-re "^\\([^#]\\|#\\+[a-z]\\)"))
907 (save-excursion
908 (re-search-backward comment-re nil 'move)
909 (if (bobp) (setq keywords nil beg-coms (point))
910 (forward-line)
911 (setq keywords (org-element-collect-affiliated-keywords)
912 beg-coms (point))))
913 (re-search-forward comment-re nil 'move)
914 (setq end-coms (if (eobp) (point) (match-beginning 0))))
915 ;; Second type of comment: indented comments.
916 (let ((comment-re "[ \t]*#\\+\\(?: \\|$\\)"))
917 (unless (bobp)
918 (while (and (not (bobp)) (looking-at comment-re))
919 (forward-line -1))
920 (unless (looking-at comment-re) (forward-line)))
921 (setq beg-coms (point))
922 (setq keywords (org-element-collect-affiliated-keywords))
923 ;; Get comments ending. This may not be accurate if
924 ;; commented lines within an item are followed by commented
925 ;; lines outside of the list. Though, parser will always
926 ;; get it right as it already knows surrounding element and
927 ;; has narrowed buffer to its contents.
928 (while (looking-at comment-re) (forward-line))
929 (setq end-coms (point))))
930 ;; Find position after blank.
931 (goto-char end-coms)
932 (org-skip-whitespace)
933 (setq end (if (eobp) (point) (point-at-bol))))
934 `(comment
935 (:begin ,(or (car keywords) beg-coms)
936 :end ,end
937 :value ,(buffer-substring-no-properties beg-coms end-coms)
938 :post-blank ,(count-lines end-coms end)
939 ,@(cadr keywords)))))
941 (defun org-element-comment-interpreter (comment contents)
942 "Interpret COMMENT element as Org syntax.
943 CONTENTS is nil."
944 (org-element-property :value comment))
947 ;;;; Comment Block
949 (defun org-element-comment-block-parser ()
950 "Parse an export block.
952 Return a list whose car is `comment-block' and cdr is a plist
953 containing `:begin', `:end', `:hiddenp', `:value' and
954 `:post-blank' keywords."
955 (save-excursion
956 (end-of-line)
957 (let* ((case-fold-search t)
958 (keywords (progn
959 (re-search-backward "^[ \t]*#\\+begin_comment" nil t)
960 (org-element-collect-affiliated-keywords)))
961 (begin (car keywords))
962 (contents-begin (progn (forward-line) (point)))
963 (hidden (org-truely-invisible-p))
964 (contents-end (progn (re-search-forward
965 "^[ \t]*#\\+end_comment" nil t)
966 (point-at-bol)))
967 (pos-before-blank (progn (forward-line) (point)))
968 (end (progn (org-skip-whitespace)
969 (if (eobp) (point) (point-at-bol))))
970 (value (buffer-substring-no-properties contents-begin contents-end)))
971 `(comment-block
972 (:begin ,begin
973 :end ,end
974 :value ,value
975 :hiddenp ,hidden
976 :post-blank ,(count-lines pos-before-blank end)
977 ,@(cadr keywords))))))
979 (defun org-element-comment-block-interpreter (comment-block contents)
980 "Interpret COMMENT-BLOCK element as Org syntax.
981 CONTENTS is nil."
982 (concat "#+begin_comment\n"
983 (org-remove-indentation
984 (org-element-property :value comment-block))
985 "#+begin_comment"))
988 ;;;; Example Block
990 (defun org-element-example-block-parser ()
991 "Parse an example block.
993 Return a list whose car is `example' and cdr is a plist
994 containing `:begin', `:end', `:options', `:hiddenp', `:value' and
995 `:post-blank' keywords."
996 (save-excursion
997 (end-of-line)
998 (let* ((case-fold-search t)
999 (switches (progn
1000 (re-search-backward
1001 "^[ \t]*#\\+begin_example\\(?: +\\(.*\\)\\)?" nil t)
1002 (org-match-string-no-properties 1)))
1003 (keywords (org-element-collect-affiliated-keywords))
1004 (begin (car keywords))
1005 (contents-begin (progn (forward-line) (point)))
1006 (hidden (org-truely-invisible-p))
1007 (contents-end (progn
1008 (re-search-forward "^[ \t]*#\\+end_example" nil t)
1009 (point-at-bol)))
1010 (value (buffer-substring-no-properties contents-begin contents-end))
1011 (pos-before-blank (progn (forward-line) (point)))
1012 (end (progn (org-skip-whitespace)
1013 (if (eobp) (point) (point-at-bol)))))
1014 `(example-block
1015 (:begin ,begin
1016 :end ,end
1017 :value ,value
1018 :switches ,switches
1019 :hiddenp ,hidden
1020 :post-blank ,(count-lines pos-before-blank end)
1021 ,@(cadr keywords))))))
1023 (defun org-element-example-block-interpreter (example-block contents)
1024 "Interpret EXAMPLE-BLOCK element as Org syntax.
1025 CONTENTS is nil."
1026 (let ((options (org-element-property :options example-block)))
1027 (concat "#+begin_example" (and options (concat " " options)) "\n"
1028 (org-remove-indentation
1029 (org-element-property :value example-block))
1030 "#+end_example")))
1033 ;;;; Export Block
1035 (defun org-element-export-block-parser ()
1036 "Parse an export block.
1038 Return a list whose car is `export-block' and cdr is a plist
1039 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1040 `:post-blank' keywords."
1041 (save-excursion
1042 (end-of-line)
1043 (let* ((case-fold-search t)
1044 (contents)
1045 (type (progn (re-search-backward
1046 (concat "[ \t]*#\\+begin_"
1047 (org-re "\\([[:alnum:]]+\\)")))
1048 (downcase (org-match-string-no-properties 1))))
1049 (keywords (org-element-collect-affiliated-keywords))
1050 (begin (car keywords))
1051 (contents-begin (progn (forward-line) (point)))
1052 (hidden (org-truely-invisible-p))
1053 (contents-end (progn (re-search-forward
1054 (concat "^[ \t]*#\\+end_" type) nil t)
1055 (point-at-bol)))
1056 (pos-before-blank (progn (forward-line) (point)))
1057 (end (progn (org-skip-whitespace)
1058 (if (eobp) (point) (point-at-bol))))
1059 (value (buffer-substring-no-properties contents-begin contents-end)))
1060 `(export-block
1061 (:begin ,begin
1062 :end ,end
1063 :type ,type
1064 :value ,value
1065 :hiddenp ,hidden
1066 :post-blank ,(count-lines pos-before-blank end)
1067 ,@(cadr keywords))))))
1069 (defun org-element-export-block-interpreter (export-block contents)
1070 "Interpret EXPORT-BLOCK element as Org syntax.
1071 CONTENTS is nil."
1072 (let ((type (org-element-property :type export-block)))
1073 (concat (format "#+begin_%s\n" type)
1074 (org-element-property :value export-block)
1075 (format "#+end_%s" type))))
1078 ;;;; Fixed-width
1080 (defun org-element-fixed-width-parser ()
1081 "Parse a fixed-width section.
1083 Return a list whose car is `fixed-width' and cdr is a plist
1084 containing `:begin', `:end', `:value' and `:post-blank'
1085 keywords."
1086 (let ((fixed-re "[ \t]*:\\( \\|$\\)")
1087 beg-area begin end value pos-before-blank keywords)
1088 (save-excursion
1089 ;; Move to the beginning of the fixed-width area.
1090 (unless (bobp)
1091 (while (and (not (bobp)) (looking-at fixed-re))
1092 (forward-line -1))
1093 (unless (looking-at fixed-re) (forward-line 1)))
1094 (setq beg-area (point))
1095 ;; Get affiliated keywords, if any.
1096 (setq keywords (org-element-collect-affiliated-keywords))
1097 ;; Store true beginning of element.
1098 (setq begin (car keywords))
1099 ;; Get ending of fixed-width area. If point is in a list,
1100 ;; ensure to not get outside of it.
1101 (let* ((itemp (org-in-item-p))
1102 (max-pos (if itemp
1103 (org-list-get-bottom-point
1104 (save-excursion (goto-char itemp) (org-list-struct)))
1105 (point-max))))
1106 (while (and (looking-at fixed-re) (< (point) max-pos))
1107 (forward-line)))
1108 (setq pos-before-blank (point))
1109 ;; Find position after blank
1110 (org-skip-whitespace)
1111 (setq end (if (eobp) (point) (point-at-bol)))
1112 ;; Extract value.
1113 (setq value (buffer-substring-no-properties beg-area pos-before-blank)))
1114 `(fixed-width
1115 (:begin ,begin
1116 :end ,end
1117 :value ,value
1118 :post-blank ,(count-lines pos-before-blank end)
1119 ,@(cadr keywords)))))
1121 (defun org-element-fixed-width-interpreter (fixed-width contents)
1122 "Interpret FIXED-WIDTH element as Org syntax.
1123 CONTENTS is nil."
1124 (org-remove-indentation (org-element-property :value fixed-width)))
1127 ;;;; Horizontal Rule
1129 (defun org-element-horizontal-rule-parser ()
1130 "Parse an horizontal rule.
1132 Return a list whose car is `horizontal-rule' and cdr is
1133 a plist containing `:begin', `:end' and `:post-blank'
1134 keywords."
1135 (save-excursion
1136 (let* ((keywords (org-element-collect-affiliated-keywords))
1137 (begin (car keywords))
1138 (post-hr (progn (forward-line) (point)))
1139 (end (progn (org-skip-whitespace)
1140 (if (eobp) (point) (point-at-bol)))))
1141 `(horizontal-rule
1142 (:begin ,begin
1143 :end ,end
1144 :post-blank ,(count-lines post-hr end)
1145 ,@(cadr keywords))))))
1147 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1148 "Interpret HORIZONTAL-RULE element as Org syntax.
1149 CONTENTS is nil."
1150 "-----")
1153 ;;;; Keyword
1155 (defun org-element-keyword-parser ()
1156 "Parse a keyword at point.
1158 Return a list whose car is `keyword' and cdr is a plist
1159 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1160 keywords."
1161 (save-excursion
1162 (let* ((begin (point))
1163 (key (progn (looking-at
1164 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1165 (org-match-string-no-properties 1)))
1166 (value (org-trim (buffer-substring-no-properties
1167 (match-end 0) (point-at-eol))))
1168 (pos-before-blank (progn (forward-line) (point)))
1169 (end (progn (org-skip-whitespace)
1170 (if (eobp) (point) (point-at-bol)))))
1171 `(keyword
1172 (:key ,key
1173 :value ,value
1174 :begin ,begin
1175 :end ,end
1176 :post-blank ,(count-lines pos-before-blank end))))))
1178 (defun org-element-keyword-interpreter (keyword contents)
1179 "Interpret KEYWORD element as Org syntax.
1180 CONTENTS is nil."
1181 (format "#+%s: %s"
1182 (org-element-property :key keyword)
1183 (org-element-property :value keyword)))
1186 ;;;; Latex Environment
1188 (defun org-element-latex-environment-parser ()
1189 "Parse a LaTeX environment.
1191 Return a list whose car is `latex-environment' and cdr is a plist
1192 containing `:begin', `:end', `:value' and `:post-blank' keywords."
1193 (save-excursion
1194 (end-of-line)
1195 (let* ((case-fold-search t)
1196 (contents-begin (re-search-backward "^[ \t]*\\\\begin" nil t))
1197 (keywords (org-element-collect-affiliated-keywords))
1198 (begin (car keywords))
1199 (contents-end (progn (re-search-forward "^[ \t]*\\\\end")
1200 (forward-line)
1201 (point)))
1202 (value (buffer-substring-no-properties contents-begin contents-end))
1203 (end (progn (org-skip-whitespace)
1204 (if (eobp) (point) (point-at-bol)))))
1205 `(latex-environment
1206 (:begin ,begin
1207 :end ,end
1208 :value ,value
1209 :post-blank ,(count-lines contents-end end)
1210 ,@(cadr keywords))))))
1212 (defun org-element-latex-environment-interpreter (latex-environment contents)
1213 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1214 CONTENTS is nil."
1215 (org-element-property :value latex-environment))
1218 ;;;; Paragraph
1220 (defun org-element-paragraph-parser ()
1221 "Parse a paragraph.
1223 Return a list whose car is `paragraph' and cdr is a plist
1224 containing `:begin', `:end', `:contents-begin' and
1225 `:contents-end' and `:post-blank' keywords.
1227 Assume point is at the beginning of the paragraph."
1228 (save-excursion
1229 (let* ((contents-begin (point))
1230 (keywords (org-element-collect-affiliated-keywords))
1231 (begin (car keywords))
1232 (contents-end (progn
1233 (end-of-line)
1234 (if (re-search-forward
1235 org-element-paragraph-separate nil 'm)
1236 (progn (forward-line -1) (end-of-line) (point))
1237 (point))))
1238 (pos-before-blank (progn (forward-line) (point)))
1239 (end (progn (org-skip-whitespace)
1240 (if (eobp) (point) (point-at-bol)))))
1241 `(paragraph
1242 (:begin ,begin
1243 :end ,end
1244 :contents-begin ,contents-begin
1245 :contents-end ,contents-end
1246 :post-blank ,(count-lines pos-before-blank end)
1247 ,@(cadr keywords))))))
1249 (defun org-element-paragraph-interpreter (paragraph contents)
1250 "Interpret PARAGRAPH element as Org syntax.
1251 CONTENTS is the contents of the element."
1252 contents)
1255 ;;;; Property Drawer
1257 (defun org-element-property-drawer-parser ()
1258 "Parse a property drawer.
1260 Return a list whose car is `property-drawer' and cdr is a plist
1261 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1262 `:contents-end', `:properties' and `:post-blank' keywords."
1263 (save-excursion
1264 (let ((case-fold-search t)
1265 (begin (progn (end-of-line)
1266 (re-search-backward org-property-start-re)
1267 (match-beginning 0)))
1268 (contents-begin (progn (forward-line) (point)))
1269 (hidden (org-truely-invisible-p))
1270 (properties (let (val)
1271 (while (not (looking-at "^[ \t]*:END:"))
1272 (when (looking-at
1273 (org-re
1274 "[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):"))
1275 (push (cons (match-string 1)
1276 (org-trim
1277 (buffer-substring
1278 (match-end 0) (point-at-eol))))
1279 val))
1280 (forward-line))
1281 val))
1282 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t)
1283 (point-at-bol)))
1284 (pos-before-blank (progn (forward-line) (point)))
1285 (end (progn (org-skip-whitespace)
1286 (if (eobp) (point) (point-at-bol)))))
1287 `(property-drawer
1288 (:begin ,begin
1289 :end ,end
1290 :hiddenp ,hidden
1291 :properties ,properties
1292 :post-blank ,(count-lines pos-before-blank end))))))
1294 (defun org-element-property-drawer-interpreter (property-drawer contents)
1295 "Interpret PROPERTY-DRAWER element as Org syntax.
1296 CONTENTS is nil."
1297 (let ((props (org-element-property :properties property-drawer)))
1298 (concat
1299 ":PROPERTIES:\n"
1300 (mapconcat (lambda (p)
1301 (format org-property-format (format ":%s:" (car p)) (cdr p)))
1302 (nreverse props) "\n")
1303 "\n:END:")))
1306 ;;;; Quote Section
1308 (defun org-element-quote-section-parser ()
1309 "Parse a quote section.
1311 Return a list whose car is `quote-section' and cdr is a plist
1312 containing `:begin', `:end', `:value' and `:post-blank'
1313 keywords.
1315 Assume point is at beginning of the section."
1316 (save-excursion
1317 (let* ((begin (point))
1318 (end (progn (org-with-limited-levels (outline-next-heading))
1319 (point)))
1320 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1321 (forward-line)
1322 (point)))
1323 (value (buffer-substring-no-properties begin pos-before-blank)))
1324 `(quote-section
1325 (:begin ,begin
1326 :end ,end
1327 :value ,value
1328 :post-blank ,(count-lines pos-before-blank end))))))
1330 (defun org-element-quote-section-interpreter (quote-section contents)
1331 "Interpret QUOTE-SECTION element as Org syntax.
1332 CONTENTS is nil."
1333 (org-element-property :value quote-section))
1336 ;;;; Src Block
1338 (defun org-element-src-block-parser ()
1339 "Parse a src block.
1341 Return a list whose car is `src-block' and cdr is a plist
1342 containing `:language', `:switches', `:parameters', `:begin',
1343 `:end', `:hiddenp', `:contents-begin', `:contents-end', `:value'
1344 and `:post-blank' keywords."
1345 (save-excursion
1346 (end-of-line)
1347 (let* ((case-fold-search t)
1348 ;; Get position at beginning of block.
1349 (contents-begin
1350 (re-search-backward
1351 (concat "^[ \t]*#\\+begin_src"
1352 "\\(?: +\\(\\S-+\\)\\)?" ; language
1353 "\\(\\(?: +[-+][A-Za-z]\\)*\\)" ; switches
1354 "\\(.*\\)[ \t]*$") ; arguments
1355 nil t))
1356 ;; Get language as a string.
1357 (language (org-match-string-no-properties 1))
1358 ;; Get switches.
1359 (switches (org-match-string-no-properties 2))
1360 ;; Get parameters.
1361 (parameters (org-trim (org-match-string-no-properties 3)))
1362 ;; Get affiliated keywords.
1363 (keywords (org-element-collect-affiliated-keywords))
1364 ;; Get beginning position.
1365 (begin (car keywords))
1366 ;; Get position at end of block.
1367 (contents-end (progn (re-search-forward "^[ \t]*#\\+end_src" nil t)
1368 (forward-line)
1369 (point)))
1370 ;; Retrieve code.
1371 (value (buffer-substring-no-properties
1372 (save-excursion (goto-char contents-begin)
1373 (forward-line)
1374 (point))
1375 (match-beginning 0)))
1376 ;; Get position after ending blank lines.
1377 (end (progn (org-skip-whitespace)
1378 (if (eobp) (point) (point-at-bol))))
1379 ;; Get visibility status.
1380 (hidden (progn (goto-char contents-begin)
1381 (forward-line)
1382 (org-truely-invisible-p))))
1383 `(src-block
1384 (:language ,language
1385 :switches ,switches
1386 :parameters ,parameters
1387 :begin ,begin
1388 :end ,end
1389 :hiddenp ,hidden
1390 :value ,value
1391 :post-blank ,(count-lines contents-end end)
1392 ,@(cadr keywords))))))
1394 (defun org-element-src-block-interpreter (src-block contents)
1395 "Interpret SRC-BLOCK element as Org syntax.
1396 CONTENTS is nil."
1397 (let ((lang (org-element-property :language src-block))
1398 (switches (org-element-property :switches src-block))
1399 (params (org-element-property :parameters src-block))
1400 (value (let ((val (org-element-property :value src-block)))
1401 (cond
1402 (org-src-preserve-indentation val)
1403 ((zerop org-edit-src-content-indentation)
1404 (org-remove-indentation val))
1406 (let ((ind (make-string
1407 org-edit-src-content-indentation 32)))
1408 (replace-regexp-in-string
1409 "\\(^\\)[ \t]*\\S-" ind
1410 (org-remove-indentation val) nil nil 1)))))))
1411 (concat (format "#+begin_src%s\n"
1412 (concat (and lang (concat " " lang))
1413 (and switches (concat " " switches))
1414 (and params (concat " " params))))
1415 value
1416 "#+end_src")))
1419 ;;;; Table
1421 (defun org-element-table-parser ()
1422 "Parse a table at point.
1424 Return a list whose car is `table' and cdr is a plist containing
1425 `:begin', `:end', `:contents-begin', `:contents-end', `:tblfm',
1426 `:type', `:raw-table' and `:post-blank' keywords."
1427 (save-excursion
1428 (let* ((table-begin (goto-char (org-table-begin t)))
1429 (type (if (org-at-table.el-p) 'table.el 'org))
1430 (keywords (org-element-collect-affiliated-keywords))
1431 (begin (car keywords))
1432 (table-end (goto-char (marker-position (org-table-end t))))
1433 (tblfm (when (looking-at "[ \t]*#\\+tblfm: +\\(.*\\)[ \t]*")
1434 (prog1 (org-match-string-no-properties 1)
1435 (forward-line))))
1436 (pos-before-blank (point))
1437 (end (progn (org-skip-whitespace)
1438 (if (eobp) (point) (point-at-bol))))
1439 (raw-table (org-remove-indentation
1440 (buffer-substring-no-properties table-begin table-end))))
1441 `(table
1442 (:begin ,begin
1443 :end ,end
1444 :type ,type
1445 :raw-table ,raw-table
1446 :tblfm ,tblfm
1447 :post-blank ,(count-lines pos-before-blank end)
1448 ,@(cadr keywords))))))
1450 (defun org-element-table-interpreter (table contents)
1451 "Interpret TABLE element as Org syntax.
1452 CONTENTS is nil."
1453 (org-element-property :raw-table table))
1456 ;;;; Verse Block
1458 (defun org-element-verse-block-parser ()
1459 "Parse a verse block.
1461 Return a list whose car is `verse-block' and cdr is a plist
1462 containing `:begin', `:end', `:hiddenp', `:raw-value', `:value'
1463 and `:post-blank' keywords.
1465 Assume point is at beginning or end of the block."
1466 (save-excursion
1467 (let* ((case-fold-search t)
1468 (keywords (progn
1469 (end-of-line)
1470 (re-search-backward
1471 (concat "^[ \t]*#\\+begin_verse") nil t)
1472 (org-element-collect-affiliated-keywords)))
1473 (begin (car keywords))
1474 (hidden (progn (forward-line) (org-truely-invisible-p)))
1475 (raw-val (buffer-substring-no-properties
1476 (point)
1477 (progn
1478 (re-search-forward (concat "^[ \t]*#\\+end_verse") nil t)
1479 (point-at-bol))))
1480 (pos-before-blank (progn (forward-line) (point)))
1481 (end (progn (org-skip-whitespace)
1482 (if (eobp) (point) (point-at-bol))))
1483 (value (org-element-parse-secondary-string
1484 (org-remove-indentation raw-val)
1485 (cdr (assq 'verse-block org-element-string-restrictions)))))
1486 `(verse-block
1487 (:begin ,begin
1488 :end ,end
1489 :hiddenp ,hidden
1490 :raw-value ,raw-val
1491 :value ,value
1492 :post-blank ,(count-lines pos-before-blank end)
1493 ,@(cadr keywords))))))
1495 (defun org-element-verse-block-interpreter (verse-block contents)
1496 "Interpret VERSE-BLOCK element as Org syntax.
1497 CONTENTS is nil."
1498 (format "#+begin_verse\n%s#+end_verse"
1499 (org-remove-indentation
1500 (org-element-property :raw-value verse-block))))
1504 ;;; Objects
1506 ;; Unlike to elements, interstices can be found between objects.
1507 ;; That's why, along with the parser, successor functions are provided
1508 ;; for each object. Some objects share the same successor
1509 ;; (i.e. `emphasis' and `verbatim' objects).
1511 ;; A successor must accept a single argument bounding the search. It
1512 ;; will return either a cons cell whose car is the object's type, as
1513 ;; a symbol, and cdr the position of its next occurrence, or nil.
1515 ;; Successors follow the naming convention:
1516 ;; org-element-NAME-successor, where NAME is the name of the
1517 ;; successor, as defined in `org-element-all-successors'.
1519 ;; Some object types (i.e. `emphasis') are recursive. Restrictions on
1520 ;; object types they can contain will be specified in
1521 ;; `org-element-object-restrictions'.
1523 ;; Adding a new type of object is simple. Implement a successor,
1524 ;; a parser, and an interpreter for it, all following the naming
1525 ;; convention. Register type in `org-element-all-objects' and
1526 ;; successor in `org-element-all-successors'. Maybe tweak
1527 ;; restrictions about it, and that's it.
1529 ;;;; Emphasis
1531 (defun org-element-emphasis-parser ()
1532 "Parse text markup object at point.
1534 Return a list whose car is `emphasis' and cdr is a plist with
1535 `:marker', `:begin', `:end', `:contents-begin' and
1536 `:contents-end' and `:post-blank' keywords.
1538 Assume point is at the first emphasis marker."
1539 (save-excursion
1540 (unless (bolp) (backward-char 1))
1541 (looking-at org-emph-re)
1542 (let ((begin (match-beginning 2))
1543 (marker (org-match-string-no-properties 3))
1544 (contents-begin (match-beginning 4))
1545 (contents-end (match-end 4))
1546 (post-blank (progn (goto-char (match-end 2))
1547 (skip-chars-forward " \t")))
1548 (end (point)))
1549 `(emphasis
1550 (:marker ,marker
1551 :begin ,begin
1552 :end ,end
1553 :contents-begin ,contents-begin
1554 :contents-end ,contents-end
1555 :post-blank ,post-blank)))))
1557 (defun org-element-emphasis-interpreter (emphasis contents)
1558 "Interpret EMPHASIS object as Org syntax.
1559 CONTENTS is the contents of the object."
1560 (let ((marker (org-element-property :marker emphasis)))
1561 (concat marker contents marker)))
1563 (defun org-element-text-markup-successor (limit)
1564 "Search for the next emphasis or verbatim object.
1566 LIMIT bounds the search.
1568 Return value is a cons cell whose car is `emphasis' or
1569 `verbatim' and cdr is beginning position."
1570 (save-excursion
1571 (unless (bolp) (backward-char))
1572 (when (re-search-forward org-emph-re limit t)
1573 (cons (if (nth 4 (assoc (match-string 3) org-emphasis-alist))
1574 'verbatim
1575 'emphasis)
1576 (match-beginning 2)))))
1578 ;;;; Entity
1580 (defun org-element-entity-parser ()
1581 "Parse entity at point.
1583 Return a list whose car is `entity' and cdr a plist with
1584 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1585 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1586 keywords.
1588 Assume point is at the beginning of the entity."
1589 (save-excursion
1590 (looking-at "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1591 (let* ((value (org-entity-get (match-string 1)))
1592 (begin (match-beginning 0))
1593 (bracketsp (string= (match-string 2) "{}"))
1594 (post-blank (progn (goto-char (match-end 1))
1595 (when bracketsp (forward-char 2))
1596 (skip-chars-forward " \t")))
1597 (end (point)))
1598 `(entity
1599 (:name ,(car value)
1600 :latex ,(nth 1 value)
1601 :latex-math-p ,(nth 2 value)
1602 :html ,(nth 3 value)
1603 :ascii ,(nth 4 value)
1604 :latin1 ,(nth 5 value)
1605 :utf-8 ,(nth 6 value)
1606 :begin ,begin
1607 :end ,end
1608 :use-brackets-p ,bracketsp
1609 :post-blank ,post-blank)))))
1611 (defun org-element-entity-interpreter (entity contents)
1612 "Interpret ENTITY object as Org syntax.
1613 CONTENTS is nil."
1614 (concat "\\"
1615 (org-element-property :name entity)
1616 (when (org-element-property :use-brackets-p entity) "{}")))
1618 (defun org-element-latex-or-entity-successor (limit)
1619 "Search for the next latex-fragment or entity object.
1621 LIMIT bounds the search.
1623 Return value is a cons cell whose car is `entity' or
1624 `latex-fragment' and cdr is beginning position."
1625 (save-excursion
1626 (let ((matchers (plist-get org-format-latex-options :matchers))
1627 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1628 (entity-re
1629 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"))
1630 (when (re-search-forward
1631 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
1632 matchers "\\|")
1633 "\\|" entity-re)
1634 limit t)
1635 (goto-char (match-beginning 0))
1636 (if (looking-at entity-re)
1637 ;; Determine if it's a real entity or a LaTeX command.
1638 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
1639 (match-beginning 0))
1640 ;; No entity nor command: point is at a LaTeX fragment.
1641 ;; Determine its type to get the correct beginning position.
1642 (cons 'latex-fragment
1643 (catch 'return
1644 (mapc (lambda (e)
1645 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
1646 (throw 'return
1647 (match-beginning
1648 (nth 2 (assoc e org-latex-regexps))))))
1649 matchers)
1650 (point))))))))
1653 ;;;; Export Snippet
1655 (defun org-element-export-snippet-parser ()
1656 "Parse export snippet at point.
1658 Return a list whose car is `export-snippet' and cdr a plist with
1659 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1660 keywords.
1662 Assume point is at the beginning of the snippet."
1663 (save-excursion
1664 (looking-at "@\\([-A-Za-z0-9]+\\){")
1665 (let* ((begin (point))
1666 (back-end (org-match-string-no-properties 1))
1667 (before-blank (progn (goto-char (scan-sexps (1- (match-end 0)) 1))))
1668 (value (buffer-substring-no-properties
1669 (match-end 0) (1- before-blank)))
1670 (post-blank (skip-chars-forward " \t"))
1671 (end (point)))
1672 `(export-snippet
1673 (:back-end ,back-end
1674 :value ,value
1675 :begin ,begin
1676 :end ,end
1677 :post-blank ,post-blank)))))
1679 (defun org-element-export-snippet-interpreter (export-snippet contents)
1680 "Interpret EXPORT-SNIPPET object as Org syntax.
1681 CONTENTS is nil."
1682 (format "@%s{%s}"
1683 (org-element-property :back-end export-snippet)
1684 (org-element-property :value export-snippet)))
1686 (defun org-element-export-snippet-successor (limit)
1687 "Search for the next export-snippet object.
1689 LIMIT bounds the search.
1691 Return value is a cons cell whose car is `export-snippet' cdr is
1692 its beginning position."
1693 (save-excursion
1694 (catch 'exit
1695 (while (re-search-forward "@[-A-Za-z0-9]+{" limit t)
1696 (when (let ((end (ignore-errors (scan-sexps (1- (point)) 1))))
1697 (and end (eq (char-before end) ?})))
1698 (throw 'exit (cons 'export-snippet (match-beginning 0))))))))
1701 ;;;; Footnote Reference
1703 (defun org-element-footnote-reference-parser ()
1704 "Parse footnote reference at point.
1706 Return a list whose car is `footnote-reference' and cdr a plist
1707 with `:label', `:type', `:definition', `:begin', `:end' and
1708 `:post-blank' as keywords."
1709 (save-excursion
1710 (let* ((ref (org-footnote-at-reference-p))
1711 (label (car ref))
1712 (raw-def (nth 3 ref))
1713 (inline-def
1714 (and raw-def
1715 (org-element-parse-secondary-string
1716 raw-def
1717 (cdr (assq 'footnote-reference
1718 org-element-string-restrictions)))))
1719 (type (if (nth 3 ref) 'inline 'standard))
1720 (begin (nth 1 ref))
1721 (post-blank (progn (goto-char (nth 2 ref))
1722 (skip-chars-forward " \t")))
1723 (end (point)))
1724 `(footnote-reference
1725 (:label ,label
1726 :type ,type
1727 :inline-definition ,inline-def
1728 :begin ,begin
1729 :end ,end
1730 :post-blank ,post-blank
1731 :raw-definition ,raw-def)))))
1733 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
1734 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1735 CONTENTS is nil."
1736 (let ((label (or (org-element-property :label footnote-reference) "fn:"))
1737 (def
1738 (let ((raw (org-element-property :raw-definition footnote-reference)))
1739 (if raw (concat ":" raw) ""))))
1740 (format "[%s]" (concat label def))))
1742 (defun org-element-footnote-reference-successor (limit)
1743 "Search for the next footnote-reference object.
1745 LIMIT bounds the search.
1747 Return value is a cons cell whose car is `footnote-reference' and
1748 cdr is beginning position."
1749 (let (fn-ref)
1750 (when (setq fn-ref (org-footnote-get-next-reference nil nil limit))
1751 (cons 'footnote-reference (nth 1 fn-ref)))))
1754 ;;;; Inline Babel Call
1756 (defun org-element-inline-babel-call-parser ()
1757 "Parse inline babel call at point.
1759 Return a list whose car is `inline-babel-call' and cdr a plist with
1760 `:begin', `:end', `:info' and `:post-blank' as keywords.
1762 Assume point is at the beginning of the babel call."
1763 (save-excursion
1764 (unless (bolp) (backward-char))
1765 (looking-at org-babel-inline-lob-one-liner-regexp)
1766 (let ((info (save-match-data (org-babel-lob-get-info)))
1767 (begin (match-end 1))
1768 (post-blank (progn (goto-char (match-end 0))
1769 (skip-chars-forward " \t")))
1770 (end (point)))
1771 `(inline-babel-call
1772 (:begin ,begin
1773 :end ,end
1774 :info ,info
1775 :post-blank ,post-blank)))))
1777 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
1778 "Interpret INLINE-BABEL-CALL object as Org syntax.
1779 CONTENTS is nil."
1780 (let* ((babel-info (org-element-property :info inline-babel-call))
1781 (main-source (car babel-info))
1782 (post-options (nth 1 babel-info)))
1783 (concat "call_"
1784 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
1785 ;; Remove redundant square brackets.
1786 (replace-match
1787 (match-string 1 main-source) nil nil main-source)
1788 main-source)
1789 (and post-options (format "[%s]" post-options)))))
1791 (defun org-element-inline-babel-call-successor (limit)
1792 "Search for the next inline-babel-call object.
1794 LIMIT bounds the search.
1796 Return value is a cons cell whose car is `inline-babel-call' and
1797 cdr is beginning position."
1798 (save-excursion
1799 ;; Use a simplified version of
1800 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
1801 (when (re-search-forward
1802 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
1803 limit t)
1804 (cons 'inline-babel-call (match-beginning 0)))))
1807 ;;;; Inline Src Block
1809 (defun org-element-inline-src-block-parser ()
1810 "Parse inline source block at point.
1812 Return a list whose car is `inline-src-block' and cdr a plist
1813 with `:begin', `:end', `:language', `:value', `:parameters' and
1814 `:post-blank' as keywords.
1816 Assume point is at the beginning of the inline src block."
1817 (save-excursion
1818 (unless (bolp) (backward-char))
1819 (looking-at org-babel-inline-src-block-regexp)
1820 (let ((begin (match-beginning 1))
1821 (language (org-match-string-no-properties 2))
1822 (parameters (org-match-string-no-properties 4))
1823 (value (org-match-string-no-properties 5))
1824 (post-blank (progn (goto-char (match-end 0))
1825 (skip-chars-forward " \t")))
1826 (end (point)))
1827 `(inline-src-block
1828 (:language ,language
1829 :value ,value
1830 :parameters ,parameters
1831 :begin ,begin
1832 :end ,end
1833 :post-blank ,post-blank)))))
1835 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
1836 "Interpret INLINE-SRC-BLOCK object as Org syntax.
1837 CONTENTS is nil."
1838 (let ((language (org-element-property :language inline-src-block))
1839 (arguments (org-element-property :parameters inline-src-block))
1840 (body (org-element-property :value inline-src-block)))
1841 (format "src_%s%s{%s}"
1842 language
1843 (if arguments (format "[%s]" arguments) "")
1844 body)))
1846 (defun org-element-inline-src-block-successor (limit)
1847 "Search for the next inline-babel-call element.
1849 LIMIT bounds the search.
1851 Return value is a cons cell whose car is `inline-babel-call' and
1852 cdr is beginning position."
1853 (save-excursion
1854 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
1855 (cons 'inline-src-block (match-beginning 1)))))
1858 ;;;; Latex Fragment
1860 (defun org-element-latex-fragment-parser ()
1861 "Parse latex fragment at point.
1863 Return a list whose car is `latex-fragment' and cdr a plist with
1864 `:value', `:begin', `:end', and `:post-blank' as keywords.
1866 Assume point is at the beginning of the latex fragment."
1867 (save-excursion
1868 (let* ((begin (point))
1869 (substring-match
1870 (catch 'exit
1871 (mapc (lambda (e)
1872 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
1873 (when (or (looking-at latex-regexp)
1874 (and (not (bobp))
1875 (save-excursion
1876 (backward-char)
1877 (looking-at latex-regexp))))
1878 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
1879 (plist-get org-format-latex-options :matchers))
1880 ;; None found: it's a macro.
1881 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
1883 (value (match-string-no-properties substring-match))
1884 (post-blank (progn (goto-char (match-end substring-match))
1885 (skip-chars-forward " \t")))
1886 (end (point)))
1887 `(latex-fragment
1888 (:value ,value
1889 :begin ,begin
1890 :end ,end
1891 :post-blank ,post-blank)))))
1893 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
1894 "Interpret LATEX-FRAGMENT object as Org syntax.
1895 CONTENTS is nil."
1896 (org-element-property :value latex-fragment))
1898 ;;;; Line Break
1900 (defun org-element-line-break-parser ()
1901 "Parse line break at point.
1903 Return a list whose car is `line-break', and cdr a plist with
1904 `:begin', `:end' and `:post-blank' keywords.
1906 Assume point is at the beginning of the line break."
1907 (let ((begin (point))
1908 (end (save-excursion (forward-line) (point))))
1909 `(line-break (:begin ,begin :end ,end :post-blank 0))))
1911 (defun org-element-line-break-interpreter (line-break contents)
1912 "Interpret LINE-BREAK object as Org syntax.
1913 CONTENTS is nil."
1914 "\\\\\n")
1916 (defun org-element-line-break-successor (limit)
1917 "Search for the next line-break object.
1919 LIMIT bounds the search.
1921 Return value is a cons cell whose car is `line-break' and cdr is
1922 beginning position."
1923 (save-excursion
1924 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
1925 (goto-char (match-beginning 1)))))
1926 ;; A line break can only happen on a non-empty line.
1927 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
1928 (cons 'line-break beg)))))
1931 ;;;; Link
1933 (defun org-element-link-parser ()
1934 "Parse link at point.
1936 Return a list whose car is `link' and cdr a plist with `:type',
1937 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
1938 `:contents-end' and `:post-blank' as keywords.
1940 Assume point is at the beginning of the link."
1941 (save-excursion
1942 (let ((begin (point))
1943 end contents-begin contents-end link-end post-blank path type
1944 raw-link link)
1945 (cond
1946 ;; Type 1: Text targeted from a radio target.
1947 ((and org-target-link-regexp (looking-at org-target-link-regexp))
1948 (setq type "radio"
1949 link-end (match-end 0)
1950 path (org-match-string-no-properties 0)))
1951 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
1952 ((looking-at org-bracket-link-regexp)
1953 (setq contents-begin (match-beginning 3)
1954 contents-end (match-end 3)
1955 link-end (match-end 0)
1956 ;; RAW-LINK is the original link.
1957 raw-link (org-match-string-no-properties 1)
1958 link (org-link-expand-abbrev
1959 (replace-regexp-in-string
1960 " *\n *" " " (org-link-unescape raw-link) t t)))
1961 ;; Determine TYPE of link and set PATH accordingly.
1962 (cond
1963 ;; File type.
1964 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
1965 (setq type "file" path link))
1966 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
1967 ((string-match org-link-re-with-space3 link)
1968 (setq type (match-string 1 link) path (match-string 2 link)))
1969 ;; Ref type: PATH is the name of the target element.
1970 ((string-match "^ref:\\(.*\\)" link)
1971 (setq type "ref" path (org-trim (match-string 1 link))))
1972 ;; Id type: PATH is the id.
1973 ((string-match "^id:\\([-a-f0-9]+\\)" link)
1974 (setq type "id" path (match-string 1 link)))
1975 ;; Code-ref type: PATH is the name of the reference.
1976 ((string-match "^(\\(.*\\))$" link)
1977 (setq type "coderef" path (match-string 1 link)))
1978 ;; Custom-id type: PATH is the name of the custom id.
1979 ((= (aref link 0) ?#)
1980 (setq type "custom-id" path (substring link 1)))
1981 ;; Fuzzy type: Internal link either matches a target, an
1982 ;; headline name or nothing. PATH is the target or headline's
1983 ;; name.
1984 (t (setq type "fuzzy" path link))))
1985 ;; Type 3: Plain link, i.e. http://orgmode.org
1986 ((looking-at org-plain-link-re)
1987 (setq raw-link (org-match-string-no-properties 0)
1988 type (org-match-string-no-properties 1)
1989 path (org-match-string-no-properties 2)
1990 link-end (match-end 0)))
1991 ;; Type 4: Angular link, i.e. <http://orgmode.org>
1992 ((looking-at org-angle-link-re)
1993 (setq raw-link (buffer-substring-no-properties
1994 (match-beginning 1) (match-end 2))
1995 type (org-match-string-no-properties 1)
1996 path (org-match-string-no-properties 2)
1997 link-end (match-end 0))))
1998 ;; In any case, deduce end point after trailing white space from
1999 ;; LINK-END variable.
2000 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
2001 end (point))
2002 `(link
2003 (:type ,type
2004 :path ,path
2005 :raw-link ,(or raw-link path)
2006 :begin ,begin
2007 :end ,end
2008 :contents-begin ,contents-begin
2009 :contents-end ,contents-end
2010 :post-blank ,post-blank)))))
2012 (defun org-element-link-interpreter (link contents)
2013 "Interpret LINK object as Org syntax.
2014 CONTENTS is the contents of the object."
2015 (let ((type (org-element-property :type link))
2016 (raw-link (org-element-property :raw-link link)))
2017 (if (string= type "radio") raw-link
2018 (format "[[%s]%s]"
2019 raw-link
2020 (if (string= contents "") "" (format "[%s]" contents))))))
2022 (defun org-element-link-successor (limit)
2023 "Search for the next link object.
2025 LIMIT bounds the search.
2027 Return value is a cons cell whose car is `link' and cdr is
2028 beginning position."
2029 (save-excursion
2030 (let ((link-regexp
2031 (if (not org-target-link-regexp) org-any-link-re
2032 (concat org-any-link-re "\\|" org-target-link-regexp))))
2033 (when (re-search-forward link-regexp limit t)
2034 (cons 'link (match-beginning 0))))))
2037 ;;;; Macro
2039 (defun org-element-macro-parser ()
2040 "Parse macro at point.
2042 Return a list whose car is `macro' and cdr a plist with `:key',
2043 `:args', `:begin', `:end', `:value' and `:post-blank' as
2044 keywords.
2046 Assume point is at the macro."
2047 (save-excursion
2048 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2049 (let ((begin (point))
2050 (key (downcase (org-match-string-no-properties 1)))
2051 (value (org-match-string-no-properties 0))
2052 (post-blank (progn (goto-char (match-end 0))
2053 (skip-chars-forward " \t")))
2054 (end (point))
2055 (args (let ((args (org-match-string-no-properties 3)) args2)
2056 (when args
2057 (setq args (org-split-string args ","))
2058 (while args
2059 (while (string-match "\\\\\\'" (car args))
2060 ;; Repair bad splits.
2061 (setcar (cdr args) (concat (substring (car args) 0 -1)
2062 "," (nth 1 args)))
2063 (pop args))
2064 (push (pop args) args2))
2065 (mapcar 'org-trim (nreverse args2))))))
2066 `(macro
2067 (:key ,key
2068 :value ,value
2069 :args ,args
2070 :begin ,begin
2071 :end ,end
2072 :post-blank ,post-blank)))))
2074 (defun org-element-macro-interpreter (macro contents)
2075 "Interpret MACRO object as Org syntax.
2076 CONTENTS is nil."
2077 (org-element-property :value macro))
2079 (defun org-element-macro-successor (limit)
2080 "Search for the next macro object.
2082 LIMIT bounds the search.
2084 Return value is cons cell whose car is `macro' and cdr is
2085 beginning position."
2086 (save-excursion
2087 (when (re-search-forward
2088 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2089 limit t)
2090 (cons 'macro (match-beginning 0)))))
2093 ;;;; Radio-target
2095 (defun org-element-radio-target-parser ()
2096 "Parse radio target at point.
2098 Return a list whose car is `radio-target' and cdr a plist with
2099 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2100 and `:post-blank' as keywords.
2102 Assume point is at the radio target."
2103 (save-excursion
2104 (looking-at org-radio-target-regexp)
2105 (let ((begin (point))
2106 (contents-begin (match-beginning 1))
2107 (contents-end (match-end 1))
2108 (raw-value (org-match-string-no-properties 1))
2109 (post-blank (progn (goto-char (match-end 0))
2110 (skip-chars-forward " \t")))
2111 (end (point)))
2112 `(radio-target
2113 (:begin ,begin
2114 :end ,end
2115 :contents-begin ,contents-begin
2116 :contents-end ,contents-end
2117 :raw-value ,raw-value
2118 :post-blank ,post-blank)))))
2120 (defun org-element-radio-target-interpreter (target contents)
2121 "Interpret TARGET object as Org syntax.
2122 CONTENTS is the contents of the object."
2123 (concat "<<<" contents ">>>"))
2125 (defun org-element-radio-target-successor (limit)
2126 "Search for the next radio-target object.
2128 LIMIT bounds the search.
2130 Return value is a cons cell whose car is `radio-target' and cdr
2131 is beginning position."
2132 (save-excursion
2133 (when (re-search-forward org-radio-target-regexp limit t)
2134 (cons 'radio-target (match-beginning 0)))))
2137 ;;;; Statistics Cookie
2139 (defun org-element-statistics-cookie-parser ()
2140 "Parse statistics cookie at point.
2142 Return a list whose car is `statistics-cookie', and cdr a plist
2143 with `:begin', `:end', `:value' and `:post-blank' keywords.
2145 Assume point is at the beginning of the statistics-cookie."
2146 (save-excursion
2147 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2148 (let* ((begin (point))
2149 (value (buffer-substring-no-properties
2150 (match-beginning 0) (match-end 0)))
2151 (post-blank (progn (goto-char (match-end 0))
2152 (skip-chars-forward " \t")))
2153 (end (point)))
2154 `(statistics-cookie
2155 (:begin ,begin
2156 :end ,end
2157 :value ,value
2158 :post-blank ,post-blank)))))
2160 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
2161 "Interpret STATISTICS-COOKIE object as Org syntax.
2162 CONTENTS is nil."
2163 (org-element-property :value statistics-cookie))
2165 (defun org-element-statistics-cookie-successor (limit)
2166 "Search for the next statistics cookie object.
2168 LIMIT bounds the search.
2170 Return value is a cons cell whose car is `statistics-cookie' and
2171 cdr is beginning position."
2172 (save-excursion
2173 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
2174 (cons 'statistics-cookie (match-beginning 0)))))
2177 ;;;; Subscript
2179 (defun org-element-subscript-parser ()
2180 "Parse subscript at point.
2182 Return a list whose car is `subscript' and cdr a plist with
2183 `:begin', `:end', `:contents-begin', `:contents-end',
2184 `:use-brackets-p' and `:post-blank' as keywords.
2186 Assume point is at the underscore."
2187 (save-excursion
2188 (unless (bolp) (backward-char))
2189 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
2191 (not (looking-at org-match-substring-regexp))))
2192 (begin (match-beginning 2))
2193 (contents-begin (or (match-beginning 5)
2194 (match-beginning 3)))
2195 (contents-end (or (match-end 5) (match-end 3)))
2196 (post-blank (progn (goto-char (match-end 0))
2197 (skip-chars-forward " \t")))
2198 (end (point)))
2199 `(subscript
2200 (:begin ,begin
2201 :end ,end
2202 :use-brackets-p ,bracketsp
2203 :contents-begin ,contents-begin
2204 :contents-end ,contents-end
2205 :post-blank ,post-blank)))))
2207 (defun org-element-subscript-interpreter (subscript contents)
2208 "Interpret SUBSCRIPT object as Org syntax.
2209 CONTENTS is the contents of the object."
2210 (format
2211 (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
2212 contents))
2214 (defun org-element-sub/superscript-successor (limit)
2215 "Search for the next sub/superscript object.
2217 LIMIT bounds the search.
2219 Return value is a cons cell whose car is either `subscript' or
2220 `superscript' and cdr is beginning position."
2221 (save-excursion
2222 (when (re-search-forward org-match-substring-regexp limit t)
2223 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
2224 (match-beginning 2)))))
2227 ;;;; Superscript
2229 (defun org-element-superscript-parser ()
2230 "Parse superscript at point.
2232 Return a list whose car is `superscript' and cdr a plist with
2233 `:begin', `:end', `:contents-begin', `:contents-end',
2234 `:use-brackets-p' and `:post-blank' as keywords.
2236 Assume point is at the caret."
2237 (save-excursion
2238 (unless (bolp) (backward-char))
2239 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
2241 (not (looking-at org-match-substring-regexp))))
2242 (begin (match-beginning 2))
2243 (contents-begin (or (match-beginning 5)
2244 (match-beginning 3)))
2245 (contents-end (or (match-end 5) (match-end 3)))
2246 (post-blank (progn (goto-char (match-end 0))
2247 (skip-chars-forward " \t")))
2248 (end (point)))
2249 `(superscript
2250 (:begin ,begin
2251 :end ,end
2252 :use-brackets-p ,bracketsp
2253 :contents-begin ,contents-begin
2254 :contents-end ,contents-end
2255 :post-blank ,post-blank)))))
2257 (defun org-element-superscript-interpreter (superscript contents)
2258 "Interpret SUPERSCRIPT object as Org syntax.
2259 CONTENTS is the contents of the object."
2260 (format
2261 (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
2262 contents))
2265 ;;;; Target
2267 (defun org-element-target-parser ()
2268 "Parse target at point.
2270 Return a list whose car is `target' and cdr a plist with
2271 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2272 and `:post-blank' as keywords.
2274 Assume point is at the target."
2275 (save-excursion
2276 (looking-at org-target-regexp)
2277 (let ((begin (point))
2278 (contents-begin (match-beginning 1))
2279 (contents-end (match-end 1))
2280 (raw-value (org-match-string-no-properties 1))
2281 (post-blank (progn (goto-char (match-end 0))
2282 (skip-chars-forward " \t")))
2283 (end (point)))
2284 `(target
2285 (:begin ,begin
2286 :end ,end
2287 :contents-begin ,contents-begin
2288 :contents-end ,contents-end
2289 :raw-value ,raw-value
2290 :post-blank ,post-blank)))))
2292 (defun org-element-target-interpreter (target contents)
2293 "Interpret TARGET object as Org syntax.
2294 CONTENTS is the contents of target."
2295 (concat ""))
2297 (defun org-element-target-successor (limit)
2298 "Search for the next target object.
2300 LIMIT bounds the search.
2302 Return value is a cons cell whose car is `target' and cdr is
2303 beginning position."
2304 (save-excursion
2305 (when (re-search-forward org-target-regexp limit t)
2306 (cons 'target (match-beginning 0)))))
2309 ;;;; Time-stamp
2311 (defun org-element-time-stamp-parser ()
2312 "Parse time stamp at point.
2314 Return a list whose car is `time-stamp', and cdr a plist with
2315 `:appt-type', `:type', `:begin', `:end', `:value' and
2316 `:post-blank' keywords.
2318 Assume point is at the beginning of the time-stamp."
2319 (save-excursion
2320 (let* ((appt-type (cond
2321 ((looking-at (concat org-deadline-string " +"))
2322 (goto-char (match-end 0))
2323 'deadline)
2324 ((looking-at (concat org-scheduled-string " +"))
2325 (goto-char (match-end 0))
2326 'scheduled)
2327 ((looking-at (concat org-closed-string " +"))
2328 (goto-char (match-end 0))
2329 'closed)))
2330 (begin (and appt-type (match-beginning 0)))
2331 (type (cond
2332 ((looking-at org-tsr-regexp)
2333 (if (match-string 2) 'active-range 'active))
2334 ((looking-at org-tsr-regexp-both)
2335 (if (match-string 2) 'inactive-range 'inactive))
2336 ((looking-at (concat
2337 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2338 "\\|"
2339 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2340 'diary)))
2341 (begin (or begin (match-beginning 0)))
2342 (value (buffer-substring-no-properties
2343 (match-beginning 0) (match-end 0)))
2344 (post-blank (progn (goto-char (match-end 0))
2345 (skip-chars-forward " \t")))
2346 (end (point)))
2347 `(time-stamp
2348 (:appt-type ,appt-type
2349 :type ,type
2350 :value ,value
2351 :begin ,begin
2352 :end ,end
2353 :post-blank ,post-blank)))))
2355 (defun org-element-time-stamp-interpreter (time-stamp contents)
2356 "Interpret TIME-STAMP object as Org syntax.
2357 CONTENTS is nil."
2358 (concat
2359 (case (org-element-property :appt-type time-stamp)
2360 (closed (concat org-closed-string " "))
2361 (deadline (concat org-deadline-string " "))
2362 (scheduled (concat org-scheduled-string " ")))
2363 (org-element-property :value time-stamp)))
2365 (defun org-element-time-stamp-successor (limit)
2366 "Search for the next time-stamp object.
2368 LIMIT bounds the search.
2370 Return value is a cons cell whose car is `time-stamp' and cdr is
2371 beginning position."
2372 (save-excursion
2373 (when (re-search-forward
2374 (concat "\\(?:" org-scheduled-string " +\\|"
2375 org-deadline-string " +\\|" org-closed-string " +\\)?"
2376 org-ts-regexp-both
2377 "\\|"
2378 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2379 "\\|"
2380 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2381 limit t)
2382 (cons 'time-stamp (match-beginning 0)))))
2385 ;;;; Verbatim
2387 (defun org-element-verbatim-parser ()
2388 "Parse verbatim object at point.
2390 Return a list whose car is `verbatim' and cdr is a plist with
2391 `:marker', `:begin', `:end' and `:post-blank' keywords.
2393 Assume point is at the first verbatim marker."
2394 (save-excursion
2395 (unless (bolp) (backward-char 1))
2396 (looking-at org-emph-re)
2397 (let ((begin (match-beginning 2))
2398 (marker (org-match-string-no-properties 3))
2399 (value (org-match-string-no-properties 4))
2400 (post-blank (progn (goto-char (match-end 2))
2401 (skip-chars-forward " \t")))
2402 (end (point)))
2403 `(verbatim
2404 (:marker ,marker
2405 :begin ,begin
2406 :end ,end
2407 :value ,value
2408 :post-blank ,post-blank)))))
2410 (defun org-element-verbatim-interpreter (verbatim contents)
2411 "Interpret VERBATIM object as Org syntax.
2412 CONTENTS is nil."
2413 (let ((marker (org-element-property :marker verbatim))
2414 (value (org-element-property :value verbatim)))
2415 (concat marker value marker)))
2419 ;;; Definitions And Rules
2421 ;; Define elements, greater elements and specify recursive objects,
2422 ;; along with the affiliated keywords recognized. Also set up
2423 ;; restrictions on recursive objects combinations.
2425 ;; These variables really act as a control center for the parsing
2426 ;; process.
2427 (defconst org-element-paragraph-separate
2428 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2429 ;; Headlines and inlinetasks.
2430 org-outline-regexp-bol "\\|"
2431 ;; Comments, blocks (any type), keywords and babel calls.
2432 "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
2433 ;; Lists.
2434 (org-item-beginning-re) "\\|"
2435 ;; Fixed-width, drawers (any type) and tables.
2436 "^[ \t]*[:|]" "\\|"
2437 ;; Footnote definitions.
2438 org-footnote-definition-re "\\|"
2439 ;; Horizontal rules.
2440 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2441 ;; LaTeX environments.
2442 "^[ \t]*\\\\\\(begin\\|end\\)")
2443 "Regexp to separate paragraphs in an Org buffer.")
2445 (defconst org-element-all-elements
2446 '(center-block comment comment-block drawer dynamic-block example-block
2447 export-block fixed-width footnote-definition headline
2448 horizontal-rule inlinetask item keyword latex-environment
2449 babel-call paragraph plain-list property-drawer quote-block
2450 quote-section section special-block src-block table
2451 verse-block)
2452 "Complete list of element types.")
2454 (defconst org-element-greater-elements
2455 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2456 item plain-list quote-block section special-block)
2457 "List of recursive element types aka Greater Elements.")
2459 (defconst org-element-all-successors
2460 '(export-snippet footnote-reference inline-babel-call inline-src-block
2461 latex-or-entity line-break link macro radio-target
2462 statistics-cookie sub/superscript target text-markup
2463 time-stamp)
2464 "Complete list of successors.")
2466 (defconst org-element-object-successor-alist
2467 '((subscript . sub/superscript) (superscript . sub/superscript)
2468 (emphasis . text-markup) (verbatim . text-markup)
2469 (entity . latex-or-entity) (latex-fragment . latex-or-entity))
2470 "Alist of translations between object type and successor name.
2472 Sharing the same successor comes handy when, for example, the
2473 regexp matching one object can also match the other object.")
2475 (defconst org-element-all-objects
2476 '(emphasis entity export-snippet footnote-reference inline-babel-call
2477 inline-src-block line-break latex-fragment link macro radio-target
2478 statistics-cookie subscript superscript target time-stamp
2479 verbatim)
2480 "Complete list of object types.")
2482 (defconst org-element-recursive-objects
2483 '(emphasis link macro subscript superscript target radio-target)
2484 "List of recursive object types.")
2486 (defconst org-element-non-recursive-block-alist
2487 '(("ascii" . export-block)
2488 ("comment" . comment-block)
2489 ("docbook" . export-block)
2490 ("example" . example-block)
2491 ("html" . export-block)
2492 ("latex" . export-block)
2493 ("odt" . export-block)
2494 ("src" . src-block)
2495 ("verse" . verse-block))
2496 "Alist between non-recursive block name and their element type.")
2498 (defconst org-element-affiliated-keywords
2499 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "caption"
2500 "data" "header" "headers" "label" "name" "plot" "resname" "result" "results"
2501 "source" "srcname" "tblname")
2502 "List of affiliated keywords as strings.")
2504 (defconst org-element-keyword-translation-alist
2505 '(("data" . "name") ("label" . "name") ("resname" . "name")
2506 ("source" . "name") ("srcname" . "name") ("tblname" . "name")
2507 ("result" . "results") ("headers" . "header"))
2508 "Alist of usual translations for keywords.
2509 The key is the old name and the value the new one. The property
2510 holding their value will be named after the translated name.")
2512 (defconst org-element-multiple-keywords
2513 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "header")
2514 "List of affiliated keywords that can occur more that once in an element.
2516 Their value will be consed into a list of strings, which will be
2517 returned as the value of the property.
2519 This list is checked after translations have been applied. See
2520 `org-element-keyword-translation-alist'.")
2522 (defconst org-element-parsed-keywords '("author" "caption" "title")
2523 "List of keywords whose value can be parsed.
2525 Their value will be stored as a secondary string: a list of
2526 strings and objects.
2528 This list is checked after translations have been applied. See
2529 `org-element-keyword-translation-alist'.")
2531 (defconst org-element-dual-keywords '("caption" "results")
2532 "List of keywords which can have a secondary value.
2534 In Org syntax, they can be written with optional square brackets
2535 before the colons. For example, results keyword can be
2536 associated to a hash value with the following:
2538 #+results[hash-string]: some-source
2540 This list is checked after translations have been applied. See
2541 `org-element-keyword-translation-alist'.")
2543 (defconst org-element-object-restrictions
2544 '((emphasis entity export-snippet inline-babel-call inline-src-block link
2545 radio-target sub/superscript target text-markup time-stamp)
2546 (link entity export-snippet inline-babel-call inline-src-block
2547 latex-fragment link sub/superscript text-markup)
2548 (macro macro)
2549 (radio-target entity export-snippet latex-fragment sub/superscript)
2550 (subscript entity export-snippet inline-babel-call inline-src-block
2551 latex-fragment sub/superscript text-markup)
2552 (superscript entity export-snippet inline-babel-call inline-src-block
2553 latex-fragment sub/superscript text-markup)
2554 (target entity export-snippet latex-fragment sub/superscript text-markup))
2555 "Alist of recursive objects restrictions.
2557 CAR is a recursive object type and CDR is a list of successors
2558 that will be called within an object of such type.
2560 For example, in a `radio-target' object, one can only find
2561 entities, export snippets, latex-fragments, subscript and
2562 superscript.")
2564 (defconst org-element-string-restrictions
2565 '((footnote-reference entity export-snippet inline-babel-call inline-src-block
2566 latex-fragment line-break link macro radio-target
2567 sub/superscript target text-markup time-stamp)
2568 (headline entity inline-babel-call inline-src-block latex-fragment link
2569 macro radio-target statistics-cookie sub/superscript text-markup
2570 time-stamp)
2571 (inlinetask entity inline-babel-call inline-src-block latex-fragment link
2572 macro radio-target sub/superscript text-markup time-stamp)
2573 (item entity inline-babel-call latex-fragment macro radio-target
2574 sub/superscript target text-markup)
2575 (keyword entity latex-fragment macro sub/superscript text-markup)
2576 (table entity latex-fragment macro target text-markup)
2577 (verse-block entity footnote-reference inline-babel-call inline-src-block
2578 latex-fragment line-break link macro radio-target
2579 sub/superscript target text-markup time-stamp))
2580 "Alist of secondary strings restrictions.
2582 When parsed, some elements have a secondary string which could
2583 contain various objects (i.e. headline's name, or table's cells).
2584 For association, CAR is the element type, and CDR a list of
2585 successors that will be called in that secondary string.
2587 Note: `keyword' secondary string type only applies to keywords
2588 matching `org-element-parsed-keywords'.")
2590 (defconst org-element-secondary-value-alist
2591 '((headline . :title)
2592 (inlinetask . :title)
2593 (item . :tag)
2594 (footnote-reference . :inline-definition)
2595 (verse-block . :value))
2596 "Alist between element types and location of secondary value.
2597 Only elements with a secondary value available at parse time are
2598 considered here. This is used internally by `org-element-map',
2599 which will look into the secondary strings of an element only if
2600 its type is listed here.")
2604 ;;; Accessors
2606 ;; Provide three accessors: `org-element-type', `org-element-property'
2607 ;; and `org-element-contents'.
2609 (defun org-element-type (element)
2610 "Return type of element ELEMENT.
2612 The function returns the type of the element or object provided.
2613 It can also return the following special value:
2614 `plain-text' for a string
2615 `org-data' for a complete document
2616 nil in any other case."
2617 (cond
2618 ((not (consp element)) (and (stringp element) 'plain-text))
2619 ((symbolp (car element)) (car element))))
2621 (defun org-element-property (property element)
2622 "Extract the value from the PROPERTY of an ELEMENT."
2623 (plist-get (nth 1 element) property))
2625 (defun org-element-contents (element)
2626 "Extract contents from an ELEMENT."
2627 (nthcdr 2 element))
2631 ;; Obtaining The Smallest Element Containing Point
2633 ;; `org-element-at-point' is the core function of this section. It
2634 ;; returns the Lisp representation of the element at point. It uses
2635 ;; `org-element-guess-type' and `org-element-skip-keywords' as helper
2636 ;; functions.
2638 ;; When point is at an item, there is no automatic way to determine if
2639 ;; the function should return the `plain-list' element, or the
2640 ;; corresponding `item' element. By default, `org-element-at-point'
2641 ;; works at the `plain-list' level. But, by providing an optional
2642 ;; argument, one can make it switch to the `item' level.
2644 (defconst org-element--affiliated-re
2645 (format "[ \t]*#\\+\\(%s\\):"
2646 (mapconcat
2647 (lambda (keyword)
2648 (if (member keyword org-element-dual-keywords)
2649 (format "\\(%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
2650 (regexp-quote keyword))
2651 (regexp-quote keyword)))
2652 org-element-affiliated-keywords "\\|"))
2653 "Regexp matching any affiliated keyword.
2655 Keyword name is put in match group 1. Moreover, if keyword
2656 belongs to `org-element-dual-keywords', put the dual value in
2657 match group 2.
2659 Don't modify it, set `org-element--affiliated-keywords' instead.")
2661 (defun org-element-at-point (&optional special structure)
2662 "Determine closest element around point.
2664 Return value is a list \(TYPE PROPS\) where TYPE is the type of
2665 the element and PROPS a plist of properties associated to the
2666 element.
2668 Possible types are defined in `org-element-all-elements'.
2670 Optional argument SPECIAL, when non-nil, can be either `item' or
2671 `section'. The former allows to parse item wise instead of
2672 plain-list wise, using STRUCTURE as the current list structure.
2673 The latter will try to parse a section before anything else.
2675 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
2676 be computed."
2677 (save-excursion
2678 (beginning-of-line)
2679 ;; Move before any blank line.
2680 (when (looking-at "[ \t]*$")
2681 (skip-chars-backward " \r\t\n")
2682 (beginning-of-line))
2683 (let ((case-fold-search t))
2684 ;; Check if point is at an affiliated keyword. In that case,
2685 ;; try moving to the beginning of the associated element. If
2686 ;; the keyword is orphaned, treat it as plain text.
2687 (when (looking-at org-element--affiliated-re)
2688 (let ((opoint (point)))
2689 (while (looking-at org-element--affiliated-re) (forward-line))
2690 (when (looking-at "[ \t]*$") (goto-char opoint))))
2691 (let ((type (org-element-guess-type (eq special 'section))))
2692 (cond
2693 ;; Guessing element type on the current line is impossible:
2694 ;; try to find the beginning of the current element to get
2695 ;; more information.
2696 ((not type)
2697 (let ((search-origin (point))
2698 (opoint-in-item-p (org-in-item-p))
2699 (par-found-p
2700 (progn
2701 (end-of-line)
2702 (re-search-backward org-element-paragraph-separate nil 'm))))
2703 (cond
2704 ;; Unable to find a paragraph delimiter above: we're at
2705 ;; bob and looking at a paragraph.
2706 ((not par-found-p) (org-element-paragraph-parser))
2707 ;; Trying to find element's beginning set point back to
2708 ;; its original position. There's something peculiar on
2709 ;; this line that prevents parsing, probably an
2710 ;; ill-formed keyword or an undefined drawer name. Parse
2711 ;; it as plain text anyway.
2712 ((< search-origin (point-at-eol)) (org-element-paragraph-parser))
2713 ;; Original point wasn't in a list but previous paragraph
2714 ;; is. It means that either point was inside some block,
2715 ;; or current list was ended without using a blank line.
2716 ;; In the last case, paragraph really starts at list end.
2717 ((let (item)
2718 (and (not opoint-in-item-p)
2719 (not (looking-at "[ \t]*#\\+begin"))
2720 (setq item (org-in-item-p))
2721 (let ((struct (save-excursion (goto-char item)
2722 (org-list-struct))))
2723 (goto-char (org-list-get-bottom-point struct))
2724 (org-skip-whitespace)
2725 (beginning-of-line)
2726 (org-element-paragraph-parser)))))
2727 ((org-footnote-at-definition-p)
2728 (org-element-footnote-definition-parser))
2729 ((and opoint-in-item-p (org-at-item-p) (= opoint-in-item-p (point)))
2730 (if (eq special 'item)
2731 (org-element-item-parser (or structure (org-list-struct)))
2732 (org-element-plain-list-parser (or structure (org-list-struct)))))
2733 ;; In any other case, the paragraph started the line
2734 ;; below.
2735 (t (forward-line) (org-element-paragraph-parser)))))
2736 ((eq type 'plain-list)
2737 (if (eq special 'item)
2738 (org-element-item-parser (or structure (org-list-struct)))
2739 (org-element-plain-list-parser (or structure (org-list-struct)))))
2740 ;; Straightforward case: call the appropriate parser.
2741 (t (funcall (intern (format "org-element-%s-parser" type)))))))))
2744 ;; It is obvious to tell if point is in most elements, either by
2745 ;; looking for a specific regexp in the current line, or by using
2746 ;; already implemented functions. This is the goal of
2747 ;; `org-element-guess-type'.
2749 (defconst org-element--element-block-types
2750 (mapcar 'car org-element-non-recursive-block-alist)
2751 "List of non-recursive block types, as strings.
2752 Used internally by `org-element-guess-type'. Do not modify it
2753 directly, set `org-element-non-recursive-block-alist' instead.")
2755 (defun org-element-guess-type (&optional section-mode)
2756 "Return the type of element at point, or nil if undetermined.
2758 This function may move point to an appropriate position for
2759 parsing. Used internally by `org-element-at-point'.
2761 When optional argument SECTION-MODE is non-nil, try to find if
2762 point is in a section in priority."
2763 ;; Beware: Order matters for some cases in that function.
2764 (beginning-of-line)
2765 (let ((case-fold-search t))
2766 (cond
2767 ((org-with-limited-levels (org-at-heading-p)) 'headline)
2768 ((let ((headline (ignore-errors (nth 4 (org-heading-components)))))
2769 (and headline
2770 (let (case-fold-search)
2771 (string-match (format "^%s\\(?: \\|$\\)" org-quote-string)
2772 headline))))
2773 ;; Move to section beginning.
2774 (org-back-to-heading t)
2775 (forward-line)
2776 (org-skip-whitespace)
2777 (beginning-of-line)
2778 'quote-section)
2779 ;; Any buffer position not at an headline or in a quote section
2780 ;; is inside a section, provided function is actively looking for
2781 ;; them.
2782 (section-mode 'section)
2783 ;; Non-recursive block.
2784 ((let ((type (org-in-block-p org-element--element-block-types)))
2785 (and type (cdr (assoc type org-element-non-recursive-block-alist)))))
2786 ((org-at-heading-p) 'inlinetask)
2787 ((org-between-regexps-p
2788 "^[ \t]*\\\\begin{" "^[ \t]*\\\\end{[^}]*}[ \t]*") 'latex-environment)
2789 ;; Property drawer. Almost `org-at-property-p', but allow drawer
2790 ;; boundaries.
2791 ((org-with-wide-buffer
2792 (and (not (org-before-first-heading-p))
2793 (let ((pblock (org-get-property-block)))
2794 (and pblock
2795 (<= (point) (cdr pblock))
2796 (>= (point-at-eol) (1- (car pblock)))))))
2797 'property-drawer)
2798 ;; Recursive block. If the block isn't complete, parse the
2799 ;; current part as a paragraph.
2800 ((looking-at "[ \t]*#\\+\\(begin\\|end\\)_\\([-A-Za-z0-9]+\\)\\(?:$\\|\\s-\\)")
2801 (let ((type (downcase (match-string 2))))
2802 (cond
2803 ((not (org-in-block-p (list type))) 'paragraph)
2804 ((string= type "center") 'center-block)
2805 ((string= type "quote") 'quote-block)
2806 (t 'special-block))))
2807 ;; Regular drawers must be tested after property drawer as both
2808 ;; elements share the same ending regexp.
2809 ((or (looking-at org-drawer-regexp) (looking-at "[ \t]*:END:[ \t]*$"))
2810 (let ((completep (org-between-regexps-p
2811 org-drawer-regexp "^[ \t]*:END:[ \t]*$")))
2812 (if (not completep) 'paragraph
2813 (goto-char (car completep)) 'drawer)))
2814 ((looking-at "[ \t]*:\\( \\|$\\)") 'fixed-width)
2815 ;; Babel calls must be tested before general keywords as they are
2816 ;; a subset of them.
2817 ((looking-at org-babel-block-lob-one-liner-regexp) 'babel-call)
2818 ((looking-at org-footnote-definition-re) 'footnote-definition)
2819 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
2820 (if (member (downcase (match-string 1)) org-element-affiliated-keywords)
2821 'paragraph
2822 'keyword))
2823 ;; Dynamic block: simplify regexp used for match. If it isn't
2824 ;; complete, parse the current part as a paragraph.
2825 ((looking-at "[ \t]*#\\+\\(begin\\end\\):\\(?:\\s-\\|$\\)")
2826 (let ((completep (org-between-regexps-p
2827 "^[ \t]*#\\+begin:\\(?:\\s-\\|$\\)"
2828 "^[ \t]*#\\+end:\\(?:\\s-\\|$\\)")))
2829 (if (not completep) 'paragraph
2830 (goto-char (car completep)) 'dynamic-block)))
2831 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)") 'comment)
2832 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$") 'horizontal-rule)
2833 ((org-at-table-p t) 'table)
2834 ((looking-at "[ \t]*#\\+tblfm:")
2835 (forward-line -1)
2836 ;; A TBLFM line separated from any table is just plain text.
2837 (if (org-at-table-p) 'table
2838 (forward-line) 'paragraph))
2839 ((looking-at (org-item-re)) 'plain-list))))
2841 ;; Most elements can have affiliated keywords. When looking for an
2842 ;; element beginning, we want to move before them, as they belong to
2843 ;; that element, and, in the meantime, collect information they give
2844 ;; into appropriate properties. Hence the following function.
2846 ;; Usage of optional arguments may not be obvious at first glance:
2848 ;; - TRANS-LIST is used to polish keywords names that have evolved
2849 ;; during Org history. In example, even though =result= and
2850 ;; =results= coexist, we want to have them under the same =result=
2851 ;; property. It's also true for "srcname" and "name", where the
2852 ;; latter seems to be preferred nowadays (thus the "name" property).
2854 ;; - CONSED allows to regroup multi-lines keywords under the same
2855 ;; property, while preserving their own identity. This is mostly
2856 ;; used for "attr_latex" and al.
2858 ;; - PARSED prepares a keyword value for export. This is useful for
2859 ;; "caption". Objects restrictions for such keywords are defined in
2860 ;; `org-element-string-restrictions'.
2862 ;; - DUALS is used to take care of keywords accepting a main and an
2863 ;; optional secondary values. For example "results" has its
2864 ;; source's name as the main value, and may have an hash string in
2865 ;; optional square brackets as the secondary one.
2867 ;; A keyword may belong to more than one category.
2869 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
2870 consed parsed duals)
2871 "Collect affiliated keywords before point.
2873 Optional argument KEY-RE is a regexp matching keywords, which
2874 puts matched keyword in group 1. It defaults to
2875 `org-element--affiliated-re'.
2877 TRANS-LIST is an alist where key is the keyword and value the
2878 property name it should be translated to, without the colons. It
2879 defaults to `org-element-keyword-translation-alist'.
2881 CONSED is a list of strings. Any keyword belonging to that list
2882 will have its value consed. The check is done after keyword
2883 translation. It defaults to `org-element-multiple-keywords'.
2885 PARSED is a list of strings. Any keyword member of this list
2886 will have its value parsed. The check is done after keyword
2887 translation. If a keyword is a member of both CONSED and PARSED,
2888 it's value will be a list of parsed strings. It defaults to
2889 `org-element-parsed-keywords'.
2891 DUALS is a list of strings. Any keyword member of this list can
2892 have two parts: one mandatory and one optional. Its value is
2893 a cons cell whose car is the former, and the cdr the latter. If
2894 a keyword is a member of both PARSED and DUALS, both values will
2895 be parsed. It defaults to `org-element-dual-keywords'.
2897 Return a list whose car is the position at the first of them and
2898 cdr a plist of keywords and values."
2899 (save-excursion
2900 (let ((case-fold-search t)
2901 (key-re (or key-re org-element--affiliated-re))
2902 (trans-list (or trans-list org-element-keyword-translation-alist))
2903 (consed (or consed org-element-multiple-keywords))
2904 (parsed (or parsed org-element-parsed-keywords))
2905 (duals (or duals org-element-dual-keywords))
2906 ;; RESTRICT is the list of objects allowed in parsed
2907 ;; keywords value.
2908 (restrict (cdr (assq 'keyword org-element-string-restrictions)))
2909 output)
2910 (unless (bobp)
2911 (while (and (not (bobp))
2912 (progn (forward-line -1) (looking-at key-re)))
2913 (let* ((raw-kwd (downcase (or (match-string 2) (match-string 1))))
2914 ;; Apply translation to RAW-KWD. From there, KWD is
2915 ;; the official keyword.
2916 (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
2917 ;; Find main value for any keyword.
2918 (value
2919 (save-match-data
2920 (org-trim
2921 (buffer-substring-no-properties
2922 (match-end 0) (point-at-eol)))))
2923 ;; If KWD is a dual keyword, find its secondary
2924 ;; value. Maybe parse it.
2925 (dual-value
2926 (and (member kwd duals)
2927 (let ((sec (org-match-string-no-properties 3)))
2928 (if (or (not sec) (not (member kwd parsed))) sec
2929 (org-element-parse-secondary-string sec restrict)))))
2930 ;; Attribute a property name to KWD.
2931 (kwd-sym (and kwd (intern (concat ":" kwd)))))
2932 ;; Now set final shape for VALUE.
2933 (when (member kwd parsed)
2934 (setq value (org-element-parse-secondary-string value restrict)))
2935 (when (member kwd duals)
2936 ;; VALUE is mandatory. Set it to nil if there is none.
2937 (setq value (and value (cons value dual-value))))
2938 (when (member kwd consed)
2939 (setq value (cons value (plist-get output kwd-sym))))
2940 ;; Eventually store the new value in OUTPUT.
2941 (setq output (plist-put output kwd-sym value))))
2942 (unless (looking-at key-re) (forward-line 1)))
2943 (list (point) output))))
2947 ;;; The Org Parser
2949 ;; The two major functions here are `org-element-parse-buffer', which
2950 ;; parses Org syntax inside the current buffer, taking into account
2951 ;; region, narrowing, or even visibility if specified, and
2952 ;; `org-element-parse-secondary-string', which parses objects within
2953 ;; a given string.
2955 ;; The (almost) almighty `org-element-map' allows to apply a function
2956 ;; on elements or objects matching some type, and accumulate the
2957 ;; resulting values. In an export situation, it also skips unneeded
2958 ;; parts of the parse tree, transparently walks into included files,
2959 ;; and maintain a list of local properties (i.e. those inherited from
2960 ;; parent headlines) for function's consumption.
2962 (defun org-element-parse-buffer (&optional granularity visible-only)
2963 "Recursively parse the buffer and return structure.
2964 If narrowing is in effect, only parse the visible part of the
2965 buffer.
2967 Optional argument GRANULARITY determines the depth of the
2968 recursion. It can be set to the following symbols:
2970 `headline' Only parse headlines.
2971 `greater-element' Don't recurse into greater elements. Thus,
2972 elements parsed are the top-level ones.
2973 `element' Parse everything but objects and plain text.
2974 `object' Parse the complete buffer (default).
2976 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
2977 elements.
2979 Assume buffer is in Org mode."
2980 (save-excursion
2981 (goto-char (point-min))
2982 (org-skip-whitespace)
2983 (nconc (list 'org-data nil)
2984 (org-element-parse-elements
2985 (point-at-bol) (point-max)
2986 ;; Start is section mode so text before the first headline
2987 ;; belongs to a section.
2988 'section nil granularity visible-only nil))))
2990 (defun org-element-parse-secondary-string (string restriction &optional buffer)
2991 "Recursively parse objects in STRING and return structure.
2993 RESTRICTION, when non-nil, is a symbol limiting the object types
2994 that will be looked after.
2996 Optional argument BUFFER indicates the buffer from where the
2997 secondary string was extracted. It is used to determine where to
2998 get extraneous information for an object \(i.e. when resolving
2999 a link or looking for a footnote definition\). It defaults to
3000 the current buffer."
3001 (with-temp-buffer
3002 (insert string)
3003 (org-element-parse-objects (point-min) (point-max) nil restriction)))
3005 (defun org-element-map (data types fun &optional info first-match)
3006 "Map a function on selected elements or objects.
3008 DATA is the parsed tree, as returned by, i.e,
3009 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
3010 of elements or objects types. FUN is the function called on the
3011 matching element or object. It must accept two arguments: the
3012 element or object itself and a plist holding contextual
3013 information.
3015 When optional argument INFO is non-nil, it should be a plist
3016 holding export options. In that case, parts of the parse tree
3017 not exportable according to that property list will be skipped.
3019 When optional argument FIRST-MATCH is non-nil, stop at the first
3020 match for which FUN doesn't return nil, and return that value.
3022 Nil values returned from FUN are ignored in the result."
3023 ;; Ensure TYPES is a list, even of one element.
3024 (unless (listp types) (setq types (list types)))
3025 ;; Recursion depth is determined by --CATEGORY.
3026 (let* ((--category
3027 (cond
3028 ((loop for type in types
3029 always (memq type org-element-greater-elements))
3030 'greater-elements)
3031 ((loop for type in types
3032 always (memq type org-element-all-elements))
3033 'elements)
3034 (t 'objects)))
3035 ;; --RESTRICTS is a list of element types whose secondary
3036 ;; string could possibly contain an object with a type among
3037 ;; TYPES.
3038 (--restricts
3039 (and (eq --category 'objects)
3040 (loop for el in org-element-secondary-value-alist
3041 when
3042 (loop for o in types
3043 thereis
3044 (memq o (cdr
3045 (assq (car el)
3046 org-element-string-restrictions))))
3047 collect (car el))))
3048 --walk-tree ; For byte-compiler
3049 --acc
3050 (--check-blob
3051 (function
3052 (lambda (--type types fun --blob info)
3053 ;; Check if TYPE is matching among TYPES. If so, apply
3054 ;; FUN to --BLOB and accumulate return value into --ACC.
3055 ;; INFO is the communication channel. If --BLOB has
3056 ;; a secondary string that can contain objects with their
3057 ;; type amond TYPES, look into that string first.
3058 (when (memq --type --restricts)
3059 (funcall
3060 --walk-tree
3061 `(org-data
3063 ,@(org-element-property
3064 (cdr (assq --type org-element-secondary-value-alist))
3065 --blob))
3066 info))
3067 (when (memq --type types)
3068 (let ((result (funcall fun --blob info)))
3069 (cond ((not result))
3070 (first-match (throw 'first-match result))
3071 (t (push result --acc))))))))
3072 (--walk-tree
3073 (function
3074 (lambda (--data info)
3075 ;; Recursively walk DATA. INFO, if non-nil, is
3076 ;; a plist holding contextual information.
3077 (mapc
3078 (lambda (--blob)
3079 (let ((--type (org-element-type --blob)))
3080 ;; Determine if a recursion into --BLOB is
3081 ;; possible and allowed.
3082 (cond
3083 ;; Element or object not exportable.
3084 ((and info (member --blob (plist-get info :ignore-list))))
3085 ;; Limiting recursion to greater elements, and --BLOB
3086 ;; isn't one.
3087 ((and (eq --category 'greater-elements)
3088 (not (memq --type org-element-greater-elements)))
3089 (funcall --check-blob --type types fun --blob info))
3090 ;; Limiting recursion to elements, and --BLOB only
3091 ;; contains objects.
3092 ((and (eq --category 'elements) (eq --type 'paragraph)))
3093 ;; No limitation on recursion, but --BLOB hasn't
3094 ;; got a recursive type.
3095 ((and (eq --category 'objects)
3096 (not (or (eq --type 'paragraph)
3097 (memq --type org-element-greater-elements)
3098 (memq --type org-element-recursive-objects))))
3099 (funcall --check-blob --type types fun --blob info))
3100 ;; Recursion is possible and allowed: Maybe apply
3101 ;; FUN to --BLOB, then move into it.
3102 (t (funcall --check-blob --type types fun --blob info)
3103 (funcall --walk-tree --blob info)))))
3104 (org-element-contents --data))))))
3105 (catch 'first-match
3106 (funcall --walk-tree data info)
3107 ;; Return value in a proper order.
3108 (reverse --acc))))
3110 ;; The following functions are internal parts of the parser.
3112 ;; The first one, `org-element-parse-elements' acts at the element's
3113 ;; level. As point is always at the beginning of an element during
3114 ;; parsing, it doesn't have to rely on `org-element-at-point'.
3115 ;; Instead, it calls a more restrictive, though way quicker,
3116 ;; alternative: `org-element-current-element'. That function
3117 ;; internally uses `org-element--element-block-re' for quick access to
3118 ;; a common regexp.
3120 ;; The second one, `org-element-parse-objects' applies on all objects
3121 ;; of a paragraph or a secondary string. It uses
3122 ;; `org-element-get-candidates' to optimize the search of the next
3123 ;; object in the buffer.
3125 ;; More precisely, that function looks for every allowed object type
3126 ;; first. Then, it discards failed searches, keeps further matches,
3127 ;; and searches again types matched behind point, for subsequent
3128 ;; calls. Thus, searching for a given type fails only once, and every
3129 ;; object is searched only once at top level (but sometimes more for
3130 ;; nested types).
3132 (defun org-element-parse-elements
3133 (beg end special structure granularity visible-only acc)
3134 "Parse elements between BEG and END positions.
3136 SPECIAL prioritize some elements over the others. It can set to
3137 `quote-section', `section' or `item', which will focus search,
3138 respectively, on quote sections, sections and items. Moreover,
3139 when value is `item', STRUCTURE will be used as the current list
3140 structure.
3142 GRANULARITY determines the depth of the recursion. It can be set
3143 to the following symbols:
3145 `headline' Only parse headlines.
3146 `greater-element' Don't recurse into greater elements. Thus,
3147 elements parsed are the top-level ones.
3148 `element' Parse everything but objects and plain text.
3149 `object' or nil Parse the complete buffer.
3151 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3152 elements.
3154 Elements are accumulated into ACC."
3155 (save-excursion
3156 (save-restriction
3157 (narrow-to-region beg end)
3158 (goto-char beg)
3159 ;; When parsing only headlines, skip any text before first one.
3160 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
3161 (org-with-limited-levels (outline-next-heading)))
3162 ;; Main loop start.
3163 (while (not (eobp))
3164 (push
3165 ;; 1. Item mode is active: point must be at an item. Parse it
3166 ;; directly, skipping `org-element-current-element'.
3167 (if (eq special 'item)
3168 (let ((element (org-element-item-parser structure)))
3169 (goto-char (org-element-property :end element))
3170 (org-element-parse-elements
3171 (org-element-property :contents-begin element)
3172 (org-element-property :contents-end element)
3173 nil structure granularity visible-only (reverse element)))
3174 ;; 2. When ITEM is nil, find current element's type and parse
3175 ;; it accordingly to its category.
3176 (let ((element (org-element-current-element special structure)))
3177 (goto-char (org-element-property :end element))
3178 (cond
3179 ;; Case 1. ELEMENT is a paragraph. Parse objects inside,
3180 ;; if GRANULARITY allows it.
3181 ((and (eq (org-element-type element) 'paragraph)
3182 (or (not granularity) (eq granularity 'object)))
3183 (org-element-parse-objects
3184 (org-element-property :contents-begin element)
3185 (org-element-property :contents-end element)
3186 (reverse element) nil))
3187 ;; Case 2. ELEMENT is recursive: parse it between
3188 ;; `contents-begin' and `contents-end'. Make sure
3189 ;; GRANULARITY allows the recursion, or ELEMENT is an
3190 ;; headline, in which case going inside is mandatory, in
3191 ;; order to get sub-level headings. If VISIBLE-ONLY is
3192 ;; true and element is hidden, do not recurse into it.
3193 ((and (memq (org-element-type element) org-element-greater-elements)
3194 (or (not granularity)
3195 (memq granularity '(element object))
3196 (eq (org-element-type element) 'headline))
3197 (not (and visible-only
3198 (org-element-property :hiddenp element))))
3199 (org-element-parse-elements
3200 (org-element-property :contents-begin element)
3201 (org-element-property :contents-end element)
3202 ;; At a plain list, switch to item mode. At an
3203 ;; headline, switch to section mode. Any other
3204 ;; element turns off special modes.
3205 (case (org-element-type element)
3206 (plain-list 'item)
3207 (headline (if (org-element-property :quotedp element)
3208 'quote-section
3209 'section)))
3210 (org-element-property :structure element)
3211 granularity visible-only (reverse element)))
3212 ;; Case 3. Else, just accumulate ELEMENT.
3213 (t element))))
3214 acc)))
3215 ;; Return result.
3216 (nreverse acc)))
3218 (defconst org-element--element-block-re
3219 (format "[ \t]*#\\+begin_\\(%s\\)\\(?: \\|$\\)"
3220 (mapconcat
3221 'regexp-quote
3222 (mapcar 'car org-element-non-recursive-block-alist) "\\|"))
3223 "Regexp matching the beginning of a non-recursive block type.
3224 Used internally by `org-element-current-element'. Do not modify
3225 it directly, set `org-element-recursive-block-alist' instead.")
3227 (defun org-element-current-element (&optional special structure)
3228 "Parse the element at point.
3230 Return value is a list \(TYPE PROPS\) where TYPE is the type of
3231 the element and PROPS a plist of properties associated to the
3232 element.
3234 Possible types are defined in `org-element-all-elements'.
3236 Optional argument SPECIAL, when non-nil, can be either `item',
3237 `section' or `quote-section'. `item' allows to parse item wise
3238 instead of plain-list wise, using STRUCTURE as the current list
3239 structure. `section' (resp. `quote-section') will try to parse
3240 a section (resp. a quote section) before anything else.
3242 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
3243 be computed.
3245 Unlike to `org-element-at-point', this function assumes point is
3246 always at the beginning of the element it has to parse. As such,
3247 it is quicker than its counterpart and always accurate, albeit
3248 more restrictive."
3249 (save-excursion
3250 (beginning-of-line)
3251 ;; If point is at an affiliated keyword, try moving to the
3252 ;; beginning of the associated element. If none is found, the
3253 ;; keyword is orphaned and will be treated as plain text.
3254 (when (looking-at org-element--affiliated-re)
3255 (let ((opoint (point)))
3256 (while (looking-at org-element--affiliated-re) (forward-line))
3257 (when (looking-at "[ \t]*$") (goto-char opoint))))
3258 (let ((case-fold-search t))
3259 (cond
3260 ;; Headline.
3261 ((org-with-limited-levels (org-at-heading-p))
3262 (org-element-headline-parser))
3263 ;; Quote section.
3264 ((eq special 'quote-section) (org-element-quote-section-parser))
3265 ;; Section.
3266 ((eq special 'section) (org-element-section-parser))
3267 ;; Non-recursive block.
3268 ((when (looking-at org-element--element-block-re)
3269 (let ((type (downcase (match-string 1))))
3270 (if (save-excursion
3271 (re-search-forward
3272 (format "[ \t]*#\\+end_%s\\(?: \\|$\\)" type) nil t))
3273 ;; Build appropriate parser.
3274 (funcall
3275 (intern
3276 (format "org-element-%s-parser"
3277 (cdr (assoc type
3278 org-element-non-recursive-block-alist)))))
3279 (org-element-paragraph-parser)))))
3280 ;; Inlinetask.
3281 ((org-at-heading-p) (org-element-inlinetask-parser))
3282 ;; LaTeX Environment or paragraph if incomplete.
3283 ((looking-at "^[ \t]*\\\\begin{")
3284 (if (save-excursion
3285 (re-search-forward "^[ \t]*\\\\end{[^}]*}[ \t]*" nil t))
3286 (org-element-latex-environment-parser)
3287 (org-element-paragraph-parser)))
3288 ;; Property drawer.
3289 ((looking-at org-property-start-re)
3290 (if (save-excursion (re-search-forward org-property-end-re nil t))
3291 (org-element-property-drawer-parser)
3292 (org-element-paragraph-parser)))
3293 ;; Recursive block, or paragraph if incomplete.
3294 ((looking-at "[ \t]*#\\+begin_\\([-A-Za-z0-9]+\\)\\(?: \\|$\\)")
3295 (let ((type (downcase (match-string 1))))
3296 (cond
3297 ((not (save-excursion
3298 (re-search-forward
3299 (format "[ \t]*#\\+end_%s\\(?: \\|$\\)" type) nil t)))
3300 (org-element-paragraph-parser))
3301 ((string= type "center") (org-element-center-block-parser))
3302 ((string= type "quote") (org-element-quote-block-parser))
3303 (t (org-element-special-block-parser)))))
3304 ;; Drawer.
3305 ((looking-at org-drawer-regexp)
3306 (if (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" nil t))
3307 (org-element-drawer-parser)
3308 (org-element-paragraph-parser)))
3309 ((looking-at "[ \t]*:\\( \\|$\\)") (org-element-fixed-width-parser))
3310 ;; Babel call.
3311 ((looking-at org-babel-block-lob-one-liner-regexp)
3312 (org-element-babel-call-parser))
3313 ;; Keyword, or paragraph if at an affiliated keyword.
3314 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
3315 (let ((key (downcase (match-string 1))))
3316 (if (or (string= key "tblfm")
3317 (member key org-element-affiliated-keywords))
3318 (org-element-paragraph-parser)
3319 (org-element-keyword-parser))))
3320 ;; Footnote definition.
3321 ((looking-at org-footnote-definition-re)
3322 (org-element-footnote-definition-parser))
3323 ;; Dynamic block or paragraph if incomplete.
3324 ((looking-at "[ \t]*#\\+begin:\\(?: \\|$\\)")
3325 (if (save-excursion
3326 (re-search-forward "^[ \t]*#\\+end:\\(?: \\|$\\)" nil t))
3327 (org-element-dynamic-block-parser)
3328 (org-element-paragraph-parser)))
3329 ;; Comment.
3330 ((looking-at "\\(#\\|[ \t]*#\\+\\(?: \\|$\\)\\)")
3331 (org-element-comment-parser))
3332 ;; Horizontal rule.
3333 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
3334 (org-element-horizontal-rule-parser))
3335 ;; Table.
3336 ((org-at-table-p t) (org-element-table-parser))
3337 ;; List or item.
3338 ((looking-at (org-item-re))
3339 (if (eq special 'item)
3340 (org-element-item-parser (or structure (org-list-struct)))
3341 (org-element-plain-list-parser (or structure (org-list-struct)))))
3342 ;; Default element: Paragraph.
3343 (t (org-element-paragraph-parser))))))
3345 (defun org-element-parse-objects (beg end acc restriction)
3346 "Parse objects between BEG and END and return recursive structure.
3348 Objects are accumulated in ACC.
3350 RESTRICTION, when non-nil, is a list of object types which are
3351 allowed in the current object."
3352 (let ((get-next-object
3353 (function
3354 (lambda (cand)
3355 ;; Return the parsing function associated to the nearest
3356 ;; object among list of candidates CAND.
3357 (let ((pos (apply #'min (mapcar #'cdr cand))))
3358 (save-excursion
3359 (goto-char pos)
3360 (funcall
3361 (intern
3362 (format "org-element-%s-parser" (car (rassq pos cand))))))))))
3363 next-object candidates)
3364 (save-excursion
3365 (goto-char beg)
3366 (while (setq candidates (org-element-get-next-object-candidates
3367 end restriction candidates))
3368 (setq next-object (funcall get-next-object candidates))
3369 ;; 1. Text before any object. Untabify it.
3370 (let ((obj-beg (org-element-property :begin next-object)))
3371 (unless (= (point) obj-beg)
3372 (push (replace-regexp-in-string
3373 "\t" (make-string tab-width ? )
3374 (buffer-substring-no-properties (point) obj-beg))
3375 acc)))
3376 ;; 2. Object...
3377 (let ((obj-end (org-element-property :end next-object))
3378 (cont-beg (org-element-property :contents-begin next-object)))
3379 (push (if (and (memq (car next-object) org-element-recursive-objects)
3380 cont-beg)
3381 ;; ... recursive. The CONT-BEG check is for
3382 ;; links, as some of them might not be recursive
3383 ;; (i.e. plain links).
3384 (save-restriction
3385 (narrow-to-region
3386 cont-beg
3387 (org-element-property :contents-end next-object))
3388 (org-element-parse-objects
3389 (point-min) (point-max) (reverse next-object)
3390 ;; Restrict allowed objects. This is the
3391 ;; intersection of current restriction and next
3392 ;; object's restriction.
3393 (let ((new-restr
3394 (cdr (assq (car next-object)
3395 org-element-object-restrictions))))
3396 (if (not restriction) new-restr
3397 (delq nil (mapcar
3398 (lambda (e) (and (memq e restriction) e))
3399 new-restr))))))
3400 ;; ... not recursive.
3401 next-object)
3402 acc)
3403 (goto-char obj-end)))
3404 ;; 3. Text after last object. Untabify it.
3405 (unless (= (point) end)
3406 (push (replace-regexp-in-string
3407 "\t" (make-string tab-width ? )
3408 (buffer-substring-no-properties (point) end))
3409 acc))
3410 ;; Result.
3411 (nreverse acc))))
3413 (defun org-element-get-next-object-candidates (limit restriction objects)
3414 "Return an alist of candidates for the next object.
3416 LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
3417 the possible object types.
3419 Return value is an alist whose car is position and cdr the object
3420 type, as a string. There is an association for the closest
3421 object of each type within RESTRICTION when non-nil, or for every
3422 type otherwise.
3424 OBJECTS is the previous candidates alist."
3425 (let ((restriction (or restriction org-element-all-successors))
3426 next-candidates types-to-search)
3427 ;; If no previous result, search every object type in RESTRICTION.
3428 ;; Otherwise, keep potential candidates (old objects located after
3429 ;; point) and ask to search again those which had matched before.
3430 (if (not objects) (setq types-to-search restriction)
3431 (mapc (lambda (obj)
3432 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
3433 (push obj next-candidates)))
3434 objects))
3435 ;; Call the appropriate "get-next" function for each type to
3436 ;; search and accumulate matches.
3437 (mapc
3438 (lambda (type)
3439 (let* ((successor-fun
3440 (intern
3441 (format "org-element-%s-successor"
3442 (or (cdr (assq type org-element-object-successor-alist))
3443 type))))
3444 (obj (funcall successor-fun limit)))
3445 (and obj (push obj next-candidates))))
3446 types-to-search)
3447 ;; Return alist.
3448 next-candidates))
3452 ;;; Towards A Bijective Process
3454 ;; The parse tree obtained with `org-element-parse-buffer' is really
3455 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3456 ;; interpreted and expanded into a string with canonical Org
3457 ;; syntax. Hence `org-element-interpret-data'.
3459 ;; Data parsed from secondary strings, whose shape is slightly
3460 ;; different than the standard parse tree, is expanded with the
3461 ;; equivalent function `org-element-interpret-secondary'.
3463 ;; Both functions rely internally on
3464 ;; `org-element-interpret--affiliated-keywords'.
3466 (defun org-element-interpret-data (data &optional genealogy previous)
3467 "Interpret a parse tree representing Org data.
3469 DATA is the parse tree to interpret.
3471 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3472 calls:
3473 GENEALOGY is the list of its parents types.
3474 PREVIOUS is the type of the element or object at the same level
3475 interpreted before.
3477 Return Org syntax as a string."
3478 (mapconcat
3479 (lambda (blob)
3480 ;; BLOB can be an element, an object, a string, or nil.
3481 (cond
3482 ((not blob) nil)
3483 ((equal blob "") nil)
3484 ((stringp blob) blob)
3486 (let* ((type (org-element-type blob))
3487 (interpreter
3488 (if (eq type 'org-data) 'identity
3489 (intern (format "org-element-%s-interpreter" type))))
3490 (contents
3491 (cond
3492 ;; Full Org document.
3493 ((eq type 'org-data)
3494 (org-element-interpret-data blob genealogy previous))
3495 ;; Recursive objects.
3496 ((memq type org-element-recursive-objects)
3497 (org-element-interpret-data
3498 blob (cons type genealogy) nil))
3499 ;; Recursive elements.
3500 ((memq type org-element-greater-elements)
3501 (org-element-normalize-string
3502 (org-element-interpret-data
3503 blob (cons type genealogy) nil)))
3504 ;; Paragraphs.
3505 ((eq type 'paragraph)
3506 (let ((paragraph
3507 (org-element-normalize-contents
3508 blob
3509 ;; When normalizing contents of an item,
3510 ;; ignore first line's indentation.
3511 (and (not previous)
3512 (memq (car genealogy)
3513 '(footnote-definiton item))))))
3514 (org-element-interpret-data
3515 paragraph (cons type genealogy) nil)))))
3516 (results (funcall interpreter blob contents)))
3517 ;; Update PREVIOUS.
3518 (setq previous type)
3519 ;; Build white spaces.
3520 (cond
3521 ((eq type 'org-data) results)
3522 ((memq type org-element-all-elements)
3523 (concat
3524 (org-element-interpret--affiliated-keywords blob)
3525 (org-element-normalize-string results)
3526 (make-string (org-element-property :post-blank blob) 10)))
3527 (t (concat
3528 results
3529 (make-string (org-element-property :post-blank blob) 32))))))))
3530 (org-element-contents data) ""))
3532 (defun org-element-interpret-secondary (secondary)
3533 "Interpret SECONDARY string as Org syntax.
3535 SECONDARY-STRING is a nested list as returned by
3536 `org-element-parse-secondary-string'.
3538 Return interpreted string."
3539 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3540 (let ((s (if (listp secondary) secondary (list secondary))))
3541 (org-element-interpret-data `(org-data nil ,@s) nil nil)))
3543 ;; Both functions internally use `org-element--affiliated-keywords'.
3545 (defun org-element-interpret--affiliated-keywords (element)
3546 "Return ELEMENT's affiliated keywords as Org syntax.
3547 If there is no affiliated keyword, return the empty string."
3548 (let ((keyword-to-org
3549 (function
3550 (lambda (key value)
3551 (let (dual)
3552 (when (member key org-element-dual-keywords)
3553 (setq dual (cdr value) value (car value)))
3554 (concat "#+" key (and dual (format "[%s]" dual)) ": "
3555 (if (member key org-element-parsed-keywords)
3556 (org-element-interpret-secondary value)
3557 value)
3558 "\n"))))))
3559 (mapconcat
3560 (lambda (key)
3561 (let ((value (org-element-property (intern (concat ":" key)) element)))
3562 (when value
3563 (if (member key org-element-multiple-keywords)
3564 (mapconcat (lambda (line)
3565 (funcall keyword-to-org key line))
3566 value "")
3567 (funcall keyword-to-org key value)))))
3568 ;; Remove translated keywords.
3569 (delq nil
3570 (mapcar
3571 (lambda (key)
3572 (and (not (assoc key org-element-keyword-translation-alist)) key))
3573 org-element-affiliated-keywords))
3574 "")))
3576 ;; Because interpretation of the parse tree must return the same
3577 ;; number of blank lines between elements and the same number of white
3578 ;; space after objects, some special care must be given to white
3579 ;; spaces.
3581 ;; The first function, `org-element-normalize-string', ensures any
3582 ;; string different from the empty string will end with a single
3583 ;; newline character.
3585 ;; The second function, `org-element-normalize-contents', removes
3586 ;; global indentation from the contents of the current element.
3588 (defun org-element-normalize-string (s)
3589 "Ensure string S ends with a single newline character.
3591 If S isn't a string return it unchanged. If S is the empty
3592 string, return it. Otherwise, return a new string with a single
3593 newline character at its end."
3594 (cond
3595 ((not (stringp s)) s)
3596 ((string= "" s) "")
3597 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
3598 (replace-match "\n" nil nil s)))))
3600 (defun org-element-normalize-contents (element &optional ignore-first)
3601 "Normalize plain text in ELEMENT's contents.
3603 ELEMENT must only contain plain text and objects.
3605 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3606 indentation to compute maximal common indentation.
3608 Return the normalized element that is element with global
3609 indentation removed from its contents. The function assumes that
3610 indentation is not done with TAB characters."
3611 (let (ind-list
3612 (collect-inds
3613 (function
3614 ;; Return list of indentations within BLOB. This is done by
3615 ;; walking recursively BLOB and updating IND-LIST along the
3616 ;; way. FIRST-FLAG is non-nil when the first string hasn't
3617 ;; been seen yet. It is required as this string is the only
3618 ;; one whose indentation doesn't happen after a newline
3619 ;; character.
3620 (lambda (blob first-flag)
3621 (mapc
3622 (lambda (object)
3623 (when (and first-flag (stringp object))
3624 (setq first-flag nil)
3625 (string-match "\\`\\( *\\)" object)
3626 (let ((len (length (match-string 1 object))))
3627 ;; An indentation of zero means no string will be
3628 ;; modified. Quit the process.
3629 (if (zerop len) (throw 'zero (setq ind-list nil))
3630 (push len ind-list))))
3631 (cond
3632 ((stringp object)
3633 (let ((start 0))
3634 (while (string-match "\n\\( *\\)" object start)
3635 (setq start (match-end 0))
3636 (push (length (match-string 1 object)) ind-list))))
3637 ((memq (org-element-type object) org-element-recursive-objects)
3638 (funcall collect-inds object first-flag))))
3639 (org-element-contents blob))))))
3640 ;; Collect indentation list in ELEMENT. Possibly remove first
3641 ;; value if IGNORE-FIRST is non-nil.
3642 (catch 'zero (funcall collect-inds element (not ignore-first)))
3643 (if (not ind-list) element
3644 ;; Build ELEMENT back, replacing each string with the same
3645 ;; string minus common indentation.
3646 (let ((build
3647 (function
3648 (lambda (blob mci first-flag)
3649 ;; Return BLOB with all its strings indentation
3650 ;; shortened from MCI white spaces. FIRST-FLAG is
3651 ;; non-nil when the first string hasn't been seen
3652 ;; yet.
3653 (nconc
3654 (list (org-element-type blob) (nth 1 blob))
3655 (mapcar
3656 (lambda (object)
3657 (when (and first-flag (stringp object))
3658 (setq first-flag nil)
3659 (setq object
3660 (replace-regexp-in-string
3661 (format "\\` \\{%d\\}" mci) "" object)))
3662 (cond
3663 ((stringp object)
3664 (replace-regexp-in-string
3665 (format "\n \\{%d\\}" mci) "\n" object))
3666 ((memq (org-element-type object) org-element-recursive-objects)
3667 (funcall build object mci first-flag))
3668 (t object)))
3669 (org-element-contents blob)))))))
3670 (funcall build element (apply 'min ind-list) (not ignore-first))))))
3674 ;;; The Toolbox
3676 ;; Once the structure of an Org file is well understood, it's easy to
3677 ;; implement some replacements for `forward-paragraph'
3678 ;; `backward-paragraph', namely `org-element-forward' and
3679 ;; `org-element-backward'.
3681 ;; Also, `org-transpose-elements' mimics the behaviour of
3682 ;; `transpose-words', at the element's level, whereas
3683 ;; `org-element-drag-forward', `org-element-drag-backward', and
3684 ;; `org-element-up' generalize, respectively, functions
3685 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3687 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3688 ;; smartly remove global indentation from buffer, making it possible
3689 ;; to use Org indent mode on a file created with hard indentation.
3691 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3692 ;; internally by some of the previously cited tools.
3694 (defsubst org-element-nested-p (elem-A elem-B)
3695 "Non-nil when elements ELEM-A and ELEM-B are nested."
3696 (let ((beg-A (org-element-property :begin elem-A))
3697 (beg-B (org-element-property :begin elem-B))
3698 (end-A (org-element-property :end elem-A))
3699 (end-B (org-element-property :end elem-B)))
3700 (or (and (>= beg-A beg-B) (<= end-A end-B))
3701 (and (>= beg-B beg-A) (<= end-B end-A)))))
3703 (defun org-element-swap-A-B (elem-A elem-B)
3704 "Swap elements ELEM-A and ELEM-B.
3706 Leave point at the end of ELEM-A.
3708 Assume ELEM-A is before ELEM-B and that they are not nested."
3709 (goto-char (org-element-property :begin elem-A))
3710 (let* ((beg-B (org-element-property :begin elem-B))
3711 (end-B-no-blank (save-excursion
3712 (goto-char (org-element-property :end elem-B))
3713 (skip-chars-backward " \r\t\n")
3714 (forward-line)
3715 (point)))
3716 (beg-A (org-element-property :begin elem-A))
3717 (end-A-no-blank (save-excursion
3718 (goto-char (org-element-property :end elem-A))
3719 (skip-chars-backward " \r\t\n")
3720 (forward-line)
3721 (point)))
3722 (body-A (buffer-substring beg-A end-A-no-blank))
3723 (body-B (buffer-substring beg-B end-B-no-blank))
3724 (between-A-B (buffer-substring end-A-no-blank beg-B)))
3725 (delete-region beg-A end-B-no-blank)
3726 (insert body-B between-A-B body-A)
3727 (goto-char (org-element-property :end elem-B))))
3729 (defun org-element-backward ()
3730 "Move backward by one element."
3731 (interactive)
3732 (let* ((opoint (point))
3733 (element (org-element-at-point))
3734 (start-el-beg (org-element-property :begin element)))
3735 ;; At an headline. The previous element is the previous sibling,
3736 ;; or the parent if any.
3737 (cond
3738 ;; Already at the beginning of the current element: move to the
3739 ;; beginning of the previous one.
3740 ((= opoint start-el-beg)
3741 (forward-line -1)
3742 (skip-chars-backward " \r\t\n")
3743 (let* ((prev-element (org-element-at-point))
3744 (itemp (org-in-item-p))
3745 (struct (and itemp
3746 (save-excursion (goto-char itemp)
3747 (org-list-struct)))))
3748 ;; When moving into a new list, go directly at the
3749 ;; beginning of the top list structure.
3750 (if (and itemp (<= (org-list-get-bottom-point struct) opoint))
3751 (progn
3752 (goto-char (org-list-get-top-point struct))
3753 (goto-char (org-element-property
3754 :begin (org-element-at-point))))
3755 (goto-char (org-element-property :begin prev-element))))
3756 (while (org-truely-invisible-p) (org-element-up)))
3757 ;; Else, move at the element beginning. One exception: if point
3758 ;; was in the blank lines after the end of a list, move directly
3759 ;; to the top item.
3761 (let (struct itemp)
3762 (if (and (setq itemp (org-in-item-p))
3763 (<= (org-list-get-bottom-point
3764 (save-excursion (goto-char itemp)
3765 (setq struct (org-list-struct))))
3766 opoint))
3767 (progn
3768 (goto-char (org-list-get-top-point struct))
3769 (goto-char (org-element-property :begin (org-element-at-point))))
3770 (goto-char start-el-beg)))))))
3772 (defun org-element-drag-backward ()
3773 "Drag backward element at point."
3774 (interactive)
3775 (let* ((pos (point))
3776 (elem (org-element-at-point)))
3777 (when (= (progn (goto-char (point-min))
3778 (org-skip-whitespace)
3779 (point-at-bol))
3780 (org-element-property :end elem))
3781 (error "Cannot drag element backward"))
3782 (goto-char (org-element-property :begin elem))
3783 (org-element-backward)
3784 (let ((prev-elem (org-element-at-point)))
3785 (when (or (org-element-nested-p elem prev-elem)
3786 (and (eq (car elem) 'headline)
3787 (not (eq (car prev-elem) 'headline))))
3788 (goto-char pos)
3789 (error "Cannot drag element backward"))
3790 ;; Compute new position of point: it's shifted by PREV-ELEM
3791 ;; body's length.
3792 (let ((size-prev (- (org-element-property :end prev-elem)
3793 (org-element-property :begin prev-elem))))
3794 (org-element-swap-A-B prev-elem elem)
3795 (goto-char (- pos size-prev))))))
3797 (defun org-element-drag-forward ()
3798 "Move forward element at point."
3799 (interactive)
3800 (let* ((pos (point))
3801 (elem (org-element-at-point)))
3802 (when (= (point-max) (org-element-property :end elem))
3803 (error "Cannot drag element forward"))
3804 (goto-char (org-element-property :end elem))
3805 (let ((next-elem (org-element-at-point)))
3806 (when (or (org-element-nested-p elem next-elem)
3807 (and (eq (car next-elem) 'headline)
3808 (not (eq (car elem) 'headline))))
3809 (goto-char pos)
3810 (error "Cannot drag element forward"))
3811 ;; Compute new position of point: it's shifted by NEXT-ELEM
3812 ;; body's length (without final blanks) and by the length of
3813 ;; blanks between ELEM and NEXT-ELEM.
3814 (let ((size-next (- (save-excursion
3815 (goto-char (org-element-property :end next-elem))
3816 (skip-chars-backward " \r\t\n")
3817 (forward-line)
3818 (point))
3819 (org-element-property :begin next-elem)))
3820 (size-blank (- (org-element-property :end elem)
3821 (save-excursion
3822 (goto-char (org-element-property :end elem))
3823 (skip-chars-backward " \r\t\n")
3824 (forward-line)
3825 (point)))))
3826 (org-element-swap-A-B elem next-elem)
3827 (goto-char (+ pos size-next size-blank))))))
3829 (defun org-element-forward ()
3830 "Move forward by one element."
3831 (interactive)
3832 (beginning-of-line)
3833 (cond ((eobp) (error "Cannot move further down"))
3834 ((looking-at "[ \t]*$")
3835 (org-skip-whitespace)
3836 (goto-char (if (eobp) (point) (point-at-bol))))
3838 (let ((element (org-element-at-point t))
3839 (origin (point)))
3840 (cond
3841 ;; At an item: Either move to the next element inside, or
3842 ;; to its end if it's hidden.
3843 ((eq (org-element-type element) 'item)
3844 (if (org-element-property :hiddenp element)
3845 (goto-char (org-element-property :end element))
3846 (end-of-line)
3847 (re-search-forward org-element-paragraph-separate nil t)
3848 (org-skip-whitespace)
3849 (beginning-of-line)))
3850 ;; At a recursive element: Either move inside, or if it's
3851 ;; hidden, move to its end.
3852 ((memq (org-element-type element) org-element-greater-elements)
3853 (let ((cbeg (org-element-property :contents-begin element)))
3854 (goto-char
3855 (if (or (org-element-property :hiddenp element)
3856 (> origin cbeg))
3857 (org-element-property :end element)
3858 cbeg))))
3859 ;; Else: move to the current element's end.
3860 (t (goto-char (org-element-property :end element))))))))
3862 (defun org-element-mark-element ()
3863 "Put point at beginning of this element, mark at end.
3865 Interactively, if this command is repeated or (in Transient Mark
3866 mode) if the mark is active, it marks the next element after the
3867 ones already marked."
3868 (interactive)
3869 (let (deactivate-mark)
3870 (if (or (and (eq last-command this-command) (mark t))
3871 (and transient-mark-mode mark-active))
3872 (set-mark
3873 (save-excursion
3874 (goto-char (mark))
3875 (goto-char (org-element-property :end (org-element-at-point)))))
3876 (let ((element (org-element-at-point)))
3877 (end-of-line)
3878 (push-mark (org-element-property :end element) t t)
3879 (goto-char (org-element-property :begin element))))))
3881 (defun org-narrow-to-element ()
3882 "Narrow buffer to current element."
3883 (interactive)
3884 (let ((elem (org-element-at-point)))
3885 (cond
3886 ((eq (car elem) 'headline)
3887 (narrow-to-region
3888 (org-element-property :begin elem)
3889 (org-element-property :end elem)))
3890 ((memq (car elem) org-element-greater-elements)
3891 (narrow-to-region
3892 (org-element-property :contents-begin elem)
3893 (org-element-property :contents-end elem)))
3895 (narrow-to-region
3896 (org-element-property :begin elem)
3897 (org-element-property :end elem))))))
3899 (defun org-transpose-elements ()
3900 "Transpose current and previous elements, keeping blank lines between.
3901 Point is moved after both elements."
3902 (interactive)
3903 (org-skip-whitespace)
3904 (let ((pos (point))
3905 (cur (org-element-at-point)))
3906 (when (= (save-excursion (goto-char (point-min))
3907 (org-skip-whitespace)
3908 (point-at-bol))
3909 (org-element-property :begin cur))
3910 (error "No previous element"))
3911 (goto-char (org-element-property :begin cur))
3912 (forward-line -1)
3913 (let ((prev (org-element-at-point)))
3914 (when (org-element-nested-p cur prev)
3915 (goto-char pos)
3916 (error "Cannot transpose nested elements"))
3917 (org-element-swap-A-B prev cur))))
3919 (defun org-element-unindent-buffer ()
3920 "Un-indent the visible part of the buffer.
3921 Relative indentation \(between items, inside blocks, etc.\) isn't
3922 modified."
3923 (interactive)
3924 (unless (eq major-mode 'org-mode)
3925 (error "Cannot un-indent a buffer not in Org mode"))
3926 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
3927 unindent-tree ; For byte-compiler.
3928 (unindent-tree
3929 (function
3930 (lambda (contents)
3931 (mapc (lambda (element)
3932 (if (eq (org-element-type element) 'headline)
3933 (funcall unindent-tree
3934 (org-element-contents element))
3935 (save-excursion
3936 (save-restriction
3937 (narrow-to-region
3938 (org-element-property :begin element)
3939 (org-element-property :end element))
3940 (org-do-remove-indentation)))))
3941 (reverse contents))))))
3942 (funcall unindent-tree (org-element-contents parse-tree))))
3944 (defun org-element-up ()
3945 "Move to upper element.
3946 Return position at the beginning of the upper element."
3947 (interactive)
3948 (let ((opoint (point)) elem)
3949 (cond
3950 ((bobp) (error "No surrounding element"))
3951 ((org-with-limited-levels (org-at-heading-p))
3952 (or (org-up-heading-safe) (error "No surronding element")))
3953 ((and (org-at-item-p)
3954 (setq elem (org-element-at-point))
3955 (let* ((top-list-p (zerop (org-element-property :level elem))))
3956 (unless top-list-p
3957 ;; If parent is bound to be in the same list as the
3958 ;; original point, move to that parent.
3959 (let ((struct (org-element-property :structure elem)))
3960 (goto-char
3961 (org-list-get-parent
3962 (point-at-bol) struct (org-list-parents-alist struct))))))))
3964 (let* ((elem (or elem (org-element-at-point)))
3965 (end (save-excursion
3966 (goto-char (org-element-property :end elem))
3967 (skip-chars-backward " \r\t\n")
3968 (forward-line)
3969 (point)))
3970 prev-elem)
3971 (goto-char (org-element-property :begin elem))
3972 (forward-line -1)
3973 (while (and (< (org-element-property
3974 :end (setq prev-elem (org-element-at-point)))
3975 end)
3976 (not (bobp)))
3977 (goto-char (org-element-property :begin prev-elem))
3978 (forward-line -1))
3979 (if (and (bobp) (< (org-element-property :end prev-elem) end))
3980 (progn (goto-char opoint)
3981 (error "No surrounding element"))
3982 (goto-char (org-element-property :begin prev-elem))))))))
3985 (provide 'org-element)
3986 ;;; org-element.el ends here