org-element.el 214 KB

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