ox.el 248 KB

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