ox.el 247 KB

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