ox.el 264 KB

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