ox.el 245 KB

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