ox.el 245 KB

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