ox.el 248 KB

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