org-element: Minor fix
[org-mode.git] / contrib / lisp / org-element.el
blobca43caefde12f6312ce2a5e5cda2f53503218fd9
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 two accessors and a function
83 ;; retrieving the smallest element containing point (respectively
84 ;; `org-element-get-property', `org-element-get-contents' and
85 ;; `org-element-at-point').
87 ;; The following part creates a fully recursive buffer parser. It
88 ;; also provides a tool to map a function to elements or objects
89 ;; matching some criteria in the parse tree. Functions of interest
90 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
91 ;; extent, `org-element-parse-secondary-string'.
93 ;; The penultimate part is the cradle of an interpreter for the
94 ;; obtained parse tree: `org-element-interpret-data' (and its
95 ;; relative, `org-element-interpret-secondary').
97 ;; The library ends by furnishing a set of interactive tools for
98 ;; element's navigation and manipulation.
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-get-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-get-property :block-name dynamic-block)
259 (let ((args (org-element-get-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-get-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-get-property :level headline))
411 (todo (org-element-get-property :todo-keyword headline))
412 (priority (org-element-get-property :priority headline))
413 (title (org-element-get-property :raw-value headline))
414 (tags (let ((tag-string (org-element-get-property :tags headline))
415 (archivedp (org-element-get-property :archivedp headline)))
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-get-property :commentedp headline))
422 (quotedp (org-element-get-property :quotedp headline))
423 (pre-blank (org-element-get-property :pre-blank headline))
424 (heading (concat (make-string level ?*)
425 (and todo (concat " " todo))
426 (and quotedp (concat " " org-quote-string))
427 (and commentedp (concat " " org-comment-string))
428 (and priority (concat " " priority))
429 (cond ((and org-footnote-section
430 (org-element-get-property
431 :footnote-section-p headline))
432 (concat " " org-footnote-section))
433 (title (concat " " title)))))
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-get-property :level inlinetask))
530 (todo (org-element-get-property :todo-keyword inlinetask))
531 (priority (org-element-get-property :priority inlinetask))
532 (title (org-element-get-property :raw-value inlinetask))
533 (tags (org-element-get-property :tags inlinetask))
534 (task (concat (make-string level ?*)
535 (and todo (concat " " todo))
536 (and priority (concat " " priority))
537 (and title (concat " " title))))
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 (>= (point) end)
592 (point-at-bol)
593 (point))))
594 (hidden (progn (forward-line)
595 (and (not (= (point) end))
596 (org-truely-invisible-p))))
597 (contents-end (progn (goto-char end)
598 (skip-chars-backward " \r\t\n")
599 (forward-line)
600 (point))))
601 `(item
602 (:bullet ,bullet
603 :begin ,begin
604 :end ,end
605 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
606 ;; up in the case of an empty item separated
607 ;; from the next by a blank line. Thus, ensure
608 ;; the former is always the smallest of two.
609 :contents-begin ,(min contents-begin contents-end)
610 :contents-end ,(max contents-begin contents-end)
611 :checkbox ,checkbox
612 :counter ,counter
613 :raw-tag ,raw-tag
614 :tag ,tag
615 :hiddenp ,hidden
616 :structure ,struct
617 :post-blank ,(count-lines contents-end end))))))
619 (defun org-element-item-interpreter (item contents)
620 "Interpret ITEM element as Org syntax.
621 CONTENTS is the contents of the element."
622 (let* ((bullet (org-element-get-property :bullet item))
623 (checkbox (org-element-get-property :checkbox item))
624 (counter (org-element-get-property :counter item))
625 (tag (org-element-get-property :raw-tag item))
626 ;; Compute indentation.
627 (ind (make-string (length bullet) 32)))
628 ;; Indent contents.
629 (concat
630 bullet
631 (when (and org-list-two-spaces-after-bullet-regexp
632 (string-match org-list-two-spaces-after-bullet-regexp bullet))
633 " ")
634 (and counter (format "[@%d] " counter))
635 (cond
636 ((eq checkbox 'on) "[X] ")
637 ((eq checkbox 'off) "[ ] ")
638 ((eq checkbox 'trans) "[-] "))
639 (and tag (format "%s :: " tag))
640 (org-trim
641 (replace-regexp-in-string
642 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))))
645 ;;;; Plain List
647 (defun org-element-plain-list-parser (&optional structure)
648 "Parse a plain list.
650 Return a list whose car is `plain-list' and cdr is a plist
651 containing `:type', `:begin', `:end', `:contents-begin' and
652 `:contents-end', `:level', `:structure' and `:post-blank'
653 keywords.
655 Assume point is at one of the list items."
656 (save-excursion
657 (let* ((struct (or structure (org-list-struct)))
658 (prevs (org-list-prevs-alist struct))
659 (parents (org-list-parents-alist struct))
660 (type (org-list-get-list-type (point) struct prevs))
661 (contents-begin (goto-char
662 (org-list-get-list-begin (point) struct prevs)))
663 (keywords (org-element-collect-affiliated-keywords))
664 (begin (car keywords))
665 (contents-end (goto-char
666 (org-list-get-list-end (point) struct prevs)))
667 (end (save-excursion (org-skip-whitespace)
668 (if (eobp) (point) (point-at-bol))))
669 (level 0))
670 ;; Get list level.
671 (let ((item contents-begin))
672 (while (setq item
673 (org-list-get-parent
674 (org-list-get-list-begin item struct prevs)
675 struct parents))
676 (incf level)))
677 ;; Blank lines below list belong to the top-level list only.
678 (when (> level 0)
679 (setq end (min (org-list-get-bottom-point struct)
680 (progn (org-skip-whitespace)
681 (if (eobp) (point) (point-at-bol))))))
682 ;; Return value.
683 `(plain-list
684 (:type ,type
685 :begin ,begin
686 :end ,end
687 :contents-begin ,contents-begin
688 :contents-end ,contents-end
689 :level ,level
690 :structure ,struct
691 :post-blank ,(count-lines contents-end end)
692 ,@(cadr keywords))))))
694 (defun org-element-plain-list-interpreter (plain-list contents)
695 "Interpret PLAIN-LIST element as Org syntax.
696 CONTENTS is the contents of the element."
697 contents)
700 ;;;; Quote Block
702 (defun org-element-quote-block-parser ()
703 "Parse a quote block.
705 Return a list whose car is `quote-block' and cdr is a plist
706 containing `:begin', `:end', `:hiddenp', `:contents-begin',
707 `:contents-end' and `:post-blank' keywords.
709 Assume point is at beginning or end of the block."
710 (save-excursion
711 (let* ((case-fold-search t)
712 (keywords (progn
713 (end-of-line)
714 (re-search-backward
715 (concat "^[ \t]*#\\+begin_quote") nil t)
716 (org-element-collect-affiliated-keywords)))
717 (begin (car keywords))
718 (contents-begin (progn (forward-line) (point)))
719 (hidden (org-truely-invisible-p))
720 (contents-end (progn (re-search-forward
721 (concat "^[ \t]*#\\+end_quote") nil t)
722 (point-at-bol)))
723 (pos-before-blank (progn (forward-line) (point)))
724 (end (progn (org-skip-whitespace)
725 (if (eobp) (point) (point-at-bol)))))
726 `(quote-block
727 (:begin ,begin
728 :end ,end
729 :hiddenp ,hidden
730 :contents-begin ,contents-begin
731 :contents-end ,contents-end
732 :post-blank ,(count-lines pos-before-blank end)
733 ,@(cadr keywords))))))
735 (defun org-element-quote-block-interpreter (quote-block contents)
736 "Interpret QUOTE-BLOCK element as Org syntax.
737 CONTENTS is the contents of the element."
738 (format "#+begin_quote\n%s#+end_quote" contents))
741 ;;;; Section
743 (defun org-element-section-parser ()
744 "Parse a section.
746 Return a list whose car is `section' and cdr is a plist
747 containing `:begin', `:end', `:contents-begin', `contents-end'
748 and `:post-blank' keywords."
749 (save-excursion
750 ;; Beginning of section is the beginning of the first non-blank
751 ;; line after previous headline.
752 (let ((begin (save-excursion
753 (org-with-limited-levels (outline-previous-heading))
754 (if (not (org-at-heading-p)) (point)
755 (forward-line) (org-skip-whitespace) (point-at-bol))))
756 (end (progn (org-with-limited-levels (outline-next-heading))
757 (point)))
758 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
759 (forward-line)
760 (point))))
761 `(section
762 (:begin ,begin
763 :end ,end
764 :contents-begin ,begin
765 :contents-end ,pos-before-blank
766 :post-blank ,(count-lines pos-before-blank end))))))
768 (defun org-element-section-interpreter (section contents)
769 "Interpret SECTION element as Org syntax.
770 CONTENTS is the contents of the element."
771 contents)
774 ;;;; Special Block
776 (defun org-element-special-block-parser ()
777 "Parse a special block.
779 Return a list whose car is `special-block' and cdr is a plist
780 containing `:type', `:begin', `:end', `:hiddenp',
781 `:contents-begin', `:contents-end' and `:post-blank' keywords.
783 Assume point is at beginning or end of the block."
784 (save-excursion
785 (let* ((case-fold-search t)
786 (type (progn (looking-at
787 "[ \t]*#\\+\\(?:begin\\|end\\)_\\([-A-Za-z0-9]+\\)")
788 (org-match-string-no-properties 1)))
789 (keywords (progn
790 (end-of-line)
791 (re-search-backward
792 (concat "^[ \t]*#\\+begin_" type) nil t)
793 (org-element-collect-affiliated-keywords)))
794 (begin (car keywords))
795 (contents-begin (progn (forward-line) (point)))
796 (hidden (org-truely-invisible-p))
797 (contents-end (progn (re-search-forward
798 (concat "^[ \t]*#\\+end_" type) nil t)
799 (point-at-bol)))
800 (pos-before-blank (progn (forward-line) (point)))
801 (end (progn (org-skip-whitespace)
802 (if (eobp) (point) (point-at-bol)))))
803 `(special-block
804 (:type ,type
805 :begin ,begin
806 :end ,end
807 :hiddenp ,hidden
808 :contents-begin ,contents-begin
809 :contents-end ,contents-end
810 :post-blank ,(count-lines pos-before-blank end)
811 ,@(cadr keywords))))))
813 (defun org-element-special-block-interpreter (special-block contents)
814 "Interpret SPECIAL-BLOCK element as Org syntax.
815 CONTENTS is the contents of the element."
816 (let ((block-type (org-element-get-property :type special-block)))
817 (format "#+begin_%s\n%s#+end_%s" block-type contents block-type)))
821 ;;; Elements
823 ;; For each element, a parser and an interpreter are also defined.
824 ;; Both follow the same naming convention used for greater elements.
826 ;; Also, as for greater elements, adding a new element type is done
827 ;; through the following steps: implement a parser and an interpreter,
828 ;; tweak `org-element-guess-type' so that it recognizes the new type
829 ;; and add that new type to `org-element-all-elements'.
831 ;; As a special case, when the newly defined type is a block type,
832 ;; `org-element-non-recursive-block-alist' has to be modified
833 ;; accordingly.
836 ;;;; Babel Call
838 (defun org-element-babel-call-parser ()
839 "Parse a babel call.
841 Return a list whose car is `babel-call' and cdr is a plist
842 containing `:begin', `:end', `:info' and `:post-blank' as
843 keywords."
844 (save-excursion
845 (let ((info (progn (looking-at org-babel-block-lob-one-liner-regexp)
846 (org-babel-lob-get-info)))
847 (beg (point-at-bol))
848 (pos-before-blank (progn (forward-line) (point)))
849 (end (progn (org-skip-whitespace)
850 (if (eobp) (point) (point-at-bol)))))
851 `(babel-call
852 (:beg ,beg
853 :end ,end
854 :info ,info
855 :post-blank ,(count-lines pos-before-blank end))))))
857 (defun org-element-babel-call-interpreter (inline-babel-call contents)
858 "Interpret INLINE-BABEL-CALL object as Org syntax.
859 CONTENTS is nil."
860 (let* ((babel-info (org-element-get-property :info inline-babel-call))
861 (main-source (car babel-info))
862 (post-options (nth 1 babel-info)))
863 (concat "#+call: "
864 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
865 ;; Remove redundant square brackets.
866 (replace-match
867 (match-string 1 main-source) nil nil main-source)
868 main-source)
869 (and post-options (format "[%s]" post-options)))))
872 ;;;; Comment
874 (defun org-element-comment-parser ()
875 "Parse a comment.
877 Return a list whose car is `comment' and cdr is a plist
878 containing `:begin', `:end', `:value' and `:post-blank'
879 keywords."
880 (let ((comment-re "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)")
881 beg-coms begin end value pos-before-blank keywords)
882 (save-excursion
883 ;; Move to the beginning of comments.
884 (unless (bobp)
885 (while (and (not (bobp)) (looking-at comment-re))
886 (forward-line -1))
887 (unless (looking-at comment-re) (forward-line 1)))
888 (setq beg-coms (point))
889 ;; Get affiliated keywords, if any.
890 (setq keywords (org-element-collect-affiliated-keywords))
891 ;; Store true beginning of element.
892 (setq begin (car keywords))
893 ;; Get ending of comments. If point is in a list, ensure to not
894 ;; get outside of it.
895 (let* ((itemp (org-in-item-p))
896 (max-pos (if itemp
897 (org-list-get-bottom-point
898 (save-excursion (goto-char itemp) (org-list-struct)))
899 (point-max))))
900 (while (and (looking-at comment-re) (< (point) max-pos))
901 (forward-line)))
902 (setq pos-before-blank (point))
903 ;; Find position after blank.
904 (org-skip-whitespace)
905 (setq end (if (eobp) (point) (point-at-bol)))
906 ;; Extract value.
907 (setq value (buffer-substring-no-properties beg-coms pos-before-blank)))
908 `(comment
909 (:begin ,begin
910 :end ,end
911 :value ,value
912 :post-blank ,(count-lines pos-before-blank end)
913 ,@(cadr keywords)))))
915 (defun org-element-comment-interpreter (comment contents)
916 "Interpret COMMENT element as Org syntax.
917 CONTENTS is nil."
918 (org-element-get-property :value comment))
921 ;;;; Comment Block
923 (defun org-element-comment-block-parser ()
924 "Parse an export block.
926 Return a list whose car is `comment-block' and cdr is a plist
927 containing `:begin', `:end', `:hiddenp', `:value' and
928 `:post-blank' keywords."
929 (save-excursion
930 (end-of-line)
931 (let* ((case-fold-search t)
932 (keywords (progn
933 (re-search-backward "^[ \t]*#\\+begin_comment" nil t)
934 (org-element-collect-affiliated-keywords)))
935 (begin (car keywords))
936 (contents-begin (progn (forward-line) (point)))
937 (hidden (org-truely-invisible-p))
938 (contents-end (progn (re-search-forward
939 "^[ \t]*#\\+end_comment" nil t)
940 (point-at-bol)))
941 (pos-before-blank (progn (forward-line) (point)))
942 (end (progn (org-skip-whitespace)
943 (if (eobp) (point) (point-at-bol))))
944 (value (buffer-substring-no-properties contents-begin contents-end)))
945 `(comment-block
946 (:begin ,begin
947 :end ,end
948 :value ,value
949 :hiddenp ,hidden
950 :post-blank ,(count-lines pos-before-blank end)
951 ,@(cadr keywords))))))
953 (defun org-element-comment-block-interpreter (comment-block contents)
954 "Interpret COMMENT-BLOCK element as Org syntax.
955 CONTENTS is nil."
956 (concat "#+begin_comment\n"
957 (org-remove-indentation
958 (org-element-get-property :value comment-block))
959 "#+begin_comment"))
962 ;;;; Example Block
964 (defun org-element-example-block-parser ()
965 "Parse an example block.
967 Return a list whose car is `example' and cdr is a plist
968 containing `:begin', `:end', `:options', `:hiddenp', `:value' and
969 `:post-blank' keywords."
970 (save-excursion
971 (end-of-line)
972 (let* ((case-fold-search t)
973 (switches (progn
974 (re-search-backward
975 "^[ \t]*#\\+begin_example\\(?: +\\(.*\\)\\)?" nil t)
976 (org-match-string-no-properties 1)))
977 (keywords (org-element-collect-affiliated-keywords))
978 (begin (car keywords))
979 (contents-begin (progn (forward-line) (point)))
980 (hidden (org-truely-invisible-p))
981 (contents-end (progn
982 (re-search-forward "^[ \t]*#\\+end_example" nil t)
983 (point-at-bol)))
984 (value (buffer-substring-no-properties contents-begin contents-end))
985 (pos-before-blank (progn (forward-line) (point)))
986 (end (progn (org-skip-whitespace)
987 (if (eobp) (point) (point-at-bol)))))
988 `(example-block
989 (:begin ,begin
990 :end ,end
991 :value ,value
992 :switches ,switches
993 :hiddenp ,hidden
994 :post-blank ,(count-lines pos-before-blank end)
995 ,@(cadr keywords))))))
997 (defun org-element-example-block-interpreter (example-block contents)
998 "Interpret EXAMPLE-BLOCK element as Org syntax.
999 CONTENTS is nil."
1000 (let ((options (org-element-get-property :options example-block)))
1001 (concat "#+begin_example" (and options (concat " " options)) "\n"
1002 (org-remove-indentation
1003 (org-element-get-property :value example-block))
1004 "#+end_example")))
1007 ;;;; Export Block
1009 (defun org-element-export-block-parser ()
1010 "Parse an export block.
1012 Return a list whose car is `export-block' and cdr is a plist
1013 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1014 `:post-blank' keywords."
1015 (save-excursion
1016 (end-of-line)
1017 (let* ((case-fold-search t)
1018 (contents)
1019 (type (progn (re-search-backward
1020 (concat "[ \t]*#\\+begin_"
1021 (org-re "\\([[:alnum:]]+\\)")))
1022 (downcase (org-match-string-no-properties 1))))
1023 (keywords (org-element-collect-affiliated-keywords))
1024 (begin (car keywords))
1025 (contents-begin (progn (forward-line) (point)))
1026 (hidden (org-truely-invisible-p))
1027 (contents-end (progn (re-search-forward
1028 (concat "^[ \t]*#\\+end_" type) nil t)
1029 (point-at-bol)))
1030 (pos-before-blank (progn (forward-line) (point)))
1031 (end (progn (org-skip-whitespace)
1032 (if (eobp) (point) (point-at-bol))))
1033 (value (buffer-substring-no-properties contents-begin contents-end)))
1034 `(export-block
1035 (:begin ,begin
1036 :end ,end
1037 :type ,type
1038 :value ,value
1039 :hiddenp ,hidden
1040 :post-blank ,(count-lines pos-before-blank end)
1041 ,@(cadr keywords))))))
1043 (defun org-element-export-block-interpreter (export-block contents)
1044 "Interpret EXPORT-BLOCK element as Org syntax.
1045 CONTENTS is nil."
1046 (let ((type (org-element-get-property :type export-block)))
1047 (concat (format "#+begin_%s\n" type)
1048 (org-element-get-property :value export-block)
1049 (format "#+end_%s" type))))
1052 ;;;; Fixed-width
1054 (defun org-element-fixed-width-parser ()
1055 "Parse a fixed-width section.
1057 Return a list whose car is `fixed-width' and cdr is a plist
1058 containing `:begin', `:end', `:value' and `:post-blank'
1059 keywords."
1060 (let ((fixed-re "[ \t]*:\\( \\|$\\)")
1061 beg-area begin end value pos-before-blank keywords)
1062 (save-excursion
1063 ;; Move to the beginning of the fixed-width area.
1064 (unless (bobp)
1065 (while (and (not (bobp)) (looking-at fixed-re))
1066 (forward-line -1))
1067 (unless (looking-at fixed-re) (forward-line 1)))
1068 (setq beg-area (point))
1069 ;; Get affiliated keywords, if any.
1070 (setq keywords (org-element-collect-affiliated-keywords))
1071 ;; Store true beginning of element.
1072 (setq begin (car keywords))
1073 ;; Get ending of fixed-width area. If point is in a list,
1074 ;; ensure to not get outside of it.
1075 (let* ((itemp (org-in-item-p))
1076 (max-pos (if itemp
1077 (org-list-get-bottom-point
1078 (save-excursion (goto-char itemp) (org-list-struct)))
1079 (point-max))))
1080 (while (and (looking-at fixed-re) (< (point) max-pos))
1081 (forward-line)))
1082 (setq pos-before-blank (point))
1083 ;; Find position after blank
1084 (org-skip-whitespace)
1085 (setq end (if (eobp) (point) (point-at-bol)))
1086 ;; Extract value.
1087 (setq value (buffer-substring-no-properties beg-area pos-before-blank)))
1088 `(fixed-width
1089 (:begin ,begin
1090 :end ,end
1091 :value ,value
1092 :post-blank ,(count-lines pos-before-blank end)
1093 ,@(cadr keywords)))))
1095 (defun org-element-fixed-width-interpreter (fixed-width contents)
1096 "Interpret FIXED-WIDTH element as Org syntax.
1097 CONTENTS is nil."
1098 (org-remove-indentation (org-element-get-property :value fixed-width)))
1101 ;;;; Horizontal Rule
1103 (defun org-element-horizontal-rule-parser ()
1104 "Parse an horizontal rule.
1106 Return a list whose car is `horizontal-rule' and cdr is
1107 a plist containing `:begin', `:end' and `:post-blank'
1108 keywords."
1109 (save-excursion
1110 (let* ((keywords (org-element-collect-affiliated-keywords))
1111 (begin (car keywords))
1112 (post-hr (progn (forward-line) (point)))
1113 (end (progn (org-skip-whitespace)
1114 (if (eobp) (point) (point-at-bol)))))
1115 `(horizontal-rule
1116 (:begin ,begin
1117 :end ,end
1118 :post-blank ,(count-lines post-hr end)
1119 ,@(cadr keywords))))))
1121 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1122 "Interpret HORIZONTAL-RULE element as Org syntax.
1123 CONTENTS is nil."
1124 "-----")
1127 ;;;; Keyword
1129 (defun org-element-keyword-parser ()
1130 "Parse a keyword at point.
1132 Return a list whose car is `keyword' and cdr is a plist
1133 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1134 keywords."
1135 (save-excursion
1136 (let* ((begin (point))
1137 (key (progn (looking-at
1138 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1139 (org-match-string-no-properties 1)))
1140 (value (org-trim (buffer-substring-no-properties
1141 (match-end 0) (point-at-eol))))
1142 (pos-before-blank (progn (forward-line) (point)))
1143 (end (progn (org-skip-whitespace)
1144 (if (eobp) (point) (point-at-bol)))))
1145 `(keyword
1146 (:key ,key
1147 :value ,value
1148 :begin ,begin
1149 :end ,end
1150 :post-blank ,(count-lines pos-before-blank end))))))
1152 (defun org-element-keyword-interpreter (keyword contents)
1153 "Interpret KEYWORD element as Org syntax.
1154 CONTENTS is nil."
1155 (format "#+%s: %s"
1156 (org-element-get-property :key keyword)
1157 (org-element-get-property :value keyword)))
1160 ;;;; Latex Environment
1162 (defun org-element-latex-environment-parser ()
1163 "Parse a LaTeX environment.
1165 Return a list whose car is `latex-environment' and cdr is a plist
1166 containing `:begin', `:end', `:value' and `:post-blank' keywords."
1167 (save-excursion
1168 (end-of-line)
1169 (let* ((case-fold-search t)
1170 (contents-begin (re-search-backward "^[ \t]*\\\\begin" nil t))
1171 (keywords (org-element-collect-affiliated-keywords))
1172 (begin (car keywords))
1173 (contents-end (progn (re-search-forward "^[ \t]*\\\\end")
1174 (forward-line)
1175 (point)))
1176 (value (buffer-substring-no-properties contents-begin contents-end))
1177 (end (progn (org-skip-whitespace)
1178 (if (eobp) (point) (point-at-bol)))))
1179 `(latex-environment
1180 (:begin ,begin
1181 :end ,end
1182 :value ,value
1183 :post-blank ,(count-lines contents-end end)
1184 ,@(cadr keywords))))))
1186 (defun org-element-latex-environment-interpreter (latex-environment contents)
1187 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1188 CONTENTS is nil."
1189 (org-element-get-property :value latex-environment))
1192 ;;;; Paragraph
1194 (defun org-element-paragraph-parser ()
1195 "Parse a paragraph.
1197 Return a list whose car is `paragraph' and cdr is a plist
1198 containing `:begin', `:end', `:contents-begin' and
1199 `:contents-end' and `:post-blank' keywords.
1201 Assume point is at the beginning of the paragraph."
1202 (save-excursion
1203 (let* ((contents-begin (point))
1204 (keywords (org-element-collect-affiliated-keywords))
1205 (begin (car keywords))
1206 (contents-end (progn
1207 (end-of-line)
1208 (if (re-search-forward
1209 org-element-paragraph-separate nil 'm)
1210 (progn (forward-line -1) (end-of-line) (point))
1211 (point))))
1212 (pos-before-blank (progn (forward-line) (point)))
1213 (end (progn (org-skip-whitespace)
1214 (if (eobp) (point) (point-at-bol)))))
1215 `(paragraph
1216 (:begin ,begin
1217 :end ,end
1218 :contents-begin ,contents-begin
1219 :contents-end ,contents-end
1220 :post-blank ,(count-lines pos-before-blank end)
1221 ,@(cadr keywords))))))
1223 (defun org-element-paragraph-interpreter (paragraph contents)
1224 "Interpret PARAGRAPH element as Org syntax.
1225 CONTENTS is the contents of the element."
1226 contents)
1229 ;;;; Property Drawer
1231 (defun org-element-property-drawer-parser ()
1232 "Parse a property drawer.
1234 Return a list whose car is `property-drawer' and cdr is a plist
1235 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1236 `:contents-end', `:properties' and `:post-blank' keywords."
1237 (save-excursion
1238 (let ((case-fold-search t)
1239 (begin (progn (end-of-line)
1240 (re-search-backward org-property-start-re)
1241 (match-beginning 0)))
1242 (contents-begin (progn (forward-line) (point)))
1243 (hidden (org-truely-invisible-p))
1244 (properties (let (val)
1245 (while (not (looking-at "^[ \t]*:END:"))
1246 (when (looking-at
1247 (org-re
1248 "[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):"))
1249 (push (cons (match-string 1)
1250 (org-trim
1251 (buffer-substring
1252 (match-end 0) (point-at-eol))))
1253 val))
1254 (forward-line))
1255 val))
1256 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t)
1257 (point-at-bol)))
1258 (pos-before-blank (progn (forward-line) (point)))
1259 (end (progn (org-skip-whitespace)
1260 (if (eobp) (point) (point-at-bol)))))
1261 `(property-drawer
1262 (:begin ,begin
1263 :end ,end
1264 :hiddenp ,hidden
1265 :properties ,properties
1266 :post-blank ,(count-lines pos-before-blank end))))))
1268 (defun org-element-property-drawer-interpreter (property-drawer contents)
1269 "Interpret PROPERTY-DRAWER element as Org syntax.
1270 CONTENTS is nil."
1271 (let ((props (org-element-get-property :properties property-drawer)))
1272 (concat
1273 ":PROPERTIES:\n"
1274 (mapconcat (lambda (p)
1275 (format org-property-format (format ":%s:" (car p)) (cdr p)))
1276 (nreverse props) "\n")
1277 "\n:END:")))
1280 ;;;; Quote Section
1282 (defun org-element-quote-section-parser ()
1283 "Parse a quote section.
1285 Return a list whose car is `quote-section' and cdr is a plist
1286 containing `:begin', `:end', `:value' and `:post-blank'
1287 keywords."
1288 (save-excursion
1289 (let* ((begin (progn (org-back-to-heading t)
1290 (forward-line)
1291 (org-skip-whitespace)
1292 (point-at-bol)))
1293 (end (progn (org-with-limited-levels (outline-next-heading))
1294 (point)))
1295 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1296 (forward-line)
1297 (point)))
1298 (value (unless (= begin end)
1299 (buffer-substring-no-properties begin pos-before-blank))))
1300 `(quote-section
1301 (:begin ,begin
1302 :end ,end
1303 :value ,value
1304 :post-blank ,(if value
1305 (count-lines pos-before-blank end)
1306 0))))))
1308 (defun org-element-quote-section-interpreter (quote-section contents)
1309 "Interpret QUOTE-SECTION element as Org syntax.
1310 CONTENTS is nil."
1311 (org-element-get-property :value quote-section))
1314 ;;;; Src Block
1316 (defun org-element-src-block-parser ()
1317 "Parse a src block.
1319 Return a list whose car is `src-block' and cdr is a plist
1320 containing `:language', `:switches', `:parameters', `:begin',
1321 `:end', `:hiddenp', `:contents-begin', `:contents-end', `:value'
1322 and `:post-blank' keywords."
1323 (save-excursion
1324 (end-of-line)
1325 (let* ((case-fold-search t)
1326 ;; Get position at beginning of block.
1327 (contents-begin
1328 (re-search-backward
1329 (concat "^[ \t]*#\\+begin_src"
1330 "\\(?: +\\(\\S-+\\)\\)?" ; language
1331 "\\(\\(?: +[-+][A-Za-z]\\)*\\)" ; switches
1332 "\\(.*\\)[ \t]*$") ; arguments
1333 nil t))
1334 ;; Get language as a string.
1335 (language (org-match-string-no-properties 1))
1336 ;; Get switches.
1337 (switches (org-match-string-no-properties 2))
1338 ;; Get parameters.
1339 (parameters (org-trim (org-match-string-no-properties 3)))
1340 ;; Get affiliated keywords.
1341 (keywords (org-element-collect-affiliated-keywords))
1342 ;; Get beginning position.
1343 (begin (car keywords))
1344 ;; Get position at end of block.
1345 (contents-end (progn (re-search-forward "^[ \t]*#\\+end_src" nil t)
1346 (forward-line)
1347 (point)))
1348 ;; Retrieve code.
1349 (value (buffer-substring-no-properties
1350 (save-excursion (goto-char contents-begin)
1351 (forward-line)
1352 (point))
1353 (match-beginning 0)))
1354 ;; Get position after ending blank lines.
1355 (end (progn (org-skip-whitespace)
1356 (if (eobp) (point) (point-at-bol))))
1357 ;; Get visibility status.
1358 (hidden (progn (goto-char contents-begin)
1359 (forward-line)
1360 (org-truely-invisible-p))))
1361 `(src-block
1362 (:language ,language
1363 :switches ,switches
1364 :parameters ,parameters
1365 :begin ,begin
1366 :end ,end
1367 :hiddenp ,hidden
1368 :value ,value
1369 :post-blank ,(count-lines contents-end end)
1370 ,@(cadr keywords))))))
1372 (defun org-element-src-block-interpreter (src-block contents)
1373 "Interpret SRC-BLOCK element as Org syntax.
1374 CONTENTS is nil."
1375 (let ((lang (org-element-get-property :language src-block))
1376 (switches (org-element-get-property :switches src-block))
1377 (params (org-element-get-property :parameters src-block))
1378 (value (let ((val (org-element-get-property :value src-block)))
1379 (cond
1380 (org-src-preserve-indentation val)
1381 ((zerop org-edit-src-content-indentation)
1382 (org-remove-indentation val))
1384 (let ((ind (make-string
1385 org-edit-src-content-indentation 32)))
1386 (replace-regexp-in-string
1387 "\\(^\\)[ \t]*\\S-" ind
1388 (org-remove-indentation val) nil nil 1)))))))
1389 (concat (format "#+begin_src%s\n"
1390 (concat (and lang (concat " " lang))
1391 (and switches (concat " " switches))
1392 (and params (concat " " params))))
1393 value
1394 "#+end_src")))
1397 ;;;; Table
1399 (defun org-element-table-parser ()
1400 "Parse a table at point.
1402 Return a list whose car is `table' and cdr is a plist containing
1403 `:begin', `:end', `:contents-begin', `:contents-end', `:tblfm',
1404 `:type', `:raw-table' and `:post-blank' keywords."
1405 (save-excursion
1406 (let* ((table-begin (goto-char (org-table-begin t)))
1407 (type (if (org-at-table.el-p) 'table.el 'org))
1408 (keywords (org-element-collect-affiliated-keywords))
1409 (begin (car keywords))
1410 (table-end (goto-char (marker-position (org-table-end t))))
1411 (tblfm (when (looking-at "[ \t]*#\\+tblfm: +\\(.*\\)[ \t]*")
1412 (prog1 (org-match-string-no-properties 1)
1413 (forward-line))))
1414 (pos-before-blank (point))
1415 (end (progn (org-skip-whitespace)
1416 (if (eobp) (point) (point-at-bol))))
1417 (raw-table (org-remove-indentation
1418 (buffer-substring-no-properties table-begin table-end))))
1419 `(table
1420 (:begin ,begin
1421 :end ,end
1422 :type ,type
1423 :raw-table ,raw-table
1424 :tblfm ,tblfm
1425 :post-blank ,(count-lines pos-before-blank end)
1426 ,@(cadr keywords))))))
1428 (defun org-element-table-interpreter (table contents)
1429 "Interpret TABLE element as Org syntax.
1430 CONTENTS is nil."
1431 (org-element-get-property :raw-table table))
1434 ;;;; Verse Block
1436 (defun org-element-verse-block-parser ()
1437 "Parse a verse block.
1439 Return a list whose car is `verse-block' and cdr is a plist
1440 containing `:begin', `:end', `:hiddenp', `:raw-value', `:value'
1441 and `:post-blank' keywords.
1443 Assume point is at beginning or end of the block."
1444 (save-excursion
1445 (let* ((case-fold-search t)
1446 (keywords (progn
1447 (end-of-line)
1448 (re-search-backward
1449 (concat "^[ \t]*#\\+begin_verse") nil t)
1450 (org-element-collect-affiliated-keywords)))
1451 (begin (car keywords))
1452 (hidden (progn (forward-line) (org-truely-invisible-p)))
1453 (raw-val (buffer-substring-no-properties
1454 (point)
1455 (progn
1456 (re-search-forward (concat "^[ \t]*#\\+end_verse") nil t)
1457 (point-at-bol))))
1458 (pos-before-blank (progn (forward-line) (point)))
1459 (end (progn (org-skip-whitespace)
1460 (if (eobp) (point) (point-at-bol))))
1461 (value (org-element-parse-secondary-string
1462 (org-remove-indentation raw-val)
1463 (cdr (assq 'verse org-element-string-restrictions)))))
1464 `(verse-block
1465 (:begin ,begin
1466 :end ,end
1467 :hiddenp ,hidden
1468 :raw-value ,raw-val
1469 :value ,value
1470 :post-blank ,(count-lines pos-before-blank end)
1471 ,@(cadr keywords))))))
1473 (defun org-element-verse-block-interpreter (verse-block contents)
1474 "Interpret VERSE-BLOCK element as Org syntax.
1475 CONTENTS is nil."
1476 (format "#+begin_verse\n%s#+end_verse"
1477 (org-remove-indentation
1478 (org-element-get-property :raw-value verse-block))))
1482 ;;; Objects
1484 ;; Unlike to elements, interstices can be found between objects.
1485 ;; That's why, along with the parser, successor functions are provided
1486 ;; for each object. Some objects share the same successor
1487 ;; (i.e. `emphasis' and `verbatim' objects).
1489 ;; A successor must accept a single argument bounding the search. It
1490 ;; will return either a cons cell whose car is the object's type, as
1491 ;; a symbol, and cdr the position of its next occurrence, or nil.
1493 ;; Successors follow the naming convention:
1494 ;; org-element-NAME-successor, where NAME is the name of the
1495 ;; successor, as defined in `org-element-all-successors'.
1497 ;; Some object types (i.e `emphasis') are recursive. Restrictions on
1498 ;; object types they can contain will be specified in
1499 ;; `org-element-object-restrictions'.
1501 ;; Adding a new type of object is simple. Implement a successor,
1502 ;; a parser, and an interpreter for it, all following the naming
1503 ;; convention. Register successor in `org-element-all-successors',
1504 ;; maybe tweak restrictions about it, and that's it.
1506 ;;;; Emphasis
1508 (defun org-element-emphasis-parser ()
1509 "Parse text markup object at point.
1511 Return a list whose car is `emphasis' and cdr is a plist with
1512 `:marker', `:begin', `:end', `:contents-begin' and
1513 `:contents-end' and `:post-blank' keywords.
1515 Assume point is at the first emphasis marker."
1516 (save-excursion
1517 (unless (bolp) (backward-char 1))
1518 (looking-at org-emph-re)
1519 (let ((begin (match-beginning 2))
1520 (marker (org-match-string-no-properties 3))
1521 (contents-begin (match-beginning 4))
1522 (contents-end (match-end 4))
1523 (post-blank (progn (goto-char (match-end 2))
1524 (skip-chars-forward " \t")))
1525 (end (point)))
1526 `(emphasis
1527 (:marker ,marker
1528 :begin ,begin
1529 :end ,end
1530 :contents-begin ,contents-begin
1531 :contents-end ,contents-end
1532 :post-blank ,post-blank)))))
1534 (defun org-element-emphasis-interpreter (emphasis contents)
1535 "Interpret EMPHASIS object as Org syntax.
1536 CONTENTS is the contents of the object."
1537 (let ((marker (org-element-get-property :marker emphasis)))
1538 (concat marker contents marker)))
1540 (defun org-element-text-markup-successor (limit)
1541 "Search for the next emphasis or verbatim and return position.
1543 LIMIT bounds the search.
1545 Return value is a cons cell whose car is `emphasis' or
1546 `verbatim' and cdr is beginning position."
1547 (save-excursion
1548 (unless (bolp) (backward-char))
1549 (when (re-search-forward org-emph-re limit t)
1550 (cons (if (nth 4 (assoc (match-string 3) org-emphasis-alist))
1551 'verbatim
1552 'emphasis)
1553 (match-beginning 2)))))
1555 ;;;; Entity
1557 (defun org-element-entity-parser ()
1558 "Parse entity at point.
1560 Return a list whose car is `entity' and cdr a plist with
1561 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1562 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1563 keywords.
1565 Assume point is at the beginning of the entity."
1566 (save-excursion
1567 (looking-at "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1568 (let* ((value (org-entity-get (match-string 1)))
1569 (begin (match-beginning 0))
1570 (bracketsp (string= (match-string 2) "{}"))
1571 (post-blank (progn (goto-char (match-end 1))
1572 (when bracketsp (forward-char 2))
1573 (skip-chars-forward " \t")))
1574 (end (point)))
1575 `(entity
1576 (:name ,(car value)
1577 :latex ,(nth 1 value)
1578 :latex-math-p ,(nth 2 value)
1579 :html ,(nth 3 value)
1580 :ascii ,(nth 4 value)
1581 :latin1 ,(nth 5 value)
1582 :utf-8 ,(nth 6 value)
1583 :begin ,begin
1584 :end ,end
1585 :use-brackets-p ,bracketsp
1586 :post-blank ,post-blank)))))
1588 (defun org-element-entity-interpreter (entity contents)
1589 "Interpret ENTITY object as Org syntax.
1590 CONTENTS is nil."
1591 (concat "\\"
1592 (org-element-get-property :name entity)
1593 (when (org-element-get-property :use-brackets-p entity) "{}")))
1595 (defun org-element-latex-or-entity-successor (limit)
1596 "Search for the next latex-fragment or entity object.
1598 LIMIT bounds the search.
1600 Return value is a cons cell whose car is `entity' or
1601 `latex-fragment' and cdr is beginning position."
1602 (save-excursion
1603 (let ((matchers (plist-get org-format-latex-options :matchers))
1604 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1605 (entity-re
1606 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"))
1607 (when (re-search-forward
1608 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
1609 matchers "\\|")
1610 "\\|" entity-re)
1611 limit t)
1612 (goto-char (match-beginning 0))
1613 (if (looking-at entity-re)
1614 ;; Determine if it's a real entity or a LaTeX command.
1615 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
1616 (match-beginning 0))
1617 ;; No entity nor command: point is at a LaTeX fragment.
1618 ;; Determine its type to get the correct beginning position.
1619 (cons 'latex-fragment
1620 (catch 'return
1621 (mapc (lambda (e)
1622 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
1623 (throw 'return
1624 (match-beginning
1625 (nth 2 (assoc e org-latex-regexps))))))
1626 matchers)
1627 (point))))))))
1630 ;;;; Export Snippet
1632 (defun org-element-export-snippet-parser ()
1633 "Parse export snippet at point.
1635 Return a list whose car is `export-snippet' and cdr a plist with
1636 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1637 keywords.
1639 Assume point is at the beginning of the snippet."
1640 (save-excursion
1641 (looking-at "@\\([-A-Za-z0-9]+\\){")
1642 (let* ((begin (point))
1643 (back-end (org-match-string-no-properties 1))
1644 (before-blank (progn (goto-char (scan-sexps (1- (match-end 0)) 1))))
1645 (value (buffer-substring-no-properties
1646 (match-end 0) (1- before-blank)))
1647 (post-blank (skip-chars-forward " \t"))
1648 (end (point)))
1649 `(export-snippet
1650 (:back-end ,back-end
1651 :value ,value
1652 :begin ,begin
1653 :end ,end
1654 :post-blank ,post-blank)))))
1656 (defun org-element-export-snippet-interpreter (export-snippet contents)
1657 "Interpret EXPORT-SNIPPET object as Org syntax.
1658 CONTENTS is nil."
1659 (format "@%s{%s}"
1660 (org-element-get-property :back-end export-snippet)
1661 (org-element-get-property :value export-snippet)))
1663 (defun org-element-export-snippet-successor (limit)
1664 "Search for the next export-snippet object.
1666 LIMIT bounds the search.
1668 Return value is a cons cell whose car is `export-snippet' cdr is
1669 its beginning position."
1670 (save-excursion
1671 (catch 'exit
1672 (while (re-search-forward "@[-A-Za-z0-9]+{" limit t)
1673 (when (let ((end (ignore-errors (scan-sexps (1- (point)) 1))))
1674 (and end (eq (char-before end) ?})))
1675 (throw 'exit (cons 'export-snippet (match-beginning 0))))))))
1678 ;;;; Footnote Reference
1680 (defun org-element-footnote-reference-parser ()
1681 "Parse footnote reference at point.
1683 Return a list whose car is `footnote-reference' and cdr a plist
1684 with `:label', `:type', `:definition', `:begin', `:end' and
1685 `:post-blank' as keywords."
1686 (save-excursion
1687 (let* ((ref (org-footnote-at-reference-p))
1688 (label (car ref))
1689 (raw-def (nth 3 ref))
1690 (inline-def (and raw-def
1691 (org-element-parse-secondary-string raw-def nil)))
1692 (type (if (nth 3 ref) 'inline 'standard))
1693 (begin (nth 1 ref))
1694 (post-blank (progn (goto-char (nth 2 ref))
1695 (skip-chars-forward " \t")))
1696 (end (point)))
1697 `(footnote-reference
1698 (:label ,label
1699 :type ,type
1700 :inline-definition ,inline-def
1701 :begin ,begin
1702 :end ,end
1703 :post-blank ,post-blank
1704 :raw-definition ,raw-def)))))
1706 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
1707 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1708 CONTENTS is nil."
1709 (let ((label (or (org-element-get-property :label footnote-reference)
1710 "fn:"))
1711 (def (let ((raw (org-element-get-property
1712 :raw-definition footnote-reference)))
1713 (if raw (concat ":" raw) ""))))
1714 (format "[%s]" (concat label def))))
1716 (defun org-element-footnote-reference-successor (limit)
1717 "Search for the next footnote-reference and return beginning
1718 position.
1720 LIMIT bounds the search.
1722 Return value is a cons cell whose car is `footnote-reference' and
1723 cdr is beginning position."
1724 (let (fn-ref)
1725 (when (setq fn-ref (org-footnote-get-next-reference nil nil limit))
1726 (cons 'footnote-reference (nth 1 fn-ref)))))
1729 ;;;; Inline Babel Call
1731 (defun org-element-inline-babel-call-parser ()
1732 "Parse inline babel call at point.
1734 Return a list whose car is `inline-babel-call' and cdr a plist with
1735 `:begin', `:end', `:info' and `:post-blank' as keywords.
1737 Assume point is at the beginning of the babel call."
1738 (save-excursion
1739 (unless (bolp) (backward-char))
1740 (looking-at org-babel-inline-lob-one-liner-regexp)
1741 (let ((info (save-match-data (org-babel-lob-get-info)))
1742 (begin (match-end 1))
1743 (post-blank (progn (goto-char (match-end 0))
1744 (skip-chars-forward " \t")))
1745 (end (point)))
1746 `(inline-babel-call
1747 (:begin ,begin
1748 :end ,end
1749 :info ,info
1750 :post-blank ,post-blank)))))
1752 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
1753 "Interpret INLINE-BABEL-CALL object as Org syntax.
1754 CONTENTS is nil."
1755 (let* ((babel-info (org-element-get-property :info inline-babel-call))
1756 (main-source (car babel-info))
1757 (post-options (nth 1 babel-info)))
1758 (concat "call_"
1759 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
1760 ;; Remove redundant square brackets.
1761 (replace-match
1762 (match-string 1 main-source) nil nil main-source)
1763 main-source)
1764 (and post-options (format "[%s]" post-options)))))
1766 (defun org-element-inline-babel-call-successor (limit)
1767 "Search for the next inline-babel-call and return beginning
1768 position.
1770 LIMIT bounds the search.
1772 Return value is a cons cell whose car is `inline-babel-call' and
1773 cdr is beginning position."
1774 (save-excursion
1775 ;; Use a simplified version of
1776 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
1777 (when (re-search-forward
1778 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
1779 limit t)
1780 (cons 'inline-babel-call (match-beginning 0)))))
1783 ;;;; Inline Src Block
1785 (defun org-element-inline-src-block-parser ()
1786 "Parse inline source block at point.
1788 Return a list whose car is `inline-src-block' and cdr a plist
1789 with `:begin', `:end', `:language', `:value', `:parameters' and
1790 `:post-blank' as keywords.
1792 Assume point is at the beginning of the inline src block."
1793 (save-excursion
1794 (unless (bolp) (backward-char))
1795 (looking-at org-babel-inline-src-block-regexp)
1796 (let ((begin (match-beginning 1))
1797 (language (org-match-string-no-properties 2))
1798 (parameters (org-match-string-no-properties 4))
1799 (value (org-match-string-no-properties 5))
1800 (post-blank (progn (goto-char (match-end 0))
1801 (skip-chars-forward " \t")))
1802 (end (point)))
1803 `(inline-src-block
1804 (:language ,language
1805 :value ,value
1806 :parameters ,parameters
1807 :begin ,begin
1808 :end ,end
1809 :post-blank ,post-blank)))))
1811 (defun org-element-inline-src-block-interpreter (inline-src-block contents)
1812 "Interpret INLINE-SRC-BLOCK object as Org syntax.
1813 CONTENTS is nil."
1814 (let ((language (org-element-get-property :language inline-src-block))
1815 (arguments (org-element-get-property :parameters inline-src-block))
1816 (body (org-element-get-property :value inline-src-block)))
1817 (format "src_%s%s{%s}"
1818 language
1819 (if arguments (format "[%s]" arguments) "")
1820 body)))
1822 (defun org-element-inline-src-block-successor (limit)
1823 "Search for the next inline-babel-call and return beginning position.
1825 LIMIT bounds the search.
1827 Return value is a cons cell whose car is `inline-babel-call' and
1828 cdr is beginning position."
1829 (save-excursion
1830 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
1831 (cons 'inline-src-block (match-beginning 1)))))
1834 ;;;; Latex Fragment
1836 (defun org-element-latex-fragment-parser ()
1837 "Parse latex fragment at point.
1839 Return a list whose car is `latex-fragment' and cdr a plist with
1840 `:value', `:begin', `:end', and `:post-blank' as keywords.
1842 Assume point is at the beginning of the latex fragment."
1843 (save-excursion
1844 (let* ((begin (point))
1845 (substring-match
1846 (catch 'exit
1847 (mapc (lambda (e)
1848 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
1849 (when (or (looking-at latex-regexp)
1850 (and (not (bobp))
1851 (save-excursion
1852 (backward-char)
1853 (looking-at latex-regexp))))
1854 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
1855 (plist-get org-format-latex-options :matchers))
1856 ;; None found: it's a macro.
1857 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
1859 (value (match-string-no-properties substring-match))
1860 (post-blank (progn (goto-char (match-end substring-match))
1861 (skip-chars-forward " \t")))
1862 (end (point)))
1863 `(latex-fragment
1864 (:value ,value
1865 :begin ,begin
1866 :end ,end
1867 :post-blank ,post-blank)))))
1869 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
1870 "Interpret LATEX-FRAGMENT object as Org syntax.
1871 CONTENTS is nil."
1872 (org-element-get-property :value latex-fragment))
1874 ;;;; Line Break
1876 (defun org-element-line-break-parser ()
1877 "Parse line break at point.
1879 Return a list whose car is `line-break', and cdr a plist with
1880 `:begin', `:end' and `:post-blank' keywords.
1882 Assume point is at the beginning of the line break."
1883 (let ((begin (point))
1884 (end (save-excursion (forward-line) (point))))
1885 `(line-break (:begin ,begin :end ,end :post-blank 0))))
1887 (defun org-element-line-break-interpreter (line-break contents)
1888 "Interpret LINE-BREAK object as Org syntax.
1889 CONTENTS is nil."
1890 "\\\\\n")
1892 (defun org-element-line-break-successor (limit)
1893 "Search for the next statistics cookie and return position.
1895 LIMIT bounds the search.
1897 Return value is a cons cell whose car is `line-break' and cdr is
1898 beginning position."
1899 (save-excursion
1900 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
1901 (goto-char (match-beginning 1)))))
1902 ;; A line break can only happen on a non-empty line.
1903 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
1904 (cons 'line-break beg)))))
1907 ;;;; Link
1909 (defun org-element-link-parser ()
1910 "Parse link at point.
1912 Return a list whose car is `link' and cdr a plist with `:type',
1913 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
1914 `:contents-end' and `:post-blank' as keywords.
1916 Assume point is at the beginning of the link."
1917 (save-excursion
1918 (let ((begin (point))
1919 end contents-begin contents-end link-end post-blank path type
1920 raw-link link)
1921 (cond
1922 ;; Type 1: text targeted from a radio target.
1923 ((and org-target-link-regexp (looking-at org-target-link-regexp))
1924 (setq type "radio"
1925 path (org-match-string-no-properties 0)
1926 contents-begin (match-beginning 0)
1927 contents-end (match-end 0)
1928 link-end (match-end 0)))
1929 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
1930 ((looking-at org-bracket-link-regexp)
1931 (setq contents-begin (match-beginning 3)
1932 contents-end (match-end 3)
1933 link-end (match-end 0)
1934 ;; RAW-LINK is the original link.
1935 raw-link (org-match-string-no-properties 1)
1936 link (org-link-expand-abbrev
1937 (replace-regexp-in-string
1938 " *\n *" " " (org-link-unescape raw-link) t t)))
1939 ;; Determine TYPE of link and set PATH accordingly.
1940 (cond
1941 ;; File type.
1942 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
1943 (setq type "file" path link))
1944 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
1945 ((string-match org-link-re-with-space3 link)
1946 (setq type (match-string 1 link) path (match-string 2 link)))
1947 ;; Id type: PATH is the id.
1948 ((string-match "^id:\\([-a-f0-9]+\\)" link)
1949 (setq type "id" path (match-string 1 link)))
1950 ;; Code-ref type: PATH is the name of the reference.
1951 ((string-match "^(\\(.*\\))$" link)
1952 (setq type "coderef" path (match-string 1 link)))
1953 ;; Custom-id type: PATH is the name of the custom id.
1954 ((= (aref link 0) ?#)
1955 (setq type "custom-id" path (substring link 1)))
1956 ;; Fuzzy type: Internal link either matches a target, an
1957 ;; headline name or nothing. PATH is the target or headline's
1958 ;; name.
1959 (t (setq type "fuzzy" path link))))
1960 ;; Type 3: Plain link, i.e. http://orgmode.org
1961 ((looking-at org-plain-link-re)
1962 (setq raw-link (org-match-string-no-properties 0)
1963 type (org-match-string-no-properties 1)
1964 path (org-match-string-no-properties 2)
1965 link-end (match-end 0)))
1966 ;; Type 4: Angular link, i.e. <http://orgmode.org>
1967 ((looking-at org-angle-link-re)
1968 (setq raw-link (buffer-substring-no-properties
1969 (match-beginning 1) (match-end 2))
1970 type (org-match-string-no-properties 1)
1971 path (org-match-string-no-properties 2)
1972 link-end (match-end 0))))
1973 ;; In any case, deduce end point after trailing white space from
1974 ;; LINK-END variable.
1975 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
1976 end (point))
1977 `(link
1978 (:type ,type
1979 :path ,path
1980 :raw-link ,(or raw-link path)
1981 :begin ,begin
1982 :end ,end
1983 :contents-begin ,contents-begin
1984 :contents-end ,contents-end
1985 :post-blank ,post-blank)))))
1987 (defun org-element-link-interpreter (link contents)
1988 "Interpret LINK object as Org syntax.
1989 CONTENTS is the contents of the object."
1990 (let ((type (org-element-get-property :type link))
1991 (raw-link (org-element-get-property :raw-link link)))
1992 (cond
1993 ((string= type "radio") raw-link)
1994 (t (format "[[%s]%s]"
1995 raw-link
1996 (if (string= contents "") "" (format "[%s]" contents)))))))
1998 (defun org-element-link-successor (limit)
1999 "Search for the next link and return position.
2001 LIMIT bounds the search.
2003 Return value is a cons cell whose car is `link' and cdr is
2004 beginning position."
2005 (save-excursion
2006 (let ((link-regexp
2007 (if org-target-link-regexp
2008 (concat org-any-link-re "\\|" org-target-link-regexp)
2009 org-any-link-re)))
2010 (when (re-search-forward link-regexp limit t)
2011 (cons 'link (match-beginning 0))))))
2014 ;;;; Macro
2016 (defun org-element-macro-parser ()
2017 "Parse macro at point.
2019 Return a list whose car is `macro' and cdr a plist with `:key',
2020 `:args', `:begin', `:end', `:value' and `:post-blank' as
2021 keywords.
2023 Assume point is at the macro."
2024 (save-excursion
2025 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2026 (let ((begin (point))
2027 (key (downcase (org-match-string-no-properties 1)))
2028 (value (org-match-string-no-properties 0))
2029 (post-blank (progn (goto-char (match-end 0))
2030 (skip-chars-forward " \t")))
2031 (end (point))
2032 (args (let ((args (org-match-string-no-properties 3)) args2)
2033 (when args
2034 (setq args (org-split-string args ","))
2035 (while args
2036 (while (string-match "\\\\\\'" (car args))
2037 ;; Repair bad splits.
2038 (setcar (cdr args) (concat (substring (car args) 0 -1)
2039 "," (nth 1 args)))
2040 (pop args))
2041 (push (pop args) args2))
2042 (mapcar 'org-trim (nreverse args2))))))
2043 `(macro
2044 (:key ,key
2045 :value ,value
2046 :args ,args
2047 :begin ,begin
2048 :end ,end
2049 :post-blank ,post-blank)))))
2051 (defun org-element-macro-interpreter (macro contents)
2052 "Interpret MACRO object as Org syntax.
2053 CONTENTS is nil."
2054 (org-element-get-property :value macro))
2056 (defun org-element-macro-successor (limit)
2057 "Search for the next macro and return position.
2059 LIMIT bounds the search.
2061 Return value is cons cell whose car is `macro' and cdr is
2062 beginning position."
2063 (save-excursion
2064 (when (re-search-forward
2065 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2066 limit t)
2067 (cons 'macro (match-beginning 0)))))
2070 ;;;; Radio-target
2072 (defun org-element-radio-target-parser ()
2073 "Parse radio target at point.
2075 Return a list whose car is `radio-target' and cdr a plist with
2076 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2077 and `:post-blank' as keywords.
2079 Assume point is at the radio target."
2080 (save-excursion
2081 (looking-at org-radio-target-regexp)
2082 (let ((begin (point))
2083 (contents-begin (match-beginning 1))
2084 (contents-end (match-end 1))
2085 (raw-value (org-match-string-no-properties 1))
2086 (post-blank (progn (goto-char (match-end 0))
2087 (skip-chars-forward " \t")))
2088 (end (point)))
2089 `(radio-target
2090 (:begin ,begin
2091 :end ,end
2092 :contents-begin ,contents-begin
2093 :contents-end ,contents-end
2094 :raw-value ,raw-value
2095 :post-blank ,post-blank)))))
2097 (defun org-element-radio-target-interpreter (target contents)
2098 "Interpret TARGET object as Org syntax.
2099 CONTENTS is the contents of the object."
2100 (concat "<<<" contents ">>>"))
2102 (defun org-element-radio-target-successor (limit)
2103 "Search for the next radio-target and return position.
2105 LIMIT bounds the search.
2107 Return value is a cons cell whose car is `radio-target' and cdr
2108 is beginning position."
2109 (save-excursion
2110 (when (re-search-forward org-radio-target-regexp limit t)
2111 (cons 'radio-target (match-beginning 0)))))
2114 ;;;; Statistics Cookie
2116 (defun org-element-statistics-cookie-parser ()
2117 "Parse statistics cookie at point.
2119 Return a list whose car is `statistics-cookie', and cdr a plist
2120 with `:begin', `:end', `:value' and `:post-blank' keywords.
2122 Assume point is at the beginning of the statistics-cookie."
2123 (save-excursion
2124 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2125 (let* ((begin (point))
2126 (value (buffer-substring-no-properties
2127 (match-beginning 0) (match-end 0)))
2128 (post-blank (progn (goto-char (match-end 0))
2129 (skip-chars-forward " \t")))
2130 (end (point)))
2131 `(statistics-cookie
2132 (:begin ,begin
2133 :end ,end
2134 :value ,value
2135 :post-blank ,post-blank)))))
2137 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
2138 "Interpret STATISTICS-COOKIE object as Org syntax.
2139 CONTENTS is nil."
2140 (org-element-get-property :value statistics-cookie))
2142 (defun org-element-statistics-cookie-successor (limit)
2143 "Search for the next statistics cookie and return position.
2145 LIMIT bounds the search.
2147 Return value is a cons cell whose car is `statistics-cookie' and
2148 cdr is beginning position."
2149 (save-excursion
2150 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
2151 (cons 'statistics-cookie (match-beginning 0)))))
2154 ;;;; Subscript
2156 (defun org-element-subscript-parser ()
2157 "Parse subscript at point.
2159 Return a list whose car is `subscript' and cdr a plist with
2160 `:begin', `:end', `:contents-begin', `:contents-end',
2161 `:use-brackets-p' and `:post-blank' as keywords.
2163 Assume point is at the underscore."
2164 (save-excursion
2165 (unless (bolp) (backward-char))
2166 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
2168 (not (looking-at org-match-substring-regexp))))
2169 (begin (match-beginning 2))
2170 (contents-begin (or (match-beginning 5)
2171 (match-beginning 3)))
2172 (contents-end (or (match-end 5) (match-end 3)))
2173 (post-blank (progn (goto-char (match-end 0))
2174 (skip-chars-forward " \t")))
2175 (end (point)))
2176 `(subscript
2177 (:begin ,begin
2178 :end ,end
2179 :use-brackets-p ,bracketsp
2180 :contents-begin ,contents-begin
2181 :contents-end ,contents-end
2182 :post-blank ,post-blank)))))
2184 (defun org-element-subscript-interpreter (subscript contents)
2185 "Interpret SUBSCRIPT object as Org syntax.
2186 CONTENTS is the contents of the object."
2187 (format
2188 (if (org-element-get-property :use-brackets-p subscript) "_{%s}" "_%s")
2189 contents))
2191 (defun org-element-sub/superscript-successor (limit)
2192 "Search for the next sub/superscript and return beginning
2193 position.
2195 LIMIT bounds the search.
2197 Return value is a cons cell whose car is either `subscript' or
2198 `superscript' and cdr is beginning position."
2199 (save-excursion
2200 (when (re-search-forward org-match-substring-regexp limit t)
2201 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
2202 (match-beginning 2)))))
2205 ;;;; Superscript
2207 (defun org-element-superscript-parser ()
2208 "Parse superscript at point.
2210 Return a list whose car is `superscript' and cdr a plist with
2211 `:begin', `:end', `:contents-begin', `:contents-end',
2212 `:use-brackets-p' and `:post-blank' as keywords.
2214 Assume point is at the caret."
2215 (save-excursion
2216 (unless (bolp) (backward-char))
2217 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
2219 (not (looking-at org-match-substring-regexp))))
2220 (begin (match-beginning 2))
2221 (contents-begin (or (match-beginning 5)
2222 (match-beginning 3)))
2223 (contents-end (or (match-end 5) (match-end 3)))
2224 (post-blank (progn (goto-char (match-end 0))
2225 (skip-chars-forward " \t")))
2226 (end (point)))
2227 `(superscript
2228 (:begin ,begin
2229 :end ,end
2230 :use-brackets-p ,bracketsp
2231 :contents-begin ,contents-begin
2232 :contents-end ,contents-end
2233 :post-blank ,post-blank)))))
2235 (defun org-element-superscript-interpreter (superscript contents)
2236 "Interpret SUPERSCRIPT object as Org syntax.
2237 CONTENTS is the contents of the object."
2238 (format
2239 (if (org-element-get-property :use-brackets-p superscript) "^{%s}" "^%s")
2240 contents))
2243 ;;;; Target
2245 (defun org-element-target-parser ()
2246 "Parse target at point.
2248 Return a list whose car is `target' and cdr a plist with
2249 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2250 and `:post-blank' as keywords.
2252 Assume point is at the target."
2253 (save-excursion
2254 (looking-at org-target-regexp)
2255 (let ((begin (point))
2256 (contents-begin (match-beginning 1))
2257 (contents-end (match-end 1))
2258 (raw-value (org-match-string-no-properties 1))
2259 (post-blank (progn (goto-char (match-end 0))
2260 (skip-chars-forward " \t")))
2261 (end (point)))
2262 `(target
2263 (:begin ,begin
2264 :end ,end
2265 :contents-begin ,contents-begin
2266 :contents-end ,contents-end
2267 :raw-value ,raw-value
2268 :post-blank ,post-blank)))))
2270 (defun org-element-target-interpreter (target contents)
2271 "Interpret TARGET object as Org syntax.
2272 CONTENTS is the contents of target."
2273 (concat ""))
2275 (defun org-element-target-successor (limit)
2276 "Search for the next target and return position.
2278 LIMIT bounds the search.
2280 Return value is a cons cell whose car is `target' and cdr is
2281 beginning position."
2282 (save-excursion
2283 (when (re-search-forward org-target-regexp limit t)
2284 (cons 'target (match-beginning 0)))))
2287 ;;;; Time-stamp
2289 (defun org-element-time-stamp-parser ()
2290 "Parse time stamp at point.
2292 Return a list whose car is `time-stamp', and cdr a plist with
2293 `:appt-type', `:type', `:begin', `:end', `:value' and
2294 `:post-blank' keywords.
2296 Assume point is at the beginning of the time-stamp."
2297 (save-excursion
2298 (let* ((appt-type (cond
2299 ((looking-at (concat org-deadline-string " +"))
2300 (goto-char (match-end 0))
2301 'deadline)
2302 ((looking-at (concat org-scheduled-string " +"))
2303 (goto-char (match-end 0))
2304 'scheduled)
2305 ((looking-at (concat org-closed-string " +"))
2306 (goto-char (match-end 0))
2307 'closed)))
2308 (begin (and appt-type (match-beginning 0)))
2309 (type (cond
2310 ((looking-at org-tsr-regexp)
2311 (if (match-string 2) 'active-range 'active))
2312 ((looking-at org-tsr-regexp-both)
2313 (if (match-string 2) 'inactive-range 'inactive))
2314 ((looking-at (concat
2315 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2316 "\\|"
2317 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2318 'diary)))
2319 (begin (or begin (match-beginning 0)))
2320 (value (buffer-substring-no-properties
2321 (match-beginning 0) (match-end 0)))
2322 (post-blank (progn (goto-char (match-end 0))
2323 (skip-chars-forward " \t")))
2324 (end (point)))
2325 `(time-stamp
2326 (:appt-type ,appt-type
2327 :type ,type
2328 :value ,value
2329 :begin ,begin
2330 :end ,end
2331 :post-blank ,post-blank)))))
2333 (defun org-element-time-stamp-interpreter (time-stamp contents)
2334 "Interpret TIME-STAMP object as Org syntax.
2335 CONTENTS is nil."
2336 (concat
2337 (case (org-element-get-property :appt-type time-stamp)
2338 (closed (concat org-closed-string " "))
2339 (deadline (concat org-deadline-string " "))
2340 (scheduled (concat org-scheduled-string " ")))
2341 (org-element-get-property :value time-stamp)))
2343 (defun org-element-time-stamp-successor (limit)
2344 "Search for the next time-stamp and return position.
2346 LIMIT bounds the search.
2348 Return value is a cons cell whose car is `time-stamp' and cdr is
2349 beginning position."
2350 (save-excursion
2351 (when (re-search-forward
2352 (concat "\\(?:" org-scheduled-string " +\\|"
2353 org-deadline-string " +\\|" org-closed-string " +\\)?"
2354 org-ts-regexp-both
2355 "\\|"
2356 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2357 "\\|"
2358 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2359 limit t)
2360 (cons 'time-stamp (match-beginning 0)))))
2363 ;;;; Verbatim
2365 (defun org-element-verbatim-parser ()
2366 "Parse verbatim object at point.
2368 Return a list whose car is `verbatim' and cdr is a plist with
2369 `:marker', `:begin', `:end' and `:post-blank' keywords.
2371 Assume point is at the first verbatim marker."
2372 (save-excursion
2373 (unless (bolp) (backward-char 1))
2374 (looking-at org-emph-re)
2375 (let ((begin (match-beginning 2))
2376 (marker (org-match-string-no-properties 3))
2377 (value (org-match-string-no-properties 4))
2378 (post-blank (progn (goto-char (match-end 2))
2379 (skip-chars-forward " \t")))
2380 (end (point)))
2381 `(verbatim
2382 (:marker ,marker
2383 :begin ,begin
2384 :end ,end
2385 :value ,value
2386 :post-blank ,post-blank)))))
2388 (defun org-element-verbatim-interpreter (verbatim contents)
2389 "Interpret VERBATIM object as Org syntax.
2390 CONTENTS is nil."
2391 (let ((marker (org-element-get-property :marker verbatim))
2392 (value (org-element-get-property :value verbatim)))
2393 (concat marker value marker)))
2397 ;;; Definitions And Rules
2399 ;; Define elements, greater elements and specify recursive objects,
2400 ;; along with the affiliated keywords recognized. Also set up
2401 ;; restrictions on recursive objects combinations.
2403 ;; These variables really act as a control center for the parsing
2404 ;; process.
2405 (defconst org-element-paragraph-separate
2406 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2407 ;; Headlines and inlinetasks.
2408 org-outline-regexp-bol "\\|"
2409 ;; Comments, blocks (any type), keywords and babel calls.
2410 "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
2411 ;; Lists.
2412 (org-item-beginning-re) "\\|"
2413 ;; Fixed-width, drawers (any type) and tables.
2414 "^[ \t]*[:|]" "\\|"
2415 ;; Footnote definitions.
2416 org-footnote-definition-re "\\|"
2417 ;; Horizontal rules.
2418 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2419 ;; LaTeX environments.
2420 "^[ \t]*\\\\\\(begin\\|end\\)")
2421 "Regexp to separate paragraphs in an Org buffer.")
2423 (defconst org-element-all-elements
2424 '(center-block comment comment-block drawer dynamic-block example-block
2425 export-block fixed-width footnote-definition headline
2426 horizontal-rule inlinetask item keyword latex-environment
2427 babel-call paragraph plain-list property-drawer quote-block
2428 quote-section section special-block src-block table
2429 verse-block)
2430 "Complete list of elements.")
2432 (defconst org-element-greater-elements
2433 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2434 item plain-list quote-block section special-block)
2435 "List of recursive element types aka Greater Elements.")
2437 (defconst org-element-all-successors
2438 '(export-snippet footnote-reference inline-babel-call inline-src-block
2439 latex-or-entity line-break link macro radio-target
2440 statistics-cookie sub/superscript target text-markup
2441 time-stamp)
2442 "Complete list of successors.")
2444 (defconst org-element-object-successor-alist
2445 '((subscript . sub/superscript) (superscript . sub/superscript)
2446 (emphasis . text-markup) (verbatim . text-markup)
2447 (entity . latex-or-entity) (latex-fragment . latex-or-entity))
2448 "Alist of translations between object type and successor name.
2450 Sharing the same successor comes handy when, for example, the
2451 regexp matching one object can also match the other object.")
2453 (defconst org-element-recursive-objects
2454 '(emphasis link subscript superscript target radio-target)
2455 "List of recursive object types.")
2457 (defconst org-element-non-recursive-block-alist
2458 '(("ascii" . export-block)
2459 ("comment" . comment-block)
2460 ("docbook" . export-block)
2461 ("example" . example-block)
2462 ("html" . export-block)
2463 ("latex" . latex-block)
2464 ("odt" . export-block)
2465 ("src" . src-block)
2466 ("verse" . verse-block))
2467 "Alist between non-recursive block name and their element type.")
2469 (defconst org-element-affiliated-keywords
2470 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "caption"
2471 "data" "header" "headers" "label" "name" "plot" "resname" "result" "results"
2472 "source" "srcname" "tblname")
2473 "List of affiliated keywords as strings.")
2475 (defconst org-element-keyword-translation-alist
2476 '(("data" . "name") ("label" . "name") ("resname" . "name")
2477 ("source" . "name") ("srcname" . "name") ("tblname" . "name")
2478 ("result" . "results") ("headers" . "header"))
2479 "Alist of usual translations for keywords.
2480 The key is the old name and the value the new one. The property
2481 holding their value will be named after the translated name.")
2483 (defconst org-element-multiple-keywords
2484 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "header")
2485 "List of affiliated keywords that can occur more that once in an element.
2487 Their value will be consed into a list of strings, which will be
2488 returned as the value of the property.
2490 This list is checked after translations have been applied. See
2491 `org-element-keyword-translation-alist'.")
2493 (defconst org-element-parsed-keywords '("author" "caption" "title")
2494 "List of keywords whose value can be parsed.
2496 Their value will be stored as a secondary string: a list of
2497 strings and objects.
2499 This list is checked after translations have been applied. See
2500 `org-element-keyword-translation-alist'.")
2502 (defconst org-element-dual-keywords '("results")
2503 "List of keywords which can have a secondary value.
2505 In Org syntax, they can be written with optional square brackets
2506 before the colons. For example, results keyword can be
2507 associated to a hash value with the following:
2509 #+results[hash-string]: some-source
2511 This list is checked after translations have been applied. See
2512 `org-element-keyword-translation-alist'.")
2514 (defconst org-element-object-restrictions
2515 '((emphasis entity export-snippet inline-babel-call inline-src-block
2516 radio-target sub/superscript target text-markup time-stamp)
2517 (link entity export-snippet inline-babel-call inline-src-block
2518 latex-fragment sub/superscript text-markup)
2519 (radio-target entity export-snippet latex-fragment sub/superscript)
2520 (subscript entity export-snippet inline-babel-call inline-src-block
2521 latex-fragment sub/superscript text-markup)
2522 (superscript entity export-snippet inline-babel-call inline-src-block
2523 latex-fragment sub/superscript text-markup)
2524 (target entity export-snippet latex-fragment sub/superscript text-markup))
2525 "Alist of recursive objects restrictions.
2527 Car is a recursive object type and cdr is a list of successors
2528 that will be called within an object of such type.
2530 For example, in a `radio-target' object, one can only find
2531 entities, export snippets, latex-fragments, subscript and
2532 superscript.")
2534 (defconst org-element-string-restrictions
2535 '((headline entity inline-babel-call latex-fragment link macro radio-target
2536 statistics-cookie sub/superscript text-markup time-stamp)
2537 (inlinetask entity inline-babel-call latex-fragment link macro radio-target
2538 sub/superscript text-markup time-stamp)
2539 (item entity inline-babel-call latex-fragment macro radio-target
2540 sub/superscript target verbatim)
2541 (keyword entity latex-fragment macro sub/superscript text-markup)
2542 (table entity latex-fragment macro text-markup)
2543 (verse entity footnote-reference inline-babel-call inline-src-block
2544 latex-fragment line-break link macro radio-target sub/superscript
2545 target text-markup time-stamp))
2546 "Alist of secondary strings restrictions.
2548 When parsed, some elements have a secondary string which could
2549 contain various objects (i.e. headline's name, or table's cells).
2550 For association, the car is the element type, and the cdr a list
2551 of successors that will be called in that secondary string.
2553 Note: `keyword' secondary string type only applies to keywords
2554 matching `org-element-parsed-keywords'.")
2558 ;;; Accessors
2560 ;; Provide two accessors: `org-element-get-property' and
2561 ;; `org-element-get-contents'.
2563 (defun org-element-get-property (property element)
2564 "Extract the value from the PROPERTY of an ELEMENT."
2565 (plist-get (nth 1 element) property))
2567 (defun org-element-get-contents (element)
2568 "Extract contents from an ELEMENT."
2569 (nthcdr 2 element))
2573 ;; Obtaining The Smallest Element Containing Point
2575 ;; `org-element-at-point' is the core function of this section. It
2576 ;; returns the Lisp representation of the element at point. It uses
2577 ;; `org-element-guess-type' and `org-element-skip-keywords' as helper
2578 ;; functions.
2580 ;; When point is at an item, there is no automatic way to determine if
2581 ;; the function should return the `plain-list' element, or the
2582 ;; corresponding `item' element. By default, `org-element-at-point'
2583 ;; works at the `plain-list' level. But, by providing an optional
2584 ;; argument, one can make it switch to the `item' level.
2586 (defconst org-element--affiliated-re
2587 (format "[ \t]*#\\+\\(%s\\):"
2588 (mapconcat
2589 (lambda (keyword)
2590 (if (member keyword org-element-dual-keywords)
2591 (format "\\(%s\\)\\(?:\\[\\(.*?\\)\\]\\)?"
2592 (regexp-quote keyword))
2593 (regexp-quote keyword)))
2594 org-element-affiliated-keywords "\\|"))
2595 "Regexp matching any affiliated keyword.
2597 Keyword name is put in match group 1. Moreover, if keyword
2598 belongs to `org-element-dual-keywords', put the dual value in
2599 match group 2.
2601 Don't modify it, set `org-element--affiliated-keywords' instead.")
2603 (defun org-element-at-point (&optional special structure)
2604 "Determine closest element around point.
2606 Return value is a list \(TYPE PROPS\) where TYPE is the type of
2607 the element and PROPS a plist of properties associated to the
2608 element.
2610 Possible types are defined in `org-element-all-elements'.
2612 Optional argument SPECIAL, when non-nil, can be either `item' or
2613 `section'. The former allows to parse item wise instead of
2614 plain-list wise, using STRUCTURE as the current list structure.
2615 The latter will try to parse a section before anything else.
2617 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
2618 be computed."
2619 (save-excursion
2620 (beginning-of-line)
2621 ;; Move before any blank line.
2622 (when (looking-at "[ \t]*$")
2623 (skip-chars-backward " \r\t\n")
2624 (beginning-of-line))
2625 (let ((case-fold-search t))
2626 ;; Check if point is at an affiliated keyword. In that case,
2627 ;; try moving to the beginning of the associated element. If
2628 ;; the keyword is orphaned, treat it as plain text.
2629 (when (looking-at org-element--affiliated-re)
2630 (let ((opoint (point)))
2631 (while (looking-at org-element--affiliated-re) (forward-line))
2632 (when (looking-at "[ \t]*$") (goto-char opoint))))
2633 (let ((type (org-element-guess-type (eq special 'section))))
2634 (cond
2635 ;; Guessing element type on the current line is impossible:
2636 ;; try to find the beginning of the current element to get
2637 ;; more information.
2638 ((not type)
2639 (let ((search-origin (point))
2640 (opoint-in-item-p (org-in-item-p))
2641 (par-found-p
2642 (progn
2643 (end-of-line)
2644 (re-search-backward org-element-paragraph-separate nil 'm))))
2645 (cond
2646 ;; Unable to find a paragraph delimiter above: we're at
2647 ;; bob and looking at a paragraph.
2648 ((not par-found-p) (org-element-paragraph-parser))
2649 ;; Trying to find element's beginning set point back to
2650 ;; its original position. There's something peculiar on
2651 ;; this line that prevents parsing, probably an
2652 ;; ill-formed keyword or an undefined drawer name. Parse
2653 ;; it as plain text anyway.
2654 ((< search-origin (point-at-eol)) (org-element-paragraph-parser))
2655 ;; Original point wasn't in a list but previous paragraph
2656 ;; is. It means that either point was inside some block,
2657 ;; or current list was ended without using a blank line.
2658 ;; In the last case, paragraph really starts at list end.
2659 ((let (item)
2660 (and (not opoint-in-item-p)
2661 (not (looking-at "[ \t]*#\\+begin"))
2662 (setq item (org-in-item-p))
2663 (let ((struct (save-excursion (goto-char item)
2664 (org-list-struct))))
2665 (goto-char (org-list-get-bottom-point struct))
2666 (org-skip-whitespace)
2667 (beginning-of-line)
2668 (org-element-paragraph-parser)))))
2669 ((org-footnote-at-definition-p)
2670 (org-element-footnote-definition-parser))
2671 ((and opoint-in-item-p (org-at-item-p) (= opoint-in-item-p (point)))
2672 (if (eq special 'item)
2673 (org-element-item-parser (or structure (org-list-struct)))
2674 (org-element-plain-list-parser (or structure (org-list-struct)))))
2675 ;; In any other case, the paragraph started the line
2676 ;; below.
2677 (t (forward-line) (org-element-paragraph-parser)))))
2678 ((eq type 'plain-list)
2679 (if (eq special 'item)
2680 (org-element-item-parser (or structure (org-list-struct)))
2681 (org-element-plain-list-parser (or structure (org-list-struct)))))
2682 ;; Straightforward case: call the appropriate parser.
2683 (t (funcall (intern (format "org-element-%s-parser" type)))))))))
2686 ;; It is obvious to tell if point is in most elements, either by
2687 ;; looking for a specific regexp in the current line, or by using
2688 ;; already implemented functions. This is the goal of
2689 ;; `org-element-guess-type'.
2691 (defconst org-element--element-block-types
2692 (mapcar 'car org-element-non-recursive-block-alist)
2693 "List of non-recursive block types, as strings.
2694 Used internally by `org-element-guess-type'. Do not modify it
2695 directly, set `org-element-non-recursive-block-alist' instead.")
2697 (defun org-element-guess-type (&optional section-mode)
2698 "Return the type of element at point, or nil if undetermined.
2700 This function may move point to an appropriate position for
2701 parsing. Used internally by `org-element-at-point'.
2703 When optional argument SECTION-MODE is non-nil, try to find if
2704 point is in a section in priority."
2705 ;; Beware: Order matters for some cases in that function.
2706 (beginning-of-line)
2707 (let ((case-fold-search t))
2708 (cond
2709 ((org-with-limited-levels (org-at-heading-p)) 'headline)
2710 ((let ((headline (ignore-errors (nth 4 (org-heading-components)))))
2711 (and headline
2712 (let (case-fold-search)
2713 (string-match (format "^%s\\(?: \\|$\\)" org-quote-string)
2714 headline))))
2715 'quote-section)
2716 ;; Any buffer position not at an headline or in a quote section
2717 ;; is inside a section, provided function is actively looking for
2718 ;; them.
2719 (section-mode 'section)
2720 ;; Non-recursive block.
2721 ((let ((type (org-in-block-p org-element--element-block-types)))
2722 (and type (cdr (assoc type org-element-non-recursive-block-alist)))))
2723 ((org-at-heading-p) 'inlinetask)
2724 ((org-between-regexps-p
2725 "^[ \t]*\\\\begin{" "^[ \t]*\\\\end{[^}]*}[ \t]*") 'latex-environment)
2726 ;; Property drawer. Almost `org-at-property-p', but allow drawer
2727 ;; boundaries.
2728 ((org-with-wide-buffer
2729 (and (not (org-before-first-heading-p))
2730 (let ((pblock (org-get-property-block)))
2731 (and pblock
2732 (<= (point) (cdr pblock))
2733 (>= (point-at-eol) (1- (car pblock)))))))
2734 'property-drawer)
2735 ;; Recursive block. If the block isn't complete, parse the
2736 ;; current part as a paragraph.
2737 ((looking-at "[ \t]*#\\+\\(begin\\|end\\)_\\([-A-Za-z0-9]+\\)\\(?:$\\|\\s-\\)")
2738 (let ((type (downcase (match-string 2))))
2739 (cond
2740 ((not (org-in-block-p (list type))) 'paragraph)
2741 ((string= type "center") 'center-block)
2742 ((string= type "quote") 'quote-block)
2743 (t 'special-block))))
2744 ;; Regular drawers must be tested after property drawer as both
2745 ;; elements share the same ending regexp.
2746 ((or (looking-at org-drawer-regexp) (looking-at "[ \t]*:END:[ \t]*$"))
2747 (let ((completep (org-between-regexps-p
2748 org-drawer-regexp "^[ \t]*:END:[ \t]*$")))
2749 (if (not completep) 'paragraph
2750 (goto-char (car completep)) 'drawer)))
2751 ((looking-at "[ \t]*:\\( \\|$\\)") 'fixed-width)
2752 ;; Babel calls must be tested before general keywords as they are
2753 ;; a subset of them.
2754 ((looking-at org-babel-block-lob-one-liner-regexp) 'babel-call)
2755 ((looking-at org-footnote-definition-re) 'footnote-definition)
2756 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
2757 (if (member (downcase (match-string 1)) org-element-affiliated-keywords)
2758 'paragraph
2759 'keyword))
2760 ;; Dynamic block: simplify regexp used for match. If it isn't
2761 ;; complete, parse the current part as a paragraph.
2762 ((looking-at "[ \t]*#\\+\\(begin\\end\\):\\(?:\\s-\\|$\\)")
2763 (let ((completep (org-between-regexps-p
2764 "^[ \t]*#\\+begin:\\(?:\\s-\\|$\\)"
2765 "^[ \t]*#\\+end:\\(?:\\s-\\|$\\)")))
2766 (if (not completep) 'paragraph
2767 (goto-char (car completep)) 'dynamic-block)))
2768 ((looking-at "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)") 'comment)
2769 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$") 'horizontal-rule)
2770 ((org-at-table-p t) 'table)
2771 ((looking-at "[ \t]*#\\+tblfm:")
2772 (forward-line -1)
2773 ;; A TBLFM line separated from any table is just plain text.
2774 (if (org-at-table-p) 'table
2775 (forward-line) 'paragraph))
2776 ((looking-at (org-item-re)) 'plain-list))))
2778 ;; Most elements can have affiliated keywords. When looking for an
2779 ;; element beginning, we want to move before them, as they belong to
2780 ;; that element, and, in the meantime, collect information they give
2781 ;; into appropriate properties. Hence the following function.
2783 ;; Usage of optional arguments may not be obvious at first glance:
2785 ;; - TRANS-LIST is used to polish keywords names that have evolved
2786 ;; during Org history. In example, even though =result= and
2787 ;; =results= coexist, we want to have them under the same =result=
2788 ;; property. It's also true for "srcname" and "name", where the
2789 ;; latter seems to be preferred nowadays (thus the "name" property).
2791 ;; - CONSED allows to regroup multi-lines keywords under the same
2792 ;; property, while preserving their own identity. This is mostly
2793 ;; used for "attr_latex" and al.
2795 ;; - PARSED prepares a keyword value for export. This is useful for
2796 ;; "caption". Objects restrictions for such keywords are defined in
2797 ;; `org-element-string-restrictions'.
2799 ;; - DUALS is used to take care of keywords accepting a main and an
2800 ;; optional secondary values. For example "results" has its
2801 ;; source's name as the main value, and may have an hash string in
2802 ;; optional square brackets as the secondary one.
2804 ;; A keyword may belong to more than one category.
2806 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
2807 consed parsed duals)
2808 "Collect affiliated keywords before point.
2810 Optional argument KEY-RE is a regexp matching keywords, which
2811 puts matched keyword in group 1. It defaults to
2812 `org-element--affiliated-re'.
2814 TRANS-LIST is an alist where key is the keyword and value the
2815 property name it should be translated to, without the colons. It
2816 defaults to `org-element-keyword-translation-alist'.
2818 CONSED is a list of strings. Any keyword belonging to that list
2819 will have its value consed. The check is done after keyword
2820 translation. It defaults to `org-element-multiple-keywords'.
2822 PARSED is a list of strings. Any keyword member of this list
2823 will have its value parsed. The check is done after keyword
2824 translation. If a keyword is a member of both CONSED and PARSED,
2825 it's value will be a list of parsed strings. It defaults to
2826 `org-element-parsed-keywords'.
2828 DUALS is a list of strings. Any keyword member of this list can
2829 have two parts: one mandatory and one optional. Its value is
2830 a cons cell whose car is the former, and the cdr the latter. If
2831 a keyword is a member of both PARSED and DUALS, only the primary
2832 part will be parsed. It defaults to `org-element-dual-keywords'.
2834 Return a list whose car is the position at the first of them and
2835 cdr a plist of keywords and values."
2836 (save-excursion
2837 (let ((case-fold-search t)
2838 (key-re (or key-re org-element--affiliated-re))
2839 (trans-list (or trans-list org-element-keyword-translation-alist))
2840 (consed (or consed org-element-multiple-keywords))
2841 (parsed (or parsed org-element-parsed-keywords))
2842 (duals (or duals org-element-dual-keywords))
2843 output)
2844 (unless (bobp)
2845 (while (and (not (bobp))
2846 (progn (forward-line -1) (looking-at key-re)))
2847 (let* ((raw-kwd (downcase (or (match-string 2) (match-string 1))))
2848 ;; Apply translation to RAW-KWD. From there, KWD is
2849 ;; the official keyword.
2850 (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
2851 ;; If KWD is a dual keyword, find it secondary value.
2852 (dual-value (and (member kwd duals)
2853 (org-match-string-no-properties 3)))
2854 ;; Find main value for any keyword.
2855 (value (org-trim (buffer-substring-no-properties
2856 (match-end 0) (point-at-eol))))
2857 ;; Attribute a property name to KWD.
2858 (kwd-sym (and kwd (intern (concat ":" kwd)))))
2859 ;; Now set final shape for VALUE.
2860 (when (member kwd parsed)
2861 (setq value
2862 (org-element-parse-secondary-string
2863 value
2864 (cdr (assq 'keyword org-element-string-restrictions)))))
2865 (when (member kwd duals) (setq value (cons value dual-value)))
2866 (when (member kwd consed)
2867 (setq value (cons value (plist-get output kwd-sym))))
2868 ;; Eventually store the new value in OUTPUT.
2869 (setq output (plist-put output kwd-sym value))))
2870 (unless (looking-at key-re) (forward-line 1)))
2871 (list (point) output))))
2875 ;;; The Org Parser
2877 ;; The two major functions here are `org-element-parse-buffer', which
2878 ;; parses Org syntax inside the current buffer, taking into account
2879 ;; region, narrowing, or even visibility if specified, and
2880 ;; `org-element-parse-secondary-string', which parses objects within
2881 ;; a given string.
2883 ;; The (almost) almighty `org-element-map' allows to apply a function
2884 ;; on elements or objects matching some type, and accumulate the
2885 ;; resulting values. In an export situation, it also skips unneeded
2886 ;; parts of the parse tree, transparently walks into included files,
2887 ;; and maintain a list of local properties (i.e. those inherited from
2888 ;; parent headlines) for function's consumption.
2890 (defun org-element-parse-buffer (&optional granularity visible-only)
2891 "Recursively parse the buffer and return structure.
2892 If narrowing is in effect, only parse the visible part of the
2893 buffer.
2895 Optional argument GRANULARITY determines the depth of the
2896 recursion. It can be set to the following symbols:
2898 `headline' Only parse headlines.
2899 `greater-element' Don't recurse into greater elements. Thus,
2900 elements parsed are the top-level ones.
2901 `element' Parse everything but objects and plain text.
2902 `object' Parse the complete buffer (default).
2904 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
2905 elements.
2907 Assume buffer is in Org mode."
2908 (save-excursion
2909 (goto-char (point-min))
2910 (org-skip-whitespace)
2911 (nconc (list 'org-data nil)
2912 (org-element-parse-elements
2913 (point-at-bol) (point-max)
2914 ;; Start is section mode so text before the first headline
2915 ;; belongs to a section.
2916 'section nil granularity visible-only nil))))
2918 (defun org-element-parse-secondary-string (string restriction &optional buffer)
2919 "Recursively parse objects in STRING and return structure.
2921 RESTRICTION, when non-nil, is a symbol limiting the object types
2922 that will be looked after.
2924 Optional argument BUFFER indicates the buffer from where the
2925 secondary string was extracted. It is used to determine where to
2926 get extraneous information for an object \(i.e. when resolving
2927 a link or looking for a footnote definition\). It defaults to
2928 the current buffer."
2929 (with-temp-buffer
2930 (insert string)
2931 (org-element-parse-objects (point-min) (point-max) nil restriction)))
2933 (defun org-element-map (data types fun &optional info first-match)
2934 "Map a function on selected elements or objects.
2936 DATA is the parsed tree, as returned by, i.e,
2937 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
2938 of elements or objects types. FUN is the function called on the
2939 matching element or object. It must accept two arguments: the
2940 element or object itself and a plist holding contextual
2941 information.
2943 When optional argument INFO is non-nil, it should be a plist
2944 holding export options. In that case, parts of the parse tree
2945 not exportable according to that property list will be skipped
2946 and files included through a keyword will be visited.
2948 When optional argument FIRST-MATCH is non-nil, stop at the first
2949 match for which FUN doesn't return nil, and return that value.
2951 Nil values returned from FUN are ignored in the result."
2952 ;; Ensure TYPES is a list, even of one element.
2953 (unless (listp types) (setq types (list types)))
2954 ;; Recursion depth is determined by --CATEGORY.
2955 (let* ((--category
2956 (cond
2957 ((loop for type in types
2958 always (memq type org-element-greater-elements))
2959 'greater-elements)
2960 ((loop for type in types
2961 always (memq type org-element-all-elements))
2962 'elements)
2963 (t 'objects)))
2964 walk-tree ; For byte-compiler
2965 --acc
2966 (accumulate-maybe
2967 (function
2968 (lambda (--type types fun --blob --local)
2969 ;; Check if TYPE is matching among TYPES. If so, apply
2970 ;; FUN to --BLOB and accumulate return value
2971 ;; into --ACC. --LOCAL is the communication channel.
2972 (when (memq --type types)
2973 (let ((result (funcall fun --blob --local)))
2974 (cond ((not result))
2975 (first-match (throw 'first-match result))
2976 (t (push result --acc))))))))
2977 (walk-tree
2978 (function
2979 (lambda (--data --local)
2980 ;; Recursively walk DATA. --LOCAL, if non-nil, is
2981 ;; a plist holding contextual information.
2982 (mapc
2983 (lambda (--blob)
2984 (let ((--type (if (stringp --blob) 'plain-text (car --blob))))
2985 ;; Determine if a recursion into --BLOB is
2986 ;; possible and allowed.
2987 (cond
2988 ;; Element or object not exportable.
2989 ((and info (org-export-skip-p --blob info)))
2990 ;; Archived headline: Maybe apply fun on it, but
2991 ;; skip contents.
2992 ((and info
2993 (eq --type 'headline)
2994 (eq (plist-get info :with-archived-trees) 'headline)
2995 (org-element-get-property :archivedp --blob))
2996 (funcall accumulate-maybe --type types fun --blob --local))
2997 ;; At an include keyword: apply mapping to its
2998 ;; contents.
2999 ((and --local
3000 (eq --type 'keyword)
3001 (string=
3002 (downcase (org-element-get-property :key --blob))
3003 "include"))
3004 (funcall accumulate-maybe --type types fun --blob --local)
3005 (let* ((--data
3006 (org-export-parse-included-file --blob --local))
3007 (--value (org-element-get-property :value --blob))
3008 (--file
3009 (and (string-match "^\"\\(\\S-+\\)\"" --value)
3010 (match-string 1 --value))))
3011 (funcall
3012 walk-tree --data
3013 (org-combine-plists
3014 --local
3015 ;; Store full path of already included files
3016 ;; to avoid recursive file inclusion.
3017 `(:included-files
3018 ,(cons (expand-file-name --file)
3019 (plist-get --local :included-files))
3020 ;; Ensure that a top-level headline in the
3021 ;; included file becomes a direct child of
3022 ;; the current headline in the buffer.
3023 :headline-offset
3024 ,(- (+ (plist-get
3025 (plist-get --local :inherited-properties)
3026 :level)
3027 (or (plist-get --local :headline-offset) 0))
3028 (1- (org-export-get-min-level
3029 --data --local))))))))
3030 ;; Limiting recursion to greater elements, and --BLOB
3031 ;; isn't one.
3032 ((and (eq --category 'greater-elements)
3033 (not (memq --type org-element-greater-elements)))
3034 (funcall accumulate-maybe --type types fun --blob --local))
3035 ;; Limiting recursion to elements, and --BLOB only
3036 ;; contains objects.
3037 ((and (eq --category 'elements) (eq --type 'paragraph)))
3038 ;; No limitation on recursion, but --BLOB hasn't
3039 ;; got a recursive type.
3040 ((and (eq --category 'objects)
3041 (not (or (eq --type 'paragraph)
3042 (memq --type org-element-greater-elements)
3043 (memq --type org-element-recursive-objects))))
3044 (funcall accumulate-maybe --type types fun --blob --local))
3045 ;; Recursion is possible and allowed: Update local
3046 ;; information and move into --BLOB.
3047 (t (funcall accumulate-maybe --type types fun --blob --local)
3048 (funcall
3049 walk-tree --blob
3050 (org-combine-plists
3051 info `(:genealogy
3052 ,(cons --blob (plist-get info :genealogy)))))))))
3053 (org-element-get-contents --data))))))
3054 (catch 'first-match
3055 (funcall walk-tree data info)
3056 ;; Return value in a proper order.
3057 (reverse --acc))))
3059 ;; The following functions are internal parts of the parser. The
3060 ;; first one, `org-element-parse-elements' acts at the element's
3061 ;; level. The second one, `org-element-parse-objects' applies on all
3062 ;; objects of a paragraph or a secondary string. It uses
3063 ;; `org-element-get-candidates' to optimize the search of the next
3064 ;; object in the buffer.
3066 ;; More precisely, that function looks for every allowed object type
3067 ;; first. Then, it discards failed searches, keeps further matches,
3068 ;; and searches again types matched behind point, for subsequent
3069 ;; calls. Thus, searching for a given type fails only once, and every
3070 ;; object is searched only once at top level (but sometimes more for
3071 ;; nested types).
3073 (defun org-element-parse-elements
3074 (beg end special structure granularity visible-only acc)
3075 "Parse ELEMENT with point at its beginning.
3077 SPECIAL prioritize some elements over the others. It can set to
3078 either `section' or `item', which will focus search,
3079 respectively, on sections and items. Moreover, when value is
3080 `item', STRUCTURE will be used as the current list structure.
3082 GRANULARITY determines the depth of the recursion. It can be set
3083 to the following symbols:
3085 `headline' Only parse headlines.
3086 `greater-element' Don't recurse into greater elements. Thus,
3087 elements parsed are the top-level ones.
3088 `element' Parse everything but objects and plain text.
3089 `object' or nil Parse the complete buffer.
3091 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3092 elements.
3094 Elements are accumulated into ACC."
3095 (save-excursion
3096 (goto-char beg)
3097 ;; Shortcut when parsing only headlines.
3098 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
3099 (org-with-limited-levels (outline-next-heading)))
3100 ;; Main loop start.
3101 (while (and (< (point) end) (not (eobp)))
3102 (push
3103 ;; 1. Item mode is active: point is at an item. Knowing that,
3104 ;; there's no need to go through `org-element-at-point'.
3105 (if (eq special 'item)
3106 (let* ((element (org-element-item-parser structure))
3107 (cbeg (org-element-get-property :contents-begin element))
3108 (cend (org-element-get-property :contents-end element)))
3109 (goto-char (org-element-get-property :end element))
3110 ;; Narrow region to contents, so that item bullet don't
3111 ;; interfere with paragraph parsing.
3112 (save-restriction
3113 (narrow-to-region cbeg cend)
3114 (org-element-parse-elements
3115 cbeg cend nil structure granularity visible-only
3116 (reverse element))))
3117 ;; 2. When ITEM is nil, find current element's type and parse
3118 ;; it accordingly to its category.
3119 (let ((element (org-element-at-point special structure)))
3120 (goto-char (org-element-get-property :end element))
3121 (cond
3122 ;; Case 1. ELEMENT is a footnote-definition. If
3123 ;; GRANURALITY allows parsing, use narrowing so that
3124 ;; footnote label don't interfere with paragraph
3125 ;; recognition.
3126 ((and (eq (car element) 'footnote-definition)
3127 (not (memq granularity '(headline greater-element))))
3128 (let ((cbeg (org-element-get-property :contents-begin element))
3129 (cend (org-element-get-property :contents-end element)))
3130 (save-restriction
3131 (narrow-to-region cbeg cend)
3132 (org-element-parse-elements
3133 cbeg cend nil structure granularity visible-only
3134 (reverse element)))))
3135 ;; Case 2. ELEMENT is a paragraph. Parse objects inside,
3136 ;; if GRANULARITY allows it.
3137 ((and (eq (car element) 'paragraph)
3138 (or (not granularity) (eq granularity 'object)))
3139 (org-element-parse-objects
3140 (org-element-get-property :contents-begin element)
3141 (org-element-get-property :contents-end element)
3142 (reverse element)
3143 nil))
3144 ;; Case 3. ELEMENT is recursive: parse it between
3145 ;; `contents-begin' and `contents-end'. Make sure
3146 ;; GRANULARITY allows the recursion, or ELEMENT is an
3147 ;; headline, in which case going inside is mandatory, in
3148 ;; order to get sub-level headings. If VISIBLE-ONLY is
3149 ;; true and element is hidden, do not recurse into it.
3150 ((and (memq (car element) org-element-greater-elements)
3151 (or (not granularity)
3152 (memq granularity '(element object))
3153 (eq (car element) 'headline))
3154 (not (and visible-only
3155 (org-element-get-property :hiddenp element))))
3156 (org-element-parse-elements
3157 (org-element-get-property :contents-begin element)
3158 (org-element-get-property :contents-end element)
3159 ;; At a plain list, switch to item mode. At an
3160 ;; headline, switch to section mode. Any other element
3161 ;; turns off special modes.
3162 (case (car element) (plain-list 'item) (headline 'section))
3163 (org-element-get-property :structure element)
3164 granularity
3165 visible-only
3166 (reverse element)))
3167 ;; Case 4. Else, just accumulate ELEMENT.
3168 (t element))))
3169 acc)
3170 (org-skip-whitespace))
3171 ;; Return result.
3172 (nreverse acc)))
3174 (defun org-element-parse-objects (beg end acc restriction)
3175 "Parse objects between BEG and END and return recursive structure.
3177 Objects are accumulated in ACC.
3179 RESTRICTION, when non-nil, is a list of object types which are
3180 allowed in the current object."
3181 (let ((get-next-object
3182 (function
3183 (lambda (cand)
3184 ;; Return the parsing function associated to the nearest
3185 ;; object among list of candidates CAND.
3186 (let ((pos (apply #'min (mapcar #'cdr cand))))
3187 (save-excursion
3188 (goto-char pos)
3189 (funcall
3190 (intern
3191 (format "org-element-%s-parser" (car (rassq pos cand))))))))))
3192 next-object candidates)
3193 (save-excursion
3194 (goto-char beg)
3195 (while (setq candidates (org-element-get-next-object-candidates
3196 end restriction candidates))
3197 (setq next-object (funcall get-next-object candidates))
3198 ;; 1. Text before any object. Untabify it.
3199 (let ((obj-beg (org-element-get-property :begin next-object)))
3200 (unless (= (point) obj-beg)
3201 (push (replace-regexp-in-string
3202 "\t" (make-string tab-width ? )
3203 (buffer-substring-no-properties (point) obj-beg))
3204 acc)))
3205 ;; 2. Object...
3206 (let ((obj-end (org-element-get-property :end next-object))
3207 (cont-beg (org-element-get-property :contents-begin next-object)))
3208 (push (if (and (memq (car next-object) org-element-recursive-objects)
3209 cont-beg)
3210 ;; ... recursive. The CONT-BEG check is for
3211 ;; links, as some of them might not be recursive
3212 ;; (i.e. plain links).
3213 (save-restriction
3214 (narrow-to-region
3215 cont-beg
3216 (org-element-get-property :contents-end next-object))
3217 (org-element-parse-objects
3218 (point-min) (point-max) (reverse next-object)
3219 ;; Restrict allowed objects. This is the
3220 ;; intersection of current restriction and next
3221 ;; object's restriction.
3222 (let ((new-restr
3223 (cdr (assq (car next-object)
3224 org-element-object-restrictions))))
3225 (if (not restriction) new-restr
3226 (delq nil (mapcar
3227 (lambda (e) (and (memq e restriction) e))
3228 new-restr))))))
3229 ;; ... not recursive.
3230 next-object)
3231 acc)
3232 (goto-char obj-end)))
3233 ;; 3. Text after last object. Untabify it.
3234 (unless (= (point) end)
3235 (push (replace-regexp-in-string
3236 "\t" (make-string tab-width ? )
3237 (buffer-substring-no-properties (point) end))
3238 acc))
3239 ;; Result.
3240 (nreverse acc))))
3242 (defun org-element-get-next-object-candidates (limit restriction objects)
3243 "Return an alist of candidates for the next object.
3245 LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
3246 the possible object types.
3248 Return value is an alist whose car is position and cdr the object
3249 type, as a string. There is an association for the closest
3250 object of each type within RESTRICTION when non-nil, or for every
3251 type otherwise.
3253 OBJECTS is the previous candidates alist."
3254 (let ((restriction (or restriction org-element-all-successors))
3255 next-candidates types-to-search)
3256 ;; If no previous result, search every object type in RESTRICTION.
3257 ;; Otherwise, keep potential candidates (old objects located after
3258 ;; point) and ask to search again those which had matched before.
3259 (if (not objects) (setq types-to-search restriction)
3260 (mapc (lambda (obj)
3261 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
3262 (push obj next-candidates)))
3263 objects))
3264 ;; Call the appropriate "get-next" function for each type to
3265 ;; search and accumulate matches.
3266 (mapc
3267 (lambda (type)
3268 (let* ((successor-fun
3269 (intern
3270 (format "org-element-%s-successor"
3271 (or (cdr (assq type org-element-object-successor-alist))
3272 type))))
3273 (obj (funcall successor-fun limit)))
3274 (and obj (push obj next-candidates))))
3275 types-to-search)
3276 ;; Return alist.
3277 next-candidates))
3281 ;;; Towards A Bijective Process
3283 ;; The parse tree obtained with `org-element-parse-buffer' is really
3284 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3285 ;; interpreted and expanded into a string with canonical Org
3286 ;; syntax. Hence `org-element-interpret-data'.
3288 ;; Data parsed from secondary strings, whose shape is slightly
3289 ;; different than the standard parse tree, is expanded with the
3290 ;; equivalent function `org-element-interpret-secondary'.
3292 ;; Both functions rely internally on
3293 ;; `org-element-interpret--affiliated-keywords'.
3295 (defun org-element-interpret-data (data &optional genealogy previous)
3296 "Interpret a parse tree representing Org data.
3298 DATA is the parse tree to interpret.
3300 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3301 calls:
3302 GENEALOGY is the list of its parents types.
3303 PREVIOUS is the type of the element or object at the same level
3304 interpreted before.
3306 Return Org syntax as a string."
3307 (mapconcat
3308 (lambda (blob)
3309 ;; BLOB can be an element, an object, a string, or nil.
3310 (cond
3311 ((not blob) nil)
3312 ((equal blob "") nil)
3313 ((stringp blob) blob)
3315 (let* ((type (car blob))
3316 (interpreter
3317 (if (eq type 'org-data) 'identity
3318 (intern (format "org-element-%s-interpreter" type))))
3319 (contents
3320 (cond
3321 ;; Full Org document.
3322 ((eq type 'org-data)
3323 (org-element-interpret-data blob genealogy previous))
3324 ;; Recursive objects.
3325 ((memq type org-element-recursive-objects)
3326 (org-element-interpret-data
3327 blob (cons type genealogy) nil))
3328 ;; Recursive elements.
3329 ((memq type org-element-greater-elements)
3330 (org-element-normalize-string
3331 (org-element-interpret-data
3332 blob (cons type genealogy) nil)))
3333 ;; Paragraphs.
3334 ((eq type 'paragraph)
3335 (let ((paragraph
3336 (org-element-normalize-contents
3337 blob
3338 ;; When normalizing contents of an item,
3339 ;; ignore first line's indentation.
3340 (and (not previous)
3341 (memq (car genealogy)
3342 '(footnote-definiton item))))))
3343 (org-element-interpret-data
3344 paragraph (cons type genealogy) nil)))))
3345 (results (funcall interpreter blob contents)))
3346 ;; Update PREVIOUS.
3347 (setq previous type)
3348 ;; Build white spaces.
3349 (cond
3350 ((eq type 'org-data) results)
3351 ((memq type org-element-all-elements)
3352 (concat
3353 (org-element-interpret--affiliated-keywords blob)
3354 (org-element-normalize-string results)
3355 (make-string (org-element-get-property :post-blank blob) 10)))
3356 (t (concat
3357 results
3358 (make-string
3359 (org-element-get-property :post-blank blob) 32))))))))
3360 (org-element-get-contents data) ""))
3362 (defun org-element-interpret-secondary (secondary)
3363 "Interpret SECONDARY string as Org syntax.
3365 SECONDARY-STRING is a nested list as returned by
3366 `org-element-parse-secondary-string'.
3368 Return interpreted string."
3369 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3370 (let ((s (if (listp secondary) secondary (list secondary))))
3371 (org-element-interpret-data `(org-data nil ,@s) nil nil)))
3373 ;; Both functions internally use `org-element--affiliated-keywords'.
3375 (defun org-element-interpret--affiliated-keywords (element)
3376 "Return ELEMENT's affiliated keywords as Org syntax.
3377 If there is no affiliated keyword, return the empty string."
3378 (let ((keyword-to-org
3379 (function
3380 (lambda (key value)
3381 (let (dual)
3382 (when (member key org-element-dual-keywords)
3383 (setq dual (cdr value) value (car value)))
3384 (concat "#+" key (and dual (format "[%s]" dual)) ": "
3385 (if (member key org-element-parsed-keywords)
3386 (org-element-interpret-secondary value)
3387 value)
3388 "\n"))))))
3389 (mapconcat
3390 (lambda (key)
3391 (let ((value (org-element-get-property (intern (concat ":" key)) element)))
3392 (when value
3393 (if (member key org-element-multiple-keywords)
3394 (mapconcat (lambda (line)
3395 (funcall keyword-to-org key line))
3396 value "")
3397 (funcall keyword-to-org key value)))))
3398 ;; Remove translated keywords.
3399 (delq nil
3400 (mapcar
3401 (lambda (key)
3402 (and (not (assoc key org-element-keyword-translation-alist)) key))
3403 org-element-affiliated-keywords))
3404 "")))
3406 ;; Because interpretation of the parse tree must return the same
3407 ;; number of blank lines between elements and the same number of white
3408 ;; space after objects, some special care must be given to white
3409 ;; spaces.
3411 ;; The first function, `org-element-normalize-string', ensures any
3412 ;; string different from the empty string will end with a single
3413 ;; newline character.
3415 ;; The second function, `org-element-normalize-contents', removes
3416 ;; global indentation from the contents of the current element.
3418 (defun org-element-normalize-string (s)
3419 "Ensure string S ends with a single newline character.
3421 If S isn't a string return it unchanged. If S is the empty
3422 string, return it. Otherwise, return a new string with a single
3423 newline character at its end."
3424 (cond
3425 ((not (stringp s)) s)
3426 ((string= "" s) "")
3427 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
3428 (replace-match "\n" nil nil s)))))
3430 (defun org-element-normalize-contents (element &optional ignore-first)
3431 "Normalize plain text in ELEMENT's contents.
3433 ELEMENT must only contain plain text and objects.
3435 The following changes are applied to plain text:
3436 - Remove global indentation, preserving relative one.
3437 - Untabify it.
3439 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3440 indentation to compute maximal common indentation.
3442 Return the normalized element."
3443 (nconc
3444 (list (car element) (nth 1 element))
3445 (let ((contents (org-element-get-contents element)))
3446 (if (not (or ignore-first (stringp (car contents)))) contents
3447 (catch 'exit
3448 ;; 1. Get maximal common indentation (MCI) among each string
3449 ;; in CONTENTS.
3450 (let* ((ind-list (unless ignore-first
3451 (list (org-get-string-indentation (car contents)))))
3452 (contents
3453 (mapcar
3454 (lambda (object)
3455 (if (not (stringp object)) object
3456 (let ((start 0))
3457 (while (string-match "\n\\( *\\)" object start)
3458 (setq start (match-end 0))
3459 (push (length (match-string 1 object)) ind-list))
3460 object)))
3461 contents))
3462 (mci (if ind-list (apply 'min ind-list)
3463 (throw 'exit contents))))
3464 ;; 2. Remove that indentation from CONTENTS. First string
3465 ;; must be treated differently because it's the only one
3466 ;; whose indentation doesn't happen after a newline
3467 ;; character.
3468 (let ((first-obj (car contents)))
3469 (unless (or (not (stringp first-obj)) ignore-first)
3470 (setq contents
3471 (cons (replace-regexp-in-string
3472 (format "\\` \\{%d\\}" mci) "" first-obj)
3473 (cdr contents)))))
3474 (mapcar (lambda (object)
3475 (if (not (stringp object)) object
3476 (replace-regexp-in-string
3477 (format "\n \\{%d\\}" mci) "\n" object)))
3478 contents)))))))
3482 ;;; The Toolbox
3484 ;; Once the structure of an Org file is well understood, it's easy to
3485 ;; implement some replacements for `forward-paragraph'
3486 ;; `backward-paragraph', namely `org-element-forward' and
3487 ;; `org-element-backward'.
3489 ;; Also, `org-transpose-elements' mimics the behaviour of
3490 ;; `transpose-words', at the element's level, whereas
3491 ;; `org-element-drag-forward', `org-element-drag-backward', and
3492 ;; `org-element-up' generalize, respectively, functions
3493 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3495 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3496 ;; smartly remove global indentation from buffer, making it possible
3497 ;; to use Org indent mode on a file created with hard indentation.
3499 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3500 ;; internally by some of the previously cited tools.
3502 (defsubst org-element-nested-p (elem-A elem-B)
3503 "Non-nil when elements ELEM-A and ELEM-B are nested."
3504 (let ((beg-A (org-element-get-property :begin elem-A))
3505 (beg-B (org-element-get-property :begin elem-B))
3506 (end-A (org-element-get-property :end elem-A))
3507 (end-B (org-element-get-property :end elem-B)))
3508 (or (and (>= beg-A beg-B) (<= end-A end-B))
3509 (and (>= beg-B beg-A) (<= end-B end-A)))))
3511 (defun org-element-swap-A-B (elem-A elem-B)
3512 "Swap elements ELEM-A and ELEM-B.
3514 Leave point at the end of ELEM-A.
3516 Assume ELEM-A is before ELEM-B and that they are not nested."
3517 (goto-char (org-element-get-property :begin elem-A))
3518 (let* ((beg-B (org-element-get-property :begin elem-B))
3519 (end-B-no-blank (save-excursion
3520 (goto-char (org-element-get-property :end elem-B))
3521 (skip-chars-backward " \r\t\n")
3522 (forward-line)
3523 (point)))
3524 (beg-A (org-element-get-property :begin elem-A))
3525 (end-A-no-blank (save-excursion
3526 (goto-char (org-element-get-property :end elem-A))
3527 (skip-chars-backward " \r\t\n")
3528 (forward-line)
3529 (point)))
3530 (body-A (buffer-substring beg-A end-A-no-blank))
3531 (body-B (buffer-substring beg-B end-B-no-blank))
3532 (between-A-B (buffer-substring end-A-no-blank beg-B)))
3533 (delete-region beg-A end-B-no-blank)
3534 (insert body-B between-A-B body-A)
3535 (goto-char (org-element-get-property :end elem-B))))
3537 (defun org-element-backward ()
3538 "Move backward by one element."
3539 (interactive)
3540 (let* ((opoint (point))
3541 (element (org-element-at-point))
3542 (start-el-beg (org-element-get-property :begin element)))
3543 ;; At an headline. The previous element is the previous sibling,
3544 ;; or the parent if any.
3545 (cond
3546 ;; Already at the beginning of the current element: move to the
3547 ;; beginning of the previous one.
3548 ((= opoint start-el-beg)
3549 (forward-line -1)
3550 (skip-chars-backward " \r\t\n")
3551 (let* ((prev-element (org-element-at-point))
3552 (itemp (org-in-item-p))
3553 (struct (and itemp
3554 (save-excursion (goto-char itemp)
3555 (org-list-struct)))))
3556 ;; When moving into a new list, go directly at the
3557 ;; beginning of the top list structure.
3558 (if (and itemp (<= (org-list-get-bottom-point struct) opoint))
3559 (progn
3560 (goto-char (org-list-get-top-point struct))
3561 (goto-char (org-element-get-property
3562 :begin (org-element-at-point))))
3563 (goto-char (org-element-get-property :begin prev-element))))
3564 (while (org-truely-invisible-p) (org-element-up)))
3565 ;; Else, move at the element beginning. One exception: if point
3566 ;; was in the blank lines after the end of a list, move directly
3567 ;; to the top item.
3569 (let (struct itemp)
3570 (if (and (setq itemp (org-in-item-p))
3571 (<= (org-list-get-bottom-point
3572 (save-excursion (goto-char itemp)
3573 (setq struct (org-list-struct))))
3574 opoint))
3575 (progn (goto-char (org-list-get-top-point struct))
3576 (goto-char (org-element-get-property
3577 :begin (org-element-at-point))))
3578 (goto-char start-el-beg)))))))
3580 (defun org-element-drag-backward ()
3581 "Drag backward element at point."
3582 (interactive)
3583 (let* ((pos (point))
3584 (elem (org-element-at-point)))
3585 (when (= (progn (goto-char (point-min))
3586 (org-skip-whitespace)
3587 (point-at-bol))
3588 (org-element-get-property :end elem))
3589 (error "Cannot drag element backward"))
3590 (goto-char (org-element-get-property :begin elem))
3591 (org-element-backward)
3592 (let ((prev-elem (org-element-at-point)))
3593 (when (or (org-element-nested-p elem prev-elem)
3594 (and (eq (car elem) 'headline)
3595 (not (eq (car prev-elem) 'headline))))
3596 (goto-char pos)
3597 (error "Cannot drag element backward"))
3598 ;; Compute new position of point: it's shifted by PREV-ELEM
3599 ;; body's length.
3600 (let ((size-prev (- (org-element-get-property :end prev-elem)
3601 (org-element-get-property :begin prev-elem))))
3602 (org-element-swap-A-B prev-elem elem)
3603 (goto-char (- pos size-prev))))))
3605 (defun org-element-drag-forward ()
3606 "Move forward element at point."
3607 (interactive)
3608 (let* ((pos (point))
3609 (elem (org-element-at-point)))
3610 (when (= (point-max) (org-element-get-property :end elem))
3611 (error "Cannot drag element forward"))
3612 (goto-char (org-element-get-property :end elem))
3613 (let ((next-elem (org-element-at-point)))
3614 (when (or (org-element-nested-p elem next-elem)
3615 (and (eq (car next-elem) 'headline)
3616 (not (eq (car elem) 'headline))))
3617 (goto-char pos)
3618 (error "Cannot drag element forward"))
3619 ;; Compute new position of point: it's shifted by NEXT-ELEM
3620 ;; body's length (without final blanks) and by the length of
3621 ;; blanks between ELEM and NEXT-ELEM.
3622 (let ((size-next (- (save-excursion
3623 (goto-char (org-element-get-property :end next-elem))
3624 (skip-chars-backward " \r\t\n")
3625 (forward-line)
3626 (point))
3627 (org-element-get-property :begin next-elem)))
3628 (size-blank (- (org-element-get-property :end elem)
3629 (save-excursion
3630 (goto-char (org-element-get-property :end elem))
3631 (skip-chars-backward " \r\t\n")
3632 (forward-line)
3633 (point)))))
3634 (org-element-swap-A-B elem next-elem)
3635 (goto-char (+ pos size-next size-blank))))))
3637 (defun org-element-forward ()
3638 "Move forward by one element."
3639 (interactive)
3640 (beginning-of-line)
3641 (cond ((eobp) (error "Cannot move further down"))
3642 ((looking-at "[ \t]*$")
3643 (org-skip-whitespace)
3644 (goto-char (if (eobp) (point) (point-at-bol))))
3646 (let ((element (org-element-at-point t))
3647 (origin (point)))
3648 (cond
3649 ;; At an item: Either move to the next element inside, or
3650 ;; to its end if it's hidden.
3651 ((eq (car element) 'item)
3652 (if (org-element-get-property :hiddenp element)
3653 (goto-char (org-element-get-property :end element))
3654 (end-of-line)
3655 (re-search-forward org-element-paragraph-separate nil t)
3656 (org-skip-whitespace)
3657 (beginning-of-line)))
3658 ;; At a recursive element: Either move inside, or if it's
3659 ;; hidden, move to its end.
3660 ((memq (car element) org-element-greater-elements)
3661 (let ((cbeg (org-element-get-property :contents-begin element)))
3662 (goto-char
3663 (if (or (org-element-get-property :hiddenp element)
3664 (> origin cbeg))
3665 (org-element-get-property :end element)
3666 cbeg))))
3667 ;; Else: move to the current element's end.
3668 (t (goto-char (org-element-get-property :end element))))))))
3670 (defun org-element-mark-element ()
3671 "Put point at beginning of this element, mark at end.
3673 Interactively, if this command is repeated or (in Transient Mark
3674 mode) if the mark is active, it marks the next element after the
3675 ones already marked."
3676 (interactive)
3677 (let (deactivate-mark)
3678 (if (or (and (eq last-command this-command) (mark t))
3679 (and transient-mark-mode mark-active))
3680 (set-mark
3681 (save-excursion
3682 (goto-char (mark))
3683 (goto-char (org-element-get-property :end (org-element-at-point)))))
3684 (let ((element (org-element-at-point)))
3685 (end-of-line)
3686 (push-mark (org-element-get-property :end element) t t)
3687 (goto-char (org-element-get-property :begin element))))))
3689 (defun org-narrow-to-element ()
3690 "Narrow buffer to current element."
3691 (interactive)
3692 (let ((elem (org-element-at-point)))
3693 (cond
3694 ((eq (car elem) 'headline)
3695 (narrow-to-region
3696 (org-element-get-property :begin elem)
3697 (org-element-get-property :end elem)))
3698 ((memq (car elem) org-element-greater-elements)
3699 (narrow-to-region
3700 (org-element-get-property :contents-begin elem)
3701 (org-element-get-property :contents-end elem)))
3703 (narrow-to-region
3704 (org-element-get-property :begin elem)
3705 (org-element-get-property :end elem))))))
3707 (defun org-transpose-elements ()
3708 "Transpose current and previous elements, keeping blank lines between.
3709 Point is moved after both elements."
3710 (interactive)
3711 (org-skip-whitespace)
3712 (let ((pos (point))
3713 (cur (org-element-at-point)))
3714 (when (= (save-excursion (goto-char (point-min))
3715 (org-skip-whitespace)
3716 (point-at-bol))
3717 (org-element-get-property :begin cur))
3718 (error "No previous element"))
3719 (goto-char (org-element-get-property :begin cur))
3720 (forward-line -1)
3721 (let ((prev (org-element-at-point)))
3722 (when (org-element-nested-p cur prev)
3723 (goto-char pos)
3724 (error "Cannot transpose nested elements"))
3725 (org-element-swap-A-B prev cur))))
3727 (defun org-element-unindent-buffer ()
3728 "Un-indent the visible part of the buffer.
3729 Relative indentation \(between items, inside blocks, etc.\) isn't
3730 modified."
3731 (interactive)
3732 (unless (eq major-mode 'org-mode)
3733 (error "Cannot un-indent a buffer not in Org mode"))
3734 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
3735 unindent-tree ; For byte-compiler.
3736 (unindent-tree
3737 (function
3738 (lambda (contents)
3739 (mapc (lambda (element)
3740 (if (eq (car element) 'headline)
3741 (funcall unindent-tree
3742 (org-element-get-contents element))
3743 (save-excursion
3744 (save-restriction
3745 (narrow-to-region
3746 (org-element-get-property :begin element)
3747 (org-element-get-property :end element))
3748 (org-do-remove-indentation)))))
3749 (reverse contents))))))
3750 (funcall unindent-tree (org-element-get-contents parse-tree))))
3752 (defun org-element-up ()
3753 "Move to upper element.
3754 Return position at the beginning of the upper element."
3755 (interactive)
3756 (let ((opoint (point)) elem)
3757 (cond
3758 ((bobp) (error "No surrounding element"))
3759 ((org-with-limited-levels (org-at-heading-p))
3760 (or (org-up-heading-safe) (error "No surronding element")))
3761 ((and (org-at-item-p)
3762 (setq elem (org-element-at-point))
3763 (let* ((top-list-p (zerop (org-element-get-property :level elem))))
3764 (unless top-list-p
3765 ;; If parent is bound to be in the same list as the
3766 ;; original point, move to that parent.
3767 (let ((struct (org-element-get-property :structure elem)))
3768 (goto-char
3769 (org-list-get-parent
3770 (point-at-bol) struct (org-list-parents-alist struct))))))))
3772 (let* ((elem (or elem (org-element-at-point)))
3773 (end (save-excursion
3774 (goto-char (org-element-get-property :end elem))
3775 (skip-chars-backward " \r\t\n")
3776 (forward-line)
3777 (point)))
3778 prev-elem)
3779 (goto-char (org-element-get-property :begin elem))
3780 (forward-line -1)
3781 (while (and (< (org-element-get-property
3782 :end (setq prev-elem (org-element-at-point)))
3783 end)
3784 (not (bobp)))
3785 (goto-char (org-element-get-property :begin prev-elem))
3786 (forward-line -1))
3787 (if (and (bobp) (< (org-element-get-property :end prev-elem) end))
3788 (progn (goto-char opoint)
3789 (error "No surrounding element"))
3790 (goto-char (org-element-get-property :begin prev-elem))))))))
3793 (provide 'org-element)
3794 ;;; org-element.el ends here