org-element.el 212 KB

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