ox.el 246 KB

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