org-element.el 130 KB

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