org-element.el 199 KB

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