ox.el 247 KB

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