ox.el 244 KB

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