org-element.el 194 KB

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