Fix more copyright years.
[org-mode/org-kjn.git] / contrib / lisp / org-element.el
blob3db8e36e2031fb04a4d0bfffdd1d30a050c39d11
1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2011, 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' and `special-block'.
48 ;; Greater elements (excepted `headline' and `item' types) and
49 ;; elements (excepted `keyword', `babel-call', and `property-drawer'
50 ;; types) can have a fixed set of keywords as attributes. Those are
51 ;; called "affiliated keywords", to distinguish them from others
52 ;; keywords, which are full-fledged elements. In particular, the
53 ;; "name" affiliated keyword allows to label almost any element in an
54 ;; 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
137 (defun org-element-center-block-parser ()
138 "Parse a center block.
140 Return a list whose car is `center-block' and cdr is a plist
141 containing `:begin', `:end', `:hiddenp', `:contents-begin',
142 `:contents-end' and `:post-blank' keywords.
144 Assume point is at beginning or end of the block."
145 (save-excursion
146 (let* ((case-fold-search t)
147 (keywords (progn
148 (end-of-line)
149 (re-search-backward
150 (concat "^[ \t]*#\\+begin_center") nil t)
151 (org-element-collect-affiliated-keywords)))
152 (begin (car keywords))
153 (contents-begin (progn (forward-line) (point)))
154 (hidden (org-truely-invisible-p))
155 (contents-end (progn (re-search-forward
156 (concat "^[ \t]*#\\+end_center") nil t)
157 (point-at-bol)))
158 (pos-before-blank (progn (forward-line) (point)))
159 (end (progn (org-skip-whitespace)
160 (if (eobp) (point) (point-at-bol)))))
161 (list 'center-block
162 `(:begin ,begin
163 :end ,end
164 :hiddenp ,hidden
165 :contents-begin ,contents-begin
166 :contents-end ,contents-end
167 :post-blank ,(count-lines pos-before-blank end)
168 ,@(cadr keywords))))))
170 (defun org-element-center-block-interpreter (center-block contents)
171 "Interpret CENTER-BLOCK element as Org syntax.
172 CONTENTS is the contents of the element."
173 (format "#+begin_center\n%s#+end_center" contents))
175 ;;;; Drawer
176 (defun org-element-drawer-parser ()
177 "Parse a drawer.
179 Return a list whose car is `drawer' and cdr is a plist containing
180 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
181 `:contents-end' and `:post-blank' keywords.
183 Assume point is at beginning of drawer."
184 (save-excursion
185 (let* ((case-fold-search t)
186 (name (progn (looking-at org-drawer-regexp)
187 (org-match-string-no-properties 1)))
188 (keywords (org-element-collect-affiliated-keywords))
189 (begin (car keywords))
190 (contents-begin (progn (forward-line) (point)))
191 (hidden (org-truely-invisible-p))
192 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t)
193 (point-at-bol)))
194 (pos-before-blank (progn (forward-line) (point)))
195 (end (progn (org-skip-whitespace)
196 (if (eobp) (point) (point-at-bol)))))
197 (list 'drawer
198 `(:begin ,begin
199 :end ,end
200 :drawer-name ,name
201 :hiddenp ,hidden
202 :contents-begin ,contents-begin
203 :contents-end ,contents-end
204 :post-blank ,(count-lines pos-before-blank end)
205 ,@(cadr keywords))))))
207 (defun org-element-drawer-interpreter (drawer contents)
208 "Interpret DRAWER element as Org syntax.
209 CONTENTS is the contents of the element."
210 (format ":%s:\n%s:END:"
211 (org-element-get-property :drawer-name drawer)
212 contents))
214 ;;;; Dynamic Block
215 (defun org-element-dynamic-block-parser ()
216 "Parse a dynamic block.
218 Return a list whose car is `dynamic-block' and cdr is a plist
219 containing `:block-name', `:begin', `:end', `:hiddenp',
220 `:contents-begin', `:contents-end', `:arguments' and
221 `:post-blank' keywords.
223 Assume point is at beginning of dynamic block."
224 (save-excursion
225 (let* ((case-fold-search t)
226 (name (progn (looking-at org-dblock-start-re)
227 (org-match-string-no-properties 1)))
228 (arguments (org-match-string-no-properties 3))
229 (keywords (org-element-collect-affiliated-keywords))
230 (begin (car keywords))
231 (contents-begin (progn (forward-line) (point)))
232 (hidden (org-truely-invisible-p))
233 (contents-end (progn (re-search-forward org-dblock-end-re nil t)
234 (point-at-bol)))
235 (pos-before-blank (progn (forward-line) (point)))
236 (end (progn (org-skip-whitespace)
237 (if (eobp) (point) (point-at-bol)))))
238 (list 'dynamic-block
239 `(:begin ,begin
240 :end ,end
241 :block-name ,name
242 :arguments ,arguments
243 :hiddenp ,hidden
244 :contents-begin ,contents-begin
245 :contents-end ,contents-end
246 :post-blank ,(count-lines pos-before-blank end)
247 ,@(cadr keywords))))))
249 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
250 "Interpret DYNAMIC-BLOCK element as Org syntax.
251 CONTENTS is the contents of the element."
252 (format "#+BEGIN: %s%s\n%s#+END:"
253 (org-element-get-property :block-name dynamic-block)
254 (let ((args (org-element-get-property :arguments dynamic-block)))
255 (and arg (concat " " args)))
256 contents))
258 ;;;; Footnote Definition
260 (defun org-element-footnote-definition-parser ()
261 "Parse a footnote definition.
263 Return a list whose car is `footnote-definition' and cdr is
264 a plist containing `:label', `:begin' `:end', `:contents-begin',
265 `:contents-end' and `:post-blank' keywords."
266 (save-excursion
267 (let* ((f-def (org-footnote-at-definition-p))
268 (label (car f-def))
269 (keywords (progn (goto-char (nth 1 f-def))
270 (org-element-collect-affiliated-keywords)))
271 (begin (car keywords))
272 (contents-begin (progn (looking-at (concat "\\[" label "\\]"))
273 (goto-char (match-end 0))
274 (org-skip-whitespace)
275 (point)))
276 (end (goto-char (nth 2 f-def)))
277 (contents-end (progn (skip-chars-backward " \r\t\n")
278 (forward-line)
279 (point))))
280 (list 'footnote-definition
281 `(:label ,label
282 :begin ,begin
283 :end ,end
284 :contents-begin ,contents-begin
285 :contents-end ,contents-end
286 :post-blank ,(count-lines contents-end end)
287 ,@(cadr keywords))))))
289 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
290 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
291 CONTENTS is the contents of the footnote-definition."
292 (concat (format "[%s]" (org-element-get-property :label footnote-definition))
294 contents))
297 ;;;; Headline
298 (defun org-element-headline-parser ()
299 "Parse an headline.
301 Return a list whose car is `headline' and cdr is a plist
302 containing `:raw-value', `:title', `:begin', `:end',
303 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
304 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
305 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
306 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
307 keywords.
309 The plist also contains any property set in the property drawer,
310 with its name in lowercase, the underscores replaced with hyphens
311 and colons at the beginning (i.e. `:custom-id').
313 Assume point is at beginning of the headline."
314 (save-excursion
315 (let* ((components (org-heading-components))
316 (level (nth 1 components))
317 (todo (nth 2 components))
318 (todo-type (and todo
319 (if (member todo org-done-keywords) 'done 'todo)))
320 (tags (nth 5 components))
321 (raw-value (nth 4 components))
322 (quotedp (string-match (format "^%s +" org-quote-string) raw-value))
323 (commentedp (string-match
324 (format "^%s +" org-comment-string) raw-value))
325 (archivedp (and tags
326 (string-match (format ":%s:" org-archive-tag) tags)))
327 (footnote-section-p (and org-footnote-section
328 (string= org-footnote-section raw-value)))
329 (standard-props (let (plist)
330 (mapc
331 (lambda (p)
332 (let ((p-name (downcase (car p))))
333 (while (string-match "_" p-name)
334 (setq p-name
335 (replace-match "-" nil nil p-name)))
336 (setq p-name (intern (concat ":" p-name)))
337 (setq plist
338 (plist-put plist p-name (cdr p)))))
339 (org-entry-properties nil 'standard))
340 plist))
341 (time-props (org-entry-properties nil 'special "CLOCK"))
342 (scheduled (cdr (assoc "SCHEDULED" time-props)))
343 (deadline (cdr (assoc "DEADLINE" time-props)))
344 (clock (cdr (assoc "CLOCK" time-props)))
345 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
346 (begin (point))
347 (pos-after-head (save-excursion (forward-line) (point)))
348 (contents-begin (save-excursion (forward-line)
349 (org-skip-whitespace)
350 (if (eobp) (point) (point-at-bol))))
351 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
352 (end (progn (goto-char (org-end-of-subtree t t))))
353 (contents-end (progn (skip-chars-backward " \r\t\n")
354 (forward-line)
355 (point)))
356 title)
357 ;; Clean RAW-VALUE from any quote or comment string.
358 (when (or quotedp commentedp)
359 (setq raw-value
360 (replace-regexp-in-string
361 (concat "\\(" org-quote-string "\\|" org-comment-string "\\) +")
363 raw-value)))
364 ;; Clean TAGS from archive tag, if any.
365 (when archivedp
366 (setq tags
367 (and (not (string= tags (format ":%s:" org-archive-tag)))
368 (replace-regexp-in-string
369 (concat org-archive-tag ":") "" tags)))
370 (when (string= tags ":") (setq tags nil)))
371 ;; Then get TITLE.
372 (setq title (org-element-parse-secondary-string
373 raw-value
374 (cdr (assq 'headline org-element-string-restrictions))))
375 (list 'headline
376 `(:raw-value ,raw-value
377 :title ,title
378 :begin ,begin
379 :end ,end
380 :pre-blank ,(count-lines pos-after-head contents-begin)
381 :hiddenp ,hidden
382 :contents-begin ,contents-begin
383 :contents-end ,contents-end
384 :level ,level
385 :priority ,(nth 3 components)
386 :tags ,tags
387 :todo-keyword ,todo
388 :todo-type ,todo-type
389 :scheduled ,scheduled
390 :deadline ,deadline
391 :timestamp ,timestamp
392 :clock ,clock
393 :post-blank ,(count-lines contents-end end)
394 :footnote-section-p ,footnote-section-p
395 :archivedp ,archivedp
396 :commentedp ,commentedp
397 :quotedp ,quotedp
398 ,@standard-props)))))
400 (defun org-element-headline-interpreter (headline contents)
401 "Interpret HEADLINE element as Org syntax.
402 CONTENTS is the contents of the element."
403 (let* ((level (org-element-get-property :level headline))
404 (todo (org-element-get-property :todo-keyword headline))
405 (priority (org-element-get-property :priority headline))
406 (title (org-element-get-property :raw-value headline))
407 (tags (let ((tag-string (org-element-get-property :tags headline))
408 (archivedp (org-element-get-property :archivedp headline)))
409 (cond
410 ((and (not tag-string) archivedp)
411 (format ":%s:" org-archive-tag))
412 (archivedp (concat ":" org-archive-tag tag-string))
413 (t tag-string))))
414 (commentedp (org-element-get-property :commentedp headline))
415 (quotedp (org-element-get-property :quotedp headline))
416 (pre-blank (org-element-get-property :pre-blank headline))
417 (heading (concat (make-string level ?*)
418 (and todo (concat " " todo))
419 (and quotedp (concat " " org-quote-string))
420 (and commentedp (concat " " org-comment-string))
421 (and priority (concat " " priority))
422 (cond ((and org-footnote-section
423 (org-element-get-property
424 :footnote-section-p headline))
425 (concat " " org-footnote-section))
426 (title (concat " " title)))))
427 ;; Align tags.
428 (tags-fmt (when tags
429 (let ((tags-len (length tags)))
430 (format "%% %ds"
431 (cond
432 ((zerop org-tags-column) (1+ tags-len))
433 ((< org-tags-column 0)
434 (max (- (+ org-tags-column (length heading)))
435 (1+ tags-len)))
436 (t (max (+ (- org-tags-column (length heading))
437 tags-len)
438 (1+ tags-len)))))))))
439 (concat heading (and tags (format tags-fmt tags))
440 (make-string (1+ pre-blank) 10)
441 contents)))
443 ;;;; Inlinetask
444 (defun org-element-inlinetask-parser ()
445 "Parse an inline task.
447 Return a list whose car is `inlinetask' and cdr is a plist
448 containing `:raw-value', `:title', `:begin', `:end', `:hiddenp',
449 `:contents-begin' and `:contents-end', `:level', `:priority',
450 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
451 `:scheduled', `:deadline', `:timestamp', `:clock' and
452 `:post-blank' keywords.
454 The plist also contains any property set in the property drawer,
455 with its name in lowercase, the underscores replaced with hyphens
456 and colons at the beginning (i.e. `:custom-id').
458 Assume point is at beginning of the inline task."
459 (save-excursion
460 (let* ((keywords (org-element-collect-affiliated-keywords))
461 (begin (car keywords))
462 (components (org-heading-components))
463 (todo (nth 2 components))
464 (todo-type (and todo
465 (if (member todo org-done-keywords) 'done 'todo)))
466 (raw-value (nth 4 components))
467 (standard-props (let (plist)
468 (mapc
469 (lambda (p)
470 (let ((p-name (downcase (car p))))
471 (while (string-match "_" p-name)
472 (setq p-name
473 (replace-match "-" nil nil p-name)))
474 (setq p-name (intern (concat ":" p-name)))
475 (setq plist
476 (plist-put plist p-name (cdr p)))))
477 (org-entry-properties nil 'standard))
478 plist))
479 (time-props (org-entry-properties nil 'special "CLOCK"))
480 (scheduled (cdr (assoc "SCHEDULED" time-props)))
481 (deadline (cdr (assoc "DEADLINE" time-props)))
482 (clock (cdr (assoc "CLOCK" time-props)))
483 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
484 (title (org-element-parse-secondary-string
485 raw-value
486 (cdr (assq 'inlinetask org-element-string-restrictions))))
487 (contents-begin (save-excursion (forward-line) (point)))
488 (hidden (org-truely-invisible-p))
489 (pos-before-blank (org-inlinetask-goto-end))
490 ;; In the case of a single line task, CONTENTS-BEGIN and
491 ;; CONTENTS-END might overlap.
492 (contents-end (max contents-begin
493 (save-excursion (forward-line -1) (point))))
494 (end (progn (org-skip-whitespace)
495 (if (eobp) (point) (point-at-bol)))))
496 (list 'inlinetask
497 `(:raw-value ,raw-value
498 :title ,title
499 :begin ,begin
500 :end ,end
501 :hiddenp ,(and (> contents-end contents-begin) hidden)
502 :contents-begin ,contents-begin
503 :contents-end ,contents-end
504 :level ,(nth 1 components)
505 :priority ,(nth 3 components)
506 :tags ,(nth 5 components)
507 :todo-keyword ,todo
508 :todo-type ,todo-type
509 :scheduled ,scheduled
510 :deadline ,deadline
511 :timestamp ,timestamp
512 :clock ,clock
513 :post-blank ,(count-lines pos-before-blank end)
514 ,@standard-props
515 ,@(cadr keywords))))))
517 (defun org-element-inlinetask-interpreter (inlinetask contents)
518 "Interpret INLINETASK element as Org syntax.
519 CONTENTS is the contents of inlinetask."
520 (let* ((level (org-element-get-property :level inlinetask))
521 (todo (org-element-get-property :todo-keyword inlinetask))
522 (priority (org-element-get-property :priority inlinetask))
523 (title (org-element-get-property :raw-value inlinetask))
524 (tags (org-element-get-property :tags inlinetask))
525 (task (concat (make-string level ?*)
526 (and todo (concat " " todo))
527 (and priority (concat " " priority))
528 (and title (concat " " title))))
529 ;; Align tags.
530 (tags-fmt (when tags
531 (format "%% %ds"
532 (cond
533 ((zerop org-tags-column) 1)
534 ((< 0 org-tags-column)
535 (max (+ org-tags-column
536 (length inlinetask)
537 (length tags))
539 (t (max (- org-tags-column (length inlinetask))
540 1)))))))
541 (concat inlinetask (and tags (format tags-fmt tags) "\n" contents))))
543 ;;;; Item
544 (defun org-element-item-parser (struct)
545 "Parse an item.
547 STRUCT is the structure of the plain list.
549 Return a list whose car is `item' and cdr is a plist containing
550 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
551 `:checkbox', `:counter', `:tag', `:raw-tag', `:structure',
552 `:hiddenp' and `:post-blank' keywords.
554 Assume point is at the beginning of the item."
555 (save-excursion
556 (beginning-of-line)
557 (let* ((begin (point))
558 (bullet (org-list-get-bullet (point) struct))
559 (checkbox (let ((box (org-list-get-checkbox begin struct)))
560 (cond ((equal "[ ]" box) 'off)
561 ((equal "[X]" box) 'on)
562 ((equal "[-]" box) 'trans))))
563 (counter (let ((c (org-list-get-counter begin struct)))
564 (cond
565 ((not c) nil)
566 ((string-match "[A-Za-z]" c)
567 (- (string-to-char (upcase (match-string 0 c)))
568 64))
569 ((string-match "[0-9]+" c)
570 (string-to-number (match-string 0 c))))))
571 (raw-tag (org-list-get-tag begin struct))
572 (tag (and raw-tag
573 (org-element-parse-secondary-string
574 raw-tag
575 (cdr (assq 'item org-element-string-restrictions)))))
576 (end (org-list-get-item-end begin struct))
577 (contents-begin (progn (looking-at org-list-full-item-re)
578 (goto-char (match-end 0))
579 (org-skip-whitespace)
580 (if (>= (point) end)
581 (point-at-bol)
582 (point))))
583 (hidden (progn (forward-line)
584 (and (not (= (point) end))
585 (org-truely-invisible-p))))
586 (contents-end (progn (goto-char end)
587 (skip-chars-backward " \r\t\n")
588 (forward-line)
589 (point))))
590 (list 'item
591 `(:bullet ,bullet
592 :begin ,begin
593 :end ,end
594 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
595 ;; up in the case of an empty item separated
596 ;; from the next by a blank line. Thus, ensure
597 ;; the former is always the smallest of two.
598 :contents-begin ,(min contents-begin contents-end)
599 :contents-end ,(max contents-begin contents-end)
600 :checkbox ,checkbox
601 :counter ,counter
602 :raw-tag ,raw-tag
603 :tag ,tag
604 :hiddenp ,hidden
605 :structure ,struct
606 :post-blank ,(count-lines contents-end end))))))
608 (defun org-element-item-interpreter (item contents)
609 "Interpret ITEM element as Org syntax.
610 CONTENTS is the contents of the element."
611 (let* ((bullet (org-element-get-property :bullet item))
612 (checkbox (org-element-get-property :checkbox item))
613 (counter (org-element-get-property :counter item))
614 (tag (org-element-get-property :raw-tag item))
615 ;; Compute indentation.
616 (ind (make-string (length bullet) 32)))
617 ;; Indent contents.
618 (concat
619 bullet
620 (when (and org-list-two-spaces-after-bullet-regexp
621 (string-match org-list-two-spaces-after-bullet-regexp bullet))
622 " ")
623 (and counter (format "[@%d] " counter))
624 (cond
625 ((eq checkbox 'on) "[X] ")
626 ((eq checkbox 'off) "[ ] ")
627 ((eq checkbox 'trans) "[-] "))
628 (and tag (format "%s :: " tag))
629 (org-trim
630 (replace-regexp-in-string
631 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))))
633 ;;;; Plain List
634 (defun org-element-plain-list-parser (&optional structure)
635 "Parse a plain list.
637 Return a list whose car is `plain-list' and cdr is a plist
638 containing `:type', `:begin', `:end', `:contents-begin' and
639 `:contents-end', `:level', `:structure' and `:post-blank'
640 keywords.
642 Assume point is at one of the list items."
643 (save-excursion
644 (let* ((struct (or structure (org-list-struct)))
645 (prevs (org-list-prevs-alist struct))
646 (parents (org-list-parents-alist struct))
647 (type (org-list-get-list-type (point) struct prevs))
648 (contents-begin (goto-char
649 (org-list-get-list-begin (point) struct prevs)))
650 (keywords (org-element-collect-affiliated-keywords))
651 (begin (car keywords))
652 (contents-end (goto-char
653 (org-list-get-list-end (point) struct prevs)))
654 (end (save-excursion (org-skip-whitespace)
655 (if (eobp) (point) (point-at-bol))))
656 (level 0))
657 ;; Get list level.
658 (let ((item contents-begin))
659 (while (setq item
660 (org-list-get-parent
661 (org-list-get-list-begin item struct prevs)
662 struct parents))
663 (incf level)))
664 ;; Blank lines below list belong to the top-level list only.
665 (when (> level 0)
666 (setq end (min (org-list-get-bottom-point struct)
667 (progn (org-skip-whitespace)
668 (if (eobp) (point) (point-at-bol))))))
669 ;; Return value.
670 (list 'plain-list
671 `(:type ,type
672 :begin ,begin
673 :end ,end
674 :contents-begin ,contents-begin
675 :contents-end ,contents-end
676 :level ,level
677 :structure ,struct
678 :post-blank ,(count-lines contents-end end)
679 ,@(cadr keywords))))))
681 (defun org-element-plain-list-interpreter (plain-list contents)
682 "Interpret PLAIN-LIST element as Org syntax.
683 CONTENTS is the contents of the element."
684 contents)
686 ;;;; Quote Block
687 (defun org-element-quote-block-parser ()
688 "Parse a quote block.
690 Return a list whose car is `quote-block' and cdr is a plist
691 containing `:begin', `:end', `:hiddenp', `:contents-begin',
692 `:contents-end' and `:post-blank' keywords.
694 Assume point is at beginning or end of the block."
695 (save-excursion
696 (let* ((case-fold-search t)
697 (keywords (progn
698 (end-of-line)
699 (re-search-backward
700 (concat "^[ \t]*#\\+begin_quote") nil t)
701 (org-element-collect-affiliated-keywords)))
702 (begin (car keywords))
703 (contents-begin (progn (forward-line) (point)))
704 (hidden (org-truely-invisible-p))
705 (contents-end (progn (re-search-forward
706 (concat "^[ \t]*#\\+end_quote") nil t)
707 (point-at-bol)))
708 (pos-before-blank (progn (forward-line) (point)))
709 (end (progn (org-skip-whitespace)
710 (if (eobp) (point) (point-at-bol)))))
711 (list 'quote-block
712 `(:begin ,begin
713 :end ,end
714 :hiddenp ,hidden
715 :contents-begin ,contents-begin
716 :contents-end ,contents-end
717 :post-blank ,(count-lines pos-before-blank end)
718 ,@(cadr keywords))))))
721 (defun org-element-quote-block-interpreter (quote-block contents)
722 "Interpret QUOTE-BLOCK element as Org syntax.
723 CONTENTS is the contents of the element."
724 (format "#+begin_quote\n%s#+end_quote" contents))
726 ;;;; Special Block
727 (defun org-element-special-block-parser ()
728 "Parse a special block.
730 Return a list whose car is `special-block' and cdr is a plist
731 containing `:type', `:begin', `:end', `:hiddenp',
732 `:contents-begin', `:contents-end' and `:post-blank' keywords.
734 Assume point is at beginning or end of the block."
735 (save-excursion
736 (let* ((case-fold-search t)
737 (type (progn (looking-at
738 "[ \t]*#\\+\\(?:begin\\|end\\)_\\([-A-Za-z0-9]+\\)")
739 (org-match-string-no-properties 1)))
740 (keywords (progn
741 (end-of-line)
742 (re-search-backward
743 (concat "^[ \t]*#\\+begin_" type) nil t)
744 (org-element-collect-affiliated-keywords)))
745 (begin (car keywords))
746 (contents-begin (progn (forward-line) (point)))
747 (hidden (org-truely-invisible-p))
748 (contents-end (progn (re-search-forward
749 (concat "^[ \t]*#\\+end_" type) nil t)
750 (point-at-bol)))
751 (pos-before-blank (progn (forward-line) (point)))
752 (end (progn (org-skip-whitespace)
753 (if (eobp) (point) (point-at-bol)))))
754 (list 'special-block
755 `(:type ,type
756 :begin ,begin
757 :end ,end
758 :hiddenp ,hidden
759 :contents-begin ,contents-begin
760 :contents-end ,contents-end
761 :post-blank ,(count-lines pos-before-blank end)
762 ,@(cadr keywords))))))
764 (defun org-element-special-block-interpreter (special-block contents)
765 "Interpret SPECIAL-BLOCK element as Org syntax.
766 CONTENTS is the contents of the element."
767 (let ((block-type (org-element-get-property :type special-block)))
768 (format "#+begin_%s\n%s#+end_%s" block-type contents block-type)))
772 ;;; Elements
774 ;; For each element, a parser and an interpreter are also defined.
775 ;; Both follow the same naming convention used for greater elements.
777 ;; Also, as for greater elements, adding a new element type is done
778 ;; through the following steps: implement a parser and an interpreter,
779 ;; tweak `org-element-guess-type' so that it recognizes the new type
780 ;; and add that new type to `org-element-all-elements'.
782 ;; As a special case, when the newly defined type is a block type,
783 ;; `org-element-non-recursive-block-alist' has to be modified
784 ;; accordingly.
787 ;;;; Babel Call
788 (defun org-element-babel-call-parser ()
789 "Parse a babel call.
791 Return a list whose car is `babel-call' and cdr is a plist
792 containing `:begin', `:end', `:info' and `:post-blank' as
793 keywords."
794 (save-excursion
795 (let ((info (progn (looking-at org-babel-block-lob-one-liner-regexp)
796 (org-babel-lob-get-info)))
797 (beg (point-at-bol))
798 (pos-before-blank (progn (forward-line) (point)))
799 (end (progn (org-skip-whitespace)
800 (if (eobp) (point) (point-at-bol)))))
801 (list 'babel-call
802 `(:beg ,beg
803 :end ,end
804 :info ,info
805 :post-blank ,(count-lines pos-before-blank end))))))
807 (defun org-element-babel-call-interpreter (inline-babel-call contents)
808 "Interpret INLINE-BABEL-CALL object as Org syntax.
809 CONTENTS is nil."
810 (let* ((babel-info (org-element-get-property :info inline-babel-call))
811 (main-source (car babel-info))
812 (post-options (nth 1 babel-info)))
813 (concat "#+call: "
814 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
815 ;; Remove redundant square brackets.
816 (replace-match
817 (match-string 1 main-source) nil nil main-source)
818 main-source)
819 (and post-options (format "[%s]" post-options)))))
821 ;;;; Comment
822 (defun org-element-comment-parser ()
823 "Parse a comment.
825 Return a list whose car is `comment' and cdr is a plist
826 containing `:begin', `:end', `:value' and `:post-blank'
827 keywords."
828 (let ((comment-re "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)")
829 beg-coms begin end value pos-before-blank keywords)
830 (save-excursion
831 ;; Move to the beginning of comments.
832 (unless (bobp)
833 (while (and (not (bobp)) (looking-at comment-re))
834 (forward-line -1))
835 (unless (looking-at comment-re) (forward-line 1)))
836 (setq beg-coms (point))
837 ;; Get affiliated keywords, if any.
838 (setq keywords (org-element-collect-affiliated-keywords))
839 ;; Store true beginning of element.
840 (setq begin (car keywords))
841 ;; Get ending of comments. If point is in a list, ensure to not
842 ;; get outside of it.
843 (let* ((itemp (org-in-item-p))
844 (max-pos (if itemp
845 (org-list-get-bottom-point
846 (save-excursion (goto-char itemp) (org-list-struct)))
847 (point-max))))
848 (while (and (looking-at comment-re) (< (point) max-pos))
849 (forward-line)))
850 (setq pos-before-blank (point))
851 ;; Find position after blank.
852 (org-skip-whitespace)
853 (setq end (if (eobp) (point) (point-at-bol)))
854 ;; Extract value.
855 (setq value (buffer-substring-no-properties beg-coms pos-before-blank)))
856 (list 'comment
857 `(:begin ,begin
858 :end ,end
859 :value ,value
860 :post-blank ,(count-lines pos-before-blank end)
861 ,@(cadr keywords)))))
863 (defun org-element-comment-interpreter (comment contents)
864 "Interpret COMMENT element as Org syntax.
865 CONTENTS is nil."
866 (org-element-get-property :value comment))
868 ;;;; Comment Block
869 (defun org-element-comment-block-parser ()
870 "Parse an export block.
872 Return a list whose car is `comment-block' and cdr is a plist
873 containing `:begin', `:end', `:hiddenp', `:value' and
874 `:post-blank' keywords."
875 (save-excursion
876 (end-of-line)
877 (let* ((case-fold-search t)
878 (keywords (progn
879 (re-search-backward "^[ \t]*#\\+begin_comment" nil t)
880 (org-element-collect-affiliated-keywords)))
881 (begin (car keywords))
882 (contents-begin (progn (forward-line) (point)))
883 (hidden (org-truely-invisible-p))
884 (contents-end (progn (re-search-forward
885 "^[ \t]*#\\+end_comment" nil t)
886 (point-at-bol)))
887 (pos-before-blank (progn (forward-line) (point)))
888 (end (progn (org-skip-whitespace)
889 (if (eobp) (point) (point-at-bol))))
890 (value (buffer-substring-no-properties contents-begin contents-end)))
891 (list 'comment-block
892 `(:begin ,begin
893 :end ,end
894 :value ,value
895 :hiddenp ,hidden
896 :post-blank ,(count-lines pos-before-blank end)
897 ,@(cadr keywords))))))
899 (defun org-element-comment-block-interpreter (comment-block contents)
900 "Interpret COMMENT-BLOCK element as Org syntax.
901 CONTENTS is nil."
902 (concat "#+begin_comment\n"
903 (org-remove-indentation
904 (org-element-get-property :value comment-block))
905 "#+begin_comment"))
907 ;;;; Example Block
908 (defun org-element-example-block-parser ()
909 "Parse an example block.
911 Return a list whose car is `example' and cdr is a plist
912 containing `:begin', `:end', `:options', `:hiddenp', `:value' and
913 `:post-blank' keywords."
914 (save-excursion
915 (end-of-line)
916 (let* ((case-fold-search t)
917 (options (progn
918 (re-search-backward
919 "^[ \t]*#\\+begin_example\\(?: +\\(.*\\)\\)?" nil t)
920 (org-match-string-no-properties 1)))
921 (keywords (org-element-collect-affiliated-keywords))
922 (begin (car keywords))
923 (contents-begin (progn (forward-line) (point)))
924 (hidden (org-truely-invisible-p))
925 (contents-end (progn
926 (re-search-forward "^[ \t]*#\\+end_example" nil t)
927 (point-at-bol)))
928 (value (buffer-substring-no-properties contents-begin contents-end))
929 (pos-before-blank (progn (forward-line) (point)))
930 (end (progn (org-skip-whitespace)
931 (if (eobp) (point) (point-at-bol)))))
932 (list 'example-block
933 `(:begin ,begin
934 :end ,end
935 :value ,value
936 :options ,options
937 :hiddenp ,hidden
938 :post-blank ,(count-lines pos-before-blank end)
939 ,@(cadr keywords))))))
941 (defun org-element-example-block-interpreter (example-block contents)
942 "Interpret EXAMPLE-BLOCK element as Org syntax.
943 CONTENTS is nil."
944 (let ((options (org-element-get-property :options example-block)))
945 (concat "#+begin_example" (and options (concat " " options)) "\n"
946 (org-remove-indentation
947 (org-element-get-property :value example-block))
948 "#+end_example")))
950 ;;;; Export Block
951 (defun org-element-export-block-parser ()
952 "Parse an export block.
954 Return a list whose car is `export-block' and cdr is a plist
955 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
956 `:post-blank' keywords."
957 (save-excursion
958 (end-of-line)
959 (let* ((case-fold-search t)
960 (contents)
961 (type (progn (re-search-backward
962 (concat "[ \t]*#\\+begin_"
963 (org-re "\\([[:alnum:]]+\\)")))
964 (downcase (org-match-string-no-properties 1))))
965 (keywords (org-element-collect-affiliated-keywords))
966 (begin (car keywords))
967 (contents-begin (progn (forward-line) (point)))
968 (hidden (org-truely-invisible-p))
969 (contents-end (progn (re-search-forward
970 (concat "^[ \t]*#\\+end_" type) nil t)
971 (point-at-bol)))
972 (pos-before-blank (progn (forward-line) (point)))
973 (end (progn (org-skip-whitespace)
974 (if (eobp) (point) (point-at-bol))))
975 (value (buffer-substring-no-properties contents-begin contents-end)))
976 (list 'export-block
977 `(:begin ,begin
978 :end ,end
979 :type ,type
980 :value ,value
981 :hiddenp ,hidden
982 :post-blank ,(count-lines pos-before-blank end)
983 ,@(cadr keywords))))))
985 (defun org-element-export-block-interpreter (export-block contents)
986 "Interpret EXPORT-BLOCK element as Org syntax.
987 CONTENTS is nil."
988 (let ((type (org-element-get-property :type export-block)))
989 (concat (format "#+begin_%s\n" type)
990 (org-element-get-property :value export-block)
991 (format "#+end_%s" type))))
993 ;;;; Fixed-width
994 (defun org-element-fixed-width-parser ()
995 "Parse a fixed-width section.
997 Return a list whose car is `fixed-width' and cdr is a plist
998 containing `:begin', `:end', `:value' and `:post-blank'
999 keywords."
1000 (let ((fixed-re "[ \t]*:\\( \\|$\\)")
1001 beg-area begin end value pos-before-blank keywords)
1002 (save-excursion
1003 ;; Move to the beginning of the fixed-width area.
1004 (unless (bobp)
1005 (while (and (not (bobp)) (looking-at fixed-re))
1006 (forward-line -1))
1007 (unless (looking-at fixed-re) (forward-line 1)))
1008 (setq beg-area (point))
1009 ;; Get affiliated keywords, if any.
1010 (setq keywords (org-element-collect-affiliated-keywords))
1011 ;; Store true beginning of element.
1012 (setq begin (car keywords))
1013 ;; Get ending of fixed-width area. If point is in a list,
1014 ;; ensure to not get outside of it.
1015 (let* ((itemp (org-in-item-p))
1016 (max-pos (if itemp
1017 (org-list-get-bottom-point
1018 (save-excursion (goto-char itemp) (org-list-struct)))
1019 (point-max))))
1020 (while (and (looking-at fixed-re) (< (point) max-pos))
1021 (forward-line)))
1022 (setq pos-before-blank (point))
1023 ;; Find position after blank
1024 (org-skip-whitespace)
1025 (setq end (if (eobp) (point) (point-at-bol)))
1026 ;; Extract value.
1027 (setq value (buffer-substring-no-properties beg-area pos-before-blank)))
1028 (list 'fixed-width
1029 `(:begin ,begin
1030 :end ,end
1031 :value ,value
1032 :post-blank ,(count-lines pos-before-blank end)
1033 ,@(cadr keywords)))))
1035 (defun org-element-fixed-width-interpreter (fixed-width contents)
1036 "Interpret FIXED-WIDTH element as Org syntax.
1037 CONTENTS is nil."
1038 (org-remove-indentation (org-element-get-property :value fixed-width)))
1040 ;;;; Horizontal Rule
1041 (defun org-element-horizontal-rule-parser ()
1042 "Parse an horizontal rule.
1044 Return a list whose car is `horizontal-rule' and cdr is
1045 a plist containing `:begin', `:end' and `:post-blank'
1046 keywords."
1047 (save-excursion
1048 (let* ((keywords (org-element-collect-affiliated-keywords))
1049 (begin (car keywords))
1050 (post-hr (progn (forward-line) (point)))
1051 (end (progn (org-skip-whitespace)
1052 (if (eobp) (point) (point-at-bol)))))
1053 (list 'horizontal-rule
1054 `(:begin ,begin
1055 :end ,end
1056 :post-blank ,(count-lines post-hr end)
1057 ,@(cadr keywords))))))
1059 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1060 "Interpret HORIZONTAL-RULE element as Org syntax.
1061 CONTENTS is nil."
1062 "-----")
1064 ;;;; Keyword
1065 (defun org-element-keyword-parser ()
1066 "Parse a keyword at point.
1068 Return a list whose car is `keyword' and cdr is a plist
1069 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1070 keywords."
1071 (save-excursion
1072 (let* ((begin (point))
1073 (key (progn (looking-at
1074 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1075 (org-match-string-no-properties 1)))
1076 (value (org-trim (buffer-substring-no-properties
1077 (match-end 0) (point-at-eol))))
1078 (pos-before-blank (progn (forward-line) (point)))
1079 (end (progn (org-skip-whitespace)
1080 (if (eobp) (point) (point-at-bol)))))
1081 (list 'keyword
1082 `(:key ,key
1083 :value ,value
1084 :begin ,begin
1085 :end ,end
1086 :post-blank ,(count-lines pos-before-blank end))))))
1088 (defun org-element-keyword-interpreter (keyword contents)
1089 "Interpret KEYWORD element as Org syntax.
1090 CONTENTS is nil."
1091 (format "#+%s: %s"
1092 (org-element-get-property :key keyword)
1093 (org-element-get-property :value keyword)))
1095 ;;;; Latex Environment
1096 (defun org-element-latex-environment-parser ()
1097 "Parse a LaTeX environment.
1099 Return a list whose car is `latex-environment' and cdr is a plist
1100 containing `:begin', `:end', `:value' and `:post-blank' keywords."
1101 (save-excursion
1102 (end-of-line)
1103 (let* ((case-fold-search t)
1104 (contents-begin (re-search-backward "^[ \t]*\\\\begin" nil t))
1105 (keywords (org-element-collect-affiliated-keywords))
1106 (begin (car keywords))
1107 (contents-end (progn (re-search-forward "^[ \t]*\\\\end")
1108 (forward-line)
1109 (point)))
1110 (value (buffer-substring-no-properties contents-begin contents-end))
1111 (end (progn (org-skip-whitespace)
1112 (if (eobp) (point) (point-at-bol)))))
1113 (list 'latex-environment
1114 `(:begin ,begin
1115 :end ,end
1116 :value ,value
1117 :post-blank ,(count-lines contents-end end)
1118 ,@(cadr keywords))))))
1120 (defun org-element-latex-environment-interpreter (latex-environment contents)
1121 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1122 CONTENTS is nil."
1123 (org-element-get-property :value latex-environment))
1125 ;;;; Paragraph
1126 (defun org-element-paragraph-parser ()
1127 "Parse a paragraph.
1129 Return a list whose car is `paragraph' and cdr is a plist
1130 containing `:begin', `:end', `:contents-begin' and
1131 `:contents-end' and `:post-blank' keywords.
1133 Assume point is at the beginning of the paragraph."
1134 (save-excursion
1135 (let* ((contents-begin (point))
1136 (keywords (org-element-collect-affiliated-keywords))
1137 (begin (car keywords))
1138 (contents-end (progn
1139 (end-of-line)
1140 (if (re-search-forward
1141 org-element-paragraph-separate nil 'm)
1142 (progn (forward-line -1) (end-of-line) (point))
1143 (point))))
1144 (pos-before-blank (progn (forward-line) (point)))
1145 (end (progn (org-skip-whitespace)
1146 (if (eobp) (point) (point-at-bol)))))
1147 (list 'paragraph
1148 `(:begin ,begin
1149 :end ,end
1150 :contents-begin ,contents-begin
1151 :contents-end ,contents-end
1152 :post-blank ,(count-lines pos-before-blank end)
1153 ,@(cadr keywords))))))
1155 (defun org-element-paragraph-interpreter (paragraph contents)
1156 "Interpret PARAGRAPH element as Org syntax.
1157 CONTENTS is the contents of the element."
1158 contents)
1160 ;;;; Property Drawer
1161 (defun org-element-property-drawer-parser ()
1162 "Parse a property drawer.
1164 Return a list whose car is `property-drawer' and cdr is a plist
1165 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1166 `:contents-end', `:properties' and `:post-blank' keywords."
1167 (save-excursion
1168 (let ((case-fold-search t)
1169 (begin (progn (end-of-line)
1170 (re-search-backward org-property-start-re)
1171 (match-beginning 0)))
1172 (contents-begin (progn (forward-line) (point)))
1173 (hidden (org-truely-invisible-p))
1174 (properties (let (val)
1175 (while (not (looking-at "^[ \t]*:END:"))
1176 (when (looking-at
1177 (org-re
1178 "[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):"))
1179 (push (cons (match-string 1)
1180 (org-trim
1181 (buffer-substring
1182 (match-end 0) (point-at-eol))))
1183 val))
1184 (forward-line))
1185 val))
1186 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t)
1187 (point-at-bol)))
1188 (pos-before-blank (progn (forward-line) (point)))
1189 (end (progn (org-skip-whitespace)
1190 (if (eobp) (point) (point-at-bol)))))
1191 (list 'property-drawer
1192 `(:begin ,begin
1193 :end ,end
1194 :hiddenp ,hidden
1195 :properties ,properties
1196 :post-blank ,(count-lines pos-before-blank end))))))
1198 (defun org-element-property-drawer-interpreter (property-drawer contents)
1199 "Interpret PROPERTY-DRAWER element as Org syntax.
1200 CONTENTS is nil."
1201 (let ((props (org-element-get-property :properties property-drawer)))
1202 (concat
1203 ":PROPERTIES:\n"
1204 (mapconcat (lambda (p)
1205 (format org-property-format (format ":%s:" (car p)) (cdr p)))
1206 (nreverse props) "\n")
1207 "\n:END:")))
1209 ;;;; Quote Section
1210 (defun org-element-quote-section-parser ()
1211 "Parse a quote section.
1213 Return a list whose car is `quote-section' and cdr is a plist
1214 containing `:begin', `:end', `:value' and `:post-blank'
1215 keywords."
1216 (save-excursion
1217 (let* ((begin (progn (org-back-to-heading t)
1218 (forward-line)
1219 (org-skip-whitespace)
1220 (point-at-bol)))
1221 (end (progn (org-with-limited-levels (outline-next-heading))
1222 (point)))
1223 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1224 (forward-line)
1225 (point)))
1226 (value (unless (= begin end)
1227 (buffer-substring-no-properties begin pos-before-blank))))
1228 (list 'quote-section
1229 `(:begin ,begin
1230 :end ,end
1231 :value ,value
1232 :post-blank ,(if value
1233 (count-lines pos-before-blank end)
1234 0))))))
1236 (defun org-element-quote-section-interpreter (quote-section contents)
1237 "Interpret QUOTE-SECTION element as Org syntax.
1238 CONTENTS is nil."
1239 (org-element-get-property :value quote-section))
1241 ;;;; Src Block
1242 (defun org-element-src-block-parser ()
1243 "Parse a src block.
1245 Return a list whose car is `src-block' and cdr is a plist
1246 containing `:language', `:switches', `:parameters', `:begin',
1247 `:end', `:hiddenp', `:contents-begin', `:contents-end', `:value'
1248 and `:post-blank' keywords."
1249 (save-excursion
1250 (end-of-line)
1251 (let* ((case-fold-search t)
1252 ;; Get position at beginning of block.
1253 (contents-begin
1254 (re-search-backward
1255 (concat "^[ \t]*#\\+begin_src"
1256 "\\(?: +\\(\\S-+\\)\\)?" ; language
1257 "\\(\\(?: +[-+][A-Za-z]\\)*\\)" ; switches
1258 "\\(.*\\)[ \t]*$") ; arguments
1259 nil t))
1260 ;; Get language as a string.
1261 (language (org-match-string-no-properties 1))
1262 ;; Get switches.
1263 (switches (org-match-string-no-properties 2))
1264 ;; Get parameters.
1265 (parameters (org-trim (org-match-string-no-properties 3)))
1266 ;; Get affiliated keywords.
1267 (keywords (org-element-collect-affiliated-keywords))
1268 ;; Get beginning position.
1269 (begin (car keywords))
1270 ;; Get position at end of block.
1271 (contents-end (progn (re-search-forward "^[ \t]*#\\+end_src" nil t)
1272 (forward-line)
1273 (point)))
1274 ;; Retrieve code.
1275 (value (buffer-substring-no-properties
1276 (save-excursion (goto-char contents-begin)
1277 (forward-line)
1278 (point))
1279 (match-beginning 0)))
1280 ;; Get position after ending blank lines.
1281 (end (progn (org-skip-whitespace)
1282 (if (eobp) (point) (point-at-bol))))
1283 ;; Get visibility status.
1284 (hidden (progn (goto-char contents-begin)
1285 (forward-line)
1286 (org-truely-invisible-p))))
1287 (list 'src-block
1288 `(:language ,language
1289 :switches ,switches
1290 :parameters ,parameters
1291 :begin ,begin
1292 :end ,end
1293 :hiddenp ,hidden
1294 :value ,value
1295 :post-blank ,(count-lines contents-end end)
1296 ,@(cadr keywords))))))
1298 (defun org-element-src-block-interpreter (src-block contents)
1299 "Interpret SRC-BLOCK element as Org syntax.
1300 CONTENTS is nil."
1301 (let ((lang (org-element-get-property :language src-block))
1302 (switches (org-element-get-property :switches src-block))
1303 (params (org-element-get-property :parameters src-block))
1304 (value (let ((val (org-element-get-property :value src-block)))
1305 (cond
1306 (org-src-preserve-indentation val)
1307 ((zerop org-edit-src-content-indentation)
1308 (org-remove-indentation val))
1310 (let ((ind (make-string
1311 org-edit-src-content-indentation 32)))
1312 (replace-regexp-in-string
1313 "\\(^\\)[ \t]*\\S-" ind
1314 (org-remove-indentation val) nil nil 1)))))))
1315 (concat (format "#+begin_src%s\n"
1316 (concat (and lang (concat " " lang))
1317 (and switches (concat " " switches))
1318 (and params (concat " " params))))
1319 value
1320 "#+end_src")))
1322 ;;;; Table
1323 (defun org-element-table-parser ()
1324 "Parse a table at point.
1326 Return a list whose car is `table' and cdr is a plist containing
1327 `:begin', `:end', `:contents-begin', `:contents-end', `:tblfm',
1328 `:type', `:raw-table' and `:post-blank' keywords."
1329 (save-excursion
1330 (let* ((table-begin (goto-char (org-table-begin t)))
1331 (type (if (org-at-table.el-p) 'table.el 'org))
1332 (keywords (org-element-collect-affiliated-keywords))
1333 (begin (car keywords))
1334 (table-end (goto-char (marker-position (org-table-end t))))
1335 (tblfm (when (looking-at "[ \t]*#\\+tblfm: +\\(.*\\)[ \t]*")
1336 (prog1 (org-match-string-no-properties 1)
1337 (forward-line))))
1338 (pos-before-blank (point))
1339 (end (progn (org-skip-whitespace)
1340 (if (eobp) (point) (point-at-bol))))
1341 (raw-table (org-remove-indentation
1342 (buffer-substring-no-properties table-begin table-end))))
1343 (list 'table
1344 `(:begin ,begin
1345 :end ,end
1346 :type ,type
1347 :raw-table ,raw-table
1348 :tblfm ,tblfm
1349 :post-blank ,(count-lines pos-before-blank end)
1350 ,@(cadr keywords))))))
1352 (defun org-element-table-interpreter (table contents)
1353 "Interpret TABLE element as Org syntax.
1354 CONTENTS is nil."
1355 (org-element-get-property :raw-table table))
1357 ;;;; Verse Block
1358 (defun org-element-verse-block-parser ()
1359 "Parse a verse block.
1361 Return a list whose car is `verse-block' and cdr is a plist
1362 containing `:begin', `:end', `:hiddenp', `:raw-value', `:value'
1363 and `:post-blank' keywords.
1365 Assume point is at beginning or end of the block."
1366 (save-excursion
1367 (let* ((case-fold-search t)
1368 (keywords (progn
1369 (end-of-line)
1370 (re-search-backward
1371 (concat "^[ \t]*#\\+begin_verse") nil t)
1372 (org-element-collect-affiliated-keywords)))
1373 (begin (car keywords))
1374 (hidden (progn (forward-line) (org-truely-invisible-p)))
1375 (raw-val (buffer-substring-no-properties
1376 (point)
1377 (progn
1378 (re-search-forward (concat "^[ \t]*#\\+end_verse") nil t)
1379 (point-at-bol))))
1380 (pos-before-blank (progn (forward-line) (point)))
1381 (end (progn (org-skip-whitespace)
1382 (if (eobp) (point) (point-at-bol))))
1383 (value (org-element-parse-secondary-string
1384 (org-remove-indentation raw-val)
1385 (cdr (assq 'verse org-element-string-restrictions)))))
1386 (list 'verse-block
1387 `(:begin ,begin
1388 :end ,end
1389 :hiddenp ,hidden
1390 :raw-value ,raw-val
1391 :value ,value
1392 :post-blank ,(count-lines pos-before-blank end)
1393 ,@(cadr keywords))))))
1396 (defun org-element-verse-block-interpreter (verse-block contents)
1397 "Interpret VERSE-BLOCK element as Org syntax.
1398 CONTENTS is nil."
1399 (format "#+begin_verse\n%s#+end_verse"
1400 (org-remove-indentation
1401 (org-element-get-property :raw-value verse-block))))
1405 ;;; Objects
1407 ;; Unlike to elements, interstices can be found between objects.
1408 ;; That's why, along with the parser, successor functions are provided
1409 ;; for each object. Some objects share the same successor
1410 ;; (i.e. `emphasis' and `verbatim' objects).
1412 ;; A successor must accept a single argument bounding the search. It
1413 ;; will return either a cons cell whose car is the object's type, as
1414 ;; a symbol, and cdr the position of its next occurrence, or nil.
1416 ;; Successors follow the naming convention:
1417 ;; org-element-NAME-successor, where NAME is the name of the
1418 ;; successor, as defined in `org-element-all-successors'.
1420 ;; Some object types (i.e `emphasis') are recursive. Restrictions on
1421 ;; object types they can contain will be specified in
1422 ;; `org-element-object-restrictions'.
1424 ;; Adding a new type of object is simple. Implement a successor,
1425 ;; a parser, and an interpreter for it, all following the naming
1426 ;; convention. Register successor in `org-element-all-successors',
1427 ;; maybe tweak restrictions about it, and that's it.
1429 ;;;; Emphasis
1430 (defun org-element-emphasis-parser ()
1431 "Parse text markup object at point.
1433 Return a list whose car is `emphasis' and cdr is a plist with
1434 `:marker', `:begin', `:end', `:contents-begin' and
1435 `:contents-end' and `:post-blank' keywords.
1437 Assume point is at the first emphasis marker."
1438 (save-excursion
1439 (unless (bolp) (backward-char 1))
1440 (looking-at org-emph-re)
1441 (let ((begin (match-beginning 2))
1442 (marker (org-match-string-no-properties 3))
1443 (contents-begin (match-beginning 4))
1444 (contents-end (match-end 4))
1445 (post-blank (progn (goto-char (match-end 2))
1446 (skip-chars-forward " \t")))
1447 (end (point)))
1448 (list 'emphasis
1449 `(:marker ,marker
1450 :begin ,begin
1451 :end ,end
1452 :contents-begin ,contents-begin
1453 :contents-end ,contents-end
1454 :post-blank ,post-blank)))))
1456 (defun org-element-emphasis-interpreter (emphasis contents)
1457 "Interpret EMPHASIS object as Org syntax.
1458 CONTENTS is the contents of the object."
1459 (let ((marker (org-element-get-property :marker emphasis)))
1460 (concat marker contents marker)))
1462 (defun org-element-text-markup-successor (limit)
1463 "Search for the next emphasis or verbatim and return position.
1465 LIMIT bounds the search.
1467 Return value is a cons cell whose car is `emphasis' or
1468 `verbatim' and cdr is beginning position."
1469 (save-excursion
1470 (unless (bolp) (backward-char))
1471 (when (re-search-forward org-emph-re limit t)
1472 (cons (if (nth 4 (assoc (match-string 3) org-emphasis-alist))
1473 'verbatim
1474 'emphasis)
1475 (match-beginning 2)))))
1477 ;;;; Entity
1478 (defun org-element-entity-parser ()
1479 "Parse entity at point.
1481 Return a list whose car is `entity' and cdr a plist with
1482 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1483 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1484 keywords.
1486 Assume point is at the beginning of the entity."
1487 (save-excursion
1488 (looking-at "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1489 (let* ((value (org-entity-get (match-string 1)))
1490 (begin (match-beginning 0))
1491 (bracketsp (string= (match-string 2) "{}"))
1492 (post-blank (progn (goto-char (match-end 1))
1493 (when bracketsp (forward-char 2))
1494 (skip-chars-forward " \t")))
1495 (end (point)))
1496 (list 'entity
1497 `(:name ,(car value)
1498 :latex ,(nth 1 value)
1499 :latex-math-p ,(nth 2 value)
1500 :html ,(nth 3 value)
1501 :ascii ,(nth 4 value)
1502 :latin1 ,(nth 5 value)
1503 :utf-8 ,(nth 6 value)
1504 :begin ,begin
1505 :end ,end
1506 :use-brackets-p ,bracketsp
1507 :post-blank ,post-blank)))))
1509 (defun org-element-entity-interpreter (entity contents)
1510 "Interpret ENTITY object as Org syntax.
1511 CONTENTS is nil."
1512 (concat "\\"
1513 (org-element-get-property :name entity)
1514 (when (org-element-get-property :use-brackets-p entity) "{}")))
1516 (defun org-element-latex-or-entity-successor (limit)
1517 "Search for the next latex-fragment or entity object.
1519 LIMIT bounds the search.
1521 Return value is a cons cell whose car is `entity' or
1522 `latex-fragment' and cdr is beginning position."
1523 (save-excursion
1524 (let ((matchers (plist-get org-format-latex-options :matchers))
1525 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1526 (entity-re
1527 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"))
1528 (when (re-search-forward
1529 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
1530 matchers "\\|")
1531 "\\|" entity-re)
1532 limit t)
1533 (goto-char (match-beginning 0))
1534 (if (looking-at entity-re)
1535 ;; Determine if it's a real entity or a LaTeX command.
1536 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
1537 (match-beginning 0))
1538 ;; No entity nor command: point is at a LaTeX fragment.
1539 ;; Determine its type to get the correct beginning position.
1540 (cons 'latex-fragment
1541 (catch 'return
1542 (mapc (lambda (e)
1543 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
1544 (throw 'return
1545 (match-beginning
1546 (nth 2 (assoc e org-latex-regexps))))))
1547 matchers)
1548 (point))))))))
1550 ;;;; Export Snippet
1551 (defun org-element-export-snippet-parser ()
1552 "Parse export snippet at point.
1554 Return a list whose car is `export-snippet' and cdr a plist with
1555 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1556 keywords.
1558 Assume point is at the beginning of the snippet."
1559 (save-excursion
1560 (looking-at "@\\([-A-Za-z0-9]+\\){")
1561 (let* ((begin (point))
1562 (back-end (org-match-string-no-properties 1))
1563 (before-blank (progn (goto-char (scan-sexps (1- (match-end 0)) 1))))
1564 (value (buffer-substring-no-properties
1565 (match-end 0) (1- before-blank)))
1566 (post-blank (skip-chars-forward " \t"))
1567 (end (point)))
1568 (list 'export-snippet
1569 `(:back-end ,back-end
1570 :value ,value
1571 :begin ,begin
1572 :end ,end
1573 :post-blank ,post-blank)))))
1575 (defun org-element-export-snippet-interpreter (export-snippet contents)
1576 "Interpret EXPORT-SNIPPET object as Org syntax.
1577 CONTENTS is nil."
1578 (format "@%s{%s}"
1579 (org-element-get-property :back-end export-snippet)
1580 (org-element-get-property :value export-snippet)))
1582 (defun org-element-export-snippet-successor (limit)
1583 "Search for the next export-snippet object.
1585 LIMIT bounds the search.
1587 Return value is a cons cell whose car is `export-snippet' cdr is
1588 its beginning position."
1589 (save-excursion
1590 (catch 'exit
1591 (while (re-search-forward "@[-A-Za-z0-9]+{" limit t)
1592 (when (let ((end (ignore-errors (scan-sexps (1- (point)) 1))))
1593 (and end (eq (char-before end) ?})))
1594 (throw 'exit (cons 'export-snippet (match-beginning 0))))))))
1596 ;;;; Footnote Reference
1598 (defun org-element-footnote-reference-parser ()
1599 "Parse footnote reference at point.
1601 Return a list whose car is `footnote-reference' and cdr a plist
1602 with `:label', `:type', `:definition', `:begin', `:end' and
1603 `:post-blank' as keywords."
1604 (save-excursion
1605 (let* ((ref (org-footnote-at-reference-p))
1606 (label (car ref))
1607 (raw-def (nth 3 ref))
1608 (inline-def (and raw-def
1609 (org-element-parse-secondary-string raw-def nil)))
1610 (type (if (nth 3 ref) 'inline 'standard))
1611 (begin (nth 1 ref))
1612 (post-blank (progn (goto-char (nth 2 ref))
1613 (skip-chars-forward " \t")))
1614 (end (point)))
1615 (list 'footnote-reference
1616 `(:label ,label
1617 :type ,type
1618 :inline-definition ,inline-def
1619 :begin ,begin
1620 :end ,end
1621 :post-blank ,post-blank
1622 :raw-definition ,raw-def)))))
1624 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
1625 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1626 CONTENTS is nil."
1627 (let ((label (or (org-element-get-property :label footnote-reference)
1628 "fn:"))
1629 (def (let ((raw (org-element-get-property
1630 :raw-definition footnote-reference)))
1631 (if raw (concat ":" raw) ""))))
1632 (format "[%s]" (concat label def))))
1634 (defun org-element-footnote-reference-successor (limit)
1635 "Search for the next footnote-reference and return beginning
1636 position.
1638 LIMIT bounds the search.
1640 Return value is a cons cell whose car is `footnote-reference' and
1641 cdr is beginning position."
1642 (let (fn-ref)
1643 (when (setq fn-ref (org-footnote-get-next-reference nil nil limit))
1644 (cons 'footnote-reference (nth 1 fn-ref)))))
1647 ;;;; Inline Babel Call
1648 (defun org-element-inline-babel-call-parser ()
1649 "Parse inline babel call at point.
1651 Return a list whose car is `inline-babel-call' and cdr a plist with
1652 `:begin', `:end', `:info' and `:post-blank' as keywords.
1654 Assume point is at the beginning of the babel call."
1655 (save-excursion
1656 (unless (bolp) (backward-char))
1657 (looking-at org-babel-inline-lob-one-liner-regexp)
1658 (let ((info (save-match-data (org-babel-lob-get-info)))
1659 (begin (match-end 1))
1660 (post-blank (progn (goto-char (match-end 0))
1661 (skip-chars-forward " \t")))
1662 (end (point)))
1663 (list 'inline-babel-call
1664 `(:begin ,begin
1665 :end ,end
1666 :info ,info
1667 :post-blank ,post-blank)))))
1669 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
1670 "Interpret INLINE-BABEL-CALL object as Org syntax.
1671 CONTENTS is nil."
1672 (let* ((babel-info (org-element-get-property :info inline-babel-call))
1673 (main-source (car babel-info))
1674 (post-options (nth 1 babel-info)))
1675 (concat "call_"
1676 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
1677 ;; Remove redundant square brackets.
1678 (replace-match
1679 (match-string 1 main-source) nil nil main-source)
1680 main-source)
1681 (and post-options (format "[%s]" post-options)))))
1683 (defun org-element-inline-babel-call-successor (limit)
1684 "Search for the next inline-babel-call and return beginning
1685 position.
1687 LIMIT bounds the search.
1689 Return value is a cons cell whose car is `inline-babel-call' and
1690 cdr is beginning position."
1691 (save-excursion
1692 ;; Use a simplified version of
1693 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
1694 (when (re-search-forward
1695 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
1696 limit t)
1697 (cons 'inline-babel-call (match-beginning 0)))))
1699 ;;;; Inline Src Block
1700 (defun org-element-inline-src-block-parser ()
1701 "Parse inline source block at point.
1703 Return a list whose car is `inline-src-block' and cdr a plist
1704 with `:begin', `:end', `:language', `:value', `:parameters' and
1705 `:post-blank' as keywords.
1707 Assume point is at the beginning of the inline src block."
1708 (save-excursion
1709 (unless (bolp) (backward-char))
1710 (looking-at org-babel-inline-src-block-regexp)
1711 (let ((begin (match-beginning 1))
1712 (language (org-match-string-no-properties 2))
1713 (parameters (org-match-string-no-properties 4))
1714 (value (org-match-string-no-properties 5))
1715 (post-blank (progn (goto-char (match-end 0))
1716 (skip-chars-forward " \t")))
1717 (end (point)))
1718 (list 'inline-src-block
1719 `(:language ,language
1720 :value ,value
1721 :parameters ,parameters
1722 :begin ,begin
1723 :end ,end
1724 :post-blank ,post-blank)))))
1728 (defun org-element-inline-src-block-successor (limit)
1729 "Search for the next inline-babel-call and return beginning position.
1731 LIMIT bounds the search.
1733 Return value is a cons cell whose car is `inline-babel-call' and
1734 cdr is beginning position."
1735 (save-excursion
1736 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
1737 (cons 'inline-src-block (match-beginning 1)))))
1739 ;;;; Latex Fragment
1740 (defun org-element-latex-fragment-parser ()
1741 "Parse latex fragment at point.
1743 Return a list whose car is `latex-fragment' and cdr a plist with
1744 `:value', `:begin', `:end', and `:post-blank' as keywords.
1746 Assume point is at the beginning of the latex fragment."
1747 (save-excursion
1748 (let* ((begin (point))
1749 (substring-match
1750 (catch 'exit
1751 (mapc (lambda (e)
1752 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
1753 (when (or (looking-at latex-regexp)
1754 (and (not (bobp))
1755 (save-excursion
1756 (backward-char)
1757 (looking-at latex-regexp))))
1758 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
1759 (plist-get org-format-latex-options :matchers))
1760 ;; None found: it's a macro.
1761 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
1763 (value (match-string-no-properties substring-match))
1764 (post-blank (progn (goto-char (match-end substring-match))
1765 (skip-chars-forward " \t")))
1766 (end (point)))
1767 (list 'latex-fragment
1768 `(:value ,value
1769 :begin ,begin
1770 :end ,end
1771 :post-blank ,post-blank)))))
1773 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
1774 "Interpret LATEX-FRAGMENT object as Org syntax.
1775 CONTENTS is nil."
1776 (org-element-get-property :value latex-fragment))
1778 ;;;; Line Break
1779 (defun org-element-line-break-parser ()
1780 "Parse line break at point.
1782 Return a list whose car is `line-break', and cdr a plist with
1783 `:begin', `:end' and `:post-blank' keywords.
1785 Assume point is at the beginning of the line break."
1786 (save-excursion
1787 (let* ((begin (point))
1788 (end (progn (end-of-line) (point)))
1789 (post-blank (- (skip-chars-backward " \t")))
1790 (end (point)))
1791 (list 'line-break
1792 `(:begin ,begin
1793 :end ,end
1794 :post-blank ,post-blank)))))
1796 (defun org-element-line-break-interpreter (line-break contents)
1797 "Interpret LINE-BREAK object as Org syntax.
1798 CONTENTS is nil."
1799 (org-element-get-property :value line-break))
1801 (defun org-element-line-break-successor (limit)
1802 "Search for the next statistics cookie and return position.
1804 LIMIT bounds the search.
1806 Return value is a cons cell whose car is `line-break' and cdr is
1807 beginning position."
1808 (save-excursion
1809 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
1810 (goto-char (match-beginning 1)))))
1811 ;; A line break can only happen on a non-empty line.
1812 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
1813 (cons 'line-break beg)))))
1815 ;;;; Link
1816 (defun org-element-link-parser ()
1817 "Parse link at point.
1819 Return a list whose car is `link' and cdr a plist with `:type',
1820 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
1821 `:contents-end' and `:post-blank' as keywords.
1823 Assume point is at the beginning of the link."
1824 (save-excursion
1825 (let ((begin (point))
1826 end contents-begin contents-end link-end post-blank path type
1827 raw-link link)
1828 (cond
1829 ;; Type 1: text targeted from a radio target.
1830 ((and org-target-link-regexp (looking-at org-target-link-regexp))
1831 (setq type "radio"
1832 path (org-match-string-no-properties 0)
1833 contents-begin (match-beginning 0)
1834 contents-end (match-end 0)
1835 link-end (match-end 0)))
1836 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
1837 ((looking-at org-bracket-link-regexp)
1838 (setq contents-begin (match-beginning 3)
1839 contents-end (match-end 3)
1840 link-end (match-end 0)
1841 ;; RAW-LINK is the original link.
1842 raw-link (org-match-string-no-properties 1)
1843 link (org-link-expand-abbrev
1844 (replace-regexp-in-string
1845 " *\n *" " " (org-link-unescape raw-link) t t)))
1846 ;; Determine TYPE of link and set PATH accordingly.
1847 (cond
1848 ;; File type.
1849 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
1850 (setq type "file" path link))
1851 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
1852 ((string-match org-link-re-with-space3 link)
1853 (setq type (match-string 1 link) path (match-string 2 link)))
1854 ;; Id type: PATH is the id.
1855 ((string-match "^id:\\([-a-f0-9]+\\)" link)
1856 (setq type "id" path (match-string 1 link)))
1857 ;; Code-ref type: PATH is the name of the reference.
1858 ((string-match "^(\\(.*\\))$" link)
1859 (setq type "coderef" path (match-string 1 link)))
1860 ;; Custom-id type: PATH is the name of the custom id.
1861 ((= (aref link 0) ?#)
1862 (setq type "custom-id" path (substring link 1)))
1863 ;; Fuzzy type: Internal link either matches a target, an
1864 ;; headline name or nothing. PATH is the target or headline's
1865 ;; name.
1866 (t (setq type "fuzzy" path link))))
1867 ;; Type 3: Plain link, i.e. http://orgmode.org
1868 ((looking-at org-plain-link-re)
1869 (setq raw-link (org-match-string-no-properties 0)
1870 type (org-match-string-no-properties 1)
1871 path (org-match-string-no-properties 2)
1872 link-end (match-end 0)))
1873 ;; Type 4: Angular link, i.e. <http://orgmode.org>
1874 ((looking-at org-angle-link-re)
1875 (setq raw-link (buffer-substring-no-properties
1876 (match-beginning 1) (match-end 2))
1877 type (org-match-string-no-properties 1)
1878 path (org-match-string-no-properties 2)
1879 link-end (match-end 0))))
1880 ;; In any case, deduce end point after trailing white space from
1881 ;; LINK-END variable.
1882 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
1883 end (point))
1884 (list 'link
1885 `(:type ,type
1886 :path ,path
1887 :raw-link ,(or raw-link path)
1888 :begin ,begin
1889 :end ,end
1890 :contents-begin ,contents-begin
1891 :contents-end ,contents-end
1892 :post-blank ,post-blank)))))
1894 (defun org-element-link-interpreter (link contents)
1895 "Interpret LINK object as Org syntax.
1896 CONTENTS is the contents of the object."
1897 (let ((type (org-element-get-property :type link))
1898 (raw-link (org-element-get-property :raw-link link)))
1899 (cond
1900 ((string= type "radio") raw-link)
1901 (t (format "[[%s]%s]"
1902 raw-link
1903 (if (string= contents "") "" (format "[%s]" contents)))))))
1905 (defun org-element-link-successor (limit)
1906 "Search for the next link and return position.
1908 LIMIT bounds the search.
1910 Return value is a cons cell whose car is `link' and cdr is
1911 beginning position."
1912 (save-excursion
1913 (let ((link-regexp
1914 (if org-target-link-regexp
1915 (concat org-any-link-re "\\|" org-target-link-regexp)
1916 org-any-link-re)))
1917 (when (re-search-forward link-regexp limit t)
1918 (cons 'link (match-beginning 0))))))
1920 ;;;; Macro
1921 (defun org-element-macro-parser ()
1922 "Parse macro at point.
1924 Return a list whose car is `macro' and cdr a plist with `:key',
1925 `:args', `:begin', `:end', `:value' and `:post-blank' as
1926 keywords.
1928 Assume point is at the macro."
1929 (save-excursion
1930 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
1931 (let ((begin (point))
1932 (key (downcase (org-match-string-no-properties 1)))
1933 (value (org-match-string-no-properties 0))
1934 (post-blank (progn (goto-char (match-end 0))
1935 (skip-chars-forward " \t")))
1936 (end (point))
1937 (args (let ((args (org-match-string-no-properties 3)) args2)
1938 (when args
1939 (setq args (org-split-string args ","))
1940 (while args
1941 (while (string-match "\\\\\\'" (car args))
1942 ;; Repair bad splits.
1943 (setcar (cdr args) (concat (substring (car args) 0 -1)
1944 "," (nth 1 args)))
1945 (pop args))
1946 (push (pop args) args2))
1947 (mapcar 'org-trim (nreverse args2))))))
1948 (list 'macro
1949 `(:key ,key
1950 :value ,value
1951 :args ,args
1952 :begin ,begin
1953 :end ,end
1954 :post-blank ,post-blank)))))
1956 (defun org-element-macro-interpreter (macro contents)
1957 "Interpret MACRO object as Org syntax.
1958 CONTENTS is nil."
1959 (org-element-get-property :value macro))
1961 (defun org-element-macro-successor (limit)
1962 "Search for the next macro and return position.
1964 LIMIT bounds the search.
1966 Return value is cons cell whose car is `macro' and cdr is
1967 beginning position."
1968 (save-excursion
1969 (when (re-search-forward
1970 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
1971 limit t)
1972 (cons 'macro (match-beginning 0)))))
1974 ;;;; Radio-target
1975 (defun org-element-radio-target-parser ()
1976 "Parse radio target at point.
1978 Return a list whose car is `radio-target' and cdr a plist with
1979 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
1980 and `:post-blank' as keywords.
1982 Assume point is at the radio target."
1983 (save-excursion
1984 (looking-at org-radio-target-regexp)
1985 (let ((begin (point))
1986 (contents-begin (match-beginning 1))
1987 (contents-end (match-end 1))
1988 (raw-value (org-match-string-no-properties 1))
1989 (post-blank (progn (goto-char (match-end 0))
1990 (skip-chars-forward " \t")))
1991 (end (point)))
1992 (list 'radio-target
1993 `(:begin ,begin
1994 :end ,end
1995 :contents-begin ,contents-begin
1996 :contents-end ,contents-end
1997 :raw-value ,raw-value
1998 :post-blank ,post-blank)))))
2000 (defun org-element-radio-target-interpreter (target contents)
2001 "Interpret TARGET object as Org syntax.
2002 CONTENTS is the contents of the object."
2003 (concat ">"))
2005 (defun org-element-radio-target-successor (limit)
2006 "Search for the next radio-target and return position.
2008 LIMIT bounds the search.
2010 Return value is a cons cell whose car is `radio-target' and cdr
2011 is beginning position."
2012 (save-excursion
2013 (when (re-search-forward org-radio-target-regexp limit t)
2014 (cons 'radio-target (match-beginning 0)))))
2016 ;;;; Statistics Cookie
2017 (defun org-element-statistics-cookie-parser ()
2018 "Parse statistics cookie at point.
2020 Return a list whose car is `statistics-cookie', and cdr a plist
2021 with `:begin', `:end', `:value' and `:post-blank' keywords.
2023 Assume point is at the beginning of the statistics-cookie."
2024 (save-excursion
2025 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2026 (let* ((begin (point))
2027 (value (buffer-substring-no-properties
2028 (match-beginning 0) (match-end 0)))
2029 (post-blank (progn (goto-char (match-end 0))
2030 (skip-chars-forward " \t")))
2031 (end (point)))
2032 (list 'statistics-cookie
2033 `(:begin ,begin
2034 :end ,end
2035 :value ,value
2036 :post-blank ,post-blank)))))
2038 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
2039 "Interpret STATISTICS-COOKIE object as Org syntax.
2040 CONTENTS is nil."
2041 (org-element-get-property :value statistics-cookie))
2043 (defun org-element-statistics-cookie-successor (limit)
2044 "Search for the next statistics cookie and return position.
2046 LIMIT bounds the search.
2048 Return value is a cons cell whose car is `statistics-cookie' and
2049 cdr is beginning position."
2050 (save-excursion
2051 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
2052 (cons 'statistics-cookie (match-beginning 0)))))
2054 ;;;; Subscript
2055 (defun org-element-subscript-parser ()
2056 "Parse subscript at point.
2058 Return a list whose car is `subscript' and cdr a plist with
2059 `:begin', `:end', `:contents-begin', `:contents-end',
2060 `:use-brackets-p' and `:post-blank' as keywords.
2062 Assume point is at the underscore."
2063 (save-excursion
2064 (unless (bolp) (backward-char))
2065 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
2067 (not (looking-at org-match-substring-regexp))))
2068 (begin (match-beginning 2))
2069 (contents-begin (or (match-beginning 5)
2070 (match-beginning 3)))
2071 (contents-end (or (match-end 5) (match-end 3)))
2072 (post-blank (progn (goto-char (match-end 0))
2073 (skip-chars-forward " \t")))
2074 (end (point)))
2075 (list 'subscript
2076 `(:begin ,begin
2077 :end ,end
2078 :use-brackets-p ,bracketsp
2079 :contents-begin ,contents-begin
2080 :contents-end ,contents-end
2081 :post-blank ,post-blank)))))
2083 (defun org-element-subscript-interpreter (subscript contents)
2084 "Interpret SUBSCRIPT object as Org syntax.
2085 CONTENTS is the contents of the object."
2086 (format
2087 (if (org-element-get-property :use-brackets-p subscript) "_{%s}" "_%s")
2088 contents))
2090 (defun org-element-sub/superscript-successor (limit)
2091 "Search for the next sub/superscript and return beginning
2092 position.
2094 LIMIT bounds the search.
2096 Return value is a cons cell whose car is either `subscript' or
2097 `superscript' and cdr is beginning position."
2098 (save-excursion
2099 (when (re-search-forward org-match-substring-regexp limit t)
2100 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
2101 (match-beginning 2)))))
2103 ;;;; Superscript
2104 (defun org-element-superscript-parser ()
2105 "Parse superscript at point.
2107 Return a list whose car is `superscript' and cdr a plist with
2108 `:begin', `:end', `:contents-begin', `:contents-end',
2109 `:use-brackets-p' and `:post-blank' as keywords.
2111 Assume point is at the caret."
2112 (save-excursion
2113 (unless (bolp) (backward-char))
2114 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
2116 (not (looking-at org-match-substring-regexp))))
2117 (begin (match-beginning 2))
2118 (contents-begin (or (match-beginning 5)
2119 (match-beginning 3)))
2120 (contents-end (or (match-end 5) (match-end 3)))
2121 (post-blank (progn (goto-char (match-end 0))
2122 (skip-chars-forward " \t")))
2123 (end (point)))
2124 (list 'superscript
2125 `(:begin ,begin
2126 :end ,end
2127 :use-brackets-p ,bracketsp
2128 :contents-begin ,contents-begin
2129 :contents-end ,contents-end
2130 :post-blank ,post-blank)))))
2132 (defun org-element-superscript-interpreter (superscript contents)
2133 "Interpret SUPERSCRIPT object as Org syntax.
2134 CONTENTS is the contents of the object."
2135 (format
2136 (if (org-element-get-property :use-brackets-p superscript) "^{%s}" "^%s")
2137 contents))
2139 ;;;; Target
2140 (defun org-element-target-parser ()
2141 "Parse target at point.
2143 Return a list whose car is `target' and cdr a plist with
2144 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2145 and `:post-blank' as keywords.
2147 Assume point is at the target."
2148 (save-excursion
2149 (looking-at org-target-regexp)
2150 (let ((begin (point))
2151 (contents-begin (match-beginning 1))
2152 (contents-end (match-end 1))
2153 (raw-value (org-match-string-no-properties 1))
2154 (post-blank (progn (goto-char (match-end 0))
2155 (skip-chars-forward " \t")))
2156 (end (point)))
2157 (list 'target
2158 `(:begin ,begin
2159 :end ,end
2160 :contents-begin ,contents-begin
2161 :contents-end ,contents-end
2162 :raw-value ,raw-value
2163 :post-blank ,post-blank)))))
2165 (defun org-element-target-interpreter (target contents)
2166 "Interpret TARGET object as Org syntax.
2167 CONTENTS is the contents of target."
2168 (concat ""))
2170 (defun org-element-target-successor (limit)
2171 "Search for the next target and return position.
2173 LIMIT bounds the search.
2175 Return value is a cons cell whose car is `target' and cdr is
2176 beginning position."
2177 (save-excursion
2178 (when (re-search-forward org-target-regexp limit t)
2179 (cons 'target (match-beginning 0)))))
2181 ;;;; Time-stamp
2182 (defun org-element-time-stamp-parser ()
2183 "Parse time stamp at point.
2185 Return a list whose car is `time-stamp', and cdr a plist with
2186 `:appt-type', `:type', `:begin', `:end', `:value' and
2187 `:post-blank' keywords.
2189 Assume point is at the beginning of the time-stamp."
2190 (save-excursion
2191 (let* ((appt-type (cond
2192 ((looking-at (concat org-deadline-string " +"))
2193 (goto-char (match-end 0))
2194 'deadline)
2195 ((looking-at (concat org-scheduled-string " +"))
2196 (goto-char (match-end 0))
2197 'scheduled)
2198 ((looking-at (concat org-closed-string " +"))
2199 (goto-char (match-end 0))
2200 'closed)))
2201 (begin (and appt-type (match-beginning 0)))
2202 (type (cond
2203 ((looking-at org-tsr-regexp)
2204 (if (match-string 2) 'active-range 'active))
2205 ((looking-at org-tsr-regexp-both)
2206 (if (match-string 2) 'inactive-range 'inactive))
2207 ((looking-at (concat
2208 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2209 "\\|"
2210 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2211 'diary)))
2212 (begin (or begin (match-beginning 0)))
2213 (value (buffer-substring-no-properties
2214 (match-beginning 0) (match-end 0)))
2215 (post-blank (progn (goto-char (match-end 0))
2216 (skip-chars-forward " \t")))
2217 (end (point)))
2218 (list 'time-stamp
2219 `(:appt-type ,appt-type
2220 :type ,type
2221 :value ,value
2222 :begin ,begin
2223 :end ,end
2224 :post-blank ,post-blank)))))
2226 (defun org-element-time-stamp-interpreter (time-stamp contents)
2227 "Interpret TIME-STAMP object as Org syntax.
2228 CONTENTS is nil."
2229 (concat
2230 (case (org-element-get-property :appt-type time-stamp)
2231 (closed (concat org-closed-string " "))
2232 (deadline (concat org-deadline-string " "))
2233 (scheduled (concat org-scheduled-string " ")))
2234 (org-element-get-property :value time-stamp)))
2236 (defun org-element-time-stamp-successor (limit)
2237 "Search for the next time-stamp and return position.
2239 LIMIT bounds the search.
2241 Return value is a cons cell whose car is `time-stamp' and cdr is
2242 beginning position."
2243 (save-excursion
2244 (when (re-search-forward
2245 (concat "\\(?:" org-scheduled-string " +\\|"
2246 org-deadline-string " +\\|" org-closed-string " +\\)?"
2247 org-ts-regexp-both
2248 "\\|"
2249 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2250 "\\|"
2251 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2252 limit t)
2253 (cons 'time-stamp (match-beginning 0)))))
2255 ;;;; Verbatim
2256 (defun org-element-verbatim-parser ()
2257 "Parse verbatim object at point.
2259 Return a list whose car is `verbatim' and cdr is a plist with
2260 `:marker', `:begin', `:end' and `:post-blank' keywords.
2262 Assume point is at the first verbatim marker."
2263 (save-excursion
2264 (unless (bolp) (backward-char 1))
2265 (looking-at org-emph-re)
2266 (let ((begin (match-beginning 2))
2267 (marker (org-match-string-no-properties 3))
2268 (value (org-match-string-no-properties 4))
2269 (post-blank (progn (goto-char (match-end 2))
2270 (skip-chars-forward " \t")))
2271 (end (point)))
2272 (list 'verbatim
2273 `(:marker ,marker
2274 :begin ,begin
2275 :end ,end
2276 :value ,value
2277 :post-blank ,post-blank)))))
2279 (defun org-element-verbatim-interpreter (verbatim contents)
2280 "Interpret VERBATIM object as Org syntax.
2281 CONTENTS is nil."
2282 (let ((marker (org-element-get-property :marker verbatim))
2283 (value (org-element-get-property :value verbatim)))
2284 (concat marker value marker)))
2288 ;;; Definitions And Rules
2290 ;; Define elements, greater elements and specify recursive objects,
2291 ;; along with the affiliated keywords recognized. Also set up
2292 ;; restrictions on recursive objects combinations.
2294 ;; These variables really act as a control center for the parsing
2295 ;; process.
2296 (defconst org-element-paragraph-separate
2297 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2298 ;; Headlines and inlinetasks.
2299 org-outline-regexp-bol "\\|"
2300 ;; Comments, blocks (any type), keywords and babel calls.
2301 "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
2302 ;; Lists.
2303 (org-item-beginning-re) "\\|"
2304 ;; Fixed-width, drawers (any type) and tables.
2305 "^[ \t]*[:|]" "\\|"
2306 ;; Footnote definitions.
2307 org-footnote-definition-re "\\|"
2308 ;; Horizontal rules.
2309 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2310 ;; LaTeX environments.
2311 "^[ \t]*\\\\\\(begin\\|end\\)")
2312 "Regexp to separate paragraphs in an Org buffer.")
2314 (defconst org-element-all-elements
2315 '(center-block comment comment-block drawer dynamic-block example-block
2316 export-block fixed-width footnote-definition headline
2317 horizontal-rule inlinetask item keyword latex-environment
2318 babel-call paragraph plain-list property-drawer quote-block
2319 quote-section special-block src-block table verse-block)
2320 "Complete list of elements.")
2322 (defconst org-element-greater-elements
2323 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2324 item plain-list quote-block special-block)
2325 "List of recursive element types aka Greater Elements.")
2327 (defconst org-element-all-successors
2328 '(export-snippet footnote-reference inline-babel-call inline-src-block
2329 latex-or-entity line-break link macro radio-target
2330 statistics-cookie sub/superscript target text-markup
2331 time-stamp)
2332 "Complete list of successors.")
2334 (defconst org-element-object-successor-alist
2335 '((subscript . sub/superscript) (superscript . sub/superscript)
2336 (emphasis . text-markup) (verbatim . text-markup)
2337 (entity . latex-or-entity) (latex-fragment . latex-or-entity))
2338 "Alist of translations between object type and successor name.
2340 Sharing the same successor comes handy when, for example, the
2341 regexp matching one object can also match the other object.")
2343 (defconst org-element-recursive-objects
2344 '(emphasis link subscript superscript target radio-target)
2345 "List of recursive object types.")
2347 (defconst org-element-non-recursive-block-alist
2348 '(("ascii" . export-block)
2349 ("comment" . comment-block)
2350 ("docbook" . export-block)
2351 ("example" . example-block)
2352 ("html" . export-block)
2353 ("latex" . latex-block)
2354 ("odt" . export-block)
2355 ("src" . src-block)
2356 ("verse" . verse-block))
2357 "Alist between non-recursive block name and their element type.")
2359 (defconst org-element-affiliated-keywords
2360 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "caption"
2361 "data" "header" "headers" "label" "name" "plot" "resname" "result" "results"
2362 "source" "srcname" "tblname")
2363 "List of affiliated keywords as strings.")
2365 (defconst org-element-keyword-translation-alist
2366 '(("data" . "name") ("label" . "name") ("resname" . "name")
2367 ("source" . "name") ("srcname" . "name") ("tblname" . "name")
2368 ("result" . "results") ("headers" . "header"))
2369 "Alist of usual translations for keywords.
2370 The key is the old name and the value the new one. The property
2371 holding their value will be named after the translated name.")
2373 (defconst org-element-multiple-keywords
2374 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "header")
2375 "List of affiliated keywords that can occur more that once in an element.
2377 Their value will be consed into a list of strings, which will be
2378 returned as the value of the property.
2380 This list is checked after translations have been applied. See
2381 `org-element-keyword-translation-alist'.")
2383 (defconst org-element-parsed-keywords '("author" "caption" "title")
2384 "List of keywords whose value can be parsed.
2386 Their value will be stored as a secondary string: a list of
2387 strings and objects.
2389 This list is checked after translations have been applied. See
2390 `org-element-keyword-translation-alist'.")
2392 (defconst org-element-dual-keywords '("results")
2393 "List of keywords which can have a secondary value.
2395 In Org syntax, they can be written with optional square brackets
2396 before the colons. For example, results keyword can be
2397 associated to a hash value with the following:
2399 #+results[hash-string]: some-source
2401 This list is checked after translations have been applied. See
2402 `org-element-keyword-translation-alist'.")
2404 (defconst org-element-object-restrictions
2405 '((emphasis entity export-snippet inline-babel-call inline-src-block
2406 radio-target sub/superscript target text-markup time-stamp)
2407 (link entity export-snippet inline-babel-call inline-src-block
2408 latex-fragment sub/superscript text-markup)
2409 (radio-target entity export-snippet latex-fragment sub/superscript)
2410 (subscript entity export-snippet inline-babel-call inline-src-block
2411 latex-fragment sub/superscript text-markup)
2412 (superscript entity export-snippet inline-babel-call inline-src-block
2413 latex-fragment sub/superscript text-markup)
2414 (target entity export-snippet latex-fragment sub/superscript text-markup))
2415 "Alist of recursive objects restrictions.
2417 Car is a recursive object type and cdr is a list of successors
2418 that will be called within an object of such type.
2420 For example, in a `radio-target' object, one can only find
2421 entities, export snippets, latex-fragments, subscript and
2422 superscript.")
2424 (defconst org-element-string-restrictions
2425 '((headline entity inline-babel-call latex-fragment link macro radio-target
2426 statistics-cookie sub/superscript text-markup time-stamp)
2427 (inlinetask entity inline-babel-call latex-fragment link macro radio-target
2428 sub/superscript text-markup time-stamp)
2429 (item entity inline-babel-call latex-fragment macro radio-target
2430 sub/superscript target verbatim)
2431 (keyword entity latex-fragment macro sub/superscript text-markup)
2432 (table entity latex-fragment macro text-markup)
2433 (verse entity footnote-reference inline-babel-call inline-src-block
2434 latex-fragment line-break link macro radio-target sub/superscript
2435 target text-markup time-stamp))
2436 "Alist of secondary strings restrictions.
2438 When parsed, some elements have a secondary string which could
2439 contain various objects (i.e. headline's name, or table's cells).
2440 For association, the car is the element type, and the cdr a list
2441 of successors that will be called in that secondary string.
2443 Note: `keyword' secondary string type only applies to keywords
2444 matching `org-element-parsed-keywords'.")
2448 ;;; Accessors
2450 ;; Provide two accessors: `org-element-get-property' and
2451 ;; `org-element-get-contents'.
2452 (defun org-element-get-property (property element)
2453 "Extract the value from the PROPERTY of an ELEMENT."
2454 (plist-get (nth 1 element) property))
2456 (defun org-element-get-contents (element)
2457 "Extract contents from an ELEMENT."
2458 (nthcdr 2 element))
2462 ;; Obtaining The Smallest Element Containing Point
2464 ;; `org-element-at-point' is the core function of this section. It
2465 ;; returns the Lisp representation of the element at point. It uses
2466 ;; `org-element-guess-type' and `org-element-skip-keywords' as helper
2467 ;; functions.
2469 ;; When point is at an item, there is no automatic way to determine if
2470 ;; the function should return the `plain-list' element, or the
2471 ;; corresponding `item' element. By default, `org-element-at-point'
2472 ;; works at the `plain-list' level. But, by providing an optional
2473 ;; argument, one can make it switch to the `item' level.
2474 (defconst org-element--affiliated-re
2475 (format "[ \t]*#\\+\\(%s\\):"
2476 (mapconcat
2477 (lambda (keyword)
2478 (if (member keyword org-element-dual-keywords)
2479 (format "\\(%s\\)\\(?:\\[\\(.*?\\)\\]\\)?"
2480 (regexp-quote keyword))
2481 (regexp-quote keyword)))
2482 org-element-affiliated-keywords "\\|"))
2483 "Regexp matching any affiliated keyword.
2485 Keyword name is put in match group 1. Moreover, if keyword
2486 belongs to `org-element-dual-keywords', put the dual value in
2487 match group 2.
2489 Don't modify it, set `org-element--affiliated-keywords' instead.")
2491 (defun org-element-at-point (&optional toggle-item structure)
2492 "Determine closest element around point.
2494 Return value is a list \(TYPE PROPS\) where TYPE is the type of
2495 the element and PROPS a plist of properties associated to the
2496 element.
2498 Possible types are defined in `org-element-all-elements'.
2500 If optional argument TOGGLE-ITEM is non-nil, parse item wise
2501 instead of plain-list wise, using STRUCTURE as the current list
2502 structure.
2504 If STRUCTURE isn't provided but TOGGLE-ITEM is non-nil, it will
2505 be computed."
2506 (save-excursion
2507 (beginning-of-line)
2508 ;; Move before any blank line.
2509 (when (looking-at "[ \t]*$")
2510 (skip-chars-backward " \r\t\n")
2511 (beginning-of-line))
2512 (let ((case-fold-search t))
2513 ;; Check if point is at an affiliated keyword. In that case,
2514 ;; try moving to the beginning of the associated element. If
2515 ;; the keyword is orphaned, treat it as plain text.
2516 (when (looking-at org-element--affiliated-re)
2517 (let ((opoint (point)))
2518 (while (looking-at org-element--affiliated-re) (forward-line))
2519 (when (looking-at "[ \t]*$") (goto-char opoint))))
2520 (let ((type (org-element-guess-type)))
2521 (cond
2522 ;; Guessing element type on the current line is impossible:
2523 ;; try to find the beginning of the current element to get
2524 ;; more information.
2525 ((not type)
2526 (let ((search-origin (point))
2527 (opoint-in-item-p (org-in-item-p))
2528 (par-found-p
2529 (progn
2530 (end-of-line)
2531 (re-search-backward org-element-paragraph-separate nil 'm))))
2532 (cond
2533 ;; Unable to find a paragraph delimiter above: we're at
2534 ;; bob and looking at a paragraph.
2535 ((not par-found-p) (org-element-paragraph-parser))
2536 ;; Trying to find element's beginning set point back to
2537 ;; its original position. There's something peculiar on
2538 ;; this line that prevents parsing, probably an
2539 ;; ill-formed keyword or an undefined drawer name. Parse
2540 ;; it as plain text anyway.
2541 ((< search-origin (point-at-eol)) (org-element-paragraph-parser))
2542 ;; Original point wasn't in a list but previous paragraph
2543 ;; is. It means that either point was inside some block,
2544 ;; or current list was ended without using a blank line.
2545 ;; In the last case, paragraph really starts at list end.
2546 ((let (item)
2547 (and (not opoint-in-item-p)
2548 (not (looking-at "[ \t]*#\\+begin"))
2549 (setq item (org-in-item-p))
2550 (let ((struct (save-excursion (goto-char item)
2551 (org-list-struct))))
2552 (goto-char (org-list-get-bottom-point struct))
2553 (org-skip-whitespace)
2554 (beginning-of-line)
2555 (org-element-paragraph-parser)))))
2556 ((org-footnote-at-definition-p)
2557 (org-element-footnote-definition-parser))
2558 ((and opoint-in-item-p (org-at-item-p) (= opoint-in-item-p (point)))
2559 (if toggle-item
2560 (org-element-item-parser (or structure (org-list-struct)))
2561 (org-element-plain-list-parser (or structure (org-list-struct)))))
2562 ;; In any other case, the paragraph started the line
2563 ;; below.
2564 (t (forward-line) (org-element-paragraph-parser)))))
2565 ((eq type 'plain-list)
2566 (if toggle-item
2567 (org-element-item-parser (or structure (org-list-struct)))
2568 (org-element-plain-list-parser (or structure (org-list-struct)))))
2569 ;; Straightforward case: call the appropriate parser.
2570 (t (funcall (intern (format "org-element-%s-parser" type)))))))))
2573 ;; It is obvious to tell if point is in most elements, either by
2574 ;; looking for a specific regexp in the current line, or by using
2575 ;; already implemented functions. This is the goal of
2576 ;; `org-element-guess-type'.
2577 (defconst org-element--element-block-types
2578 (mapcar 'car org-element-non-recursive-block-alist)
2579 "List of non-recursive block types, as strings.
2580 Used internally by `org-element-guess-type'. Do not modify it
2581 directly, set `org-element-non-recursive-block-alist' instead.")
2583 (defun org-element-guess-type ()
2584 "Return the type of element at point, or nil if undetermined.
2585 This function may move point to an appropriate position for
2586 parsing. Used internally by `org-element-at-point'."
2587 ;; Beware: Order matters for some cases in that function.
2588 (beginning-of-line)
2589 (let ((case-fold-search t))
2590 (cond
2591 ((org-with-limited-levels (org-at-heading-p)) 'headline)
2592 ((let ((headline (ignore-errors (nth 4 (org-heading-components)))))
2593 (and headline
2594 (let (case-fold-search)
2595 (string-match (format "^%s\\(?: \\|$\\)" org-quote-string)
2596 headline))))
2597 'quote-section)
2598 ;; Non-recursive block.
2599 ((let ((type (org-in-block-p org-element--element-block-types)))
2600 (and type (cdr (assoc type org-element-non-recursive-block-alist)))))
2601 ((org-at-heading-p) 'inlinetask)
2602 ((org-between-regexps-p
2603 "^[ \t]*\\\\begin{" "^[ \t]*\\\\end{[^}]*}[ \t]*") 'latex-environment)
2604 ;; Property drawer. Almost `org-at-property-p', but allow drawer
2605 ;; boundaries.
2606 ((org-with-wide-buffer
2607 (and (not (org-before-first-heading-p))
2608 (let ((pblock (org-get-property-block)))
2609 (and pblock
2610 (<= (point) (cdr pblock))
2611 (>= (point-at-eol) (1- (car pblock)))))))
2612 'property-drawer)
2613 ;; Recursive block. If the block isn't complete, parse the
2614 ;; current part as a paragraph.
2615 ((looking-at "[ \t]*#\\+\\(begin\\|end\\)_\\([-A-Za-z0-9]+\\)\\(?:$\\|\\s-\\)")
2616 (let ((type (downcase (match-string 2))))
2617 (cond
2618 ((not (org-in-block-p (list type))) 'paragraph)
2619 ((string= type "center") 'center-block)
2620 ((string= type "quote") 'quote-block)
2621 (t 'special-block))))
2622 ;; Regular drawers must be tested after property drawer as both
2623 ;; elements share the same ending regexp.
2624 ((or (looking-at org-drawer-regexp) (looking-at "[ \t]*:END:[ \t]*$"))
2625 (let ((completep (org-between-regexps-p
2626 org-drawer-regexp "^[ \t]*:END:[ \t]*$")))
2627 (if (not completep)
2628 'paragraph
2629 (goto-char (car completep)) 'drawer)))
2630 ((looking-at "[ \t]*:\\( \\|$\\)") 'fixed-width)
2631 ;; Babel calls must be tested before general keywords as they are
2632 ;; a subset of them.
2633 ((looking-at org-babel-block-lob-one-liner-regexp) 'babel-call)
2634 ((looking-at org-footnote-definition-re) 'footnote-definition)
2635 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
2636 (if (member (downcase (match-string 1)) org-element-affiliated-keywords)
2637 'paragraph
2638 'keyword))
2639 ;; Dynamic block: simplify regexp used for match. If it isn't
2640 ;; complete, parse the current part as a paragraph.
2641 ((looking-at "[ \t]*#\\+\\(begin\\end\\):\\(?:\\s-\\|$\\)")
2642 (let ((completep (org-between-regexps-p
2643 "^[ \t]*#\\+begin:\\(?:\\s-\\|$\\)"
2644 "^[ \t]*#\\+end:\\(?:\\s-\\|$\\)")))
2645 (if (not completep)
2646 'paragraph
2647 (goto-char (car completep)) 'dynamic-block)))
2648 ((looking-at "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)") 'comment)
2649 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$") 'horizontal-rule)
2650 ((org-at-table-p t) 'table)
2651 ((looking-at "[ \t]*#\\+tblfm:")
2652 (forward-line -1)
2653 ;; A TBLFM line separated from any table is just plain text.
2654 (if (org-at-table-p)
2655 'table
2656 (forward-line) 'paragraph))
2657 ((looking-at (org-item-re)) 'plain-list))))
2659 ;; Most elements can have affiliated keywords. When looking for an
2660 ;; element beginning, we want to move before them, as they belong to
2661 ;; that element, and, in the meantime, collect information they give
2662 ;; into appropriate properties. Hence the following function.
2664 ;; Usage of optional arguments may not be obvious at first glance:
2666 ;; - TRANS-LIST is used to polish keywords names that have evolved
2667 ;; during Org history. In example, even though =result= and
2668 ;; =results= coexist, we want to have them under the same =result=
2669 ;; property. It's also true for "srcname" and "name", where the
2670 ;; latter seems to be preferred nowadays (thus the "name" property).
2672 ;; - CONSED allows to regroup multi-lines keywords under the same
2673 ;; property, while preserving their own identity. This is mostly
2674 ;; used for "attr_latex" and al.
2676 ;; - PARSED prepares a keyword value for export. This is useful for
2677 ;; "caption". Objects restrictions for such keywords are defined in
2678 ;; `org-element-string-restrictions'.
2680 ;; - DUALS is used to take care of keywords accepting a main and an
2681 ;; optional secondary values. For example "results" has its
2682 ;; source's name as the main value, and may have an hash string in
2683 ;; optional square brackets as the secondary one.
2685 ;; A keyword may belong to more than one category.
2686 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
2687 consed parsed duals)
2688 "Collect affiliated keywords before point.
2690 Optional argument KEY-RE is a regexp matching keywords, which
2691 puts matched keyword in group 1. It defaults to
2692 `org-element--affiliated-re'.
2694 TRANS-LIST is an alist where key is the keyword and value the
2695 property name it should be translated to, without the colons. It
2696 defaults to `org-element-keyword-translation-alist'.
2698 CONSED is a list of strings. Any keyword belonging to that list
2699 will have its value consed. The check is done after keyword
2700 translation. It defaults to `org-element-multiple-keywords'.
2702 PARSED is a list of strings. Any keyword member of this list
2703 will have its value parsed. The check is done after keyword
2704 translation. If a keyword is a member of both CONSED and PARSED,
2705 it's value will be a list of parsed strings. It defaults to
2706 `org-element-parsed-keywords'.
2708 DUALS is a list of strings. Any keyword member of this list can
2709 have two parts: one mandatory and one optional. Its value is
2710 a cons cell whose car is the former, and the cdr the latter. If
2711 a keyword is a member of both PARSED and DUALS, only the primary
2712 part will be parsed. It defaults to `org-element-dual-keywords'.
2714 Return a list whose car is the position at the first of them and
2715 cdr a plist of keywords and values."
2716 (save-excursion
2717 (let ((case-fold-search t)
2718 (key-re (or key-re org-element--affiliated-re))
2719 (trans-list (or trans-list org-element-keyword-translation-alist))
2720 (consed (or consed org-element-multiple-keywords))
2721 (parsed (or parsed org-element-parsed-keywords))
2722 (duals (or duals org-element-dual-keywords))
2723 output)
2724 (unless (bobp)
2725 (while (and (not (bobp))
2726 (progn (forward-line -1) (looking-at key-re)))
2727 (let* ((raw-kwd (downcase (or (match-string 2) (match-string 1))))
2728 ;; Apply translation to RAW-KWD. From there, KWD is
2729 ;; the official keyword.
2730 (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
2731 ;; If KWD is a dual keyword, find it secondary value.
2732 (dual-value (and (member kwd duals)
2733 (org-match-string-no-properties 3)))
2734 ;; Find main value for any keyword.
2735 (value (org-trim (buffer-substring-no-properties
2736 (match-end 0) (point-at-eol))))
2737 ;; Attribute a property name to KWD.
2738 (kwd-sym (and kwd (intern (concat ":" kwd)))))
2739 ;; Now set final shape for VALUE.
2740 (when (member kwd parsed)
2741 (setq value
2742 (org-element-parse-secondary-string
2743 value
2744 (cdr (assq 'keyword org-element-string-restrictions)))))
2745 (when (member kwd duals) (setq value (cons value dual-value)))
2746 (when (member kwd consed)
2747 (setq value (cons value (plist-get output kwd-sym))))
2748 ;; Eventually store the new value in OUTPUT.
2749 (setq output (plist-put output kwd-sym value))))
2750 (unless (looking-at key-re) (forward-line 1)))
2751 (list (point) output))))
2755 ;;; The Org Parser
2757 ;; The two major functions here are `org-element-parse-buffer', which
2758 ;; parses Org syntax inside the current buffer, taking into account
2759 ;; region, narrowing, or even visibility if specified, and
2760 ;; `org-element-parse-secondary-string', which parses objects within
2761 ;; a given string.
2763 ;; The (almost) almighty `org-element-map' allows to apply a function
2764 ;; on elements or objects matching some type, and accumulate the
2765 ;; resulting values. In an export situation, it also skips unneeded
2766 ;; parts of the parse tree, transparently walks into included files,
2767 ;; and maintain a list of local properties (i.e. those inherited from
2768 ;; parent headlines) for function's consumption.
2769 (defun org-element-parse-buffer (&optional granularity visible-only)
2770 "Recursively parse the buffer and return structure.
2771 If narrowing is in effect, only parse the visible part of the
2772 buffer.
2774 Optional argument GRANULARITY determines the depth of the
2775 recursion. It can be set to the following symbols:
2777 `headline' Only parse headlines.
2778 `greater-element' Don't recurse into greater elements. Thus,
2779 elements parsed are the top-level ones.
2780 `element' Parse everything but objects and plain text.
2781 `object' Parse the complete buffer (default).
2783 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
2784 elements.
2786 Assume buffer is in Org mode."
2787 (save-excursion
2788 (goto-char (point-min))
2789 (org-skip-whitespace)
2790 (nconc (list 'org-data nil)
2791 (org-element-parse-elements
2792 (point-at-bol) (point-max)
2793 nil nil granularity visible-only nil))))
2795 (defun org-element-parse-secondary-string (string restriction &optional buffer)
2796 "Recursively parse objects in STRING and return structure.
2798 RESTRICTION, when non-nil, is a symbol limiting the object types
2799 that will be looked after.
2801 Optional argument BUFFER indicates the buffer from where the
2802 secondary string was extracted. It is used to determine where to
2803 get extraneous information for an object \(i.e. when resolving
2804 a link or looking for a footnote definition\). It defaults to
2805 the current buffer."
2806 (with-temp-buffer
2807 (insert string)
2808 (org-element-parse-objects (point-min) (point-max) nil restriction)))
2810 (defun org-element-map (data types fun &optional info first-match)
2811 "Map a function on selected elements or objects.
2813 DATA is the parsed tree, as returned by, i.e,
2814 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
2815 of elements or objects types. FUN is the function called on the
2816 matching element or object. It must accept two arguments: the
2817 element or object itself and a plist holding contextual
2818 information.
2820 When optional argument INFO is non-nil, it should be a plist
2821 holding export options. In that case, parts of the parse tree
2822 not exportable according to that property list will be skipped
2823 and files included through a keyword will be visited.
2825 When optional argument FIRST-MATCH is non-nil, stop at the first
2826 match for which FUN doesn't return nil, and return that value.
2828 Nil values returned from FUN are ignored in the result."
2829 ;; Ensure TYPES is a list, even of one element.
2830 (unless (listp types) (setq types (list types)))
2831 ;; Recursion depth is determined by --CATEGORY.
2832 (let* ((--category
2833 (cond
2834 ((loop for type in types
2835 always (memq type org-element-greater-elements))
2836 'greater-elements)
2837 ((loop for type in types
2838 always (memq type org-element-all-elements))
2839 'elements)
2840 (t 'objects)))
2841 walk-tree ; For byte-compiler
2842 --acc
2843 (accumulate-maybe
2844 (function
2845 (lambda (--type types fun --blob --local)
2846 ;; Check if TYPE is matching among TYPES. If so, apply
2847 ;; FUN to --BLOB and accumulate return value
2848 ;; into --ACC. --LOCAL is the communication channel.
2849 (when (memq --type types)
2850 (let ((result (funcall fun --blob --local)))
2851 (cond ((not result))
2852 (first-match (throw 'first-match result))
2853 (t (push result --acc))))))))
2854 (walk-tree
2855 (function
2856 (lambda (--data --local)
2857 ;; Recursively walk DATA. --LOCAL, if non-nil, is
2858 ;; a plist holding contextual information.
2859 (mapc
2860 (lambda (--blob)
2861 (let ((--type (if (stringp --blob) 'plain-text (car --blob))))
2862 ;; Determine if a recursion into --BLOB is
2863 ;; possible and allowed.
2864 (cond
2865 ;; Element or object not exportable.
2866 ((and info (org-export-skip-p --blob info)))
2867 ;; Archived headline: Maybe apply fun on it, but
2868 ;; skip contents.
2869 ((and info
2870 (eq --type 'headline)
2871 (eq (plist-get info :with-archived-trees) 'headline)
2872 (org-element-get-property :archivedp --blob))
2873 (funcall accumulate-maybe --type types fun --blob --local))
2874 ;; At an include keyword: apply mapping to its
2875 ;; contents.
2876 ((and --local
2877 (eq --type 'keyword)
2878 (string=
2879 (downcase (org-element-get-property :key --blob))
2880 "include"))
2881 (funcall accumulate-maybe --type types fun --blob --local)
2882 (let* ((--data
2883 (org-export-parse-included-file --blob --local))
2884 (--value (org-element-get-property :value --blob))
2885 (--file
2886 (and (string-match "^\"\\(\\S-+\\)\"" --value)
2887 (match-string 1 --value))))
2888 (funcall
2889 walk-tree --data
2890 (org-combine-plists
2891 --local
2892 ;; Store full path of already included files
2893 ;; to avoid recursive file inclusion.
2894 `(:included-files
2895 ,(cons (expand-file-name --file)
2896 (plist-get --local :included-files))
2897 ;; Ensure that a top-level headline in the
2898 ;; included file becomes a direct child of
2899 ;; the current headline in the buffer.
2900 :headline-offset
2901 ,(- (+ (plist-get
2902 (plist-get --local :inherited-properties)
2903 :level)
2904 (or (plist-get --local :headline-offset) 0))
2905 (1- (org-export-get-min-level
2906 --data --local))))))))
2907 ;; Limiting recursion to greater elements, and --BLOB
2908 ;; isn't one.
2909 ((and (eq --category 'greater-elements)
2910 (not (memq --type org-element-greater-elements)))
2911 (funcall accumulate-maybe --type types fun --blob --local))
2912 ;; Limiting recursion to elements, and --BLOB only
2913 ;; contains objects.
2914 ((and (eq --category 'elements) (eq --type 'paragraph)))
2915 ;; No limitation on recursion, but --BLOB hasn't
2916 ;; got a recursive type.
2917 ((and (eq --category 'objects)
2918 (not (or (eq --type 'paragraph)
2919 (memq --type org-element-greater-elements)
2920 (memq --type org-element-recursive-objects))))
2921 (funcall accumulate-maybe --type types fun --blob --local))
2922 ;; Recursion is possible and allowed: Update local
2923 ;; information and move into --BLOB.
2924 (t (funcall accumulate-maybe --type types fun --blob --local)
2925 (funcall
2926 walk-tree --blob
2927 (and info (org-export-update-info --blob --local t)))))))
2928 (org-element-get-contents --data))))))
2929 (catch 'first-match
2930 (funcall walk-tree data info)
2931 ;; Return value in a proper order.
2932 (reverse --acc))))
2934 ;; The following functions are internal parts of the parser. The
2935 ;; first one, `org-element-parse-elements' acts at the element's
2936 ;; level. The second one, `org-element-parse-objects' applies on all
2937 ;; objects of a paragraph or a secondary string. It uses
2938 ;; `org-element-get-candidates' to optimize the search of the next
2939 ;; object in the buffer.
2941 ;; More precisely, that function looks for every allowed object type
2942 ;; first. Then, it discards failed searches, keeps further matches,
2943 ;; and searches again types matched behind point, for subsequent
2944 ;; calls. Thus, searching for a given type fails only once, and every
2945 ;; object is searched only once at top level (but sometimes more for
2946 ;; nested types).
2947 (defun org-element-parse-elements (beg end item structure granularity visible-only acc)
2948 "Parse ELEMENT with point at its beginning.
2950 If ITEM is non-nil, parse item wise instead of plain-list wise,
2951 using STRUCTURE as the current list structure.
2953 GRANULARITY determines the depth of the recursion. It can be set
2954 to the following symbols:
2956 `headline' Only parse headlines.
2957 `greater-element' Don't recurse into greater elements. Thus,
2958 elements parsed are the top-level ones.
2959 `element' Parse everything but objects and plain text.
2960 `object' or nil Parse the complete buffer.
2962 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
2963 greater elements.
2965 Elements are accumulated into ACC."
2966 (save-excursion
2967 (goto-char beg)
2968 ;; Shortcut when parsing only headlines.
2969 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
2970 (org-with-limited-levels (outline-next-heading)))
2971 ;; Main loop start.
2972 (while (and (< (point) end) (not (eobp)))
2973 (push
2974 ;; 1. If ITEM is toggled, point is at an item. Knowing that,
2975 ;; there's no need to go through `org-element-at-point'.
2976 (if item
2977 (let* ((element (org-element-item-parser structure))
2978 (cbeg (org-element-get-property :contents-begin element))
2979 (cend (org-element-get-property :contents-end element)))
2980 (goto-char (org-element-get-property :end element))
2981 ;; Narrow region to contents, so that item bullet don't
2982 ;; interfere with paragraph parsing.
2983 (save-restriction
2984 (narrow-to-region cbeg cend)
2985 (org-element-parse-elements
2986 cbeg cend nil structure granularity visible-only
2987 (reverse element))))
2988 ;; 2. When ITEM is nil, find current element's type and parse
2989 ;; it accordingly to its category.
2990 (let ((element (org-element-at-point nil structure)))
2991 (goto-char (org-element-get-property :end element))
2992 (cond
2993 ;; Case 1: ELEMENT is a footnote-definition. If
2994 ;; GRANURALITY allows parsing, use narrowing so that
2995 ;; footnote label don't interfere with paragraph
2996 ;; recognition.
2997 ((and (eq (car element) 'footnote-definition)
2998 (not (memq granularity '(headline greater-element))))
2999 (let ((cbeg (org-element-get-property :contents-begin element))
3000 (cend (org-element-get-property :contents-end element)))
3001 (save-restriction
3002 (narrow-to-region cbeg cend)
3003 (org-element-parse-elements
3004 cbeg cend nil structure granularity visible-only
3005 (reverse element)))))
3006 ;; Case 1: ELEMENT is a paragraph. Parse objects inside,
3007 ;; if GRANULARITY allows it.
3008 ((and (eq (car element) 'paragraph)
3009 (or (not granularity) (eq granularity 'object)))
3010 (org-element-parse-objects
3011 (org-element-get-property :contents-begin element)
3012 (org-element-get-property :contents-end element)
3013 (reverse element)
3014 nil))
3015 ;; Case 2: ELEMENT is recursive: parse it between
3016 ;; `contents-begin' and `contents-end'. If it's
3017 ;; a plain list, also switch to item mode. Make
3018 ;; sure GRANULARITY allows the recursion, or
3019 ;; ELEMENT is an headline, in which case going
3020 ;; inside is mandatory, in order to get sub-level
3021 ;; headings. If VISIBLE-ONLY is true and element
3022 ;; is hidden, do not recurse into it.
3023 ((and (memq (car element) org-element-greater-elements)
3024 (or (not granularity)
3025 (memq granularity '(element object))
3026 (eq (car element) 'headline))
3027 (not (and visible-only
3028 (org-element-get-property :hiddenp element))))
3029 (org-element-parse-elements
3030 (org-element-get-property :contents-begin element)
3031 (org-element-get-property :contents-end element)
3032 (eq (car element) 'plain-list)
3033 (org-element-get-property :structure element)
3034 granularity
3035 visible-only
3036 (reverse element)))
3037 ;; Case 3: Else, just accumulate ELEMENT, unless
3038 ;; GRANULARITY is set to `headline'.
3039 ((not (eq granularity 'headline)) element))))
3040 acc)
3041 (org-skip-whitespace))
3042 ;; Return result.
3043 (nreverse acc)))
3045 (defun org-element-parse-objects (beg end acc restriction)
3046 "Parse objects between BEG and END and return recursive structure.
3048 Objects are accumulated in ACC.
3050 RESTRICTION, when non-nil, is a list of object types which are
3051 allowed in the current object."
3052 (let ((get-next-object
3053 (function
3054 (lambda (cand)
3055 ;; Return the parsing function associated to the nearest
3056 ;; object among list of candidates CAND.
3057 (let ((pos (apply #'min (mapcar #'cdr cand))))
3058 (save-excursion
3059 (goto-char pos)
3060 (funcall
3061 (intern
3062 (format "org-element-%s-parser" (car (rassq pos cand))))))))))
3063 next-object candidates)
3064 (save-excursion
3065 (goto-char beg)
3066 (while (setq candidates (org-element-get-next-object-candidates
3067 end restriction candidates))
3068 (setq next-object (funcall get-next-object candidates))
3069 ;; 1. Text before any object.
3070 (let ((obj-beg (org-element-get-property :begin next-object)))
3071 (unless (= beg obj-beg)
3072 (push (buffer-substring-no-properties (point) obj-beg) acc)))
3073 ;; 2. Object...
3074 (let ((obj-end (org-element-get-property :end next-object))
3075 (cont-beg (org-element-get-property :contents-begin next-object)))
3076 (push (if (and (memq (car next-object) org-element-recursive-objects)
3077 cont-beg)
3078 ;; ... recursive. The CONT-BEG check is for
3079 ;; links, as some of them might not be recursive
3080 ;; (i.e. plain links).
3081 (save-restriction
3082 (narrow-to-region
3083 cont-beg
3084 (org-element-get-property :contents-end next-object))
3085 (org-element-parse-objects
3086 (point-min) (point-max) (reverse next-object)
3087 ;; Restrict allowed objects. This is the
3088 ;; intersection of current restriction and next
3089 ;; object's restriction.
3090 (let ((new-restr
3091 (cdr (assq (car next-object)
3092 org-element-object-restrictions))))
3093 (if (not restriction)
3094 new-restr
3095 (delq nil
3096 (mapcar (lambda (e)
3097 (and (memq e restriction) e))
3098 new-restr))))))
3099 ;; ... not recursive.
3100 next-object)
3101 acc)
3102 (goto-char obj-end)))
3103 ;; 3. Text after last object.
3104 (unless (= (point) end)
3105 (push (buffer-substring-no-properties (point) end) acc))
3106 ;; Result.
3107 (nreverse acc))))
3109 (defun org-element-get-next-object-candidates (limit restriction objects)
3110 "Return an alist of candidates for the next object.
3112 LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
3113 the possible object types.
3115 Return value is an alist whose car is position and cdr the object
3116 type, as a string. There is an association for the closest
3117 object of each type within RESTRICTION when non-nil, or for every
3118 type otherwise.
3120 OBJECTS is the previous candidates alist."
3121 (let ((restriction (or restriction org-element-all-successors))
3122 next-candidates types-to-search)
3123 ;; If no previous result, search every object type in RESTRICTION.
3124 ;; Otherwise, keep potential candidates (old objects located after
3125 ;; point) and ask to search again those which had matched before.
3126 (if objects
3127 (mapc (lambda (obj)
3128 (if (< (cdr obj) (point))
3129 (push (car obj) types-to-search)
3130 (push obj next-candidates)))
3131 objects)
3132 (setq types-to-search restriction))
3133 ;; Call the appropriate "get-next" function for each type to
3134 ;; search and accumulate matches.
3135 (mapc
3136 (lambda (type)
3137 (let* ((successor-fun
3138 (intern
3139 (format "org-element-%s-successor"
3140 (or (cdr (assq type org-element-object-successor-alist))
3141 type))))
3142 (obj (funcall successor-fun limit)))
3143 (and obj (push obj next-candidates))))
3144 types-to-search)
3145 ;; Return alist.
3146 next-candidates))
3150 ;;; Towards A Bijective Process
3152 ;; The parse tree obtained with `org-element-parse-buffer' is really
3153 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3154 ;; interpreted and expanded into a string with canonical Org
3155 ;; syntax. Hence `org-element-interpret-data'.
3157 ;; Data parsed from secondary strings, whose shape is slightly
3158 ;; different than the standard parse tree, is expanded with the
3159 ;; equivalent function `org-element-interpret-secondary'.
3161 ;; Both functions rely internally on
3162 ;; `org-element-interpret--affiliated-keywords'.
3163 (defun org-element-interpret-data (data &optional genealogy previous)
3164 "Interpret a parse tree representing Org data.
3166 DATA is the parse tree to interpret.
3168 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3169 calls:
3170 GENEALOGY is the list of its parents types.
3171 PREVIOUS is the type of the element or object at the same level
3172 interpreted before.
3174 Return Org syntax as a string."
3175 (mapconcat
3176 (lambda (blob)
3177 ;; BLOB can be an element, an object, a string, or nil.
3178 (cond
3179 ((not blob) nil)
3180 ((equal blob "") nil)
3181 ((stringp blob) blob)
3183 (let* ((type (car blob))
3184 (interpreter
3185 (if (eq type 'org-data)
3186 'identity
3187 (intern (format "org-element-%s-interpreter" type))))
3188 (contents
3189 (cond
3190 ;; Full Org document.
3191 ((eq type 'org-data)
3192 (org-element-interpret-data blob genealogy previous))
3193 ;; Recursive objects.
3194 ((memq type org-element-recursive-objects)
3195 (org-element-interpret-data
3196 blob (cons type genealogy) nil))
3197 ;; Recursive elements.
3198 ((memq type org-element-greater-elements)
3199 (org-element-normalize-string
3200 (org-element-interpret-data
3201 blob (cons type genealogy) nil)))
3202 ;; Paragraphs.
3203 ((eq type 'paragraph)
3204 (let ((paragraph
3205 (org-element-normalize-contents
3206 blob
3207 ;; When normalizing contents of an item,
3208 ;; ignore first line's indentation.
3209 (and (not previous)
3210 (memq (car genealogy)
3211 '(footnote-definiton item))))))
3212 (org-element-interpret-data
3213 paragraph (cons type genealogy) nil)))))
3214 (results (funcall interpreter blob contents)))
3215 ;; Update PREVIOUS.
3216 (setq previous type)
3217 ;; Build white spaces.
3218 (cond
3219 ((eq type 'org-data) results)
3220 ((memq type org-element-all-elements)
3221 (concat
3222 (org-element-interpret--affiliated-keywords blob)
3223 (org-element-normalize-string results)
3224 (make-string (org-element-get-property :post-blank blob) 10)))
3225 (t (concat
3226 results
3227 (make-string
3228 (org-element-get-property :post-blank blob) 32))))))))
3229 (org-element-get-contents data) ""))
3231 (defun org-element-interpret-secondary (secondary)
3232 "Interpret SECONDARY string as Org syntax.
3234 SECONDARY-STRING is a nested list as returned by
3235 `org-element-parse-secondary-string'.
3237 Return interpreted string."
3238 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3239 (let ((s (if (listp secondary) secondary (list secondary))))
3240 (org-element-interpret-data `(org-data nil ,@s) nil nil)))
3242 ;; Both functions internally use `org-element--affiliated-keywords'.
3244 (defun org-element-interpret--affiliated-keywords (element)
3245 "Return ELEMENT's affiliated keywords as Org syntax.
3246 If there is no affiliated keyword, return the empty string."
3247 (let ((keyword-to-org
3248 (function
3249 (lambda (key value)
3250 (let (dual)
3251 (when (member key org-element-dual-keywords)
3252 (setq dual (cdr value) value (car value)))
3253 (concat "#+" key (and dual (format "[%s]" dual)) ": "
3254 (if (member key org-element-parsed-keywords)
3255 (org-element-interpret-secondary value)
3256 value)
3257 "\n"))))))
3258 (mapconcat
3259 (lambda (key)
3260 (let ((value (org-element-get-property (intern (concat ":" key)) element)))
3261 (when value
3262 (if (member key org-element-multiple-keywords)
3263 (mapconcat (lambda (line)
3264 (funcall keyword-to-org key line))
3265 value "")
3266 (funcall keyword-to-org key value)))))
3267 ;; Remove translated keywords.
3268 (delq nil
3269 (mapcar
3270 (lambda (key)
3271 (and (not (assoc key org-element-keyword-translation-alist)) key))
3272 org-element-affiliated-keywords))
3273 "")))
3275 ;; Because interpretation of the parse tree must return the same
3276 ;; number of blank lines between elements and the same number of white
3277 ;; space after objects, some special care must be given to white
3278 ;; spaces.
3280 ;; The first function, `org-element-normalize-string', ensures any
3281 ;; string different from the empty string will end with a single
3282 ;; newline character.
3284 ;; The second function, `org-element-normalize-contents', removes
3285 ;; global indentation from the contents of the current element.
3286 (defun org-element-normalize-string (s)
3287 "Ensure string S ends with a single newline character.
3289 If S isn't a string return it unchanged. If S is the empty
3290 string, return it. Otherwise, return a new string with a single
3291 newline character at its end."
3292 (cond
3293 ((not (stringp s)) s)
3294 ((string= "" s) "")
3295 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
3296 (replace-match "\n" nil nil s)))))
3298 (defun org-element-normalize-contents (element &optional ignore-first)
3299 "Normalize plain text in ELEMENT's contents.
3301 ELEMENT must only contain plain text and objects.
3303 The following changes are applied to plain text:
3304 - Remove global indentation, preserving relative one.
3305 - Untabify it.
3307 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3308 indentation to compute maximal common indentation.
3310 Return the normalized element."
3311 (nconc
3312 (list (car element) (nth 1 element))
3313 (let ((contents (org-element-get-contents element)))
3314 (cond
3315 ((and (not ignore-first) (not (stringp (car contents)))) contents)
3317 (catch 'exit
3318 ;; 1. Remove tabs from each string in CONTENTS. Get maximal
3319 ;; common indentation (MCI) along the way.
3320 (let* ((ind-list (unless ignore-first
3321 (list (org-get-string-indentation (car contents)))))
3322 (contents
3323 (mapcar (lambda (object)
3324 (if (not (stringp object))
3325 object
3326 (let ((start 0)
3327 (object (org-remove-tabs object)))
3328 (while (string-match "\n\\( *\\)" object start)
3329 (setq start (match-end 0))
3330 (push (length (match-string 1 object))
3331 ind-list))
3332 object)))
3333 contents))
3334 (mci (if ind-list
3335 (apply 'min ind-list)
3336 (throw 'exit contents))))
3337 ;; 2. Remove that indentation from CONTENTS. First string
3338 ;; must be treated differently because it's the only one
3339 ;; whose indentation doesn't happen after a newline
3340 ;; character.
3341 (let ((first-obj (car contents)))
3342 (unless (or (not (stringp first-obj)) ignore-first)
3343 (setq contents
3344 (cons (replace-regexp-in-string
3345 (format "\\` \\{%d\\}" mci) "" first-obj)
3346 (cdr contents)))))
3347 (mapcar (lambda (object)
3348 (if (not (stringp object))
3349 object
3350 (replace-regexp-in-string
3351 (format "\n \\{%d\\}" mci) "\n" object)))
3352 contents))))))))
3356 ;;; The Toolbox
3358 ;; Once the structure of an Org file is well understood, it's easy to
3359 ;; implement some replacements for `forward-paragraph'
3360 ;; `backward-paragraph', namely `org-element-forward' and
3361 ;; `org-element-backward'.
3363 ;; Also, `org-transpose-elements' mimics the behaviour of
3364 ;; `transpose-words', at the element's level, whereas
3365 ;; `org-element-drag-forward', `org-element-drag-backward', and
3366 ;; `org-element-up' generalize, respectively, functions
3367 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3369 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3370 ;; smartly remove global indentation from buffer, making it possible
3371 ;; to use Org indent mode on a file created with hard indentation.
3373 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3374 ;; internally by some of the previously cited tools.
3375 (defsubst org-element-nested-p (elem-A elem-B)
3376 "Non-nil when elements ELEM-A and ELEM-B are nested."
3377 (let ((beg-A (org-element-get-property :begin elem-A))
3378 (beg-B (org-element-get-property :begin elem-B))
3379 (end-A (org-element-get-property :end elem-A))
3380 (end-B (org-element-get-property :end elem-B)))
3381 (or (and (>= beg-A beg-B) (<= end-A end-B))
3382 (and (>= beg-B beg-A) (<= end-B end-A)))))
3384 (defun org-element-swap-A-B (elem-A elem-B)
3385 "Swap elements ELEM-A and ELEM-B.
3387 Leave point at the end of ELEM-A.
3389 Assume ELEM-A is before ELEM-B and that they are not nested."
3390 (goto-char (org-element-get-property :begin elem-A))
3391 (let* ((beg-B (org-element-get-property :begin elem-B))
3392 (end-B-no-blank (save-excursion
3393 (goto-char (org-element-get-property :end elem-B))
3394 (skip-chars-backward " \r\t\n")
3395 (forward-line)
3396 (point)))
3397 (beg-A (org-element-get-property :begin elem-A))
3398 (end-A-no-blank (save-excursion
3399 (goto-char (org-element-get-property :end elem-A))
3400 (skip-chars-backward " \r\t\n")
3401 (forward-line)
3402 (point)))
3403 (body-A (buffer-substring beg-A end-A-no-blank))
3404 (body-B (buffer-substring beg-B end-B-no-blank))
3405 (between-A-B (buffer-substring end-A-no-blank beg-B)))
3406 (delete-region beg-A end-B-no-blank)
3407 (insert body-B between-A-B body-A)
3408 (goto-char (org-element-get-property :end elem-B))))
3410 (defun org-element-backward ()
3411 "Move backward by one element."
3412 (interactive)
3413 (let* ((opoint (point))
3414 (element (org-element-at-point))
3415 (start-el-beg (org-element-get-property :begin element)))
3416 ;; At an headline. The previous element is the previous sibling,
3417 ;; or the parent if any.
3418 (cond
3419 ;; Already at the beginning of the current element: move to the
3420 ;; beginning of the previous one.
3421 ((= opoint start-el-beg)
3422 (forward-line -1)
3423 (skip-chars-backward " \r\t\n")
3424 (let* ((prev-element (org-element-at-point))
3425 (itemp (org-in-item-p))
3426 (struct (and itemp
3427 (save-excursion (goto-char itemp)
3428 (org-list-struct)))))
3429 ;; When moving into a new list, go directly at the
3430 ;; beginning of the top list structure.
3431 (if (and itemp (<= (org-list-get-bottom-point struct) opoint))
3432 (progn
3433 (goto-char (org-list-get-top-point struct))
3434 (goto-char (org-element-get-property
3435 :begin (org-element-at-point))))
3436 (goto-char (org-element-get-property :begin prev-element))))
3437 (while (org-truely-invisible-p) (org-element-up)))
3438 ;; Else, move at the element beginning. One exception: if point
3439 ;; was in the blank lines after the end of a list, move directly
3440 ;; to the top item.
3442 (let (struct itemp)
3443 (if (and (setq itemp (org-in-item-p))
3444 (<= (org-list-get-bottom-point
3445 (save-excursion (goto-char itemp)
3446 (setq struct (org-list-struct))))
3447 opoint))
3448 (progn (goto-char (org-list-get-top-point struct))
3449 (goto-char (org-element-get-property
3450 :begin (org-element-at-point))))
3451 (goto-char start-el-beg)))))))
3453 (defun org-element-drag-backward ()
3454 "Drag backward element at point."
3455 (interactive)
3456 (let* ((pos (point))
3457 (elem (org-element-at-point)))
3458 (when (= (progn (goto-char (point-min))
3459 (org-skip-whitespace)
3460 (point-at-bol))
3461 (org-element-get-property :end elem))
3462 (error "Cannot drag element backward"))
3463 (goto-char (org-element-get-property :begin elem))
3464 (org-element-backward)
3465 (let ((prev-elem (org-element-at-point)))
3466 (when (or (org-element-nested-p elem prev-elem)
3467 (and (eq (car elem) 'headline)
3468 (not (eq (car prev-elem) 'headline))))
3469 (goto-char pos)
3470 (error "Cannot drag element backward"))
3471 ;; Compute new position of point: it's shifted by PREV-ELEM
3472 ;; body's length.
3473 (let ((size-prev (- (org-element-get-property :end prev-elem)
3474 (org-element-get-property :begin prev-elem))))
3475 (org-element-swap-A-B prev-elem elem)
3476 (goto-char (- pos size-prev))))))
3478 (defun org-element-drag-forward ()
3479 "Move forward element at point."
3480 (interactive)
3481 (let* ((pos (point))
3482 (elem (org-element-at-point)))
3483 (when (= (point-max) (org-element-get-property :end elem))
3484 (error "Cannot drag element forward"))
3485 (goto-char (org-element-get-property :end elem))
3486 (let ((next-elem (org-element-at-point)))
3487 (when (or (org-element-nested-p elem next-elem)
3488 (and (eq (car next-elem) 'headline)
3489 (not (eq (car elem) 'headline))))
3490 (goto-char pos)
3491 (error "Cannot drag element forward"))
3492 ;; Compute new position of point: it's shifted by NEXT-ELEM
3493 ;; body's length (without final blanks) and by the length of
3494 ;; blanks between ELEM and NEXT-ELEM.
3495 (let ((size-next (- (save-excursion
3496 (goto-char (org-element-get-property :end next-elem))
3497 (skip-chars-backward " \r\t\n")
3498 (forward-line)
3499 (point))
3500 (org-element-get-property :begin next-elem)))
3501 (size-blank (- (org-element-get-property :end elem)
3502 (save-excursion
3503 (goto-char (org-element-get-property :end elem))
3504 (skip-chars-backward " \r\t\n")
3505 (forward-line)
3506 (point)))))
3507 (org-element-swap-A-B elem next-elem)
3508 (goto-char (+ pos size-next size-blank))))))
3510 (defun org-element-forward ()
3511 "Move forward by one element."
3512 (interactive)
3513 (beginning-of-line)
3514 (cond ((eobp) (error "Cannot move further down"))
3515 ((looking-at "[ \t]*$")
3516 (org-skip-whitespace)
3517 (goto-char (if (eobp) (point) (point-at-bol))))
3519 (let ((element (org-element-at-point t))
3520 (origin (point)))
3521 (cond
3522 ;; At an item: Either move to the next element inside, or
3523 ;; to its end if it's hidden.
3524 ((eq (car element) 'item)
3525 (if (org-element-get-property :hiddenp element)
3526 (goto-char (org-element-get-property :end element))
3527 (end-of-line)
3528 (re-search-forward org-element-paragraph-separate nil t)
3529 (org-skip-whitespace)
3530 (beginning-of-line)))
3531 ;; At a recursive element: Either move inside, or if it's
3532 ;; hidden, move to its end.
3533 ((memq (car element) org-element-greater-elements)
3534 (let ((cbeg (org-element-get-property :contents-begin element)))
3535 (goto-char
3536 (if (or (org-element-get-property :hiddenp element)
3537 (> origin cbeg))
3538 (org-element-get-property :end element)
3539 cbeg))))
3540 ;; Else: move to the current element's end.
3541 (t (goto-char (org-element-get-property :end element))))))))
3543 (defun org-element-mark-element ()
3544 "Put point at beginning of this element, mark at end.
3546 Interactively, if this command is repeated or (in Transient Mark
3547 mode) if the mark is active, it marks the next element after the
3548 ones already marked."
3549 (interactive)
3550 (let (deactivate-mark)
3551 (if (or (and (eq last-command this-command) (mark t))
3552 (and transient-mark-mode mark-active))
3553 (set-mark
3554 (save-excursion
3555 (goto-char (mark))
3556 (goto-char (org-element-get-property :end (org-element-at-point)))))
3557 (let ((element (org-element-at-point)))
3558 (end-of-line)
3559 (push-mark (org-element-get-property :end element) t t)
3560 (goto-char (org-element-get-property :begin element))))))
3562 (defun org-narrow-to-element ()
3563 "Narrow buffer to current element."
3564 (interactive)
3565 (let ((elem (org-element-at-point)))
3566 (cond
3567 ((eq (car elem) 'headline)
3568 (narrow-to-region
3569 (org-element-get-property :begin elem)
3570 (org-element-get-property :end elem)))
3571 ((memq (car elem) org-element-greater-elements)
3572 (narrow-to-region
3573 (org-element-get-property :contents-begin elem)
3574 (org-element-get-property :contents-end elem)))
3576 (narrow-to-region
3577 (org-element-get-property :begin elem)
3578 (org-element-get-property :end elem))))))
3580 (defun org-transpose-elements ()
3581 "Transpose current and previous elements, keeping blank lines between.
3582 Point is moved after both elements."
3583 (interactive)
3584 (org-skip-whitespace)
3585 (let ((pos (point))
3586 (cur (org-element-at-point)))
3587 (when (= (save-excursion (goto-char (point-min))
3588 (org-skip-whitespace)
3589 (point-at-bol))
3590 (org-element-get-property :begin cur))
3591 (error "No previous element"))
3592 (goto-char (org-element-get-property :begin cur))
3593 (forward-line -1)
3594 (let ((prev (org-element-at-point)))
3595 (when (org-element-nested-p cur prev)
3596 (goto-char pos)
3597 (error "Cannot transpose nested elements"))
3598 (org-element-swap-A-B prev cur))))
3600 (defun org-element-unindent-buffer ()
3601 "Un-indent the visible part of the buffer.
3602 Relative indentation \(between items, inside blocks, etc.\) isn't
3603 modified."
3604 (interactive)
3605 (unless (eq major-mode 'org-mode)
3606 (error "Cannot un-indent a buffer not in Org mode"))
3607 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
3608 unindent-tree ; For byte-compiler.
3609 (unindent-tree
3610 (function
3611 (lambda (contents)
3612 (mapc (lambda (element)
3613 (if (eq (car element) 'headline)
3614 (funcall unindent-tree
3615 (org-element-get-contents element))
3616 (save-excursion
3617 (save-restriction
3618 (narrow-to-region
3619 (org-element-get-property :begin element)
3620 (org-element-get-property :end element))
3621 (org-do-remove-indentation)))))
3622 (reverse contents))))))
3623 (funcall unindent-tree (org-element-get-contents parse-tree))))
3625 (defun org-element-up ()
3626 "Move to upper element.
3627 Return position at the beginning of the upper element."
3628 (interactive)
3629 (let ((opoint (point)) elem)
3630 (cond
3631 ((bobp) (error "No surrounding element"))
3632 ((org-with-limited-levels (org-at-heading-p))
3633 (or (org-up-heading-safe) (error "No surronding element")))
3634 ((and (org-at-item-p)
3635 (setq elem (org-element-at-point))
3636 (let* ((top-list-p (zerop (org-element-get-property :level elem))))
3637 (unless top-list-p
3638 ;; If parent is bound to be in the same list as the
3639 ;; original point, move to that parent.
3640 (let ((struct (org-element-get-property :structure elem)))
3641 (goto-char
3642 (org-list-get-parent
3643 (point-at-bol) struct (org-list-parents-alist struct))))))))
3645 (let* ((elem (or elem (org-element-at-point)))
3646 (end (save-excursion
3647 (goto-char (org-element-get-property :end elem))
3648 (skip-chars-backward " \r\t\n")
3649 (forward-line)
3650 (point)))
3651 prev-elem)
3652 (goto-char (org-element-get-property :begin elem))
3653 (forward-line -1)
3654 (while (and (< (org-element-get-property
3655 :end (setq prev-elem (org-element-at-point)))
3656 end)
3657 (not (bobp)))
3658 (goto-char (org-element-get-property :begin prev-elem))
3659 (forward-line -1))
3660 (if (and (bobp) (< (org-element-get-property :end prev-elem) end))
3661 (progn (goto-char opoint)
3662 (error "No surrounding element"))
3663 (goto-char (org-element-get-property :begin prev-elem))))))))
3666 (provide 'org-element)
3667 ;;; org-element.el ends here