ox.el 246 KB

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