ox.el 251 KB

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