org-element.el 170 KB

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