ox.el 226 KB

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