ox.el 246 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384
  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. (org-element-parse-secondary-string
  1581. value (org-element-restriction 'keyword))))))))))
  1582. (defun org-export--get-buffer-attributes ()
  1583. "Return properties related to buffer attributes, as a plist."
  1584. ;; Store full path of input file name, or nil. For internal use.
  1585. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  1586. (list :input-buffer (buffer-name (buffer-base-buffer))
  1587. :input-file visited-file
  1588. :title (if (not visited-file) (buffer-name (buffer-base-buffer))
  1589. (file-name-sans-extension
  1590. (file-name-nondirectory visited-file))))))
  1591. (defun org-export--get-global-options (&optional backend)
  1592. "Return global export options as a plist.
  1593. Optional argument BACKEND, if non-nil, is an export back-end, as
  1594. returned by, e.g., `org-export-create-backend'. It specifies
  1595. which back-end specific export options should also be read in the
  1596. process."
  1597. (let (plist
  1598. ;; Priority is given to back-end specific options.
  1599. (all (append (and backend (org-export-get-all-options backend))
  1600. org-export-options-alist)))
  1601. (dolist (cell all plist)
  1602. (let ((prop (car cell))
  1603. (default-value (nth 3 cell)))
  1604. (unless (or (not default-value) (plist-member plist prop))
  1605. (setq plist
  1606. (plist-put
  1607. plist
  1608. prop
  1609. ;; Eval 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 (not (stringp value)) value
  1614. (let ((keyword (nth 1 cell)))
  1615. (if (member keyword 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. ;; Expand export-specific set of macros: {{{author}}},
  2751. ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
  2752. ;; once regular macros have been expanded, since document
  2753. ;; keywords may contain one of them.
  2754. (org-macro-replace-all
  2755. (list (cons "author"
  2756. (org-element-interpret-data (plist-get info :author)))
  2757. (cons "date"
  2758. (org-element-interpret-data (plist-get info :date)))
  2759. ;; EMAIL is not a parsed keyword: store it as-is.
  2760. (cons "email" (or (plist-get info :email) ""))
  2761. (cons "title"
  2762. (org-element-interpret-data (plist-get info :title)))))
  2763. ;; Parse buffer.
  2764. (setq tree (org-element-parse-buffer nil visible-only))
  2765. ;; Handle left-over uninterpreted elements or objects in
  2766. ;; parse tree and communication channel.
  2767. (org-export-remove-uninterpreted-data tree info)
  2768. ;; Call options filters and update export options. We do not
  2769. ;; use `org-export-filter-apply-functions' here since the
  2770. ;; arity of such filters is different.
  2771. (let ((backend-name (org-export-backend-name backend)))
  2772. (dolist (filter (plist-get info :filter-options))
  2773. (let ((result (funcall filter info backend-name)))
  2774. (when result (setq info result)))))
  2775. ;; Parse buffer, handle uninterpreted elements or objects,
  2776. ;; then call parse-tree filters.
  2777. (setq tree
  2778. (org-export-filter-apply-functions
  2779. (plist-get info :filter-parse-tree) tree info))
  2780. ;; Now tree is complete, compute its properties and add them
  2781. ;; to communication channel.
  2782. (setq info
  2783. (org-combine-plists
  2784. info (org-export-collect-tree-properties tree info)))
  2785. ;; Eventually transcode TREE. Wrap the resulting string into
  2786. ;; a template.
  2787. (let* ((body (org-element-normalize-string
  2788. (or (org-export-data tree info) "")))
  2789. (inner-template (cdr (assq 'inner-template
  2790. (plist-get info :translate-alist))))
  2791. (full-body (if (not (functionp inner-template)) body
  2792. (funcall inner-template body info)))
  2793. (template (cdr (assq 'template
  2794. (plist-get info :translate-alist)))))
  2795. ;; Remove all text properties since they cannot be
  2796. ;; retrieved from an external process. Finally call
  2797. ;; final-output filter and return result.
  2798. (org-no-properties
  2799. (org-export-filter-apply-functions
  2800. (plist-get info :filter-final-output)
  2801. (if (or (not (functionp template)) body-only) full-body
  2802. (funcall template full-body info))
  2803. info))))))))
  2804. ;;;###autoload
  2805. (defun org-export-string-as (string backend &optional body-only ext-plist)
  2806. "Transcode STRING into BACKEND code.
  2807. BACKEND is either an export back-end, as returned by, e.g.,
  2808. `org-export-create-backend', or a symbol referring to
  2809. a registered back-end.
  2810. When optional argument BODY-ONLY is non-nil, only return body
  2811. code, without preamble nor postamble.
  2812. Optional argument EXT-PLIST, when provided, is a property list
  2813. with external parameters overriding Org default settings, but
  2814. still inferior to file-local settings.
  2815. Return code as a string."
  2816. (with-temp-buffer
  2817. (insert string)
  2818. (let ((org-inhibit-startup t)) (org-mode))
  2819. (org-export-as backend nil nil body-only ext-plist)))
  2820. ;;;###autoload
  2821. (defun org-export-replace-region-by (backend)
  2822. "Replace the active region by its export to BACKEND.
  2823. BACKEND is either an export back-end, as returned by, e.g.,
  2824. `org-export-create-backend', or a symbol referring to
  2825. a registered back-end."
  2826. (if (not (org-region-active-p))
  2827. (user-error "No active region to replace")
  2828. (let* ((beg (region-beginning))
  2829. (end (region-end))
  2830. (str (buffer-substring beg end)) rpl)
  2831. (setq rpl (org-export-string-as str backend t))
  2832. (delete-region beg end)
  2833. (insert rpl))))
  2834. ;;;###autoload
  2835. (defun org-export-insert-default-template (&optional backend subtreep)
  2836. "Insert all export keywords with default values at beginning of line.
  2837. BACKEND is a symbol referring to the name of a registered export
  2838. back-end, for which specific export options should be added to
  2839. the template, or `default' for default template. When it is nil,
  2840. the user will be prompted for a category.
  2841. If SUBTREEP is non-nil, export configuration will be set up
  2842. locally for the subtree through node properties."
  2843. (interactive)
  2844. (unless (derived-mode-p 'org-mode) (user-error "Not in an Org mode buffer"))
  2845. (when (and subtreep (org-before-first-heading-p))
  2846. (user-error "No subtree to set export options for"))
  2847. (let ((node (and subtreep (save-excursion (org-back-to-heading t) (point))))
  2848. (backend
  2849. (or backend
  2850. (intern
  2851. (org-completing-read
  2852. "Options category: "
  2853. (cons "default"
  2854. (mapcar #'(lambda (b)
  2855. (symbol-name (org-export-backend-name b)))
  2856. org-export--registered-backends))
  2857. nil t))))
  2858. options keywords)
  2859. ;; Populate OPTIONS and KEYWORDS.
  2860. (dolist (entry (cond ((eq backend 'default) org-export-options-alist)
  2861. ((org-export-backend-p backend)
  2862. (org-export-backend-options backend))
  2863. (t (org-export-backend-options
  2864. (org-export-get-backend backend)))))
  2865. (let ((keyword (nth 1 entry))
  2866. (option (nth 2 entry)))
  2867. (cond
  2868. (keyword (unless (assoc keyword keywords)
  2869. (let ((value
  2870. (if (eq (nth 4 entry) 'split)
  2871. (mapconcat #'identity (eval (nth 3 entry)) " ")
  2872. (eval (nth 3 entry)))))
  2873. (push (cons keyword value) keywords))))
  2874. (option (unless (assoc option options)
  2875. (push (cons option (eval (nth 3 entry))) options))))))
  2876. ;; Move to an appropriate location in order to insert options.
  2877. (unless subtreep (beginning-of-line))
  2878. ;; First (multiple) OPTIONS lines. Never go past fill-column.
  2879. (when options
  2880. (let ((items
  2881. (mapcar
  2882. #'(lambda (opt) (format "%s:%S" (car opt) (cdr opt)))
  2883. (sort options (lambda (k1 k2) (string< (car k1) (car k2)))))))
  2884. (if subtreep
  2885. (org-entry-put
  2886. node "EXPORT_OPTIONS" (mapconcat 'identity items " "))
  2887. (while items
  2888. (insert "#+OPTIONS:")
  2889. (let ((width 10))
  2890. (while (and items
  2891. (< (+ width (length (car items)) 1) fill-column))
  2892. (let ((item (pop items)))
  2893. (insert " " item)
  2894. (incf width (1+ (length item))))))
  2895. (insert "\n")))))
  2896. ;; Then the rest of keywords, in the order specified in either
  2897. ;; `org-export-options-alist' or respective export back-ends.
  2898. (dolist (key (nreverse keywords))
  2899. (let ((val (cond ((equal (car key) "DATE")
  2900. (or (cdr key)
  2901. (with-temp-buffer
  2902. (org-insert-time-stamp (current-time)))))
  2903. ((equal (car key) "TITLE")
  2904. (or (let ((visited-file
  2905. (buffer-file-name (buffer-base-buffer))))
  2906. (and visited-file
  2907. (file-name-sans-extension
  2908. (file-name-nondirectory visited-file))))
  2909. (buffer-name (buffer-base-buffer))))
  2910. (t (cdr key)))))
  2911. (if subtreep (org-entry-put node (concat "EXPORT_" (car key)) val)
  2912. (insert
  2913. (format "#+%s:%s\n"
  2914. (car key)
  2915. (if (org-string-nw-p val) (format " %s" val) ""))))))))
  2916. (defun org-export-expand-include-keyword (&optional included dir)
  2917. "Expand every include keyword in buffer.
  2918. Optional argument INCLUDED is a list of included file names along
  2919. with their line restriction, when appropriate. It is used to
  2920. avoid infinite recursion. Optional argument DIR is the current
  2921. working directory. It is used to properly resolve relative
  2922. paths."
  2923. (let ((case-fold-search t)
  2924. (file-prefix (make-hash-table :test #'equal))
  2925. (current-prefix 0))
  2926. (goto-char (point-min))
  2927. (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
  2928. (let ((element (save-match-data (org-element-at-point))))
  2929. (when (eq (org-element-type element) 'keyword)
  2930. (beginning-of-line)
  2931. ;; Extract arguments from keyword's value.
  2932. (let* ((value (org-element-property :value element))
  2933. (ind (org-get-indentation))
  2934. (file (and (string-match
  2935. "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value)
  2936. (prog1 (expand-file-name
  2937. (org-remove-double-quotes
  2938. (match-string 1 value))
  2939. dir)
  2940. (setq value (replace-match "" nil nil value)))))
  2941. (lines
  2942. (and (string-match
  2943. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
  2944. value)
  2945. (prog1 (match-string 1 value)
  2946. (setq value (replace-match "" nil nil value)))))
  2947. (env (cond ((string-match "\\<example\\>" value) 'example)
  2948. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  2949. (match-string 1 value))))
  2950. ;; Minimal level of included file defaults to the child
  2951. ;; level of the current headline, if any, or one. It
  2952. ;; only applies is the file is meant to be included as
  2953. ;; an Org one.
  2954. (minlevel
  2955. (and (not env)
  2956. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  2957. (prog1 (string-to-number (match-string 1 value))
  2958. (setq value (replace-match "" nil nil value)))
  2959. (let ((cur (org-current-level)))
  2960. (if cur (1+ (org-reduced-level cur)) 1))))))
  2961. ;; Remove keyword.
  2962. (delete-region (point) (progn (forward-line) (point)))
  2963. (cond
  2964. ((not file) nil)
  2965. ((not (file-readable-p file))
  2966. (error "Cannot include file %s" file))
  2967. ;; Check if files has already been parsed. Look after
  2968. ;; inclusion lines too, as different parts of the same file
  2969. ;; can be included too.
  2970. ((member (list file lines) included)
  2971. (error "Recursive file inclusion: %s" file))
  2972. (t
  2973. (cond
  2974. ((eq env 'example)
  2975. (insert
  2976. (let ((ind-str (make-string ind ? ))
  2977. (contents
  2978. (org-escape-code-in-string
  2979. (org-export--prepare-file-contents file lines))))
  2980. (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
  2981. ind-str contents ind-str))))
  2982. ((stringp env)
  2983. (insert
  2984. (let ((ind-str (make-string ind ? ))
  2985. (contents
  2986. (org-escape-code-in-string
  2987. (org-export--prepare-file-contents file lines))))
  2988. (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
  2989. ind-str env contents ind-str))))
  2990. (t
  2991. (insert
  2992. (with-temp-buffer
  2993. (let ((org-inhibit-startup t)) (org-mode))
  2994. (insert
  2995. (org-export--prepare-file-contents
  2996. file lines ind minlevel
  2997. (or (gethash file file-prefix)
  2998. (puthash file (incf current-prefix) file-prefix))))
  2999. (org-export-expand-include-keyword
  3000. (cons (list file lines) included)
  3001. (file-name-directory file))
  3002. (buffer-string)))))))))))))
  3003. (defun org-export--prepare-file-contents (file &optional lines ind minlevel id)
  3004. "Prepare the contents of FILE for inclusion and return them as a string.
  3005. When optional argument LINES is a string specifying a range of
  3006. lines, include only those lines.
  3007. Optional argument IND, when non-nil, is an integer specifying the
  3008. global indentation of returned contents. Since its purpose is to
  3009. allow an included file to stay in the same environment it was
  3010. created \(i.e. a list item), it doesn't apply past the first
  3011. headline encountered.
  3012. Optional argument MINLEVEL, when non-nil, is an integer
  3013. specifying the level that any top-level headline in the included
  3014. file should have.
  3015. Optional argument ID is an integer that will be inserted before
  3016. each footnote definition and reference if FILE is an Org file.
  3017. This is useful to avoid conflicts when more than one Org file
  3018. with footnotes is included in a document."
  3019. (with-temp-buffer
  3020. (insert-file-contents file)
  3021. (when lines
  3022. (let* ((lines (split-string lines "-"))
  3023. (lbeg (string-to-number (car lines)))
  3024. (lend (string-to-number (cadr lines)))
  3025. (beg (if (zerop lbeg) (point-min)
  3026. (goto-char (point-min))
  3027. (forward-line (1- lbeg))
  3028. (point)))
  3029. (end (if (zerop lend) (point-max)
  3030. (goto-char (point-min))
  3031. (forward-line (1- lend))
  3032. (point))))
  3033. (narrow-to-region beg end)))
  3034. ;; Remove blank lines at beginning and end of contents. The logic
  3035. ;; behind that removal is that blank lines around include keyword
  3036. ;; override blank lines in included file.
  3037. (goto-char (point-min))
  3038. (org-skip-whitespace)
  3039. (beginning-of-line)
  3040. (delete-region (point-min) (point))
  3041. (goto-char (point-max))
  3042. (skip-chars-backward " \r\t\n")
  3043. (forward-line)
  3044. (delete-region (point) (point-max))
  3045. ;; If IND is set, preserve indentation of include keyword until
  3046. ;; the first headline encountered.
  3047. (when ind
  3048. (unless (eq major-mode 'org-mode)
  3049. (let ((org-inhibit-startup t)) (org-mode)))
  3050. (goto-char (point-min))
  3051. (let ((ind-str (make-string ind ? )))
  3052. (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
  3053. ;; Do not move footnote definitions out of column 0.
  3054. (unless (and (looking-at org-footnote-definition-re)
  3055. (eq (org-element-type (org-element-at-point))
  3056. 'footnote-definition))
  3057. (insert ind-str))
  3058. (forward-line))))
  3059. ;; When MINLEVEL is specified, compute minimal level for headlines
  3060. ;; in the file (CUR-MIN), and remove stars to each headline so
  3061. ;; that headlines with minimal level have a level of MINLEVEL.
  3062. (when minlevel
  3063. (unless (eq major-mode 'org-mode)
  3064. (let ((org-inhibit-startup t)) (org-mode)))
  3065. (org-with-limited-levels
  3066. (let ((levels (org-map-entries
  3067. (lambda () (org-reduced-level (org-current-level))))))
  3068. (when levels
  3069. (let ((offset (- minlevel (apply 'min levels))))
  3070. (unless (zerop offset)
  3071. (when org-odd-levels-only (setq offset (* offset 2)))
  3072. ;; Only change stars, don't bother moving whole
  3073. ;; sections.
  3074. (org-map-entries
  3075. (lambda () (if (< offset 0) (delete-char (abs offset))
  3076. (insert (make-string offset ?*)))))))))))
  3077. ;; Append ID to all footnote references and definitions, so they
  3078. ;; become file specific and cannot collide with footnotes in other
  3079. ;; included files.
  3080. (when id
  3081. (goto-char (point-min))
  3082. (while (re-search-forward org-footnote-re nil t)
  3083. (let ((reference (org-element-context)))
  3084. (when (memq (org-element-type reference)
  3085. '(footnote-reference footnote-definition))
  3086. (goto-char (org-element-property :begin reference))
  3087. (forward-char)
  3088. (let ((label (org-element-property :label reference)))
  3089. (cond ((not label))
  3090. ((org-string-match-p "\\`[0-9]+\\'" label)
  3091. (insert (format "fn:%d-" id)))
  3092. (t (forward-char 3) (insert (format "%d-" id)))))))))
  3093. (org-element-normalize-string (buffer-string))))
  3094. (defun org-export-execute-babel-code ()
  3095. "Execute every Babel code in the visible part of current buffer."
  3096. ;; Get a pristine copy of current buffer so Babel references can be
  3097. ;; properly resolved.
  3098. (let ((reference (org-export-copy-buffer)))
  3099. (unwind-protect (org-babel-exp-process-buffer reference)
  3100. (kill-buffer reference))))
  3101. (defun org-export--copy-to-kill-ring-p ()
  3102. "Return a non-nil value when output should be added to the kill ring.
  3103. See also `org-export-copy-to-kill-ring'."
  3104. (if (eq org-export-copy-to-kill-ring 'if-interactive)
  3105. (not (or executing-kbd-macro noninteractive))
  3106. (eq org-export-copy-to-kill-ring t)))
  3107. ;;; Tools For Back-Ends
  3108. ;;
  3109. ;; A whole set of tools is available to help build new exporters. Any
  3110. ;; function general enough to have its use across many back-ends
  3111. ;; should be added here.
  3112. ;;;; For Affiliated Keywords
  3113. ;;
  3114. ;; `org-export-read-attribute' reads a property from a given element
  3115. ;; as a plist. It can be used to normalize affiliated keywords'
  3116. ;; syntax.
  3117. ;;
  3118. ;; Since captions can span over multiple lines and accept dual values,
  3119. ;; their internal representation is a bit tricky. Therefore,
  3120. ;; `org-export-get-caption' transparently returns a given element's
  3121. ;; caption as a secondary string.
  3122. (defun org-export-read-attribute (attribute element &optional property)
  3123. "Turn ATTRIBUTE property from ELEMENT into a plist.
  3124. When optional argument PROPERTY is non-nil, return the value of
  3125. that property within attributes.
  3126. This function assumes attributes are defined as \":keyword
  3127. value\" pairs. It is appropriate for `:attr_html' like
  3128. properties.
  3129. All values will become strings except the empty string and
  3130. \"nil\", which will become nil. Also, values containing only
  3131. double quotes will be read as-is, which means that \"\" value
  3132. will become the empty string."
  3133. (let* ((prepare-value
  3134. (lambda (str)
  3135. (save-match-data
  3136. (cond ((member str '(nil "" "nil")) nil)
  3137. ((string-match "^\"\\(\"+\\)?\"$" str)
  3138. (or (match-string 1 str) ""))
  3139. (t str)))))
  3140. (attributes
  3141. (let ((value (org-element-property attribute element)))
  3142. (when value
  3143. (let ((s (mapconcat 'identity value " ")) result)
  3144. (while (string-match
  3145. "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
  3146. s)
  3147. (let ((value (substring s 0 (match-beginning 0))))
  3148. (push (funcall prepare-value value) result))
  3149. (push (intern (match-string 1 s)) result)
  3150. (setq s (substring s (match-end 0))))
  3151. ;; Ignore any string before first property with `cdr'.
  3152. (cdr (nreverse (cons (funcall prepare-value s) result))))))))
  3153. (if property (plist-get attributes property) attributes)))
  3154. (defun org-export-get-caption (element &optional shortp)
  3155. "Return caption from ELEMENT as a secondary string.
  3156. When optional argument SHORTP is non-nil, return short caption,
  3157. as a secondary string, instead.
  3158. Caption lines are separated by a white space."
  3159. (let ((full-caption (org-element-property :caption element)) caption)
  3160. (dolist (line full-caption (cdr caption))
  3161. (let ((cap (funcall (if shortp 'cdr 'car) line)))
  3162. (when cap
  3163. (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
  3164. ;;;; For Derived Back-ends
  3165. ;;
  3166. ;; `org-export-with-backend' is a function allowing to locally use
  3167. ;; another back-end to transcode some object or element. In a derived
  3168. ;; back-end, it may be used as a fall-back function once all specific
  3169. ;; cases have been treated.
  3170. (defun org-export-with-backend (backend data &optional contents info)
  3171. "Call a transcoder from BACKEND on DATA.
  3172. BACKEND is an export back-end, as returned by, e.g.,
  3173. `org-export-create-backend', or a symbol referring to
  3174. a registered back-end. DATA is an Org element, object, secondary
  3175. string or string. CONTENTS, when non-nil, is the transcoded
  3176. contents of DATA element, as a string. INFO, when non-nil, is
  3177. the communication channel used for export, as a plist."
  3178. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  3179. (org-export-barf-if-invalid-backend backend)
  3180. (let ((type (org-element-type data)))
  3181. (if (memq type '(nil org-data)) (error "No foreign transcoder available")
  3182. (let* ((all-transcoders (org-export-get-all-transcoders backend))
  3183. (transcoder (cdr (assq type all-transcoders))))
  3184. (if (not (functionp transcoder))
  3185. (error "No foreign transcoder available")
  3186. (funcall
  3187. transcoder data contents
  3188. (org-combine-plists
  3189. info (list :back-end backend
  3190. :translate-alist all-transcoders
  3191. :exported-data (make-hash-table :test 'eq :size 401)))))))))
  3192. ;;;; For Export Snippets
  3193. ;;
  3194. ;; Every export snippet is transmitted to the back-end. Though, the
  3195. ;; latter will only retain one type of export-snippet, ignoring
  3196. ;; others, based on the former's target back-end. The function
  3197. ;; `org-export-snippet-backend' returns that back-end for a given
  3198. ;; export-snippet.
  3199. (defun org-export-snippet-backend (export-snippet)
  3200. "Return EXPORT-SNIPPET targeted back-end as a symbol.
  3201. Translation, with `org-export-snippet-translation-alist', is
  3202. applied."
  3203. (let ((back-end (org-element-property :back-end export-snippet)))
  3204. (intern
  3205. (or (cdr (assoc back-end org-export-snippet-translation-alist))
  3206. back-end))))
  3207. ;;;; For Footnotes
  3208. ;;
  3209. ;; `org-export-collect-footnote-definitions' is a tool to list
  3210. ;; actually used footnotes definitions in the whole parse tree, or in
  3211. ;; a headline, in order to add footnote listings throughout the
  3212. ;; transcoded data.
  3213. ;;
  3214. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  3215. ;; back-ends, when they need to attach the footnote definition only to
  3216. ;; the first occurrence of the corresponding label.
  3217. ;;
  3218. ;; `org-export-get-footnote-definition' and
  3219. ;; `org-export-get-footnote-number' provide easier access to
  3220. ;; additional information relative to a footnote reference.
  3221. (defun org-export-collect-footnote-definitions (data info)
  3222. "Return an alist between footnote numbers, labels and definitions.
  3223. DATA is the parse tree from which definitions are collected.
  3224. INFO is the plist used as a communication channel.
  3225. Definitions are sorted by order of references. They either
  3226. appear as Org data or as a secondary string for inlined
  3227. footnotes. Unreferenced definitions are ignored."
  3228. (let* (num-alist
  3229. collect-fn ; for byte-compiler.
  3230. (collect-fn
  3231. (function
  3232. (lambda (data)
  3233. ;; Collect footnote number, label and definition in DATA.
  3234. (org-element-map data 'footnote-reference
  3235. (lambda (fn)
  3236. (when (org-export-footnote-first-reference-p fn info)
  3237. (let ((def (org-export-get-footnote-definition fn info)))
  3238. (push
  3239. (list (org-export-get-footnote-number fn info)
  3240. (org-element-property :label fn)
  3241. def)
  3242. num-alist)
  3243. ;; Also search in definition for nested footnotes.
  3244. (when (eq (org-element-property :type fn) 'standard)
  3245. (funcall collect-fn def)))))
  3246. ;; Don't enter footnote definitions since it will happen
  3247. ;; when their first reference is found.
  3248. info nil 'footnote-definition)))))
  3249. (funcall collect-fn (plist-get info :parse-tree))
  3250. (reverse num-alist)))
  3251. (defun org-export-footnote-first-reference-p (footnote-reference info)
  3252. "Non-nil when a footnote reference is the first one for its label.
  3253. FOOTNOTE-REFERENCE is the footnote reference being considered.
  3254. INFO is the plist used as a communication channel."
  3255. (let ((label (org-element-property :label footnote-reference)))
  3256. ;; Anonymous footnotes are always a first reference.
  3257. (if (not label) t
  3258. ;; Otherwise, return the first footnote with the same LABEL and
  3259. ;; test if it is equal to FOOTNOTE-REFERENCE.
  3260. (let* (search-refs ; for byte-compiler.
  3261. (search-refs
  3262. (function
  3263. (lambda (data)
  3264. (org-element-map data 'footnote-reference
  3265. (lambda (fn)
  3266. (cond
  3267. ((string= (org-element-property :label fn) label)
  3268. (throw 'exit fn))
  3269. ;; If FN isn't inlined, be sure to traverse its
  3270. ;; definition before resuming search. See
  3271. ;; comments in `org-export-get-footnote-number'
  3272. ;; for more information.
  3273. ((eq (org-element-property :type fn) 'standard)
  3274. (funcall search-refs
  3275. (org-export-get-footnote-definition fn info)))))
  3276. ;; Don't enter footnote definitions since it will
  3277. ;; happen when their first reference is found.
  3278. info 'first-match 'footnote-definition)))))
  3279. (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
  3280. footnote-reference)))))
  3281. (defun org-export-get-footnote-definition (footnote-reference info)
  3282. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  3283. INFO is the plist used as a communication channel. If no such
  3284. definition can be found, return the \"DEFINITION NOT FOUND\"
  3285. string."
  3286. (let ((label (org-element-property :label footnote-reference)))
  3287. (or (org-element-property :inline-definition footnote-reference)
  3288. (cdr (assoc label (plist-get info :footnote-definition-alist)))
  3289. "DEFINITION NOT FOUND.")))
  3290. (defun org-export-get-footnote-number (footnote info)
  3291. "Return number associated to a footnote.
  3292. FOOTNOTE is either a footnote reference or a footnote definition.
  3293. INFO is the plist used as a communication channel."
  3294. (let* ((label (org-element-property :label footnote))
  3295. seen-refs
  3296. search-ref ; For byte-compiler.
  3297. (search-ref
  3298. (function
  3299. (lambda (data)
  3300. ;; Search footnote references through DATA, filling
  3301. ;; SEEN-REFS along the way.
  3302. (org-element-map data 'footnote-reference
  3303. (lambda (fn)
  3304. (let ((fn-lbl (org-element-property :label fn)))
  3305. (cond
  3306. ;; Anonymous footnote match: return number.
  3307. ((and (not fn-lbl) (eq fn footnote))
  3308. (throw 'exit (1+ (length seen-refs))))
  3309. ;; Labels match: return number.
  3310. ((and label (string= label fn-lbl))
  3311. (throw 'exit (1+ (length seen-refs))))
  3312. ;; Anonymous footnote: it's always a new one.
  3313. ;; Also, be sure to return nil from the `cond' so
  3314. ;; `first-match' doesn't get us out of the loop.
  3315. ((not fn-lbl) (push 'inline seen-refs) nil)
  3316. ;; Label not seen so far: add it so SEEN-REFS.
  3317. ;;
  3318. ;; Also search for subsequent references in
  3319. ;; footnote definition so numbering follows
  3320. ;; reading logic. Note that we don't have to care
  3321. ;; about inline definitions, since
  3322. ;; `org-element-map' already traverses them at the
  3323. ;; right time.
  3324. ;;
  3325. ;; Once again, return nil to stay in the loop.
  3326. ((not (member fn-lbl seen-refs))
  3327. (push fn-lbl seen-refs)
  3328. (funcall search-ref
  3329. (org-export-get-footnote-definition fn info))
  3330. nil))))
  3331. ;; Don't enter footnote definitions since it will
  3332. ;; happen when their first reference is found.
  3333. info 'first-match 'footnote-definition)))))
  3334. (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
  3335. ;;;; For Headlines
  3336. ;;
  3337. ;; `org-export-get-relative-level' is a shortcut to get headline
  3338. ;; level, relatively to the lower headline level in the parsed tree.
  3339. ;;
  3340. ;; `org-export-get-headline-number' returns the section number of an
  3341. ;; headline, while `org-export-number-to-roman' allows to convert it
  3342. ;; to roman numbers.
  3343. ;;
  3344. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  3345. ;; `org-export-last-sibling-p' are three useful predicates when it
  3346. ;; comes to fulfill the `:headline-levels' property.
  3347. ;;
  3348. ;; `org-export-get-tags', `org-export-get-category' and
  3349. ;; `org-export-get-node-property' extract useful information from an
  3350. ;; headline or a parent headline. They all handle inheritance.
  3351. ;;
  3352. ;; `org-export-get-alt-title' tries to retrieve an alternative title,
  3353. ;; as a secondary string, suitable for table of contents. It falls
  3354. ;; back onto default title.
  3355. (defun org-export-get-relative-level (headline info)
  3356. "Return HEADLINE relative level within current parsed tree.
  3357. INFO is a plist holding contextual information."
  3358. (+ (org-element-property :level headline)
  3359. (or (plist-get info :headline-offset) 0)))
  3360. (defun org-export-low-level-p (headline info)
  3361. "Non-nil when HEADLINE is considered as low level.
  3362. INFO is a plist used as a communication channel.
  3363. A low level headlines has a relative level greater than
  3364. `:headline-levels' property value.
  3365. Return value is the difference between HEADLINE relative level
  3366. and the last level being considered as high enough, or nil."
  3367. (let ((limit (plist-get info :headline-levels)))
  3368. (when (wholenump limit)
  3369. (let ((level (org-export-get-relative-level headline info)))
  3370. (and (> level limit) (- level limit))))))
  3371. (defun org-export-get-headline-number (headline info)
  3372. "Return HEADLINE numbering as a list of numbers.
  3373. INFO is a plist holding contextual information."
  3374. (cdr (assoc headline (plist-get info :headline-numbering))))
  3375. (defun org-export-numbered-headline-p (headline info)
  3376. "Return a non-nil value if HEADLINE element should be numbered.
  3377. INFO is a plist used as a communication channel."
  3378. (let ((sec-num (plist-get info :section-numbers))
  3379. (level (org-export-get-relative-level headline info)))
  3380. (if (wholenump sec-num) (<= level sec-num) sec-num)))
  3381. (defun org-export-number-to-roman (n)
  3382. "Convert integer N into a roman numeral."
  3383. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  3384. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  3385. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  3386. ( 1 . "I")))
  3387. (res ""))
  3388. (if (<= n 0)
  3389. (number-to-string n)
  3390. (while roman
  3391. (if (>= n (caar roman))
  3392. (setq n (- n (caar roman))
  3393. res (concat res (cdar roman)))
  3394. (pop roman)))
  3395. res)))
  3396. (defun org-export-get-tags (element info &optional tags inherited)
  3397. "Return list of tags associated to ELEMENT.
  3398. ELEMENT has either an `headline' or an `inlinetask' type. INFO
  3399. is a plist used as a communication channel.
  3400. Select tags (see `org-export-select-tags') and exclude tags (see
  3401. `org-export-exclude-tags') are removed from the list.
  3402. When non-nil, optional argument TAGS should be a list of strings.
  3403. Any tag belonging to this list will also be removed.
  3404. When optional argument INHERITED is non-nil, tags can also be
  3405. inherited from parent headlines and FILETAGS keywords."
  3406. (org-remove-if
  3407. (lambda (tag) (or (member tag (plist-get info :select-tags))
  3408. (member tag (plist-get info :exclude-tags))
  3409. (member tag tags)))
  3410. (if (not inherited) (org-element-property :tags element)
  3411. ;; Build complete list of inherited tags.
  3412. (let ((current-tag-list (org-element-property :tags element)))
  3413. (mapc
  3414. (lambda (parent)
  3415. (mapc
  3416. (lambda (tag)
  3417. (when (and (memq (org-element-type parent) '(headline inlinetask))
  3418. (not (member tag current-tag-list)))
  3419. (push tag current-tag-list)))
  3420. (org-element-property :tags parent)))
  3421. (org-export-get-genealogy element))
  3422. ;; Add FILETAGS keywords and return results.
  3423. (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
  3424. (defun org-export-get-node-property (property blob &optional inherited)
  3425. "Return node PROPERTY value for BLOB.
  3426. PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
  3427. element or object.
  3428. If optional argument INHERITED is non-nil, the value can be
  3429. inherited from a parent headline.
  3430. Return value is a string or nil."
  3431. (let ((headline (if (eq (org-element-type blob) 'headline) blob
  3432. (org-export-get-parent-headline blob))))
  3433. (if (not inherited) (org-element-property property blob)
  3434. (let ((parent headline) value)
  3435. (catch 'found
  3436. (while parent
  3437. (when (plist-member (nth 1 parent) property)
  3438. (throw 'found (org-element-property property parent)))
  3439. (setq parent (org-element-property :parent parent))))))))
  3440. (defun org-export-get-category (blob info)
  3441. "Return category for element or object BLOB.
  3442. INFO is a plist used as a communication channel.
  3443. CATEGORY is automatically inherited from a parent headline, from
  3444. #+CATEGORY: keyword or created out of original file name. If all
  3445. fail, the fall-back value is \"???\"."
  3446. (or (let ((headline (if (eq (org-element-type blob) 'headline) blob
  3447. (org-export-get-parent-headline blob))))
  3448. ;; Almost like `org-export-node-property', but we cannot trust
  3449. ;; `plist-member' as every headline has a `:CATEGORY'
  3450. ;; property, would it be nil or equal to "???" (which has the
  3451. ;; same meaning).
  3452. (let ((parent headline) value)
  3453. (catch 'found
  3454. (while parent
  3455. (let ((category (org-element-property :CATEGORY parent)))
  3456. (and category (not (equal "???" category))
  3457. (throw 'found category)))
  3458. (setq parent (org-element-property :parent parent))))))
  3459. (org-element-map (plist-get info :parse-tree) 'keyword
  3460. (lambda (kwd)
  3461. (when (equal (org-element-property :key kwd) "CATEGORY")
  3462. (org-element-property :value kwd)))
  3463. info 'first-match)
  3464. (let ((file (plist-get info :input-file)))
  3465. (and file (file-name-sans-extension (file-name-nondirectory file))))
  3466. "???"))
  3467. (defun org-export-get-alt-title (headline info)
  3468. "Return alternative title for HEADLINE, as a secondary string.
  3469. INFO is a plist used as a communication channel. If no optional
  3470. title is defined, fall-back to the regular title."
  3471. (or (org-element-property :alt-title headline)
  3472. (org-element-property :title headline)))
  3473. (defun org-export-first-sibling-p (headline info)
  3474. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  3475. INFO is a plist used as a communication channel."
  3476. (not (eq (org-element-type (org-export-get-previous-element headline info))
  3477. 'headline)))
  3478. (defun org-export-last-sibling-p (headline info)
  3479. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  3480. INFO is a plist used as a communication channel."
  3481. (not (org-export-get-next-element headline info)))
  3482. ;;;; For Keywords
  3483. ;;
  3484. ;; `org-export-get-date' returns a date appropriate for the document
  3485. ;; to about to be exported. In particular, it takes care of
  3486. ;; `org-export-date-timestamp-format'.
  3487. (defun org-export-get-date (info &optional fmt)
  3488. "Return date value for the current document.
  3489. INFO is a plist used as a communication channel. FMT, when
  3490. non-nil, is a time format string that will be applied on the date
  3491. if it consists in a single timestamp object. It defaults to
  3492. `org-export-date-timestamp-format' when nil.
  3493. A proper date can be a secondary string, a string or nil. It is
  3494. meant to be translated with `org-export-data' or alike."
  3495. (let ((date (plist-get info :date))
  3496. (fmt (or fmt org-export-date-timestamp-format)))
  3497. (cond ((not date) nil)
  3498. ((and fmt
  3499. (not (cdr date))
  3500. (eq (org-element-type (car date)) 'timestamp))
  3501. (org-timestamp-format (car date) fmt))
  3502. (t date))))
  3503. ;;;; For Links
  3504. ;;
  3505. ;; `org-export-solidify-link-text' turns a string into a safer version
  3506. ;; for links, replacing most non-standard characters with hyphens.
  3507. ;;
  3508. ;; `org-export-get-coderef-format' returns an appropriate format
  3509. ;; string for coderefs.
  3510. ;;
  3511. ;; `org-export-inline-image-p' returns a non-nil value when the link
  3512. ;; provided should be considered as an inline image.
  3513. ;;
  3514. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  3515. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  3516. ;; returns an appropriate unique identifier when found, or nil.
  3517. ;;
  3518. ;; `org-export-resolve-id-link' returns the first headline with
  3519. ;; specified id or custom-id in parse tree, the path to the external
  3520. ;; file with the id or nil when neither was found.
  3521. ;;
  3522. ;; `org-export-resolve-coderef' associates a reference to a line
  3523. ;; number in the element it belongs, or returns the reference itself
  3524. ;; when the element isn't numbered.
  3525. (defun org-export-solidify-link-text (s)
  3526. "Take link text S and make a safe target out of it."
  3527. (save-match-data
  3528. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-:]+") "-")))
  3529. (defun org-export-get-coderef-format (path desc)
  3530. "Return format string for code reference link.
  3531. PATH is the link path. DESC is its description."
  3532. (save-match-data
  3533. (cond ((not desc) "%s")
  3534. ((string-match (regexp-quote (concat "(" path ")")) desc)
  3535. (replace-match "%s" t t desc))
  3536. (t desc))))
  3537. (defun org-export-inline-image-p (link &optional rules)
  3538. "Non-nil if LINK object points to an inline image.
  3539. Optional argument is a set of RULES defining inline images. It
  3540. is an alist where associations have the following shape:
  3541. \(TYPE . REGEXP)
  3542. Applying a rule means apply REGEXP against LINK's path when its
  3543. type is TYPE. The function will return a non-nil value if any of
  3544. the provided rules is non-nil. The default rule is
  3545. `org-export-default-inline-image-rule'.
  3546. This only applies to links without a description."
  3547. (and (not (org-element-contents link))
  3548. (let ((case-fold-search t)
  3549. (rules (or rules org-export-default-inline-image-rule)))
  3550. (catch 'exit
  3551. (mapc
  3552. (lambda (rule)
  3553. (and (string= (org-element-property :type link) (car rule))
  3554. (string-match (cdr rule)
  3555. (org-element-property :path link))
  3556. (throw 'exit t)))
  3557. rules)
  3558. ;; Return nil if no rule matched.
  3559. nil))))
  3560. (defun org-export-resolve-coderef (ref info)
  3561. "Resolve a code reference REF.
  3562. INFO is a plist used as a communication channel.
  3563. Return associated line number in source code, or REF itself,
  3564. depending on src-block or example element's switches."
  3565. (org-element-map (plist-get info :parse-tree) '(example-block src-block)
  3566. (lambda (el)
  3567. (with-temp-buffer
  3568. (insert (org-trim (org-element-property :value el)))
  3569. (let* ((label-fmt (regexp-quote
  3570. (or (org-element-property :label-fmt el)
  3571. org-coderef-label-format)))
  3572. (ref-re
  3573. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  3574. (replace-regexp-in-string "%s" ref label-fmt nil t))))
  3575. ;; Element containing REF is found. Resolve it to either
  3576. ;; a label or a line number, as needed.
  3577. (when (re-search-backward ref-re nil t)
  3578. (cond
  3579. ((org-element-property :use-labels el) ref)
  3580. ((eq (org-element-property :number-lines el) 'continued)
  3581. (+ (org-export-get-loc el info) (line-number-at-pos)))
  3582. (t (line-number-at-pos)))))))
  3583. info 'first-match))
  3584. (defun org-export-resolve-fuzzy-link (link info)
  3585. "Return LINK destination.
  3586. INFO is a plist holding contextual information.
  3587. Return value can be an object, an element, or nil:
  3588. - If LINK path matches a target object (i.e. <<path>>) return it.
  3589. - If LINK path exactly matches the name affiliated keyword
  3590. \(i.e. #+NAME: path) of an element, return that element.
  3591. - If LINK path exactly matches any headline name, return that
  3592. element. If more than one headline share that name, priority
  3593. will be given to the one with the closest common ancestor, if
  3594. any, or the first one in the parse tree otherwise.
  3595. - Otherwise, return nil.
  3596. Assume LINK type is \"fuzzy\". White spaces are not
  3597. significant."
  3598. (let* ((raw-path (org-element-property :path link))
  3599. (match-title-p (eq (aref raw-path 0) ?*))
  3600. ;; Split PATH at white spaces so matches are space
  3601. ;; insensitive.
  3602. (path (org-split-string
  3603. (if match-title-p (substring raw-path 1) raw-path)))
  3604. ;; Cache for destinations that are not position dependent.
  3605. (link-cache
  3606. (or (plist-get info :resolve-fuzzy-link-cache)
  3607. (plist-get (setq info (plist-put info :resolve-fuzzy-link-cache
  3608. (make-hash-table :test 'equal)))
  3609. :resolve-fuzzy-link-cache)))
  3610. (cached (gethash path link-cache 'not-found)))
  3611. (cond
  3612. ;; Destination is not position dependent: use cached value.
  3613. ((and (not match-title-p) (not (eq cached 'not-found))) cached)
  3614. ;; First try to find a matching "<<path>>" unless user specified
  3615. ;; he was looking for a headline (path starts with a "*"
  3616. ;; character).
  3617. ((and (not match-title-p)
  3618. (let ((match (org-element-map (plist-get info :parse-tree) 'target
  3619. (lambda (blob)
  3620. (and (equal (org-split-string
  3621. (org-element-property :value blob))
  3622. path)
  3623. blob))
  3624. info 'first-match)))
  3625. (and match (puthash path match link-cache)))))
  3626. ;; Then try to find an element with a matching "#+NAME: path"
  3627. ;; affiliated keyword.
  3628. ((and (not match-title-p)
  3629. (let ((match (org-element-map (plist-get info :parse-tree)
  3630. org-element-all-elements
  3631. (lambda (el)
  3632. (let ((name (org-element-property :name el)))
  3633. (when (and name
  3634. (equal (org-split-string name) path))
  3635. el)))
  3636. info 'first-match)))
  3637. (and match (puthash path match link-cache)))))
  3638. ;; Last case: link either points to a headline or to nothingness.
  3639. ;; Try to find the source, with priority given to headlines with
  3640. ;; the closest common ancestor. If such candidate is found,
  3641. ;; return it, otherwise return nil.
  3642. (t
  3643. (let ((find-headline
  3644. (function
  3645. ;; Return first headline whose `:raw-value' property is
  3646. ;; NAME in parse tree DATA, or nil. Statistics cookies
  3647. ;; are ignored.
  3648. (lambda (name data)
  3649. (org-element-map data 'headline
  3650. (lambda (headline)
  3651. (when (equal (org-split-string
  3652. (replace-regexp-in-string
  3653. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  3654. (org-element-property :raw-value headline)))
  3655. name)
  3656. headline))
  3657. info 'first-match)))))
  3658. ;; Search among headlines sharing an ancestor with link, from
  3659. ;; closest to farthest.
  3660. (catch 'exit
  3661. (mapc
  3662. (lambda (parent)
  3663. (let ((foundp (funcall find-headline path parent)))
  3664. (when foundp (throw 'exit foundp))))
  3665. (let ((parent-hl (org-export-get-parent-headline link)))
  3666. (if (not parent-hl) (list (plist-get info :parse-tree))
  3667. (cons parent-hl (org-export-get-genealogy parent-hl)))))
  3668. ;; No destination found: return nil.
  3669. (and (not match-title-p) (puthash path nil link-cache))))))))
  3670. (defun org-export-resolve-id-link (link info)
  3671. "Return headline referenced as LINK destination.
  3672. INFO is a plist used as a communication channel.
  3673. Return value can be the headline element matched in current parse
  3674. tree, a file name or nil. Assume LINK type is either \"id\" or
  3675. \"custom-id\"."
  3676. (let ((id (org-element-property :path link)))
  3677. ;; First check if id is within the current parse tree.
  3678. (or (org-element-map (plist-get info :parse-tree) 'headline
  3679. (lambda (headline)
  3680. (when (or (string= (org-element-property :ID headline) id)
  3681. (string= (org-element-property :CUSTOM_ID headline) id))
  3682. headline))
  3683. info 'first-match)
  3684. ;; Otherwise, look for external files.
  3685. (cdr (assoc id (plist-get info :id-alist))))))
  3686. (defun org-export-resolve-radio-link (link info)
  3687. "Return radio-target object referenced as LINK destination.
  3688. INFO is a plist used as a communication channel.
  3689. Return value can be a radio-target object or nil. Assume LINK
  3690. has type \"radio\"."
  3691. (let ((path (replace-regexp-in-string
  3692. "[ \r\t\n]+" " " (org-element-property :path link))))
  3693. (org-element-map (plist-get info :parse-tree) 'radio-target
  3694. (lambda (radio)
  3695. (and (eq (compare-strings
  3696. (replace-regexp-in-string
  3697. "[ \r\t\n]+" " " (org-element-property :value radio))
  3698. nil nil path nil nil t)
  3699. t)
  3700. radio))
  3701. info 'first-match)))
  3702. ;;;; For References
  3703. ;;
  3704. ;; `org-export-get-ordinal' associates a sequence number to any object
  3705. ;; or element.
  3706. (defun org-export-get-ordinal (element info &optional types predicate)
  3707. "Return ordinal number of an element or object.
  3708. ELEMENT is the element or object considered. INFO is the plist
  3709. used as a communication channel.
  3710. Optional argument TYPES, when non-nil, is a list of element or
  3711. object types, as symbols, that should also be counted in.
  3712. Otherwise, only provided element's type is considered.
  3713. Optional argument PREDICATE is a function returning a non-nil
  3714. value if the current element or object should be counted in. It
  3715. accepts two arguments: the element or object being considered and
  3716. the plist used as a communication channel. This allows to count
  3717. only a certain type of objects (i.e. inline images).
  3718. Return value is a list of numbers if ELEMENT is a headline or an
  3719. item. It is nil for keywords. It represents the footnote number
  3720. for footnote definitions and footnote references. If ELEMENT is
  3721. a target, return the same value as if ELEMENT was the closest
  3722. table, item or headline containing the target. In any other
  3723. case, return the sequence number of ELEMENT among elements or
  3724. objects of the same type."
  3725. ;; Ordinal of a target object refer to the ordinal of the closest
  3726. ;; table, item, or headline containing the object.
  3727. (when (eq (org-element-type element) 'target)
  3728. (setq element
  3729. (loop for parent in (org-export-get-genealogy element)
  3730. when
  3731. (memq
  3732. (org-element-type parent)
  3733. '(footnote-definition footnote-reference headline item
  3734. table))
  3735. return parent)))
  3736. (case (org-element-type element)
  3737. ;; Special case 1: A headline returns its number as a list.
  3738. (headline (org-export-get-headline-number element info))
  3739. ;; Special case 2: An item returns its number as a list.
  3740. (item (let ((struct (org-element-property :structure element)))
  3741. (org-list-get-item-number
  3742. (org-element-property :begin element)
  3743. struct
  3744. (org-list-prevs-alist struct)
  3745. (org-list-parents-alist struct))))
  3746. ((footnote-definition footnote-reference)
  3747. (org-export-get-footnote-number element info))
  3748. (otherwise
  3749. (let ((counter 0))
  3750. ;; Increment counter until ELEMENT is found again.
  3751. (org-element-map (plist-get info :parse-tree)
  3752. (or types (org-element-type element))
  3753. (lambda (el)
  3754. (cond
  3755. ((eq element el) (1+ counter))
  3756. ((not predicate) (incf counter) nil)
  3757. ((funcall predicate el info) (incf counter) nil)))
  3758. info 'first-match)))))
  3759. ;;;; For Src-Blocks
  3760. ;;
  3761. ;; `org-export-get-loc' counts number of code lines accumulated in
  3762. ;; src-block or example-block elements with a "+n" switch until
  3763. ;; a given element, excluded. Note: "-n" switches reset that count.
  3764. ;;
  3765. ;; `org-export-unravel-code' extracts source code (along with a code
  3766. ;; references alist) from an `element-block' or `src-block' type
  3767. ;; element.
  3768. ;;
  3769. ;; `org-export-format-code' applies a formatting function to each line
  3770. ;; of code, providing relative line number and code reference when
  3771. ;; appropriate. Since it doesn't access the original element from
  3772. ;; which the source code is coming, it expects from the code calling
  3773. ;; it to know if lines should be numbered and if code references
  3774. ;; should appear.
  3775. ;;
  3776. ;; Eventually, `org-export-format-code-default' is a higher-level
  3777. ;; function (it makes use of the two previous functions) which handles
  3778. ;; line numbering and code references inclusion, and returns source
  3779. ;; code in a format suitable for plain text or verbatim output.
  3780. (defun org-export-get-loc (element info)
  3781. "Return accumulated lines of code up to ELEMENT.
  3782. INFO is the plist used as a communication channel.
  3783. ELEMENT is excluded from count."
  3784. (let ((loc 0))
  3785. (org-element-map (plist-get info :parse-tree)
  3786. `(src-block example-block ,(org-element-type element))
  3787. (lambda (el)
  3788. (cond
  3789. ;; ELEMENT is reached: Quit the loop.
  3790. ((eq el element))
  3791. ;; Only count lines from src-block and example-block elements
  3792. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  3793. ((not (memq (org-element-type el) '(src-block example-block))) nil)
  3794. ((let ((linums (org-element-property :number-lines el)))
  3795. (when linums
  3796. ;; Accumulate locs or reset them.
  3797. (let ((lines (org-count-lines
  3798. (org-trim (org-element-property :value el)))))
  3799. (setq loc (if (eq linums 'new) lines (+ loc lines))))))
  3800. ;; Return nil to stay in the loop.
  3801. nil)))
  3802. info 'first-match)
  3803. ;; Return value.
  3804. loc))
  3805. (defun org-export-unravel-code (element)
  3806. "Clean source code and extract references out of it.
  3807. ELEMENT has either a `src-block' an `example-block' type.
  3808. Return a cons cell whose CAR is the source code, cleaned from any
  3809. reference, protective commas and spurious indentation, and CDR is
  3810. an alist between relative line number (integer) and name of code
  3811. reference on that line (string)."
  3812. (let* ((line 0) refs
  3813. (value (org-element-property :value element))
  3814. ;; Get code and clean it. Remove blank lines at its
  3815. ;; beginning and end.
  3816. (code (replace-regexp-in-string
  3817. "\\`\\([ \t]*\n\\)+" ""
  3818. (replace-regexp-in-string
  3819. "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
  3820. (if (or org-src-preserve-indentation
  3821. (org-element-property :preserve-indent element))
  3822. value
  3823. (org-element-remove-indentation value)))))
  3824. ;; Get format used for references.
  3825. (label-fmt (regexp-quote
  3826. (or (org-element-property :label-fmt element)
  3827. org-coderef-label-format)))
  3828. ;; Build a regexp matching a loc with a reference.
  3829. (with-ref-re
  3830. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
  3831. (replace-regexp-in-string
  3832. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  3833. ;; Return value.
  3834. (cons
  3835. ;; Code with references removed.
  3836. (org-element-normalize-string
  3837. (mapconcat
  3838. (lambda (loc)
  3839. (incf line)
  3840. (if (not (string-match with-ref-re loc)) loc
  3841. ;; Ref line: remove ref, and signal its position in REFS.
  3842. (push (cons line (match-string 3 loc)) refs)
  3843. (replace-match "" nil nil loc 1)))
  3844. (org-split-string code "\n") "\n"))
  3845. ;; Reference alist.
  3846. refs)))
  3847. (defun org-export-format-code (code fun &optional num-lines ref-alist)
  3848. "Format CODE by applying FUN line-wise and return it.
  3849. CODE is a string representing the code to format. FUN is
  3850. a function. It must accept three arguments: a line of
  3851. code (string), the current line number (integer) or nil and the
  3852. reference associated to the current line (string) or nil.
  3853. Optional argument NUM-LINES can be an integer representing the
  3854. number of code lines accumulated until the current code. Line
  3855. numbers passed to FUN will take it into account. If it is nil,
  3856. FUN's second argument will always be nil. This number can be
  3857. obtained with `org-export-get-loc' function.
  3858. Optional argument REF-ALIST can be an alist between relative line
  3859. number (i.e. ignoring NUM-LINES) and the name of the code
  3860. reference on it. If it is nil, FUN's third argument will always
  3861. be nil. It can be obtained through the use of
  3862. `org-export-unravel-code' function."
  3863. (let ((--locs (org-split-string code "\n"))
  3864. (--line 0))
  3865. (org-element-normalize-string
  3866. (mapconcat
  3867. (lambda (--loc)
  3868. (incf --line)
  3869. (let ((--ref (cdr (assq --line ref-alist))))
  3870. (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
  3871. --locs "\n"))))
  3872. (defun org-export-format-code-default (element info)
  3873. "Return source code from ELEMENT, formatted in a standard way.
  3874. ELEMENT is either a `src-block' or `example-block' element. INFO
  3875. is a plist used as a communication channel.
  3876. This function takes care of line numbering and code references
  3877. inclusion. Line numbers, when applicable, appear at the
  3878. beginning of the line, separated from the code by two white
  3879. spaces. Code references, on the other hand, appear flushed to
  3880. the right, separated by six white spaces from the widest line of
  3881. code."
  3882. ;; Extract code and references.
  3883. (let* ((code-info (org-export-unravel-code element))
  3884. (code (car code-info))
  3885. (code-lines (org-split-string code "\n")))
  3886. (if (null code-lines) ""
  3887. (let* ((refs (and (org-element-property :retain-labels element)
  3888. (cdr code-info)))
  3889. ;; Handle line numbering.
  3890. (num-start (case (org-element-property :number-lines element)
  3891. (continued (org-export-get-loc element info))
  3892. (new 0)))
  3893. (num-fmt
  3894. (and num-start
  3895. (format "%%%ds "
  3896. (length (number-to-string
  3897. (+ (length code-lines) num-start))))))
  3898. ;; Prepare references display, if required. Any reference
  3899. ;; should start six columns after the widest line of code,
  3900. ;; wrapped with parenthesis.
  3901. (max-width
  3902. (+ (apply 'max (mapcar 'length code-lines))
  3903. (if (not num-start) 0 (length (format num-fmt num-start))))))
  3904. (org-export-format-code
  3905. code
  3906. (lambda (loc line-num ref)
  3907. (let ((number-str (and num-fmt (format num-fmt line-num))))
  3908. (concat
  3909. number-str
  3910. loc
  3911. (and ref
  3912. (concat (make-string
  3913. (- (+ 6 max-width)
  3914. (+ (length loc) (length number-str))) ? )
  3915. (format "(%s)" ref))))))
  3916. num-start refs)))))
  3917. ;;;; For Tables
  3918. ;;
  3919. ;; `org-export-table-has-special-column-p' and and
  3920. ;; `org-export-table-row-is-special-p' are predicates used to look for
  3921. ;; meta-information about the table structure.
  3922. ;;
  3923. ;; `org-table-has-header-p' tells when the rows before the first rule
  3924. ;; should be considered as table's header.
  3925. ;;
  3926. ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
  3927. ;; and `org-export-table-cell-borders' extract information from
  3928. ;; a table-cell element.
  3929. ;;
  3930. ;; `org-export-table-dimensions' gives the number on rows and columns
  3931. ;; in the table, ignoring horizontal rules and special columns.
  3932. ;; `org-export-table-cell-address', given a table-cell object, returns
  3933. ;; the absolute address of a cell. On the other hand,
  3934. ;; `org-export-get-table-cell-at' does the contrary.
  3935. ;;
  3936. ;; `org-export-table-cell-starts-colgroup-p',
  3937. ;; `org-export-table-cell-ends-colgroup-p',
  3938. ;; `org-export-table-row-starts-rowgroup-p',
  3939. ;; `org-export-table-row-ends-rowgroup-p',
  3940. ;; `org-export-table-row-starts-header-p' and
  3941. ;; `org-export-table-row-ends-header-p' indicate position of current
  3942. ;; row or cell within the table.
  3943. (defun org-export-table-has-special-column-p (table)
  3944. "Non-nil when TABLE has a special column.
  3945. All special columns will be ignored during export."
  3946. ;; The table has a special column when every first cell of every row
  3947. ;; has an empty value or contains a symbol among "/", "#", "!", "$",
  3948. ;; "*" "_" and "^". Though, do not consider a first row containing
  3949. ;; only empty cells as special.
  3950. (let ((special-column-p 'empty))
  3951. (catch 'exit
  3952. (mapc
  3953. (lambda (row)
  3954. (when (eq (org-element-property :type row) 'standard)
  3955. (let ((value (org-element-contents
  3956. (car (org-element-contents row)))))
  3957. (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
  3958. (setq special-column-p 'special))
  3959. ((not value))
  3960. (t (throw 'exit nil))))))
  3961. (org-element-contents table))
  3962. (eq special-column-p 'special))))
  3963. (defun org-export-table-has-header-p (table info)
  3964. "Non-nil when TABLE has a header.
  3965. INFO is a plist used as a communication channel.
  3966. A table has a header when it contains at least two row groups."
  3967. (let ((cache (or (plist-get info :table-header-cache)
  3968. (plist-get (setq info
  3969. (plist-put info :table-header-cache
  3970. (make-hash-table :test 'eq)))
  3971. :table-header-cache))))
  3972. (or (gethash table cache)
  3973. (let ((rowgroup 1) row-flag)
  3974. (puthash
  3975. table
  3976. (org-element-map table 'table-row
  3977. (lambda (row)
  3978. (cond
  3979. ((> rowgroup 1) t)
  3980. ((and row-flag (eq (org-element-property :type row) 'rule))
  3981. (incf rowgroup) (setq row-flag nil))
  3982. ((and (not row-flag) (eq (org-element-property :type row)
  3983. 'standard))
  3984. (setq row-flag t) nil)))
  3985. info 'first-match)
  3986. cache)))))
  3987. (defun org-export-table-row-is-special-p (table-row info)
  3988. "Non-nil if TABLE-ROW is considered special.
  3989. INFO is a plist used as the communication channel.
  3990. All special rows will be ignored during export."
  3991. (when (eq (org-element-property :type table-row) 'standard)
  3992. (let ((first-cell (org-element-contents
  3993. (car (org-element-contents table-row)))))
  3994. ;; A row is special either when...
  3995. (or
  3996. ;; ... it starts with a field only containing "/",
  3997. (equal first-cell '("/"))
  3998. ;; ... the table contains a special column and the row start
  3999. ;; with a marking character among, "^", "_", "$" or "!",
  4000. (and (org-export-table-has-special-column-p
  4001. (org-export-get-parent table-row))
  4002. (member first-cell '(("^") ("_") ("$") ("!"))))
  4003. ;; ... it contains only alignment cookies and empty cells.
  4004. (let ((special-row-p 'empty))
  4005. (catch 'exit
  4006. (mapc
  4007. (lambda (cell)
  4008. (let ((value (org-element-contents cell)))
  4009. ;; Since VALUE is a secondary string, the following
  4010. ;; checks avoid expanding it with `org-export-data'.
  4011. (cond ((not value))
  4012. ((and (not (cdr value))
  4013. (stringp (car value))
  4014. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
  4015. (car value)))
  4016. (setq special-row-p 'cookie))
  4017. (t (throw 'exit nil)))))
  4018. (org-element-contents table-row))
  4019. (eq special-row-p 'cookie)))))))
  4020. (defun org-export-table-row-group (table-row info)
  4021. "Return TABLE-ROW's group number, as an integer.
  4022. INFO is a plist used as the communication channel.
  4023. Return value is the group number, as an integer, or nil for
  4024. special rows and rows separators. First group is also table's
  4025. header."
  4026. (let ((cache (or (plist-get info :table-row-group-cache)
  4027. (plist-get (setq info
  4028. (plist-put info :table-row-group-cache
  4029. (make-hash-table :test 'eq)))
  4030. :table-row-group-cache))))
  4031. (cond ((gethash table-row cache))
  4032. ((eq (org-element-property :type table-row) 'rule) nil)
  4033. (t (let ((group 0) row-flag)
  4034. (org-element-map (org-export-get-parent table-row) 'table-row
  4035. (lambda (row)
  4036. (if (eq (org-element-property :type row) 'rule)
  4037. (setq row-flag nil)
  4038. (unless row-flag (incf group) (setq row-flag t)))
  4039. (when (eq table-row row) (puthash table-row group cache)))
  4040. info 'first-match))))))
  4041. (defun org-export-table-cell-width (table-cell info)
  4042. "Return TABLE-CELL contents width.
  4043. INFO is a plist used as the communication channel.
  4044. Return value is the width given by the last width cookie in the
  4045. same column as TABLE-CELL, or nil."
  4046. (let* ((row (org-export-get-parent table-cell))
  4047. (table (org-export-get-parent row))
  4048. (cells (org-element-contents row))
  4049. (columns (length cells))
  4050. (column (- columns (length (memq table-cell cells))))
  4051. (cache (or (plist-get info :table-cell-width-cache)
  4052. (plist-get (setq info
  4053. (plist-put info :table-cell-width-cache
  4054. (make-hash-table :test 'eq)))
  4055. :table-cell-width-cache)))
  4056. (width-vector (or (gethash table cache)
  4057. (puthash table (make-vector columns 'empty) cache)))
  4058. (value (aref width-vector column)))
  4059. (if (not (eq value 'empty)) value
  4060. (let (cookie-width)
  4061. (dolist (row (org-element-contents table)
  4062. (aset width-vector column cookie-width))
  4063. (when (org-export-table-row-is-special-p row info)
  4064. ;; In a special row, try to find a width cookie at COLUMN.
  4065. (let* ((value (org-element-contents
  4066. (elt (org-element-contents row) column)))
  4067. (cookie (car value)))
  4068. ;; The following checks avoid expanding unnecessarily
  4069. ;; the cell with `org-export-data'.
  4070. (when (and value
  4071. (not (cdr value))
  4072. (stringp cookie)
  4073. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie)
  4074. (match-string 1 cookie))
  4075. (setq cookie-width
  4076. (string-to-number (match-string 1 cookie)))))))))))
  4077. (defun org-export-table-cell-alignment (table-cell info)
  4078. "Return TABLE-CELL contents alignment.
  4079. INFO is a plist used as the communication channel.
  4080. Return alignment as specified by the last alignment cookie in the
  4081. same column as TABLE-CELL. If no such cookie is found, a default
  4082. alignment value will be deduced from fraction of numbers in the
  4083. column (see `org-table-number-fraction' for more information).
  4084. Possible values are `left', `right' and `center'."
  4085. ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
  4086. (require 'org-table)
  4087. (let* ((row (org-export-get-parent table-cell))
  4088. (table (org-export-get-parent row))
  4089. (cells (org-element-contents row))
  4090. (columns (length cells))
  4091. (column (- columns (length (memq table-cell cells))))
  4092. (cache (or (plist-get info :table-cell-alignment-cache)
  4093. (plist-get (setq info
  4094. (plist-put info :table-cell-alignment-cache
  4095. (make-hash-table :test 'eq)))
  4096. :table-cell-alignment-cache)))
  4097. (align-vector (or (gethash table cache)
  4098. (puthash table (make-vector columns nil) cache))))
  4099. (or (aref align-vector column)
  4100. (let ((number-cells 0)
  4101. (total-cells 0)
  4102. cookie-align
  4103. previous-cell-number-p)
  4104. (dolist (row (org-element-contents (org-export-get-parent row)))
  4105. (cond
  4106. ;; In a special row, try to find an alignment cookie at
  4107. ;; COLUMN.
  4108. ((org-export-table-row-is-special-p row info)
  4109. (let ((value (org-element-contents
  4110. (elt (org-element-contents row) column))))
  4111. ;; Since VALUE is a secondary string, the following
  4112. ;; checks avoid useless expansion through
  4113. ;; `org-export-data'.
  4114. (when (and value
  4115. (not (cdr value))
  4116. (stringp (car value))
  4117. (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
  4118. (car value))
  4119. (match-string 1 (car value)))
  4120. (setq cookie-align (match-string 1 (car value))))))
  4121. ;; Ignore table rules.
  4122. ((eq (org-element-property :type row) 'rule))
  4123. ;; In a standard row, check if cell's contents are
  4124. ;; expressing some kind of number. Increase NUMBER-CELLS
  4125. ;; accordingly. Though, don't bother if an alignment
  4126. ;; cookie has already defined cell's alignment.
  4127. ((not cookie-align)
  4128. (let ((value (org-export-data
  4129. (org-element-contents
  4130. (elt (org-element-contents row) column))
  4131. info)))
  4132. (incf total-cells)
  4133. ;; Treat an empty cell as a number if it follows
  4134. ;; a number.
  4135. (if (not (or (string-match org-table-number-regexp value)
  4136. (and (string= value "") previous-cell-number-p)))
  4137. (setq previous-cell-number-p nil)
  4138. (setq previous-cell-number-p t)
  4139. (incf number-cells))))))
  4140. ;; Return value. Alignment specified by cookies has
  4141. ;; precedence over alignment deduced from cell's contents.
  4142. (aset align-vector
  4143. column
  4144. (cond ((equal cookie-align "l") 'left)
  4145. ((equal cookie-align "r") 'right)
  4146. ((equal cookie-align "c") 'center)
  4147. ((>= (/ (float number-cells) total-cells)
  4148. org-table-number-fraction)
  4149. 'right)
  4150. (t 'left)))))))
  4151. (defun org-export-table-cell-borders (table-cell info)
  4152. "Return TABLE-CELL borders.
  4153. INFO is a plist used as a communication channel.
  4154. Return value is a list of symbols, or nil. Possible values are:
  4155. `top', `bottom', `above', `below', `left' and `right'. Note:
  4156. `top' (resp. `bottom') only happen for a cell in the first
  4157. row (resp. last row) of the table, ignoring table rules, if any.
  4158. Returned borders ignore special rows."
  4159. (let* ((row (org-export-get-parent table-cell))
  4160. (table (org-export-get-parent-table table-cell))
  4161. borders)
  4162. ;; Top/above border? TABLE-CELL has a border above when a rule
  4163. ;; used to demarcate row groups can be found above. Hence,
  4164. ;; finding a rule isn't sufficient to push `above' in BORDERS:
  4165. ;; another regular row has to be found above that rule.
  4166. (let (rule-flag)
  4167. (catch 'exit
  4168. (mapc (lambda (row)
  4169. (cond ((eq (org-element-property :type row) 'rule)
  4170. (setq rule-flag t))
  4171. ((not (org-export-table-row-is-special-p row info))
  4172. (if rule-flag (throw 'exit (push 'above borders))
  4173. (throw 'exit nil)))))
  4174. ;; Look at every row before the current one.
  4175. (cdr (memq row (reverse (org-element-contents table)))))
  4176. ;; No rule above, or rule found starts the table (ignoring any
  4177. ;; special row): TABLE-CELL is at the top of the table.
  4178. (when rule-flag (push 'above borders))
  4179. (push 'top borders)))
  4180. ;; Bottom/below border? TABLE-CELL has a border below when next
  4181. ;; non-regular row below is a rule.
  4182. (let (rule-flag)
  4183. (catch 'exit
  4184. (mapc (lambda (row)
  4185. (cond ((eq (org-element-property :type row) 'rule)
  4186. (setq rule-flag t))
  4187. ((not (org-export-table-row-is-special-p row info))
  4188. (if rule-flag (throw 'exit (push 'below borders))
  4189. (throw 'exit nil)))))
  4190. ;; Look at every row after the current one.
  4191. (cdr (memq row (org-element-contents table))))
  4192. ;; No rule below, or rule found ends the table (modulo some
  4193. ;; special row): TABLE-CELL is at the bottom of the table.
  4194. (when rule-flag (push 'below borders))
  4195. (push 'bottom borders)))
  4196. ;; Right/left borders? They can only be specified by column
  4197. ;; groups. Column groups are defined in a row starting with "/".
  4198. ;; Also a column groups row only contains "<", "<>", ">" or blank
  4199. ;; cells.
  4200. (catch 'exit
  4201. (let ((column (let ((cells (org-element-contents row)))
  4202. (- (length cells) (length (memq table-cell cells))))))
  4203. (mapc
  4204. (lambda (row)
  4205. (unless (eq (org-element-property :type row) 'rule)
  4206. (when (equal (org-element-contents
  4207. (car (org-element-contents row)))
  4208. '("/"))
  4209. (let ((column-groups
  4210. (mapcar
  4211. (lambda (cell)
  4212. (let ((value (org-element-contents cell)))
  4213. (when (member value '(("<") ("<>") (">") nil))
  4214. (car value))))
  4215. (org-element-contents row))))
  4216. ;; There's a left border when previous cell, if
  4217. ;; any, ends a group, or current one starts one.
  4218. (when (or (and (not (zerop column))
  4219. (member (elt column-groups (1- column))
  4220. '(">" "<>")))
  4221. (member (elt column-groups column) '("<" "<>")))
  4222. (push 'left borders))
  4223. ;; There's a right border when next cell, if any,
  4224. ;; starts a group, or current one ends one.
  4225. (when (or (and (/= (1+ column) (length column-groups))
  4226. (member (elt column-groups (1+ column))
  4227. '("<" "<>")))
  4228. (member (elt column-groups column) '(">" "<>")))
  4229. (push 'right borders))
  4230. (throw 'exit nil)))))
  4231. ;; Table rows are read in reverse order so last column groups
  4232. ;; row has precedence over any previous one.
  4233. (reverse (org-element-contents table)))))
  4234. ;; Return value.
  4235. borders))
  4236. (defun org-export-table-cell-starts-colgroup-p (table-cell info)
  4237. "Non-nil when TABLE-CELL is at the beginning of a column group.
  4238. INFO is a plist used as a communication channel."
  4239. ;; A cell starts a column group either when it is at the beginning
  4240. ;; of a row (or after the special column, if any) or when it has
  4241. ;; a left border.
  4242. (or (eq (org-element-map (org-export-get-parent table-cell) 'table-cell
  4243. 'identity info 'first-match)
  4244. table-cell)
  4245. (memq 'left (org-export-table-cell-borders table-cell info))))
  4246. (defun org-export-table-cell-ends-colgroup-p (table-cell info)
  4247. "Non-nil when TABLE-CELL is at the end of a column group.
  4248. INFO is a plist used as a communication channel."
  4249. ;; A cell ends a column group either when it is at the end of a row
  4250. ;; or when it has a right border.
  4251. (or (eq (car (last (org-element-contents
  4252. (org-export-get-parent table-cell))))
  4253. table-cell)
  4254. (memq 'right (org-export-table-cell-borders table-cell info))))
  4255. (defun org-export-table-row-starts-rowgroup-p (table-row info)
  4256. "Non-nil when TABLE-ROW is at the beginning of a row group.
  4257. INFO is a plist used as a communication channel."
  4258. (unless (or (eq (org-element-property :type table-row) 'rule)
  4259. (org-export-table-row-is-special-p table-row info))
  4260. (let ((borders (org-export-table-cell-borders
  4261. (car (org-element-contents table-row)) info)))
  4262. (or (memq 'top borders) (memq 'above borders)))))
  4263. (defun org-export-table-row-ends-rowgroup-p (table-row info)
  4264. "Non-nil when TABLE-ROW is at the end of a row group.
  4265. INFO is a plist used as a communication channel."
  4266. (unless (or (eq (org-element-property :type table-row) 'rule)
  4267. (org-export-table-row-is-special-p table-row info))
  4268. (let ((borders (org-export-table-cell-borders
  4269. (car (org-element-contents table-row)) info)))
  4270. (or (memq 'bottom borders) (memq 'below borders)))))
  4271. (defun org-export-table-row-starts-header-p (table-row info)
  4272. "Non-nil when TABLE-ROW is the first table header's row.
  4273. INFO is a plist used as a communication channel."
  4274. (and (org-export-table-has-header-p
  4275. (org-export-get-parent-table table-row) info)
  4276. (org-export-table-row-starts-rowgroup-p table-row info)
  4277. (= (org-export-table-row-group table-row info) 1)))
  4278. (defun org-export-table-row-ends-header-p (table-row info)
  4279. "Non-nil when TABLE-ROW is the last table header's row.
  4280. INFO is a plist used as a communication channel."
  4281. (and (org-export-table-has-header-p
  4282. (org-export-get-parent-table table-row) info)
  4283. (org-export-table-row-ends-rowgroup-p table-row info)
  4284. (= (org-export-table-row-group table-row info) 1)))
  4285. (defun org-export-table-row-number (table-row info)
  4286. "Return TABLE-ROW number.
  4287. INFO is a plist used as a communication channel. Return value is
  4288. zero-based and ignores separators. The function returns nil for
  4289. special columns and separators."
  4290. (when (and (eq (org-element-property :type table-row) 'standard)
  4291. (not (org-export-table-row-is-special-p table-row info)))
  4292. (let ((number 0))
  4293. (org-element-map (org-export-get-parent-table table-row) 'table-row
  4294. (lambda (row)
  4295. (cond ((eq row table-row) number)
  4296. ((eq (org-element-property :type row) 'standard)
  4297. (incf number) nil)))
  4298. info 'first-match))))
  4299. (defun org-export-table-dimensions (table info)
  4300. "Return TABLE dimensions.
  4301. INFO is a plist used as a communication channel.
  4302. Return value is a CONS like (ROWS . COLUMNS) where
  4303. ROWS (resp. COLUMNS) is the number of exportable
  4304. rows (resp. columns)."
  4305. (let (first-row (columns 0) (rows 0))
  4306. ;; Set number of rows, and extract first one.
  4307. (org-element-map table 'table-row
  4308. (lambda (row)
  4309. (when (eq (org-element-property :type row) 'standard)
  4310. (incf rows)
  4311. (unless first-row (setq first-row row)))) info)
  4312. ;; Set number of columns.
  4313. (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
  4314. ;; Return value.
  4315. (cons rows columns)))
  4316. (defun org-export-table-cell-address (table-cell info)
  4317. "Return address of a regular TABLE-CELL object.
  4318. TABLE-CELL is the cell considered. INFO is a plist used as
  4319. a communication channel.
  4320. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  4321. zero-based index. Only exportable cells are considered. The
  4322. function returns nil for other cells."
  4323. (let* ((table-row (org-export-get-parent table-cell))
  4324. (row-number (org-export-table-row-number table-row info)))
  4325. (when row-number
  4326. (cons row-number
  4327. (let ((col-count 0))
  4328. (org-element-map table-row 'table-cell
  4329. (lambda (cell)
  4330. (if (eq cell table-cell) col-count (incf col-count) nil))
  4331. info 'first-match))))))
  4332. (defun org-export-get-table-cell-at (address table info)
  4333. "Return regular table-cell object at ADDRESS in TABLE.
  4334. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  4335. zero-based index. TABLE is a table type element. INFO is
  4336. a plist used as a communication channel.
  4337. If no table-cell, among exportable cells, is found at ADDRESS,
  4338. return nil."
  4339. (let ((column-pos (cdr address)) (column-count 0))
  4340. (org-element-map
  4341. ;; Row at (car address) or nil.
  4342. (let ((row-pos (car address)) (row-count 0))
  4343. (org-element-map table 'table-row
  4344. (lambda (row)
  4345. (cond ((eq (org-element-property :type row) 'rule) nil)
  4346. ((= row-count row-pos) row)
  4347. (t (incf row-count) nil)))
  4348. info 'first-match))
  4349. 'table-cell
  4350. (lambda (cell)
  4351. (if (= column-count column-pos) cell
  4352. (incf column-count) nil))
  4353. info 'first-match)))
  4354. ;;;; For Tables Of Contents
  4355. ;;
  4356. ;; `org-export-collect-headlines' builds a list of all exportable
  4357. ;; headline elements, maybe limited to a certain depth. One can then
  4358. ;; easily parse it and transcode it.
  4359. ;;
  4360. ;; Building lists of tables, figures or listings is quite similar.
  4361. ;; Once the generic function `org-export-collect-elements' is defined,
  4362. ;; `org-export-collect-tables', `org-export-collect-figures' and
  4363. ;; `org-export-collect-listings' can be derived from it.
  4364. (defun org-export-collect-headlines (info &optional n)
  4365. "Collect headlines in order to build a table of contents.
  4366. INFO is a plist used as a communication channel.
  4367. When optional argument N is an integer, it specifies the depth of
  4368. the table of contents. Otherwise, it is set to the value of the
  4369. last headline level. See `org-export-headline-levels' for more
  4370. information.
  4371. Return a list of all exportable headlines as parsed elements.
  4372. Footnote sections, if any, will be ignored."
  4373. (let ((limit (plist-get info :headline-levels)))
  4374. (setq n (if (wholenump n) (min n limit) limit))
  4375. (org-element-map (plist-get info :parse-tree) 'headline
  4376. #'(lambda (headline)
  4377. (unless (org-element-property :footnote-section-p headline)
  4378. (let ((level (org-export-get-relative-level headline info)))
  4379. (and (<= level n) headline))))
  4380. info)))
  4381. (defun org-export-collect-elements (type info &optional predicate)
  4382. "Collect referenceable elements of a determined type.
  4383. TYPE can be a symbol or a list of symbols specifying element
  4384. types to search. Only elements with a caption are collected.
  4385. INFO is a plist used as a communication channel.
  4386. When non-nil, optional argument PREDICATE is a function accepting
  4387. one argument, an element of type TYPE. It returns a non-nil
  4388. value when that element should be collected.
  4389. Return a list of all elements found, in order of appearance."
  4390. (org-element-map (plist-get info :parse-tree) type
  4391. (lambda (element)
  4392. (and (org-element-property :caption element)
  4393. (or (not predicate) (funcall predicate element))
  4394. element))
  4395. info))
  4396. (defun org-export-collect-tables (info)
  4397. "Build a list of tables.
  4398. INFO is a plist used as a communication channel.
  4399. Return a list of table elements with a caption."
  4400. (org-export-collect-elements 'table info))
  4401. (defun org-export-collect-figures (info predicate)
  4402. "Build a list of figures.
  4403. INFO is a plist used as a communication channel. PREDICATE is
  4404. a function which accepts one argument: a paragraph element and
  4405. whose return value is non-nil when that element should be
  4406. collected.
  4407. A figure is a paragraph type element, with a caption, verifying
  4408. PREDICATE. The latter has to be provided since a \"figure\" is
  4409. a vague concept that may depend on back-end.
  4410. Return a list of elements recognized as figures."
  4411. (org-export-collect-elements 'paragraph info predicate))
  4412. (defun org-export-collect-listings (info)
  4413. "Build a list of src blocks.
  4414. INFO is a plist used as a communication channel.
  4415. Return a list of src-block elements with a caption."
  4416. (org-export-collect-elements 'src-block info))
  4417. ;;;; Smart Quotes
  4418. ;;
  4419. ;; The main function for the smart quotes sub-system is
  4420. ;; `org-export-activate-smart-quotes', which replaces every quote in
  4421. ;; a given string from the parse tree with its "smart" counterpart.
  4422. ;;
  4423. ;; Dictionary for smart quotes is stored in
  4424. ;; `org-export-smart-quotes-alist'.
  4425. ;;
  4426. ;; Internally, regexps matching potential smart quotes (checks at
  4427. ;; string boundaries are also necessary) are defined in
  4428. ;; `org-export-smart-quotes-regexps'.
  4429. (defconst org-export-smart-quotes-alist
  4430. '(("da"
  4431. ;; one may use: »...«, "...", ›...‹, or '...'.
  4432. ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
  4433. ;; LaTeX quotes require Babel!
  4434. (opening-double-quote :utf-8 "»" :html "&raquo;" :latex ">>"
  4435. :texinfo "@guillemetright{}")
  4436. (closing-double-quote :utf-8 "«" :html "&laquo;" :latex "<<"
  4437. :texinfo "@guillemetleft{}")
  4438. (opening-single-quote :utf-8 "›" :html "&rsaquo;" :latex "\\frq{}"
  4439. :texinfo "@guilsinglright{}")
  4440. (closing-single-quote :utf-8 "‹" :html "&lsaquo;" :latex "\\flq{}"
  4441. :texinfo "@guilsingleft{}")
  4442. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4443. ("de"
  4444. (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
  4445. :texinfo "@quotedblbase{}")
  4446. (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
  4447. :texinfo "@quotedblleft{}")
  4448. (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
  4449. :texinfo "@quotesinglbase{}")
  4450. (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
  4451. :texinfo "@quoteleft{}")
  4452. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4453. ("en"
  4454. (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
  4455. (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
  4456. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4457. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4458. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4459. ("es"
  4460. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4461. :texinfo "@guillemetleft{}")
  4462. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4463. :texinfo "@guillemetright{}")
  4464. (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
  4465. (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
  4466. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4467. ("fr"
  4468. (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
  4469. :texinfo "@guillemetleft{}@tie{}")
  4470. (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
  4471. :texinfo "@tie{}@guillemetright{}")
  4472. (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
  4473. :texinfo "@guillemetleft{}@tie{}")
  4474. (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
  4475. :texinfo "@tie{}@guillemetright{}")
  4476. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4477. ("no"
  4478. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4479. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4480. :texinfo "@guillemetleft{}")
  4481. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4482. :texinfo "@guillemetright{}")
  4483. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4484. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4485. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4486. ("nb"
  4487. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4488. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4489. :texinfo "@guillemetleft{}")
  4490. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4491. :texinfo "@guillemetright{}")
  4492. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4493. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4494. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4495. ("nn"
  4496. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4497. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4498. :texinfo "@guillemetleft{}")
  4499. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4500. :texinfo "@guillemetright{}")
  4501. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4502. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4503. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4504. ("ru"
  4505. ;; 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
  4506. ;; http://www.artlebedev.ru/kovodstvo/sections/104/
  4507. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "{}<<"
  4508. :texinfo "@guillemetleft{}")
  4509. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex ">>{}"
  4510. :texinfo "@guillemetright{}")
  4511. (opening-single-quote :utf-8 "„" :html "&bdquo;" :latex "\\glqq{}"
  4512. :texinfo "@quotedblbase{}")
  4513. (closing-single-quote :utf-8 "“" :html "&ldquo;" :latex "\\grqq{}"
  4514. :texinfo "@quotedblleft{}")
  4515. (apostrophe :utf-8 "’" :html: "&#39;"))
  4516. ("sv"
  4517. ;; based on https://sv.wikipedia.org/wiki/Citattecken
  4518. (opening-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
  4519. (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
  4520. (opening-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "`")
  4521. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "'")
  4522. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4523. )
  4524. "Smart quotes translations.
  4525. Alist whose CAR is a language string and CDR is an alist with
  4526. quote type as key and a plist associating various encodings to
  4527. their translation as value.
  4528. A quote type can be any symbol among `opening-double-quote',
  4529. `closing-double-quote', `opening-single-quote',
  4530. `closing-single-quote' and `apostrophe'.
  4531. Valid encodings include `:utf-8', `:html', `:latex' and
  4532. `:texinfo'.
  4533. If no translation is found, the quote character is left as-is.")
  4534. (defconst org-export-smart-quotes-regexps
  4535. (list
  4536. ;; Possible opening quote at beginning of string.
  4537. "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\|\\s(\\)"
  4538. ;; Possible closing quote at beginning of string.
  4539. "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
  4540. ;; Possible apostrophe at beginning of string.
  4541. "\\`\\('\\)\\S-"
  4542. ;; Opening single and double quotes.
  4543. "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
  4544. ;; Closing single and double quotes.
  4545. "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
  4546. ;; Apostrophe.
  4547. "\\S-\\('\\)\\S-"
  4548. ;; Possible opening quote at end of string.
  4549. "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
  4550. ;; Possible closing quote at end of string.
  4551. "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
  4552. ;; Possible apostrophe at end of string.
  4553. "\\S-\\('\\)\\'")
  4554. "List of regexps matching a quote or an apostrophe.
  4555. In every regexp, quote or apostrophe matched is put in group 1.")
  4556. (defun org-export-activate-smart-quotes (s encoding info &optional original)
  4557. "Replace regular quotes with \"smart\" quotes in string S.
  4558. ENCODING is a symbol among `:html', `:latex', `:texinfo' and
  4559. `:utf-8'. INFO is a plist used as a communication channel.
  4560. The function has to retrieve information about string
  4561. surroundings in parse tree. It can only happen with an
  4562. unmodified string. Thus, if S has already been through another
  4563. process, a non-nil ORIGINAL optional argument will provide that
  4564. original string.
  4565. Return the new string."
  4566. (if (equal s "") ""
  4567. (let* ((prev (org-export-get-previous-element (or original s) info))
  4568. ;; Try to be flexible when computing number of blanks
  4569. ;; before object. The previous object may be a string
  4570. ;; introduced by the back-end and not completely parsed.
  4571. (pre-blank (and prev
  4572. (or (org-element-property :post-blank prev)
  4573. ;; A string with missing `:post-blank'
  4574. ;; property.
  4575. (and (stringp prev)
  4576. (string-match " *\\'" prev)
  4577. (length (match-string 0 prev)))
  4578. ;; Fallback value.
  4579. 0)))
  4580. (next (org-export-get-next-element (or original s) info))
  4581. (get-smart-quote
  4582. (lambda (q type)
  4583. ;; Return smart quote associated to a give quote Q, as
  4584. ;; a string. TYPE is a symbol among `open', `close' and
  4585. ;; `apostrophe'.
  4586. (let ((key (case type
  4587. (apostrophe 'apostrophe)
  4588. (open (if (equal "'" q) 'opening-single-quote
  4589. 'opening-double-quote))
  4590. (otherwise (if (equal "'" q) 'closing-single-quote
  4591. 'closing-double-quote)))))
  4592. (or (plist-get
  4593. (cdr (assq key
  4594. (cdr (assoc (plist-get info :language)
  4595. org-export-smart-quotes-alist))))
  4596. encoding)
  4597. q)))))
  4598. (if (or (equal "\"" s) (equal "'" s))
  4599. ;; Only a quote: no regexp can match. We have to check both
  4600. ;; sides and decide what to do.
  4601. (cond ((and (not prev) (not next)) s)
  4602. ((not prev) (funcall get-smart-quote s 'open))
  4603. ((and (not next) (zerop pre-blank))
  4604. (funcall get-smart-quote s 'close))
  4605. ((not next) s)
  4606. ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
  4607. (t (funcall get-smart-quote 'open)))
  4608. ;; 1. Replace quote character at the beginning of S.
  4609. (cond
  4610. ;; Apostrophe?
  4611. ((and prev (zerop pre-blank)
  4612. (string-match (nth 2 org-export-smart-quotes-regexps) s))
  4613. (setq s (replace-match
  4614. (funcall get-smart-quote (match-string 1 s) 'apostrophe)
  4615. nil t s 1)))
  4616. ;; Closing quote?
  4617. ((and prev (zerop pre-blank)
  4618. (string-match (nth 1 org-export-smart-quotes-regexps) s))
  4619. (setq s (replace-match
  4620. (funcall get-smart-quote (match-string 1 s) 'close)
  4621. nil t s 1)))
  4622. ;; Opening quote?
  4623. ((and (or (not prev) (> pre-blank 0))
  4624. (string-match (nth 0 org-export-smart-quotes-regexps) s))
  4625. (setq s (replace-match
  4626. (funcall get-smart-quote (match-string 1 s) 'open)
  4627. nil t s 1))))
  4628. ;; 2. Replace quotes in the middle of the string.
  4629. (setq s (replace-regexp-in-string
  4630. ;; Opening quotes.
  4631. (nth 3 org-export-smart-quotes-regexps)
  4632. (lambda (text)
  4633. (funcall get-smart-quote (match-string 1 text) 'open))
  4634. s nil t 1))
  4635. (setq s (replace-regexp-in-string
  4636. ;; Closing quotes.
  4637. (nth 4 org-export-smart-quotes-regexps)
  4638. (lambda (text)
  4639. (funcall get-smart-quote (match-string 1 text) 'close))
  4640. s nil t 1))
  4641. (setq s (replace-regexp-in-string
  4642. ;; Apostrophes.
  4643. (nth 5 org-export-smart-quotes-regexps)
  4644. (lambda (text)
  4645. (funcall get-smart-quote (match-string 1 text) 'apostrophe))
  4646. s nil t 1))
  4647. ;; 3. Replace quote character at the end of S.
  4648. (cond
  4649. ;; Apostrophe?
  4650. ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
  4651. (setq s (replace-match
  4652. (funcall get-smart-quote (match-string 1 s) 'apostrophe)
  4653. nil t s 1)))
  4654. ;; Closing quote?
  4655. ((and (not next)
  4656. (string-match (nth 7 org-export-smart-quotes-regexps) s))
  4657. (setq s (replace-match
  4658. (funcall get-smart-quote (match-string 1 s) 'close)
  4659. nil t s 1)))
  4660. ;; Opening quote?
  4661. ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
  4662. (setq s (replace-match
  4663. (funcall get-smart-quote (match-string 1 s) 'open)
  4664. nil t s 1))))
  4665. ;; Return string with smart quotes.
  4666. s))))
  4667. ;;;; Topology
  4668. ;;
  4669. ;; Here are various functions to retrieve information about the
  4670. ;; neighborhood of a given element or object. Neighbors of interest
  4671. ;; are direct parent (`org-export-get-parent'), parent headline
  4672. ;; (`org-export-get-parent-headline'), first element containing an
  4673. ;; object, (`org-export-get-parent-element'), parent table
  4674. ;; (`org-export-get-parent-table'), previous element or object
  4675. ;; (`org-export-get-previous-element') and next element or object
  4676. ;; (`org-export-get-next-element').
  4677. ;;
  4678. ;; `org-export-get-genealogy' returns the full genealogy of a given
  4679. ;; element or object, from closest parent to full parse tree.
  4680. ;; defsubst org-export-get-parent must be defined before first use
  4681. (defun org-export-get-genealogy (blob)
  4682. "Return full genealogy relative to a given element or object.
  4683. BLOB is the element or object being considered.
  4684. Ancestors are returned from closest to farthest, the last one
  4685. being the full parse tree."
  4686. (let (genealogy (parent blob))
  4687. (while (setq parent (org-element-property :parent parent))
  4688. (push parent genealogy))
  4689. (nreverse genealogy)))
  4690. (defun org-export-get-parent-headline (blob)
  4691. "Return BLOB parent headline or nil.
  4692. BLOB is the element or object being considered."
  4693. (let ((parent blob))
  4694. (while (and (setq parent (org-element-property :parent parent))
  4695. (not (eq (org-element-type parent) 'headline))))
  4696. parent))
  4697. (defun org-export-get-parent-element (object)
  4698. "Return first element containing OBJECT or nil.
  4699. OBJECT is the object to consider."
  4700. (let ((parent object))
  4701. (while (and (setq parent (org-element-property :parent parent))
  4702. (memq (org-element-type parent) org-element-all-objects)))
  4703. parent))
  4704. (defun org-export-get-parent-table (object)
  4705. "Return OBJECT parent table or nil.
  4706. OBJECT is either a `table-cell' or `table-element' type object."
  4707. (let ((parent object))
  4708. (while (and (setq parent (org-element-property :parent parent))
  4709. (not (eq (org-element-type parent) 'table))))
  4710. parent))
  4711. (defun org-export-get-previous-element (blob info &optional n)
  4712. "Return previous element or object.
  4713. BLOB is an element or object. INFO is a plist used as
  4714. a communication channel. Return previous exportable element or
  4715. object, a string, or nil.
  4716. When optional argument N is a positive integer, return a list
  4717. containing up to N siblings before BLOB, from farthest to
  4718. closest. With any other non-nil value, return a list containing
  4719. all of them."
  4720. (let* ((secondary (org-element-secondary-p blob))
  4721. (parent (org-export-get-parent blob))
  4722. (siblings
  4723. (if secondary (org-element-property secondary parent)
  4724. (org-element-contents parent)))
  4725. prev)
  4726. (catch 'exit
  4727. (dolist (obj (cdr (memq blob (reverse siblings))) prev)
  4728. (cond ((memq obj (plist-get info :ignore-list)))
  4729. ((null n) (throw 'exit obj))
  4730. ((not (wholenump n)) (push obj prev))
  4731. ((zerop n) (throw 'exit prev))
  4732. (t (decf n) (push obj prev)))))))
  4733. (defun org-export-get-next-element (blob info &optional n)
  4734. "Return next element or object.
  4735. BLOB is an element or object. INFO is a plist used as
  4736. a communication channel. Return next exportable element or
  4737. object, a string, or nil.
  4738. When optional argument N is a positive integer, return a list
  4739. containing up to N siblings after BLOB, from closest to farthest.
  4740. With any other non-nil value, return a list containing all of
  4741. them."
  4742. (let* ((secondary (org-element-secondary-p blob))
  4743. (parent (org-export-get-parent blob))
  4744. (siblings
  4745. (cdr (memq blob
  4746. (if secondary (org-element-property secondary parent)
  4747. (org-element-contents parent)))))
  4748. next)
  4749. (catch 'exit
  4750. (dolist (obj siblings (nreverse next))
  4751. (cond ((memq obj (plist-get info :ignore-list)))
  4752. ((null n) (throw 'exit obj))
  4753. ((not (wholenump n)) (push obj next))
  4754. ((zerop n) (throw 'exit (nreverse next)))
  4755. (t (decf n) (push obj next)))))))
  4756. ;;;; Translation
  4757. ;;
  4758. ;; `org-export-translate' translates a string according to the language
  4759. ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
  4760. ;; the dictionary used for the translation.
  4761. (defconst org-export-dictionary
  4762. '(("%e %n: %c"
  4763. ("fr" :default "%e %n : %c" :html "%e&nbsp;%n&nbsp;: %c"))
  4764. ("Author"
  4765. ("ca" :default "Autor")
  4766. ("cs" :default "Autor")
  4767. ("da" :default "Forfatter")
  4768. ("de" :default "Autor")
  4769. ("eo" :html "A&#365;toro")
  4770. ("es" :default "Autor")
  4771. ("et" :default "Autor")
  4772. ("fi" :html "Tekij&auml;")
  4773. ("fr" :default "Auteur")
  4774. ("hu" :default "Szerz&otilde;")
  4775. ("is" :html "H&ouml;fundur")
  4776. ("it" :default "Autore")
  4777. ("ja" :default "著者" :html "&#33879;&#32773;")
  4778. ("nl" :default "Auteur")
  4779. ("no" :default "Forfatter")
  4780. ("nb" :default "Forfatter")
  4781. ("nn" :default "Forfattar")
  4782. ("pl" :default "Autor")
  4783. ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
  4784. ("sv" :html "F&ouml;rfattare")
  4785. ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
  4786. ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
  4787. ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
  4788. ("Continued from previous page"
  4789. ("de" :default "Fortsetzung von vorheriger Seite")
  4790. ("es" :default "Continúa de la página anterior")
  4791. ("fr" :default "Suite de la page précédente")
  4792. ("it" :default "Continua da pagina precedente")
  4793. ("ja" :default "前ページからの続き")
  4794. ("nl" :default "Vervolg van vorige pagina")
  4795. ("pt" :default "Continuação da página anterior")
  4796. ("ru" :html "(&#1055;&#1088;&#1086;&#1076;&#1086;&#1083;&#1078;&#1077;&#1085;&#1080;&#1077;)"
  4797. :utf-8 "(Продолжение)"))
  4798. ("Continued on next page"
  4799. ("de" :default "Fortsetzung nächste Seite")
  4800. ("es" :default "Continúa en la siguiente página")
  4801. ("fr" :default "Suite page suivante")
  4802. ("it" :default "Continua alla pagina successiva")
  4803. ("ja" :default "次ページに続く")
  4804. ("nl" :default "Vervolg op volgende pagina")
  4805. ("pt" :default "Continua na página seguinte")
  4806. ("ru" :html "(&#1055;&#1088;&#1086;&#1076;&#1086;&#1083;&#1078;&#1077;&#1085;&#1080;&#1077; &#1089;&#1083;&#1077;&#1076;&#1091;&#1077;&#1090;)"
  4807. :utf-8 "(Продолжение следует)"))
  4808. ("Date"
  4809. ("ca" :default "Data")
  4810. ("cs" :default "Datum")
  4811. ("da" :default "Dato")
  4812. ("de" :default "Datum")
  4813. ("eo" :default "Dato")
  4814. ("es" :default "Fecha")
  4815. ("et" :html "Kuup&#228;ev" :utf-8 "Kuupäev")
  4816. ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
  4817. ("hu" :html "D&aacute;tum")
  4818. ("is" :default "Dagsetning")
  4819. ("it" :default "Data")
  4820. ("ja" :default "日付" :html "&#26085;&#20184;")
  4821. ("nl" :default "Datum")
  4822. ("no" :default "Dato")
  4823. ("nb" :default "Dato")
  4824. ("nn" :default "Dato")
  4825. ("pl" :default "Data")
  4826. ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
  4827. ("sv" :default "Datum")
  4828. ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
  4829. ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
  4830. ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
  4831. ("Equation"
  4832. ("da" :default "Ligning")
  4833. ("de" :default "Gleichung")
  4834. ("es" :html "Ecuaci&oacute;n" :default "Ecuación")
  4835. ("et" :html "V&#245;rrand" :utf-8 "Võrrand")
  4836. ("fr" :ascii "Equation" :default "Équation")
  4837. ("ja" :default "方程式")
  4838. ("no" :default "Ligning")
  4839. ("nb" :default "Ligning")
  4840. ("nn" :default "Likning")
  4841. ("ru" :html "&#1059;&#1088;&#1072;&#1074;&#1085;&#1077;&#1085;&#1080;&#1077;"
  4842. :utf-8 "Уравнение")
  4843. ("sv" :default "Ekvation")
  4844. ("zh-CN" :html "&#26041;&#31243;" :utf-8 "方程"))
  4845. ("Figure"
  4846. ("da" :default "Figur")
  4847. ("de" :default "Abbildung")
  4848. ("es" :default "Figura")
  4849. ("et" :default "Joonis")
  4850. ("ja" :default "図" :html "&#22259;")
  4851. ("no" :default "Illustrasjon")
  4852. ("nb" :default "Illustrasjon")
  4853. ("nn" :default "Illustrasjon")
  4854. ("ru" :html "&#1056;&#1080;&#1089;&#1091;&#1085;&#1086;&#1082;" :utf-8 "Рисунок")
  4855. ("sv" :default "Illustration")
  4856. ("zh-CN" :html "&#22270;" :utf-8 "图"))
  4857. ("Figure %d:"
  4858. ("da" :default "Figur %d")
  4859. ("de" :default "Abbildung %d:")
  4860. ("es" :default "Figura %d:")
  4861. ("et" :default "Joonis %d:")
  4862. ("fr" :default "Figure %d :" :html "Figure&nbsp;%d&nbsp;:")
  4863. ("ja" :default "図%d: " :html "&#22259;%d: ")
  4864. ("no" :default "Illustrasjon %d")
  4865. ("nb" :default "Illustrasjon %d")
  4866. ("nn" :default "Illustrasjon %d")
  4867. ("ru" :html "&#1056;&#1080;&#1089;. %d.:" :utf-8 "Рис. %d.:")
  4868. ("sv" :default "Illustration %d")
  4869. ("zh-CN" :html "&#22270;%d&nbsp;" :utf-8 "图%d "))
  4870. ("Footnotes"
  4871. ("ca" :html "Peus de p&agrave;gina")
  4872. ("cs" :default "Pozn\xe1mky pod carou")
  4873. ("da" :default "Fodnoter")
  4874. ("de" :html "Fu&szlig;noten" :default "Fußnoten")
  4875. ("eo" :default "Piednotoj")
  4876. ("es" :html "Nota al pie de p&aacute;gina" :default "Nota al pie de página")
  4877. ("et" :html "Allm&#228;rkused" :utf-8 "Allmärkused")
  4878. ("fi" :default "Alaviitteet")
  4879. ("fr" :default "Notes de bas de page")
  4880. ("hu" :html "L&aacute;bjegyzet")
  4881. ("is" :html "Aftanm&aacute;lsgreinar")
  4882. ("it" :html "Note a pi&egrave; di pagina")
  4883. ("ja" :default "脚注" :html "&#33050;&#27880;")
  4884. ("nl" :default "Voetnoten")
  4885. ("no" :default "Fotnoter")
  4886. ("nb" :default "Fotnoter")
  4887. ("nn" :default "Fotnotar")
  4888. ("pl" :default "Przypis")
  4889. ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
  4890. ("sv" :default "Fotnoter")
  4891. ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
  4892. :utf-8 "Примітки")
  4893. ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
  4894. ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
  4895. ("List of Listings"
  4896. ("da" :default "Programmer")
  4897. ("de" :default "Programmauflistungsverzeichnis")
  4898. ("es" :default "Indice de Listados de programas")
  4899. ("et" :default "Loendite nimekiri")
  4900. ("fr" :default "Liste des programmes")
  4901. ("ja" :default "ソースコード目次")
  4902. ("no" :default "Dataprogrammer")
  4903. ("nb" :default "Dataprogrammer")
  4904. ("ru" :html "&#1057;&#1087;&#1080;&#1089;&#1086;&#1082; &#1088;&#1072;&#1089;&#1087;&#1077;&#1095;&#1072;&#1090;&#1086;&#1082;"
  4905. :utf-8 "Список распечаток")
  4906. ("zh-CN" :html "&#20195;&#30721;&#30446;&#24405;" :utf-8 "代码目录"))
  4907. ("List of Tables"
  4908. ("da" :default "Tabeller")
  4909. ("de" :default "Tabellenverzeichnis")
  4910. ("es" :default "Indice de tablas")
  4911. ("et" :default "Tabelite nimekiri")
  4912. ("fr" :default "Liste des tableaux")
  4913. ("ja" :default "表目次")
  4914. ("no" :default "Tabeller")
  4915. ("nb" :default "Tabeller")
  4916. ("nn" :default "Tabeller")
  4917. ("ru" :html "&#1057;&#1087;&#1080;&#1089;&#1086;&#1082; &#1090;&#1072;&#1073;&#1083;&#1080;&#1094;"
  4918. :utf-8 "Список таблиц")
  4919. ("sv" :default "Tabeller")
  4920. ("zh-CN" :html "&#34920;&#26684;&#30446;&#24405;" :utf-8 "表格目录"))
  4921. ("Listing %d:"
  4922. ("da" :default "Program %d")
  4923. ("de" :default "Programmlisting %d")
  4924. ("es" :default "Listado de programa %d")
  4925. ("et" :default "Loend %d")
  4926. ("fr" :default "Programme %d :" :html "Programme&nbsp;%d&nbsp;:")
  4927. ("ja" :default "ソースコード%d:")
  4928. ("no" :default "Dataprogram %d")
  4929. ("nb" :default "Dataprogram %d")
  4930. ("ru" :html "&#1056;&#1072;&#1089;&#1087;&#1077;&#1095;&#1072;&#1090;&#1082;&#1072; %d.:"
  4931. :utf-8 "Распечатка %d.:")
  4932. ("zh-CN" :html "&#20195;&#30721;%d&nbsp;" :utf-8 "代码%d "))
  4933. ("References"
  4934. ("fr" :ascii "References" :default "Références")
  4935. ("de" :default "Quellen"))
  4936. ("See section %s"
  4937. ("da" :default "jævnfør afsnit %s")
  4938. ("de" :default "siehe Abschnitt %s")
  4939. ("es" :default "vea seccion %s")
  4940. ("et" :html "Vaata peat&#252;kki %s" :utf-8 "Vaata peatükki %s")
  4941. ("fr" :default "cf. section %s")
  4942. ("ja" :default "セクション %s を参照")
  4943. ("ru" :html "&#1057;&#1084;. &#1088;&#1072;&#1079;&#1076;&#1077;&#1083; %s"
  4944. :utf-8 "См. раздел %s")
  4945. ("zh-CN" :html "&#21442;&#35265;&#31532;%s&#33410;" :utf-8 "参见第%s节"))
  4946. ("Table"
  4947. ("de" :default "Tabelle")
  4948. ("es" :default "Tabla")
  4949. ("et" :default "Tabel")
  4950. ("fr" :default "Tableau")
  4951. ("ja" :default "表" :html "&#34920;")
  4952. ("ru" :html "&#1058;&#1072;&#1073;&#1083;&#1080;&#1094;&#1072;" :utf-8 "Таблица")
  4953. ("zh-CN" :html "&#34920;" :utf-8 "表"))
  4954. ("Table %d:"
  4955. ("da" :default "Tabel %d")
  4956. ("de" :default "Tabelle %d")
  4957. ("es" :default "Tabla %d")
  4958. ("et" :default "Tabel %d")
  4959. ("fr" :default "Tableau %d :")
  4960. ("ja" :default "表%d:" :html "&#34920;%d:")
  4961. ("no" :default "Tabell %d")
  4962. ("nb" :default "Tabell %d")
  4963. ("nn" :default "Tabell %d")
  4964. ("ru" :html "&#1058;&#1072;&#1073;&#1083;&#1080;&#1094;&#1072; %d.:"
  4965. :utf-8 "Таблица %d.:")
  4966. ("sv" :default "Tabell %d")
  4967. ("zh-CN" :html "&#34920;%d&nbsp;" :utf-8 "表%d "))
  4968. ("Table of Contents"
  4969. ("ca" :html "&Iacute;ndex")
  4970. ("cs" :default "Obsah")
  4971. ("da" :default "Indhold")
  4972. ("de" :default "Inhaltsverzeichnis")
  4973. ("eo" :default "Enhavo")
  4974. ("es" :html "&Iacute;ndice")
  4975. ("et" :default "Sisukord")
  4976. ("fi" :html "Sis&auml;llysluettelo")
  4977. ("fr" :ascii "Sommaire" :default "Table des matières")
  4978. ("hu" :html "Tartalomjegyz&eacute;k")
  4979. ("is" :default "Efnisyfirlit")
  4980. ("it" :default "Indice")
  4981. ("ja" :default "目次" :html "&#30446;&#27425;")
  4982. ("nl" :default "Inhoudsopgave")
  4983. ("no" :default "Innhold")
  4984. ("nb" :default "Innhold")
  4985. ("nn" :default "Innhald")
  4986. ("pl" :html "Spis tre&#x015b;ci")
  4987. ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
  4988. :utf-8 "Содержание")
  4989. ("sv" :html "Inneh&aring;ll")
  4990. ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
  4991. ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
  4992. ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
  4993. ("Unknown reference"
  4994. ("da" :default "ukendt reference")
  4995. ("de" :default "Unbekannter Verweis")
  4996. ("es" :default "referencia desconocida")
  4997. ("et" :default "Tundmatu viide")
  4998. ("fr" :ascii "Destination inconnue" :default "Référence inconnue")
  4999. ("ja" :default "不明な参照先")
  5000. ("ru" :html "&#1053;&#1077;&#1080;&#1079;&#1074;&#1077;&#1089;&#1090;&#1085;&#1072;&#1103; &#1089;&#1089;&#1099;&#1083;&#1082;&#1072;"
  5001. :utf-8 "Неизвестная ссылка")
  5002. ("zh-CN" :html "&#26410;&#30693;&#24341;&#29992;" :utf-8 "未知引用")))
  5003. "Dictionary for export engine.
  5004. Alist whose CAR is the string to translate and CDR is an alist
  5005. whose CAR is the language string and CDR is a plist whose
  5006. properties are possible charsets and values translated terms.
  5007. It is used as a database for `org-export-translate'. Since this
  5008. function returns the string as-is if no translation was found,
  5009. the variable only needs to record values different from the
  5010. entry.")
  5011. (defun org-export-translate (s encoding info)
  5012. "Translate string S according to language specification.
  5013. ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
  5014. and `:utf-8'. INFO is a plist used as a communication channel.
  5015. Translation depends on `:language' property. Return the
  5016. translated string. If no translation is found, try to fall back
  5017. to `:default' encoding. If it fails, return S."
  5018. (let* ((lang (plist-get info :language))
  5019. (translations (cdr (assoc lang
  5020. (cdr (assoc s org-export-dictionary))))))
  5021. (or (plist-get translations encoding)
  5022. (plist-get translations :default)
  5023. s)))
  5024. ;;; Asynchronous Export
  5025. ;;
  5026. ;; `org-export-async-start' is the entry point for asynchronous
  5027. ;; export. It recreates current buffer (including visibility,
  5028. ;; narrowing and visited file) in an external Emacs process, and
  5029. ;; evaluates a command there. It then applies a function on the
  5030. ;; returned results in the current process.
  5031. ;;
  5032. ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
  5033. ;; allow to export to a buffer or a file, asynchronously or not.
  5034. ;;
  5035. ;; `org-export-output-file-name' is an auxiliary function meant to be
  5036. ;; used with `org-export-to-file'. With a given extension, it tries
  5037. ;; to provide a canonical file name to write export output to.
  5038. ;;
  5039. ;; Asynchronously generated results are never displayed directly.
  5040. ;; Instead, they are stored in `org-export-stack-contents'. They can
  5041. ;; then be retrieved by calling `org-export-stack'.
  5042. ;;
  5043. ;; Export Stack is viewed through a dedicated major mode
  5044. ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
  5045. ;;`org-export-stack-delete', `org-export-stack-view' and
  5046. ;;`org-export-stack-clear'.
  5047. ;;
  5048. ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
  5049. ;; It should be used whenever `org-export-async-start' is called.
  5050. (defmacro org-export-async-start (fun &rest body)
  5051. "Call function FUN on the results returned by BODY evaluation.
  5052. BODY evaluation happens in an asynchronous process, from a buffer
  5053. which is an exact copy of the current one.
  5054. Use `org-export-add-to-stack' in FUN in order to register results
  5055. in the stack.
  5056. This is a low level function. See also `org-export-to-buffer'
  5057. and `org-export-to-file' for more specialized functions."
  5058. (declare (indent 1) (debug t))
  5059. (org-with-gensyms (process temp-file copy-fun proc-buffer coding)
  5060. ;; Write the full sexp evaluating BODY in a copy of the current
  5061. ;; buffer to a temporary file, as it may be too long for program
  5062. ;; args in `start-process'.
  5063. `(with-temp-message "Initializing asynchronous export process"
  5064. (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
  5065. (,temp-file (make-temp-file "org-export-process"))
  5066. (,coding buffer-file-coding-system))
  5067. (with-temp-file ,temp-file
  5068. (insert
  5069. ;; Null characters (from variable values) are inserted
  5070. ;; within the file. As a consequence, coding system for
  5071. ;; buffer contents will not be recognized properly. So,
  5072. ;; we make sure it is the same as the one used to display
  5073. ;; the original buffer.
  5074. (format ";; -*- coding: %s; -*-\n%S"
  5075. ,coding
  5076. `(with-temp-buffer
  5077. (when org-export-async-debug '(setq debug-on-error t))
  5078. ;; Ignore `kill-emacs-hook' and code evaluation
  5079. ;; queries from Babel as we need a truly
  5080. ;; non-interactive process.
  5081. (setq kill-emacs-hook nil
  5082. org-babel-confirm-evaluate-answer-no t)
  5083. ;; Initialize export framework.
  5084. (require 'ox)
  5085. ;; Re-create current buffer there.
  5086. (funcall ,,copy-fun)
  5087. (restore-buffer-modified-p nil)
  5088. ;; Sexp to evaluate in the buffer.
  5089. (print (progn ,,@body))))))
  5090. ;; Start external process.
  5091. (let* ((process-connection-type nil)
  5092. (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
  5093. (,process
  5094. (apply
  5095. #'start-process
  5096. (append
  5097. (list "org-export-process"
  5098. ,proc-buffer
  5099. (expand-file-name invocation-name invocation-directory)
  5100. "--batch")
  5101. (if org-export-async-init-file
  5102. (list "-Q" "-l" org-export-async-init-file)
  5103. (list "-l" user-init-file))
  5104. (list "-l" ,temp-file)))))
  5105. ;; Register running process in stack.
  5106. (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
  5107. ;; Set-up sentinel in order to catch results.
  5108. (let ((handler ,fun))
  5109. (set-process-sentinel
  5110. ,process
  5111. `(lambda (p status)
  5112. (let ((proc-buffer (process-buffer p)))
  5113. (when (eq (process-status p) 'exit)
  5114. (unwind-protect
  5115. (if (zerop (process-exit-status p))
  5116. (unwind-protect
  5117. (let ((results
  5118. (with-current-buffer proc-buffer
  5119. (goto-char (point-max))
  5120. (backward-sexp)
  5121. (read (current-buffer)))))
  5122. (funcall ,handler results))
  5123. (unless org-export-async-debug
  5124. (and (get-buffer proc-buffer)
  5125. (kill-buffer proc-buffer))))
  5126. (org-export-add-to-stack proc-buffer nil p)
  5127. (ding)
  5128. (message "Process '%s' exited abnormally" p))
  5129. (unless org-export-async-debug
  5130. (delete-file ,,temp-file)))))))))))))
  5131. ;;;###autoload
  5132. (defun org-export-to-buffer
  5133. (backend buffer
  5134. &optional async subtreep visible-only body-only ext-plist
  5135. post-process)
  5136. "Call `org-export-as' with output to a specified buffer.
  5137. BACKEND is either an export back-end, as returned by, e.g.,
  5138. `org-export-create-backend', or a symbol referring to
  5139. a registered back-end.
  5140. BUFFER is the name of the output buffer. If it already exists,
  5141. it will be erased first, otherwise, it will be created.
  5142. A non-nil optional argument ASYNC means the process should happen
  5143. asynchronously. The resulting buffer should then be accessible
  5144. through the `org-export-stack' interface. When ASYNC is nil, the
  5145. buffer is displayed if `org-export-show-temporary-export-buffer'
  5146. is non-nil.
  5147. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  5148. EXT-PLIST are similar to those used in `org-export-as', which
  5149. see.
  5150. Optional argument POST-PROCESS is a function which should accept
  5151. no argument. It is always called within the current process,
  5152. from BUFFER, with point at its beginning. Export back-ends can
  5153. use it to set a major mode there, e.g,
  5154. \(defun org-latex-export-as-latex
  5155. \(&optional async subtreep visible-only body-only ext-plist)
  5156. \(interactive)
  5157. \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
  5158. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  5159. This function returns BUFFER."
  5160. (declare (indent 2))
  5161. (if async
  5162. (org-export-async-start
  5163. `(lambda (output)
  5164. (with-current-buffer (get-buffer-create ,buffer)
  5165. (erase-buffer)
  5166. (setq buffer-file-coding-system ',buffer-file-coding-system)
  5167. (insert output)
  5168. (goto-char (point-min))
  5169. (org-export-add-to-stack (current-buffer) ',backend)
  5170. (ignore-errors (funcall ,post-process))))
  5171. `(org-export-as
  5172. ',backend ,subtreep ,visible-only ,body-only ',ext-plist))
  5173. (let ((output
  5174. (org-export-as backend subtreep visible-only body-only ext-plist))
  5175. (buffer (get-buffer-create buffer))
  5176. (encoding buffer-file-coding-system))
  5177. (when (and (org-string-nw-p output) (org-export--copy-to-kill-ring-p))
  5178. (org-kill-new output))
  5179. (with-current-buffer buffer
  5180. (erase-buffer)
  5181. (setq buffer-file-coding-system encoding)
  5182. (insert output)
  5183. (goto-char (point-min))
  5184. (and (functionp post-process) (funcall post-process)))
  5185. (when org-export-show-temporary-export-buffer
  5186. (switch-to-buffer-other-window buffer))
  5187. buffer)))
  5188. ;;;###autoload
  5189. (defun org-export-to-file
  5190. (backend file &optional async subtreep visible-only body-only ext-plist
  5191. post-process)
  5192. "Call `org-export-as' with output to a specified file.
  5193. BACKEND is either an export back-end, as returned by, e.g.,
  5194. `org-export-create-backend', or a symbol referring to
  5195. a registered back-end. FILE is the name of the output file, as
  5196. a string.
  5197. A non-nil optional argument ASYNC means the process should happen
  5198. asynchronously. The resulting buffer will then be accessible
  5199. through the `org-export-stack' interface.
  5200. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  5201. EXT-PLIST are similar to those used in `org-export-as', which
  5202. see.
  5203. Optional argument POST-PROCESS is called with FILE as its
  5204. argument and happens asynchronously when ASYNC is non-nil. It
  5205. has to return a file name, or nil. Export back-ends can use this
  5206. to send the output file through additional processing, e.g,
  5207. \(defun org-latex-export-to-latex
  5208. \(&optional async subtreep visible-only body-only ext-plist)
  5209. \(interactive)
  5210. \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
  5211. \(org-export-to-file 'latex outfile
  5212. async subtreep visible-only body-only ext-plist
  5213. \(lambda (file) (org-latex-compile file)))
  5214. The function returns either a file name returned by POST-PROCESS,
  5215. or FILE."
  5216. (declare (indent 2))
  5217. (if (not (file-writable-p file)) (error "Output file not writable")
  5218. (let ((encoding (or org-export-coding-system buffer-file-coding-system)))
  5219. (if async
  5220. (org-export-async-start
  5221. `(lambda (file)
  5222. (org-export-add-to-stack (expand-file-name file) ',backend))
  5223. `(let ((output
  5224. (org-export-as
  5225. ',backend ,subtreep ,visible-only ,body-only
  5226. ',ext-plist)))
  5227. (with-temp-buffer
  5228. (insert output)
  5229. (let ((coding-system-for-write ',encoding))
  5230. (write-file ,file)))
  5231. (or (ignore-errors (funcall ',post-process ,file)) ,file)))
  5232. (let ((output (org-export-as
  5233. backend subtreep visible-only body-only ext-plist)))
  5234. (with-temp-buffer
  5235. (insert output)
  5236. (let ((coding-system-for-write encoding))
  5237. (write-file file)))
  5238. (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output))
  5239. (org-kill-new output))
  5240. ;; Get proper return value.
  5241. (or (and (functionp post-process) (funcall post-process file))
  5242. file))))))
  5243. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  5244. "Return output file's name according to buffer specifications.
  5245. EXTENSION is a string representing the output file extension,
  5246. with the leading dot.
  5247. With a non-nil optional argument SUBTREEP, try to determine
  5248. output file's name by looking for \"EXPORT_FILE_NAME\" property
  5249. of subtree at point.
  5250. When optional argument PUB-DIR is set, use it as the publishing
  5251. directory.
  5252. When optional argument VISIBLE-ONLY is non-nil, don't export
  5253. contents of hidden elements.
  5254. Return file name as a string."
  5255. (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
  5256. (base-name
  5257. ;; File name may come from EXPORT_FILE_NAME subtree
  5258. ;; property, assuming point is at beginning of said
  5259. ;; sub-tree.
  5260. (file-name-sans-extension
  5261. (or (and subtreep
  5262. (org-entry-get
  5263. (save-excursion
  5264. (ignore-errors (org-back-to-heading) (point)))
  5265. "EXPORT_FILE_NAME" t))
  5266. ;; File name may be extracted from buffer's associated
  5267. ;; file, if any.
  5268. (and visited-file (file-name-nondirectory visited-file))
  5269. ;; Can't determine file name on our own: Ask user.
  5270. (let ((read-file-name-function
  5271. (and org-completion-use-ido 'ido-read-file-name)))
  5272. (read-file-name
  5273. "Output file: " pub-dir nil nil nil
  5274. (lambda (name)
  5275. (string= (file-name-extension name t) extension)))))))
  5276. (output-file
  5277. ;; Build file name. Enforce EXTENSION over whatever user
  5278. ;; may have come up with. PUB-DIR, if defined, always has
  5279. ;; precedence over any provided path.
  5280. (cond
  5281. (pub-dir
  5282. (concat (file-name-as-directory pub-dir)
  5283. (file-name-nondirectory base-name)
  5284. extension))
  5285. ((file-name-absolute-p base-name) (concat base-name extension))
  5286. (t (concat (file-name-as-directory ".") base-name extension)))))
  5287. ;; If writing to OUTPUT-FILE would overwrite original file, append
  5288. ;; EXTENSION another time to final name.
  5289. (if (and visited-file (org-file-equal-p visited-file output-file))
  5290. (concat output-file extension)
  5291. output-file)))
  5292. (defun org-export-add-to-stack (source backend &optional process)
  5293. "Add a new result to export stack if not present already.
  5294. SOURCE is a buffer or a file name containing export results.
  5295. BACKEND is a symbol representing export back-end used to generate
  5296. it.
  5297. Entries already pointing to SOURCE and unavailable entries are
  5298. removed beforehand. Return the new stack."
  5299. (setq org-export-stack-contents
  5300. (cons (list source backend (or process (current-time)))
  5301. (org-export-stack-remove source))))
  5302. (defun org-export-stack ()
  5303. "Menu for asynchronous export results and running processes."
  5304. (interactive)
  5305. (let ((buffer (get-buffer-create "*Org Export Stack*")))
  5306. (set-buffer buffer)
  5307. (when (zerop (buffer-size)) (org-export-stack-mode))
  5308. (org-export-stack-refresh)
  5309. (pop-to-buffer buffer))
  5310. (message "Type \"q\" to quit, \"?\" for help"))
  5311. (defun org-export--stack-source-at-point ()
  5312. "Return source from export results at point in stack."
  5313. (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
  5314. (if (not source) (error "Source unavailable, please refresh buffer")
  5315. (let ((source-name (if (stringp source) source (buffer-name source))))
  5316. (if (save-excursion
  5317. (beginning-of-line)
  5318. (looking-at (concat ".* +" (regexp-quote source-name) "$")))
  5319. source
  5320. ;; SOURCE is not consistent with current line. The stack
  5321. ;; view is outdated.
  5322. (error "Source unavailable; type `g' to update buffer"))))))
  5323. (defun org-export-stack-clear ()
  5324. "Remove all entries from export stack."
  5325. (interactive)
  5326. (setq org-export-stack-contents nil))
  5327. (defun org-export-stack-refresh (&rest dummy)
  5328. "Refresh the asynchronous export stack.
  5329. DUMMY is ignored. Unavailable sources are removed from the list.
  5330. Return the new stack."
  5331. (let ((inhibit-read-only t))
  5332. (org-preserve-lc
  5333. (erase-buffer)
  5334. (insert (concat
  5335. (let ((counter 0))
  5336. (mapconcat
  5337. (lambda (entry)
  5338. (let ((proc-p (processp (nth 2 entry))))
  5339. (concat
  5340. ;; Back-end.
  5341. (format " %-12s " (or (nth 1 entry) ""))
  5342. ;; Age.
  5343. (let ((data (nth 2 entry)))
  5344. (if proc-p (format " %6s " (process-status data))
  5345. ;; Compute age of the results.
  5346. (org-format-seconds
  5347. "%4h:%.2m "
  5348. (float-time (time-since data)))))
  5349. ;; Source.
  5350. (format " %s"
  5351. (let ((source (car entry)))
  5352. (if (stringp source) source
  5353. (buffer-name source)))))))
  5354. ;; Clear stack from exited processes, dead buffers or
  5355. ;; non-existent files.
  5356. (setq org-export-stack-contents
  5357. (org-remove-if-not
  5358. (lambda (el)
  5359. (if (processp (nth 2 el))
  5360. (buffer-live-p (process-buffer (nth 2 el)))
  5361. (let ((source (car el)))
  5362. (if (bufferp source) (buffer-live-p source)
  5363. (file-exists-p source)))))
  5364. org-export-stack-contents)) "\n")))))))
  5365. (defun org-export-stack-remove (&optional source)
  5366. "Remove export results at point from stack.
  5367. If optional argument SOURCE is non-nil, remove it instead."
  5368. (interactive)
  5369. (let ((source (or source (org-export--stack-source-at-point))))
  5370. (setq org-export-stack-contents
  5371. (org-remove-if (lambda (el) (equal (car el) source))
  5372. org-export-stack-contents))))
  5373. (defun org-export-stack-view (&optional in-emacs)
  5374. "View export results at point in stack.
  5375. With an optional prefix argument IN-EMACS, force viewing files
  5376. within Emacs."
  5377. (interactive "P")
  5378. (let ((source (org-export--stack-source-at-point)))
  5379. (cond ((processp source)
  5380. (org-switch-to-buffer-other-window (process-buffer source)))
  5381. ((bufferp source) (org-switch-to-buffer-other-window source))
  5382. (t (org-open-file source in-emacs)))))
  5383. (defvar org-export-stack-mode-map
  5384. (let ((km (make-sparse-keymap)))
  5385. (define-key km " " 'next-line)
  5386. (define-key km "n" 'next-line)
  5387. (define-key km "\C-n" 'next-line)
  5388. (define-key km [down] 'next-line)
  5389. (define-key km "p" 'previous-line)
  5390. (define-key km "\C-p" 'previous-line)
  5391. (define-key km "\C-?" 'previous-line)
  5392. (define-key km [up] 'previous-line)
  5393. (define-key km "C" 'org-export-stack-clear)
  5394. (define-key km "v" 'org-export-stack-view)
  5395. (define-key km (kbd "RET") 'org-export-stack-view)
  5396. (define-key km "d" 'org-export-stack-remove)
  5397. km)
  5398. "Keymap for Org Export Stack.")
  5399. (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
  5400. "Mode for displaying asynchronous export stack.
  5401. Type \\[org-export-stack] to visualize the asynchronous export
  5402. stack.
  5403. In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
  5404. on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
  5405. stack completely.
  5406. Removing entries in an Org Export Stack buffer doesn't affect
  5407. files or buffers, only the display.
  5408. \\{org-export-stack-mode-map}"
  5409. (abbrev-mode 0)
  5410. (auto-fill-mode 0)
  5411. (setq buffer-read-only t
  5412. buffer-undo-list t
  5413. truncate-lines t
  5414. header-line-format
  5415. '(:eval
  5416. (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
  5417. (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
  5418. (set (make-local-variable 'revert-buffer-function)
  5419. 'org-export-stack-refresh))
  5420. ;;; The Dispatcher
  5421. ;;
  5422. ;; `org-export-dispatch' is the standard interactive way to start an
  5423. ;; export process. It uses `org-export--dispatch-ui' as a subroutine
  5424. ;; for its interface, which, in turn, delegates response to key
  5425. ;; pressed to `org-export--dispatch-action'.
  5426. ;;;###autoload
  5427. (defun org-export-dispatch (&optional arg)
  5428. "Export dispatcher for Org mode.
  5429. It provides an access to common export related tasks in a buffer.
  5430. Its interface comes in two flavors: standard and expert.
  5431. While both share the same set of bindings, only the former
  5432. displays the valid keys associations in a dedicated buffer.
  5433. Scrolling (resp. line-wise motion) in this buffer is done with
  5434. SPC and DEL (resp. C-n and C-p) keys.
  5435. Set variable `org-export-dispatch-use-expert-ui' to switch to one
  5436. flavor or the other.
  5437. When ARG is \\[universal-argument], repeat the last export action, with the same set
  5438. of options used back then, on the current buffer.
  5439. When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
  5440. (interactive "P")
  5441. (let* ((input
  5442. (cond ((equal arg '(16)) '(stack))
  5443. ((and arg org-export-dispatch-last-action))
  5444. (t (save-window-excursion
  5445. (unwind-protect
  5446. (progn
  5447. ;; Remember where we are
  5448. (move-marker org-export-dispatch-last-position
  5449. (point)
  5450. (org-base-buffer (current-buffer)))
  5451. ;; Get and store an export command
  5452. (setq org-export-dispatch-last-action
  5453. (org-export--dispatch-ui
  5454. (list org-export-initial-scope
  5455. (and org-export-in-background 'async))
  5456. nil
  5457. org-export-dispatch-use-expert-ui)))
  5458. (and (get-buffer "*Org Export Dispatcher*")
  5459. (kill-buffer "*Org Export Dispatcher*")))))))
  5460. (action (car input))
  5461. (optns (cdr input)))
  5462. (unless (memq 'subtree optns)
  5463. (move-marker org-export-dispatch-last-position nil))
  5464. (case action
  5465. ;; First handle special hard-coded actions.
  5466. (template (org-export-insert-default-template nil optns))
  5467. (stack (org-export-stack))
  5468. (publish-current-file
  5469. (org-publish-current-file (memq 'force optns) (memq 'async optns)))
  5470. (publish-current-project
  5471. (org-publish-current-project (memq 'force optns) (memq 'async optns)))
  5472. (publish-choose-project
  5473. (org-publish (assoc (org-icompleting-read
  5474. "Publish project: "
  5475. org-publish-project-alist nil t)
  5476. org-publish-project-alist)
  5477. (memq 'force optns)
  5478. (memq 'async optns)))
  5479. (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
  5480. (otherwise
  5481. (save-excursion
  5482. (when arg
  5483. ;; Repeating command, maybe move cursor to restore subtree
  5484. ;; context.
  5485. (if (eq (marker-buffer org-export-dispatch-last-position)
  5486. (org-base-buffer (current-buffer)))
  5487. (goto-char org-export-dispatch-last-position)
  5488. ;; We are in a different buffer, forget position.
  5489. (move-marker org-export-dispatch-last-position nil)))
  5490. (funcall action
  5491. ;; Return a symbol instead of a list to ease
  5492. ;; asynchronous export macro use.
  5493. (and (memq 'async optns) t)
  5494. (and (memq 'subtree optns) t)
  5495. (and (memq 'visible optns) t)
  5496. (and (memq 'body optns) t)))))))
  5497. (defun org-export--dispatch-ui (options first-key expertp)
  5498. "Handle interface for `org-export-dispatch'.
  5499. OPTIONS is a list containing current interactive options set for
  5500. export. It can contain any of the following symbols:
  5501. `body' toggles a body-only export
  5502. `subtree' restricts export to current subtree
  5503. `visible' restricts export to visible part of buffer.
  5504. `force' force publishing files.
  5505. `async' use asynchronous export process
  5506. FIRST-KEY is the key pressed to select the first level menu. It
  5507. is nil when this menu hasn't been selected yet.
  5508. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  5509. buffer is provided, but indications about currently active
  5510. options are given in the prompt. Moreover, \[?] allows to switch
  5511. back to standard interface."
  5512. (let* ((fontify-key
  5513. (lambda (key &optional access-key)
  5514. ;; Fontify KEY string. Optional argument ACCESS-KEY, when
  5515. ;; non-nil is the required first-level key to activate
  5516. ;; KEY. When its value is t, activate KEY independently
  5517. ;; on the first key, if any. A nil value means KEY will
  5518. ;; only be activated at first level.
  5519. (if (or (eq access-key t) (eq access-key first-key))
  5520. (org-propertize key 'face 'org-warning)
  5521. key)))
  5522. (fontify-value
  5523. (lambda (value)
  5524. ;; Fontify VALUE string.
  5525. (org-propertize value 'face 'font-lock-variable-name-face)))
  5526. ;; Prepare menu entries by extracting them from registered
  5527. ;; back-ends and sorting them by access key and by ordinal,
  5528. ;; if any.
  5529. (entries
  5530. (sort (sort (delq nil
  5531. (mapcar 'org-export-backend-menu
  5532. org-export--registered-backends))
  5533. (lambda (a b)
  5534. (let ((key-a (nth 1 a))
  5535. (key-b (nth 1 b)))
  5536. (cond ((and (numberp key-a) (numberp key-b))
  5537. (< key-a key-b))
  5538. ((numberp key-b) t)))))
  5539. 'car-less-than-car))
  5540. ;; Compute a list of allowed keys based on the first key
  5541. ;; pressed, if any. Some keys
  5542. ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
  5543. ;; available.
  5544. (allowed-keys
  5545. (nconc (list 2 22 19 6 1)
  5546. (if (not first-key) (org-uniquify (mapcar 'car entries))
  5547. (let (sub-menu)
  5548. (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
  5549. (when (eq (car entry) first-key)
  5550. (setq sub-menu (append (nth 2 entry) sub-menu))))))
  5551. (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
  5552. ((not first-key) (list ?P)))
  5553. (list ?& ?#)
  5554. (when expertp (list ??))
  5555. (list ?q)))
  5556. ;; Build the help menu for standard UI.
  5557. (help
  5558. (unless expertp
  5559. (concat
  5560. ;; Options are hard-coded.
  5561. (format "[%s] Body only: %s [%s] Visible only: %s
  5562. \[%s] Export scope: %s [%s] Force publishing: %s
  5563. \[%s] Async export: %s\n\n"
  5564. (funcall fontify-key "C-b" t)
  5565. (funcall fontify-value
  5566. (if (memq 'body options) "On " "Off"))
  5567. (funcall fontify-key "C-v" t)
  5568. (funcall fontify-value
  5569. (if (memq 'visible options) "On " "Off"))
  5570. (funcall fontify-key "C-s" t)
  5571. (funcall fontify-value
  5572. (if (memq 'subtree options) "Subtree" "Buffer "))
  5573. (funcall fontify-key "C-f" t)
  5574. (funcall fontify-value
  5575. (if (memq 'force options) "On " "Off"))
  5576. (funcall fontify-key "C-a" t)
  5577. (funcall fontify-value
  5578. (if (memq 'async options) "On " "Off")))
  5579. ;; Display registered back-end entries. When a key
  5580. ;; appears for the second time, do not create another
  5581. ;; entry, but append its sub-menu to existing menu.
  5582. (let (last-key)
  5583. (mapconcat
  5584. (lambda (entry)
  5585. (let ((top-key (car entry)))
  5586. (concat
  5587. (unless (eq top-key last-key)
  5588. (setq last-key top-key)
  5589. (format "\n[%s] %s\n"
  5590. (funcall fontify-key (char-to-string top-key))
  5591. (nth 1 entry)))
  5592. (let ((sub-menu (nth 2 entry)))
  5593. (unless (functionp sub-menu)
  5594. ;; Split sub-menu into two columns.
  5595. (let ((index -1))
  5596. (concat
  5597. (mapconcat
  5598. (lambda (sub-entry)
  5599. (incf index)
  5600. (format
  5601. (if (zerop (mod index 2)) " [%s] %-26s"
  5602. "[%s] %s\n")
  5603. (funcall fontify-key
  5604. (char-to-string (car sub-entry))
  5605. top-key)
  5606. (nth 1 sub-entry)))
  5607. sub-menu "")
  5608. (when (zerop (mod index 2)) "\n"))))))))
  5609. entries ""))
  5610. ;; Publishing menu is hard-coded.
  5611. (format "\n[%s] Publish
  5612. [%s] Current file [%s] Current project
  5613. [%s] Choose project [%s] All projects\n\n\n"
  5614. (funcall fontify-key "P")
  5615. (funcall fontify-key "f" ?P)
  5616. (funcall fontify-key "p" ?P)
  5617. (funcall fontify-key "x" ?P)
  5618. (funcall fontify-key "a" ?P))
  5619. (format "[%s] Export stack [%s] Insert template\n"
  5620. (funcall fontify-key "&" t)
  5621. (funcall fontify-key "#" t))
  5622. (format "[%s] %s"
  5623. (funcall fontify-key "q" t)
  5624. (if first-key "Main menu" "Exit")))))
  5625. ;; Build prompts for both standard and expert UI.
  5626. (standard-prompt (unless expertp "Export command: "))
  5627. (expert-prompt
  5628. (when expertp
  5629. (format
  5630. "Export command (C-%s%s%s%s%s) [%s]: "
  5631. (if (memq 'body options) (funcall fontify-key "b" t) "b")
  5632. (if (memq 'visible options) (funcall fontify-key "v" t) "v")
  5633. (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
  5634. (if (memq 'force options) (funcall fontify-key "f" t) "f")
  5635. (if (memq 'async options) (funcall fontify-key "a" t) "a")
  5636. (mapconcat (lambda (k)
  5637. ;; Strip control characters.
  5638. (unless (< k 27) (char-to-string k)))
  5639. allowed-keys "")))))
  5640. ;; With expert UI, just read key with a fancy prompt. In standard
  5641. ;; UI, display an intrusive help buffer.
  5642. (if expertp
  5643. (org-export--dispatch-action
  5644. expert-prompt allowed-keys entries options first-key expertp)
  5645. ;; At first call, create frame layout in order to display menu.
  5646. (unless (get-buffer "*Org Export Dispatcher*")
  5647. (delete-other-windows)
  5648. (org-switch-to-buffer-other-window
  5649. (get-buffer-create "*Org Export Dispatcher*"))
  5650. (setq cursor-type nil
  5651. header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
  5652. ;; Make sure that invisible cursor will not highlight square
  5653. ;; brackets.
  5654. (set-syntax-table (copy-syntax-table))
  5655. (modify-syntax-entry ?\[ "w"))
  5656. ;; At this point, the buffer containing the menu exists and is
  5657. ;; visible in the current window. So, refresh it.
  5658. (with-current-buffer "*Org Export Dispatcher*"
  5659. ;; Refresh help. Maintain display continuity by re-visiting
  5660. ;; previous window position.
  5661. (let ((pos (window-start)))
  5662. (erase-buffer)
  5663. (insert help)
  5664. (set-window-start nil pos)))
  5665. (org-fit-window-to-buffer)
  5666. (org-export--dispatch-action
  5667. standard-prompt allowed-keys entries options first-key expertp))))
  5668. (defun org-export--dispatch-action
  5669. (prompt allowed-keys entries options first-key expertp)
  5670. "Read a character from command input and act accordingly.
  5671. PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
  5672. a list of characters available at a given step in the process.
  5673. ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
  5674. EXPERTP are the same as defined in `org-export--dispatch-ui',
  5675. which see.
  5676. Toggle export options when required. Otherwise, return value is
  5677. a list with action as CAR and a list of interactive export
  5678. options as CDR."
  5679. (let (key)
  5680. ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
  5681. ;; C-p, SPC, DEL).
  5682. (while (and (setq key (read-char-exclusive prompt))
  5683. (not expertp)
  5684. (memq key '(14 16 ?\s ?\d)))
  5685. (case key
  5686. (14 (if (not (pos-visible-in-window-p (point-max)))
  5687. (ignore-errors (scroll-up 1))
  5688. (message "End of buffer")
  5689. (sit-for 1)))
  5690. (16 (if (not (pos-visible-in-window-p (point-min)))
  5691. (ignore-errors (scroll-down 1))
  5692. (message "Beginning of buffer")
  5693. (sit-for 1)))
  5694. (?\s (if (not (pos-visible-in-window-p (point-max)))
  5695. (scroll-up nil)
  5696. (message "End of buffer")
  5697. (sit-for 1)))
  5698. (?\d (if (not (pos-visible-in-window-p (point-min)))
  5699. (scroll-down nil)
  5700. (message "Beginning of buffer")
  5701. (sit-for 1)))))
  5702. (cond
  5703. ;; Ignore undefined associations.
  5704. ((not (memq key allowed-keys))
  5705. (ding)
  5706. (unless expertp (message "Invalid key") (sit-for 1))
  5707. (org-export--dispatch-ui options first-key expertp))
  5708. ;; q key at first level aborts export. At second level, cancel
  5709. ;; first key instead.
  5710. ((eq key ?q) (if (not first-key) (error "Export aborted")
  5711. (org-export--dispatch-ui options nil expertp)))
  5712. ;; Help key: Switch back to standard interface if expert UI was
  5713. ;; active.
  5714. ((eq key ??) (org-export--dispatch-ui options first-key nil))
  5715. ;; Send request for template insertion along with export scope.
  5716. ((eq key ?#) (cons 'template (memq 'subtree options)))
  5717. ;; Switch to asynchronous export stack.
  5718. ((eq key ?&) '(stack))
  5719. ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
  5720. ((memq key '(2 22 19 6 1))
  5721. (org-export--dispatch-ui
  5722. (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
  5723. (6 'force) (1 'async))))
  5724. (if (memq option options) (remq option options)
  5725. (cons option options)))
  5726. first-key expertp))
  5727. ;; Action selected: Send key and options back to
  5728. ;; `org-export-dispatch'.
  5729. ((or first-key (functionp (nth 2 (assq key entries))))
  5730. (cons (cond
  5731. ((not first-key) (nth 2 (assq key entries)))
  5732. ;; Publishing actions are hard-coded. Send a special
  5733. ;; signal to `org-export-dispatch'.
  5734. ((eq first-key ?P)
  5735. (case key
  5736. (?f 'publish-current-file)
  5737. (?p 'publish-current-project)
  5738. (?x 'publish-choose-project)
  5739. (?a 'publish-all)))
  5740. ;; Return first action associated to FIRST-KEY + KEY
  5741. ;; path. Indeed, derived backends can share the same
  5742. ;; FIRST-KEY.
  5743. (t (catch 'found
  5744. (mapc (lambda (entry)
  5745. (let ((match (assq key (nth 2 entry))))
  5746. (when match (throw 'found (nth 2 match)))))
  5747. (member (assq first-key entries) entries)))))
  5748. options))
  5749. ;; Otherwise, enter sub-menu.
  5750. (t (org-export--dispatch-ui options key expertp)))))
  5751. (provide 'ox)
  5752. ;; Local variables:
  5753. ;; generated-autoload-file: "org-loaddefs.el"
  5754. ;; End:
  5755. ;;; ox.el ends here