ox.el 228 KB

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