org-element.el 152 KB

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