ox.el 264 KB

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