org-element.el 211 KB

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