ox.el 245 KB

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