ox.el 246 KB

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