ox.el 246 KB

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