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