ox.el 246 KB

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