Commit | Line | Data |
---|---|---|
8223b1d2 BG |
1 | ;;; org-element.el --- Parser And Applications for Org syntax |
2 | ||
3 | ;; Copyright (C) 2012 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com> | |
6 | ;; Keywords: outlines, hypermedia, calendar, wp | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation, either version 3 of the License, or | |
13 | ;; (at your option) any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
22 | ||
23 | ;;; Commentary: | |
24 | ;; | |
25 | ;; Org syntax can be divided into three categories: "Greater | |
26 | ;; elements", "Elements" and "Objects". | |
27 | ;; | |
28 | ;; Elements are related to the structure of the document. Indeed, all | |
29 | ;; elements are a cover for the document: each position within belongs | |
30 | ;; to at least one element. | |
31 | ;; | |
32 | ;; An element always starts and ends at the beginning of a line. With | |
33 | ;; a few exceptions (namely `babel-call', `clock', `headline', `item', | |
34 | ;; `keyword', `planning', `property-drawer' and `section' types), it | |
35 | ;; can also accept a fixed set of keywords as attributes. Those are | |
36 | ;; called "affiliated keywords" to distinguish them from other | |
37 | ;; keywords, which are full-fledged elements. Almost all affiliated | |
38 | ;; keywords are referenced in `org-element-affiliated-keywords'; the | |
39 | ;; others are export attributes and start with "ATTR_" prefix. | |
40 | ;; | |
41 | ;; Element containing other elements (and only elements) are called | |
42 | ;; greater elements. Concerned types are: `center-block', `drawer', | |
43 | ;; `dynamic-block', `footnote-definition', `headline', `inlinetask', | |
44 | ;; `item', `plain-list', `quote-block', `section' and `special-block'. | |
45 | ;; | |
46 | ;; Other element types are: `babel-call', `clock', `comment', | |
47 | ;; `comment-block', `example-block', `export-block', `fixed-width', | |
48 | ;; `horizontal-rule', `keyword', `latex-environment', `paragraph', | |
49 | ;; `planning', `property-drawer', `quote-section', `src-block', | |
50 | ;; `table', `table-row' and `verse-block'. Among them, `paragraph' | |
51 | ;; and `verse-block' types can contain Org objects and plain text. | |
52 | ;; | |
53 | ;; Objects are related to document's contents. Some of them are | |
54 | ;; recursive. Associated types are of the following: `bold', `code', | |
55 | ;; `entity', `export-snippet', `footnote-reference', | |
56 | ;; `inline-babel-call', `inline-src-block', `italic', | |
57 | ;; `latex-fragment', `line-break', `link', `macro', `radio-target', | |
58 | ;; `statistics-cookie', `strike-through', `subscript', `superscript', | |
59 | ;; `table-cell', `target', `timestamp', `underline' and `verbatim'. | |
60 | ;; | |
61 | ;; Some elements also have special properties whose value can hold | |
62 | ;; objects themselves (i.e. an item tag or an headline name). Such | |
63 | ;; values are called "secondary strings". Any object belongs to | |
64 | ;; either an element or a secondary string. | |
65 | ;; | |
66 | ;; Notwithstanding affiliated keywords, each greater element, element | |
67 | ;; and object has a fixed set of properties attached to it. Among | |
68 | ;; them, four are shared by all types: `:begin' and `:end', which | |
69 | ;; refer to the beginning and ending buffer positions of the | |
70 | ;; considered element or object, `:post-blank', which holds the number | |
71 | ;; of blank lines, or white spaces, at its end and `:parent' which | |
72 | ;; refers to the element or object containing it. Greater elements | |
73 | ;; and elements containing objects will also have `:contents-begin' | |
74 | ;; and `:contents-end' properties to delimit contents. | |
75 | ;; | |
76 | ;; Lisp-wise, an element or an object can be represented as a list. | |
77 | ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where: | |
78 | ;; TYPE is a symbol describing the Org element or object. | |
79 | ;; PROPERTIES is the property list attached to it. See docstring of | |
80 | ;; appropriate parsing function to get an exhaustive | |
81 | ;; list. | |
82 | ;; CONTENTS is a list of elements, objects or raw strings contained | |
83 | ;; in the current element or object, when applicable. | |
84 | ;; | |
85 | ;; An Org buffer is a nested list of such elements and objects, whose | |
86 | ;; type is `org-data' and properties is nil. | |
87 | ;; | |
88 | ;; The first part of this file defines Org syntax, while the second | |
89 | ;; one provide accessors and setters functions. | |
90 | ;; | |
91 | ;; The next part implements a parser and an interpreter for each | |
92 | ;; element and object type in Org syntax. | |
93 | ;; | |
94 | ;; The following part creates a fully recursive buffer parser. It | |
95 | ;; also provides a tool to map a function to elements or objects | |
96 | ;; matching some criteria in the parse tree. Functions of interest | |
97 | ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser | |
98 | ;; extent, `org-element-parse-secondary-string'. | |
99 | ;; | |
100 | ;; The penultimate part is the cradle of an interpreter for the | |
101 | ;; obtained parse tree: `org-element-interpret-data'. | |
102 | ;; | |
103 | ;; The library ends by furnishing `org-element-at-point' function, and | |
104 | ;; a way to give information about document structure around point | |
105 | ;; with `org-element-context'. | |
106 | ||
107 | ||
108 | ;;; Code: | |
109 | ||
110 | (eval-when-compile | |
111 | (require 'cl)) | |
112 | ||
113 | (require 'org) | |
114 | ||
115 | \f | |
116 | ;;; Definitions And Rules | |
117 | ;; | |
118 | ;; Define elements, greater elements and specify recursive objects, | |
119 | ;; along with the affiliated keywords recognized. Also set up | |
120 | ;; restrictions on recursive objects combinations. | |
121 | ;; | |
122 | ;; These variables really act as a control center for the parsing | |
123 | ;; process. | |
124 | ||
125 | (defconst org-element-paragraph-separate | |
126 | (concat "^\\(?:" | |
127 | ;; Headlines, inlinetasks. | |
128 | org-outline-regexp "\\|" | |
129 | ;; Footnote definitions. | |
130 | "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|" | |
131 | "[ \t]*\\(?:" | |
132 | ;; Empty lines. | |
133 | "$" "\\|" | |
134 | ;; Tables (any type). | |
135 | "\\(?:|\\|\\+-[-+]\\)" "\\|" | |
136 | ;; Blocks (any type), Babel calls, drawers (any type), | |
137 | ;; fixed-width areas and keywords. Note: this is only an | |
138 | ;; indication and need some thorough check. | |
139 | "[#:]" "\\|" | |
140 | ;; Horizontal rules. | |
141 | "-\\{5,\\}[ \t]*$" "\\|" | |
142 | ;; LaTeX environments. | |
143 | "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|" | |
144 | ;; Planning and Clock lines. | |
145 | (regexp-opt (list org-scheduled-string | |
146 | org-deadline-string | |
147 | org-closed-string | |
148 | org-clock-string)) | |
149 | "\\|" | |
150 | ;; Lists. | |
151 | (let ((term (case org-plain-list-ordered-item-terminator | |
152 | (?\) ")") (?. "\\.") (otherwise "[.)]"))) | |
153 | (alpha (and org-alphabetical-lists "\\|[A-Za-z]"))) | |
154 | (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)" | |
155 | "\\(?:[ \t]\\|$\\)")) | |
156 | "\\)\\)") | |
157 | "Regexp to separate paragraphs in an Org buffer. | |
158 | In the case of lines starting with \"#\" and \":\", this regexp | |
159 | is not sufficient to know if point is at a paragraph ending. See | |
160 | `org-element-paragraph-parser' for more information.") | |
161 | ||
162 | (defconst org-element-all-elements | |
163 | '(center-block clock comment comment-block drawer dynamic-block example-block | |
164 | export-block fixed-width footnote-definition headline | |
165 | horizontal-rule inlinetask item keyword latex-environment | |
166 | babel-call paragraph plain-list planning property-drawer | |
167 | quote-block quote-section section special-block src-block table | |
168 | table-row verse-block) | |
169 | "Complete list of element types.") | |
170 | ||
171 | (defconst org-element-greater-elements | |
172 | '(center-block drawer dynamic-block footnote-definition headline inlinetask | |
173 | item plain-list quote-block section special-block table) | |
174 | "List of recursive element types aka Greater Elements.") | |
175 | ||
176 | (defconst org-element-all-successors | |
177 | '(export-snippet footnote-reference inline-babel-call inline-src-block | |
178 | latex-or-entity line-break link macro radio-target | |
179 | statistics-cookie sub/superscript table-cell target | |
180 | text-markup timestamp) | |
181 | "Complete list of successors.") | |
182 | ||
183 | (defconst org-element-object-successor-alist | |
184 | '((subscript . sub/superscript) (superscript . sub/superscript) | |
185 | (bold . text-markup) (code . text-markup) (italic . text-markup) | |
186 | (strike-through . text-markup) (underline . text-markup) | |
187 | (verbatim . text-markup) (entity . latex-or-entity) | |
188 | (latex-fragment . latex-or-entity)) | |
189 | "Alist of translations between object type and successor name. | |
190 | ||
191 | Sharing the same successor comes handy when, for example, the | |
192 | regexp matching one object can also match the other object.") | |
193 | ||
194 | (defconst org-element-all-objects | |
195 | '(bold code entity export-snippet footnote-reference inline-babel-call | |
196 | inline-src-block italic line-break latex-fragment link macro | |
197 | radio-target statistics-cookie strike-through subscript superscript | |
198 | table-cell target timestamp underline verbatim) | |
199 | "Complete list of object types.") | |
200 | ||
201 | (defconst org-element-recursive-objects | |
202 | '(bold italic link macro subscript radio-target strike-through superscript | |
203 | table-cell underline) | |
204 | "List of recursive object types.") | |
205 | ||
206 | (defconst org-element-block-name-alist | |
207 | '(("CENTER" . org-element-center-block-parser) | |
208 | ("COMMENT" . org-element-comment-block-parser) | |
209 | ("EXAMPLE" . org-element-example-block-parser) | |
210 | ("QUOTE" . org-element-quote-block-parser) | |
211 | ("SRC" . org-element-src-block-parser) | |
212 | ("VERSE" . org-element-verse-block-parser)) | |
213 | "Alist between block names and the associated parsing function. | |
214 | Names must be uppercase. Any block whose name has no association | |
215 | is parsed with `org-element-special-block-parser'.") | |
216 | ||
217 | (defconst org-element-affiliated-keywords | |
218 | '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT" | |
219 | "RESULTS" "SOURCE" "SRCNAME" "TBLNAME") | |
220 | "List of affiliated keywords as strings. | |
221 | By default, all keywords setting attributes (i.e. \"ATTR_LATEX\") | |
222 | are affiliated keywords and need not to be in this list.") | |
223 | ||
224 | (defconst org-element--affiliated-re | |
225 | (format "[ \t]*#\\+%s:" | |
226 | ;; Regular affiliated keywords. | |
227 | (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?" | |
228 | (regexp-opt org-element-affiliated-keywords))) | |
229 | "Regexp matching any affiliated keyword. | |
230 | ||
231 | Keyword name is put in match group 1. Moreover, if keyword | |
232 | belongs to `org-element-dual-keywords', put the dual value in | |
233 | match group 2. | |
234 | ||
235 | Don't modify it, set `org-element-affiliated-keywords' instead.") | |
236 | ||
237 | (defconst org-element-keyword-translation-alist | |
238 | '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME") | |
239 | ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME") | |
240 | ("RESULT" . "RESULTS") ("HEADERS" . "HEADER")) | |
241 | "Alist of usual translations for keywords. | |
242 | The key is the old name and the value the new one. The property | |
243 | holding their value will be named after the translated name.") | |
244 | ||
245 | (defconst org-element-multiple-keywords '("HEADER") | |
246 | "List of affiliated keywords that can occur more that once in an element. | |
247 | ||
248 | Their value will be consed into a list of strings, which will be | |
249 | returned as the value of the property. | |
250 | ||
251 | This list is checked after translations have been applied. See | |
252 | `org-element-keyword-translation-alist'. | |
253 | ||
254 | By default, all keywords setting attributes (i.e. \"ATTR_LATEX\") | |
255 | allow multiple occurrences and need not to be in this list.") | |
256 | ||
257 | (defconst org-element-parsed-keywords '("AUTHOR" "CAPTION" "DATE" "TITLE") | |
258 | "List of keywords whose value can be parsed. | |
259 | ||
260 | Their value will be stored as a secondary string: a list of | |
261 | strings and objects. | |
262 | ||
263 | This list is checked after translations have been applied. See | |
264 | `org-element-keyword-translation-alist'.") | |
265 | ||
266 | (defconst org-element-dual-keywords '("CAPTION" "RESULTS") | |
267 | "List of keywords which can have a secondary value. | |
268 | ||
269 | In Org syntax, they can be written with optional square brackets | |
270 | before the colons. For example, results keyword can be | |
271 | associated to a hash value with the following: | |
272 | ||
273 | #+RESULTS[hash-string]: some-source | |
274 | ||
275 | This list is checked after translations have been applied. See | |
276 | `org-element-keyword-translation-alist'.") | |
277 | ||
278 | (defconst org-element-object-restrictions | |
279 | '((bold export-snippet inline-babel-call inline-src-block latex-or-entity link | |
280 | radio-target sub/superscript target text-markup timestamp) | |
281 | (footnote-reference export-snippet footnote-reference inline-babel-call | |
282 | inline-src-block latex-or-entity line-break link macro | |
283 | radio-target sub/superscript target text-markup | |
284 | timestamp) | |
285 | (headline inline-babel-call inline-src-block latex-or-entity link macro | |
286 | radio-target statistics-cookie sub/superscript target text-markup | |
287 | timestamp) | |
288 | (inlinetask inline-babel-call inline-src-block latex-or-entity link macro | |
289 | radio-target sub/superscript target text-markup timestamp) | |
290 | (italic export-snippet inline-babel-call inline-src-block latex-or-entity | |
291 | link radio-target sub/superscript target text-markup timestamp) | |
292 | (item export-snippet footnote-reference inline-babel-call latex-or-entity | |
293 | link macro radio-target sub/superscript target text-markup) | |
294 | (keyword latex-or-entity macro sub/superscript text-markup) | |
295 | (link export-snippet inline-babel-call inline-src-block latex-or-entity link | |
296 | sub/superscript text-markup) | |
297 | (macro macro) | |
298 | (paragraph export-snippet footnote-reference inline-babel-call | |
299 | inline-src-block latex-or-entity line-break link macro | |
300 | radio-target statistics-cookie sub/superscript target text-markup | |
301 | timestamp) | |
302 | (radio-target export-snippet latex-or-entity sub/superscript) | |
303 | (strike-through export-snippet inline-babel-call inline-src-block | |
304 | latex-or-entity link radio-target sub/superscript target | |
305 | text-markup timestamp) | |
306 | (subscript export-snippet inline-babel-call inline-src-block latex-or-entity | |
307 | sub/superscript target text-markup) | |
308 | (superscript export-snippet inline-babel-call inline-src-block | |
309 | latex-or-entity sub/superscript target text-markup) | |
310 | (table-cell export-snippet latex-or-entity link macro radio-target | |
311 | sub/superscript target text-markup timestamp) | |
312 | (table-row table-cell) | |
313 | (underline export-snippet inline-babel-call inline-src-block latex-or-entity | |
314 | link radio-target sub/superscript target text-markup timestamp) | |
315 | (verse-block footnote-reference inline-babel-call inline-src-block | |
316 | latex-or-entity line-break link macro radio-target | |
317 | sub/superscript target text-markup timestamp)) | |
318 | "Alist of objects restrictions. | |
319 | ||
320 | CAR is an element or object type containing objects and CDR is | |
321 | a list of successors that will be called within an element or | |
322 | object of such type. | |
323 | ||
324 | For example, in a `radio-target' object, one can only find | |
325 | entities, export snippets, latex-fragments, subscript and | |
326 | superscript. | |
327 | ||
328 | This alist also applies to secondary string. For example, an | |
329 | `headline' type element doesn't directly contain objects, but | |
330 | still has an entry since one of its properties (`:title') does.") | |
331 | ||
332 | (defconst org-element-secondary-value-alist | |
333 | '((headline . :title) | |
334 | (inlinetask . :title) | |
335 | (item . :tag) | |
336 | (footnote-reference . :inline-definition)) | |
337 | "Alist between element types and location of secondary value.") | |
338 | ||
339 | ||
340 | \f | |
341 | ;;; Accessors and Setters | |
342 | ;; | |
343 | ;; Provide four accessors: `org-element-type', `org-element-property' | |
344 | ;; `org-element-contents' and `org-element-restriction'. | |
345 | ;; | |
346 | ;; Setter functions allow to modify elements by side effect. There is | |
347 | ;; `org-element-put-property', `org-element-set-contents', | |
348 | ;; `org-element-set-element' and `org-element-adopt-element'. Note | |
349 | ;; that `org-element-set-element' and `org-element-adopt-elements' are | |
350 | ;; higher level functions since also update `:parent' property. | |
351 | ||
352 | (defsubst org-element-type (element) | |
353 | "Return type of ELEMENT. | |
354 | ||
355 | The function returns the type of the element or object provided. | |
356 | It can also return the following special value: | |
357 | `plain-text' for a string | |
358 | `org-data' for a complete document | |
359 | nil in any other case." | |
360 | (cond | |
361 | ((not (consp element)) (and (stringp element) 'plain-text)) | |
362 | ((symbolp (car element)) (car element)))) | |
363 | ||
364 | (defsubst org-element-property (property element) | |
365 | "Extract the value from the PROPERTY of an ELEMENT." | |
366 | (plist-get (nth 1 element) property)) | |
367 | ||
368 | (defsubst org-element-contents (element) | |
369 | "Extract contents from an ELEMENT." | |
370 | (and (consp element) (nthcdr 2 element))) | |
371 | ||
372 | (defsubst org-element-restriction (element) | |
373 | "Return restriction associated to ELEMENT. | |
374 | ELEMENT can be an element, an object or a symbol representing an | |
375 | element or object type." | |
376 | (cdr (assq (if (symbolp element) element (org-element-type element)) | |
377 | org-element-object-restrictions))) | |
378 | ||
379 | (defsubst org-element-put-property (element property value) | |
380 | "In ELEMENT set PROPERTY to VALUE. | |
381 | Return modified element." | |
382 | (when (consp element) | |
383 | (setcar (cdr element) (plist-put (nth 1 element) property value))) | |
384 | element) | |
385 | ||
386 | (defsubst org-element-set-contents (element &rest contents) | |
387 | "Set ELEMENT contents to CONTENTS. | |
388 | Return modified element." | |
389 | (cond ((not element) (list contents)) | |
390 | ((cdr element) (setcdr (cdr element) contents)) | |
391 | (t (nconc element contents)))) | |
392 | ||
393 | (defsubst org-element-set-element (old new) | |
394 | "Replace element or object OLD with element or object NEW. | |
395 | The function takes care of setting `:parent' property for NEW." | |
396 | ;; Since OLD is going to be changed into NEW by side-effect, first | |
397 | ;; make sure that every element or object within NEW has OLD as | |
398 | ;; parent. | |
399 | (mapc (lambda (blob) (org-element-put-property blob :parent old)) | |
400 | (org-element-contents new)) | |
401 | ;; Transfer contents. | |
402 | (apply 'org-element-set-contents old (org-element-contents new)) | |
403 | ;; Ensure NEW has same parent as OLD, then overwrite OLD properties | |
404 | ;; with NEW's. | |
405 | (org-element-put-property new :parent (org-element-property :parent old)) | |
406 | (setcar (cdr old) (nth 1 new)) | |
407 | ;; Transfer type. | |
408 | (setcar old (car new))) | |
409 | ||
410 | (defsubst org-element-adopt-elements (parent &rest children) | |
411 | "Append elements to the contents of another element. | |
412 | ||
413 | PARENT is an element or object. CHILDREN can be elements, | |
414 | objects, or a strings. | |
415 | ||
416 | The function takes care of setting `:parent' property for CHILD. | |
417 | Return parent element." | |
418 | (if (not parent) children | |
419 | ;; Link every child to PARENT. | |
420 | (mapc (lambda (child) | |
421 | (unless (stringp child) | |
422 | (org-element-put-property child :parent parent))) | |
423 | children) | |
424 | ;; Add CHILDREN at the end of PARENT contents. | |
425 | (apply 'org-element-set-contents | |
426 | parent | |
427 | (nconc (org-element-contents parent) children)) | |
428 | ;; Return modified PARENT element. | |
429 | parent)) | |
430 | ||
431 | ||
432 | \f | |
433 | ;;; Greater elements | |
434 | ;; | |
435 | ;; For each greater element type, we define a parser and an | |
436 | ;; interpreter. | |
437 | ;; | |
438 | ;; A parser returns the element or object as the list described above. | |
439 | ;; Most of them accepts no argument. Though, exceptions exist. Hence | |
440 | ;; every element containing a secondary string (see | |
441 | ;; `org-element-secondary-value-alist') will accept an optional | |
442 | ;; argument to toggle parsing of that secondary string. Moreover, | |
443 | ;; `item' parser requires current list's structure as its first | |
444 | ;; element. | |
445 | ;; | |
446 | ;; An interpreter accepts two arguments: the list representation of | |
447 | ;; the element or object, and its contents. The latter may be nil, | |
448 | ;; depending on the element or object considered. It returns the | |
449 | ;; appropriate Org syntax, as a string. | |
450 | ;; | |
451 | ;; Parsing functions must follow the naming convention: | |
452 | ;; org-element-TYPE-parser, where TYPE is greater element's type, as | |
453 | ;; defined in `org-element-greater-elements'. | |
454 | ;; | |
455 | ;; Similarly, interpreting functions must follow the naming | |
456 | ;; convention: org-element-TYPE-interpreter. | |
457 | ;; | |
458 | ;; With the exception of `headline' and `item' types, greater elements | |
459 | ;; cannot contain other greater elements of their own type. | |
460 | ;; | |
461 | ;; Beside implementing a parser and an interpreter, adding a new | |
462 | ;; greater element requires to tweak `org-element--current-element'. | |
463 | ;; Moreover, the newly defined type must be added to both | |
464 | ;; `org-element-all-elements' and `org-element-greater-elements'. | |
465 | ||
466 | ||
467 | ;;;; Center Block | |
468 | ||
469 | (defun org-element-center-block-parser (limit) | |
470 | "Parse a center block. | |
471 | ||
472 | LIMIT bounds the search. | |
473 | ||
474 | Return a list whose CAR is `center-block' and CDR is a plist | |
475 | containing `:begin', `:end', `:hiddenp', `:contents-begin', | |
476 | `:contents-end' and `:post-blank' keywords. | |
477 | ||
478 | Assume point is at the beginning of the block." | |
479 | (let ((case-fold-search t)) | |
480 | (if (not (save-excursion | |
481 | (re-search-forward "^[ \t]*#\\+END_CENTER" limit t))) | |
482 | ;; Incomplete block: parse it as a paragraph. | |
483 | (org-element-paragraph-parser limit) | |
484 | (let ((block-end-line (match-beginning 0))) | |
485 | (let* ((keywords (org-element--collect-affiliated-keywords)) | |
486 | (begin (car keywords)) | |
487 | ;; Empty blocks have no contents. | |
488 | (contents-begin (progn (forward-line) | |
489 | (and (< (point) block-end-line) | |
490 | (point)))) | |
491 | (contents-end (and contents-begin block-end-line)) | |
492 | (hidden (org-invisible-p2)) | |
493 | (pos-before-blank (progn (goto-char block-end-line) | |
494 | (forward-line) | |
495 | (point))) | |
496 | (end (save-excursion (skip-chars-forward " \r\t\n" limit) | |
497 | (if (eobp) (point) (point-at-bol))))) | |
498 | (list 'center-block | |
499 | (nconc | |
500 | (list :begin begin | |
501 | :end end | |
502 | :hiddenp hidden | |
503 | :contents-begin contents-begin | |
504 | :contents-end contents-end | |
505 | :post-blank (count-lines pos-before-blank end)) | |
506 | (cadr keywords)))))))) | |
507 | ||
508 | (defun org-element-center-block-interpreter (center-block contents) | |
509 | "Interpret CENTER-BLOCK element as Org syntax. | |
510 | CONTENTS is the contents of the element." | |
511 | (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents)) | |
512 | ||
513 | ||
514 | ;;;; Drawer | |
515 | ||
516 | (defun org-element-drawer-parser (limit) | |
517 | "Parse a drawer. | |
518 | ||
519 | LIMIT bounds the search. | |
520 | ||
521 | Return a list whose CAR is `drawer' and CDR is a plist containing | |
522 | `:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin', | |
523 | `:contents-end' and `:post-blank' keywords. | |
524 | ||
525 | Assume point is at beginning of drawer." | |
526 | (let ((case-fold-search t)) | |
527 | (if (not (save-excursion (re-search-forward "^[ \t]*:END:" limit t))) | |
528 | ;; Incomplete drawer: parse it as a paragraph. | |
529 | (org-element-paragraph-parser limit) | |
530 | (let ((drawer-end-line (match-beginning 0))) | |
531 | (save-excursion | |
532 | (let* ((case-fold-search t) | |
533 | (name (progn (looking-at org-drawer-regexp) | |
534 | (org-match-string-no-properties 1))) | |
535 | (keywords (org-element--collect-affiliated-keywords)) | |
536 | (begin (car keywords)) | |
537 | ;; Empty drawers have no contents. | |
538 | (contents-begin (progn (forward-line) | |
539 | (and (< (point) drawer-end-line) | |
540 | (point)))) | |
541 | (contents-end (and contents-begin drawer-end-line)) | |
542 | (hidden (org-invisible-p2)) | |
543 | (pos-before-blank (progn (goto-char drawer-end-line) | |
544 | (forward-line) | |
545 | (point))) | |
546 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
547 | (if (eobp) (point) (point-at-bol))))) | |
548 | (list 'drawer | |
549 | (nconc | |
550 | (list :begin begin | |
551 | :end end | |
552 | :drawer-name name | |
553 | :hiddenp hidden | |
554 | :contents-begin contents-begin | |
555 | :contents-end contents-end | |
556 | :post-blank (count-lines pos-before-blank end)) | |
557 | (cadr keywords))))))))) | |
558 | ||
559 | (defun org-element-drawer-interpreter (drawer contents) | |
560 | "Interpret DRAWER element as Org syntax. | |
561 | CONTENTS is the contents of the element." | |
562 | (format ":%s:\n%s:END:" | |
563 | (org-element-property :drawer-name drawer) | |
564 | contents)) | |
565 | ||
566 | ||
567 | ;;;; Dynamic Block | |
568 | ||
569 | (defun org-element-dynamic-block-parser (limit) | |
570 | "Parse a dynamic block. | |
571 | ||
572 | LIMIT bounds the search. | |
573 | ||
574 | Return a list whose CAR is `dynamic-block' and CDR is a plist | |
575 | containing `:block-name', `:begin', `:end', `:hiddenp', | |
576 | `:contents-begin', `:contents-end', `:arguments' and | |
577 | `:post-blank' keywords. | |
578 | ||
579 | Assume point is at beginning of dynamic block." | |
580 | (let ((case-fold-search t)) | |
581 | (if (not (save-excursion (re-search-forward org-dblock-end-re limit t))) | |
582 | ;; Incomplete block: parse it as a paragraph. | |
583 | (org-element-paragraph-parser limit) | |
584 | (let ((block-end-line (match-beginning 0))) | |
585 | (save-excursion | |
586 | (let* ((name (progn (looking-at org-dblock-start-re) | |
587 | (org-match-string-no-properties 1))) | |
588 | (arguments (org-match-string-no-properties 3)) | |
589 | (keywords (org-element--collect-affiliated-keywords)) | |
590 | (begin (car keywords)) | |
591 | ;; Empty blocks have no contents. | |
592 | (contents-begin (progn (forward-line) | |
593 | (and (< (point) block-end-line) | |
594 | (point)))) | |
595 | (contents-end (and contents-begin block-end-line)) | |
596 | (hidden (org-invisible-p2)) | |
597 | (pos-before-blank (progn (goto-char block-end-line) | |
598 | (forward-line) | |
599 | (point))) | |
600 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
601 | (if (eobp) (point) (point-at-bol))))) | |
602 | (list 'dynamic-block | |
603 | (nconc | |
604 | (list :begin begin | |
605 | :end end | |
606 | :block-name name | |
607 | :arguments arguments | |
608 | :hiddenp hidden | |
609 | :contents-begin contents-begin | |
610 | :contents-end contents-end | |
611 | :post-blank (count-lines pos-before-blank end)) | |
612 | (cadr keywords))))))))) | |
613 | ||
614 | (defun org-element-dynamic-block-interpreter (dynamic-block contents) | |
615 | "Interpret DYNAMIC-BLOCK element as Org syntax. | |
616 | CONTENTS is the contents of the element." | |
617 | (format "#+BEGIN: %s%s\n%s#+END:" | |
618 | (org-element-property :block-name dynamic-block) | |
619 | (let ((args (org-element-property :arguments dynamic-block))) | |
620 | (and args (concat " " args))) | |
621 | contents)) | |
622 | ||
623 | ||
624 | ;;;; Footnote Definition | |
625 | ||
626 | (defun org-element-footnote-definition-parser (limit) | |
627 | "Parse a footnote definition. | |
628 | ||
629 | LIMIT bounds the search. | |
630 | ||
631 | Return a list whose CAR is `footnote-definition' and CDR is | |
632 | a plist containing `:label', `:begin' `:end', `:contents-begin', | |
633 | `:contents-end' and `:post-blank' keywords. | |
634 | ||
635 | Assume point is at the beginning of the footnote definition." | |
636 | (save-excursion | |
637 | (let* ((label (progn (looking-at org-footnote-definition-re) | |
638 | (org-match-string-no-properties 1))) | |
639 | (keywords (org-element--collect-affiliated-keywords)) | |
640 | (begin (car keywords)) | |
641 | (ending (save-excursion | |
642 | (if (progn | |
643 | (end-of-line) | |
644 | (re-search-forward | |
645 | (concat org-outline-regexp-bol "\\|" | |
646 | org-footnote-definition-re "\\|" | |
647 | "^[ \t]*$") limit 'move)) | |
648 | (match-beginning 0) | |
649 | (point)))) | |
650 | (contents-begin (progn (search-forward "]") | |
651 | (skip-chars-forward " \r\t\n" ending) | |
652 | (and (/= (point) ending) (point)))) | |
653 | (contents-end (and contents-begin ending)) | |
654 | (end (progn (goto-char ending) | |
655 | (skip-chars-forward " \r\t\n" limit) | |
656 | (if (eobp) (point) (point-at-bol))))) | |
657 | (list 'footnote-definition | |
658 | (nconc | |
659 | (list :label label | |
660 | :begin begin | |
661 | :end end | |
662 | :contents-begin contents-begin | |
663 | :contents-end contents-end | |
664 | :post-blank (count-lines ending end)) | |
665 | (cadr keywords)))))) | |
666 | ||
667 | (defun org-element-footnote-definition-interpreter (footnote-definition contents) | |
668 | "Interpret FOOTNOTE-DEFINITION element as Org syntax. | |
669 | CONTENTS is the contents of the footnote-definition." | |
670 | (concat (format "[%s]" (org-element-property :label footnote-definition)) | |
671 | " " | |
672 | contents)) | |
673 | ||
674 | ||
675 | ;;;; Headline | |
676 | ||
677 | (defun org-element-headline-parser (limit &optional raw-secondary-p) | |
678 | "Parse an headline. | |
679 | ||
680 | Return a list whose CAR is `headline' and CDR is a plist | |
681 | containing `:raw-value', `:title', `:begin', `:end', | |
682 | `:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end', | |
683 | `:level', `:priority', `:tags', `:todo-keyword',`:todo-type', | |
684 | `:scheduled', `:deadline', `:timestamp', `:clock', `:category', | |
685 | `:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p' | |
686 | keywords. | |
687 | ||
688 | The plist also contains any property set in the property drawer, | |
689 | with its name in lowercase, the underscores replaced with hyphens | |
690 | and colons at the beginning (i.e. `:custom-id'). | |
691 | ||
692 | When RAW-SECONDARY-P is non-nil, headline's title will not be | |
693 | parsed as a secondary string, but as a plain string instead. | |
694 | ||
695 | Assume point is at beginning of the headline." | |
696 | (save-excursion | |
697 | (let* ((components (org-heading-components)) | |
698 | (level (nth 1 components)) | |
699 | (todo (nth 2 components)) | |
700 | (todo-type | |
701 | (and todo (if (member todo org-done-keywords) 'done 'todo))) | |
702 | (tags (let ((raw-tags (nth 5 components))) | |
703 | (and raw-tags (org-split-string raw-tags ":")))) | |
704 | (raw-value (or (nth 4 components) "")) | |
705 | (quotedp | |
706 | (let ((case-fold-search nil)) | |
707 | (string-match (format "^%s\\( \\|$\\)" org-quote-string) | |
708 | raw-value))) | |
709 | (commentedp | |
710 | (let ((case-fold-search nil)) | |
711 | (string-match (format "^%s\\( \\|$\\)" org-comment-string) | |
712 | raw-value))) | |
713 | (archivedp (member org-archive-tag tags)) | |
714 | (footnote-section-p (and org-footnote-section | |
715 | (string= org-footnote-section raw-value))) | |
716 | ;; Normalize property names: ":SOME_PROP:" becomes | |
717 | ;; ":some-prop". | |
718 | (standard-props (let (plist) | |
719 | (mapc | |
720 | (lambda (p) | |
721 | (let ((p-name (downcase (car p)))) | |
722 | (while (string-match "_" p-name) | |
723 | (setq p-name | |
724 | (replace-match "-" nil nil p-name))) | |
725 | (setq p-name (intern (concat ":" p-name))) | |
726 | (setq plist | |
727 | (plist-put plist p-name (cdr p))))) | |
728 | (org-entry-properties nil 'standard)) | |
729 | plist)) | |
730 | (time-props (org-entry-properties nil 'special "CLOCK")) | |
731 | (scheduled (cdr (assoc "SCHEDULED" time-props))) | |
732 | (deadline (cdr (assoc "DEADLINE" time-props))) | |
733 | (clock (cdr (assoc "CLOCK" time-props))) | |
734 | (timestamp (cdr (assoc "TIMESTAMP" time-props))) | |
735 | (begin (point)) | |
736 | (end (save-excursion (goto-char (org-end-of-subtree t t)))) | |
737 | (pos-after-head (progn (forward-line) (point))) | |
738 | (contents-begin (save-excursion | |
739 | (skip-chars-forward " \r\t\n" end) | |
740 | (and (/= (point) end) (line-beginning-position)))) | |
741 | (hidden (org-invisible-p2)) | |
742 | (contents-end (and contents-begin | |
743 | (progn (goto-char end) | |
744 | (skip-chars-backward " \r\t\n") | |
745 | (forward-line) | |
746 | (point))))) | |
747 | ;; Clean RAW-VALUE from any quote or comment string. | |
748 | (when (or quotedp commentedp) | |
749 | (let ((case-fold-search nil)) | |
750 | (setq raw-value | |
751 | (replace-regexp-in-string | |
752 | (concat | |
753 | (regexp-opt (list org-quote-string org-comment-string)) | |
754 | "\\(?: \\|$\\)") | |
755 | "" | |
756 | raw-value)))) | |
757 | ;; Clean TAGS from archive tag, if any. | |
758 | (when archivedp (setq tags (delete org-archive-tag tags))) | |
759 | (let ((headline | |
760 | (list 'headline | |
761 | (nconc | |
762 | (list :raw-value raw-value | |
763 | :begin begin | |
764 | :end end | |
765 | :pre-blank | |
766 | (if (not contents-begin) 0 | |
767 | (count-lines pos-after-head contents-begin)) | |
768 | :hiddenp hidden | |
769 | :contents-begin contents-begin | |
770 | :contents-end contents-end | |
771 | :level level | |
772 | :priority (nth 3 components) | |
773 | :tags tags | |
774 | :todo-keyword todo | |
775 | :todo-type todo-type | |
776 | :scheduled scheduled | |
777 | :deadline deadline | |
778 | :timestamp timestamp | |
779 | :clock clock | |
780 | :post-blank (count-lines | |
781 | (if (not contents-end) pos-after-head | |
782 | (goto-char contents-end) | |
783 | (forward-line) | |
784 | (point)) | |
785 | end) | |
786 | :footnote-section-p footnote-section-p | |
787 | :archivedp archivedp | |
788 | :commentedp commentedp | |
789 | :quotedp quotedp) | |
790 | standard-props)))) | |
791 | (org-element-put-property | |
792 | headline :title | |
793 | (if raw-secondary-p raw-value | |
794 | (org-element-parse-secondary-string | |
795 | raw-value (org-element-restriction 'headline) headline))))))) | |
796 | ||
797 | (defun org-element-headline-interpreter (headline contents) | |
798 | "Interpret HEADLINE element as Org syntax. | |
799 | CONTENTS is the contents of the element." | |
800 | (let* ((level (org-element-property :level headline)) | |
801 | (todo (org-element-property :todo-keyword headline)) | |
802 | (priority (org-element-property :priority headline)) | |
803 | (title (org-element-interpret-data | |
804 | (org-element-property :title headline))) | |
805 | (tags (let ((tag-list (if (org-element-property :archivedp headline) | |
806 | (cons org-archive-tag | |
807 | (org-element-property :tags headline)) | |
808 | (org-element-property :tags headline)))) | |
809 | (and tag-list | |
810 | (format ":%s:" (mapconcat 'identity tag-list ":"))))) | |
811 | (commentedp (org-element-property :commentedp headline)) | |
812 | (quotedp (org-element-property :quotedp headline)) | |
813 | (pre-blank (or (org-element-property :pre-blank headline) 0)) | |
814 | (heading (concat (make-string level ?*) | |
815 | (and todo (concat " " todo)) | |
816 | (and quotedp (concat " " org-quote-string)) | |
817 | (and commentedp (concat " " org-comment-string)) | |
818 | (and priority | |
819 | (format " [#%s]" (char-to-string priority))) | |
820 | (cond ((and org-footnote-section | |
821 | (org-element-property | |
822 | :footnote-section-p headline)) | |
823 | (concat " " org-footnote-section)) | |
824 | (title (concat " " title)))))) | |
825 | (concat heading | |
826 | ;; Align tags. | |
827 | (when tags | |
828 | (cond | |
829 | ((zerop org-tags-column) (format " %s" tags)) | |
830 | ((< org-tags-column 0) | |
831 | (concat | |
832 | (make-string | |
833 | (max (- (+ org-tags-column (length heading) (length tags))) 1) | |
834 | ? ) | |
835 | tags)) | |
836 | (t | |
837 | (concat | |
838 | (make-string (max (- org-tags-column (length heading)) 1) ? ) | |
839 | tags)))) | |
840 | (make-string (1+ pre-blank) 10) | |
841 | contents))) | |
842 | ||
843 | ||
844 | ;;;; Inlinetask | |
845 | ||
846 | (defun org-element-inlinetask-parser (limit &optional raw-secondary-p) | |
847 | "Parse an inline task. | |
848 | ||
849 | Return a list whose CAR is `inlinetask' and CDR is a plist | |
850 | containing `:title', `:begin', `:end', `:hiddenp', | |
851 | `:contents-begin' and `:contents-end', `:level', `:priority', | |
852 | `:raw-value', `:tags', `:todo-keyword', `:todo-type', | |
853 | `:scheduled', `:deadline', `:timestamp', `:clock' and | |
854 | `:post-blank' keywords. | |
855 | ||
856 | The plist also contains any property set in the property drawer, | |
857 | with its name in lowercase, the underscores replaced with hyphens | |
858 | and colons at the beginning (i.e. `:custom-id'). | |
859 | ||
860 | When optional argument RAW-SECONDARY-P is non-nil, inline-task's | |
861 | title will not be parsed as a secondary string, but as a plain | |
862 | string instead. | |
863 | ||
864 | Assume point is at beginning of the inline task." | |
865 | (save-excursion | |
866 | (let* ((keywords (org-element--collect-affiliated-keywords)) | |
867 | (begin (car keywords)) | |
868 | (components (org-heading-components)) | |
869 | (todo (nth 2 components)) | |
870 | (todo-type (and todo | |
871 | (if (member todo org-done-keywords) 'done 'todo))) | |
872 | (tags (let ((raw-tags (nth 5 components))) | |
873 | (and raw-tags (org-split-string raw-tags ":")))) | |
874 | (raw-value (or (nth 4 components) "")) | |
875 | ;; Normalize property names: ":SOME_PROP:" becomes | |
876 | ;; ":some-prop". | |
877 | (standard-props (let (plist) | |
878 | (mapc | |
879 | (lambda (p) | |
880 | (let ((p-name (downcase (car p)))) | |
881 | (while (string-match "_" p-name) | |
882 | (setq p-name | |
883 | (replace-match "-" nil nil p-name))) | |
884 | (setq p-name (intern (concat ":" p-name))) | |
885 | (setq plist | |
886 | (plist-put plist p-name (cdr p))))) | |
887 | (org-entry-properties nil 'standard)) | |
888 | plist)) | |
889 | (time-props (org-entry-properties nil 'special "CLOCK")) | |
890 | (scheduled (cdr (assoc "SCHEDULED" time-props))) | |
891 | (deadline (cdr (assoc "DEADLINE" time-props))) | |
892 | (clock (cdr (assoc "CLOCK" time-props))) | |
893 | (timestamp (cdr (assoc "TIMESTAMP" time-props))) | |
894 | (task-end (save-excursion | |
895 | (end-of-line) | |
896 | (and (re-search-forward "^\\*+ END" limit t) | |
897 | (match-beginning 0)))) | |
898 | (contents-begin (progn (forward-line) | |
899 | (and task-end (< (point) task-end) (point)))) | |
900 | (hidden (and contents-begin (org-invisible-p2))) | |
901 | (contents-end (and contents-begin task-end)) | |
902 | (before-blank (if (not task-end) (point) | |
903 | (goto-char task-end) | |
904 | (forward-line) | |
905 | (point))) | |
906 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
907 | (if (eobp) (point) (point-at-bol)))) | |
908 | (inlinetask | |
909 | (list 'inlinetask | |
910 | (nconc | |
911 | (list :raw-value raw-value | |
912 | :begin begin | |
913 | :end end | |
914 | :hiddenp hidden | |
915 | :contents-begin contents-begin | |
916 | :contents-end contents-end | |
917 | :level (nth 1 components) | |
918 | :priority (nth 3 components) | |
919 | :tags tags | |
920 | :todo-keyword todo | |
921 | :todo-type todo-type | |
922 | :scheduled scheduled | |
923 | :deadline deadline | |
924 | :timestamp timestamp | |
925 | :clock clock | |
926 | :post-blank (count-lines before-blank end)) | |
927 | standard-props | |
928 | (cadr keywords))))) | |
929 | (org-element-put-property | |
930 | inlinetask :title | |
931 | (if raw-secondary-p raw-value | |
932 | (org-element-parse-secondary-string | |
933 | raw-value | |
934 | (org-element-restriction 'inlinetask) | |
935 | inlinetask)))))) | |
936 | ||
937 | (defun org-element-inlinetask-interpreter (inlinetask contents) | |
938 | "Interpret INLINETASK element as Org syntax. | |
939 | CONTENTS is the contents of inlinetask." | |
940 | (let* ((level (org-element-property :level inlinetask)) | |
941 | (todo (org-element-property :todo-keyword inlinetask)) | |
942 | (priority (org-element-property :priority inlinetask)) | |
943 | (title (org-element-interpret-data | |
944 | (org-element-property :title inlinetask))) | |
945 | (tags (let ((tag-list (org-element-property :tags inlinetask))) | |
946 | (and tag-list | |
947 | (format ":%s:" (mapconcat 'identity tag-list ":"))))) | |
948 | (task (concat (make-string level ?*) | |
949 | (and todo (concat " " todo)) | |
950 | (and priority | |
951 | (format " [#%s]" (char-to-string priority))) | |
952 | (and title (concat " " title))))) | |
953 | (concat task | |
954 | ;; Align tags. | |
955 | (when tags | |
956 | (cond | |
957 | ((zerop org-tags-column) (format " %s" tags)) | |
958 | ((< org-tags-column 0) | |
959 | (concat | |
960 | (make-string | |
961 | (max (- (+ org-tags-column (length task) (length tags))) 1) | |
962 | ? ) | |
963 | tags)) | |
964 | (t | |
965 | (concat | |
966 | (make-string (max (- org-tags-column (length task)) 1) ? ) | |
967 | tags)))) | |
968 | ;; Prefer degenerate inlinetasks when there are no | |
969 | ;; contents. | |
970 | (when contents | |
971 | (concat "\n" | |
972 | contents | |
973 | (make-string level ?*) " END"))))) | |
974 | ||
975 | ||
976 | ;;;; Item | |
977 | ||
978 | (defun org-element-item-parser (limit struct &optional raw-secondary-p) | |
979 | "Parse an item. | |
980 | ||
981 | STRUCT is the structure of the plain list. | |
982 | ||
983 | Return a list whose CAR is `item' and CDR is a plist containing | |
984 | `:bullet', `:begin', `:end', `:contents-begin', `:contents-end', | |
985 | `:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and | |
986 | `:post-blank' keywords. | |
987 | ||
988 | When optional argument RAW-SECONDARY-P is non-nil, item's tag, if | |
989 | any, will not be parsed as a secondary string, but as a plain | |
990 | string instead. | |
991 | ||
992 | Assume point is at the beginning of the item." | |
993 | (save-excursion | |
994 | (beginning-of-line) | |
995 | (looking-at org-list-full-item-re) | |
996 | (let* ((begin (point)) | |
997 | (bullet (org-match-string-no-properties 1)) | |
998 | (checkbox (let ((box (org-match-string-no-properties 3))) | |
999 | (cond ((equal "[ ]" box) 'off) | |
1000 | ((equal "[X]" box) 'on) | |
1001 | ((equal "[-]" box) 'trans)))) | |
1002 | (counter (let ((c (org-match-string-no-properties 2))) | |
1003 | (save-match-data | |
1004 | (cond | |
1005 | ((not c) nil) | |
1006 | ((string-match "[A-Za-z]" c) | |
1007 | (- (string-to-char (upcase (match-string 0 c))) | |
1008 | 64)) | |
1009 | ((string-match "[0-9]+" c) | |
1010 | (string-to-number (match-string 0 c))))))) | |
1011 | (end (save-excursion (goto-char (org-list-get-item-end begin struct)) | |
1012 | (unless (bolp) (forward-line)) | |
1013 | (point))) | |
1014 | (contents-begin | |
1015 | (progn (goto-char | |
1016 | ;; Ignore tags in un-ordered lists: they are just | |
1017 | ;; a part of item's body. | |
1018 | (if (and (match-beginning 4) | |
1019 | (save-match-data (string-match "[.)]" bullet))) | |
1020 | (match-beginning 4) | |
1021 | (match-end 0))) | |
1022 | (skip-chars-forward " \r\t\n" limit) | |
1023 | ;; If first line isn't empty, contents really start | |
1024 | ;; at the text after item's meta-data. | |
1025 | (if (= (point-at-bol) begin) (point) (point-at-bol)))) | |
1026 | (hidden (progn (forward-line) | |
1027 | (and (not (= (point) end)) (org-invisible-p2)))) | |
1028 | (contents-end (progn (goto-char end) | |
1029 | (skip-chars-backward " \r\t\n") | |
1030 | (forward-line) | |
1031 | (point))) | |
1032 | (item | |
1033 | (list 'item | |
1034 | (list :bullet bullet | |
1035 | :begin begin | |
1036 | :end end | |
1037 | ;; CONTENTS-BEGIN and CONTENTS-END may be | |
1038 | ;; mixed up in the case of an empty item | |
1039 | ;; separated from the next by a blank line. | |
1040 | ;; Thus ensure the former is always the | |
1041 | ;; smallest. | |
1042 | :contents-begin (min contents-begin contents-end) | |
1043 | :contents-end (max contents-begin contents-end) | |
1044 | :checkbox checkbox | |
1045 | :counter counter | |
1046 | :hiddenp hidden | |
1047 | :structure struct | |
1048 | :post-blank (count-lines contents-end end))))) | |
1049 | (org-element-put-property | |
1050 | item :tag | |
1051 | (let ((raw-tag (org-list-get-tag begin struct))) | |
1052 | (and raw-tag | |
1053 | (if raw-secondary-p raw-tag | |
1054 | (org-element-parse-secondary-string | |
1055 | raw-tag (org-element-restriction 'item) item)))))))) | |
1056 | ||
1057 | (defun org-element-item-interpreter (item contents) | |
1058 | "Interpret ITEM element as Org syntax. | |
1059 | CONTENTS is the contents of the element." | |
1060 | (let* ((bullet (org-list-bullet-string (org-element-property :bullet item))) | |
1061 | (checkbox (org-element-property :checkbox item)) | |
1062 | (counter (org-element-property :counter item)) | |
1063 | (tag (let ((tag (org-element-property :tag item))) | |
1064 | (and tag (org-element-interpret-data tag)))) | |
1065 | ;; Compute indentation. | |
1066 | (ind (make-string (length bullet) 32)) | |
1067 | (item-starts-with-par-p | |
1068 | (eq (org-element-type (car (org-element-contents item))) | |
1069 | 'paragraph))) | |
1070 | ;; Indent contents. | |
1071 | (concat | |
1072 | bullet | |
1073 | (and counter (format "[@%d] " counter)) | |
1074 | (case checkbox | |
1075 | (on "[X] ") | |
1076 | (off "[ ] ") | |
1077 | (trans "[-] ")) | |
1078 | (and tag (format "%s :: " tag)) | |
1079 | (let ((contents (replace-regexp-in-string | |
1080 | "\\(^\\)[ \t]*\\S-" ind contents nil nil 1))) | |
1081 | (if item-starts-with-par-p (org-trim contents) | |
1082 | (concat "\n" contents)))))) | |
1083 | ||
1084 | ||
1085 | ;;;; Plain List | |
1086 | ||
1087 | (defun org-element-plain-list-parser (limit &optional structure) | |
1088 | "Parse a plain list. | |
1089 | ||
1090 | Optional argument STRUCTURE, when non-nil, is the structure of | |
1091 | the plain list being parsed. | |
1092 | ||
1093 | Return a list whose CAR is `plain-list' and CDR is a plist | |
1094 | containing `:type', `:begin', `:end', `:contents-begin' and | |
1095 | `:contents-end', `:structure' and `:post-blank' keywords. | |
1096 | ||
1097 | Assume point is at the beginning of the list." | |
1098 | (save-excursion | |
1099 | (let* ((struct (or structure (org-list-struct))) | |
1100 | (prevs (org-list-prevs-alist struct)) | |
1101 | (parents (org-list-parents-alist struct)) | |
1102 | (type (org-list-get-list-type (point) struct prevs)) | |
1103 | (contents-begin (point)) | |
1104 | (keywords (org-element--collect-affiliated-keywords)) | |
1105 | (begin (car keywords)) | |
1106 | (contents-end | |
1107 | (progn (goto-char (org-list-get-list-end (point) struct prevs)) | |
1108 | (unless (bolp) (forward-line)) | |
1109 | (point))) | |
1110 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
1111 | (if (eobp) (point) (point-at-bol))))) | |
1112 | ;; Return value. | |
1113 | (list 'plain-list | |
1114 | (nconc | |
1115 | (list :type type | |
1116 | :begin begin | |
1117 | :end end | |
1118 | :contents-begin contents-begin | |
1119 | :contents-end contents-end | |
1120 | :structure struct | |
1121 | :post-blank (count-lines contents-end end)) | |
1122 | (cadr keywords)))))) | |
1123 | ||
1124 | (defun org-element-plain-list-interpreter (plain-list contents) | |
1125 | "Interpret PLAIN-LIST element as Org syntax. | |
1126 | CONTENTS is the contents of the element." | |
1127 | (with-temp-buffer | |
1128 | (insert contents) | |
1129 | (goto-char (point-min)) | |
1130 | (org-list-repair) | |
1131 | (buffer-string))) | |
1132 | ||
1133 | ||
1134 | ;;;; Quote Block | |
1135 | ||
1136 | (defun org-element-quote-block-parser (limit) | |
1137 | "Parse a quote block. | |
1138 | ||
1139 | LIMIT bounds the search. | |
1140 | ||
1141 | Return a list whose CAR is `quote-block' and CDR is a plist | |
1142 | containing `:begin', `:end', `:hiddenp', `:contents-begin', | |
1143 | `:contents-end' and `:post-blank' keywords. | |
1144 | ||
1145 | Assume point is at the beginning of the block." | |
1146 | (let ((case-fold-search t)) | |
1147 | (if (not (save-excursion | |
1148 | (re-search-forward "^[ \t]*#\\+END_QUOTE" limit t))) | |
1149 | ;; Incomplete block: parse it as a paragraph. | |
1150 | (org-element-paragraph-parser limit) | |
1151 | (let ((block-end-line (match-beginning 0))) | |
1152 | (save-excursion | |
1153 | (let* ((keywords (org-element--collect-affiliated-keywords)) | |
1154 | (begin (car keywords)) | |
1155 | ;; Empty blocks have no contents. | |
1156 | (contents-begin (progn (forward-line) | |
1157 | (and (< (point) block-end-line) | |
1158 | (point)))) | |
1159 | (contents-end (and contents-begin block-end-line)) | |
1160 | (hidden (org-invisible-p2)) | |
1161 | (pos-before-blank (progn (goto-char block-end-line) | |
1162 | (forward-line) | |
1163 | (point))) | |
1164 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
1165 | (if (eobp) (point) (point-at-bol))))) | |
1166 | (list 'quote-block | |
1167 | (nconc | |
1168 | (list :begin begin | |
1169 | :end end | |
1170 | :hiddenp hidden | |
1171 | :contents-begin contents-begin | |
1172 | :contents-end contents-end | |
1173 | :post-blank (count-lines pos-before-blank end)) | |
1174 | (cadr keywords))))))))) | |
1175 | ||
1176 | (defun org-element-quote-block-interpreter (quote-block contents) | |
1177 | "Interpret QUOTE-BLOCK element as Org syntax. | |
1178 | CONTENTS is the contents of the element." | |
1179 | (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents)) | |
1180 | ||
1181 | ||
1182 | ;;;; Section | |
1183 | ||
1184 | (defun org-element-section-parser (limit) | |
1185 | "Parse a section. | |
1186 | ||
1187 | LIMIT bounds the search. | |
1188 | ||
1189 | Return a list whose CAR is `section' and CDR is a plist | |
1190 | containing `:begin', `:end', `:contents-begin', `contents-end' | |
1191 | and `:post-blank' keywords." | |
1192 | (save-excursion | |
1193 | ;; Beginning of section is the beginning of the first non-blank | |
1194 | ;; line after previous headline. | |
1195 | (let ((begin (point)) | |
1196 | (end (progn (org-with-limited-levels (outline-next-heading)) | |
1197 | (point))) | |
1198 | (pos-before-blank (progn (skip-chars-backward " \r\t\n") | |
1199 | (forward-line) | |
1200 | (point)))) | |
1201 | (list 'section | |
1202 | (list :begin begin | |
1203 | :end end | |
1204 | :contents-begin begin | |
1205 | :contents-end pos-before-blank | |
1206 | :post-blank (count-lines pos-before-blank end)))))) | |
1207 | ||
1208 | (defun org-element-section-interpreter (section contents) | |
1209 | "Interpret SECTION element as Org syntax. | |
1210 | CONTENTS is the contents of the element." | |
1211 | contents) | |
1212 | ||
1213 | ||
1214 | ;;;; Special Block | |
1215 | ||
1216 | (defun org-element-special-block-parser (limit) | |
1217 | "Parse a special block. | |
1218 | ||
1219 | LIMIT bounds the search. | |
1220 | ||
1221 | Return a list whose CAR is `special-block' and CDR is a plist | |
1222 | containing `:type', `:begin', `:end', `:hiddenp', | |
1223 | `:contents-begin', `:contents-end' and `:post-blank' keywords. | |
1224 | ||
1225 | Assume point is at the beginning of the block." | |
1226 | (let* ((case-fold-search t) | |
1227 | (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)") | |
1228 | (upcase (match-string-no-properties 1))))) | |
1229 | (if (not (save-excursion | |
1230 | (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t))) | |
1231 | ;; Incomplete block: parse it as a paragraph. | |
1232 | (org-element-paragraph-parser limit) | |
1233 | (let ((block-end-line (match-beginning 0))) | |
1234 | (save-excursion | |
1235 | (let* ((keywords (org-element--collect-affiliated-keywords)) | |
1236 | (begin (car keywords)) | |
1237 | ;; Empty blocks have no contents. | |
1238 | (contents-begin (progn (forward-line) | |
1239 | (and (< (point) block-end-line) | |
1240 | (point)))) | |
1241 | (contents-end (and contents-begin block-end-line)) | |
1242 | (hidden (org-invisible-p2)) | |
1243 | (pos-before-blank (progn (goto-char block-end-line) | |
1244 | (forward-line) | |
1245 | (point))) | |
1246 | (end (progn (org-skip-whitespace) | |
1247 | (if (eobp) (point) (point-at-bol))))) | |
1248 | (list 'special-block | |
1249 | (nconc | |
1250 | (list :type type | |
1251 | :begin begin | |
1252 | :end end | |
1253 | :hiddenp hidden | |
1254 | :contents-begin contents-begin | |
1255 | :contents-end contents-end | |
1256 | :post-blank (count-lines pos-before-blank end)) | |
1257 | (cadr keywords))))))))) | |
1258 | ||
1259 | (defun org-element-special-block-interpreter (special-block contents) | |
1260 | "Interpret SPECIAL-BLOCK element as Org syntax. | |
1261 | CONTENTS is the contents of the element." | |
1262 | (let ((block-type (org-element-property :type special-block))) | |
1263 | (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type))) | |
1264 | ||
1265 | ||
1266 | \f | |
1267 | ;;; Elements | |
1268 | ;; | |
1269 | ;; For each element, a parser and an interpreter are also defined. | |
1270 | ;; Both follow the same naming convention used for greater elements. | |
1271 | ;; | |
1272 | ;; Also, as for greater elements, adding a new element type is done | |
1273 | ;; through the following steps: implement a parser and an interpreter, | |
1274 | ;; tweak `org-element--current-element' so that it recognizes the new | |
1275 | ;; type and add that new type to `org-element-all-elements'. | |
1276 | ;; | |
1277 | ;; As a special case, when the newly defined type is a block type, | |
1278 | ;; `org-element-block-name-alist' has to be modified accordingly. | |
1279 | ||
1280 | ||
1281 | ;;;; Babel Call | |
1282 | ||
1283 | (defun org-element-babel-call-parser (limit) | |
1284 | "Parse a babel call. | |
1285 | ||
1286 | LIMIT bounds the search. | |
1287 | ||
1288 | Return a list whose CAR is `babel-call' and CDR is a plist | |
1289 | containing `:begin', `:end', `:info' and `:post-blank' as | |
1290 | keywords." | |
1291 | (save-excursion | |
1292 | (let ((case-fold-search t) | |
1293 | (info (progn (looking-at org-babel-block-lob-one-liner-regexp) | |
1294 | (org-babel-lob-get-info))) | |
1295 | (begin (point-at-bol)) | |
1296 | (pos-before-blank (progn (forward-line) (point))) | |
1297 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
1298 | (if (eobp) (point) (point-at-bol))))) | |
1299 | (list 'babel-call | |
1300 | (list :begin begin | |
1301 | :end end | |
1302 | :info info | |
1303 | :post-blank (count-lines pos-before-blank end)))))) | |
1304 | ||
1305 | (defun org-element-babel-call-interpreter (babel-call contents) | |
1306 | "Interpret BABEL-CALL element as Org syntax. | |
1307 | CONTENTS is nil." | |
1308 | (let* ((babel-info (org-element-property :info babel-call)) | |
1309 | (main (car babel-info)) | |
1310 | (post-options (nth 1 babel-info))) | |
1311 | (concat "#+CALL: " | |
1312 | (if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main | |
1313 | ;; Remove redundant square brackets. | |
1314 | (replace-match (match-string 1 main) nil nil main)) | |
1315 | (and post-options (format "[%s]" post-options))))) | |
1316 | ||
1317 | ||
1318 | ;;;; Clock | |
1319 | ||
1320 | (defun org-element-clock-parser (limit) | |
1321 | "Parse a clock. | |
1322 | ||
1323 | LIMIT bounds the search. | |
1324 | ||
1325 | Return a list whose CAR is `clock' and CDR is a plist containing | |
1326 | `:status', `:value', `:time', `:begin', `:end' and `:post-blank' | |
1327 | as keywords." | |
1328 | (save-excursion | |
1329 | (let* ((case-fold-search nil) | |
1330 | (begin (point)) | |
1331 | (value (progn (search-forward org-clock-string (line-end-position) t) | |
1332 | (org-skip-whitespace) | |
1333 | (looking-at "\\[.*\\]") | |
1334 | (org-match-string-no-properties 0))) | |
1335 | (time (and (progn (goto-char (match-end 0)) | |
1336 | (looking-at " +=> +\\(\\S-+\\)[ \t]*$")) | |
1337 | (org-match-string-no-properties 1))) | |
1338 | (status (if time 'closed 'running)) | |
1339 | (post-blank (let ((before-blank (progn (forward-line) (point)))) | |
1340 | (skip-chars-forward " \r\t\n" limit) | |
1341 | (unless (eobp) (beginning-of-line)) | |
1342 | (count-lines before-blank (point)))) | |
1343 | (end (point))) | |
1344 | (list 'clock | |
1345 | (list :status status | |
1346 | :value value | |
1347 | :time time | |
1348 | :begin begin | |
1349 | :end end | |
1350 | :post-blank post-blank))))) | |
1351 | ||
1352 | (defun org-element-clock-interpreter (clock contents) | |
1353 | "Interpret CLOCK element as Org syntax. | |
1354 | CONTENTS is nil." | |
1355 | (concat org-clock-string " " | |
1356 | (org-element-property :value clock) | |
1357 | (let ((time (org-element-property :time clock))) | |
1358 | (and time | |
1359 | (concat " => " | |
1360 | (apply 'format | |
1361 | "%2s:%02s" | |
1362 | (org-split-string time ":"))))))) | |
1363 | ||
1364 | ||
1365 | ;;;; Comment | |
1366 | ||
1367 | (defun org-element-comment-parser (limit) | |
1368 | "Parse a comment. | |
1369 | ||
1370 | LIMIT bounds the search. | |
1371 | ||
1372 | Return a list whose CAR is `comment' and CDR is a plist | |
1373 | containing `:begin', `:end', `:value' and `:post-blank' | |
1374 | keywords. | |
1375 | ||
1376 | Assume point is at comment beginning." | |
1377 | (save-excursion | |
1378 | (let* ((keywords (org-element--collect-affiliated-keywords)) | |
1379 | (begin (car keywords)) | |
1380 | (value (prog2 (looking-at "[ \t]*# ?") | |
1381 | (buffer-substring-no-properties | |
1382 | (match-end 0) (line-end-position)) | |
1383 | (forward-line))) | |
1384 | (com-end | |
1385 | ;; Get comments ending. | |
1386 | (progn | |
1387 | (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)")) | |
1388 | ;; Accumulate lines without leading hash and first | |
1389 | ;; whitespace. | |
1390 | (setq value | |
1391 | (concat value | |
1392 | "\n" | |
1393 | (buffer-substring-no-properties | |
1394 | (match-end 0) (line-end-position)))) | |
1395 | (forward-line)) | |
1396 | (point))) | |
1397 | (end (progn (goto-char com-end) | |
1398 | (skip-chars-forward " \r\t\n" limit) | |
1399 | (if (eobp) (point) (point-at-bol))))) | |
1400 | (list 'comment | |
1401 | (nconc | |
1402 | (list :begin begin | |
1403 | :end end | |
1404 | :value value | |
1405 | :post-blank (count-lines com-end end)) | |
1406 | (cadr keywords)))))) | |
1407 | ||
1408 | (defun org-element-comment-interpreter (comment contents) | |
1409 | "Interpret COMMENT element as Org syntax. | |
1410 | CONTENTS is nil." | |
1411 | (replace-regexp-in-string "^" "# " (org-element-property :value comment))) | |
1412 | ||
1413 | ||
1414 | ;;;; Comment Block | |
1415 | ||
1416 | (defun org-element-comment-block-parser (limit) | |
1417 | "Parse an export block. | |
1418 | ||
1419 | LIMIT bounds the search. | |
1420 | ||
1421 | Return a list whose CAR is `comment-block' and CDR is a plist | |
1422 | containing `:begin', `:end', `:hiddenp', `:value' and | |
1423 | `:post-blank' keywords. | |
1424 | ||
1425 | Assume point is at comment block beginning." | |
1426 | (let ((case-fold-search t)) | |
1427 | (if (not (save-excursion | |
1428 | (re-search-forward "^[ \t]*#\\+END_COMMENT" limit t))) | |
1429 | ;; Incomplete block: parse it as a paragraph. | |
1430 | (org-element-paragraph-parser limit) | |
1431 | (let ((contents-end (match-beginning 0))) | |
1432 | (save-excursion | |
1433 | (let* ((keywords (org-element--collect-affiliated-keywords)) | |
1434 | (begin (car keywords)) | |
1435 | (contents-begin (progn (forward-line) (point))) | |
1436 | (hidden (org-invisible-p2)) | |
1437 | (pos-before-blank (progn (goto-char contents-end) | |
1438 | (forward-line) | |
1439 | (point))) | |
1440 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
1441 | (if (eobp) (point) (point-at-bol)))) | |
1442 | (value (buffer-substring-no-properties | |
1443 | contents-begin contents-end))) | |
1444 | (list 'comment-block | |
1445 | (nconc | |
1446 | (list :begin begin | |
1447 | :end end | |
1448 | :value value | |
1449 | :hiddenp hidden | |
1450 | :post-blank (count-lines pos-before-blank end)) | |
1451 | (cadr keywords))))))))) | |
1452 | ||
1453 | (defun org-element-comment-block-interpreter (comment-block contents) | |
1454 | "Interpret COMMENT-BLOCK element as Org syntax. | |
1455 | CONTENTS is nil." | |
1456 | (format "#+BEGIN_COMMENT\n%s#+END_COMMENT" | |
1457 | (org-remove-indentation (org-element-property :value comment-block)))) | |
1458 | ||
1459 | ||
1460 | ;;;; Example Block | |
1461 | ||
1462 | (defun org-element-example-block-parser (limit) | |
1463 | "Parse an example block. | |
1464 | ||
1465 | LIMIT bounds the search. | |
1466 | ||
1467 | Return a list whose CAR is `example-block' and CDR is a plist | |
1468 | containing `:begin', `:end', `:number-lines', `:preserve-indent', | |
1469 | `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp', | |
1470 | `:switches', `:value' and `:post-blank' keywords." | |
1471 | (let ((case-fold-search t)) | |
1472 | (if (not (save-excursion | |
1473 | (re-search-forward "^[ \t]*#\\+END_EXAMPLE" limit t))) | |
1474 | ;; Incomplete block: parse it as a paragraph. | |
1475 | (org-element-paragraph-parser limit) | |
1476 | (let ((contents-end (match-beginning 0))) | |
1477 | (save-excursion | |
1478 | (let* ((switches | |
1479 | (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?") | |
1480 | (org-match-string-no-properties 1))) | |
1481 | ;; Switches analysis | |
1482 | (number-lines (cond ((not switches) nil) | |
1483 | ((string-match "-n\\>" switches) 'new) | |
1484 | ((string-match "+n\\>" switches) 'continued))) | |
1485 | (preserve-indent (and switches (string-match "-i\\>" switches))) | |
1486 | ;; Should labels be retained in (or stripped from) example | |
1487 | ;; blocks? | |
1488 | (retain-labels | |
1489 | (or (not switches) | |
1490 | (not (string-match "-r\\>" switches)) | |
1491 | (and number-lines (string-match "-k\\>" switches)))) | |
1492 | ;; What should code-references use - labels or | |
1493 | ;; line-numbers? | |
1494 | (use-labels | |
1495 | (or (not switches) | |
1496 | (and retain-labels (not (string-match "-k\\>" switches))))) | |
1497 | (label-fmt (and switches | |
1498 | (string-match "-l +\"\\([^\"\n]+\\)\"" switches) | |
1499 | (match-string 1 switches))) | |
1500 | ;; Standard block parsing. | |
1501 | (keywords (org-element--collect-affiliated-keywords)) | |
1502 | (begin (car keywords)) | |
1503 | (contents-begin (progn (forward-line) (point))) | |
1504 | (hidden (org-invisible-p2)) | |
1505 | (value (buffer-substring-no-properties contents-begin contents-end)) | |
1506 | (pos-before-blank (progn (goto-char contents-end) | |
1507 | (forward-line) | |
1508 | (point))) | |
1509 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
1510 | (if (eobp) (point) (point-at-bol))))) | |
1511 | (list 'example-block | |
1512 | (nconc | |
1513 | (list :begin begin | |
1514 | :end end | |
1515 | :value value | |
1516 | :switches switches | |
1517 | :number-lines number-lines | |
1518 | :preserve-indent preserve-indent | |
1519 | :retain-labels retain-labels | |
1520 | :use-labels use-labels | |
1521 | :label-fmt label-fmt | |
1522 | :hiddenp hidden | |
1523 | :post-blank (count-lines pos-before-blank end)) | |
1524 | (cadr keywords))))))))) | |
1525 | ||
1526 | (defun org-element-example-block-interpreter (example-block contents) | |
1527 | "Interpret EXAMPLE-BLOCK element as Org syntax. | |
1528 | CONTENTS is nil." | |
1529 | (let ((switches (org-element-property :switches example-block))) | |
1530 | (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n" | |
1531 | (org-remove-indentation | |
1532 | (org-element-property :value example-block)) | |
1533 | "#+END_EXAMPLE"))) | |
1534 | ||
1535 | ||
1536 | ;;;; Export Block | |
1537 | ||
1538 | (defun org-element-export-block-parser (limit) | |
1539 | "Parse an export block. | |
1540 | ||
1541 | LIMIT bounds the search. | |
1542 | ||
1543 | Return a list whose CAR is `export-block' and CDR is a plist | |
1544 | containing `:begin', `:end', `:type', `:hiddenp', `:value' and | |
1545 | `:post-blank' keywords. | |
1546 | ||
1547 | Assume point is at export-block beginning." | |
1548 | (let* ((case-fold-search t) | |
1549 | (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)") | |
1550 | (upcase (org-match-string-no-properties 1))))) | |
1551 | (if (not (save-excursion | |
1552 | (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t))) | |
1553 | ;; Incomplete block: parse it as a paragraph. | |
1554 | (org-element-paragraph-parser limit) | |
1555 | (let ((contents-end (match-beginning 0))) | |
1556 | (save-excursion | |
1557 | (let* ((keywords (org-element--collect-affiliated-keywords)) | |
1558 | (begin (car keywords)) | |
1559 | (contents-begin (progn (forward-line) (point))) | |
1560 | (hidden (org-invisible-p2)) | |
1561 | (pos-before-blank (progn (goto-char contents-end) | |
1562 | (forward-line) | |
1563 | (point))) | |
1564 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
1565 | (if (eobp) (point) (point-at-bol)))) | |
1566 | (value (buffer-substring-no-properties contents-begin | |
1567 | contents-end))) | |
1568 | (list 'export-block | |
1569 | (nconc | |
1570 | (list :begin begin | |
1571 | :end end | |
1572 | :type type | |
1573 | :value value | |
1574 | :hiddenp hidden | |
1575 | :post-blank (count-lines pos-before-blank end)) | |
1576 | (cadr keywords))))))))) | |
1577 | ||
1578 | (defun org-element-export-block-interpreter (export-block contents) | |
1579 | "Interpret EXPORT-BLOCK element as Org syntax. | |
1580 | CONTENTS is nil." | |
1581 | (let ((type (org-element-property :type export-block))) | |
1582 | (concat (format "#+BEGIN_%s\n" type) | |
1583 | (org-element-property :value export-block) | |
1584 | (format "#+END_%s" type)))) | |
1585 | ||
1586 | ||
1587 | ;;;; Fixed-width | |
1588 | ||
1589 | (defun org-element-fixed-width-parser (limit) | |
1590 | "Parse a fixed-width section. | |
1591 | ||
1592 | LIMIT bounds the search. | |
1593 | ||
1594 | Return a list whose CAR is `fixed-width' and CDR is a plist | |
1595 | containing `:begin', `:end', `:value' and `:post-blank' keywords. | |
1596 | ||
1597 | Assume point is at the beginning of the fixed-width area." | |
1598 | (save-excursion | |
1599 | (let* ((keywords (org-element--collect-affiliated-keywords)) | |
1600 | (begin (car keywords)) | |
1601 | value | |
1602 | (end-area | |
1603 | (progn | |
1604 | (while (and (< (point) limit) | |
1605 | (looking-at "[ \t]*:\\( \\|$\\)")) | |
1606 | ;; Accumulate text without starting colons. | |
1607 | (setq value | |
1608 | (concat value | |
1609 | (buffer-substring-no-properties | |
1610 | (match-end 0) (point-at-eol)) | |
1611 | "\n")) | |
1612 | (forward-line)) | |
1613 | (point))) | |
1614 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
1615 | (if (eobp) (point) (point-at-bol))))) | |
1616 | (list 'fixed-width | |
1617 | (nconc | |
1618 | (list :begin begin | |
1619 | :end end | |
1620 | :value value | |
1621 | :post-blank (count-lines end-area end)) | |
1622 | (cadr keywords)))))) | |
1623 | ||
1624 | (defun org-element-fixed-width-interpreter (fixed-width contents) | |
1625 | "Interpret FIXED-WIDTH element as Org syntax. | |
1626 | CONTENTS is nil." | |
1627 | (replace-regexp-in-string | |
1628 | "^" ": " (substring (org-element-property :value fixed-width) 0 -1))) | |
1629 | ||
1630 | ||
1631 | ;;;; Horizontal Rule | |
1632 | ||
1633 | (defun org-element-horizontal-rule-parser (limit) | |
1634 | "Parse an horizontal rule. | |
1635 | ||
1636 | LIMIT bounds the search. | |
1637 | ||
1638 | Return a list whose CAR is `horizontal-rule' and CDR is a plist | |
1639 | containing `:begin', `:end' and `:post-blank' keywords." | |
1640 | (save-excursion | |
1641 | (let* ((keywords (org-element--collect-affiliated-keywords)) | |
1642 | (begin (car keywords)) | |
1643 | (post-hr (progn (forward-line) (point))) | |
1644 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
1645 | (if (eobp) (point) (point-at-bol))))) | |
1646 | (list 'horizontal-rule | |
1647 | (nconc | |
1648 | (list :begin begin | |
1649 | :end end | |
1650 | :post-blank (count-lines post-hr end)) | |
1651 | (cadr keywords)))))) | |
1652 | ||
1653 | (defun org-element-horizontal-rule-interpreter (horizontal-rule contents) | |
1654 | "Interpret HORIZONTAL-RULE element as Org syntax. | |
1655 | CONTENTS is nil." | |
1656 | "-----") | |
1657 | ||
1658 | ||
1659 | ;;;; Keyword | |
1660 | ||
1661 | (defun org-element-keyword-parser (limit) | |
1662 | "Parse a keyword at point. | |
1663 | ||
1664 | LIMIT bounds the search. | |
1665 | ||
1666 | Return a list whose CAR is `keyword' and CDR is a plist | |
1667 | containing `:key', `:value', `:begin', `:end' and `:post-blank' | |
1668 | keywords." | |
1669 | (save-excursion | |
1670 | (let* ((case-fold-search t) | |
1671 | (begin (point)) | |
1672 | (key (progn (looking-at "[ \t]*#\\+\\(\\S-+\\):") | |
1673 | (upcase (org-match-string-no-properties 1)))) | |
1674 | (value (org-trim (buffer-substring-no-properties | |
1675 | (match-end 0) (point-at-eol)))) | |
1676 | (pos-before-blank (progn (forward-line) (point))) | |
1677 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
1678 | (if (eobp) (point) (point-at-bol))))) | |
1679 | (list 'keyword | |
1680 | (list :key key | |
1681 | :value value | |
1682 | :begin begin | |
1683 | :end end | |
1684 | :post-blank (count-lines pos-before-blank end)))))) | |
1685 | ||
1686 | (defun org-element-keyword-interpreter (keyword contents) | |
1687 | "Interpret KEYWORD element as Org syntax. | |
1688 | CONTENTS is nil." | |
1689 | (format "#+%s: %s" | |
1690 | (org-element-property :key keyword) | |
1691 | (org-element-property :value keyword))) | |
1692 | ||
1693 | ||
1694 | ;;;; Latex Environment | |
1695 | ||
1696 | (defun org-element-latex-environment-parser (limit) | |
1697 | "Parse a LaTeX environment. | |
1698 | ||
1699 | LIMIT bounds the search. | |
1700 | ||
1701 | Return a list whose CAR is `latex-environment' and CDR is a plist | |
1702 | containing `:begin', `:end', `:value' and `:post-blank' | |
1703 | keywords. | |
1704 | ||
1705 | Assume point is at the beginning of the latex environment." | |
1706 | (save-excursion | |
1707 | (let* ((case-fold-search t) | |
1708 | (code-begin (point)) | |
1709 | (keywords (org-element--collect-affiliated-keywords)) | |
1710 | (begin (car keywords)) | |
1711 | (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}") | |
1712 | (regexp-quote (match-string 1)))) | |
1713 | (code-end | |
1714 | (progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env) limit t) | |
1715 | (forward-line) | |
1716 | (point))) | |
1717 | (value (buffer-substring-no-properties code-begin code-end)) | |
1718 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
1719 | (if (eobp) (point) (point-at-bol))))) | |
1720 | (list 'latex-environment | |
1721 | (nconc | |
1722 | (list :begin begin | |
1723 | :end end | |
1724 | :value value | |
1725 | :post-blank (count-lines code-end end)) | |
1726 | (cadr keywords)))))) | |
1727 | ||
1728 | (defun org-element-latex-environment-interpreter (latex-environment contents) | |
1729 | "Interpret LATEX-ENVIRONMENT element as Org syntax. | |
1730 | CONTENTS is nil." | |
1731 | (org-element-property :value latex-environment)) | |
1732 | ||
1733 | ||
1734 | ;;;; Paragraph | |
1735 | ||
1736 | (defun org-element-paragraph-parser (limit) | |
1737 | "Parse a paragraph. | |
1738 | ||
1739 | LIMIT bounds the search. | |
1740 | ||
1741 | Return a list whose CAR is `paragraph' and CDR is a plist | |
1742 | containing `:begin', `:end', `:contents-begin' and | |
1743 | `:contents-end' and `:post-blank' keywords. | |
1744 | ||
1745 | Assume point is at the beginning of the paragraph." | |
1746 | (save-excursion | |
1747 | (let* (;; INNER-PAR-P is non-nil when paragraph is at the | |
1748 | ;; beginning of an item or a footnote reference. In that | |
1749 | ;; case, we mustn't look for affiliated keywords since they | |
1750 | ;; belong to the container. | |
1751 | (inner-par-p (not (bolp))) | |
1752 | (contents-begin (point)) | |
1753 | (keywords (unless inner-par-p | |
1754 | (org-element--collect-affiliated-keywords))) | |
1755 | (begin (if inner-par-p contents-begin (car keywords))) | |
1756 | (before-blank | |
1757 | (let ((case-fold-search t)) | |
1758 | (end-of-line) | |
1759 | (re-search-forward org-element-paragraph-separate limit 'm) | |
1760 | (while (and (/= (point) limit) | |
1761 | (cond | |
1762 | ;; Skip non-existent or incomplete drawer. | |
1763 | ((save-excursion | |
1764 | (beginning-of-line) | |
1765 | (and (looking-at "[ \t]*:\\S-") | |
1766 | (or (not (looking-at org-drawer-regexp)) | |
1767 | (not (save-excursion | |
1768 | (re-search-forward | |
1769 | "^[ \t]*:END:" limit t))))))) | |
1770 | ;; Stop at comments. | |
1771 | ((save-excursion | |
1772 | (beginning-of-line) | |
1773 | (not (looking-at "[ \t]*#\\S-"))) nil) | |
1774 | ;; Skip incomplete dynamic blocks. | |
1775 | ((save-excursion | |
1776 | (beginning-of-line) | |
1777 | (looking-at "[ \t]*#\\+BEGIN: ")) | |
1778 | (not (save-excursion | |
1779 | (re-search-forward | |
1780 | "^[ \t]*\\+END:" limit t)))) | |
1781 | ;; Skip incomplete blocks. | |
1782 | ((save-excursion | |
1783 | (beginning-of-line) | |
1784 | (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")) | |
1785 | (not (save-excursion | |
1786 | (re-search-forward | |
1787 | (concat "^[ \t]*#\\+END_" | |
1788 | (match-string 1)) | |
1789 | limit t)))) | |
1790 | ;; Skip incomplete latex environments. | |
1791 | ((save-excursion | |
1792 | (beginning-of-line) | |
1793 | (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")) | |
1794 | (not (save-excursion | |
1795 | (re-search-forward | |
1796 | (format "^[ \t]*\\\\end{%s}" | |
1797 | (match-string 1)) | |
1798 | limit t)))) | |
1799 | ;; Skip ill-formed keywords. | |
1800 | ((not (save-excursion | |
1801 | (beginning-of-line) | |
1802 | (looking-at "[ \t]*#\\+\\S-+:")))))) | |
1803 | (re-search-forward org-element-paragraph-separate limit 'm)) | |
1804 | (if (eobp) (point) (goto-char (line-beginning-position))))) | |
1805 | (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin) | |
1806 | (forward-line) | |
1807 | (point))) | |
1808 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
1809 | (if (eobp) (point) (point-at-bol))))) | |
1810 | (list 'paragraph | |
1811 | (nconc | |
1812 | (list :begin begin | |
1813 | :end end | |
1814 | :contents-begin contents-begin | |
1815 | :contents-end contents-end | |
1816 | :post-blank (count-lines before-blank end)) | |
1817 | (cadr keywords)))))) | |
1818 | ||
1819 | (defun org-element-paragraph-interpreter (paragraph contents) | |
1820 | "Interpret PARAGRAPH element as Org syntax. | |
1821 | CONTENTS is the contents of the element." | |
1822 | contents) | |
1823 | ||
1824 | ||
1825 | ;;;; Planning | |
1826 | ||
1827 | (defun org-element-planning-parser (limit) | |
1828 | "Parse a planning. | |
1829 | ||
1830 | LIMIT bounds the search. | |
1831 | ||
1832 | Return a list whose CAR is `planning' and CDR is a plist | |
1833 | containing `:closed', `:deadline', `:scheduled', `:begin', `:end' | |
1834 | and `:post-blank' keywords." | |
1835 | (save-excursion | |
1836 | (let* ((case-fold-search nil) | |
1837 | (begin (point)) | |
1838 | (post-blank (let ((before-blank (progn (forward-line) (point)))) | |
1839 | (skip-chars-forward " \r\t\n" limit) | |
1840 | (unless (eobp) (beginning-of-line)) | |
1841 | (count-lines before-blank (point)))) | |
1842 | (end (point)) | |
1843 | closed deadline scheduled) | |
1844 | (goto-char begin) | |
1845 | (while (re-search-forward org-keyword-time-not-clock-regexp | |
1846 | (line-end-position) t) | |
1847 | (goto-char (match-end 1)) | |
1848 | (org-skip-whitespace) | |
1849 | (let ((time (buffer-substring-no-properties | |
1850 | (1+ (point)) (1- (match-end 0)))) | |
1851 | (keyword (match-string 1))) | |
1852 | (cond ((equal keyword org-closed-string) (setq closed time)) | |
1853 | ((equal keyword org-deadline-string) (setq deadline time)) | |
1854 | (t (setq scheduled time))))) | |
1855 | (list 'planning | |
1856 | (list :closed closed | |
1857 | :deadline deadline | |
1858 | :scheduled scheduled | |
1859 | :begin begin | |
1860 | :end end | |
1861 | :post-blank post-blank))))) | |
1862 | ||
1863 | (defun org-element-planning-interpreter (planning contents) | |
1864 | "Interpret PLANNING element as Org syntax. | |
1865 | CONTENTS is nil." | |
1866 | (mapconcat | |
1867 | 'identity | |
1868 | (delq nil | |
1869 | (list (let ((closed (org-element-property :closed planning))) | |
1870 | (when closed (concat org-closed-string " [" closed "]"))) | |
1871 | (let ((deadline (org-element-property :deadline planning))) | |
1872 | (when deadline (concat org-deadline-string " <" deadline ">"))) | |
1873 | (let ((scheduled (org-element-property :scheduled planning))) | |
1874 | (when scheduled | |
1875 | (concat org-scheduled-string " <" scheduled ">"))))) | |
1876 | " ")) | |
1877 | ||
1878 | ||
1879 | ;;;; Property Drawer | |
1880 | ||
1881 | (defun org-element-property-drawer-parser (limit) | |
1882 | "Parse a property drawer. | |
1883 | ||
1884 | LIMIT bounds the search. | |
1885 | ||
1886 | Return a list whose CAR is `property-drawer' and CDR is a plist | |
1887 | containing `:begin', `:end', `:hiddenp', `:contents-begin', | |
1888 | `:contents-end', `:properties' and `:post-blank' keywords. | |
1889 | ||
1890 | Assume point is at the beginning of the property drawer." | |
1891 | (save-excursion | |
1892 | (let ((case-fold-search t) | |
1893 | (begin (point)) | |
1894 | (prop-begin (progn (forward-line) (point))) | |
1895 | (hidden (org-invisible-p2)) | |
1896 | (properties | |
1897 | (let (val) | |
1898 | (while (not (looking-at "^[ \t]*:END:")) | |
1899 | (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):") | |
1900 | (push (cons (org-match-string-no-properties 1) | |
1901 | (org-trim | |
1902 | (buffer-substring-no-properties | |
1903 | (match-end 0) (point-at-eol)))) | |
1904 | val)) | |
1905 | (forward-line)) | |
1906 | val)) | |
1907 | (prop-end (progn (re-search-forward "^[ \t]*:END:" limit t) | |
1908 | (point-at-bol))) | |
1909 | (pos-before-blank (progn (forward-line) (point))) | |
1910 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
1911 | (if (eobp) (point) (point-at-bol))))) | |
1912 | (list 'property-drawer | |
1913 | (list :begin begin | |
1914 | :end end | |
1915 | :hiddenp hidden | |
1916 | :properties properties | |
1917 | :post-blank (count-lines pos-before-blank end)))))) | |
1918 | ||
1919 | (defun org-element-property-drawer-interpreter (property-drawer contents) | |
1920 | "Interpret PROPERTY-DRAWER element as Org syntax. | |
1921 | CONTENTS is nil." | |
1922 | (let ((props (org-element-property :properties property-drawer))) | |
1923 | (concat | |
1924 | ":PROPERTIES:\n" | |
1925 | (mapconcat (lambda (p) | |
1926 | (format org-property-format (format ":%s:" (car p)) (cdr p))) | |
1927 | (nreverse props) "\n") | |
1928 | "\n:END:"))) | |
1929 | ||
1930 | ||
1931 | ;;;; Quote Section | |
1932 | ||
1933 | (defun org-element-quote-section-parser (limit) | |
1934 | "Parse a quote section. | |
1935 | ||
1936 | LIMIT bounds the search. | |
1937 | ||
1938 | Return a list whose CAR is `quote-section' and CDR is a plist | |
1939 | containing `:begin', `:end', `:value' and `:post-blank' keywords. | |
1940 | ||
1941 | Assume point is at beginning of the section." | |
1942 | (save-excursion | |
1943 | (let* ((begin (point)) | |
1944 | (end (progn (org-with-limited-levels (outline-next-heading)) | |
1945 | (point))) | |
1946 | (pos-before-blank (progn (skip-chars-backward " \r\t\n") | |
1947 | (forward-line) | |
1948 | (point))) | |
1949 | (value (buffer-substring-no-properties begin pos-before-blank))) | |
1950 | (list 'quote-section | |
1951 | (list :begin begin | |
1952 | :end end | |
1953 | :value value | |
1954 | :post-blank (count-lines pos-before-blank end)))))) | |
1955 | ||
1956 | (defun org-element-quote-section-interpreter (quote-section contents) | |
1957 | "Interpret QUOTE-SECTION element as Org syntax. | |
1958 | CONTENTS is nil." | |
1959 | (org-element-property :value quote-section)) | |
1960 | ||
1961 | ||
1962 | ;;;; Src Block | |
1963 | ||
1964 | (defun org-element-src-block-parser (limit) | |
1965 | "Parse a src block. | |
1966 | ||
1967 | LIMIT bounds the search. | |
1968 | ||
1969 | Return a list whose CAR is `src-block' and CDR is a plist | |
1970 | containing `:language', `:switches', `:parameters', `:begin', | |
1971 | `:end', `:hiddenp', `:number-lines', `:retain-labels', | |
1972 | `:use-labels', `:label-fmt', `:preserve-indent', `:value' and | |
1973 | `:post-blank' keywords. | |
1974 | ||
1975 | Assume point is at the beginning of the block." | |
1976 | (let ((case-fold-search t)) | |
1977 | (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC" limit t))) | |
1978 | ;; Incomplete block: parse it as a paragraph. | |
1979 | (org-element-paragraph-parser limit) | |
1980 | (let ((contents-end (match-beginning 0))) | |
1981 | (save-excursion | |
1982 | (let* ((keywords (org-element--collect-affiliated-keywords)) | |
1983 | ;; Get beginning position. | |
1984 | (begin (car keywords)) | |
1985 | ;; Get language as a string. | |
1986 | (language | |
1987 | (progn | |
1988 | (looking-at | |
1989 | (concat "^[ \t]*#\\+BEGIN_SRC" | |
1990 | "\\(?: +\\(\\S-+\\)\\)?" | |
1991 | "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?" | |
1992 | "\\(.*\\)[ \t]*$")) | |
1993 | (org-match-string-no-properties 1))) | |
1994 | ;; Get switches. | |
1995 | (switches (org-match-string-no-properties 2)) | |
1996 | ;; Get parameters. | |
1997 | (parameters (org-match-string-no-properties 3)) | |
1998 | ;; Switches analysis | |
1999 | (number-lines (cond ((not switches) nil) | |
2000 | ((string-match "-n\\>" switches) 'new) | |
2001 | ((string-match "+n\\>" switches) 'continued))) | |
2002 | (preserve-indent (and switches (string-match "-i\\>" switches))) | |
2003 | (label-fmt (and switches | |
2004 | (string-match "-l +\"\\([^\"\n]+\\)\"" switches) | |
2005 | (match-string 1 switches))) | |
2006 | ;; Should labels be retained in (or stripped from) | |
2007 | ;; src blocks? | |
2008 | (retain-labels | |
2009 | (or (not switches) | |
2010 | (not (string-match "-r\\>" switches)) | |
2011 | (and number-lines (string-match "-k\\>" switches)))) | |
2012 | ;; What should code-references use - labels or | |
2013 | ;; line-numbers? | |
2014 | (use-labels | |
2015 | (or (not switches) | |
2016 | (and retain-labels (not (string-match "-k\\>" switches))))) | |
2017 | ;; Get visibility status. | |
2018 | (hidden (progn (forward-line) (org-invisible-p2))) | |
2019 | ;; Retrieve code. | |
2020 | (value (buffer-substring-no-properties (point) contents-end)) | |
2021 | (pos-before-blank (progn (goto-char contents-end) | |
2022 | (forward-line) | |
2023 | (point))) | |
2024 | ;; Get position after ending blank lines. | |
2025 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
2026 | (if (eobp) (point) (point-at-bol))))) | |
2027 | (list 'src-block | |
2028 | (nconc | |
2029 | (list :language language | |
2030 | :switches (and (org-string-nw-p switches) | |
2031 | (org-trim switches)) | |
2032 | :parameters (and (org-string-nw-p parameters) | |
2033 | (org-trim parameters)) | |
2034 | :begin begin | |
2035 | :end end | |
2036 | :number-lines number-lines | |
2037 | :preserve-indent preserve-indent | |
2038 | :retain-labels retain-labels | |
2039 | :use-labels use-labels | |
2040 | :label-fmt label-fmt | |
2041 | :hiddenp hidden | |
2042 | :value value | |
2043 | :post-blank (count-lines pos-before-blank end)) | |
2044 | (cadr keywords))))))))) | |
2045 | ||
2046 | (defun org-element-src-block-interpreter (src-block contents) | |
2047 | "Interpret SRC-BLOCK element as Org syntax. | |
2048 | CONTENTS is nil." | |
2049 | (let ((lang (org-element-property :language src-block)) | |
2050 | (switches (org-element-property :switches src-block)) | |
2051 | (params (org-element-property :parameters src-block)) | |
2052 | (value (let ((val (org-element-property :value src-block))) | |
2053 | (cond | |
2054 | ||
2055 | (org-src-preserve-indentation val) | |
2056 | ((zerop org-edit-src-content-indentation) | |
2057 | (org-remove-indentation val)) | |
2058 | (t | |
2059 | (let ((ind (make-string | |
2060 | org-edit-src-content-indentation 32))) | |
2061 | (replace-regexp-in-string | |
2062 | "\\(^\\)[ \t]*\\S-" ind | |
2063 | (org-remove-indentation val) nil nil 1))))))) | |
2064 | (concat (format "#+BEGIN_SRC%s\n" | |
2065 | (concat (and lang (concat " " lang)) | |
2066 | (and switches (concat " " switches)) | |
2067 | (and params (concat " " params)))) | |
2068 | value | |
2069 | "#+END_SRC"))) | |
2070 | ||
2071 | ||
2072 | ;;;; Table | |
2073 | ||
2074 | (defun org-element-table-parser (limit) | |
2075 | "Parse a table at point. | |
2076 | ||
2077 | LIMIT bounds the search. | |
2078 | ||
2079 | Return a list whose CAR is `table' and CDR is a plist containing | |
2080 | `:begin', `:end', `:tblfm', `:type', `:contents-begin', | |
2081 | `:contents-end', `:value' and `:post-blank' keywords. | |
2082 | ||
2083 | Assume point is at the beginning of the table." | |
2084 | (save-excursion | |
2085 | (let* ((case-fold-search t) | |
2086 | (table-begin (point)) | |
2087 | (type (if (org-at-table.el-p) 'table.el 'org)) | |
2088 | (keywords (org-element--collect-affiliated-keywords)) | |
2089 | (begin (car keywords)) | |
2090 | (table-end (goto-char (marker-position (org-table-end t)))) | |
2091 | (tblfm (let (acc) | |
2092 | (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$") | |
2093 | (push (org-match-string-no-properties 1) acc) | |
2094 | (forward-line)) | |
2095 | acc)) | |
2096 | (pos-before-blank (point)) | |
2097 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
2098 | (if (eobp) (point) (point-at-bol))))) | |
2099 | (list 'table | |
2100 | (nconc | |
2101 | (list :begin begin | |
2102 | :end end | |
2103 | :type type | |
2104 | :tblfm tblfm | |
2105 | ;; Only `org' tables have contents. `table.el' tables | |
2106 | ;; use a `:value' property to store raw table as | |
2107 | ;; a string. | |
2108 | :contents-begin (and (eq type 'org) table-begin) | |
2109 | :contents-end (and (eq type 'org) table-end) | |
2110 | :value (and (eq type 'table.el) | |
2111 | (buffer-substring-no-properties | |
2112 | table-begin table-end)) | |
2113 | :post-blank (count-lines pos-before-blank end)) | |
2114 | (cadr keywords)))))) | |
2115 | ||
2116 | (defun org-element-table-interpreter (table contents) | |
2117 | "Interpret TABLE element as Org syntax. | |
2118 | CONTENTS is nil." | |
2119 | (if (eq (org-element-property :type table) 'table.el) | |
2120 | (org-remove-indentation (org-element-property :value table)) | |
2121 | (concat (with-temp-buffer (insert contents) | |
2122 | (org-table-align) | |
2123 | (buffer-string)) | |
2124 | (mapconcat (lambda (fm) (concat "#+TBLFM: " fm)) | |
2125 | (reverse (org-element-property :tblfm table)) | |
2126 | "\n")))) | |
2127 | ||
2128 | ||
2129 | ;;;; Table Row | |
2130 | ||
2131 | (defun org-element-table-row-parser (limit) | |
2132 | "Parse table row at point. | |
2133 | ||
2134 | LIMIT bounds the search. | |
2135 | ||
2136 | Return a list whose CAR is `table-row' and CDR is a plist | |
2137 | containing `:begin', `:end', `:contents-begin', `:contents-end', | |
2138 | `:type' and `:post-blank' keywords." | |
2139 | (save-excursion | |
2140 | (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard)) | |
2141 | (begin (point)) | |
2142 | ;; A table rule has no contents. In that case, ensure | |
2143 | ;; CONTENTS-BEGIN matches CONTENTS-END. | |
2144 | (contents-begin (and (eq type 'standard) | |
2145 | (search-forward "|") | |
2146 | (point))) | |
2147 | (contents-end (and (eq type 'standard) | |
2148 | (progn | |
2149 | (end-of-line) | |
2150 | (skip-chars-backward " \t") | |
2151 | (point)))) | |
2152 | (end (progn (forward-line) (point)))) | |
2153 | (list 'table-row | |
2154 | (list :type type | |
2155 | :begin begin | |
2156 | :end end | |
2157 | :contents-begin contents-begin | |
2158 | :contents-end contents-end | |
2159 | :post-blank 0))))) | |
2160 | ||
2161 | (defun org-element-table-row-interpreter (table-row contents) | |
2162 | "Interpret TABLE-ROW element as Org syntax. | |
2163 | CONTENTS is the contents of the table row." | |
2164 | (if (eq (org-element-property :type table-row) 'rule) "|-" | |
2165 | (concat "| " contents))) | |
2166 | ||
2167 | ||
2168 | ;;;; Verse Block | |
2169 | ||
2170 | (defun org-element-verse-block-parser (limit) | |
2171 | "Parse a verse block. | |
2172 | ||
2173 | LIMIT bounds the search. | |
2174 | ||
2175 | Return a list whose CAR is `verse-block' and CDR is a plist | |
2176 | containing `:begin', `:end', `:contents-begin', `:contents-end', | |
2177 | `:hiddenp' and `:post-blank' keywords. | |
2178 | ||
2179 | Assume point is at beginning of the block." | |
2180 | (let ((case-fold-search t)) | |
2181 | (if (not (save-excursion | |
2182 | (re-search-forward "^[ \t]*#\\+END_VERSE" limit t))) | |
2183 | ;; Incomplete block: parse it as a paragraph. | |
2184 | (org-element-paragraph-parser limit) | |
2185 | (let ((contents-end (match-beginning 0))) | |
2186 | (save-excursion | |
2187 | (let* ((keywords (org-element--collect-affiliated-keywords)) | |
2188 | (begin (car keywords)) | |
2189 | (hidden (progn (forward-line) (org-invisible-p2))) | |
2190 | (contents-begin (point)) | |
2191 | (pos-before-blank (progn (goto-char contents-end) | |
2192 | (forward-line) | |
2193 | (point))) | |
2194 | (end (progn (skip-chars-forward " \r\t\n" limit) | |
2195 | (if (eobp) (point) (point-at-bol))))) | |
2196 | (list 'verse-block | |
2197 | (nconc | |
2198 | (list :begin begin | |
2199 | :end end | |
2200 | :contents-begin contents-begin | |
2201 | :contents-end contents-end | |
2202 | :hiddenp hidden | |
2203 | :post-blank (count-lines pos-before-blank end)) | |
2204 | (cadr keywords))))))))) | |
2205 | ||
2206 | (defun org-element-verse-block-interpreter (verse-block contents) | |
2207 | "Interpret VERSE-BLOCK element as Org syntax. | |
2208 | CONTENTS is verse block contents." | |
2209 | (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents)) | |
2210 | ||
2211 | ||
2212 | \f | |
2213 | ;;; Objects | |
2214 | ;; | |
2215 | ;; Unlike to elements, interstices can be found between objects. | |
2216 | ;; That's why, along with the parser, successor functions are provided | |
2217 | ;; for each object. Some objects share the same successor (i.e. `code' | |
2218 | ;; and `verbatim' objects). | |
2219 | ;; | |
2220 | ;; A successor must accept a single argument bounding the search. It | |
2221 | ;; will return either a cons cell whose CAR is the object's type, as | |
2222 | ;; a symbol, and CDR the position of its next occurrence, or nil. | |
2223 | ;; | |
2224 | ;; Successors follow the naming convention: | |
2225 | ;; org-element-NAME-successor, where NAME is the name of the | |
2226 | ;; successor, as defined in `org-element-all-successors'. | |
2227 | ;; | |
2228 | ;; Some object types (i.e. `italic') are recursive. Restrictions on | |
2229 | ;; object types they can contain will be specified in | |
2230 | ;; `org-element-object-restrictions'. | |
2231 | ;; | |
2232 | ;; Adding a new type of object is simple. Implement a successor, | |
2233 | ;; a parser, and an interpreter for it, all following the naming | |
2234 | ;; convention. Register type in `org-element-all-objects' and | |
2235 | ;; successor in `org-element-all-successors'. Maybe tweak | |
2236 | ;; restrictions about it, and that's it. | |
2237 | ||
2238 | ||
2239 | ;;;; Bold | |
2240 | ||
2241 | (defun org-element-bold-parser () | |
2242 | "Parse bold object at point. | |
2243 | ||
2244 | Return a list whose CAR is `bold' and CDR is a plist with | |
2245 | `:begin', `:end', `:contents-begin' and `:contents-end' and | |
2246 | `:post-blank' keywords. | |
2247 | ||
2248 | Assume point is at the first star marker." | |
2249 | (save-excursion | |
2250 | (unless (bolp) (backward-char 1)) | |
2251 | (looking-at org-emph-re) | |
2252 | (let ((begin (match-beginning 2)) | |
2253 | (contents-begin (match-beginning 4)) | |
2254 | (contents-end (match-end 4)) | |
2255 | (post-blank (progn (goto-char (match-end 2)) | |
2256 | (skip-chars-forward " \t"))) | |
2257 | (end (point))) | |
2258 | (list 'bold | |
2259 | (list :begin begin | |
2260 | :end end | |
2261 | :contents-begin contents-begin | |
2262 | :contents-end contents-end | |
2263 | :post-blank post-blank))))) | |
2264 | ||
2265 | (defun org-element-bold-interpreter (bold contents) | |
2266 | "Interpret BOLD object as Org syntax. | |
2267 | CONTENTS is the contents of the object." | |
2268 | (format "*%s*" contents)) | |
2269 | ||
2270 | (defun org-element-text-markup-successor (limit) | |
2271 | "Search for the next text-markup object. | |
2272 | ||
2273 | LIMIT bounds the search. | |
2274 | ||
2275 | Return value is a cons cell whose CAR is a symbol among `bold', | |
2276 | `italic', `underline', `strike-through', `code' and `verbatim' | |
2277 | and CDR is beginning position." | |
2278 | (save-excursion | |
2279 | (unless (bolp) (backward-char)) | |
2280 | (when (re-search-forward org-emph-re limit t) | |
2281 | (let ((marker (match-string 3))) | |
2282 | (cons (cond | |
2283 | ((equal marker "*") 'bold) | |
2284 | ((equal marker "/") 'italic) | |
2285 | ((equal marker "_") 'underline) | |
2286 | ((equal marker "+") 'strike-through) | |
2287 | ((equal marker "~") 'code) | |
2288 | ((equal marker "=") 'verbatim) | |
2289 | (t (error "Unknown marker at %d" (match-beginning 3)))) | |
2290 | (match-beginning 2)))))) | |
2291 | ||
2292 | ||
2293 | ;;;; Code | |
2294 | ||
2295 | (defun org-element-code-parser () | |
2296 | "Parse code object at point. | |
2297 | ||
2298 | Return a list whose CAR is `code' and CDR is a plist with | |
2299 | `:value', `:begin', `:end' and `:post-blank' keywords. | |
2300 | ||
2301 | Assume point is at the first tilde marker." | |
2302 | (save-excursion | |
2303 | (unless (bolp) (backward-char 1)) | |
2304 | (looking-at org-emph-re) | |
2305 | (let ((begin (match-beginning 2)) | |
2306 | (value (org-match-string-no-properties 4)) | |
2307 | (post-blank (progn (goto-char (match-end 2)) | |
2308 | (skip-chars-forward " \t"))) | |
2309 | (end (point))) | |
2310 | (list 'code | |
2311 | (list :value value | |
2312 | :begin begin | |
2313 | :end end | |
2314 | :post-blank post-blank))))) | |
2315 | ||
2316 | (defun org-element-code-interpreter (code contents) | |
2317 | "Interpret CODE object as Org syntax. | |
2318 | CONTENTS is nil." | |
2319 | (format "~%s~" (org-element-property :value code))) | |
2320 | ||
2321 | ||
2322 | ;;;; Entity | |
2323 | ||
2324 | (defun org-element-entity-parser () | |
2325 | "Parse entity at point. | |
2326 | ||
2327 | Return a list whose CAR is `entity' and CDR a plist with | |
2328 | `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1', | |
2329 | `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as | |
2330 | keywords. | |
2331 | ||
2332 | Assume point is at the beginning of the entity." | |
2333 | (save-excursion | |
2334 | (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)") | |
2335 | (let* ((value (org-entity-get (match-string 1))) | |
2336 | (begin (match-beginning 0)) | |
2337 | (bracketsp (string= (match-string 2) "{}")) | |
2338 | (post-blank (progn (goto-char (match-end 1)) | |
2339 | (when bracketsp (forward-char 2)) | |
2340 | (skip-chars-forward " \t"))) | |
2341 | (end (point))) | |
2342 | (list 'entity | |
2343 | (list :name (car value) | |
2344 | :latex (nth 1 value) | |
2345 | :latex-math-p (nth 2 value) | |
2346 | :html (nth 3 value) | |
2347 | :ascii (nth 4 value) | |
2348 | :latin1 (nth 5 value) | |
2349 | :utf-8 (nth 6 value) | |
2350 | :begin begin | |
2351 | :end end | |
2352 | :use-brackets-p bracketsp | |
2353 | :post-blank post-blank))))) | |
2354 | ||
2355 | (defun org-element-entity-interpreter (entity contents) | |
2356 | "Interpret ENTITY object as Org syntax. | |
2357 | CONTENTS is nil." | |
2358 | (concat "\\" | |
2359 | (org-element-property :name entity) | |
2360 | (when (org-element-property :use-brackets-p entity) "{}"))) | |
2361 | ||
2362 | (defun org-element-latex-or-entity-successor (limit) | |
2363 | "Search for the next latex-fragment or entity object. | |
2364 | ||
2365 | LIMIT bounds the search. | |
2366 | ||
2367 | Return value is a cons cell whose CAR is `entity' or | |
2368 | `latex-fragment' and CDR is beginning position." | |
2369 | (save-excursion | |
2370 | (let ((matchers | |
2371 | (remove "begin" (plist-get org-format-latex-options :matchers))) | |
2372 | ;; ENTITY-RE matches both LaTeX commands and Org entities. | |
2373 | (entity-re | |
2374 | "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")) | |
2375 | (when (re-search-forward | |
2376 | (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps))) | |
2377 | matchers "\\|") | |
2378 | "\\|" entity-re) | |
2379 | limit t) | |
2380 | (goto-char (match-beginning 0)) | |
2381 | (if (looking-at entity-re) | |
2382 | ;; Determine if it's a real entity or a LaTeX command. | |
2383 | (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment) | |
2384 | (match-beginning 0)) | |
2385 | ;; No entity nor command: point is at a LaTeX fragment. | |
2386 | ;; Determine its type to get the correct beginning position. | |
2387 | (cons 'latex-fragment | |
2388 | (catch 'return | |
2389 | (mapc (lambda (e) | |
2390 | (when (looking-at (nth 1 (assoc e org-latex-regexps))) | |
2391 | (throw 'return | |
2392 | (match-beginning | |
2393 | (nth 2 (assoc e org-latex-regexps)))))) | |
2394 | matchers) | |
2395 | (point)))))))) | |
2396 | ||
2397 | ||
2398 | ;;;; Export Snippet | |
2399 | ||
2400 | (defun org-element-export-snippet-parser () | |
2401 | "Parse export snippet at point. | |
2402 | ||
2403 | Return a list whose CAR is `export-snippet' and CDR a plist with | |
2404 | `:begin', `:end', `:back-end', `:value' and `:post-blank' as | |
2405 | keywords. | |
2406 | ||
2407 | Assume point is at the beginning of the snippet." | |
2408 | (save-excursion | |
2409 | (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t) | |
2410 | (let* ((begin (match-beginning 0)) | |
2411 | (back-end (org-match-string-no-properties 1)) | |
2412 | (value (buffer-substring-no-properties | |
2413 | (point) | |
2414 | (progn (re-search-forward "@@" nil t) (match-beginning 0)))) | |
2415 | (post-blank (skip-chars-forward " \t")) | |
2416 | (end (point))) | |
2417 | (list 'export-snippet | |
2418 | (list :back-end back-end | |
2419 | :value value | |
2420 | :begin begin | |
2421 | :end end | |
2422 | :post-blank post-blank))))) | |
2423 | ||
2424 | (defun org-element-export-snippet-interpreter (export-snippet contents) | |
2425 | "Interpret EXPORT-SNIPPET object as Org syntax. | |
2426 | CONTENTS is nil." | |
2427 | (format "@@%s:%s@@" | |
2428 | (org-element-property :back-end export-snippet) | |
2429 | (org-element-property :value export-snippet))) | |
2430 | ||
2431 | (defun org-element-export-snippet-successor (limit) | |
2432 | "Search for the next export-snippet object. | |
2433 | ||
2434 | LIMIT bounds the search. | |
2435 | ||
2436 | Return value is a cons cell whose CAR is `export-snippet' and CDR | |
2437 | its beginning position." | |
2438 | (save-excursion | |
2439 | (let (beg) | |
2440 | (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t) | |
2441 | (setq beg (match-beginning 0)) | |
2442 | (search-forward "@@" limit t)) | |
2443 | (cons 'export-snippet beg))))) | |
2444 | ||
2445 | ||
2446 | ;;;; Footnote Reference | |
2447 | ||
2448 | (defun org-element-footnote-reference-parser () | |
2449 | "Parse footnote reference at point. | |
2450 | ||
2451 | Return a list whose CAR is `footnote-reference' and CDR a plist | |
2452 | with `:label', `:type', `:inline-definition', `:begin', `:end' | |
2453 | and `:post-blank' as keywords." | |
2454 | (save-excursion | |
2455 | (looking-at org-footnote-re) | |
2456 | (let* ((begin (point)) | |
2457 | (label (or (org-match-string-no-properties 2) | |
2458 | (org-match-string-no-properties 3) | |
2459 | (and (match-string 1) | |
2460 | (concat "fn:" (org-match-string-no-properties 1))))) | |
2461 | (type (if (or (not label) (match-string 1)) 'inline 'standard)) | |
2462 | (inner-begin (match-end 0)) | |
2463 | (inner-end | |
2464 | (let ((count 1)) | |
2465 | (forward-char) | |
2466 | (while (and (> count 0) (re-search-forward "[][]" nil t)) | |
2467 | (if (equal (match-string 0) "[") (incf count) (decf count))) | |
2468 | (1- (point)))) | |
2469 | (post-blank (progn (goto-char (1+ inner-end)) | |
2470 | (skip-chars-forward " \t"))) | |
2471 | (end (point)) | |
2472 | (footnote-reference | |
2473 | (list 'footnote-reference | |
2474 | (list :label label | |
2475 | :type type | |
2476 | :begin begin | |
2477 | :end end | |
2478 | :post-blank post-blank)))) | |
2479 | (org-element-put-property | |
2480 | footnote-reference :inline-definition | |
2481 | (and (eq type 'inline) | |
2482 | (org-element-parse-secondary-string | |
2483 | (buffer-substring inner-begin inner-end) | |
2484 | (org-element-restriction 'footnote-reference) | |
2485 | footnote-reference)))))) | |
2486 | ||
2487 | (defun org-element-footnote-reference-interpreter (footnote-reference contents) | |
2488 | "Interpret FOOTNOTE-REFERENCE object as Org syntax. | |
2489 | CONTENTS is nil." | |
2490 | (let ((label (or (org-element-property :label footnote-reference) "fn:")) | |
2491 | (def | |
2492 | (let ((inline-def | |
2493 | (org-element-property :inline-definition footnote-reference))) | |
2494 | (if (not inline-def) "" | |
2495 | (concat ":" (org-element-interpret-data inline-def)))))) | |
2496 | (format "[%s]" (concat label def)))) | |
2497 | ||
2498 | (defun org-element-footnote-reference-successor (limit) | |
2499 | "Search for the next footnote-reference object. | |
2500 | ||
2501 | LIMIT bounds the search. | |
2502 | ||
2503 | Return value is a cons cell whose CAR is `footnote-reference' and | |
2504 | CDR is beginning position." | |
2505 | (save-excursion | |
2506 | (catch 'exit | |
2507 | (while (re-search-forward org-footnote-re limit t) | |
2508 | (save-excursion | |
2509 | (let ((beg (match-beginning 0)) | |
2510 | (count 1)) | |
2511 | (backward-char) | |
2512 | (while (re-search-forward "[][]" limit t) | |
2513 | (if (equal (match-string 0) "[") (incf count) (decf count)) | |
2514 | (when (zerop count) | |
2515 | (throw 'exit (cons 'footnote-reference beg)))))))))) | |
2516 | ||
2517 | ||
2518 | ;;;; Inline Babel Call | |
2519 | ||
2520 | (defun org-element-inline-babel-call-parser () | |
2521 | "Parse inline babel call at point. | |
2522 | ||
2523 | Return a list whose CAR is `inline-babel-call' and CDR a plist | |
2524 | with `:begin', `:end', `:info' and `:post-blank' as keywords. | |
2525 | ||
2526 | Assume point is at the beginning of the babel call." | |
2527 | (save-excursion | |
2528 | (unless (bolp) (backward-char)) | |
2529 | (looking-at org-babel-inline-lob-one-liner-regexp) | |
2530 | (let ((info (save-match-data (org-babel-lob-get-info))) | |
2531 | (begin (match-end 1)) | |
2532 | (post-blank (progn (goto-char (match-end 0)) | |
2533 | (skip-chars-forward " \t"))) | |
2534 | (end (point))) | |
2535 | (list 'inline-babel-call | |
2536 | (list :begin begin | |
2537 | :end end | |
2538 | :info info | |
2539 | :post-blank post-blank))))) | |
2540 | ||
2541 | (defun org-element-inline-babel-call-interpreter (inline-babel-call contents) | |
2542 | "Interpret INLINE-BABEL-CALL object as Org syntax. | |
2543 | CONTENTS is nil." | |
2544 | (let* ((babel-info (org-element-property :info inline-babel-call)) | |
2545 | (main-source (car babel-info)) | |
2546 | (post-options (nth 1 babel-info))) | |
2547 | (concat "call_" | |
2548 | (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source) | |
2549 | ;; Remove redundant square brackets. | |
2550 | (replace-match | |
2551 | (match-string 1 main-source) nil nil main-source) | |
2552 | main-source) | |
2553 | (and post-options (format "[%s]" post-options))))) | |
2554 | ||
2555 | (defun org-element-inline-babel-call-successor (limit) | |
2556 | "Search for the next inline-babel-call object. | |
2557 | ||
2558 | LIMIT bounds the search. | |
2559 | ||
2560 | Return value is a cons cell whose CAR is `inline-babel-call' and | |
2561 | CDR is beginning position." | |
2562 | (save-excursion | |
2563 | ;; Use a simplified version of | |
2564 | ;; `org-babel-inline-lob-one-liner-regexp'. | |
2565 | (when (re-search-forward | |
2566 | "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?" | |
2567 | limit t) | |
2568 | (cons 'inline-babel-call (match-beginning 0))))) | |
2569 | ||
2570 | ||
2571 | ;;;; Inline Src Block | |
2572 | ||
2573 | (defun org-element-inline-src-block-parser () | |
2574 | "Parse inline source block at point. | |
2575 | ||
2576 | LIMIT bounds the search. | |
2577 | ||
2578 | Return a list whose CAR is `inline-src-block' and CDR a plist | |
2579 | with `:begin', `:end', `:language', `:value', `:parameters' and | |
2580 | `:post-blank' as keywords. | |
2581 | ||
2582 | Assume point is at the beginning of the inline src block." | |
2583 | (save-excursion | |
2584 | (unless (bolp) (backward-char)) | |
2585 | (looking-at org-babel-inline-src-block-regexp) | |
2586 | (let ((begin (match-beginning 1)) | |
2587 | (language (org-match-string-no-properties 2)) | |
2588 | (parameters (org-match-string-no-properties 4)) | |
2589 | (value (org-match-string-no-properties 5)) | |
2590 | (post-blank (progn (goto-char (match-end 0)) | |
2591 | (skip-chars-forward " \t"))) | |
2592 | (end (point))) | |
2593 | (list 'inline-src-block | |
2594 | (list :language language | |
2595 | :value value | |
2596 | :parameters parameters | |
2597 | :begin begin | |
2598 | :end end | |
2599 | :post-blank post-blank))))) | |
2600 | ||
2601 | (defun org-element-inline-src-block-interpreter (inline-src-block contents) | |
2602 | "Interpret INLINE-SRC-BLOCK object as Org syntax. | |
2603 | CONTENTS is nil." | |
2604 | (let ((language (org-element-property :language inline-src-block)) | |
2605 | (arguments (org-element-property :parameters inline-src-block)) | |
2606 | (body (org-element-property :value inline-src-block))) | |
2607 | (format "src_%s%s{%s}" | |
2608 | language | |
2609 | (if arguments (format "[%s]" arguments) "") | |
2610 | body))) | |
2611 | ||
2612 | (defun org-element-inline-src-block-successor (limit) | |
2613 | "Search for the next inline-babel-call element. | |
2614 | ||
2615 | LIMIT bounds the search. | |
2616 | ||
2617 | Return value is a cons cell whose CAR is `inline-babel-call' and | |
2618 | CDR is beginning position." | |
2619 | (save-excursion | |
2620 | (unless (bolp) (backward-char)) | |
2621 | (when (re-search-forward org-babel-inline-src-block-regexp limit t) | |
2622 | (cons 'inline-src-block (match-beginning 1))))) | |
2623 | ||
2624 | ;;;; Italic | |
2625 | ||
2626 | (defun org-element-italic-parser () | |
2627 | "Parse italic object at point. | |
2628 | ||
2629 | Return a list whose CAR is `italic' and CDR is a plist with | |
2630 | `:begin', `:end', `:contents-begin' and `:contents-end' and | |
2631 | `:post-blank' keywords. | |
2632 | ||
2633 | Assume point is at the first slash marker." | |
2634 | (save-excursion | |
2635 | (unless (bolp) (backward-char 1)) | |
2636 | (looking-at org-emph-re) | |
2637 | (let ((begin (match-beginning 2)) | |
2638 | (contents-begin (match-beginning 4)) | |
2639 | (contents-end (match-end 4)) | |
2640 | (post-blank (progn (goto-char (match-end 2)) | |
2641 | (skip-chars-forward " \t"))) | |
2642 | (end (point))) | |
2643 | (list 'italic | |
2644 | (list :begin begin | |
2645 | :end end | |
2646 | :contents-begin contents-begin | |
2647 | :contents-end contents-end | |
2648 | :post-blank post-blank))))) | |
2649 | ||
2650 | (defun org-element-italic-interpreter (italic contents) | |
2651 | "Interpret ITALIC object as Org syntax. | |
2652 | CONTENTS is the contents of the object." | |
2653 | (format "/%s/" contents)) | |
2654 | ||
2655 | ||
2656 | ;;;; Latex Fragment | |
2657 | ||
2658 | (defun org-element-latex-fragment-parser () | |
2659 | "Parse latex fragment at point. | |
2660 | ||
2661 | Return a list whose CAR is `latex-fragment' and CDR a plist with | |
2662 | `:value', `:begin', `:end', and `:post-blank' as keywords. | |
2663 | ||
2664 | Assume point is at the beginning of the latex fragment." | |
2665 | (save-excursion | |
2666 | (let* ((begin (point)) | |
2667 | (substring-match | |
2668 | (catch 'exit | |
2669 | (mapc (lambda (e) | |
2670 | (let ((latex-regexp (nth 1 (assoc e org-latex-regexps)))) | |
2671 | (when (or (looking-at latex-regexp) | |
2672 | (and (not (bobp)) | |
2673 | (save-excursion | |
2674 | (backward-char) | |
2675 | (looking-at latex-regexp)))) | |
2676 | (throw 'exit (nth 2 (assoc e org-latex-regexps)))))) | |
2677 | (plist-get org-format-latex-options :matchers)) | |
2678 | ;; None found: it's a macro. | |
2679 | (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*") | |
2680 | 0)) | |
2681 | (value (match-string-no-properties substring-match)) | |
2682 | (post-blank (progn (goto-char (match-end substring-match)) | |
2683 | (skip-chars-forward " \t"))) | |
2684 | (end (point))) | |
2685 | (list 'latex-fragment | |
2686 | (list :value value | |
2687 | :begin begin | |
2688 | :end end | |
2689 | :post-blank post-blank))))) | |
2690 | ||
2691 | (defun org-element-latex-fragment-interpreter (latex-fragment contents) | |
2692 | "Interpret LATEX-FRAGMENT object as Org syntax. | |
2693 | CONTENTS is nil." | |
2694 | (org-element-property :value latex-fragment)) | |
2695 | ||
2696 | ;;;; Line Break | |
2697 | ||
2698 | (defun org-element-line-break-parser () | |
2699 | "Parse line break at point. | |
2700 | ||
2701 | Return a list whose CAR is `line-break', and CDR a plist with | |
2702 | `:begin', `:end' and `:post-blank' keywords. | |
2703 | ||
2704 | Assume point is at the beginning of the line break." | |
2705 | (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0))) | |
2706 | ||
2707 | (defun org-element-line-break-interpreter (line-break contents) | |
2708 | "Interpret LINE-BREAK object as Org syntax. | |
2709 | CONTENTS is nil." | |
2710 | "\\\\") | |
2711 | ||
2712 | (defun org-element-line-break-successor (limit) | |
2713 | "Search for the next line-break object. | |
2714 | ||
2715 | LIMIT bounds the search. | |
2716 | ||
2717 | Return value is a cons cell whose CAR is `line-break' and CDR is | |
2718 | beginning position." | |
2719 | (save-excursion | |
2720 | (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t) | |
2721 | (goto-char (match-beginning 1))))) | |
2722 | ;; A line break can only happen on a non-empty line. | |
2723 | (when (and beg (re-search-backward "\\S-" (point-at-bol) t)) | |
2724 | (cons 'line-break beg))))) | |
2725 | ||
2726 | ||
2727 | ;;;; Link | |
2728 | ||
2729 | (defun org-element-link-parser () | |
2730 | "Parse link at point. | |
2731 | ||
2732 | Return a list whose CAR is `link' and CDR a plist with `:type', | |
2733 | `:path', `:raw-link', `:begin', `:end', `:contents-begin', | |
2734 | `:contents-end' and `:post-blank' as keywords. | |
2735 | ||
2736 | Assume point is at the beginning of the link." | |
2737 | (save-excursion | |
2738 | (let ((begin (point)) | |
2739 | end contents-begin contents-end link-end post-blank path type | |
2740 | raw-link link) | |
2741 | (cond | |
2742 | ;; Type 1: Text targeted from a radio target. | |
2743 | ((and org-target-link-regexp (looking-at org-target-link-regexp)) | |
2744 | (setq type "radio" | |
2745 | link-end (match-end 0) | |
2746 | path (org-match-string-no-properties 0))) | |
2747 | ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]] | |
2748 | ((looking-at org-bracket-link-regexp) | |
2749 | (setq contents-begin (match-beginning 3) | |
2750 | contents-end (match-end 3) | |
2751 | link-end (match-end 0) | |
2752 | ;; RAW-LINK is the original link. | |
2753 | raw-link (org-match-string-no-properties 1) | |
2754 | link (org-translate-link | |
2755 | (org-link-expand-abbrev | |
2756 | (org-link-unescape raw-link)))) | |
2757 | ;; Determine TYPE of link and set PATH accordingly. | |
2758 | (cond | |
2759 | ;; File type. | |
2760 | ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link)) | |
2761 | (setq type "file" path link)) | |
2762 | ;; Explicit type (http, irc, bbdb...). See `org-link-types'. | |
2763 | ((string-match org-link-re-with-space3 link) | |
2764 | (setq type (match-string 1 link) path (match-string 2 link))) | |
2765 | ;; Id type: PATH is the id. | |
2766 | ((string-match "^id:\\([-a-f0-9]+\\)" link) | |
2767 | (setq type "id" path (match-string 1 link))) | |
2768 | ;; Code-ref type: PATH is the name of the reference. | |
2769 | ((string-match "^(\\(.*\\))$" link) | |
2770 | (setq type "coderef" path (match-string 1 link))) | |
2771 | ;; Custom-id type: PATH is the name of the custom id. | |
2772 | ((= (aref link 0) ?#) | |
2773 | (setq type "custom-id" path (substring link 1))) | |
2774 | ;; Fuzzy type: Internal link either matches a target, an | |
2775 | ;; headline name or nothing. PATH is the target or | |
2776 | ;; headline's name. | |
2777 | (t (setq type "fuzzy" path link)))) | |
2778 | ;; Type 3: Plain link, i.e. http://orgmode.org | |
2779 | ((looking-at org-plain-link-re) | |
2780 | (setq raw-link (org-match-string-no-properties 0) | |
2781 | type (org-match-string-no-properties 1) | |
2782 | path (org-match-string-no-properties 2) | |
2783 | link-end (match-end 0))) | |
2784 | ;; Type 4: Angular link, i.e. <http://orgmode.org> | |
2785 | ((looking-at org-angle-link-re) | |
2786 | (setq raw-link (buffer-substring-no-properties | |
2787 | (match-beginning 1) (match-end 2)) | |
2788 | type (org-match-string-no-properties 1) | |
2789 | path (org-match-string-no-properties 2) | |
2790 | link-end (match-end 0)))) | |
2791 | ;; In any case, deduce end point after trailing white space from | |
2792 | ;; LINK-END variable. | |
2793 | (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t")) | |
2794 | end (point)) | |
2795 | (list 'link | |
2796 | (list :type type | |
2797 | :path path | |
2798 | :raw-link (or raw-link path) | |
2799 | :begin begin | |
2800 | :end end | |
2801 | :contents-begin contents-begin | |
2802 | :contents-end contents-end | |
2803 | :post-blank post-blank))))) | |
2804 | ||
2805 | (defun org-element-link-interpreter (link contents) | |
2806 | "Interpret LINK object as Org syntax. | |
2807 | CONTENTS is the contents of the object, or nil." | |
2808 | (let ((type (org-element-property :type link)) | |
2809 | (raw-link (org-element-property :raw-link link))) | |
2810 | (if (string= type "radio") raw-link | |
2811 | (format "[[%s]%s]" | |
2812 | raw-link | |
2813 | (if contents (format "[%s]" contents) ""))))) | |
2814 | ||
2815 | (defun org-element-link-successor (limit) | |
2816 | "Search for the next link object. | |
2817 | ||
2818 | LIMIT bounds the search. | |
2819 | ||
2820 | Return value is a cons cell whose CAR is `link' and CDR is | |
2821 | beginning position." | |
2822 | (save-excursion | |
2823 | (let ((link-regexp | |
2824 | (if (not org-target-link-regexp) org-any-link-re | |
2825 | (concat org-any-link-re "\\|" org-target-link-regexp)))) | |
2826 | (when (re-search-forward link-regexp limit t) | |
2827 | (cons 'link (match-beginning 0)))))) | |
2828 | ||
2829 | ||
2830 | ;;;; Macro | |
2831 | ||
2832 | (defun org-element-macro-parser () | |
2833 | "Parse macro at point. | |
2834 | ||
2835 | Return a list whose CAR is `macro' and CDR a plist with `:key', | |
2836 | `:args', `:begin', `:end', `:value' and `:post-blank' as | |
2837 | keywords. | |
2838 | ||
2839 | Assume point is at the macro." | |
2840 | (save-excursion | |
2841 | (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}") | |
2842 | (let ((begin (point)) | |
2843 | (key (downcase (org-match-string-no-properties 1))) | |
2844 | (value (org-match-string-no-properties 0)) | |
2845 | (post-blank (progn (goto-char (match-end 0)) | |
2846 | (skip-chars-forward " \t"))) | |
2847 | (end (point)) | |
2848 | (args (let ((args (org-match-string-no-properties 3)) args2) | |
2849 | (when args | |
2850 | (setq args (org-split-string args ",")) | |
2851 | (while args | |
2852 | (while (string-match "\\\\\\'" (car args)) | |
2853 | ;; Repair bad splits. | |
2854 | (setcar (cdr args) (concat (substring (car args) 0 -1) | |
2855 | "," (nth 1 args))) | |
2856 | (pop args)) | |
2857 | (push (pop args) args2)) | |
2858 | (mapcar 'org-trim (nreverse args2)))))) | |
2859 | (list 'macro | |
2860 | (list :key key | |
2861 | :value value | |
2862 | :args args | |
2863 | :begin begin | |
2864 | :end end | |
2865 | :post-blank post-blank))))) | |
2866 | ||
2867 | (defun org-element-macro-interpreter (macro contents) | |
2868 | "Interpret MACRO object as Org syntax. | |
2869 | CONTENTS is nil." | |
2870 | (org-element-property :value macro)) | |
2871 | ||
2872 | (defun org-element-macro-successor (limit) | |
2873 | "Search for the next macro object. | |
2874 | ||
2875 | LIMIT bounds the search. | |
2876 | ||
2877 | Return value is cons cell whose CAR is `macro' and CDR is | |
2878 | beginning position." | |
2879 | (save-excursion | |
2880 | (when (re-search-forward | |
2881 | "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}" | |
2882 | limit t) | |
2883 | (cons 'macro (match-beginning 0))))) | |
2884 | ||
2885 | ||
2886 | ;;;; Radio-target | |
2887 | ||
2888 | (defun org-element-radio-target-parser () | |
2889 | "Parse radio target at point. | |
2890 | ||
2891 | Return a list whose CAR is `radio-target' and CDR a plist with | |
2892 | `:begin', `:end', `:contents-begin', `:contents-end', `:value' | |
2893 | and `:post-blank' as keywords. | |
2894 | ||
2895 | Assume point is at the radio target." | |
2896 | (save-excursion | |
2897 | (looking-at org-radio-target-regexp) | |
2898 | (let ((begin (point)) | |
2899 | (contents-begin (match-beginning 1)) | |
2900 | (contents-end (match-end 1)) | |
2901 | (value (org-match-string-no-properties 1)) | |
2902 | (post-blank (progn (goto-char (match-end 0)) | |
2903 | (skip-chars-forward " \t"))) | |
2904 | (end (point))) | |
2905 | (list 'radio-target | |
2906 | (list :begin begin | |
2907 | :end end | |
2908 | :contents-begin contents-begin | |
2909 | :contents-end contents-end | |
2910 | :post-blank post-blank | |
2911 | :value value))))) | |
2912 | ||
2913 | (defun org-element-radio-target-interpreter (target contents) | |
2914 | "Interpret TARGET object as Org syntax. | |
2915 | CONTENTS is the contents of the object." | |
2916 | (concat "<<<" contents ">>>")) | |
2917 | ||
2918 | (defun org-element-radio-target-successor (limit) | |
2919 | "Search for the next radio-target object. | |
2920 | ||
2921 | LIMIT bounds the search. | |
2922 | ||
2923 | Return value is a cons cell whose CAR is `radio-target' and CDR | |
2924 | is beginning position." | |
2925 | (save-excursion | |
2926 | (when (re-search-forward org-radio-target-regexp limit t) | |
2927 | (cons 'radio-target (match-beginning 0))))) | |
2928 | ||
2929 | ||
2930 | ;;;; Statistics Cookie | |
2931 | ||
2932 | (defun org-element-statistics-cookie-parser () | |
2933 | "Parse statistics cookie at point. | |
2934 | ||
2935 | Return a list whose CAR is `statistics-cookie', and CDR a plist | |
2936 | with `:begin', `:end', `:value' and `:post-blank' keywords. | |
2937 | ||
2938 | Assume point is at the beginning of the statistics-cookie." | |
2939 | (save-excursion | |
2940 | (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]") | |
2941 | (let* ((begin (point)) | |
2942 | (value (buffer-substring-no-properties | |
2943 | (match-beginning 0) (match-end 0))) | |
2944 | (post-blank (progn (goto-char (match-end 0)) | |
2945 | (skip-chars-forward " \t"))) | |
2946 | (end (point))) | |
2947 | (list 'statistics-cookie | |
2948 | (list :begin begin | |
2949 | :end end | |
2950 | :value value | |
2951 | :post-blank post-blank))))) | |
2952 | ||
2953 | (defun org-element-statistics-cookie-interpreter (statistics-cookie contents) | |
2954 | "Interpret STATISTICS-COOKIE object as Org syntax. | |
2955 | CONTENTS is nil." | |
2956 | (org-element-property :value statistics-cookie)) | |
2957 | ||
2958 | (defun org-element-statistics-cookie-successor (limit) | |
2959 | "Search for the next statistics cookie object. | |
2960 | ||
2961 | LIMIT bounds the search. | |
2962 | ||
2963 | Return value is a cons cell whose CAR is `statistics-cookie' and | |
2964 | CDR is beginning position." | |
2965 | (save-excursion | |
2966 | (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t) | |
2967 | (cons 'statistics-cookie (match-beginning 0))))) | |
2968 | ||
2969 | ||
2970 | ;;;; Strike-Through | |
2971 | ||
2972 | (defun org-element-strike-through-parser () | |
2973 | "Parse strike-through object at point. | |
2974 | ||
2975 | Return a list whose CAR is `strike-through' and CDR is a plist | |
2976 | with `:begin', `:end', `:contents-begin' and `:contents-end' and | |
2977 | `:post-blank' keywords. | |
2978 | ||
2979 | Assume point is at the first plus sign marker." | |
2980 | (save-excursion | |
2981 | (unless (bolp) (backward-char 1)) | |
2982 | (looking-at org-emph-re) | |
2983 | (let ((begin (match-beginning 2)) | |
2984 | (contents-begin (match-beginning 4)) | |
2985 | (contents-end (match-end 4)) | |
2986 | (post-blank (progn (goto-char (match-end 2)) | |
2987 | (skip-chars-forward " \t"))) | |
2988 | (end (point))) | |
2989 | (list 'strike-through | |
2990 | (list :begin begin | |
2991 | :end end | |
2992 | :contents-begin contents-begin | |
2993 | :contents-end contents-end | |
2994 | :post-blank post-blank))))) | |
2995 | ||
2996 | (defun org-element-strike-through-interpreter (strike-through contents) | |
2997 | "Interpret STRIKE-THROUGH object as Org syntax. | |
2998 | CONTENTS is the contents of the object." | |
2999 | (format "+%s+" contents)) | |
3000 | ||
3001 | ||
3002 | ;;;; Subscript | |
3003 | ||
3004 | (defun org-element-subscript-parser () | |
3005 | "Parse subscript at point. | |
3006 | ||
3007 | Return a list whose CAR is `subscript' and CDR a plist with | |
3008 | `:begin', `:end', `:contents-begin', `:contents-end', | |
3009 | `:use-brackets-p' and `:post-blank' as keywords. | |
3010 | ||
3011 | Assume point is at the underscore." | |
3012 | (save-excursion | |
3013 | (unless (bolp) (backward-char)) | |
3014 | (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) | |
3015 | t | |
3016 | (not (looking-at org-match-substring-regexp)))) | |
3017 | (begin (match-beginning 2)) | |
3018 | (contents-begin (or (match-beginning 5) | |
3019 | (match-beginning 3))) | |
3020 | (contents-end (or (match-end 5) (match-end 3))) | |
3021 | (post-blank (progn (goto-char (match-end 0)) | |
3022 | (skip-chars-forward " \t"))) | |
3023 | (end (point))) | |
3024 | (list 'subscript | |
3025 | (list :begin begin | |
3026 | :end end | |
3027 | :use-brackets-p bracketsp | |
3028 | :contents-begin contents-begin | |
3029 | :contents-end contents-end | |
3030 | :post-blank post-blank))))) | |
3031 | ||
3032 | (defun org-element-subscript-interpreter (subscript contents) | |
3033 | "Interpret SUBSCRIPT object as Org syntax. | |
3034 | CONTENTS is the contents of the object." | |
3035 | (format | |
3036 | (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s") | |
3037 | contents)) | |
3038 | ||
3039 | (defun org-element-sub/superscript-successor (limit) | |
3040 | "Search for the next sub/superscript object. | |
3041 | ||
3042 | LIMIT bounds the search. | |
3043 | ||
3044 | Return value is a cons cell whose CAR is either `subscript' or | |
3045 | `superscript' and CDR is beginning position." | |
3046 | (save-excursion | |
3047 | (when (re-search-forward org-match-substring-regexp limit t) | |
3048 | (cons (if (string= (match-string 2) "_") 'subscript 'superscript) | |
3049 | (match-beginning 2))))) | |
3050 | ||
3051 | ||
3052 | ;;;; Superscript | |
3053 | ||
3054 | (defun org-element-superscript-parser () | |
3055 | "Parse superscript at point. | |
3056 | ||
3057 | Return a list whose CAR is `superscript' and CDR a plist with | |
3058 | `:begin', `:end', `:contents-begin', `:contents-end', | |
3059 | `:use-brackets-p' and `:post-blank' as keywords. | |
3060 | ||
3061 | Assume point is at the caret." | |
3062 | (save-excursion | |
3063 | (unless (bolp) (backward-char)) | |
3064 | (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t | |
3065 | (not (looking-at org-match-substring-regexp)))) | |
3066 | (begin (match-beginning 2)) | |
3067 | (contents-begin (or (match-beginning 5) | |
3068 | (match-beginning 3))) | |
3069 | (contents-end (or (match-end 5) (match-end 3))) | |
3070 | (post-blank (progn (goto-char (match-end 0)) | |
3071 | (skip-chars-forward " \t"))) | |
3072 | (end (point))) | |
3073 | (list 'superscript | |
3074 | (list :begin begin | |
3075 | :end end | |
3076 | :use-brackets-p bracketsp | |
3077 | :contents-begin contents-begin | |
3078 | :contents-end contents-end | |
3079 | :post-blank post-blank))))) | |
3080 | ||
3081 | (defun org-element-superscript-interpreter (superscript contents) | |
3082 | "Interpret SUPERSCRIPT object as Org syntax. | |
3083 | CONTENTS is the contents of the object." | |
3084 | (format | |
3085 | (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s") | |
3086 | contents)) | |
3087 | ||
3088 | ||
3089 | ;;;; Table Cell | |
3090 | ||
3091 | (defun org-element-table-cell-parser () | |
3092 | "Parse table cell at point. | |
3093 | ||
3094 | Return a list whose CAR is `table-cell' and CDR is a plist | |
3095 | containing `:begin', `:end', `:contents-begin', `:contents-end' | |
3096 | and `:post-blank' keywords." | |
3097 | (looking-at "[ \t]*\\(.*?\\)[ \t]*|") | |
3098 | (let* ((begin (match-beginning 0)) | |
3099 | (end (match-end 0)) | |
3100 | (contents-begin (match-beginning 1)) | |
3101 | (contents-end (match-end 1))) | |
3102 | (list 'table-cell | |
3103 | (list :begin begin | |
3104 | :end end | |
3105 | :contents-begin contents-begin | |
3106 | :contents-end contents-end | |
3107 | :post-blank 0)))) | |
3108 | ||
3109 | (defun org-element-table-cell-interpreter (table-cell contents) | |
3110 | "Interpret TABLE-CELL element as Org syntax. | |
3111 | CONTENTS is the contents of the cell, or nil." | |
3112 | (concat " " contents " |")) | |
3113 | ||
3114 | (defun org-element-table-cell-successor (limit) | |
3115 | "Search for the next table-cell object. | |
3116 | ||
3117 | LIMIT bounds the search. | |
3118 | ||
3119 | Return value is a cons cell whose CAR is `table-cell' and CDR is | |
3120 | beginning position." | |
3121 | (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point)))) | |
3122 | ||
3123 | ||
3124 | ;;;; Target | |
3125 | ||
3126 | (defun org-element-target-parser () | |
3127 | "Parse target at point. | |
3128 | ||
3129 | Return a list whose CAR is `target' and CDR a plist with | |
3130 | `:begin', `:end', `:value' and `:post-blank' as keywords. | |
3131 | ||
3132 | Assume point is at the target." | |
3133 | (save-excursion | |
3134 | (looking-at org-target-regexp) | |
3135 | (let ((begin (point)) | |
3136 | (value (org-match-string-no-properties 1)) | |
3137 | (post-blank (progn (goto-char (match-end 0)) | |
3138 | (skip-chars-forward " \t"))) | |
3139 | (end (point))) | |
3140 | (list 'target | |
3141 | (list :begin begin | |
3142 | :end end | |
3143 | :value value | |
3144 | :post-blank post-blank))))) | |
3145 | ||
3146 | (defun org-element-target-interpreter (target contents) | |
3147 | "Interpret TARGET object as Org syntax. | |
3148 | CONTENTS is nil." | |
3149 | (format "<<%s>>" (org-element-property :value target))) | |
3150 | ||
3151 | (defun org-element-target-successor (limit) | |
3152 | "Search for the next target object. | |
3153 | ||
3154 | LIMIT bounds the search. | |
3155 | ||
3156 | Return value is a cons cell whose CAR is `target' and CDR is | |
3157 | beginning position." | |
3158 | (save-excursion | |
3159 | (when (re-search-forward org-target-regexp limit t) | |
3160 | (cons 'target (match-beginning 0))))) | |
3161 | ||
3162 | ||
3163 | ;;;; Timestamp | |
3164 | ||
3165 | (defun org-element-timestamp-parser () | |
3166 | "Parse time stamp at point. | |
3167 | ||
3168 | Return a list whose CAR is `timestamp', and CDR a plist with | |
3169 | `:type', `:begin', `:end', `:value' and `:post-blank' keywords. | |
3170 | ||
3171 | Assume point is at the beginning of the timestamp." | |
3172 | (save-excursion | |
3173 | (let* ((begin (point)) | |
3174 | (activep (eq (char-after) ?<)) | |
3175 | (main-value | |
3176 | (progn | |
3177 | (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?") | |
3178 | (match-string-no-properties 1))) | |
3179 | (range-end (match-string-no-properties 3)) | |
3180 | (type (cond ((match-string 2) 'diary) | |
3181 | ((and activep range-end) 'active-range) | |
3182 | (activep 'active) | |
3183 | (range-end 'inactive-range) | |
3184 | (t 'inactive))) | |
3185 | (post-blank (progn (goto-char (match-end 0)) | |
3186 | (skip-chars-forward " \t"))) | |
3187 | (end (point))) | |
3188 | (list 'timestamp | |
3189 | (list :type type | |
3190 | :value main-value | |
3191 | :range-end range-end | |
3192 | :begin begin | |
3193 | :end end | |
3194 | :post-blank post-blank))))) | |
3195 | ||
3196 | (defun org-element-timestamp-interpreter (timestamp contents) | |
3197 | "Interpret TIMESTAMP object as Org syntax. | |
3198 | CONTENTS is nil." | |
3199 | (let ((type (org-element-property :type timestamp) )) | |
3200 | (concat | |
3201 | (format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>") | |
3202 | (org-element-property :value timestamp)) | |
3203 | (let ((range-end (org-element-property :range-end timestamp))) | |
3204 | (when range-end | |
3205 | (concat "--" | |
3206 | (format (if (eq type 'inactive-range) "[%s]" "<%s>") | |
3207 | range-end))))))) | |
3208 | ||
3209 | (defun org-element-timestamp-successor (limit) | |
3210 | "Search for the next timestamp object. | |
3211 | ||
3212 | LIMIT bounds the search. | |
3213 | ||
3214 | Return value is a cons cell whose CAR is `timestamp' and CDR is | |
3215 | beginning position." | |
3216 | (save-excursion | |
3217 | (when (re-search-forward | |
3218 | (concat org-ts-regexp-both | |
3219 | "\\|" | |
3220 | "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)" | |
3221 | "\\|" | |
3222 | "\\(?:<%%\\(?:([^>\n]+)\\)>\\)") | |
3223 | limit t) | |
3224 | (cons 'timestamp (match-beginning 0))))) | |
3225 | ||
3226 | ||
3227 | ;;;; Underline | |
3228 | ||
3229 | (defun org-element-underline-parser () | |
3230 | "Parse underline object at point. | |
3231 | ||
3232 | Return a list whose CAR is `underline' and CDR is a plist with | |
3233 | `:begin', `:end', `:contents-begin' and `:contents-end' and | |
3234 | `:post-blank' keywords. | |
3235 | ||
3236 | Assume point is at the first underscore marker." | |
3237 | (save-excursion | |
3238 | (unless (bolp) (backward-char 1)) | |
3239 | (looking-at org-emph-re) | |
3240 | (let ((begin (match-beginning 2)) | |
3241 | (contents-begin (match-beginning 4)) | |
3242 | (contents-end (match-end 4)) | |
3243 | (post-blank (progn (goto-char (match-end 2)) | |
3244 | (skip-chars-forward " \t"))) | |
3245 | (end (point))) | |
3246 | (list 'underline | |
3247 | (list :begin begin | |
3248 | :end end | |
3249 | :contents-begin contents-begin | |
3250 | :contents-end contents-end | |
3251 | :post-blank post-blank))))) | |
3252 | ||
3253 | (defun org-element-underline-interpreter (underline contents) | |
3254 | "Interpret UNDERLINE object as Org syntax. | |
3255 | CONTENTS is the contents of the object." | |
3256 | (format "_%s_" contents)) | |
3257 | ||
3258 | ||
3259 | ;;;; Verbatim | |
3260 | ||
3261 | (defun org-element-verbatim-parser () | |
3262 | "Parse verbatim object at point. | |
3263 | ||
3264 | Return a list whose CAR is `verbatim' and CDR is a plist with | |
3265 | `:value', `:begin', `:end' and `:post-blank' keywords. | |
3266 | ||
3267 | Assume point is at the first equal sign marker." | |
3268 | (save-excursion | |
3269 | (unless (bolp) (backward-char 1)) | |
3270 | (looking-at org-emph-re) | |
3271 | (let ((begin (match-beginning 2)) | |
3272 | (value (org-match-string-no-properties 4)) | |
3273 | (post-blank (progn (goto-char (match-end 2)) | |
3274 | (skip-chars-forward " \t"))) | |
3275 | (end (point))) | |
3276 | (list 'verbatim | |
3277 | (list :value value | |
3278 | :begin begin | |
3279 | :end end | |
3280 | :post-blank post-blank))))) | |
3281 | ||
3282 | (defun org-element-verbatim-interpreter (verbatim contents) | |
3283 | "Interpret VERBATIM object as Org syntax. | |
3284 | CONTENTS is nil." | |
3285 | (format "=%s=" (org-element-property :value verbatim))) | |
3286 | ||
3287 | ||
3288 | \f | |
3289 | ;;; Parsing Element Starting At Point | |
3290 | ;; | |
3291 | ;; `org-element--current-element' is the core function of this section. | |
3292 | ;; It returns the Lisp representation of the element starting at | |
3293 | ;; point. | |
3294 | ;; | |
3295 | ;; `org-element--current-element' makes use of special modes. They | |
3296 | ;; are activated for fixed element chaining (i.e. `plain-list' > | |
3297 | ;; `item') or fixed conditional element chaining (i.e. `headline' > | |
3298 | ;; `section'). Special modes are: `first-section', `section', | |
3299 | ;; `quote-section', `item' and `table-row'. | |
3300 | ||
3301 | (defun org-element--current-element | |
3302 | (limit &optional granularity special structure) | |
3303 | "Parse the element starting at point. | |
3304 | ||
3305 | LIMIT bounds the search. | |
3306 | ||
3307 | Return value is a list like (TYPE PROPS) where TYPE is the type | |
3308 | of the element and PROPS a plist of properties associated to the | |
3309 | element. | |
3310 | ||
3311 | Possible types are defined in `org-element-all-elements'. | |
3312 | ||
3313 | Optional argument GRANULARITY determines the depth of the | |
3314 | recursion. Allowed values are `headline', `greater-element', | |
3315 | `element', `object' or nil. When it is broader than `object' (or | |
3316 | nil), secondary values will not be parsed, since they only | |
3317 | contain objects. | |
3318 | ||
3319 | Optional argument SPECIAL, when non-nil, can be either | |
3320 | `first-section', `section', `quote-section', `table-row' and | |
3321 | `item'. | |
3322 | ||
3323 | If STRUCTURE isn't provided but SPECIAL is set to `item', it will | |
3324 | be computed. | |
3325 | ||
3326 | This function assumes point is always at the beginning of the | |
3327 | element it has to parse." | |
3328 | (save-excursion | |
3329 | ;; If point is at an affiliated keyword, try moving to the | |
3330 | ;; beginning of the associated element. If none is found, the | |
3331 | ;; keyword is orphaned and will be treated as plain text. | |
3332 | (when (looking-at org-element--affiliated-re) | |
3333 | (let ((opoint (point))) | |
3334 | (while (looking-at org-element--affiliated-re) (forward-line)) | |
3335 | (when (looking-at "[ \t]*$") (goto-char opoint)))) | |
3336 | (let ((case-fold-search t) | |
3337 | ;; Determine if parsing depth allows for secondary strings | |
3338 | ;; parsing. It only applies to elements referenced in | |
3339 | ;; `org-element-secondary-value-alist'. | |
3340 | (raw-secondary-p (and granularity (not (eq granularity 'object))))) | |
3341 | (cond | |
3342 | ;; Item. | |
3343 | ((eq special 'item) | |
3344 | (org-element-item-parser limit structure raw-secondary-p)) | |
3345 | ;; Table Row. | |
3346 | ((eq special 'table-row) (org-element-table-row-parser limit)) | |
3347 | ;; Headline. | |
3348 | ((org-with-limited-levels (org-at-heading-p)) | |
3349 | (org-element-headline-parser limit raw-secondary-p)) | |
3350 | ;; Sections (must be checked after headline). | |
3351 | ((eq special 'section) (org-element-section-parser limit)) | |
3352 | ((eq special 'quote-section) (org-element-quote-section-parser limit)) | |
3353 | ((eq special 'first-section) | |
3354 | (org-element-section-parser | |
3355 | (or (save-excursion (org-with-limited-levels (outline-next-heading))) | |
3356 | limit))) | |
3357 | ;; When not at bol, point is at the beginning of an item or | |
3358 | ;; a footnote definition: next item is always a paragraph. | |
3359 | ((not (bolp)) (org-element-paragraph-parser limit)) | |
3360 | ;; Planning and Clock. | |
3361 | ((and (looking-at org-planning-or-clock-line-re)) | |
3362 | (if (equal (match-string 1) org-clock-string) | |
3363 | (org-element-clock-parser limit) | |
3364 | (org-element-planning-parser limit))) | |
3365 | ;; Inlinetask. | |
3366 | ((org-at-heading-p) | |
3367 | (org-element-inlinetask-parser limit raw-secondary-p)) | |
3368 | ;; LaTeX Environment. | |
3369 | ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}") | |
3370 | (if (save-excursion | |
3371 | (re-search-forward | |
3372 | (format "[ \t]*\\\\end{%s}[ \t]*" | |
3373 | (regexp-quote (match-string 1))) | |
3374 | nil t)) | |
3375 | (org-element-latex-environment-parser limit) | |
3376 | (org-element-paragraph-parser limit))) | |
3377 | ;; Drawer and Property Drawer. | |
3378 | ((looking-at org-drawer-regexp) | |
3379 | (let ((name (match-string 1))) | |
3380 | (cond | |
3381 | ((not (save-excursion | |
3382 | (re-search-forward "^[ \t]*:END:[ \t]*$" nil t))) | |
3383 | (org-element-paragraph-parser limit)) | |
3384 | ((equal "PROPERTIES" name) | |
3385 | (org-element-property-drawer-parser limit)) | |
3386 | (t (org-element-drawer-parser limit))))) | |
3387 | ;; Fixed Width | |
3388 | ((looking-at "[ \t]*:\\( \\|$\\)") | |
3389 | (org-element-fixed-width-parser limit)) | |
3390 | ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and | |
3391 | ;; Keywords. | |
3392 | ((looking-at "[ \t]*#") | |
3393 | (goto-char (match-end 0)) | |
3394 | (cond ((looking-at "\\(?: \\|$\\)") | |
3395 | (beginning-of-line) | |
3396 | (org-element-comment-parser limit)) | |
3397 | ((looking-at "\\+BEGIN_\\(\\S-+\\)") | |
3398 | (beginning-of-line) | |
3399 | (let ((parser (assoc (upcase (match-string 1)) | |
3400 | org-element-block-name-alist))) | |
3401 | (if parser (funcall (cdr parser) limit) | |
3402 | (org-element-special-block-parser limit)))) | |
3403 | ((looking-at "\\+CALL:") | |
3404 | (beginning-of-line) | |
3405 | (org-element-babel-call-parser limit)) | |
3406 | ((looking-at "\\+BEGIN:? ") | |
3407 | (beginning-of-line) | |
3408 | (org-element-dynamic-block-parser limit)) | |
3409 | ((looking-at "\\+\\S-+:") | |
3410 | (beginning-of-line) | |
3411 | (org-element-keyword-parser limit)) | |
3412 | (t | |
3413 | (beginning-of-line) | |
3414 | (org-element-paragraph-parser limit)))) | |
3415 | ;; Footnote Definition. | |
3416 | ((looking-at org-footnote-definition-re) | |
3417 | (org-element-footnote-definition-parser limit)) | |
3418 | ;; Horizontal Rule. | |
3419 | ((looking-at "[ \t]*-\\{5,\\}[ \t]*$") | |
3420 | (org-element-horizontal-rule-parser limit)) | |
3421 | ;; Table. | |
3422 | ((org-at-table-p t) (org-element-table-parser limit)) | |
3423 | ;; List. | |
3424 | ((looking-at (org-item-re)) | |
3425 | (org-element-plain-list-parser limit (or structure (org-list-struct)))) | |
3426 | ;; Default element: Paragraph. | |
3427 | (t (org-element-paragraph-parser limit)))))) | |
3428 | ||
3429 | ||
3430 | ;; Most elements can have affiliated keywords. When looking for an | |
3431 | ;; element beginning, we want to move before them, as they belong to | |
3432 | ;; that element, and, in the meantime, collect information they give | |
3433 | ;; into appropriate properties. Hence the following function. | |
3434 | ;; | |
3435 | ;; Usage of optional arguments may not be obvious at first glance: | |
3436 | ;; | |
3437 | ;; - TRANS-LIST is used to polish keywords names that have evolved | |
3438 | ;; during Org history. In example, even though =result= and | |
3439 | ;; =results= coexist, we want to have them under the same =result= | |
3440 | ;; property. It's also true for "srcname" and "name", where the | |
3441 | ;; latter seems to be preferred nowadays (thus the "name" property). | |
3442 | ;; | |
3443 | ;; - CONSED allows to regroup multi-lines keywords under the same | |
3444 | ;; property, while preserving their own identity. This is mostly | |
3445 | ;; used for "attr_latex" and al. | |
3446 | ;; | |
3447 | ;; - PARSED prepares a keyword value for export. This is useful for | |
3448 | ;; "caption". Objects restrictions for such keywords are defined in | |
3449 | ;; `org-element-object-restrictions'. | |
3450 | ;; | |
3451 | ;; - DUALS is used to take care of keywords accepting a main and an | |
3452 | ;; optional secondary values. For example "results" has its | |
3453 | ;; source's name as the main value, and may have an hash string in | |
3454 | ;; optional square brackets as the secondary one. | |
3455 | ;; | |
3456 | ;; A keyword may belong to more than one category. | |
3457 | ||
3458 | (defun org-element--collect-affiliated-keywords | |
3459 | (&optional key-re trans-list consed parsed duals) | |
3460 | "Collect affiliated keywords before point. | |
3461 | ||
3462 | Optional argument KEY-RE is a regexp matching keywords, which | |
3463 | puts matched keyword in group 1. It defaults to | |
3464 | `org-element--affiliated-re'. | |
3465 | ||
3466 | TRANS-LIST is an alist where key is the keyword and value the | |
3467 | property name it should be translated to, without the colons. It | |
3468 | defaults to `org-element-keyword-translation-alist'. | |
3469 | ||
3470 | CONSED is a list of strings. Any keyword belonging to that list | |
3471 | will have its value consed. The check is done after keyword | |
3472 | translation. It defaults to `org-element-multiple-keywords'. | |
3473 | ||
3474 | PARSED is a list of strings. Any keyword member of this list | |
3475 | will have its value parsed. The check is done after keyword | |
3476 | translation. If a keyword is a member of both CONSED and PARSED, | |
3477 | it's value will be a list of parsed strings. It defaults to | |
3478 | `org-element-parsed-keywords'. | |
3479 | ||
3480 | DUALS is a list of strings. Any keyword member of this list can | |
3481 | have two parts: one mandatory and one optional. Its value is | |
3482 | a cons cell whose CAR is the former, and the CDR the latter. If | |
3483 | a keyword is a member of both PARSED and DUALS, both values will | |
3484 | be parsed. It defaults to `org-element-dual-keywords'. | |
3485 | ||
3486 | Return a list whose CAR is the position at the first of them and | |
3487 | CDR a plist of keywords and values." | |
3488 | (save-excursion | |
3489 | (let ((case-fold-search t) | |
3490 | (key-re (or key-re org-element--affiliated-re)) | |
3491 | (trans-list (or trans-list org-element-keyword-translation-alist)) | |
3492 | (consed (or consed org-element-multiple-keywords)) | |
3493 | (parsed (or parsed org-element-parsed-keywords)) | |
3494 | (duals (or duals org-element-dual-keywords)) | |
3495 | ;; RESTRICT is the list of objects allowed in parsed | |
3496 | ;; keywords value. | |
3497 | (restrict (org-element-restriction 'keyword)) | |
3498 | output) | |
3499 | (unless (bobp) | |
3500 | (while (and (not (bobp)) (progn (forward-line -1) (looking-at key-re))) | |
3501 | (let* ((raw-kwd (upcase (match-string 1))) | |
3502 | ;; Apply translation to RAW-KWD. From there, KWD is | |
3503 | ;; the official keyword. | |
3504 | (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd)) | |
3505 | ;; Find main value for any keyword. | |
3506 | (value | |
3507 | (save-match-data | |
3508 | (org-trim | |
3509 | (buffer-substring-no-properties | |
3510 | (match-end 0) (point-at-eol))))) | |
3511 | ;; If KWD is a dual keyword, find its secondary | |
3512 | ;; value. Maybe parse it. | |
3513 | (dual-value | |
3514 | (and (member kwd duals) | |
3515 | (let ((sec (org-match-string-no-properties 2))) | |
3516 | (if (or (not sec) (not (member kwd parsed))) sec | |
3517 | (org-element-parse-secondary-string sec restrict))))) | |
3518 | ;; Attribute a property name to KWD. | |
3519 | (kwd-sym (and kwd (intern (concat ":" (downcase kwd)))))) | |
3520 | ;; Now set final shape for VALUE. | |
3521 | (when (member kwd parsed) | |
3522 | (setq value (org-element-parse-secondary-string value restrict))) | |
3523 | (when (member kwd duals) | |
3524 | ;; VALUE is mandatory. Set it to nil if there is none. | |
3525 | (setq value (and value (cons value dual-value)))) | |
3526 | ;; Attributes are always consed. | |
3527 | (when (or (member kwd consed) (string-match "^ATTR_" kwd)) | |
3528 | (setq value (cons value (plist-get output kwd-sym)))) | |
3529 | ;; Eventually store the new value in OUTPUT. | |
3530 | (setq output (plist-put output kwd-sym value)))) | |
3531 | (unless (looking-at key-re) (forward-line 1))) | |
3532 | (list (point) output)))) | |
3533 | ||
3534 | ||
3535 | \f | |
3536 | ;;; The Org Parser | |
3537 | ;; | |
3538 | ;; The two major functions here are `org-element-parse-buffer', which | |
3539 | ;; parses Org syntax inside the current buffer, taking into account | |
3540 | ;; region, narrowing, or even visibility if specified, and | |
3541 | ;; `org-element-parse-secondary-string', which parses objects within | |
3542 | ;; a given string. | |
3543 | ;; | |
3544 | ;; The (almost) almighty `org-element-map' allows to apply a function | |
3545 | ;; on elements or objects matching some type, and accumulate the | |
3546 | ;; resulting values. In an export situation, it also skips unneeded | |
3547 | ;; parts of the parse tree. | |
3548 | ||
3549 | (defun org-element-parse-buffer (&optional granularity visible-only) | |
3550 | "Recursively parse the buffer and return structure. | |
3551 | If narrowing is in effect, only parse the visible part of the | |
3552 | buffer. | |
3553 | ||
3554 | Optional argument GRANULARITY determines the depth of the | |
3555 | recursion. It can be set to the following symbols: | |
3556 | ||
3557 | `headline' Only parse headlines. | |
3558 | `greater-element' Don't recurse into greater elements excepted | |
3559 | headlines and sections. Thus, elements | |
3560 | parsed are the top-level ones. | |
3561 | `element' Parse everything but objects and plain text. | |
3562 | `object' Parse the complete buffer (default). | |
3563 | ||
3564 | When VISIBLE-ONLY is non-nil, don't parse contents of hidden | |
3565 | elements. | |
3566 | ||
3567 | Assume buffer is in Org mode." | |
3568 | (save-excursion | |
3569 | (goto-char (point-min)) | |
3570 | (org-skip-whitespace) | |
3571 | (org-element--parse-elements | |
3572 | (point-at-bol) (point-max) | |
3573 | ;; Start in `first-section' mode so text before the first | |
3574 | ;; headline belongs to a section. | |
3575 | 'first-section nil granularity visible-only (list 'org-data nil)))) | |
3576 | ||
3577 | (defun org-element-parse-secondary-string (string restriction &optional parent) | |
3578 | "Recursively parse objects in STRING and return structure. | |
3579 | ||
3580 | RESTRICTION is a symbol limiting the object types that will be | |
3581 | looked after. | |
3582 | ||
3583 | Optional argument PARENT, when non-nil, is the element or object | |
3584 | containing the secondary string. It is used to set correctly | |
3585 | `:parent' property within the string." | |
3586 | (with-temp-buffer | |
3587 | (insert string) | |
3588 | (let ((secondary (org-element--parse-objects | |
3589 | (point-min) (point-max) nil restriction))) | |
3590 | (mapc (lambda (obj) (org-element-put-property obj :parent parent)) | |
3591 | secondary)))) | |
3592 | ||
3593 | (defun org-element-map (data types fun &optional info first-match no-recursion) | |
3594 | "Map a function on selected elements or objects. | |
3595 | ||
3596 | DATA is the parsed tree, as returned by, i.e, | |
3597 | `org-element-parse-buffer'. TYPES is a symbol or list of symbols | |
3598 | of elements or objects types. FUN is the function called on the | |
3599 | matching element or object. It must accept one arguments: the | |
3600 | element or object itself. | |
3601 | ||
3602 | When optional argument INFO is non-nil, it should be a plist | |
3603 | holding export options. In that case, parts of the parse tree | |
3604 | not exportable according to that property list will be skipped. | |
3605 | ||
3606 | When optional argument FIRST-MATCH is non-nil, stop at the first | |
3607 | match for which FUN doesn't return nil, and return that value. | |
3608 | ||
3609 | Optional argument NO-RECURSION is a symbol or a list of symbols | |
3610 | representing elements or objects types. `org-element-map' won't | |
3611 | enter any recursive element or object whose type belongs to that | |
3612 | list. Though, FUN can still be applied on them. | |
3613 | ||
3614 | Nil values returned from FUN do not appear in the results." | |
3615 | ;; Ensure TYPES and NO-RECURSION are a list, even of one element. | |
3616 | (unless (listp types) (setq types (list types))) | |
3617 | (unless (listp no-recursion) (setq no-recursion (list no-recursion))) | |
3618 | ;; Recursion depth is determined by --CATEGORY. | |
3619 | (let* ((--category | |
3620 | (catch 'found | |
3621 | (let ((category 'greater-elements)) | |
3622 | (mapc (lambda (type) | |
3623 | (cond ((or (memq type org-element-all-objects) | |
3624 | (eq type 'plain-text)) | |
3625 | ;; If one object is found, the function | |
3626 | ;; has to recurse into every object. | |
3627 | (throw 'found 'objects)) | |
3628 | ((not (memq type org-element-greater-elements)) | |
3629 | ;; If one regular element is found, the | |
3630 | ;; function has to recurse, at least, | |
3631 | ;; into every element it encounters. | |
3632 | (and (not (eq category 'elements)) | |
3633 | (setq category 'elements))))) | |
3634 | types) | |
3635 | category))) | |
3636 | --acc | |
3637 | --walk-tree | |
3638 | (--walk-tree | |
3639 | (function | |
3640 | (lambda (--data) | |
3641 | ;; Recursively walk DATA. INFO, if non-nil, is a plist | |
3642 | ;; holding contextual information. | |
3643 | (let ((--type (org-element-type --data))) | |
3644 | (cond | |
3645 | ((not --data)) | |
3646 | ;; Ignored element in an export context. | |
3647 | ((and info (memq --data (plist-get info :ignore-list)))) | |
3648 | ;; Secondary string: only objects can be found there. | |
3649 | ((not --type) | |
3650 | (when (eq --category 'objects) (mapc --walk-tree --data))) | |
3651 | ;; Unconditionally enter parse trees. | |
3652 | ((eq --type 'org-data) | |
3653 | (mapc --walk-tree (org-element-contents --data))) | |
3654 | (t | |
3655 | ;; Check if TYPE is matching among TYPES. If so, | |
3656 | ;; apply FUN to --DATA and accumulate return value | |
3657 | ;; into --ACC (or exit if FIRST-MATCH is non-nil). | |
3658 | (when (memq --type types) | |
3659 | (let ((result (funcall fun --data))) | |
3660 | (cond ((not result)) | |
3661 | (first-match (throw '--map-first-match result)) | |
3662 | (t (push result --acc))))) | |
3663 | ;; If --DATA has a secondary string that can contain | |
3664 | ;; objects with their type among TYPES, look into it. | |
3665 | (when (eq --category 'objects) | |
3666 | (let ((sec-prop | |
3667 | (assq --type org-element-secondary-value-alist))) | |
3668 | (when sec-prop | |
3669 | (funcall --walk-tree | |
3670 | (org-element-property (cdr sec-prop) --data))))) | |
3671 | ;; Determine if a recursion into --DATA is possible. | |
3672 | (cond | |
3673 | ;; --TYPE is explicitly removed from recursion. | |
3674 | ((memq --type no-recursion)) | |
3675 | ;; --DATA has no contents. | |
3676 | ((not (org-element-contents --data))) | |
3677 | ;; Looking for greater elements but --DATA is simply | |
3678 | ;; an element or an object. | |
3679 | ((and (eq --category 'greater-elements) | |
3680 | (not (memq --type org-element-greater-elements)))) | |
3681 | ;; Looking for elements but --DATA is an object. | |
3682 | ((and (eq --category 'elements) | |
3683 | (memq --type org-element-all-objects))) | |
3684 | ;; In any other case, map contents. | |
3685 | (t (mapc --walk-tree (org-element-contents --data))))))))))) | |
3686 | (catch '--map-first-match | |
3687 | (funcall --walk-tree data) | |
3688 | ;; Return value in a proper order. | |
3689 | (nreverse --acc)))) | |
3690 | ||
3691 | ;; The following functions are internal parts of the parser. | |
3692 | ;; | |
3693 | ;; The first one, `org-element--parse-elements' acts at the element's | |
3694 | ;; level. | |
3695 | ;; | |
3696 | ;; The second one, `org-element--parse-objects' applies on all objects | |
3697 | ;; of a paragraph or a secondary string. It uses | |
3698 | ;; `org-element--get-next-object-candidates' to optimize the search of | |
3699 | ;; the next object in the buffer. | |
3700 | ;; | |
3701 | ;; More precisely, that function looks for every allowed object type | |
3702 | ;; first. Then, it discards failed searches, keeps further matches, | |
3703 | ;; and searches again types matched behind point, for subsequent | |
3704 | ;; calls. Thus, searching for a given type fails only once, and every | |
3705 | ;; object is searched only once at top level (but sometimes more for | |
3706 | ;; nested types). | |
3707 | ||
3708 | (defun org-element--parse-elements | |
3709 | (beg end special structure granularity visible-only acc) | |
3710 | "Parse elements between BEG and END positions. | |
3711 | ||
3712 | SPECIAL prioritize some elements over the others. It can be set | |
3713 | to `first-section', `quote-section', `section' `item' or | |
3714 | `table-row'. | |
3715 | ||
3716 | When value is `item', STRUCTURE will be used as the current list | |
3717 | structure. | |
3718 | ||
3719 | GRANULARITY determines the depth of the recursion. See | |
3720 | `org-element-parse-buffer' for more information. | |
3721 | ||
3722 | When VISIBLE-ONLY is non-nil, don't parse contents of hidden | |
3723 | elements. | |
3724 | ||
3725 | Elements are accumulated into ACC." | |
3726 | (save-excursion | |
3727 | (goto-char beg) | |
3728 | ;; When parsing only headlines, skip any text before first one. | |
3729 | (when (and (eq granularity 'headline) (not (org-at-heading-p))) | |
3730 | (org-with-limited-levels (outline-next-heading))) | |
3731 | ;; Main loop start. | |
3732 | (while (< (point) end) | |
3733 | ;; Find current element's type and parse it accordingly to | |
3734 | ;; its category. | |
3735 | (let* ((element (org-element--current-element | |
3736 | end granularity special structure)) | |
3737 | (type (org-element-type element)) | |
3738 | (cbeg (org-element-property :contents-begin element))) | |
3739 | (goto-char (org-element-property :end element)) | |
3740 | ;; Fill ELEMENT contents by side-effect. | |
3741 | (cond | |
3742 | ;; If VISIBLE-ONLY is true and element is hidden or if it has | |
3743 | ;; no contents, don't modify it. | |
3744 | ((or (and visible-only (org-element-property :hiddenp element)) | |
3745 | (not cbeg))) | |
3746 | ;; Greater element: parse it between `contents-begin' and | |
3747 | ;; `contents-end'. Make sure GRANULARITY allows the | |
3748 | ;; recursion, or ELEMENT is an headline, in which case going | |
3749 | ;; inside is mandatory, in order to get sub-level headings. | |
3750 | ((and (memq type org-element-greater-elements) | |
3751 | (or (memq granularity '(element object nil)) | |
3752 | (and (eq granularity 'greater-element) | |
3753 | (eq type 'section)) | |
3754 | (eq type 'headline))) | |
3755 | (org-element--parse-elements | |
3756 | cbeg (org-element-property :contents-end element) | |
3757 | ;; Possibly switch to a special mode. | |
3758 | (case type | |
3759 | (headline | |
3760 | (if (org-element-property :quotedp element) 'quote-section | |
3761 | 'section)) | |
3762 | (plain-list 'item) | |
3763 | (table 'table-row)) | |
3764 | (org-element-property :structure element) | |
3765 | granularity visible-only element)) | |
3766 | ;; ELEMENT has contents. Parse objects inside, if | |
3767 | ;; GRANULARITY allows it. | |
3768 | ((memq granularity '(object nil)) | |
3769 | (org-element--parse-objects | |
3770 | cbeg (org-element-property :contents-end element) element | |
3771 | (org-element-restriction type)))) | |
3772 | (org-element-adopt-elements acc element))) | |
3773 | ;; Return result. | |
3774 | acc)) | |
3775 | ||
3776 | (defun org-element--parse-objects (beg end acc restriction) | |
3777 | "Parse objects between BEG and END and return recursive structure. | |
3778 | ||
3779 | Objects are accumulated in ACC. | |
3780 | ||
3781 | RESTRICTION is a list of object types which are allowed in the | |
3782 | current object." | |
3783 | (let (candidates) | |
3784 | (save-excursion | |
3785 | (goto-char beg) | |
3786 | (while (and (< (point) end) | |
3787 | (setq candidates (org-element--get-next-object-candidates | |
3788 | end restriction candidates))) | |
3789 | (let ((next-object | |
3790 | (let ((pos (apply 'min (mapcar 'cdr candidates)))) | |
3791 | (save-excursion | |
3792 | (goto-char pos) | |
3793 | (funcall (intern (format "org-element-%s-parser" | |
3794 | (car (rassq pos candidates))))))))) | |
3795 | ;; 1. Text before any object. Untabify it. | |
3796 | (let ((obj-beg (org-element-property :begin next-object))) | |
3797 | (unless (= (point) obj-beg) | |
3798 | (setq acc | |
3799 | (org-element-adopt-elements | |
3800 | acc | |
3801 | (replace-regexp-in-string | |
3802 | "\t" (make-string tab-width ? ) | |
3803 | (buffer-substring-no-properties (point) obj-beg)))))) | |
3804 | ;; 2. Object... | |
3805 | (let ((obj-end (org-element-property :end next-object)) | |
3806 | (cont-beg (org-element-property :contents-begin next-object))) | |
3807 | ;; Fill contents of NEXT-OBJECT by side-effect, if it has | |
3808 | ;; a recursive type. | |
3809 | (when (and cont-beg | |
3810 | (memq (car next-object) org-element-recursive-objects)) | |
3811 | (save-restriction | |
3812 | (narrow-to-region | |
3813 | cont-beg | |
3814 | (org-element-property :contents-end next-object)) | |
3815 | (org-element--parse-objects | |
3816 | (point-min) (point-max) next-object | |
3817 | (org-element-restriction next-object)))) | |
3818 | (setq acc (org-element-adopt-elements acc next-object)) | |
3819 | (goto-char obj-end)))) | |
3820 | ;; 3. Text after last object. Untabify it. | |
3821 | (unless (= (point) end) | |
3822 | (setq acc | |
3823 | (org-element-adopt-elements | |
3824 | acc | |
3825 | (replace-regexp-in-string | |
3826 | "\t" (make-string tab-width ? ) | |
3827 | (buffer-substring-no-properties (point) end))))) | |
3828 | ;; Result. | |
3829 | acc))) | |
3830 | ||
3831 | (defun org-element--get-next-object-candidates (limit restriction objects) | |
3832 | "Return an alist of candidates for the next object. | |
3833 | ||
3834 | LIMIT bounds the search, and RESTRICTION narrows candidates to | |
3835 | some object types. | |
3836 | ||
3837 | Return value is an alist whose CAR is position and CDR the object | |
3838 | type, as a symbol. | |
3839 | ||
3840 | OBJECTS is the previous candidates alist." | |
3841 | (let (next-candidates types-to-search) | |
3842 | ;; If no previous result, search every object type in RESTRICTION. | |
3843 | ;; Otherwise, keep potential candidates (old objects located after | |
3844 | ;; point) and ask to search again those which had matched before. | |
3845 | (if (not objects) (setq types-to-search restriction) | |
3846 | (mapc (lambda (obj) | |
3847 | (if (< (cdr obj) (point)) (push (car obj) types-to-search) | |
3848 | (push obj next-candidates))) | |
3849 | objects)) | |
3850 | ;; Call the appropriate successor function for each type to search | |
3851 | ;; and accumulate matches. | |
3852 | (mapc | |
3853 | (lambda (type) | |
3854 | (let* ((successor-fun | |
3855 | (intern | |
3856 | (format "org-element-%s-successor" | |
3857 | (or (cdr (assq type org-element-object-successor-alist)) | |
3858 | type)))) | |
3859 | (obj (funcall successor-fun limit))) | |
3860 | (and obj (push obj next-candidates)))) | |
3861 | types-to-search) | |
3862 | ;; Return alist. | |
3863 | next-candidates)) | |
3864 | ||
3865 | ||
3866 | \f | |
3867 | ;;; Towards A Bijective Process | |
3868 | ;; | |
3869 | ;; The parse tree obtained with `org-element-parse-buffer' is really | |
3870 | ;; a snapshot of the corresponding Org buffer. Therefore, it can be | |
3871 | ;; interpreted and expanded into a string with canonical Org syntax. | |
3872 | ;; Hence `org-element-interpret-data'. | |
3873 | ;; | |
3874 | ;; The function relies internally on | |
3875 | ;; `org-element--interpret-affiliated-keywords'. | |
3876 | ||
3877 | ;;;###autoload | |
3878 | (defun org-element-interpret-data (data &optional parent) | |
3879 | "Interpret DATA as Org syntax. | |
3880 | ||
3881 | DATA is a parse tree, an element, an object or a secondary string | |
3882 | to interpret. | |
3883 | ||
3884 | Optional argument PARENT is used for recursive calls. It contains | |
3885 | the element or object containing data, or nil. | |
3886 | ||
3887 | Return Org syntax as a string." | |
3888 | (let* ((type (org-element-type data)) | |
3889 | (results | |
3890 | (cond | |
3891 | ;; Secondary string. | |
3892 | ((not type) | |
3893 | (mapconcat | |
3894 | (lambda (obj) (org-element-interpret-data obj parent)) | |
3895 | data "")) | |
3896 | ;; Full Org document. | |
3897 | ((eq type 'org-data) | |
3898 | (mapconcat | |
3899 | (lambda (obj) (org-element-interpret-data obj parent)) | |
3900 | (org-element-contents data) "")) | |
3901 | ;; Plain text. | |
3902 | ((stringp data) data) | |
3903 | ;; Element/Object without contents. | |
3904 | ((not (org-element-contents data)) | |
3905 | (funcall (intern (format "org-element-%s-interpreter" type)) | |
3906 | data nil)) | |
3907 | ;; Element/Object with contents. | |
3908 | (t | |
3909 | (let* ((greaterp (memq type org-element-greater-elements)) | |
3910 | (objectp (and (not greaterp) | |
3911 | (memq type org-element-recursive-objects))) | |
3912 | (contents | |
3913 | (mapconcat | |
3914 | (lambda (obj) (org-element-interpret-data obj data)) | |
3915 | (org-element-contents | |
3916 | (if (or greaterp objectp) data | |
3917 | ;; Elements directly containing objects must | |
3918 | ;; have their indentation normalized first. | |
3919 | (org-element-normalize-contents | |
3920 | data | |
3921 | ;; When normalizing first paragraph of an | |
3922 | ;; item or a footnote-definition, ignore | |
3923 | ;; first line's indentation. | |
3924 | (and (eq type 'paragraph) | |
3925 | (equal data (car (org-element-contents parent))) | |
3926 | (memq (org-element-type parent) | |
3927 | '(footnote-definiton item)))))) | |
3928 | ""))) | |
3929 | (funcall (intern (format "org-element-%s-interpreter" type)) | |
3930 | data | |
3931 | (if greaterp (org-element-normalize-contents contents) | |
3932 | contents))))))) | |
3933 | (if (memq type '(org-data plain-text nil)) results | |
3934 | ;; Build white spaces. If no `:post-blank' property is | |
3935 | ;; specified, assume its value is 0. | |
3936 | (let ((post-blank (or (org-element-property :post-blank data) 0))) | |
3937 | (if (memq type org-element-all-objects) | |
3938 | (concat results (make-string post-blank 32)) | |
3939 | (concat | |
3940 | (org-element--interpret-affiliated-keywords data) | |
3941 | (org-element-normalize-string results) | |
3942 | (make-string post-blank 10))))))) | |
3943 | ||
3944 | (defun org-element--interpret-affiliated-keywords (element) | |
3945 | "Return ELEMENT's affiliated keywords as Org syntax. | |
3946 | If there is no affiliated keyword, return the empty string." | |
3947 | (let ((keyword-to-org | |
3948 | (function | |
3949 | (lambda (key value) | |
3950 | (let (dual) | |
3951 | (when (member key org-element-dual-keywords) | |
3952 | (setq dual (cdr value) value (car value))) | |
3953 | (concat "#+" key | |
3954 | (and dual | |
3955 | (format "[%s]" (org-element-interpret-data dual))) | |
3956 | ": " | |
3957 | (if (member key org-element-parsed-keywords) | |
3958 | (org-element-interpret-data value) | |
3959 | value) | |
3960 | "\n")))))) | |
3961 | (mapconcat | |
3962 | (lambda (prop) | |
3963 | (let ((value (org-element-property prop element)) | |
3964 | (keyword (upcase (substring (symbol-name prop) 1)))) | |
3965 | (when value | |
3966 | (if (or (member keyword org-element-multiple-keywords) | |
3967 | ;; All attribute keywords can have multiple lines. | |
3968 | (string-match "^ATTR_" keyword)) | |
3969 | (mapconcat (lambda (line) (funcall keyword-to-org keyword line)) | |
3970 | value | |
3971 | "") | |
3972 | (funcall keyword-to-org keyword value))))) | |
3973 | ;; List all ELEMENT's properties matching an attribute line or an | |
3974 | ;; affiliated keyword, but ignore translated keywords since they | |
3975 | ;; cannot belong to the property list. | |
3976 | (loop for prop in (nth 1 element) by 'cddr | |
3977 | when (let ((keyword (upcase (substring (symbol-name prop) 1)))) | |
3978 | (or (string-match "^ATTR_" keyword) | |
3979 | (and | |
3980 | (member keyword org-element-affiliated-keywords) | |
3981 | (not (assoc keyword | |
3982 | org-element-keyword-translation-alist))))) | |
3983 | collect prop) | |
3984 | ""))) | |
3985 | ||
3986 | ;; Because interpretation of the parse tree must return the same | |
3987 | ;; number of blank lines between elements and the same number of white | |
3988 | ;; space after objects, some special care must be given to white | |
3989 | ;; spaces. | |
3990 | ;; | |
3991 | ;; The first function, `org-element-normalize-string', ensures any | |
3992 | ;; string different from the empty string will end with a single | |
3993 | ;; newline character. | |
3994 | ;; | |
3995 | ;; The second function, `org-element-normalize-contents', removes | |
3996 | ;; global indentation from the contents of the current element. | |
3997 | ||
3998 | (defun org-element-normalize-string (s) | |
3999 | "Ensure string S ends with a single newline character. | |
4000 | ||
4001 | If S isn't a string return it unchanged. If S is the empty | |
4002 | string, return it. Otherwise, return a new string with a single | |
4003 | newline character at its end." | |
4004 | (cond | |
4005 | ((not (stringp s)) s) | |
4006 | ((string= "" s) "") | |
4007 | (t (and (string-match "\\(\n[ \t]*\\)*\\'" s) | |
4008 | (replace-match "\n" nil nil s))))) | |
4009 | ||
4010 | (defun org-element-normalize-contents (element &optional ignore-first) | |
4011 | "Normalize plain text in ELEMENT's contents. | |
4012 | ||
4013 | ELEMENT must only contain plain text and objects. | |
4014 | ||
4015 | If optional argument IGNORE-FIRST is non-nil, ignore first line's | |
4016 | indentation to compute maximal common indentation. | |
4017 | ||
4018 | Return the normalized element that is element with global | |
4019 | indentation removed from its contents. The function assumes that | |
4020 | indentation is not done with TAB characters." | |
4021 | (let* (ind-list ; for byte-compiler | |
4022 | collect-inds ; for byte-compiler | |
4023 | (collect-inds | |
4024 | (function | |
4025 | ;; Return list of indentations within BLOB. This is done by | |
4026 | ;; walking recursively BLOB and updating IND-LIST along the | |
4027 | ;; way. FIRST-FLAG is non-nil when the first string hasn't | |
4028 | ;; been seen yet. It is required as this string is the only | |
4029 | ;; one whose indentation doesn't happen after a newline | |
4030 | ;; character. | |
4031 | (lambda (blob first-flag) | |
4032 | (mapc | |
4033 | (lambda (object) | |
4034 | (when (and first-flag (stringp object)) | |
4035 | (setq first-flag nil) | |
4036 | (string-match "\\`\\( *\\)" object) | |
4037 | (let ((len (length (match-string 1 object)))) | |
4038 | ;; An indentation of zero means no string will be | |
4039 | ;; modified. Quit the process. | |
4040 | (if (zerop len) (throw 'zero (setq ind-list nil)) | |
4041 | (push len ind-list)))) | |
4042 | (cond | |
4043 | ((stringp object) | |
4044 | (let ((start 0)) | |
4045 | ;; Avoid matching blank or empty lines. | |
4046 | (while (and (string-match "\n\\( *\\)\\(.\\)" object start) | |
4047 | (not (equal (match-string 2 object) " "))) | |
4048 | (setq start (match-end 0)) | |
4049 | (push (length (match-string 1 object)) ind-list)))) | |
4050 | ((memq (org-element-type object) org-element-recursive-objects) | |
4051 | (funcall collect-inds object first-flag)))) | |
4052 | (org-element-contents blob)))))) | |
4053 | ;; Collect indentation list in ELEMENT. Possibly remove first | |
4054 | ;; value if IGNORE-FIRST is non-nil. | |
4055 | (catch 'zero (funcall collect-inds element (not ignore-first))) | |
4056 | (if (not ind-list) element | |
4057 | ;; Build ELEMENT back, replacing each string with the same | |
4058 | ;; string minus common indentation. | |
4059 | (let* (build ; For byte compiler. | |
4060 | (build | |
4061 | (function | |
4062 | (lambda (blob mci first-flag) | |
4063 | ;; Return BLOB with all its strings indentation | |
4064 | ;; shortened from MCI white spaces. FIRST-FLAG is | |
4065 | ;; non-nil when the first string hasn't been seen | |
4066 | ;; yet. | |
4067 | (setcdr (cdr blob) | |
4068 | (mapcar | |
4069 | (lambda (object) | |
4070 | (when (and first-flag (stringp object)) | |
4071 | (setq first-flag nil) | |
4072 | (setq object | |
4073 | (replace-regexp-in-string | |
4074 | (format "\\` \\{%d\\}" mci) "" object))) | |
4075 | (cond | |
4076 | ((stringp object) | |
4077 | (replace-regexp-in-string | |
4078 | (format "\n \\{%d\\}" mci) "\n" object)) | |
4079 | ((memq (org-element-type object) | |
4080 | org-element-recursive-objects) | |
4081 | (funcall build object mci first-flag)) | |
4082 | (t object))) | |
4083 | (org-element-contents blob))) | |
4084 | blob)))) | |
4085 | (funcall build element (apply 'min ind-list) (not ignore-first)))))) | |
4086 | ||
4087 | ||
4088 | \f | |
4089 | ;;; The Toolbox | |
4090 | ;; | |
4091 | ;; The first move is to implement a way to obtain the smallest element | |
4092 | ;; containing point. This is the job of `org-element-at-point'. It | |
4093 | ;; basically jumps back to the beginning of section containing point | |
4094 | ;; and moves, element after element, with | |
4095 | ;; `org-element--current-element' until the container is found. Note: | |
4096 | ;; When using `org-element-at-point', secondary values are never | |
4097 | ;; parsed since the function focuses on elements, not on objects. | |
4098 | ;; | |
4099 | ;; At a deeper level, `org-element-context' lists all elements and | |
4100 | ;; objects containing point. | |
4101 | ;; | |
4102 | ;; `org-element-nested-p' and `org-element-swap-A-B' may be used | |
4103 | ;; internally by navigation and manipulation tools. | |
4104 | ||
4105 | ;;;###autoload | |
4106 | (defun org-element-at-point (&optional keep-trail) | |
4107 | "Determine closest element around point. | |
4108 | ||
4109 | Return value is a list like (TYPE PROPS) where TYPE is the type | |
4110 | of the element and PROPS a plist of properties associated to the | |
4111 | element. | |
4112 | ||
4113 | Possible types are defined in `org-element-all-elements'. | |
4114 | Properties depend on element or object type, but always | |
4115 | include :begin, :end, :parent and :post-blank properties. | |
4116 | ||
4117 | As a special case, if point is at the very beginning of a list or | |
4118 | sub-list, returned element will be that list instead of the first | |
4119 | item. In the same way, if point is at the beginning of the first | |
4120 | row of a table, returned element will be the table instead of the | |
4121 | first row. | |
4122 | ||
4123 | If optional argument KEEP-TRAIL is non-nil, the function returns | |
4124 | a list of of elements leading to element at point. The list's | |
4125 | CAR is always the element at point. Following positions contain | |
4126 | element's siblings, then parents, siblings of parents, until the | |
4127 | first element of current section." | |
4128 | (org-with-wide-buffer | |
4129 | ;; If at an headline, parse it. It is the sole element that | |
4130 | ;; doesn't require to know about context. Be sure to disallow | |
4131 | ;; secondary string parsing, though. | |
4132 | (if (org-with-limited-levels (org-at-heading-p)) | |
4133 | (progn | |
4134 | (beginning-of-line) | |
4135 | (if (not keep-trail) (org-element-headline-parser (point-max) t) | |
4136 | (list (org-element-headline-parser (point-max) t)))) | |
4137 | ;; Otherwise move at the beginning of the section containing | |
4138 | ;; point. | |
4139 | (let ((origin (point)) | |
4140 | (end (save-excursion | |
4141 | (org-with-limited-levels (outline-next-heading)) (point))) | |
4142 | element type special-flag trail struct prevs parent) | |
4143 | (org-with-limited-levels | |
4144 | (if (org-with-limited-levels (org-before-first-heading-p)) | |
4145 | (goto-char (point-min)) | |
4146 | (org-back-to-heading) | |
4147 | (forward-line))) | |
4148 | (org-skip-whitespace) | |
4149 | (beginning-of-line) | |
4150 | ;; Parse successively each element, skipping those ending | |
4151 | ;; before original position. | |
4152 | (catch 'exit | |
4153 | (while t | |
4154 | (setq element | |
4155 | (org-element--current-element end 'element special-flag struct) | |
4156 | type (car element)) | |
4157 | (org-element-put-property element :parent parent) | |
4158 | (when keep-trail (push element trail)) | |
4159 | (cond | |
4160 | ;; 1. Skip any element ending before point. Also skip | |
4161 | ;; element ending at point when we're sure that another | |
4162 | ;; element has started. | |
4163 | ((let ((elem-end (org-element-property :end element))) | |
4164 | (when (or (< elem-end origin) | |
4165 | (and (= elem-end origin) (/= elem-end end))) | |
4166 | (goto-char elem-end)))) | |
4167 | ;; 2. An element containing point is always the element at | |
4168 | ;; point. | |
4169 | ((not (memq type org-element-greater-elements)) | |
4170 | (throw 'exit (if keep-trail trail element))) | |
4171 | ;; 3. At any other greater element type, if point is | |
4172 | ;; within contents, move into it. | |
4173 | (t | |
4174 | (let ((cbeg (org-element-property :contents-begin element)) | |
4175 | (cend (org-element-property :contents-end element))) | |
4176 | (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin) | |
4177 | ;; Create an anchor for tables and plain lists: | |
4178 | ;; when point is at the very beginning of these | |
4179 | ;; elements, ignoring affiliated keywords, | |
4180 | ;; target them instead of their contents. | |
4181 | (and (= cbeg origin) (memq type '(plain-list table))) | |
4182 | ;; When point is at contents end, do not move | |
4183 | ;; into elements with an explicit ending, but | |
4184 | ;; return that element instead. | |
4185 | (and (= cend origin) | |
4186 | (memq type | |
4187 | '(center-block | |
4188 | drawer dynamic-block inlinetask item | |
4189 | plain-list quote-block special-block)))) | |
4190 | (throw 'exit (if keep-trail trail element)) | |
4191 | (setq parent element) | |
4192 | (case type | |
4193 | (plain-list | |
4194 | (setq special-flag 'item | |
4195 | struct (org-element-property :structure element))) | |
4196 | (table (setq special-flag 'table-row)) | |
4197 | (otherwise (setq special-flag nil))) | |
4198 | (setq end cend) | |
4199 | (goto-char cbeg))))))))))) | |
4200 | ||
4201 | ;;;###autoload | |
4202 | (defun org-element-context () | |
4203 | "Return closest element or object around point. | |
4204 | ||
4205 | Return value is a list like (TYPE PROPS) where TYPE is the type | |
4206 | of the element or object and PROPS a plist of properties | |
4207 | associated to it. | |
4208 | ||
4209 | Possible types are defined in `org-element-all-elements' and | |
4210 | `org-element-all-objects'. Properties depend on element or | |
4211 | object type, but always include :begin, :end, :parent | |
4212 | and :post-blank properties." | |
4213 | (org-with-wide-buffer | |
4214 | (let* ((origin (point)) | |
4215 | (element (org-element-at-point)) | |
4216 | (type (car element)) | |
4217 | end) | |
4218 | ;; Check if point is inside an element containing objects or at | |
4219 | ;; a secondary string. In that case, move to beginning of the | |
4220 | ;; element or secondary string and set END to the other side. | |
4221 | (if (not (or (and (eq type 'item) | |
4222 | (let ((tag (org-element-property :tag element))) | |
4223 | (and tag | |
4224 | (progn | |
4225 | (beginning-of-line) | |
4226 | (search-forward tag (point-at-eol)) | |
4227 | (goto-char (match-beginning 0)) | |
4228 | (and (>= origin (point)) | |
4229 | (<= origin | |
4230 | ;; `1+' is required so some | |
4231 | ;; successors can match | |
4232 | ;; properly their object. | |
4233 | (setq end (1+ (match-end 0))))))))) | |
4234 | (and (memq type '(headline inlinetask)) | |
4235 | (progn (beginning-of-line) | |
4236 | (skip-chars-forward "* ") | |
4237 | (setq end (point-at-eol)))) | |
4238 | (and (memq type '(paragraph table-cell verse-block)) | |
4239 | (let ((cbeg (org-element-property | |
4240 | :contents-begin element)) | |
4241 | (cend (org-element-property | |
4242 | :contents-end element))) | |
4243 | (and (>= origin cbeg) | |
4244 | (<= origin cend) | |
4245 | (progn (goto-char cbeg) (setq end cend))))))) | |
4246 | element | |
4247 | (let ((restriction (org-element-restriction element)) | |
4248 | (parent element) | |
4249 | candidates) | |
4250 | (catch 'exit | |
4251 | (while (setq candidates (org-element--get-next-object-candidates | |
4252 | end restriction candidates)) | |
4253 | (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates)) | |
4254 | candidates))) | |
4255 | ;; If ORIGIN is before next object in element, there's | |
4256 | ;; no point in looking further. | |
4257 | (if (> (cdr closest-cand) origin) (throw 'exit element) | |
4258 | (let* ((object | |
4259 | (progn (goto-char (cdr closest-cand)) | |
4260 | (funcall (intern (format "org-element-%s-parser" | |
4261 | (car closest-cand)))))) | |
4262 | (cbeg (org-element-property :contents-begin object)) | |
4263 | (cend (org-element-property :contents-end object))) | |
4264 | (cond | |
4265 | ;; ORIGIN is after OBJECT, so skip it. | |
4266 | ((< (org-element-property :end object) origin) | |
4267 | (goto-char (org-element-property :end object))) | |
4268 | ;; ORIGIN is within a non-recursive object or at an | |
4269 | ;; object boundaries: Return that object. | |
4270 | ((or (not cbeg) (> cbeg origin) (< cend origin)) | |
4271 | (throw 'exit | |
4272 | (org-element-put-property object :parent parent))) | |
4273 | ;; Otherwise, move within current object and restrict | |
4274 | ;; search to the end of its contents. | |
4275 | (t (goto-char cbeg) | |
4276 | (org-element-put-property object :parent parent) | |
4277 | (setq parent object end cend))))))) | |
4278 | parent)))))) | |
4279 | ||
4280 | (defsubst org-element-nested-p (elem-A elem-B) | |
4281 | "Non-nil when elements ELEM-A and ELEM-B are nested." | |
4282 | (let ((beg-A (org-element-property :begin elem-A)) | |
4283 | (beg-B (org-element-property :begin elem-B)) | |
4284 | (end-A (org-element-property :end elem-A)) | |
4285 | (end-B (org-element-property :end elem-B))) | |
4286 | (or (and (>= beg-A beg-B) (<= end-A end-B)) | |
4287 | (and (>= beg-B beg-A) (<= end-B end-A))))) | |
4288 | ||
4289 | (defun org-element-swap-A-B (elem-A elem-B) | |
4290 | "Swap elements ELEM-A and ELEM-B. | |
4291 | Assume ELEM-B is after ELEM-A in the buffer. Leave point at the | |
4292 | end of ELEM-A." | |
4293 | (goto-char (org-element-property :begin elem-A)) | |
4294 | ;; There are two special cases when an element doesn't start at bol: | |
4295 | ;; the first paragraph in an item or in a footnote definition. | |
4296 | (let ((specialp (not (bolp)))) | |
4297 | ;; Only a paragraph without any affiliated keyword can be moved at | |
4298 | ;; ELEM-A position in such a situation. Note that the case of | |
4299 | ;; a footnote definition is impossible: it cannot contain two | |
4300 | ;; paragraphs in a row because it cannot contain a blank line. | |
4301 | (if (and specialp | |
4302 | (or (not (eq (org-element-type elem-B) 'paragraph)) | |
4303 | (/= (org-element-property :begin elem-B) | |
4304 | (org-element-property :contents-begin elem-B)))) | |
4305 | (error "Cannot swap elements")) | |
4306 | ;; In a special situation, ELEM-A will have no indentation. We'll | |
4307 | ;; give it ELEM-B's (which will in, in turn, have no indentation). | |
4308 | (let* ((ind-B (when specialp | |
4309 | (goto-char (org-element-property :begin elem-B)) | |
4310 | (org-get-indentation))) | |
4311 | (beg-A (org-element-property :begin elem-A)) | |
4312 | (end-A (save-excursion | |
4313 | (goto-char (org-element-property :end elem-A)) | |
4314 | (skip-chars-backward " \r\t\n") | |
4315 | (point-at-eol))) | |
4316 | (beg-B (org-element-property :begin elem-B)) | |
4317 | (end-B (save-excursion | |
4318 | (goto-char (org-element-property :end elem-B)) | |
4319 | (skip-chars-backward " \r\t\n") | |
4320 | (point-at-eol))) | |
4321 | ;; Store overlays responsible for visibility status. We | |
4322 | ;; also need to store their boundaries as they will be | |
4323 | ;; removed from buffer. | |
4324 | (overlays | |
4325 | (cons | |
4326 | (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov))) | |
4327 | (overlays-in beg-A end-A)) | |
4328 | (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov))) | |
4329 | (overlays-in beg-B end-B)))) | |
4330 | ;; Get contents. | |
4331 | (body-A (buffer-substring beg-A end-A)) | |
4332 | (body-B (delete-and-extract-region beg-B end-B))) | |
4333 | (goto-char beg-B) | |
4334 | (when specialp | |
4335 | (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B)) | |
4336 | (org-indent-to-column ind-B)) | |
4337 | (insert body-A) | |
4338 | ;; Restore ex ELEM-A overlays. | |
4339 | (let ((offset (- beg-B beg-A))) | |
4340 | (mapc (lambda (ov) | |
4341 | (move-overlay | |
4342 | (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset))) | |
4343 | (car overlays)) | |
4344 | (goto-char beg-A) | |
4345 | (delete-region beg-A end-A) | |
4346 | (insert body-B) | |
4347 | ;; Restore ex ELEM-B overlays. | |
4348 | (mapc (lambda (ov) | |
4349 | (move-overlay | |
4350 | (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset))) | |
4351 | (cdr overlays))) | |
4352 | (goto-char (org-element-property :end elem-B))))) | |
4353 | ||
4354 | ||
4355 | (provide 'org-element) | |
4356 | ;;; org-element.el ends here |