ox.el 242 KB

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