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