ox.el 228 KB

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