org-element.el 213 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964
  1. ;;; org-element.el --- Parser And Applications for Org syntax
  2. ;; Copyright (C) 2012-2014 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', `section' and `table-row' types), it
  28. ;; can also accept a fixed set of keywords as attributes. Those are
  29. ;; called "affiliated keywords" to distinguish them from other
  30. ;; keywords, which are full-fledged elements. Almost all affiliated
  31. ;; keywords are referenced in `org-element-affiliated-keywords'; the
  32. ;; others are export attributes and start with "ATTR_" prefix.
  33. ;;
  34. ;; Element containing other elements (and only elements) are called
  35. ;; greater elements. Concerned types are: `center-block', `drawer',
  36. ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
  37. ;; `item', `plain-list', `property-drawer', `quote-block', `section'
  38. ;; and `special-block'.
  39. ;;
  40. ;; Other element types are: `babel-call', `clock', `comment',
  41. ;; `comment-block', `diary-sexp', `example-block', `export-block',
  42. ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
  43. ;; `node-property', `paragraph', `planning', `src-block', `table',
  44. ;; `table-row' and `verse-block'. Among them, `paragraph' and
  45. ;; `verse-block' types can contain Org objects and plain text.
  46. ;;
  47. ;; Objects are related to document's contents. Some of them are
  48. ;; recursive. Associated types are of the following: `bold', `code',
  49. ;; `entity', `export-snippet', `footnote-reference',
  50. ;; `inline-babel-call', `inline-src-block', `italic',
  51. ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
  52. ;; `statistics-cookie', `strike-through', `subscript', `superscript',
  53. ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
  54. ;;
  55. ;; Some elements also have special properties whose value can hold
  56. ;; objects themselves (e.g. an item tag or a headline name). Such
  57. ;; values are called "secondary strings". Any object belongs to
  58. ;; either an element or a secondary string.
  59. ;;
  60. ;; Notwithstanding affiliated keywords, each greater element, element
  61. ;; and object has a fixed set of properties attached to it. Among
  62. ;; them, four are shared by all types: `:begin' and `:end', which
  63. ;; refer to the beginning and ending buffer positions of the
  64. ;; considered element or object, `:post-blank', which holds the number
  65. ;; of blank lines, or white spaces, at its end and `:parent' which
  66. ;; refers to the element or object containing it. Greater elements,
  67. ;; elements and objects containing objects will also have
  68. ;; `:contents-begin' and `:contents-end' properties to delimit
  69. ;; contents. Eventually, greater elements and elements accepting
  70. ;; affiliated keywords will have a `:post-affiliated' property,
  71. ;; referring to the buffer position after all such keywords.
  72. ;;
  73. ;; At the lowest level, a `:parent' property is also attached to any
  74. ;; string, as a text property.
  75. ;;
  76. ;; Lisp-wise, an element or an object can be represented as a list.
  77. ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
  78. ;; TYPE is a symbol describing the Org element or object.
  79. ;; PROPERTIES is the property list attached to it. See docstring of
  80. ;; appropriate parsing function to get an exhaustive
  81. ;; list.
  82. ;; CONTENTS is a list of elements, objects or raw strings contained
  83. ;; in the current element or object, when applicable.
  84. ;;
  85. ;; An Org buffer is a nested list of such elements and objects, whose
  86. ;; type is `org-data' and properties is nil.
  87. ;;
  88. ;; The first part of this file defines Org syntax, while the second
  89. ;; one provide accessors and setters functions.
  90. ;;
  91. ;; The next part implements a parser and an interpreter for each
  92. ;; element and object type in Org syntax.
  93. ;;
  94. ;; The following part creates a fully recursive buffer parser. It
  95. ;; also provides a tool to map a function to elements or objects
  96. ;; matching some criteria in the parse tree. Functions of interest
  97. ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
  98. ;; extent, `org-element-parse-secondary-string'.
  99. ;;
  100. ;; The penultimate part is the cradle of an interpreter for the
  101. ;; obtained parse tree: `org-element-interpret-data'.
  102. ;;
  103. ;; The library ends by furnishing `org-element-at-point' function, and
  104. ;; a way to give information about document structure around point
  105. ;; with `org-element-context'. A cache mechanism is also provided for
  106. ;; these functions.
  107. ;;; Code:
  108. (eval-when-compile (require 'cl))
  109. (require 'org)
  110. (require 'avl-tree)
  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 and keywords. Note: this
  133. ;; is only an indication and need some thorough check.
  134. "#\\(?:[+ ]\\|$\\)" "\\|"
  135. ;; Drawers (any type) and fixed-width areas. This is also
  136. ;; only an indication.
  137. ":" "\\|"
  138. ;; Horizontal rules.
  139. "-\\{5,\\}[ \t]*$" "\\|"
  140. ;; LaTeX environments.
  141. "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
  142. ;; Planning and Clock lines.
  143. (regexp-opt (list org-scheduled-string
  144. org-deadline-string
  145. org-closed-string
  146. org-clock-string))
  147. "\\|"
  148. ;; Lists.
  149. (let ((term (case org-plain-list-ordered-item-terminator
  150. (?\) ")") (?. "\\.") (otherwise "[.)]")))
  151. (alpha (and org-list-allow-alphabetical "\\|[A-Za-z]")))
  152. (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
  153. "\\(?:[ \t]\\|$\\)"))
  154. "\\)\\)")
  155. "Regexp to separate paragraphs in an Org buffer.
  156. In the case of lines starting with \"#\" and \":\", this regexp
  157. is not sufficient to know if point is at a paragraph ending. See
  158. `org-element-paragraph-parser' for more information.")
  159. (defconst org-element-all-elements
  160. '(babel-call center-block clock comment comment-block diary-sexp drawer
  161. dynamic-block example-block export-block fixed-width
  162. footnote-definition headline horizontal-rule inlinetask item
  163. keyword latex-environment node-property paragraph plain-list
  164. planning property-drawer quote-block section
  165. special-block src-block table table-row verse-block)
  166. "Complete list of element types.")
  167. (defconst org-element-greater-elements
  168. '(center-block drawer dynamic-block footnote-definition headline inlinetask
  169. item plain-list property-drawer quote-block section
  170. special-block table)
  171. "List of recursive element types aka Greater Elements.")
  172. (defconst org-element-all-objects
  173. '(bold code entity export-snippet footnote-reference inline-babel-call
  174. inline-src-block italic line-break latex-fragment link macro
  175. radio-target statistics-cookie strike-through subscript superscript
  176. table-cell target timestamp underline verbatim)
  177. "Complete list of object types.")
  178. (defconst org-element-recursive-objects
  179. '(bold footnote-reference italic link subscript radio-target strike-through
  180. superscript table-cell underline)
  181. "List of recursive object types.")
  182. (defvar org-element-block-name-alist
  183. '(("CENTER" . org-element-center-block-parser)
  184. ("COMMENT" . org-element-comment-block-parser)
  185. ("EXAMPLE" . org-element-example-block-parser)
  186. ("QUOTE" . org-element-quote-block-parser)
  187. ("SRC" . org-element-src-block-parser)
  188. ("VERSE" . org-element-verse-block-parser))
  189. "Alist between block names and the associated parsing function.
  190. Names must be uppercase. Any block whose name has no association
  191. is parsed with `org-element-special-block-parser'.")
  192. (defconst org-element-link-type-is-file
  193. '("file" "file+emacs" "file+sys" "docview")
  194. "List of link types equivalent to \"file\".
  195. Only these types can accept search options and an explicit
  196. application to open them.")
  197. (defconst org-element-affiliated-keywords
  198. '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
  199. "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
  200. "List of affiliated keywords as strings.
  201. By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
  202. are affiliated keywords and need not to be in this list.")
  203. (defconst org-element-keyword-translation-alist
  204. '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
  205. ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
  206. ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
  207. "Alist of usual translations for keywords.
  208. The key is the old name and the value the new one. The property
  209. holding their value will be named after the translated name.")
  210. (defconst org-element-multiple-keywords '("CAPTION" "HEADER")
  211. "List of affiliated keywords that can occur more than once in an element.
  212. Their value will be consed into a list of strings, which will be
  213. returned as the value of the property.
  214. This list is checked after translations have been applied. See
  215. `org-element-keyword-translation-alist'.
  216. By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
  217. allow multiple occurrences and need not to be in this list.")
  218. (defconst org-element-parsed-keywords '("CAPTION")
  219. "List of affiliated keywords whose value can be parsed.
  220. Their value will be stored as a secondary string: a list of
  221. strings and objects.
  222. This list is checked after translations have been applied. See
  223. `org-element-keyword-translation-alist'.")
  224. (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
  225. "List of affiliated keywords which can have a secondary value.
  226. In Org syntax, they can be written with optional square brackets
  227. before the colons. For example, RESULTS keyword can be
  228. associated to a hash value with the following:
  229. #+RESULTS[hash-string]: some-source
  230. This list is checked after translations have been applied. See
  231. `org-element-keyword-translation-alist'.")
  232. (defconst org-element-document-properties '("AUTHOR" "DATE" "TITLE")
  233. "List of properties associated to the whole document.
  234. Any keyword in this list will have its value parsed and stored as
  235. a secondary string.")
  236. (defconst org-element--affiliated-re
  237. (format "[ \t]*#\\+\\(?:%s\\):\\(?: \\|$\\)"
  238. (concat
  239. ;; Dual affiliated keywords.
  240. (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
  241. (regexp-opt org-element-dual-keywords))
  242. "\\|"
  243. ;; Regular affiliated keywords.
  244. (format "\\(?1:%s\\)"
  245. (regexp-opt
  246. (org-remove-if
  247. #'(lambda (keyword)
  248. (member keyword org-element-dual-keywords))
  249. org-element-affiliated-keywords)))
  250. "\\|"
  251. ;; Export attributes.
  252. "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
  253. "Regexp matching any affiliated keyword.
  254. Keyword name is put in match group 1. Moreover, if keyword
  255. belongs to `org-element-dual-keywords', put the dual value in
  256. match group 2.
  257. Don't modify it, set `org-element-affiliated-keywords' instead.")
  258. (defconst org-element-object-restrictions
  259. (let* ((standard-set (remq 'table-cell org-element-all-objects))
  260. (standard-set-no-line-break (remq 'line-break standard-set)))
  261. `((bold ,@standard-set)
  262. (footnote-reference ,@standard-set)
  263. (headline ,@standard-set-no-line-break)
  264. (inlinetask ,@standard-set-no-line-break)
  265. (italic ,@standard-set)
  266. (item ,@standard-set-no-line-break)
  267. (keyword ,@standard-set)
  268. ;; Ignore all links excepted plain links in a link description.
  269. ;; Also ignore radio-targets and line breaks.
  270. (link bold code entity export-snippet inline-babel-call inline-src-block
  271. italic latex-fragment macro plain-link statistics-cookie
  272. strike-through subscript superscript underline verbatim)
  273. (paragraph ,@standard-set)
  274. ;; Remove any variable object from radio target as it would
  275. ;; prevent it from being properly recognized.
  276. (radio-target bold code entity italic latex-fragment strike-through
  277. subscript superscript underline superscript)
  278. (strike-through ,@standard-set)
  279. (subscript ,@standard-set)
  280. (superscript ,@standard-set)
  281. ;; Ignore inline babel call and inline src block as formulas are
  282. ;; possible. Also ignore line breaks and statistics cookies.
  283. (table-cell bold code entity export-snippet footnote-reference italic
  284. latex-fragment link macro radio-target strike-through
  285. subscript superscript target timestamp underline verbatim)
  286. (table-row table-cell)
  287. (underline ,@standard-set)
  288. (verse-block ,@standard-set)))
  289. "Alist of objects restrictions.
  290. key is an element or object type containing objects and value is
  291. a list of types that can be contained within an element or object
  292. of such type.
  293. For example, in a `radio-target' object, one can only find
  294. entities, latex-fragments, subscript, superscript and text
  295. markup.
  296. This alist also applies to secondary string. For example, an
  297. `headline' type element doesn't directly contain objects, but
  298. still has an entry since one of its properties (`:title') does.")
  299. (defconst org-element-secondary-value-alist
  300. '((headline . :title)
  301. (inlinetask . :title)
  302. (item . :tag))
  303. "Alist between element types and location of secondary value.")
  304. ;;; Accessors and Setters
  305. ;;
  306. ;; Provide four accessors: `org-element-type', `org-element-property'
  307. ;; `org-element-contents' and `org-element-restriction'.
  308. ;;
  309. ;; Setter functions allow to modify elements by side effect. There is
  310. ;; `org-element-put-property', `org-element-set-contents'. These
  311. ;; low-level functions are useful to build a parse tree.
  312. ;;
  313. ;; `org-element-adopt-element', `org-element-set-element',
  314. ;; `org-element-extract-element' and `org-element-insert-before' are
  315. ;; high-level functions useful to modify a parse tree.
  316. ;;
  317. ;; `org-element-secondary-p' is a predicate used to know if a given
  318. ;; object belongs to a secondary string.
  319. (defsubst org-element-type (element)
  320. "Return type of ELEMENT.
  321. The function returns the type of the element or object provided.
  322. It can also return the following special value:
  323. `plain-text' for a string
  324. `org-data' for a complete document
  325. nil in any other case."
  326. (cond
  327. ((not (consp element)) (and (stringp element) 'plain-text))
  328. ((symbolp (car element)) (car element))))
  329. (defsubst org-element-property (property element)
  330. "Extract the value from the PROPERTY of an ELEMENT."
  331. (if (stringp element) (get-text-property 0 property element)
  332. (plist-get (nth 1 element) property)))
  333. (defsubst org-element-contents (element)
  334. "Extract contents from an ELEMENT."
  335. (cond ((not (consp element)) nil)
  336. ((symbolp (car element)) (nthcdr 2 element))
  337. (t element)))
  338. (defsubst org-element-restriction (element)
  339. "Return restriction associated to ELEMENT.
  340. ELEMENT can be an element, an object or a symbol representing an
  341. element or object type."
  342. (cdr (assq (if (symbolp element) element (org-element-type element))
  343. org-element-object-restrictions)))
  344. (defsubst org-element-put-property (element property value)
  345. "In ELEMENT set PROPERTY to VALUE.
  346. Return modified element."
  347. (if (stringp element) (org-add-props element nil property value)
  348. (setcar (cdr element) (plist-put (nth 1 element) property value))
  349. element))
  350. (defsubst org-element-set-contents (element &rest contents)
  351. "Set ELEMENT contents to CONTENTS.
  352. Return modified element."
  353. (cond ((not element) (list contents))
  354. ((not (symbolp (car element))) contents)
  355. ((cdr element) (setcdr (cdr element) contents))
  356. (t (nconc element contents))))
  357. (defun org-element-secondary-p (object)
  358. "Non-nil when OBJECT belongs to a secondary string.
  359. Return value is the property name, as a keyword, or nil."
  360. (let* ((parent (org-element-property :parent object))
  361. (property (cdr (assq (org-element-type parent)
  362. org-element-secondary-value-alist))))
  363. (and property
  364. (memq object (org-element-property property parent))
  365. property)))
  366. (defsubst org-element-adopt-elements (parent &rest children)
  367. "Append elements to the contents of another element.
  368. PARENT is an element or object. CHILDREN can be elements,
  369. objects, or a strings.
  370. The function takes care of setting `:parent' property for CHILD.
  371. Return parent element."
  372. ;; Link every child to PARENT. If PARENT is nil, it is a secondary
  373. ;; string: parent is the list itself.
  374. (mapc (lambda (child)
  375. (org-element-put-property child :parent (or parent children)))
  376. children)
  377. ;; Add CHILDREN at the end of PARENT contents.
  378. (when parent
  379. (apply 'org-element-set-contents
  380. parent
  381. (nconc (org-element-contents parent) children)))
  382. ;; Return modified PARENT element.
  383. (or parent children))
  384. (defun org-element-extract-element (element)
  385. "Extract ELEMENT from parse tree.
  386. Remove element from the parse tree by side-effect, and return it
  387. with its `:parent' property stripped out."
  388. (let ((parent (org-element-property :parent element))
  389. (secondary (org-element-secondary-p element)))
  390. (if secondary
  391. (org-element-put-property
  392. parent secondary
  393. (delq element (org-element-property secondary parent)))
  394. (apply #'org-element-set-contents
  395. parent
  396. (delq element (org-element-contents parent))))
  397. ;; Return ELEMENT with its :parent removed.
  398. (org-element-put-property element :parent nil)))
  399. (defun org-element-insert-before (element location)
  400. "Insert ELEMENT before LOCATION in parse tree.
  401. LOCATION is an element, object or string within the parse tree.
  402. Parse tree is modified by side effect."
  403. (let* ((parent (org-element-property :parent location))
  404. (property (org-element-secondary-p location))
  405. (siblings (if property (org-element-property property parent)
  406. (org-element-contents parent)))
  407. ;; Special case: LOCATION is the first element of an
  408. ;; independent secondary string (e.g. :title property). Add
  409. ;; ELEMENT in-place.
  410. (specialp (and (not property)
  411. (eq siblings parent)
  412. (eq (car parent) location))))
  413. ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
  414. (cond (specialp)
  415. ((or (null siblings) (eq (car siblings) location))
  416. (push element siblings))
  417. ((null location) (nconc siblings (list element)))
  418. (t (let ((previous (cadr (memq location (reverse siblings)))))
  419. (if (not previous)
  420. (error "No location found to insert element")
  421. (let ((next (memq previous siblings)))
  422. (setcdr next (cons element (cdr next))))))))
  423. ;; Store SIBLINGS at appropriate place in parse tree.
  424. (cond
  425. (specialp (setcdr parent (copy-sequence parent)) (setcar parent element))
  426. (property (org-element-put-property parent property siblings))
  427. (t (apply #'org-element-set-contents parent siblings)))
  428. ;; Set appropriate :parent property.
  429. (org-element-put-property element :parent parent)))
  430. (defun org-element-set-element (old new)
  431. "Replace element or object OLD with element or object NEW.
  432. The function takes care of setting `:parent' property for NEW."
  433. ;; Ensure OLD and NEW have the same parent.
  434. (org-element-put-property new :parent (org-element-property :parent old))
  435. (if (or (memq (org-element-type old) '(plain-text nil))
  436. (memq (org-element-type new) '(plain-text nil)))
  437. ;; We cannot replace OLD with NEW since one of them is not an
  438. ;; object or element. We take the long path.
  439. (progn (org-element-insert-before new old)
  440. (org-element-extract-element old))
  441. ;; Since OLD is going to be changed into NEW by side-effect, first
  442. ;; make sure that every element or object within NEW has OLD as
  443. ;; parent.
  444. (dolist (blob (org-element-contents new))
  445. (org-element-put-property blob :parent old))
  446. ;; Transfer contents.
  447. (apply #'org-element-set-contents old (org-element-contents new))
  448. ;; Overwrite OLD's properties with NEW's.
  449. (setcar (cdr old) (nth 1 new))
  450. ;; Transfer type.
  451. (setcar old (car new))))
  452. ;;; Greater elements
  453. ;;
  454. ;; For each greater element type, we define a parser and an
  455. ;; interpreter.
  456. ;;
  457. ;; A parser returns the element or object as the list described above.
  458. ;; Most of them accepts no argument. Though, exceptions exist. Hence
  459. ;; every element containing a secondary string (see
  460. ;; `org-element-secondary-value-alist') will accept an optional
  461. ;; argument to toggle parsing of that secondary string. Moreover,
  462. ;; `item' parser requires current list's structure as its first
  463. ;; element.
  464. ;;
  465. ;; An interpreter accepts two arguments: the list representation of
  466. ;; the element or object, and its contents. The latter may be nil,
  467. ;; depending on the element or object considered. It returns the
  468. ;; appropriate Org syntax, as a string.
  469. ;;
  470. ;; Parsing functions must follow the naming convention:
  471. ;; org-element-TYPE-parser, where TYPE is greater element's type, as
  472. ;; defined in `org-element-greater-elements'.
  473. ;;
  474. ;; Similarly, interpreting functions must follow the naming
  475. ;; convention: org-element-TYPE-interpreter.
  476. ;;
  477. ;; With the exception of `headline' and `item' types, greater elements
  478. ;; cannot contain other greater elements of their own type.
  479. ;;
  480. ;; Beside implementing a parser and an interpreter, adding a new
  481. ;; greater element requires to tweak `org-element--current-element'.
  482. ;; Moreover, the newly defined type must be added to both
  483. ;; `org-element-all-elements' and `org-element-greater-elements'.
  484. ;;;; Center Block
  485. (defun org-element-center-block-parser (limit affiliated)
  486. "Parse a center block.
  487. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  488. the buffer position at the beginning of the first affiliated
  489. keyword and CDR is a plist of affiliated keywords along with
  490. their value.
  491. Return a list whose CAR is `center-block' and CDR is a plist
  492. containing `:begin', `:end', `:contents-begin', `:contents-end',
  493. `:post-blank' and `:post-affiliated' keywords.
  494. Assume point is at the beginning of the block."
  495. (let ((case-fold-search t))
  496. (if (not (save-excursion
  497. (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
  498. ;; Incomplete block: parse it as a paragraph.
  499. (org-element-paragraph-parser limit affiliated)
  500. (let ((block-end-line (match-beginning 0)))
  501. (let* ((begin (car affiliated))
  502. (post-affiliated (point))
  503. ;; Empty blocks have no contents.
  504. (contents-begin (progn (forward-line)
  505. (and (< (point) block-end-line)
  506. (point))))
  507. (contents-end (and contents-begin block-end-line))
  508. (pos-before-blank (progn (goto-char block-end-line)
  509. (forward-line)
  510. (point)))
  511. (end (save-excursion
  512. (skip-chars-forward " \r\t\n" limit)
  513. (if (eobp) (point) (line-beginning-position)))))
  514. (list 'center-block
  515. (nconc
  516. (list :begin begin
  517. :end end
  518. :contents-begin contents-begin
  519. :contents-end contents-end
  520. :post-blank (count-lines pos-before-blank end)
  521. :post-affiliated post-affiliated)
  522. (cdr affiliated))))))))
  523. (defun org-element-center-block-interpreter (center-block contents)
  524. "Interpret CENTER-BLOCK element as Org syntax.
  525. CONTENTS is the contents of the element."
  526. (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
  527. ;;;; Drawer
  528. (defun org-element-drawer-parser (limit affiliated)
  529. "Parse a drawer.
  530. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  531. the buffer position at the beginning of the first affiliated
  532. keyword and CDR is a plist of affiliated keywords along with
  533. their value.
  534. Return a list whose CAR is `drawer' and CDR is a plist containing
  535. `:drawer-name', `:begin', `:end', `:contents-begin',
  536. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  537. Assume point is at beginning of drawer."
  538. (let ((case-fold-search t))
  539. (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
  540. ;; Incomplete drawer: parse it as a paragraph.
  541. (org-element-paragraph-parser limit affiliated)
  542. (save-excursion
  543. (let* ((drawer-end-line (match-beginning 0))
  544. (name (progn (looking-at org-drawer-regexp)
  545. (org-match-string-no-properties 1)))
  546. (begin (car affiliated))
  547. (post-affiliated (point))
  548. ;; Empty drawers have no contents.
  549. (contents-begin (progn (forward-line)
  550. (and (< (point) drawer-end-line)
  551. (point))))
  552. (contents-end (and contents-begin drawer-end-line))
  553. (pos-before-blank (progn (goto-char drawer-end-line)
  554. (forward-line)
  555. (point)))
  556. (end (progn (skip-chars-forward " \r\t\n" limit)
  557. (if (eobp) (point) (line-beginning-position)))))
  558. (list 'drawer
  559. (nconc
  560. (list :begin begin
  561. :end end
  562. :drawer-name name
  563. :contents-begin contents-begin
  564. :contents-end contents-end
  565. :post-blank (count-lines pos-before-blank end)
  566. :post-affiliated post-affiliated)
  567. (cdr affiliated))))))))
  568. (defun org-element-drawer-interpreter (drawer contents)
  569. "Interpret DRAWER element as Org syntax.
  570. CONTENTS is the contents of the element."
  571. (format ":%s:\n%s:END:"
  572. (org-element-property :drawer-name drawer)
  573. contents))
  574. ;;;; Dynamic Block
  575. (defun org-element-dynamic-block-parser (limit affiliated)
  576. "Parse a dynamic block.
  577. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  578. the buffer position at the beginning of the first affiliated
  579. keyword and CDR is a plist of affiliated keywords along with
  580. their value.
  581. Return a list whose CAR is `dynamic-block' and CDR is a plist
  582. containing `:block-name', `:begin', `:end', `:contents-begin',
  583. `:contents-end', `:arguments', `:post-blank' and
  584. `:post-affiliated' keywords.
  585. Assume point is at beginning of dynamic block."
  586. (let ((case-fold-search t))
  587. (if (not (save-excursion
  588. (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
  589. ;; Incomplete block: parse it as a paragraph.
  590. (org-element-paragraph-parser limit affiliated)
  591. (let ((block-end-line (match-beginning 0)))
  592. (save-excursion
  593. (let* ((name (progn (looking-at org-dblock-start-re)
  594. (org-match-string-no-properties 1)))
  595. (arguments (org-match-string-no-properties 3))
  596. (begin (car affiliated))
  597. (post-affiliated (point))
  598. ;; Empty blocks have no contents.
  599. (contents-begin (progn (forward-line)
  600. (and (< (point) block-end-line)
  601. (point))))
  602. (contents-end (and contents-begin block-end-line))
  603. (pos-before-blank (progn (goto-char block-end-line)
  604. (forward-line)
  605. (point)))
  606. (end (progn (skip-chars-forward " \r\t\n" limit)
  607. (if (eobp) (point) (line-beginning-position)))))
  608. (list 'dynamic-block
  609. (nconc
  610. (list :begin begin
  611. :end end
  612. :block-name name
  613. :arguments arguments
  614. :contents-begin contents-begin
  615. :contents-end contents-end
  616. :post-blank (count-lines pos-before-blank end)
  617. :post-affiliated post-affiliated)
  618. (cdr affiliated)))))))))
  619. (defun org-element-dynamic-block-interpreter (dynamic-block contents)
  620. "Interpret DYNAMIC-BLOCK element as Org syntax.
  621. CONTENTS is the contents of the element."
  622. (format "#+BEGIN: %s%s\n%s#+END:"
  623. (org-element-property :block-name dynamic-block)
  624. (let ((args (org-element-property :arguments dynamic-block)))
  625. (and args (concat " " args)))
  626. contents))
  627. ;;;; Footnote Definition
  628. (defun org-element-footnote-definition-parser (limit affiliated)
  629. "Parse a footnote definition.
  630. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  631. the buffer position at the beginning of the first affiliated
  632. keyword and CDR is a plist of affiliated keywords along with
  633. their value.
  634. Return a list whose CAR is `footnote-definition' and CDR is
  635. a plist containing `:label', `:begin' `:end', `:contents-begin',
  636. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  637. Assume point is at the beginning of the footnote definition."
  638. (save-excursion
  639. (let* ((label (progn (looking-at org-footnote-definition-re)
  640. (org-match-string-no-properties 1)))
  641. (begin (car affiliated))
  642. (post-affiliated (point))
  643. (ending (save-excursion
  644. (if (progn
  645. (end-of-line)
  646. (re-search-forward
  647. (concat org-outline-regexp-bol "\\|"
  648. org-footnote-definition-re "\\|"
  649. "^\\([ \t]*\n\\)\\{2,\\}") limit 'move))
  650. (match-beginning 0)
  651. (point))))
  652. (contents-begin (progn
  653. (search-forward "]")
  654. (skip-chars-forward " \r\t\n" ending)
  655. (cond ((= (point) ending) nil)
  656. ((= (line-beginning-position) begin) (point))
  657. (t (line-beginning-position)))))
  658. (contents-end (and contents-begin ending))
  659. (end (progn (goto-char ending)
  660. (skip-chars-forward " \r\t\n" limit)
  661. (if (eobp) (point) (line-beginning-position)))))
  662. (list 'footnote-definition
  663. (nconc
  664. (list :label label
  665. :begin begin
  666. :end end
  667. :contents-begin contents-begin
  668. :contents-end contents-end
  669. :post-blank (count-lines ending end)
  670. :post-affiliated post-affiliated)
  671. (cdr affiliated))))))
  672. (defun org-element-footnote-definition-interpreter (footnote-definition contents)
  673. "Interpret FOOTNOTE-DEFINITION element as Org syntax.
  674. CONTENTS is the contents of the footnote-definition."
  675. (concat (format "[%s]" (org-element-property :label footnote-definition))
  676. " "
  677. contents))
  678. ;;;; Headline
  679. (defun org-element-headline-parser (limit &optional raw-secondary-p)
  680. "Parse a headline.
  681. Return a list whose CAR is `headline' and CDR is a plist
  682. containing `:raw-value', `:title', `:alt-title', `:begin',
  683. `:end', `:pre-blank', `:contents-begin' and `:contents-end',
  684. `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
  685. `:scheduled', `:deadline', `:closed', `:archivedp', `:commentedp'
  686. and `:footnote-section-p' keywords.
  687. The plist also contains any property set in the property drawer,
  688. with its name in upper cases and colons added at the
  689. beginning (e.g., `:CUSTOM_ID').
  690. LIMIT is a buffer position bounding the search.
  691. When RAW-SECONDARY-P is non-nil, headline's title will not be
  692. parsed as a secondary string, but as a plain string instead.
  693. Assume point is at beginning of the headline."
  694. (save-excursion
  695. (let* ((components (org-heading-components))
  696. (level (nth 1 components))
  697. (todo (nth 2 components))
  698. (todo-type
  699. (and todo (if (member todo org-done-keywords) 'done 'todo)))
  700. (tags (let ((raw-tags (nth 5 components)))
  701. (and raw-tags (org-split-string raw-tags ":"))))
  702. (raw-value (or (nth 4 components) ""))
  703. (commentedp
  704. (let ((case-fold-search nil))
  705. (string-match (format "^%s\\( \\|$\\)" org-comment-string)
  706. raw-value)))
  707. (archivedp (member org-archive-tag tags))
  708. (footnote-section-p (and org-footnote-section
  709. (string= org-footnote-section raw-value)))
  710. (standard-props
  711. ;; Find property drawer associated to current headline and
  712. ;; extract properties.
  713. ;;
  714. ;; Upcase property names. It avoids confusion between
  715. ;; properties obtained through property drawer and default
  716. ;; properties from the parser (e.g. `:end' and :END:)
  717. (let ((end (save-excursion
  718. (org-with-limited-levels (outline-next-heading))
  719. (point)))
  720. plist)
  721. (save-excursion
  722. (while (and (null plist)
  723. (re-search-forward org-property-start-re end t))
  724. (let ((drawer (org-element-at-point)))
  725. (when (and (eq (org-element-type drawer) 'property-drawer)
  726. ;; Make sure drawer is not associated
  727. ;; to an inlinetask.
  728. (let ((p drawer))
  729. (while (and (setq p (org-element-property
  730. :parent p))
  731. (not (eq (org-element-type p)
  732. 'inlinetask))))
  733. (not p)))
  734. (let ((end (org-element-property :contents-end drawer)))
  735. (when end
  736. (forward-line)
  737. (while (< (point) end)
  738. (when (looking-at org-property-re)
  739. (setq plist
  740. (plist-put
  741. plist
  742. (intern
  743. (concat ":" (upcase (match-string 2))))
  744. (org-match-string-no-properties 3))))
  745. (forward-line)))))))
  746. plist)))
  747. (time-props
  748. ;; Read time properties on the line below the headline.
  749. (save-excursion
  750. (forward-line)
  751. (when (looking-at org-planning-or-clock-line-re)
  752. (let ((end (line-end-position)) plist)
  753. (while (re-search-forward
  754. org-keyword-time-not-clock-regexp end t)
  755. (goto-char (match-end 1))
  756. (skip-chars-forward " \t")
  757. (let ((keyword (match-string 1))
  758. (time (org-element-timestamp-parser)))
  759. (cond ((equal keyword org-scheduled-string)
  760. (setq plist (plist-put plist :scheduled time)))
  761. ((equal keyword org-deadline-string)
  762. (setq plist (plist-put plist :deadline time)))
  763. (t (setq plist (plist-put plist :closed time))))))
  764. plist))))
  765. (begin (point))
  766. (end (min (save-excursion (org-end-of-subtree t t)) limit))
  767. (pos-after-head (progn (forward-line) (point)))
  768. (contents-begin (save-excursion
  769. (skip-chars-forward " \r\t\n" end)
  770. (and (/= (point) end) (line-beginning-position))))
  771. (contents-end (and contents-begin
  772. (progn (goto-char end)
  773. (skip-chars-backward " \r\t\n")
  774. (forward-line)
  775. (point)))))
  776. ;; Clean RAW-VALUE from any comment string.
  777. (when commentedp
  778. (let ((case-fold-search nil))
  779. (setq raw-value
  780. (replace-regexp-in-string
  781. (concat (regexp-quote org-comment-string) "\\(?: \\|$\\)")
  782. ""
  783. raw-value))))
  784. ;; Clean TAGS from archive tag, if any.
  785. (when archivedp (setq tags (delete org-archive-tag tags)))
  786. (let ((headline
  787. (list 'headline
  788. (nconc
  789. (list :raw-value raw-value
  790. :begin begin
  791. :end end
  792. :pre-blank
  793. (if (not contents-begin) 0
  794. (count-lines pos-after-head contents-begin))
  795. :contents-begin contents-begin
  796. :contents-end contents-end
  797. :level level
  798. :priority (nth 3 components)
  799. :tags tags
  800. :todo-keyword todo
  801. :todo-type todo-type
  802. :post-blank (count-lines
  803. (or contents-end pos-after-head)
  804. end)
  805. :footnote-section-p footnote-section-p
  806. :archivedp archivedp
  807. :commentedp commentedp)
  808. time-props
  809. standard-props))))
  810. (let ((alt-title (org-element-property :ALT_TITLE headline)))
  811. (when alt-title
  812. (org-element-put-property
  813. headline :alt-title
  814. (if raw-secondary-p alt-title
  815. (org-element-parse-secondary-string
  816. alt-title (org-element-restriction 'headline) headline)))))
  817. (org-element-put-property
  818. headline :title
  819. (if raw-secondary-p raw-value
  820. (org-element-parse-secondary-string
  821. raw-value (org-element-restriction 'headline) headline)))))))
  822. (defun org-element-headline-interpreter (headline contents)
  823. "Interpret HEADLINE element as Org syntax.
  824. CONTENTS is the contents of the element."
  825. (let* ((level (org-element-property :level headline))
  826. (todo (org-element-property :todo-keyword headline))
  827. (priority (org-element-property :priority headline))
  828. (title (org-element-interpret-data
  829. (org-element-property :title headline)))
  830. (tags (let ((tag-list (if (org-element-property :archivedp headline)
  831. (cons org-archive-tag
  832. (org-element-property :tags headline))
  833. (org-element-property :tags headline))))
  834. (and tag-list
  835. (format ":%s:" (mapconcat 'identity tag-list ":")))))
  836. (commentedp (org-element-property :commentedp headline))
  837. (pre-blank (or (org-element-property :pre-blank headline) 0))
  838. (heading (concat (make-string (org-reduced-level level) ?*)
  839. (and todo (concat " " todo))
  840. (and commentedp (concat " " org-comment-string))
  841. (and priority
  842. (format " [#%s]" (char-to-string priority)))
  843. (cond ((and org-footnote-section
  844. (org-element-property
  845. :footnote-section-p headline))
  846. (concat " " org-footnote-section))
  847. (title (concat " " title))))))
  848. (concat heading
  849. ;; Align tags.
  850. (when tags
  851. (cond
  852. ((zerop org-tags-column) (format " %s" tags))
  853. ((< org-tags-column 0)
  854. (concat
  855. (make-string
  856. (max (- (+ org-tags-column (length heading) (length tags))) 1)
  857. ? )
  858. tags))
  859. (t
  860. (concat
  861. (make-string (max (- org-tags-column (length heading)) 1) ? )
  862. tags))))
  863. (make-string (1+ pre-blank) 10)
  864. contents)))
  865. ;;;; Inlinetask
  866. (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
  867. "Parse an inline task.
  868. Return a list whose CAR is `inlinetask' and CDR is a plist
  869. containing `:title', `:begin', `:end', `:contents-begin' and
  870. `:contents-end', `:level', `:priority', `:raw-value', `:tags',
  871. `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
  872. `:closed' and `:post-blank' keywords.
  873. The plist also contains any property set in the property drawer,
  874. with its name in upper cases and colons added at the
  875. beginning (e.g., `:CUSTOM_ID').
  876. When optional argument RAW-SECONDARY-P is non-nil, inline-task's
  877. title will not be parsed as a secondary string, but as a plain
  878. string instead.
  879. Assume point is at beginning of the inline task."
  880. (save-excursion
  881. (let* ((begin (point))
  882. (components (org-heading-components))
  883. (todo (nth 2 components))
  884. (todo-type (and todo
  885. (if (member todo org-done-keywords) 'done 'todo)))
  886. (tags (let ((raw-tags (nth 5 components)))
  887. (and raw-tags (org-split-string raw-tags ":"))))
  888. (raw-value (or (nth 4 components) ""))
  889. (task-end (save-excursion
  890. (end-of-line)
  891. (and (re-search-forward org-outline-regexp-bol limit t)
  892. (org-looking-at-p "END[ \t]*$")
  893. (line-beginning-position))))
  894. (time-props
  895. ;; Read time properties on the line below the inlinetask
  896. ;; opening string.
  897. (when task-end
  898. (save-excursion
  899. (when (progn (forward-line)
  900. (looking-at org-planning-or-clock-line-re))
  901. (let ((end (line-end-position)) plist)
  902. (while (re-search-forward
  903. org-keyword-time-not-clock-regexp end t)
  904. (goto-char (match-end 1))
  905. (skip-chars-forward " \t")
  906. (let ((keyword (match-string 1))
  907. (time (org-element-timestamp-parser)))
  908. (cond ((equal keyword org-scheduled-string)
  909. (setq plist (plist-put plist :scheduled time)))
  910. ((equal keyword org-deadline-string)
  911. (setq plist (plist-put plist :deadline time)))
  912. (t (setq plist (plist-put plist :closed time))))))
  913. plist)))))
  914. (contents-begin (progn (forward-line)
  915. (and task-end (< (point) task-end) (point))))
  916. (contents-end (and contents-begin task-end))
  917. (before-blank (if (not task-end) (point)
  918. (goto-char task-end)
  919. (forward-line)
  920. (point)))
  921. (end (progn (skip-chars-forward " \r\t\n" limit)
  922. (if (eobp) (point) (line-beginning-position))))
  923. (standard-props
  924. ;; Find property drawer associated to current inlinetask
  925. ;; and extract properties.
  926. ;;
  927. ;; HACK: Calling `org-element-at-point' triggers a parsing
  928. ;; of this inlinetask and, thus, an infloop. To avoid the
  929. ;; problem, we extract contents of the inlinetask and
  930. ;; parse them in a new buffer.
  931. ;;
  932. ;; Upcase property names. It avoids confusion between
  933. ;; properties obtained through property drawer and default
  934. ;; properties from the parser (e.g. `:end' and :END:)
  935. (when contents-begin
  936. (let ((contents (buffer-substring contents-begin contents-end))
  937. plist)
  938. (with-temp-buffer
  939. (let ((org-inhibit-startup t)) (org-mode))
  940. (insert contents)
  941. (goto-char (point-min))
  942. (while (and (null plist)
  943. (re-search-forward
  944. org-property-start-re task-end t))
  945. (let ((d (org-element-at-point)))
  946. (when (eq (org-element-type d) 'property-drawer)
  947. (let ((end (org-element-property :contents-end d)))
  948. (when end
  949. (forward-line)
  950. (while (< (point) end)
  951. (when (looking-at org-property-re)
  952. (setq plist
  953. (plist-put
  954. plist
  955. (intern
  956. (concat ":" (upcase (match-string 2))))
  957. (org-match-string-no-properties 3))))
  958. (forward-line))))))))
  959. plist)))
  960. (inlinetask
  961. (list 'inlinetask
  962. (nconc
  963. (list :raw-value raw-value
  964. :begin begin
  965. :end end
  966. :contents-begin contents-begin
  967. :contents-end contents-end
  968. :level (nth 1 components)
  969. :priority (nth 3 components)
  970. :tags tags
  971. :todo-keyword todo
  972. :todo-type todo-type
  973. :post-blank (count-lines before-blank end))
  974. time-props
  975. standard-props))))
  976. (org-element-put-property
  977. inlinetask :title
  978. (if raw-secondary-p raw-value
  979. (org-element-parse-secondary-string
  980. raw-value
  981. (org-element-restriction 'inlinetask)
  982. inlinetask))))))
  983. (defun org-element-inlinetask-interpreter (inlinetask contents)
  984. "Interpret INLINETASK element as Org syntax.
  985. CONTENTS is the contents of inlinetask."
  986. (let* ((level (org-element-property :level inlinetask))
  987. (todo (org-element-property :todo-keyword inlinetask))
  988. (priority (org-element-property :priority inlinetask))
  989. (title (org-element-interpret-data
  990. (org-element-property :title inlinetask)))
  991. (tags (let ((tag-list (org-element-property :tags inlinetask)))
  992. (and tag-list
  993. (format ":%s:" (mapconcat 'identity tag-list ":")))))
  994. (task (concat (make-string level ?*)
  995. (and todo (concat " " todo))
  996. (and priority
  997. (format " [#%s]" (char-to-string priority)))
  998. (and title (concat " " title)))))
  999. (concat task
  1000. ;; Align tags.
  1001. (when tags
  1002. (cond
  1003. ((zerop org-tags-column) (format " %s" tags))
  1004. ((< org-tags-column 0)
  1005. (concat
  1006. (make-string
  1007. (max (- (+ org-tags-column (length task) (length tags))) 1)
  1008. ? )
  1009. tags))
  1010. (t
  1011. (concat
  1012. (make-string (max (- org-tags-column (length task)) 1) ? )
  1013. tags))))
  1014. ;; Prefer degenerate inlinetasks when there are no
  1015. ;; contents.
  1016. (when contents
  1017. (concat "\n"
  1018. contents
  1019. (make-string level ?*) " END")))))
  1020. ;;;; Item
  1021. (defun org-element-item-parser (limit struct &optional raw-secondary-p)
  1022. "Parse an item.
  1023. STRUCT is the structure of the plain list.
  1024. Return a list whose CAR is `item' and CDR is a plist containing
  1025. `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
  1026. `:checkbox', `:counter', `:tag', `:structure' and `:post-blank'
  1027. keywords.
  1028. When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
  1029. any, will not be parsed as a secondary string, but as a plain
  1030. string instead.
  1031. Assume point is at the beginning of the item."
  1032. (save-excursion
  1033. (beginning-of-line)
  1034. (looking-at org-list-full-item-re)
  1035. (let* ((begin (point))
  1036. (bullet (org-match-string-no-properties 1))
  1037. (checkbox (let ((box (org-match-string-no-properties 3)))
  1038. (cond ((equal "[ ]" box) 'off)
  1039. ((equal "[X]" box) 'on)
  1040. ((equal "[-]" box) 'trans))))
  1041. (counter (let ((c (org-match-string-no-properties 2)))
  1042. (save-match-data
  1043. (cond
  1044. ((not c) nil)
  1045. ((string-match "[A-Za-z]" c)
  1046. (- (string-to-char (upcase (match-string 0 c)))
  1047. 64))
  1048. ((string-match "[0-9]+" c)
  1049. (string-to-number (match-string 0 c)))))))
  1050. (end (progn (goto-char (nth 6 (assq (point) struct)))
  1051. (unless (bolp) (forward-line))
  1052. (point)))
  1053. (contents-begin
  1054. (progn (goto-char
  1055. ;; Ignore tags in un-ordered lists: they are just
  1056. ;; a part of item's body.
  1057. (if (and (match-beginning 4)
  1058. (save-match-data (string-match "[.)]" bullet)))
  1059. (match-beginning 4)
  1060. (match-end 0)))
  1061. (skip-chars-forward " \r\t\n" limit)
  1062. ;; If first line isn't empty, contents really start
  1063. ;; at the text after item's meta-data.
  1064. (if (= (point-at-bol) begin) (point) (point-at-bol))))
  1065. (contents-end (progn (goto-char end)
  1066. (skip-chars-backward " \r\t\n")
  1067. (forward-line)
  1068. (point)))
  1069. (item
  1070. (list 'item
  1071. (list :bullet bullet
  1072. :begin begin
  1073. :end end
  1074. ;; CONTENTS-BEGIN and CONTENTS-END may be
  1075. ;; mixed up in the case of an empty item
  1076. ;; separated from the next by a blank line.
  1077. ;; Thus ensure the former is always the
  1078. ;; smallest.
  1079. :contents-begin (min contents-begin contents-end)
  1080. :contents-end (max contents-begin contents-end)
  1081. :checkbox checkbox
  1082. :counter counter
  1083. :structure struct
  1084. :post-blank (count-lines contents-end end)))))
  1085. (org-element-put-property
  1086. item :tag
  1087. (let ((raw-tag (org-list-get-tag begin struct)))
  1088. (and raw-tag
  1089. (if raw-secondary-p raw-tag
  1090. (org-element-parse-secondary-string
  1091. raw-tag (org-element-restriction 'item) item))))))))
  1092. (defun org-element-item-interpreter (item contents)
  1093. "Interpret ITEM element as Org syntax.
  1094. CONTENTS is the contents of the element."
  1095. (let* ((bullet (let ((bullet (org-element-property :bullet item)))
  1096. (org-list-bullet-string
  1097. (cond ((not (string-match "[0-9a-zA-Z]" bullet)) "- ")
  1098. ((eq org-plain-list-ordered-item-terminator ?\)) "1)")
  1099. (t "1.")))))
  1100. (checkbox (org-element-property :checkbox item))
  1101. (counter (org-element-property :counter item))
  1102. (tag (let ((tag (org-element-property :tag item)))
  1103. (and tag (org-element-interpret-data tag))))
  1104. ;; Compute indentation.
  1105. (ind (make-string (length bullet) 32))
  1106. (item-starts-with-par-p
  1107. (eq (org-element-type (car (org-element-contents item)))
  1108. 'paragraph)))
  1109. ;; Indent contents.
  1110. (concat
  1111. bullet
  1112. (and counter (format "[@%d] " counter))
  1113. (case checkbox
  1114. (on "[X] ")
  1115. (off "[ ] ")
  1116. (trans "[-] "))
  1117. (and tag (format "%s :: " tag))
  1118. (when contents
  1119. (let ((contents (replace-regexp-in-string
  1120. "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
  1121. (if item-starts-with-par-p (org-trim contents)
  1122. (concat "\n" contents)))))))
  1123. ;;;; Plain List
  1124. (defun org-element--list-struct (limit)
  1125. ;; Return structure of list at point. Internal function. See
  1126. ;; `org-list-struct' for details.
  1127. (let ((case-fold-search t)
  1128. (top-ind limit)
  1129. (item-re (org-item-re))
  1130. (inlinetask-re (and (featurep 'org-inlinetask) "^\\*+ "))
  1131. items struct)
  1132. (save-excursion
  1133. (catch 'exit
  1134. (while t
  1135. (cond
  1136. ;; At limit: end all items.
  1137. ((>= (point) limit)
  1138. (throw 'exit
  1139. (let ((end (progn (skip-chars-backward " \r\t\n")
  1140. (forward-line)
  1141. (point))))
  1142. (dolist (item items (sort (nconc items struct)
  1143. 'car-less-than-car))
  1144. (setcar (nthcdr 6 item) end)))))
  1145. ;; At list end: end all items.
  1146. ((looking-at org-list-end-re)
  1147. (throw 'exit (dolist (item items (sort (nconc items struct)
  1148. 'car-less-than-car))
  1149. (setcar (nthcdr 6 item) (point)))))
  1150. ;; At a new item: end previous sibling.
  1151. ((looking-at item-re)
  1152. (let ((ind (save-excursion (skip-chars-forward " \t")
  1153. (current-column))))
  1154. (setq top-ind (min top-ind ind))
  1155. (while (and items (<= ind (nth 1 (car items))))
  1156. (let ((item (pop items)))
  1157. (setcar (nthcdr 6 item) (point))
  1158. (push item struct)))
  1159. (push (progn (looking-at org-list-full-item-re)
  1160. (let ((bullet (match-string-no-properties 1)))
  1161. (list (point)
  1162. ind
  1163. bullet
  1164. (match-string-no-properties 2) ; counter
  1165. (match-string-no-properties 3) ; checkbox
  1166. ;; Description tag.
  1167. (and (save-match-data
  1168. (string-match "[-+*]" bullet))
  1169. (match-string-no-properties 4))
  1170. ;; Ending position, unknown so far.
  1171. nil)))
  1172. items))
  1173. (forward-line 1))
  1174. ;; Skip empty lines.
  1175. ((looking-at "^[ \t]*$") (forward-line))
  1176. ;; Skip inline tasks and blank lines along the way.
  1177. ((and inlinetask-re (looking-at inlinetask-re))
  1178. (forward-line)
  1179. (let ((origin (point)))
  1180. (when (re-search-forward inlinetask-re limit t)
  1181. (if (org-looking-at-p "END[ \t]*$") (forward-line)
  1182. (goto-char origin)))))
  1183. ;; At some text line. Check if it ends any previous item.
  1184. (t
  1185. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  1186. (when (<= ind top-ind)
  1187. (skip-chars-backward " \r\t\n")
  1188. (forward-line))
  1189. (while (<= ind (nth 1 (car items)))
  1190. (let ((item (pop items)))
  1191. (setcar (nthcdr 6 item) (line-beginning-position))
  1192. (push item struct)
  1193. (unless items
  1194. (throw 'exit (sort struct 'car-less-than-car))))))
  1195. ;; Skip blocks (any type) and drawers contents.
  1196. (cond
  1197. ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
  1198. (re-search-forward
  1199. (format "^[ \t]*#\\+END%s[ \t]*$" (match-string 1))
  1200. limit t)))
  1201. ((and (looking-at org-drawer-regexp)
  1202. (re-search-forward "^[ \t]*:END:[ \t]*$" limit t))))
  1203. (forward-line))))))))
  1204. (defun org-element-plain-list-parser (limit affiliated structure)
  1205. "Parse a plain list.
  1206. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1207. the buffer position at the beginning of the first affiliated
  1208. keyword and CDR is a plist of affiliated keywords along with
  1209. their value. STRUCTURE is the structure of the plain list being
  1210. parsed.
  1211. Return a list whose CAR is `plain-list' and CDR is a plist
  1212. containing `:type', `:begin', `:end', `:contents-begin' and
  1213. `:contents-end', `:structure', `:post-blank' and
  1214. `:post-affiliated' keywords.
  1215. Assume point is at the beginning of the list."
  1216. (save-excursion
  1217. (let* ((struct (or structure (org-element--list-struct limit)))
  1218. (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered)
  1219. ((nth 5 (assq (point) struct)) 'descriptive)
  1220. (t 'unordered)))
  1221. (contents-begin (point))
  1222. (begin (car affiliated))
  1223. (contents-end (let* ((item (assq contents-begin struct))
  1224. (ind (nth 1 item))
  1225. (pos (nth 6 item)))
  1226. (while (and (setq item (assq pos struct))
  1227. (= (nth 1 item) ind))
  1228. (setq pos (nth 6 item)))
  1229. pos))
  1230. (end (progn (goto-char contents-end)
  1231. (skip-chars-forward " \r\t\n" limit)
  1232. (if (= (point) limit) limit (line-beginning-position)))))
  1233. ;; Return value.
  1234. (list 'plain-list
  1235. (nconc
  1236. (list :type type
  1237. :begin begin
  1238. :end end
  1239. :contents-begin contents-begin
  1240. :contents-end contents-end
  1241. :structure struct
  1242. :post-blank (count-lines contents-end end)
  1243. :post-affiliated contents-begin)
  1244. (cdr affiliated))))))
  1245. (defun org-element-plain-list-interpreter (plain-list contents)
  1246. "Interpret PLAIN-LIST element as Org syntax.
  1247. CONTENTS is the contents of the element."
  1248. (with-temp-buffer
  1249. (insert contents)
  1250. (goto-char (point-min))
  1251. (org-list-repair)
  1252. (buffer-string)))
  1253. ;;;; Property Drawer
  1254. (defun org-element-property-drawer-parser (limit affiliated)
  1255. "Parse a property drawer.
  1256. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1257. the buffer position at the beginning of the first affiliated
  1258. keyword and CDR is a plist of affiliated keywords along with
  1259. their value.
  1260. Return a list whose CAR is `property-drawer' and CDR is a plist
  1261. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1262. `:post-blank' and `:post-affiliated' keywords.
  1263. Assume point is at the beginning of the property drawer."
  1264. (save-excursion
  1265. (let ((case-fold-search t))
  1266. (if (not (save-excursion
  1267. (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
  1268. ;; Incomplete drawer: parse it as a paragraph.
  1269. (org-element-paragraph-parser limit affiliated)
  1270. (save-excursion
  1271. (let* ((drawer-end-line (match-beginning 0))
  1272. (begin (car affiliated))
  1273. (post-affiliated (point))
  1274. (contents-begin (progn (forward-line)
  1275. (and (< (point) drawer-end-line)
  1276. (point))))
  1277. (contents-end (and contents-begin drawer-end-line))
  1278. (pos-before-blank (progn (goto-char drawer-end-line)
  1279. (forward-line)
  1280. (point)))
  1281. (end (progn (skip-chars-forward " \r\t\n" limit)
  1282. (if (eobp) (point) (line-beginning-position)))))
  1283. (list 'property-drawer
  1284. (nconc
  1285. (list :begin begin
  1286. :end end
  1287. :contents-begin contents-begin
  1288. :contents-end contents-end
  1289. :post-blank (count-lines pos-before-blank end)
  1290. :post-affiliated post-affiliated)
  1291. (cdr affiliated)))))))))
  1292. (defun org-element-property-drawer-interpreter (property-drawer contents)
  1293. "Interpret PROPERTY-DRAWER element as Org syntax.
  1294. CONTENTS is the properties within the drawer."
  1295. (format ":PROPERTIES:\n%s:END:" contents))
  1296. ;;;; Quote Block
  1297. (defun org-element-quote-block-parser (limit affiliated)
  1298. "Parse a quote block.
  1299. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1300. the buffer position at the beginning of the first affiliated
  1301. keyword and CDR is a plist of affiliated keywords along with
  1302. their value.
  1303. Return a list whose CAR is `quote-block' and CDR is a plist
  1304. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1305. `:post-blank' and `:post-affiliated' keywords.
  1306. Assume point is at the beginning of the block."
  1307. (let ((case-fold-search t))
  1308. (if (not (save-excursion
  1309. (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
  1310. ;; Incomplete block: parse it as a paragraph.
  1311. (org-element-paragraph-parser limit affiliated)
  1312. (let ((block-end-line (match-beginning 0)))
  1313. (save-excursion
  1314. (let* ((begin (car affiliated))
  1315. (post-affiliated (point))
  1316. ;; Empty blocks have no contents.
  1317. (contents-begin (progn (forward-line)
  1318. (and (< (point) block-end-line)
  1319. (point))))
  1320. (contents-end (and contents-begin block-end-line))
  1321. (pos-before-blank (progn (goto-char block-end-line)
  1322. (forward-line)
  1323. (point)))
  1324. (end (progn (skip-chars-forward " \r\t\n" limit)
  1325. (if (eobp) (point) (line-beginning-position)))))
  1326. (list 'quote-block
  1327. (nconc
  1328. (list :begin begin
  1329. :end end
  1330. :contents-begin contents-begin
  1331. :contents-end contents-end
  1332. :post-blank (count-lines pos-before-blank end)
  1333. :post-affiliated post-affiliated)
  1334. (cdr affiliated)))))))))
  1335. (defun org-element-quote-block-interpreter (quote-block contents)
  1336. "Interpret QUOTE-BLOCK element as Org syntax.
  1337. CONTENTS is the contents of the element."
  1338. (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
  1339. ;;;; Section
  1340. (defun org-element-section-parser (limit)
  1341. "Parse a section.
  1342. LIMIT bounds the search.
  1343. Return a list whose CAR is `section' and CDR is a plist
  1344. containing `:begin', `:end', `:contents-begin', `contents-end'
  1345. and `:post-blank' keywords."
  1346. (save-excursion
  1347. ;; Beginning of section is the beginning of the first non-blank
  1348. ;; line after previous headline.
  1349. (let ((begin (point))
  1350. (end (progn (org-with-limited-levels (outline-next-heading))
  1351. (point)))
  1352. (pos-before-blank (progn (skip-chars-backward " \r\t\n")
  1353. (forward-line)
  1354. (point))))
  1355. (list 'section
  1356. (list :begin begin
  1357. :end end
  1358. :contents-begin begin
  1359. :contents-end pos-before-blank
  1360. :post-blank (count-lines pos-before-blank end))))))
  1361. (defun org-element-section-interpreter (section contents)
  1362. "Interpret SECTION element as Org syntax.
  1363. CONTENTS is the contents of the element."
  1364. contents)
  1365. ;;;; Special Block
  1366. (defun org-element-special-block-parser (limit affiliated)
  1367. "Parse a special block.
  1368. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1369. the buffer position at the beginning of the first affiliated
  1370. keyword and CDR is a plist of affiliated keywords along with
  1371. their value.
  1372. Return a list whose CAR is `special-block' and CDR is a plist
  1373. containing `:type', `:begin', `:end', `:contents-begin',
  1374. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  1375. Assume point is at the beginning of the block."
  1376. (let* ((case-fold-search t)
  1377. (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1378. (upcase (match-string-no-properties 1)))))
  1379. (if (not (save-excursion
  1380. (re-search-forward
  1381. (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type))
  1382. limit t)))
  1383. ;; Incomplete block: parse it as a paragraph.
  1384. (org-element-paragraph-parser limit affiliated)
  1385. (let ((block-end-line (match-beginning 0)))
  1386. (save-excursion
  1387. (let* ((begin (car affiliated))
  1388. (post-affiliated (point))
  1389. ;; Empty blocks have no contents.
  1390. (contents-begin (progn (forward-line)
  1391. (and (< (point) block-end-line)
  1392. (point))))
  1393. (contents-end (and contents-begin block-end-line))
  1394. (pos-before-blank (progn (goto-char block-end-line)
  1395. (forward-line)
  1396. (point)))
  1397. (end (progn (skip-chars-forward " \r\t\n" limit)
  1398. (if (eobp) (point) (line-beginning-position)))))
  1399. (list 'special-block
  1400. (nconc
  1401. (list :type type
  1402. :begin begin
  1403. :end end
  1404. :contents-begin contents-begin
  1405. :contents-end contents-end
  1406. :post-blank (count-lines pos-before-blank end)
  1407. :post-affiliated post-affiliated)
  1408. (cdr affiliated)))))))))
  1409. (defun org-element-special-block-interpreter (special-block contents)
  1410. "Interpret SPECIAL-BLOCK element as Org syntax.
  1411. CONTENTS is the contents of the element."
  1412. (let ((block-type (org-element-property :type special-block)))
  1413. (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
  1414. ;;; Elements
  1415. ;;
  1416. ;; For each element, a parser and an interpreter are also defined.
  1417. ;; Both follow the same naming convention used for greater elements.
  1418. ;;
  1419. ;; Also, as for greater elements, adding a new element type is done
  1420. ;; through the following steps: implement a parser and an interpreter,
  1421. ;; tweak `org-element--current-element' so that it recognizes the new
  1422. ;; type and add that new type to `org-element-all-elements'.
  1423. ;;
  1424. ;; As a special case, when the newly defined type is a block type,
  1425. ;; `org-element-block-name-alist' has to be modified accordingly.
  1426. ;;;; Babel Call
  1427. (defun org-element-babel-call-parser (limit affiliated)
  1428. "Parse a babel call.
  1429. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1430. the buffer position at the beginning of the first affiliated
  1431. keyword and CDR is a plist of affiliated keywords along with
  1432. their value.
  1433. Return a list whose CAR is `babel-call' and CDR is a plist
  1434. containing `:begin', `:end', `:value', `:post-blank' and
  1435. `:post-affiliated' as keywords."
  1436. (save-excursion
  1437. (let ((begin (car affiliated))
  1438. (post-affiliated (point))
  1439. (value (progn (let ((case-fold-search t))
  1440. (re-search-forward "call:[ \t]*" nil t))
  1441. (buffer-substring-no-properties (point)
  1442. (line-end-position))))
  1443. (pos-before-blank (progn (forward-line) (point)))
  1444. (end (progn (skip-chars-forward " \r\t\n" limit)
  1445. (if (eobp) (point) (line-beginning-position)))))
  1446. (list 'babel-call
  1447. (nconc
  1448. (list :begin begin
  1449. :end end
  1450. :value value
  1451. :post-blank (count-lines pos-before-blank end)
  1452. :post-affiliated post-affiliated)
  1453. (cdr affiliated))))))
  1454. (defun org-element-babel-call-interpreter (babel-call contents)
  1455. "Interpret BABEL-CALL element as Org syntax.
  1456. CONTENTS is nil."
  1457. (concat "#+CALL: " (org-element-property :value babel-call)))
  1458. ;;;; Clock
  1459. (defun org-element-clock-parser (limit)
  1460. "Parse a clock.
  1461. LIMIT bounds the search.
  1462. Return a list whose CAR is `clock' and CDR is a plist containing
  1463. `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
  1464. as keywords."
  1465. (save-excursion
  1466. (let* ((case-fold-search nil)
  1467. (begin (point))
  1468. (value (progn (search-forward org-clock-string (line-end-position) t)
  1469. (skip-chars-forward " \t")
  1470. (org-element-timestamp-parser)))
  1471. (duration (and (search-forward " => " (line-end-position) t)
  1472. (progn (skip-chars-forward " \t")
  1473. (looking-at "\\(\\S-+\\)[ \t]*$"))
  1474. (org-match-string-no-properties 1)))
  1475. (status (if duration 'closed 'running))
  1476. (post-blank (let ((before-blank (progn (forward-line) (point))))
  1477. (skip-chars-forward " \r\t\n" limit)
  1478. (skip-chars-backward " \t")
  1479. (unless (bolp) (end-of-line))
  1480. (count-lines before-blank (point))))
  1481. (end (point)))
  1482. (list 'clock
  1483. (list :status status
  1484. :value value
  1485. :duration duration
  1486. :begin begin
  1487. :end end
  1488. :post-blank post-blank)))))
  1489. (defun org-element-clock-interpreter (clock contents)
  1490. "Interpret CLOCK element as Org syntax.
  1491. CONTENTS is nil."
  1492. (concat org-clock-string " "
  1493. (org-element-timestamp-interpreter
  1494. (org-element-property :value clock) nil)
  1495. (let ((duration (org-element-property :duration clock)))
  1496. (and duration
  1497. (concat " => "
  1498. (apply 'format
  1499. "%2s:%02s"
  1500. (org-split-string duration ":")))))))
  1501. ;;;; Comment
  1502. (defun org-element-comment-parser (limit affiliated)
  1503. "Parse a comment.
  1504. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1505. the buffer position at the beginning of the first affiliated
  1506. keyword and CDR is a plist of affiliated keywords along with
  1507. their value.
  1508. Return a list whose CAR is `comment' and CDR is a plist
  1509. containing `:begin', `:end', `:value', `:post-blank',
  1510. `:post-affiliated' keywords.
  1511. Assume point is at comment beginning."
  1512. (save-excursion
  1513. (let* ((begin (car affiliated))
  1514. (post-affiliated (point))
  1515. (value (prog2 (looking-at "[ \t]*# ?")
  1516. (buffer-substring-no-properties
  1517. (match-end 0) (line-end-position))
  1518. (forward-line)))
  1519. (com-end
  1520. ;; Get comments ending.
  1521. (progn
  1522. (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
  1523. ;; Accumulate lines without leading hash and first
  1524. ;; whitespace.
  1525. (setq value
  1526. (concat value
  1527. "\n"
  1528. (buffer-substring-no-properties
  1529. (match-end 0) (line-end-position))))
  1530. (forward-line))
  1531. (point)))
  1532. (end (progn (goto-char com-end)
  1533. (skip-chars-forward " \r\t\n" limit)
  1534. (if (eobp) (point) (line-beginning-position)))))
  1535. (list 'comment
  1536. (nconc
  1537. (list :begin begin
  1538. :end end
  1539. :value value
  1540. :post-blank (count-lines com-end end)
  1541. :post-affiliated post-affiliated)
  1542. (cdr affiliated))))))
  1543. (defun org-element-comment-interpreter (comment contents)
  1544. "Interpret COMMENT element as Org syntax.
  1545. CONTENTS is nil."
  1546. (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
  1547. ;;;; Comment Block
  1548. (defun org-element-comment-block-parser (limit affiliated)
  1549. "Parse an export block.
  1550. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1551. the buffer position at the beginning of the first affiliated
  1552. keyword and CDR is a plist of affiliated keywords along with
  1553. their value.
  1554. Return a list whose CAR is `comment-block' and CDR is a plist
  1555. containing `:begin', `:end', `:value', `:post-blank' and
  1556. `:post-affiliated' keywords.
  1557. Assume point is at comment block beginning."
  1558. (let ((case-fold-search t))
  1559. (if (not (save-excursion
  1560. (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
  1561. ;; Incomplete block: parse it as a paragraph.
  1562. (org-element-paragraph-parser limit affiliated)
  1563. (let ((contents-end (match-beginning 0)))
  1564. (save-excursion
  1565. (let* ((begin (car affiliated))
  1566. (post-affiliated (point))
  1567. (contents-begin (progn (forward-line) (point)))
  1568. (pos-before-blank (progn (goto-char contents-end)
  1569. (forward-line)
  1570. (point)))
  1571. (end (progn (skip-chars-forward " \r\t\n" limit)
  1572. (if (eobp) (point) (line-beginning-position))))
  1573. (value (buffer-substring-no-properties
  1574. contents-begin contents-end)))
  1575. (list 'comment-block
  1576. (nconc
  1577. (list :begin begin
  1578. :end end
  1579. :value value
  1580. :post-blank (count-lines pos-before-blank end)
  1581. :post-affiliated post-affiliated)
  1582. (cdr affiliated)))))))))
  1583. (defun org-element-comment-block-interpreter (comment-block contents)
  1584. "Interpret COMMENT-BLOCK element as Org syntax.
  1585. CONTENTS is nil."
  1586. (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
  1587. (org-remove-indentation (org-element-property :value comment-block))))
  1588. ;;;; Diary Sexp
  1589. (defun org-element-diary-sexp-parser (limit affiliated)
  1590. "Parse a diary sexp.
  1591. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1592. the buffer position at the beginning of the first affiliated
  1593. keyword and CDR is a plist of affiliated keywords along with
  1594. their value.
  1595. Return a list whose CAR is `diary-sexp' and CDR is a plist
  1596. containing `:begin', `:end', `:value', `:post-blank' and
  1597. `:post-affiliated' keywords."
  1598. (save-excursion
  1599. (let ((begin (car affiliated))
  1600. (post-affiliated (point))
  1601. (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
  1602. (org-match-string-no-properties 1)))
  1603. (pos-before-blank (progn (forward-line) (point)))
  1604. (end (progn (skip-chars-forward " \r\t\n" limit)
  1605. (if (eobp) (point) (line-beginning-position)))))
  1606. (list 'diary-sexp
  1607. (nconc
  1608. (list :value value
  1609. :begin begin
  1610. :end end
  1611. :post-blank (count-lines pos-before-blank end)
  1612. :post-affiliated post-affiliated)
  1613. (cdr affiliated))))))
  1614. (defun org-element-diary-sexp-interpreter (diary-sexp contents)
  1615. "Interpret DIARY-SEXP as Org syntax.
  1616. CONTENTS is nil."
  1617. (org-element-property :value diary-sexp))
  1618. ;;;; Example Block
  1619. (defun org-element-example-block-parser (limit affiliated)
  1620. "Parse an example block.
  1621. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1622. the buffer position at the beginning of the first affiliated
  1623. keyword and CDR is a plist of affiliated keywords along with
  1624. their value.
  1625. Return a list whose CAR is `example-block' and CDR is a plist
  1626. containing `:begin', `:end', `:number-lines', `:preserve-indent',
  1627. `:retain-labels', `:use-labels', `:label-fmt', `:switches',
  1628. `:value', `:post-blank' and `:post-affiliated' keywords."
  1629. (let ((case-fold-search t))
  1630. (if (not (save-excursion
  1631. (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
  1632. ;; Incomplete block: parse it as a paragraph.
  1633. (org-element-paragraph-parser limit affiliated)
  1634. (let ((contents-end (match-beginning 0)))
  1635. (save-excursion
  1636. (let* ((switches
  1637. (progn
  1638. (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
  1639. (org-match-string-no-properties 1)))
  1640. ;; Switches analysis
  1641. (number-lines
  1642. (cond ((not switches) nil)
  1643. ((string-match "-n\\>" switches) 'new)
  1644. ((string-match "+n\\>" switches) 'continued)))
  1645. (preserve-indent
  1646. (and switches (string-match "-i\\>" switches)))
  1647. ;; Should labels be retained in (or stripped from) example
  1648. ;; blocks?
  1649. (retain-labels
  1650. (or (not switches)
  1651. (not (string-match "-r\\>" switches))
  1652. (and number-lines (string-match "-k\\>" switches))))
  1653. ;; What should code-references use - labels or
  1654. ;; line-numbers?
  1655. (use-labels
  1656. (or (not switches)
  1657. (and retain-labels
  1658. (not (string-match "-k\\>" switches)))))
  1659. (label-fmt
  1660. (and switches
  1661. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  1662. (match-string 1 switches)))
  1663. ;; Standard block parsing.
  1664. (begin (car affiliated))
  1665. (post-affiliated (point))
  1666. (block-ind (progn (skip-chars-forward " \t") (current-column)))
  1667. (contents-begin (progn (forward-line) (point)))
  1668. (value (org-element-remove-indentation
  1669. (org-unescape-code-in-string
  1670. (buffer-substring-no-properties
  1671. contents-begin contents-end))
  1672. block-ind))
  1673. (pos-before-blank (progn (goto-char contents-end)
  1674. (forward-line)
  1675. (point)))
  1676. (end (progn (skip-chars-forward " \r\t\n" limit)
  1677. (if (eobp) (point) (line-beginning-position)))))
  1678. (list 'example-block
  1679. (nconc
  1680. (list :begin begin
  1681. :end end
  1682. :value value
  1683. :switches switches
  1684. :number-lines number-lines
  1685. :preserve-indent preserve-indent
  1686. :retain-labels retain-labels
  1687. :use-labels use-labels
  1688. :label-fmt label-fmt
  1689. :post-blank (count-lines pos-before-blank end)
  1690. :post-affiliated post-affiliated)
  1691. (cdr affiliated)))))))))
  1692. (defun org-element-example-block-interpreter (example-block contents)
  1693. "Interpret EXAMPLE-BLOCK element as Org syntax.
  1694. CONTENTS is nil."
  1695. (let ((switches (org-element-property :switches example-block))
  1696. (value (org-element-property :value example-block)))
  1697. (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
  1698. (org-escape-code-in-string
  1699. (if (or org-src-preserve-indentation
  1700. (org-element-property :preserve-indent example-block))
  1701. value
  1702. (org-element-remove-indentation value)))
  1703. "#+END_EXAMPLE")))
  1704. ;;;; Export Block
  1705. (defun org-element-export-block-parser (limit affiliated)
  1706. "Parse an export block.
  1707. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1708. the buffer position at the beginning of the first affiliated
  1709. keyword and CDR is a plist of affiliated keywords along with
  1710. their value.
  1711. Return a list whose CAR is `export-block' and CDR is a plist
  1712. containing `:begin', `:end', `:type', `:value', `:post-blank' and
  1713. `:post-affiliated' keywords.
  1714. Assume point is at export-block beginning."
  1715. (let* ((case-fold-search t)
  1716. (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1717. (upcase (org-match-string-no-properties 1)))))
  1718. (if (not (save-excursion
  1719. (re-search-forward
  1720. (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
  1721. ;; Incomplete block: parse it as a paragraph.
  1722. (org-element-paragraph-parser limit affiliated)
  1723. (let ((contents-end (match-beginning 0)))
  1724. (save-excursion
  1725. (let* ((begin (car affiliated))
  1726. (post-affiliated (point))
  1727. (contents-begin (progn (forward-line) (point)))
  1728. (pos-before-blank (progn (goto-char contents-end)
  1729. (forward-line)
  1730. (point)))
  1731. (end (progn (skip-chars-forward " \r\t\n" limit)
  1732. (if (eobp) (point) (line-beginning-position))))
  1733. (value (buffer-substring-no-properties contents-begin
  1734. contents-end)))
  1735. (list 'export-block
  1736. (nconc
  1737. (list :begin begin
  1738. :end end
  1739. :type type
  1740. :value value
  1741. :post-blank (count-lines pos-before-blank end)
  1742. :post-affiliated post-affiliated)
  1743. (cdr affiliated)))))))))
  1744. (defun org-element-export-block-interpreter (export-block contents)
  1745. "Interpret EXPORT-BLOCK element as Org syntax.
  1746. CONTENTS is nil."
  1747. (let ((type (org-element-property :type export-block)))
  1748. (concat (format "#+BEGIN_%s\n" type)
  1749. (org-element-property :value export-block)
  1750. (format "#+END_%s" type))))
  1751. ;;;; Fixed-width
  1752. (defun org-element-fixed-width-parser (limit affiliated)
  1753. "Parse a fixed-width section.
  1754. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1755. the buffer position at the beginning of the first affiliated
  1756. keyword and CDR is a plist of affiliated keywords along with
  1757. their value.
  1758. Return a list whose CAR is `fixed-width' and CDR is a plist
  1759. containing `:begin', `:end', `:value', `:post-blank' and
  1760. `:post-affiliated' keywords.
  1761. Assume point is at the beginning of the fixed-width area."
  1762. (save-excursion
  1763. (let* ((begin (car affiliated))
  1764. (post-affiliated (point))
  1765. value
  1766. (end-area
  1767. (progn
  1768. (while (and (< (point) limit)
  1769. (looking-at "[ \t]*:\\( \\|$\\)"))
  1770. ;; Accumulate text without starting colons.
  1771. (setq value
  1772. (concat value
  1773. (buffer-substring-no-properties
  1774. (match-end 0) (point-at-eol))
  1775. "\n"))
  1776. (forward-line))
  1777. (point)))
  1778. (end (progn (skip-chars-forward " \r\t\n" limit)
  1779. (if (eobp) (point) (line-beginning-position)))))
  1780. (list 'fixed-width
  1781. (nconc
  1782. (list :begin begin
  1783. :end end
  1784. :value value
  1785. :post-blank (count-lines end-area end)
  1786. :post-affiliated post-affiliated)
  1787. (cdr affiliated))))))
  1788. (defun org-element-fixed-width-interpreter (fixed-width contents)
  1789. "Interpret FIXED-WIDTH element as Org syntax.
  1790. CONTENTS is nil."
  1791. (let ((value (org-element-property :value fixed-width)))
  1792. (and value
  1793. (replace-regexp-in-string
  1794. "^" ": "
  1795. (if (string-match "\n\\'" value) (substring value 0 -1) value)))))
  1796. ;;;; Horizontal Rule
  1797. (defun org-element-horizontal-rule-parser (limit affiliated)
  1798. "Parse an horizontal rule.
  1799. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1800. the buffer position at the beginning of the first affiliated
  1801. keyword and CDR is a plist of affiliated keywords along with
  1802. their value.
  1803. Return a list whose CAR is `horizontal-rule' and CDR is a plist
  1804. containing `:begin', `:end', `:post-blank' and `:post-affiliated'
  1805. keywords."
  1806. (save-excursion
  1807. (let ((begin (car affiliated))
  1808. (post-affiliated (point))
  1809. (post-hr (progn (forward-line) (point)))
  1810. (end (progn (skip-chars-forward " \r\t\n" limit)
  1811. (if (eobp) (point) (line-beginning-position)))))
  1812. (list 'horizontal-rule
  1813. (nconc
  1814. (list :begin begin
  1815. :end end
  1816. :post-blank (count-lines post-hr end)
  1817. :post-affiliated post-affiliated)
  1818. (cdr affiliated))))))
  1819. (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
  1820. "Interpret HORIZONTAL-RULE element as Org syntax.
  1821. CONTENTS is nil."
  1822. "-----")
  1823. ;;;; Keyword
  1824. (defun org-element-keyword-parser (limit affiliated)
  1825. "Parse a keyword at point.
  1826. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1827. the buffer position at the beginning of the first affiliated
  1828. keyword and CDR is a plist of affiliated keywords along with
  1829. their value.
  1830. Return a list whose CAR is `keyword' and CDR is a plist
  1831. containing `:key', `:value', `:begin', `:end', `:post-blank' and
  1832. `:post-affiliated' keywords."
  1833. (save-excursion
  1834. ;; An orphaned affiliated keyword is considered as a regular
  1835. ;; keyword. In this case AFFILIATED is nil, so we take care of
  1836. ;; this corner case.
  1837. (let ((begin (or (car affiliated) (point)))
  1838. (post-affiliated (point))
  1839. (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
  1840. (upcase (org-match-string-no-properties 1))))
  1841. (value (org-trim (buffer-substring-no-properties
  1842. (match-end 0) (point-at-eol))))
  1843. (pos-before-blank (progn (forward-line) (point)))
  1844. (end (progn (skip-chars-forward " \r\t\n" limit)
  1845. (if (eobp) (point) (line-beginning-position)))))
  1846. (list 'keyword
  1847. (nconc
  1848. (list :key key
  1849. :value value
  1850. :begin begin
  1851. :end end
  1852. :post-blank (count-lines pos-before-blank end)
  1853. :post-affiliated post-affiliated)
  1854. (cdr affiliated))))))
  1855. (defun org-element-keyword-interpreter (keyword contents)
  1856. "Interpret KEYWORD element as Org syntax.
  1857. CONTENTS is nil."
  1858. (format "#+%s: %s"
  1859. (org-element-property :key keyword)
  1860. (org-element-property :value keyword)))
  1861. ;;;; Latex Environment
  1862. (defconst org-element--latex-begin-environment
  1863. "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}"
  1864. "Regexp matching the beginning of a LaTeX environment.
  1865. The environment is captured by the first group.
  1866. See also `org-element--latex-end-environment'.")
  1867. (defconst org-element--latex-end-environment
  1868. "\\\\end{%s}[ \t]*$"
  1869. "Format string matching the ending of a LaTeX environment.
  1870. See also `org-element--latex-begin-environment'.")
  1871. (defun org-element-latex-environment-parser (limit affiliated)
  1872. "Parse a LaTeX environment.
  1873. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1874. the buffer position at the beginning of the first affiliated
  1875. keyword and CDR is a plist of affiliated keywords along with
  1876. their value.
  1877. Return a list whose CAR is `latex-environment' and CDR is a plist
  1878. containing `:begin', `:end', `:value', `:post-blank' and
  1879. `:post-affiliated' keywords.
  1880. Assume point is at the beginning of the latex environment."
  1881. (save-excursion
  1882. (let ((case-fold-search t)
  1883. (code-begin (point)))
  1884. (looking-at org-element--latex-begin-environment)
  1885. (if (not (re-search-forward (format org-element--latex-end-environment
  1886. (regexp-quote (match-string 1)))
  1887. limit t))
  1888. ;; Incomplete latex environment: parse it as a paragraph.
  1889. (org-element-paragraph-parser limit affiliated)
  1890. (let* ((code-end (progn (forward-line) (point)))
  1891. (begin (car affiliated))
  1892. (value (buffer-substring-no-properties code-begin code-end))
  1893. (end (progn (skip-chars-forward " \r\t\n" limit)
  1894. (if (eobp) (point) (line-beginning-position)))))
  1895. (list 'latex-environment
  1896. (nconc
  1897. (list :begin begin
  1898. :end end
  1899. :value value
  1900. :post-blank (count-lines code-end end)
  1901. :post-affiliated code-begin)
  1902. (cdr affiliated))))))))
  1903. (defun org-element-latex-environment-interpreter (latex-environment contents)
  1904. "Interpret LATEX-ENVIRONMENT element as Org syntax.
  1905. CONTENTS is nil."
  1906. (org-element-property :value latex-environment))
  1907. ;;;; Node Property
  1908. (defun org-element-node-property-parser (limit)
  1909. "Parse a node-property at point.
  1910. LIMIT bounds the search.
  1911. Return a list whose CAR is `node-property' and CDR is a plist
  1912. containing `:key', `:value', `:begin', `:end' and `:post-blank'
  1913. keywords."
  1914. (save-excursion
  1915. (looking-at org-property-re)
  1916. (let ((case-fold-search t)
  1917. (begin (point))
  1918. (key (org-match-string-no-properties 2))
  1919. (value (org-match-string-no-properties 3))
  1920. (pos-before-blank (progn (forward-line) (point)))
  1921. (end (progn (skip-chars-forward " \r\t\n" limit)
  1922. (if (eobp) (point) (point-at-bol)))))
  1923. (list 'node-property
  1924. (list :key key
  1925. :value value
  1926. :begin begin
  1927. :end end
  1928. :post-blank (count-lines pos-before-blank end))))))
  1929. (defun org-element-node-property-interpreter (node-property contents)
  1930. "Interpret NODE-PROPERTY element as Org syntax.
  1931. CONTENTS is nil."
  1932. (format org-property-format
  1933. (format ":%s:" (org-element-property :key node-property))
  1934. (org-element-property :value node-property)))
  1935. ;;;; Paragraph
  1936. (defun org-element-paragraph-parser (limit affiliated)
  1937. "Parse a paragraph.
  1938. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1939. the buffer position at the beginning of the first affiliated
  1940. keyword and CDR is a plist of affiliated keywords along with
  1941. their value.
  1942. Return a list whose CAR is `paragraph' and CDR is a plist
  1943. containing `:begin', `:end', `:contents-begin' and
  1944. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  1945. Assume point is at the beginning of the paragraph."
  1946. (save-excursion
  1947. (let* ((begin (car affiliated))
  1948. (contents-begin (point))
  1949. (before-blank
  1950. (let ((case-fold-search t))
  1951. (end-of-line)
  1952. (if (not (re-search-forward
  1953. org-element-paragraph-separate limit 'm))
  1954. limit
  1955. ;; A matching `org-element-paragraph-separate' is not
  1956. ;; necessarily the end of the paragraph. In
  1957. ;; particular, lines starting with # or : as a first
  1958. ;; non-space character are ambiguous. We have to
  1959. ;; check if they are valid Org syntax (e.g., not an
  1960. ;; incomplete keyword).
  1961. (beginning-of-line)
  1962. (while (not
  1963. (or
  1964. ;; There's no ambiguity for other symbols or
  1965. ;; empty lines: stop here.
  1966. (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
  1967. ;; Stop at valid fixed-width areas.
  1968. (looking-at "[ \t]*:\\(?: \\|$\\)")
  1969. ;; Stop at drawers.
  1970. (and (looking-at org-drawer-regexp)
  1971. (save-excursion
  1972. (re-search-forward
  1973. "^[ \t]*:END:[ \t]*$" limit t)))
  1974. ;; Stop at valid comments.
  1975. (looking-at "[ \t]*#\\(?: \\|$\\)")
  1976. ;; Stop at valid dynamic blocks.
  1977. (and (looking-at org-dblock-start-re)
  1978. (save-excursion
  1979. (re-search-forward
  1980. "^[ \t]*#\\+END:?[ \t]*$" limit t)))
  1981. ;; Stop at valid blocks.
  1982. (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1983. (save-excursion
  1984. (re-search-forward
  1985. (format "^[ \t]*#\\+END_%s[ \t]*$"
  1986. (regexp-quote
  1987. (org-match-string-no-properties 1)))
  1988. limit t)))
  1989. ;; Stop at valid latex environments.
  1990. (and (looking-at org-element--latex-begin-environment)
  1991. (save-excursion
  1992. (re-search-forward
  1993. (format org-element--latex-end-environment
  1994. (regexp-quote
  1995. (org-match-string-no-properties 1)))
  1996. limit t)))
  1997. ;; Stop at valid keywords.
  1998. (looking-at "[ \t]*#\\+\\S-+:")
  1999. ;; Skip everything else.
  2000. (not
  2001. (progn
  2002. (end-of-line)
  2003. (re-search-forward org-element-paragraph-separate
  2004. limit 'm)))))
  2005. (beginning-of-line)))
  2006. (if (= (point) limit) limit
  2007. (goto-char (line-beginning-position)))))
  2008. (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
  2009. (forward-line)
  2010. (point)))
  2011. (end (progn (skip-chars-forward " \r\t\n" limit)
  2012. (if (eobp) (point) (line-beginning-position)))))
  2013. (list 'paragraph
  2014. (nconc
  2015. (list :begin begin
  2016. :end end
  2017. :contents-begin contents-begin
  2018. :contents-end contents-end
  2019. :post-blank (count-lines before-blank end)
  2020. :post-affiliated contents-begin)
  2021. (cdr affiliated))))))
  2022. (defun org-element-paragraph-interpreter (paragraph contents)
  2023. "Interpret PARAGRAPH element as Org syntax.
  2024. CONTENTS is the contents of the element."
  2025. contents)
  2026. ;;;; Planning
  2027. (defun org-element-planning-parser (limit)
  2028. "Parse a planning.
  2029. LIMIT bounds the search.
  2030. Return a list whose CAR is `planning' and CDR is a plist
  2031. containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
  2032. and `:post-blank' keywords."
  2033. (save-excursion
  2034. (let* ((case-fold-search nil)
  2035. (begin (point))
  2036. (post-blank (let ((before-blank (progn (forward-line) (point))))
  2037. (skip-chars-forward " \r\t\n" limit)
  2038. (skip-chars-backward " \t")
  2039. (unless (bolp) (end-of-line))
  2040. (count-lines before-blank (point))))
  2041. (end (point))
  2042. closed deadline scheduled)
  2043. (goto-char begin)
  2044. (while (re-search-forward org-keyword-time-not-clock-regexp end t)
  2045. (goto-char (match-end 1))
  2046. (skip-chars-forward " \t" end)
  2047. (let ((keyword (match-string 1))
  2048. (time (org-element-timestamp-parser)))
  2049. (cond ((equal keyword org-closed-string) (setq closed time))
  2050. ((equal keyword org-deadline-string) (setq deadline time))
  2051. (t (setq scheduled time)))))
  2052. (list 'planning
  2053. (list :closed closed
  2054. :deadline deadline
  2055. :scheduled scheduled
  2056. :begin begin
  2057. :end end
  2058. :post-blank post-blank)))))
  2059. (defun org-element-planning-interpreter (planning contents)
  2060. "Interpret PLANNING element as Org syntax.
  2061. CONTENTS is nil."
  2062. (mapconcat
  2063. 'identity
  2064. (delq nil
  2065. (list (let ((deadline (org-element-property :deadline planning)))
  2066. (when deadline
  2067. (concat org-deadline-string " "
  2068. (org-element-timestamp-interpreter deadline nil))))
  2069. (let ((scheduled (org-element-property :scheduled planning)))
  2070. (when scheduled
  2071. (concat org-scheduled-string " "
  2072. (org-element-timestamp-interpreter scheduled nil))))
  2073. (let ((closed (org-element-property :closed planning)))
  2074. (when closed
  2075. (concat org-closed-string " "
  2076. (org-element-timestamp-interpreter closed nil))))))
  2077. " "))
  2078. ;;;; Src Block
  2079. (defun org-element-src-block-parser (limit affiliated)
  2080. "Parse a src block.
  2081. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  2082. the buffer position at the beginning of the first affiliated
  2083. keyword and CDR is a plist of affiliated keywords along with
  2084. their value.
  2085. Return a list whose CAR is `src-block' and CDR is a plist
  2086. containing `:language', `:switches', `:parameters', `:begin',
  2087. `:end', `:number-lines', `:retain-labels', `:use-labels',
  2088. `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
  2089. `:post-affiliated' keywords.
  2090. Assume point is at the beginning of the block."
  2091. (let ((case-fold-search t))
  2092. (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
  2093. limit t)))
  2094. ;; Incomplete block: parse it as a paragraph.
  2095. (org-element-paragraph-parser limit affiliated)
  2096. (let ((contents-end (match-beginning 0)))
  2097. (save-excursion
  2098. (let* ((begin (car affiliated))
  2099. (post-affiliated (point))
  2100. ;; Get language as a string.
  2101. (language
  2102. (progn
  2103. (looking-at
  2104. (concat "^[ \t]*#\\+BEGIN_SRC"
  2105. "\\(?: +\\(\\S-+\\)\\)?"
  2106. "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
  2107. "\\(.*\\)[ \t]*$"))
  2108. (org-match-string-no-properties 1)))
  2109. ;; Get switches.
  2110. (switches (org-match-string-no-properties 2))
  2111. ;; Get parameters.
  2112. (parameters (org-match-string-no-properties 3))
  2113. ;; Switches analysis
  2114. (number-lines
  2115. (cond ((not switches) nil)
  2116. ((string-match "-n\\>" switches) 'new)
  2117. ((string-match "+n\\>" switches) 'continued)))
  2118. (preserve-indent (and switches
  2119. (string-match "-i\\>" switches)))
  2120. (label-fmt
  2121. (and switches
  2122. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2123. (match-string 1 switches)))
  2124. ;; Should labels be retained in (or stripped from)
  2125. ;; src blocks?
  2126. (retain-labels
  2127. (or (not switches)
  2128. (not (string-match "-r\\>" switches))
  2129. (and number-lines (string-match "-k\\>" switches))))
  2130. ;; What should code-references use - labels or
  2131. ;; line-numbers?
  2132. (use-labels
  2133. (or (not switches)
  2134. (and retain-labels
  2135. (not (string-match "-k\\>" switches)))))
  2136. ;; Indentation.
  2137. (block-ind (progn (skip-chars-forward " \t") (current-column)))
  2138. ;; Retrieve code.
  2139. (value (org-element-remove-indentation
  2140. (org-unescape-code-in-string
  2141. (buffer-substring-no-properties
  2142. (progn (forward-line) (point)) contents-end))
  2143. block-ind))
  2144. (pos-before-blank (progn (goto-char contents-end)
  2145. (forward-line)
  2146. (point)))
  2147. ;; Get position after ending blank lines.
  2148. (end (progn (skip-chars-forward " \r\t\n" limit)
  2149. (if (eobp) (point) (line-beginning-position)))))
  2150. (list 'src-block
  2151. (nconc
  2152. (list :language language
  2153. :switches (and (org-string-nw-p switches)
  2154. (org-trim switches))
  2155. :parameters (and (org-string-nw-p parameters)
  2156. (org-trim parameters))
  2157. :begin begin
  2158. :end end
  2159. :number-lines number-lines
  2160. :preserve-indent preserve-indent
  2161. :retain-labels retain-labels
  2162. :use-labels use-labels
  2163. :label-fmt label-fmt
  2164. :value value
  2165. :post-blank (count-lines pos-before-blank end)
  2166. :post-affiliated post-affiliated)
  2167. (cdr affiliated)))))))))
  2168. (defun org-element-src-block-interpreter (src-block contents)
  2169. "Interpret SRC-BLOCK element as Org syntax.
  2170. CONTENTS is nil."
  2171. (let ((lang (org-element-property :language src-block))
  2172. (switches (org-element-property :switches src-block))
  2173. (params (org-element-property :parameters src-block))
  2174. (value
  2175. (let ((val (org-element-property :value src-block)))
  2176. (cond
  2177. ((or org-src-preserve-indentation
  2178. (org-element-property :preserve-indent src-block))
  2179. val)
  2180. ((zerop org-edit-src-content-indentation) val)
  2181. (t
  2182. (let ((ind (make-string org-edit-src-content-indentation ?\s)))
  2183. (replace-regexp-in-string
  2184. "\\(^\\)[ \t]*\\S-" ind val nil nil 1)))))))
  2185. (concat (format "#+BEGIN_SRC%s\n"
  2186. (concat (and lang (concat " " lang))
  2187. (and switches (concat " " switches))
  2188. (and params (concat " " params))))
  2189. (org-escape-code-in-string value)
  2190. "#+END_SRC")))
  2191. ;;;; Table
  2192. (defun org-element-table-parser (limit affiliated)
  2193. "Parse a table at point.
  2194. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  2195. the buffer position at the beginning of the first affiliated
  2196. keyword and CDR is a plist of affiliated keywords along with
  2197. their value.
  2198. Return a list whose CAR is `table' and CDR is a plist containing
  2199. `:begin', `:end', `:tblfm', `:type', `:contents-begin',
  2200. `:contents-end', `:value', `:post-blank' and `:post-affiliated'
  2201. keywords.
  2202. Assume point is at the beginning of the table."
  2203. (save-excursion
  2204. (let* ((case-fold-search t)
  2205. (table-begin (point))
  2206. (type (if (org-at-table.el-p) 'table.el 'org))
  2207. (begin (car affiliated))
  2208. (table-end
  2209. (if (re-search-forward org-table-any-border-regexp limit 'm)
  2210. (goto-char (match-beginning 0))
  2211. (point)))
  2212. (tblfm (let (acc)
  2213. (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
  2214. (push (org-match-string-no-properties 1) acc)
  2215. (forward-line))
  2216. acc))
  2217. (pos-before-blank (point))
  2218. (end (progn (skip-chars-forward " \r\t\n" limit)
  2219. (if (eobp) (point) (line-beginning-position)))))
  2220. (list 'table
  2221. (nconc
  2222. (list :begin begin
  2223. :end end
  2224. :type type
  2225. :tblfm tblfm
  2226. ;; Only `org' tables have contents. `table.el' tables
  2227. ;; use a `:value' property to store raw table as
  2228. ;; a string.
  2229. :contents-begin (and (eq type 'org) table-begin)
  2230. :contents-end (and (eq type 'org) table-end)
  2231. :value (and (eq type 'table.el)
  2232. (buffer-substring-no-properties
  2233. table-begin table-end))
  2234. :post-blank (count-lines pos-before-blank end)
  2235. :post-affiliated table-begin)
  2236. (cdr affiliated))))))
  2237. (defun org-element-table-interpreter (table contents)
  2238. "Interpret TABLE element as Org syntax.
  2239. CONTENTS is nil."
  2240. (if (eq (org-element-property :type table) 'table.el)
  2241. (org-remove-indentation (org-element-property :value table))
  2242. (concat (with-temp-buffer (insert contents)
  2243. (org-table-align)
  2244. (buffer-string))
  2245. (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
  2246. (reverse (org-element-property :tblfm table))
  2247. "\n"))))
  2248. ;;;; Table Row
  2249. (defun org-element-table-row-parser (limit)
  2250. "Parse table row at point.
  2251. LIMIT bounds the search.
  2252. Return a list whose CAR is `table-row' and CDR is a plist
  2253. containing `:begin', `:end', `:contents-begin', `:contents-end',
  2254. `:type' and `:post-blank' keywords."
  2255. (save-excursion
  2256. (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
  2257. (begin (point))
  2258. ;; A table rule has no contents. In that case, ensure
  2259. ;; CONTENTS-BEGIN matches CONTENTS-END.
  2260. (contents-begin (and (eq type 'standard)
  2261. (search-forward "|")
  2262. (point)))
  2263. (contents-end (and (eq type 'standard)
  2264. (progn
  2265. (end-of-line)
  2266. (skip-chars-backward " \t")
  2267. (point))))
  2268. (end (progn (forward-line) (point))))
  2269. (list 'table-row
  2270. (list :type type
  2271. :begin begin
  2272. :end end
  2273. :contents-begin contents-begin
  2274. :contents-end contents-end
  2275. :post-blank 0)))))
  2276. (defun org-element-table-row-interpreter (table-row contents)
  2277. "Interpret TABLE-ROW element as Org syntax.
  2278. CONTENTS is the contents of the table row."
  2279. (if (eq (org-element-property :type table-row) 'rule) "|-"
  2280. (concat "| " contents)))
  2281. ;;;; Verse Block
  2282. (defun org-element-verse-block-parser (limit affiliated)
  2283. "Parse a verse block.
  2284. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  2285. the buffer position at the beginning of the first affiliated
  2286. keyword and CDR is a plist of affiliated keywords along with
  2287. their value.
  2288. Return a list whose CAR is `verse-block' and CDR is a plist
  2289. containing `:begin', `:end', `:contents-begin', `:contents-end',
  2290. `:post-blank' and `:post-affiliated' keywords.
  2291. Assume point is at beginning of the block."
  2292. (let ((case-fold-search t))
  2293. (if (not (save-excursion
  2294. (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
  2295. ;; Incomplete block: parse it as a paragraph.
  2296. (org-element-paragraph-parser limit affiliated)
  2297. (let ((contents-end (match-beginning 0)))
  2298. (save-excursion
  2299. (let* ((begin (car affiliated))
  2300. (post-affiliated (point))
  2301. (contents-begin (progn (forward-line) (point)))
  2302. (pos-before-blank (progn (goto-char contents-end)
  2303. (forward-line)
  2304. (point)))
  2305. (end (progn (skip-chars-forward " \r\t\n" limit)
  2306. (if (eobp) (point) (line-beginning-position)))))
  2307. (list 'verse-block
  2308. (nconc
  2309. (list :begin begin
  2310. :end end
  2311. :contents-begin contents-begin
  2312. :contents-end contents-end
  2313. :post-blank (count-lines pos-before-blank end)
  2314. :post-affiliated post-affiliated)
  2315. (cdr affiliated)))))))))
  2316. (defun org-element-verse-block-interpreter (verse-block contents)
  2317. "Interpret VERSE-BLOCK element as Org syntax.
  2318. CONTENTS is verse block contents."
  2319. (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
  2320. ;;; Objects
  2321. ;;
  2322. ;; Unlike to elements, raw text can be found between objects. Hence,
  2323. ;; `org-element--object-lex' is provided to find the next object in
  2324. ;; buffer.
  2325. ;;
  2326. ;; Some object types (e.g., `italic') are recursive. Restrictions on
  2327. ;; object types they can contain will be specified in
  2328. ;; `org-element-object-restrictions'.
  2329. ;;
  2330. ;; Creating a new type of object requires to alter
  2331. ;; `org-element--object-regexp' and `org-element--object-lex', add the
  2332. ;; new type in `org-element-all-objects', and possibly add
  2333. ;; restrictions in `org-element-object-restrictions'.
  2334. ;;;; Bold
  2335. (defun org-element-bold-parser ()
  2336. "Parse bold object at point, if any.
  2337. When at a bold object, return a list whose car is `bold' and cdr
  2338. is a plist with `:begin', `:end', `:contents-begin' and
  2339. `:contents-end' and `:post-blank' keywords. Otherwise, return
  2340. nil.
  2341. Assume point is at the first star marker."
  2342. (save-excursion
  2343. (unless (bolp) (backward-char 1))
  2344. (when (looking-at org-emph-re)
  2345. (let ((begin (match-beginning 2))
  2346. (contents-begin (match-beginning 4))
  2347. (contents-end (match-end 4))
  2348. (post-blank (progn (goto-char (match-end 2))
  2349. (skip-chars-forward " \t")))
  2350. (end (point)))
  2351. (list 'bold
  2352. (list :begin begin
  2353. :end end
  2354. :contents-begin contents-begin
  2355. :contents-end contents-end
  2356. :post-blank post-blank))))))
  2357. (defun org-element-bold-interpreter (bold contents)
  2358. "Interpret BOLD object as Org syntax.
  2359. CONTENTS is the contents of the object."
  2360. (format "*%s*" contents))
  2361. ;;;; Code
  2362. (defun org-element-code-parser ()
  2363. "Parse code object at point, if any.
  2364. When at a code object, return a list whose car is `code' and cdr
  2365. is a plist with `:value', `:begin', `:end' and `:post-blank'
  2366. keywords. Otherwise, return nil.
  2367. Assume point is at the first tilde marker."
  2368. (save-excursion
  2369. (unless (bolp) (backward-char 1))
  2370. (when (looking-at org-emph-re)
  2371. (let ((begin (match-beginning 2))
  2372. (value (org-match-string-no-properties 4))
  2373. (post-blank (progn (goto-char (match-end 2))
  2374. (skip-chars-forward " \t")))
  2375. (end (point)))
  2376. (list 'code
  2377. (list :value value
  2378. :begin begin
  2379. :end end
  2380. :post-blank post-blank))))))
  2381. (defun org-element-code-interpreter (code contents)
  2382. "Interpret CODE object as Org syntax.
  2383. CONTENTS is nil."
  2384. (format "~%s~" (org-element-property :value code)))
  2385. ;;;; Entity
  2386. (defun org-element-entity-parser ()
  2387. "Parse entity at point, if any.
  2388. When at an entity, return a list whose car is `entity' and cdr
  2389. a plist with `:begin', `:end', `:latex', `:latex-math-p',
  2390. `:html', `:latin1', `:utf-8', `:ascii', `:use-brackets-p' and
  2391. `:post-blank' as keywords. Otherwise, return nil.
  2392. Assume point is at the beginning of the entity."
  2393. (catch 'no-object
  2394. (when (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
  2395. (save-excursion
  2396. (let* ((value (or (org-entity-get (match-string 1))
  2397. (throw 'no-object nil)))
  2398. (begin (match-beginning 0))
  2399. (bracketsp (string= (match-string 2) "{}"))
  2400. (post-blank (progn (goto-char (match-end 1))
  2401. (when bracketsp (forward-char 2))
  2402. (skip-chars-forward " \t")))
  2403. (end (point)))
  2404. (list 'entity
  2405. (list :name (car value)
  2406. :latex (nth 1 value)
  2407. :latex-math-p (nth 2 value)
  2408. :html (nth 3 value)
  2409. :ascii (nth 4 value)
  2410. :latin1 (nth 5 value)
  2411. :utf-8 (nth 6 value)
  2412. :begin begin
  2413. :end end
  2414. :use-brackets-p bracketsp
  2415. :post-blank post-blank)))))))
  2416. (defun org-element-entity-interpreter (entity contents)
  2417. "Interpret ENTITY object as Org syntax.
  2418. CONTENTS is nil."
  2419. (concat "\\"
  2420. (org-element-property :name entity)
  2421. (when (org-element-property :use-brackets-p entity) "{}")))
  2422. ;;;; Export Snippet
  2423. (defun org-element-export-snippet-parser ()
  2424. "Parse export snippet at point.
  2425. When at an export snippet, return a list whose car is
  2426. `export-snippet' and cdr a plist with `:begin', `:end',
  2427. `:back-end', `:value' and `:post-blank' as keywords. Otherwise,
  2428. return nil.
  2429. Assume point is at the beginning of the snippet."
  2430. (save-excursion
  2431. (let (contents-end)
  2432. (when (and (looking-at "@@\\([-A-Za-z0-9]+\\):")
  2433. (setq contents-end
  2434. (save-match-data (goto-char (match-end 0))
  2435. (re-search-forward "@@" nil t)
  2436. (match-beginning 0))))
  2437. (let* ((begin (match-beginning 0))
  2438. (back-end (org-match-string-no-properties 1))
  2439. (value (buffer-substring-no-properties
  2440. (match-end 0) contents-end))
  2441. (post-blank (skip-chars-forward " \t"))
  2442. (end (point)))
  2443. (list 'export-snippet
  2444. (list :back-end back-end
  2445. :value value
  2446. :begin begin
  2447. :end end
  2448. :post-blank post-blank)))))))
  2449. (defun org-element-export-snippet-interpreter (export-snippet contents)
  2450. "Interpret EXPORT-SNIPPET object as Org syntax.
  2451. CONTENTS is nil."
  2452. (format "@@%s:%s@@"
  2453. (org-element-property :back-end export-snippet)
  2454. (org-element-property :value export-snippet)))
  2455. ;;;; Footnote Reference
  2456. (defun org-element-footnote-reference-parser ()
  2457. "Parse footnote reference at point, if any.
  2458. When at a footnote reference, return a list whose car is
  2459. `footnote-reference' and cdr a plist with `:label', `:type',
  2460. `:begin', `:end', `:content-begin', `:contents-end' and
  2461. `:post-blank' as keywords. Otherwise, return nil."
  2462. (catch 'no-object
  2463. (when (looking-at org-footnote-re)
  2464. (save-excursion
  2465. (let* ((begin (point))
  2466. (label
  2467. (or (org-match-string-no-properties 2)
  2468. (org-match-string-no-properties 3)
  2469. (and (match-string 1)
  2470. (concat "fn:" (org-match-string-no-properties 1)))))
  2471. (type (if (or (not label) (match-string 1)) 'inline 'standard))
  2472. (inner-begin (match-end 0))
  2473. (inner-end
  2474. (let ((count 1))
  2475. (forward-char)
  2476. (while (and (> count 0) (re-search-forward "[][]" nil t))
  2477. (if (equal (match-string 0) "[") (incf count) (decf count)))
  2478. (unless (zerop count) (throw 'no-object nil))
  2479. (1- (point))))
  2480. (post-blank (progn (goto-char (1+ inner-end))
  2481. (skip-chars-forward " \t")))
  2482. (end (point)))
  2483. (list 'footnote-reference
  2484. (list :label label
  2485. :type type
  2486. :begin begin
  2487. :end end
  2488. :contents-begin (and (eq type 'inline) inner-begin)
  2489. :contents-end (and (eq type 'inline) inner-end)
  2490. :post-blank post-blank)))))))
  2491. (defun org-element-footnote-reference-interpreter (footnote-reference contents)
  2492. "Interpret FOOTNOTE-REFERENCE object as Org syntax.
  2493. CONTENTS is its definition, when inline, or nil."
  2494. (format "[%s]"
  2495. (concat (or (org-element-property :label footnote-reference) "fn:")
  2496. (and contents (concat ":" contents)))))
  2497. ;;;; Inline Babel Call
  2498. (defun org-element-inline-babel-call-parser ()
  2499. "Parse inline babel call at point, if any.
  2500. When at an inline babel call, return a list whose car is
  2501. `inline-babel-call' and cdr a plist with `:begin', `:end',
  2502. `:value' and `:post-blank' as keywords. Otherwise, return nil.
  2503. Assume point is at the beginning of the babel call."
  2504. (save-excursion
  2505. (unless (bolp) (backward-char))
  2506. (when (let ((case-fold-search t))
  2507. (looking-at org-babel-inline-lob-one-liner-regexp))
  2508. (let ((begin (match-end 1))
  2509. (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
  2510. (post-blank (progn (goto-char (match-end 0))
  2511. (skip-chars-forward " \t")))
  2512. (end (point)))
  2513. (list 'inline-babel-call
  2514. (list :begin begin
  2515. :end end
  2516. :value value
  2517. :post-blank post-blank))))))
  2518. (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
  2519. "Interpret INLINE-BABEL-CALL object as Org syntax.
  2520. CONTENTS is nil."
  2521. (org-element-property :value inline-babel-call))
  2522. ;;;; Inline Src Block
  2523. (defun org-element-inline-src-block-parser ()
  2524. "Parse inline source block at point, if any.
  2525. When at an inline source block, return a list whose car is
  2526. `inline-src-block' and cdr a plist with `:begin', `:end',
  2527. `:language', `:value', `:parameters' and `:post-blank' as
  2528. keywords. Otherwise, return nil.
  2529. Assume point is at the beginning of the inline src block."
  2530. (save-excursion
  2531. (unless (bolp) (backward-char))
  2532. (when (looking-at org-babel-inline-src-block-regexp)
  2533. (let ((begin (match-beginning 1))
  2534. (language (org-match-string-no-properties 2))
  2535. (parameters (org-match-string-no-properties 4))
  2536. (value (org-match-string-no-properties 5))
  2537. (post-blank (progn (goto-char (match-end 0))
  2538. (skip-chars-forward " \t")))
  2539. (end (point)))
  2540. (list 'inline-src-block
  2541. (list :language language
  2542. :value value
  2543. :parameters parameters
  2544. :begin begin
  2545. :end end
  2546. :post-blank post-blank))))))
  2547. (defun org-element-inline-src-block-interpreter (inline-src-block contents)
  2548. "Interpret INLINE-SRC-BLOCK object as Org syntax.
  2549. CONTENTS is nil."
  2550. (let ((language (org-element-property :language inline-src-block))
  2551. (arguments (org-element-property :parameters inline-src-block))
  2552. (body (org-element-property :value inline-src-block)))
  2553. (format "src_%s%s{%s}"
  2554. language
  2555. (if arguments (format "[%s]" arguments) "")
  2556. body)))
  2557. ;;;; Italic
  2558. (defun org-element-italic-parser ()
  2559. "Parse italic object at point, if any.
  2560. When at an italic object, return a list whose car is `italic' and
  2561. cdr is a plist with `:begin', `:end', `:contents-begin' and
  2562. `:contents-end' and `:post-blank' keywords. Otherwise, return
  2563. nil.
  2564. Assume point is at the first slash marker."
  2565. (save-excursion
  2566. (unless (bolp) (backward-char 1))
  2567. (when (looking-at org-emph-re)
  2568. (let ((begin (match-beginning 2))
  2569. (contents-begin (match-beginning 4))
  2570. (contents-end (match-end 4))
  2571. (post-blank (progn (goto-char (match-end 2))
  2572. (skip-chars-forward " \t")))
  2573. (end (point)))
  2574. (list 'italic
  2575. (list :begin begin
  2576. :end end
  2577. :contents-begin contents-begin
  2578. :contents-end contents-end
  2579. :post-blank post-blank))))))
  2580. (defun org-element-italic-interpreter (italic contents)
  2581. "Interpret ITALIC object as Org syntax.
  2582. CONTENTS is the contents of the object."
  2583. (format "/%s/" contents))
  2584. ;;;; Latex Fragment
  2585. (defun org-element-latex-fragment-parser ()
  2586. "Parse LaTeX fragment at point, if any.
  2587. When at a LaTeX fragment, return a list whose car is
  2588. `latex-fragment' and cdr a plist with `:value', `:begin', `:end',
  2589. and `:post-blank' as keywords. Otherwise, return nil.
  2590. Assume point is at the beginning of the LaTeX fragment."
  2591. (catch 'no-object
  2592. (save-excursion
  2593. (let* ((begin (point))
  2594. (after-fragment
  2595. (if (eq (char-after) ?$)
  2596. (if (eq (char-after (1+ (point))) ?$)
  2597. (search-forward "$$" nil t 2)
  2598. (and (not (eq (char-before) ?$))
  2599. (search-forward "$" nil t 2)
  2600. (not (memq (char-before (match-beginning 0))
  2601. '(?\s ?\t ?\n ?, ?.)))
  2602. (looking-at "\\([- \t.,?;:'\"]\\|$\\)")
  2603. (point)))
  2604. (case (char-after (1+ (point)))
  2605. (?\( (search-forward "\\)" nil t))
  2606. (?\[ (search-forward "\\]" nil t))
  2607. (otherwise
  2608. ;; Macro.
  2609. (and (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
  2610. (match-end 0))))))
  2611. (post-blank (if (not after-fragment) (throw 'no-object nil)
  2612. (goto-char after-fragment)
  2613. (skip-chars-forward " \t")))
  2614. (end (point)))
  2615. (list 'latex-fragment
  2616. (list :value (buffer-substring-no-properties begin after-fragment)
  2617. :begin begin
  2618. :end end
  2619. :post-blank post-blank))))))
  2620. (defun org-element-latex-fragment-interpreter (latex-fragment contents)
  2621. "Interpret LATEX-FRAGMENT object as Org syntax.
  2622. CONTENTS is nil."
  2623. (org-element-property :value latex-fragment))
  2624. ;;;; Line Break
  2625. (defun org-element-line-break-parser ()
  2626. "Parse line break at point, if any.
  2627. When at a line break, return a list whose car is `line-break',
  2628. and cdr a plist with `:begin', `:end' and `:post-blank' keywords.
  2629. Otherwise, return nil.
  2630. Assume point is at the beginning of the line break."
  2631. (when (and (org-looking-at-p "\\\\\\\\[ \t]*$")
  2632. (not (eq (char-before) ?\\)))
  2633. (list 'line-break
  2634. (list :begin (point)
  2635. :end (progn (forward-line) (point))
  2636. :post-blank 0))))
  2637. (defun org-element-line-break-interpreter (line-break contents)
  2638. "Interpret LINE-BREAK object as Org syntax.
  2639. CONTENTS is nil."
  2640. "\\\\\n")
  2641. ;;;; Link
  2642. (defun org-element-link-parser ()
  2643. "Parse link at point, if any.
  2644. When at a link, return a list whose car is `link' and cdr a plist
  2645. with `:type', `:path', `:raw-link', `:application',
  2646. `:search-option', `:begin', `:end', `:contents-begin',
  2647. `:contents-end' and `:post-blank' as keywords. Otherwise, return
  2648. nil.
  2649. Assume point is at the beginning of the link."
  2650. (catch 'no-object
  2651. (let ((begin (point))
  2652. end contents-begin contents-end link-end post-blank path type
  2653. raw-link link search-option application)
  2654. (cond
  2655. ;; Type 1: Text targeted from a radio target.
  2656. ((and org-target-link-regexp
  2657. (save-excursion (or (bolp) (backward-char))
  2658. (looking-at org-target-link-regexp)))
  2659. (setq type "radio"
  2660. link-end (match-end 1)
  2661. path (org-match-string-no-properties 1)
  2662. contents-begin (match-beginning 1)
  2663. contents-end (match-end 1)))
  2664. ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
  2665. ((looking-at org-bracket-link-regexp)
  2666. (setq contents-begin (match-beginning 3)
  2667. contents-end (match-end 3)
  2668. link-end (match-end 0)
  2669. ;; RAW-LINK is the original link. Expand any
  2670. ;; abbreviation in it.
  2671. raw-link (org-translate-link
  2672. (org-link-expand-abbrev
  2673. (org-match-string-no-properties 1))))
  2674. ;; Determine TYPE of link and set PATH accordingly.
  2675. (cond
  2676. ;; File type.
  2677. ((or (file-name-absolute-p raw-link)
  2678. (string-match "^\\.\\.?/" raw-link))
  2679. (setq type "file" path raw-link))
  2680. ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
  2681. ((string-match org-link-re-with-space3 raw-link)
  2682. (setq type (match-string 1 raw-link) path (match-string 2 raw-link)))
  2683. ;; Id type: PATH is the id.
  2684. ((string-match "^id:\\([-a-f0-9]+\\)" raw-link)
  2685. (setq type "id" path (match-string 1 raw-link)))
  2686. ;; Code-ref type: PATH is the name of the reference.
  2687. ((string-match "^(\\(.*\\))$" raw-link)
  2688. (setq type "coderef" path (match-string 1 raw-link)))
  2689. ;; Custom-id type: PATH is the name of the custom id.
  2690. ((= (aref raw-link 0) ?#)
  2691. (setq type "custom-id" path (substring raw-link 1)))
  2692. ;; Fuzzy type: Internal link either matches a target, an
  2693. ;; headline name or nothing. PATH is the target or
  2694. ;; headline's name.
  2695. (t (setq type "fuzzy" path raw-link))))
  2696. ;; Type 3: Plain link, e.g., http://orgmode.org
  2697. ((looking-at org-plain-link-re)
  2698. (setq raw-link (org-match-string-no-properties 0)
  2699. type (org-match-string-no-properties 1)
  2700. link-end (match-end 0)
  2701. path (org-match-string-no-properties 2)))
  2702. ;; Type 4: Angular link, e.g., <http://orgmode.org>
  2703. ((looking-at org-angle-link-re)
  2704. (setq raw-link (buffer-substring-no-properties
  2705. (match-beginning 1) (match-end 2))
  2706. type (org-match-string-no-properties 1)
  2707. link-end (match-end 0)
  2708. path (org-match-string-no-properties 2)))
  2709. (t (throw 'no-object nil)))
  2710. ;; In any case, deduce end point after trailing white space from
  2711. ;; LINK-END variable.
  2712. (save-excursion
  2713. (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
  2714. end (point))
  2715. ;; Special "file" type link processing.
  2716. (when (member type org-element-link-type-is-file)
  2717. ;; Extract opening application and search option.
  2718. (cond ((string-match "^file\\+\\(.*\\)$" type)
  2719. (setq application (match-string 1 type)))
  2720. ((not (string-match "^file" type))
  2721. (setq application type)))
  2722. (when (string-match "::\\(.*\\)\\'" path)
  2723. (setq search-option (match-string 1 path)
  2724. path (replace-match "" nil nil path)))
  2725. ;; Normalize URI.
  2726. (when (and (file-name-absolute-p path)
  2727. (not (org-string-match-p "\\`[/~]/" path)))
  2728. (setq path (concat "//" path)))
  2729. ;; Make sure TYPE always reports "file".
  2730. (setq type "file"))
  2731. (list 'link
  2732. (list :type type
  2733. :path path
  2734. :raw-link (or raw-link path)
  2735. :application application
  2736. :search-option search-option
  2737. :begin begin
  2738. :end end
  2739. :contents-begin contents-begin
  2740. :contents-end contents-end
  2741. :post-blank post-blank))))))
  2742. (defun org-element-link-interpreter (link contents)
  2743. "Interpret LINK object as Org syntax.
  2744. CONTENTS is the contents of the object, or nil."
  2745. (let ((type (org-element-property :type link))
  2746. (raw-link (org-element-property :raw-link link)))
  2747. (if (string= type "radio") raw-link
  2748. (format "[[%s]%s]"
  2749. raw-link
  2750. (if contents (format "[%s]" contents) "")))))
  2751. ;;;; Macro
  2752. (defun org-element-macro-parser ()
  2753. "Parse macro at point, if any.
  2754. When at a macro, return a list whose car is `macro' and cdr
  2755. a plist with `:key', `:args', `:begin', `:end', `:value' and
  2756. `:post-blank' as keywords. Otherwise, return nil.
  2757. Assume point is at the macro."
  2758. (save-excursion
  2759. (when (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
  2760. (let ((begin (point))
  2761. (key (downcase (org-match-string-no-properties 1)))
  2762. (value (org-match-string-no-properties 0))
  2763. (post-blank (progn (goto-char (match-end 0))
  2764. (skip-chars-forward " \t")))
  2765. (end (point))
  2766. (args (let ((args (org-match-string-no-properties 3)))
  2767. (when args
  2768. ;; Do not use `org-split-string' since empty
  2769. ;; strings are meaningful here.
  2770. (split-string
  2771. (replace-regexp-in-string
  2772. "\\(\\\\*\\)\\(,\\)"
  2773. (lambda (str)
  2774. (let ((len (length (match-string 1 str))))
  2775. (concat (make-string (/ len 2) ?\\)
  2776. (if (zerop (mod len 2)) "\000" ","))))
  2777. args nil t)
  2778. "\000")))))
  2779. (list 'macro
  2780. (list :key key
  2781. :value value
  2782. :args args
  2783. :begin begin
  2784. :end end
  2785. :post-blank post-blank))))))
  2786. (defun org-element-macro-interpreter (macro contents)
  2787. "Interpret MACRO object as Org syntax.
  2788. CONTENTS is nil."
  2789. (org-element-property :value macro))
  2790. ;;;; Radio-target
  2791. (defun org-element-radio-target-parser ()
  2792. "Parse radio target at point, if any.
  2793. When at a radio target, return a list whose car is `radio-target'
  2794. and cdr a plist with `:begin', `:end', `:contents-begin',
  2795. `:contents-end', `:value' and `:post-blank' as keywords.
  2796. Otherwise, return nil.
  2797. Assume point is at the radio target."
  2798. (save-excursion
  2799. (when (looking-at org-radio-target-regexp)
  2800. (let ((begin (point))
  2801. (contents-begin (match-beginning 1))
  2802. (contents-end (match-end 1))
  2803. (value (org-match-string-no-properties 1))
  2804. (post-blank (progn (goto-char (match-end 0))
  2805. (skip-chars-forward " \t")))
  2806. (end (point)))
  2807. (list 'radio-target
  2808. (list :begin begin
  2809. :end end
  2810. :contents-begin contents-begin
  2811. :contents-end contents-end
  2812. :post-blank post-blank
  2813. :value value))))))
  2814. (defun org-element-radio-target-interpreter (target contents)
  2815. "Interpret TARGET object as Org syntax.
  2816. CONTENTS is the contents of the object."
  2817. (concat "<<<" contents ">>>"))
  2818. ;;;; Statistics Cookie
  2819. (defun org-element-statistics-cookie-parser ()
  2820. "Parse statistics cookie at point, if any.
  2821. When at a statistics cookie, return a list whose car is
  2822. `statistics-cookie', and cdr a plist with `:begin', `:end',
  2823. `:value' and `:post-blank' keywords. Otherwise, return nil.
  2824. Assume point is at the beginning of the statistics-cookie."
  2825. (save-excursion
  2826. (when (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
  2827. (let* ((begin (point))
  2828. (value (buffer-substring-no-properties
  2829. (match-beginning 0) (match-end 0)))
  2830. (post-blank (progn (goto-char (match-end 0))
  2831. (skip-chars-forward " \t")))
  2832. (end (point)))
  2833. (list 'statistics-cookie
  2834. (list :begin begin
  2835. :end end
  2836. :value value
  2837. :post-blank post-blank))))))
  2838. (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
  2839. "Interpret STATISTICS-COOKIE object as Org syntax.
  2840. CONTENTS is nil."
  2841. (org-element-property :value statistics-cookie))
  2842. ;;;; Strike-Through
  2843. (defun org-element-strike-through-parser ()
  2844. "Parse strike-through object at point, if any.
  2845. When at a strike-through object, return a list whose car is
  2846. `strike-through' and cdr is a plist with `:begin', `:end',
  2847. `:contents-begin' and `:contents-end' and `:post-blank' keywords.
  2848. Otherwise, return nil.
  2849. Assume point is at the first plus sign marker."
  2850. (save-excursion
  2851. (unless (bolp) (backward-char 1))
  2852. (when (looking-at org-emph-re)
  2853. (let ((begin (match-beginning 2))
  2854. (contents-begin (match-beginning 4))
  2855. (contents-end (match-end 4))
  2856. (post-blank (progn (goto-char (match-end 2))
  2857. (skip-chars-forward " \t")))
  2858. (end (point)))
  2859. (list 'strike-through
  2860. (list :begin begin
  2861. :end end
  2862. :contents-begin contents-begin
  2863. :contents-end contents-end
  2864. :post-blank post-blank))))))
  2865. (defun org-element-strike-through-interpreter (strike-through contents)
  2866. "Interpret STRIKE-THROUGH object as Org syntax.
  2867. CONTENTS is the contents of the object."
  2868. (format "+%s+" contents))
  2869. ;;;; Subscript
  2870. (defun org-element-subscript-parser ()
  2871. "Parse subscript at point, if any.
  2872. When at a subscript object, return a list whose car is
  2873. `subscript' and cdr a plist with `:begin', `:end',
  2874. `:contents-begin', `:contents-end', `:use-brackets-p' and
  2875. `:post-blank' as keywords. Otherwise, return nil.
  2876. Assume point is at the underscore."
  2877. (save-excursion
  2878. (unless (bolp) (backward-char))
  2879. (when (looking-at org-match-substring-regexp)
  2880. (let ((bracketsp (match-beginning 4))
  2881. (begin (match-beginning 2))
  2882. (contents-begin (or (match-beginning 4)
  2883. (match-beginning 3)))
  2884. (contents-end (or (match-end 4) (match-end 3)))
  2885. (post-blank (progn (goto-char (match-end 0))
  2886. (skip-chars-forward " \t")))
  2887. (end (point)))
  2888. (list 'subscript
  2889. (list :begin begin
  2890. :end end
  2891. :use-brackets-p bracketsp
  2892. :contents-begin contents-begin
  2893. :contents-end contents-end
  2894. :post-blank post-blank))))))
  2895. (defun org-element-subscript-interpreter (subscript contents)
  2896. "Interpret SUBSCRIPT object as Org syntax.
  2897. CONTENTS is the contents of the object."
  2898. (format
  2899. (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
  2900. contents))
  2901. ;;;; Superscript
  2902. (defun org-element-superscript-parser ()
  2903. "Parse superscript at point, if any.
  2904. When at a superscript object, return a list whose car is
  2905. `superscript' and cdr a plist with `:begin', `:end',
  2906. `:contents-begin', `:contents-end', `:use-brackets-p' and
  2907. `:post-blank' as keywords. Otherwise, return nil.
  2908. Assume point is at the caret."
  2909. (save-excursion
  2910. (unless (bolp) (backward-char))
  2911. (when (looking-at org-match-substring-regexp)
  2912. (let ((bracketsp (match-beginning 4))
  2913. (begin (match-beginning 2))
  2914. (contents-begin (or (match-beginning 4)
  2915. (match-beginning 3)))
  2916. (contents-end (or (match-end 4) (match-end 3)))
  2917. (post-blank (progn (goto-char (match-end 0))
  2918. (skip-chars-forward " \t")))
  2919. (end (point)))
  2920. (list 'superscript
  2921. (list :begin begin
  2922. :end end
  2923. :use-brackets-p bracketsp
  2924. :contents-begin contents-begin
  2925. :contents-end contents-end
  2926. :post-blank post-blank))))))
  2927. (defun org-element-superscript-interpreter (superscript contents)
  2928. "Interpret SUPERSCRIPT object as Org syntax.
  2929. CONTENTS is the contents of the object."
  2930. (format
  2931. (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
  2932. contents))
  2933. ;;;; Table Cell
  2934. (defun org-element-table-cell-parser ()
  2935. "Parse table cell at point.
  2936. Return a list whose car is `table-cell' and cdr is a plist
  2937. containing `:begin', `:end', `:contents-begin', `:contents-end'
  2938. and `:post-blank' keywords."
  2939. (looking-at "[ \t]*\\(.*?\\)[ \t]*\\(?:|\\|$\\)")
  2940. (let* ((begin (match-beginning 0))
  2941. (end (match-end 0))
  2942. (contents-begin (match-beginning 1))
  2943. (contents-end (match-end 1)))
  2944. (list 'table-cell
  2945. (list :begin begin
  2946. :end end
  2947. :contents-begin contents-begin
  2948. :contents-end contents-end
  2949. :post-blank 0))))
  2950. (defun org-element-table-cell-interpreter (table-cell contents)
  2951. "Interpret TABLE-CELL element as Org syntax.
  2952. CONTENTS is the contents of the cell, or nil."
  2953. (concat " " contents " |"))
  2954. ;;;; Target
  2955. (defun org-element-target-parser ()
  2956. "Parse target at point, if any.
  2957. When at a target, return a list whose car is `target' and cdr
  2958. a plist with `:begin', `:end', `:value' and `:post-blank' as
  2959. keywords. Otherwise, return nil.
  2960. Assume point is at the target."
  2961. (save-excursion
  2962. (when (looking-at org-target-regexp)
  2963. (let ((begin (point))
  2964. (value (org-match-string-no-properties 1))
  2965. (post-blank (progn (goto-char (match-end 0))
  2966. (skip-chars-forward " \t")))
  2967. (end (point)))
  2968. (list 'target
  2969. (list :begin begin
  2970. :end end
  2971. :value value
  2972. :post-blank post-blank))))))
  2973. (defun org-element-target-interpreter (target contents)
  2974. "Interpret TARGET object as Org syntax.
  2975. CONTENTS is nil."
  2976. (format "<<%s>>" (org-element-property :value target)))
  2977. ;;;; Timestamp
  2978. (defconst org-element--timestamp-regexp
  2979. (concat org-ts-regexp-both
  2980. "\\|"
  2981. "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  2982. "\\|"
  2983. "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
  2984. "Regexp matching any timestamp type object.")
  2985. (defun org-element-timestamp-parser ()
  2986. "Parse time stamp at point, if any.
  2987. When at a time stamp, return a list whose car is `timestamp', and
  2988. cdr a plist with `:type', `:raw-value', `:year-start',
  2989. `:month-start', `:day-start', `:hour-start', `:minute-start',
  2990. `:year-end', `:month-end', `:day-end', `:hour-end',
  2991. `:minute-end', `:repeater-type', `:repeater-value',
  2992. `:repeater-unit', `:warning-type', `:warning-value',
  2993. `:warning-unit', `:begin', `:end' and `:post-blank' keywords.
  2994. Otherwise, return nil.
  2995. Assume point is at the beginning of the timestamp."
  2996. (when (org-looking-at-p org-element--timestamp-regexp)
  2997. (save-excursion
  2998. (let* ((begin (point))
  2999. (activep (eq (char-after) ?<))
  3000. (raw-value
  3001. (progn
  3002. (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
  3003. (match-string-no-properties 0)))
  3004. (date-start (match-string-no-properties 1))
  3005. (date-end (match-string 3))
  3006. (diaryp (match-beginning 2))
  3007. (post-blank (progn (goto-char (match-end 0))
  3008. (skip-chars-forward " \t")))
  3009. (end (point))
  3010. (time-range
  3011. (and (not diaryp)
  3012. (string-match
  3013. "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
  3014. date-start)
  3015. (cons (string-to-number (match-string 2 date-start))
  3016. (string-to-number (match-string 3 date-start)))))
  3017. (type (cond (diaryp 'diary)
  3018. ((and activep (or date-end time-range)) 'active-range)
  3019. (activep 'active)
  3020. ((or date-end time-range) 'inactive-range)
  3021. (t 'inactive)))
  3022. (repeater-props
  3023. (and (not diaryp)
  3024. (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
  3025. raw-value)
  3026. (list
  3027. :repeater-type
  3028. (let ((type (match-string 1 raw-value)))
  3029. (cond ((equal "++" type) 'catch-up)
  3030. ((equal ".+" type) 'restart)
  3031. (t 'cumulate)))
  3032. :repeater-value (string-to-number (match-string 2 raw-value))
  3033. :repeater-unit
  3034. (case (string-to-char (match-string 3 raw-value))
  3035. (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
  3036. (warning-props
  3037. (and (not diaryp)
  3038. (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value)
  3039. (list
  3040. :warning-type (if (match-string 1 raw-value) 'first 'all)
  3041. :warning-value (string-to-number (match-string 2 raw-value))
  3042. :warning-unit
  3043. (case (string-to-char (match-string 3 raw-value))
  3044. (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
  3045. year-start month-start day-start hour-start minute-start year-end
  3046. month-end day-end hour-end minute-end)
  3047. ;; Parse date-start.
  3048. (unless diaryp
  3049. (let ((date (org-parse-time-string date-start t)))
  3050. (setq year-start (nth 5 date)
  3051. month-start (nth 4 date)
  3052. day-start (nth 3 date)
  3053. hour-start (nth 2 date)
  3054. minute-start (nth 1 date))))
  3055. ;; Compute date-end. It can be provided directly in time-stamp,
  3056. ;; or extracted from time range. Otherwise, it defaults to the
  3057. ;; same values as date-start.
  3058. (unless diaryp
  3059. (let ((date (and date-end (org-parse-time-string date-end t))))
  3060. (setq year-end (or (nth 5 date) year-start)
  3061. month-end (or (nth 4 date) month-start)
  3062. day-end (or (nth 3 date) day-start)
  3063. hour-end (or (nth 2 date) (car time-range) hour-start)
  3064. minute-end (or (nth 1 date) (cdr time-range) minute-start))))
  3065. (list 'timestamp
  3066. (nconc (list :type type
  3067. :raw-value raw-value
  3068. :year-start year-start
  3069. :month-start month-start
  3070. :day-start day-start
  3071. :hour-start hour-start
  3072. :minute-start minute-start
  3073. :year-end year-end
  3074. :month-end month-end
  3075. :day-end day-end
  3076. :hour-end hour-end
  3077. :minute-end minute-end
  3078. :begin begin
  3079. :end end
  3080. :post-blank post-blank)
  3081. repeater-props
  3082. warning-props))))))
  3083. (defun org-element-timestamp-interpreter (timestamp contents)
  3084. "Interpret TIMESTAMP object as Org syntax.
  3085. CONTENTS is nil."
  3086. (let* ((repeat-string
  3087. (concat
  3088. (case (org-element-property :repeater-type timestamp)
  3089. (cumulate "+") (catch-up "++") (restart ".+"))
  3090. (let ((val (org-element-property :repeater-value timestamp)))
  3091. (and val (number-to-string val)))
  3092. (case (org-element-property :repeater-unit timestamp)
  3093. (hour "h") (day "d") (week "w") (month "m") (year "y"))))
  3094. (warning-string
  3095. (concat
  3096. (case (org-element-property :warning-type timestamp)
  3097. (first "--")
  3098. (all "-"))
  3099. (let ((val (org-element-property :warning-value timestamp)))
  3100. (and val (number-to-string val)))
  3101. (case (org-element-property :warning-unit timestamp)
  3102. (hour "h") (day "d") (week "w") (month "m") (year "y"))))
  3103. (build-ts-string
  3104. ;; Build an Org timestamp string from TIME. ACTIVEP is
  3105. ;; non-nil when time stamp is active. If WITH-TIME-P is
  3106. ;; non-nil, add a time part. HOUR-END and MINUTE-END
  3107. ;; specify a time range in the timestamp. REPEAT-STRING is
  3108. ;; the repeater string, if any.
  3109. (lambda (time activep &optional with-time-p hour-end minute-end)
  3110. (let ((ts (format-time-string
  3111. (funcall (if with-time-p 'cdr 'car)
  3112. org-time-stamp-formats)
  3113. time)))
  3114. (when (and hour-end minute-end)
  3115. (string-match "[012]?[0-9]:[0-5][0-9]" ts)
  3116. (setq ts
  3117. (replace-match
  3118. (format "\\&-%02d:%02d" hour-end minute-end)
  3119. nil nil ts)))
  3120. (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
  3121. (dolist (s (list repeat-string warning-string))
  3122. (when (org-string-nw-p s)
  3123. (setq ts (concat (substring ts 0 -1)
  3124. " "
  3125. s
  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. ;;;; Underline
  3178. (defun org-element-underline-parser ()
  3179. "Parse underline object at point, if any.
  3180. When at an underline object, return a list whose car is
  3181. `underline' and cdr is a plist with `:begin', `:end',
  3182. `:contents-begin' and `:contents-end' and `:post-blank' keywords.
  3183. Otherwise, return nil.
  3184. Assume point is at the first underscore marker."
  3185. (save-excursion
  3186. (unless (bolp) (backward-char 1))
  3187. (when (looking-at org-emph-re)
  3188. (let ((begin (match-beginning 2))
  3189. (contents-begin (match-beginning 4))
  3190. (contents-end (match-end 4))
  3191. (post-blank (progn (goto-char (match-end 2))
  3192. (skip-chars-forward " \t")))
  3193. (end (point)))
  3194. (list 'underline
  3195. (list :begin begin
  3196. :end end
  3197. :contents-begin contents-begin
  3198. :contents-end contents-end
  3199. :post-blank post-blank))))))
  3200. (defun org-element-underline-interpreter (underline contents)
  3201. "Interpret UNDERLINE object as Org syntax.
  3202. CONTENTS is the contents of the object."
  3203. (format "_%s_" contents))
  3204. ;;;; Verbatim
  3205. (defun org-element-verbatim-parser ()
  3206. "Parse verbatim object at point, if any.
  3207. When at a verbatim object, return a list whose car is `verbatim'
  3208. and cdr is a plist with `:value', `:begin', `:end' and
  3209. `:post-blank' keywords. Otherwise, return nil.
  3210. Assume point is at the first equal sign marker."
  3211. (save-excursion
  3212. (unless (bolp) (backward-char 1))
  3213. (when (looking-at org-emph-re)
  3214. (let ((begin (match-beginning 2))
  3215. (value (org-match-string-no-properties 4))
  3216. (post-blank (progn (goto-char (match-end 2))
  3217. (skip-chars-forward " \t")))
  3218. (end (point)))
  3219. (list 'verbatim
  3220. (list :value value
  3221. :begin begin
  3222. :end end
  3223. :post-blank post-blank))))))
  3224. (defun org-element-verbatim-interpreter (verbatim contents)
  3225. "Interpret VERBATIM object as Org syntax.
  3226. CONTENTS is nil."
  3227. (format "=%s=" (org-element-property :value verbatim)))
  3228. ;;; Parsing Element Starting At Point
  3229. ;;
  3230. ;; `org-element--current-element' is the core function of this section.
  3231. ;; It returns the Lisp representation of the element starting at
  3232. ;; point.
  3233. ;;
  3234. ;; `org-element--current-element' makes use of special modes. They
  3235. ;; are activated for fixed element chaining (e.g., `plain-list' >
  3236. ;; `item') or fixed conditional element chaining (e.g., `headline' >
  3237. ;; `section'). Special modes are: `first-section', `item',
  3238. ;; `node-property', `section' and `table-row'.
  3239. (defun org-element--current-element
  3240. (limit &optional granularity special structure)
  3241. "Parse the element starting at point.
  3242. Return value is a list like (TYPE PROPS) where TYPE is the type
  3243. of the element and PROPS a plist of properties associated to the
  3244. element.
  3245. Possible types are defined in `org-element-all-elements'.
  3246. LIMIT bounds the search.
  3247. Optional argument GRANULARITY determines the depth of the
  3248. recursion. Allowed values are `headline', `greater-element',
  3249. `element', `object' or nil. When it is broader than `object' (or
  3250. nil), secondary values will not be parsed, since they only
  3251. contain objects.
  3252. Optional argument SPECIAL, when non-nil, can be either
  3253. `first-section', `item', `node-property', `section', and
  3254. `table-row'.
  3255. If STRUCTURE isn't provided but SPECIAL is set to `item', it will
  3256. be computed.
  3257. This function assumes point is always at the beginning of the
  3258. element it has to parse."
  3259. (save-excursion
  3260. (let ((case-fold-search t)
  3261. ;; Determine if parsing depth allows for secondary strings
  3262. ;; parsing. It only applies to elements referenced in
  3263. ;; `org-element-secondary-value-alist'.
  3264. (raw-secondary-p (and granularity (not (eq granularity 'object)))))
  3265. (cond
  3266. ;; Item.
  3267. ((eq special 'item)
  3268. (org-element-item-parser limit structure raw-secondary-p))
  3269. ;; Table Row.
  3270. ((eq special 'table-row) (org-element-table-row-parser limit))
  3271. ;; Node Property.
  3272. ((eq special 'node-property) (org-element-node-property-parser limit))
  3273. ;; Headline.
  3274. ((org-with-limited-levels (org-at-heading-p))
  3275. (org-element-headline-parser limit raw-secondary-p))
  3276. ;; Sections (must be checked after headline).
  3277. ((eq special 'section) (org-element-section-parser limit))
  3278. ((eq special 'first-section)
  3279. (org-element-section-parser
  3280. (or (save-excursion (org-with-limited-levels (outline-next-heading)))
  3281. limit)))
  3282. ;; When not at bol, point is at the beginning of an item or
  3283. ;; a footnote definition: next item is always a paragraph.
  3284. ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
  3285. ;; Planning and Clock.
  3286. ((looking-at org-planning-or-clock-line-re)
  3287. (if (equal (match-string 1) org-clock-string)
  3288. (org-element-clock-parser limit)
  3289. (org-element-planning-parser limit)))
  3290. ;; Inlinetask.
  3291. ((org-at-heading-p)
  3292. (org-element-inlinetask-parser limit raw-secondary-p))
  3293. ;; From there, elements can have affiliated keywords.
  3294. (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
  3295. (cond
  3296. ;; Jumping over affiliated keywords put point off-limits.
  3297. ;; Parse them as regular keywords.
  3298. ((and (cdr affiliated) (>= (point) limit))
  3299. (goto-char (car affiliated))
  3300. (org-element-keyword-parser limit nil))
  3301. ;; LaTeX Environment.
  3302. ((looking-at org-element--latex-begin-environment)
  3303. (org-element-latex-environment-parser limit affiliated))
  3304. ;; Drawer and Property Drawer.
  3305. ((looking-at org-drawer-regexp)
  3306. (if (equal (match-string 1) "PROPERTIES")
  3307. (org-element-property-drawer-parser limit affiliated)
  3308. (org-element-drawer-parser limit affiliated)))
  3309. ;; Fixed Width
  3310. ((looking-at "[ \t]*:\\( \\|$\\)")
  3311. (org-element-fixed-width-parser limit affiliated))
  3312. ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
  3313. ;; Keywords.
  3314. ((looking-at "[ \t]*#")
  3315. (goto-char (match-end 0))
  3316. (cond ((looking-at "\\(?: \\|$\\)")
  3317. (beginning-of-line)
  3318. (org-element-comment-parser limit affiliated))
  3319. ((looking-at "\\+BEGIN_\\(\\S-+\\)")
  3320. (beginning-of-line)
  3321. (let ((parser (assoc (upcase (match-string 1))
  3322. org-element-block-name-alist)))
  3323. (if parser (funcall (cdr parser) limit affiliated)
  3324. (org-element-special-block-parser limit affiliated))))
  3325. ((looking-at "\\+CALL:")
  3326. (beginning-of-line)
  3327. (org-element-babel-call-parser limit affiliated))
  3328. ((looking-at "\\+BEGIN:? ")
  3329. (beginning-of-line)
  3330. (org-element-dynamic-block-parser limit affiliated))
  3331. ((looking-at "\\+\\S-+:")
  3332. (beginning-of-line)
  3333. (org-element-keyword-parser limit affiliated))
  3334. (t
  3335. (beginning-of-line)
  3336. (org-element-paragraph-parser limit affiliated))))
  3337. ;; Footnote Definition.
  3338. ((looking-at org-footnote-definition-re)
  3339. (org-element-footnote-definition-parser limit affiliated))
  3340. ;; Horizontal Rule.
  3341. ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
  3342. (org-element-horizontal-rule-parser limit affiliated))
  3343. ;; Diary Sexp.
  3344. ((looking-at "%%(")
  3345. (org-element-diary-sexp-parser limit affiliated))
  3346. ;; Table.
  3347. ((org-at-table-p t) (org-element-table-parser limit affiliated))
  3348. ;; List.
  3349. ((looking-at (org-item-re))
  3350. (org-element-plain-list-parser
  3351. limit affiliated
  3352. (or structure (org-element--list-struct limit))))
  3353. ;; Default element: Paragraph.
  3354. (t (org-element-paragraph-parser limit affiliated)))))))))
  3355. ;; Most elements can have affiliated keywords. When looking for an
  3356. ;; element beginning, we want to move before them, as they belong to
  3357. ;; that element, and, in the meantime, collect information they give
  3358. ;; into appropriate properties. Hence the following function.
  3359. (defun org-element--collect-affiliated-keywords (limit)
  3360. "Collect affiliated keywords from point down to LIMIT.
  3361. Return a list whose CAR is the position at the first of them and
  3362. CDR a plist of keywords and values and move point to the
  3363. beginning of the first line after them.
  3364. As a special case, if element doesn't start at the beginning of
  3365. the line (e.g., a paragraph starting an item), CAR is current
  3366. position of point and CDR is nil."
  3367. (if (not (bolp)) (list (point))
  3368. (let ((case-fold-search t)
  3369. (origin (point))
  3370. ;; RESTRICT is the list of objects allowed in parsed
  3371. ;; keywords value.
  3372. (restrict (org-element-restriction 'keyword))
  3373. output)
  3374. (while (and (< (point) limit) (looking-at org-element--affiliated-re))
  3375. (let* ((raw-kwd (upcase (match-string 1)))
  3376. ;; Apply translation to RAW-KWD. From there, KWD is
  3377. ;; the official keyword.
  3378. (kwd (or (cdr (assoc raw-kwd
  3379. org-element-keyword-translation-alist))
  3380. raw-kwd))
  3381. ;; Find main value for any keyword.
  3382. (value
  3383. (save-match-data
  3384. (org-trim
  3385. (buffer-substring-no-properties
  3386. (match-end 0) (point-at-eol)))))
  3387. ;; PARSEDP is non-nil when keyword should have its
  3388. ;; value parsed.
  3389. (parsedp (member kwd org-element-parsed-keywords))
  3390. ;; If KWD is a dual keyword, find its secondary
  3391. ;; value. Maybe parse it.
  3392. (dualp (member kwd org-element-dual-keywords))
  3393. (dual-value
  3394. (and dualp
  3395. (let ((sec (org-match-string-no-properties 2)))
  3396. (if (or (not sec) (not parsedp)) sec
  3397. (org-element-parse-secondary-string sec restrict)))))
  3398. ;; Attribute a property name to KWD.
  3399. (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
  3400. ;; Now set final shape for VALUE.
  3401. (when parsedp
  3402. (setq value (org-element-parse-secondary-string value restrict)))
  3403. (when dualp
  3404. (setq value (and (or value dual-value) (cons value dual-value))))
  3405. (when (or (member kwd org-element-multiple-keywords)
  3406. ;; Attributes can always appear on multiple lines.
  3407. (string-match "^ATTR_" kwd))
  3408. (setq value (cons value (plist-get output kwd-sym))))
  3409. ;; Eventually store the new value in OUTPUT.
  3410. (setq output (plist-put output kwd-sym value))
  3411. ;; Move to next keyword.
  3412. (forward-line)))
  3413. ;; If affiliated keywords are orphaned: move back to first one.
  3414. ;; They will be parsed as a paragraph.
  3415. (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
  3416. ;; Return value.
  3417. (cons origin output))))
  3418. ;;; The Org Parser
  3419. ;;
  3420. ;; The two major functions here are `org-element-parse-buffer', which
  3421. ;; parses Org syntax inside the current buffer, taking into account
  3422. ;; region, narrowing, or even visibility if specified, and
  3423. ;; `org-element-parse-secondary-string', which parses objects within
  3424. ;; a given string.
  3425. ;;
  3426. ;; The (almost) almighty `org-element-map' allows to apply a function
  3427. ;; on elements or objects matching some type, and accumulate the
  3428. ;; resulting values. In an export situation, it also skips unneeded
  3429. ;; parts of the parse tree.
  3430. (defun org-element-parse-buffer (&optional granularity visible-only)
  3431. "Recursively parse the buffer and return structure.
  3432. If narrowing is in effect, only parse the visible part of the
  3433. buffer.
  3434. Optional argument GRANULARITY determines the depth of the
  3435. recursion. It can be set to the following symbols:
  3436. `headline' Only parse headlines.
  3437. `greater-element' Don't recurse into greater elements excepted
  3438. headlines and sections. Thus, elements
  3439. parsed are the top-level ones.
  3440. `element' Parse everything but objects and plain text.
  3441. `object' Parse the complete buffer (default).
  3442. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3443. elements.
  3444. An element or an objects is represented as a list with the
  3445. pattern (TYPE PROPERTIES CONTENTS), where :
  3446. TYPE is a symbol describing the element or object. See
  3447. `org-element-all-elements' and `org-element-all-objects' for an
  3448. exhaustive list of such symbols. One can retrieve it with
  3449. `org-element-type' function.
  3450. PROPERTIES is the list of attributes attached to the element or
  3451. object, as a plist. Although most of them are specific to the
  3452. element or object type, all types share `:begin', `:end',
  3453. `:post-blank' and `:parent' properties, which respectively
  3454. refer to buffer position where the element or object starts,
  3455. ends, the number of white spaces or blank lines after it, and
  3456. the element or object containing it. Properties values can be
  3457. obtained by using `org-element-property' function.
  3458. CONTENTS is a list of elements, objects or raw strings
  3459. contained in the current element or object, when applicable.
  3460. One can access them with `org-element-contents' function.
  3461. The Org buffer has `org-data' as type and nil as properties.
  3462. `org-element-map' function can be used to find specific elements
  3463. or objects within the parse tree.
  3464. This function assumes that current major mode is `org-mode'."
  3465. (save-excursion
  3466. (goto-char (point-min))
  3467. (org-skip-whitespace)
  3468. (org-element--parse-elements
  3469. (point-at-bol) (point-max)
  3470. ;; Start in `first-section' mode so text before the first
  3471. ;; headline belongs to a section.
  3472. 'first-section nil granularity visible-only (list 'org-data nil))))
  3473. (defun org-element-parse-secondary-string (string restriction &optional parent)
  3474. "Recursively parse objects in STRING and return structure.
  3475. RESTRICTION is a symbol limiting the object types that will be
  3476. looked after.
  3477. Optional argument PARENT, when non-nil, is the element or object
  3478. containing the secondary string. It is used to set correctly
  3479. `:parent' property within the string."
  3480. (let ((local-variables (buffer-local-variables)))
  3481. (with-temp-buffer
  3482. (dolist (v local-variables)
  3483. (ignore-errors
  3484. (if (symbolp v) (makunbound v)
  3485. (org-set-local (car v) (cdr v)))))
  3486. (insert string)
  3487. (restore-buffer-modified-p nil)
  3488. (let ((secondary (org-element--parse-objects
  3489. (point-min) (point-max) nil restriction)))
  3490. (when parent
  3491. (dolist (o secondary) (org-element-put-property o :parent parent)))
  3492. secondary))))
  3493. (defun org-element-map
  3494. (data types fun &optional info first-match no-recursion with-affiliated)
  3495. "Map a function on selected elements or objects.
  3496. DATA is a parse tree, an element, an object, a string, or a list
  3497. of such constructs. TYPES is a symbol or list of symbols of
  3498. elements or objects types (see `org-element-all-elements' and
  3499. `org-element-all-objects' for a complete list of types). FUN is
  3500. the function called on the matching element or object. It has to
  3501. accept one argument: the element or object itself.
  3502. When optional argument INFO is non-nil, it should be a plist
  3503. holding export options. In that case, parts of the parse tree
  3504. not exportable according to that property list will be skipped.
  3505. When optional argument FIRST-MATCH is non-nil, stop at the first
  3506. match for which FUN doesn't return nil, and return that value.
  3507. Optional argument NO-RECURSION is a symbol or a list of symbols
  3508. representing elements or objects types. `org-element-map' won't
  3509. enter any recursive element or object whose type belongs to that
  3510. list. Though, FUN can still be applied on them.
  3511. When optional argument WITH-AFFILIATED is non-nil, FUN will also
  3512. apply to matching objects within parsed affiliated keywords (see
  3513. `org-element-parsed-keywords').
  3514. Nil values returned from FUN do not appear in the results.
  3515. Examples:
  3516. ---------
  3517. Assuming TREE is a variable containing an Org buffer parse tree,
  3518. the following example will return a flat list of all `src-block'
  3519. and `example-block' elements in it:
  3520. \(org-element-map tree '(example-block src-block) 'identity)
  3521. The following snippet will find the first headline with a level
  3522. of 1 and a \"phone\" tag, and will return its beginning position:
  3523. \(org-element-map tree 'headline
  3524. \(lambda (hl)
  3525. \(and (= (org-element-property :level hl) 1)
  3526. \(member \"phone\" (org-element-property :tags hl))
  3527. \(org-element-property :begin hl)))
  3528. nil t)
  3529. The next example will return a flat list of all `plain-list' type
  3530. elements in TREE that are not a sub-list themselves:
  3531. \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
  3532. Eventually, this example will return a flat list of all `bold'
  3533. type objects containing a `latex-snippet' type object, even
  3534. looking into captions:
  3535. \(org-element-map tree 'bold
  3536. \(lambda (b)
  3537. \(and (org-element-map b 'latex-snippet 'identity nil t) b))
  3538. nil nil nil t)"
  3539. ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
  3540. (unless (listp types) (setq types (list types)))
  3541. (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
  3542. ;; Recursion depth is determined by --CATEGORY.
  3543. (let* ((--category
  3544. (catch 'found
  3545. (let ((category 'greater-elements))
  3546. (mapc (lambda (type)
  3547. (cond ((or (memq type org-element-all-objects)
  3548. (eq type 'plain-text))
  3549. ;; If one object is found, the function
  3550. ;; has to recurse into every object.
  3551. (throw 'found 'objects))
  3552. ((not (memq type org-element-greater-elements))
  3553. ;; If one regular element is found, the
  3554. ;; function has to recurse, at least,
  3555. ;; into every element it encounters.
  3556. (and (not (eq category 'elements))
  3557. (setq category 'elements)))))
  3558. types)
  3559. category)))
  3560. ;; Compute properties for affiliated keywords if necessary.
  3561. (--affiliated-alist
  3562. (and with-affiliated
  3563. (mapcar (lambda (kwd)
  3564. (cons kwd (intern (concat ":" (downcase kwd)))))
  3565. org-element-affiliated-keywords)))
  3566. --acc
  3567. --walk-tree
  3568. (--walk-tree
  3569. (function
  3570. (lambda (--data)
  3571. ;; Recursively walk DATA. INFO, if non-nil, is a plist
  3572. ;; holding contextual information.
  3573. (let ((--type (org-element-type --data)))
  3574. (cond
  3575. ((not --data))
  3576. ;; Ignored element in an export context.
  3577. ((and info (memq --data (plist-get info :ignore-list))))
  3578. ;; List of elements or objects.
  3579. ((not --type) (mapc --walk-tree --data))
  3580. ;; Unconditionally enter parse trees.
  3581. ((eq --type 'org-data)
  3582. (mapc --walk-tree (org-element-contents --data)))
  3583. (t
  3584. ;; Check if TYPE is matching among TYPES. If so,
  3585. ;; apply FUN to --DATA and accumulate return value
  3586. ;; into --ACC (or exit if FIRST-MATCH is non-nil).
  3587. (when (memq --type types)
  3588. (let ((result (funcall fun --data)))
  3589. (cond ((not result))
  3590. (first-match (throw '--map-first-match result))
  3591. (t (push result --acc)))))
  3592. ;; If --DATA has a secondary string that can contain
  3593. ;; objects with their type among TYPES, look into it.
  3594. (when (and (eq --category 'objects) (not (stringp --data)))
  3595. (let ((sec-prop
  3596. (assq --type org-element-secondary-value-alist)))
  3597. (when sec-prop
  3598. (funcall --walk-tree
  3599. (org-element-property (cdr sec-prop) --data)))))
  3600. ;; If --DATA has any affiliated keywords and
  3601. ;; WITH-AFFILIATED is non-nil, look for objects in
  3602. ;; them.
  3603. (when (and with-affiliated
  3604. (eq --category 'objects)
  3605. (memq --type org-element-all-elements))
  3606. (mapc (lambda (kwd-pair)
  3607. (let ((kwd (car kwd-pair))
  3608. (value (org-element-property
  3609. (cdr kwd-pair) --data)))
  3610. ;; Pay attention to the type of value.
  3611. ;; Preserve order for multiple keywords.
  3612. (cond
  3613. ((not value))
  3614. ((and (member kwd org-element-multiple-keywords)
  3615. (member kwd org-element-dual-keywords))
  3616. (mapc (lambda (line)
  3617. (funcall --walk-tree (cdr line))
  3618. (funcall --walk-tree (car line)))
  3619. (reverse value)))
  3620. ((member kwd org-element-multiple-keywords)
  3621. (mapc (lambda (line) (funcall --walk-tree line))
  3622. (reverse value)))
  3623. ((member kwd org-element-dual-keywords)
  3624. (funcall --walk-tree (cdr value))
  3625. (funcall --walk-tree (car value)))
  3626. (t (funcall --walk-tree value)))))
  3627. --affiliated-alist))
  3628. ;; Determine if a recursion into --DATA is possible.
  3629. (cond
  3630. ;; --TYPE is explicitly removed from recursion.
  3631. ((memq --type no-recursion))
  3632. ;; --DATA has no contents.
  3633. ((not (org-element-contents --data)))
  3634. ;; Looking for greater elements but --DATA is simply
  3635. ;; an element or an object.
  3636. ((and (eq --category 'greater-elements)
  3637. (not (memq --type org-element-greater-elements))))
  3638. ;; Looking for elements but --DATA is an object.
  3639. ((and (eq --category 'elements)
  3640. (memq --type org-element-all-objects)))
  3641. ;; In any other case, map contents.
  3642. (t (mapc --walk-tree (org-element-contents --data)))))))))))
  3643. (catch '--map-first-match
  3644. (funcall --walk-tree data)
  3645. ;; Return value in a proper order.
  3646. (nreverse --acc))))
  3647. (put 'org-element-map 'lisp-indent-function 2)
  3648. ;; The following functions are internal parts of the parser.
  3649. ;;
  3650. ;; The first one, `org-element--parse-elements' acts at the element's
  3651. ;; level.
  3652. ;;
  3653. ;; The second one, `org-element--parse-objects' applies on all objects
  3654. ;; of a paragraph or a secondary string.
  3655. ;;
  3656. ;; More precisely, that function looks for every allowed object type
  3657. ;; first. Then, it discards failed searches, keeps further matches,
  3658. ;; and searches again types matched behind point, for subsequent
  3659. ;; calls. Thus, searching for a given type fails only once, and every
  3660. ;; object is searched only once at top level (but sometimes more for
  3661. ;; nested types).
  3662. (defun org-element--parse-elements
  3663. (beg end special structure granularity visible-only acc)
  3664. "Parse elements between BEG and END positions.
  3665. SPECIAL prioritize some elements over the others. It can be set
  3666. to `first-section', `section' `item' or `table-row'.
  3667. When value is `item', STRUCTURE will be used as the current list
  3668. structure.
  3669. GRANULARITY determines the depth of the recursion. See
  3670. `org-element-parse-buffer' for more information.
  3671. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3672. elements.
  3673. Elements are accumulated into ACC."
  3674. (save-excursion
  3675. (goto-char beg)
  3676. ;; Visible only: skip invisible parts at the beginning of the
  3677. ;; element.
  3678. (when (and visible-only (org-invisible-p2))
  3679. (goto-char (min (1+ (org-find-visible)) end)))
  3680. ;; When parsing only headlines, skip any text before first one.
  3681. (when (and (eq granularity 'headline) (not (org-at-heading-p)))
  3682. (org-with-limited-levels (outline-next-heading)))
  3683. ;; Main loop start.
  3684. (while (< (point) end)
  3685. ;; Find current element's type and parse it accordingly to
  3686. ;; its category.
  3687. (let* ((element (org-element--current-element
  3688. end granularity special structure))
  3689. (type (org-element-type element))
  3690. (cbeg (org-element-property :contents-begin element)))
  3691. (goto-char (org-element-property :end element))
  3692. ;; Visible only: skip invisible parts between siblings.
  3693. (when (and visible-only (org-invisible-p2))
  3694. (goto-char (min (1+ (org-find-visible)) end)))
  3695. ;; Fill ELEMENT contents by side-effect.
  3696. (cond
  3697. ;; If element has no contents, don't modify it.
  3698. ((not cbeg))
  3699. ;; Greater element: parse it between `contents-begin' and
  3700. ;; `contents-end'. Make sure GRANULARITY allows the
  3701. ;; recursion, or ELEMENT is a headline, in which case going
  3702. ;; inside is mandatory, in order to get sub-level headings.
  3703. ((and (memq type org-element-greater-elements)
  3704. (or (memq granularity '(element object nil))
  3705. (and (eq granularity 'greater-element)
  3706. (eq type 'section))
  3707. (eq type 'headline)))
  3708. (org-element--parse-elements
  3709. cbeg (org-element-property :contents-end element)
  3710. ;; Possibly switch to a special mode.
  3711. (case type
  3712. (headline 'section)
  3713. (plain-list 'item)
  3714. (property-drawer 'node-property)
  3715. (table 'table-row))
  3716. (and (memq type '(item plain-list))
  3717. (org-element-property :structure element))
  3718. granularity visible-only element))
  3719. ;; ELEMENT has contents. Parse objects inside, if
  3720. ;; GRANULARITY allows it.
  3721. ((memq granularity '(object nil))
  3722. (org-element--parse-objects
  3723. cbeg (org-element-property :contents-end element) element
  3724. (org-element-restriction type))))
  3725. (org-element-adopt-elements acc element)))
  3726. ;; Return result.
  3727. acc))
  3728. (defconst org-element--object-regexp
  3729. (mapconcat #'identity
  3730. (let ((link-types (regexp-opt org-link-types)))
  3731. (list
  3732. ;; Sub/superscript.
  3733. "\\(?:[_^][-{(*+.,[:alnum:]]\\)"
  3734. ;; Bold, code, italic, strike-through, underline and
  3735. ;; verbatim.
  3736. (concat "[*~=+_/]"
  3737. (format "[^%s]" (nth 2 org-emphasis-regexp-components)))
  3738. ;; Plain links.
  3739. (concat "\\<" link-types ":")
  3740. ;; Objects starting with "[": regular link, footnote
  3741. ;; reference, statistics cookie, timestamp (inactive).
  3742. "\\[\\(?:fn:\\|\\(?:[0-9]\\|\\(?:%\\|/[0-9]*\\)\\]\\)\\|\\[\\)"
  3743. ;; Objects starting with "@": export snippets.
  3744. "@@"
  3745. ;; Objects starting with "{": macro.
  3746. "{{{"
  3747. ;; Objects starting with "<" : timestamp (active,
  3748. ;; diary), target, radio target and angular links.
  3749. (concat "<\\(?:%%\\|<\\|[0-9]\\|" link-types "\\)")
  3750. ;; Objects starting with "$": latex fragment.
  3751. "\\$"
  3752. ;; Objects starting with "\": line break, entity,
  3753. ;; latex fragment.
  3754. "\\\\\\(?:[a-zA-Z[(]\\|\\\\[ \t]*$\\)"
  3755. ;; Objects starting with raw text: inline Babel
  3756. ;; source block, inline Babel call.
  3757. "\\(?:call\\|src\\)_"))
  3758. "\\|")
  3759. "Regexp possibly matching the beginning of an object.
  3760. This regexp allows false positives. Dedicated parser (e.g.,
  3761. `org-export-bold-parser') will take care of further filtering.
  3762. Radio links are not matched by this regexp, as they are treated
  3763. specially in `org-element--object-lex'.")
  3764. (defun org-element--object-lex (restriction)
  3765. "Return next object in current buffer or nil.
  3766. RESTRICTION is a list of object types, as symbols, that should be
  3767. looked after. This function assumes that the buffer is narrowed
  3768. to an appropriate container (e.g., a paragraph)."
  3769. (if (memq 'table-cell restriction) (org-element-table-cell-parser)
  3770. (save-excursion
  3771. (let ((limit (and org-target-link-regexp
  3772. (save-excursion
  3773. (or (bolp) (backward-char))
  3774. (re-search-forward org-target-link-regexp nil t))
  3775. (match-beginning 1)))
  3776. found)
  3777. (while (and (not found)
  3778. (re-search-forward org-element--object-regexp limit t))
  3779. (goto-char (match-beginning 0))
  3780. (let ((result (match-string 0)))
  3781. (setq found
  3782. (cond
  3783. ((eq (compare-strings result nil nil "call_" nil nil t) t)
  3784. (and (memq 'inline-babel-call restriction)
  3785. (org-element-inline-babel-call-parser)))
  3786. ((eq (compare-strings result nil nil "src_" nil nil t) t)
  3787. (and (memq 'inline-src-block restriction)
  3788. (org-element-inline-src-block-parser)))
  3789. (t
  3790. (case (char-after)
  3791. (?^ (and (memq 'superscript restriction)
  3792. (org-element-superscript-parser)))
  3793. (?_ (or (and (memq 'subscript restriction)
  3794. (org-element-subscript-parser))
  3795. (and (memq 'underline restriction)
  3796. (org-element-underline-parser))))
  3797. (?* (and (memq 'bold restriction)
  3798. (org-element-bold-parser)))
  3799. (?/ (and (memq 'italic restriction)
  3800. (org-element-italic-parser)))
  3801. (?~ (and (memq 'code restriction)
  3802. (org-element-code-parser)))
  3803. (?= (and (memq 'verbatim restriction)
  3804. (org-element-verbatim-parser)))
  3805. (?+ (and (memq 'strike-through restriction)
  3806. (org-element-strike-through-parser)))
  3807. (?@ (and (memq 'export-snippet restriction)
  3808. (org-element-export-snippet-parser)))
  3809. (?{ (and (memq 'macro restriction)
  3810. (org-element-macro-parser)))
  3811. (?$ (and (memq 'latex-fragment restriction)
  3812. (org-element-latex-fragment-parser)))
  3813. (?<
  3814. (if (eq (aref result 1) ?<)
  3815. (or (and (memq 'radio-target restriction)
  3816. (org-element-radio-target-parser))
  3817. (and (memq 'target restriction)
  3818. (org-element-target-parser)))
  3819. (or (and (memq 'timestamp restriction)
  3820. (org-element-timestamp-parser))
  3821. (and (memq 'link restriction)
  3822. (org-element-link-parser)))))
  3823. (?\\
  3824. (if (eq (aref result 1) ?\\)
  3825. (and (memq 'line-break restriction)
  3826. (org-element-line-break-parser))
  3827. (or (and (memq 'entity restriction)
  3828. (org-element-entity-parser))
  3829. (and (memq 'latex-fragment restriction)
  3830. (org-element-latex-fragment-parser)))))
  3831. (?\[
  3832. (if (eq (aref result 1) ?\[)
  3833. (and (memq 'link restriction)
  3834. (org-element-link-parser))
  3835. (or (and (memq 'footnote-reference restriction)
  3836. (org-element-footnote-reference-parser))
  3837. (and (memq 'timestamp restriction)
  3838. (org-element-timestamp-parser))
  3839. (and (memq 'statistics-cookie restriction)
  3840. (org-element-statistics-cookie-parser)))))
  3841. ;; This is probably a plain link.
  3842. (otherwise (and (or (memq 'link restriction)
  3843. (memq 'plain-link restriction))
  3844. (org-element-link-parser)))))))
  3845. (or (eobp) (forward-char))))
  3846. (cond (found)
  3847. ;; Radio link.
  3848. ((and limit (memq 'link restriction))
  3849. (goto-char limit) (org-element-link-parser)))))))
  3850. (defun org-element--parse-objects (beg end acc restriction)
  3851. "Parse objects between BEG and END and return recursive structure.
  3852. Objects are accumulated in ACC.
  3853. RESTRICTION is a list of object successors which are allowed in
  3854. the current object."
  3855. (save-excursion
  3856. (save-restriction
  3857. (narrow-to-region beg end)
  3858. (goto-char (point-min))
  3859. (let (next-object)
  3860. (while (and (not (eobp))
  3861. (setq next-object (org-element--object-lex restriction)))
  3862. ;; 1. Text before any object. Untabify it.
  3863. (let ((obj-beg (org-element-property :begin next-object)))
  3864. (unless (= (point) obj-beg)
  3865. (setq acc
  3866. (org-element-adopt-elements
  3867. acc
  3868. (replace-regexp-in-string
  3869. "\t" (make-string tab-width ? )
  3870. (buffer-substring-no-properties (point) obj-beg))))))
  3871. ;; 2. Object...
  3872. (let ((obj-end (org-element-property :end next-object))
  3873. (cont-beg (org-element-property :contents-begin next-object)))
  3874. ;; Fill contents of NEXT-OBJECT by side-effect, if it has
  3875. ;; a recursive type.
  3876. (when (and cont-beg
  3877. (memq (car next-object) org-element-recursive-objects))
  3878. (org-element--parse-objects
  3879. cont-beg (org-element-property :contents-end next-object)
  3880. next-object (org-element-restriction next-object)))
  3881. (setq acc (org-element-adopt-elements acc next-object))
  3882. (goto-char obj-end))))
  3883. ;; 3. Text after last object. Untabify it.
  3884. (unless (eobp)
  3885. (setq acc
  3886. (org-element-adopt-elements
  3887. acc
  3888. (replace-regexp-in-string
  3889. "\t" (make-string tab-width ? )
  3890. (buffer-substring-no-properties (point) end)))))
  3891. ;; Result.
  3892. acc)))
  3893. ;;; Towards A Bijective Process
  3894. ;;
  3895. ;; The parse tree obtained with `org-element-parse-buffer' is really
  3896. ;; a snapshot of the corresponding Org buffer. Therefore, it can be
  3897. ;; interpreted and expanded into a string with canonical Org syntax.
  3898. ;; Hence `org-element-interpret-data'.
  3899. ;;
  3900. ;; The function relies internally on
  3901. ;; `org-element--interpret-affiliated-keywords'.
  3902. ;;;###autoload
  3903. (defun org-element-interpret-data (data &optional pseudo-objects)
  3904. "Interpret DATA as Org syntax.
  3905. DATA is a parse tree, an element, an object or a secondary string
  3906. to interpret.
  3907. Optional argument PSEUDO-OBJECTS is a list of symbols defining
  3908. new types that should be treated as objects. An unknown type not
  3909. belonging to this list is seen as a pseudo-element instead. Both
  3910. pseudo-objects and pseudo-elements are transparent entities, i.e.
  3911. only their contents are interpreted.
  3912. Return Org syntax as a string."
  3913. (org-element--interpret-data-1 data nil pseudo-objects))
  3914. (defun org-element--interpret-data-1 (data parent pseudo-objects)
  3915. "Interpret DATA as Org syntax.
  3916. DATA is a parse tree, an element, an object or a secondary string
  3917. to interpret. PARENT is used for recursive calls. It contains
  3918. the element or object containing data, or nil. PSEUDO-OBJECTS
  3919. are list of symbols defining new element or object types.
  3920. Unknown types that don't belong to this list are treated as
  3921. pseudo-elements instead.
  3922. Return Org syntax as a string."
  3923. (let* ((type (org-element-type data))
  3924. ;; Find interpreter for current object or element. If it
  3925. ;; doesn't exist (e.g. this is a pseudo object or element),
  3926. ;; return contents, if any.
  3927. (interpret
  3928. (let ((fun (intern (format "org-element-%s-interpreter" type))))
  3929. (if (fboundp fun) fun (lambda (data contents) contents))))
  3930. (results
  3931. (cond
  3932. ;; Secondary string.
  3933. ((not type)
  3934. (mapconcat
  3935. (lambda (obj)
  3936. (org-element--interpret-data-1 obj parent pseudo-objects))
  3937. data ""))
  3938. ;; Full Org document.
  3939. ((eq type 'org-data)
  3940. (mapconcat
  3941. (lambda (obj)
  3942. (org-element--interpret-data-1 obj parent pseudo-objects))
  3943. (org-element-contents data) ""))
  3944. ;; Plain text: return it.
  3945. ((stringp data) data)
  3946. ;; Element or object without contents.
  3947. ((not (org-element-contents data)) (funcall interpret data nil))
  3948. ;; Element or object with contents.
  3949. (t
  3950. (funcall interpret data
  3951. ;; Recursively interpret contents.
  3952. (mapconcat
  3953. (lambda (obj)
  3954. (org-element--interpret-data-1 obj data pseudo-objects))
  3955. (org-element-contents
  3956. (if (not (memq type '(paragraph verse-block)))
  3957. data
  3958. ;; Fix indentation of elements containing
  3959. ;; objects. We ignore `table-row' elements
  3960. ;; as they are one line long anyway.
  3961. (org-element-normalize-contents
  3962. data
  3963. ;; When normalizing first paragraph of an
  3964. ;; item or a footnote-definition, ignore
  3965. ;; first line's indentation.
  3966. (and (eq type 'paragraph)
  3967. (equal data (car (org-element-contents parent)))
  3968. (memq (org-element-type parent)
  3969. '(footnote-definition item))))))
  3970. ""))))))
  3971. (if (memq type '(org-data plain-text nil)) results
  3972. ;; Build white spaces. If no `:post-blank' property is
  3973. ;; specified, assume its value is 0.
  3974. (let ((post-blank (or (org-element-property :post-blank data) 0)))
  3975. (if (or (memq type org-element-all-objects)
  3976. (memq type pseudo-objects))
  3977. (concat results (make-string post-blank ?\s))
  3978. (concat
  3979. (org-element--interpret-affiliated-keywords data)
  3980. (org-element-normalize-string results)
  3981. (make-string post-blank ?\n)))))))
  3982. (defun org-element--interpret-affiliated-keywords (element)
  3983. "Return ELEMENT's affiliated keywords as Org syntax.
  3984. If there is no affiliated keyword, return the empty string."
  3985. (let ((keyword-to-org
  3986. (function
  3987. (lambda (key value)
  3988. (let (dual)
  3989. (when (member key org-element-dual-keywords)
  3990. (setq dual (cdr value) value (car value)))
  3991. (concat "#+" key
  3992. (and dual
  3993. (format "[%s]" (org-element-interpret-data dual)))
  3994. ": "
  3995. (if (member key org-element-parsed-keywords)
  3996. (org-element-interpret-data value)
  3997. value)
  3998. "\n"))))))
  3999. (mapconcat
  4000. (lambda (prop)
  4001. (let ((value (org-element-property prop element))
  4002. (keyword (upcase (substring (symbol-name prop) 1))))
  4003. (when value
  4004. (if (or (member keyword org-element-multiple-keywords)
  4005. ;; All attribute keywords can have multiple lines.
  4006. (string-match "^ATTR_" keyword))
  4007. (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
  4008. (reverse value)
  4009. "")
  4010. (funcall keyword-to-org keyword value)))))
  4011. ;; List all ELEMENT's properties matching an attribute line or an
  4012. ;; affiliated keyword, but ignore translated keywords since they
  4013. ;; cannot belong to the property list.
  4014. (loop for prop in (nth 1 element) by 'cddr
  4015. when (let ((keyword (upcase (substring (symbol-name prop) 1))))
  4016. (or (string-match "^ATTR_" keyword)
  4017. (and
  4018. (member keyword org-element-affiliated-keywords)
  4019. (not (assoc keyword
  4020. org-element-keyword-translation-alist)))))
  4021. collect prop)
  4022. "")))
  4023. ;; Because interpretation of the parse tree must return the same
  4024. ;; number of blank lines between elements and the same number of white
  4025. ;; space after objects, some special care must be given to white
  4026. ;; spaces.
  4027. ;;
  4028. ;; The first function, `org-element-normalize-string', ensures any
  4029. ;; string different from the empty string will end with a single
  4030. ;; newline character.
  4031. ;;
  4032. ;; The second function, `org-element-normalize-contents', removes
  4033. ;; global indentation from the contents of the current element.
  4034. (defun org-element-normalize-string (s)
  4035. "Ensure string S ends with a single newline character.
  4036. If S isn't a string return it unchanged. If S is the empty
  4037. string, return it. Otherwise, return a new string with a single
  4038. newline character at its end."
  4039. (cond
  4040. ((not (stringp s)) s)
  4041. ((string= "" s) "")
  4042. (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
  4043. (replace-match "\n" nil nil s)))))
  4044. (defun org-element-normalize-contents (element &optional ignore-first)
  4045. "Normalize plain text in ELEMENT's contents.
  4046. ELEMENT must only contain plain text and objects.
  4047. If optional argument IGNORE-FIRST is non-nil, ignore first line's
  4048. indentation to compute maximal common indentation.
  4049. Return the normalized element that is element with global
  4050. indentation removed from its contents. The function assumes that
  4051. indentation is not done with TAB characters."
  4052. (let* ((min-ind most-positive-fixnum)
  4053. find-min-ind ; For byte-compiler.
  4054. (find-min-ind
  4055. (function
  4056. ;; Return minimal common indentation within BLOB. This is
  4057. ;; done by walking recursively BLOB and updating MIN-IND
  4058. ;; along the way. FIRST-FLAG is non-nil when the first
  4059. ;; string hasn't been seen yet. It is required as this
  4060. ;; string is the only one whose indentation doesn't happen
  4061. ;; after a newline character.
  4062. (lambda (blob first-flag)
  4063. (dolist (object (org-element-contents blob))
  4064. (when (and first-flag (stringp object))
  4065. (setq first-flag nil)
  4066. (string-match "\\`\\( *\\)" object)
  4067. (let ((len (length (match-string 1 object))))
  4068. ;; An indentation of zero means no string will be
  4069. ;; modified. Quit the process.
  4070. (if (zerop len) (throw 'zero (setq min-ind 0))
  4071. (setq min-ind (min len min-ind)))))
  4072. (cond
  4073. ((stringp object)
  4074. (dolist (line (delq "" (cdr (org-split-string object " *\n"))))
  4075. (setq min-ind (min (org-get-indentation line) min-ind))))
  4076. ((memq (org-element-type object) org-element-recursive-objects)
  4077. (funcall find-min-ind object first-flag))))))))
  4078. ;; Find minimal indentation in ELEMENT.
  4079. (catch 'zero (funcall find-min-ind element (not ignore-first)))
  4080. (if (or (zerop min-ind) (= min-ind most-positive-fixnum)) element
  4081. ;; Build ELEMENT back, replacing each string with the same
  4082. ;; string minus common indentation.
  4083. (let* (build ; For byte compiler.
  4084. (build
  4085. (function
  4086. (lambda (blob first-flag)
  4087. ;; Return BLOB with all its strings indentation
  4088. ;; shortened from MIN-IND white spaces. FIRST-FLAG
  4089. ;; is non-nil when the first string hasn't been seen
  4090. ;; yet.
  4091. (setcdr (cdr blob)
  4092. (mapcar
  4093. #'(lambda (object)
  4094. (when (and first-flag (stringp object))
  4095. (setq first-flag nil)
  4096. (setq object
  4097. (replace-regexp-in-string
  4098. (format "\\` \\{%d\\}" min-ind)
  4099. "" object)))
  4100. (cond
  4101. ((stringp object)
  4102. (replace-regexp-in-string
  4103. (format "\n \\{%d\\}" min-ind) "\n" object))
  4104. ((memq (org-element-type object)
  4105. org-element-recursive-objects)
  4106. (funcall build object first-flag))
  4107. (t object)))
  4108. (org-element-contents blob)))
  4109. blob))))
  4110. (funcall build element (not ignore-first))))))
  4111. ;;; Cache
  4112. ;;
  4113. ;; Implement a caching mechanism for `org-element-at-point' and
  4114. ;; `org-element-context', which see.
  4115. ;;
  4116. ;; A single public function is provided: `org-element-cache-reset'.
  4117. ;;
  4118. ;; Cache is enabled by default, but can be disabled globally with
  4119. ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
  4120. ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
  4121. ;; can be tweaked to control caching behaviour.
  4122. ;;
  4123. ;; Internally, parsed elements are stored in an AVL tree,
  4124. ;; `org-element--cache'. This tree is updated lazily: whenever
  4125. ;; a change happens to the buffer, a synchronization request is
  4126. ;; registered in `org-element--cache-sync-requests' (see
  4127. ;; `org-element--cache-submit-request'). During idle time, requests
  4128. ;; are processed by `org-element--cache-sync'. Synchronization also
  4129. ;; happens when an element is required from the cache. In this case,
  4130. ;; the process stops as soon as the needed element is up-to-date.
  4131. ;;
  4132. ;; A synchronization request can only apply on a synchronized part of
  4133. ;; the cache. Therefore, the cache is updated at least to the
  4134. ;; location where the new request applies. Thus, requests are ordered
  4135. ;; from left to right and all elements starting before the first
  4136. ;; request are correct. This property is used by functions like
  4137. ;; `org-element--cache-find' to retrieve elements in the part of the
  4138. ;; cache that can be trusted.
  4139. ;;
  4140. ;; A request applies to every element, starting from its original
  4141. ;; location (or key, see below). When a request is processed, it
  4142. ;; moves forward and may collide the next one. In this case, both
  4143. ;; requests are merged into a new one that starts from that element.
  4144. ;; As a consequence, the whole synchronization complexity does not
  4145. ;; depend on the number of pending requests, but on the number of
  4146. ;; elements the very first request will be applied on.
  4147. ;;
  4148. ;; Elements cannot be accessed through their beginning position, which
  4149. ;; may or may not be up-to-date. Instead, each element in the tree is
  4150. ;; associated to a key, obtained with `org-element--cache-key'. This
  4151. ;; mechanism is robust enough to preserve total order among elements
  4152. ;; even when the tree is only partially synchronized.
  4153. ;;
  4154. ;; Objects contained in an element are stored in a hash table,
  4155. ;; `org-element--cache-objects'.
  4156. (defvar org-element-use-cache t
  4157. "Non nil when Org parser should cache its results.
  4158. This is mostly for debugging purpose.")
  4159. (defvar org-element-cache-sync-idle-time 0.6
  4160. "Length, in seconds, of idle time before syncing cache.")
  4161. (defvar org-element-cache-sync-duration (seconds-to-time 0.04)
  4162. "Maximum duration, as a time value, for a cache synchronization.
  4163. If the synchronization is not over after this delay, the process
  4164. pauses and resumes after `org-element-cache-sync-break'
  4165. seconds.")
  4166. (defvar org-element-cache-sync-break (seconds-to-time 0.3)
  4167. "Duration, as a time value, of the pause between synchronizations.
  4168. See `org-element-cache-sync-duration' for more information.")
  4169. ;;;; Data Structure
  4170. (defvar org-element--cache nil
  4171. "AVL tree used to cache elements.
  4172. Each node of the tree contains an element. Comparison is done
  4173. with `org-element--cache-compare'. This cache is used in
  4174. `org-element-at-point'.")
  4175. (defvar org-element--cache-objects nil
  4176. "Hash table used as to cache objects.
  4177. Key is an element, as returned by `org-element-at-point', and
  4178. value is an alist where each association is:
  4179. \(PARENT COMPLETEP . OBJECTS)
  4180. where PARENT is an element or object, COMPLETEP is a boolean,
  4181. non-nil when all direct children of parent are already cached and
  4182. OBJECTS is a list of such children, as objects, from farthest to
  4183. closest.
  4184. In the following example, \\alpha, bold object and \\beta are
  4185. contained within a paragraph
  4186. \\alpha *\\beta*
  4187. If the paragraph is completely parsed, OBJECTS-DATA will be
  4188. \((PARAGRAPH t BOLD-OBJECT ENTITY-OBJECT)
  4189. \(BOLD-OBJECT t ENTITY-OBJECT))
  4190. whereas in a partially parsed paragraph, it could be
  4191. \((PARAGRAPH nil ENTITY-OBJECT))
  4192. This cache is used in `org-element-context'.")
  4193. (defvar org-element--cache-sync-requests nil
  4194. "List of pending synchronization requests.
  4195. A request is a vector with the following pattern:
  4196. \[NEXT BEG END OFFSET OUTREACH PARENT PHASE]
  4197. Processing a synchronization request consists of three phases:
  4198. 0. Delete modified elements,
  4199. 1. Fill missing area in cache,
  4200. 2. Shift positions and re-parent elements after the changes.
  4201. During phase 0, NEXT is the key of the first element to be
  4202. removed, BEG and END is buffer position delimiting the
  4203. modifications. Elements starting between them (inclusive) are
  4204. removed and so are those contained within OUTREACH. PARENT, when
  4205. non-nil, is the parent of the first element to be removed.
  4206. During phase 1, NEXT is the key of the next known element in
  4207. cache and BEG its beginning position. Parse buffer between that
  4208. element and the one before it in order to determine the parent of
  4209. the next element. Set PARENT to the element containing NEXT.
  4210. During phase 2, NEXT is the key of the next element to shift in
  4211. the parse tree. All elements starting from this one have their
  4212. properties relatives to buffer positions shifted by integer
  4213. OFFSET and, if they belong to element PARENT, are adopted by it.
  4214. PHASE specifies the phase number, as an integer.")
  4215. (defvar org-element--cache-sync-timer nil
  4216. "Timer used for cache synchronization.")
  4217. (defvar org-element--cache-sync-keys nil
  4218. "Hash table used to store keys during synchronization.
  4219. See `org-element--cache-key' for more information.")
  4220. (defsubst org-element--cache-key (element)
  4221. "Return a unique key for ELEMENT in cache tree.
  4222. Keys are used to keep a total order among elements in the cache.
  4223. Comparison is done with `org-element--cache-key-less-p'.
  4224. When no synchronization is taking place, a key is simply the
  4225. beginning position of the element, or that position plus one in
  4226. the case of an first item (respectively row) in
  4227. a list (respectively a table).
  4228. During a synchronization, the key is the one the element had when
  4229. the cache was synchronized for the last time. Elements added to
  4230. cache during the synchronization get a new key generated with
  4231. `org-element--cache-generate-key'.
  4232. Such keys are stored in `org-element--cache-sync-keys'. The hash
  4233. table is cleared once the synchronization is complete."
  4234. (or (gethash element org-element--cache-sync-keys)
  4235. (let* ((begin (org-element-property :begin element))
  4236. ;; Increase beginning position of items (respectively
  4237. ;; table rows) by one, so the first item can get
  4238. ;; a different key from its parent list (respectively
  4239. ;; table).
  4240. (key (if (memq (org-element-type element) '(item table-row))
  4241. (1+ begin)
  4242. begin)))
  4243. (if org-element--cache-sync-requests
  4244. (puthash element key org-element--cache-sync-keys)
  4245. key))))
  4246. (defun org-element--cache-generate-key (lower upper)
  4247. "Generate a key between LOWER and UPPER.
  4248. LOWER and UPPER are integers or lists, possibly empty.
  4249. If LOWER and UPPER are equals, return LOWER. Otherwise, return
  4250. a unique key, as an integer or a list of integers, according to
  4251. the following rules:
  4252. - LOWER and UPPER are compared level-wise until values differ.
  4253. - If, at a given level, LOWER and UPPER differ from more than
  4254. 2, the new key shares all the levels above with LOWER and
  4255. gets a new level. Its value is the mean between LOWER and
  4256. UPPER:
  4257. \(1 2) + (1 4) --> (1 3)
  4258. - If LOWER has no value to compare with, it is assumed that its
  4259. value is `most-negative-fixnum'. E.g.,
  4260. \(1 1) + (1 1 2)
  4261. is equivalent to
  4262. \(1 1 m) + (1 1 2)
  4263. where m is `most-negative-fixnum'. Likewise, if UPPER is
  4264. short of levels, the current value is `most-positive-fixnum'.
  4265. - If they differ from only one, the new key inherits from
  4266. current LOWER level and fork it at the next level. E.g.,
  4267. \(2 1) + (3 3)
  4268. is equivalent to
  4269. \(2 1) + (2 M)
  4270. where M is `most-positive-fixnum'.
  4271. - If the key is only one level long, it is returned as an
  4272. integer:
  4273. \(1 2) + (3 2) --> 2
  4274. When they are not equals, the function assumes that LOWER is
  4275. lesser than UPPER, per `org-element--cache-key-less-p'."
  4276. (if (equal lower upper) lower
  4277. (let ((lower (if (integerp lower) (list lower) lower))
  4278. (upper (if (integerp upper) (list upper) upper))
  4279. skip-upper key)
  4280. (catch 'exit
  4281. (while t
  4282. (let ((min (or (car lower) most-negative-fixnum))
  4283. (max (cond (skip-upper most-positive-fixnum)
  4284. ((car upper))
  4285. (t most-positive-fixnum))))
  4286. (if (< (1+ min) max)
  4287. (let ((mean (+ (ash min -1) (ash max -1) (logand min max 1))))
  4288. (throw 'exit (if key (nreverse (cons mean key)) mean)))
  4289. (when (and (< min max) (not skip-upper))
  4290. ;; When at a given level, LOWER and UPPER differ from
  4291. ;; 1, ignore UPPER altogether. Instead create a key
  4292. ;; between LOWER and the greatest key with the same
  4293. ;; prefix as LOWER so far.
  4294. (setq skip-upper t))
  4295. (push min key)
  4296. (setq lower (cdr lower) upper (cdr upper)))))))))
  4297. (defsubst org-element--cache-key-less-p (a b)
  4298. "Non-nil if key A is less than key B.
  4299. A and B are either integers or lists of integers, as returned by
  4300. `org-element--cache-key'."
  4301. (if (integerp a) (if (integerp b) (< a b) (<= a (car b)))
  4302. (if (integerp b) (< (car a) b)
  4303. (catch 'exit
  4304. (while (and a b)
  4305. (cond ((car-less-than-car a b) (throw 'exit t))
  4306. ((car-less-than-car b a) (throw 'exit nil))
  4307. (t (setq a (cdr a) b (cdr b)))))
  4308. ;; If A is empty, either keys are equal (B is also empty) and
  4309. ;; we return nil, or A is lesser than B (B is longer) and we
  4310. ;; return a non-nil value.
  4311. ;;
  4312. ;; If A is not empty, B is necessarily empty and A is greater
  4313. ;; than B (A is longer). Therefore, return nil.
  4314. (and (null a) b)))))
  4315. (defun org-element--cache-compare (a b)
  4316. "Non-nil when element A is located before element B."
  4317. (org-element--cache-key-less-p (org-element--cache-key a)
  4318. (org-element--cache-key b)))
  4319. (defsubst org-element--cache-root ()
  4320. "Return root value in cache.
  4321. This function assumes `org-element--cache' is a valid AVL tree."
  4322. (avl-tree--node-left (avl-tree--dummyroot org-element--cache)))
  4323. ;;;; Tools
  4324. (defsubst org-element--cache-active-p ()
  4325. "Non-nil when cache is active in current buffer."
  4326. (and org-element-use-cache
  4327. (or (derived-mode-p 'org-mode) orgstruct-mode)))
  4328. (defun org-element--cache-find (pos &optional side)
  4329. "Find element in cache starting at POS or before.
  4330. POS refers to a buffer position.
  4331. When optional argument SIDE is non-nil, the function checks for
  4332. elements starting at or past POS instead. If SIDE is `both', the
  4333. function returns a cons cell where car is the first element
  4334. starting at or before POS and cdr the first element starting
  4335. after POS.
  4336. The function can only find elements in the synchronized part of
  4337. the cache."
  4338. (let ((limit (and org-element--cache-sync-requests
  4339. (aref (car org-element--cache-sync-requests) 0)))
  4340. (node (org-element--cache-root))
  4341. lower upper)
  4342. (while node
  4343. (let* ((element (avl-tree--node-data node))
  4344. (begin (org-element-property :begin element)))
  4345. (cond
  4346. ((and limit
  4347. (not (org-element--cache-key-less-p
  4348. (org-element--cache-key element) limit)))
  4349. (setq node (avl-tree--node-left node)))
  4350. ((> begin pos)
  4351. (setq upper element
  4352. node (avl-tree--node-left node)))
  4353. ((< begin pos)
  4354. (setq lower element
  4355. node (avl-tree--node-right node)))
  4356. ;; We found an element in cache starting at POS. If `side'
  4357. ;; is `both' we also want the next one in order to generate
  4358. ;; a key in-between.
  4359. ;;
  4360. ;; If the element is the first row or item in a table or
  4361. ;; a plain list, we always return the table or the plain
  4362. ;; list.
  4363. ;;
  4364. ;; In any other case, we return the element found.
  4365. ((eq side 'both)
  4366. (setq lower element)
  4367. (setq node (avl-tree--node-right node)))
  4368. ((and (memq (org-element-type element) '(item table-row))
  4369. (let ((parent (org-element-property :parent element)))
  4370. (and (= (org-element-property :begin element)
  4371. (org-element-property :contents-begin parent))
  4372. (setq node nil
  4373. lower parent
  4374. upper parent)))))
  4375. (t
  4376. (setq node nil
  4377. lower element
  4378. upper element)))))
  4379. (case side
  4380. (both (cons lower upper))
  4381. ((nil) lower)
  4382. (otherwise upper))))
  4383. (defun org-element--cache-put (element &optional data)
  4384. "Store ELEMENT in current buffer's cache, if allowed.
  4385. When optional argument DATA is non-nil, assume is it object data
  4386. relative to ELEMENT and store it in the objects cache."
  4387. (cond ((not (org-element--cache-active-p)) nil)
  4388. ((not data)
  4389. (when org-element--cache-sync-requests
  4390. ;; During synchronization, first build an appropriate key
  4391. ;; for the new element so `avl-tree-enter' can insert it at
  4392. ;; the right spot in the cache.
  4393. (let ((keys (org-element--cache-find
  4394. (org-element-property :begin element) 'both)))
  4395. (puthash element
  4396. (org-element--cache-generate-key
  4397. (and (car keys) (org-element--cache-key (car keys)))
  4398. (cond ((cdr keys) (org-element--cache-key (cdr keys)))
  4399. (org-element--cache-sync-requests
  4400. (aref (car org-element--cache-sync-requests) 0))))
  4401. org-element--cache-sync-keys)))
  4402. (avl-tree-enter org-element--cache element))
  4403. ;; Headlines are not stored in cache, so objects in titles are
  4404. ;; not stored either.
  4405. ((eq (org-element-type element) 'headline) nil)
  4406. (t (puthash element data org-element--cache-objects))))
  4407. (defsubst org-element--cache-remove (element)
  4408. "Remove ELEMENT from cache.
  4409. Assume ELEMENT belongs to cache and that a cache is active."
  4410. (avl-tree-delete org-element--cache element)
  4411. (remhash element org-element--cache-objects))
  4412. ;;;; Synchronization
  4413. (defsubst org-element--cache-set-timer (buffer)
  4414. "Set idle timer for cache synchronization in BUFFER."
  4415. (when org-element--cache-sync-timer
  4416. (cancel-timer org-element--cache-sync-timer))
  4417. (setq org-element--cache-sync-timer
  4418. (run-with-idle-timer
  4419. (let ((idle (current-idle-time)))
  4420. (if idle (time-add idle org-element-cache-sync-break)
  4421. org-element-cache-sync-idle-time))
  4422. nil
  4423. #'org-element--cache-sync
  4424. buffer)))
  4425. (defsubst org-element--cache-interrupt-p (time-limit)
  4426. "Non-nil when synchronization process should be interrupted.
  4427. TIME-LIMIT is a time value or nil."
  4428. (and time-limit
  4429. (or (input-pending-p)
  4430. (time-less-p time-limit (current-time)))))
  4431. (defsubst org-element--cache-shift-positions (element offset &optional props)
  4432. "Shift ELEMENT properties relative to buffer positions by OFFSET.
  4433. Properties containing buffer positions are `:begin', `:end',
  4434. `:contents-begin', `:contents-end' and `:structure'. When
  4435. optional argument PROPS is a list of keywords, only shift
  4436. properties provided in that list.
  4437. Properties are modified by side-effect."
  4438. (let ((properties (nth 1 element)))
  4439. ;; Shift `:structure' property for the first plain list only: it
  4440. ;; is the only one that really matters and it prevents from
  4441. ;; shifting it more than once.
  4442. (when (and (or (not props) (memq :structure props))
  4443. (eq (org-element-type element) 'plain-list)
  4444. (not (eq (org-element-type (plist-get properties :parent))
  4445. 'item)))
  4446. (dolist (item (plist-get properties :structure))
  4447. (incf (car item) offset)
  4448. (incf (nth 6 item) offset)))
  4449. (dolist (key '(:begin :contents-begin :contents-end :end :post-affiliated))
  4450. (let ((value (and (or (not props) (memq key props))
  4451. (plist-get properties key))))
  4452. (and value (plist-put properties key (+ offset value)))))))
  4453. (defun org-element--cache-sync (buffer &optional threshold future-change)
  4454. "Synchronize cache with recent modification in BUFFER.
  4455. When optional argument THRESHOLD is non-nil, do the
  4456. synchronization for all elements starting before or at threshold,
  4457. then exit. Otherwise, synchronize cache for as long as
  4458. `org-element-cache-sync-duration' or until Emacs leaves idle
  4459. state.
  4460. FUTURE-CHANGE, when non-nil, is a buffer position where changes
  4461. not registered yet in the cache are going to happen. It is used
  4462. in `org-element--cache-submit-request', where cache is partially
  4463. updated before current modification are actually submitted."
  4464. (when (buffer-live-p buffer)
  4465. (with-current-buffer buffer
  4466. (let ((inhibit-quit t) request next)
  4467. (when org-element--cache-sync-timer
  4468. (cancel-timer org-element--cache-sync-timer))
  4469. (catch 'interrupt
  4470. (while org-element--cache-sync-requests
  4471. (setq request (car org-element--cache-sync-requests)
  4472. next (nth 1 org-element--cache-sync-requests))
  4473. (org-element--cache-process-request
  4474. request
  4475. (and next (aref next 0))
  4476. threshold
  4477. (and (not threshold)
  4478. (time-add (current-time)
  4479. org-element-cache-sync-duration))
  4480. future-change)
  4481. ;; Request processed. Merge current and next offsets and
  4482. ;; transfer ending position.
  4483. (when next
  4484. (incf (aref next 3) (aref request 3))
  4485. (aset next 2 (aref request 2)))
  4486. (setq org-element--cache-sync-requests
  4487. (cdr org-element--cache-sync-requests))))
  4488. ;; If more requests are awaiting, set idle timer accordingly.
  4489. ;; Otherwise, reset keys.
  4490. (if org-element--cache-sync-requests
  4491. (org-element--cache-set-timer buffer)
  4492. (clrhash org-element--cache-sync-keys))))))
  4493. (defun org-element--cache-process-request
  4494. (request next threshold time-limit future-change)
  4495. "Process synchronization REQUEST for all entries before NEXT.
  4496. REQUEST is a vector, built by `org-element--cache-submit-request'.
  4497. NEXT is a cache key, as returned by `org-element--cache-key'.
  4498. When non-nil, THRESHOLD is a buffer position. Synchronization
  4499. stops as soon as a shifted element begins after it.
  4500. When non-nil, TIME-LIMIT is a time value. Synchronization stops
  4501. after this time or when Emacs exits idle state.
  4502. When non-nil, FUTURE-CHANGE is a buffer position where changes
  4503. not registered yet in the cache are going to happen. See
  4504. `org-element--cache-submit-request' for more information.
  4505. Throw `interrupt' if the process stops before completing the
  4506. request."
  4507. (catch 'quit
  4508. (when (= (aref request 6) 0)
  4509. ;; Phase 0.
  4510. ;;
  4511. ;; Delete all elements starting after BEG, but not after buffer
  4512. ;; position END or past element with key NEXT.
  4513. ;;
  4514. ;; At each iteration, we start again at tree root since
  4515. ;; a deletion modifies structure of the balanced tree.
  4516. (catch 'end-phase
  4517. (let ((beg (aref request 0))
  4518. (end (aref request 2))
  4519. (outreach (aref request 4)))
  4520. (while t
  4521. (when (org-element--cache-interrupt-p time-limit)
  4522. (throw 'interrupt nil))
  4523. ;; Find first element in cache with key BEG or after it.
  4524. (let ((node (org-element--cache-root)) data data-key)
  4525. (while node
  4526. (let* ((element (avl-tree--node-data node))
  4527. (key (org-element--cache-key element)))
  4528. (cond
  4529. ((org-element--cache-key-less-p key beg)
  4530. (setq node (avl-tree--node-right node)))
  4531. ((org-element--cache-key-less-p beg key)
  4532. (setq data element
  4533. data-key key
  4534. node (avl-tree--node-left node)))
  4535. (t (setq data element
  4536. data-key key
  4537. node nil)))))
  4538. (if data
  4539. (let ((pos (org-element-property :begin data)))
  4540. (if (if (or (not next)
  4541. (org-element--cache-key-less-p data-key next))
  4542. (<= pos end)
  4543. (let ((up data))
  4544. (while (and up (not (eq up outreach)))
  4545. (setq up (org-element-property :parent up)))
  4546. up))
  4547. (org-element--cache-remove data)
  4548. (aset request 0 data-key)
  4549. (aset request 1 pos)
  4550. (aset request 6 1)
  4551. (throw 'end-phase nil)))
  4552. ;; No element starting after modifications left in
  4553. ;; cache: further processing is futile.
  4554. (throw 'quit t)))))))
  4555. (when (= (aref request 6) 1)
  4556. ;; Phase 1.
  4557. ;;
  4558. ;; Phase 0 left a hole in the cache. Some elements after it
  4559. ;; could have parents within. For example, in the following
  4560. ;; buffer:
  4561. ;;
  4562. ;; - item
  4563. ;;
  4564. ;;
  4565. ;; Paragraph1
  4566. ;;
  4567. ;; Paragraph2
  4568. ;;
  4569. ;; if we remove a blank line between "item" and "Paragraph1",
  4570. ;; everything down to "Paragraph2" is removed from cache. But
  4571. ;; the paragraph now belongs to the list, and its `:parent'
  4572. ;; property no longer is accurate.
  4573. ;;
  4574. ;; Therefore we need to parse again elements in the hole, or at
  4575. ;; least in its last section, so that we can re-parent
  4576. ;; subsequent elements, during phase 2.
  4577. ;;
  4578. ;; Note that we only need to get the parent from the first
  4579. ;; element in cache after the hole.
  4580. ;;
  4581. ;; When next key is lesser or equal to the current one, delegate
  4582. ;; phase 1 processing to next request in order to preserve key
  4583. ;; order among requests.
  4584. (let ((key (aref request 0)))
  4585. (when (and next (not (org-element--cache-key-less-p key next)))
  4586. (let ((next-request (nth 1 org-element--cache-sync-requests)))
  4587. (aset next-request 0 key)
  4588. (aset next-request 1 (aref request 1))
  4589. (aset next-request 6 1))
  4590. (throw 'quit t)))
  4591. ;; Next element will start at its beginning position plus
  4592. ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
  4593. ;; contains the real beginning position of the first element to
  4594. ;; shift and re-parent.
  4595. (let ((limit (+ (aref request 1) (aref request 3))))
  4596. (cond ((and threshold (> limit threshold)) (throw 'interrupt nil))
  4597. ((and future-change (>= limit future-change))
  4598. ;; Changes are going to happen around this element and
  4599. ;; they will trigger another phase 1 request. Skip the
  4600. ;; current one.
  4601. (aset request 6 2))
  4602. (t
  4603. (let ((parent (org-element--parse-to limit t time-limit)))
  4604. (aset request 5 parent)
  4605. (aset request 6 2))))))
  4606. ;; Phase 2.
  4607. ;;
  4608. ;; Shift all elements starting from key START, but before NEXT, by
  4609. ;; OFFSET, and re-parent them when appropriate.
  4610. ;;
  4611. ;; Elements are modified by side-effect so the tree structure
  4612. ;; remains intact.
  4613. ;;
  4614. ;; Once THRESHOLD, if any, is reached, or once there is an input
  4615. ;; pending, exit. Before leaving, the current synchronization
  4616. ;; request is updated.
  4617. (let ((start (aref request 0))
  4618. (offset (aref request 3))
  4619. (parent (aref request 5))
  4620. (node (org-element--cache-root))
  4621. (stack (list nil))
  4622. (leftp t)
  4623. exit-flag)
  4624. ;; No re-parenting nor shifting planned: request is over.
  4625. (when (and (not parent) (zerop offset)) (throw 'quit t))
  4626. (while node
  4627. (let* ((data (avl-tree--node-data node))
  4628. (key (org-element--cache-key data)))
  4629. (if (and leftp (avl-tree--node-left node)
  4630. (not (org-element--cache-key-less-p key start)))
  4631. (progn (push node stack)
  4632. (setq node (avl-tree--node-left node)))
  4633. (unless (org-element--cache-key-less-p key start)
  4634. ;; We reached NEXT. Request is complete.
  4635. (when (equal key next) (throw 'quit t))
  4636. ;; Handle interruption request. Update current request.
  4637. (when (or exit-flag (org-element--cache-interrupt-p time-limit))
  4638. (aset request 0 key)
  4639. (aset request 5 parent)
  4640. (throw 'interrupt nil))
  4641. ;; Shift element.
  4642. (unless (zerop offset)
  4643. (org-element--cache-shift-positions data offset)
  4644. ;; Shift associated objects data, if any.
  4645. (dolist (object-data (gethash data org-element--cache-objects))
  4646. (dolist (object (cddr object-data))
  4647. (org-element--cache-shift-positions object offset))))
  4648. (let ((begin (org-element-property :begin data)))
  4649. ;; Update PARENT and re-parent DATA, only when
  4650. ;; necessary. Propagate new structures for lists.
  4651. (while (and parent
  4652. (<= (org-element-property :end parent) begin))
  4653. (setq parent (org-element-property :parent parent)))
  4654. (cond ((and (not parent) (zerop offset)) (throw 'quit nil))
  4655. ((and parent
  4656. (let ((p (org-element-property :parent data)))
  4657. (or (not p)
  4658. (< (org-element-property :begin p)
  4659. (org-element-property :begin parent)))))
  4660. (org-element-put-property data :parent parent)
  4661. (let ((s (org-element-property :structure parent)))
  4662. (when (and s (org-element-property :structure data))
  4663. (org-element-put-property data :structure s)))))
  4664. ;; Cache is up-to-date past THRESHOLD. Request
  4665. ;; interruption.
  4666. (when (and threshold (> begin threshold)) (setq exit-flag t))))
  4667. (setq node (if (setq leftp (avl-tree--node-right node))
  4668. (avl-tree--node-right node)
  4669. (pop stack))))))
  4670. ;; We reached end of tree: synchronization complete.
  4671. t)))
  4672. (defun org-element--parse-to (pos &optional syncp time-limit)
  4673. "Parse elements in current section, down to POS.
  4674. Start parsing from the closest between the last known element in
  4675. cache or headline above. Return the smallest element containing
  4676. POS.
  4677. When optional argument SYNCP is non-nil, return the parent of the
  4678. element containing POS instead. In that case, it is also
  4679. possible to provide TIME-LIMIT, which is a time value specifying
  4680. when the parsing should stop. The function throws `interrupt' if
  4681. the process stopped before finding the expected result."
  4682. (catch 'exit
  4683. (org-with-wide-buffer
  4684. (goto-char pos)
  4685. (let* ((cached (and (org-element--cache-active-p)
  4686. (org-element--cache-find pos nil)))
  4687. (begin (org-element-property :begin cached))
  4688. element next)
  4689. (cond
  4690. ;; Nothing in cache before point: start parsing from first
  4691. ;; element following headline above, or first element in
  4692. ;; buffer.
  4693. ((not cached)
  4694. (when (org-with-limited-levels (outline-previous-heading))
  4695. (forward-line))
  4696. (skip-chars-forward " \r\t\n")
  4697. (beginning-of-line))
  4698. ;; Cache returned exact match: return it.
  4699. ((= pos begin)
  4700. (throw 'exit (if syncp (org-element-property :parent cached) cached)))
  4701. ;; There's a headline between cached value and POS: cached
  4702. ;; value is invalid. Start parsing from first element
  4703. ;; following the headline.
  4704. ((re-search-backward
  4705. (org-with-limited-levels org-outline-regexp-bol) begin t)
  4706. (forward-line)
  4707. (skip-chars-forward " \r\t\n")
  4708. (beginning-of-line))
  4709. ;; Check if CACHED or any of its ancestors contain point.
  4710. ;;
  4711. ;; If there is such an element, we inspect it in order to know
  4712. ;; if we return it or if we need to parse its contents.
  4713. ;; Otherwise, we just start parsing from current location,
  4714. ;; which is right after the top-most element containing
  4715. ;; CACHED.
  4716. ;;
  4717. ;; As a special case, if POS is at the end of the buffer, we
  4718. ;; want to return the innermost element ending there.
  4719. ;;
  4720. ;; Also, if we find an ancestor and discover that we need to
  4721. ;; parse its contents, make sure we don't start from
  4722. ;; `:contents-begin', as we would otherwise go past CACHED
  4723. ;; again. Instead, in that situation, we will resume parsing
  4724. ;; from NEXT, which is located after CACHED or its higher
  4725. ;; ancestor not containing point.
  4726. (t
  4727. (let ((up cached)
  4728. (pos (if (= (point-max) pos) (1- pos) pos)))
  4729. (goto-char (or (org-element-property :contents-begin cached) begin))
  4730. (while (let ((end (org-element-property :end up)))
  4731. (and (<= end pos)
  4732. (goto-char end)
  4733. (setq up (org-element-property :parent up)))))
  4734. (cond ((not up))
  4735. ((eobp) (setq element up))
  4736. (t (setq element up next (point)))))))
  4737. ;; Parse successively each element until we reach POS.
  4738. (let ((end (or (org-element-property :end element)
  4739. (save-excursion
  4740. (org-with-limited-levels (outline-next-heading))
  4741. (point))))
  4742. (parent element)
  4743. special-flag)
  4744. (while t
  4745. (when syncp
  4746. (cond ((= (point) pos) (throw 'exit parent))
  4747. ((org-element--cache-interrupt-p time-limit)
  4748. (throw 'interrupt nil))))
  4749. (unless element
  4750. (setq element (org-element--current-element
  4751. end 'element special-flag
  4752. (org-element-property :structure parent)))
  4753. (org-element-put-property element :parent parent)
  4754. (org-element--cache-put element))
  4755. (let ((elem-end (org-element-property :end element))
  4756. (type (org-element-type element)))
  4757. (cond
  4758. ;; Skip any element ending before point. Also skip
  4759. ;; element ending at point (unless it is also the end of
  4760. ;; buffer) since we're sure that another element begins
  4761. ;; after it.
  4762. ((and (<= elem-end pos) (/= (point-max) elem-end))
  4763. (goto-char elem-end))
  4764. ;; A non-greater element contains point: return it.
  4765. ((not (memq type org-element-greater-elements))
  4766. (throw 'exit element))
  4767. ;; Otherwise, we have to decide if ELEMENT really
  4768. ;; contains POS. In that case we start parsing from
  4769. ;; contents' beginning.
  4770. ;;
  4771. ;; If POS is at contents' beginning but it is also at
  4772. ;; the beginning of the first item in a list or a table.
  4773. ;; In that case, we need to create an anchor for that
  4774. ;; list or table, so return it.
  4775. ;;
  4776. ;; Also, if POS is at the end of the buffer, no element
  4777. ;; can start after it, but more than one may end there.
  4778. ;; Arbitrarily, we choose to return the innermost of
  4779. ;; such elements.
  4780. ((let ((cbeg (org-element-property :contents-begin element))
  4781. (cend (org-element-property :contents-end element)))
  4782. (when (or syncp
  4783. (and cbeg cend
  4784. (or (< cbeg pos)
  4785. (and (= cbeg pos)
  4786. (not (memq type '(plain-list table)))))
  4787. (or (> cend pos)
  4788. (and (= cend pos) (= (point-max) pos)))))
  4789. (goto-char (or next cbeg))
  4790. (setq next nil
  4791. special-flag (case type
  4792. (plain-list 'item)
  4793. (property-drawer 'node-property)
  4794. (table 'table-row))
  4795. parent element
  4796. end cend))))
  4797. ;; Otherwise, return ELEMENT as it is the smallest
  4798. ;; element containing POS.
  4799. (t (throw 'exit element))))
  4800. (setq element nil)))))))
  4801. ;;;; Staging Buffer Changes
  4802. (defconst org-element--cache-sensitive-re
  4803. (concat
  4804. org-outline-regexp-bol "\\|"
  4805. "^[ \t]*\\(?:"
  4806. ;; Blocks
  4807. "#\\+\\(?:BEGIN[:_]\\|END\\(?:_\\|:?[ \t]*$\\)\\)" "\\|"
  4808. ;; LaTeX environments.
  4809. "\\\\\\(?:begin{[A-Za-z0-9*]+}\\|end{[A-Za-z0-9*]+}[ \t]*$\\)" "\\|"
  4810. ;; Drawers.
  4811. ":\\(?:\\w\\|[-_]\\)+:[ \t]*$"
  4812. "\\)")
  4813. "Regexp matching a sensitive line, structure wise.
  4814. A sensitive line is a headline, inlinetask, block, drawer, or
  4815. latex-environment boundary. When such a line is modified,
  4816. structure changes in the document may propagate in the whole
  4817. section, possibly making cache invalid.")
  4818. (defvar org-element--cache-change-warning nil
  4819. "Non-nil when a sensitive line is about to be changed.
  4820. It is a symbol among nil, t and `headline'.")
  4821. (defun org-element--cache-before-change (beg end)
  4822. "Request extension of area going to be modified if needed.
  4823. BEG and END are the beginning and end of the range of changed
  4824. text. See `before-change-functions' for more information."
  4825. (when (org-element--cache-active-p)
  4826. (org-with-wide-buffer
  4827. (goto-char beg)
  4828. (beginning-of-line)
  4829. (let ((bottom (save-excursion (goto-char end) (line-end-position))))
  4830. (setq org-element--cache-change-warning
  4831. (save-match-data
  4832. (if (and (org-with-limited-levels (org-at-heading-p))
  4833. (= (line-end-position) bottom))
  4834. 'headline
  4835. (let ((case-fold-search t))
  4836. (re-search-forward
  4837. org-element--cache-sensitive-re bottom t)))))))))
  4838. (defun org-element--cache-after-change (beg end pre)
  4839. "Update buffer modifications for current buffer.
  4840. BEG and END are the beginning and end of the range of changed
  4841. text, and the length in bytes of the pre-change text replaced by
  4842. that range. See `after-change-functions' for more information."
  4843. (when (org-element--cache-active-p)
  4844. (org-with-wide-buffer
  4845. (goto-char beg)
  4846. (beginning-of-line)
  4847. (save-match-data
  4848. (let ((top (point))
  4849. (bottom (save-excursion (goto-char end) (line-end-position))))
  4850. ;; Determine if modified area needs to be extended, according
  4851. ;; to both previous and current state. We make a special
  4852. ;; case for headline editing: if a headline is modified but
  4853. ;; not removed, do not extend.
  4854. (when (case org-element--cache-change-warning
  4855. ((t) t)
  4856. (headline
  4857. (not (and (org-with-limited-levels (org-at-heading-p))
  4858. (= (line-end-position) bottom))))
  4859. (otherwise
  4860. (let ((case-fold-search t))
  4861. (re-search-forward
  4862. org-element--cache-sensitive-re bottom t))))
  4863. ;; Effectively extend modified area.
  4864. (org-with-limited-levels
  4865. (setq top (progn (goto-char top)
  4866. (when (outline-previous-heading) (forward-line))
  4867. (point)))
  4868. (setq bottom (progn (goto-char bottom)
  4869. (if (outline-next-heading) (1- (point))
  4870. (point))))))
  4871. ;; Store synchronization request.
  4872. (let ((offset (- end beg pre)))
  4873. (org-element--cache-submit-request top (- bottom offset) offset)))))
  4874. ;; Activate a timer to process the request during idle time.
  4875. (org-element--cache-set-timer (current-buffer))))
  4876. (defun org-element--cache-for-removal (beg end offset)
  4877. "Return first element to remove from cache.
  4878. BEG and END are buffer positions delimiting buffer modifications.
  4879. OFFSET is the size of the changes.
  4880. Returned element is usually the first element in cache containing
  4881. any position between BEG and END. As an exception, greater
  4882. elements around the changes that are robust to contents
  4883. modifications are preserved and updated according to the
  4884. changes."
  4885. (let* ((elements (org-element--cache-find (1- beg) 'both))
  4886. (before (car elements))
  4887. (after (cdr elements)))
  4888. (if (not before) after
  4889. (let ((up before)
  4890. (robust-flag t))
  4891. (while up
  4892. (if (and (memq (org-element-type up)
  4893. '(center-block drawer dynamic-block
  4894. property-drawer quote-block
  4895. special-block))
  4896. (<= (org-element-property :contents-begin up) beg)
  4897. (> (org-element-property :contents-end up) end))
  4898. ;; UP is a robust greater element containing changes.
  4899. ;; We only need to extend its ending boundaries.
  4900. (org-element--cache-shift-positions
  4901. up offset '(:contents-end :end))
  4902. (setq before up)
  4903. (when robust-flag (setq robust-flag nil)))
  4904. (setq up (org-element-property :parent up)))
  4905. ;; We're at top level element containing ELEMENT: if it's
  4906. ;; altered by buffer modifications, it is first element in
  4907. ;; cache to be removed. Otherwise, that first element is the
  4908. ;; following one.
  4909. ;;
  4910. ;; As a special case, do not remove BEFORE if it is a robust
  4911. ;; container for current changes.
  4912. (if (or (< (org-element-property :end before) beg) robust-flag) after
  4913. before)))))
  4914. (defun org-element--cache-submit-request (beg end offset)
  4915. "Submit a new cache synchronization request for current buffer.
  4916. BEG and END are buffer positions delimiting the minimal area
  4917. where cache data should be removed. OFFSET is the size of the
  4918. change, as an integer."
  4919. (let ((next (car org-element--cache-sync-requests))
  4920. delete-to delete-from)
  4921. (if (and next
  4922. (zerop (aref next 6))
  4923. (> (setq delete-to (+ (aref next 2) (aref next 3))) end)
  4924. (<= (setq delete-from (aref next 1)) end))
  4925. ;; Current changes can be merged with first sync request: we
  4926. ;; can save a partial cache synchronization.
  4927. (progn
  4928. (incf (aref next 3) offset)
  4929. ;; If last change happened within area to be removed, extend
  4930. ;; boundaries of robust parents, if any. Otherwise, find
  4931. ;; first element to remove and update request accordingly.
  4932. (if (> beg delete-from)
  4933. (let ((up (aref next 5)))
  4934. (while up
  4935. (org-element--cache-shift-positions
  4936. up offset '(:contents-end :end))
  4937. (setq up (org-element-property :parent up))))
  4938. (let ((first (org-element--cache-for-removal beg delete-to offset)))
  4939. (when first
  4940. (aset next 0 (org-element--cache-key first))
  4941. (aset next 1 (org-element-property :begin first))
  4942. (aset next 5 (org-element-property :parent first))))))
  4943. ;; Ensure cache is correct up to END. Also make sure that NEXT,
  4944. ;; if any, is no longer a 0-phase request, thus ensuring that
  4945. ;; phases are properly ordered. We need to provide OFFSET as
  4946. ;; optional parameter since current modifications are not known
  4947. ;; yet to the otherwise correct part of the cache (i.e, before
  4948. ;; the first request).
  4949. (when next (org-element--cache-sync (current-buffer) end beg))
  4950. (let ((first (org-element--cache-for-removal beg end offset)))
  4951. (if first
  4952. (push (let ((beg (org-element-property :begin first))
  4953. (key (org-element--cache-key first)))
  4954. (cond
  4955. ;; When changes happen before the first known
  4956. ;; element, re-parent and shift the rest of the
  4957. ;; cache.
  4958. ((> beg end) (vector key beg nil offset nil nil 1))
  4959. ;; Otherwise, we find the first non robust
  4960. ;; element containing END. All elements between
  4961. ;; FIRST and this one are to be removed.
  4962. ;;
  4963. ;; Among them, some could be located outside the
  4964. ;; synchronized part of the cache, in which case
  4965. ;; comparing buffer positions to find them is
  4966. ;; useless. Instead, we store the element
  4967. ;; containing them in the request itself. All
  4968. ;; its children will be removed.
  4969. ((let ((first-end (org-element-property :end first)))
  4970. (and (> first-end end)
  4971. (vector key beg first-end offset first
  4972. (org-element-property :parent first) 0))))
  4973. (t
  4974. (let* ((element (org-element--cache-find end))
  4975. (end (org-element-property :end element))
  4976. (up element))
  4977. (while (and (setq up (org-element-property :parent up))
  4978. (>= (org-element-property :begin up) beg))
  4979. (setq end (org-element-property :end up)
  4980. element up))
  4981. (vector key beg end offset element
  4982. (org-element-property :parent first) 0)))))
  4983. org-element--cache-sync-requests)
  4984. ;; No element to remove. No need to re-parent either.
  4985. ;; Simply shift additional elements, if any, by OFFSET.
  4986. (when org-element--cache-sync-requests
  4987. (incf (aref (car org-element--cache-sync-requests) 3) offset)))))))
  4988. ;;;; Public Functions
  4989. ;;;###autoload
  4990. (defun org-element-cache-reset (&optional all)
  4991. "Reset cache in current buffer.
  4992. When optional argument ALL is non-nil, reset cache in all Org
  4993. buffers."
  4994. (interactive "P")
  4995. (dolist (buffer (if all (buffer-list) (list (current-buffer))))
  4996. (with-current-buffer buffer
  4997. (when (org-element--cache-active-p)
  4998. (org-set-local 'org-element--cache
  4999. (avl-tree-create #'org-element--cache-compare))
  5000. (org-set-local 'org-element--cache-objects (make-hash-table :test #'eq))
  5001. (org-set-local 'org-element--cache-sync-keys
  5002. (make-hash-table :weakness 'key :test #'eq))
  5003. (org-set-local 'org-element--cache-change-warning nil)
  5004. (org-set-local 'org-element--cache-sync-requests nil)
  5005. (org-set-local 'org-element--cache-sync-timer nil)
  5006. (add-hook 'before-change-functions
  5007. #'org-element--cache-before-change nil t)
  5008. (add-hook 'after-change-functions
  5009. #'org-element--cache-after-change nil t)))))
  5010. ;;;###autoload
  5011. (defun org-element-cache-refresh (pos)
  5012. "Refresh cache at position POS."
  5013. (when (org-element--cache-active-p)
  5014. (org-element--cache-sync (current-buffer) pos)
  5015. (org-element--cache-submit-request pos pos 0)
  5016. (org-element--cache-set-timer (current-buffer))))
  5017. ;;; The Toolbox
  5018. ;;
  5019. ;; The first move is to implement a way to obtain the smallest element
  5020. ;; containing point. This is the job of `org-element-at-point'. It
  5021. ;; basically jumps back to the beginning of section containing point
  5022. ;; and proceed, one element after the other, with
  5023. ;; `org-element--current-element' until the container is found. Note:
  5024. ;; When using `org-element-at-point', secondary values are never
  5025. ;; parsed since the function focuses on elements, not on objects.
  5026. ;;
  5027. ;; At a deeper level, `org-element-context' lists all elements and
  5028. ;; objects containing point.
  5029. ;;
  5030. ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
  5031. ;; internally by navigation and manipulation tools.
  5032. ;;;###autoload
  5033. (defun org-element-at-point ()
  5034. "Determine closest element around point.
  5035. Return value is a list like (TYPE PROPS) where TYPE is the type
  5036. of the element and PROPS a plist of properties associated to the
  5037. element.
  5038. Possible types are defined in `org-element-all-elements'.
  5039. Properties depend on element or object type, but always include
  5040. `:begin', `:end', `:parent' and `:post-blank' properties.
  5041. As a special case, if point is at the very beginning of the first
  5042. item in a list or sub-list, returned element will be that list
  5043. instead of the item. Likewise, if point is at the beginning of
  5044. the first row of a table, returned element will be the table
  5045. instead of the first row.
  5046. When point is at the end of the buffer, return the innermost
  5047. element ending there."
  5048. (org-with-wide-buffer
  5049. (let ((origin (point)))
  5050. (end-of-line)
  5051. (skip-chars-backward " \r\t\n")
  5052. (cond
  5053. ;; Within blank lines at the beginning of buffer, return nil.
  5054. ((bobp) nil)
  5055. ;; Within blank lines right after a headline, return that
  5056. ;; headline.
  5057. ((org-with-limited-levels (org-at-heading-p))
  5058. (beginning-of-line)
  5059. (org-element-headline-parser (point-max) t))
  5060. ;; Otherwise parse until we find element containing ORIGIN.
  5061. (t
  5062. (when (org-element--cache-active-p)
  5063. (if (not org-element--cache) (org-element-cache-reset)
  5064. (org-element--cache-sync (current-buffer) origin)))
  5065. (org-element--parse-to origin))))))
  5066. ;;;###autoload
  5067. (defun org-element-context (&optional element)
  5068. "Return smallest element or object around point.
  5069. Return value is a list like (TYPE PROPS) where TYPE is the type
  5070. of the element or object and PROPS a plist of properties
  5071. associated to it.
  5072. Possible types are defined in `org-element-all-elements' and
  5073. `org-element-all-objects'. Properties depend on element or
  5074. object type, but always include `:begin', `:end', `:parent' and
  5075. `:post-blank'.
  5076. As a special case, if point is right after an object and not at
  5077. the beginning of any other object, return that object.
  5078. Optional argument ELEMENT, when non-nil, is the closest element
  5079. containing point, as returned by `org-element-at-point'.
  5080. Providing it allows for quicker computation."
  5081. (catch 'objects-forbidden
  5082. (org-with-wide-buffer
  5083. (let* ((pos (point))
  5084. (element (or element (org-element-at-point)))
  5085. (type (org-element-type element)))
  5086. ;; If point is inside an element containing objects or
  5087. ;; a secondary string, narrow buffer to the container and
  5088. ;; proceed with parsing. Otherwise, return ELEMENT.
  5089. (cond
  5090. ;; At a parsed affiliated keyword, check if we're inside main
  5091. ;; or dual value.
  5092. ((let ((post (org-element-property :post-affiliated element)))
  5093. (and post (< pos post)))
  5094. (beginning-of-line)
  5095. (let ((case-fold-search t)) (looking-at org-element--affiliated-re))
  5096. (cond
  5097. ((not (member-ignore-case (match-string 1)
  5098. org-element-parsed-keywords))
  5099. (throw 'objects-forbidden element))
  5100. ((< (match-end 0) pos)
  5101. (narrow-to-region (match-end 0) (line-end-position)))
  5102. ((and (match-beginning 2)
  5103. (>= pos (match-beginning 2))
  5104. (< pos (match-end 2)))
  5105. (narrow-to-region (match-beginning 2) (match-end 2)))
  5106. (t (throw 'objects-forbidden element)))
  5107. ;; Also change type to retrieve correct restrictions.
  5108. (setq type 'keyword))
  5109. ;; At an item, objects can only be located within tag, if any.
  5110. ((eq type 'item)
  5111. (let ((tag (org-element-property :tag element)))
  5112. (if (not tag) (throw 'objects-forbidden element)
  5113. (beginning-of-line)
  5114. (search-forward tag (line-end-position))
  5115. (goto-char (match-beginning 0))
  5116. (if (and (>= pos (point)) (< pos (match-end 0)))
  5117. (narrow-to-region (point) (match-end 0))
  5118. (throw 'objects-forbidden element)))))
  5119. ;; At an headline or inlinetask, objects are in title.
  5120. ((memq type '(headline inlinetask))
  5121. (goto-char (org-element-property :begin element))
  5122. (skip-chars-forward "*")
  5123. (if (and (> pos (point)) (< pos (line-end-position)))
  5124. (narrow-to-region (point) (line-end-position))
  5125. (throw 'objects-forbidden element)))
  5126. ;; At a paragraph, a table-row or a verse block, objects are
  5127. ;; located within their contents.
  5128. ((memq type '(paragraph table-row verse-block))
  5129. (let ((cbeg (org-element-property :contents-begin element))
  5130. (cend (org-element-property :contents-end element)))
  5131. ;; CBEG is nil for table rules.
  5132. (if (and cbeg cend (>= pos cbeg)
  5133. (or (< pos cend) (and (= pos cend) (eobp))))
  5134. (narrow-to-region cbeg cend)
  5135. (throw 'objects-forbidden element))))
  5136. ;; At a parsed keyword, objects are located within value.
  5137. ((eq type 'keyword)
  5138. (if (not (member (org-element-property :key element)
  5139. org-element-document-properties))
  5140. (throw 'objects-forbidden element)
  5141. (beginning-of-line)
  5142. (search-forward ":")
  5143. (if (and (>= pos (point)) (< pos (line-end-position)))
  5144. (narrow-to-region (point) (line-end-position))
  5145. (throw 'objects-forbidden element))))
  5146. ;; At a planning line, if point is at a timestamp, return it,
  5147. ;; otherwise, return element.
  5148. ((eq type 'planning)
  5149. (dolist (p '(:closed :deadline :scheduled))
  5150. (let ((timestamp (org-element-property p element)))
  5151. (when (and timestamp
  5152. (<= (org-element-property :begin timestamp) pos)
  5153. (> (org-element-property :end timestamp) pos))
  5154. (throw 'objects-forbidden timestamp))))
  5155. ;; All other locations cannot contain objects: bail out.
  5156. (throw 'objects-forbidden element))
  5157. (t (throw 'objects-forbidden element)))
  5158. (goto-char (point-min))
  5159. (let ((restriction (org-element-restriction type))
  5160. (parent element)
  5161. (cache (cond ((not (org-element--cache-active-p)) nil)
  5162. (org-element--cache-objects
  5163. (gethash element org-element--cache-objects))
  5164. (t (org-element-cache-reset) nil)))
  5165. next object-data last)
  5166. (prog1
  5167. (catch 'exit
  5168. (while t
  5169. ;; When entering PARENT for the first time, get list
  5170. ;; of objects within known so far. Store it in
  5171. ;; OBJECT-DATA.
  5172. (unless next
  5173. (let ((data (assq parent cache)))
  5174. (if data (setq object-data data)
  5175. (push (setq object-data (list parent nil)) cache))))
  5176. ;; Find NEXT object for analysis.
  5177. (catch 'found
  5178. ;; If NEXT is non-nil, we already exhausted the
  5179. ;; cache so we can parse buffer to find the object
  5180. ;; after it.
  5181. (if next (setq next (org-element--object-lex restriction))
  5182. ;; Otherwise, check if cache can help us.
  5183. (let ((objects (cddr object-data))
  5184. (completep (nth 1 object-data)))
  5185. (cond
  5186. ((and (not objects) completep) (throw 'exit parent))
  5187. ((not objects)
  5188. (setq next (org-element--object-lex restriction)))
  5189. (t
  5190. (let ((cache-limit
  5191. (org-element-property :end (car objects))))
  5192. (if (>= cache-limit pos)
  5193. ;; Cache contains the information needed.
  5194. (dolist (object objects (throw 'exit parent))
  5195. (when (<= (org-element-property :begin object)
  5196. pos)
  5197. (if (>= (org-element-property :end object)
  5198. pos)
  5199. (throw 'found (setq next object))
  5200. (throw 'exit parent))))
  5201. (goto-char cache-limit)
  5202. (setq next
  5203. (org-element--object-lex restriction))))))))
  5204. ;; If we have a new object to analyze, store it in
  5205. ;; cache. Otherwise record that there is nothing
  5206. ;; more to parse in this element at this depth.
  5207. (if next
  5208. (progn (org-element-put-property next :parent parent)
  5209. (push next (cddr object-data)))
  5210. (setcar (cdr object-data) t)))
  5211. ;; Process NEXT, if any, in order to know if we need
  5212. ;; to skip it, return it or move into it.
  5213. (if (or (not next) (> (org-element-property :begin next) pos))
  5214. (throw 'exit (or last parent))
  5215. (let ((end (org-element-property :end next))
  5216. (cbeg (org-element-property :contents-begin next))
  5217. (cend (org-element-property :contents-end next)))
  5218. (cond
  5219. ;; Skip objects ending before point. Also skip
  5220. ;; objects ending at point unless it is also the
  5221. ;; end of buffer, since we want to return the
  5222. ;; innermost object.
  5223. ((and (<= end pos) (/= (point-max) end))
  5224. (goto-char end)
  5225. ;; For convenience, when object ends at POS,
  5226. ;; without any space, store it in LAST, as we
  5227. ;; will return it if no object starts here.
  5228. (when (and (= end pos)
  5229. (not (memq (char-before) '(?\s ?\t))))
  5230. (setq last next)))
  5231. ;; If POS is within a container object, move
  5232. ;; into that object.
  5233. ((and cbeg cend
  5234. (>= pos cbeg)
  5235. (or (< pos cend)
  5236. ;; At contents' end, if there is no
  5237. ;; space before point, also move into
  5238. ;; object, for consistency with
  5239. ;; convenience feature above.
  5240. (and (= pos cend)
  5241. (or (= (point-max) pos)
  5242. (not (memq (char-before pos)
  5243. '(?\s ?\t)))))))
  5244. (goto-char cbeg)
  5245. (narrow-to-region (point) cend)
  5246. (setq parent next
  5247. restriction (org-element-restriction next)
  5248. next nil
  5249. object-data nil))
  5250. ;; Otherwise, return NEXT.
  5251. (t (throw 'exit next)))))))
  5252. ;; Store results in cache, if applicable.
  5253. (org-element--cache-put element cache)))))))
  5254. (defun org-element-nested-p (elem-A elem-B)
  5255. "Non-nil when elements ELEM-A and ELEM-B are nested."
  5256. (let ((beg-A (org-element-property :begin elem-A))
  5257. (beg-B (org-element-property :begin elem-B))
  5258. (end-A (org-element-property :end elem-A))
  5259. (end-B (org-element-property :end elem-B)))
  5260. (or (and (>= beg-A beg-B) (<= end-A end-B))
  5261. (and (>= beg-B beg-A) (<= end-B end-A)))))
  5262. (defun org-element-swap-A-B (elem-A elem-B)
  5263. "Swap elements ELEM-A and ELEM-B.
  5264. Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
  5265. end of ELEM-A."
  5266. (goto-char (org-element-property :begin elem-A))
  5267. ;; There are two special cases when an element doesn't start at bol:
  5268. ;; the first paragraph in an item or in a footnote definition.
  5269. (let ((specialp (not (bolp))))
  5270. ;; Only a paragraph without any affiliated keyword can be moved at
  5271. ;; ELEM-A position in such a situation. Note that the case of
  5272. ;; a footnote definition is impossible: it cannot contain two
  5273. ;; paragraphs in a row because it cannot contain a blank line.
  5274. (if (and specialp
  5275. (or (not (eq (org-element-type elem-B) 'paragraph))
  5276. (/= (org-element-property :begin elem-B)
  5277. (org-element-property :contents-begin elem-B))))
  5278. (error "Cannot swap elements"))
  5279. ;; In a special situation, ELEM-A will have no indentation. We'll
  5280. ;; give it ELEM-B's (which will in, in turn, have no indentation).
  5281. (let* ((ind-B (when specialp
  5282. (goto-char (org-element-property :begin elem-B))
  5283. (org-get-indentation)))
  5284. (beg-A (org-element-property :begin elem-A))
  5285. (end-A (save-excursion
  5286. (goto-char (org-element-property :end elem-A))
  5287. (skip-chars-backward " \r\t\n")
  5288. (point-at-eol)))
  5289. (beg-B (org-element-property :begin elem-B))
  5290. (end-B (save-excursion
  5291. (goto-char (org-element-property :end elem-B))
  5292. (skip-chars-backward " \r\t\n")
  5293. (point-at-eol)))
  5294. ;; Store overlays responsible for visibility status. We
  5295. ;; also need to store their boundaries as they will be
  5296. ;; removed from buffer.
  5297. (overlays
  5298. (cons
  5299. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  5300. (overlays-in beg-A end-A))
  5301. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  5302. (overlays-in beg-B end-B))))
  5303. ;; Get contents.
  5304. (body-A (buffer-substring beg-A end-A))
  5305. (body-B (delete-and-extract-region beg-B end-B)))
  5306. (goto-char beg-B)
  5307. (when specialp
  5308. (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
  5309. (org-indent-to-column ind-B))
  5310. (insert body-A)
  5311. ;; Restore ex ELEM-A overlays.
  5312. (let ((offset (- beg-B beg-A)))
  5313. (mapc (lambda (ov)
  5314. (move-overlay
  5315. (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
  5316. (car overlays))
  5317. (goto-char beg-A)
  5318. (delete-region beg-A end-A)
  5319. (insert body-B)
  5320. ;; Restore ex ELEM-B overlays.
  5321. (mapc (lambda (ov)
  5322. (move-overlay
  5323. (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
  5324. (cdr overlays)))
  5325. (goto-char (org-element-property :end elem-B)))))
  5326. (defun org-element-remove-indentation (s &optional n)
  5327. "Remove maximum common indentation in string S and return it.
  5328. When optional argument N is a positive integer, remove exactly
  5329. that much characters from indentation, if possible, or return
  5330. S as-is otherwise. Unlike to `org-remove-indentation', this
  5331. function doesn't call `untabify' on S."
  5332. (catch 'exit
  5333. (with-temp-buffer
  5334. (insert s)
  5335. (goto-char (point-min))
  5336. ;; Find maximum common indentation, if not specified.
  5337. (setq n (or n
  5338. (let ((min-ind (point-max)))
  5339. (save-excursion
  5340. (while (re-search-forward "^[ \t]*\\S-" nil t)
  5341. (let ((ind (1- (current-column))))
  5342. (if (zerop ind) (throw 'exit s)
  5343. (setq min-ind (min min-ind ind))))))
  5344. min-ind)))
  5345. (if (zerop n) s
  5346. ;; Remove exactly N indentation, but give up if not possible.
  5347. (while (not (eobp))
  5348. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  5349. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  5350. ((< ind n) (throw 'exit s))
  5351. (t (org-indent-line-to (- ind n))))
  5352. (forward-line)))
  5353. (buffer-string)))))
  5354. (provide 'org-element)
  5355. ;; Local variables:
  5356. ;; generated-autoload-file: "org-loaddefs.el"
  5357. ;; End:
  5358. ;;; org-element.el ends here