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