ox.el 246 KB

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