ox.el 262 KB

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