ox.el 225 KB

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