ox.el 245 KB

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