org-element.el 169 KB

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