org-element: White space fixing and trivial refactoring
[org-mode/org-mode-NeilSmithlineMods.git] / contrib / lisp / org-element.el
blob184ff998c735dc1e7b5ac49fb8f032c0406d81cb
1 ;;; org-element.el --- Parser And Applications for Org syntax
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; This file is not part of GNU Emacs.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Org syntax can be divided into three categories: "Greater
26 ;; elements", "Elements" and "Objects".
28 ;; An object can be defined anywhere on a line. It may span over more
29 ;; than a line but never contains a blank one. Objects belong to the
30 ;; following types: `emphasis', `entity', `export-snippet',
31 ;; `footnote-reference', `inline-babel-call', `inline-src-block',
32 ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
33 ;; `statistics-cookie', `subscript', `superscript', `target',
34 ;; `time-stamp' and `verbatim'.
36 ;; An element always starts and ends at the beginning of a line. The
37 ;; only element's type containing objects is called a `paragraph'.
38 ;; Other types are: `comment', `comment-block', `example-block',
39 ;; `export-block', `fixed-width', `horizontal-rule', `keyword',
40 ;; `latex-environment', `babel-call', `property-drawer',
41 ;; `quote-section', `src-block', `table' and `verse-block'.
43 ;; Elements containing paragraphs are called greater elements.
44 ;; Concerned types are: `center-block', `drawer', `dynamic-block',
45 ;; `footnote-definition', `headline', `inlinetask', `item',
46 ;; `plain-list', `quote-block', `section' and `special-block'.
48 ;; Greater elements (excepted `headline', `item' and `section' types)
49 ;; and elements (excepted `keyword', `babel-call', and
50 ;; `property-drawer' types) can have a fixed set of keywords as
51 ;; attributes. Those are called "affiliated keywords", to distinguish
52 ;; them from others keywords, which are full-fledged elements. In
53 ;; particular, the "name" affiliated keyword allows to label almost
54 ;; any element in an Org buffer.
56 ;; Notwithstanding affiliated keywords, each greater element, element
57 ;; and object has a fixed set of properties attached to it. Among
58 ;; them, three are shared by all types: `:begin' and `:end', which
59 ;; refer to the beginning and ending buffer positions of the
60 ;; considered element or object, and `:post-blank', which holds the
61 ;; number of blank lines, or white spaces, at its end.
63 ;; Some elements also have special properties whose value can hold
64 ;; objects themselves (i.e. an item tag, an headline name, a table
65 ;; cell). Such values are called "secondary strings".
67 ;; Lisp-wise, an element or an object can be represented as a list.
68 ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
69 ;; TYPE is a symbol describing the Org element or object.
70 ;; PROPERTIES is the property list attached to it. See docstring of
71 ;; appropriate parsing function to get an exhaustive
72 ;; list.
73 ;; CONTENTS is a list of elements, objects or raw strings contained
74 ;; in the current element or object, when applicable.
76 ;; An Org buffer is a nested list of such elements and objects, whose
77 ;; type is `org-data' and properties is nil.
79 ;; The first part of this file implements a parser and an interpreter
80 ;; for each type of Org syntax.
82 ;; The next two parts introduce two accessors and a function
83 ;; retrieving the smallest element containing point (respectively
84 ;; `org-element-get-property', `org-element-get-contents' and
85 ;; `org-element-at-point').
87 ;; The following part creates a fully recursive buffer parser. It
88 ;; also provides a tool to map a function to elements or objects
89 ;; matching some criteria in the parse tree. Functions of interest
90 ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
91 ;; extent, `org-element-parse-secondary-string'.
93 ;; The penultimate part is the cradle of an interpreter for the
94 ;; obtained parse tree: `org-element-interpret-data' (and its
95 ;; relative, `org-element-interpret-secondary').
97 ;; The library ends by furnishing a set of interactive tools for
98 ;; element's navigation and manipulation.
101 ;;; Code:
103 (eval-when-compile (require 'cl))
104 (require 'org)
105 (declare-function org-inlinetask-goto-end "org-inlinetask" ())
108 ;;; Greater elements
110 ;; For each greater element type, we define a parser and an
111 ;; interpreter.
113 ;; A parser (`item''s excepted) accepts no argument and represents the
114 ;; element or object as the list described above. An interpreter
115 ;; accepts two arguments: the list representation of the element or
116 ;; object, and its contents. The latter may be nil, depending on the
117 ;; element or object considered. It returns the appropriate Org
118 ;; syntax, as a string.
120 ;; Parsing functions must follow the naming convention:
121 ;; org-element-TYPE-parser, where TYPE is greater element's type, as
122 ;; defined in `org-element-greater-elements'.
124 ;; Similarly, interpreting functions must follow the naming
125 ;; convention: org-element-TYPE-interpreter.
127 ;; With the exception of `headline' and `item' types, greater elements
128 ;; cannot contain other greater elements of their own type.
130 ;; Beside implementing a parser and an interpreter, adding a new
131 ;; greater element requires to tweak `org-element-guess-type'.
132 ;; Moreover, the newly defined type must be added to both
133 ;; `org-element-all-elements' and `org-element-greater-elements'.
136 ;;;; Center Block
138 (defun org-element-center-block-parser ()
139 "Parse a center block.
141 Return a list whose car is `center-block' and cdr is a plist
142 containing `:begin', `:end', `:hiddenp', `:contents-begin',
143 `:contents-end' and `:post-blank' keywords.
145 Assume point is at beginning or end of the block."
146 (save-excursion
147 (let* ((case-fold-search t)
148 (keywords (progn
149 (end-of-line)
150 (re-search-backward
151 (concat "^[ \t]*#\\+begin_center") nil t)
152 (org-element-collect-affiliated-keywords)))
153 (begin (car keywords))
154 (contents-begin (progn (forward-line) (point)))
155 (hidden (org-truely-invisible-p))
156 (contents-end (progn (re-search-forward
157 (concat "^[ \t]*#\\+end_center") nil t)
158 (point-at-bol)))
159 (pos-before-blank (progn (forward-line) (point)))
160 (end (progn (org-skip-whitespace)
161 (if (eobp) (point) (point-at-bol)))))
162 `(center-block
163 (:begin ,begin
164 :end ,end
165 :hiddenp ,hidden
166 :contents-begin ,contents-begin
167 :contents-end ,contents-end
168 :post-blank ,(count-lines pos-before-blank end)
169 ,@(cadr keywords))))))
171 (defun org-element-center-block-interpreter (center-block contents)
172 "Interpret CENTER-BLOCK element as Org syntax.
173 CONTENTS is the contents of the element."
174 (format "#+begin_center\n%s#+end_center" contents))
177 ;;;; Drawer
179 (defun org-element-drawer-parser ()
180 "Parse a drawer.
182 Return a list whose car is `drawer' and cdr is a plist containing
183 `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
184 `:contents-end' and `:post-blank' keywords.
186 Assume point is at beginning of drawer."
187 (save-excursion
188 (let* ((case-fold-search t)
189 (name (progn (looking-at org-drawer-regexp)
190 (org-match-string-no-properties 1)))
191 (keywords (org-element-collect-affiliated-keywords))
192 (begin (car keywords))
193 (contents-begin (progn (forward-line) (point)))
194 (hidden (org-truely-invisible-p))
195 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t)
196 (point-at-bol)))
197 (pos-before-blank (progn (forward-line) (point)))
198 (end (progn (org-skip-whitespace)
199 (if (eobp) (point) (point-at-bol)))))
200 `(drawer
201 (:begin ,begin
202 :end ,end
203 :drawer-name ,name
204 :hiddenp ,hidden
205 :contents-begin ,contents-begin
206 :contents-end ,contents-end
207 :post-blank ,(count-lines pos-before-blank end)
208 ,@(cadr keywords))))))
210 (defun org-element-drawer-interpreter (drawer contents)
211 "Interpret DRAWER element as Org syntax.
212 CONTENTS is the contents of the element."
213 (format ":%s:\n%s:END:"
214 (org-element-get-property :drawer-name drawer)
215 contents))
218 ;;;; Dynamic Block
220 (defun org-element-dynamic-block-parser ()
221 "Parse a dynamic block.
223 Return a list whose car is `dynamic-block' and cdr is a plist
224 containing `:block-name', `:begin', `:end', `:hiddenp',
225 `:contents-begin', `:contents-end', `:arguments' and
226 `:post-blank' keywords.
228 Assume point is at beginning of dynamic block."
229 (save-excursion
230 (let* ((case-fold-search t)
231 (name (progn (looking-at org-dblock-start-re)
232 (org-match-string-no-properties 1)))
233 (arguments (org-match-string-no-properties 3))
234 (keywords (org-element-collect-affiliated-keywords))
235 (begin (car keywords))
236 (contents-begin (progn (forward-line) (point)))
237 (hidden (org-truely-invisible-p))
238 (contents-end (progn (re-search-forward org-dblock-end-re nil t)
239 (point-at-bol)))
240 (pos-before-blank (progn (forward-line) (point)))
241 (end (progn (org-skip-whitespace)
242 (if (eobp) (point) (point-at-bol)))))
243 (list 'dynamic-block
244 `(:begin ,begin
245 :end ,end
246 :block-name ,name
247 :arguments ,arguments
248 :hiddenp ,hidden
249 :contents-begin ,contents-begin
250 :contents-end ,contents-end
251 :post-blank ,(count-lines pos-before-blank end)
252 ,@(cadr keywords))))))
254 (defun org-element-dynamic-block-interpreter (dynamic-block contents)
255 "Interpret DYNAMIC-BLOCK element as Org syntax.
256 CONTENTS is the contents of the element."
257 (format "#+BEGIN: %s%s\n%s#+END:"
258 (org-element-get-property :block-name dynamic-block)
259 (let ((args (org-element-get-property :arguments dynamic-block)))
260 (and arg (concat " " args)))
261 contents))
264 ;;;; Footnote Definition
266 (defun org-element-footnote-definition-parser ()
267 "Parse a footnote definition.
269 Return a list whose car is `footnote-definition' and cdr is
270 a plist containing `:label', `:begin' `:end', `:contents-begin',
271 `:contents-end' and `:post-blank' keywords."
272 (save-excursion
273 (let* ((f-def (org-footnote-at-definition-p))
274 (label (car f-def))
275 (keywords (progn (goto-char (nth 1 f-def))
276 (org-element-collect-affiliated-keywords)))
277 (begin (car keywords))
278 (contents-begin (progn (looking-at (concat "\\[" label "\\]"))
279 (goto-char (match-end 0))
280 (org-skip-whitespace)
281 (point)))
282 (end (goto-char (nth 2 f-def)))
283 (contents-end (progn (skip-chars-backward " \r\t\n")
284 (forward-line)
285 (point))))
286 `(footnote-definition
287 (:label ,label
288 :begin ,begin
289 :end ,end
290 :contents-begin ,contents-begin
291 :contents-end ,contents-end
292 :post-blank ,(count-lines contents-end end)
293 ,@(cadr keywords))))))
295 (defun org-element-footnote-definition-interpreter (footnote-definition contents)
296 "Interpret FOOTNOTE-DEFINITION element as Org syntax.
297 CONTENTS is the contents of the footnote-definition."
298 (concat (format "[%s]" (org-element-get-property :label footnote-definition))
300 contents))
303 ;;;; Headline
305 (defun org-element-headline-parser ()
306 "Parse an headline.
308 Return a list whose car is `headline' and cdr is a plist
309 containing `:raw-value', `:title', `:begin', `:end',
310 `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
311 `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
312 `:scheduled', `:deadline', `:timestamp', `:clock', `:category',
313 `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
314 keywords.
316 The plist also contains any property set in the property drawer,
317 with its name in lowercase, the underscores replaced with hyphens
318 and colons at the beginning (i.e. `:custom-id').
320 Assume point is at beginning of the headline."
321 (save-excursion
322 (let* ((components (org-heading-components))
323 (level (nth 1 components))
324 (todo (nth 2 components))
325 (todo-type (and todo
326 (if (member todo org-done-keywords) 'done 'todo)))
327 (tags (nth 5 components))
328 (raw-value (nth 4 components))
329 (quotedp (string-match (format "^%s +" org-quote-string) raw-value))
330 (commentedp (string-match
331 (format "^%s +" org-comment-string) raw-value))
332 (archivedp (and tags
333 (string-match (format ":%s:" org-archive-tag) tags)))
334 (footnote-section-p (and org-footnote-section
335 (string= org-footnote-section raw-value)))
336 (standard-props (let (plist)
337 (mapc
338 (lambda (p)
339 (let ((p-name (downcase (car p))))
340 (while (string-match "_" p-name)
341 (setq p-name
342 (replace-match "-" nil nil p-name)))
343 (setq p-name (intern (concat ":" p-name)))
344 (setq plist
345 (plist-put plist p-name (cdr p)))))
346 (org-entry-properties nil 'standard))
347 plist))
348 (time-props (org-entry-properties nil 'special "CLOCK"))
349 (scheduled (cdr (assoc "SCHEDULED" time-props)))
350 (deadline (cdr (assoc "DEADLINE" time-props)))
351 (clock (cdr (assoc "CLOCK" time-props)))
352 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
353 (begin (point))
354 (pos-after-head (save-excursion (forward-line) (point)))
355 (contents-begin (save-excursion (forward-line)
356 (org-skip-whitespace)
357 (if (eobp) (point) (point-at-bol))))
358 (hidden (save-excursion (forward-line) (org-truely-invisible-p)))
359 (end (progn (goto-char (org-end-of-subtree t t))))
360 (contents-end (progn (skip-chars-backward " \r\t\n")
361 (forward-line)
362 (point)))
363 title)
364 ;; Clean RAW-VALUE from any quote or comment string.
365 (when (or quotedp commentedp)
366 (setq raw-value
367 (replace-regexp-in-string
368 (concat "\\(" org-quote-string "\\|" org-comment-string "\\) +")
370 raw-value)))
371 ;; Clean TAGS from archive tag, if any.
372 (when archivedp
373 (setq tags
374 (and (not (string= tags (format ":%s:" org-archive-tag)))
375 (replace-regexp-in-string
376 (concat org-archive-tag ":") "" tags)))
377 (when (string= tags ":") (setq tags nil)))
378 ;; Then get TITLE.
379 (setq title (org-element-parse-secondary-string
380 raw-value
381 (cdr (assq 'headline org-element-string-restrictions))))
382 `(headline
383 (:raw-value ,raw-value
384 :title ,title
385 :begin ,begin
386 :end ,end
387 :pre-blank ,(count-lines pos-after-head contents-begin)
388 :hiddenp ,hidden
389 :contents-begin ,contents-begin
390 :contents-end ,contents-end
391 :level ,level
392 :priority ,(nth 3 components)
393 :tags ,tags
394 :todo-keyword ,todo
395 :todo-type ,todo-type
396 :scheduled ,scheduled
397 :deadline ,deadline
398 :timestamp ,timestamp
399 :clock ,clock
400 :post-blank ,(count-lines contents-end end)
401 :footnote-section-p ,footnote-section-p
402 :archivedp ,archivedp
403 :commentedp ,commentedp
404 :quotedp ,quotedp
405 ,@standard-props)))))
407 (defun org-element-headline-interpreter (headline contents)
408 "Interpret HEADLINE element as Org syntax.
409 CONTENTS is the contents of the element."
410 (let* ((level (org-element-get-property :level headline))
411 (todo (org-element-get-property :todo-keyword headline))
412 (priority (org-element-get-property :priority headline))
413 (title (org-element-get-property :raw-value headline))
414 (tags (let ((tag-string (org-element-get-property :tags headline))
415 (archivedp (org-element-get-property :archivedp headline)))
416 (cond
417 ((and (not tag-string) archivedp)
418 (format ":%s:" org-archive-tag))
419 (archivedp (concat ":" org-archive-tag tag-string))
420 (t tag-string))))
421 (commentedp (org-element-get-property :commentedp headline))
422 (quotedp (org-element-get-property :quotedp headline))
423 (pre-blank (org-element-get-property :pre-blank headline))
424 (heading (concat (make-string level ?*)
425 (and todo (concat " " todo))
426 (and quotedp (concat " " org-quote-string))
427 (and commentedp (concat " " org-comment-string))
428 (and priority (concat " " priority))
429 (cond ((and org-footnote-section
430 (org-element-get-property
431 :footnote-section-p headline))
432 (concat " " org-footnote-section))
433 (title (concat " " title)))))
434 ;; Align tags.
435 (tags-fmt (when tags
436 (let ((tags-len (length tags)))
437 (format "%% %ds"
438 (cond
439 ((zerop org-tags-column) (1+ tags-len))
440 ((< org-tags-column 0)
441 (max (- (+ org-tags-column (length heading)))
442 (1+ tags-len)))
443 (t (max (+ (- org-tags-column (length heading))
444 tags-len)
445 (1+ tags-len)))))))))
446 (concat heading (and tags (format tags-fmt tags))
447 (make-string (1+ pre-blank) 10)
448 contents)))
451 ;;;; Inlinetask
453 (defun org-element-inlinetask-parser ()
454 "Parse an inline task.
456 Return a list whose car is `inlinetask' and cdr is a plist
457 containing `:raw-value', `:title', `:begin', `:end', `:hiddenp',
458 `:contents-begin' and `:contents-end', `:level', `:priority',
459 `:raw-value', `:tags', `:todo-keyword', `:todo-type',
460 `:scheduled', `:deadline', `:timestamp', `:clock' and
461 `:post-blank' keywords.
463 The plist also contains any property set in the property drawer,
464 with its name in lowercase, the underscores replaced with hyphens
465 and colons at the beginning (i.e. `:custom-id').
467 Assume point is at beginning of the inline task."
468 (save-excursion
469 (let* ((keywords (org-element-collect-affiliated-keywords))
470 (begin (car keywords))
471 (components (org-heading-components))
472 (todo (nth 2 components))
473 (todo-type (and todo
474 (if (member todo org-done-keywords) 'done 'todo)))
475 (raw-value (nth 4 components))
476 (standard-props (let (plist)
477 (mapc
478 (lambda (p)
479 (let ((p-name (downcase (car p))))
480 (while (string-match "_" p-name)
481 (setq p-name
482 (replace-match "-" nil nil p-name)))
483 (setq p-name (intern (concat ":" p-name)))
484 (setq plist
485 (plist-put plist p-name (cdr p)))))
486 (org-entry-properties nil 'standard))
487 plist))
488 (time-props (org-entry-properties nil 'special "CLOCK"))
489 (scheduled (cdr (assoc "SCHEDULED" time-props)))
490 (deadline (cdr (assoc "DEADLINE" time-props)))
491 (clock (cdr (assoc "CLOCK" time-props)))
492 (timestamp (cdr (assoc "TIMESTAMP" time-props)))
493 (title (org-element-parse-secondary-string
494 raw-value
495 (cdr (assq 'inlinetask org-element-string-restrictions))))
496 (contents-begin (save-excursion (forward-line) (point)))
497 (hidden (org-truely-invisible-p))
498 (pos-before-blank (org-inlinetask-goto-end))
499 ;; In the case of a single line task, CONTENTS-BEGIN and
500 ;; CONTENTS-END might overlap.
501 (contents-end (max contents-begin
502 (save-excursion (forward-line -1) (point))))
503 (end (progn (org-skip-whitespace)
504 (if (eobp) (point) (point-at-bol)))))
505 `(inlinetask
506 (:raw-value ,raw-value
507 :title ,title
508 :begin ,begin
509 :end ,end
510 :hiddenp ,(and (> contents-end contents-begin) hidden)
511 :contents-begin ,contents-begin
512 :contents-end ,contents-end
513 :level ,(nth 1 components)
514 :priority ,(nth 3 components)
515 :tags ,(nth 5 components)
516 :todo-keyword ,todo
517 :todo-type ,todo-type
518 :scheduled ,scheduled
519 :deadline ,deadline
520 :timestamp ,timestamp
521 :clock ,clock
522 :post-blank ,(count-lines pos-before-blank end)
523 ,@standard-props
524 ,@(cadr keywords))))))
526 (defun org-element-inlinetask-interpreter (inlinetask contents)
527 "Interpret INLINETASK element as Org syntax.
528 CONTENTS is the contents of inlinetask."
529 (let* ((level (org-element-get-property :level inlinetask))
530 (todo (org-element-get-property :todo-keyword inlinetask))
531 (priority (org-element-get-property :priority inlinetask))
532 (title (org-element-get-property :raw-value inlinetask))
533 (tags (org-element-get-property :tags inlinetask))
534 (task (concat (make-string level ?*)
535 (and todo (concat " " todo))
536 (and priority (concat " " priority))
537 (and title (concat " " title))))
538 ;; Align tags.
539 (tags-fmt (when tags
540 (format "%% %ds"
541 (cond
542 ((zerop org-tags-column) 1)
543 ((< 0 org-tags-column)
544 (max (+ org-tags-column
545 (length inlinetask)
546 (length tags))
548 (t (max (- org-tags-column (length inlinetask))
549 1)))))))
550 (concat inlinetask (and tags (format tags-fmt tags) "\n" contents))))
553 ;;;; Item
555 (defun org-element-item-parser (struct)
556 "Parse an item.
558 STRUCT is the structure of the plain list.
560 Return a list whose car is `item' and cdr is a plist containing
561 `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
562 `:checkbox', `:counter', `:tag', `:raw-tag', `:structure',
563 `:hiddenp' and `:post-blank' keywords.
565 Assume point is at the beginning of the item."
566 (save-excursion
567 (beginning-of-line)
568 (let* ((begin (point))
569 (bullet (org-list-get-bullet (point) struct))
570 (checkbox (let ((box (org-list-get-checkbox begin struct)))
571 (cond ((equal "[ ]" box) 'off)
572 ((equal "[X]" box) 'on)
573 ((equal "[-]" box) 'trans))))
574 (counter (let ((c (org-list-get-counter begin struct)))
575 (cond
576 ((not c) nil)
577 ((string-match "[A-Za-z]" c)
578 (- (string-to-char (upcase (match-string 0 c)))
579 64))
580 ((string-match "[0-9]+" c)
581 (string-to-number (match-string 0 c))))))
582 (raw-tag (org-list-get-tag begin struct))
583 (tag (and raw-tag
584 (org-element-parse-secondary-string
585 raw-tag
586 (cdr (assq 'item org-element-string-restrictions)))))
587 (end (org-list-get-item-end begin struct))
588 (contents-begin (progn (looking-at org-list-full-item-re)
589 (goto-char (match-end 0))
590 (org-skip-whitespace)
591 (if (>= (point) end)
592 (point-at-bol)
593 (point))))
594 (hidden (progn (forward-line)
595 (and (not (= (point) end))
596 (org-truely-invisible-p))))
597 (contents-end (progn (goto-char end)
598 (skip-chars-backward " \r\t\n")
599 (forward-line)
600 (point))))
601 `(item
602 (:bullet ,bullet
603 :begin ,begin
604 :end ,end
605 ;; CONTENTS-BEGIN and CONTENTS-END may be mixed
606 ;; up in the case of an empty item separated
607 ;; from the next by a blank line. Thus, ensure
608 ;; the former is always the smallest of two.
609 :contents-begin ,(min contents-begin contents-end)
610 :contents-end ,(max contents-begin contents-end)
611 :checkbox ,checkbox
612 :counter ,counter
613 :raw-tag ,raw-tag
614 :tag ,tag
615 :hiddenp ,hidden
616 :structure ,struct
617 :post-blank ,(count-lines contents-end end))))))
619 (defun org-element-item-interpreter (item contents)
620 "Interpret ITEM element as Org syntax.
621 CONTENTS is the contents of the element."
622 (let* ((bullet (org-element-get-property :bullet item))
623 (checkbox (org-element-get-property :checkbox item))
624 (counter (org-element-get-property :counter item))
625 (tag (org-element-get-property :raw-tag item))
626 ;; Compute indentation.
627 (ind (make-string (length bullet) 32)))
628 ;; Indent contents.
629 (concat
630 bullet
631 (when (and org-list-two-spaces-after-bullet-regexp
632 (string-match org-list-two-spaces-after-bullet-regexp bullet))
633 " ")
634 (and counter (format "[@%d] " counter))
635 (cond
636 ((eq checkbox 'on) "[X] ")
637 ((eq checkbox 'off) "[ ] ")
638 ((eq checkbox 'trans) "[-] "))
639 (and tag (format "%s :: " tag))
640 (org-trim
641 (replace-regexp-in-string
642 "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))))
645 ;;;; Plain List
647 (defun org-element-plain-list-parser (&optional structure)
648 "Parse a plain list.
650 Return a list whose car is `plain-list' and cdr is a plist
651 containing `:type', `:begin', `:end', `:contents-begin' and
652 `:contents-end', `:level', `:structure' and `:post-blank'
653 keywords.
655 Assume point is at one of the list items."
656 (save-excursion
657 (let* ((struct (or structure (org-list-struct)))
658 (prevs (org-list-prevs-alist struct))
659 (parents (org-list-parents-alist struct))
660 (type (org-list-get-list-type (point) struct prevs))
661 (contents-begin (goto-char
662 (org-list-get-list-begin (point) struct prevs)))
663 (keywords (org-element-collect-affiliated-keywords))
664 (begin (car keywords))
665 (contents-end (goto-char
666 (org-list-get-list-end (point) struct prevs)))
667 (end (save-excursion (org-skip-whitespace)
668 (if (eobp) (point) (point-at-bol))))
669 (level 0))
670 ;; Get list level.
671 (let ((item contents-begin))
672 (while (setq item
673 (org-list-get-parent
674 (org-list-get-list-begin item struct prevs)
675 struct parents))
676 (incf level)))
677 ;; Blank lines below list belong to the top-level list only.
678 (when (> level 0)
679 (setq end (min (org-list-get-bottom-point struct)
680 (progn (org-skip-whitespace)
681 (if (eobp) (point) (point-at-bol))))))
682 ;; Return value.
683 `(plain-list
684 (:type ,type
685 :begin ,begin
686 :end ,end
687 :contents-begin ,contents-begin
688 :contents-end ,contents-end
689 :level ,level
690 :structure ,struct
691 :post-blank ,(count-lines contents-end end)
692 ,@(cadr keywords))))))
694 (defun org-element-plain-list-interpreter (plain-list contents)
695 "Interpret PLAIN-LIST element as Org syntax.
696 CONTENTS is the contents of the element."
697 contents)
700 ;;;; Quote Block
702 (defun org-element-quote-block-parser ()
703 "Parse a quote block.
705 Return a list whose car is `quote-block' and cdr is a plist
706 containing `:begin', `:end', `:hiddenp', `:contents-begin',
707 `:contents-end' and `:post-blank' keywords.
709 Assume point is at beginning or end of the block."
710 (save-excursion
711 (let* ((case-fold-search t)
712 (keywords (progn
713 (end-of-line)
714 (re-search-backward
715 (concat "^[ \t]*#\\+begin_quote") nil t)
716 (org-element-collect-affiliated-keywords)))
717 (begin (car keywords))
718 (contents-begin (progn (forward-line) (point)))
719 (hidden (org-truely-invisible-p))
720 (contents-end (progn (re-search-forward
721 (concat "^[ \t]*#\\+end_quote") nil t)
722 (point-at-bol)))
723 (pos-before-blank (progn (forward-line) (point)))
724 (end (progn (org-skip-whitespace)
725 (if (eobp) (point) (point-at-bol)))))
726 `(quote-block
727 (:begin ,begin
728 :end ,end
729 :hiddenp ,hidden
730 :contents-begin ,contents-begin
731 :contents-end ,contents-end
732 :post-blank ,(count-lines pos-before-blank end)
733 ,@(cadr keywords))))))
735 (defun org-element-quote-block-interpreter (quote-block contents)
736 "Interpret QUOTE-BLOCK element as Org syntax.
737 CONTENTS is the contents of the element."
738 (format "#+begin_quote\n%s#+end_quote" contents))
741 ;;;; Section
743 (defun org-element-section-parser ()
744 "Parse a section.
746 Return a list whose car is `section' and cdr is a plist
747 containing `:begin', `:end', `:contents-begin', `contents-end'
748 and `:post-blank' keywords."
749 (save-excursion
750 ;; Beginning of section is the beginning of the first non-blank
751 ;; line after previous headline.
752 (let ((begin (save-excursion
753 (org-with-limited-levels (outline-previous-heading))
754 (if (not (org-at-heading-p)) (point)
755 (forward-line) (org-skip-whitespace) (point-at-bol))))
756 (end (progn (org-with-limited-levels (outline-next-heading))
757 (point)))
758 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
759 (forward-line)
760 (point))))
761 `(section
762 (:begin ,begin
763 :end ,end
764 :contents-begin ,begin
765 :contents-end ,pos-before-blank
766 :post-blank ,(count-lines pos-before-blank end))))))
768 (defun org-element-section-interpreter (section contents)
769 "Interpret SECTION element as Org syntax.
770 CONTENTS is the contents of the element."
771 contents)
774 ;;;; Special Block
776 (defun org-element-special-block-parser ()
777 "Parse a special block.
779 Return a list whose car is `special-block' and cdr is a plist
780 containing `:type', `:begin', `:end', `:hiddenp',
781 `:contents-begin', `:contents-end' and `:post-blank' keywords.
783 Assume point is at beginning or end of the block."
784 (save-excursion
785 (let* ((case-fold-search t)
786 (type (progn (looking-at
787 "[ \t]*#\\+\\(?:begin\\|end\\)_\\([-A-Za-z0-9]+\\)")
788 (org-match-string-no-properties 1)))
789 (keywords (progn
790 (end-of-line)
791 (re-search-backward
792 (concat "^[ \t]*#\\+begin_" type) nil t)
793 (org-element-collect-affiliated-keywords)))
794 (begin (car keywords))
795 (contents-begin (progn (forward-line) (point)))
796 (hidden (org-truely-invisible-p))
797 (contents-end (progn (re-search-forward
798 (concat "^[ \t]*#\\+end_" type) nil t)
799 (point-at-bol)))
800 (pos-before-blank (progn (forward-line) (point)))
801 (end (progn (org-skip-whitespace)
802 (if (eobp) (point) (point-at-bol)))))
803 `(special-block
804 (:type ,type
805 :begin ,begin
806 :end ,end
807 :hiddenp ,hidden
808 :contents-begin ,contents-begin
809 :contents-end ,contents-end
810 :post-blank ,(count-lines pos-before-blank end)
811 ,@(cadr keywords))))))
813 (defun org-element-special-block-interpreter (special-block contents)
814 "Interpret SPECIAL-BLOCK element as Org syntax.
815 CONTENTS is the contents of the element."
816 (let ((block-type (org-element-get-property :type special-block)))
817 (format "#+begin_%s\n%s#+end_%s" block-type contents block-type)))
821 ;;; Elements
823 ;; For each element, a parser and an interpreter are also defined.
824 ;; Both follow the same naming convention used for greater elements.
826 ;; Also, as for greater elements, adding a new element type is done
827 ;; through the following steps: implement a parser and an interpreter,
828 ;; tweak `org-element-guess-type' so that it recognizes the new type
829 ;; and add that new type to `org-element-all-elements'.
831 ;; As a special case, when the newly defined type is a block type,
832 ;; `org-element-non-recursive-block-alist' has to be modified
833 ;; accordingly.
836 ;;;; Babel Call
838 (defun org-element-babel-call-parser ()
839 "Parse a babel call.
841 Return a list whose car is `babel-call' and cdr is a plist
842 containing `:begin', `:end', `:info' and `:post-blank' as
843 keywords."
844 (save-excursion
845 (let ((info (progn (looking-at org-babel-block-lob-one-liner-regexp)
846 (org-babel-lob-get-info)))
847 (beg (point-at-bol))
848 (pos-before-blank (progn (forward-line) (point)))
849 (end (progn (org-skip-whitespace)
850 (if (eobp) (point) (point-at-bol)))))
851 `(babel-call
852 (:beg ,beg
853 :end ,end
854 :info ,info
855 :post-blank ,(count-lines pos-before-blank end))))))
857 (defun org-element-babel-call-interpreter (inline-babel-call contents)
858 "Interpret INLINE-BABEL-CALL object as Org syntax.
859 CONTENTS is nil."
860 (let* ((babel-info (org-element-get-property :info inline-babel-call))
861 (main-source (car babel-info))
862 (post-options (nth 1 babel-info)))
863 (concat "#+call: "
864 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
865 ;; Remove redundant square brackets.
866 (replace-match
867 (match-string 1 main-source) nil nil main-source)
868 main-source)
869 (and post-options (format "[%s]" post-options)))))
872 ;;;; Comment
874 (defun org-element-comment-parser ()
875 "Parse a comment.
877 Return a list whose car is `comment' and cdr is a plist
878 containing `:begin', `:end', `:value' and `:post-blank'
879 keywords."
880 (let ((comment-re "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)")
881 beg-coms begin end value pos-before-blank keywords)
882 (save-excursion
883 ;; Move to the beginning of comments.
884 (unless (bobp)
885 (while (and (not (bobp)) (looking-at comment-re))
886 (forward-line -1))
887 (unless (looking-at comment-re) (forward-line 1)))
888 (setq beg-coms (point))
889 ;; Get affiliated keywords, if any.
890 (setq keywords (org-element-collect-affiliated-keywords))
891 ;; Store true beginning of element.
892 (setq begin (car keywords))
893 ;; Get ending of comments. If point is in a list, ensure to not
894 ;; get outside of it.
895 (let* ((itemp (org-in-item-p))
896 (max-pos (if itemp
897 (org-list-get-bottom-point
898 (save-excursion (goto-char itemp) (org-list-struct)))
899 (point-max))))
900 (while (and (looking-at comment-re) (< (point) max-pos))
901 (forward-line)))
902 (setq pos-before-blank (point))
903 ;; Find position after blank.
904 (org-skip-whitespace)
905 (setq end (if (eobp) (point) (point-at-bol)))
906 ;; Extract value.
907 (setq value (buffer-substring-no-properties beg-coms pos-before-blank)))
908 `(comment
909 (:begin ,begin
910 :end ,end
911 :value ,value
912 :post-blank ,(count-lines pos-before-blank end)
913 ,@(cadr keywords)))))
915 (defun org-element-comment-interpreter (comment contents)
916 "Interpret COMMENT element as Org syntax.
917 CONTENTS is nil."
918 (org-element-get-property :value comment))
921 ;;;; Comment Block
923 (defun org-element-comment-block-parser ()
924 "Parse an export block.
926 Return a list whose car is `comment-block' and cdr is a plist
927 containing `:begin', `:end', `:hiddenp', `:value' and
928 `:post-blank' keywords."
929 (save-excursion
930 (end-of-line)
931 (let* ((case-fold-search t)
932 (keywords (progn
933 (re-search-backward "^[ \t]*#\\+begin_comment" nil t)
934 (org-element-collect-affiliated-keywords)))
935 (begin (car keywords))
936 (contents-begin (progn (forward-line) (point)))
937 (hidden (org-truely-invisible-p))
938 (contents-end (progn (re-search-forward
939 "^[ \t]*#\\+end_comment" nil t)
940 (point-at-bol)))
941 (pos-before-blank (progn (forward-line) (point)))
942 (end (progn (org-skip-whitespace)
943 (if (eobp) (point) (point-at-bol))))
944 (value (buffer-substring-no-properties contents-begin contents-end)))
945 `(comment-block
946 (:begin ,begin
947 :end ,end
948 :value ,value
949 :hiddenp ,hidden
950 :post-blank ,(count-lines pos-before-blank end)
951 ,@(cadr keywords))))))
953 (defun org-element-comment-block-interpreter (comment-block contents)
954 "Interpret COMMENT-BLOCK element as Org syntax.
955 CONTENTS is nil."
956 (concat "#+begin_comment\n"
957 (org-remove-indentation
958 (org-element-get-property :value comment-block))
959 "#+begin_comment"))
962 ;;;; Example Block
964 (defun org-element-example-block-parser ()
965 "Parse an example block.
967 Return a list whose car is `example' and cdr is a plist
968 containing `:begin', `:end', `:options', `:hiddenp', `:value' and
969 `:post-blank' keywords."
970 (save-excursion
971 (end-of-line)
972 (let* ((case-fold-search t)
973 (options (progn
974 (re-search-backward
975 "^[ \t]*#\\+begin_example\\(?: +\\(.*\\)\\)?" nil t)
976 (org-match-string-no-properties 1)))
977 (keywords (org-element-collect-affiliated-keywords))
978 (begin (car keywords))
979 (contents-begin (progn (forward-line) (point)))
980 (hidden (org-truely-invisible-p))
981 (contents-end (progn
982 (re-search-forward "^[ \t]*#\\+end_example" nil t)
983 (point-at-bol)))
984 (value (buffer-substring-no-properties contents-begin contents-end))
985 (pos-before-blank (progn (forward-line) (point)))
986 (end (progn (org-skip-whitespace)
987 (if (eobp) (point) (point-at-bol)))))
988 `(example-block
989 (:begin ,begin
990 :end ,end
991 :value ,value
992 :options ,options
993 :hiddenp ,hidden
994 :post-blank ,(count-lines pos-before-blank end)
995 ,@(cadr keywords))))))
997 (defun org-element-example-block-interpreter (example-block contents)
998 "Interpret EXAMPLE-BLOCK element as Org syntax.
999 CONTENTS is nil."
1000 (let ((options (org-element-get-property :options example-block)))
1001 (concat "#+begin_example" (and options (concat " " options)) "\n"
1002 (org-remove-indentation
1003 (org-element-get-property :value example-block))
1004 "#+end_example")))
1007 ;;;; Export Block
1009 (defun org-element-export-block-parser ()
1010 "Parse an export block.
1012 Return a list whose car is `export-block' and cdr is a plist
1013 containing `:begin', `:end', `:type', `:hiddenp', `:value' and
1014 `:post-blank' keywords."
1015 (save-excursion
1016 (end-of-line)
1017 (let* ((case-fold-search t)
1018 (contents)
1019 (type (progn (re-search-backward
1020 (concat "[ \t]*#\\+begin_"
1021 (org-re "\\([[:alnum:]]+\\)")))
1022 (downcase (org-match-string-no-properties 1))))
1023 (keywords (org-element-collect-affiliated-keywords))
1024 (begin (car keywords))
1025 (contents-begin (progn (forward-line) (point)))
1026 (hidden (org-truely-invisible-p))
1027 (contents-end (progn (re-search-forward
1028 (concat "^[ \t]*#\\+end_" type) nil t)
1029 (point-at-bol)))
1030 (pos-before-blank (progn (forward-line) (point)))
1031 (end (progn (org-skip-whitespace)
1032 (if (eobp) (point) (point-at-bol))))
1033 (value (buffer-substring-no-properties contents-begin contents-end)))
1034 `(export-block
1035 (:begin ,begin
1036 :end ,end
1037 :type ,type
1038 :value ,value
1039 :hiddenp ,hidden
1040 :post-blank ,(count-lines pos-before-blank end)
1041 ,@(cadr keywords))))))
1043 (defun org-element-export-block-interpreter (export-block contents)
1044 "Interpret EXPORT-BLOCK element as Org syntax.
1045 CONTENTS is nil."
1046 (let ((type (org-element-get-property :type export-block)))
1047 (concat (format "#+begin_%s\n" type)
1048 (org-element-get-property :value export-block)
1049 (format "#+end_%s" type))))
1052 ;;;; Fixed-width
1054 (defun org-element-fixed-width-parser ()
1055 "Parse a fixed-width section.
1057 Return a list whose car is `fixed-width' and cdr is a plist
1058 containing `:begin', `:end', `:value' and `:post-blank'
1059 keywords."
1060 (let ((fixed-re "[ \t]*:\\( \\|$\\)")
1061 beg-area begin end value pos-before-blank keywords)
1062 (save-excursion
1063 ;; Move to the beginning of the fixed-width area.
1064 (unless (bobp)
1065 (while (and (not (bobp)) (looking-at fixed-re))
1066 (forward-line -1))
1067 (unless (looking-at fixed-re) (forward-line 1)))
1068 (setq beg-area (point))
1069 ;; Get affiliated keywords, if any.
1070 (setq keywords (org-element-collect-affiliated-keywords))
1071 ;; Store true beginning of element.
1072 (setq begin (car keywords))
1073 ;; Get ending of fixed-width area. If point is in a list,
1074 ;; ensure to not get outside of it.
1075 (let* ((itemp (org-in-item-p))
1076 (max-pos (if itemp
1077 (org-list-get-bottom-point
1078 (save-excursion (goto-char itemp) (org-list-struct)))
1079 (point-max))))
1080 (while (and (looking-at fixed-re) (< (point) max-pos))
1081 (forward-line)))
1082 (setq pos-before-blank (point))
1083 ;; Find position after blank
1084 (org-skip-whitespace)
1085 (setq end (if (eobp) (point) (point-at-bol)))
1086 ;; Extract value.
1087 (setq value (buffer-substring-no-properties beg-area pos-before-blank)))
1088 `(fixed-width
1089 (:begin ,begin
1090 :end ,end
1091 :value ,value
1092 :post-blank ,(count-lines pos-before-blank end)
1093 ,@(cadr keywords)))))
1095 (defun org-element-fixed-width-interpreter (fixed-width contents)
1096 "Interpret FIXED-WIDTH element as Org syntax.
1097 CONTENTS is nil."
1098 (org-remove-indentation (org-element-get-property :value fixed-width)))
1101 ;;;; Horizontal Rule
1103 (defun org-element-horizontal-rule-parser ()
1104 "Parse an horizontal rule.
1106 Return a list whose car is `horizontal-rule' and cdr is
1107 a plist containing `:begin', `:end' and `:post-blank'
1108 keywords."
1109 (save-excursion
1110 (let* ((keywords (org-element-collect-affiliated-keywords))
1111 (begin (car keywords))
1112 (post-hr (progn (forward-line) (point)))
1113 (end (progn (org-skip-whitespace)
1114 (if (eobp) (point) (point-at-bol)))))
1115 `(horizontal-rule
1116 (:begin ,begin
1117 :end ,end
1118 :post-blank ,(count-lines post-hr end)
1119 ,@(cadr keywords))))))
1121 (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
1122 "Interpret HORIZONTAL-RULE element as Org syntax.
1123 CONTENTS is nil."
1124 "-----")
1127 ;;;; Keyword
1129 (defun org-element-keyword-parser ()
1130 "Parse a keyword at point.
1132 Return a list whose car is `keyword' and cdr is a plist
1133 containing `:key', `:value', `:begin', `:end' and `:post-blank'
1134 keywords."
1135 (save-excursion
1136 (let* ((begin (point))
1137 (key (progn (looking-at
1138 "[ \t]*#\\+\\(\\(?:[a-z]+\\)\\(?:_[a-z]+\\)*\\):")
1139 (org-match-string-no-properties 1)))
1140 (value (org-trim (buffer-substring-no-properties
1141 (match-end 0) (point-at-eol))))
1142 (pos-before-blank (progn (forward-line) (point)))
1143 (end (progn (org-skip-whitespace)
1144 (if (eobp) (point) (point-at-bol)))))
1145 `(keyword
1146 (:key ,key
1147 :value ,value
1148 :begin ,begin
1149 :end ,end
1150 :post-blank ,(count-lines pos-before-blank end))))))
1152 (defun org-element-keyword-interpreter (keyword contents)
1153 "Interpret KEYWORD element as Org syntax.
1154 CONTENTS is nil."
1155 (format "#+%s: %s"
1156 (org-element-get-property :key keyword)
1157 (org-element-get-property :value keyword)))
1160 ;;;; Latex Environment
1162 (defun org-element-latex-environment-parser ()
1163 "Parse a LaTeX environment.
1165 Return a list whose car is `latex-environment' and cdr is a plist
1166 containing `:begin', `:end', `:value' and `:post-blank' keywords."
1167 (save-excursion
1168 (end-of-line)
1169 (let* ((case-fold-search t)
1170 (contents-begin (re-search-backward "^[ \t]*\\\\begin" nil t))
1171 (keywords (org-element-collect-affiliated-keywords))
1172 (begin (car keywords))
1173 (contents-end (progn (re-search-forward "^[ \t]*\\\\end")
1174 (forward-line)
1175 (point)))
1176 (value (buffer-substring-no-properties contents-begin contents-end))
1177 (end (progn (org-skip-whitespace)
1178 (if (eobp) (point) (point-at-bol)))))
1179 `(latex-environment
1180 (:begin ,begin
1181 :end ,end
1182 :value ,value
1183 :post-blank ,(count-lines contents-end end)
1184 ,@(cadr keywords))))))
1186 (defun org-element-latex-environment-interpreter (latex-environment contents)
1187 "Interpret LATEX-ENVIRONMENT element as Org syntax.
1188 CONTENTS is nil."
1189 (org-element-get-property :value latex-environment))
1192 ;;;; Paragraph
1194 (defun org-element-paragraph-parser ()
1195 "Parse a paragraph.
1197 Return a list whose car is `paragraph' and cdr is a plist
1198 containing `:begin', `:end', `:contents-begin' and
1199 `:contents-end' and `:post-blank' keywords.
1201 Assume point is at the beginning of the paragraph."
1202 (save-excursion
1203 (let* ((contents-begin (point))
1204 (keywords (org-element-collect-affiliated-keywords))
1205 (begin (car keywords))
1206 (contents-end (progn
1207 (end-of-line)
1208 (if (re-search-forward
1209 org-element-paragraph-separate nil 'm)
1210 (progn (forward-line -1) (end-of-line) (point))
1211 (point))))
1212 (pos-before-blank (progn (forward-line) (point)))
1213 (end (progn (org-skip-whitespace)
1214 (if (eobp) (point) (point-at-bol)))))
1215 `(paragraph
1216 (:begin ,begin
1217 :end ,end
1218 :contents-begin ,contents-begin
1219 :contents-end ,contents-end
1220 :post-blank ,(count-lines pos-before-blank end)
1221 ,@(cadr keywords))))))
1223 (defun org-element-paragraph-interpreter (paragraph contents)
1224 "Interpret PARAGRAPH element as Org syntax.
1225 CONTENTS is the contents of the element."
1226 contents)
1229 ;;;; Property Drawer
1231 (defun org-element-property-drawer-parser ()
1232 "Parse a property drawer.
1234 Return a list whose car is `property-drawer' and cdr is a plist
1235 containing `:begin', `:end', `:hiddenp', `:contents-begin',
1236 `:contents-end', `:properties' and `:post-blank' keywords."
1237 (save-excursion
1238 (let ((case-fold-search t)
1239 (begin (progn (end-of-line)
1240 (re-search-backward org-property-start-re)
1241 (match-beginning 0)))
1242 (contents-begin (progn (forward-line) (point)))
1243 (hidden (org-truely-invisible-p))
1244 (properties (let (val)
1245 (while (not (looking-at "^[ \t]*:END:"))
1246 (when (looking-at
1247 (org-re
1248 "[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):"))
1249 (push (cons (match-string 1)
1250 (org-trim
1251 (buffer-substring
1252 (match-end 0) (point-at-eol))))
1253 val))
1254 (forward-line))
1255 val))
1256 (contents-end (progn (re-search-forward "^[ \t]*:END:" nil t)
1257 (point-at-bol)))
1258 (pos-before-blank (progn (forward-line) (point)))
1259 (end (progn (org-skip-whitespace)
1260 (if (eobp) (point) (point-at-bol)))))
1261 `(property-drawer
1262 (:begin ,begin
1263 :end ,end
1264 :hiddenp ,hidden
1265 :properties ,properties
1266 :post-blank ,(count-lines pos-before-blank end))))))
1268 (defun org-element-property-drawer-interpreter (property-drawer contents)
1269 "Interpret PROPERTY-DRAWER element as Org syntax.
1270 CONTENTS is nil."
1271 (let ((props (org-element-get-property :properties property-drawer)))
1272 (concat
1273 ":PROPERTIES:\n"
1274 (mapconcat (lambda (p)
1275 (format org-property-format (format ":%s:" (car p)) (cdr p)))
1276 (nreverse props) "\n")
1277 "\n:END:")))
1280 ;;;; Quote Section
1282 (defun org-element-quote-section-parser ()
1283 "Parse a quote section.
1285 Return a list whose car is `quote-section' and cdr is a plist
1286 containing `:begin', `:end', `:value' and `:post-blank'
1287 keywords."
1288 (save-excursion
1289 (let* ((begin (progn (org-back-to-heading t)
1290 (forward-line)
1291 (org-skip-whitespace)
1292 (point-at-bol)))
1293 (end (progn (org-with-limited-levels (outline-next-heading))
1294 (point)))
1295 (pos-before-blank (progn (skip-chars-backward " \r\t\n")
1296 (forward-line)
1297 (point)))
1298 (value (unless (= begin end)
1299 (buffer-substring-no-properties begin pos-before-blank))))
1300 `(quote-section
1301 (:begin ,begin
1302 :end ,end
1303 :value ,value
1304 :post-blank ,(if value
1305 (count-lines pos-before-blank end)
1306 0))))))
1308 (defun org-element-quote-section-interpreter (quote-section contents)
1309 "Interpret QUOTE-SECTION element as Org syntax.
1310 CONTENTS is nil."
1311 (org-element-get-property :value quote-section))
1314 ;;;; Src Block
1316 (defun org-element-src-block-parser ()
1317 "Parse a src block.
1319 Return a list whose car is `src-block' and cdr is a plist
1320 containing `:language', `:switches', `:parameters', `:begin',
1321 `:end', `:hiddenp', `:contents-begin', `:contents-end', `:value'
1322 and `:post-blank' keywords."
1323 (save-excursion
1324 (end-of-line)
1325 (let* ((case-fold-search t)
1326 ;; Get position at beginning of block.
1327 (contents-begin
1328 (re-search-backward
1329 (concat "^[ \t]*#\\+begin_src"
1330 "\\(?: +\\(\\S-+\\)\\)?" ; language
1331 "\\(\\(?: +[-+][A-Za-z]\\)*\\)" ; switches
1332 "\\(.*\\)[ \t]*$") ; arguments
1333 nil t))
1334 ;; Get language as a string.
1335 (language (org-match-string-no-properties 1))
1336 ;; Get switches.
1337 (switches (org-match-string-no-properties 2))
1338 ;; Get parameters.
1339 (parameters (org-trim (org-match-string-no-properties 3)))
1340 ;; Get affiliated keywords.
1341 (keywords (org-element-collect-affiliated-keywords))
1342 ;; Get beginning position.
1343 (begin (car keywords))
1344 ;; Get position at end of block.
1345 (contents-end (progn (re-search-forward "^[ \t]*#\\+end_src" nil t)
1346 (forward-line)
1347 (point)))
1348 ;; Retrieve code.
1349 (value (buffer-substring-no-properties
1350 (save-excursion (goto-char contents-begin)
1351 (forward-line)
1352 (point))
1353 (match-beginning 0)))
1354 ;; Get position after ending blank lines.
1355 (end (progn (org-skip-whitespace)
1356 (if (eobp) (point) (point-at-bol))))
1357 ;; Get visibility status.
1358 (hidden (progn (goto-char contents-begin)
1359 (forward-line)
1360 (org-truely-invisible-p))))
1361 `(src-block
1362 (:language ,language
1363 :switches ,switches
1364 :parameters ,parameters
1365 :begin ,begin
1366 :end ,end
1367 :hiddenp ,hidden
1368 :value ,value
1369 :post-blank ,(count-lines contents-end end)
1370 ,@(cadr keywords))))))
1372 (defun org-element-src-block-interpreter (src-block contents)
1373 "Interpret SRC-BLOCK element as Org syntax.
1374 CONTENTS is nil."
1375 (let ((lang (org-element-get-property :language src-block))
1376 (switches (org-element-get-property :switches src-block))
1377 (params (org-element-get-property :parameters src-block))
1378 (value (let ((val (org-element-get-property :value src-block)))
1379 (cond
1380 (org-src-preserve-indentation val)
1381 ((zerop org-edit-src-content-indentation)
1382 (org-remove-indentation val))
1384 (let ((ind (make-string
1385 org-edit-src-content-indentation 32)))
1386 (replace-regexp-in-string
1387 "\\(^\\)[ \t]*\\S-" ind
1388 (org-remove-indentation val) nil nil 1)))))))
1389 (concat (format "#+begin_src%s\n"
1390 (concat (and lang (concat " " lang))
1391 (and switches (concat " " switches))
1392 (and params (concat " " params))))
1393 value
1394 "#+end_src")))
1397 ;;;; Table
1399 (defun org-element-table-parser ()
1400 "Parse a table at point.
1402 Return a list whose car is `table' and cdr is a plist containing
1403 `:begin', `:end', `:contents-begin', `:contents-end', `:tblfm',
1404 `:type', `:raw-table' and `:post-blank' keywords."
1405 (save-excursion
1406 (let* ((table-begin (goto-char (org-table-begin t)))
1407 (type (if (org-at-table.el-p) 'table.el 'org))
1408 (keywords (org-element-collect-affiliated-keywords))
1409 (begin (car keywords))
1410 (table-end (goto-char (marker-position (org-table-end t))))
1411 (tblfm (when (looking-at "[ \t]*#\\+tblfm: +\\(.*\\)[ \t]*")
1412 (prog1 (org-match-string-no-properties 1)
1413 (forward-line))))
1414 (pos-before-blank (point))
1415 (end (progn (org-skip-whitespace)
1416 (if (eobp) (point) (point-at-bol))))
1417 (raw-table (org-remove-indentation
1418 (buffer-substring-no-properties table-begin table-end))))
1419 `(table
1420 (:begin ,begin
1421 :end ,end
1422 :type ,type
1423 :raw-table ,raw-table
1424 :tblfm ,tblfm
1425 :post-blank ,(count-lines pos-before-blank end)
1426 ,@(cadr keywords))))))
1428 (defun org-element-table-interpreter (table contents)
1429 "Interpret TABLE element as Org syntax.
1430 CONTENTS is nil."
1431 (org-element-get-property :raw-table table))
1434 ;;;; Verse Block
1436 (defun org-element-verse-block-parser ()
1437 "Parse a verse block.
1439 Return a list whose car is `verse-block' and cdr is a plist
1440 containing `:begin', `:end', `:hiddenp', `:raw-value', `:value'
1441 and `:post-blank' keywords.
1443 Assume point is at beginning or end of the block."
1444 (save-excursion
1445 (let* ((case-fold-search t)
1446 (keywords (progn
1447 (end-of-line)
1448 (re-search-backward
1449 (concat "^[ \t]*#\\+begin_verse") nil t)
1450 (org-element-collect-affiliated-keywords)))
1451 (begin (car keywords))
1452 (hidden (progn (forward-line) (org-truely-invisible-p)))
1453 (raw-val (buffer-substring-no-properties
1454 (point)
1455 (progn
1456 (re-search-forward (concat "^[ \t]*#\\+end_verse") nil t)
1457 (point-at-bol))))
1458 (pos-before-blank (progn (forward-line) (point)))
1459 (end (progn (org-skip-whitespace)
1460 (if (eobp) (point) (point-at-bol))))
1461 (value (org-element-parse-secondary-string
1462 (org-remove-indentation raw-val)
1463 (cdr (assq 'verse org-element-string-restrictions)))))
1464 `(verse-block
1465 (:begin ,begin
1466 :end ,end
1467 :hiddenp ,hidden
1468 :raw-value ,raw-val
1469 :value ,value
1470 :post-blank ,(count-lines pos-before-blank end)
1471 ,@(cadr keywords))))))
1473 (defun org-element-verse-block-interpreter (verse-block contents)
1474 "Interpret VERSE-BLOCK element as Org syntax.
1475 CONTENTS is nil."
1476 (format "#+begin_verse\n%s#+end_verse"
1477 (org-remove-indentation
1478 (org-element-get-property :raw-value verse-block))))
1482 ;;; Objects
1484 ;; Unlike to elements, interstices can be found between objects.
1485 ;; That's why, along with the parser, successor functions are provided
1486 ;; for each object. Some objects share the same successor
1487 ;; (i.e. `emphasis' and `verbatim' objects).
1489 ;; A successor must accept a single argument bounding the search. It
1490 ;; will return either a cons cell whose car is the object's type, as
1491 ;; a symbol, and cdr the position of its next occurrence, or nil.
1493 ;; Successors follow the naming convention:
1494 ;; org-element-NAME-successor, where NAME is the name of the
1495 ;; successor, as defined in `org-element-all-successors'.
1497 ;; Some object types (i.e `emphasis') are recursive. Restrictions on
1498 ;; object types they can contain will be specified in
1499 ;; `org-element-object-restrictions'.
1501 ;; Adding a new type of object is simple. Implement a successor,
1502 ;; a parser, and an interpreter for it, all following the naming
1503 ;; convention. Register successor in `org-element-all-successors',
1504 ;; maybe tweak restrictions about it, and that's it.
1506 ;;;; Emphasis
1508 (defun org-element-emphasis-parser ()
1509 "Parse text markup object at point.
1511 Return a list whose car is `emphasis' and cdr is a plist with
1512 `:marker', `:begin', `:end', `:contents-begin' and
1513 `:contents-end' and `:post-blank' keywords.
1515 Assume point is at the first emphasis marker."
1516 (save-excursion
1517 (unless (bolp) (backward-char 1))
1518 (looking-at org-emph-re)
1519 (let ((begin (match-beginning 2))
1520 (marker (org-match-string-no-properties 3))
1521 (contents-begin (match-beginning 4))
1522 (contents-end (match-end 4))
1523 (post-blank (progn (goto-char (match-end 2))
1524 (skip-chars-forward " \t")))
1525 (end (point)))
1526 `(emphasis
1527 (:marker ,marker
1528 :begin ,begin
1529 :end ,end
1530 :contents-begin ,contents-begin
1531 :contents-end ,contents-end
1532 :post-blank ,post-blank)))))
1534 (defun org-element-emphasis-interpreter (emphasis contents)
1535 "Interpret EMPHASIS object as Org syntax.
1536 CONTENTS is the contents of the object."
1537 (let ((marker (org-element-get-property :marker emphasis)))
1538 (concat marker contents marker)))
1540 (defun org-element-text-markup-successor (limit)
1541 "Search for the next emphasis or verbatim and return position.
1543 LIMIT bounds the search.
1545 Return value is a cons cell whose car is `emphasis' or
1546 `verbatim' and cdr is beginning position."
1547 (save-excursion
1548 (unless (bolp) (backward-char))
1549 (when (re-search-forward org-emph-re limit t)
1550 (cons (if (nth 4 (assoc (match-string 3) org-emphasis-alist))
1551 'verbatim
1552 'emphasis)
1553 (match-beginning 2)))))
1555 ;;;; Entity
1557 (defun org-element-entity-parser ()
1558 "Parse entity at point.
1560 Return a list whose car is `entity' and cdr a plist with
1561 `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
1562 `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
1563 keywords.
1565 Assume point is at the beginning of the entity."
1566 (save-excursion
1567 (looking-at "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
1568 (let* ((value (org-entity-get (match-string 1)))
1569 (begin (match-beginning 0))
1570 (bracketsp (string= (match-string 2) "{}"))
1571 (post-blank (progn (goto-char (match-end 1))
1572 (when bracketsp (forward-char 2))
1573 (skip-chars-forward " \t")))
1574 (end (point)))
1575 `(entity
1576 (:name ,(car value)
1577 :latex ,(nth 1 value)
1578 :latex-math-p ,(nth 2 value)
1579 :html ,(nth 3 value)
1580 :ascii ,(nth 4 value)
1581 :latin1 ,(nth 5 value)
1582 :utf-8 ,(nth 6 value)
1583 :begin ,begin
1584 :end ,end
1585 :use-brackets-p ,bracketsp
1586 :post-blank ,post-blank)))))
1588 (defun org-element-entity-interpreter (entity contents)
1589 "Interpret ENTITY object as Org syntax.
1590 CONTENTS is nil."
1591 (concat "\\"
1592 (org-element-get-property :name entity)
1593 (when (org-element-get-property :use-brackets-p entity) "{}")))
1595 (defun org-element-latex-or-entity-successor (limit)
1596 "Search for the next latex-fragment or entity object.
1598 LIMIT bounds the search.
1600 Return value is a cons cell whose car is `entity' or
1601 `latex-fragment' and cdr is beginning position."
1602 (save-excursion
1603 (let ((matchers (plist-get org-format-latex-options :matchers))
1604 ;; ENTITY-RE matches both LaTeX commands and Org entities.
1605 (entity-re
1606 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"))
1607 (when (re-search-forward
1608 (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
1609 matchers "\\|")
1610 "\\|" entity-re)
1611 limit t)
1612 (goto-char (match-beginning 0))
1613 (if (looking-at entity-re)
1614 ;; Determine if it's a real entity or a LaTeX command.
1615 (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
1616 (match-beginning 0))
1617 ;; No entity nor command: point is at a LaTeX fragment.
1618 ;; Determine its type to get the correct beginning position.
1619 (cons 'latex-fragment
1620 (catch 'return
1621 (mapc (lambda (e)
1622 (when (looking-at (nth 1 (assoc e org-latex-regexps)))
1623 (throw 'return
1624 (match-beginning
1625 (nth 2 (assoc e org-latex-regexps))))))
1626 matchers)
1627 (point))))))))
1630 ;;;; Export Snippet
1632 (defun org-element-export-snippet-parser ()
1633 "Parse export snippet at point.
1635 Return a list whose car is `export-snippet' and cdr a plist with
1636 `:begin', `:end', `:back-end', `:value' and `:post-blank' as
1637 keywords.
1639 Assume point is at the beginning of the snippet."
1640 (save-excursion
1641 (looking-at "@\\([-A-Za-z0-9]+\\){")
1642 (let* ((begin (point))
1643 (back-end (org-match-string-no-properties 1))
1644 (before-blank (progn (goto-char (scan-sexps (1- (match-end 0)) 1))))
1645 (value (buffer-substring-no-properties
1646 (match-end 0) (1- before-blank)))
1647 (post-blank (skip-chars-forward " \t"))
1648 (end (point)))
1649 `(export-snippet
1650 (:back-end ,back-end
1651 :value ,value
1652 :begin ,begin
1653 :end ,end
1654 :post-blank ,post-blank)))))
1656 (defun org-element-export-snippet-interpreter (export-snippet contents)
1657 "Interpret EXPORT-SNIPPET object as Org syntax.
1658 CONTENTS is nil."
1659 (format "@%s{%s}"
1660 (org-element-get-property :back-end export-snippet)
1661 (org-element-get-property :value export-snippet)))
1663 (defun org-element-export-snippet-successor (limit)
1664 "Search for the next export-snippet object.
1666 LIMIT bounds the search.
1668 Return value is a cons cell whose car is `export-snippet' cdr is
1669 its beginning position."
1670 (save-excursion
1671 (catch 'exit
1672 (while (re-search-forward "@[-A-Za-z0-9]+{" limit t)
1673 (when (let ((end (ignore-errors (scan-sexps (1- (point)) 1))))
1674 (and end (eq (char-before end) ?})))
1675 (throw 'exit (cons 'export-snippet (match-beginning 0))))))))
1678 ;;;; Footnote Reference
1680 (defun org-element-footnote-reference-parser ()
1681 "Parse footnote reference at point.
1683 Return a list whose car is `footnote-reference' and cdr a plist
1684 with `:label', `:type', `:definition', `:begin', `:end' and
1685 `:post-blank' as keywords."
1686 (save-excursion
1687 (let* ((ref (org-footnote-at-reference-p))
1688 (label (car ref))
1689 (raw-def (nth 3 ref))
1690 (inline-def (and raw-def
1691 (org-element-parse-secondary-string raw-def nil)))
1692 (type (if (nth 3 ref) 'inline 'standard))
1693 (begin (nth 1 ref))
1694 (post-blank (progn (goto-char (nth 2 ref))
1695 (skip-chars-forward " \t")))
1696 (end (point)))
1697 `(footnote-reference
1698 (:label ,label
1699 :type ,type
1700 :inline-definition ,inline-def
1701 :begin ,begin
1702 :end ,end
1703 :post-blank ,post-blank
1704 :raw-definition ,raw-def)))))
1706 (defun org-element-footnote-reference-interpreter (footnote-reference contents)
1707 "Interpret FOOTNOTE-REFERENCE object as Org syntax.
1708 CONTENTS is nil."
1709 (let ((label (or (org-element-get-property :label footnote-reference)
1710 "fn:"))
1711 (def (let ((raw (org-element-get-property
1712 :raw-definition footnote-reference)))
1713 (if raw (concat ":" raw) ""))))
1714 (format "[%s]" (concat label def))))
1716 (defun org-element-footnote-reference-successor (limit)
1717 "Search for the next footnote-reference and return beginning
1718 position.
1720 LIMIT bounds the search.
1722 Return value is a cons cell whose car is `footnote-reference' and
1723 cdr is beginning position."
1724 (let (fn-ref)
1725 (when (setq fn-ref (org-footnote-get-next-reference nil nil limit))
1726 (cons 'footnote-reference (nth 1 fn-ref)))))
1729 ;;;; Inline Babel Call
1731 (defun org-element-inline-babel-call-parser ()
1732 "Parse inline babel call at point.
1734 Return a list whose car is `inline-babel-call' and cdr a plist with
1735 `:begin', `:end', `:info' and `:post-blank' as keywords.
1737 Assume point is at the beginning of the babel call."
1738 (save-excursion
1739 (unless (bolp) (backward-char))
1740 (looking-at org-babel-inline-lob-one-liner-regexp)
1741 (let ((info (save-match-data (org-babel-lob-get-info)))
1742 (begin (match-end 1))
1743 (post-blank (progn (goto-char (match-end 0))
1744 (skip-chars-forward " \t")))
1745 (end (point)))
1746 `(inline-babel-call
1747 (:begin ,begin
1748 :end ,end
1749 :info ,info
1750 :post-blank ,post-blank)))))
1752 (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
1753 "Interpret INLINE-BABEL-CALL object as Org syntax.
1754 CONTENTS is nil."
1755 (let* ((babel-info (org-element-get-property :info inline-babel-call))
1756 (main-source (car babel-info))
1757 (post-options (nth 1 babel-info)))
1758 (concat "call_"
1759 (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
1760 ;; Remove redundant square brackets.
1761 (replace-match
1762 (match-string 1 main-source) nil nil main-source)
1763 main-source)
1764 (and post-options (format "[%s]" post-options)))))
1766 (defun org-element-inline-babel-call-successor (limit)
1767 "Search for the next inline-babel-call and return beginning
1768 position.
1770 LIMIT bounds the search.
1772 Return value is a cons cell whose car is `inline-babel-call' and
1773 cdr is beginning position."
1774 (save-excursion
1775 ;; Use a simplified version of
1776 ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
1777 (when (re-search-forward
1778 "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
1779 limit t)
1780 (cons 'inline-babel-call (match-beginning 0)))))
1783 ;;;; Inline Src Block
1785 (defun org-element-inline-src-block-parser ()
1786 "Parse inline source block at point.
1788 Return a list whose car is `inline-src-block' and cdr a plist
1789 with `:begin', `:end', `:language', `:value', `:parameters' and
1790 `:post-blank' as keywords.
1792 Assume point is at the beginning of the inline src block."
1793 (save-excursion
1794 (unless (bolp) (backward-char))
1795 (looking-at org-babel-inline-src-block-regexp)
1796 (let ((begin (match-beginning 1))
1797 (language (org-match-string-no-properties 2))
1798 (parameters (org-match-string-no-properties 4))
1799 (value (org-match-string-no-properties 5))
1800 (post-blank (progn (goto-char (match-end 0))
1801 (skip-chars-forward " \t")))
1802 (end (point)))
1803 `(inline-src-block
1804 (:language ,language
1805 :value ,value
1806 :parameters ,parameters
1807 :begin ,begin
1808 :end ,end
1809 :post-blank ,post-blank)))))
1811 (defun org-element-inline-src-block-successor (limit)
1812 "Search for the next inline-babel-call and return beginning position.
1814 LIMIT bounds the search.
1816 Return value is a cons cell whose car is `inline-babel-call' and
1817 cdr is beginning position."
1818 (save-excursion
1819 (when (re-search-forward org-babel-inline-src-block-regexp limit t)
1820 (cons 'inline-src-block (match-beginning 1)))))
1823 ;;;; Latex Fragment
1825 (defun org-element-latex-fragment-parser ()
1826 "Parse latex fragment at point.
1828 Return a list whose car is `latex-fragment' and cdr a plist with
1829 `:value', `:begin', `:end', and `:post-blank' as keywords.
1831 Assume point is at the beginning of the latex fragment."
1832 (save-excursion
1833 (let* ((begin (point))
1834 (substring-match
1835 (catch 'exit
1836 (mapc (lambda (e)
1837 (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
1838 (when (or (looking-at latex-regexp)
1839 (and (not (bobp))
1840 (save-excursion
1841 (backward-char)
1842 (looking-at latex-regexp))))
1843 (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
1844 (plist-get org-format-latex-options :matchers))
1845 ;; None found: it's a macro.
1846 (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
1848 (value (match-string-no-properties substring-match))
1849 (post-blank (progn (goto-char (match-end substring-match))
1850 (skip-chars-forward " \t")))
1851 (end (point)))
1852 `(latex-fragment
1853 (:value ,value
1854 :begin ,begin
1855 :end ,end
1856 :post-blank ,post-blank)))))
1858 (defun org-element-latex-fragment-interpreter (latex-fragment contents)
1859 "Interpret LATEX-FRAGMENT object as Org syntax.
1860 CONTENTS is nil."
1861 (org-element-get-property :value latex-fragment))
1863 ;;;; Line Break
1865 (defun org-element-line-break-parser ()
1866 "Parse line break at point.
1868 Return a list whose car is `line-break', and cdr a plist with
1869 `:begin', `:end' and `:post-blank' keywords.
1871 Assume point is at the beginning of the line break."
1872 (save-excursion
1873 (let* ((begin (point))
1874 (end (progn (end-of-line) (point)))
1875 (post-blank (- (skip-chars-backward " \t")))
1876 (end (point)))
1877 `(line-break
1878 (:begin ,begin
1879 :end ,end
1880 :post-blank ,post-blank)))))
1882 (defun org-element-line-break-interpreter (line-break contents)
1883 "Interpret LINE-BREAK object as Org syntax.
1884 CONTENTS is nil."
1885 (org-element-get-property :value line-break))
1887 (defun org-element-line-break-successor (limit)
1888 "Search for the next statistics cookie and return position.
1890 LIMIT bounds the search.
1892 Return value is a cons cell whose car is `line-break' and cdr is
1893 beginning position."
1894 (save-excursion
1895 (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
1896 (goto-char (match-beginning 1)))))
1897 ;; A line break can only happen on a non-empty line.
1898 (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
1899 (cons 'line-break beg)))))
1902 ;;;; Link
1904 (defun org-element-link-parser ()
1905 "Parse link at point.
1907 Return a list whose car is `link' and cdr a plist with `:type',
1908 `:path', `:raw-link', `:begin', `:end', `:contents-begin',
1909 `:contents-end' and `:post-blank' as keywords.
1911 Assume point is at the beginning of the link."
1912 (save-excursion
1913 (let ((begin (point))
1914 end contents-begin contents-end link-end post-blank path type
1915 raw-link link)
1916 (cond
1917 ;; Type 1: text targeted from a radio target.
1918 ((and org-target-link-regexp (looking-at org-target-link-regexp))
1919 (setq type "radio"
1920 path (org-match-string-no-properties 0)
1921 contents-begin (match-beginning 0)
1922 contents-end (match-end 0)
1923 link-end (match-end 0)))
1924 ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
1925 ((looking-at org-bracket-link-regexp)
1926 (setq contents-begin (match-beginning 3)
1927 contents-end (match-end 3)
1928 link-end (match-end 0)
1929 ;; RAW-LINK is the original link.
1930 raw-link (org-match-string-no-properties 1)
1931 link (org-link-expand-abbrev
1932 (replace-regexp-in-string
1933 " *\n *" " " (org-link-unescape raw-link) t t)))
1934 ;; Determine TYPE of link and set PATH accordingly.
1935 (cond
1936 ;; File type.
1937 ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
1938 (setq type "file" path link))
1939 ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
1940 ((string-match org-link-re-with-space3 link)
1941 (setq type (match-string 1 link) path (match-string 2 link)))
1942 ;; Id type: PATH is the id.
1943 ((string-match "^id:\\([-a-f0-9]+\\)" link)
1944 (setq type "id" path (match-string 1 link)))
1945 ;; Code-ref type: PATH is the name of the reference.
1946 ((string-match "^(\\(.*\\))$" link)
1947 (setq type "coderef" path (match-string 1 link)))
1948 ;; Custom-id type: PATH is the name of the custom id.
1949 ((= (aref link 0) ?#)
1950 (setq type "custom-id" path (substring link 1)))
1951 ;; Fuzzy type: Internal link either matches a target, an
1952 ;; headline name or nothing. PATH is the target or headline's
1953 ;; name.
1954 (t (setq type "fuzzy" path link))))
1955 ;; Type 3: Plain link, i.e. http://orgmode.org
1956 ((looking-at org-plain-link-re)
1957 (setq raw-link (org-match-string-no-properties 0)
1958 type (org-match-string-no-properties 1)
1959 path (org-match-string-no-properties 2)
1960 link-end (match-end 0)))
1961 ;; Type 4: Angular link, i.e. <http://orgmode.org>
1962 ((looking-at org-angle-link-re)
1963 (setq raw-link (buffer-substring-no-properties
1964 (match-beginning 1) (match-end 2))
1965 type (org-match-string-no-properties 1)
1966 path (org-match-string-no-properties 2)
1967 link-end (match-end 0))))
1968 ;; In any case, deduce end point after trailing white space from
1969 ;; LINK-END variable.
1970 (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
1971 end (point))
1972 `(link
1973 (:type ,type
1974 :path ,path
1975 :raw-link ,(or raw-link path)
1976 :begin ,begin
1977 :end ,end
1978 :contents-begin ,contents-begin
1979 :contents-end ,contents-end
1980 :post-blank ,post-blank)))))
1982 (defun org-element-link-interpreter (link contents)
1983 "Interpret LINK object as Org syntax.
1984 CONTENTS is the contents of the object."
1985 (let ((type (org-element-get-property :type link))
1986 (raw-link (org-element-get-property :raw-link link)))
1987 (cond
1988 ((string= type "radio") raw-link)
1989 (t (format "[[%s]%s]"
1990 raw-link
1991 (if (string= contents "") "" (format "[%s]" contents)))))))
1993 (defun org-element-link-successor (limit)
1994 "Search for the next link and return position.
1996 LIMIT bounds the search.
1998 Return value is a cons cell whose car is `link' and cdr is
1999 beginning position."
2000 (save-excursion
2001 (let ((link-regexp
2002 (if org-target-link-regexp
2003 (concat org-any-link-re "\\|" org-target-link-regexp)
2004 org-any-link-re)))
2005 (when (re-search-forward link-regexp limit t)
2006 (cons 'link (match-beginning 0))))))
2009 ;;;; Macro
2011 (defun org-element-macro-parser ()
2012 "Parse macro at point.
2014 Return a list whose car is `macro' and cdr a plist with `:key',
2015 `:args', `:begin', `:end', `:value' and `:post-blank' as
2016 keywords.
2018 Assume point is at the macro."
2019 (save-excursion
2020 (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
2021 (let ((begin (point))
2022 (key (downcase (org-match-string-no-properties 1)))
2023 (value (org-match-string-no-properties 0))
2024 (post-blank (progn (goto-char (match-end 0))
2025 (skip-chars-forward " \t")))
2026 (end (point))
2027 (args (let ((args (org-match-string-no-properties 3)) args2)
2028 (when args
2029 (setq args (org-split-string args ","))
2030 (while args
2031 (while (string-match "\\\\\\'" (car args))
2032 ;; Repair bad splits.
2033 (setcar (cdr args) (concat (substring (car args) 0 -1)
2034 "," (nth 1 args)))
2035 (pop args))
2036 (push (pop args) args2))
2037 (mapcar 'org-trim (nreverse args2))))))
2038 `(macro
2039 (:key ,key
2040 :value ,value
2041 :args ,args
2042 :begin ,begin
2043 :end ,end
2044 :post-blank ,post-blank)))))
2046 (defun org-element-macro-interpreter (macro contents)
2047 "Interpret MACRO object as Org syntax.
2048 CONTENTS is nil."
2049 (org-element-get-property :value macro))
2051 (defun org-element-macro-successor (limit)
2052 "Search for the next macro and return position.
2054 LIMIT bounds the search.
2056 Return value is cons cell whose car is `macro' and cdr is
2057 beginning position."
2058 (save-excursion
2059 (when (re-search-forward
2060 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2061 limit t)
2062 (cons 'macro (match-beginning 0)))))
2065 ;;;; Radio-target
2067 (defun org-element-radio-target-parser ()
2068 "Parse radio target at point.
2070 Return a list whose car is `radio-target' and cdr a plist with
2071 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2072 and `:post-blank' as keywords.
2074 Assume point is at the radio target."
2075 (save-excursion
2076 (looking-at org-radio-target-regexp)
2077 (let ((begin (point))
2078 (contents-begin (match-beginning 1))
2079 (contents-end (match-end 1))
2080 (raw-value (org-match-string-no-properties 1))
2081 (post-blank (progn (goto-char (match-end 0))
2082 (skip-chars-forward " \t")))
2083 (end (point)))
2084 `(radio-target
2085 (:begin ,begin
2086 :end ,end
2087 :contents-begin ,contents-begin
2088 :contents-end ,contents-end
2089 :raw-value ,raw-value
2090 :post-blank ,post-blank)))))
2092 (defun org-element-radio-target-interpreter (target contents)
2093 "Interpret TARGET object as Org syntax.
2094 CONTENTS is the contents of the object."
2095 (concat "<<<" contents ">>>"))
2097 (defun org-element-radio-target-successor (limit)
2098 "Search for the next radio-target and return position.
2100 LIMIT bounds the search.
2102 Return value is a cons cell whose car is `radio-target' and cdr
2103 is beginning position."
2104 (save-excursion
2105 (when (re-search-forward org-radio-target-regexp limit t)
2106 (cons 'radio-target (match-beginning 0)))))
2109 ;;;; Statistics Cookie
2111 (defun org-element-statistics-cookie-parser ()
2112 "Parse statistics cookie at point.
2114 Return a list whose car is `statistics-cookie', and cdr a plist
2115 with `:begin', `:end', `:value' and `:post-blank' keywords.
2117 Assume point is at the beginning of the statistics-cookie."
2118 (save-excursion
2119 (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
2120 (let* ((begin (point))
2121 (value (buffer-substring-no-properties
2122 (match-beginning 0) (match-end 0)))
2123 (post-blank (progn (goto-char (match-end 0))
2124 (skip-chars-forward " \t")))
2125 (end (point)))
2126 `(statistics-cookie
2127 (:begin ,begin
2128 :end ,end
2129 :value ,value
2130 :post-blank ,post-blank)))))
2132 (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
2133 "Interpret STATISTICS-COOKIE object as Org syntax.
2134 CONTENTS is nil."
2135 (org-element-get-property :value statistics-cookie))
2137 (defun org-element-statistics-cookie-successor (limit)
2138 "Search for the next statistics cookie and return position.
2140 LIMIT bounds the search.
2142 Return value is a cons cell whose car is `statistics-cookie' and
2143 cdr is beginning position."
2144 (save-excursion
2145 (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
2146 (cons 'statistics-cookie (match-beginning 0)))))
2149 ;;;; Subscript
2151 (defun org-element-subscript-parser ()
2152 "Parse subscript at point.
2154 Return a list whose car is `subscript' and cdr a plist with
2155 `:begin', `:end', `:contents-begin', `:contents-end',
2156 `:use-brackets-p' and `:post-blank' as keywords.
2158 Assume point is at the underscore."
2159 (save-excursion
2160 (unless (bolp) (backward-char))
2161 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
2163 (not (looking-at org-match-substring-regexp))))
2164 (begin (match-beginning 2))
2165 (contents-begin (or (match-beginning 5)
2166 (match-beginning 3)))
2167 (contents-end (or (match-end 5) (match-end 3)))
2168 (post-blank (progn (goto-char (match-end 0))
2169 (skip-chars-forward " \t")))
2170 (end (point)))
2171 `(subscript
2172 (:begin ,begin
2173 :end ,end
2174 :use-brackets-p ,bracketsp
2175 :contents-begin ,contents-begin
2176 :contents-end ,contents-end
2177 :post-blank ,post-blank)))))
2179 (defun org-element-subscript-interpreter (subscript contents)
2180 "Interpret SUBSCRIPT object as Org syntax.
2181 CONTENTS is the contents of the object."
2182 (format
2183 (if (org-element-get-property :use-brackets-p subscript) "_{%s}" "_%s")
2184 contents))
2186 (defun org-element-sub/superscript-successor (limit)
2187 "Search for the next sub/superscript and return beginning
2188 position.
2190 LIMIT bounds the search.
2192 Return value is a cons cell whose car is either `subscript' or
2193 `superscript' and cdr is beginning position."
2194 (save-excursion
2195 (when (re-search-forward org-match-substring-regexp limit t)
2196 (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
2197 (match-beginning 2)))))
2200 ;;;; Superscript
2202 (defun org-element-superscript-parser ()
2203 "Parse superscript at point.
2205 Return a list whose car is `superscript' and cdr a plist with
2206 `:begin', `:end', `:contents-begin', `:contents-end',
2207 `:use-brackets-p' and `:post-blank' as keywords.
2209 Assume point is at the caret."
2210 (save-excursion
2211 (unless (bolp) (backward-char))
2212 (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
2214 (not (looking-at org-match-substring-regexp))))
2215 (begin (match-beginning 2))
2216 (contents-begin (or (match-beginning 5)
2217 (match-beginning 3)))
2218 (contents-end (or (match-end 5) (match-end 3)))
2219 (post-blank (progn (goto-char (match-end 0))
2220 (skip-chars-forward " \t")))
2221 (end (point)))
2222 `(superscript
2223 (:begin ,begin
2224 :end ,end
2225 :use-brackets-p ,bracketsp
2226 :contents-begin ,contents-begin
2227 :contents-end ,contents-end
2228 :post-blank ,post-blank)))))
2230 (defun org-element-superscript-interpreter (superscript contents)
2231 "Interpret SUPERSCRIPT object as Org syntax.
2232 CONTENTS is the contents of the object."
2233 (format
2234 (if (org-element-get-property :use-brackets-p superscript) "^{%s}" "^%s")
2235 contents))
2238 ;;;; Target
2240 (defun org-element-target-parser ()
2241 "Parse target at point.
2243 Return a list whose car is `target' and cdr a plist with
2244 `:begin', `:end', `:contents-begin', `:contents-end', `raw-value'
2245 and `:post-blank' as keywords.
2247 Assume point is at the target."
2248 (save-excursion
2249 (looking-at org-target-regexp)
2250 (let ((begin (point))
2251 (contents-begin (match-beginning 1))
2252 (contents-end (match-end 1))
2253 (raw-value (org-match-string-no-properties 1))
2254 (post-blank (progn (goto-char (match-end 0))
2255 (skip-chars-forward " \t")))
2256 (end (point)))
2257 `(target
2258 (:begin ,begin
2259 :end ,end
2260 :contents-begin ,contents-begin
2261 :contents-end ,contents-end
2262 :raw-value ,raw-value
2263 :post-blank ,post-blank)))))
2265 (defun org-element-target-interpreter (target contents)
2266 "Interpret TARGET object as Org syntax.
2267 CONTENTS is the contents of target."
2268 (concat ""))
2270 (defun org-element-target-successor (limit)
2271 "Search for the next target and return position.
2273 LIMIT bounds the search.
2275 Return value is a cons cell whose car is `target' and cdr is
2276 beginning position."
2277 (save-excursion
2278 (when (re-search-forward org-target-regexp limit t)
2279 (cons 'target (match-beginning 0)))))
2282 ;;;; Time-stamp
2284 (defun org-element-time-stamp-parser ()
2285 "Parse time stamp at point.
2287 Return a list whose car is `time-stamp', and cdr a plist with
2288 `:appt-type', `:type', `:begin', `:end', `:value' and
2289 `:post-blank' keywords.
2291 Assume point is at the beginning of the time-stamp."
2292 (save-excursion
2293 (let* ((appt-type (cond
2294 ((looking-at (concat org-deadline-string " +"))
2295 (goto-char (match-end 0))
2296 'deadline)
2297 ((looking-at (concat org-scheduled-string " +"))
2298 (goto-char (match-end 0))
2299 'scheduled)
2300 ((looking-at (concat org-closed-string " +"))
2301 (goto-char (match-end 0))
2302 'closed)))
2303 (begin (and appt-type (match-beginning 0)))
2304 (type (cond
2305 ((looking-at org-tsr-regexp)
2306 (if (match-string 2) 'active-range 'active))
2307 ((looking-at org-tsr-regexp-both)
2308 (if (match-string 2) 'inactive-range 'inactive))
2309 ((looking-at (concat
2310 "\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2311 "\\|"
2312 "\\(<%%\\(([^>\n]+)\\)>\\)"))
2313 'diary)))
2314 (begin (or begin (match-beginning 0)))
2315 (value (buffer-substring-no-properties
2316 (match-beginning 0) (match-end 0)))
2317 (post-blank (progn (goto-char (match-end 0))
2318 (skip-chars-forward " \t")))
2319 (end (point)))
2320 `(time-stamp
2321 (:appt-type ,appt-type
2322 :type ,type
2323 :value ,value
2324 :begin ,begin
2325 :end ,end
2326 :post-blank ,post-blank)))))
2328 (defun org-element-time-stamp-interpreter (time-stamp contents)
2329 "Interpret TIME-STAMP object as Org syntax.
2330 CONTENTS is nil."
2331 (concat
2332 (case (org-element-get-property :appt-type time-stamp)
2333 (closed (concat org-closed-string " "))
2334 (deadline (concat org-deadline-string " "))
2335 (scheduled (concat org-scheduled-string " ")))
2336 (org-element-get-property :value time-stamp)))
2338 (defun org-element-time-stamp-successor (limit)
2339 "Search for the next time-stamp and return position.
2341 LIMIT bounds the search.
2343 Return value is a cons cell whose car is `time-stamp' and cdr is
2344 beginning position."
2345 (save-excursion
2346 (when (re-search-forward
2347 (concat "\\(?:" org-scheduled-string " +\\|"
2348 org-deadline-string " +\\|" org-closed-string " +\\)?"
2349 org-ts-regexp-both
2350 "\\|"
2351 "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
2352 "\\|"
2353 "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
2354 limit t)
2355 (cons 'time-stamp (match-beginning 0)))))
2358 ;;;; Verbatim
2360 (defun org-element-verbatim-parser ()
2361 "Parse verbatim object at point.
2363 Return a list whose car is `verbatim' and cdr is a plist with
2364 `:marker', `:begin', `:end' and `:post-blank' keywords.
2366 Assume point is at the first verbatim marker."
2367 (save-excursion
2368 (unless (bolp) (backward-char 1))
2369 (looking-at org-emph-re)
2370 (let ((begin (match-beginning 2))
2371 (marker (org-match-string-no-properties 3))
2372 (value (org-match-string-no-properties 4))
2373 (post-blank (progn (goto-char (match-end 2))
2374 (skip-chars-forward " \t")))
2375 (end (point)))
2376 `(verbatim
2377 (:marker ,marker
2378 :begin ,begin
2379 :end ,end
2380 :value ,value
2381 :post-blank ,post-blank)))))
2383 (defun org-element-verbatim-interpreter (verbatim contents)
2384 "Interpret VERBATIM object as Org syntax.
2385 CONTENTS is nil."
2386 (let ((marker (org-element-get-property :marker verbatim))
2387 (value (org-element-get-property :value verbatim)))
2388 (concat marker value marker)))
2392 ;;; Definitions And Rules
2394 ;; Define elements, greater elements and specify recursive objects,
2395 ;; along with the affiliated keywords recognized. Also set up
2396 ;; restrictions on recursive objects combinations.
2398 ;; These variables really act as a control center for the parsing
2399 ;; process.
2400 (defconst org-element-paragraph-separate
2401 (concat "\f" "\\|" "^[ \t]*$" "\\|"
2402 ;; Headlines and inlinetasks.
2403 org-outline-regexp-bol "\\|"
2404 ;; Comments, blocks (any type), keywords and babel calls.
2405 "^[ \t]*#\\+" "\\|" "^#\\( \\|$\\)" "\\|"
2406 ;; Lists.
2407 (org-item-beginning-re) "\\|"
2408 ;; Fixed-width, drawers (any type) and tables.
2409 "^[ \t]*[:|]" "\\|"
2410 ;; Footnote definitions.
2411 org-footnote-definition-re "\\|"
2412 ;; Horizontal rules.
2413 "^[ \t]*-\\{5,\\}[ \t]*$" "\\|"
2414 ;; LaTeX environments.
2415 "^[ \t]*\\\\\\(begin\\|end\\)")
2416 "Regexp to separate paragraphs in an Org buffer.")
2418 (defconst org-element-all-elements
2419 '(center-block comment comment-block drawer dynamic-block example-block
2420 export-block fixed-width footnote-definition headline
2421 horizontal-rule inlinetask item keyword latex-environment
2422 babel-call paragraph plain-list property-drawer quote-block
2423 quote-section section special-block src-block table
2424 verse-block)
2425 "Complete list of elements.")
2427 (defconst org-element-greater-elements
2428 '(center-block drawer dynamic-block footnote-definition headline inlinetask
2429 item plain-list quote-block section special-block)
2430 "List of recursive element types aka Greater Elements.")
2432 (defconst org-element-all-successors
2433 '(export-snippet footnote-reference inline-babel-call inline-src-block
2434 latex-or-entity line-break link macro radio-target
2435 statistics-cookie sub/superscript target text-markup
2436 time-stamp)
2437 "Complete list of successors.")
2439 (defconst org-element-object-successor-alist
2440 '((subscript . sub/superscript) (superscript . sub/superscript)
2441 (emphasis . text-markup) (verbatim . text-markup)
2442 (entity . latex-or-entity) (latex-fragment . latex-or-entity))
2443 "Alist of translations between object type and successor name.
2445 Sharing the same successor comes handy when, for example, the
2446 regexp matching one object can also match the other object.")
2448 (defconst org-element-recursive-objects
2449 '(emphasis link subscript superscript target radio-target)
2450 "List of recursive object types.")
2452 (defconst org-element-non-recursive-block-alist
2453 '(("ascii" . export-block)
2454 ("comment" . comment-block)
2455 ("docbook" . export-block)
2456 ("example" . example-block)
2457 ("html" . export-block)
2458 ("latex" . latex-block)
2459 ("odt" . export-block)
2460 ("src" . src-block)
2461 ("verse" . verse-block))
2462 "Alist between non-recursive block name and their element type.")
2464 (defconst org-element-affiliated-keywords
2465 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "caption"
2466 "data" "header" "headers" "label" "name" "plot" "resname" "result" "results"
2467 "source" "srcname" "tblname")
2468 "List of affiliated keywords as strings.")
2470 (defconst org-element-keyword-translation-alist
2471 '(("data" . "name") ("label" . "name") ("resname" . "name")
2472 ("source" . "name") ("srcname" . "name") ("tblname" . "name")
2473 ("result" . "results") ("headers" . "header"))
2474 "Alist of usual translations for keywords.
2475 The key is the old name and the value the new one. The property
2476 holding their value will be named after the translated name.")
2478 (defconst org-element-multiple-keywords
2479 '("attr_ascii" "attr_docbook" "attr_html" "attr_latex" "attr_odt" "header")
2480 "List of affiliated keywords that can occur more that once in an element.
2482 Their value will be consed into a list of strings, which will be
2483 returned as the value of the property.
2485 This list is checked after translations have been applied. See
2486 `org-element-keyword-translation-alist'.")
2488 (defconst org-element-parsed-keywords '("author" "caption" "title")
2489 "List of keywords whose value can be parsed.
2491 Their value will be stored as a secondary string: a list of
2492 strings and objects.
2494 This list is checked after translations have been applied. See
2495 `org-element-keyword-translation-alist'.")
2497 (defconst org-element-dual-keywords '("results")
2498 "List of keywords which can have a secondary value.
2500 In Org syntax, they can be written with optional square brackets
2501 before the colons. For example, results keyword can be
2502 associated to a hash value with the following:
2504 #+results[hash-string]: some-source
2506 This list is checked after translations have been applied. See
2507 `org-element-keyword-translation-alist'.")
2509 (defconst org-element-object-restrictions
2510 '((emphasis entity export-snippet inline-babel-call inline-src-block
2511 radio-target sub/superscript target text-markup time-stamp)
2512 (link entity export-snippet inline-babel-call inline-src-block
2513 latex-fragment sub/superscript text-markup)
2514 (radio-target entity export-snippet latex-fragment sub/superscript)
2515 (subscript entity export-snippet inline-babel-call inline-src-block
2516 latex-fragment sub/superscript text-markup)
2517 (superscript entity export-snippet inline-babel-call inline-src-block
2518 latex-fragment sub/superscript text-markup)
2519 (target entity export-snippet latex-fragment sub/superscript text-markup))
2520 "Alist of recursive objects restrictions.
2522 Car is a recursive object type and cdr is a list of successors
2523 that will be called within an object of such type.
2525 For example, in a `radio-target' object, one can only find
2526 entities, export snippets, latex-fragments, subscript and
2527 superscript.")
2529 (defconst org-element-string-restrictions
2530 '((headline entity inline-babel-call latex-fragment link macro radio-target
2531 statistics-cookie sub/superscript text-markup time-stamp)
2532 (inlinetask entity inline-babel-call latex-fragment link macro radio-target
2533 sub/superscript text-markup time-stamp)
2534 (item entity inline-babel-call latex-fragment macro radio-target
2535 sub/superscript target verbatim)
2536 (keyword entity latex-fragment macro sub/superscript text-markup)
2537 (table entity latex-fragment macro text-markup)
2538 (verse entity footnote-reference inline-babel-call inline-src-block
2539 latex-fragment line-break link macro radio-target sub/superscript
2540 target text-markup time-stamp))
2541 "Alist of secondary strings restrictions.
2543 When parsed, some elements have a secondary string which could
2544 contain various objects (i.e. headline's name, or table's cells).
2545 For association, the car is the element type, and the cdr a list
2546 of successors that will be called in that secondary string.
2548 Note: `keyword' secondary string type only applies to keywords
2549 matching `org-element-parsed-keywords'.")
2553 ;;; Accessors
2555 ;; Provide two accessors: `org-element-get-property' and
2556 ;; `org-element-get-contents'.
2558 (defun org-element-get-property (property element)
2559 "Extract the value from the PROPERTY of an ELEMENT."
2560 (plist-get (nth 1 element) property))
2562 (defun org-element-get-contents (element)
2563 "Extract contents from an ELEMENT."
2564 (nthcdr 2 element))
2568 ;; Obtaining The Smallest Element Containing Point
2570 ;; `org-element-at-point' is the core function of this section. It
2571 ;; returns the Lisp representation of the element at point. It uses
2572 ;; `org-element-guess-type' and `org-element-skip-keywords' as helper
2573 ;; functions.
2575 ;; When point is at an item, there is no automatic way to determine if
2576 ;; the function should return the `plain-list' element, or the
2577 ;; corresponding `item' element. By default, `org-element-at-point'
2578 ;; works at the `plain-list' level. But, by providing an optional
2579 ;; argument, one can make it switch to the `item' level.
2581 (defconst org-element--affiliated-re
2582 (format "[ \t]*#\\+\\(%s\\):"
2583 (mapconcat
2584 (lambda (keyword)
2585 (if (member keyword org-element-dual-keywords)
2586 (format "\\(%s\\)\\(?:\\[\\(.*?\\)\\]\\)?"
2587 (regexp-quote keyword))
2588 (regexp-quote keyword)))
2589 org-element-affiliated-keywords "\\|"))
2590 "Regexp matching any affiliated keyword.
2592 Keyword name is put in match group 1. Moreover, if keyword
2593 belongs to `org-element-dual-keywords', put the dual value in
2594 match group 2.
2596 Don't modify it, set `org-element--affiliated-keywords' instead.")
2598 (defun org-element-at-point (&optional special structure)
2599 "Determine closest element around point.
2601 Return value is a list \(TYPE PROPS\) where TYPE is the type of
2602 the element and PROPS a plist of properties associated to the
2603 element.
2605 Possible types are defined in `org-element-all-elements'.
2607 Optional argument SPECIAL, when non-nil, can be either `item' or
2608 `section'. The former allows to parse item wise instead of
2609 plain-list wise, using STRUCTURE as the current list structure.
2610 The latter will try to parse a section before anything else.
2612 If STRUCTURE isn't provided but SPECIAL is set to `item', it will
2613 be computed."
2614 (save-excursion
2615 (beginning-of-line)
2616 ;; Move before any blank line.
2617 (when (looking-at "[ \t]*$")
2618 (skip-chars-backward " \r\t\n")
2619 (beginning-of-line))
2620 (let ((case-fold-search t))
2621 ;; Check if point is at an affiliated keyword. In that case,
2622 ;; try moving to the beginning of the associated element. If
2623 ;; the keyword is orphaned, treat it as plain text.
2624 (when (looking-at org-element--affiliated-re)
2625 (let ((opoint (point)))
2626 (while (looking-at org-element--affiliated-re) (forward-line))
2627 (when (looking-at "[ \t]*$") (goto-char opoint))))
2628 (let ((type (org-element-guess-type (eq special 'section))))
2629 (cond
2630 ;; Guessing element type on the current line is impossible:
2631 ;; try to find the beginning of the current element to get
2632 ;; more information.
2633 ((not type)
2634 (let ((search-origin (point))
2635 (opoint-in-item-p (org-in-item-p))
2636 (par-found-p
2637 (progn
2638 (end-of-line)
2639 (re-search-backward org-element-paragraph-separate nil 'm))))
2640 (cond
2641 ;; Unable to find a paragraph delimiter above: we're at
2642 ;; bob and looking at a paragraph.
2643 ((not par-found-p) (org-element-paragraph-parser))
2644 ;; Trying to find element's beginning set point back to
2645 ;; its original position. There's something peculiar on
2646 ;; this line that prevents parsing, probably an
2647 ;; ill-formed keyword or an undefined drawer name. Parse
2648 ;; it as plain text anyway.
2649 ((< search-origin (point-at-eol)) (org-element-paragraph-parser))
2650 ;; Original point wasn't in a list but previous paragraph
2651 ;; is. It means that either point was inside some block,
2652 ;; or current list was ended without using a blank line.
2653 ;; In the last case, paragraph really starts at list end.
2654 ((let (item)
2655 (and (not opoint-in-item-p)
2656 (not (looking-at "[ \t]*#\\+begin"))
2657 (setq item (org-in-item-p))
2658 (let ((struct (save-excursion (goto-char item)
2659 (org-list-struct))))
2660 (goto-char (org-list-get-bottom-point struct))
2661 (org-skip-whitespace)
2662 (beginning-of-line)
2663 (org-element-paragraph-parser)))))
2664 ((org-footnote-at-definition-p)
2665 (org-element-footnote-definition-parser))
2666 ((and opoint-in-item-p (org-at-item-p) (= opoint-in-item-p (point)))
2667 (if (eq special 'item)
2668 (org-element-item-parser (or structure (org-list-struct)))
2669 (org-element-plain-list-parser (or structure (org-list-struct)))))
2670 ;; In any other case, the paragraph started the line
2671 ;; below.
2672 (t (forward-line) (org-element-paragraph-parser)))))
2673 ((eq type 'plain-list)
2674 (if (eq special 'item)
2675 (org-element-item-parser (or structure (org-list-struct)))
2676 (org-element-plain-list-parser (or structure (org-list-struct)))))
2677 ;; Straightforward case: call the appropriate parser.
2678 (t (funcall (intern (format "org-element-%s-parser" type)))))))))
2681 ;; It is obvious to tell if point is in most elements, either by
2682 ;; looking for a specific regexp in the current line, or by using
2683 ;; already implemented functions. This is the goal of
2684 ;; `org-element-guess-type'.
2686 (defconst org-element--element-block-types
2687 (mapcar 'car org-element-non-recursive-block-alist)
2688 "List of non-recursive block types, as strings.
2689 Used internally by `org-element-guess-type'. Do not modify it
2690 directly, set `org-element-non-recursive-block-alist' instead.")
2692 (defun org-element-guess-type (&optional section-mode)
2693 "Return the type of element at point, or nil if undetermined.
2695 This function may move point to an appropriate position for
2696 parsing. Used internally by `org-element-at-point'.
2698 When optional argument SECTION-MODE is non-nil, try to find if
2699 point is in a section in priority."
2700 ;; Beware: Order matters for some cases in that function.
2701 (beginning-of-line)
2702 (let ((case-fold-search t))
2703 (cond
2704 ((org-with-limited-levels (org-at-heading-p)) 'headline)
2705 ((let ((headline (ignore-errors (nth 4 (org-heading-components)))))
2706 (and headline
2707 (let (case-fold-search)
2708 (string-match (format "^%s\\(?: \\|$\\)" org-quote-string)
2709 headline))))
2710 'quote-section)
2711 ;; Any buffer position not at an headline or in a quote section
2712 ;; is inside a section, provided function is actively looking for
2713 ;; them.
2714 (section-mode 'section)
2715 ;; Non-recursive block.
2716 ((let ((type (org-in-block-p org-element--element-block-types)))
2717 (and type (cdr (assoc type org-element-non-recursive-block-alist)))))
2718 ((org-at-heading-p) 'inlinetask)
2719 ((org-between-regexps-p
2720 "^[ \t]*\\\\begin{" "^[ \t]*\\\\end{[^}]*}[ \t]*") 'latex-environment)
2721 ;; Property drawer. Almost `org-at-property-p', but allow drawer
2722 ;; boundaries.
2723 ((org-with-wide-buffer
2724 (and (not (org-before-first-heading-p))
2725 (let ((pblock (org-get-property-block)))
2726 (and pblock
2727 (<= (point) (cdr pblock))
2728 (>= (point-at-eol) (1- (car pblock)))))))
2729 'property-drawer)
2730 ;; Recursive block. If the block isn't complete, parse the
2731 ;; current part as a paragraph.
2732 ((looking-at "[ \t]*#\\+\\(begin\\|end\\)_\\([-A-Za-z0-9]+\\)\\(?:$\\|\\s-\\)")
2733 (let ((type (downcase (match-string 2))))
2734 (cond
2735 ((not (org-in-block-p (list type))) 'paragraph)
2736 ((string= type "center") 'center-block)
2737 ((string= type "quote") 'quote-block)
2738 (t 'special-block))))
2739 ;; Regular drawers must be tested after property drawer as both
2740 ;; elements share the same ending regexp.
2741 ((or (looking-at org-drawer-regexp) (looking-at "[ \t]*:END:[ \t]*$"))
2742 (let ((completep (org-between-regexps-p
2743 org-drawer-regexp "^[ \t]*:END:[ \t]*$")))
2744 (if (not completep) 'paragraph
2745 (goto-char (car completep)) 'drawer)))
2746 ((looking-at "[ \t]*:\\( \\|$\\)") 'fixed-width)
2747 ;; Babel calls must be tested before general keywords as they are
2748 ;; a subset of them.
2749 ((looking-at org-babel-block-lob-one-liner-regexp) 'babel-call)
2750 ((looking-at org-footnote-definition-re) 'footnote-definition)
2751 ((looking-at "[ \t]*#\\+\\([a-z]+\\(:?_[a-z]+\\)*\\):")
2752 (if (member (downcase (match-string 1)) org-element-affiliated-keywords)
2753 'paragraph
2754 'keyword))
2755 ;; Dynamic block: simplify regexp used for match. If it isn't
2756 ;; complete, parse the current part as a paragraph.
2757 ((looking-at "[ \t]*#\\+\\(begin\\end\\):\\(?:\\s-\\|$\\)")
2758 (let ((completep (org-between-regexps-p
2759 "^[ \t]*#\\+begin:\\(?:\\s-\\|$\\)"
2760 "^[ \t]*#\\+end:\\(?:\\s-\\|$\\)")))
2761 (if (not completep) 'paragraph
2762 (goto-char (car completep)) 'dynamic-block)))
2763 ((looking-at "\\(#\\|[ \t]*#\\+\\( \\|$\\)\\)") 'comment)
2764 ((looking-at "[ \t]*-\\{5,\\}[ \t]*$") 'horizontal-rule)
2765 ((org-at-table-p t) 'table)
2766 ((looking-at "[ \t]*#\\+tblfm:")
2767 (forward-line -1)
2768 ;; A TBLFM line separated from any table is just plain text.
2769 (if (org-at-table-p) 'table
2770 (forward-line) 'paragraph))
2771 ((looking-at (org-item-re)) 'plain-list))))
2773 ;; Most elements can have affiliated keywords. When looking for an
2774 ;; element beginning, we want to move before them, as they belong to
2775 ;; that element, and, in the meantime, collect information they give
2776 ;; into appropriate properties. Hence the following function.
2778 ;; Usage of optional arguments may not be obvious at first glance:
2780 ;; - TRANS-LIST is used to polish keywords names that have evolved
2781 ;; during Org history. In example, even though =result= and
2782 ;; =results= coexist, we want to have them under the same =result=
2783 ;; property. It's also true for "srcname" and "name", where the
2784 ;; latter seems to be preferred nowadays (thus the "name" property).
2786 ;; - CONSED allows to regroup multi-lines keywords under the same
2787 ;; property, while preserving their own identity. This is mostly
2788 ;; used for "attr_latex" and al.
2790 ;; - PARSED prepares a keyword value for export. This is useful for
2791 ;; "caption". Objects restrictions for such keywords are defined in
2792 ;; `org-element-string-restrictions'.
2794 ;; - DUALS is used to take care of keywords accepting a main and an
2795 ;; optional secondary values. For example "results" has its
2796 ;; source's name as the main value, and may have an hash string in
2797 ;; optional square brackets as the secondary one.
2799 ;; A keyword may belong to more than one category.
2801 (defun org-element-collect-affiliated-keywords (&optional key-re trans-list
2802 consed parsed duals)
2803 "Collect affiliated keywords before point.
2805 Optional argument KEY-RE is a regexp matching keywords, which
2806 puts matched keyword in group 1. It defaults to
2807 `org-element--affiliated-re'.
2809 TRANS-LIST is an alist where key is the keyword and value the
2810 property name it should be translated to, without the colons. It
2811 defaults to `org-element-keyword-translation-alist'.
2813 CONSED is a list of strings. Any keyword belonging to that list
2814 will have its value consed. The check is done after keyword
2815 translation. It defaults to `org-element-multiple-keywords'.
2817 PARSED is a list of strings. Any keyword member of this list
2818 will have its value parsed. The check is done after keyword
2819 translation. If a keyword is a member of both CONSED and PARSED,
2820 it's value will be a list of parsed strings. It defaults to
2821 `org-element-parsed-keywords'.
2823 DUALS is a list of strings. Any keyword member of this list can
2824 have two parts: one mandatory and one optional. Its value is
2825 a cons cell whose car is the former, and the cdr the latter. If
2826 a keyword is a member of both PARSED and DUALS, only the primary
2827 part will be parsed. It defaults to `org-element-dual-keywords'.
2829 Return a list whose car is the position at the first of them and
2830 cdr a plist of keywords and values."
2831 (save-excursion
2832 (let ((case-fold-search t)
2833 (key-re (or key-re org-element--affiliated-re))
2834 (trans-list (or trans-list org-element-keyword-translation-alist))
2835 (consed (or consed org-element-multiple-keywords))
2836 (parsed (or parsed org-element-parsed-keywords))
2837 (duals (or duals org-element-dual-keywords))
2838 output)
2839 (unless (bobp)
2840 (while (and (not (bobp))
2841 (progn (forward-line -1) (looking-at key-re)))
2842 (let* ((raw-kwd (downcase (or (match-string 2) (match-string 1))))
2843 ;; Apply translation to RAW-KWD. From there, KWD is
2844 ;; the official keyword.
2845 (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
2846 ;; If KWD is a dual keyword, find it secondary value.
2847 (dual-value (and (member kwd duals)
2848 (org-match-string-no-properties 3)))
2849 ;; Find main value for any keyword.
2850 (value (org-trim (buffer-substring-no-properties
2851 (match-end 0) (point-at-eol))))
2852 ;; Attribute a property name to KWD.
2853 (kwd-sym (and kwd (intern (concat ":" kwd)))))
2854 ;; Now set final shape for VALUE.
2855 (when (member kwd parsed)
2856 (setq value
2857 (org-element-parse-secondary-string
2858 value
2859 (cdr (assq 'keyword org-element-string-restrictions)))))
2860 (when (member kwd duals) (setq value (cons value dual-value)))
2861 (when (member kwd consed)
2862 (setq value (cons value (plist-get output kwd-sym))))
2863 ;; Eventually store the new value in OUTPUT.
2864 (setq output (plist-put output kwd-sym value))))
2865 (unless (looking-at key-re) (forward-line 1)))
2866 (list (point) output))))
2870 ;;; The Org Parser
2872 ;; The two major functions here are `org-element-parse-buffer', which
2873 ;; parses Org syntax inside the current buffer, taking into account
2874 ;; region, narrowing, or even visibility if specified, and
2875 ;; `org-element-parse-secondary-string', which parses objects within
2876 ;; a given string.
2878 ;; The (almost) almighty `org-element-map' allows to apply a function
2879 ;; on elements or objects matching some type, and accumulate the
2880 ;; resulting values. In an export situation, it also skips unneeded
2881 ;; parts of the parse tree, transparently walks into included files,
2882 ;; and maintain a list of local properties (i.e. those inherited from
2883 ;; parent headlines) for function's consumption.
2885 (defun org-element-parse-buffer (&optional granularity visible-only)
2886 "Recursively parse the buffer and return structure.
2887 If narrowing is in effect, only parse the visible part of the
2888 buffer.
2890 Optional argument GRANULARITY determines the depth of the
2891 recursion. It can be set to the following symbols:
2893 `headline' Only parse headlines.
2894 `greater-element' Don't recurse into greater elements. Thus,
2895 elements parsed are the top-level ones.
2896 `element' Parse everything but objects and plain text.
2897 `object' Parse the complete buffer (default).
2899 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
2900 elements.
2902 Assume buffer is in Org mode."
2903 (save-excursion
2904 (goto-char (point-min))
2905 (org-skip-whitespace)
2906 (nconc (list 'org-data nil)
2907 (org-element-parse-elements
2908 (point-at-bol) (point-max)
2909 ;; Start is section mode so text before the first headline
2910 ;; belongs to a section.
2911 'section nil granularity visible-only nil))))
2913 (defun org-element-parse-secondary-string (string restriction &optional buffer)
2914 "Recursively parse objects in STRING and return structure.
2916 RESTRICTION, when non-nil, is a symbol limiting the object types
2917 that will be looked after.
2919 Optional argument BUFFER indicates the buffer from where the
2920 secondary string was extracted. It is used to determine where to
2921 get extraneous information for an object \(i.e. when resolving
2922 a link or looking for a footnote definition\). It defaults to
2923 the current buffer."
2924 (with-temp-buffer
2925 (insert string)
2926 (org-element-parse-objects (point-min) (point-max) nil restriction)))
2928 (defun org-element-map (data types fun &optional info first-match)
2929 "Map a function on selected elements or objects.
2931 DATA is the parsed tree, as returned by, i.e,
2932 `org-element-parse-buffer'. TYPES is a symbol or list of symbols
2933 of elements or objects types. FUN is the function called on the
2934 matching element or object. It must accept two arguments: the
2935 element or object itself and a plist holding contextual
2936 information.
2938 When optional argument INFO is non-nil, it should be a plist
2939 holding export options. In that case, parts of the parse tree
2940 not exportable according to that property list will be skipped
2941 and files included through a keyword will be visited.
2943 When optional argument FIRST-MATCH is non-nil, stop at the first
2944 match for which FUN doesn't return nil, and return that value.
2946 Nil values returned from FUN are ignored in the result."
2947 ;; Ensure TYPES is a list, even of one element.
2948 (unless (listp types) (setq types (list types)))
2949 ;; Recursion depth is determined by --CATEGORY.
2950 (let* ((--category
2951 (cond
2952 ((loop for type in types
2953 always (memq type org-element-greater-elements))
2954 'greater-elements)
2955 ((loop for type in types
2956 always (memq type org-element-all-elements))
2957 'elements)
2958 (t 'objects)))
2959 walk-tree ; For byte-compiler
2960 --acc
2961 (accumulate-maybe
2962 (function
2963 (lambda (--type types fun --blob --local)
2964 ;; Check if TYPE is matching among TYPES. If so, apply
2965 ;; FUN to --BLOB and accumulate return value
2966 ;; into --ACC. --LOCAL is the communication channel.
2967 (when (memq --type types)
2968 (let ((result (funcall fun --blob --local)))
2969 (cond ((not result))
2970 (first-match (throw 'first-match result))
2971 (t (push result --acc))))))))
2972 (walk-tree
2973 (function
2974 (lambda (--data --local)
2975 ;; Recursively walk DATA. --LOCAL, if non-nil, is
2976 ;; a plist holding contextual information.
2977 (mapc
2978 (lambda (--blob)
2979 (let ((--type (if (stringp --blob) 'plain-text (car --blob))))
2980 ;; Determine if a recursion into --BLOB is
2981 ;; possible and allowed.
2982 (cond
2983 ;; Element or object not exportable.
2984 ((and info (org-export-skip-p --blob info)))
2985 ;; Archived headline: Maybe apply fun on it, but
2986 ;; skip contents.
2987 ((and info
2988 (eq --type 'headline)
2989 (eq (plist-get info :with-archived-trees) 'headline)
2990 (org-element-get-property :archivedp --blob))
2991 (funcall accumulate-maybe --type types fun --blob --local))
2992 ;; At an include keyword: apply mapping to its
2993 ;; contents.
2994 ((and --local
2995 (eq --type 'keyword)
2996 (string=
2997 (downcase (org-element-get-property :key --blob))
2998 "include"))
2999 (funcall accumulate-maybe --type types fun --blob --local)
3000 (let* ((--data
3001 (org-export-parse-included-file --blob --local))
3002 (--value (org-element-get-property :value --blob))
3003 (--file
3004 (and (string-match "^\"\\(\\S-+\\)\"" --value)
3005 (match-string 1 --value))))
3006 (funcall
3007 walk-tree --data
3008 (org-combine-plists
3009 --local
3010 ;; Store full path of already included files
3011 ;; to avoid recursive file inclusion.
3012 `(:included-files
3013 ,(cons (expand-file-name --file)
3014 (plist-get --local :included-files))
3015 ;; Ensure that a top-level headline in the
3016 ;; included file becomes a direct child of
3017 ;; the current headline in the buffer.
3018 :headline-offset
3019 ,(- (+ (plist-get
3020 (plist-get --local :inherited-properties)
3021 :level)
3022 (or (plist-get --local :headline-offset) 0))
3023 (1- (org-export-get-min-level
3024 --data --local))))))))
3025 ;; Limiting recursion to greater elements, and --BLOB
3026 ;; isn't one.
3027 ((and (eq --category 'greater-elements)
3028 (not (memq --type org-element-greater-elements)))
3029 (funcall accumulate-maybe --type types fun --blob --local))
3030 ;; Limiting recursion to elements, and --BLOB only
3031 ;; contains objects.
3032 ((and (eq --category 'elements) (eq --type 'paragraph)))
3033 ;; No limitation on recursion, but --BLOB hasn't
3034 ;; got a recursive type.
3035 ((and (eq --category 'objects)
3036 (not (or (eq --type 'paragraph)
3037 (memq --type org-element-greater-elements)
3038 (memq --type org-element-recursive-objects))))
3039 (funcall accumulate-maybe --type types fun --blob --local))
3040 ;; Recursion is possible and allowed: Update local
3041 ;; information and move into --BLOB.
3042 (t (funcall accumulate-maybe --type types fun --blob --local)
3043 (funcall
3044 walk-tree --blob
3045 (org-combine-plists
3046 info `(:genealogy
3047 ,(cons --blob (plist-get info :genealogy)))))))))
3048 (org-element-get-contents --data))))))
3049 (catch 'first-match
3050 (funcall walk-tree data info)
3051 ;; Return value in a proper order.
3052 (reverse --acc))))
3054 ;; The following functions are internal parts of the parser. The
3055 ;; first one, `org-element-parse-elements' acts at the element's
3056 ;; level. The second one, `org-element-parse-objects' applies on all
3057 ;; objects of a paragraph or a secondary string. It uses
3058 ;; `org-element-get-candidates' to optimize the search of the next
3059 ;; object in the buffer.
3061 ;; More precisely, that function looks for every allowed object type
3062 ;; first. Then, it discards failed searches, keeps further matches,
3063 ;; and searches again types matched behind point, for subsequent
3064 ;; calls. Thus, searching for a given type fails only once, and every
3065 ;; object is searched only once at top level (but sometimes more for
3066 ;; nested types).
3068 (defun org-element-parse-elements
3069 (beg end special structure granularity visible-only acc)
3070 "Parse ELEMENT with point at its beginning.
3072 SPECIAL prioritize some elements over the others. It can set to
3073 either `section' or `item', which will focus search,
3074 respectively, on sections and items. Moreover, when value is
3075 `item', STRUCTURE will be used as the current list structure.
3077 GRANULARITY determines the depth of the recursion. It can be set
3078 to the following symbols:
3080 `headline' Only parse headlines.
3081 `greater-element' Don't recurse into greater elements. Thus,
3082 elements parsed are the top-level ones.
3083 `element' Parse everything but objects and plain text.
3084 `object' or nil Parse the complete buffer.
3086 When VISIBLE-ONLY is non-nil, don't parse contents of hidden
3087 elements.
3089 Elements are accumulated into ACC."
3090 (save-excursion
3091 (goto-char beg)
3092 ;; Shortcut when parsing only headlines.
3093 (when (and (eq granularity 'headline) (not (org-at-heading-p)))
3094 (org-with-limited-levels (outline-next-heading)))
3095 ;; Main loop start.
3096 (while (and (< (point) end) (not (eobp)))
3097 (push
3098 ;; 1. Item mode is active: point is at an item. Knowing that,
3099 ;; there's no need to go through `org-element-at-point'.
3100 (if (eq special 'item)
3101 (let* ((element (org-element-item-parser structure))
3102 (cbeg (org-element-get-property :contents-begin element))
3103 (cend (org-element-get-property :contents-end element)))
3104 (goto-char (org-element-get-property :end element))
3105 ;; Narrow region to contents, so that item bullet don't
3106 ;; interfere with paragraph parsing.
3107 (save-restriction
3108 (narrow-to-region cbeg cend)
3109 (org-element-parse-elements
3110 cbeg cend nil structure granularity visible-only
3111 (reverse element))))
3112 ;; 2. When ITEM is nil, find current element's type and parse
3113 ;; it accordingly to its category.
3114 (let ((element (org-element-at-point special structure)))
3115 (goto-char (org-element-get-property :end element))
3116 (cond
3117 ;; Case 1. ELEMENT is a footnote-definition. If
3118 ;; GRANURALITY allows parsing, use narrowing so that
3119 ;; footnote label don't interfere with paragraph
3120 ;; recognition.
3121 ((and (eq (car element) 'footnote-definition)
3122 (not (memq granularity '(headline greater-element))))
3123 (let ((cbeg (org-element-get-property :contents-begin element))
3124 (cend (org-element-get-property :contents-end element)))
3125 (save-restriction
3126 (narrow-to-region cbeg cend)
3127 (org-element-parse-elements
3128 cbeg cend nil structure granularity visible-only
3129 (reverse element)))))
3130 ;; Case 2. ELEMENT is a paragraph. Parse objects inside,
3131 ;; if GRANULARITY allows it.
3132 ((and (eq (car element) 'paragraph)
3133 (or (not granularity) (eq granularity 'object)))
3134 (org-element-parse-objects
3135 (org-element-get-property :contents-begin element)
3136 (org-element-get-property :contents-end element)
3137 (reverse element)
3138 nil))
3139 ;; Case 3. ELEMENT is recursive: parse it between
3140 ;; `contents-begin' and `contents-end'. Make sure
3141 ;; GRANULARITY allows the recursion, or ELEMENT is an
3142 ;; headline, in which case going inside is mandatory, in
3143 ;; order to get sub-level headings. If VISIBLE-ONLY is
3144 ;; true and element is hidden, do not recurse into it.
3145 ((and (memq (car element) org-element-greater-elements)
3146 (or (not granularity)
3147 (memq granularity '(element object))
3148 (eq (car element) 'headline))
3149 (not (and visible-only
3150 (org-element-get-property :hiddenp element))))
3151 (org-element-parse-elements
3152 (org-element-get-property :contents-begin element)
3153 (org-element-get-property :contents-end element)
3154 ;; At a plain list, switch to item mode. At an
3155 ;; headline, switch to section mode. Any other element
3156 ;; turns off special modes.
3157 (case (car element) (plain-list 'item) (headline 'section))
3158 (org-element-get-property :structure element)
3159 granularity
3160 visible-only
3161 (reverse element)))
3162 ;; Case 4. Else, just accumulate ELEMENT.
3163 (t element))))
3164 acc)
3165 (org-skip-whitespace))
3166 ;; Return result.
3167 (nreverse acc)))
3169 (defun org-element-parse-objects (beg end acc restriction)
3170 "Parse objects between BEG and END and return recursive structure.
3172 Objects are accumulated in ACC.
3174 RESTRICTION, when non-nil, is a list of object types which are
3175 allowed in the current object."
3176 (let ((get-next-object
3177 (function
3178 (lambda (cand)
3179 ;; Return the parsing function associated to the nearest
3180 ;; object among list of candidates CAND.
3181 (let ((pos (apply #'min (mapcar #'cdr cand))))
3182 (save-excursion
3183 (goto-char pos)
3184 (funcall
3185 (intern
3186 (format "org-element-%s-parser" (car (rassq pos cand))))))))))
3187 next-object candidates)
3188 (save-excursion
3189 (goto-char beg)
3190 (while (setq candidates (org-element-get-next-object-candidates
3191 end restriction candidates))
3192 (setq next-object (funcall get-next-object candidates))
3193 ;; 1. Text before any object.
3194 (let ((obj-beg (org-element-get-property :begin next-object)))
3195 (unless (= (point) obj-beg)
3196 (push (buffer-substring-no-properties (point) obj-beg) acc)))
3197 ;; 2. Object...
3198 (let ((obj-end (org-element-get-property :end next-object))
3199 (cont-beg (org-element-get-property :contents-begin next-object)))
3200 (push (if (and (memq (car next-object) org-element-recursive-objects)
3201 cont-beg)
3202 ;; ... recursive. The CONT-BEG check is for
3203 ;; links, as some of them might not be recursive
3204 ;; (i.e. plain links).
3205 (save-restriction
3206 (narrow-to-region
3207 cont-beg
3208 (org-element-get-property :contents-end next-object))
3209 (org-element-parse-objects
3210 (point-min) (point-max) (reverse next-object)
3211 ;; Restrict allowed objects. This is the
3212 ;; intersection of current restriction and next
3213 ;; object's restriction.
3214 (let ((new-restr
3215 (cdr (assq (car next-object)
3216 org-element-object-restrictions))))
3217 (if (not restriction) new-restr
3218 (delq nil (mapcar
3219 (lambda (e) (and (memq e restriction) e))
3220 new-restr))))))
3221 ;; ... not recursive.
3222 next-object)
3223 acc)
3224 (goto-char obj-end)))
3225 ;; 3. Text after last object.
3226 (unless (= (point) end)
3227 (push (buffer-substring-no-properties (point) end) acc))
3228 ;; Result.
3229 (nreverse acc))))
3231 (defun org-element-get-next-object-candidates (limit restriction objects)
3232 "Return an alist of candidates for the next object.
3234 LIMIT bounds the search, and RESTRICTION, when non-nil, bounds
3235 the possible object types.
3237 Return value is an alist whose car is position and cdr the object
3238 type, as a string. There is an association for the closest
3239 object of each type within RESTRICTION when non-nil, or for every
3240 type otherwise.
3242 OBJECTS is the previous candidates alist."
3243 (let ((restriction (or restriction org-element-all-successors))
3244 next-candidates types-to-search)
3245 ;; If no previous result, search every object type in RESTRICTION.
3246 ;; Otherwise, keep potential candidates (old objects located after
3247 ;; point) and ask to search again those which had matched before.
3248 (if (not objects) (setq types-to-search restriction)
3249 (mapc (lambda (obj)
3250 (if (< (cdr obj) (point)) (push (car obj) types-to-search)
3251 (push obj next-candidates)))
3252 objects))
3253 ;; Call the appropriate "get-next" function for each type to
3254 ;; search and accumulate matches.
3255 (mapc
3256 (lambda (type)
3257 (let* ((successor-fun
3258 (intern
3259 (format "org-element-%s-successor"
3260 (or (cdr (assq type org-element-object-successor-alist))
3261 type))))
3262 (obj (funcall successor-fun limit)))
3263 (and obj (push obj next-candidates))))
3264 types-to-search)
3265 ;; Return alist.
3266 next-candidates))
3270 ;;; Towards A Bijective Process
3272 ;; The parse tree obtained with `org-element-parse-buffer' is really
3273 ;; a snapshot of the corresponding Org buffer. Therefore, it can be
3274 ;; interpreted and expanded into a string with canonical Org
3275 ;; syntax. Hence `org-element-interpret-data'.
3277 ;; Data parsed from secondary strings, whose shape is slightly
3278 ;; different than the standard parse tree, is expanded with the
3279 ;; equivalent function `org-element-interpret-secondary'.
3281 ;; Both functions rely internally on
3282 ;; `org-element-interpret--affiliated-keywords'.
3284 (defun org-element-interpret-data (data &optional genealogy previous)
3285 "Interpret a parse tree representing Org data.
3287 DATA is the parse tree to interpret.
3289 Optional arguments GENEALOGY and PREVIOUS are used for recursive
3290 calls:
3291 GENEALOGY is the list of its parents types.
3292 PREVIOUS is the type of the element or object at the same level
3293 interpreted before.
3295 Return Org syntax as a string."
3296 (mapconcat
3297 (lambda (blob)
3298 ;; BLOB can be an element, an object, a string, or nil.
3299 (cond
3300 ((not blob) nil)
3301 ((equal blob "") nil)
3302 ((stringp blob) blob)
3304 (let* ((type (car blob))
3305 (interpreter
3306 (if (eq type 'org-data) 'identity
3307 (intern (format "org-element-%s-interpreter" type))))
3308 (contents
3309 (cond
3310 ;; Full Org document.
3311 ((eq type 'org-data)
3312 (org-element-interpret-data blob genealogy previous))
3313 ;; Recursive objects.
3314 ((memq type org-element-recursive-objects)
3315 (org-element-interpret-data
3316 blob (cons type genealogy) nil))
3317 ;; Recursive elements.
3318 ((memq type org-element-greater-elements)
3319 (org-element-normalize-string
3320 (org-element-interpret-data
3321 blob (cons type genealogy) nil)))
3322 ;; Paragraphs.
3323 ((eq type 'paragraph)
3324 (let ((paragraph
3325 (org-element-normalize-contents
3326 blob
3327 ;; When normalizing contents of an item,
3328 ;; ignore first line's indentation.
3329 (and (not previous)
3330 (memq (car genealogy)
3331 '(footnote-definiton item))))))
3332 (org-element-interpret-data
3333 paragraph (cons type genealogy) nil)))))
3334 (results (funcall interpreter blob contents)))
3335 ;; Update PREVIOUS.
3336 (setq previous type)
3337 ;; Build white spaces.
3338 (cond
3339 ((eq type 'org-data) results)
3340 ((memq type org-element-all-elements)
3341 (concat
3342 (org-element-interpret--affiliated-keywords blob)
3343 (org-element-normalize-string results)
3344 (make-string (org-element-get-property :post-blank blob) 10)))
3345 (t (concat
3346 results
3347 (make-string
3348 (org-element-get-property :post-blank blob) 32))))))))
3349 (org-element-get-contents data) ""))
3351 (defun org-element-interpret-secondary (secondary)
3352 "Interpret SECONDARY string as Org syntax.
3354 SECONDARY-STRING is a nested list as returned by
3355 `org-element-parse-secondary-string'.
3357 Return interpreted string."
3358 ;; Make SECONDARY acceptable for `org-element-interpret-data'.
3359 (let ((s (if (listp secondary) secondary (list secondary))))
3360 (org-element-interpret-data `(org-data nil ,@s) nil nil)))
3362 ;; Both functions internally use `org-element--affiliated-keywords'.
3364 (defun org-element-interpret--affiliated-keywords (element)
3365 "Return ELEMENT's affiliated keywords as Org syntax.
3366 If there is no affiliated keyword, return the empty string."
3367 (let ((keyword-to-org
3368 (function
3369 (lambda (key value)
3370 (let (dual)
3371 (when (member key org-element-dual-keywords)
3372 (setq dual (cdr value) value (car value)))
3373 (concat "#+" key (and dual (format "[%s]" dual)) ": "
3374 (if (member key org-element-parsed-keywords)
3375 (org-element-interpret-secondary value)
3376 value)
3377 "\n"))))))
3378 (mapconcat
3379 (lambda (key)
3380 (let ((value (org-element-get-property (intern (concat ":" key)) element)))
3381 (when value
3382 (if (member key org-element-multiple-keywords)
3383 (mapconcat (lambda (line)
3384 (funcall keyword-to-org key line))
3385 value "")
3386 (funcall keyword-to-org key value)))))
3387 ;; Remove translated keywords.
3388 (delq nil
3389 (mapcar
3390 (lambda (key)
3391 (and (not (assoc key org-element-keyword-translation-alist)) key))
3392 org-element-affiliated-keywords))
3393 "")))
3395 ;; Because interpretation of the parse tree must return the same
3396 ;; number of blank lines between elements and the same number of white
3397 ;; space after objects, some special care must be given to white
3398 ;; spaces.
3400 ;; The first function, `org-element-normalize-string', ensures any
3401 ;; string different from the empty string will end with a single
3402 ;; newline character.
3404 ;; The second function, `org-element-normalize-contents', removes
3405 ;; global indentation from the contents of the current element.
3407 (defun org-element-normalize-string (s)
3408 "Ensure string S ends with a single newline character.
3410 If S isn't a string return it unchanged. If S is the empty
3411 string, return it. Otherwise, return a new string with a single
3412 newline character at its end."
3413 (cond
3414 ((not (stringp s)) s)
3415 ((string= "" s) "")
3416 (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
3417 (replace-match "\n" nil nil s)))))
3419 (defun org-element-normalize-contents (element &optional ignore-first)
3420 "Normalize plain text in ELEMENT's contents.
3422 ELEMENT must only contain plain text and objects.
3424 The following changes are applied to plain text:
3425 - Remove global indentation, preserving relative one.
3426 - Untabify it.
3428 If optional argument IGNORE-FIRST is non-nil, ignore first line's
3429 indentation to compute maximal common indentation.
3431 Return the normalized element."
3432 (nconc
3433 (list (car element) (nth 1 element))
3434 (let ((contents (org-element-get-contents element)))
3435 (if (not (or ignore-first (stringp (car contents)))) contents
3436 (catch 'exit
3437 ;; 1. Remove tabs from each string in CONTENTS. Get maximal
3438 ;; common indentation (MCI) along the way.
3439 (let* ((ind-list (unless ignore-first
3440 (list (org-get-string-indentation (car contents)))))
3441 (contents
3442 (mapcar
3443 (lambda (object)
3444 (if (not (stringp object)) object
3445 (let ((start 0)
3446 (object (org-remove-tabs object)))
3447 (while (string-match "\n\\( *\\)" object start)
3448 (setq start (match-end 0))
3449 (push (length (match-string 1 object)) ind-list))
3450 object)))
3451 contents))
3452 (mci (if ind-list (apply 'min ind-list)
3453 (throw 'exit contents))))
3454 ;; 2. Remove that indentation from CONTENTS. First string
3455 ;; must be treated differently because it's the only one
3456 ;; whose indentation doesn't happen after a newline
3457 ;; character.
3458 (let ((first-obj (car contents)))
3459 (unless (or (not (stringp first-obj)) ignore-first)
3460 (setq contents
3461 (cons (replace-regexp-in-string
3462 (format "\\` \\{%d\\}" mci) "" first-obj)
3463 (cdr contents)))))
3464 (mapcar (lambda (object)
3465 (if (not (stringp object)) object
3466 (replace-regexp-in-string
3467 (format "\n \\{%d\\}" mci) "\n" object)))
3468 contents)))))))
3472 ;;; The Toolbox
3474 ;; Once the structure of an Org file is well understood, it's easy to
3475 ;; implement some replacements for `forward-paragraph'
3476 ;; `backward-paragraph', namely `org-element-forward' and
3477 ;; `org-element-backward'.
3479 ;; Also, `org-transpose-elements' mimics the behaviour of
3480 ;; `transpose-words', at the element's level, whereas
3481 ;; `org-element-drag-forward', `org-element-drag-backward', and
3482 ;; `org-element-up' generalize, respectively, functions
3483 ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
3485 ;; `org-element-unindent-buffer' will, as its name almost suggests,
3486 ;; smartly remove global indentation from buffer, making it possible
3487 ;; to use Org indent mode on a file created with hard indentation.
3489 ;; `org-element-nested-p' and `org-element-swap-A-B' are used
3490 ;; internally by some of the previously cited tools.
3492 (defsubst org-element-nested-p (elem-A elem-B)
3493 "Non-nil when elements ELEM-A and ELEM-B are nested."
3494 (let ((beg-A (org-element-get-property :begin elem-A))
3495 (beg-B (org-element-get-property :begin elem-B))
3496 (end-A (org-element-get-property :end elem-A))
3497 (end-B (org-element-get-property :end elem-B)))
3498 (or (and (>= beg-A beg-B) (<= end-A end-B))
3499 (and (>= beg-B beg-A) (<= end-B end-A)))))
3501 (defun org-element-swap-A-B (elem-A elem-B)
3502 "Swap elements ELEM-A and ELEM-B.
3504 Leave point at the end of ELEM-A.
3506 Assume ELEM-A is before ELEM-B and that they are not nested."
3507 (goto-char (org-element-get-property :begin elem-A))
3508 (let* ((beg-B (org-element-get-property :begin elem-B))
3509 (end-B-no-blank (save-excursion
3510 (goto-char (org-element-get-property :end elem-B))
3511 (skip-chars-backward " \r\t\n")
3512 (forward-line)
3513 (point)))
3514 (beg-A (org-element-get-property :begin elem-A))
3515 (end-A-no-blank (save-excursion
3516 (goto-char (org-element-get-property :end elem-A))
3517 (skip-chars-backward " \r\t\n")
3518 (forward-line)
3519 (point)))
3520 (body-A (buffer-substring beg-A end-A-no-blank))
3521 (body-B (buffer-substring beg-B end-B-no-blank))
3522 (between-A-B (buffer-substring end-A-no-blank beg-B)))
3523 (delete-region beg-A end-B-no-blank)
3524 (insert body-B between-A-B body-A)
3525 (goto-char (org-element-get-property :end elem-B))))
3527 (defun org-element-backward ()
3528 "Move backward by one element."
3529 (interactive)
3530 (let* ((opoint (point))
3531 (element (org-element-at-point))
3532 (start-el-beg (org-element-get-property :begin element)))
3533 ;; At an headline. The previous element is the previous sibling,
3534 ;; or the parent if any.
3535 (cond
3536 ;; Already at the beginning of the current element: move to the
3537 ;; beginning of the previous one.
3538 ((= opoint start-el-beg)
3539 (forward-line -1)
3540 (skip-chars-backward " \r\t\n")
3541 (let* ((prev-element (org-element-at-point))
3542 (itemp (org-in-item-p))
3543 (struct (and itemp
3544 (save-excursion (goto-char itemp)
3545 (org-list-struct)))))
3546 ;; When moving into a new list, go directly at the
3547 ;; beginning of the top list structure.
3548 (if (and itemp (<= (org-list-get-bottom-point struct) opoint))
3549 (progn
3550 (goto-char (org-list-get-top-point struct))
3551 (goto-char (org-element-get-property
3552 :begin (org-element-at-point))))
3553 (goto-char (org-element-get-property :begin prev-element))))
3554 (while (org-truely-invisible-p) (org-element-up)))
3555 ;; Else, move at the element beginning. One exception: if point
3556 ;; was in the blank lines after the end of a list, move directly
3557 ;; to the top item.
3559 (let (struct itemp)
3560 (if (and (setq itemp (org-in-item-p))
3561 (<= (org-list-get-bottom-point
3562 (save-excursion (goto-char itemp)
3563 (setq struct (org-list-struct))))
3564 opoint))
3565 (progn (goto-char (org-list-get-top-point struct))
3566 (goto-char (org-element-get-property
3567 :begin (org-element-at-point))))
3568 (goto-char start-el-beg)))))))
3570 (defun org-element-drag-backward ()
3571 "Drag backward element at point."
3572 (interactive)
3573 (let* ((pos (point))
3574 (elem (org-element-at-point)))
3575 (when (= (progn (goto-char (point-min))
3576 (org-skip-whitespace)
3577 (point-at-bol))
3578 (org-element-get-property :end elem))
3579 (error "Cannot drag element backward"))
3580 (goto-char (org-element-get-property :begin elem))
3581 (org-element-backward)
3582 (let ((prev-elem (org-element-at-point)))
3583 (when (or (org-element-nested-p elem prev-elem)
3584 (and (eq (car elem) 'headline)
3585 (not (eq (car prev-elem) 'headline))))
3586 (goto-char pos)
3587 (error "Cannot drag element backward"))
3588 ;; Compute new position of point: it's shifted by PREV-ELEM
3589 ;; body's length.
3590 (let ((size-prev (- (org-element-get-property :end prev-elem)
3591 (org-element-get-property :begin prev-elem))))
3592 (org-element-swap-A-B prev-elem elem)
3593 (goto-char (- pos size-prev))))))
3595 (defun org-element-drag-forward ()
3596 "Move forward element at point."
3597 (interactive)
3598 (let* ((pos (point))
3599 (elem (org-element-at-point)))
3600 (when (= (point-max) (org-element-get-property :end elem))
3601 (error "Cannot drag element forward"))
3602 (goto-char (org-element-get-property :end elem))
3603 (let ((next-elem (org-element-at-point)))
3604 (when (or (org-element-nested-p elem next-elem)
3605 (and (eq (car next-elem) 'headline)
3606 (not (eq (car elem) 'headline))))
3607 (goto-char pos)
3608 (error "Cannot drag element forward"))
3609 ;; Compute new position of point: it's shifted by NEXT-ELEM
3610 ;; body's length (without final blanks) and by the length of
3611 ;; blanks between ELEM and NEXT-ELEM.
3612 (let ((size-next (- (save-excursion
3613 (goto-char (org-element-get-property :end next-elem))
3614 (skip-chars-backward " \r\t\n")
3615 (forward-line)
3616 (point))
3617 (org-element-get-property :begin next-elem)))
3618 (size-blank (- (org-element-get-property :end elem)
3619 (save-excursion
3620 (goto-char (org-element-get-property :end elem))
3621 (skip-chars-backward " \r\t\n")
3622 (forward-line)
3623 (point)))))
3624 (org-element-swap-A-B elem next-elem)
3625 (goto-char (+ pos size-next size-blank))))))
3627 (defun org-element-forward ()
3628 "Move forward by one element."
3629 (interactive)
3630 (beginning-of-line)
3631 (cond ((eobp) (error "Cannot move further down"))
3632 ((looking-at "[ \t]*$")
3633 (org-skip-whitespace)
3634 (goto-char (if (eobp) (point) (point-at-bol))))
3636 (let ((element (org-element-at-point t))
3637 (origin (point)))
3638 (cond
3639 ;; At an item: Either move to the next element inside, or
3640 ;; to its end if it's hidden.
3641 ((eq (car element) 'item)
3642 (if (org-element-get-property :hiddenp element)
3643 (goto-char (org-element-get-property :end element))
3644 (end-of-line)
3645 (re-search-forward org-element-paragraph-separate nil t)
3646 (org-skip-whitespace)
3647 (beginning-of-line)))
3648 ;; At a recursive element: Either move inside, or if it's
3649 ;; hidden, move to its end.
3650 ((memq (car element) org-element-greater-elements)
3651 (let ((cbeg (org-element-get-property :contents-begin element)))
3652 (goto-char
3653 (if (or (org-element-get-property :hiddenp element)
3654 (> origin cbeg))
3655 (org-element-get-property :end element)
3656 cbeg))))
3657 ;; Else: move to the current element's end.
3658 (t (goto-char (org-element-get-property :end element))))))))
3660 (defun org-element-mark-element ()
3661 "Put point at beginning of this element, mark at end.
3663 Interactively, if this command is repeated or (in Transient Mark
3664 mode) if the mark is active, it marks the next element after the
3665 ones already marked."
3666 (interactive)
3667 (let (deactivate-mark)
3668 (if (or (and (eq last-command this-command) (mark t))
3669 (and transient-mark-mode mark-active))
3670 (set-mark
3671 (save-excursion
3672 (goto-char (mark))
3673 (goto-char (org-element-get-property :end (org-element-at-point)))))
3674 (let ((element (org-element-at-point)))
3675 (end-of-line)
3676 (push-mark (org-element-get-property :end element) t t)
3677 (goto-char (org-element-get-property :begin element))))))
3679 (defun org-narrow-to-element ()
3680 "Narrow buffer to current element."
3681 (interactive)
3682 (let ((elem (org-element-at-point)))
3683 (cond
3684 ((eq (car elem) 'headline)
3685 (narrow-to-region
3686 (org-element-get-property :begin elem)
3687 (org-element-get-property :end elem)))
3688 ((memq (car elem) org-element-greater-elements)
3689 (narrow-to-region
3690 (org-element-get-property :contents-begin elem)
3691 (org-element-get-property :contents-end elem)))
3693 (narrow-to-region
3694 (org-element-get-property :begin elem)
3695 (org-element-get-property :end elem))))))
3697 (defun org-transpose-elements ()
3698 "Transpose current and previous elements, keeping blank lines between.
3699 Point is moved after both elements."
3700 (interactive)
3701 (org-skip-whitespace)
3702 (let ((pos (point))
3703 (cur (org-element-at-point)))
3704 (when (= (save-excursion (goto-char (point-min))
3705 (org-skip-whitespace)
3706 (point-at-bol))
3707 (org-element-get-property :begin cur))
3708 (error "No previous element"))
3709 (goto-char (org-element-get-property :begin cur))
3710 (forward-line -1)
3711 (let ((prev (org-element-at-point)))
3712 (when (org-element-nested-p cur prev)
3713 (goto-char pos)
3714 (error "Cannot transpose nested elements"))
3715 (org-element-swap-A-B prev cur))))
3717 (defun org-element-unindent-buffer ()
3718 "Un-indent the visible part of the buffer.
3719 Relative indentation \(between items, inside blocks, etc.\) isn't
3720 modified."
3721 (interactive)
3722 (unless (eq major-mode 'org-mode)
3723 (error "Cannot un-indent a buffer not in Org mode"))
3724 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
3725 unindent-tree ; For byte-compiler.
3726 (unindent-tree
3727 (function
3728 (lambda (contents)
3729 (mapc (lambda (element)
3730 (if (eq (car element) 'headline)
3731 (funcall unindent-tree
3732 (org-element-get-contents element))
3733 (save-excursion
3734 (save-restriction
3735 (narrow-to-region
3736 (org-element-get-property :begin element)
3737 (org-element-get-property :end element))
3738 (org-do-remove-indentation)))))
3739 (reverse contents))))))
3740 (funcall unindent-tree (org-element-get-contents parse-tree))))
3742 (defun org-element-up ()
3743 "Move to upper element.
3744 Return position at the beginning of the upper element."
3745 (interactive)
3746 (let ((opoint (point)) elem)
3747 (cond
3748 ((bobp) (error "No surrounding element"))
3749 ((org-with-limited-levels (org-at-heading-p))
3750 (or (org-up-heading-safe) (error "No surronding element")))
3751 ((and (org-at-item-p)
3752 (setq elem (org-element-at-point))
3753 (let* ((top-list-p (zerop (org-element-get-property :level elem))))
3754 (unless top-list-p
3755 ;; If parent is bound to be in the same list as the
3756 ;; original point, move to that parent.
3757 (let ((struct (org-element-get-property :structure elem)))
3758 (goto-char
3759 (org-list-get-parent
3760 (point-at-bol) struct (org-list-parents-alist struct))))))))
3762 (let* ((elem (or elem (org-element-at-point)))
3763 (end (save-excursion
3764 (goto-char (org-element-get-property :end elem))
3765 (skip-chars-backward " \r\t\n")
3766 (forward-line)
3767 (point)))
3768 prev-elem)
3769 (goto-char (org-element-get-property :begin elem))
3770 (forward-line -1)
3771 (while (and (< (org-element-get-property
3772 :end (setq prev-elem (org-element-at-point)))
3773 end)
3774 (not (bobp)))
3775 (goto-char (org-element-get-property :begin prev-elem))
3776 (forward-line -1))
3777 (if (and (bobp) (< (org-element-get-property :end prev-elem) end))
3778 (progn (goto-char opoint)
3779 (error "No surrounding element"))
3780 (goto-char (org-element-get-property :begin prev-elem))))))))
3783 (provide 'org-element)
3784 ;;; org-element.el ends here