ox.el 246 KB

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