ox.el 247 KB

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