ox.el 235 KB

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