ox.el 235 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159
  1. ;;; ox.el --- Generic Export Engine for Org Mode
  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. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; This library implements a generic export engine for Org, built on
  18. ;; its syntactical parser: Org Elements.
  19. ;;
  20. ;; Besides that parser, the generic exporter is made of three distinct
  21. ;; parts:
  22. ;;
  23. ;; - The communication channel consists in a property list, which is
  24. ;; created and updated during the process. Its use is to offer
  25. ;; every piece of information, would it be about initial environment
  26. ;; or contextual data, all in a single place. The exhaustive list
  27. ;; of properties is given in "The Communication Channel" section of
  28. ;; this file.
  29. ;;
  30. ;; - The transcoder walks the parse tree, ignores or treat as plain
  31. ;; text elements and objects according to export options, and
  32. ;; eventually calls back-end specific functions to do the real
  33. ;; transcoding, concatenating their return value along the way.
  34. ;;
  35. ;; - The filter system is activated at the very beginning and the very
  36. ;; end of the export process, and each time an element or an object
  37. ;; has been converted. It is the entry point to fine-tune standard
  38. ;; output from back-end transcoders. See "The Filter System"
  39. ;; section for more information.
  40. ;;
  41. ;; The core function is `org-export-as'. It returns the transcoded
  42. ;; buffer as a string.
  43. ;;
  44. ;; An export back-end is defined with `org-export-define-backend'.
  45. ;; This function can also support specific buffer keywords, OPTION
  46. ;; keyword's items and filters. Refer to function's documentation for
  47. ;; more information.
  48. ;;
  49. ;; If the new back-end shares most properties with another one,
  50. ;; `org-export-define-derived-backend' can be used to simplify the
  51. ;; process.
  52. ;;
  53. ;; Any back-end can define its own variables. Among them, those
  54. ;; customizable should belong to the `org-export-BACKEND' group.
  55. ;;
  56. ;; Tools for common tasks across back-ends are implemented in the
  57. ;; following part of the file.
  58. ;;
  59. ;; Then, a wrapper macro for asynchronous export,
  60. ;; `org-export-async-start', along with tools to display results. are
  61. ;; given in the penultimate part.
  62. ;;
  63. ;; Eventually, a dispatcher (`org-export-dispatch') for standard
  64. ;; back-ends is provided in the last one.
  65. ;;; Code:
  66. (eval-when-compile (require 'cl))
  67. (require 'org-element)
  68. (require 'org-macro)
  69. (require 'ob-exp)
  70. (declare-function org-publish "ox-publish" (project &optional force async))
  71. (declare-function org-publish-all "ox-publish" (&optional force async))
  72. (declare-function
  73. org-publish-current-file "ox-publish" (&optional force async))
  74. (declare-function org-publish-current-project "ox-publish"
  75. (&optional force async))
  76. (defvar org-publish-project-alist)
  77. (defvar org-table-number-fraction)
  78. (defvar org-table-number-regexp)
  79. ;;; Internal Variables
  80. ;;
  81. ;; Among internal variables, the most important is
  82. ;; `org-export-options-alist'. This variable define the global export
  83. ;; options, shared between every exporter, and how they are acquired.
  84. (defconst org-export-max-depth 19
  85. "Maximum nesting depth for headlines, counting from 0.")
  86. (defconst org-export-options-alist
  87. '((:author "AUTHOR" nil user-full-name t)
  88. (:creator "CREATOR" nil org-export-creator-string)
  89. (:date "DATE" nil nil t)
  90. (:description "DESCRIPTION" nil nil newline)
  91. (:email "EMAIL" nil user-mail-address t)
  92. (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
  93. (:headline-levels nil "H" org-export-headline-levels)
  94. (:keywords "KEYWORDS" nil nil space)
  95. (:language "LANGUAGE" nil org-export-default-language t)
  96. (:preserve-breaks nil "\\n" org-export-preserve-breaks)
  97. (:section-numbers nil "num" org-export-with-section-numbers)
  98. (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
  99. (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
  100. (:title "TITLE" nil org-export--default-title space)
  101. (:with-archived-trees nil "arch" org-export-with-archived-trees)
  102. (:with-author nil "author" org-export-with-author)
  103. (:with-clocks nil "c" org-export-with-clocks)
  104. (:with-creator nil "creator" org-export-with-creator)
  105. (:with-date nil "date" org-export-with-date)
  106. (:with-drawers nil "d" org-export-with-drawers)
  107. (:with-email nil "email" org-export-with-email)
  108. (:with-emphasize nil "*" org-export-with-emphasize)
  109. (:with-entities nil "e" org-export-with-entities)
  110. (:with-fixed-width nil ":" org-export-with-fixed-width)
  111. (:with-footnotes nil "f" org-export-with-footnotes)
  112. (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
  113. (:with-latex nil "tex" org-export-with-latex)
  114. (:with-planning nil "p" org-export-with-planning)
  115. (:with-priority nil "pri" org-export-with-priority)
  116. (:with-smart-quotes nil "'" org-export-with-smart-quotes)
  117. (:with-special-strings nil "-" org-export-with-special-strings)
  118. (:with-statistics-cookies nil "stat" org-export-with-statistics-cookies)
  119. (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
  120. (:with-toc nil "toc" org-export-with-toc)
  121. (:with-tables nil "|" org-export-with-tables)
  122. (:with-tags nil "tags" org-export-with-tags)
  123. (:with-tasks nil "tasks" org-export-with-tasks)
  124. (:with-timestamps nil "<" org-export-with-timestamps)
  125. (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
  126. "Alist between export properties and ways to set them.
  127. The CAR of the alist is the property name, and the CDR is a list
  128. like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
  129. KEYWORD is a string representing a buffer keyword, or nil. Each
  130. property defined this way can also be set, during subtree
  131. export, through a headline property named after the keyword
  132. with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
  133. property).
  134. OPTION is a string that could be found in an #+OPTIONS: line.
  135. DEFAULT is the default value for the property.
  136. BEHAVIOUR determines how Org should handle multiple keywords for
  137. the same property. It is a symbol among:
  138. nil Keep old value and discard the new one.
  139. t Replace old value with the new one.
  140. `space' Concatenate the values, separating them with a space.
  141. `newline' Concatenate the values, separating them with
  142. a newline.
  143. `split' Split values at white spaces, and cons them to the
  144. previous list.
  145. Values set through KEYWORD and OPTION have precedence over
  146. DEFAULT.
  147. All these properties should be back-end agnostic. Back-end
  148. specific properties are set through `org-export-define-backend'.
  149. Properties redefined there have precedence over these.")
  150. (defconst org-export-special-keywords '("FILETAGS" "SETUPFILE" "OPTIONS")
  151. "List of in-buffer keywords that require special treatment.
  152. These keywords are not directly associated to a property. The
  153. way they are handled must be hard-coded into
  154. `org-export--get-inbuffer-options' function.")
  155. (defconst org-export-filters-alist
  156. '((:filter-bold . org-export-filter-bold-functions)
  157. (:filter-babel-call . org-export-filter-babel-call-functions)
  158. (:filter-center-block . org-export-filter-center-block-functions)
  159. (:filter-clock . org-export-filter-clock-functions)
  160. (:filter-code . org-export-filter-code-functions)
  161. (:filter-comment . org-export-filter-comment-functions)
  162. (:filter-comment-block . org-export-filter-comment-block-functions)
  163. (:filter-diary-sexp . org-export-filter-diary-sexp-functions)
  164. (:filter-drawer . org-export-filter-drawer-functions)
  165. (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
  166. (:filter-entity . org-export-filter-entity-functions)
  167. (:filter-example-block . org-export-filter-example-block-functions)
  168. (:filter-export-block . org-export-filter-export-block-functions)
  169. (:filter-export-snippet . org-export-filter-export-snippet-functions)
  170. (:filter-final-output . org-export-filter-final-output-functions)
  171. (:filter-fixed-width . org-export-filter-fixed-width-functions)
  172. (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
  173. (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
  174. (:filter-headline . org-export-filter-headline-functions)
  175. (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
  176. (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
  177. (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
  178. (:filter-inlinetask . org-export-filter-inlinetask-functions)
  179. (:filter-italic . org-export-filter-italic-functions)
  180. (:filter-item . org-export-filter-item-functions)
  181. (:filter-keyword . org-export-filter-keyword-functions)
  182. (:filter-latex-environment . org-export-filter-latex-environment-functions)
  183. (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
  184. (:filter-line-break . org-export-filter-line-break-functions)
  185. (:filter-link . org-export-filter-link-functions)
  186. (:filter-node-property . org-export-filter-node-property-functions)
  187. (:filter-options . org-export-filter-options-functions)
  188. (:filter-paragraph . org-export-filter-paragraph-functions)
  189. (:filter-parse-tree . org-export-filter-parse-tree-functions)
  190. (:filter-plain-list . org-export-filter-plain-list-functions)
  191. (:filter-plain-text . org-export-filter-plain-text-functions)
  192. (:filter-planning . org-export-filter-planning-functions)
  193. (:filter-property-drawer . org-export-filter-property-drawer-functions)
  194. (:filter-quote-block . org-export-filter-quote-block-functions)
  195. (:filter-quote-section . org-export-filter-quote-section-functions)
  196. (:filter-radio-target . org-export-filter-radio-target-functions)
  197. (:filter-section . org-export-filter-section-functions)
  198. (:filter-special-block . org-export-filter-special-block-functions)
  199. (:filter-src-block . org-export-filter-src-block-functions)
  200. (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
  201. (:filter-strike-through . org-export-filter-strike-through-functions)
  202. (:filter-subscript . org-export-filter-subscript-functions)
  203. (:filter-superscript . org-export-filter-superscript-functions)
  204. (:filter-table . org-export-filter-table-functions)
  205. (:filter-table-cell . org-export-filter-table-cell-functions)
  206. (:filter-table-row . org-export-filter-table-row-functions)
  207. (:filter-target . org-export-filter-target-functions)
  208. (:filter-timestamp . org-export-filter-timestamp-functions)
  209. (:filter-underline . org-export-filter-underline-functions)
  210. (:filter-verbatim . org-export-filter-verbatim-functions)
  211. (:filter-verse-block . org-export-filter-verse-block-functions))
  212. "Alist between filters properties and initial values.
  213. The key of each association is a property name accessible through
  214. the communication channel. Its value is a configurable global
  215. variable defining initial filters.
  216. This list is meant to install user specified filters. Back-end
  217. developers may install their own filters using
  218. `org-export-define-backend'. Filters defined there will always
  219. be prepended to the current list, so they always get applied
  220. first.")
  221. (defconst org-export-default-inline-image-rule
  222. `(("file" .
  223. ,(format "\\.%s\\'"
  224. (regexp-opt
  225. '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
  226. "xpm" "pbm" "pgm" "ppm") t))))
  227. "Default rule for link matching an inline image.
  228. This rule applies to links with no description. By default, it
  229. will be considered as an inline image if it targets a local file
  230. whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
  231. \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
  232. See `org-export-inline-image-p' for more information about
  233. rules.")
  234. (defvar org-export-async-debug nil
  235. "Non-nil means asynchronous export process should leave data behind.
  236. This data is found in the appropriate \"*Org Export Process*\"
  237. buffer, and in files prefixed with \"org-export-process\" and
  238. located in `temporary-file-directory'.
  239. When non-nil, it will also set `debug-on-error' to a non-nil
  240. value in the external process.")
  241. (defvar org-export-stack-contents nil
  242. "Record asynchronously generated export results and processes.
  243. This is an alist: its CAR is the source of the
  244. result (destination file or buffer for a finished process,
  245. original buffer for a running one) and its CDR is a list
  246. containing the back-end used, as a symbol, and either a process
  247. or the time at which it finished. It is used to build the menu
  248. from `org-export-stack'.")
  249. (defvar org-export--registered-backends nil
  250. "List of backends currently available in the exporter.
  251. This variable is set with `org-export-define-backend' and
  252. `org-export-define-derived-backend' functions.")
  253. (defvar org-export-dispatch-last-action nil
  254. "Last command called from the dispatcher.
  255. The value should be a list. Its CAR is the action, as a symbol,
  256. and its CDR is a list of export options.")
  257. (defvar org-export-dispatch-last-position (make-marker)
  258. "The position where the last export command was created using the dispatcher.
  259. This marker will be used with `C-u C-c C-e' to make sure export repetition
  260. uses the same subtree if the previous command was restricted to a subtree.")
  261. ;; For compatibility with Org < 8
  262. (defvar org-export-current-backend nil
  263. "Name, if any, of the back-end used during an export process.
  264. Its value is a symbol such as `html', `latex', `ascii', or nil if
  265. the back-end is anonymous (see `org-export-create-backend') or if
  266. there is no export process in progress.
  267. It can be used to teach Babel blocks how to act differently
  268. according to the back-end used.")
  269. ;;; User-configurable Variables
  270. ;;
  271. ;; Configuration for the masses.
  272. ;;
  273. ;; They should never be accessed directly, as their value is to be
  274. ;; stored in a property list (cf. `org-export-options-alist').
  275. ;; Back-ends will read their value from there instead.
  276. (defgroup org-export nil
  277. "Options for exporting Org mode files."
  278. :tag "Org Export"
  279. :group 'org)
  280. (defgroup org-export-general nil
  281. "General options for export engine."
  282. :tag "Org Export General"
  283. :group 'org-export)
  284. (defcustom org-export-with-archived-trees 'headline
  285. "Whether sub-trees with the ARCHIVE tag should be exported.
  286. This can have three different values:
  287. nil Do not export, pretend this tree is not present.
  288. t Do export the entire tree.
  289. `headline' Only export the headline, but skip the tree below it.
  290. This option can also be set with the OPTIONS keyword,
  291. e.g. \"arch:nil\"."
  292. :group 'org-export-general
  293. :type '(choice
  294. (const :tag "Not at all" nil)
  295. (const :tag "Headline only" headline)
  296. (const :tag "Entirely" t)))
  297. (defcustom org-export-with-author t
  298. "Non-nil means insert author name into the exported file.
  299. This option can also be set with the OPTIONS keyword,
  300. e.g. \"author:nil\"."
  301. :group 'org-export-general
  302. :type 'boolean)
  303. (defcustom org-export-with-clocks nil
  304. "Non-nil means export CLOCK keywords.
  305. This option can also be set with the OPTIONS keyword,
  306. e.g. \"c:t\"."
  307. :group 'org-export-general
  308. :type 'boolean)
  309. (defcustom org-export-with-creator 'comment
  310. "Non-nil means the postamble should contain a creator sentence.
  311. The sentence can be set in `org-export-creator-string' and
  312. defaults to \"Generated by Org mode XX in Emacs XXX.\".
  313. If the value is `comment' insert it as a comment."
  314. :group 'org-export-general
  315. :type '(choice
  316. (const :tag "No creator sentence" nil)
  317. (const :tag "Sentence as a comment" 'comment)
  318. (const :tag "Insert the sentence" t)))
  319. (defcustom org-export-with-date t
  320. "Non-nil means insert date in the exported document.
  321. This option can also be set with the OPTIONS keyword,
  322. e.g. \"date:nil\"."
  323. :group 'org-export-general
  324. :type 'boolean)
  325. (defcustom org-export-date-timestamp-format nil
  326. "Time-stamp format string to use for DATE keyword.
  327. The format string, when specified, only applies if date consists
  328. in a single time-stamp. Otherwise its value will be ignored.
  329. See `format-time-string' for details on how to build this
  330. string."
  331. :group 'org-export-general
  332. :type '(choice
  333. (string :tag "Time-stamp format string")
  334. (const :tag "No format string" nil)))
  335. (defcustom org-export-creator-string
  336. (format "Emacs %s (Org mode %s)"
  337. emacs-version
  338. (if (fboundp 'org-version) (org-version) "unknown version"))
  339. "Information about the creator of the document.
  340. This option can also be set on with the CREATOR keyword."
  341. :group 'org-export-general
  342. :type '(string :tag "Creator string"))
  343. (defcustom org-export-with-drawers '(not "LOGBOOK")
  344. "Non-nil means export contents of standard drawers.
  345. When t, all drawers are exported. This may also be a list of
  346. drawer names to export. If that list starts with `not', only
  347. drawers with such names will be ignored.
  348. This variable doesn't apply to properties drawers.
  349. This option can also be set with the OPTIONS keyword,
  350. e.g. \"d:nil\"."
  351. :group 'org-export-general
  352. :version "24.4"
  353. :package-version '(Org . "8.0")
  354. :type '(choice
  355. (const :tag "All drawers" t)
  356. (const :tag "None" nil)
  357. (repeat :tag "Selected drawers"
  358. (string :tag "Drawer name"))
  359. (list :tag "Ignored drawers"
  360. (const :format "" not)
  361. (repeat :tag "Specify names of drawers to ignore during export"
  362. :inline t
  363. (string :tag "Drawer name")))))
  364. (defcustom org-export-with-email nil
  365. "Non-nil means insert author email into the exported file.
  366. This option can also be set with the OPTIONS keyword,
  367. e.g. \"email:t\"."
  368. :group 'org-export-general
  369. :type 'boolean)
  370. (defcustom org-export-with-emphasize t
  371. "Non-nil means interpret *word*, /word/, _word_ and +word+.
  372. If the export target supports emphasizing text, the word will be
  373. typeset in bold, italic, with an underline or strike-through,
  374. respectively.
  375. This option can also be set with the OPTIONS keyword,
  376. e.g. \"*:nil\"."
  377. :group 'org-export-general
  378. :type 'boolean)
  379. (defcustom org-export-exclude-tags '("noexport")
  380. "Tags that exclude a tree from export.
  381. All trees carrying any of these tags will be excluded from
  382. export. This is without condition, so even subtrees inside that
  383. carry one of the `org-export-select-tags' will be removed.
  384. This option can also be set with the EXCLUDE_TAGS keyword."
  385. :group 'org-export-general
  386. :type '(repeat (string :tag "Tag")))
  387. (defcustom org-export-with-fixed-width t
  388. "Non-nil means lines starting with \":\" will be in fixed width font.
  389. This can be used to have pre-formatted text, fragments of code
  390. etc. For example:
  391. : ;; Some Lisp examples
  392. : (while (defc cnt)
  393. : (ding))
  394. will be looking just like this in also HTML. See also the QUOTE
  395. keyword. Not all export backends support this.
  396. This option can also be set with the OPTIONS keyword,
  397. e.g. \"::nil\"."
  398. :group 'org-export-general
  399. :type 'boolean)
  400. (defcustom org-export-with-footnotes t
  401. "Non-nil means Org footnotes should be exported.
  402. This option can also be set with the OPTIONS keyword,
  403. e.g. \"f:nil\"."
  404. :group 'org-export-general
  405. :type 'boolean)
  406. (defcustom org-export-with-latex t
  407. "Non-nil means process LaTeX environments and fragments.
  408. This option can also be set with the OPTIONS line,
  409. e.g. \"tex:verbatim\". Allowed values are:
  410. nil Ignore math snippets.
  411. `verbatim' Keep everything in verbatim.
  412. t Allow export of math snippets."
  413. :group 'org-export-general
  414. :version "24.4"
  415. :package-version '(Org . "8.0")
  416. :type '(choice
  417. (const :tag "Do not process math in any way" nil)
  418. (const :tag "Interpret math snippets" t)
  419. (const :tag "Leave math verbatim" verbatim)))
  420. (defcustom org-export-headline-levels 3
  421. "The last level which is still exported as a headline.
  422. Inferior levels will usually produce itemize or enumerate lists
  423. when exported, but back-end behaviour may differ.
  424. This option can also be set with the OPTIONS keyword,
  425. e.g. \"H:2\"."
  426. :group 'org-export-general
  427. :type 'integer)
  428. (defcustom org-export-default-language "en"
  429. "The default language for export and clocktable translations, as a string.
  430. This may have an association in
  431. `org-clock-clocktable-language-setup',
  432. `org-export-smart-quotes-alist' and `org-export-dictionary'.
  433. This option can also be set with the LANGUAGE keyword."
  434. :group 'org-export-general
  435. :type '(string :tag "Language"))
  436. (defcustom org-export-preserve-breaks nil
  437. "Non-nil means preserve all line breaks when exporting.
  438. This option can also be set with the OPTIONS keyword,
  439. e.g. \"\\n:t\"."
  440. :group 'org-export-general
  441. :type 'boolean)
  442. (defcustom org-export-with-entities t
  443. "Non-nil means interpret entities when exporting.
  444. For example, HTML export converts \\alpha to &alpha; and \\AA to
  445. &Aring;.
  446. For a list of supported names, see the constant `org-entities'
  447. and the user option `org-entities-user'.
  448. This option can also be set with the OPTIONS keyword,
  449. e.g. \"e:nil\"."
  450. :group 'org-export-general
  451. :type 'boolean)
  452. (defcustom org-export-with-inlinetasks t
  453. "Non-nil means inlinetasks should be exported.
  454. This option can also be set with the OPTIONS keyword,
  455. e.g. \"inline:nil\"."
  456. :group 'org-export-general
  457. :version "24.4"
  458. :package-version '(Org . "8.0")
  459. :type 'boolean)
  460. (defcustom org-export-with-planning nil
  461. "Non-nil means include planning info in export.
  462. Planning info is the line containing either SCHEDULED:,
  463. DEADLINE:, CLOSED: time-stamps, or a combination of them.
  464. This option can also be set with the OPTIONS keyword,
  465. e.g. \"p:t\"."
  466. :group 'org-export-general
  467. :version "24.4"
  468. :package-version '(Org . "8.0")
  469. :type 'boolean)
  470. (defcustom org-export-with-priority nil
  471. "Non-nil means include priority cookies in export.
  472. This option can also be set with the OPTIONS keyword,
  473. e.g. \"pri:t\"."
  474. :group 'org-export-general
  475. :type 'boolean)
  476. (defcustom org-export-with-section-numbers t
  477. "Non-nil means add section numbers to headlines when exporting.
  478. When set to an integer n, numbering will only happen for
  479. headlines whose relative level is higher or equal to n.
  480. This option can also be set with the OPTIONS keyword,
  481. e.g. \"num:t\"."
  482. :group 'org-export-general
  483. :type 'boolean)
  484. (defcustom org-export-select-tags '("export")
  485. "Tags that select a tree for export.
  486. If any such tag is found in a buffer, all trees that do not carry
  487. one of these tags will be ignored during export. Inside trees
  488. that are selected like this, you can still deselect a subtree by
  489. tagging it with one of the `org-export-exclude-tags'.
  490. This option can also be set with the SELECT_TAGS keyword."
  491. :group 'org-export-general
  492. :type '(repeat (string :tag "Tag")))
  493. (defcustom org-export-with-smart-quotes nil
  494. "Non-nil means activate smart quotes during export.
  495. This option can also be set with the OPTIONS keyword,
  496. e.g., \"':t\".
  497. When setting this to non-nil, you need to take care of
  498. using the correct Babel package when exporting to LaTeX.
  499. E.g., you can load Babel for french like this:
  500. #+LATEX_HEADER: \\usepackage[french]{babel}"
  501. :group 'org-export-general
  502. :version "24.4"
  503. :package-version '(Org . "8.0")
  504. :type 'boolean)
  505. (defcustom org-export-with-special-strings t
  506. "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
  507. When this option is turned on, these strings will be exported as:
  508. Org HTML LaTeX UTF-8
  509. -----+----------+--------+-------
  510. \\- &shy; \\-
  511. -- &ndash; -- –
  512. --- &mdash; --- —
  513. ... &hellip; \\ldots …
  514. This option can also be set with the OPTIONS keyword,
  515. e.g. \"-:nil\"."
  516. :group 'org-export-general
  517. :type 'boolean)
  518. (defcustom org-export-with-statistics-cookies t
  519. "Non-nil means include statistics cookies in export.
  520. This option can also be set with the OPTIONS keyword,
  521. e.g. \"stat:nil\""
  522. :group 'org-export-general
  523. :version "24.4"
  524. :package-version '(Org . "8.0")
  525. :type 'boolean)
  526. (defcustom org-export-with-sub-superscripts t
  527. "Non-nil means interpret \"_\" and \"^\" for export.
  528. When this option is turned on, you can use TeX-like syntax for
  529. sub- and superscripts. Several characters after \"_\" or \"^\"
  530. will be considered as a single item - so grouping with {} is
  531. normally not needed. For example, the following things will be
  532. parsed as single sub- or superscripts.
  533. 10^24 or 10^tau several digits will be considered 1 item.
  534. 10^-12 or 10^-tau a leading sign with digits or a word
  535. x^2-y^3 will be read as x^2 - y^3, because items are
  536. terminated by almost any nonword/nondigit char.
  537. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  538. Still, ambiguity is possible - so when in doubt use {} to enclose
  539. the sub/superscript. If you set this variable to the symbol
  540. `{}', the braces are *required* in order to trigger
  541. interpretations as sub/superscript. This can be helpful in
  542. documents that need \"_\" frequently in plain text.
  543. This option can also be set with the OPTIONS keyword,
  544. e.g. \"^:nil\"."
  545. :group 'org-export-general
  546. :type '(choice
  547. (const :tag "Interpret them" t)
  548. (const :tag "Curly brackets only" {})
  549. (const :tag "Do not interpret them" nil)))
  550. (defcustom org-export-with-toc t
  551. "Non-nil means create a table of contents in exported files.
  552. The TOC contains headlines with levels up
  553. to`org-export-headline-levels'. When an integer, include levels
  554. up to N in the toc, this may then be different from
  555. `org-export-headline-levels', but it will not be allowed to be
  556. larger than the number of headline levels. When nil, no table of
  557. contents is made.
  558. This option can also be set with the OPTIONS keyword,
  559. e.g. \"toc:nil\" or \"toc:3\"."
  560. :group 'org-export-general
  561. :type '(choice
  562. (const :tag "No Table of Contents" nil)
  563. (const :tag "Full Table of Contents" t)
  564. (integer :tag "TOC to level")))
  565. (defcustom org-export-with-tables t
  566. "If non-nil, lines starting with \"|\" define a table.
  567. For example:
  568. | Name | Address | Birthday |
  569. |-------------+----------+-----------|
  570. | Arthur Dent | England | 29.2.2100 |
  571. This option can also be set with the OPTIONS keyword,
  572. e.g. \"|:nil\"."
  573. :group 'org-export-general
  574. :type 'boolean)
  575. (defcustom org-export-with-tags t
  576. "If nil, do not export tags, just remove them from headlines.
  577. If this is the symbol `not-in-toc', tags will be removed from
  578. table of contents entries, but still be shown in the headlines of
  579. the document.
  580. This option can also be set with the OPTIONS keyword,
  581. e.g. \"tags:nil\"."
  582. :group 'org-export-general
  583. :type '(choice
  584. (const :tag "Off" nil)
  585. (const :tag "Not in TOC" not-in-toc)
  586. (const :tag "On" t)))
  587. (defcustom org-export-with-tasks t
  588. "Non-nil means include TODO items for export.
  589. This may have the following values:
  590. t include tasks independent of state.
  591. `todo' include only tasks that are not yet done.
  592. `done' include only tasks that are already done.
  593. nil ignore all tasks.
  594. list of keywords include tasks with these keywords.
  595. This option can also be set with the OPTIONS keyword,
  596. e.g. \"tasks:nil\"."
  597. :group 'org-export-general
  598. :type '(choice
  599. (const :tag "All tasks" t)
  600. (const :tag "No tasks" nil)
  601. (const :tag "Not-done tasks" todo)
  602. (const :tag "Only done tasks" done)
  603. (repeat :tag "Specific TODO keywords"
  604. (string :tag "Keyword"))))
  605. (defcustom org-export-time-stamp-file t
  606. "Non-nil means insert a time stamp into the exported file.
  607. The time stamp shows when the file was created. This option can
  608. also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
  609. :group 'org-export-general
  610. :type 'boolean)
  611. (defcustom org-export-with-timestamps t
  612. "Non nil means allow timestamps in export.
  613. It can be set to any of the following values:
  614. t export all timestamps.
  615. `active' export active timestamps only.
  616. `inactive' export inactive timestamps only.
  617. nil do not export timestamps
  618. This only applies to timestamps isolated in a paragraph
  619. containing only timestamps. Other timestamps are always
  620. exported.
  621. This option can also be set with the OPTIONS keyword, e.g.
  622. \"<:nil\"."
  623. :group 'org-export-general
  624. :type '(choice
  625. (const :tag "All timestamps" t)
  626. (const :tag "Only active timestamps" active)
  627. (const :tag "Only inactive timestamps" inactive)
  628. (const :tag "No timestamp" nil)))
  629. (defcustom org-export-with-todo-keywords t
  630. "Non-nil means include TODO keywords in export.
  631. When nil, remove all these keywords from the export. This option
  632. can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
  633. :group 'org-export-general
  634. :type 'boolean)
  635. (defcustom org-export-allow-bind-keywords nil
  636. "Non-nil means BIND keywords can define local variable values.
  637. This is a potential security risk, which is why the default value
  638. is nil. You can also allow them through local buffer variables."
  639. :group 'org-export-general
  640. :version "24.4"
  641. :package-version '(Org . "8.0")
  642. :type 'boolean)
  643. (defcustom org-export-snippet-translation-alist nil
  644. "Alist between export snippets back-ends and exporter back-ends.
  645. This variable allows to provide shortcuts for export snippets.
  646. For example, with a value of '\(\(\"h\" . \"html\"\)\), the
  647. HTML back-end will recognize the contents of \"@@h:<b>@@\" as
  648. HTML code while every other back-end will ignore it."
  649. :group 'org-export-general
  650. :version "24.4"
  651. :package-version '(Org . "8.0")
  652. :type '(repeat
  653. (cons (string :tag "Shortcut")
  654. (string :tag "Back-end"))))
  655. (defcustom org-export-coding-system nil
  656. "Coding system for the exported file."
  657. :group 'org-export-general
  658. :version "24.4"
  659. :package-version '(Org . "8.0")
  660. :type 'coding-system)
  661. (defcustom org-export-copy-to-kill-ring 'if-interactive
  662. "Should we push exported content to the kill ring?"
  663. :group 'org-export-general
  664. :version "24.3"
  665. :type '(choice
  666. (const :tag "Always" t)
  667. (const :tag "When export is done interactively" if-interactive)
  668. (const :tag "Never" nil)))
  669. (defcustom org-export-initial-scope 'buffer
  670. "The initial scope when exporting with `org-export-dispatch'.
  671. This variable can be either set to `buffer' or `subtree'."
  672. :group 'org-export-general
  673. :type '(choice
  674. (const :tag "Export current buffer" buffer)
  675. (const :tag "Export current subtree" subtree)))
  676. (defcustom org-export-show-temporary-export-buffer t
  677. "Non-nil means show buffer after exporting to temp buffer.
  678. When Org exports to a file, the buffer visiting that file is ever
  679. shown, but remains buried. However, when exporting to
  680. a temporary buffer, that buffer is popped up in a second window.
  681. When this variable is nil, the buffer remains buried also in
  682. these cases."
  683. :group 'org-export-general
  684. :type 'boolean)
  685. (defcustom org-export-in-background nil
  686. "Non-nil means export and publishing commands will run in background.
  687. Results from an asynchronous export are never displayed
  688. automatically. But you can retrieve them with \\[org-export-stack]."
  689. :group 'org-export-general
  690. :version "24.4"
  691. :package-version '(Org . "8.0")
  692. :type 'boolean)
  693. (defcustom org-export-async-init-file user-init-file
  694. "File used to initialize external export process.
  695. Value must be an absolute file name. It defaults to user's
  696. initialization file. Though, a specific configuration makes the
  697. process faster and the export more portable."
  698. :group 'org-export-general
  699. :version "24.4"
  700. :package-version '(Org . "8.0")
  701. :type '(file :must-match t))
  702. (defcustom org-export-dispatch-use-expert-ui nil
  703. "Non-nil means using a non-intrusive `org-export-dispatch'.
  704. In that case, no help buffer is displayed. Though, an indicator
  705. for current export scope is added to the prompt (\"b\" when
  706. output is restricted to body only, \"s\" when it is restricted to
  707. the current subtree, \"v\" when only visible elements are
  708. considered for export, \"f\" when publishing functions should be
  709. passed the FORCE argument and \"a\" when the export should be
  710. asynchronous). Also, \[?] allows to switch back to standard
  711. mode."
  712. :group 'org-export-general
  713. :version "24.4"
  714. :package-version '(Org . "8.0")
  715. :type 'boolean)
  716. ;;; Defining Back-ends
  717. ;;
  718. ;; An export back-end is a structure with `org-export-backend' type
  719. ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
  720. ;; and `menu' slots.
  721. ;;
  722. ;; At the lowest level, a back-end is created with
  723. ;; `org-export-create-backend' function.
  724. ;;
  725. ;; A named back-end can be registered with
  726. ;; `org-export-register-backend' function. A registered back-end can
  727. ;; later be referred to by its name, with `org-export-get-backend'
  728. ;; function. Also, such a back-end can become the parent of a derived
  729. ;; back-end from which slot values will be inherited by default.
  730. ;; `org-export-derived-backend-p' can check if a given back-end is
  731. ;; derived from a list of back-end names.
  732. ;;
  733. ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
  734. ;; `org-export-get-all-filters' return the full alist of transcoders,
  735. ;; options and filters, including those inherited from ancestors.
  736. ;;
  737. ;; At a higher level, `org-export-define-backend' is the standard way
  738. ;; to define an export back-end. If the new back-end is similar to
  739. ;; a registered back-end, `org-export-define-derived-backend' may be
  740. ;; used instead.
  741. ;;
  742. ;; Eventually `org-export-barf-if-invalid-backend' returns an error
  743. ;; when a given back-end hasn't been registered yet.
  744. (defstruct (org-export-backend (:constructor org-export-create-backend)
  745. (:copier nil))
  746. name parent transcoders options filters blocks menu)
  747. (defun org-export-get-backend (name)
  748. "Return export back-end named after NAME.
  749. NAME is a symbol. Return nil if no such back-end is found."
  750. (catch 'found
  751. (dolist (b org-export--registered-backends)
  752. (when (eq (org-export-backend-name b) name)
  753. (throw 'found b)))))
  754. (defun org-export-register-backend (backend)
  755. "Register BACKEND as a known export back-end.
  756. BACKEND is a structure with `org-export-backend' type."
  757. ;; Refuse to register an unnamed back-end.
  758. (unless (org-export-backend-name backend)
  759. (error "Cannot register a unnamed export back-end"))
  760. ;; Refuse to register a back-end with an unknown parent.
  761. (let ((parent (org-export-backend-parent backend)))
  762. (when (and parent (not (org-export-get-backend parent)))
  763. (error "Cannot use unknown \"%s\" back-end as a parent" parent)))
  764. ;; Register dedicated export blocks in the parser.
  765. (dolist (name (org-export-backend-blocks backend))
  766. (add-to-list 'org-element-block-name-alist
  767. (cons name 'org-element-export-block-parser)))
  768. ;; If a back-end with the same name as BACKEND is already
  769. ;; registered, replace it with BACKEND. Otherwise, simply add
  770. ;; BACKEND to the list of registered back-ends.
  771. (let ((old (org-export-get-backend (org-export-backend-name backend))))
  772. (if old (setcar (memq old org-export--registered-backends) backend)
  773. (push backend org-export--registered-backends))))
  774. (defun org-export-barf-if-invalid-backend (backend)
  775. "Signal an error if BACKEND isn't defined."
  776. (unless (org-export-backend-p backend)
  777. (error "Unknown \"%s\" back-end: Aborting export" backend)))
  778. (defun org-export-derived-backend-p (backend &rest backends)
  779. "Non-nil if BACKEND is derived from one of BACKENDS.
  780. BACKEND is an export back-end, as returned by, e.g.,
  781. `org-export-create-backend', or a symbol referring to
  782. a registered back-end. BACKENDS is constituted of symbols."
  783. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  784. (when backend
  785. (catch 'exit
  786. (while (org-export-backend-parent backend)
  787. (when (memq (org-export-backend-name backend) backends)
  788. (throw 'exit t))
  789. (setq backend
  790. (org-export-get-backend (org-export-backend-parent backend))))
  791. (memq (org-export-backend-name backend) backends))))
  792. (defun org-export-get-all-transcoders (backend)
  793. "Return full translation table for BACKEND.
  794. BACKEND is an export back-end, as return by, e.g,,
  795. `org-export-create-backend'. Return value is an alist where
  796. keys are element or object types, as symbols, and values are
  797. transcoders.
  798. Unlike to `org-export-backend-transcoders', this function
  799. also returns transcoders inherited from parent back-ends,
  800. if any."
  801. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  802. (when backend
  803. (let ((transcoders (org-export-backend-transcoders backend))
  804. parent)
  805. (while (setq parent (org-export-backend-parent backend))
  806. (setq backend (org-export-get-backend parent))
  807. (setq transcoders
  808. (append transcoders (org-export-backend-transcoders backend))))
  809. transcoders)))
  810. (defun org-export-get-all-options (backend)
  811. "Return export options for BACKEND.
  812. BACKEND is an export back-end, as return by, e.g,,
  813. `org-export-create-backend'. See `org-export-options-alist'
  814. for the shape of the return value.
  815. Unlike to `org-export-backend-options', this function also
  816. returns options inherited from parent back-ends, if any."
  817. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  818. (when backend
  819. (let ((options (org-export-backend-options backend))
  820. parent)
  821. (while (setq parent (org-export-backend-parent backend))
  822. (setq backend (org-export-get-backend parent))
  823. (setq options (append options (org-export-backend-options backend))))
  824. options)))
  825. (defun org-export-get-all-filters (backend)
  826. "Return complete list of filters for BACKEND.
  827. BACKEND is an export back-end, as return by, e.g,,
  828. `org-export-create-backend'. Return value is an alist where
  829. keys are symbols and values lists of functions.
  830. Unlike to `org-export-backend-filters', this function also
  831. returns filters inherited from parent back-ends, if any."
  832. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  833. (when backend
  834. (let ((filters (org-export-backend-filters backend))
  835. parent)
  836. (while (setq parent (org-export-backend-parent backend))
  837. (setq backend (org-export-get-backend parent))
  838. (setq filters (append filters (org-export-backend-filters backend))))
  839. filters)))
  840. (defun org-export-define-backend (backend transcoders &rest body)
  841. "Define a new back-end BACKEND.
  842. TRANSCODERS is an alist between object or element types and
  843. functions handling them.
  844. These functions should return a string without any trailing
  845. space, or nil. They must accept three arguments: the object or
  846. element itself, its contents or nil when it isn't recursive and
  847. the property list used as a communication channel.
  848. Contents, when not nil, are stripped from any global indentation
  849. \(although the relative one is preserved). They also always end
  850. with a single newline character.
  851. If, for a given type, no function is found, that element or
  852. object type will simply be ignored, along with any blank line or
  853. white space at its end. The same will happen if the function
  854. returns the nil value. If that function returns the empty
  855. string, the type will be ignored, but the blank lines or white
  856. spaces will be kept.
  857. In addition to element and object types, one function can be
  858. associated to the `template' (or `inner-template') symbol and
  859. another one to the `plain-text' symbol.
  860. The former returns the final transcoded string, and can be used
  861. to add a preamble and a postamble to document's body. It must
  862. accept two arguments: the transcoded string and the property list
  863. containing export options. A function associated to `template'
  864. will not be applied if export has option \"body-only\".
  865. A function associated to `inner-template' is always applied.
  866. The latter, when defined, is to be called on every text not
  867. recognized as an element or an object. It must accept two
  868. arguments: the text string and the information channel. It is an
  869. appropriate place to protect special chars relative to the
  870. back-end.
  871. BODY can start with pre-defined keyword arguments. The following
  872. keywords are understood:
  873. :export-block
  874. String, or list of strings, representing block names that
  875. will not be parsed. This is used to specify blocks that will
  876. contain raw code specific to the back-end. These blocks
  877. still have to be handled by the relative `export-block' type
  878. translator.
  879. :filters-alist
  880. Alist between filters and function, or list of functions,
  881. specific to the back-end. See `org-export-filters-alist' for
  882. a list of all allowed filters. Filters defined here
  883. shouldn't make a back-end test, as it may prevent back-ends
  884. derived from this one to behave properly.
  885. :menu-entry
  886. Menu entry for the export dispatcher. It should be a list
  887. like:
  888. '(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
  889. where :
  890. KEY is a free character selecting the back-end.
  891. DESCRIPTION-OR-ORDINAL is either a string or a number.
  892. If it is a string, is will be used to name the back-end in
  893. its menu entry. If it is a number, the following menu will
  894. be displayed as a sub-menu of the back-end with the same
  895. KEY. Also, the number will be used to determine in which
  896. order such sub-menus will appear (lowest first).
  897. ACTION-OR-MENU is either a function or an alist.
  898. If it is an action, it will be called with four
  899. arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
  900. BODY-ONLY. See `org-export-as' for further explanations on
  901. some of them.
  902. If it is an alist, associations should follow the
  903. pattern:
  904. '(KEY DESCRIPTION ACTION)
  905. where KEY, DESCRIPTION and ACTION are described above.
  906. Valid values include:
  907. '(?m \"My Special Back-end\" my-special-export-function)
  908. or
  909. '(?l \"Export to LaTeX\"
  910. \(?p \"As PDF file\" org-latex-export-to-pdf)
  911. \(?o \"As PDF file and open\"
  912. \(lambda (a s v b)
  913. \(if a (org-latex-export-to-pdf t s v b)
  914. \(org-open-file
  915. \(org-latex-export-to-pdf nil s v b)))))))
  916. or the following, which will be added to the previous
  917. sub-menu,
  918. '(?l 1
  919. \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
  920. \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
  921. :options-alist
  922. Alist between back-end specific properties introduced in
  923. communication channel and how their value are acquired. See
  924. `org-export-options-alist' for more information about
  925. structure of the values."
  926. (declare (indent 1))
  927. (let (blocks filters menu-entry options contents)
  928. (while (keywordp (car body))
  929. (case (pop body)
  930. (:export-block (let ((names (pop body)))
  931. (setq blocks (if (consp names) (mapcar 'upcase names)
  932. (list (upcase names))))))
  933. (:filters-alist (setq filters (pop body)))
  934. (:menu-entry (setq menu-entry (pop body)))
  935. (:options-alist (setq options (pop body)))
  936. (t (pop body))))
  937. (org-export-register-backend
  938. (org-export-create-backend :name backend
  939. :transcoders transcoders
  940. :options options
  941. :filters filters
  942. :blocks blocks
  943. :menu menu-entry))))
  944. (defun org-export-define-derived-backend (child parent &rest body)
  945. "Create a new back-end as a variant of an existing one.
  946. CHILD is the name of the derived back-end. PARENT is the name of
  947. the parent back-end.
  948. BODY can start with pre-defined keyword arguments. The following
  949. keywords are understood:
  950. :export-block
  951. String, or list of strings, representing block names that
  952. will not be parsed. This is used to specify blocks that will
  953. contain raw code specific to the back-end. These blocks
  954. still have to be handled by the relative `export-block' type
  955. translator.
  956. :filters-alist
  957. Alist of filters that will overwrite or complete filters
  958. defined in PARENT back-end. See `org-export-filters-alist'
  959. for a list of allowed filters.
  960. :menu-entry
  961. Menu entry for the export dispatcher. See
  962. `org-export-define-backend' for more information about the
  963. expected value.
  964. :options-alist
  965. Alist of back-end specific properties that will overwrite or
  966. complete those defined in PARENT back-end. Refer to
  967. `org-export-options-alist' for more information about
  968. structure of the values.
  969. :translate-alist
  970. Alist of element and object types and transcoders that will
  971. overwrite or complete transcode table from PARENT back-end.
  972. Refer to `org-export-define-backend' for detailed information
  973. about transcoders.
  974. As an example, here is how one could define \"my-latex\" back-end
  975. as a variant of `latex' back-end with a custom template function:
  976. \(org-export-define-derived-backend 'my-latex 'latex
  977. :translate-alist '((template . my-latex-template-fun)))
  978. The back-end could then be called with, for example:
  979. \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
  980. (declare (indent 2))
  981. (let (blocks filters menu-entry options transcoders contents)
  982. (while (keywordp (car body))
  983. (case (pop body)
  984. (:export-block (let ((names (pop body)))
  985. (setq blocks (if (consp names) (mapcar 'upcase names)
  986. (list (upcase names))))))
  987. (:filters-alist (setq filters (pop body)))
  988. (:menu-entry (setq menu-entry (pop body)))
  989. (:options-alist (setq options (pop body)))
  990. (:translate-alist (setq transcoders (pop body)))
  991. (t (pop body))))
  992. (org-export-register-backend
  993. (org-export-create-backend :name child
  994. :parent parent
  995. :transcoders transcoders
  996. :options options
  997. :filters filters
  998. :blocks blocks
  999. :menu menu-entry))))
  1000. ;;; The Communication Channel
  1001. ;;
  1002. ;; During export process, every function has access to a number of
  1003. ;; properties. They are of two types:
  1004. ;;
  1005. ;; 1. Environment options are collected once at the very beginning of
  1006. ;; the process, out of the original buffer and configuration.
  1007. ;; Collecting them is handled by `org-export-get-environment'
  1008. ;; function.
  1009. ;;
  1010. ;; Most environment options are defined through the
  1011. ;; `org-export-options-alist' variable.
  1012. ;;
  1013. ;; 2. Tree properties are extracted directly from the parsed tree,
  1014. ;; just before export, by `org-export-collect-tree-properties'.
  1015. ;;
  1016. ;; Here is the full list of properties available during transcode
  1017. ;; process, with their category and their value type.
  1018. ;;
  1019. ;; + `:author' :: Author's name.
  1020. ;; - category :: option
  1021. ;; - type :: string
  1022. ;;
  1023. ;; + `:back-end' :: Current back-end used for transcoding.
  1024. ;; - category :: tree
  1025. ;; - type :: symbol
  1026. ;;
  1027. ;; + `:creator' :: String to write as creation information.
  1028. ;; - category :: option
  1029. ;; - type :: string
  1030. ;;
  1031. ;; + `:date' :: String to use as date.
  1032. ;; - category :: option
  1033. ;; - type :: string
  1034. ;;
  1035. ;; + `:description' :: Description text for the current data.
  1036. ;; - category :: option
  1037. ;; - type :: string
  1038. ;;
  1039. ;; + `:email' :: Author's email.
  1040. ;; - category :: option
  1041. ;; - type :: string
  1042. ;;
  1043. ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
  1044. ;; process.
  1045. ;; - category :: option
  1046. ;; - type :: list of strings
  1047. ;;
  1048. ;; + `:export-options' :: List of export options available for current
  1049. ;; process.
  1050. ;; - category :: none
  1051. ;; - type :: list of symbols, among `subtree', `body-only' and
  1052. ;; `visible-only'.
  1053. ;;
  1054. ;; + `:exported-data' :: Hash table used for memoizing
  1055. ;; `org-export-data'.
  1056. ;; - category :: tree
  1057. ;; - type :: hash table
  1058. ;;
  1059. ;; + `:filetags' :: List of global tags for buffer. Used by
  1060. ;; `org-export-get-tags' to get tags with inheritance.
  1061. ;; - category :: option
  1062. ;; - type :: list of strings
  1063. ;;
  1064. ;; + `:footnote-definition-alist' :: Alist between footnote labels and
  1065. ;; their definition, as parsed data. Only non-inlined footnotes
  1066. ;; are represented in this alist. Also, every definition isn't
  1067. ;; guaranteed to be referenced in the parse tree. The purpose of
  1068. ;; this property is to preserve definitions from oblivion
  1069. ;; (i.e. when the parse tree comes from a part of the original
  1070. ;; buffer), it isn't meant for direct use in a back-end. To
  1071. ;; retrieve a definition relative to a reference, use
  1072. ;; `org-export-get-footnote-definition' instead.
  1073. ;; - category :: option
  1074. ;; - type :: alist (STRING . LIST)
  1075. ;;
  1076. ;; + `:headline-levels' :: Maximum level being exported as an
  1077. ;; headline. Comparison is done with the relative level of
  1078. ;; headlines in the parse tree, not necessarily with their
  1079. ;; actual level.
  1080. ;; - category :: option
  1081. ;; - type :: integer
  1082. ;;
  1083. ;; + `:headline-offset' :: Difference between relative and real level
  1084. ;; of headlines in the parse tree. For example, a value of -1
  1085. ;; means a level 2 headline should be considered as level
  1086. ;; 1 (cf. `org-export-get-relative-level').
  1087. ;; - category :: tree
  1088. ;; - type :: integer
  1089. ;;
  1090. ;; + `:headline-numbering' :: Alist between headlines and their
  1091. ;; numbering, as a list of numbers
  1092. ;; (cf. `org-export-get-headline-number').
  1093. ;; - category :: tree
  1094. ;; - type :: alist (INTEGER . LIST)
  1095. ;;
  1096. ;; + `:id-alist' :: Alist between ID strings and destination file's
  1097. ;; path, relative to current directory. It is used by
  1098. ;; `org-export-resolve-id-link' to resolve ID links targeting an
  1099. ;; external file.
  1100. ;; - category :: option
  1101. ;; - type :: alist (STRING . STRING)
  1102. ;;
  1103. ;; + `:ignore-list' :: List of elements and objects that should be
  1104. ;; ignored during export.
  1105. ;; - category :: tree
  1106. ;; - type :: list of elements and objects
  1107. ;;
  1108. ;; + `:input-file' :: Full path to input file, if any.
  1109. ;; - category :: option
  1110. ;; - type :: string or nil
  1111. ;;
  1112. ;; + `:keywords' :: List of keywords attached to data.
  1113. ;; - category :: option
  1114. ;; - type :: string
  1115. ;;
  1116. ;; + `:language' :: Default language used for translations.
  1117. ;; - category :: option
  1118. ;; - type :: string
  1119. ;;
  1120. ;; + `:parse-tree' :: Whole parse tree, available at any time during
  1121. ;; transcoding.
  1122. ;; - category :: option
  1123. ;; - type :: list (as returned by `org-element-parse-buffer')
  1124. ;;
  1125. ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
  1126. ;; all line breaks.
  1127. ;; - category :: option
  1128. ;; - type :: symbol (nil, t)
  1129. ;;
  1130. ;; + `:section-numbers' :: Non-nil means transcoding should add
  1131. ;; section numbers to headlines.
  1132. ;; - category :: option
  1133. ;; - type :: symbol (nil, t)
  1134. ;;
  1135. ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
  1136. ;; in transcoding. When such a tag is present, subtrees without
  1137. ;; it are de facto excluded from the process. See
  1138. ;; `use-select-tags'.
  1139. ;; - category :: option
  1140. ;; - type :: list of strings
  1141. ;;
  1142. ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
  1143. ;; a time stamp in the output.
  1144. ;; - category :: option
  1145. ;; - type :: symbol (nil, t)
  1146. ;;
  1147. ;; + `:translate-alist' :: Alist between element and object types and
  1148. ;; transcoding functions relative to the current back-end.
  1149. ;; Special keys `inner-template', `template' and `plain-text' are
  1150. ;; also possible.
  1151. ;; - category :: option
  1152. ;; - type :: alist (SYMBOL . FUNCTION)
  1153. ;;
  1154. ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
  1155. ;; also be transcoded. If it is set to the `headline' symbol,
  1156. ;; only the archived headline's name is retained.
  1157. ;; - category :: option
  1158. ;; - type :: symbol (nil, t, `headline')
  1159. ;;
  1160. ;; + `:with-author' :: Non-nil means author's name should be included
  1161. ;; in the output.
  1162. ;; - category :: option
  1163. ;; - type :: symbol (nil, t)
  1164. ;;
  1165. ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
  1166. ;; - category :: option
  1167. ;; - type :: symbol (nil, t)
  1168. ;;
  1169. ;; + `:with-creator' :: Non-nil means a creation sentence should be
  1170. ;; inserted at the end of the transcoded string. If the value
  1171. ;; is `comment', it should be commented.
  1172. ;; - category :: option
  1173. ;; - type :: symbol (`comment', nil, t)
  1174. ;;
  1175. ;; + `:with-date' :: Non-nil means output should contain a date.
  1176. ;; - category :: option
  1177. ;; - type :. symbol (nil, t)
  1178. ;;
  1179. ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
  1180. ;; its value is a list of names, only drawers with such names
  1181. ;; will be transcoded. If that list starts with `not', drawer
  1182. ;; with these names will be skipped.
  1183. ;; - category :: option
  1184. ;; - type :: symbol (nil, t) or list of strings
  1185. ;;
  1186. ;; + `:with-email' :: Non-nil means output should contain author's
  1187. ;; email.
  1188. ;; - category :: option
  1189. ;; - type :: symbol (nil, t)
  1190. ;;
  1191. ;; + `:with-emphasize' :: Non-nil means emphasized text should be
  1192. ;; interpreted.
  1193. ;; - category :: option
  1194. ;; - type :: symbol (nil, t)
  1195. ;;
  1196. ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
  1197. ;; strings starting with a colon as a fixed-with (verbatim) area.
  1198. ;; - category :: option
  1199. ;; - type :: symbol (nil, t)
  1200. ;;
  1201. ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
  1202. ;; footnotes.
  1203. ;; - category :: option
  1204. ;; - type :: symbol (nil, t)
  1205. ;;
  1206. ;; + `:with-latex' :: Non-nil means `latex-environment' elements and
  1207. ;; `latex-fragment' objects should appear in export output. When
  1208. ;; this property is set to `verbatim', they will be left as-is.
  1209. ;; - category :: option
  1210. ;; - type :: symbol (`verbatim', nil, t)
  1211. ;;
  1212. ;; + `:with-planning' :: Non-nil means transcoding should include
  1213. ;; planning info.
  1214. ;; - category :: option
  1215. ;; - type :: symbol (nil, t)
  1216. ;;
  1217. ;; + `:with-priority' :: Non-nil means transcoding should include
  1218. ;; priority cookies.
  1219. ;; - category :: option
  1220. ;; - type :: symbol (nil, t)
  1221. ;;
  1222. ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
  1223. ;; plain text.
  1224. ;; - category :: option
  1225. ;; - type :: symbol (nil, t)
  1226. ;;
  1227. ;; + `:with-special-strings' :: Non-nil means transcoding should
  1228. ;; interpret special strings in plain text.
  1229. ;; - category :: option
  1230. ;; - type :: symbol (nil, t)
  1231. ;;
  1232. ;; + `:with-sub-superscript' :: Non-nil means transcoding should
  1233. ;; interpret subscript and superscript. With a value of "{}",
  1234. ;; only interpret those using curly brackets.
  1235. ;; - category :: option
  1236. ;; - type :: symbol (nil, {}, t)
  1237. ;;
  1238. ;; + `:with-tables' :: Non-nil means transcoding should interpret
  1239. ;; tables.
  1240. ;; - category :: option
  1241. ;; - type :: symbol (nil, t)
  1242. ;;
  1243. ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
  1244. ;; headlines. A `not-in-toc' value will remove them from the
  1245. ;; table of contents, if any, nonetheless.
  1246. ;; - category :: option
  1247. ;; - type :: symbol (nil, t, `not-in-toc')
  1248. ;;
  1249. ;; + `:with-tasks' :: Non-nil means transcoding should include
  1250. ;; headlines with a TODO keyword. A `todo' value will only
  1251. ;; include headlines with a todo type keyword while a `done'
  1252. ;; value will do the contrary. If a list of strings is provided,
  1253. ;; only tasks with keywords belonging to that list will be kept.
  1254. ;; - category :: option
  1255. ;; - type :: symbol (t, todo, done, nil) or list of strings
  1256. ;;
  1257. ;; + `:with-timestamps' :: Non-nil means transcoding should include
  1258. ;; time stamps. Special value `active' (resp. `inactive') ask to
  1259. ;; export only active (resp. inactive) timestamps. Otherwise,
  1260. ;; completely remove them.
  1261. ;; - category :: option
  1262. ;; - type :: symbol: (`active', `inactive', t, nil)
  1263. ;;
  1264. ;; + `:with-toc' :: Non-nil means that a table of contents has to be
  1265. ;; added to the output. An integer value limits its depth.
  1266. ;; - category :: option
  1267. ;; - type :: symbol (nil, t or integer)
  1268. ;;
  1269. ;; + `:with-todo-keywords' :: Non-nil means transcoding should
  1270. ;; include TODO keywords.
  1271. ;; - category :: option
  1272. ;; - type :: symbol (nil, t)
  1273. ;;;; Environment Options
  1274. ;;
  1275. ;; Environment options encompass all parameters defined outside the
  1276. ;; scope of the parsed data. They come from five sources, in
  1277. ;; increasing precedence order:
  1278. ;;
  1279. ;; - Global variables,
  1280. ;; - Buffer's attributes,
  1281. ;; - Options keyword symbols,
  1282. ;; - Buffer keywords,
  1283. ;; - Subtree properties.
  1284. ;;
  1285. ;; The central internal function with regards to environment options
  1286. ;; is `org-export-get-environment'. It updates global variables with
  1287. ;; "#+BIND:" keywords, then retrieve and prioritize properties from
  1288. ;; the different sources.
  1289. ;;
  1290. ;; The internal functions doing the retrieval are:
  1291. ;; `org-export--get-global-options',
  1292. ;; `org-export--get-buffer-attributes',
  1293. ;; `org-export--parse-option-keyword',
  1294. ;; `org-export--get-subtree-options' and
  1295. ;; `org-export--get-inbuffer-options'
  1296. ;;
  1297. ;; Also, `org-export--list-bound-variables' collects bound variables
  1298. ;; along with their value in order to set them as buffer local
  1299. ;; variables later in the process.
  1300. (defun org-export-get-environment (&optional backend subtreep ext-plist)
  1301. "Collect export options from the current buffer.
  1302. Optional argument BACKEND is an export back-end, as returned by
  1303. `org-export-create-backend'.
  1304. When optional argument SUBTREEP is non-nil, assume the export is
  1305. done against the current sub-tree.
  1306. Third optional argument EXT-PLIST is a property list with
  1307. external parameters overriding Org default settings, but still
  1308. inferior to file-local settings."
  1309. ;; First install #+BIND variables since these must be set before
  1310. ;; global options are read.
  1311. (dolist (pair (org-export--list-bound-variables))
  1312. (org-set-local (car pair) (nth 1 pair)))
  1313. ;; Get and prioritize export options...
  1314. (org-combine-plists
  1315. ;; ... from global variables...
  1316. (org-export--get-global-options backend)
  1317. ;; ... from an external property list...
  1318. ext-plist
  1319. ;; ... from in-buffer settings...
  1320. (org-export--get-inbuffer-options backend)
  1321. ;; ... and from subtree, when appropriate.
  1322. (and subtreep (org-export--get-subtree-options backend))
  1323. ;; Eventually add misc. properties.
  1324. (list
  1325. :back-end
  1326. backend
  1327. :translate-alist (org-export-get-all-transcoders backend)
  1328. :footnote-definition-alist
  1329. ;; Footnotes definitions must be collected in the original
  1330. ;; buffer, as there's no insurance that they will still be in
  1331. ;; the parse tree, due to possible narrowing.
  1332. (let (alist)
  1333. (org-with-wide-buffer
  1334. (goto-char (point-min))
  1335. (while (re-search-forward org-footnote-definition-re nil t)
  1336. (let ((def (save-match-data (org-element-at-point))))
  1337. (when (eq (org-element-type def) 'footnote-definition)
  1338. (push
  1339. (cons (org-element-property :label def)
  1340. (let ((cbeg (org-element-property :contents-begin def)))
  1341. (when cbeg
  1342. (org-element--parse-elements
  1343. cbeg (org-element-property :contents-end def)
  1344. nil nil nil nil (list 'org-data nil)))))
  1345. alist))))
  1346. alist))
  1347. :id-alist
  1348. ;; Collect id references.
  1349. (let (alist)
  1350. (org-with-wide-buffer
  1351. (goto-char (point-min))
  1352. (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
  1353. (let ((link (org-element-context)))
  1354. (when (eq (org-element-type link) 'link)
  1355. (let* ((id (org-element-property :path link))
  1356. (file (org-id-find-id-file id)))
  1357. (when file
  1358. (push (cons id (file-relative-name file)) alist)))))))
  1359. alist))))
  1360. (defun org-export--parse-option-keyword (options &optional backend)
  1361. "Parse an OPTIONS line and return values as a plist.
  1362. Optional argument BACKEND is an export back-end, as returned by,
  1363. e.g., `org-export-create-backend'. It specifies which back-end
  1364. specific items to read, if any."
  1365. (let* ((all
  1366. ;; Priority is given to back-end specific options.
  1367. (append (and backend (org-export-get-all-options backend))
  1368. org-export-options-alist))
  1369. plist)
  1370. (dolist (option all)
  1371. (let ((property (car option))
  1372. (item (nth 2 option)))
  1373. (when (and item
  1374. (not (plist-member plist property))
  1375. (string-match (concat "\\(\\`\\|[ \t]\\)"
  1376. (regexp-quote item)
  1377. ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
  1378. options))
  1379. (setq plist (plist-put plist
  1380. property
  1381. (car (read-from-string
  1382. (match-string 2 options))))))))
  1383. plist))
  1384. (defun org-export--get-subtree-options (&optional backend)
  1385. "Get export options in subtree at point.
  1386. Optional argument BACKEND is an export back-end, as returned by,
  1387. e.g., `org-export-create-backend'. It specifies back-end used
  1388. for export. Return options as a plist."
  1389. ;; For each buffer keyword, create a headline property setting the
  1390. ;; same property in communication channel. The name for the property
  1391. ;; is the keyword with "EXPORT_" appended to it.
  1392. (org-with-wide-buffer
  1393. (let (prop plist)
  1394. ;; Make sure point is at a heading.
  1395. (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t))
  1396. ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
  1397. ;; title as its fallback value.
  1398. (when (setq prop (or (org-entry-get (point) "EXPORT_TITLE")
  1399. (progn (looking-at org-todo-line-regexp)
  1400. (org-match-string-no-properties 3))))
  1401. (setq plist
  1402. (plist-put
  1403. plist :title
  1404. (org-element-parse-secondary-string
  1405. prop (org-element-restriction 'keyword)))))
  1406. ;; EXPORT_OPTIONS are parsed in a non-standard way.
  1407. (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
  1408. (setq plist
  1409. (nconc plist (org-export--parse-option-keyword prop backend))))
  1410. ;; Handle other keywords. TITLE keyword is excluded as it has
  1411. ;; been handled already.
  1412. (let ((seen '("TITLE")))
  1413. (mapc
  1414. (lambda (option)
  1415. (let ((property (car option))
  1416. (keyword (nth 1 option)))
  1417. (when (and keyword (not (member keyword seen)))
  1418. (let* ((subtree-prop (concat "EXPORT_" keyword))
  1419. ;; Export properties are not case-sensitive.
  1420. (value (let ((case-fold-search t))
  1421. (org-entry-get (point) subtree-prop))))
  1422. (push keyword seen)
  1423. (when (and value (not (plist-member plist property)))
  1424. (setq plist
  1425. (plist-put
  1426. plist
  1427. property
  1428. (cond
  1429. ;; Parse VALUE if required.
  1430. ((member keyword org-element-document-properties)
  1431. (org-element-parse-secondary-string
  1432. value (org-element-restriction 'keyword)))
  1433. ;; If BEHAVIOUR is `split' expected value is
  1434. ;; a list of strings, not a string.
  1435. ((eq (nth 4 option) 'split) (org-split-string value))
  1436. (t value)))))))))
  1437. ;; Look for both general keywords and back-end specific
  1438. ;; options, with priority given to the latter.
  1439. (append (and backend (org-export-get-all-options backend))
  1440. org-export-options-alist)))
  1441. ;; Return value.
  1442. plist)))
  1443. (defun org-export--get-inbuffer-options (&optional backend)
  1444. "Return current buffer export options, as a plist.
  1445. Optional argument BACKEND, when non-nil, is an export back-end,
  1446. as returned by, e.g., `org-export-create-backend'. It specifies
  1447. which back-end specific options should also be read in the
  1448. process.
  1449. Assume buffer is in Org mode. Narrowing, if any, is ignored."
  1450. (let* (plist
  1451. get-options ; For byte-compiler.
  1452. (case-fold-search t)
  1453. (options (append
  1454. ;; Priority is given to back-end specific options.
  1455. (and backend (org-export-get-all-options backend))
  1456. org-export-options-alist))
  1457. (regexp (format "^[ \t]*#\\+%s:"
  1458. (regexp-opt (nconc (delq nil (mapcar 'cadr options))
  1459. org-export-special-keywords))))
  1460. (find-properties
  1461. (lambda (keyword)
  1462. ;; Return all properties associated to KEYWORD.
  1463. (let (properties)
  1464. (dolist (option options properties)
  1465. (when (equal (nth 1 option) keyword)
  1466. (pushnew (car option) properties))))))
  1467. (get-options
  1468. (lambda (&optional files plist)
  1469. ;; Recursively read keywords in buffer. FILES is a list
  1470. ;; of files read so far. PLIST is the current property
  1471. ;; list obtained.
  1472. (org-with-wide-buffer
  1473. (goto-char (point-min))
  1474. (while (re-search-forward regexp nil t)
  1475. (let ((element (org-element-at-point)))
  1476. (when (eq (org-element-type element) 'keyword)
  1477. (let ((key (org-element-property :key element))
  1478. (val (org-element-property :value element)))
  1479. (cond
  1480. ;; Options in `org-export-special-keywords'.
  1481. ((equal key "SETUPFILE")
  1482. (let ((file (expand-file-name
  1483. (org-remove-double-quotes (org-trim val)))))
  1484. ;; Avoid circular dependencies.
  1485. (unless (member file files)
  1486. (with-temp-buffer
  1487. (insert (org-file-contents file 'noerror))
  1488. (let ((org-inhibit-startup t)) (org-mode))
  1489. (setq plist (funcall get-options
  1490. (cons file files) plist))))))
  1491. ((equal key "OPTIONS")
  1492. (setq plist
  1493. (org-combine-plists
  1494. plist
  1495. (org-export--parse-option-keyword val backend))))
  1496. ((equal key "FILETAGS")
  1497. (setq plist
  1498. (org-combine-plists
  1499. plist
  1500. (list :filetags
  1501. (org-uniquify
  1502. (append (org-split-string val ":")
  1503. (plist-get plist :filetags)))))))
  1504. (t
  1505. ;; Options in `org-export-options-alist'.
  1506. (dolist (property (funcall find-properties key))
  1507. (let ((behaviour (nth 4 (assq property options))))
  1508. (setq plist
  1509. (plist-put
  1510. plist property
  1511. ;; Handle value depending on specified
  1512. ;; BEHAVIOUR.
  1513. (case behaviour
  1514. (space
  1515. (if (not (plist-get plist property))
  1516. (org-trim val)
  1517. (concat (plist-get plist property)
  1518. " "
  1519. (org-trim val))))
  1520. (newline
  1521. (org-trim
  1522. (concat (plist-get plist property)
  1523. "\n"
  1524. (org-trim val))))
  1525. (split `(,@(plist-get plist property)
  1526. ,@(org-split-string val)))
  1527. ('t val)
  1528. (otherwise
  1529. (if (not (plist-member plist property)) val
  1530. (plist-get plist property))))))))))))))
  1531. ;; Return final value.
  1532. plist))))
  1533. ;; Read options in the current buffer.
  1534. (setq plist (funcall get-options buffer-file-name nil))
  1535. ;; Parse keywords specified in `org-element-document-properties'
  1536. ;; and return PLIST.
  1537. (dolist (keyword org-element-document-properties plist)
  1538. (dolist (property (funcall find-properties keyword))
  1539. (let ((value (plist-get plist property)))
  1540. (when (stringp value)
  1541. (setq plist
  1542. (plist-put plist property
  1543. (org-element-parse-secondary-string
  1544. value (org-element-restriction 'keyword))))))))))
  1545. (defun org-export--get-buffer-attributes ()
  1546. "Return properties related to buffer attributes, as a plist."
  1547. ;; Store full path of input file name, or nil. For internal use.
  1548. (list :input-file (buffer-file-name (buffer-base-buffer))))
  1549. (defvar org-export--default-title nil) ; Dynamically scoped.
  1550. (defun org-export-store-default-title ()
  1551. "Return default title for current document, as a string.
  1552. Title is extracted from associated file name, if any, or buffer's
  1553. name."
  1554. (setq org-export--default-title
  1555. (or (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  1556. (and visited-file
  1557. (file-name-sans-extension
  1558. (file-name-nondirectory visited-file))))
  1559. (buffer-name (buffer-base-buffer)))))
  1560. (defun org-export--get-global-options (&optional backend)
  1561. "Return global export options as a plist.
  1562. Optional argument BACKEND, if non-nil, is an export back-end, as
  1563. returned by, e.g., `org-export-create-backend'. It specifies
  1564. which back-end specific export options should also be read in the
  1565. process."
  1566. (let (plist
  1567. ;; Priority is given to back-end specific options.
  1568. (all (append (and backend (org-export-get-all-options backend))
  1569. org-export-options-alist)))
  1570. (dolist (cell all plist)
  1571. (let ((prop (car cell)))
  1572. (unless (plist-member plist prop)
  1573. (setq plist
  1574. (plist-put
  1575. plist
  1576. prop
  1577. ;; Eval default value provided. If keyword is
  1578. ;; a member of `org-element-document-properties',
  1579. ;; parse it as a secondary string before storing it.
  1580. (let ((value (eval (nth 3 cell))))
  1581. (if (not (stringp value)) value
  1582. (let ((keyword (nth 1 cell)))
  1583. (if (member keyword org-element-document-properties)
  1584. (org-element-parse-secondary-string
  1585. value (org-element-restriction 'keyword))
  1586. value)))))))))))
  1587. (defun org-export--list-bound-variables ()
  1588. "Return variables bound from BIND keywords in current buffer.
  1589. Also look for BIND keywords in setup files. The return value is
  1590. an alist where associations are (VARIABLE-NAME VALUE)."
  1591. (when org-export-allow-bind-keywords
  1592. (let* (collect-bind ; For byte-compiler.
  1593. (collect-bind
  1594. (lambda (files alist)
  1595. ;; Return an alist between variable names and their
  1596. ;; value. FILES is a list of setup files names read so
  1597. ;; far, used to avoid circular dependencies. ALIST is
  1598. ;; the alist collected so far.
  1599. (let ((case-fold-search t))
  1600. (org-with-wide-buffer
  1601. (goto-char (point-min))
  1602. (while (re-search-forward
  1603. "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t)
  1604. (let ((element (org-element-at-point)))
  1605. (when (eq (org-element-type element) 'keyword)
  1606. (let ((val (org-element-property :value element)))
  1607. (if (equal (org-element-property :key element) "BIND")
  1608. (push (read (format "(%s)" val)) alist)
  1609. ;; Enter setup file.
  1610. (let ((file (expand-file-name
  1611. (org-remove-double-quotes val))))
  1612. (unless (member file files)
  1613. (with-temp-buffer
  1614. (let ((org-inhibit-startup t)) (org-mode))
  1615. (insert (org-file-contents file 'noerror))
  1616. (setq alist
  1617. (funcall collect-bind
  1618. (cons file files)
  1619. alist))))))))))
  1620. alist)))))
  1621. ;; Return value in appropriate order of appearance.
  1622. (nreverse (funcall collect-bind nil nil)))))
  1623. ;;;; Tree Properties
  1624. ;;
  1625. ;; Tree properties are information extracted from parse tree. They
  1626. ;; are initialized at the beginning of the transcoding process by
  1627. ;; `org-export-collect-tree-properties'.
  1628. ;;
  1629. ;; Dedicated functions focus on computing the value of specific tree
  1630. ;; properties during initialization. Thus,
  1631. ;; `org-export--populate-ignore-list' lists elements and objects that
  1632. ;; should be skipped during export, `org-export--get-min-level' gets
  1633. ;; the minimal exportable level, used as a basis to compute relative
  1634. ;; level for headlines. Eventually
  1635. ;; `org-export--collect-headline-numbering' builds an alist between
  1636. ;; headlines and their numbering.
  1637. (defun org-export-collect-tree-properties (data info)
  1638. "Extract tree properties from parse tree.
  1639. DATA is the parse tree from which information is retrieved. INFO
  1640. is a list holding export options.
  1641. Following tree properties are set or updated:
  1642. `:exported-data' Hash table used to memoize results from
  1643. `org-export-data'.
  1644. `:footnote-definition-alist' List of footnotes definitions in
  1645. original buffer and current parse tree.
  1646. `:headline-offset' Offset between true level of headlines and
  1647. local level. An offset of -1 means a headline
  1648. of level 2 should be considered as a level
  1649. 1 headline in the context.
  1650. `:headline-numbering' Alist of all headlines as key an the
  1651. associated numbering as value.
  1652. `:ignore-list' List of elements that should be ignored during
  1653. export.
  1654. Return updated plist."
  1655. ;; Install the parse tree in the communication channel, in order to
  1656. ;; use `org-export-get-genealogy' and al.
  1657. (setq info (plist-put info :parse-tree data))
  1658. ;; Get the list of elements and objects to ignore, and put it into
  1659. ;; `:ignore-list'. Do not overwrite any user ignore that might have
  1660. ;; been done during parse tree filtering.
  1661. (setq info
  1662. (plist-put info
  1663. :ignore-list
  1664. (append (org-export--populate-ignore-list data info)
  1665. (plist-get info :ignore-list))))
  1666. ;; Compute `:headline-offset' in order to be able to use
  1667. ;; `org-export-get-relative-level'.
  1668. (setq info
  1669. (plist-put info
  1670. :headline-offset
  1671. (- 1 (org-export--get-min-level data info))))
  1672. ;; Update footnotes definitions list with definitions in parse tree.
  1673. ;; This is required since buffer expansion might have modified
  1674. ;; boundaries of footnote definitions contained in the parse tree.
  1675. ;; This way, definitions in `footnote-definition-alist' are bound to
  1676. ;; match those in the parse tree.
  1677. (let ((defs (plist-get info :footnote-definition-alist)))
  1678. (org-element-map data 'footnote-definition
  1679. (lambda (fn)
  1680. (push (cons (org-element-property :label fn)
  1681. `(org-data nil ,@(org-element-contents fn)))
  1682. defs)))
  1683. (setq info (plist-put info :footnote-definition-alist defs)))
  1684. ;; Properties order doesn't matter: get the rest of the tree
  1685. ;; properties.
  1686. (nconc
  1687. `(:headline-numbering ,(org-export--collect-headline-numbering data info)
  1688. :exported-data ,(make-hash-table :test 'eq :size 4001))
  1689. info))
  1690. (defun org-export--get-min-level (data options)
  1691. "Return minimum exportable headline's level in DATA.
  1692. DATA is parsed tree as returned by `org-element-parse-buffer'.
  1693. OPTIONS is a plist holding export options."
  1694. (catch 'exit
  1695. (let ((min-level 10000))
  1696. (mapc
  1697. (lambda (blob)
  1698. (when (and (eq (org-element-type blob) 'headline)
  1699. (not (org-element-property :footnote-section-p blob))
  1700. (not (memq blob (plist-get options :ignore-list))))
  1701. (setq min-level (min (org-element-property :level blob) min-level)))
  1702. (when (= min-level 1) (throw 'exit 1)))
  1703. (org-element-contents data))
  1704. ;; If no headline was found, for the sake of consistency, set
  1705. ;; minimum level to 1 nonetheless.
  1706. (if (= min-level 10000) 1 min-level))))
  1707. (defun org-export--collect-headline-numbering (data options)
  1708. "Return numbering of all exportable headlines in a parse tree.
  1709. DATA is the parse tree. OPTIONS is the plist holding export
  1710. options.
  1711. Return an alist whose key is a headline and value is its
  1712. associated numbering \(in the shape of a list of numbers\) or nil
  1713. for a footnotes section."
  1714. (let ((numbering (make-vector org-export-max-depth 0)))
  1715. (org-element-map data 'headline
  1716. (lambda (headline)
  1717. (unless (org-element-property :footnote-section-p headline)
  1718. (let ((relative-level
  1719. (1- (org-export-get-relative-level headline options))))
  1720. (cons
  1721. headline
  1722. (loop for n across numbering
  1723. for idx from 0 to org-export-max-depth
  1724. when (< idx relative-level) collect n
  1725. when (= idx relative-level) collect (aset numbering idx (1+ n))
  1726. when (> idx relative-level) do (aset numbering idx 0))))))
  1727. options)))
  1728. (defun org-export--populate-ignore-list (data options)
  1729. "Return list of elements and objects to ignore during export.
  1730. DATA is the parse tree to traverse. OPTIONS is the plist holding
  1731. export options."
  1732. (let* (ignore
  1733. walk-data
  1734. ;; First find trees containing a select tag, if any.
  1735. (selected (org-export--selected-trees data options))
  1736. (walk-data
  1737. (lambda (data)
  1738. ;; Collect ignored elements or objects into IGNORE-LIST.
  1739. (let ((type (org-element-type data)))
  1740. (if (org-export--skip-p data options selected) (push data ignore)
  1741. (if (and (eq type 'headline)
  1742. (eq (plist-get options :with-archived-trees) 'headline)
  1743. (org-element-property :archivedp data))
  1744. ;; If headline is archived but tree below has
  1745. ;; to be skipped, add it to ignore list.
  1746. (mapc (lambda (e) (push e ignore))
  1747. (org-element-contents data))
  1748. ;; Move into secondary string, if any.
  1749. (let ((sec-prop
  1750. (cdr (assq type org-element-secondary-value-alist))))
  1751. (when sec-prop
  1752. (mapc walk-data (org-element-property sec-prop data))))
  1753. ;; Move into recursive objects/elements.
  1754. (mapc walk-data (org-element-contents data))))))))
  1755. ;; Main call.
  1756. (funcall walk-data data)
  1757. ;; Return value.
  1758. ignore))
  1759. (defun org-export--selected-trees (data info)
  1760. "Return list of headlines and inlinetasks with a select tag in their tree.
  1761. DATA is parsed data as returned by `org-element-parse-buffer'.
  1762. INFO is a plist holding export options."
  1763. (let* (selected-trees
  1764. walk-data ; For byte-compiler.
  1765. (walk-data
  1766. (function
  1767. (lambda (data genealogy)
  1768. (let ((type (org-element-type data)))
  1769. (cond
  1770. ((memq type '(headline inlinetask))
  1771. (let ((tags (org-element-property :tags data)))
  1772. (if (loop for tag in (plist-get info :select-tags)
  1773. thereis (member tag tags))
  1774. ;; When a select tag is found, mark full
  1775. ;; genealogy and every headline within the tree
  1776. ;; as acceptable.
  1777. (setq selected-trees
  1778. (append
  1779. genealogy
  1780. (org-element-map data '(headline inlinetask)
  1781. 'identity)
  1782. selected-trees))
  1783. ;; If at a headline, continue searching in tree,
  1784. ;; recursively.
  1785. (when (eq type 'headline)
  1786. (mapc (lambda (el)
  1787. (funcall walk-data el (cons data genealogy)))
  1788. (org-element-contents data))))))
  1789. ((or (eq type 'org-data)
  1790. (memq type org-element-greater-elements))
  1791. (mapc (lambda (el) (funcall walk-data el genealogy))
  1792. (org-element-contents data)))))))))
  1793. (funcall walk-data data nil)
  1794. selected-trees))
  1795. (defun org-export--skip-p (blob options selected)
  1796. "Non-nil when element or object BLOB should be skipped during export.
  1797. OPTIONS is the plist holding export options. SELECTED, when
  1798. non-nil, is a list of headlines or inlinetasks belonging to
  1799. a tree with a select tag."
  1800. (case (org-element-type blob)
  1801. (clock (not (plist-get options :with-clocks)))
  1802. (drawer
  1803. (let ((with-drawers-p (plist-get options :with-drawers)))
  1804. (or (not with-drawers-p)
  1805. (and (consp with-drawers-p)
  1806. ;; If `:with-drawers' value starts with `not', ignore
  1807. ;; every drawer whose name belong to that list.
  1808. ;; Otherwise, ignore drawers whose name isn't in that
  1809. ;; list.
  1810. (let ((name (org-element-property :drawer-name blob)))
  1811. (if (eq (car with-drawers-p) 'not)
  1812. (member-ignore-case name (cdr with-drawers-p))
  1813. (not (member-ignore-case name with-drawers-p))))))))
  1814. ((footnote-definition footnote-reference)
  1815. (not (plist-get options :with-footnotes)))
  1816. ((headline inlinetask)
  1817. (let ((with-tasks (plist-get options :with-tasks))
  1818. (todo (org-element-property :todo-keyword blob))
  1819. (todo-type (org-element-property :todo-type blob))
  1820. (archived (plist-get options :with-archived-trees))
  1821. (tags (org-element-property :tags blob)))
  1822. (or
  1823. (and (eq (org-element-type blob) 'inlinetask)
  1824. (not (plist-get options :with-inlinetasks)))
  1825. ;; Ignore subtrees with an exclude tag.
  1826. (loop for k in (plist-get options :exclude-tags)
  1827. thereis (member k tags))
  1828. ;; When a select tag is present in the buffer, ignore any tree
  1829. ;; without it.
  1830. (and selected (not (memq blob selected)))
  1831. ;; Ignore commented sub-trees.
  1832. (org-element-property :commentedp blob)
  1833. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1834. (and (not archived) (org-element-property :archivedp blob))
  1835. ;; Ignore tasks, if specified by `:with-tasks' property.
  1836. (and todo
  1837. (or (not with-tasks)
  1838. (and (memq with-tasks '(todo done))
  1839. (not (eq todo-type with-tasks)))
  1840. (and (consp with-tasks) (not (member todo with-tasks))))))))
  1841. ((latex-environment latex-fragment) (not (plist-get options :with-latex)))
  1842. (planning (not (plist-get options :with-planning)))
  1843. (statistics-cookie (not (plist-get options :with-statistics-cookies)))
  1844. (table-cell
  1845. (and (org-export-table-has-special-column-p
  1846. (org-export-get-parent-table blob))
  1847. (not (org-export-get-previous-element blob options))))
  1848. (table-row (org-export-table-row-is-special-p blob options))
  1849. (timestamp
  1850. ;; `:with-timestamps' only applies to isolated timestamps
  1851. ;; objects, i.e. timestamp objects in a paragraph containing only
  1852. ;; timestamps and whitespaces.
  1853. (when (let ((parent (org-export-get-parent-element blob)))
  1854. (and (memq (org-element-type parent) '(paragraph verse-block))
  1855. (not (org-element-map parent
  1856. (cons 'plain-text
  1857. (remq 'timestamp org-element-all-objects))
  1858. (lambda (obj)
  1859. (or (not (stringp obj)) (org-string-nw-p obj)))
  1860. options t))))
  1861. (case (plist-get options :with-timestamps)
  1862. ('nil t)
  1863. (active
  1864. (not (memq (org-element-property :type blob) '(active active-range))))
  1865. (inactive
  1866. (not (memq (org-element-property :type blob)
  1867. '(inactive inactive-range)))))))))
  1868. ;;; The Transcoder
  1869. ;;
  1870. ;; `org-export-data' reads a parse tree (obtained with, i.e.
  1871. ;; `org-element-parse-buffer') and transcodes it into a specified
  1872. ;; back-end output. It takes care of filtering out elements or
  1873. ;; objects according to export options and organizing the output blank
  1874. ;; lines and white space are preserved. The function memoizes its
  1875. ;; results, so it is cheap to call it within transcoders.
  1876. ;;
  1877. ;; It is possible to modify locally the back-end used by
  1878. ;; `org-export-data' or even use a temporary back-end by using
  1879. ;; `org-export-data-with-backend'.
  1880. ;;
  1881. ;; Internally, three functions handle the filtering of objects and
  1882. ;; elements during the export. In particular,
  1883. ;; `org-export-ignore-element' marks an element or object so future
  1884. ;; parse tree traversals skip it, `org-export--interpret-p' tells which
  1885. ;; elements or objects should be seen as real Org syntax and
  1886. ;; `org-export-expand' transforms the others back into their original
  1887. ;; shape
  1888. ;;
  1889. ;; `org-export-transcoder' is an accessor returning appropriate
  1890. ;; translator function for a given element or object.
  1891. (defun org-export-transcoder (blob info)
  1892. "Return appropriate transcoder for BLOB.
  1893. INFO is a plist containing export directives."
  1894. (let ((type (org-element-type blob)))
  1895. ;; Return contents only for complete parse trees.
  1896. (if (eq type 'org-data) (lambda (blob contents info) contents)
  1897. (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
  1898. (and (functionp transcoder) transcoder)))))
  1899. (defun org-export-data (data info)
  1900. "Convert DATA into current back-end format.
  1901. DATA is a parse tree, an element or an object or a secondary
  1902. string. INFO is a plist holding export options.
  1903. Return transcoded string."
  1904. (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
  1905. (if (not (eq memo 'no-memo)) memo
  1906. (let* ((type (org-element-type data))
  1907. (results
  1908. (cond
  1909. ;; Ignored element/object.
  1910. ((memq data (plist-get info :ignore-list)) nil)
  1911. ;; Plain text.
  1912. ((eq type 'plain-text)
  1913. (org-export-filter-apply-functions
  1914. (plist-get info :filter-plain-text)
  1915. (let ((transcoder (org-export-transcoder data info)))
  1916. (if transcoder (funcall transcoder data info) data))
  1917. info))
  1918. ;; Uninterpreted element/object: change it back to Org
  1919. ;; syntax and export again resulting raw string.
  1920. ((not (org-export--interpret-p data info))
  1921. (org-export-data
  1922. (org-export-expand
  1923. data
  1924. (mapconcat (lambda (blob) (org-export-data blob info))
  1925. (org-element-contents data)
  1926. ""))
  1927. info))
  1928. ;; Secondary string.
  1929. ((not type)
  1930. (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
  1931. ;; Element/Object without contents or, as a special case,
  1932. ;; headline with archive tag and archived trees restricted
  1933. ;; to title only.
  1934. ((or (not (org-element-contents data))
  1935. (and (eq type 'headline)
  1936. (eq (plist-get info :with-archived-trees) 'headline)
  1937. (org-element-property :archivedp data)))
  1938. (let ((transcoder (org-export-transcoder data info)))
  1939. (or (and (functionp transcoder)
  1940. (funcall transcoder data nil info))
  1941. ;; Export snippets never return a nil value so
  1942. ;; that white spaces following them are never
  1943. ;; ignored.
  1944. (and (eq type 'export-snippet) ""))))
  1945. ;; Element/Object with contents.
  1946. (t
  1947. (let ((transcoder (org-export-transcoder data info)))
  1948. (when transcoder
  1949. (let* ((greaterp (memq type org-element-greater-elements))
  1950. (objectp
  1951. (and (not greaterp)
  1952. (memq type org-element-recursive-objects)))
  1953. (contents
  1954. (mapconcat
  1955. (lambda (element) (org-export-data element info))
  1956. (org-element-contents
  1957. (if (or greaterp objectp) data
  1958. ;; Elements directly containing objects
  1959. ;; must have their indentation normalized
  1960. ;; first.
  1961. (org-element-normalize-contents
  1962. data
  1963. ;; When normalizing contents of the first
  1964. ;; paragraph in an item or a footnote
  1965. ;; definition, ignore first line's
  1966. ;; indentation: there is none and it
  1967. ;; might be misleading.
  1968. (when (eq type 'paragraph)
  1969. (let ((parent (org-export-get-parent data)))
  1970. (and
  1971. (eq (car (org-element-contents parent))
  1972. data)
  1973. (memq (org-element-type parent)
  1974. '(footnote-definition item))))))))
  1975. "")))
  1976. (funcall transcoder data
  1977. (if (not greaterp) contents
  1978. (org-element-normalize-string contents))
  1979. info))))))))
  1980. ;; Final result will be memoized before being returned.
  1981. (puthash
  1982. data
  1983. (cond
  1984. ((not results) nil)
  1985. ((memq type '(org-data plain-text nil)) results)
  1986. ;; Append the same white space between elements or objects as in
  1987. ;; the original buffer, and call appropriate filters.
  1988. (t
  1989. (let ((results
  1990. (org-export-filter-apply-functions
  1991. (plist-get info (intern (format ":filter-%s" type)))
  1992. (let ((post-blank (or (org-element-property :post-blank data)
  1993. 0)))
  1994. (if (memq type org-element-all-elements)
  1995. (concat (org-element-normalize-string results)
  1996. (make-string post-blank ?\n))
  1997. (concat results (make-string post-blank ? ))))
  1998. info)))
  1999. results)))
  2000. (plist-get info :exported-data))))))
  2001. (defun org-export-data-with-backend (data backend info)
  2002. "Convert DATA into BACKEND format.
  2003. DATA is an element, an object, a secondary string or a string.
  2004. BACKEND is a symbol. INFO is a plist used as a communication
  2005. channel.
  2006. Unlike to `org-export-with-backend', this function will
  2007. recursively convert DATA using BACKEND translation table."
  2008. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  2009. (org-export-data
  2010. data
  2011. ;; Set-up a new communication channel with translations defined in
  2012. ;; BACKEND as the translate table and a new hash table for
  2013. ;; memoization.
  2014. (org-combine-plists
  2015. info
  2016. (list :translate-alist (org-export-get-all-transcoders backend)
  2017. ;; Size of the hash table is reduced since this function
  2018. ;; will probably be used on short trees.
  2019. :exported-data (make-hash-table :test 'eq :size 401)))))
  2020. (defun org-export--interpret-p (blob info)
  2021. "Non-nil if element or object BLOB should be interpreted during export.
  2022. If nil, BLOB will appear as raw Org syntax. Check is done
  2023. according to export options INFO, stored as a plist."
  2024. (case (org-element-type blob)
  2025. ;; ... entities...
  2026. (entity (plist-get info :with-entities))
  2027. ;; ... emphasis...
  2028. ((bold italic strike-through underline)
  2029. (plist-get info :with-emphasize))
  2030. ;; ... fixed-width areas.
  2031. (fixed-width (plist-get info :with-fixed-width))
  2032. ;; ... LaTeX environments and fragments...
  2033. ((latex-environment latex-fragment)
  2034. (let ((with-latex-p (plist-get info :with-latex)))
  2035. (and with-latex-p (not (eq with-latex-p 'verbatim)))))
  2036. ;; ... sub/superscripts...
  2037. ((subscript superscript)
  2038. (let ((sub/super-p (plist-get info :with-sub-superscript)))
  2039. (if (eq sub/super-p '{})
  2040. (org-element-property :use-brackets-p blob)
  2041. sub/super-p)))
  2042. ;; ... tables...
  2043. (table (plist-get info :with-tables))
  2044. (otherwise t)))
  2045. (defun org-export-expand (blob contents &optional with-affiliated)
  2046. "Expand a parsed element or object to its original state.
  2047. BLOB is either an element or an object. CONTENTS is its
  2048. contents, as a string or nil.
  2049. When optional argument WITH-AFFILIATED is non-nil, add affiliated
  2050. keywords before output."
  2051. (let ((type (org-element-type blob)))
  2052. (concat (and with-affiliated (memq type org-element-all-elements)
  2053. (org-element--interpret-affiliated-keywords blob))
  2054. (funcall (intern (format "org-element-%s-interpreter" type))
  2055. blob contents))))
  2056. (defun org-export-ignore-element (element info)
  2057. "Add ELEMENT to `:ignore-list' in INFO.
  2058. Any element in `:ignore-list' will be skipped when using
  2059. `org-element-map'. INFO is modified by side effects."
  2060. (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
  2061. ;;; The Filter System
  2062. ;;
  2063. ;; Filters allow end-users to tweak easily the transcoded output.
  2064. ;; They are the functional counterpart of hooks, as every filter in
  2065. ;; a set is applied to the return value of the previous one.
  2066. ;;
  2067. ;; Every set is back-end agnostic. Although, a filter is always
  2068. ;; called, in addition to the string it applies to, with the back-end
  2069. ;; used as argument, so it's easy for the end-user to add back-end
  2070. ;; specific filters in the set. The communication channel, as
  2071. ;; a plist, is required as the third argument.
  2072. ;;
  2073. ;; From the developer side, filters sets can be installed in the
  2074. ;; process with the help of `org-export-define-backend', which
  2075. ;; internally stores filters as an alist. Each association has a key
  2076. ;; among the following symbols and a function or a list of functions
  2077. ;; as value.
  2078. ;;
  2079. ;; - `:filter-options' applies to the property list containing export
  2080. ;; options. Unlike to other filters, functions in this list accept
  2081. ;; two arguments instead of three: the property list containing
  2082. ;; export options and the back-end. Users can set its value through
  2083. ;; `org-export-filter-options-functions' variable.
  2084. ;;
  2085. ;; - `:filter-parse-tree' applies directly to the complete parsed
  2086. ;; tree. Users can set it through
  2087. ;; `org-export-filter-parse-tree-functions' variable.
  2088. ;;
  2089. ;; - `:filter-final-output' applies to the final transcoded string.
  2090. ;; Users can set it with `org-export-filter-final-output-functions'
  2091. ;; variable
  2092. ;;
  2093. ;; - `:filter-plain-text' applies to any string not recognized as Org
  2094. ;; syntax. `org-export-filter-plain-text-functions' allows users to
  2095. ;; configure it.
  2096. ;;
  2097. ;; - `:filter-TYPE' applies on the string returned after an element or
  2098. ;; object of type TYPE has been transcoded. A user can modify
  2099. ;; `org-export-filter-TYPE-functions'
  2100. ;;
  2101. ;; All filters sets are applied with
  2102. ;; `org-export-filter-apply-functions' function. Filters in a set are
  2103. ;; applied in a LIFO fashion. It allows developers to be sure that
  2104. ;; their filters will be applied first.
  2105. ;;
  2106. ;; Filters properties are installed in communication channel with
  2107. ;; `org-export-install-filters' function.
  2108. ;;
  2109. ;; Eventually, two hooks (`org-export-before-processing-hook' and
  2110. ;; `org-export-before-parsing-hook') are run at the beginning of the
  2111. ;; export process and just before parsing to allow for heavy structure
  2112. ;; modifications.
  2113. ;;;; Hooks
  2114. (defvar org-export-before-processing-hook nil
  2115. "Hook run at the beginning of the export process.
  2116. This is run before include keywords and macros are expanded and
  2117. Babel code blocks executed, on a copy of the original buffer
  2118. being exported. Visibility and narrowing are preserved. Point
  2119. is at the beginning of the buffer.
  2120. Every function in this hook will be called with one argument: the
  2121. back-end currently used, as a symbol.")
  2122. (defvar org-export-before-parsing-hook nil
  2123. "Hook run before parsing an export buffer.
  2124. This is run after include keywords and macros have been expanded
  2125. and Babel code blocks executed, on a copy of the original buffer
  2126. being exported. Visibility and narrowing are preserved. Point
  2127. is at the beginning of the buffer.
  2128. Every function in this hook will be called with one argument: the
  2129. back-end currently used, as a symbol.")
  2130. ;;;; Special Filters
  2131. (defvar org-export-filter-options-functions nil
  2132. "List of functions applied to the export options.
  2133. Each filter is called with two arguments: the export options, as
  2134. a plist, and the back-end, as a symbol. It must return
  2135. a property list containing export options.")
  2136. (defvar org-export-filter-parse-tree-functions nil
  2137. "List of functions applied to the parsed tree.
  2138. Each filter is called with three arguments: the parse tree, as
  2139. returned by `org-element-parse-buffer', the back-end, as
  2140. a symbol, and the communication channel, as a plist. It must
  2141. return the modified parse tree to transcode.")
  2142. (defvar org-export-filter-plain-text-functions nil
  2143. "List of functions applied to plain text.
  2144. Each filter is called with three arguments: a string which
  2145. contains no Org syntax, the back-end, as a symbol, and the
  2146. communication channel, as a plist. It must return a string or
  2147. nil.")
  2148. (defvar org-export-filter-final-output-functions nil
  2149. "List of functions applied to the transcoded string.
  2150. Each filter is called with three arguments: the full transcoded
  2151. string, the back-end, as a symbol, and the communication channel,
  2152. as a plist. It must return a string that will be used as the
  2153. final export output.")
  2154. ;;;; Elements Filters
  2155. (defvar org-export-filter-babel-call-functions nil
  2156. "List of functions applied to a transcoded babel-call.
  2157. Each filter is called with three arguments: the transcoded data,
  2158. as a string, the back-end, as a symbol, and the communication
  2159. channel, as a plist. It must return a string or nil.")
  2160. (defvar org-export-filter-center-block-functions nil
  2161. "List of functions applied to a transcoded center block.
  2162. Each filter is called with three arguments: the transcoded data,
  2163. as a string, the back-end, as a symbol, and the communication
  2164. channel, as a plist. It must return a string or nil.")
  2165. (defvar org-export-filter-clock-functions nil
  2166. "List of functions applied to a transcoded clock.
  2167. Each filter is called with three arguments: the transcoded data,
  2168. as a string, the back-end, as a symbol, and the communication
  2169. channel, as a plist. It must return a string or nil.")
  2170. (defvar org-export-filter-comment-functions nil
  2171. "List of functions applied to a transcoded comment.
  2172. Each filter is called with three arguments: the transcoded data,
  2173. as a string, the back-end, as a symbol, and the communication
  2174. channel, as a plist. It must return a string or nil.")
  2175. (defvar org-export-filter-comment-block-functions nil
  2176. "List of functions applied to a transcoded comment-block.
  2177. Each filter is called with three arguments: the transcoded data,
  2178. as a string, the back-end, as a symbol, and the communication
  2179. channel, as a plist. It must return a string or nil.")
  2180. (defvar org-export-filter-diary-sexp-functions nil
  2181. "List of functions applied to a transcoded diary-sexp.
  2182. Each filter is called with three arguments: the transcoded data,
  2183. as a string, the back-end, as a symbol, and the communication
  2184. channel, as a plist. It must return a string or nil.")
  2185. (defvar org-export-filter-drawer-functions nil
  2186. "List of functions applied to a transcoded drawer.
  2187. Each filter is called with three arguments: the transcoded data,
  2188. as a string, the back-end, as a symbol, and the communication
  2189. channel, as a plist. It must return a string or nil.")
  2190. (defvar org-export-filter-dynamic-block-functions nil
  2191. "List of functions applied to a transcoded dynamic-block.
  2192. Each filter is called with three arguments: the transcoded data,
  2193. as a string, the back-end, as a symbol, and the communication
  2194. channel, as a plist. It must return a string or nil.")
  2195. (defvar org-export-filter-example-block-functions nil
  2196. "List of functions applied to a transcoded example-block.
  2197. Each filter is called with three arguments: the transcoded data,
  2198. as a string, the back-end, as a symbol, and the communication
  2199. channel, as a plist. It must return a string or nil.")
  2200. (defvar org-export-filter-export-block-functions nil
  2201. "List of functions applied to a transcoded export-block.
  2202. Each filter is called with three arguments: the transcoded data,
  2203. as a string, the back-end, as a symbol, and the communication
  2204. channel, as a plist. It must return a string or nil.")
  2205. (defvar org-export-filter-fixed-width-functions nil
  2206. "List of functions applied to a transcoded fixed-width.
  2207. Each filter is called with three arguments: the transcoded data,
  2208. as a string, the back-end, as a symbol, and the communication
  2209. channel, as a plist. It must return a string or nil.")
  2210. (defvar org-export-filter-footnote-definition-functions nil
  2211. "List of functions applied to a transcoded footnote-definition.
  2212. Each filter is called with three arguments: the transcoded data,
  2213. as a string, the back-end, as a symbol, and the communication
  2214. channel, as a plist. It must return a string or nil.")
  2215. (defvar org-export-filter-headline-functions nil
  2216. "List of functions applied to a transcoded headline.
  2217. Each filter is called with three arguments: the transcoded data,
  2218. as a string, the back-end, as a symbol, and the communication
  2219. channel, as a plist. It must return a string or nil.")
  2220. (defvar org-export-filter-horizontal-rule-functions nil
  2221. "List of functions applied to a transcoded horizontal-rule.
  2222. Each filter is called with three arguments: the transcoded data,
  2223. as a string, the back-end, as a symbol, and the communication
  2224. channel, as a plist. It must return a string or nil.")
  2225. (defvar org-export-filter-inlinetask-functions nil
  2226. "List of functions applied to a transcoded inlinetask.
  2227. Each filter is called with three arguments: the transcoded data,
  2228. as a string, the back-end, as a symbol, and the communication
  2229. channel, as a plist. It must return a string or nil.")
  2230. (defvar org-export-filter-item-functions nil
  2231. "List of functions applied to a transcoded item.
  2232. Each filter is called with three arguments: the transcoded data,
  2233. as a string, the back-end, as a symbol, and the communication
  2234. channel, as a plist. It must return a string or nil.")
  2235. (defvar org-export-filter-keyword-functions nil
  2236. "List of functions applied to a transcoded keyword.
  2237. Each filter is called with three arguments: the transcoded data,
  2238. as a string, the back-end, as a symbol, and the communication
  2239. channel, as a plist. It must return a string or nil.")
  2240. (defvar org-export-filter-latex-environment-functions nil
  2241. "List of functions applied to a transcoded latex-environment.
  2242. Each filter is called with three arguments: the transcoded data,
  2243. as a string, the back-end, as a symbol, and the communication
  2244. channel, as a plist. It must return a string or nil.")
  2245. (defvar org-export-filter-node-property-functions nil
  2246. "List of functions applied to a transcoded node-property.
  2247. Each filter is called with three arguments: the transcoded data,
  2248. as a string, the back-end, as a symbol, and the communication
  2249. channel, as a plist. It must return a string or nil.")
  2250. (defvar org-export-filter-paragraph-functions nil
  2251. "List of functions applied to a transcoded paragraph.
  2252. Each filter is called with three arguments: the transcoded data,
  2253. as a string, the back-end, as a symbol, and the communication
  2254. channel, as a plist. It must return a string or nil.")
  2255. (defvar org-export-filter-plain-list-functions nil
  2256. "List of functions applied to a transcoded plain-list.
  2257. Each filter is called with three arguments: the transcoded data,
  2258. as a string, the back-end, as a symbol, and the communication
  2259. channel, as a plist. It must return a string or nil.")
  2260. (defvar org-export-filter-planning-functions nil
  2261. "List of functions applied to a transcoded planning.
  2262. Each filter is called with three arguments: the transcoded data,
  2263. as a string, the back-end, as a symbol, and the communication
  2264. channel, as a plist. It must return a string or nil.")
  2265. (defvar org-export-filter-property-drawer-functions nil
  2266. "List of functions applied to a transcoded property-drawer.
  2267. Each filter is called with three arguments: the transcoded data,
  2268. as a string, the back-end, as a symbol, and the communication
  2269. channel, as a plist. It must return a string or nil.")
  2270. (defvar org-export-filter-quote-block-functions nil
  2271. "List of functions applied to a transcoded quote block.
  2272. Each filter is called with three arguments: the transcoded quote
  2273. data, as a string, the back-end, as a symbol, and the
  2274. communication channel, as a plist. It must return a string or
  2275. nil.")
  2276. (defvar org-export-filter-quote-section-functions nil
  2277. "List of functions applied to a transcoded quote-section.
  2278. Each filter is called with three arguments: the transcoded data,
  2279. as a string, the back-end, as a symbol, and the communication
  2280. channel, as a plist. It must return a string or nil.")
  2281. (defvar org-export-filter-section-functions nil
  2282. "List of functions applied to a transcoded section.
  2283. Each filter is called with three arguments: the transcoded data,
  2284. as a string, the back-end, as a symbol, and the communication
  2285. channel, as a plist. It must return a string or nil.")
  2286. (defvar org-export-filter-special-block-functions nil
  2287. "List of functions applied to a transcoded special block.
  2288. Each filter is called with three arguments: the transcoded data,
  2289. as a string, the back-end, as a symbol, and the communication
  2290. channel, as a plist. It must return a string or nil.")
  2291. (defvar org-export-filter-src-block-functions nil
  2292. "List of functions applied to a transcoded src-block.
  2293. Each filter is called with three arguments: the transcoded data,
  2294. as a string, the back-end, as a symbol, and the communication
  2295. channel, as a plist. It must return a string or nil.")
  2296. (defvar org-export-filter-table-functions nil
  2297. "List of functions applied to a transcoded table.
  2298. Each filter is called with three arguments: the transcoded data,
  2299. as a string, the back-end, as a symbol, and the communication
  2300. channel, as a plist. It must return a string or nil.")
  2301. (defvar org-export-filter-table-cell-functions nil
  2302. "List of functions applied to a transcoded table-cell.
  2303. Each filter is called with three arguments: the transcoded data,
  2304. as a string, the back-end, as a symbol, and the communication
  2305. channel, as a plist. It must return a string or nil.")
  2306. (defvar org-export-filter-table-row-functions nil
  2307. "List of functions applied to a transcoded table-row.
  2308. Each filter is called with three arguments: the transcoded data,
  2309. as a string, the back-end, as a symbol, and the communication
  2310. channel, as a plist. It must return a string or nil.")
  2311. (defvar org-export-filter-verse-block-functions nil
  2312. "List of functions applied to a transcoded verse block.
  2313. Each filter is called with three arguments: the transcoded data,
  2314. as a string, the back-end, as a symbol, and the communication
  2315. channel, as a plist. It must return a string or nil.")
  2316. ;;;; Objects Filters
  2317. (defvar org-export-filter-bold-functions nil
  2318. "List of functions applied to transcoded bold text.
  2319. Each filter is called with three arguments: the transcoded data,
  2320. as a string, the back-end, as a symbol, and the communication
  2321. channel, as a plist. It must return a string or nil.")
  2322. (defvar org-export-filter-code-functions nil
  2323. "List of functions applied to transcoded code text.
  2324. Each filter is called with three arguments: the transcoded data,
  2325. as a string, the back-end, as a symbol, and the communication
  2326. channel, as a plist. It must return a string or nil.")
  2327. (defvar org-export-filter-entity-functions nil
  2328. "List of functions applied to a transcoded entity.
  2329. Each filter is called with three arguments: the transcoded data,
  2330. as a string, the back-end, as a symbol, and the communication
  2331. channel, as a plist. It must return a string or nil.")
  2332. (defvar org-export-filter-export-snippet-functions nil
  2333. "List of functions applied to a transcoded export-snippet.
  2334. Each filter is called with three arguments: the transcoded data,
  2335. as a string, the back-end, as a symbol, and the communication
  2336. channel, as a plist. It must return a string or nil.")
  2337. (defvar org-export-filter-footnote-reference-functions nil
  2338. "List of functions applied to a transcoded footnote-reference.
  2339. Each filter is called with three arguments: the transcoded data,
  2340. as a string, the back-end, as a symbol, and the communication
  2341. channel, as a plist. It must return a string or nil.")
  2342. (defvar org-export-filter-inline-babel-call-functions nil
  2343. "List of functions applied to a transcoded inline-babel-call.
  2344. Each filter is called with three arguments: the transcoded data,
  2345. as a string, the back-end, as a symbol, and the communication
  2346. channel, as a plist. It must return a string or nil.")
  2347. (defvar org-export-filter-inline-src-block-functions nil
  2348. "List of functions applied to a transcoded inline-src-block.
  2349. Each filter is called with three arguments: the transcoded data,
  2350. as a string, the back-end, as a symbol, and the communication
  2351. channel, as a plist. It must return a string or nil.")
  2352. (defvar org-export-filter-italic-functions nil
  2353. "List of functions applied to transcoded italic text.
  2354. Each filter is called with three arguments: the transcoded data,
  2355. as a string, the back-end, as a symbol, and the communication
  2356. channel, as a plist. It must return a string or nil.")
  2357. (defvar org-export-filter-latex-fragment-functions nil
  2358. "List of functions applied to a transcoded latex-fragment.
  2359. Each filter is called with three arguments: the transcoded data,
  2360. as a string, the back-end, as a symbol, and the communication
  2361. channel, as a plist. It must return a string or nil.")
  2362. (defvar org-export-filter-line-break-functions nil
  2363. "List of functions applied to a transcoded line-break.
  2364. Each filter is called with three arguments: the transcoded data,
  2365. as a string, the back-end, as a symbol, and the communication
  2366. channel, as a plist. It must return a string or nil.")
  2367. (defvar org-export-filter-link-functions nil
  2368. "List of functions applied to a transcoded link.
  2369. Each filter is called with three arguments: the transcoded data,
  2370. as a string, the back-end, as a symbol, and the communication
  2371. channel, as a plist. It must return a string or nil.")
  2372. (defvar org-export-filter-radio-target-functions nil
  2373. "List of functions applied to a transcoded radio-target.
  2374. Each filter is called with three arguments: the transcoded data,
  2375. as a string, the back-end, as a symbol, and the communication
  2376. channel, as a plist. It must return a string or nil.")
  2377. (defvar org-export-filter-statistics-cookie-functions nil
  2378. "List of functions applied to a transcoded statistics-cookie.
  2379. Each filter is called with three arguments: the transcoded data,
  2380. as a string, the back-end, as a symbol, and the communication
  2381. channel, as a plist. It must return a string or nil.")
  2382. (defvar org-export-filter-strike-through-functions nil
  2383. "List of functions applied to transcoded strike-through text.
  2384. Each filter is called with three arguments: the transcoded data,
  2385. as a string, the back-end, as a symbol, and the communication
  2386. channel, as a plist. It must return a string or nil.")
  2387. (defvar org-export-filter-subscript-functions nil
  2388. "List of functions applied to a transcoded subscript.
  2389. Each filter is called with three arguments: the transcoded data,
  2390. as a string, the back-end, as a symbol, and the communication
  2391. channel, as a plist. It must return a string or nil.")
  2392. (defvar org-export-filter-superscript-functions nil
  2393. "List of functions applied to a transcoded superscript.
  2394. Each filter is called with three arguments: the transcoded data,
  2395. as a string, the back-end, as a symbol, and the communication
  2396. channel, as a plist. It must return a string or nil.")
  2397. (defvar org-export-filter-target-functions nil
  2398. "List of functions applied to a transcoded target.
  2399. Each filter is called with three arguments: the transcoded data,
  2400. as a string, the back-end, as a symbol, and the communication
  2401. channel, as a plist. It must return a string or nil.")
  2402. (defvar org-export-filter-timestamp-functions nil
  2403. "List of functions applied to a transcoded timestamp.
  2404. Each filter is called with three arguments: the transcoded data,
  2405. as a string, the back-end, as a symbol, and the communication
  2406. channel, as a plist. It must return a string or nil.")
  2407. (defvar org-export-filter-underline-functions nil
  2408. "List of functions applied to transcoded underline text.
  2409. Each filter is called with three arguments: the transcoded data,
  2410. as a string, the back-end, as a symbol, and the communication
  2411. channel, as a plist. It must return a string or nil.")
  2412. (defvar org-export-filter-verbatim-functions nil
  2413. "List of functions applied to transcoded verbatim text.
  2414. Each filter is called with three arguments: the transcoded data,
  2415. as a string, the back-end, as a symbol, and the communication
  2416. channel, as a plist. It must return a string or nil.")
  2417. ;;;; Filters Tools
  2418. ;;
  2419. ;; Internal function `org-export-install-filters' installs filters
  2420. ;; hard-coded in back-ends (developer filters) and filters from global
  2421. ;; variables (user filters) in the communication channel.
  2422. ;;
  2423. ;; Internal function `org-export-filter-apply-functions' takes care
  2424. ;; about applying each filter in order to a given data. It ignores
  2425. ;; filters returning a nil value but stops whenever a filter returns
  2426. ;; an empty string.
  2427. (defun org-export-filter-apply-functions (filters value info)
  2428. "Call every function in FILTERS.
  2429. Functions are called with arguments VALUE, current export
  2430. back-end's name and INFO. A function returning a nil value will
  2431. be skipped. If it returns the empty string, the process ends and
  2432. VALUE is ignored.
  2433. Call is done in a LIFO fashion, to be sure that developer
  2434. specified filters, if any, are called first."
  2435. (catch 'exit
  2436. (let ((backend-name (plist-get info :back-end)))
  2437. (dolist (filter filters value)
  2438. (let ((result (funcall filter value backend-name info)))
  2439. (cond ((not result) value)
  2440. ((equal value "") (throw 'exit nil))
  2441. (t (setq value result))))))))
  2442. (defun org-export-install-filters (info)
  2443. "Install filters properties in communication channel.
  2444. INFO is a plist containing the current communication channel.
  2445. Return the updated communication channel."
  2446. (let (plist)
  2447. ;; Install user-defined filters with `org-export-filters-alist'
  2448. ;; and filters already in INFO (through ext-plist mechanism).
  2449. (mapc (lambda (p)
  2450. (let* ((prop (car p))
  2451. (info-value (plist-get info prop))
  2452. (default-value (symbol-value (cdr p))))
  2453. (setq plist
  2454. (plist-put plist prop
  2455. ;; Filters in INFO will be called
  2456. ;; before those user provided.
  2457. (append (if (listp info-value) info-value
  2458. (list info-value))
  2459. default-value)))))
  2460. org-export-filters-alist)
  2461. ;; Prepend back-end specific filters to that list.
  2462. (mapc (lambda (p)
  2463. ;; Single values get consed, lists are appended.
  2464. (let ((key (car p)) (value (cdr p)))
  2465. (when value
  2466. (setq plist
  2467. (plist-put
  2468. plist key
  2469. (if (atom value) (cons value (plist-get plist key))
  2470. (append value (plist-get plist key))))))))
  2471. (org-export-get-all-filters (plist-get info :back-end)))
  2472. ;; Return new communication channel.
  2473. (org-combine-plists info plist)))
  2474. ;;; Core functions
  2475. ;;
  2476. ;; This is the room for the main function, `org-export-as', along with
  2477. ;; its derivatives, `org-export-to-buffer', `org-export-to-file' and
  2478. ;; `org-export-string-as'. They differ either by the way they output
  2479. ;; the resulting code (for the first two) or by the input type (for
  2480. ;; the latter). `org-export--copy-to-kill-ring-p' determines if
  2481. ;; output of these function should be added to kill ring.
  2482. ;;
  2483. ;; `org-export-output-file-name' is an auxiliary function meant to be
  2484. ;; used with `org-export-to-file'. With a given extension, it tries
  2485. ;; to provide a canonical file name to write export output to.
  2486. ;;
  2487. ;; Note that `org-export-as' doesn't really parse the current buffer,
  2488. ;; but a copy of it (with the same buffer-local variables and
  2489. ;; visibility), where macros and include keywords are expanded and
  2490. ;; Babel blocks are executed, if appropriate.
  2491. ;; `org-export-with-buffer-copy' macro prepares that copy.
  2492. ;;
  2493. ;; File inclusion is taken care of by
  2494. ;; `org-export-expand-include-keyword' and
  2495. ;; `org-export--prepare-file-contents'. Structure wise, including
  2496. ;; a whole Org file in a buffer often makes little sense. For
  2497. ;; example, if the file contains a headline and the include keyword
  2498. ;; was within an item, the item should contain the headline. That's
  2499. ;; why file inclusion should be done before any structure can be
  2500. ;; associated to the file, that is before parsing.
  2501. ;;
  2502. ;; `org-export-insert-default-template' is a command to insert
  2503. ;; a default template (or a back-end specific template) at point or in
  2504. ;; current subtree.
  2505. (defun org-export-copy-buffer ()
  2506. "Return a copy of the current buffer.
  2507. The copy preserves Org buffer-local variables, visibility and
  2508. narrowing."
  2509. (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
  2510. (new-buf (generate-new-buffer (buffer-name))))
  2511. (with-current-buffer new-buf
  2512. (funcall copy-buffer-fun)
  2513. (set-buffer-modified-p nil))
  2514. new-buf))
  2515. (defmacro org-export-with-buffer-copy (&rest body)
  2516. "Apply BODY in a copy of the current buffer.
  2517. The copy preserves local variables, visibility and contents of
  2518. the original buffer. Point is at the beginning of the buffer
  2519. when BODY is applied."
  2520. (declare (debug t))
  2521. (org-with-gensyms (buf-copy)
  2522. `(let ((,buf-copy (org-export-copy-buffer)))
  2523. (unwind-protect
  2524. (with-current-buffer ,buf-copy
  2525. (goto-char (point-min))
  2526. (progn ,@body))
  2527. (and (buffer-live-p ,buf-copy)
  2528. ;; Kill copy without confirmation.
  2529. (progn (with-current-buffer ,buf-copy
  2530. (restore-buffer-modified-p nil))
  2531. (kill-buffer ,buf-copy)))))))
  2532. (defun org-export--generate-copy-script (buffer)
  2533. "Generate a function duplicating BUFFER.
  2534. The copy will preserve local variables, visibility, contents and
  2535. narrowing of the original buffer. If a region was active in
  2536. BUFFER, contents will be narrowed to that region instead.
  2537. The resulting function can be evaled at a later time, from
  2538. another buffer, effectively cloning the original buffer there.
  2539. The function assumes BUFFER's major mode is `org-mode'."
  2540. (with-current-buffer buffer
  2541. `(lambda ()
  2542. (let ((inhibit-modification-hooks t))
  2543. ;; Set major mode. Ignore `org-mode-hook' as it has been run
  2544. ;; already in BUFFER.
  2545. (let ((org-mode-hook nil) (org-inhibit-startup t)) (org-mode))
  2546. ;; Copy specific buffer local variables and variables set
  2547. ;; through BIND keywords.
  2548. ,@(let ((bound-variables (org-export--list-bound-variables))
  2549. vars)
  2550. (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars)
  2551. (when (consp entry)
  2552. (let ((var (car entry))
  2553. (val (cdr entry)))
  2554. (and (not (eq var 'org-font-lock-keywords))
  2555. (or (memq var
  2556. '(default-directory
  2557. buffer-file-name
  2558. buffer-file-coding-system))
  2559. (assq var bound-variables)
  2560. (string-match "^\\(org-\\|orgtbl-\\)"
  2561. (symbol-name var)))
  2562. ;; Skip unreadable values, as they cannot be
  2563. ;; sent to external process.
  2564. (or (not val) (ignore-errors (read (format "%S" val))))
  2565. (push `(set (make-local-variable (quote ,var))
  2566. (quote ,val))
  2567. vars))))))
  2568. ;; Whole buffer contents.
  2569. (insert
  2570. ,(org-with-wide-buffer
  2571. (buffer-substring-no-properties
  2572. (point-min) (point-max))))
  2573. ;; Narrowing.
  2574. ,(if (org-region-active-p)
  2575. `(narrow-to-region ,(region-beginning) ,(region-end))
  2576. `(narrow-to-region ,(point-min) ,(point-max)))
  2577. ;; Current position of point.
  2578. (goto-char ,(point))
  2579. ;; Overlays with invisible property.
  2580. ,@(let (ov-set)
  2581. (mapc
  2582. (lambda (ov)
  2583. (let ((invis-prop (overlay-get ov 'invisible)))
  2584. (when invis-prop
  2585. (push `(overlay-put
  2586. (make-overlay ,(overlay-start ov)
  2587. ,(overlay-end ov))
  2588. 'invisible (quote ,invis-prop))
  2589. ov-set))))
  2590. (overlays-in (point-min) (point-max)))
  2591. ov-set)))))
  2592. ;;;###autoload
  2593. (defun org-export-as
  2594. (backend &optional subtreep visible-only body-only ext-plist)
  2595. "Transcode current Org buffer into BACKEND code.
  2596. BACKEND is either an export back-end, as returned by, e.g.,
  2597. `org-export-create-backend', or a symbol referring to
  2598. a registered back-end.
  2599. If narrowing is active in the current buffer, only transcode its
  2600. narrowed part.
  2601. If a region is active, transcode that region.
  2602. When optional argument SUBTREEP is non-nil, transcode the
  2603. sub-tree at point, extracting information from the headline
  2604. properties first.
  2605. When optional argument VISIBLE-ONLY is non-nil, don't export
  2606. contents of hidden elements.
  2607. When optional argument BODY-ONLY is non-nil, only return body
  2608. code, without surrounding template.
  2609. Optional argument EXT-PLIST, when provided, is a property list
  2610. with external parameters overriding Org default settings, but
  2611. still inferior to file-local settings.
  2612. Return code as a string."
  2613. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  2614. (org-export-barf-if-invalid-backend backend)
  2615. (save-excursion
  2616. (save-restriction
  2617. ;; Narrow buffer to an appropriate region or subtree for
  2618. ;; parsing. If parsing subtree, be sure to remove main headline
  2619. ;; too.
  2620. (cond ((org-region-active-p)
  2621. (narrow-to-region (region-beginning) (region-end)))
  2622. (subtreep
  2623. (org-narrow-to-subtree)
  2624. (goto-char (point-min))
  2625. (forward-line)
  2626. (narrow-to-region (point) (point-max))))
  2627. ;; Initialize communication channel with original buffer
  2628. ;; attributes, unavailable in its copy.
  2629. (let* ((org-export-current-backend backend)
  2630. (info (org-combine-plists
  2631. (list :export-options
  2632. (delq nil
  2633. (list (and subtreep 'subtree)
  2634. (and visible-only 'visible-only)
  2635. (and body-only 'body-only))))
  2636. (org-export--get-buffer-attributes)))
  2637. tree)
  2638. ;; Store default title in `org-export--default-title' so that
  2639. ;; `org-export-get-environment' can access it from buffer's
  2640. ;; copy and then add it properly to communication channel.
  2641. (org-export-store-default-title)
  2642. ;; Update communication channel and get parse tree. Buffer
  2643. ;; isn't parsed directly. Instead, a temporary copy is
  2644. ;; created, where include keywords, macros are expanded and
  2645. ;; code blocks are evaluated.
  2646. (org-export-with-buffer-copy
  2647. ;; Run first hook with current back-end's name as argument.
  2648. (run-hook-with-args 'org-export-before-processing-hook
  2649. (org-export-backend-name backend))
  2650. (org-export-expand-include-keyword)
  2651. ;; Update macro templates since #+INCLUDE keywords might have
  2652. ;; added some new ones.
  2653. (org-macro-initialize-templates)
  2654. (org-macro-replace-all org-macro-templates)
  2655. (org-export-execute-babel-code)
  2656. ;; Update radio targets since keyword inclusion might have
  2657. ;; added some more.
  2658. (org-update-radio-target-regexp)
  2659. ;; Run last hook with current back-end's name as argument.
  2660. (goto-char (point-min))
  2661. (save-excursion
  2662. (run-hook-with-args 'org-export-before-parsing-hook
  2663. (org-export-backend-name backend)))
  2664. ;; Update communication channel with environment. Also
  2665. ;; install user's and developer's filters.
  2666. (setq info
  2667. (org-export-install-filters
  2668. (org-combine-plists
  2669. info (org-export-get-environment backend subtreep ext-plist))))
  2670. ;; Expand export-specific set of macros: {{{author}}},
  2671. ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
  2672. ;; once regular macros have been expanded, since document
  2673. ;; keywords may contain one of them.
  2674. (org-macro-replace-all
  2675. (list (cons "author"
  2676. (org-element-interpret-data (plist-get info :author)))
  2677. (cons "date"
  2678. (org-element-interpret-data (plist-get info :date)))
  2679. ;; EMAIL is not a parsed keyword: store it as-is.
  2680. (cons "email" (or (plist-get info :email) ""))
  2681. (cons "title"
  2682. (org-element-interpret-data (plist-get info :title)))))
  2683. ;; Call options filters and update export options. We do not
  2684. ;; use `org-export-filter-apply-functions' here since the
  2685. ;; arity of such filters is different.
  2686. (let ((backend-name (org-export-backend-name backend)))
  2687. (dolist (filter (plist-get info :filter-options))
  2688. (let ((result (funcall filter info backend-name)))
  2689. (when result (setq info result)))))
  2690. ;; Parse buffer and call parse-tree filter on it.
  2691. (setq tree
  2692. (org-export-filter-apply-functions
  2693. (plist-get info :filter-parse-tree)
  2694. (org-element-parse-buffer nil visible-only) info))
  2695. ;; Now tree is complete, compute its properties and add them
  2696. ;; to communication channel.
  2697. (setq info
  2698. (org-combine-plists
  2699. info (org-export-collect-tree-properties tree info)))
  2700. ;; Eventually transcode TREE. Wrap the resulting string into
  2701. ;; a template.
  2702. (let* ((body (org-element-normalize-string
  2703. (or (org-export-data tree info) "")))
  2704. (inner-template (cdr (assq 'inner-template
  2705. (plist-get info :translate-alist))))
  2706. (full-body (if (not (functionp inner-template)) body
  2707. (funcall inner-template body info)))
  2708. (template (cdr (assq 'template
  2709. (plist-get info :translate-alist)))))
  2710. ;; Remove all text properties since they cannot be
  2711. ;; retrieved from an external process. Finally call
  2712. ;; final-output filter and return result.
  2713. (org-no-properties
  2714. (org-export-filter-apply-functions
  2715. (plist-get info :filter-final-output)
  2716. (if (or (not (functionp template)) body-only) full-body
  2717. (funcall template full-body info))
  2718. info))))))))
  2719. ;;;###autoload
  2720. (defun org-export-to-buffer
  2721. (backend buffer &optional subtreep visible-only body-only ext-plist)
  2722. "Call `org-export-as' with output to a specified buffer.
  2723. BACKEND is either an export back-end, as returned by, e.g.,
  2724. `org-export-create-backend', or a symbol referring to
  2725. a registered back-end.
  2726. BUFFER is the output buffer. If it already exists, it will be
  2727. erased first, otherwise, it will be created.
  2728. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  2729. EXT-PLIST are similar to those used in `org-export-as', which
  2730. see.
  2731. Depending on `org-export-copy-to-kill-ring', add buffer contents
  2732. to kill ring. Return buffer."
  2733. (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))
  2734. (buffer (get-buffer-create buffer)))
  2735. (with-current-buffer buffer
  2736. (erase-buffer)
  2737. (insert out)
  2738. (goto-char (point-min)))
  2739. ;; Maybe add buffer contents to kill ring.
  2740. (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p out))
  2741. (org-kill-new out))
  2742. ;; Return buffer.
  2743. buffer))
  2744. ;;;###autoload
  2745. (defun org-export-to-file
  2746. (backend file &optional subtreep visible-only body-only ext-plist)
  2747. "Call `org-export-as' with output to a specified file.
  2748. BACKEND is either an export back-end, as returned by, e.g.,
  2749. `org-export-create-backend', or a symbol referring to
  2750. a registered back-end. FILE is the name of the output file, as
  2751. a string.
  2752. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  2753. EXT-PLIST are similar to those used in `org-export-as', which
  2754. see.
  2755. Depending on `org-export-copy-to-kill-ring', add file contents
  2756. to kill ring. Return output file's name."
  2757. ;; Checks for FILE permissions. `write-file' would do the same, but
  2758. ;; we'd rather avoid needless transcoding of parse tree.
  2759. (unless (file-writable-p file) (error "Output file not writable"))
  2760. ;; Insert contents to a temporary buffer and write it to FILE.
  2761. (let ((coding buffer-file-coding-system)
  2762. (out (org-export-as backend subtreep visible-only body-only ext-plist)))
  2763. (with-temp-buffer
  2764. (insert out)
  2765. (let ((coding-system-for-write (or org-export-coding-system coding)))
  2766. (write-file file)))
  2767. ;; Maybe add file contents to kill ring.
  2768. (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p out))
  2769. (org-kill-new out)))
  2770. ;; Return full path.
  2771. file)
  2772. ;;;###autoload
  2773. (defun org-export-string-as (string backend &optional body-only ext-plist)
  2774. "Transcode STRING into BACKEND code.
  2775. BACKEND is either an export back-end, as returned by, e.g.,
  2776. `org-export-create-backend', or a symbol referring to
  2777. a registered back-end.
  2778. When optional argument BODY-ONLY is non-nil, only return body
  2779. code, without preamble nor postamble.
  2780. Optional argument EXT-PLIST, when provided, is a property list
  2781. with external parameters overriding Org default settings, but
  2782. still inferior to file-local settings.
  2783. Return code as a string."
  2784. (with-temp-buffer
  2785. (insert string)
  2786. (let ((org-inhibit-startup t)) (org-mode))
  2787. (org-export-as backend nil nil body-only ext-plist)))
  2788. ;;;###autoload
  2789. (defun org-export-replace-region-by (backend)
  2790. "Replace the active region by its export to BACKEND.
  2791. BACKEND is either an export back-end, as returned by, e.g.,
  2792. `org-export-create-backend', or a symbol referring to
  2793. a registered back-end."
  2794. (if (not (org-region-active-p))
  2795. (user-error "No active region to replace")
  2796. (let* ((beg (region-beginning))
  2797. (end (region-end))
  2798. (str (buffer-substring beg end)) rpl)
  2799. (setq rpl (org-export-string-as str backend t))
  2800. (delete-region beg end)
  2801. (insert rpl))))
  2802. ;;;###autoload
  2803. (defun org-export-insert-default-template (&optional backend subtreep)
  2804. "Insert all export keywords with default values at beginning of line.
  2805. BACKEND is a symbol referring to the name of a registered export
  2806. back-end, for which specific export options should be added to
  2807. the template, or `default' for default template. When it is nil,
  2808. the user will be prompted for a category.
  2809. If SUBTREEP is non-nil, export configuration will be set up
  2810. locally for the subtree through node properties."
  2811. (interactive)
  2812. (unless (derived-mode-p 'org-mode) (user-error "Not in an Org mode buffer"))
  2813. (when (and subtreep (org-before-first-heading-p))
  2814. (user-error "No subtree to set export options for"))
  2815. (let ((node (and subtreep (save-excursion (org-back-to-heading t) (point))))
  2816. (backend
  2817. (or backend
  2818. (intern
  2819. (org-completing-read
  2820. "Options category: "
  2821. (cons "default"
  2822. (mapcar (lambda (b)
  2823. (symbol-name (org-export-backend-name b)))
  2824. org-export--registered-backends))))))
  2825. options keywords)
  2826. ;; Populate OPTIONS and KEYWORDS.
  2827. (dolist (entry (cond ((eq backend 'default) org-export-options-alist)
  2828. ((org-export-backend-p backend)
  2829. (org-export-get-all-options backend))
  2830. (t (org-export-get-all-options
  2831. (org-export-get-backend backend)))))
  2832. (let ((keyword (nth 1 entry))
  2833. (option (nth 2 entry)))
  2834. (cond
  2835. (keyword (unless (assoc keyword keywords)
  2836. (let ((value
  2837. (if (eq (nth 4 entry) 'split)
  2838. (mapconcat 'identity (eval (nth 3 entry)) " ")
  2839. (eval (nth 3 entry)))))
  2840. (push (cons keyword value) keywords))))
  2841. (option (unless (assoc option options)
  2842. (push (cons option (eval (nth 3 entry))) options))))))
  2843. ;; Move to an appropriate location in order to insert options.
  2844. (unless subtreep (beginning-of-line))
  2845. ;; First get TITLE, DATE, AUTHOR and EMAIL if they belong to the
  2846. ;; list of available keywords.
  2847. (when (assoc "TITLE" keywords)
  2848. (let ((title
  2849. (or (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  2850. (and visited-file
  2851. (file-name-sans-extension
  2852. (file-name-nondirectory visited-file))))
  2853. (buffer-name (buffer-base-buffer)))))
  2854. (if (not subtreep) (insert (format "#+TITLE: %s\n" title))
  2855. (org-entry-put node "EXPORT_TITLE" title))))
  2856. (when (assoc "DATE" keywords)
  2857. (let ((date (with-temp-buffer (org-insert-time-stamp (current-time)))))
  2858. (if (not subtreep) (insert "#+DATE: " date "\n")
  2859. (org-entry-put node "EXPORT_DATE" date))))
  2860. (when (assoc "AUTHOR" keywords)
  2861. (let ((author (cdr (assoc "AUTHOR" keywords))))
  2862. (if subtreep (org-entry-put node "EXPORT_AUTHOR" author)
  2863. (insert
  2864. (format "#+AUTHOR:%s\n"
  2865. (if (not (org-string-nw-p author)) ""
  2866. (concat " " author)))))))
  2867. (when (assoc "EMAIL" keywords)
  2868. (let ((email (cdr (assoc "EMAIL" keywords))))
  2869. (if subtreep (org-entry-put node "EXPORT_EMAIL" email)
  2870. (insert
  2871. (format "#+EMAIL:%s\n"
  2872. (if (not (org-string-nw-p email)) ""
  2873. (concat " " email)))))))
  2874. ;; Then (multiple) OPTIONS lines. Never go past fill-column.
  2875. (when options
  2876. (let ((items
  2877. (mapcar
  2878. (lambda (opt)
  2879. (format "%s:%s" (car opt) (format "%s" (cdr opt))))
  2880. (sort options (lambda (k1 k2) (string< (car k1) (car k2)))))))
  2881. (if subtreep
  2882. (org-entry-put
  2883. node "EXPORT_OPTIONS" (mapconcat 'identity items " "))
  2884. (while items
  2885. (insert "#+OPTIONS:")
  2886. (let ((width 10))
  2887. (while (and items
  2888. (< (+ width (length (car items)) 1) fill-column))
  2889. (let ((item (pop items)))
  2890. (insert " " item)
  2891. (incf width (1+ (length item))))))
  2892. (insert "\n")))))
  2893. ;; And the rest of keywords.
  2894. (dolist (key (sort keywords (lambda (k1 k2) (string< (car k1) (car k2)))))
  2895. (unless (member (car key) '("TITLE" "DATE" "AUTHOR" "EMAIL"))
  2896. (let ((val (cdr key)))
  2897. (if subtreep (org-entry-put node (concat "EXPORT_" (car key)) val)
  2898. (insert
  2899. (format "#+%s:%s\n"
  2900. (car key)
  2901. (if (org-string-nw-p val) (format " %s" val) "")))))))))
  2902. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  2903. "Return output file's name according to buffer specifications.
  2904. EXTENSION is a string representing the output file extension,
  2905. with the leading dot.
  2906. With a non-nil optional argument SUBTREEP, try to determine
  2907. output file's name by looking for \"EXPORT_FILE_NAME\" property
  2908. of subtree at point.
  2909. When optional argument PUB-DIR is set, use it as the publishing
  2910. directory.
  2911. When optional argument VISIBLE-ONLY is non-nil, don't export
  2912. contents of hidden elements.
  2913. Return file name as a string."
  2914. (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
  2915. (base-name
  2916. ;; File name may come from EXPORT_FILE_NAME subtree
  2917. ;; property, assuming point is at beginning of said
  2918. ;; sub-tree.
  2919. (file-name-sans-extension
  2920. (or (and subtreep
  2921. (org-entry-get
  2922. (save-excursion
  2923. (ignore-errors (org-back-to-heading) (point)))
  2924. "EXPORT_FILE_NAME" t))
  2925. ;; File name may be extracted from buffer's associated
  2926. ;; file, if any.
  2927. (and visited-file (file-name-nondirectory visited-file))
  2928. ;; Can't determine file name on our own: Ask user.
  2929. (let ((read-file-name-function
  2930. (and org-completion-use-ido 'ido-read-file-name)))
  2931. (read-file-name
  2932. "Output file: " pub-dir nil nil nil
  2933. (lambda (name)
  2934. (string= (file-name-extension name t) extension)))))))
  2935. (output-file
  2936. ;; Build file name. Enforce EXTENSION over whatever user
  2937. ;; may have come up with. PUB-DIR, if defined, always has
  2938. ;; precedence over any provided path.
  2939. (cond
  2940. (pub-dir
  2941. (concat (file-name-as-directory pub-dir)
  2942. (file-name-nondirectory base-name)
  2943. extension))
  2944. ((file-name-absolute-p base-name) (concat base-name extension))
  2945. (t (concat (file-name-as-directory ".") base-name extension)))))
  2946. ;; If writing to OUTPUT-FILE would overwrite original file, append
  2947. ;; EXTENSION another time to final name.
  2948. (if (and visited-file (org-file-equal-p visited-file output-file))
  2949. (concat output-file extension)
  2950. output-file)))
  2951. (defun org-export-expand-include-keyword (&optional included dir)
  2952. "Expand every include keyword in buffer.
  2953. Optional argument INCLUDED is a list of included file names along
  2954. with their line restriction, when appropriate. It is used to
  2955. avoid infinite recursion. Optional argument DIR is the current
  2956. working directory. It is used to properly resolve relative
  2957. paths."
  2958. (let ((case-fold-search t))
  2959. (goto-char (point-min))
  2960. (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
  2961. (let ((element (save-match-data (org-element-at-point))))
  2962. (when (eq (org-element-type element) 'keyword)
  2963. (beginning-of-line)
  2964. ;; Extract arguments from keyword's value.
  2965. (let* ((value (org-element-property :value element))
  2966. (ind (org-get-indentation))
  2967. (file (and (string-match
  2968. "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value)
  2969. (prog1 (expand-file-name
  2970. (org-remove-double-quotes
  2971. (match-string 1 value))
  2972. dir)
  2973. (setq value (replace-match "" nil nil value)))))
  2974. (lines
  2975. (and (string-match
  2976. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
  2977. value)
  2978. (prog1 (match-string 1 value)
  2979. (setq value (replace-match "" nil nil value)))))
  2980. (env (cond ((string-match "\\<example\\>" value) 'example)
  2981. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  2982. (match-string 1 value))))
  2983. ;; Minimal level of included file defaults to the child
  2984. ;; level of the current headline, if any, or one. It
  2985. ;; only applies is the file is meant to be included as
  2986. ;; an Org one.
  2987. (minlevel
  2988. (and (not env)
  2989. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  2990. (prog1 (string-to-number (match-string 1 value))
  2991. (setq value (replace-match "" nil nil value)))
  2992. (let ((cur (org-current-level)))
  2993. (if cur (1+ (org-reduced-level cur)) 1))))))
  2994. ;; Remove keyword.
  2995. (delete-region (point) (progn (forward-line) (point)))
  2996. (cond
  2997. ((not file) nil)
  2998. ((not (file-readable-p file))
  2999. (error "Cannot include file %s" file))
  3000. ;; Check if files has already been parsed. Look after
  3001. ;; inclusion lines too, as different parts of the same file
  3002. ;; can be included too.
  3003. ((member (list file lines) included)
  3004. (error "Recursive file inclusion: %s" file))
  3005. (t
  3006. (cond
  3007. ((eq env 'example)
  3008. (insert
  3009. (let ((ind-str (make-string ind ? ))
  3010. (contents
  3011. (org-escape-code-in-string
  3012. (org-export--prepare-file-contents file lines))))
  3013. (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
  3014. ind-str contents ind-str))))
  3015. ((stringp env)
  3016. (insert
  3017. (let ((ind-str (make-string ind ? ))
  3018. (contents
  3019. (org-escape-code-in-string
  3020. (org-export--prepare-file-contents file lines))))
  3021. (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
  3022. ind-str env contents ind-str))))
  3023. (t
  3024. (insert
  3025. (with-temp-buffer
  3026. (let ((org-inhibit-startup t)) (org-mode))
  3027. (insert
  3028. (org-export--prepare-file-contents file lines ind minlevel))
  3029. (org-export-expand-include-keyword
  3030. (cons (list file lines) included)
  3031. (file-name-directory file))
  3032. (buffer-string)))))))))))))
  3033. (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
  3034. "Prepare the contents of FILE for inclusion and return them as a string.
  3035. When optional argument LINES is a string specifying a range of
  3036. lines, include only those lines.
  3037. Optional argument IND, when non-nil, is an integer specifying the
  3038. global indentation of returned contents. Since its purpose is to
  3039. allow an included file to stay in the same environment it was
  3040. created \(i.e. a list item), it doesn't apply past the first
  3041. headline encountered.
  3042. Optional argument MINLEVEL, when non-nil, is an integer
  3043. specifying the level that any top-level headline in the included
  3044. file should have."
  3045. (with-temp-buffer
  3046. (insert-file-contents file)
  3047. (when lines
  3048. (let* ((lines (split-string lines "-"))
  3049. (lbeg (string-to-number (car lines)))
  3050. (lend (string-to-number (cadr lines)))
  3051. (beg (if (zerop lbeg) (point-min)
  3052. (goto-char (point-min))
  3053. (forward-line (1- lbeg))
  3054. (point)))
  3055. (end (if (zerop lend) (point-max)
  3056. (goto-char (point-min))
  3057. (forward-line (1- lend))
  3058. (point))))
  3059. (narrow-to-region beg end)))
  3060. ;; Remove blank lines at beginning and end of contents. The logic
  3061. ;; behind that removal is that blank lines around include keyword
  3062. ;; override blank lines in included file.
  3063. (goto-char (point-min))
  3064. (org-skip-whitespace)
  3065. (beginning-of-line)
  3066. (delete-region (point-min) (point))
  3067. (goto-char (point-max))
  3068. (skip-chars-backward " \r\t\n")
  3069. (forward-line)
  3070. (delete-region (point) (point-max))
  3071. ;; If IND is set, preserve indentation of include keyword until
  3072. ;; the first headline encountered.
  3073. (when ind
  3074. (unless (eq major-mode 'org-mode)
  3075. (let ((org-inhibit-startup t)) (org-mode)))
  3076. (goto-char (point-min))
  3077. (let ((ind-str (make-string ind ? )))
  3078. (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
  3079. ;; Do not move footnote definitions out of column 0.
  3080. (unless (and (looking-at org-footnote-definition-re)
  3081. (eq (org-element-type (org-element-at-point))
  3082. 'footnote-definition))
  3083. (insert ind-str))
  3084. (forward-line))))
  3085. ;; When MINLEVEL is specified, compute minimal level for headlines
  3086. ;; in the file (CUR-MIN), and remove stars to each headline so
  3087. ;; that headlines with minimal level have a level of MINLEVEL.
  3088. (when minlevel
  3089. (unless (eq major-mode 'org-mode)
  3090. (let ((org-inhibit-startup t)) (org-mode)))
  3091. (org-with-limited-levels
  3092. (let ((levels (org-map-entries
  3093. (lambda () (org-reduced-level (org-current-level))))))
  3094. (when levels
  3095. (let ((offset (- minlevel (apply 'min levels))))
  3096. (unless (zerop offset)
  3097. (when org-odd-levels-only (setq offset (* offset 2)))
  3098. ;; Only change stars, don't bother moving whole
  3099. ;; sections.
  3100. (org-map-entries
  3101. (lambda () (if (< offset 0) (delete-char (abs offset))
  3102. (insert (make-string offset ?*)))))))))))
  3103. (org-element-normalize-string (buffer-string))))
  3104. (defun org-export-execute-babel-code ()
  3105. "Execute every Babel code in the visible part of current buffer."
  3106. ;; Get a pristine copy of current buffer so Babel references can be
  3107. ;; properly resolved.
  3108. (let ((reference (org-export-copy-buffer)))
  3109. (unwind-protect (let ((org-current-export-file reference))
  3110. (org-babel-exp-process-buffer))
  3111. (kill-buffer reference))))
  3112. (defun org-export--copy-to-kill-ring-p ()
  3113. "Return a non-nil value when output should be added to the kill ring.
  3114. See also `org-export-copy-to-kill-ring'."
  3115. (if (eq org-export-copy-to-kill-ring 'if-interactive)
  3116. (not (or executing-kbd-macro noninteractive))
  3117. (eq org-export-copy-to-kill-ring t)))
  3118. ;;; Tools For Back-Ends
  3119. ;;
  3120. ;; A whole set of tools is available to help build new exporters. Any
  3121. ;; function general enough to have its use across many back-ends
  3122. ;; should be added here.
  3123. ;;;; For Affiliated Keywords
  3124. ;;
  3125. ;; `org-export-read-attribute' reads a property from a given element
  3126. ;; as a plist. It can be used to normalize affiliated keywords'
  3127. ;; syntax.
  3128. ;;
  3129. ;; Since captions can span over multiple lines and accept dual values,
  3130. ;; their internal representation is a bit tricky. Therefore,
  3131. ;; `org-export-get-caption' transparently returns a given element's
  3132. ;; caption as a secondary string.
  3133. (defun org-export-read-attribute (attribute element &optional property)
  3134. "Turn ATTRIBUTE property from ELEMENT into a plist.
  3135. When optional argument PROPERTY is non-nil, return the value of
  3136. that property within attributes.
  3137. This function assumes attributes are defined as \":keyword
  3138. value\" pairs. It is appropriate for `:attr_html' like
  3139. properties.
  3140. All values will become strings except the empty string and
  3141. \"nil\", which will become nil. Also, values containing only
  3142. double quotes will be read as-is, which means that \"\" value
  3143. will become the empty string."
  3144. (let* ((prepare-value
  3145. (lambda (str)
  3146. (save-match-data
  3147. (cond ((member str '(nil "" "nil")) nil)
  3148. ((string-match "^\"\\(\"+\\)?\"$" str)
  3149. (or (match-string 1 str) ""))
  3150. (t str)))))
  3151. (attributes
  3152. (let ((value (org-element-property attribute element)))
  3153. (when value
  3154. (let ((s (mapconcat 'identity value " ")) result)
  3155. (while (string-match
  3156. "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
  3157. s)
  3158. (let ((value (substring s 0 (match-beginning 0))))
  3159. (push (funcall prepare-value value) result))
  3160. (push (intern (match-string 1 s)) result)
  3161. (setq s (substring s (match-end 0))))
  3162. ;; Ignore any string before first property with `cdr'.
  3163. (cdr (nreverse (cons (funcall prepare-value s) result))))))))
  3164. (if property (plist-get attributes property) attributes)))
  3165. (defun org-export-get-caption (element &optional shortp)
  3166. "Return caption from ELEMENT as a secondary string.
  3167. When optional argument SHORTP is non-nil, return short caption,
  3168. as a secondary string, instead.
  3169. Caption lines are separated by a white space."
  3170. (let ((full-caption (org-element-property :caption element)) caption)
  3171. (dolist (line full-caption (cdr caption))
  3172. (let ((cap (funcall (if shortp 'cdr 'car) line)))
  3173. (when cap
  3174. (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
  3175. ;;;; For Derived Back-ends
  3176. ;;
  3177. ;; `org-export-with-backend' is a function allowing to locally use
  3178. ;; another back-end to transcode some object or element. In a derived
  3179. ;; back-end, it may be used as a fall-back function once all specific
  3180. ;; cases have been treated.
  3181. (defun org-export-with-backend (backend data &optional contents info)
  3182. "Call a transcoder from BACKEND on DATA.
  3183. BACKEND is an export back-end, as returned by, e.g.,
  3184. `org-export-create-backend', or a symbol referring to
  3185. a registered back-end. DATA is an Org element, object, secondary
  3186. string or string. CONTENTS, when non-nil, is the transcoded
  3187. contents of DATA element, as a string. INFO, when non-nil, is
  3188. the communication channel used for export, as a plist."
  3189. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  3190. (org-export-barf-if-invalid-backend backend)
  3191. (let ((type (org-element-type data)))
  3192. (if (memq type '(nil org-data)) (error "No foreign transcoder available")
  3193. (let ((transcoder
  3194. (cdr (assq type (org-export-get-all-transcoders backend)))))
  3195. (if (functionp transcoder) (funcall transcoder data contents info)
  3196. (error "No foreign transcoder available"))))))
  3197. ;;;; For Export Snippets
  3198. ;;
  3199. ;; Every export snippet is transmitted to the back-end. Though, the
  3200. ;; latter will only retain one type of export-snippet, ignoring
  3201. ;; others, based on the former's target back-end. The function
  3202. ;; `org-export-snippet-backend' returns that back-end for a given
  3203. ;; export-snippet.
  3204. (defun org-export-snippet-backend (export-snippet)
  3205. "Return EXPORT-SNIPPET targeted back-end as a symbol.
  3206. Translation, with `org-export-snippet-translation-alist', is
  3207. applied."
  3208. (let ((back-end (org-element-property :back-end export-snippet)))
  3209. (intern
  3210. (or (cdr (assoc back-end org-export-snippet-translation-alist))
  3211. back-end))))
  3212. ;;;; For Footnotes
  3213. ;;
  3214. ;; `org-export-collect-footnote-definitions' is a tool to list
  3215. ;; actually used footnotes definitions in the whole parse tree, or in
  3216. ;; a headline, in order to add footnote listings throughout the
  3217. ;; transcoded data.
  3218. ;;
  3219. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  3220. ;; back-ends, when they need to attach the footnote definition only to
  3221. ;; the first occurrence of the corresponding label.
  3222. ;;
  3223. ;; `org-export-get-footnote-definition' and
  3224. ;; `org-export-get-footnote-number' provide easier access to
  3225. ;; additional information relative to a footnote reference.
  3226. (defun org-export-collect-footnote-definitions (data info)
  3227. "Return an alist between footnote numbers, labels and definitions.
  3228. DATA is the parse tree from which definitions are collected.
  3229. INFO is the plist used as a communication channel.
  3230. Definitions are sorted by order of references. They either
  3231. appear as Org data or as a secondary string for inlined
  3232. footnotes. Unreferenced definitions are ignored."
  3233. (let* (num-alist
  3234. collect-fn ; for byte-compiler.
  3235. (collect-fn
  3236. (function
  3237. (lambda (data)
  3238. ;; Collect footnote number, label and definition in DATA.
  3239. (org-element-map data 'footnote-reference
  3240. (lambda (fn)
  3241. (when (org-export-footnote-first-reference-p fn info)
  3242. (let ((def (org-export-get-footnote-definition fn info)))
  3243. (push
  3244. (list (org-export-get-footnote-number fn info)
  3245. (org-element-property :label fn)
  3246. def)
  3247. num-alist)
  3248. ;; Also search in definition for nested footnotes.
  3249. (when (eq (org-element-property :type fn) 'standard)
  3250. (funcall collect-fn def)))))
  3251. ;; Don't enter footnote definitions since it will happen
  3252. ;; when their first reference is found.
  3253. info nil 'footnote-definition)))))
  3254. (funcall collect-fn (plist-get info :parse-tree))
  3255. (reverse num-alist)))
  3256. (defun org-export-footnote-first-reference-p (footnote-reference info)
  3257. "Non-nil when a footnote reference is the first one for its label.
  3258. FOOTNOTE-REFERENCE is the footnote reference being considered.
  3259. INFO is the plist used as a communication channel."
  3260. (let ((label (org-element-property :label footnote-reference)))
  3261. ;; Anonymous footnotes are always a first reference.
  3262. (if (not label) t
  3263. ;; Otherwise, return the first footnote with the same LABEL and
  3264. ;; test if it is equal to FOOTNOTE-REFERENCE.
  3265. (let* (search-refs ; for byte-compiler.
  3266. (search-refs
  3267. (function
  3268. (lambda (data)
  3269. (org-element-map data 'footnote-reference
  3270. (lambda (fn)
  3271. (cond
  3272. ((string= (org-element-property :label fn) label)
  3273. (throw 'exit fn))
  3274. ;; If FN isn't inlined, be sure to traverse its
  3275. ;; definition before resuming search. See
  3276. ;; comments in `org-export-get-footnote-number'
  3277. ;; for more information.
  3278. ((eq (org-element-property :type fn) 'standard)
  3279. (funcall search-refs
  3280. (org-export-get-footnote-definition fn info)))))
  3281. ;; Don't enter footnote definitions since it will
  3282. ;; happen when their first reference is found.
  3283. info 'first-match 'footnote-definition)))))
  3284. (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
  3285. footnote-reference)))))
  3286. (defun org-export-get-footnote-definition (footnote-reference info)
  3287. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  3288. INFO is the plist used as a communication channel. If no such
  3289. definition can be found, return the \"DEFINITION NOT FOUND\"
  3290. string."
  3291. (let ((label (org-element-property :label footnote-reference)))
  3292. (or (org-element-property :inline-definition footnote-reference)
  3293. (cdr (assoc label (plist-get info :footnote-definition-alist)))
  3294. "DEFINITION NOT FOUND.")))
  3295. (defun org-export-get-footnote-number (footnote info)
  3296. "Return number associated to a footnote.
  3297. FOOTNOTE is either a footnote reference or a footnote definition.
  3298. INFO is the plist used as a communication channel."
  3299. (let* ((label (org-element-property :label footnote))
  3300. seen-refs
  3301. search-ref ; For byte-compiler.
  3302. (search-ref
  3303. (function
  3304. (lambda (data)
  3305. ;; Search footnote references through DATA, filling
  3306. ;; SEEN-REFS along the way.
  3307. (org-element-map data 'footnote-reference
  3308. (lambda (fn)
  3309. (let ((fn-lbl (org-element-property :label fn)))
  3310. (cond
  3311. ;; Anonymous footnote match: return number.
  3312. ((and (not fn-lbl) (eq fn footnote))
  3313. (throw 'exit (1+ (length seen-refs))))
  3314. ;; Labels match: return number.
  3315. ((and label (string= label fn-lbl))
  3316. (throw 'exit (1+ (length seen-refs))))
  3317. ;; Anonymous footnote: it's always a new one.
  3318. ;; Also, be sure to return nil from the `cond' so
  3319. ;; `first-match' doesn't get us out of the loop.
  3320. ((not fn-lbl) (push 'inline seen-refs) nil)
  3321. ;; Label not seen so far: add it so SEEN-REFS.
  3322. ;;
  3323. ;; Also search for subsequent references in
  3324. ;; footnote definition so numbering follows
  3325. ;; reading logic. Note that we don't have to care
  3326. ;; about inline definitions, since
  3327. ;; `org-element-map' already traverses them at the
  3328. ;; right time.
  3329. ;;
  3330. ;; Once again, return nil to stay in the loop.
  3331. ((not (member fn-lbl seen-refs))
  3332. (push fn-lbl seen-refs)
  3333. (funcall search-ref
  3334. (org-export-get-footnote-definition fn info))
  3335. nil))))
  3336. ;; Don't enter footnote definitions since it will
  3337. ;; happen when their first reference is found.
  3338. info 'first-match 'footnote-definition)))))
  3339. (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
  3340. ;;;; For Headlines
  3341. ;;
  3342. ;; `org-export-get-relative-level' is a shortcut to get headline
  3343. ;; level, relatively to the lower headline level in the parsed tree.
  3344. ;;
  3345. ;; `org-export-get-headline-number' returns the section number of an
  3346. ;; headline, while `org-export-number-to-roman' allows to convert it
  3347. ;; to roman numbers.
  3348. ;;
  3349. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  3350. ;; `org-export-last-sibling-p' are three useful predicates when it
  3351. ;; comes to fulfill the `:headline-levels' property.
  3352. ;;
  3353. ;; `org-export-get-tags', `org-export-get-category' and
  3354. ;; `org-export-get-node-property' extract useful information from an
  3355. ;; headline or a parent headline. They all handle inheritance.
  3356. ;;
  3357. ;; `org-export-get-alt-title' tries to retrieve an alternative title,
  3358. ;; as a secondary string, suitable for table of contents. It falls
  3359. ;; back onto default title.
  3360. (defun org-export-get-relative-level (headline info)
  3361. "Return HEADLINE relative level within current parsed tree.
  3362. INFO is a plist holding contextual information."
  3363. (+ (org-element-property :level headline)
  3364. (or (plist-get info :headline-offset) 0)))
  3365. (defun org-export-low-level-p (headline info)
  3366. "Non-nil when HEADLINE is considered as low level.
  3367. INFO is a plist used as a communication channel.
  3368. A low level headlines has a relative level greater than
  3369. `:headline-levels' property value.
  3370. Return value is the difference between HEADLINE relative level
  3371. and the last level being considered as high enough, or nil."
  3372. (let ((limit (plist-get info :headline-levels)))
  3373. (when (wholenump limit)
  3374. (let ((level (org-export-get-relative-level headline info)))
  3375. (and (> level limit) (- level limit))))))
  3376. (defun org-export-get-headline-number (headline info)
  3377. "Return HEADLINE numbering as a list of numbers.
  3378. INFO is a plist holding contextual information."
  3379. (cdr (assoc headline (plist-get info :headline-numbering))))
  3380. (defun org-export-numbered-headline-p (headline info)
  3381. "Return a non-nil value if HEADLINE element should be numbered.
  3382. INFO is a plist used as a communication channel."
  3383. (let ((sec-num (plist-get info :section-numbers))
  3384. (level (org-export-get-relative-level headline info)))
  3385. (if (wholenump sec-num) (<= level sec-num) sec-num)))
  3386. (defun org-export-number-to-roman (n)
  3387. "Convert integer N into a roman numeral."
  3388. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  3389. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  3390. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  3391. ( 1 . "I")))
  3392. (res ""))
  3393. (if (<= n 0)
  3394. (number-to-string n)
  3395. (while roman
  3396. (if (>= n (caar roman))
  3397. (setq n (- n (caar roman))
  3398. res (concat res (cdar roman)))
  3399. (pop roman)))
  3400. res)))
  3401. (defun org-export-get-tags (element info &optional tags inherited)
  3402. "Return list of tags associated to ELEMENT.
  3403. ELEMENT has either an `headline' or an `inlinetask' type. INFO
  3404. is a plist used as a communication channel.
  3405. Select tags (see `org-export-select-tags') and exclude tags (see
  3406. `org-export-exclude-tags') are removed from the list.
  3407. When non-nil, optional argument TAGS should be a list of strings.
  3408. Any tag belonging to this list will also be removed.
  3409. When optional argument INHERITED is non-nil, tags can also be
  3410. inherited from parent headlines and FILETAGS keywords."
  3411. (org-remove-if
  3412. (lambda (tag) (or (member tag (plist-get info :select-tags))
  3413. (member tag (plist-get info :exclude-tags))
  3414. (member tag tags)))
  3415. (if (not inherited) (org-element-property :tags element)
  3416. ;; Build complete list of inherited tags.
  3417. (let ((current-tag-list (org-element-property :tags element)))
  3418. (mapc
  3419. (lambda (parent)
  3420. (mapc
  3421. (lambda (tag)
  3422. (when (and (memq (org-element-type parent) '(headline inlinetask))
  3423. (not (member tag current-tag-list)))
  3424. (push tag current-tag-list)))
  3425. (org-element-property :tags parent)))
  3426. (org-export-get-genealogy element))
  3427. ;; Add FILETAGS keywords and return results.
  3428. (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
  3429. (defun org-export-get-node-property (property blob &optional inherited)
  3430. "Return node PROPERTY value for BLOB.
  3431. PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
  3432. element or object.
  3433. If optional argument INHERITED is non-nil, the value can be
  3434. inherited from a parent headline.
  3435. Return value is a string or nil."
  3436. (let ((headline (if (eq (org-element-type blob) 'headline) blob
  3437. (org-export-get-parent-headline blob))))
  3438. (if (not inherited) (org-element-property property blob)
  3439. (let ((parent headline) value)
  3440. (catch 'found
  3441. (while parent
  3442. (when (plist-member (nth 1 parent) property)
  3443. (throw 'found (org-element-property property parent)))
  3444. (setq parent (org-element-property :parent parent))))))))
  3445. (defun org-export-get-category (blob info)
  3446. "Return category for element or object BLOB.
  3447. INFO is a plist used as a communication channel.
  3448. CATEGORY is automatically inherited from a parent headline, from
  3449. #+CATEGORY: keyword or created out of original file name. If all
  3450. fail, the fall-back value is \"???\"."
  3451. (or (let ((headline (if (eq (org-element-type blob) 'headline) blob
  3452. (org-export-get-parent-headline blob))))
  3453. ;; Almost like `org-export-node-property', but we cannot trust
  3454. ;; `plist-member' as every headline has a `:CATEGORY'
  3455. ;; property, would it be nil or equal to "???" (which has the
  3456. ;; same meaning).
  3457. (let ((parent headline) value)
  3458. (catch 'found
  3459. (while parent
  3460. (let ((category (org-element-property :CATEGORY parent)))
  3461. (and category (not (equal "???" category))
  3462. (throw 'found category)))
  3463. (setq parent (org-element-property :parent parent))))))
  3464. (org-element-map (plist-get info :parse-tree) 'keyword
  3465. (lambda (kwd)
  3466. (when (equal (org-element-property :key kwd) "CATEGORY")
  3467. (org-element-property :value kwd)))
  3468. info 'first-match)
  3469. (let ((file (plist-get info :input-file)))
  3470. (and file (file-name-sans-extension (file-name-nondirectory file))))
  3471. "???"))
  3472. (defun org-export-get-alt-title (headline info)
  3473. "Return alternative title for HEADLINE, as a secondary string.
  3474. INFO is a plist used as a communication channel. If no optional
  3475. title is defined, fall-back to the regular title."
  3476. (or (org-element-property :alt-title headline)
  3477. (org-element-property :title headline)))
  3478. (defun org-export-first-sibling-p (headline info)
  3479. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  3480. INFO is a plist used as a communication channel."
  3481. (not (eq (org-element-type (org-export-get-previous-element headline info))
  3482. 'headline)))
  3483. (defun org-export-last-sibling-p (headline info)
  3484. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  3485. INFO is a plist used as a communication channel."
  3486. (not (org-export-get-next-element headline info)))
  3487. ;;;; For Keywords
  3488. ;;
  3489. ;; `org-export-get-date' returns a date appropriate for the document
  3490. ;; to about to be exported. In particular, it takes care of
  3491. ;; `org-export-date-timestamp-format'.
  3492. (defun org-export-get-date (info &optional fmt)
  3493. "Return date value for the current document.
  3494. INFO is a plist used as a communication channel. FMT, when
  3495. non-nil, is a time format string that will be applied on the date
  3496. if it consists in a single timestamp object. It defaults to
  3497. `org-export-date-timestamp-format' when nil.
  3498. A proper date can be a secondary string, a string or nil. It is
  3499. meant to be translated with `org-export-data' or alike."
  3500. (let ((date (plist-get info :date))
  3501. (fmt (or fmt org-export-date-timestamp-format)))
  3502. (cond ((not date) nil)
  3503. ((and fmt
  3504. (not (cdr date))
  3505. (eq (org-element-type (car date)) 'timestamp))
  3506. (org-timestamp-format (car date) fmt))
  3507. (t date))))
  3508. ;;;; For Links
  3509. ;;
  3510. ;; `org-export-solidify-link-text' turns a string into a safer version
  3511. ;; for links, replacing most non-standard characters with hyphens.
  3512. ;;
  3513. ;; `org-export-get-coderef-format' returns an appropriate format
  3514. ;; string for coderefs.
  3515. ;;
  3516. ;; `org-export-inline-image-p' returns a non-nil value when the link
  3517. ;; provided should be considered as an inline image.
  3518. ;;
  3519. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  3520. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  3521. ;; returns an appropriate unique identifier when found, or nil.
  3522. ;;
  3523. ;; `org-export-resolve-id-link' returns the first headline with
  3524. ;; specified id or custom-id in parse tree, the path to the external
  3525. ;; file with the id or nil when neither was found.
  3526. ;;
  3527. ;; `org-export-resolve-coderef' associates a reference to a line
  3528. ;; number in the element it belongs, or returns the reference itself
  3529. ;; when the element isn't numbered.
  3530. (defun org-export-solidify-link-text (s)
  3531. "Take link text S and make a safe target out of it."
  3532. (save-match-data
  3533. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-:]+") "-")))
  3534. (defun org-export-get-coderef-format (path desc)
  3535. "Return format string for code reference link.
  3536. PATH is the link path. DESC is its description."
  3537. (save-match-data
  3538. (cond ((not desc) "%s")
  3539. ((string-match (regexp-quote (concat "(" path ")")) desc)
  3540. (replace-match "%s" t t desc))
  3541. (t desc))))
  3542. (defun org-export-inline-image-p (link &optional rules)
  3543. "Non-nil if LINK object points to an inline image.
  3544. Optional argument is a set of RULES defining inline images. It
  3545. is an alist where associations have the following shape:
  3546. \(TYPE . REGEXP)
  3547. Applying a rule means apply REGEXP against LINK's path when its
  3548. type is TYPE. The function will return a non-nil value if any of
  3549. the provided rules is non-nil. The default rule is
  3550. `org-export-default-inline-image-rule'.
  3551. This only applies to links without a description."
  3552. (and (not (org-element-contents link))
  3553. (let ((case-fold-search t)
  3554. (rules (or rules org-export-default-inline-image-rule)))
  3555. (catch 'exit
  3556. (mapc
  3557. (lambda (rule)
  3558. (and (string= (org-element-property :type link) (car rule))
  3559. (string-match (cdr rule)
  3560. (org-element-property :path link))
  3561. (throw 'exit t)))
  3562. rules)
  3563. ;; Return nil if no rule matched.
  3564. nil))))
  3565. (defun org-export-resolve-coderef (ref info)
  3566. "Resolve a code reference REF.
  3567. INFO is a plist used as a communication channel.
  3568. Return associated line number in source code, or REF itself,
  3569. depending on src-block or example element's switches."
  3570. (org-element-map (plist-get info :parse-tree) '(example-block src-block)
  3571. (lambda (el)
  3572. (with-temp-buffer
  3573. (insert (org-trim (org-element-property :value el)))
  3574. (let* ((label-fmt (regexp-quote
  3575. (or (org-element-property :label-fmt el)
  3576. org-coderef-label-format)))
  3577. (ref-re
  3578. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  3579. (replace-regexp-in-string "%s" ref label-fmt nil t))))
  3580. ;; Element containing REF is found. Resolve it to either
  3581. ;; a label or a line number, as needed.
  3582. (when (re-search-backward ref-re nil t)
  3583. (cond
  3584. ((org-element-property :use-labels el) ref)
  3585. ((eq (org-element-property :number-lines el) 'continued)
  3586. (+ (org-export-get-loc el info) (line-number-at-pos)))
  3587. (t (line-number-at-pos)))))))
  3588. info 'first-match))
  3589. (defun org-export-resolve-fuzzy-link (link info)
  3590. "Return LINK destination.
  3591. INFO is a plist holding contextual information.
  3592. Return value can be an object, an element, or nil:
  3593. - If LINK path matches a target object (i.e. <<path>>) return it.
  3594. - If LINK path exactly matches the name affiliated keyword
  3595. \(i.e. #+NAME: path) of an element, return that element.
  3596. - If LINK path exactly matches any headline name, return that
  3597. element. If more than one headline share that name, priority
  3598. will be given to the one with the closest common ancestor, if
  3599. any, or the first one in the parse tree otherwise.
  3600. - Otherwise, return nil.
  3601. Assume LINK type is \"fuzzy\". White spaces are not
  3602. significant."
  3603. (let* ((raw-path (org-element-property :path link))
  3604. (match-title-p (eq (aref raw-path 0) ?*))
  3605. ;; Split PATH at white spaces so matches are space
  3606. ;; insensitive.
  3607. (path (org-split-string
  3608. (if match-title-p (substring raw-path 1) raw-path)))
  3609. ;; Cache for destinations that are not position dependent.
  3610. (link-cache
  3611. (or (plist-get info :resolve-fuzzy-link-cache)
  3612. (plist-get (setq info (plist-put info :resolve-fuzzy-link-cache
  3613. (make-hash-table :test 'equal)))
  3614. :resolve-fuzzy-link-cache)))
  3615. (cached (gethash path link-cache 'not-found)))
  3616. (cond
  3617. ;; Destination is not position dependent: use cached value.
  3618. ((and (not match-title-p) (not (eq cached 'not-found))) cached)
  3619. ;; First try to find a matching "<<path>>" unless user specified
  3620. ;; he was looking for a headline (path starts with a "*"
  3621. ;; character).
  3622. ((and (not match-title-p)
  3623. (let ((match (org-element-map (plist-get info :parse-tree) 'target
  3624. (lambda (blob)
  3625. (and (equal (org-split-string
  3626. (org-element-property :value blob))
  3627. path)
  3628. blob))
  3629. info 'first-match)))
  3630. (and match (puthash path match link-cache)))))
  3631. ;; Then try to find an element with a matching "#+NAME: path"
  3632. ;; affiliated keyword.
  3633. ((and (not match-title-p)
  3634. (let ((match (org-element-map (plist-get info :parse-tree)
  3635. org-element-all-elements
  3636. (lambda (el)
  3637. (let ((name (org-element-property :name el)))
  3638. (when (and name
  3639. (equal (org-split-string name) path))
  3640. el)))
  3641. info 'first-match)))
  3642. (and match (puthash path match link-cache)))))
  3643. ;; Last case: link either points to a headline or to nothingness.
  3644. ;; Try to find the source, with priority given to headlines with
  3645. ;; the closest common ancestor. If such candidate is found,
  3646. ;; return it, otherwise return nil.
  3647. (t
  3648. (let ((find-headline
  3649. (function
  3650. ;; Return first headline whose `:raw-value' property is
  3651. ;; NAME in parse tree DATA, or nil. Statistics cookies
  3652. ;; are ignored.
  3653. (lambda (name data)
  3654. (org-element-map data 'headline
  3655. (lambda (headline)
  3656. (when (equal (org-split-string
  3657. (replace-regexp-in-string
  3658. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  3659. (org-element-property :raw-value headline)))
  3660. name)
  3661. headline))
  3662. info 'first-match)))))
  3663. ;; Search among headlines sharing an ancestor with link, from
  3664. ;; closest to farthest.
  3665. (catch 'exit
  3666. (mapc
  3667. (lambda (parent)
  3668. (let ((foundp (funcall find-headline path parent)))
  3669. (when foundp (throw 'exit foundp))))
  3670. (let ((parent-hl (org-export-get-parent-headline link)))
  3671. (if (not parent-hl) (list (plist-get info :parse-tree))
  3672. (cons parent-hl (org-export-get-genealogy parent-hl)))))
  3673. ;; No destination found: return nil.
  3674. (and (not match-title-p) (puthash path nil link-cache))))))))
  3675. (defun org-export-resolve-id-link (link info)
  3676. "Return headline referenced as LINK destination.
  3677. INFO is a plist used as a communication channel.
  3678. Return value can be the headline element matched in current parse
  3679. tree, a file name or nil. Assume LINK type is either \"id\" or
  3680. \"custom-id\"."
  3681. (let ((id (org-element-property :path link)))
  3682. ;; First check if id is within the current parse tree.
  3683. (or (org-element-map (plist-get info :parse-tree) 'headline
  3684. (lambda (headline)
  3685. (when (or (string= (org-element-property :ID headline) id)
  3686. (string= (org-element-property :CUSTOM_ID headline) id))
  3687. headline))
  3688. info 'first-match)
  3689. ;; Otherwise, look for external files.
  3690. (cdr (assoc id (plist-get info :id-alist))))))
  3691. (defun org-export-resolve-radio-link (link info)
  3692. "Return radio-target object referenced as LINK destination.
  3693. INFO is a plist used as a communication channel.
  3694. Return value can be a radio-target object or nil. Assume LINK
  3695. has type \"radio\"."
  3696. (let ((path (replace-regexp-in-string
  3697. "[ \r\t\n]+" " " (org-element-property :path link))))
  3698. (org-element-map (plist-get info :parse-tree) 'radio-target
  3699. (lambda (radio)
  3700. (and (eq (compare-strings
  3701. (replace-regexp-in-string
  3702. "[ \r\t\n]+" " " (org-element-property :value radio))
  3703. nil nil path nil nil t)
  3704. t)
  3705. radio))
  3706. info 'first-match)))
  3707. ;;;; For References
  3708. ;;
  3709. ;; `org-export-get-ordinal' associates a sequence number to any object
  3710. ;; or element.
  3711. (defun org-export-get-ordinal (element info &optional types predicate)
  3712. "Return ordinal number of an element or object.
  3713. ELEMENT is the element or object considered. INFO is the plist
  3714. used as a communication channel.
  3715. Optional argument TYPES, when non-nil, is a list of element or
  3716. object types, as symbols, that should also be counted in.
  3717. Otherwise, only provided element's type is considered.
  3718. Optional argument PREDICATE is a function returning a non-nil
  3719. value if the current element or object should be counted in. It
  3720. accepts two arguments: the element or object being considered and
  3721. the plist used as a communication channel. This allows to count
  3722. only a certain type of objects (i.e. inline images).
  3723. Return value is a list of numbers if ELEMENT is a headline or an
  3724. item. It is nil for keywords. It represents the footnote number
  3725. for footnote definitions and footnote references. If ELEMENT is
  3726. a target, return the same value as if ELEMENT was the closest
  3727. table, item or headline containing the target. In any other
  3728. case, return the sequence number of ELEMENT among elements or
  3729. objects of the same type."
  3730. ;; Ordinal of a target object refer to the ordinal of the closest
  3731. ;; table, item, or headline containing the object.
  3732. (when (eq (org-element-type element) 'target)
  3733. (setq element
  3734. (loop for parent in (org-export-get-genealogy element)
  3735. when
  3736. (memq
  3737. (org-element-type parent)
  3738. '(footnote-definition footnote-reference headline item
  3739. table))
  3740. return parent)))
  3741. (case (org-element-type element)
  3742. ;; Special case 1: A headline returns its number as a list.
  3743. (headline (org-export-get-headline-number element info))
  3744. ;; Special case 2: An item returns its number as a list.
  3745. (item (let ((struct (org-element-property :structure element)))
  3746. (org-list-get-item-number
  3747. (org-element-property :begin element)
  3748. struct
  3749. (org-list-prevs-alist struct)
  3750. (org-list-parents-alist struct))))
  3751. ((footnote-definition footnote-reference)
  3752. (org-export-get-footnote-number element info))
  3753. (otherwise
  3754. (let ((counter 0))
  3755. ;; Increment counter until ELEMENT is found again.
  3756. (org-element-map (plist-get info :parse-tree)
  3757. (or types (org-element-type element))
  3758. (lambda (el)
  3759. (cond
  3760. ((eq element el) (1+ counter))
  3761. ((not predicate) (incf counter) nil)
  3762. ((funcall predicate el info) (incf counter) nil)))
  3763. info 'first-match)))))
  3764. ;;;; For Src-Blocks
  3765. ;;
  3766. ;; `org-export-get-loc' counts number of code lines accumulated in
  3767. ;; src-block or example-block elements with a "+n" switch until
  3768. ;; a given element, excluded. Note: "-n" switches reset that count.
  3769. ;;
  3770. ;; `org-export-unravel-code' extracts source code (along with a code
  3771. ;; references alist) from an `element-block' or `src-block' type
  3772. ;; element.
  3773. ;;
  3774. ;; `org-export-format-code' applies a formatting function to each line
  3775. ;; of code, providing relative line number and code reference when
  3776. ;; appropriate. Since it doesn't access the original element from
  3777. ;; which the source code is coming, it expects from the code calling
  3778. ;; it to know if lines should be numbered and if code references
  3779. ;; should appear.
  3780. ;;
  3781. ;; Eventually, `org-export-format-code-default' is a higher-level
  3782. ;; function (it makes use of the two previous functions) which handles
  3783. ;; line numbering and code references inclusion, and returns source
  3784. ;; code in a format suitable for plain text or verbatim output.
  3785. (defun org-export-get-loc (element info)
  3786. "Return accumulated lines of code up to ELEMENT.
  3787. INFO is the plist used as a communication channel.
  3788. ELEMENT is excluded from count."
  3789. (let ((loc 0))
  3790. (org-element-map (plist-get info :parse-tree)
  3791. `(src-block example-block ,(org-element-type element))
  3792. (lambda (el)
  3793. (cond
  3794. ;; ELEMENT is reached: Quit the loop.
  3795. ((eq el element))
  3796. ;; Only count lines from src-block and example-block elements
  3797. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  3798. ((not (memq (org-element-type el) '(src-block example-block))) nil)
  3799. ((let ((linums (org-element-property :number-lines el)))
  3800. (when linums
  3801. ;; Accumulate locs or reset them.
  3802. (let ((lines (org-count-lines
  3803. (org-trim (org-element-property :value el)))))
  3804. (setq loc (if (eq linums 'new) lines (+ loc lines))))))
  3805. ;; Return nil to stay in the loop.
  3806. nil)))
  3807. info 'first-match)
  3808. ;; Return value.
  3809. loc))
  3810. (defun org-export-unravel-code (element)
  3811. "Clean source code and extract references out of it.
  3812. ELEMENT has either a `src-block' an `example-block' type.
  3813. Return a cons cell whose CAR is the source code, cleaned from any
  3814. reference and protective comma and CDR is an alist between
  3815. relative line number (integer) and name of code reference on that
  3816. line (string)."
  3817. (let* ((line 0) refs
  3818. ;; Get code and clean it. Remove blank lines at its
  3819. ;; beginning and end.
  3820. (code (replace-regexp-in-string
  3821. "\\`\\([ \t]*\n\\)+" ""
  3822. (replace-regexp-in-string
  3823. "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
  3824. (org-element-property :value element))))
  3825. ;; Get format used for references.
  3826. (label-fmt (regexp-quote
  3827. (or (org-element-property :label-fmt element)
  3828. org-coderef-label-format)))
  3829. ;; Build a regexp matching a loc with a reference.
  3830. (with-ref-re
  3831. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
  3832. (replace-regexp-in-string
  3833. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  3834. ;; Return value.
  3835. (cons
  3836. ;; Code with references removed.
  3837. (org-element-normalize-string
  3838. (mapconcat
  3839. (lambda (loc)
  3840. (incf line)
  3841. (if (not (string-match with-ref-re loc)) loc
  3842. ;; Ref line: remove ref, and signal its position in REFS.
  3843. (push (cons line (match-string 3 loc)) refs)
  3844. (replace-match "" nil nil loc 1)))
  3845. (org-split-string code "\n") "\n"))
  3846. ;; Reference alist.
  3847. refs)))
  3848. (defun org-export-format-code (code fun &optional num-lines ref-alist)
  3849. "Format CODE by applying FUN line-wise and return it.
  3850. CODE is a string representing the code to format. FUN is
  3851. a function. It must accept three arguments: a line of
  3852. code (string), the current line number (integer) or nil and the
  3853. reference associated to the current line (string) or nil.
  3854. Optional argument NUM-LINES can be an integer representing the
  3855. number of code lines accumulated until the current code. Line
  3856. numbers passed to FUN will take it into account. If it is nil,
  3857. FUN's second argument will always be nil. This number can be
  3858. obtained with `org-export-get-loc' function.
  3859. Optional argument REF-ALIST can be an alist between relative line
  3860. number (i.e. ignoring NUM-LINES) and the name of the code
  3861. reference on it. If it is nil, FUN's third argument will always
  3862. be nil. It can be obtained through the use of
  3863. `org-export-unravel-code' function."
  3864. (let ((--locs (org-split-string code "\n"))
  3865. (--line 0))
  3866. (org-element-normalize-string
  3867. (mapconcat
  3868. (lambda (--loc)
  3869. (incf --line)
  3870. (let ((--ref (cdr (assq --line ref-alist))))
  3871. (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
  3872. --locs "\n"))))
  3873. (defun org-export-format-code-default (element info)
  3874. "Return source code from ELEMENT, formatted in a standard way.
  3875. ELEMENT is either a `src-block' or `example-block' element. INFO
  3876. is a plist used as a communication channel.
  3877. This function takes care of line numbering and code references
  3878. inclusion. Line numbers, when applicable, appear at the
  3879. beginning of the line, separated from the code by two white
  3880. spaces. Code references, on the other hand, appear flushed to
  3881. the right, separated by six white spaces from the widest line of
  3882. code."
  3883. ;; Extract code and references.
  3884. (let* ((code-info (org-export-unravel-code element))
  3885. (code (car code-info))
  3886. (code-lines (org-split-string code "\n")))
  3887. (if (null code-lines) ""
  3888. (let* ((refs (and (org-element-property :retain-labels element)
  3889. (cdr code-info)))
  3890. ;; Handle line numbering.
  3891. (num-start (case (org-element-property :number-lines element)
  3892. (continued (org-export-get-loc element info))
  3893. (new 0)))
  3894. (num-fmt
  3895. (and num-start
  3896. (format "%%%ds "
  3897. (length (number-to-string
  3898. (+ (length code-lines) num-start))))))
  3899. ;; Prepare references display, if required. Any reference
  3900. ;; should start six columns after the widest line of code,
  3901. ;; wrapped with parenthesis.
  3902. (max-width
  3903. (+ (apply 'max (mapcar 'length code-lines))
  3904. (if (not num-start) 0 (length (format num-fmt num-start))))))
  3905. (org-export-format-code
  3906. code
  3907. (lambda (loc line-num ref)
  3908. (let ((number-str (and num-fmt (format num-fmt line-num))))
  3909. (concat
  3910. number-str
  3911. loc
  3912. (and ref
  3913. (concat (make-string
  3914. (- (+ 6 max-width)
  3915. (+ (length loc) (length number-str))) ? )
  3916. (format "(%s)" ref))))))
  3917. num-start refs)))))
  3918. ;;;; For Tables
  3919. ;;
  3920. ;; `org-export-table-has-special-column-p' and and
  3921. ;; `org-export-table-row-is-special-p' are predicates used to look for
  3922. ;; meta-information about the table structure.
  3923. ;;
  3924. ;; `org-table-has-header-p' tells when the rows before the first rule
  3925. ;; should be considered as table's header.
  3926. ;;
  3927. ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
  3928. ;; and `org-export-table-cell-borders' extract information from
  3929. ;; a table-cell element.
  3930. ;;
  3931. ;; `org-export-table-dimensions' gives the number on rows and columns
  3932. ;; in the table, ignoring horizontal rules and special columns.
  3933. ;; `org-export-table-cell-address', given a table-cell object, returns
  3934. ;; the absolute address of a cell. On the other hand,
  3935. ;; `org-export-get-table-cell-at' does the contrary.
  3936. ;;
  3937. ;; `org-export-table-cell-starts-colgroup-p',
  3938. ;; `org-export-table-cell-ends-colgroup-p',
  3939. ;; `org-export-table-row-starts-rowgroup-p',
  3940. ;; `org-export-table-row-ends-rowgroup-p',
  3941. ;; `org-export-table-row-starts-header-p' and
  3942. ;; `org-export-table-row-ends-header-p' indicate position of current
  3943. ;; row or cell within the table.
  3944. (defun org-export-table-has-special-column-p (table)
  3945. "Non-nil when TABLE has a special column.
  3946. All special columns will be ignored during export."
  3947. ;; The table has a special column when every first cell of every row
  3948. ;; has an empty value or contains a symbol among "/", "#", "!", "$",
  3949. ;; "*" "_" and "^". Though, do not consider a first row containing
  3950. ;; only empty cells as special.
  3951. (let ((special-column-p 'empty))
  3952. (catch 'exit
  3953. (mapc
  3954. (lambda (row)
  3955. (when (eq (org-element-property :type row) 'standard)
  3956. (let ((value (org-element-contents
  3957. (car (org-element-contents row)))))
  3958. (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
  3959. (setq special-column-p 'special))
  3960. ((not value))
  3961. (t (throw 'exit nil))))))
  3962. (org-element-contents table))
  3963. (eq special-column-p 'special))))
  3964. (defun org-export-table-has-header-p (table info)
  3965. "Non-nil when TABLE has a header.
  3966. INFO is a plist used as a communication channel.
  3967. A table has a header when it contains at least two row groups."
  3968. (let ((cache (or (plist-get info :table-header-cache)
  3969. (plist-get (setq info
  3970. (plist-put info :table-header-cache
  3971. (make-hash-table :test 'eq)))
  3972. :table-header-cache))))
  3973. (or (gethash table cache)
  3974. (let ((rowgroup 1) row-flag)
  3975. (puthash
  3976. table
  3977. (org-element-map table 'table-row
  3978. (lambda (row)
  3979. (cond
  3980. ((> rowgroup 1) t)
  3981. ((and row-flag (eq (org-element-property :type row) 'rule))
  3982. (incf rowgroup) (setq row-flag nil))
  3983. ((and (not row-flag) (eq (org-element-property :type row)
  3984. 'standard))
  3985. (setq row-flag t) nil)))
  3986. info 'first-match)
  3987. cache)))))
  3988. (defun org-export-table-row-is-special-p (table-row info)
  3989. "Non-nil if TABLE-ROW is considered special.
  3990. INFO is a plist used as the communication channel.
  3991. All special rows will be ignored during export."
  3992. (when (eq (org-element-property :type table-row) 'standard)
  3993. (let ((first-cell (org-element-contents
  3994. (car (org-element-contents table-row)))))
  3995. ;; A row is special either when...
  3996. (or
  3997. ;; ... it starts with a field only containing "/",
  3998. (equal first-cell '("/"))
  3999. ;; ... the table contains a special column and the row start
  4000. ;; with a marking character among, "^", "_", "$" or "!",
  4001. (and (org-export-table-has-special-column-p
  4002. (org-export-get-parent table-row))
  4003. (member first-cell '(("^") ("_") ("$") ("!"))))
  4004. ;; ... it contains only alignment cookies and empty cells.
  4005. (let ((special-row-p 'empty))
  4006. (catch 'exit
  4007. (mapc
  4008. (lambda (cell)
  4009. (let ((value (org-element-contents cell)))
  4010. ;; Since VALUE is a secondary string, the following
  4011. ;; checks avoid expanding it with `org-export-data'.
  4012. (cond ((not value))
  4013. ((and (not (cdr value))
  4014. (stringp (car value))
  4015. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
  4016. (car value)))
  4017. (setq special-row-p 'cookie))
  4018. (t (throw 'exit nil)))))
  4019. (org-element-contents table-row))
  4020. (eq special-row-p 'cookie)))))))
  4021. (defun org-export-table-row-group (table-row info)
  4022. "Return TABLE-ROW's group number, as an integer.
  4023. INFO is a plist used as the communication channel.
  4024. Return value is the group number, as an integer, or nil for
  4025. special rows and rows separators. First group is also table's
  4026. header."
  4027. (let ((cache (or (plist-get info :table-row-group-cache)
  4028. (plist-get (setq info
  4029. (plist-put info :table-row-group-cache
  4030. (make-hash-table :test 'eq)))
  4031. :table-row-group-cache))))
  4032. (cond ((gethash table-row cache))
  4033. ((eq (org-element-property :type table-row) 'rule) nil)
  4034. (t (let ((group 0) row-flag)
  4035. (org-element-map (org-export-get-parent table-row) 'table-row
  4036. (lambda (row)
  4037. (if (eq (org-element-property :type row) 'rule)
  4038. (setq row-flag nil)
  4039. (unless row-flag (incf group) (setq row-flag t)))
  4040. (when (eq table-row row) (puthash table-row group cache)))
  4041. info 'first-match))))))
  4042. (defun org-export-table-cell-width (table-cell info)
  4043. "Return TABLE-CELL contents width.
  4044. INFO is a plist used as the communication channel.
  4045. Return value is the width given by the last width cookie in the
  4046. same column as TABLE-CELL, or nil."
  4047. (let* ((row (org-export-get-parent table-cell))
  4048. (table (org-export-get-parent row))
  4049. (column (let ((cells (org-element-contents row)))
  4050. (- (length cells) (length (memq table-cell cells)))))
  4051. (cache (or (plist-get info :table-cell-width-cache)
  4052. (plist-get (setq info
  4053. (plist-put info :table-cell-width-cache
  4054. (make-hash-table :test 'equal)))
  4055. :table-cell-width-cache)))
  4056. (key (cons table column))
  4057. (value (gethash key cache 'no-result)))
  4058. (if (not (eq value 'no-result)) value
  4059. (let (cookie-width)
  4060. (dolist (row (org-element-contents table)
  4061. (puthash key cookie-width cache))
  4062. (when (org-export-table-row-is-special-p row info)
  4063. ;; In a special row, try to find a width cookie at COLUMN.
  4064. (let* ((value (org-element-contents
  4065. (elt (org-element-contents row) column)))
  4066. (cookie (car value)))
  4067. ;; The following checks avoid expanding unnecessarily
  4068. ;; the cell with `org-export-data'.
  4069. (when (and value
  4070. (not (cdr value))
  4071. (stringp cookie)
  4072. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie)
  4073. (match-string 1 cookie))
  4074. (setq cookie-width
  4075. (string-to-number (match-string 1 cookie)))))))))))
  4076. (defun org-export-table-cell-alignment (table-cell info)
  4077. "Return TABLE-CELL contents alignment.
  4078. INFO is a plist used as the communication channel.
  4079. Return alignment as specified by the last alignment cookie in the
  4080. same column as TABLE-CELL. If no such cookie is found, a default
  4081. alignment value will be deduced from fraction of numbers in the
  4082. column (see `org-table-number-fraction' for more information).
  4083. Possible values are `left', `right' and `center'."
  4084. (let* ((row (org-export-get-parent table-cell))
  4085. (table (org-export-get-parent row))
  4086. (column (let ((cells (org-element-contents row)))
  4087. (- (length cells) (length (memq table-cell cells)))))
  4088. (cache (or (plist-get info :table-cell-alignment-cache)
  4089. (plist-get (setq info
  4090. (plist-put info :table-cell-alignment-cache
  4091. (make-hash-table :test 'equal)))
  4092. :table-cell-alignment-cache))))
  4093. (or (gethash (cons table column) cache)
  4094. (let ((number-cells 0)
  4095. (total-cells 0)
  4096. cookie-align
  4097. previous-cell-number-p)
  4098. (dolist (row (org-element-contents (org-export-get-parent row)))
  4099. (cond
  4100. ;; In a special row, try to find an alignment cookie at
  4101. ;; COLUMN.
  4102. ((org-export-table-row-is-special-p row info)
  4103. (let ((value (org-element-contents
  4104. (elt (org-element-contents row) column))))
  4105. ;; Since VALUE is a secondary string, the following
  4106. ;; checks avoid useless expansion through
  4107. ;; `org-export-data'.
  4108. (when (and value
  4109. (not (cdr value))
  4110. (stringp (car value))
  4111. (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
  4112. (car value))
  4113. (match-string 1 (car value)))
  4114. (setq cookie-align (match-string 1 (car value))))))
  4115. ;; Ignore table rules.
  4116. ((eq (org-element-property :type row) 'rule))
  4117. ;; In a standard row, check if cell's contents are
  4118. ;; expressing some kind of number. Increase NUMBER-CELLS
  4119. ;; accordingly. Though, don't bother if an alignment
  4120. ;; cookie has already defined cell's alignment.
  4121. ((not cookie-align)
  4122. (let ((value (org-export-data
  4123. (org-element-contents
  4124. (elt (org-element-contents row) column))
  4125. info)))
  4126. (incf total-cells)
  4127. ;; Treat an empty cell as a number if it follows
  4128. ;; a number.
  4129. (if (not (or (string-match org-table-number-regexp value)
  4130. (and (string= value "") previous-cell-number-p)))
  4131. (setq previous-cell-number-p nil)
  4132. (setq previous-cell-number-p t)
  4133. (incf number-cells))))))
  4134. ;; Return value. Alignment specified by cookies has
  4135. ;; precedence over alignment deduced from cell's contents.
  4136. (puthash (cons table column)
  4137. (cond ((equal cookie-align "l") 'left)
  4138. ((equal cookie-align "r") 'right)
  4139. ((equal cookie-align "c") 'center)
  4140. ((>= (/ (float number-cells) total-cells)
  4141. org-table-number-fraction)
  4142. 'right)
  4143. (t 'left))
  4144. cache)))))
  4145. (defun org-export-table-cell-borders (table-cell info)
  4146. "Return TABLE-CELL borders.
  4147. INFO is a plist used as a communication channel.
  4148. Return value is a list of symbols, or nil. Possible values are:
  4149. `top', `bottom', `above', `below', `left' and `right'. Note:
  4150. `top' (resp. `bottom') only happen for a cell in the first
  4151. row (resp. last row) of the table, ignoring table rules, if any.
  4152. Returned borders ignore special rows."
  4153. (let* ((row (org-export-get-parent table-cell))
  4154. (table (org-export-get-parent-table table-cell))
  4155. borders)
  4156. ;; Top/above border? TABLE-CELL has a border above when a rule
  4157. ;; used to demarcate row groups can be found above. Hence,
  4158. ;; finding a rule isn't sufficient to push `above' in BORDERS:
  4159. ;; another regular row has to be found above that rule.
  4160. (let (rule-flag)
  4161. (catch 'exit
  4162. (mapc (lambda (row)
  4163. (cond ((eq (org-element-property :type row) 'rule)
  4164. (setq rule-flag t))
  4165. ((not (org-export-table-row-is-special-p row info))
  4166. (if rule-flag (throw 'exit (push 'above borders))
  4167. (throw 'exit nil)))))
  4168. ;; Look at every row before the current one.
  4169. (cdr (memq row (reverse (org-element-contents table)))))
  4170. ;; No rule above, or rule found starts the table (ignoring any
  4171. ;; special row): TABLE-CELL is at the top of the table.
  4172. (when rule-flag (push 'above borders))
  4173. (push 'top borders)))
  4174. ;; Bottom/below border? TABLE-CELL has a border below when next
  4175. ;; non-regular row below is a rule.
  4176. (let (rule-flag)
  4177. (catch 'exit
  4178. (mapc (lambda (row)
  4179. (cond ((eq (org-element-property :type row) 'rule)
  4180. (setq rule-flag t))
  4181. ((not (org-export-table-row-is-special-p row info))
  4182. (if rule-flag (throw 'exit (push 'below borders))
  4183. (throw 'exit nil)))))
  4184. ;; Look at every row after the current one.
  4185. (cdr (memq row (org-element-contents table))))
  4186. ;; No rule below, or rule found ends the table (modulo some
  4187. ;; special row): TABLE-CELL is at the bottom of the table.
  4188. (when rule-flag (push 'below borders))
  4189. (push 'bottom borders)))
  4190. ;; Right/left borders? They can only be specified by column
  4191. ;; groups. Column groups are defined in a row starting with "/".
  4192. ;; Also a column groups row only contains "<", "<>", ">" or blank
  4193. ;; cells.
  4194. (catch 'exit
  4195. (let ((column (let ((cells (org-element-contents row)))
  4196. (- (length cells) (length (memq table-cell cells))))))
  4197. (mapc
  4198. (lambda (row)
  4199. (unless (eq (org-element-property :type row) 'rule)
  4200. (when (equal (org-element-contents
  4201. (car (org-element-contents row)))
  4202. '("/"))
  4203. (let ((column-groups
  4204. (mapcar
  4205. (lambda (cell)
  4206. (let ((value (org-element-contents cell)))
  4207. (when (member value '(("<") ("<>") (">") nil))
  4208. (car value))))
  4209. (org-element-contents row))))
  4210. ;; There's a left border when previous cell, if
  4211. ;; any, ends a group, or current one starts one.
  4212. (when (or (and (not (zerop column))
  4213. (member (elt column-groups (1- column))
  4214. '(">" "<>")))
  4215. (member (elt column-groups column) '("<" "<>")))
  4216. (push 'left borders))
  4217. ;; There's a right border when next cell, if any,
  4218. ;; starts a group, or current one ends one.
  4219. (when (or (and (/= (1+ column) (length column-groups))
  4220. (member (elt column-groups (1+ column))
  4221. '("<" "<>")))
  4222. (member (elt column-groups column) '(">" "<>")))
  4223. (push 'right borders))
  4224. (throw 'exit nil)))))
  4225. ;; Table rows are read in reverse order so last column groups
  4226. ;; row has precedence over any previous one.
  4227. (reverse (org-element-contents table)))))
  4228. ;; Return value.
  4229. borders))
  4230. (defun org-export-table-cell-starts-colgroup-p (table-cell info)
  4231. "Non-nil when TABLE-CELL is at the beginning of a row group.
  4232. INFO is a plist used as a communication channel."
  4233. ;; A cell starts a column group either when it is at the beginning
  4234. ;; of a row (or after the special column, if any) or when it has
  4235. ;; a left border.
  4236. (or (eq (org-element-map (org-export-get-parent table-cell) 'table-cell
  4237. 'identity info 'first-match)
  4238. table-cell)
  4239. (memq 'left (org-export-table-cell-borders table-cell info))))
  4240. (defun org-export-table-cell-ends-colgroup-p (table-cell info)
  4241. "Non-nil when TABLE-CELL is at the end of a row group.
  4242. INFO is a plist used as a communication channel."
  4243. ;; A cell ends a column group either when it is at the end of a row
  4244. ;; or when it has a right border.
  4245. (or (eq (car (last (org-element-contents
  4246. (org-export-get-parent table-cell))))
  4247. table-cell)
  4248. (memq 'right (org-export-table-cell-borders table-cell info))))
  4249. (defun org-export-table-row-starts-rowgroup-p (table-row info)
  4250. "Non-nil when TABLE-ROW is at the beginning of a column group.
  4251. INFO is a plist used as a communication channel."
  4252. (unless (or (eq (org-element-property :type table-row) 'rule)
  4253. (org-export-table-row-is-special-p table-row info))
  4254. (let ((borders (org-export-table-cell-borders
  4255. (car (org-element-contents table-row)) info)))
  4256. (or (memq 'top borders) (memq 'above borders)))))
  4257. (defun org-export-table-row-ends-rowgroup-p (table-row info)
  4258. "Non-nil when TABLE-ROW is at the end of a column group.
  4259. INFO is a plist used as a communication channel."
  4260. (unless (or (eq (org-element-property :type table-row) 'rule)
  4261. (org-export-table-row-is-special-p table-row info))
  4262. (let ((borders (org-export-table-cell-borders
  4263. (car (org-element-contents table-row)) info)))
  4264. (or (memq 'bottom borders) (memq 'below borders)))))
  4265. (defun org-export-table-row-starts-header-p (table-row info)
  4266. "Non-nil when TABLE-ROW is the first table header's row.
  4267. INFO is a plist used as a communication channel."
  4268. (and (org-export-table-has-header-p
  4269. (org-export-get-parent-table table-row) info)
  4270. (org-export-table-row-starts-rowgroup-p table-row info)
  4271. (= (org-export-table-row-group table-row info) 1)))
  4272. (defun org-export-table-row-ends-header-p (table-row info)
  4273. "Non-nil when TABLE-ROW is the last table header's row.
  4274. INFO is a plist used as a communication channel."
  4275. (and (org-export-table-has-header-p
  4276. (org-export-get-parent-table table-row) info)
  4277. (org-export-table-row-ends-rowgroup-p table-row info)
  4278. (= (org-export-table-row-group table-row info) 1)))
  4279. (defun org-export-table-row-number (table-row info)
  4280. "Return TABLE-ROW number.
  4281. INFO is a plist used as a communication channel. Return value is
  4282. zero-based and ignores separators. The function returns nil for
  4283. special colums and separators."
  4284. (when (and (eq (org-element-property :type table-row) 'standard)
  4285. (not (org-export-table-row-is-special-p table-row info)))
  4286. (let ((number 0))
  4287. (org-element-map (org-export-get-parent-table table-row) 'table-row
  4288. (lambda (row)
  4289. (cond ((eq row table-row) number)
  4290. ((eq (org-element-property :type row) 'standard)
  4291. (incf number) nil)))
  4292. info 'first-match))))
  4293. (defun org-export-table-dimensions (table info)
  4294. "Return TABLE dimensions.
  4295. INFO is a plist used as a communication channel.
  4296. Return value is a CONS like (ROWS . COLUMNS) where
  4297. ROWS (resp. COLUMNS) is the number of exportable
  4298. rows (resp. columns)."
  4299. (let (first-row (columns 0) (rows 0))
  4300. ;; Set number of rows, and extract first one.
  4301. (org-element-map table 'table-row
  4302. (lambda (row)
  4303. (when (eq (org-element-property :type row) 'standard)
  4304. (incf rows)
  4305. (unless first-row (setq first-row row)))) info)
  4306. ;; Set number of columns.
  4307. (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
  4308. ;; Return value.
  4309. (cons rows columns)))
  4310. (defun org-export-table-cell-address (table-cell info)
  4311. "Return address of a regular TABLE-CELL object.
  4312. TABLE-CELL is the cell considered. INFO is a plist used as
  4313. a communication channel.
  4314. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  4315. zero-based index. Only exportable cells are considered. The
  4316. function returns nil for other cells."
  4317. (let* ((table-row (org-export-get-parent table-cell))
  4318. (row-number (org-export-table-row-number table-row info)))
  4319. (when row-number
  4320. (cons row-number
  4321. (let ((col-count 0))
  4322. (org-element-map table-row 'table-cell
  4323. (lambda (cell)
  4324. (if (eq cell table-cell) col-count (incf col-count) nil))
  4325. info 'first-match))))))
  4326. (defun org-export-get-table-cell-at (address table info)
  4327. "Return regular table-cell object at ADDRESS in TABLE.
  4328. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  4329. zero-based index. TABLE is a table type element. INFO is
  4330. a plist used as a communication channel.
  4331. If no table-cell, among exportable cells, is found at ADDRESS,
  4332. return nil."
  4333. (let ((column-pos (cdr address)) (column-count 0))
  4334. (org-element-map
  4335. ;; Row at (car address) or nil.
  4336. (let ((row-pos (car address)) (row-count 0))
  4337. (org-element-map table 'table-row
  4338. (lambda (row)
  4339. (cond ((eq (org-element-property :type row) 'rule) nil)
  4340. ((= row-count row-pos) row)
  4341. (t (incf row-count) nil)))
  4342. info 'first-match))
  4343. 'table-cell
  4344. (lambda (cell)
  4345. (if (= column-count column-pos) cell
  4346. (incf column-count) nil))
  4347. info 'first-match)))
  4348. ;;;; For Tables Of Contents
  4349. ;;
  4350. ;; `org-export-collect-headlines' builds a list of all exportable
  4351. ;; headline elements, maybe limited to a certain depth. One can then
  4352. ;; easily parse it and transcode it.
  4353. ;;
  4354. ;; Building lists of tables, figures or listings is quite similar.
  4355. ;; Once the generic function `org-export-collect-elements' is defined,
  4356. ;; `org-export-collect-tables', `org-export-collect-figures' and
  4357. ;; `org-export-collect-listings' can be derived from it.
  4358. (defun org-export-collect-headlines (info &optional n)
  4359. "Collect headlines in order to build a table of contents.
  4360. INFO is a plist used as a communication channel.
  4361. When optional argument N is an integer, it specifies the depth of
  4362. the table of contents. Otherwise, it is set to the value of the
  4363. last headline level. See `org-export-headline-levels' for more
  4364. information.
  4365. Return a list of all exportable headlines as parsed elements.
  4366. Footnote sections, if any, will be ignored."
  4367. (unless (wholenump n) (setq n (plist-get info :headline-levels)))
  4368. (org-element-map (plist-get info :parse-tree) 'headline
  4369. (lambda (headline)
  4370. (unless (org-element-property :footnote-section-p headline)
  4371. ;; Strip contents from HEADLINE.
  4372. (let ((relative-level (org-export-get-relative-level headline info)))
  4373. (unless (> relative-level n) headline))))
  4374. info))
  4375. (defun org-export-collect-elements (type info &optional predicate)
  4376. "Collect referenceable elements of a determined type.
  4377. TYPE can be a symbol or a list of symbols specifying element
  4378. types to search. Only elements with a caption are collected.
  4379. INFO is a plist used as a communication channel.
  4380. When non-nil, optional argument PREDICATE is a function accepting
  4381. one argument, an element of type TYPE. It returns a non-nil
  4382. value when that element should be collected.
  4383. Return a list of all elements found, in order of appearance."
  4384. (org-element-map (plist-get info :parse-tree) type
  4385. (lambda (element)
  4386. (and (org-element-property :caption element)
  4387. (or (not predicate) (funcall predicate element))
  4388. element))
  4389. info))
  4390. (defun org-export-collect-tables (info)
  4391. "Build a list of tables.
  4392. INFO is a plist used as a communication channel.
  4393. Return a list of table elements with a caption."
  4394. (org-export-collect-elements 'table info))
  4395. (defun org-export-collect-figures (info predicate)
  4396. "Build a list of figures.
  4397. INFO is a plist used as a communication channel. PREDICATE is
  4398. a function which accepts one argument: a paragraph element and
  4399. whose return value is non-nil when that element should be
  4400. collected.
  4401. A figure is a paragraph type element, with a caption, verifying
  4402. PREDICATE. The latter has to be provided since a \"figure\" is
  4403. a vague concept that may depend on back-end.
  4404. Return a list of elements recognized as figures."
  4405. (org-export-collect-elements 'paragraph info predicate))
  4406. (defun org-export-collect-listings (info)
  4407. "Build a list of src blocks.
  4408. INFO is a plist used as a communication channel.
  4409. Return a list of src-block elements with a caption."
  4410. (org-export-collect-elements 'src-block info))
  4411. ;;;; Smart Quotes
  4412. ;;
  4413. ;; The main function for the smart quotes sub-system is
  4414. ;; `org-export-activate-smart-quotes', which replaces every quote in
  4415. ;; a given string from the parse tree with its "smart" counterpart.
  4416. ;;
  4417. ;; Dictionary for smart quotes is stored in
  4418. ;; `org-export-smart-quotes-alist'.
  4419. ;;
  4420. ;; Internally, regexps matching potential smart quotes (checks at
  4421. ;; string boundaries are also necessary) are defined in
  4422. ;; `org-export-smart-quotes-regexps'.
  4423. (defconst org-export-smart-quotes-alist
  4424. '(("da"
  4425. ;; one may use: »...«, "...", ›...‹, or '...'.
  4426. ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
  4427. ;; LaTeX quotes require Babel!
  4428. (opening-double-quote :utf-8 "»" :html "&raquo;" :latex ">>"
  4429. :texinfo "@guillemetright{}")
  4430. (closing-double-quote :utf-8 "«" :html "&laquo;" :latex "<<"
  4431. :texinfo "@guillemetleft{}")
  4432. (opening-single-quote :utf-8 "›" :html "&rsaquo;" :latex "\\frq{}"
  4433. :texinfo "@guilsinglright{}")
  4434. (closing-single-quote :utf-8 "‹" :html "&lsaquo;" :latex "\\flq{}"
  4435. :texinfo "@guilsingleft{}")
  4436. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4437. ("de"
  4438. (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
  4439. :texinfo "@quotedblbase{}")
  4440. (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
  4441. :texinfo "@quotedblleft{}")
  4442. (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
  4443. :texinfo "@quotesinglbase{}")
  4444. (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
  4445. :texinfo "@quoteleft{}")
  4446. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4447. ("en"
  4448. (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
  4449. (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
  4450. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4451. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4452. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4453. ("es"
  4454. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4455. :texinfo "@guillemetleft{}")
  4456. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4457. :texinfo "@guillemetright{}")
  4458. (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
  4459. (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
  4460. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4461. ("fr"
  4462. (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
  4463. :texinfo "@guillemetleft{}@tie{}")
  4464. (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
  4465. :texinfo "@tie{}@guillemetright{}")
  4466. (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
  4467. :texinfo "@guillemetleft{}@tie{}")
  4468. (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
  4469. :texinfo "@tie{}@guillemetright{}")
  4470. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4471. ("no"
  4472. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4473. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4474. :texinfo "@guillemetleft{}")
  4475. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4476. :texinfo "@guillemetright{}")
  4477. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4478. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4479. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4480. ("nb"
  4481. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4482. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4483. :texinfo "@guillemetleft{}")
  4484. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4485. :texinfo "@guillemetright{}")
  4486. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4487. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4488. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4489. ("nn"
  4490. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4491. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4492. :texinfo "@guillemetleft{}")
  4493. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4494. :texinfo "@guillemetright{}")
  4495. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4496. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4497. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4498. ("sv"
  4499. ;; based on https://sv.wikipedia.org/wiki/Citattecken
  4500. (opening-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
  4501. (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
  4502. (opening-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "`")
  4503. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "'")
  4504. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4505. )
  4506. "Smart quotes translations.
  4507. Alist whose CAR is a language string and CDR is an alist with
  4508. quote type as key and a plist associating various encodings to
  4509. their translation as value.
  4510. A quote type can be any symbol among `opening-double-quote',
  4511. `closing-double-quote', `opening-single-quote',
  4512. `closing-single-quote' and `apostrophe'.
  4513. Valid encodings include `:utf-8', `:html', `:latex' and
  4514. `:texinfo'.
  4515. If no translation is found, the quote character is left as-is.")
  4516. (defconst org-export-smart-quotes-regexps
  4517. (list
  4518. ;; Possible opening quote at beginning of string.
  4519. "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
  4520. ;; Possible closing quote at beginning of string.
  4521. "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
  4522. ;; Possible apostrophe at beginning of string.
  4523. "\\`\\('\\)\\S-"
  4524. ;; Opening single and double quotes.
  4525. "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
  4526. ;; Closing single and double quotes.
  4527. "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
  4528. ;; Apostrophe.
  4529. "\\S-\\('\\)\\S-"
  4530. ;; Possible opening quote at end of string.
  4531. "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
  4532. ;; Possible closing quote at end of string.
  4533. "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
  4534. ;; Possible apostrophe at end of string.
  4535. "\\S-\\('\\)\\'")
  4536. "List of regexps matching a quote or an apostrophe.
  4537. In every regexp, quote or apostrophe matched is put in group 1.")
  4538. (defun org-export-activate-smart-quotes (s encoding info &optional original)
  4539. "Replace regular quotes with \"smart\" quotes in string S.
  4540. ENCODING is a symbol among `:html', `:latex', `:texinfo' and
  4541. `:utf-8'. INFO is a plist used as a communication channel.
  4542. The function has to retrieve information about string
  4543. surroundings in parse tree. It can only happen with an
  4544. unmodified string. Thus, if S has already been through another
  4545. process, a non-nil ORIGINAL optional argument will provide that
  4546. original string.
  4547. Return the new string."
  4548. (if (equal s "") ""
  4549. (let* ((prev (org-export-get-previous-element (or original s) info))
  4550. ;; Try to be flexible when computing number of blanks
  4551. ;; before object. The previous object may be a string
  4552. ;; introduced by the back-end and not completely parsed.
  4553. (pre-blank (and prev
  4554. (or (org-element-property :post-blank prev)
  4555. ;; A string with missing `:post-blank'
  4556. ;; property.
  4557. (and (stringp prev)
  4558. (string-match " *\\'" prev)
  4559. (length (match-string 0 prev)))
  4560. ;; Fallback value.
  4561. 0)))
  4562. (next (org-export-get-next-element (or original s) info))
  4563. (get-smart-quote
  4564. (lambda (q type)
  4565. ;; Return smart quote associated to a give quote Q, as
  4566. ;; a string. TYPE is a symbol among `open', `close' and
  4567. ;; `apostrophe'.
  4568. (let ((key (case type
  4569. (apostrophe 'apostrophe)
  4570. (open (if (equal "'" q) 'opening-single-quote
  4571. 'opening-double-quote))
  4572. (otherwise (if (equal "'" q) 'closing-single-quote
  4573. 'closing-double-quote)))))
  4574. (or (plist-get
  4575. (cdr (assq key
  4576. (cdr (assoc (plist-get info :language)
  4577. org-export-smart-quotes-alist))))
  4578. encoding)
  4579. q)))))
  4580. (if (or (equal "\"" s) (equal "'" s))
  4581. ;; Only a quote: no regexp can match. We have to check both
  4582. ;; sides and decide what to do.
  4583. (cond ((and (not prev) (not next)) s)
  4584. ((not prev) (funcall get-smart-quote s 'open))
  4585. ((and (not next) (zerop pre-blank))
  4586. (funcall get-smart-quote s 'close))
  4587. ((not next) s)
  4588. ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
  4589. (t (funcall get-smart-quote 'open)))
  4590. ;; 1. Replace quote character at the beginning of S.
  4591. (cond
  4592. ;; Apostrophe?
  4593. ((and prev (zerop pre-blank)
  4594. (string-match (nth 2 org-export-smart-quotes-regexps) s))
  4595. (setq s (replace-match
  4596. (funcall get-smart-quote (match-string 1 s) 'apostrophe)
  4597. nil t s 1)))
  4598. ;; Closing quote?
  4599. ((and prev (zerop pre-blank)
  4600. (string-match (nth 1 org-export-smart-quotes-regexps) s))
  4601. (setq s (replace-match
  4602. (funcall get-smart-quote (match-string 1 s) 'close)
  4603. nil t s 1)))
  4604. ;; Opening quote?
  4605. ((and (or (not prev) (> pre-blank 0))
  4606. (string-match (nth 0 org-export-smart-quotes-regexps) s))
  4607. (setq s (replace-match
  4608. (funcall get-smart-quote (match-string 1 s) 'open)
  4609. nil t s 1))))
  4610. ;; 2. Replace quotes in the middle of the string.
  4611. (setq s (replace-regexp-in-string
  4612. ;; Opening quotes.
  4613. (nth 3 org-export-smart-quotes-regexps)
  4614. (lambda (text)
  4615. (funcall get-smart-quote (match-string 1 text) 'open))
  4616. s nil t 1))
  4617. (setq s (replace-regexp-in-string
  4618. ;; Closing quotes.
  4619. (nth 4 org-export-smart-quotes-regexps)
  4620. (lambda (text)
  4621. (funcall get-smart-quote (match-string 1 text) 'close))
  4622. s nil t 1))
  4623. (setq s (replace-regexp-in-string
  4624. ;; Apostrophes.
  4625. (nth 5 org-export-smart-quotes-regexps)
  4626. (lambda (text)
  4627. (funcall get-smart-quote (match-string 1 text) 'apostrophe))
  4628. s nil t 1))
  4629. ;; 3. Replace quote character at the end of S.
  4630. (cond
  4631. ;; Apostrophe?
  4632. ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
  4633. (setq s (replace-match
  4634. (funcall get-smart-quote (match-string 1 s) 'apostrophe)
  4635. nil t s 1)))
  4636. ;; Closing quote?
  4637. ((and (not next)
  4638. (string-match (nth 7 org-export-smart-quotes-regexps) s))
  4639. (setq s (replace-match
  4640. (funcall get-smart-quote (match-string 1 s) 'close)
  4641. nil t s 1)))
  4642. ;; Opening quote?
  4643. ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
  4644. (setq s (replace-match
  4645. (funcall get-smart-quote (match-string 1 s) 'open)
  4646. nil t s 1))))
  4647. ;; Return string with smart quotes.
  4648. s))))
  4649. ;;;; Topology
  4650. ;;
  4651. ;; Here are various functions to retrieve information about the
  4652. ;; neighbourhood of a given element or object. Neighbours of interest
  4653. ;; are direct parent (`org-export-get-parent'), parent headline
  4654. ;; (`org-export-get-parent-headline'), first element containing an
  4655. ;; object, (`org-export-get-parent-element'), parent table
  4656. ;; (`org-export-get-parent-table'), previous element or object
  4657. ;; (`org-export-get-previous-element') and next element or object
  4658. ;; (`org-export-get-next-element').
  4659. ;;
  4660. ;; `org-export-get-genealogy' returns the full genealogy of a given
  4661. ;; element or object, from closest parent to full parse tree.
  4662. (defsubst org-export-get-parent (blob)
  4663. "Return BLOB parent or nil.
  4664. BLOB is the element or object considered."
  4665. (org-element-property :parent blob))
  4666. (defun org-export-get-genealogy (blob)
  4667. "Return full genealogy relative to a given element or object.
  4668. BLOB is the element or object being considered.
  4669. Ancestors are returned from closest to farthest, the last one
  4670. being the full parse tree."
  4671. (let (genealogy (parent blob))
  4672. (while (setq parent (org-element-property :parent parent))
  4673. (push parent genealogy))
  4674. (nreverse genealogy)))
  4675. (defun org-export-get-parent-headline (blob)
  4676. "Return BLOB parent headline or nil.
  4677. BLOB is the element or object being considered."
  4678. (let ((parent blob))
  4679. (while (and (setq parent (org-element-property :parent parent))
  4680. (not (eq (org-element-type parent) 'headline))))
  4681. parent))
  4682. (defun org-export-get-parent-element (object)
  4683. "Return first element containing OBJECT or nil.
  4684. OBJECT is the object to consider."
  4685. (let ((parent object))
  4686. (while (and (setq parent (org-element-property :parent parent))
  4687. (memq (org-element-type parent) org-element-all-objects)))
  4688. parent))
  4689. (defun org-export-get-parent-table (object)
  4690. "Return OBJECT parent table or nil.
  4691. OBJECT is either a `table-cell' or `table-element' type object."
  4692. (let ((parent object))
  4693. (while (and (setq parent (org-element-property :parent parent))
  4694. (not (eq (org-element-type parent) 'table))))
  4695. parent))
  4696. (defun org-export-get-previous-element (blob info &optional n)
  4697. "Return previous element or object.
  4698. BLOB is an element or object. INFO is a plist used as
  4699. a communication channel. Return previous exportable element or
  4700. object, a string, or nil.
  4701. When optional argument N is a positive integer, return a list
  4702. containing up to N siblings before BLOB, from farthest to
  4703. closest. With any other non-nil value, return a list containing
  4704. all of them."
  4705. (let ((siblings
  4706. ;; An object can belong to the contents of its parent or
  4707. ;; to a secondary string. We check the latter option
  4708. ;; first.
  4709. (let ((parent (org-export-get-parent blob)))
  4710. (or (and (not (memq (org-element-type blob)
  4711. org-element-all-elements))
  4712. (let ((sec-value
  4713. (org-element-property
  4714. (cdr (assq (org-element-type parent)
  4715. org-element-secondary-value-alist))
  4716. parent)))
  4717. (and (memq blob sec-value) sec-value)))
  4718. (org-element-contents parent))))
  4719. prev)
  4720. (catch 'exit
  4721. (mapc (lambda (obj)
  4722. (cond ((memq obj (plist-get info :ignore-list)))
  4723. ((null n) (throw 'exit obj))
  4724. ((not (wholenump n)) (push obj prev))
  4725. ((zerop n) (throw 'exit prev))
  4726. (t (decf n) (push obj prev))))
  4727. (cdr (memq blob (reverse siblings))))
  4728. prev)))
  4729. (defun org-export-get-next-element (blob info &optional n)
  4730. "Return next element or object.
  4731. BLOB is an element or object. INFO is a plist used as
  4732. a communication channel. Return next exportable element or
  4733. object, a string, or nil.
  4734. When optional argument N is a positive integer, return a list
  4735. containing up to N siblings after BLOB, from closest to farthest.
  4736. With any other non-nil value, return a list containing all of
  4737. them."
  4738. (let ((siblings
  4739. ;; An object can belong to the contents of its parent or to
  4740. ;; a secondary string. We check the latter option first.
  4741. (let ((parent (org-export-get-parent blob)))
  4742. (or (and (not (memq (org-element-type blob)
  4743. org-element-all-objects))
  4744. (let ((sec-value
  4745. (org-element-property
  4746. (cdr (assq (org-element-type parent)
  4747. org-element-secondary-value-alist))
  4748. parent)))
  4749. (cdr (memq blob sec-value))))
  4750. (cdr (memq blob (org-element-contents parent))))))
  4751. next)
  4752. (catch 'exit
  4753. (mapc (lambda (obj)
  4754. (cond ((memq obj (plist-get info :ignore-list)))
  4755. ((null n) (throw 'exit obj))
  4756. ((not (wholenump n)) (push obj next))
  4757. ((zerop n) (throw 'exit (nreverse next)))
  4758. (t (decf n) (push obj next))))
  4759. siblings)
  4760. (nreverse next))))
  4761. ;;;; Translation
  4762. ;;
  4763. ;; `org-export-translate' translates a string according to the language
  4764. ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
  4765. ;; the dictionary used for the translation.
  4766. (defconst org-export-dictionary
  4767. '(("%e %n: %c"
  4768. ("fr" :default "%e %n : %c" :html "%e&nbsp;%n&nbsp;: %c"))
  4769. ("Author"
  4770. ("ca" :default "Autor")
  4771. ("cs" :default "Autor")
  4772. ("da" :default "Forfatter")
  4773. ("de" :default "Autor")
  4774. ("eo" :html "A&#365;toro")
  4775. ("es" :default "Autor")
  4776. ("fi" :html "Tekij&auml;")
  4777. ("fr" :default "Auteur")
  4778. ("hu" :default "Szerz&otilde;")
  4779. ("is" :html "H&ouml;fundur")
  4780. ("it" :default "Autore")
  4781. ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
  4782. ("nl" :default "Auteur")
  4783. ("no" :default "Forfatter")
  4784. ("nb" :default "Forfatter")
  4785. ("nn" :default "Forfattar")
  4786. ("pl" :default "Autor")
  4787. ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
  4788. ("sv" :html "F&ouml;rfattare")
  4789. ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
  4790. ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
  4791. ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
  4792. ("Date"
  4793. ("ca" :default "Data")
  4794. ("cs" :default "Datum")
  4795. ("da" :default "Dato")
  4796. ("de" :default "Datum")
  4797. ("eo" :default "Dato")
  4798. ("es" :default "Fecha")
  4799. ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
  4800. ("hu" :html "D&aacute;tum")
  4801. ("is" :default "Dagsetning")
  4802. ("it" :default "Data")
  4803. ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
  4804. ("nl" :default "Datum")
  4805. ("no" :default "Dato")
  4806. ("nb" :default "Dato")
  4807. ("nn" :default "Dato")
  4808. ("pl" :default "Data")
  4809. ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
  4810. ("sv" :default "Datum")
  4811. ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
  4812. ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
  4813. ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
  4814. ("Equation"
  4815. ("da" :default "Ligning")
  4816. ("de" :default "Gleichung")
  4817. ("es" :html "Ecuaci&oacute;n" :default "Ecuación")
  4818. ("fr" :ascii "Equation" :default "Équation")
  4819. ("no" :default "Ligning")
  4820. ("nb" :default "Ligning")
  4821. ("nn" :default "Likning")
  4822. ("sv" :default "Ekvation")
  4823. ("zh-CN" :html "&#26041;&#31243;" :utf-8 "方程"))
  4824. ("Figure"
  4825. ("da" :default "Figur")
  4826. ("de" :default "Abbildung")
  4827. ("es" :default "Figura")
  4828. ("ja" :html "&#22259;" :utf-8 "図")
  4829. ("no" :default "Illustrasjon")
  4830. ("nb" :default "Illustrasjon")
  4831. ("nn" :default "Illustrasjon")
  4832. ("sv" :default "Illustration")
  4833. ("zh-CN" :html "&#22270;" :utf-8 "图"))
  4834. ("Figure %d:"
  4835. ("da" :default "Figur %d")
  4836. ("de" :default "Abbildung %d:")
  4837. ("es" :default "Figura %d:")
  4838. ("fr" :default "Figure %d :" :html "Figure&nbsp;%d&nbsp;:")
  4839. ("ja" :html "&#22259;%d: " :utf-8 "図%d: ")
  4840. ("no" :default "Illustrasjon %d")
  4841. ("nb" :default "Illustrasjon %d")
  4842. ("nn" :default "Illustrasjon %d")
  4843. ("sv" :default "Illustration %d")
  4844. ("zh-CN" :html "&#22270;%d&nbsp;" :utf-8 "图%d "))
  4845. ("Footnotes"
  4846. ("ca" :html "Peus de p&agrave;gina")
  4847. ("cs" :default "Pozn\xe1mky pod carou")
  4848. ("da" :default "Fodnoter")
  4849. ("de" :html "Fu&szlig;noten" :default "Fußnoten")
  4850. ("eo" :default "Piednotoj")
  4851. ("es" :html "Nota al pie de p&aacute;gina" :default "Nota al pie de página")
  4852. ("fi" :default "Alaviitteet")
  4853. ("fr" :default "Notes de bas de page")
  4854. ("hu" :html "L&aacute;bjegyzet")
  4855. ("is" :html "Aftanm&aacute;lsgreinar")
  4856. ("it" :html "Note a pi&egrave; di pagina")
  4857. ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
  4858. ("nl" :default "Voetnoten")
  4859. ("no" :default "Fotnoter")
  4860. ("nb" :default "Fotnoter")
  4861. ("nn" :default "Fotnotar")
  4862. ("pl" :default "Przypis")
  4863. ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
  4864. ("sv" :default "Fotnoter")
  4865. ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
  4866. :utf-8 "Примітки")
  4867. ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
  4868. ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
  4869. ("List of Listings"
  4870. ("da" :default "Programmer")
  4871. ("de" :default "Programmauflistungsverzeichnis")
  4872. ("es" :default "Indice de Listados de programas")
  4873. ("fr" :default "Liste des programmes")
  4874. ("no" :default "Dataprogrammer")
  4875. ("nb" :default "Dataprogrammer")
  4876. ("zh-CN" :html "&#20195;&#30721;&#30446;&#24405;" :utf-8 "代码目录"))
  4877. ("List of Tables"
  4878. ("da" :default "Tabeller")
  4879. ("de" :default "Tabellenverzeichnis")
  4880. ("es" :default "Indice de tablas")
  4881. ("fr" :default "Liste des tableaux")
  4882. ("no" :default "Tabeller")
  4883. ("nb" :default "Tabeller")
  4884. ("nn" :default "Tabeller")
  4885. ("sv" :default "Tabeller")
  4886. ("zh-CN" :html "&#34920;&#26684;&#30446;&#24405;" :utf-8 "表格目录"))
  4887. ("Listing %d:"
  4888. ("da" :default "Program %d")
  4889. ("de" :default "Programmlisting %d")
  4890. ("es" :default "Listado de programa %d")
  4891. ("fr" :default "Programme %d :" :html "Programme&nbsp;%d&nbsp;:")
  4892. ("no" :default "Dataprogram")
  4893. ("nb" :default "Dataprogram")
  4894. ("zh-CN" :html "&#20195;&#30721;%d&nbsp;" :utf-8 "代码%d "))
  4895. ("See section %s"
  4896. ("da" :default "jævnfør afsnit %s")
  4897. ("de" :default "siehe Abschnitt %s")
  4898. ("es" :default "vea seccion %s")
  4899. ("fr" :default "cf. section %s")
  4900. ("zh-CN" :html "&#21442;&#35265;&#31532;%d&#33410;" :utf-8 "参见第%s节"))
  4901. ("Table"
  4902. ("de" :default "Tabelle")
  4903. ("es" :default "Tabla")
  4904. ("fr" :default "Tableau")
  4905. ("ja" :html "&#34920;" :utf-8 "表")
  4906. ("zh-CN" :html "&#34920;" :utf-8 "表"))
  4907. ("Table %d:"
  4908. ("da" :default "Tabel %d")
  4909. ("de" :default "Tabelle %d")
  4910. ("es" :default "Tabla %d")
  4911. ("fr" :default "Tableau %d :")
  4912. ("ja" :html "&#34920;%d:" :utf-8 "表%d:")
  4913. ("no" :default "Tabell %d")
  4914. ("nb" :default "Tabell %d")
  4915. ("nn" :default "Tabell %d")
  4916. ("sv" :default "Tabell %d")
  4917. ("zh-CN" :html "&#34920;%d&nbsp;" :utf-8 "表%d "))
  4918. ("Table of Contents"
  4919. ("ca" :html "&Iacute;ndex")
  4920. ("cs" :default "Obsah")
  4921. ("da" :default "Indhold")
  4922. ("de" :default "Inhaltsverzeichnis")
  4923. ("eo" :default "Enhavo")
  4924. ("es" :html "&Iacute;ndice")
  4925. ("fi" :html "Sis&auml;llysluettelo")
  4926. ("fr" :ascii "Sommaire" :default "Table des matières")
  4927. ("hu" :html "Tartalomjegyz&eacute;k")
  4928. ("is" :default "Efnisyfirlit")
  4929. ("it" :default "Indice")
  4930. ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
  4931. ("nl" :default "Inhoudsopgave")
  4932. ("no" :default "Innhold")
  4933. ("nb" :default "Innhold")
  4934. ("nn" :default "Innhald")
  4935. ("pl" :html "Spis tre&#x015b;ci")
  4936. ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
  4937. :utf-8 "Содержание")
  4938. ("sv" :html "Inneh&aring;ll")
  4939. ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
  4940. ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
  4941. ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
  4942. ("Unknown reference"
  4943. ("da" :default "ukendt reference")
  4944. ("de" :default "Unbekannter Verweis")
  4945. ("es" :default "referencia desconocida")
  4946. ("fr" :ascii "Destination inconnue" :default "Référence inconnue")
  4947. ("zh-CN" :html "&#26410;&#30693;&#24341;&#29992;" :utf-8 "未知引用")))
  4948. "Dictionary for export engine.
  4949. Alist whose CAR is the string to translate and CDR is an alist
  4950. whose CAR is the language string and CDR is a plist whose
  4951. properties are possible charsets and values translated terms.
  4952. It is used as a database for `org-export-translate'. Since this
  4953. function returns the string as-is if no translation was found,
  4954. the variable only needs to record values different from the
  4955. entry.")
  4956. (defun org-export-translate (s encoding info)
  4957. "Translate string S according to language specification.
  4958. ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
  4959. and `:utf-8'. INFO is a plist used as a communication channel.
  4960. Translation depends on `:language' property. Return the
  4961. translated string. If no translation is found, try to fall back
  4962. to `:default' encoding. If it fails, return S."
  4963. (let* ((lang (plist-get info :language))
  4964. (translations (cdr (assoc lang
  4965. (cdr (assoc s org-export-dictionary))))))
  4966. (or (plist-get translations encoding)
  4967. (plist-get translations :default)
  4968. s)))
  4969. ;;; Asynchronous Export
  4970. ;;
  4971. ;; `org-export-async-start' is the entry point for asynchronous
  4972. ;; export. It recreates current buffer (including visibility,
  4973. ;; narrowing and visited file) in an external Emacs process, and
  4974. ;; evaluates a command there. It then applies a function on the
  4975. ;; returned results in the current process.
  4976. ;;
  4977. ;; Asynchronously generated results are never displayed directly.
  4978. ;; Instead, they are stored in `org-export-stack-contents'. They can
  4979. ;; then be retrieved by calling `org-export-stack'.
  4980. ;;
  4981. ;; Export Stack is viewed through a dedicated major mode
  4982. ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
  4983. ;;`org-export-stack-delete', `org-export-stack-view' and
  4984. ;;`org-export-stack-clear'.
  4985. ;;
  4986. ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
  4987. ;; It should used whenever `org-export-async-start' is called.
  4988. (defmacro org-export-async-start (fun &rest body)
  4989. "Call function FUN on the results returned by BODY evaluation.
  4990. BODY evaluation happens in an asynchronous process, from a buffer
  4991. which is an exact copy of the current one.
  4992. Use `org-export-add-to-stack' in FUN in order to register results
  4993. in the stack. Examples for, respectively a temporary buffer and
  4994. a file are:
  4995. \(org-export-async-start
  4996. \(lambda (output)
  4997. \(with-current-buffer (get-buffer-create \"*Org BACKEND Export*\")
  4998. \(erase-buffer)
  4999. \(insert output)
  5000. \(goto-char (point-min))
  5001. \(org-export-add-to-stack (current-buffer) 'backend)))
  5002. `(org-export-as 'backend ,subtreep ,visible-only ,body-only ',ext-plist))
  5003. and
  5004. \(org-export-async-start
  5005. \(lambda (f) (org-export-add-to-stack f 'backend))
  5006. `(expand-file-name
  5007. \(org-export-to-file
  5008. 'backend ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))"
  5009. (declare (indent 1) (debug t))
  5010. (org-with-gensyms (process temp-file copy-fun proc-buffer handler coding)
  5011. ;; Write the full sexp evaluating BODY in a copy of the current
  5012. ;; buffer to a temporary file, as it may be too long for program
  5013. ;; args in `start-process'.
  5014. `(with-temp-message "Initializing asynchronous export process"
  5015. (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
  5016. (,temp-file (make-temp-file "org-export-process"))
  5017. (,coding buffer-file-coding-system))
  5018. (with-temp-file ,temp-file
  5019. (insert
  5020. ;; Null characters (from variable values) are inserted
  5021. ;; within the file. As a consequence, coding system for
  5022. ;; buffer contents will not be recognized properly. So,
  5023. ;; we make sure it is the same as the one used to display
  5024. ;; the original buffer.
  5025. (format ";; -*- coding: %s; -*-\n%S"
  5026. ,coding
  5027. `(with-temp-buffer
  5028. ,(when org-export-async-debug '(setq debug-on-error t))
  5029. ;; Ignore `kill-emacs-hook' and code evaluation
  5030. ;; queries from Babel as we need a truly
  5031. ;; non-interactive process.
  5032. (setq kill-emacs-hook nil
  5033. org-babel-confirm-evaluate-answer-no t)
  5034. ;; Initialize export framework.
  5035. (require 'ox)
  5036. ;; Re-create current buffer there.
  5037. (funcall ,,copy-fun)
  5038. (restore-buffer-modified-p nil)
  5039. ;; Sexp to evaluate in the buffer.
  5040. (print (progn ,,@body))))))
  5041. ;; Start external process.
  5042. (let* ((process-connection-type nil)
  5043. (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
  5044. (,process
  5045. (start-process
  5046. "org-export-process" ,proc-buffer
  5047. (expand-file-name invocation-name invocation-directory)
  5048. "-Q" "--batch"
  5049. "-l" org-export-async-init-file
  5050. "-l" ,temp-file)))
  5051. ;; Register running process in stack.
  5052. (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
  5053. ;; Set-up sentinel in order to catch results.
  5054. (set-process-sentinel
  5055. ,process
  5056. (let ((handler ',fun))
  5057. `(lambda (p status)
  5058. (let ((proc-buffer (process-buffer p)))
  5059. (when (eq (process-status p) 'exit)
  5060. (unwind-protect
  5061. (if (zerop (process-exit-status p))
  5062. (unwind-protect
  5063. (let ((results
  5064. (with-current-buffer proc-buffer
  5065. (goto-char (point-max))
  5066. (backward-sexp)
  5067. (read (current-buffer)))))
  5068. (funcall ,handler results))
  5069. (unless org-export-async-debug
  5070. (and (get-buffer proc-buffer)
  5071. (kill-buffer proc-buffer))))
  5072. (org-export-add-to-stack proc-buffer nil p)
  5073. (ding)
  5074. (message "Process '%s' exited abnormally" p))
  5075. (unless org-export-async-debug
  5076. (delete-file ,,temp-file)))))))))))))
  5077. (defun org-export-add-to-stack (source backend &optional process)
  5078. "Add a new result to export stack if not present already.
  5079. SOURCE is a buffer or a file name containing export results.
  5080. BACKEND is a symbol representing export back-end used to generate
  5081. it.
  5082. Entries already pointing to SOURCE and unavailable entries are
  5083. removed beforehand. Return the new stack."
  5084. (setq org-export-stack-contents
  5085. (cons (list source backend (or process (current-time)))
  5086. (org-export-stack-remove source))))
  5087. (defun org-export-stack ()
  5088. "Menu for asynchronous export results and running processes."
  5089. (interactive)
  5090. (let ((buffer (get-buffer-create "*Org Export Stack*")))
  5091. (set-buffer buffer)
  5092. (when (zerop (buffer-size)) (org-export-stack-mode))
  5093. (org-export-stack-refresh)
  5094. (pop-to-buffer buffer))
  5095. (message "Type \"q\" to quit, \"?\" for help"))
  5096. (defun org-export--stack-source-at-point ()
  5097. "Return source from export results at point in stack."
  5098. (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
  5099. (if (not source) (error "Source unavailable, please refresh buffer")
  5100. (let ((source-name (if (stringp source) source (buffer-name source))))
  5101. (if (save-excursion
  5102. (beginning-of-line)
  5103. (looking-at (concat ".* +" (regexp-quote source-name) "$")))
  5104. source
  5105. ;; SOURCE is not consistent with current line. The stack
  5106. ;; view is outdated.
  5107. (error "Source unavailable; type `g' to update buffer"))))))
  5108. (defun org-export-stack-clear ()
  5109. "Remove all entries from export stack."
  5110. (interactive)
  5111. (setq org-export-stack-contents nil))
  5112. (defun org-export-stack-refresh (&rest dummy)
  5113. "Refresh the asynchronous export stack.
  5114. DUMMY is ignored. Unavailable sources are removed from the list.
  5115. Return the new stack."
  5116. (let ((inhibit-read-only t))
  5117. (org-preserve-lc
  5118. (erase-buffer)
  5119. (insert (concat
  5120. (let ((counter 0))
  5121. (mapconcat
  5122. (lambda (entry)
  5123. (let ((proc-p (processp (nth 2 entry))))
  5124. (concat
  5125. ;; Back-end.
  5126. (format " %-12s " (or (nth 1 entry) ""))
  5127. ;; Age.
  5128. (let ((data (nth 2 entry)))
  5129. (if proc-p (format " %6s " (process-status data))
  5130. ;; Compute age of the results.
  5131. (org-format-seconds
  5132. "%4h:%.2m "
  5133. (float-time (time-since data)))))
  5134. ;; Source.
  5135. (format " %s"
  5136. (let ((source (car entry)))
  5137. (if (stringp source) source
  5138. (buffer-name source)))))))
  5139. ;; Clear stack from exited processes, dead buffers or
  5140. ;; non-existent files.
  5141. (setq org-export-stack-contents
  5142. (org-remove-if-not
  5143. (lambda (el)
  5144. (if (processp (nth 2 el))
  5145. (buffer-live-p (process-buffer (nth 2 el)))
  5146. (let ((source (car el)))
  5147. (if (bufferp source) (buffer-live-p source)
  5148. (file-exists-p source)))))
  5149. org-export-stack-contents)) "\n")))))))
  5150. (defun org-export-stack-remove (&optional source)
  5151. "Remove export results at point from stack.
  5152. If optional argument SOURCE is non-nil, remove it instead."
  5153. (interactive)
  5154. (let ((source (or source (org-export--stack-source-at-point))))
  5155. (setq org-export-stack-contents
  5156. (org-remove-if (lambda (el) (equal (car el) source))
  5157. org-export-stack-contents))))
  5158. (defun org-export-stack-view (&optional in-emacs)
  5159. "View export results at point in stack.
  5160. With an optional prefix argument IN-EMACS, force viewing files
  5161. within Emacs."
  5162. (interactive "P")
  5163. (let ((source (org-export--stack-source-at-point)))
  5164. (cond ((processp source)
  5165. (org-switch-to-buffer-other-window (process-buffer source)))
  5166. ((bufferp source) (org-switch-to-buffer-other-window source))
  5167. (t (org-open-file source in-emacs)))))
  5168. (defvar org-export-stack-mode-map
  5169. (let ((km (make-sparse-keymap)))
  5170. (define-key km " " 'next-line)
  5171. (define-key km "n" 'next-line)
  5172. (define-key km "\C-n" 'next-line)
  5173. (define-key km [down] 'next-line)
  5174. (define-key km "p" 'previous-line)
  5175. (define-key km "\C-p" 'previous-line)
  5176. (define-key km "\C-?" 'previous-line)
  5177. (define-key km [up] 'previous-line)
  5178. (define-key km "C" 'org-export-stack-clear)
  5179. (define-key km "v" 'org-export-stack-view)
  5180. (define-key km (kbd "RET") 'org-export-stack-view)
  5181. (define-key km "d" 'org-export-stack-remove)
  5182. km)
  5183. "Keymap for Org Export Stack.")
  5184. (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
  5185. "Mode for displaying asynchronous export stack.
  5186. Type \\[org-export-stack] to visualize the asynchronous export
  5187. stack.
  5188. In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
  5189. on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
  5190. stack completely.
  5191. Removing entries in an Org Export Stack buffer doesn't affect
  5192. files or buffers, only the display.
  5193. \\{org-export-stack-mode-map}"
  5194. (abbrev-mode 0)
  5195. (auto-fill-mode 0)
  5196. (setq buffer-read-only t
  5197. buffer-undo-list t
  5198. truncate-lines t
  5199. header-line-format
  5200. '(:eval
  5201. (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
  5202. (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
  5203. (set (make-local-variable 'revert-buffer-function)
  5204. 'org-export-stack-refresh))
  5205. ;;; The Dispatcher
  5206. ;;
  5207. ;; `org-export-dispatch' is the standard interactive way to start an
  5208. ;; export process. It uses `org-export--dispatch-ui' as a subroutine
  5209. ;; for its interface, which, in turn, delegates response to key
  5210. ;; pressed to `org-export--dispatch-action'.
  5211. ;;;###autoload
  5212. (defun org-export-dispatch (&optional arg)
  5213. "Export dispatcher for Org mode.
  5214. It provides an access to common export related tasks in a buffer.
  5215. Its interface comes in two flavours: standard and expert.
  5216. While both share the same set of bindings, only the former
  5217. displays the valid keys associations in a dedicated buffer.
  5218. Scrolling (resp. line-wise motion) in this buffer is done with
  5219. SPC and DEL (resp. C-n and C-p) keys.
  5220. Set variable `org-export-dispatch-use-expert-ui' to switch to one
  5221. flavour or the other.
  5222. When ARG is \\[universal-argument], repeat the last export action, with the same set
  5223. of options used back then, on the current buffer.
  5224. When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
  5225. (interactive "P")
  5226. (let* ((input
  5227. (cond ((equal arg '(16)) '(stack))
  5228. ((and arg org-export-dispatch-last-action))
  5229. (t (save-window-excursion
  5230. (unwind-protect
  5231. (progn
  5232. ;; Remember where we are
  5233. (move-marker org-export-dispatch-last-position
  5234. (point)
  5235. (org-base-buffer (current-buffer)))
  5236. ;; Get and store an export command
  5237. (setq org-export-dispatch-last-action
  5238. (org-export--dispatch-ui
  5239. (list org-export-initial-scope
  5240. (and org-export-in-background 'async))
  5241. nil
  5242. org-export-dispatch-use-expert-ui)))
  5243. (and (get-buffer "*Org Export Dispatcher*")
  5244. (kill-buffer "*Org Export Dispatcher*")))))))
  5245. (action (car input))
  5246. (optns (cdr input)))
  5247. (unless (memq 'subtree optns)
  5248. (move-marker org-export-dispatch-last-position nil))
  5249. (case action
  5250. ;; First handle special hard-coded actions.
  5251. (template (org-export-insert-default-template nil optns))
  5252. (stack (org-export-stack))
  5253. (publish-current-file
  5254. (org-publish-current-file (memq 'force optns) (memq 'async optns)))
  5255. (publish-current-project
  5256. (org-publish-current-project (memq 'force optns) (memq 'async optns)))
  5257. (publish-choose-project
  5258. (org-publish (assoc (org-icompleting-read
  5259. "Publish project: "
  5260. org-publish-project-alist nil t)
  5261. org-publish-project-alist)
  5262. (memq 'force optns)
  5263. (memq 'async optns)))
  5264. (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
  5265. (otherwise
  5266. (save-excursion
  5267. (when arg
  5268. ;; Repeating command, maybe move cursor to restore subtree
  5269. ;; context.
  5270. (if (eq (marker-buffer org-export-dispatch-last-position)
  5271. (org-base-buffer (current-buffer)))
  5272. (goto-char org-export-dispatch-last-position)
  5273. ;; We are in a different buffer, forget position.
  5274. (move-marker org-export-dispatch-last-position nil)))
  5275. (funcall action
  5276. ;; Return a symbol instead of a list to ease
  5277. ;; asynchronous export macro use.
  5278. (and (memq 'async optns) t)
  5279. (and (memq 'subtree optns) t)
  5280. (and (memq 'visible optns) t)
  5281. (and (memq 'body optns) t)))))))
  5282. (defun org-export--dispatch-ui (options first-key expertp)
  5283. "Handle interface for `org-export-dispatch'.
  5284. OPTIONS is a list containing current interactive options set for
  5285. export. It can contain any of the following symbols:
  5286. `body' toggles a body-only export
  5287. `subtree' restricts export to current subtree
  5288. `visible' restricts export to visible part of buffer.
  5289. `force' force publishing files.
  5290. `async' use asynchronous export process
  5291. FIRST-KEY is the key pressed to select the first level menu. It
  5292. is nil when this menu hasn't been selected yet.
  5293. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  5294. buffer is provided, but indications about currently active
  5295. options are given in the prompt. Moreover, \[?] allows to switch
  5296. back to standard interface."
  5297. (let* ((fontify-key
  5298. (lambda (key &optional access-key)
  5299. ;; Fontify KEY string. Optional argument ACCESS-KEY, when
  5300. ;; non-nil is the required first-level key to activate
  5301. ;; KEY. When its value is t, activate KEY independently
  5302. ;; on the first key, if any. A nil value means KEY will
  5303. ;; only be activated at first level.
  5304. (if (or (eq access-key t) (eq access-key first-key))
  5305. (org-propertize key 'face 'org-warning)
  5306. key)))
  5307. (fontify-value
  5308. (lambda (value)
  5309. ;; Fontify VALUE string.
  5310. (org-propertize value 'face 'font-lock-variable-name-face)))
  5311. ;; Prepare menu entries by extracting them from registered
  5312. ;; back-ends and sorting them by access key and by ordinal,
  5313. ;; if any.
  5314. (entries
  5315. (sort (sort (delq nil
  5316. (mapcar 'org-export-backend-menu
  5317. org-export--registered-backends))
  5318. (lambda (a b)
  5319. (let ((key-a (nth 1 a))
  5320. (key-b (nth 1 b)))
  5321. (cond ((and (numberp key-a) (numberp key-b))
  5322. (< key-a key-b))
  5323. ((numberp key-b) t)))))
  5324. 'car-less-than-car))
  5325. ;; Compute a list of allowed keys based on the first key
  5326. ;; pressed, if any. Some keys
  5327. ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
  5328. ;; available.
  5329. (allowed-keys
  5330. (nconc (list 2 22 19 6 1)
  5331. (if (not first-key) (org-uniquify (mapcar 'car entries))
  5332. (let (sub-menu)
  5333. (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
  5334. (when (eq (car entry) first-key)
  5335. (setq sub-menu (append (nth 2 entry) sub-menu))))))
  5336. (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
  5337. ((not first-key) (list ?P)))
  5338. (list ?& ?#)
  5339. (when expertp (list ??))
  5340. (list ?q)))
  5341. ;; Build the help menu for standard UI.
  5342. (help
  5343. (unless expertp
  5344. (concat
  5345. ;; Options are hard-coded.
  5346. (format "[%s] Body only: %s [%s] Visible only: %s
  5347. \[%s] Export scope: %s [%s] Force publishing: %s
  5348. \[%s] Async export: %s\n\n"
  5349. (funcall fontify-key "C-b" t)
  5350. (funcall fontify-value
  5351. (if (memq 'body options) "On " "Off"))
  5352. (funcall fontify-key "C-v" t)
  5353. (funcall fontify-value
  5354. (if (memq 'visible options) "On " "Off"))
  5355. (funcall fontify-key "C-s" t)
  5356. (funcall fontify-value
  5357. (if (memq 'subtree options) "Subtree" "Buffer "))
  5358. (funcall fontify-key "C-f" t)
  5359. (funcall fontify-value
  5360. (if (memq 'force options) "On " "Off"))
  5361. (funcall fontify-key "C-a" t)
  5362. (funcall fontify-value
  5363. (if (memq 'async options) "On " "Off")))
  5364. ;; Display registered back-end entries. When a key
  5365. ;; appears for the second time, do not create another
  5366. ;; entry, but append its sub-menu to existing menu.
  5367. (let (last-key)
  5368. (mapconcat
  5369. (lambda (entry)
  5370. (let ((top-key (car entry)))
  5371. (concat
  5372. (unless (eq top-key last-key)
  5373. (setq last-key top-key)
  5374. (format "\n[%s] %s\n"
  5375. (funcall fontify-key (char-to-string top-key))
  5376. (nth 1 entry)))
  5377. (let ((sub-menu (nth 2 entry)))
  5378. (unless (functionp sub-menu)
  5379. ;; Split sub-menu into two columns.
  5380. (let ((index -1))
  5381. (concat
  5382. (mapconcat
  5383. (lambda (sub-entry)
  5384. (incf index)
  5385. (format
  5386. (if (zerop (mod index 2)) " [%s] %-26s"
  5387. "[%s] %s\n")
  5388. (funcall fontify-key
  5389. (char-to-string (car sub-entry))
  5390. top-key)
  5391. (nth 1 sub-entry)))
  5392. sub-menu "")
  5393. (when (zerop (mod index 2)) "\n"))))))))
  5394. entries ""))
  5395. ;; Publishing menu is hard-coded.
  5396. (format "\n[%s] Publish
  5397. [%s] Current file [%s] Current project
  5398. [%s] Choose project [%s] All projects\n\n\n"
  5399. (funcall fontify-key "P")
  5400. (funcall fontify-key "f" ?P)
  5401. (funcall fontify-key "p" ?P)
  5402. (funcall fontify-key "x" ?P)
  5403. (funcall fontify-key "a" ?P))
  5404. (format "[%s] Export stack [%s] Insert template\n"
  5405. (funcall fontify-key "&" t)
  5406. (funcall fontify-key "#" t))
  5407. (format "[%s] %s"
  5408. (funcall fontify-key "q" t)
  5409. (if first-key "Main menu" "Exit")))))
  5410. ;; Build prompts for both standard and expert UI.
  5411. (standard-prompt (unless expertp "Export command: "))
  5412. (expert-prompt
  5413. (when expertp
  5414. (format
  5415. "Export command (C-%s%s%s%s%s) [%s]: "
  5416. (if (memq 'body options) (funcall fontify-key "b" t) "b")
  5417. (if (memq 'visible options) (funcall fontify-key "v" t) "v")
  5418. (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
  5419. (if (memq 'force options) (funcall fontify-key "f" t) "f")
  5420. (if (memq 'async options) (funcall fontify-key "a" t) "a")
  5421. (mapconcat (lambda (k)
  5422. ;; Strip control characters.
  5423. (unless (< k 27) (char-to-string k)))
  5424. allowed-keys "")))))
  5425. ;; With expert UI, just read key with a fancy prompt. In standard
  5426. ;; UI, display an intrusive help buffer.
  5427. (if expertp
  5428. (org-export--dispatch-action
  5429. expert-prompt allowed-keys entries options first-key expertp)
  5430. ;; At first call, create frame layout in order to display menu.
  5431. (unless (get-buffer "*Org Export Dispatcher*")
  5432. (delete-other-windows)
  5433. (org-switch-to-buffer-other-window
  5434. (get-buffer-create "*Org Export Dispatcher*"))
  5435. (setq cursor-type nil
  5436. header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
  5437. ;; Make sure that invisible cursor will not highlight square
  5438. ;; brackets.
  5439. (set-syntax-table (copy-syntax-table))
  5440. (modify-syntax-entry ?\[ "w"))
  5441. ;; At this point, the buffer containing the menu exists and is
  5442. ;; visible in the current window. So, refresh it.
  5443. (with-current-buffer "*Org Export Dispatcher*"
  5444. ;; Refresh help. Maintain display continuity by re-visiting
  5445. ;; previous window position.
  5446. (let ((pos (window-start)))
  5447. (erase-buffer)
  5448. (insert help)
  5449. (set-window-start nil pos)))
  5450. (org-fit-window-to-buffer)
  5451. (org-export--dispatch-action
  5452. standard-prompt allowed-keys entries options first-key expertp))))
  5453. (defun org-export--dispatch-action
  5454. (prompt allowed-keys entries options first-key expertp)
  5455. "Read a character from command input and act accordingly.
  5456. PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
  5457. a list of characters available at a given step in the process.
  5458. ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
  5459. EXPERTP are the same as defined in `org-export--dispatch-ui',
  5460. which see.
  5461. Toggle export options when required. Otherwise, return value is
  5462. a list with action as CAR and a list of interactive export
  5463. options as CDR."
  5464. (let (key)
  5465. ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
  5466. ;; C-p, SPC, DEL).
  5467. (while (and (setq key (read-char-exclusive prompt))
  5468. (not expertp)
  5469. (memq key '(14 16 ?\s ?\d)))
  5470. (case key
  5471. (14 (if (not (pos-visible-in-window-p (point-max)))
  5472. (ignore-errors (scroll-up 1))
  5473. (message "End of buffer")
  5474. (sit-for 1)))
  5475. (16 (if (not (pos-visible-in-window-p (point-min)))
  5476. (ignore-errors (scroll-down 1))
  5477. (message "Beginning of buffer")
  5478. (sit-for 1)))
  5479. (?\s (if (not (pos-visible-in-window-p (point-max)))
  5480. (scroll-up nil)
  5481. (message "End of buffer")
  5482. (sit-for 1)))
  5483. (?\d (if (not (pos-visible-in-window-p (point-min)))
  5484. (scroll-down nil)
  5485. (message "Beginning of buffer")
  5486. (sit-for 1)))))
  5487. (cond
  5488. ;; Ignore undefined associations.
  5489. ((not (memq key allowed-keys))
  5490. (ding)
  5491. (unless expertp (message "Invalid key") (sit-for 1))
  5492. (org-export--dispatch-ui options first-key expertp))
  5493. ;; q key at first level aborts export. At second level, cancel
  5494. ;; first key instead.
  5495. ((eq key ?q) (if (not first-key) (error "Export aborted")
  5496. (org-export--dispatch-ui options nil expertp)))
  5497. ;; Help key: Switch back to standard interface if expert UI was
  5498. ;; active.
  5499. ((eq key ??) (org-export--dispatch-ui options first-key nil))
  5500. ;; Send request for template insertion along with export scope.
  5501. ((eq key ?#) (cons 'template (memq 'subtree options)))
  5502. ;; Switch to asynchronous export stack.
  5503. ((eq key ?&) '(stack))
  5504. ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
  5505. ((memq key '(2 22 19 6 1))
  5506. (org-export--dispatch-ui
  5507. (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
  5508. (6 'force) (1 'async))))
  5509. (if (memq option options) (remq option options)
  5510. (cons option options)))
  5511. first-key expertp))
  5512. ;; Action selected: Send key and options back to
  5513. ;; `org-export-dispatch'.
  5514. ((or first-key (functionp (nth 2 (assq key entries))))
  5515. (cons (cond
  5516. ((not first-key) (nth 2 (assq key entries)))
  5517. ;; Publishing actions are hard-coded. Send a special
  5518. ;; signal to `org-export-dispatch'.
  5519. ((eq first-key ?P)
  5520. (case key
  5521. (?f 'publish-current-file)
  5522. (?p 'publish-current-project)
  5523. (?x 'publish-choose-project)
  5524. (?a 'publish-all)))
  5525. ;; Return first action associated to FIRST-KEY + KEY
  5526. ;; path. Indeed, derived backends can share the same
  5527. ;; FIRST-KEY.
  5528. (t (catch 'found
  5529. (mapc (lambda (entry)
  5530. (let ((match (assq key (nth 2 entry))))
  5531. (when match (throw 'found (nth 2 match)))))
  5532. (member (assq first-key entries) entries)))))
  5533. options))
  5534. ;; Otherwise, enter sub-menu.
  5535. (t (org-export--dispatch-ui options key expertp)))))
  5536. (provide 'ox)
  5537. ;; Local variables:
  5538. ;; generated-autoload-file: "org-loaddefs.el"
  5539. ;; End:
  5540. ;;; ox.el ends here