ox.el 246 KB

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