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