org-element.el 216 KB

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