ox.el 246 KB

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