ox.el 225 KB

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