ox.el 243 KB

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