ox.el 247 KB

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