ox.el 246 KB

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