ox.el 247 KB

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