org-element.el 213 KB

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