ox.el 246 KB

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