org-element.el 212 KB

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