org-element.el 212 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992
  1. ;;; org-element.el --- Parser And Applications for Org syntax
  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. ;; Org syntax can be divided into three categories: "Greater
  19. ;; elements", "Elements" and "Objects".
  20. ;;
  21. ;; Elements are related to the structure of the document. Indeed, all
  22. ;; elements are a cover for the document: each position within belongs
  23. ;; to at least one element.
  24. ;;
  25. ;; An element always starts and ends at the beginning of a line. With
  26. ;; a few exceptions (`clock', `headline', `inlinetask', `item',
  27. ;; `planning', `node-property', `section' and `table-row' types), it
  28. ;; can also accept a fixed set of keywords as attributes. Those are
  29. ;; called "affiliated keywords" to distinguish them from other
  30. ;; keywords, which are full-fledged elements. Almost all affiliated
  31. ;; keywords are referenced in `org-element-affiliated-keywords'; the
  32. ;; others are export attributes and start with "ATTR_" prefix.
  33. ;;
  34. ;; Element containing other elements (and only elements) are called
  35. ;; greater elements. Concerned types are: `center-block', `drawer',
  36. ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
  37. ;; `item', `plain-list', `property-drawer', `quote-block', `section'
  38. ;; and `special-block'.
  39. ;;
  40. ;; Other element types are: `babel-call', `clock', `comment',
  41. ;; `comment-block', `diary-sexp', `example-block', `export-block',
  42. ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
  43. ;; `node-property', `paragraph', `planning', `src-block', `table',
  44. ;; `table-row' and `verse-block'. Among them, `paragraph' and
  45. ;; `verse-block' types can contain Org objects and plain text.
  46. ;;
  47. ;; Objects are related to document's contents. Some of them are
  48. ;; recursive. Associated types are of the following: `bold', `code',
  49. ;; `entity', `export-snippet', `footnote-reference',
  50. ;; `inline-babel-call', `inline-src-block', `italic',
  51. ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
  52. ;; `statistics-cookie', `strike-through', `subscript', `superscript',
  53. ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
  54. ;;
  55. ;; Some elements also have special properties whose value can hold
  56. ;; objects themselves (e.g. an item tag or a headline name). Such
  57. ;; values are called "secondary strings". Any object belongs to
  58. ;; either an element or a secondary string.
  59. ;;
  60. ;; Notwithstanding affiliated keywords, each greater element, element
  61. ;; and object has a fixed set of properties attached to it. Among
  62. ;; them, four are shared by all types: `:begin' and `:end', which
  63. ;; refer to the beginning and ending buffer positions of the
  64. ;; considered element or object, `:post-blank', which holds the number
  65. ;; of blank lines, or white spaces, at its end and `:parent' which
  66. ;; refers to the element or object containing it. Greater elements,
  67. ;; elements and objects containing objects will also have
  68. ;; `:contents-begin' and `:contents-end' properties to delimit
  69. ;; contents. Eventually, greater elements and elements accepting
  70. ;; affiliated keywords will have a `:post-affiliated' property,
  71. ;; referring to the buffer position after all such keywords.
  72. ;;
  73. ;; At the lowest level, a `:parent' property is also attached to any
  74. ;; string, as a text property.
  75. ;;
  76. ;; Lisp-wise, an element or an object can be represented as a list.
  77. ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
  78. ;; TYPE is a symbol describing the Org element or object.
  79. ;; PROPERTIES is the property list attached to it. See docstring of
  80. ;; appropriate parsing function to get an exhaustive
  81. ;; list.
  82. ;; CONTENTS is a list of elements, objects or raw strings contained
  83. ;; in the current element or object, when applicable.
  84. ;;
  85. ;; An Org buffer is a nested list of such elements and objects, whose
  86. ;; type is `org-data' and properties is nil.
  87. ;;
  88. ;; The first part of this file defines Org syntax, while the second
  89. ;; one provide accessors and setters functions.
  90. ;;
  91. ;; The next part implements a parser and an interpreter for each
  92. ;; element and object type in Org syntax.
  93. ;;
  94. ;; The following part creates a fully recursive buffer parser. It
  95. ;; also provides a tool to map a function to elements or objects
  96. ;; matching some criteria in the parse tree. Functions of interest
  97. ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
  98. ;; extent, `org-element-parse-secondary-string'.
  99. ;;
  100. ;; The penultimate part is the cradle of an interpreter for the
  101. ;; obtained parse tree: `org-element-interpret-data'.
  102. ;;
  103. ;; The library ends by furnishing `org-element-at-point' function, and
  104. ;; a way to give information about document structure around point
  105. ;; with `org-element-context'. A cache mechanism is also provided for
  106. ;; these functions.
  107. ;;; Code:
  108. (eval-when-compile (require 'cl))
  109. (require 'org)
  110. (require 'avl-tree)
  111. ;;; Definitions And Rules
  112. ;;
  113. ;; Define elements, greater elements and specify recursive objects,
  114. ;; along with the affiliated keywords recognized. Also set up
  115. ;; restrictions on recursive objects combinations.
  116. ;;
  117. ;; These variables really act as a control center for the parsing
  118. ;; process.
  119. (defconst org-element-paragraph-separate
  120. (concat "^\\(?:"
  121. ;; Headlines, inlinetasks.
  122. org-outline-regexp "\\|"
  123. ;; Footnote definitions.
  124. "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
  125. ;; Diary sexps.
  126. "%%(" "\\|"
  127. "[ \t]*\\(?:"
  128. ;; Empty lines.
  129. "$" "\\|"
  130. ;; Tables (any type).
  131. "\\(?:|\\|\\+-[-+]\\)" "\\|"
  132. ;; Blocks (any type), Babel calls and keywords. Note: this
  133. ;; is only an indication and need some thorough check.
  134. "#\\(?:[+ ]\\|$\\)" "\\|"
  135. ;; Drawers (any type) and fixed-width areas. This is also
  136. ;; only an indication.
  137. ":" "\\|"
  138. ;; Horizontal rules.
  139. "-\\{5,\\}[ \t]*$" "\\|"
  140. ;; LaTeX environments.
  141. "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
  142. ;; Planning and Clock lines.
  143. (regexp-opt (list org-scheduled-string
  144. org-deadline-string
  145. org-closed-string
  146. org-clock-string))
  147. "\\|"
  148. ;; Lists.
  149. (let ((term (case org-plain-list-ordered-item-terminator
  150. (?\) ")") (?. "\\.") (otherwise "[.)]")))
  151. (alpha (and org-list-allow-alphabetical "\\|[A-Za-z]")))
  152. (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
  153. "\\(?:[ \t]\\|$\\)"))
  154. "\\)\\)")
  155. "Regexp to separate paragraphs in an Org buffer.
  156. In the case of lines starting with \"#\" and \":\", this regexp
  157. is not sufficient to know if point is at a paragraph ending. See
  158. `org-element-paragraph-parser' for more information.")
  159. (defconst org-element-all-elements
  160. '(babel-call center-block clock comment comment-block diary-sexp drawer
  161. dynamic-block example-block export-block fixed-width
  162. footnote-definition headline horizontal-rule inlinetask item
  163. keyword latex-environment node-property paragraph plain-list
  164. planning property-drawer quote-block section
  165. special-block src-block table table-row verse-block)
  166. "Complete list of element types.")
  167. (defconst org-element-greater-elements
  168. '(center-block drawer dynamic-block footnote-definition headline inlinetask
  169. item plain-list property-drawer quote-block section
  170. special-block table)
  171. "List of recursive element types aka Greater Elements.")
  172. (defconst org-element-all-successors
  173. '(link export-snippet footnote-reference inline-babel-call
  174. inline-src-block latex-or-entity line-break macro plain-link
  175. radio-target statistics-cookie sub/superscript table-cell target
  176. text-markup timestamp)
  177. "Complete list of successors.")
  178. (defconst org-element-object-successor-alist
  179. '((subscript . sub/superscript) (superscript . sub/superscript)
  180. (bold . text-markup) (code . text-markup) (italic . text-markup)
  181. (strike-through . text-markup) (underline . text-markup)
  182. (verbatim . text-markup) (entity . latex-or-entity)
  183. (latex-fragment . latex-or-entity))
  184. "Alist of translations between object type and successor name.
  185. Sharing the same successor comes handy when, for example, the
  186. regexp matching one object can also match the other object.")
  187. (defconst org-element-all-objects
  188. '(bold code entity export-snippet footnote-reference inline-babel-call
  189. inline-src-block italic line-break latex-fragment link macro
  190. radio-target statistics-cookie strike-through subscript superscript
  191. table-cell target timestamp underline verbatim)
  192. "Complete list of object types.")
  193. (defconst org-element-recursive-objects
  194. '(bold italic link subscript radio-target strike-through superscript
  195. table-cell underline)
  196. "List of recursive object types.")
  197. (defvar org-element-block-name-alist
  198. '(("CENTER" . org-element-center-block-parser)
  199. ("COMMENT" . org-element-comment-block-parser)
  200. ("EXAMPLE" . org-element-example-block-parser)
  201. ("QUOTE" . org-element-quote-block-parser)
  202. ("SRC" . org-element-src-block-parser)
  203. ("VERSE" . org-element-verse-block-parser))
  204. "Alist between block names and the associated parsing function.
  205. Names must be uppercase. Any block whose name has no association
  206. is parsed with `org-element-special-block-parser'.")
  207. (defconst org-element-link-type-is-file
  208. '("file" "file+emacs" "file+sys" "docview")
  209. "List of link types equivalent to \"file\".
  210. Only these types can accept search options and an explicit
  211. application to open them.")
  212. (defconst org-element-affiliated-keywords
  213. '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
  214. "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
  215. "List of affiliated keywords as strings.
  216. By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
  217. are affiliated keywords and need not to be in this list.")
  218. (defconst org-element-keyword-translation-alist
  219. '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
  220. ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
  221. ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
  222. "Alist of usual translations for keywords.
  223. The key is the old name and the value the new one. The property
  224. holding their value will be named after the translated name.")
  225. (defconst org-element-multiple-keywords '("CAPTION" "HEADER")
  226. "List of affiliated keywords that can occur more than once in an element.
  227. Their value will be consed into a list of strings, which will be
  228. returned as the value of the property.
  229. This list is checked after translations have been applied. See
  230. `org-element-keyword-translation-alist'.
  231. By default, all keywords setting attributes (e.g., \"ATTR_LATEX\")
  232. allow multiple occurrences and need not to be in this list.")
  233. (defconst org-element-parsed-keywords '("CAPTION")
  234. "List of affiliated keywords whose value can be parsed.
  235. Their value will be stored as a secondary string: a list of
  236. strings and objects.
  237. This list is checked after translations have been applied. See
  238. `org-element-keyword-translation-alist'.")
  239. (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
  240. "List of affiliated keywords which can have a secondary value.
  241. In Org syntax, they can be written with optional square brackets
  242. before the colons. For example, RESULTS keyword can be
  243. associated to a hash value with the following:
  244. #+RESULTS[hash-string]: some-source
  245. This list is checked after translations have been applied. See
  246. `org-element-keyword-translation-alist'.")
  247. (defconst org-element-document-properties '("AUTHOR" "DATE" "TITLE")
  248. "List of properties associated to the whole document.
  249. Any keyword in this list will have its value parsed and stored as
  250. a secondary string.")
  251. (defconst org-element--affiliated-re
  252. (format "[ \t]*#\\+\\(?:%s\\):\\(?: \\|$\\)"
  253. (concat
  254. ;; Dual affiliated keywords.
  255. (format "\\(?1:%s\\)\\(?:\\[\\(.*\\)\\]\\)?"
  256. (regexp-opt org-element-dual-keywords))
  257. "\\|"
  258. ;; Regular affiliated keywords.
  259. (format "\\(?1:%s\\)"
  260. (regexp-opt
  261. (org-remove-if
  262. #'(lambda (keyword)
  263. (member keyword org-element-dual-keywords))
  264. org-element-affiliated-keywords)))
  265. "\\|"
  266. ;; Export attributes.
  267. "\\(?1:ATTR_[-_A-Za-z0-9]+\\)"))
  268. "Regexp matching any affiliated keyword.
  269. Keyword name is put in match group 1. Moreover, if keyword
  270. belongs to `org-element-dual-keywords', put the dual value in
  271. match group 2.
  272. Don't modify it, set `org-element-affiliated-keywords' instead.")
  273. (defconst org-element-object-restrictions
  274. (let* ((standard-set
  275. (remq 'plain-link (remq 'table-cell org-element-all-successors)))
  276. (standard-set-no-line-break (remq 'line-break standard-set)))
  277. `((bold ,@standard-set)
  278. (footnote-reference ,@standard-set)
  279. (headline ,@standard-set-no-line-break)
  280. (inlinetask ,@standard-set-no-line-break)
  281. (italic ,@standard-set)
  282. (item ,@standard-set-no-line-break)
  283. (keyword ,@standard-set)
  284. ;; Ignore all links excepted plain links in a link description.
  285. ;; Also ignore radio-targets and line breaks.
  286. (link export-snippet inline-babel-call inline-src-block latex-or-entity
  287. macro plain-link statistics-cookie sub/superscript text-markup)
  288. (paragraph ,@standard-set)
  289. ;; Remove any variable object from radio target as it would
  290. ;; prevent it from being properly recognized.
  291. (radio-target latex-or-entity sub/superscript text-markup)
  292. (strike-through ,@standard-set)
  293. (subscript ,@standard-set)
  294. (superscript ,@standard-set)
  295. ;; Ignore inline babel call and inline src block as formulas are
  296. ;; possible. Also ignore line breaks and statistics cookies.
  297. (table-cell link export-snippet footnote-reference latex-or-entity macro
  298. radio-target sub/superscript target text-markup timestamp)
  299. (table-row table-cell)
  300. (underline ,@standard-set)
  301. (verse-block ,@standard-set)))
  302. "Alist of objects restrictions.
  303. CAR is an element or object type containing objects and CDR is
  304. a list of successors that will be called within an element or
  305. object of such type.
  306. For example, in a `radio-target' object, one can only find
  307. entities, latex-fragments, subscript, superscript and text
  308. markup.
  309. This alist also applies to secondary string. For example, an
  310. `headline' type element doesn't directly contain objects, but
  311. still has an entry since one of its properties (`:title') does.")
  312. (defconst org-element-secondary-value-alist
  313. '((headline . :title)
  314. (inlinetask . :title)
  315. (item . :tag)
  316. (footnote-reference . :inline-definition))
  317. "Alist between element types and location of secondary value.")
  318. (defconst org-element-object-variables '(org-link-abbrev-alist-local)
  319. "List of buffer-local variables used when parsing objects.
  320. These variables are copied to the temporary buffer created by
  321. `org-export-secondary-string'.")
  322. ;;; Accessors and Setters
  323. ;;
  324. ;; Provide four accessors: `org-element-type', `org-element-property'
  325. ;; `org-element-contents' and `org-element-restriction'.
  326. ;;
  327. ;; Setter functions allow to modify elements by side effect. There is
  328. ;; `org-element-put-property', `org-element-set-contents'. These
  329. ;; low-level functions are useful to build a parse tree.
  330. ;;
  331. ;; `org-element-adopt-element', `org-element-set-element',
  332. ;; `org-element-extract-element' and `org-element-insert-before' are
  333. ;; high-level functions useful to modify a parse tree.
  334. ;;
  335. ;; `org-element-secondary-p' is a predicate used to know if a given
  336. ;; object belongs to a secondary string.
  337. (defsubst org-element-type (element)
  338. "Return type of ELEMENT.
  339. The function returns the type of the element or object provided.
  340. It can also return the following special value:
  341. `plain-text' for a string
  342. `org-data' for a complete document
  343. nil in any other case."
  344. (cond
  345. ((not (consp element)) (and (stringp element) 'plain-text))
  346. ((symbolp (car element)) (car element))))
  347. (defsubst org-element-property (property element)
  348. "Extract the value from the PROPERTY of an ELEMENT."
  349. (if (stringp element) (get-text-property 0 property element)
  350. (plist-get (nth 1 element) property)))
  351. (defsubst org-element-contents (element)
  352. "Extract contents from an ELEMENT."
  353. (cond ((not (consp element)) nil)
  354. ((symbolp (car element)) (nthcdr 2 element))
  355. (t element)))
  356. (defsubst org-element-restriction (element)
  357. "Return restriction associated to ELEMENT.
  358. ELEMENT can be an element, an object or a symbol representing an
  359. element or object type."
  360. (cdr (assq (if (symbolp element) element (org-element-type element))
  361. org-element-object-restrictions)))
  362. (defsubst org-element-put-property (element property value)
  363. "In ELEMENT set PROPERTY to VALUE.
  364. Return modified element."
  365. (if (stringp element) (org-add-props element nil property value)
  366. (setcar (cdr element) (plist-put (nth 1 element) property value))
  367. element))
  368. (defsubst org-element-set-contents (element &rest contents)
  369. "Set ELEMENT contents to CONTENTS.
  370. Return modified element."
  371. (cond ((not element) (list contents))
  372. ((not (symbolp (car element))) contents)
  373. ((cdr element) (setcdr (cdr element) contents))
  374. (t (nconc element contents))))
  375. (defun org-element-secondary-p (object)
  376. "Non-nil when OBJECT belongs to a secondary string.
  377. Return value is the property name, as a keyword, or nil."
  378. (let* ((parent (org-element-property :parent object))
  379. (property (cdr (assq (org-element-type parent)
  380. org-element-secondary-value-alist))))
  381. (and property
  382. (memq object (org-element-property property parent))
  383. property)))
  384. (defsubst org-element-adopt-elements (parent &rest children)
  385. "Append elements to the contents of another element.
  386. PARENT is an element or object. CHILDREN can be elements,
  387. objects, or a strings.
  388. The function takes care of setting `:parent' property for CHILD.
  389. Return parent element."
  390. ;; Link every child to PARENT. If PARENT is nil, it is a secondary
  391. ;; string: parent is the list itself.
  392. (mapc (lambda (child)
  393. (org-element-put-property child :parent (or parent children)))
  394. children)
  395. ;; Add CHILDREN at the end of PARENT contents.
  396. (when parent
  397. (apply 'org-element-set-contents
  398. parent
  399. (nconc (org-element-contents parent) children)))
  400. ;; Return modified PARENT element.
  401. (or parent children))
  402. (defun org-element-extract-element (element)
  403. "Extract ELEMENT from parse tree.
  404. Remove element from the parse tree by side-effect, and return it
  405. with its `:parent' property stripped out."
  406. (let ((parent (org-element-property :parent element))
  407. (secondary (org-element-secondary-p element)))
  408. (if secondary
  409. (org-element-put-property
  410. parent secondary
  411. (delq element (org-element-property secondary parent)))
  412. (apply #'org-element-set-contents
  413. parent
  414. (delq element (org-element-contents parent))))
  415. ;; Return ELEMENT with its :parent removed.
  416. (org-element-put-property element :parent nil)))
  417. (defun org-element-insert-before (element location)
  418. "Insert ELEMENT before LOCATION in parse tree.
  419. LOCATION is an element, object or string within the parse tree.
  420. Parse tree is modified by side effect."
  421. (let* ((parent (org-element-property :parent location))
  422. (property (org-element-secondary-p location))
  423. (siblings (if property (org-element-property property parent)
  424. (org-element-contents parent)))
  425. ;; Special case: LOCATION is the first element of an
  426. ;; independent secondary string (e.g. :title property). Add
  427. ;; ELEMENT in-place.
  428. (specialp (and (not property)
  429. (eq siblings parent)
  430. (eq (car parent) location))))
  431. ;; Install ELEMENT at the appropriate POSITION within SIBLINGS.
  432. (cond (specialp)
  433. ((or (null siblings) (eq (car siblings) location))
  434. (push element siblings))
  435. ((null location) (nconc siblings (list element)))
  436. (t (let ((previous (cadr (memq location (reverse siblings)))))
  437. (if (not previous)
  438. (error "No location found to insert element")
  439. (let ((next (memq previous siblings)))
  440. (setcdr next (cons element (cdr next))))))))
  441. ;; Store SIBLINGS at appropriate place in parse tree.
  442. (cond
  443. (specialp (setcdr parent (copy-sequence parent)) (setcar parent element))
  444. (property (org-element-put-property parent property siblings))
  445. (t (apply #'org-element-set-contents parent siblings)))
  446. ;; Set appropriate :parent property.
  447. (org-element-put-property element :parent parent)))
  448. (defun org-element-set-element (old new)
  449. "Replace element or object OLD with element or object NEW.
  450. The function takes care of setting `:parent' property for NEW."
  451. ;; Ensure OLD and NEW have the same parent.
  452. (org-element-put-property new :parent (org-element-property :parent old))
  453. (if (or (memq (org-element-type old) '(plain-text nil))
  454. (memq (org-element-type new) '(plain-text nil)))
  455. ;; We cannot replace OLD with NEW since one of them is not an
  456. ;; object or element. We take the long path.
  457. (progn (org-element-insert-before new old)
  458. (org-element-extract-element old))
  459. ;; Since OLD is going to be changed into NEW by side-effect, first
  460. ;; make sure that every element or object within NEW has OLD as
  461. ;; parent.
  462. (dolist (blob (org-element-contents new))
  463. (org-element-put-property blob :parent old))
  464. ;; Transfer contents.
  465. (apply #'org-element-set-contents old (org-element-contents new))
  466. ;; Overwrite OLD's properties with NEW's.
  467. (setcar (cdr old) (nth 1 new))
  468. ;; Transfer type.
  469. (setcar old (car new))))
  470. ;;; Greater elements
  471. ;;
  472. ;; For each greater element type, we define a parser and an
  473. ;; interpreter.
  474. ;;
  475. ;; A parser returns the element or object as the list described above.
  476. ;; Most of them accepts no argument. Though, exceptions exist. Hence
  477. ;; every element containing a secondary string (see
  478. ;; `org-element-secondary-value-alist') will accept an optional
  479. ;; argument to toggle parsing of that secondary string. Moreover,
  480. ;; `item' parser requires current list's structure as its first
  481. ;; element.
  482. ;;
  483. ;; An interpreter accepts two arguments: the list representation of
  484. ;; the element or object, and its contents. The latter may be nil,
  485. ;; depending on the element or object considered. It returns the
  486. ;; appropriate Org syntax, as a string.
  487. ;;
  488. ;; Parsing functions must follow the naming convention:
  489. ;; org-element-TYPE-parser, where TYPE is greater element's type, as
  490. ;; defined in `org-element-greater-elements'.
  491. ;;
  492. ;; Similarly, interpreting functions must follow the naming
  493. ;; convention: org-element-TYPE-interpreter.
  494. ;;
  495. ;; With the exception of `headline' and `item' types, greater elements
  496. ;; cannot contain other greater elements of their own type.
  497. ;;
  498. ;; Beside implementing a parser and an interpreter, adding a new
  499. ;; greater element requires to tweak `org-element--current-element'.
  500. ;; Moreover, the newly defined type must be added to both
  501. ;; `org-element-all-elements' and `org-element-greater-elements'.
  502. ;;;; Center Block
  503. (defun org-element-center-block-parser (limit affiliated)
  504. "Parse a center block.
  505. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  506. the buffer position at the beginning of the first affiliated
  507. keyword and CDR is a plist of affiliated keywords along with
  508. their value.
  509. Return a list whose CAR is `center-block' and CDR is a plist
  510. containing `:begin', `:end', `:contents-begin', `:contents-end',
  511. `:post-blank' and `:post-affiliated' keywords.
  512. Assume point is at the beginning of the block."
  513. (let ((case-fold-search t))
  514. (if (not (save-excursion
  515. (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
  516. ;; Incomplete block: parse it as a paragraph.
  517. (org-element-paragraph-parser limit affiliated)
  518. (let ((block-end-line (match-beginning 0)))
  519. (let* ((begin (car affiliated))
  520. (post-affiliated (point))
  521. ;; Empty blocks have no contents.
  522. (contents-begin (progn (forward-line)
  523. (and (< (point) block-end-line)
  524. (point))))
  525. (contents-end (and contents-begin block-end-line))
  526. (pos-before-blank (progn (goto-char block-end-line)
  527. (forward-line)
  528. (point)))
  529. (end (save-excursion
  530. (skip-chars-forward " \r\t\n" limit)
  531. (if (eobp) (point) (line-beginning-position)))))
  532. (list 'center-block
  533. (nconc
  534. (list :begin begin
  535. :end end
  536. :contents-begin contents-begin
  537. :contents-end contents-end
  538. :post-blank (count-lines pos-before-blank end)
  539. :post-affiliated post-affiliated)
  540. (cdr affiliated))))))))
  541. (defun org-element-center-block-interpreter (center-block contents)
  542. "Interpret CENTER-BLOCK element as Org syntax.
  543. CONTENTS is the contents of the element."
  544. (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
  545. ;;;; Drawer
  546. (defun org-element-drawer-parser (limit affiliated)
  547. "Parse a drawer.
  548. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  549. the buffer position at the beginning of the first affiliated
  550. keyword and CDR is a plist of affiliated keywords along with
  551. their value.
  552. Return a list whose CAR is `drawer' and CDR is a plist containing
  553. `:drawer-name', `:begin', `:end', `:contents-begin',
  554. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  555. Assume point is at beginning of drawer."
  556. (let ((case-fold-search t))
  557. (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
  558. ;; Incomplete drawer: parse it as a paragraph.
  559. (org-element-paragraph-parser limit affiliated)
  560. (save-excursion
  561. (let* ((drawer-end-line (match-beginning 0))
  562. (name (progn (looking-at org-drawer-regexp)
  563. (org-match-string-no-properties 1)))
  564. (begin (car affiliated))
  565. (post-affiliated (point))
  566. ;; Empty drawers have no contents.
  567. (contents-begin (progn (forward-line)
  568. (and (< (point) drawer-end-line)
  569. (point))))
  570. (contents-end (and contents-begin drawer-end-line))
  571. (pos-before-blank (progn (goto-char drawer-end-line)
  572. (forward-line)
  573. (point)))
  574. (end (progn (skip-chars-forward " \r\t\n" limit)
  575. (if (eobp) (point) (line-beginning-position)))))
  576. (list 'drawer
  577. (nconc
  578. (list :begin begin
  579. :end end
  580. :drawer-name name
  581. :contents-begin contents-begin
  582. :contents-end contents-end
  583. :post-blank (count-lines pos-before-blank end)
  584. :post-affiliated post-affiliated)
  585. (cdr affiliated))))))))
  586. (defun org-element-drawer-interpreter (drawer contents)
  587. "Interpret DRAWER element as Org syntax.
  588. CONTENTS is the contents of the element."
  589. (format ":%s:\n%s:END:"
  590. (org-element-property :drawer-name drawer)
  591. contents))
  592. ;;;; Dynamic Block
  593. (defun org-element-dynamic-block-parser (limit affiliated)
  594. "Parse a dynamic block.
  595. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  596. the buffer position at the beginning of the first affiliated
  597. keyword and CDR is a plist of affiliated keywords along with
  598. their value.
  599. Return a list whose CAR is `dynamic-block' and CDR is a plist
  600. containing `:block-name', `:begin', `:end', `:contents-begin',
  601. `:contents-end', `:arguments', `:post-blank' and
  602. `:post-affiliated' keywords.
  603. Assume point is at beginning of dynamic block."
  604. (let ((case-fold-search t))
  605. (if (not (save-excursion
  606. (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
  607. ;; Incomplete block: parse it as a paragraph.
  608. (org-element-paragraph-parser limit affiliated)
  609. (let ((block-end-line (match-beginning 0)))
  610. (save-excursion
  611. (let* ((name (progn (looking-at org-dblock-start-re)
  612. (org-match-string-no-properties 1)))
  613. (arguments (org-match-string-no-properties 3))
  614. (begin (car affiliated))
  615. (post-affiliated (point))
  616. ;; Empty blocks have no contents.
  617. (contents-begin (progn (forward-line)
  618. (and (< (point) block-end-line)
  619. (point))))
  620. (contents-end (and contents-begin block-end-line))
  621. (pos-before-blank (progn (goto-char block-end-line)
  622. (forward-line)
  623. (point)))
  624. (end (progn (skip-chars-forward " \r\t\n" limit)
  625. (if (eobp) (point) (line-beginning-position)))))
  626. (list 'dynamic-block
  627. (nconc
  628. (list :begin begin
  629. :end end
  630. :block-name name
  631. :arguments arguments
  632. :contents-begin contents-begin
  633. :contents-end contents-end
  634. :post-blank (count-lines pos-before-blank end)
  635. :post-affiliated post-affiliated)
  636. (cdr affiliated)))))))))
  637. (defun org-element-dynamic-block-interpreter (dynamic-block contents)
  638. "Interpret DYNAMIC-BLOCK element as Org syntax.
  639. CONTENTS is the contents of the element."
  640. (format "#+BEGIN: %s%s\n%s#+END:"
  641. (org-element-property :block-name dynamic-block)
  642. (let ((args (org-element-property :arguments dynamic-block)))
  643. (and args (concat " " args)))
  644. contents))
  645. ;;;; Footnote Definition
  646. (defun org-element-footnote-definition-parser (limit affiliated)
  647. "Parse a footnote definition.
  648. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  649. the buffer position at the beginning of the first affiliated
  650. keyword and CDR is a plist of affiliated keywords along with
  651. their value.
  652. Return a list whose CAR is `footnote-definition' and CDR is
  653. a plist containing `:label', `:begin' `:end', `:contents-begin',
  654. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  655. Assume point is at the beginning of the footnote definition."
  656. (save-excursion
  657. (let* ((label (progn (looking-at org-footnote-definition-re)
  658. (org-match-string-no-properties 1)))
  659. (begin (car affiliated))
  660. (post-affiliated (point))
  661. (ending (save-excursion
  662. (if (progn
  663. (end-of-line)
  664. (re-search-forward
  665. (concat org-outline-regexp-bol "\\|"
  666. org-footnote-definition-re "\\|"
  667. "^\\([ \t]*\n\\)\\{2,\\}") limit 'move))
  668. (match-beginning 0)
  669. (point))))
  670. (contents-begin (progn
  671. (search-forward "]")
  672. (skip-chars-forward " \r\t\n" ending)
  673. (cond ((= (point) ending) nil)
  674. ((= (line-beginning-position) begin) (point))
  675. (t (line-beginning-position)))))
  676. (contents-end (and contents-begin ending))
  677. (end (progn (goto-char ending)
  678. (skip-chars-forward " \r\t\n" limit)
  679. (if (eobp) (point) (line-beginning-position)))))
  680. (list 'footnote-definition
  681. (nconc
  682. (list :label label
  683. :begin begin
  684. :end end
  685. :contents-begin contents-begin
  686. :contents-end contents-end
  687. :post-blank (count-lines ending end)
  688. :post-affiliated post-affiliated)
  689. (cdr affiliated))))))
  690. (defun org-element-footnote-definition-interpreter (footnote-definition contents)
  691. "Interpret FOOTNOTE-DEFINITION element as Org syntax.
  692. CONTENTS is the contents of the footnote-definition."
  693. (concat (format "[%s]" (org-element-property :label footnote-definition))
  694. " "
  695. contents))
  696. ;;;; Headline
  697. (defun org-element-headline-parser (limit &optional raw-secondary-p)
  698. "Parse a headline.
  699. Return a list whose CAR is `headline' and CDR is a plist
  700. containing `:raw-value', `:title', `:alt-title', `:begin',
  701. `:end', `:pre-blank', `:contents-begin' and `:contents-end',
  702. `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
  703. `:scheduled', `:deadline', `:closed', `:archivedp', `:commentedp'
  704. and `:footnote-section-p' keywords.
  705. The plist also contains any property set in the property drawer,
  706. with its name in upper cases and colons added at the
  707. beginning (e.g., `:CUSTOM_ID').
  708. When RAW-SECONDARY-P is non-nil, headline's title will not be
  709. parsed as a secondary string, but as a plain string instead.
  710. Assume point is at beginning of the headline."
  711. (save-excursion
  712. (let* ((components (org-heading-components))
  713. (level (nth 1 components))
  714. (todo (nth 2 components))
  715. (todo-type
  716. (and todo (if (member todo org-done-keywords) 'done 'todo)))
  717. (tags (let ((raw-tags (nth 5 components)))
  718. (and raw-tags (org-split-string raw-tags ":"))))
  719. (raw-value (or (nth 4 components) ""))
  720. (commentedp
  721. (let ((case-fold-search nil))
  722. (string-match (format "^%s\\( \\|$\\)" org-comment-string)
  723. raw-value)))
  724. (archivedp (member org-archive-tag tags))
  725. (footnote-section-p (and org-footnote-section
  726. (string= org-footnote-section raw-value)))
  727. ;; Upcase property names. It avoids confusion between
  728. ;; properties obtained through property drawer and default
  729. ;; properties from the parser (e.g. `:end' and :END:)
  730. (standard-props
  731. (let (plist)
  732. (mapc
  733. (lambda (p)
  734. (setq plist
  735. (plist-put plist
  736. (intern (concat ":" (upcase (car p))))
  737. (cdr p))))
  738. (org-entry-properties nil 'standard))
  739. plist))
  740. (time-props
  741. ;; Read time properties on the line below the headline.
  742. (save-excursion
  743. (when (progn (forward-line)
  744. (looking-at org-planning-or-clock-line-re))
  745. (let ((end (line-end-position)) plist)
  746. (while (re-search-forward
  747. org-keyword-time-not-clock-regexp end t)
  748. (goto-char (match-end 1))
  749. (skip-chars-forward " \t")
  750. (let ((keyword (match-string 1))
  751. (time (org-element-timestamp-parser)))
  752. (cond ((equal keyword org-scheduled-string)
  753. (setq plist (plist-put plist :scheduled time)))
  754. ((equal keyword org-deadline-string)
  755. (setq plist (plist-put plist :deadline time)))
  756. (t (setq plist (plist-put plist :closed time))))))
  757. plist))))
  758. (begin (point))
  759. (end (save-excursion (goto-char (org-end-of-subtree t t))))
  760. (pos-after-head (progn (forward-line) (point)))
  761. (contents-begin (save-excursion
  762. (skip-chars-forward " \r\t\n" end)
  763. (and (/= (point) end) (line-beginning-position))))
  764. (contents-end (and contents-begin
  765. (progn (goto-char end)
  766. (skip-chars-backward " \r\t\n")
  767. (forward-line)
  768. (point)))))
  769. ;; Clean RAW-VALUE from any comment string.
  770. (when commentedp
  771. (let ((case-fold-search nil))
  772. (setq raw-value
  773. (replace-regexp-in-string
  774. (concat (regexp-quote org-comment-string) "\\(?: \\|$\\)")
  775. ""
  776. raw-value))))
  777. ;; Clean TAGS from archive tag, if any.
  778. (when archivedp (setq tags (delete org-archive-tag tags)))
  779. (let ((headline
  780. (list 'headline
  781. (nconc
  782. (list :raw-value raw-value
  783. :begin begin
  784. :end end
  785. :pre-blank
  786. (if (not contents-begin) 0
  787. (count-lines pos-after-head contents-begin))
  788. :contents-begin contents-begin
  789. :contents-end contents-end
  790. :level level
  791. :priority (nth 3 components)
  792. :tags tags
  793. :todo-keyword todo
  794. :todo-type todo-type
  795. :post-blank (count-lines
  796. (if (not contents-end) pos-after-head
  797. (goto-char contents-end)
  798. (forward-line)
  799. (point))
  800. end)
  801. :footnote-section-p footnote-section-p
  802. :archivedp archivedp
  803. :commentedp commentedp)
  804. time-props
  805. standard-props))))
  806. (let ((alt-title (org-element-property :ALT_TITLE headline)))
  807. (when alt-title
  808. (org-element-put-property
  809. headline :alt-title
  810. (if raw-secondary-p alt-title
  811. (org-element-parse-secondary-string
  812. alt-title (org-element-restriction 'headline) headline)))))
  813. (org-element-put-property
  814. headline :title
  815. (if raw-secondary-p raw-value
  816. (org-element-parse-secondary-string
  817. raw-value (org-element-restriction 'headline) headline)))))))
  818. (defun org-element-headline-interpreter (headline contents)
  819. "Interpret HEADLINE element as Org syntax.
  820. CONTENTS is the contents of the element."
  821. (let* ((level (org-element-property :level headline))
  822. (todo (org-element-property :todo-keyword headline))
  823. (priority (org-element-property :priority headline))
  824. (title (org-element-interpret-data
  825. (org-element-property :title headline)))
  826. (tags (let ((tag-list (if (org-element-property :archivedp headline)
  827. (cons org-archive-tag
  828. (org-element-property :tags headline))
  829. (org-element-property :tags headline))))
  830. (and tag-list
  831. (format ":%s:" (mapconcat 'identity tag-list ":")))))
  832. (commentedp (org-element-property :commentedp headline))
  833. (pre-blank (or (org-element-property :pre-blank headline) 0))
  834. (heading (concat (make-string (org-reduced-level level) ?*)
  835. (and todo (concat " " todo))
  836. (and commentedp (concat " " org-comment-string))
  837. (and priority
  838. (format " [#%s]" (char-to-string priority)))
  839. (cond ((and org-footnote-section
  840. (org-element-property
  841. :footnote-section-p headline))
  842. (concat " " org-footnote-section))
  843. (title (concat " " title))))))
  844. (concat heading
  845. ;; Align tags.
  846. (when tags
  847. (cond
  848. ((zerop org-tags-column) (format " %s" tags))
  849. ((< org-tags-column 0)
  850. (concat
  851. (make-string
  852. (max (- (+ org-tags-column (length heading) (length tags))) 1)
  853. ? )
  854. tags))
  855. (t
  856. (concat
  857. (make-string (max (- org-tags-column (length heading)) 1) ? )
  858. tags))))
  859. (make-string (1+ pre-blank) 10)
  860. contents)))
  861. ;;;; Inlinetask
  862. (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
  863. "Parse an inline task.
  864. Return a list whose CAR is `inlinetask' and CDR is a plist
  865. containing `:title', `:begin', `:end', `:contents-begin' and
  866. `:contents-end', `:level', `:priority', `:raw-value', `:tags',
  867. `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
  868. `:closed' and `:post-blank' keywords.
  869. The plist also contains any property set in the property drawer,
  870. with its name in upper cases and colons added at the
  871. beginning (e.g., `:CUSTOM_ID').
  872. When optional argument RAW-SECONDARY-P is non-nil, inline-task's
  873. title will not be parsed as a secondary string, but as a plain
  874. string instead.
  875. Assume point is at beginning of the inline task."
  876. (save-excursion
  877. (let* ((begin (point))
  878. (components (org-heading-components))
  879. (todo (nth 2 components))
  880. (todo-type (and todo
  881. (if (member todo org-done-keywords) 'done 'todo)))
  882. (tags (let ((raw-tags (nth 5 components)))
  883. (and raw-tags (org-split-string raw-tags ":"))))
  884. (raw-value (or (nth 4 components) ""))
  885. ;; Upcase property names. It avoids confusion between
  886. ;; properties obtained through property drawer and default
  887. ;; properties from the parser (e.g. `:end' and :END:)
  888. (standard-props
  889. (let (plist)
  890. (mapc
  891. (lambda (p)
  892. (setq plist
  893. (plist-put plist
  894. (intern (concat ":" (upcase (car p))))
  895. (cdr p))))
  896. (org-entry-properties nil 'standard))
  897. plist))
  898. (time-props
  899. ;; Read time properties on the line below the inlinetask
  900. ;; opening string.
  901. (save-excursion
  902. (when (progn (forward-line)
  903. (looking-at org-planning-or-clock-line-re))
  904. (let ((end (line-end-position)) plist)
  905. (while (re-search-forward
  906. org-keyword-time-not-clock-regexp end t)
  907. (goto-char (match-end 1))
  908. (skip-chars-forward " \t")
  909. (let ((keyword (match-string 1))
  910. (time (org-element-timestamp-parser)))
  911. (cond ((equal keyword org-scheduled-string)
  912. (setq plist (plist-put plist :scheduled time)))
  913. ((equal keyword org-deadline-string)
  914. (setq plist (plist-put plist :deadline time)))
  915. (t (setq plist (plist-put plist :closed time))))))
  916. plist))))
  917. (task-end (save-excursion
  918. (end-of-line)
  919. (and (re-search-forward org-outline-regexp-bol limit t)
  920. (org-looking-at-p "END[ \t]*$")
  921. (line-beginning-position))))
  922. (contents-begin (progn (forward-line)
  923. (and task-end (< (point) task-end) (point))))
  924. (contents-end (and contents-begin task-end))
  925. (before-blank (if (not task-end) (point)
  926. (goto-char task-end)
  927. (forward-line)
  928. (point)))
  929. (end (progn (skip-chars-forward " \r\t\n" limit)
  930. (if (eobp) (point) (line-beginning-position))))
  931. (inlinetask
  932. (list 'inlinetask
  933. (nconc
  934. (list :raw-value raw-value
  935. :begin begin
  936. :end end
  937. :contents-begin contents-begin
  938. :contents-end contents-end
  939. :level (nth 1 components)
  940. :priority (nth 3 components)
  941. :tags tags
  942. :todo-keyword todo
  943. :todo-type todo-type
  944. :post-blank (count-lines before-blank end))
  945. time-props
  946. standard-props))))
  947. (org-element-put-property
  948. inlinetask :title
  949. (if raw-secondary-p raw-value
  950. (org-element-parse-secondary-string
  951. raw-value
  952. (org-element-restriction 'inlinetask)
  953. inlinetask))))))
  954. (defun org-element-inlinetask-interpreter (inlinetask contents)
  955. "Interpret INLINETASK element as Org syntax.
  956. CONTENTS is the contents of inlinetask."
  957. (let* ((level (org-element-property :level inlinetask))
  958. (todo (org-element-property :todo-keyword inlinetask))
  959. (priority (org-element-property :priority inlinetask))
  960. (title (org-element-interpret-data
  961. (org-element-property :title inlinetask)))
  962. (tags (let ((tag-list (org-element-property :tags inlinetask)))
  963. (and tag-list
  964. (format ":%s:" (mapconcat 'identity tag-list ":")))))
  965. (task (concat (make-string level ?*)
  966. (and todo (concat " " todo))
  967. (and priority
  968. (format " [#%s]" (char-to-string priority)))
  969. (and title (concat " " title)))))
  970. (concat task
  971. ;; Align tags.
  972. (when tags
  973. (cond
  974. ((zerop org-tags-column) (format " %s" tags))
  975. ((< org-tags-column 0)
  976. (concat
  977. (make-string
  978. (max (- (+ org-tags-column (length task) (length tags))) 1)
  979. ? )
  980. tags))
  981. (t
  982. (concat
  983. (make-string (max (- org-tags-column (length task)) 1) ? )
  984. tags))))
  985. ;; Prefer degenerate inlinetasks when there are no
  986. ;; contents.
  987. (when contents
  988. (concat "\n"
  989. contents
  990. (make-string level ?*) " END")))))
  991. ;;;; Item
  992. (defun org-element-item-parser (limit struct &optional raw-secondary-p)
  993. "Parse an item.
  994. STRUCT is the structure of the plain list.
  995. Return a list whose CAR is `item' and CDR is a plist containing
  996. `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
  997. `:checkbox', `:counter', `:tag', `:structure' and `:post-blank'
  998. keywords.
  999. When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
  1000. any, will not be parsed as a secondary string, but as a plain
  1001. string instead.
  1002. Assume point is at the beginning of the item."
  1003. (save-excursion
  1004. (beginning-of-line)
  1005. (looking-at org-list-full-item-re)
  1006. (let* ((begin (point))
  1007. (bullet (org-match-string-no-properties 1))
  1008. (checkbox (let ((box (org-match-string-no-properties 3)))
  1009. (cond ((equal "[ ]" box) 'off)
  1010. ((equal "[X]" box) 'on)
  1011. ((equal "[-]" box) 'trans))))
  1012. (counter (let ((c (org-match-string-no-properties 2)))
  1013. (save-match-data
  1014. (cond
  1015. ((not c) nil)
  1016. ((string-match "[A-Za-z]" c)
  1017. (- (string-to-char (upcase (match-string 0 c)))
  1018. 64))
  1019. ((string-match "[0-9]+" c)
  1020. (string-to-number (match-string 0 c)))))))
  1021. (end (progn (goto-char (nth 6 (assq (point) struct)))
  1022. (unless (bolp) (forward-line))
  1023. (point)))
  1024. (contents-begin
  1025. (progn (goto-char
  1026. ;; Ignore tags in un-ordered lists: they are just
  1027. ;; a part of item's body.
  1028. (if (and (match-beginning 4)
  1029. (save-match-data (string-match "[.)]" bullet)))
  1030. (match-beginning 4)
  1031. (match-end 0)))
  1032. (skip-chars-forward " \r\t\n" limit)
  1033. ;; If first line isn't empty, contents really start
  1034. ;; at the text after item's meta-data.
  1035. (if (= (point-at-bol) begin) (point) (point-at-bol))))
  1036. (contents-end (progn (goto-char end)
  1037. (skip-chars-backward " \r\t\n")
  1038. (forward-line)
  1039. (point)))
  1040. (item
  1041. (list 'item
  1042. (list :bullet bullet
  1043. :begin begin
  1044. :end end
  1045. ;; CONTENTS-BEGIN and CONTENTS-END may be
  1046. ;; mixed up in the case of an empty item
  1047. ;; separated from the next by a blank line.
  1048. ;; Thus ensure the former is always the
  1049. ;; smallest.
  1050. :contents-begin (min contents-begin contents-end)
  1051. :contents-end (max contents-begin contents-end)
  1052. :checkbox checkbox
  1053. :counter counter
  1054. :structure struct
  1055. :post-blank (count-lines contents-end end)))))
  1056. (org-element-put-property
  1057. item :tag
  1058. (let ((raw-tag (org-list-get-tag begin struct)))
  1059. (and raw-tag
  1060. (if raw-secondary-p raw-tag
  1061. (org-element-parse-secondary-string
  1062. raw-tag (org-element-restriction 'item) item))))))))
  1063. (defun org-element-item-interpreter (item contents)
  1064. "Interpret ITEM element as Org syntax.
  1065. CONTENTS is the contents of the element."
  1066. (let* ((bullet (let ((bullet (org-element-property :bullet item)))
  1067. (org-list-bullet-string
  1068. (cond ((not (string-match "[0-9a-zA-Z]" bullet)) "- ")
  1069. ((eq org-plain-list-ordered-item-terminator ?\)) "1)")
  1070. (t "1.")))))
  1071. (checkbox (org-element-property :checkbox item))
  1072. (counter (org-element-property :counter item))
  1073. (tag (let ((tag (org-element-property :tag item)))
  1074. (and tag (org-element-interpret-data tag))))
  1075. ;; Compute indentation.
  1076. (ind (make-string (length bullet) 32))
  1077. (item-starts-with-par-p
  1078. (eq (org-element-type (car (org-element-contents item)))
  1079. 'paragraph)))
  1080. ;; Indent contents.
  1081. (concat
  1082. bullet
  1083. (and counter (format "[@%d] " counter))
  1084. (case checkbox
  1085. (on "[X] ")
  1086. (off "[ ] ")
  1087. (trans "[-] "))
  1088. (and tag (format "%s :: " tag))
  1089. (when contents
  1090. (let ((contents (replace-regexp-in-string
  1091. "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
  1092. (if item-starts-with-par-p (org-trim contents)
  1093. (concat "\n" contents)))))))
  1094. ;;;; Plain List
  1095. (defun org-element--list-struct (limit)
  1096. ;; Return structure of list at point. Internal function. See
  1097. ;; `org-list-struct' for details.
  1098. (let ((case-fold-search t)
  1099. (top-ind limit)
  1100. (item-re (org-item-re))
  1101. (inlinetask-re (and (featurep 'org-inlinetask) "^\\*+ "))
  1102. items struct)
  1103. (save-excursion
  1104. (catch 'exit
  1105. (while t
  1106. (cond
  1107. ;; At limit: end all items.
  1108. ((>= (point) limit)
  1109. (throw 'exit
  1110. (let ((end (progn (skip-chars-backward " \r\t\n")
  1111. (forward-line)
  1112. (point))))
  1113. (dolist (item items (sort (nconc items struct)
  1114. 'car-less-than-car))
  1115. (setcar (nthcdr 6 item) end)))))
  1116. ;; At list end: end all items.
  1117. ((looking-at org-list-end-re)
  1118. (throw 'exit (dolist (item items (sort (nconc items struct)
  1119. 'car-less-than-car))
  1120. (setcar (nthcdr 6 item) (point)))))
  1121. ;; At a new item: end previous sibling.
  1122. ((looking-at item-re)
  1123. (let ((ind (save-excursion (skip-chars-forward " \t")
  1124. (current-column))))
  1125. (setq top-ind (min top-ind ind))
  1126. (while (and items (<= ind (nth 1 (car items))))
  1127. (let ((item (pop items)))
  1128. (setcar (nthcdr 6 item) (point))
  1129. (push item struct)))
  1130. (push (progn (looking-at org-list-full-item-re)
  1131. (let ((bullet (match-string-no-properties 1)))
  1132. (list (point)
  1133. ind
  1134. bullet
  1135. (match-string-no-properties 2) ; counter
  1136. (match-string-no-properties 3) ; checkbox
  1137. ;; Description tag.
  1138. (and (save-match-data
  1139. (string-match "[-+*]" bullet))
  1140. (match-string-no-properties 4))
  1141. ;; Ending position, unknown so far.
  1142. nil)))
  1143. items))
  1144. (forward-line 1))
  1145. ;; Skip empty lines.
  1146. ((looking-at "^[ \t]*$") (forward-line))
  1147. ;; Skip inline tasks and blank lines along the way.
  1148. ((and inlinetask-re (looking-at inlinetask-re))
  1149. (forward-line)
  1150. (let ((origin (point)))
  1151. (when (re-search-forward inlinetask-re limit t)
  1152. (if (org-looking-at-p "END[ \t]*$") (forward-line)
  1153. (goto-char origin)))))
  1154. ;; At some text line. Check if it ends any previous item.
  1155. (t
  1156. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  1157. (when (<= ind top-ind)
  1158. (skip-chars-backward " \r\t\n")
  1159. (forward-line))
  1160. (while (<= ind (nth 1 (car items)))
  1161. (let ((item (pop items)))
  1162. (setcar (nthcdr 6 item) (line-beginning-position))
  1163. (push item struct)
  1164. (unless items
  1165. (throw 'exit (sort struct 'car-less-than-car))))))
  1166. ;; Skip blocks (any type) and drawers contents.
  1167. (cond
  1168. ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
  1169. (re-search-forward
  1170. (format "^[ \t]*#\\+END%s[ \t]*$" (match-string 1))
  1171. limit t)))
  1172. ((and (looking-at org-drawer-regexp)
  1173. (re-search-forward "^[ \t]*:END:[ \t]*$" limit t))))
  1174. (forward-line))))))))
  1175. (defun org-element-plain-list-parser (limit affiliated structure)
  1176. "Parse a plain list.
  1177. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1178. the buffer position at the beginning of the first affiliated
  1179. keyword and CDR is a plist of affiliated keywords along with
  1180. their value. STRUCTURE is the structure of the plain list being
  1181. parsed.
  1182. Return a list whose CAR is `plain-list' and CDR is a plist
  1183. containing `:type', `:begin', `:end', `:contents-begin' and
  1184. `:contents-end', `:structure', `:post-blank' and
  1185. `:post-affiliated' keywords.
  1186. Assume point is at the beginning of the list."
  1187. (save-excursion
  1188. (let* ((struct (or structure (org-element--list-struct limit)))
  1189. (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered)
  1190. ((nth 5 (assq (point) struct)) 'descriptive)
  1191. (t 'unordered)))
  1192. (contents-begin (point))
  1193. (begin (car affiliated))
  1194. (contents-end (let* ((item (assq contents-begin struct))
  1195. (ind (nth 1 item))
  1196. (pos (nth 6 item)))
  1197. (while (and (setq item (assq pos struct))
  1198. (= (nth 1 item) ind))
  1199. (setq pos (nth 6 item)))
  1200. pos))
  1201. (end (progn (goto-char contents-end)
  1202. (skip-chars-forward " \r\t\n" limit)
  1203. (if (= (point) limit) limit (line-beginning-position)))))
  1204. ;; Return value.
  1205. (list 'plain-list
  1206. (nconc
  1207. (list :type type
  1208. :begin begin
  1209. :end end
  1210. :contents-begin contents-begin
  1211. :contents-end contents-end
  1212. :structure struct
  1213. :post-blank (count-lines contents-end end)
  1214. :post-affiliated contents-begin)
  1215. (cdr affiliated))))))
  1216. (defun org-element-plain-list-interpreter (plain-list contents)
  1217. "Interpret PLAIN-LIST element as Org syntax.
  1218. CONTENTS is the contents of the element."
  1219. (with-temp-buffer
  1220. (insert contents)
  1221. (goto-char (point-min))
  1222. (org-list-repair)
  1223. (buffer-string)))
  1224. ;;;; Property Drawer
  1225. (defun org-element-property-drawer-parser (limit affiliated)
  1226. "Parse a property drawer.
  1227. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1228. the buffer position at the beginning of the first affiliated
  1229. keyword and CDR is a plist of affiliated keywords along with
  1230. their value.
  1231. Return a list whose CAR is `property-drawer' and CDR is a plist
  1232. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1233. `:post-blank' and `:post-affiliated' keywords.
  1234. Assume point is at the beginning of the property drawer."
  1235. (save-excursion
  1236. (let ((case-fold-search t))
  1237. (if (not (save-excursion
  1238. (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
  1239. ;; Incomplete drawer: parse it as a paragraph.
  1240. (org-element-paragraph-parser limit affiliated)
  1241. (save-excursion
  1242. (let* ((drawer-end-line (match-beginning 0))
  1243. (begin (car affiliated))
  1244. (post-affiliated (point))
  1245. (contents-begin (progn (forward-line)
  1246. (and (< (point) drawer-end-line)
  1247. (point))))
  1248. (contents-end (and contents-begin drawer-end-line))
  1249. (pos-before-blank (progn (goto-char drawer-end-line)
  1250. (forward-line)
  1251. (point)))
  1252. (end (progn (skip-chars-forward " \r\t\n" limit)
  1253. (if (eobp) (point) (line-beginning-position)))))
  1254. (list 'property-drawer
  1255. (nconc
  1256. (list :begin begin
  1257. :end end
  1258. :contents-begin contents-begin
  1259. :contents-end contents-end
  1260. :post-blank (count-lines pos-before-blank end)
  1261. :post-affiliated post-affiliated)
  1262. (cdr affiliated)))))))))
  1263. (defun org-element-property-drawer-interpreter (property-drawer contents)
  1264. "Interpret PROPERTY-DRAWER element as Org syntax.
  1265. CONTENTS is the properties within the drawer."
  1266. (format ":PROPERTIES:\n%s:END:" contents))
  1267. ;;;; Quote Block
  1268. (defun org-element-quote-block-parser (limit affiliated)
  1269. "Parse a quote block.
  1270. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1271. the buffer position at the beginning of the first affiliated
  1272. keyword and CDR is a plist of affiliated keywords along with
  1273. their value.
  1274. Return a list whose CAR is `quote-block' and CDR is a plist
  1275. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1276. `:post-blank' and `:post-affiliated' keywords.
  1277. Assume point is at the beginning of the block."
  1278. (let ((case-fold-search t))
  1279. (if (not (save-excursion
  1280. (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
  1281. ;; Incomplete block: parse it as a paragraph.
  1282. (org-element-paragraph-parser limit affiliated)
  1283. (let ((block-end-line (match-beginning 0)))
  1284. (save-excursion
  1285. (let* ((begin (car affiliated))
  1286. (post-affiliated (point))
  1287. ;; Empty blocks have no contents.
  1288. (contents-begin (progn (forward-line)
  1289. (and (< (point) block-end-line)
  1290. (point))))
  1291. (contents-end (and contents-begin block-end-line))
  1292. (pos-before-blank (progn (goto-char block-end-line)
  1293. (forward-line)
  1294. (point)))
  1295. (end (progn (skip-chars-forward " \r\t\n" limit)
  1296. (if (eobp) (point) (line-beginning-position)))))
  1297. (list 'quote-block
  1298. (nconc
  1299. (list :begin begin
  1300. :end end
  1301. :contents-begin contents-begin
  1302. :contents-end contents-end
  1303. :post-blank (count-lines pos-before-blank end)
  1304. :post-affiliated post-affiliated)
  1305. (cdr affiliated)))))))))
  1306. (defun org-element-quote-block-interpreter (quote-block contents)
  1307. "Interpret QUOTE-BLOCK element as Org syntax.
  1308. CONTENTS is the contents of the element."
  1309. (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
  1310. ;;;; Section
  1311. (defun org-element-section-parser (limit)
  1312. "Parse a section.
  1313. LIMIT bounds the search.
  1314. Return a list whose CAR is `section' and CDR is a plist
  1315. containing `:begin', `:end', `:contents-begin', `contents-end'
  1316. and `:post-blank' keywords."
  1317. (save-excursion
  1318. ;; Beginning of section is the beginning of the first non-blank
  1319. ;; line after previous headline.
  1320. (let ((begin (point))
  1321. (end (progn (org-with-limited-levels (outline-next-heading))
  1322. (point)))
  1323. (pos-before-blank (progn (skip-chars-backward " \r\t\n")
  1324. (forward-line)
  1325. (point))))
  1326. (list 'section
  1327. (list :begin begin
  1328. :end end
  1329. :contents-begin begin
  1330. :contents-end pos-before-blank
  1331. :post-blank (count-lines pos-before-blank end))))))
  1332. (defun org-element-section-interpreter (section contents)
  1333. "Interpret SECTION element as Org syntax.
  1334. CONTENTS is the contents of the element."
  1335. contents)
  1336. ;;;; Special Block
  1337. (defun org-element-special-block-parser (limit affiliated)
  1338. "Parse a special block.
  1339. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1340. the buffer position at the beginning of the first affiliated
  1341. keyword and CDR is a plist of affiliated keywords along with
  1342. their value.
  1343. Return a list whose CAR is `special-block' and CDR is a plist
  1344. containing `:type', `:begin', `:end', `:contents-begin',
  1345. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  1346. Assume point is at the beginning of the block."
  1347. (let* ((case-fold-search t)
  1348. (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1349. (upcase (match-string-no-properties 1)))))
  1350. (if (not (save-excursion
  1351. (re-search-forward
  1352. (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type))
  1353. limit t)))
  1354. ;; Incomplete block: parse it as a paragraph.
  1355. (org-element-paragraph-parser limit affiliated)
  1356. (let ((block-end-line (match-beginning 0)))
  1357. (save-excursion
  1358. (let* ((begin (car affiliated))
  1359. (post-affiliated (point))
  1360. ;; Empty blocks have no contents.
  1361. (contents-begin (progn (forward-line)
  1362. (and (< (point) block-end-line)
  1363. (point))))
  1364. (contents-end (and contents-begin block-end-line))
  1365. (pos-before-blank (progn (goto-char block-end-line)
  1366. (forward-line)
  1367. (point)))
  1368. (end (progn (skip-chars-forward " \r\t\n" limit)
  1369. (if (eobp) (point) (line-beginning-position)))))
  1370. (list 'special-block
  1371. (nconc
  1372. (list :type type
  1373. :begin begin
  1374. :end end
  1375. :contents-begin contents-begin
  1376. :contents-end contents-end
  1377. :post-blank (count-lines pos-before-blank end)
  1378. :post-affiliated post-affiliated)
  1379. (cdr affiliated)))))))))
  1380. (defun org-element-special-block-interpreter (special-block contents)
  1381. "Interpret SPECIAL-BLOCK element as Org syntax.
  1382. CONTENTS is the contents of the element."
  1383. (let ((block-type (org-element-property :type special-block)))
  1384. (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
  1385. ;;; Elements
  1386. ;;
  1387. ;; For each element, a parser and an interpreter are also defined.
  1388. ;; Both follow the same naming convention used for greater elements.
  1389. ;;
  1390. ;; Also, as for greater elements, adding a new element type is done
  1391. ;; through the following steps: implement a parser and an interpreter,
  1392. ;; tweak `org-element--current-element' so that it recognizes the new
  1393. ;; type and add that new type to `org-element-all-elements'.
  1394. ;;
  1395. ;; As a special case, when the newly defined type is a block type,
  1396. ;; `org-element-block-name-alist' has to be modified accordingly.
  1397. ;;;; Babel Call
  1398. (defun org-element-babel-call-parser (limit affiliated)
  1399. "Parse a babel call.
  1400. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1401. the buffer position at the beginning of the first affiliated
  1402. keyword and CDR is a plist of affiliated keywords along with
  1403. their value.
  1404. Return a list whose CAR is `babel-call' and CDR is a plist
  1405. containing `:begin', `:end', `:value', `:post-blank' and
  1406. `:post-affiliated' as keywords."
  1407. (save-excursion
  1408. (let ((begin (car affiliated))
  1409. (post-affiliated (point))
  1410. (value (progn (let ((case-fold-search t))
  1411. (re-search-forward "call:[ \t]*" nil t))
  1412. (buffer-substring-no-properties (point)
  1413. (line-end-position))))
  1414. (pos-before-blank (progn (forward-line) (point)))
  1415. (end (progn (skip-chars-forward " \r\t\n" limit)
  1416. (if (eobp) (point) (line-beginning-position)))))
  1417. (list 'babel-call
  1418. (nconc
  1419. (list :begin begin
  1420. :end end
  1421. :value value
  1422. :post-blank (count-lines pos-before-blank end)
  1423. :post-affiliated post-affiliated)
  1424. (cdr affiliated))))))
  1425. (defun org-element-babel-call-interpreter (babel-call contents)
  1426. "Interpret BABEL-CALL element as Org syntax.
  1427. CONTENTS is nil."
  1428. (concat "#+CALL: " (org-element-property :value babel-call)))
  1429. ;;;; Clock
  1430. (defun org-element-clock-parser (limit)
  1431. "Parse a clock.
  1432. LIMIT bounds the search.
  1433. Return a list whose CAR is `clock' and CDR is a plist containing
  1434. `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
  1435. as keywords."
  1436. (save-excursion
  1437. (let* ((case-fold-search nil)
  1438. (begin (point))
  1439. (value (progn (search-forward org-clock-string (line-end-position) t)
  1440. (skip-chars-forward " \t")
  1441. (org-element-timestamp-parser)))
  1442. (duration (and (search-forward " => " (line-end-position) t)
  1443. (progn (skip-chars-forward " \t")
  1444. (looking-at "\\(\\S-+\\)[ \t]*$"))
  1445. (org-match-string-no-properties 1)))
  1446. (status (if duration 'closed 'running))
  1447. (post-blank (let ((before-blank (progn (forward-line) (point))))
  1448. (skip-chars-forward " \r\t\n" limit)
  1449. (skip-chars-backward " \t")
  1450. (unless (bolp) (end-of-line))
  1451. (count-lines before-blank (point))))
  1452. (end (point)))
  1453. (list 'clock
  1454. (list :status status
  1455. :value value
  1456. :duration duration
  1457. :begin begin
  1458. :end end
  1459. :post-blank post-blank)))))
  1460. (defun org-element-clock-interpreter (clock contents)
  1461. "Interpret CLOCK element as Org syntax.
  1462. CONTENTS is nil."
  1463. (concat org-clock-string " "
  1464. (org-element-timestamp-interpreter
  1465. (org-element-property :value clock) nil)
  1466. (let ((duration (org-element-property :duration clock)))
  1467. (and duration
  1468. (concat " => "
  1469. (apply 'format
  1470. "%2s:%02s"
  1471. (org-split-string duration ":")))))))
  1472. ;;;; Comment
  1473. (defun org-element-comment-parser (limit affiliated)
  1474. "Parse a comment.
  1475. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1476. the buffer position at the beginning of the first affiliated
  1477. keyword and CDR is a plist of affiliated keywords along with
  1478. their value.
  1479. Return a list whose CAR is `comment' and CDR is a plist
  1480. containing `:begin', `:end', `:value', `:post-blank',
  1481. `:post-affiliated' keywords.
  1482. Assume point is at comment beginning."
  1483. (save-excursion
  1484. (let* ((begin (car affiliated))
  1485. (post-affiliated (point))
  1486. (value (prog2 (looking-at "[ \t]*# ?")
  1487. (buffer-substring-no-properties
  1488. (match-end 0) (line-end-position))
  1489. (forward-line)))
  1490. (com-end
  1491. ;; Get comments ending.
  1492. (progn
  1493. (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
  1494. ;; Accumulate lines without leading hash and first
  1495. ;; whitespace.
  1496. (setq value
  1497. (concat value
  1498. "\n"
  1499. (buffer-substring-no-properties
  1500. (match-end 0) (line-end-position))))
  1501. (forward-line))
  1502. (point)))
  1503. (end (progn (goto-char com-end)
  1504. (skip-chars-forward " \r\t\n" limit)
  1505. (if (eobp) (point) (line-beginning-position)))))
  1506. (list 'comment
  1507. (nconc
  1508. (list :begin begin
  1509. :end end
  1510. :value value
  1511. :post-blank (count-lines com-end end)
  1512. :post-affiliated post-affiliated)
  1513. (cdr affiliated))))))
  1514. (defun org-element-comment-interpreter (comment contents)
  1515. "Interpret COMMENT element as Org syntax.
  1516. CONTENTS is nil."
  1517. (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
  1518. ;;;; Comment Block
  1519. (defun org-element-comment-block-parser (limit affiliated)
  1520. "Parse an export block.
  1521. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1522. the buffer position at the beginning of the first affiliated
  1523. keyword and CDR is a plist of affiliated keywords along with
  1524. their value.
  1525. Return a list whose CAR is `comment-block' and CDR is a plist
  1526. containing `:begin', `:end', `:value', `:post-blank' and
  1527. `:post-affiliated' keywords.
  1528. Assume point is at comment block beginning."
  1529. (let ((case-fold-search t))
  1530. (if (not (save-excursion
  1531. (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
  1532. ;; Incomplete block: parse it as a paragraph.
  1533. (org-element-paragraph-parser limit affiliated)
  1534. (let ((contents-end (match-beginning 0)))
  1535. (save-excursion
  1536. (let* ((begin (car affiliated))
  1537. (post-affiliated (point))
  1538. (contents-begin (progn (forward-line) (point)))
  1539. (pos-before-blank (progn (goto-char contents-end)
  1540. (forward-line)
  1541. (point)))
  1542. (end (progn (skip-chars-forward " \r\t\n" limit)
  1543. (if (eobp) (point) (line-beginning-position))))
  1544. (value (buffer-substring-no-properties
  1545. contents-begin contents-end)))
  1546. (list 'comment-block
  1547. (nconc
  1548. (list :begin begin
  1549. :end end
  1550. :value value
  1551. :post-blank (count-lines pos-before-blank end)
  1552. :post-affiliated post-affiliated)
  1553. (cdr affiliated)))))))))
  1554. (defun org-element-comment-block-interpreter (comment-block contents)
  1555. "Interpret COMMENT-BLOCK element as Org syntax.
  1556. CONTENTS is nil."
  1557. (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
  1558. (org-remove-indentation (org-element-property :value comment-block))))
  1559. ;;;; Diary Sexp
  1560. (defun org-element-diary-sexp-parser (limit affiliated)
  1561. "Parse a diary sexp.
  1562. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1563. the buffer position at the beginning of the first affiliated
  1564. keyword and CDR is a plist of affiliated keywords along with
  1565. their value.
  1566. Return a list whose CAR is `diary-sexp' and CDR is a plist
  1567. containing `:begin', `:end', `:value', `:post-blank' and
  1568. `:post-affiliated' keywords."
  1569. (save-excursion
  1570. (let ((begin (car affiliated))
  1571. (post-affiliated (point))
  1572. (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
  1573. (org-match-string-no-properties 1)))
  1574. (pos-before-blank (progn (forward-line) (point)))
  1575. (end (progn (skip-chars-forward " \r\t\n" limit)
  1576. (if (eobp) (point) (line-beginning-position)))))
  1577. (list 'diary-sexp
  1578. (nconc
  1579. (list :value value
  1580. :begin begin
  1581. :end end
  1582. :post-blank (count-lines pos-before-blank end)
  1583. :post-affiliated post-affiliated)
  1584. (cdr affiliated))))))
  1585. (defun org-element-diary-sexp-interpreter (diary-sexp contents)
  1586. "Interpret DIARY-SEXP as Org syntax.
  1587. CONTENTS is nil."
  1588. (org-element-property :value diary-sexp))
  1589. ;;;; Example Block
  1590. (defun org-element-example-block-parser (limit affiliated)
  1591. "Parse an example block.
  1592. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1593. the buffer position at the beginning of the first affiliated
  1594. keyword and CDR is a plist of affiliated keywords along with
  1595. their value.
  1596. Return a list whose CAR is `example-block' and CDR is a plist
  1597. containing `:begin', `:end', `:number-lines', `:preserve-indent',
  1598. `:retain-labels', `:use-labels', `:label-fmt', `:switches',
  1599. `:value', `:post-blank' and `:post-affiliated' keywords."
  1600. (let ((case-fold-search t))
  1601. (if (not (save-excursion
  1602. (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
  1603. ;; Incomplete block: parse it as a paragraph.
  1604. (org-element-paragraph-parser limit affiliated)
  1605. (let ((contents-end (match-beginning 0)))
  1606. (save-excursion
  1607. (let* ((switches
  1608. (progn
  1609. (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
  1610. (org-match-string-no-properties 1)))
  1611. ;; Switches analysis
  1612. (number-lines
  1613. (cond ((not switches) nil)
  1614. ((string-match "-n\\>" switches) 'new)
  1615. ((string-match "+n\\>" switches) 'continued)))
  1616. (preserve-indent
  1617. (and switches (string-match "-i\\>" switches)))
  1618. ;; Should labels be retained in (or stripped from) example
  1619. ;; blocks?
  1620. (retain-labels
  1621. (or (not switches)
  1622. (not (string-match "-r\\>" switches))
  1623. (and number-lines (string-match "-k\\>" switches))))
  1624. ;; What should code-references use - labels or
  1625. ;; line-numbers?
  1626. (use-labels
  1627. (or (not switches)
  1628. (and retain-labels
  1629. (not (string-match "-k\\>" switches)))))
  1630. (label-fmt
  1631. (and switches
  1632. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  1633. (match-string 1 switches)))
  1634. ;; Standard block parsing.
  1635. (begin (car affiliated))
  1636. (post-affiliated (point))
  1637. (block-ind (progn (skip-chars-forward " \t") (current-column)))
  1638. (contents-begin (progn (forward-line) (point)))
  1639. (value (org-element-remove-indentation
  1640. (org-unescape-code-in-string
  1641. (buffer-substring-no-properties
  1642. contents-begin contents-end))
  1643. block-ind))
  1644. (pos-before-blank (progn (goto-char contents-end)
  1645. (forward-line)
  1646. (point)))
  1647. (end (progn (skip-chars-forward " \r\t\n" limit)
  1648. (if (eobp) (point) (line-beginning-position)))))
  1649. (list 'example-block
  1650. (nconc
  1651. (list :begin begin
  1652. :end end
  1653. :value value
  1654. :switches switches
  1655. :number-lines number-lines
  1656. :preserve-indent preserve-indent
  1657. :retain-labels retain-labels
  1658. :use-labels use-labels
  1659. :label-fmt label-fmt
  1660. :post-blank (count-lines pos-before-blank end)
  1661. :post-affiliated post-affiliated)
  1662. (cdr affiliated)))))))))
  1663. (defun org-element-example-block-interpreter (example-block contents)
  1664. "Interpret EXAMPLE-BLOCK element as Org syntax.
  1665. CONTENTS is nil."
  1666. (let ((switches (org-element-property :switches example-block))
  1667. (value (org-element-property :value example-block)))
  1668. (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
  1669. (org-escape-code-in-string
  1670. (if (or org-src-preserve-indentation
  1671. (org-element-property :preserve-indent example-block))
  1672. value
  1673. (org-element-remove-indentation value)))
  1674. "#+END_EXAMPLE")))
  1675. ;;;; Export Block
  1676. (defun org-element-export-block-parser (limit affiliated)
  1677. "Parse an export block.
  1678. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1679. the buffer position at the beginning of the first affiliated
  1680. keyword and CDR is a plist of affiliated keywords along with
  1681. their value.
  1682. Return a list whose CAR is `export-block' and CDR is a plist
  1683. containing `:begin', `:end', `:type', `:value', `:post-blank' and
  1684. `:post-affiliated' keywords.
  1685. Assume point is at export-block beginning."
  1686. (let* ((case-fold-search t)
  1687. (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1688. (upcase (org-match-string-no-properties 1)))))
  1689. (if (not (save-excursion
  1690. (re-search-forward
  1691. (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
  1692. ;; Incomplete block: parse it as a paragraph.
  1693. (org-element-paragraph-parser limit affiliated)
  1694. (let ((contents-end (match-beginning 0)))
  1695. (save-excursion
  1696. (let* ((begin (car affiliated))
  1697. (post-affiliated (point))
  1698. (contents-begin (progn (forward-line) (point)))
  1699. (pos-before-blank (progn (goto-char contents-end)
  1700. (forward-line)
  1701. (point)))
  1702. (end (progn (skip-chars-forward " \r\t\n" limit)
  1703. (if (eobp) (point) (line-beginning-position))))
  1704. (value (buffer-substring-no-properties contents-begin
  1705. contents-end)))
  1706. (list 'export-block
  1707. (nconc
  1708. (list :begin begin
  1709. :end end
  1710. :type type
  1711. :value value
  1712. :post-blank (count-lines pos-before-blank end)
  1713. :post-affiliated post-affiliated)
  1714. (cdr affiliated)))))))))
  1715. (defun org-element-export-block-interpreter (export-block contents)
  1716. "Interpret EXPORT-BLOCK element as Org syntax.
  1717. CONTENTS is nil."
  1718. (let ((type (org-element-property :type export-block)))
  1719. (concat (format "#+BEGIN_%s\n" type)
  1720. (org-element-property :value export-block)
  1721. (format "#+END_%s" type))))
  1722. ;;;; Fixed-width
  1723. (defun org-element-fixed-width-parser (limit affiliated)
  1724. "Parse a fixed-width section.
  1725. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1726. the buffer position at the beginning of the first affiliated
  1727. keyword and CDR is a plist of affiliated keywords along with
  1728. their value.
  1729. Return a list whose CAR is `fixed-width' and CDR is a plist
  1730. containing `:begin', `:end', `:value', `:post-blank' and
  1731. `:post-affiliated' keywords.
  1732. Assume point is at the beginning of the fixed-width area."
  1733. (save-excursion
  1734. (let* ((begin (car affiliated))
  1735. (post-affiliated (point))
  1736. value
  1737. (end-area
  1738. (progn
  1739. (while (and (< (point) limit)
  1740. (looking-at "[ \t]*:\\( \\|$\\)"))
  1741. ;; Accumulate text without starting colons.
  1742. (setq value
  1743. (concat value
  1744. (buffer-substring-no-properties
  1745. (match-end 0) (point-at-eol))
  1746. "\n"))
  1747. (forward-line))
  1748. (point)))
  1749. (end (progn (skip-chars-forward " \r\t\n" limit)
  1750. (if (eobp) (point) (line-beginning-position)))))
  1751. (list 'fixed-width
  1752. (nconc
  1753. (list :begin begin
  1754. :end end
  1755. :value value
  1756. :post-blank (count-lines end-area end)
  1757. :post-affiliated post-affiliated)
  1758. (cdr affiliated))))))
  1759. (defun org-element-fixed-width-interpreter (fixed-width contents)
  1760. "Interpret FIXED-WIDTH element as Org syntax.
  1761. CONTENTS is nil."
  1762. (let ((value (org-element-property :value fixed-width)))
  1763. (and value
  1764. (replace-regexp-in-string
  1765. "^" ": "
  1766. (if (string-match "\n\\'" value) (substring value 0 -1) value)))))
  1767. ;;;; Horizontal Rule
  1768. (defun org-element-horizontal-rule-parser (limit affiliated)
  1769. "Parse an horizontal rule.
  1770. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1771. the buffer position at the beginning of the first affiliated
  1772. keyword and CDR is a plist of affiliated keywords along with
  1773. their value.
  1774. Return a list whose CAR is `horizontal-rule' and CDR is a plist
  1775. containing `:begin', `:end', `:post-blank' and `:post-affiliated'
  1776. keywords."
  1777. (save-excursion
  1778. (let ((begin (car affiliated))
  1779. (post-affiliated (point))
  1780. (post-hr (progn (forward-line) (point)))
  1781. (end (progn (skip-chars-forward " \r\t\n" limit)
  1782. (if (eobp) (point) (line-beginning-position)))))
  1783. (list 'horizontal-rule
  1784. (nconc
  1785. (list :begin begin
  1786. :end end
  1787. :post-blank (count-lines post-hr end)
  1788. :post-affiliated post-affiliated)
  1789. (cdr affiliated))))))
  1790. (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
  1791. "Interpret HORIZONTAL-RULE element as Org syntax.
  1792. CONTENTS is nil."
  1793. "-----")
  1794. ;;;; Keyword
  1795. (defun org-element-keyword-parser (limit affiliated)
  1796. "Parse a keyword at point.
  1797. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1798. the buffer position at the beginning of the first affiliated
  1799. keyword and CDR is a plist of affiliated keywords along with
  1800. their value.
  1801. Return a list whose CAR is `keyword' and CDR is a plist
  1802. containing `:key', `:value', `:begin', `:end', `:post-blank' and
  1803. `:post-affiliated' keywords."
  1804. (save-excursion
  1805. ;; An orphaned affiliated keyword is considered as a regular
  1806. ;; keyword. In this case AFFILIATED is nil, so we take care of
  1807. ;; this corner case.
  1808. (let ((begin (or (car affiliated) (point)))
  1809. (post-affiliated (point))
  1810. (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
  1811. (upcase (org-match-string-no-properties 1))))
  1812. (value (org-trim (buffer-substring-no-properties
  1813. (match-end 0) (point-at-eol))))
  1814. (pos-before-blank (progn (forward-line) (point)))
  1815. (end (progn (skip-chars-forward " \r\t\n" limit)
  1816. (if (eobp) (point) (line-beginning-position)))))
  1817. (list 'keyword
  1818. (nconc
  1819. (list :key key
  1820. :value value
  1821. :begin begin
  1822. :end end
  1823. :post-blank (count-lines pos-before-blank end)
  1824. :post-affiliated post-affiliated)
  1825. (cdr affiliated))))))
  1826. (defun org-element-keyword-interpreter (keyword contents)
  1827. "Interpret KEYWORD element as Org syntax.
  1828. CONTENTS is nil."
  1829. (format "#+%s: %s"
  1830. (org-element-property :key keyword)
  1831. (org-element-property :value keyword)))
  1832. ;;;; Latex Environment
  1833. (defun org-element-latex-environment-parser (limit affiliated)
  1834. "Parse a LaTeX environment.
  1835. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1836. the buffer position at the beginning of the first affiliated
  1837. keyword and CDR is a plist of affiliated keywords along with
  1838. their value.
  1839. Return a list whose CAR is `latex-environment' and CDR is a plist
  1840. containing `:begin', `:end', `:value', `:post-blank' and
  1841. `:post-affiliated' keywords.
  1842. Assume point is at the beginning of the latex environment."
  1843. (save-excursion
  1844. (let ((case-fold-search t)
  1845. (code-begin (point)))
  1846. (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
  1847. (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
  1848. (regexp-quote (match-string 1)))
  1849. limit t))
  1850. ;; Incomplete latex environment: parse it as a paragraph.
  1851. (org-element-paragraph-parser limit affiliated)
  1852. (let* ((code-end (progn (forward-line) (point)))
  1853. (begin (car affiliated))
  1854. (value (buffer-substring-no-properties code-begin code-end))
  1855. (end (progn (skip-chars-forward " \r\t\n" limit)
  1856. (if (eobp) (point) (line-beginning-position)))))
  1857. (list 'latex-environment
  1858. (nconc
  1859. (list :begin begin
  1860. :end end
  1861. :value value
  1862. :post-blank (count-lines code-end end)
  1863. :post-affiliated code-begin)
  1864. (cdr affiliated))))))))
  1865. (defun org-element-latex-environment-interpreter (latex-environment contents)
  1866. "Interpret LATEX-ENVIRONMENT element as Org syntax.
  1867. CONTENTS is nil."
  1868. (org-element-property :value latex-environment))
  1869. ;;;; Node Property
  1870. (defun org-element-node-property-parser (limit)
  1871. "Parse a node-property at point.
  1872. LIMIT bounds the search.
  1873. Return a list whose CAR is `node-property' and CDR is a plist
  1874. containing `:key', `:value', `:begin', `:end' and `:post-blank'
  1875. keywords."
  1876. (save-excursion
  1877. (looking-at org-property-re)
  1878. (let ((case-fold-search t)
  1879. (begin (point))
  1880. (key (org-match-string-no-properties 2))
  1881. (value (org-match-string-no-properties 3))
  1882. (pos-before-blank (progn (forward-line) (point)))
  1883. (end (progn (skip-chars-forward " \r\t\n" limit)
  1884. (if (eobp) (point) (point-at-bol)))))
  1885. (list 'node-property
  1886. (list :key key
  1887. :value value
  1888. :begin begin
  1889. :end end
  1890. :post-blank (count-lines pos-before-blank end))))))
  1891. (defun org-element-node-property-interpreter (node-property contents)
  1892. "Interpret NODE-PROPERTY element as Org syntax.
  1893. CONTENTS is nil."
  1894. (format org-property-format
  1895. (format ":%s:" (org-element-property :key node-property))
  1896. (org-element-property :value node-property)))
  1897. ;;;; Paragraph
  1898. (defun org-element-paragraph-parser (limit affiliated)
  1899. "Parse a paragraph.
  1900. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1901. the buffer position at the beginning of the first affiliated
  1902. keyword and CDR is a plist of affiliated keywords along with
  1903. their value.
  1904. Return a list whose CAR is `paragraph' and CDR is a plist
  1905. containing `:begin', `:end', `:contents-begin' and
  1906. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  1907. Assume point is at the beginning of the paragraph."
  1908. (save-excursion
  1909. (let* ((begin (car affiliated))
  1910. (contents-begin (point))
  1911. (before-blank
  1912. (let ((case-fold-search t))
  1913. (end-of-line)
  1914. (if (not (re-search-forward
  1915. org-element-paragraph-separate limit 'm))
  1916. limit
  1917. ;; A matching `org-element-paragraph-separate' is not
  1918. ;; necessarily the end of the paragraph. In
  1919. ;; particular, lines starting with # or : as a first
  1920. ;; non-space character are ambiguous. We have to
  1921. ;; check if they are valid Org syntax (e.g., not an
  1922. ;; incomplete keyword).
  1923. (beginning-of-line)
  1924. (while (not
  1925. (or
  1926. ;; There's no ambiguity for other symbols or
  1927. ;; empty lines: stop here.
  1928. (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
  1929. ;; Stop at valid fixed-width areas.
  1930. (looking-at "[ \t]*:\\(?: \\|$\\)")
  1931. ;; Stop at drawers.
  1932. (and (looking-at org-drawer-regexp)
  1933. (save-excursion
  1934. (re-search-forward
  1935. "^[ \t]*:END:[ \t]*$" limit t)))
  1936. ;; Stop at valid comments.
  1937. (looking-at "[ \t]*#\\(?: \\|$\\)")
  1938. ;; Stop at valid dynamic blocks.
  1939. (and (looking-at org-dblock-start-re)
  1940. (save-excursion
  1941. (re-search-forward
  1942. "^[ \t]*#\\+END:?[ \t]*$" limit t)))
  1943. ;; Stop at valid blocks.
  1944. (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1945. (save-excursion
  1946. (re-search-forward
  1947. (format "^[ \t]*#\\+END_%s[ \t]*$"
  1948. (regexp-quote
  1949. (org-match-string-no-properties 1)))
  1950. limit t)))
  1951. ;; Stop at valid latex environments.
  1952. (and (looking-at
  1953. "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
  1954. (save-excursion
  1955. (re-search-forward
  1956. (format "^[ \t]*\\\\end{%s}[ \t]*$"
  1957. (regexp-quote
  1958. (org-match-string-no-properties 1)))
  1959. limit t)))
  1960. ;; Stop at valid keywords.
  1961. (looking-at "[ \t]*#\\+\\S-+:")
  1962. ;; Skip everything else.
  1963. (not
  1964. (progn
  1965. (end-of-line)
  1966. (re-search-forward org-element-paragraph-separate
  1967. limit 'm)))))
  1968. (beginning-of-line)))
  1969. (if (= (point) limit) limit
  1970. (goto-char (line-beginning-position)))))
  1971. (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
  1972. (forward-line)
  1973. (point)))
  1974. (end (progn (skip-chars-forward " \r\t\n" limit)
  1975. (if (eobp) (point) (line-beginning-position)))))
  1976. (list 'paragraph
  1977. (nconc
  1978. (list :begin begin
  1979. :end end
  1980. :contents-begin contents-begin
  1981. :contents-end contents-end
  1982. :post-blank (count-lines before-blank end)
  1983. :post-affiliated contents-begin)
  1984. (cdr affiliated))))))
  1985. (defun org-element-paragraph-interpreter (paragraph contents)
  1986. "Interpret PARAGRAPH element as Org syntax.
  1987. CONTENTS is the contents of the element."
  1988. contents)
  1989. ;;;; Planning
  1990. (defun org-element-planning-parser (limit)
  1991. "Parse a planning.
  1992. LIMIT bounds the search.
  1993. Return a list whose CAR is `planning' and CDR is a plist
  1994. containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
  1995. and `:post-blank' keywords."
  1996. (save-excursion
  1997. (let* ((case-fold-search nil)
  1998. (begin (point))
  1999. (post-blank (let ((before-blank (progn (forward-line) (point))))
  2000. (skip-chars-forward " \r\t\n" limit)
  2001. (skip-chars-backward " \t")
  2002. (unless (bolp) (end-of-line))
  2003. (count-lines before-blank (point))))
  2004. (end (point))
  2005. closed deadline scheduled)
  2006. (goto-char begin)
  2007. (while (re-search-forward org-keyword-time-not-clock-regexp end t)
  2008. (goto-char (match-end 1))
  2009. (skip-chars-forward " \t" end)
  2010. (let ((keyword (match-string 1))
  2011. (time (org-element-timestamp-parser)))
  2012. (cond ((equal keyword org-closed-string) (setq closed time))
  2013. ((equal keyword org-deadline-string) (setq deadline time))
  2014. (t (setq scheduled time)))))
  2015. (list 'planning
  2016. (list :closed closed
  2017. :deadline deadline
  2018. :scheduled scheduled
  2019. :begin begin
  2020. :end end
  2021. :post-blank post-blank)))))
  2022. (defun org-element-planning-interpreter (planning contents)
  2023. "Interpret PLANNING element as Org syntax.
  2024. CONTENTS is nil."
  2025. (mapconcat
  2026. 'identity
  2027. (delq nil
  2028. (list (let ((deadline (org-element-property :deadline planning)))
  2029. (when deadline
  2030. (concat org-deadline-string " "
  2031. (org-element-timestamp-interpreter deadline nil))))
  2032. (let ((scheduled (org-element-property :scheduled planning)))
  2033. (when scheduled
  2034. (concat org-scheduled-string " "
  2035. (org-element-timestamp-interpreter scheduled nil))))
  2036. (let ((closed (org-element-property :closed planning)))
  2037. (when closed
  2038. (concat org-closed-string " "
  2039. (org-element-timestamp-interpreter closed nil))))))
  2040. " "))
  2041. ;;;; Src Block
  2042. (defun org-element-src-block-parser (limit affiliated)
  2043. "Parse a src block.
  2044. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  2045. the buffer position at the beginning of the first affiliated
  2046. keyword and CDR is a plist of affiliated keywords along with
  2047. their value.
  2048. Return a list whose CAR is `src-block' and CDR is a plist
  2049. containing `:language', `:switches', `:parameters', `:begin',
  2050. `:end', `:number-lines', `:retain-labels', `:use-labels',
  2051. `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
  2052. `:post-affiliated' keywords.
  2053. Assume point is at the beginning of the block."
  2054. (let ((case-fold-search t))
  2055. (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
  2056. limit t)))
  2057. ;; Incomplete block: parse it as a paragraph.
  2058. (org-element-paragraph-parser limit affiliated)
  2059. (let ((contents-end (match-beginning 0)))
  2060. (save-excursion
  2061. (let* ((begin (car affiliated))
  2062. (post-affiliated (point))
  2063. ;; Get language as a string.
  2064. (language
  2065. (progn
  2066. (looking-at
  2067. (concat "^[ \t]*#\\+BEGIN_SRC"
  2068. "\\(?: +\\(\\S-+\\)\\)?"
  2069. "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
  2070. "\\(.*\\)[ \t]*$"))
  2071. (org-match-string-no-properties 1)))
  2072. ;; Get switches.
  2073. (switches (org-match-string-no-properties 2))
  2074. ;; Get parameters.
  2075. (parameters (org-match-string-no-properties 3))
  2076. ;; Switches analysis
  2077. (number-lines
  2078. (cond ((not switches) nil)
  2079. ((string-match "-n\\>" switches) 'new)
  2080. ((string-match "+n\\>" switches) 'continued)))
  2081. (preserve-indent (and switches
  2082. (string-match "-i\\>" switches)))
  2083. (label-fmt
  2084. (and switches
  2085. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2086. (match-string 1 switches)))
  2087. ;; Should labels be retained in (or stripped from)
  2088. ;; src blocks?
  2089. (retain-labels
  2090. (or (not switches)
  2091. (not (string-match "-r\\>" switches))
  2092. (and number-lines (string-match "-k\\>" switches))))
  2093. ;; What should code-references use - labels or
  2094. ;; line-numbers?
  2095. (use-labels
  2096. (or (not switches)
  2097. (and retain-labels
  2098. (not (string-match "-k\\>" switches)))))
  2099. ;; Indentation.
  2100. (block-ind (progn (skip-chars-forward " \t") (current-column)))
  2101. ;; Retrieve code.
  2102. (value (org-element-remove-indentation
  2103. (org-unescape-code-in-string
  2104. (buffer-substring-no-properties
  2105. (progn (forward-line) (point)) contents-end))
  2106. block-ind))
  2107. (pos-before-blank (progn (goto-char contents-end)
  2108. (forward-line)
  2109. (point)))
  2110. ;; Get position after ending blank lines.
  2111. (end (progn (skip-chars-forward " \r\t\n" limit)
  2112. (if (eobp) (point) (line-beginning-position)))))
  2113. (list 'src-block
  2114. (nconc
  2115. (list :language language
  2116. :switches (and (org-string-nw-p switches)
  2117. (org-trim switches))
  2118. :parameters (and (org-string-nw-p parameters)
  2119. (org-trim parameters))
  2120. :begin begin
  2121. :end end
  2122. :number-lines number-lines
  2123. :preserve-indent preserve-indent
  2124. :retain-labels retain-labels
  2125. :use-labels use-labels
  2126. :label-fmt label-fmt
  2127. :value value
  2128. :post-blank (count-lines pos-before-blank end)
  2129. :post-affiliated post-affiliated)
  2130. (cdr affiliated)))))))))
  2131. (defun org-element-src-block-interpreter (src-block contents)
  2132. "Interpret SRC-BLOCK element as Org syntax.
  2133. CONTENTS is nil."
  2134. (let ((lang (org-element-property :language src-block))
  2135. (switches (org-element-property :switches src-block))
  2136. (params (org-element-property :parameters src-block))
  2137. (value
  2138. (let ((val (org-element-property :value src-block)))
  2139. (cond
  2140. ((or org-src-preserve-indentation
  2141. (org-element-property :preserve-indent src-block))
  2142. val)
  2143. ((zerop org-edit-src-content-indentation) val)
  2144. (t
  2145. (let ((ind (make-string org-edit-src-content-indentation ?\s)))
  2146. (replace-regexp-in-string
  2147. "\\(^\\)[ \t]*\\S-" ind val nil nil 1)))))))
  2148. (concat (format "#+BEGIN_SRC%s\n"
  2149. (concat (and lang (concat " " lang))
  2150. (and switches (concat " " switches))
  2151. (and params (concat " " params))))
  2152. (org-escape-code-in-string value)
  2153. "#+END_SRC")))
  2154. ;;;; Table
  2155. (defun org-element-table-parser (limit affiliated)
  2156. "Parse a table at point.
  2157. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  2158. the buffer position at the beginning of the first affiliated
  2159. keyword and CDR is a plist of affiliated keywords along with
  2160. their value.
  2161. Return a list whose CAR is `table' and CDR is a plist containing
  2162. `:begin', `:end', `:tblfm', `:type', `:contents-begin',
  2163. `:contents-end', `:value', `:post-blank' and `:post-affiliated'
  2164. keywords.
  2165. Assume point is at the beginning of the table."
  2166. (save-excursion
  2167. (let* ((case-fold-search t)
  2168. (table-begin (point))
  2169. (type (if (org-at-table.el-p) 'table.el 'org))
  2170. (begin (car affiliated))
  2171. (table-end
  2172. (if (re-search-forward org-table-any-border-regexp limit 'm)
  2173. (goto-char (match-beginning 0))
  2174. (point)))
  2175. (tblfm (let (acc)
  2176. (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
  2177. (push (org-match-string-no-properties 1) acc)
  2178. (forward-line))
  2179. acc))
  2180. (pos-before-blank (point))
  2181. (end (progn (skip-chars-forward " \r\t\n" limit)
  2182. (if (eobp) (point) (line-beginning-position)))))
  2183. (list 'table
  2184. (nconc
  2185. (list :begin begin
  2186. :end end
  2187. :type type
  2188. :tblfm tblfm
  2189. ;; Only `org' tables have contents. `table.el' tables
  2190. ;; use a `:value' property to store raw table as
  2191. ;; a string.
  2192. :contents-begin (and (eq type 'org) table-begin)
  2193. :contents-end (and (eq type 'org) table-end)
  2194. :value (and (eq type 'table.el)
  2195. (buffer-substring-no-properties
  2196. table-begin table-end))
  2197. :post-blank (count-lines pos-before-blank end)
  2198. :post-affiliated table-begin)
  2199. (cdr affiliated))))))
  2200. (defun org-element-table-interpreter (table contents)
  2201. "Interpret TABLE element as Org syntax.
  2202. CONTENTS is nil."
  2203. (if (eq (org-element-property :type table) 'table.el)
  2204. (org-remove-indentation (org-element-property :value table))
  2205. (concat (with-temp-buffer (insert contents)
  2206. (org-table-align)
  2207. (buffer-string))
  2208. (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
  2209. (reverse (org-element-property :tblfm table))
  2210. "\n"))))
  2211. ;;;; Table Row
  2212. (defun org-element-table-row-parser (limit)
  2213. "Parse table row at point.
  2214. LIMIT bounds the search.
  2215. Return a list whose CAR is `table-row' and CDR is a plist
  2216. containing `:begin', `:end', `:contents-begin', `:contents-end',
  2217. `:type' and `:post-blank' keywords."
  2218. (save-excursion
  2219. (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
  2220. (begin (point))
  2221. ;; A table rule has no contents. In that case, ensure
  2222. ;; CONTENTS-BEGIN matches CONTENTS-END.
  2223. (contents-begin (and (eq type 'standard)
  2224. (search-forward "|")
  2225. (point)))
  2226. (contents-end (and (eq type 'standard)
  2227. (progn
  2228. (end-of-line)
  2229. (skip-chars-backward " \t")
  2230. (point))))
  2231. (end (progn (forward-line) (point))))
  2232. (list 'table-row
  2233. (list :type type
  2234. :begin begin
  2235. :end end
  2236. :contents-begin contents-begin
  2237. :contents-end contents-end
  2238. :post-blank 0)))))
  2239. (defun org-element-table-row-interpreter (table-row contents)
  2240. "Interpret TABLE-ROW element as Org syntax.
  2241. CONTENTS is the contents of the table row."
  2242. (if (eq (org-element-property :type table-row) 'rule) "|-"
  2243. (concat "| " contents)))
  2244. ;;;; Verse Block
  2245. (defun org-element-verse-block-parser (limit affiliated)
  2246. "Parse a verse block.
  2247. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  2248. the buffer position at the beginning of the first affiliated
  2249. keyword and CDR is a plist of affiliated keywords along with
  2250. their value.
  2251. Return a list whose CAR is `verse-block' and CDR is a plist
  2252. containing `:begin', `:end', `:contents-begin', `:contents-end',
  2253. `:post-blank' and `:post-affiliated' keywords.
  2254. Assume point is at beginning of the block."
  2255. (let ((case-fold-search t))
  2256. (if (not (save-excursion
  2257. (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
  2258. ;; Incomplete block: parse it as a paragraph.
  2259. (org-element-paragraph-parser limit affiliated)
  2260. (let ((contents-end (match-beginning 0)))
  2261. (save-excursion
  2262. (let* ((begin (car affiliated))
  2263. (post-affiliated (point))
  2264. (contents-begin (progn (forward-line) (point)))
  2265. (pos-before-blank (progn (goto-char contents-end)
  2266. (forward-line)
  2267. (point)))
  2268. (end (progn (skip-chars-forward " \r\t\n" limit)
  2269. (if (eobp) (point) (line-beginning-position)))))
  2270. (list 'verse-block
  2271. (nconc
  2272. (list :begin begin
  2273. :end end
  2274. :contents-begin contents-begin
  2275. :contents-end contents-end
  2276. :post-blank (count-lines pos-before-blank end)
  2277. :post-affiliated post-affiliated)
  2278. (cdr affiliated)))))))))
  2279. (defun org-element-verse-block-interpreter (verse-block contents)
  2280. "Interpret VERSE-BLOCK element as Org syntax.
  2281. CONTENTS is verse block contents."
  2282. (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
  2283. ;;; Objects
  2284. ;;
  2285. ;; Unlike to elements, interstices can be found between objects.
  2286. ;; That's why, along with the parser, successor functions are provided
  2287. ;; for each object. Some objects share the same successor (e.g.,
  2288. ;; `code' and `verbatim' objects).
  2289. ;;
  2290. ;; A successor must accept a single argument bounding the search. It
  2291. ;; will return either a cons cell whose CAR is the object's type, as
  2292. ;; a symbol, and CDR the position of its next occurrence, or nil.
  2293. ;;
  2294. ;; Successors follow the naming convention:
  2295. ;; org-element-NAME-successor, where NAME is the name of the
  2296. ;; successor, as defined in `org-element-all-successors'.
  2297. ;;
  2298. ;; Some object types (e.g., `italic') are recursive. Restrictions on
  2299. ;; object types they can contain will be specified in
  2300. ;; `org-element-object-restrictions'.
  2301. ;;
  2302. ;; Adding a new type of object is simple. Implement a successor,
  2303. ;; a parser, and an interpreter for it, all following the naming
  2304. ;; convention. Register type in `org-element-all-objects' and
  2305. ;; successor in `org-element-all-successors'. Maybe tweak
  2306. ;; restrictions about it, and that's it.
  2307. ;;;; Bold
  2308. (defun org-element-bold-parser ()
  2309. "Parse bold object at point.
  2310. Return a list whose CAR is `bold' and CDR is a plist with
  2311. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2312. `:post-blank' keywords.
  2313. Assume point is at the first star marker."
  2314. (save-excursion
  2315. (unless (bolp) (backward-char 1))
  2316. (looking-at org-emph-re)
  2317. (let ((begin (match-beginning 2))
  2318. (contents-begin (match-beginning 4))
  2319. (contents-end (match-end 4))
  2320. (post-blank (progn (goto-char (match-end 2))
  2321. (skip-chars-forward " \t")))
  2322. (end (point)))
  2323. (list 'bold
  2324. (list :begin begin
  2325. :end end
  2326. :contents-begin contents-begin
  2327. :contents-end contents-end
  2328. :post-blank post-blank)))))
  2329. (defun org-element-bold-interpreter (bold contents)
  2330. "Interpret BOLD object as Org syntax.
  2331. CONTENTS is the contents of the object."
  2332. (format "*%s*" contents))
  2333. (defun org-element-text-markup-successor ()
  2334. "Search for the next text-markup object.
  2335. Return value is a cons cell whose CAR is a symbol among `bold',
  2336. `italic', `underline', `strike-through', `code' and `verbatim'
  2337. and CDR is beginning position."
  2338. (save-excursion
  2339. (unless (bolp) (backward-char))
  2340. (when (re-search-forward org-emph-re nil t)
  2341. (let ((marker (match-string 3)))
  2342. (cons (cond
  2343. ((equal marker "*") 'bold)
  2344. ((equal marker "/") 'italic)
  2345. ((equal marker "_") 'underline)
  2346. ((equal marker "+") 'strike-through)
  2347. ((equal marker "~") 'code)
  2348. ((equal marker "=") 'verbatim)
  2349. (t (error "Unknown marker at %d" (match-beginning 3))))
  2350. (match-beginning 2))))))
  2351. ;;;; Code
  2352. (defun org-element-code-parser ()
  2353. "Parse code object at point.
  2354. Return a list whose CAR is `code' and CDR is a plist with
  2355. `:value', `:begin', `:end' and `:post-blank' keywords.
  2356. Assume point is at the first tilde marker."
  2357. (save-excursion
  2358. (unless (bolp) (backward-char 1))
  2359. (looking-at org-emph-re)
  2360. (let ((begin (match-beginning 2))
  2361. (value (org-match-string-no-properties 4))
  2362. (post-blank (progn (goto-char (match-end 2))
  2363. (skip-chars-forward " \t")))
  2364. (end (point)))
  2365. (list 'code
  2366. (list :value value
  2367. :begin begin
  2368. :end end
  2369. :post-blank post-blank)))))
  2370. (defun org-element-code-interpreter (code contents)
  2371. "Interpret CODE object as Org syntax.
  2372. CONTENTS is nil."
  2373. (format "~%s~" (org-element-property :value code)))
  2374. ;;;; Entity
  2375. (defun org-element-entity-parser ()
  2376. "Parse entity at point.
  2377. Return a list whose CAR is `entity' and CDR a plist with
  2378. `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
  2379. `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
  2380. keywords.
  2381. Assume point is at the beginning of the entity."
  2382. (save-excursion
  2383. (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
  2384. (let* ((value (org-entity-get (match-string 1)))
  2385. (begin (match-beginning 0))
  2386. (bracketsp (string= (match-string 2) "{}"))
  2387. (post-blank (progn (goto-char (match-end 1))
  2388. (when bracketsp (forward-char 2))
  2389. (skip-chars-forward " \t")))
  2390. (end (point)))
  2391. (list 'entity
  2392. (list :name (car value)
  2393. :latex (nth 1 value)
  2394. :latex-math-p (nth 2 value)
  2395. :html (nth 3 value)
  2396. :ascii (nth 4 value)
  2397. :latin1 (nth 5 value)
  2398. :utf-8 (nth 6 value)
  2399. :begin begin
  2400. :end end
  2401. :use-brackets-p bracketsp
  2402. :post-blank post-blank)))))
  2403. (defun org-element-entity-interpreter (entity contents)
  2404. "Interpret ENTITY object as Org syntax.
  2405. CONTENTS is nil."
  2406. (concat "\\"
  2407. (org-element-property :name entity)
  2408. (when (org-element-property :use-brackets-p entity) "{}")))
  2409. (defun org-element-latex-or-entity-successor ()
  2410. "Search for the next latex-fragment or entity object.
  2411. Return value is a cons cell whose CAR is `entity' or
  2412. `latex-fragment' and CDR is beginning position."
  2413. (save-excursion
  2414. (unless (bolp) (backward-char))
  2415. (let ((matchers (cdr org-latex-regexps))
  2416. ;; ENTITY-RE matches both LaTeX commands and Org entities.
  2417. (entity-re
  2418. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
  2419. (when (re-search-forward
  2420. (concat (mapconcat #'cadr matchers "\\|") "\\|" entity-re) nil t)
  2421. (goto-char (match-beginning 0))
  2422. (if (looking-at entity-re)
  2423. ;; Determine if it's a real entity or a LaTeX command.
  2424. (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
  2425. (match-beginning 0))
  2426. ;; No entity nor command: point is at a LaTeX fragment.
  2427. ;; Determine its type to get the correct beginning position.
  2428. (cons 'latex-fragment
  2429. (catch 'return
  2430. (dolist (e matchers)
  2431. (when (looking-at (nth 1 e))
  2432. (throw 'return (match-beginning (nth 2 e)))))
  2433. (point))))))))
  2434. ;;;; Export Snippet
  2435. (defun org-element-export-snippet-parser ()
  2436. "Parse export snippet at point.
  2437. Return a list whose CAR is `export-snippet' and CDR a plist with
  2438. `:begin', `:end', `:back-end', `:value' and `:post-blank' as
  2439. keywords.
  2440. Assume point is at the beginning of the snippet."
  2441. (save-excursion
  2442. (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
  2443. (let* ((begin (match-beginning 0))
  2444. (back-end (org-match-string-no-properties 1))
  2445. (value (buffer-substring-no-properties
  2446. (point)
  2447. (progn (re-search-forward "@@" nil t) (match-beginning 0))))
  2448. (post-blank (skip-chars-forward " \t"))
  2449. (end (point)))
  2450. (list 'export-snippet
  2451. (list :back-end back-end
  2452. :value value
  2453. :begin begin
  2454. :end end
  2455. :post-blank post-blank)))))
  2456. (defun org-element-export-snippet-interpreter (export-snippet contents)
  2457. "Interpret EXPORT-SNIPPET object as Org syntax.
  2458. CONTENTS is nil."
  2459. (format "@@%s:%s@@"
  2460. (org-element-property :back-end export-snippet)
  2461. (org-element-property :value export-snippet)))
  2462. (defun org-element-export-snippet-successor ()
  2463. "Search for the next export-snippet object.
  2464. Return value is a cons cell whose CAR is `export-snippet' and CDR
  2465. its beginning position."
  2466. (save-excursion
  2467. (let (beg)
  2468. (when (and (re-search-forward "@@[-A-Za-z0-9]+:" nil t)
  2469. (setq beg (match-beginning 0))
  2470. (search-forward "@@" nil t))
  2471. (cons 'export-snippet beg)))))
  2472. ;;;; Footnote Reference
  2473. (defun org-element-footnote-reference-parser ()
  2474. "Parse footnote reference at point.
  2475. Return a list whose CAR is `footnote-reference' and CDR a plist
  2476. with `:label', `:type', `:inline-definition', `:begin', `:end'
  2477. and `:post-blank' as keywords."
  2478. (save-excursion
  2479. (looking-at org-footnote-re)
  2480. (let* ((begin (point))
  2481. (label (or (org-match-string-no-properties 2)
  2482. (org-match-string-no-properties 3)
  2483. (and (match-string 1)
  2484. (concat "fn:" (org-match-string-no-properties 1)))))
  2485. (type (if (or (not label) (match-string 1)) 'inline 'standard))
  2486. (inner-begin (match-end 0))
  2487. (inner-end
  2488. (let ((count 1))
  2489. (forward-char)
  2490. (while (and (> count 0) (re-search-forward "[][]" nil t))
  2491. (if (equal (match-string 0) "[") (incf count) (decf count)))
  2492. (1- (point))))
  2493. (post-blank (progn (goto-char (1+ inner-end))
  2494. (skip-chars-forward " \t")))
  2495. (end (point))
  2496. (footnote-reference
  2497. (list 'footnote-reference
  2498. (list :label label
  2499. :type type
  2500. :begin begin
  2501. :end end
  2502. :post-blank post-blank))))
  2503. (org-element-put-property
  2504. footnote-reference :inline-definition
  2505. (and (eq type 'inline)
  2506. (org-element-parse-secondary-string
  2507. (buffer-substring inner-begin inner-end)
  2508. (org-element-restriction 'footnote-reference)
  2509. footnote-reference))))))
  2510. (defun org-element-footnote-reference-interpreter (footnote-reference contents)
  2511. "Interpret FOOTNOTE-REFERENCE object as Org syntax.
  2512. CONTENTS is nil."
  2513. (let ((label (or (org-element-property :label footnote-reference) "fn:"))
  2514. (def
  2515. (let ((inline-def
  2516. (org-element-property :inline-definition footnote-reference)))
  2517. (if (not inline-def) ""
  2518. (concat ":" (org-element-interpret-data inline-def))))))
  2519. (format "[%s]" (concat label def))))
  2520. (defun org-element-footnote-reference-successor ()
  2521. "Search for the next footnote-reference object.
  2522. Return value is a cons cell whose CAR is `footnote-reference' and
  2523. CDR is beginning position."
  2524. (save-excursion
  2525. (catch 'exit
  2526. (while (re-search-forward org-footnote-re nil t)
  2527. (save-excursion
  2528. (let ((beg (match-beginning 0))
  2529. (count 1))
  2530. (backward-char)
  2531. (while (re-search-forward "[][]" nil t)
  2532. (if (equal (match-string 0) "[") (incf count) (decf count))
  2533. (when (zerop count)
  2534. (throw 'exit (cons 'footnote-reference beg))))))))))
  2535. ;;;; Inline Babel Call
  2536. (defun org-element-inline-babel-call-parser ()
  2537. "Parse inline babel call at point.
  2538. Return a list whose CAR is `inline-babel-call' and CDR a plist
  2539. with `:begin', `:end', `:value' and `:post-blank' as keywords.
  2540. Assume point is at the beginning of the babel call."
  2541. (save-excursion
  2542. (unless (bolp) (backward-char))
  2543. (let ((case-fold-search t))
  2544. (looking-at org-babel-inline-lob-one-liner-regexp))
  2545. (let ((begin (match-end 1))
  2546. (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
  2547. (post-blank (progn (goto-char (match-end 0))
  2548. (skip-chars-forward " \t")))
  2549. (end (point)))
  2550. (list 'inline-babel-call
  2551. (list :begin begin
  2552. :end end
  2553. :value value
  2554. :post-blank post-blank)))))
  2555. (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
  2556. "Interpret INLINE-BABEL-CALL object as Org syntax.
  2557. CONTENTS is nil."
  2558. (org-element-property :value inline-babel-call))
  2559. (defun org-element-inline-babel-call-successor ()
  2560. "Search for the next inline-babel-call object.
  2561. Return value is a cons cell whose CAR is `inline-babel-call' and
  2562. CDR is beginning position."
  2563. (save-excursion
  2564. (catch 'exit
  2565. (while (search-forward "call_" nil t)
  2566. (save-excursion
  2567. (goto-char (match-beginning 0))
  2568. (when (looking-at org-babel-inline-lob-one-liner-regexp)
  2569. (throw 'exit (cons 'inline-babel-call (point)))))))))
  2570. ;;;; Inline Src Block
  2571. (defun org-element-inline-src-block-parser ()
  2572. "Parse inline source block at point.
  2573. Return a list whose CAR is `inline-src-block' and CDR a plist
  2574. with `:begin', `:end', `:language', `:value', `:parameters' and
  2575. `:post-blank' as keywords.
  2576. Assume point is at the beginning of the inline src block."
  2577. (save-excursion
  2578. (unless (bolp) (backward-char))
  2579. (looking-at org-babel-inline-src-block-regexp)
  2580. (let ((begin (match-beginning 1))
  2581. (language (org-match-string-no-properties 2))
  2582. (parameters (org-match-string-no-properties 4))
  2583. (value (org-match-string-no-properties 5))
  2584. (post-blank (progn (goto-char (match-end 0))
  2585. (skip-chars-forward " \t")))
  2586. (end (point)))
  2587. (list 'inline-src-block
  2588. (list :language language
  2589. :value value
  2590. :parameters parameters
  2591. :begin begin
  2592. :end end
  2593. :post-blank post-blank)))))
  2594. (defun org-element-inline-src-block-interpreter (inline-src-block contents)
  2595. "Interpret INLINE-SRC-BLOCK object as Org syntax.
  2596. CONTENTS is nil."
  2597. (let ((language (org-element-property :language inline-src-block))
  2598. (arguments (org-element-property :parameters inline-src-block))
  2599. (body (org-element-property :value inline-src-block)))
  2600. (format "src_%s%s{%s}"
  2601. language
  2602. (if arguments (format "[%s]" arguments) "")
  2603. body)))
  2604. (defun org-element-inline-src-block-successor ()
  2605. "Search for the next inline-babel-call element.
  2606. Return value is a cons cell whose CAR is `inline-babel-call' and
  2607. CDR is beginning position."
  2608. (save-excursion
  2609. (unless (bolp) (backward-char))
  2610. (when (re-search-forward org-babel-inline-src-block-regexp nil t)
  2611. (cons 'inline-src-block (match-beginning 1)))))
  2612. ;;;; Italic
  2613. (defun org-element-italic-parser ()
  2614. "Parse italic object at point.
  2615. Return a list whose CAR is `italic' and CDR is a plist with
  2616. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2617. `:post-blank' keywords.
  2618. Assume point is at the first slash marker."
  2619. (save-excursion
  2620. (unless (bolp) (backward-char 1))
  2621. (looking-at org-emph-re)
  2622. (let ((begin (match-beginning 2))
  2623. (contents-begin (match-beginning 4))
  2624. (contents-end (match-end 4))
  2625. (post-blank (progn (goto-char (match-end 2))
  2626. (skip-chars-forward " \t")))
  2627. (end (point)))
  2628. (list 'italic
  2629. (list :begin begin
  2630. :end end
  2631. :contents-begin contents-begin
  2632. :contents-end contents-end
  2633. :post-blank post-blank)))))
  2634. (defun org-element-italic-interpreter (italic contents)
  2635. "Interpret ITALIC object as Org syntax.
  2636. CONTENTS is the contents of the object."
  2637. (format "/%s/" contents))
  2638. ;;;; Latex Fragment
  2639. (defun org-element-latex-fragment-parser ()
  2640. "Parse LaTeX fragment at point.
  2641. Return a list whose CAR is `latex-fragment' and CDR a plist with
  2642. `:value', `:begin', `:end', and `:post-blank' as keywords.
  2643. Assume point is at the beginning of the LaTeX fragment."
  2644. (save-excursion
  2645. (let* ((begin (point))
  2646. (substring-match
  2647. (catch 'exit
  2648. (dolist (e (cdr org-latex-regexps))
  2649. (let ((latex-regexp (nth 1 e)))
  2650. (when (or (looking-at latex-regexp)
  2651. (and (not (bobp))
  2652. (save-excursion
  2653. (backward-char)
  2654. (looking-at latex-regexp))))
  2655. (throw 'exit (nth 2 e)))))
  2656. ;; None found: it's a macro.
  2657. (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
  2658. 0))
  2659. (value (org-match-string-no-properties substring-match))
  2660. (post-blank (progn (goto-char (match-end substring-match))
  2661. (skip-chars-forward " \t")))
  2662. (end (point)))
  2663. (list 'latex-fragment
  2664. (list :value value
  2665. :begin begin
  2666. :end end
  2667. :post-blank post-blank)))))
  2668. (defun org-element-latex-fragment-interpreter (latex-fragment contents)
  2669. "Interpret LATEX-FRAGMENT object as Org syntax.
  2670. CONTENTS is nil."
  2671. (org-element-property :value latex-fragment))
  2672. ;;;; Line Break
  2673. (defun org-element-line-break-parser ()
  2674. "Parse line break at point.
  2675. Return a list whose CAR is `line-break', and CDR a plist with
  2676. `:begin', `:end' and `:post-blank' keywords.
  2677. Assume point is at the beginning of the line break."
  2678. (list 'line-break
  2679. (list :begin (point)
  2680. :end (progn (forward-line) (point))
  2681. :post-blank 0)))
  2682. (defun org-element-line-break-interpreter (line-break contents)
  2683. "Interpret LINE-BREAK object as Org syntax.
  2684. CONTENTS is nil."
  2685. "\\\\\n")
  2686. (defun org-element-line-break-successor ()
  2687. "Search for the next line-break object.
  2688. Return value is a cons cell whose CAR is `line-break' and CDR is
  2689. beginning position."
  2690. (save-excursion
  2691. (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" nil t)
  2692. (goto-char (match-beginning 1)))))
  2693. ;; A line break can only happen on a non-empty line.
  2694. (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
  2695. (cons 'line-break beg)))))
  2696. ;;;; Link
  2697. (defun org-element-link-parser ()
  2698. "Parse link at point.
  2699. Return a list whose CAR is `link' and CDR a plist with `:type',
  2700. `:path', `:raw-link', `:application', `:search-option', `:begin',
  2701. `:end', `:contents-begin', `:contents-end' and `:post-blank' as
  2702. keywords.
  2703. Assume point is at the beginning of the link."
  2704. (save-excursion
  2705. (let ((begin (point))
  2706. end contents-begin contents-end link-end post-blank path type
  2707. raw-link link search-option application)
  2708. (cond
  2709. ;; Type 1: Text targeted from a radio target.
  2710. ((and org-target-link-regexp (looking-at org-target-link-regexp))
  2711. (setq type "radio"
  2712. link-end (match-end 0)
  2713. path (org-match-string-no-properties 0)
  2714. contents-begin (match-beginning 0)
  2715. contents-end (match-end 0)))
  2716. ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
  2717. ((looking-at org-bracket-link-regexp)
  2718. (setq contents-begin (match-beginning 3)
  2719. contents-end (match-end 3)
  2720. link-end (match-end 0)
  2721. ;; RAW-LINK is the original link. Expand any
  2722. ;; abbreviation in it.
  2723. raw-link (org-translate-link
  2724. (org-link-expand-abbrev
  2725. (org-match-string-no-properties 1))))
  2726. ;; Determine TYPE of link and set PATH accordingly.
  2727. (cond
  2728. ;; File type.
  2729. ((or (file-name-absolute-p raw-link)
  2730. (string-match "^\\.\\.?/" raw-link))
  2731. (setq type "file" path raw-link))
  2732. ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
  2733. ((string-match org-link-re-with-space3 raw-link)
  2734. (setq type (match-string 1 raw-link) path (match-string 2 raw-link)))
  2735. ;; Id type: PATH is the id.
  2736. ((string-match "^id:\\([-a-f0-9]+\\)" raw-link)
  2737. (setq type "id" path (match-string 1 raw-link)))
  2738. ;; Code-ref type: PATH is the name of the reference.
  2739. ((string-match "^(\\(.*\\))$" raw-link)
  2740. (setq type "coderef" path (match-string 1 raw-link)))
  2741. ;; Custom-id type: PATH is the name of the custom id.
  2742. ((= (aref raw-link 0) ?#)
  2743. (setq type "custom-id" path (substring raw-link 1)))
  2744. ;; Fuzzy type: Internal link either matches a target, an
  2745. ;; headline name or nothing. PATH is the target or
  2746. ;; headline's name.
  2747. (t (setq type "fuzzy" path raw-link))))
  2748. ;; Type 3: Plain link, e.g., http://orgmode.org
  2749. ((looking-at org-plain-link-re)
  2750. (setq raw-link (org-match-string-no-properties 0)
  2751. type (org-match-string-no-properties 1)
  2752. link-end (match-end 0)
  2753. path (org-match-string-no-properties 2)))
  2754. ;; Type 4: Angular link, e.g., <http://orgmode.org>
  2755. ((looking-at org-angle-link-re)
  2756. (setq raw-link (buffer-substring-no-properties
  2757. (match-beginning 1) (match-end 2))
  2758. type (org-match-string-no-properties 1)
  2759. link-end (match-end 0)
  2760. path (org-match-string-no-properties 2))))
  2761. ;; In any case, deduce end point after trailing white space from
  2762. ;; LINK-END variable.
  2763. (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
  2764. end (point))
  2765. ;; Extract search option and opening application out of
  2766. ;; "file"-type links.
  2767. (when (member type org-element-link-type-is-file)
  2768. ;; Application.
  2769. (cond ((string-match "^file\\+\\(.*\\)$" type)
  2770. (setq application (match-string 1 type)))
  2771. ((not (string-match "^file" type))
  2772. (setq application type)))
  2773. ;; Extract search option from PATH.
  2774. (when (string-match "::\\(.*\\)$" path)
  2775. (setq search-option (match-string 1 path)
  2776. path (replace-match "" nil nil path)))
  2777. ;; Make sure TYPE always reports "file".
  2778. (setq type "file"))
  2779. (list 'link
  2780. (list :type type
  2781. :path path
  2782. :raw-link (or raw-link path)
  2783. :application application
  2784. :search-option search-option
  2785. :begin begin
  2786. :end end
  2787. :contents-begin contents-begin
  2788. :contents-end contents-end
  2789. :post-blank post-blank)))))
  2790. (defun org-element-link-interpreter (link contents)
  2791. "Interpret LINK object as Org syntax.
  2792. CONTENTS is the contents of the object, or nil."
  2793. (let ((type (org-element-property :type link))
  2794. (raw-link (org-element-property :raw-link link)))
  2795. (if (string= type "radio") raw-link
  2796. (format "[[%s]%s]"
  2797. raw-link
  2798. (if contents (format "[%s]" contents) "")))))
  2799. (defun org-element-link-successor ()
  2800. "Search for the next link object.
  2801. Return value is a cons cell whose CAR is `link' and CDR is
  2802. beginning position."
  2803. (save-excursion
  2804. (let ((link-regexp
  2805. (if (not org-target-link-regexp) org-any-link-re
  2806. (concat org-any-link-re "\\|" org-target-link-regexp))))
  2807. (when (re-search-forward link-regexp nil t)
  2808. (cons 'link (match-beginning 0))))))
  2809. (defun org-element-plain-link-successor ()
  2810. "Search for the next plain link object.
  2811. Return value is a cons cell whose CAR is `link' and CDR is
  2812. beginning position."
  2813. (and (save-excursion (re-search-forward org-plain-link-re nil t))
  2814. (cons 'link (match-beginning 0))))
  2815. ;;;; Macro
  2816. (defun org-element-macro-parser ()
  2817. "Parse macro at point.
  2818. Return a list whose CAR is `macro' and CDR a plist with `:key',
  2819. `:args', `:begin', `:end', `:value' and `:post-blank' as
  2820. keywords.
  2821. Assume point is at the macro."
  2822. (save-excursion
  2823. (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
  2824. (let ((begin (point))
  2825. (key (downcase (org-match-string-no-properties 1)))
  2826. (value (org-match-string-no-properties 0))
  2827. (post-blank (progn (goto-char (match-end 0))
  2828. (skip-chars-forward " \t")))
  2829. (end (point))
  2830. (args (let ((args (org-match-string-no-properties 3)))
  2831. (when args
  2832. ;; Do not use `org-split-string' since empty
  2833. ;; strings are meaningful here.
  2834. (split-string
  2835. (replace-regexp-in-string
  2836. "\\(\\\\*\\)\\(,\\)"
  2837. (lambda (str)
  2838. (let ((len (length (match-string 1 str))))
  2839. (concat (make-string (/ len 2) ?\\)
  2840. (if (zerop (mod len 2)) "\000" ","))))
  2841. args nil t)
  2842. "\000")))))
  2843. (list 'macro
  2844. (list :key key
  2845. :value value
  2846. :args args
  2847. :begin begin
  2848. :end end
  2849. :post-blank post-blank)))))
  2850. (defun org-element-macro-interpreter (macro contents)
  2851. "Interpret MACRO object as Org syntax.
  2852. CONTENTS is nil."
  2853. (org-element-property :value macro))
  2854. (defun org-element-macro-successor ()
  2855. "Search for the next macro object.
  2856. Return value is cons cell whose CAR is `macro' and CDR is
  2857. beginning position."
  2858. (save-excursion
  2859. (when (re-search-forward
  2860. "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
  2861. nil t)
  2862. (cons 'macro (match-beginning 0)))))
  2863. ;;;; Radio-target
  2864. (defun org-element-radio-target-parser ()
  2865. "Parse radio target at point.
  2866. Return a list whose CAR is `radio-target' and CDR a plist with
  2867. `:begin', `:end', `:contents-begin', `:contents-end', `:value'
  2868. and `:post-blank' as keywords.
  2869. Assume point is at the radio target."
  2870. (save-excursion
  2871. (looking-at org-radio-target-regexp)
  2872. (let ((begin (point))
  2873. (contents-begin (match-beginning 1))
  2874. (contents-end (match-end 1))
  2875. (value (org-match-string-no-properties 1))
  2876. (post-blank (progn (goto-char (match-end 0))
  2877. (skip-chars-forward " \t")))
  2878. (end (point)))
  2879. (list 'radio-target
  2880. (list :begin begin
  2881. :end end
  2882. :contents-begin contents-begin
  2883. :contents-end contents-end
  2884. :post-blank post-blank
  2885. :value value)))))
  2886. (defun org-element-radio-target-interpreter (target contents)
  2887. "Interpret TARGET object as Org syntax.
  2888. CONTENTS is the contents of the object."
  2889. (concat "<<<" contents ">>>"))
  2890. (defun org-element-radio-target-successor ()
  2891. "Search for the next radio-target object.
  2892. Return value is a cons cell whose CAR is `radio-target' and CDR
  2893. is beginning position."
  2894. (save-excursion
  2895. (when (re-search-forward org-radio-target-regexp nil t)
  2896. (cons 'radio-target (match-beginning 0)))))
  2897. ;;;; Statistics Cookie
  2898. (defun org-element-statistics-cookie-parser ()
  2899. "Parse statistics cookie at point.
  2900. Return a list whose CAR is `statistics-cookie', and CDR a plist
  2901. with `:begin', `:end', `:value' and `:post-blank' keywords.
  2902. Assume point is at the beginning of the statistics-cookie."
  2903. (save-excursion
  2904. (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
  2905. (let* ((begin (point))
  2906. (value (buffer-substring-no-properties
  2907. (match-beginning 0) (match-end 0)))
  2908. (post-blank (progn (goto-char (match-end 0))
  2909. (skip-chars-forward " \t")))
  2910. (end (point)))
  2911. (list 'statistics-cookie
  2912. (list :begin begin
  2913. :end end
  2914. :value value
  2915. :post-blank post-blank)))))
  2916. (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
  2917. "Interpret STATISTICS-COOKIE object as Org syntax.
  2918. CONTENTS is nil."
  2919. (org-element-property :value statistics-cookie))
  2920. (defun org-element-statistics-cookie-successor ()
  2921. "Search for the next statistics cookie object.
  2922. Return value is a cons cell whose CAR is `statistics-cookie' and
  2923. CDR is beginning position."
  2924. (save-excursion
  2925. (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" nil t)
  2926. (cons 'statistics-cookie (match-beginning 0)))))
  2927. ;;;; Strike-Through
  2928. (defun org-element-strike-through-parser ()
  2929. "Parse strike-through object at point.
  2930. Return a list whose CAR is `strike-through' and CDR is a plist
  2931. with `:begin', `:end', `:contents-begin' and `:contents-end' and
  2932. `:post-blank' keywords.
  2933. Assume point is at the first plus sign marker."
  2934. (save-excursion
  2935. (unless (bolp) (backward-char 1))
  2936. (looking-at org-emph-re)
  2937. (let ((begin (match-beginning 2))
  2938. (contents-begin (match-beginning 4))
  2939. (contents-end (match-end 4))
  2940. (post-blank (progn (goto-char (match-end 2))
  2941. (skip-chars-forward " \t")))
  2942. (end (point)))
  2943. (list 'strike-through
  2944. (list :begin begin
  2945. :end end
  2946. :contents-begin contents-begin
  2947. :contents-end contents-end
  2948. :post-blank post-blank)))))
  2949. (defun org-element-strike-through-interpreter (strike-through contents)
  2950. "Interpret STRIKE-THROUGH object as Org syntax.
  2951. CONTENTS is the contents of the object."
  2952. (format "+%s+" contents))
  2953. ;;;; Subscript
  2954. (defun org-element-subscript-parser ()
  2955. "Parse subscript at point.
  2956. Return a list whose CAR is `subscript' and CDR a plist with
  2957. `:begin', `:end', `:contents-begin', `:contents-end',
  2958. `:use-brackets-p' and `:post-blank' as keywords.
  2959. Assume point is at the underscore."
  2960. (save-excursion
  2961. (unless (bolp) (backward-char))
  2962. (looking-at org-match-substring-regexp)
  2963. (let ((bracketsp (match-beginning 4))
  2964. (begin (match-beginning 2))
  2965. (contents-begin (or (match-beginning 4)
  2966. (match-beginning 3)))
  2967. (contents-end (or (match-end 4) (match-end 3)))
  2968. (post-blank (progn (goto-char (match-end 0))
  2969. (skip-chars-forward " \t")))
  2970. (end (point)))
  2971. (list 'subscript
  2972. (list :begin begin
  2973. :end end
  2974. :use-brackets-p bracketsp
  2975. :contents-begin contents-begin
  2976. :contents-end contents-end
  2977. :post-blank post-blank)))))
  2978. (defun org-element-subscript-interpreter (subscript contents)
  2979. "Interpret SUBSCRIPT object as Org syntax.
  2980. CONTENTS is the contents of the object."
  2981. (format
  2982. (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
  2983. contents))
  2984. (defun org-element-sub/superscript-successor ()
  2985. "Search for the next sub/superscript object.
  2986. Return value is a cons cell whose CAR is either `subscript' or
  2987. `superscript' and CDR is beginning position."
  2988. (save-excursion
  2989. (unless (bolp) (backward-char))
  2990. (when (re-search-forward org-match-substring-regexp nil t)
  2991. (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
  2992. (match-beginning 2)))))
  2993. ;;;; Superscript
  2994. (defun org-element-superscript-parser ()
  2995. "Parse superscript at point.
  2996. Return a list whose CAR is `superscript' and CDR a plist with
  2997. `:begin', `:end', `:contents-begin', `:contents-end',
  2998. `:use-brackets-p' and `:post-blank' as keywords.
  2999. Assume point is at the caret."
  3000. (save-excursion
  3001. (unless (bolp) (backward-char))
  3002. (looking-at org-match-substring-regexp)
  3003. (let ((bracketsp (match-beginning 4))
  3004. (begin (match-beginning 2))
  3005. (contents-begin (or (match-beginning 4)
  3006. (match-beginning 3)))
  3007. (contents-end (or (match-end 4) (match-end 3)))
  3008. (post-blank (progn (goto-char (match-end 0))
  3009. (skip-chars-forward " \t")))
  3010. (end (point)))
  3011. (list 'superscript
  3012. (list :begin begin
  3013. :end end
  3014. :use-brackets-p bracketsp
  3015. :contents-begin contents-begin
  3016. :contents-end contents-end
  3017. :post-blank post-blank)))))
  3018. (defun org-element-superscript-interpreter (superscript contents)
  3019. "Interpret SUPERSCRIPT object as Org syntax.
  3020. CONTENTS is the contents of the object."
  3021. (format
  3022. (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
  3023. contents))
  3024. ;;;; Table Cell
  3025. (defun org-element-table-cell-parser ()
  3026. "Parse table cell at point.
  3027. Return a list whose CAR is `table-cell' and CDR is a plist
  3028. containing `:begin', `:end', `:contents-begin', `:contents-end'
  3029. and `:post-blank' keywords."
  3030. (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
  3031. (let* ((begin (match-beginning 0))
  3032. (end (match-end 0))
  3033. (contents-begin (match-beginning 1))
  3034. (contents-end (match-end 1)))
  3035. (list 'table-cell
  3036. (list :begin begin
  3037. :end end
  3038. :contents-begin contents-begin
  3039. :contents-end contents-end
  3040. :post-blank 0))))
  3041. (defun org-element-table-cell-interpreter (table-cell contents)
  3042. "Interpret TABLE-CELL element as Org syntax.
  3043. CONTENTS is the contents of the cell, or nil."
  3044. (concat " " contents " |"))
  3045. (defun org-element-table-cell-successor ()
  3046. "Search for the next table-cell object.
  3047. Return value is a cons cell whose CAR is `table-cell' and CDR is
  3048. beginning position."
  3049. (when (looking-at "[ \t]*.*?[ \t]*|") (cons 'table-cell (point))))
  3050. ;;;; Target
  3051. (defun org-element-target-parser ()
  3052. "Parse target at point.
  3053. Return a list whose CAR is `target' and CDR a plist with
  3054. `:begin', `:end', `:value' and `:post-blank' as keywords.
  3055. Assume point is at the target."
  3056. (save-excursion
  3057. (looking-at org-target-regexp)
  3058. (let ((begin (point))
  3059. (value (org-match-string-no-properties 1))
  3060. (post-blank (progn (goto-char (match-end 0))
  3061. (skip-chars-forward " \t")))
  3062. (end (point)))
  3063. (list 'target
  3064. (list :begin begin
  3065. :end end
  3066. :value value
  3067. :post-blank post-blank)))))
  3068. (defun org-element-target-interpreter (target contents)
  3069. "Interpret TARGET object as Org syntax.
  3070. CONTENTS is nil."
  3071. (format "<<%s>>" (org-element-property :value target)))
  3072. (defun org-element-target-successor ()
  3073. "Search for the next target object.
  3074. Return value is a cons cell whose CAR is `target' and CDR is
  3075. beginning position."
  3076. (save-excursion
  3077. (when (re-search-forward org-target-regexp nil t)
  3078. (cons 'target (match-beginning 0)))))
  3079. ;;;; Timestamp
  3080. (defun org-element-timestamp-parser ()
  3081. "Parse time stamp at point.
  3082. Return a list whose CAR is `timestamp', and CDR a plist with
  3083. `:type', `:raw-value', `:year-start', `:month-start',
  3084. `:day-start', `:hour-start', `:minute-start', `:year-end',
  3085. `:month-end', `:day-end', `:hour-end', `:minute-end',
  3086. `:repeater-type', `:repeater-value', `:repeater-unit',
  3087. `:warning-type', `:warning-value', `:warning-unit', `:begin',
  3088. `:end', `:value' and `:post-blank' keywords.
  3089. Assume point is at the beginning of the timestamp."
  3090. (save-excursion
  3091. (let* ((begin (point))
  3092. (activep (eq (char-after) ?<))
  3093. (raw-value
  3094. (progn
  3095. (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
  3096. (match-string-no-properties 0)))
  3097. (date-start (match-string-no-properties 1))
  3098. (date-end (match-string 3))
  3099. (diaryp (match-beginning 2))
  3100. (post-blank (progn (goto-char (match-end 0))
  3101. (skip-chars-forward " \t")))
  3102. (end (point))
  3103. (time-range
  3104. (and (not diaryp)
  3105. (string-match
  3106. "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
  3107. date-start)
  3108. (cons (string-to-number (match-string 2 date-start))
  3109. (string-to-number (match-string 3 date-start)))))
  3110. (type (cond (diaryp 'diary)
  3111. ((and activep (or date-end time-range)) 'active-range)
  3112. (activep 'active)
  3113. ((or date-end time-range) 'inactive-range)
  3114. (t 'inactive)))
  3115. (repeater-props
  3116. (and (not diaryp)
  3117. (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
  3118. raw-value)
  3119. (list
  3120. :repeater-type
  3121. (let ((type (match-string 1 raw-value)))
  3122. (cond ((equal "++" type) 'catch-up)
  3123. ((equal ".+" type) 'restart)
  3124. (t 'cumulate)))
  3125. :repeater-value (string-to-number (match-string 2 raw-value))
  3126. :repeater-unit
  3127. (case (string-to-char (match-string 3 raw-value))
  3128. (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
  3129. (warning-props
  3130. (and (not diaryp)
  3131. (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value)
  3132. (list
  3133. :warning-type (if (match-string 1 raw-value) 'first 'all)
  3134. :warning-value (string-to-number (match-string 2 raw-value))
  3135. :warning-unit
  3136. (case (string-to-char (match-string 3 raw-value))
  3137. (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
  3138. year-start month-start day-start hour-start minute-start year-end
  3139. month-end day-end hour-end minute-end)
  3140. ;; Parse date-start.
  3141. (unless diaryp
  3142. (let ((date (org-parse-time-string date-start t)))
  3143. (setq year-start (nth 5 date)
  3144. month-start (nth 4 date)
  3145. day-start (nth 3 date)
  3146. hour-start (nth 2 date)
  3147. minute-start (nth 1 date))))
  3148. ;; Compute date-end. It can be provided directly in time-stamp,
  3149. ;; or extracted from time range. Otherwise, it defaults to the
  3150. ;; same values as date-start.
  3151. (unless diaryp
  3152. (let ((date (and date-end (org-parse-time-string date-end t))))
  3153. (setq year-end (or (nth 5 date) year-start)
  3154. month-end (or (nth 4 date) month-start)
  3155. day-end (or (nth 3 date) day-start)
  3156. hour-end (or (nth 2 date) (car time-range) hour-start)
  3157. minute-end (or (nth 1 date) (cdr time-range) minute-start))))
  3158. (list 'timestamp
  3159. (nconc (list :type type
  3160. :raw-value raw-value
  3161. :year-start year-start
  3162. :month-start month-start
  3163. :day-start day-start
  3164. :hour-start hour-start
  3165. :minute-start minute-start
  3166. :year-end year-end
  3167. :month-end month-end
  3168. :day-end day-end
  3169. :hour-end hour-end
  3170. :minute-end minute-end
  3171. :begin begin
  3172. :end end
  3173. :post-blank post-blank)
  3174. repeater-props
  3175. warning-props)))))
  3176. (defun org-element-timestamp-interpreter (timestamp contents)
  3177. "Interpret TIMESTAMP object as Org syntax.
  3178. CONTENTS is nil."
  3179. (let* ((repeat-string
  3180. (concat
  3181. (case (org-element-property :repeater-type timestamp)
  3182. (cumulate "+") (catch-up "++") (restart ".+"))
  3183. (let ((val (org-element-property :repeater-value timestamp)))
  3184. (and val (number-to-string val)))
  3185. (case (org-element-property :repeater-unit timestamp)
  3186. (hour "h") (day "d") (week "w") (month "m") (year "y"))))
  3187. (warning-string
  3188. (concat
  3189. (case (org-element-property :warning-type timestamp)
  3190. (first "--")
  3191. (all "-"))
  3192. (let ((val (org-element-property :warning-value timestamp)))
  3193. (and val (number-to-string val)))
  3194. (case (org-element-property :warning-unit timestamp)
  3195. (hour "h") (day "d") (week "w") (month "m") (year "y"))))
  3196. (build-ts-string
  3197. ;; Build an Org timestamp string from TIME. ACTIVEP is
  3198. ;; non-nil when time stamp is active. If WITH-TIME-P is
  3199. ;; non-nil, add a time part. HOUR-END and MINUTE-END
  3200. ;; specify a time range in the timestamp. REPEAT-STRING is
  3201. ;; the repeater string, if any.
  3202. (lambda (time activep &optional with-time-p hour-end minute-end)
  3203. (let ((ts (format-time-string
  3204. (funcall (if with-time-p 'cdr 'car)
  3205. org-time-stamp-formats)
  3206. time)))
  3207. (when (and hour-end minute-end)
  3208. (string-match "[012]?[0-9]:[0-5][0-9]" ts)
  3209. (setq ts
  3210. (replace-match
  3211. (format "\\&-%02d:%02d" hour-end minute-end)
  3212. nil nil ts)))
  3213. (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
  3214. (dolist (s (list repeat-string warning-string))
  3215. (when (org-string-nw-p s)
  3216. (setq ts (concat (substring ts 0 -1)
  3217. " "
  3218. s
  3219. (substring ts -1)))))
  3220. ;; Return value.
  3221. ts)))
  3222. (type (org-element-property :type timestamp)))
  3223. (case type
  3224. ((active inactive)
  3225. (let* ((minute-start (org-element-property :minute-start timestamp))
  3226. (minute-end (org-element-property :minute-end timestamp))
  3227. (hour-start (org-element-property :hour-start timestamp))
  3228. (hour-end (org-element-property :hour-end timestamp))
  3229. (time-range-p (and hour-start hour-end minute-start minute-end
  3230. (or (/= hour-start hour-end)
  3231. (/= minute-start minute-end)))))
  3232. (funcall
  3233. build-ts-string
  3234. (encode-time 0
  3235. (or minute-start 0)
  3236. (or hour-start 0)
  3237. (org-element-property :day-start timestamp)
  3238. (org-element-property :month-start timestamp)
  3239. (org-element-property :year-start timestamp))
  3240. (eq type 'active)
  3241. (and hour-start minute-start)
  3242. (and time-range-p hour-end)
  3243. (and time-range-p minute-end))))
  3244. ((active-range inactive-range)
  3245. (let ((minute-start (org-element-property :minute-start timestamp))
  3246. (minute-end (org-element-property :minute-end timestamp))
  3247. (hour-start (org-element-property :hour-start timestamp))
  3248. (hour-end (org-element-property :hour-end timestamp)))
  3249. (concat
  3250. (funcall
  3251. build-ts-string (encode-time
  3252. 0
  3253. (or minute-start 0)
  3254. (or hour-start 0)
  3255. (org-element-property :day-start timestamp)
  3256. (org-element-property :month-start timestamp)
  3257. (org-element-property :year-start timestamp))
  3258. (eq type 'active-range)
  3259. (and hour-start minute-start))
  3260. "--"
  3261. (funcall build-ts-string
  3262. (encode-time 0
  3263. (or minute-end 0)
  3264. (or hour-end 0)
  3265. (org-element-property :day-end timestamp)
  3266. (org-element-property :month-end timestamp)
  3267. (org-element-property :year-end timestamp))
  3268. (eq type 'active-range)
  3269. (and hour-end minute-end))))))))
  3270. (defun org-element-timestamp-successor ()
  3271. "Search for the next timestamp object.
  3272. Return value is a cons cell whose CAR is `timestamp' and CDR is
  3273. beginning position."
  3274. (save-excursion
  3275. (when (re-search-forward
  3276. (concat org-ts-regexp-both
  3277. "\\|"
  3278. "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  3279. "\\|"
  3280. "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
  3281. nil t)
  3282. (cons 'timestamp (match-beginning 0)))))
  3283. ;;;; Underline
  3284. (defun org-element-underline-parser ()
  3285. "Parse underline object at point.
  3286. Return a list whose CAR is `underline' and CDR is a plist with
  3287. `:begin', `:end', `:contents-begin' and `:contents-end' and
  3288. `:post-blank' keywords.
  3289. Assume point is at the first underscore marker."
  3290. (save-excursion
  3291. (unless (bolp) (backward-char 1))
  3292. (looking-at org-emph-re)
  3293. (let ((begin (match-beginning 2))
  3294. (contents-begin (match-beginning 4))
  3295. (contents-end (match-end 4))
  3296. (post-blank (progn (goto-char (match-end 2))
  3297. (skip-chars-forward " \t")))
  3298. (end (point)))
  3299. (list 'underline
  3300. (list :begin begin
  3301. :end end
  3302. :contents-begin contents-begin
  3303. :contents-end contents-end
  3304. :post-blank post-blank)))))
  3305. (defun org-element-underline-interpreter (underline contents)
  3306. "Interpret UNDERLINE object as Org syntax.
  3307. CONTENTS is the contents of the object."
  3308. (format "_%s_" contents))
  3309. ;;;; Verbatim
  3310. (defun org-element-verbatim-parser ()
  3311. "Parse verbatim object at point.
  3312. Return a list whose CAR is `verbatim' and CDR is a plist with
  3313. `:value', `:begin', `:end' and `:post-blank' keywords.
  3314. Assume point is at the first equal sign marker."
  3315. (save-excursion
  3316. (unless (bolp) (backward-char 1))
  3317. (looking-at org-emph-re)
  3318. (let ((begin (match-beginning 2))
  3319. (value (org-match-string-no-properties 4))
  3320. (post-blank (progn (goto-char (match-end 2))
  3321. (skip-chars-forward " \t")))
  3322. (end (point)))
  3323. (list 'verbatim
  3324. (list :value value
  3325. :begin begin
  3326. :end end
  3327. :post-blank post-blank)))))
  3328. (defun org-element-verbatim-interpreter (verbatim contents)
  3329. "Interpret VERBATIM object as Org syntax.
  3330. CONTENTS is nil."
  3331. (format "=%s=" (org-element-property :value verbatim)))
  3332. ;;; Parsing Element Starting At Point
  3333. ;;
  3334. ;; `org-element--current-element' is the core function of this section.
  3335. ;; It returns the Lisp representation of the element starting at
  3336. ;; point.
  3337. ;;
  3338. ;; `org-element--current-element' makes use of special modes. They
  3339. ;; are activated for fixed element chaining (e.g., `plain-list' >
  3340. ;; `item') or fixed conditional element chaining (e.g., `headline' >
  3341. ;; `section'). Special modes are: `first-section', `item',
  3342. ;; `node-property', `section' and `table-row'.
  3343. (defun org-element--current-element
  3344. (limit &optional granularity special structure)
  3345. "Parse the element starting at point.
  3346. Return value is a list like (TYPE PROPS) where TYPE is the type
  3347. of the element and PROPS a plist of properties associated to the
  3348. element.
  3349. Possible types are defined in `org-element-all-elements'.
  3350. LIMIT bounds the search.
  3351. Optional argument GRANULARITY determines the depth of the
  3352. recursion. Allowed values are `headline', `greater-element',
  3353. `element', `object' or nil. When it is broader than `object' (or
  3354. nil), secondary values will not be parsed, since they only
  3355. contain objects.
  3356. Optional argument SPECIAL, when non-nil, can be either
  3357. `first-section', `item', `node-property', `section', and
  3358. `table-row'.
  3359. If STRUCTURE isn't provided but SPECIAL is set to `item', it will
  3360. be computed.
  3361. This function assumes point is always at the beginning of the
  3362. element it has to parse."
  3363. (save-excursion
  3364. (let ((case-fold-search t)
  3365. ;; Determine if parsing depth allows for secondary strings
  3366. ;; parsing. It only applies to elements referenced in
  3367. ;; `org-element-secondary-value-alist'.
  3368. (raw-secondary-p (and granularity (not (eq granularity 'object)))))
  3369. (cond
  3370. ;; Item.
  3371. ((eq special 'item)
  3372. (org-element-item-parser limit structure raw-secondary-p))
  3373. ;; Table Row.
  3374. ((eq special 'table-row) (org-element-table-row-parser limit))
  3375. ;; Node Property.
  3376. ((eq special 'node-property) (org-element-node-property-parser limit))
  3377. ;; Headline.
  3378. ((org-with-limited-levels (org-at-heading-p))
  3379. (org-element-headline-parser limit raw-secondary-p))
  3380. ;; Sections (must be checked after headline).
  3381. ((eq special 'section) (org-element-section-parser limit))
  3382. ((eq special 'first-section)
  3383. (org-element-section-parser
  3384. (or (save-excursion (org-with-limited-levels (outline-next-heading)))
  3385. limit)))
  3386. ;; When not at bol, point is at the beginning of an item or
  3387. ;; a footnote definition: next item is always a paragraph.
  3388. ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
  3389. ;; Planning and Clock.
  3390. ((looking-at org-planning-or-clock-line-re)
  3391. (if (equal (match-string 1) org-clock-string)
  3392. (org-element-clock-parser limit)
  3393. (org-element-planning-parser limit)))
  3394. ;; Inlinetask.
  3395. ((org-at-heading-p)
  3396. (org-element-inlinetask-parser limit raw-secondary-p))
  3397. ;; From there, elements can have affiliated keywords.
  3398. (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
  3399. (cond
  3400. ;; Jumping over affiliated keywords put point off-limits.
  3401. ;; Parse them as regular keywords.
  3402. ((and (cdr affiliated) (>= (point) limit))
  3403. (goto-char (car affiliated))
  3404. (org-element-keyword-parser limit nil))
  3405. ;; LaTeX Environment.
  3406. ((looking-at
  3407. "[ \t]*\\\\begin{[A-Za-z0-9*]+}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
  3408. (org-element-latex-environment-parser limit affiliated))
  3409. ;; Drawer and Property Drawer.
  3410. ((looking-at org-drawer-regexp)
  3411. (if (equal (match-string 1) "PROPERTIES")
  3412. (org-element-property-drawer-parser limit affiliated)
  3413. (org-element-drawer-parser limit affiliated)))
  3414. ;; Fixed Width
  3415. ((looking-at "[ \t]*:\\( \\|$\\)")
  3416. (org-element-fixed-width-parser limit affiliated))
  3417. ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
  3418. ;; Keywords.
  3419. ((looking-at "[ \t]*#")
  3420. (goto-char (match-end 0))
  3421. (cond ((looking-at "\\(?: \\|$\\)")
  3422. (beginning-of-line)
  3423. (org-element-comment-parser limit affiliated))
  3424. ((looking-at "\\+BEGIN_\\(\\S-+\\)")
  3425. (beginning-of-line)
  3426. (let ((parser (assoc (upcase (match-string 1))
  3427. org-element-block-name-alist)))
  3428. (if parser (funcall (cdr parser) limit affiliated)
  3429. (org-element-special-block-parser limit affiliated))))
  3430. ((looking-at "\\+CALL:")
  3431. (beginning-of-line)
  3432. (org-element-babel-call-parser limit affiliated))
  3433. ((looking-at "\\+BEGIN:? ")
  3434. (beginning-of-line)
  3435. (org-element-dynamic-block-parser limit affiliated))
  3436. ((looking-at "\\+\\S-+:")
  3437. (beginning-of-line)
  3438. (org-element-keyword-parser limit affiliated))
  3439. (t
  3440. (beginning-of-line)
  3441. (org-element-paragraph-parser limit affiliated))))
  3442. ;; Footnote Definition.
  3443. ((looking-at org-footnote-definition-re)
  3444. (org-element-footnote-definition-parser limit affiliated))
  3445. ;; Horizontal Rule.
  3446. ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
  3447. (org-element-horizontal-rule-parser limit affiliated))
  3448. ;; Diary Sexp.
  3449. ((looking-at "%%(")
  3450. (org-element-diary-sexp-parser limit affiliated))
  3451. ;; Table.
  3452. ((org-at-table-p t) (org-element-table-parser limit affiliated))
  3453. ;; List.
  3454. ((looking-at (org-item-re))
  3455. (org-element-plain-list-parser
  3456. limit affiliated
  3457. (or structure (org-element--list-struct limit))))
  3458. ;; Default element: Paragraph.
  3459. (t (org-element-paragraph-parser limit affiliated)))))))))
  3460. ;; Most elements can have affiliated keywords. When looking for an
  3461. ;; element beginning, we want to move before them, as they belong to
  3462. ;; that element, and, in the meantime, collect information they give
  3463. ;; into appropriate properties. Hence the following function.
  3464. (defun org-element--collect-affiliated-keywords (limit)
  3465. "Collect affiliated keywords from point down to LIMIT.
  3466. Return a list whose CAR is the position at the first of them and
  3467. CDR a plist of keywords and values and move point to the
  3468. beginning of the first line after them.
  3469. As a special case, if element doesn't start at the beginning of
  3470. the line (e.g., a paragraph starting an item), CAR is current
  3471. position of point and CDR is nil."
  3472. (if (not (bolp)) (list (point))
  3473. (let ((case-fold-search t)
  3474. (origin (point))
  3475. ;; RESTRICT is the list of objects allowed in parsed
  3476. ;; keywords value.
  3477. (restrict (org-element-restriction 'keyword))
  3478. output)
  3479. (while (and (< (point) limit) (looking-at org-element--affiliated-re))
  3480. (let* ((raw-kwd (upcase (match-string 1)))
  3481. ;; Apply translation to RAW-KWD. From there, KWD is
  3482. ;; the official keyword.
  3483. (kwd (or (cdr (assoc raw-kwd
  3484. org-element-keyword-translation-alist))
  3485. raw-kwd))
  3486. ;; Find main value for any keyword.
  3487. (value
  3488. (save-match-data
  3489. (org-trim
  3490. (buffer-substring-no-properties
  3491. (match-end 0) (point-at-eol)))))
  3492. ;; PARSEDP is non-nil when keyword should have its
  3493. ;; value parsed.
  3494. (parsedp (member kwd org-element-parsed-keywords))
  3495. ;; If KWD is a dual keyword, find its secondary
  3496. ;; value. Maybe parse it.
  3497. (dualp (member kwd org-element-dual-keywords))
  3498. (dual-value
  3499. (and dualp
  3500. (let ((sec (org-match-string-no-properties 2)))
  3501. (if (or (not sec) (not parsedp)) sec
  3502. (org-element-parse-secondary-string sec restrict)))))
  3503. ;; Attribute a property name to KWD.
  3504. (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
  3505. ;; Now set final shape for VALUE.
  3506. (when parsedp
  3507. (setq value (org-element-parse-secondary-string value restrict)))
  3508. (when dualp
  3509. (setq value (and (or value dual-value) (cons value dual-value))))
  3510. (when (or (member kwd org-element-multiple-keywords)
  3511. ;; Attributes can always appear on multiple lines.
  3512. (string-match "^ATTR_" kwd))
  3513. (setq value (cons value (plist-get output kwd-sym))))
  3514. ;; Eventually store the new value in OUTPUT.
  3515. (setq output (plist-put output kwd-sym value))
  3516. ;; Move to next keyword.
  3517. (forward-line)))
  3518. ;; If affiliated keywords are orphaned: move back to first one.
  3519. ;; They will be parsed as a paragraph.
  3520. (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
  3521. ;; Return value.
  3522. (cons origin output))))
  3523. ;;; The Org Parser
  3524. ;;
  3525. ;; The two major functions here are `org-element-parse-buffer', which
  3526. ;; parses Org syntax inside the current buffer, taking into account
  3527. ;; region, narrowing, or even visibility if specified, and
  3528. ;; `org-element-parse-secondary-string', which parses objects within
  3529. ;; a given string.
  3530. ;;
  3531. ;; The (almost) almighty `org-element-map' allows to apply a function
  3532. ;; on elements or objects matching some type, and accumulate the
  3533. ;; resulting values. In an export situation, it also skips unneeded
  3534. ;; parts of the parse tree.
  3535. (defun org-element-parse-buffer (&optional granularity visible-only)
  3536. "Recursively parse the buffer and return structure.
  3537. If narrowing is in effect, only parse the visible part of the
  3538. buffer.
  3539. Optional argument GRANULARITY determines the depth of the
  3540. recursion. It can be set to the following symbols:
  3541. `headline' Only parse headlines.
  3542. `greater-element' Don't recurse into greater elements excepted
  3543. headlines and sections. Thus, elements
  3544. parsed are the top-level ones.
  3545. `element' Parse everything but objects and plain text.
  3546. `object' Parse the complete buffer (default).
  3547. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3548. elements.
  3549. An element or an objects is represented as a list with the
  3550. pattern (TYPE PROPERTIES CONTENTS), where :
  3551. TYPE is a symbol describing the element or object. See
  3552. `org-element-all-elements' and `org-element-all-objects' for an
  3553. exhaustive list of such symbols. One can retrieve it with
  3554. `org-element-type' function.
  3555. PROPERTIES is the list of attributes attached to the element or
  3556. object, as a plist. Although most of them are specific to the
  3557. element or object type, all types share `:begin', `:end',
  3558. `:post-blank' and `:parent' properties, which respectively
  3559. refer to buffer position where the element or object starts,
  3560. ends, the number of white spaces or blank lines after it, and
  3561. the element or object containing it. Properties values can be
  3562. obtained by using `org-element-property' function.
  3563. CONTENTS is a list of elements, objects or raw strings
  3564. contained in the current element or object, when applicable.
  3565. One can access them with `org-element-contents' function.
  3566. The Org buffer has `org-data' as type and nil as properties.
  3567. `org-element-map' function can be used to find specific elements
  3568. or objects within the parse tree.
  3569. This function assumes that current major mode is `org-mode'."
  3570. (save-excursion
  3571. (goto-char (point-min))
  3572. (org-skip-whitespace)
  3573. (org-element--parse-elements
  3574. (point-at-bol) (point-max)
  3575. ;; Start in `first-section' mode so text before the first
  3576. ;; headline belongs to a section.
  3577. 'first-section nil granularity visible-only (list 'org-data nil))))
  3578. (defun org-element-parse-secondary-string (string restriction &optional parent)
  3579. "Recursively parse objects in STRING and return structure.
  3580. RESTRICTION is a symbol limiting the object types that will be
  3581. looked after.
  3582. Optional argument PARENT, when non-nil, is the element or object
  3583. containing the secondary string. It is used to set correctly
  3584. `:parent' property within the string."
  3585. ;; Copy buffer-local variables listed in
  3586. ;; `org-element-object-variables' into temporary buffer. This is
  3587. ;; required since object parsing is dependent on these variables.
  3588. (let ((pairs (delq nil (mapcar (lambda (var)
  3589. (when (boundp var)
  3590. (cons var (symbol-value var))))
  3591. org-element-object-variables))))
  3592. (with-temp-buffer
  3593. (mapc (lambda (pair) (org-set-local (car pair) (cdr pair))) pairs)
  3594. (insert string)
  3595. (let ((secondary (org-element--parse-objects
  3596. (point-min) (point-max) nil restriction)))
  3597. (when parent
  3598. (mapc (lambda (obj) (org-element-put-property obj :parent parent))
  3599. secondary))
  3600. secondary))))
  3601. (defun org-element-map
  3602. (data types fun &optional info first-match no-recursion with-affiliated)
  3603. "Map a function on selected elements or objects.
  3604. DATA is a parse tree, an element, an object, a string, or a list
  3605. of such constructs. TYPES is a symbol or list of symbols of
  3606. elements or objects types (see `org-element-all-elements' and
  3607. `org-element-all-objects' for a complete list of types). FUN is
  3608. the function called on the matching element or object. It has to
  3609. accept one argument: the element or object itself.
  3610. When optional argument INFO is non-nil, it should be a plist
  3611. holding export options. In that case, parts of the parse tree
  3612. not exportable according to that property list will be skipped.
  3613. When optional argument FIRST-MATCH is non-nil, stop at the first
  3614. match for which FUN doesn't return nil, and return that value.
  3615. Optional argument NO-RECURSION is a symbol or a list of symbols
  3616. representing elements or objects types. `org-element-map' won't
  3617. enter any recursive element or object whose type belongs to that
  3618. list. Though, FUN can still be applied on them.
  3619. When optional argument WITH-AFFILIATED is non-nil, FUN will also
  3620. apply to matching objects within parsed affiliated keywords (see
  3621. `org-element-parsed-keywords').
  3622. Nil values returned from FUN do not appear in the results.
  3623. Examples:
  3624. ---------
  3625. Assuming TREE is a variable containing an Org buffer parse tree,
  3626. the following example will return a flat list of all `src-block'
  3627. and `example-block' elements in it:
  3628. \(org-element-map tree '(example-block src-block) 'identity)
  3629. The following snippet will find the first headline with a level
  3630. of 1 and a \"phone\" tag, and will return its beginning position:
  3631. \(org-element-map tree 'headline
  3632. \(lambda (hl)
  3633. \(and (= (org-element-property :level hl) 1)
  3634. \(member \"phone\" (org-element-property :tags hl))
  3635. \(org-element-property :begin hl)))
  3636. nil t)
  3637. The next example will return a flat list of all `plain-list' type
  3638. elements in TREE that are not a sub-list themselves:
  3639. \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
  3640. Eventually, this example will return a flat list of all `bold'
  3641. type objects containing a `latex-snippet' type object, even
  3642. looking into captions:
  3643. \(org-element-map tree 'bold
  3644. \(lambda (b)
  3645. \(and (org-element-map b 'latex-snippet 'identity nil t) b))
  3646. nil nil nil t)"
  3647. ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
  3648. (unless (listp types) (setq types (list types)))
  3649. (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
  3650. ;; Recursion depth is determined by --CATEGORY.
  3651. (let* ((--category
  3652. (catch 'found
  3653. (let ((category 'greater-elements))
  3654. (mapc (lambda (type)
  3655. (cond ((or (memq type org-element-all-objects)
  3656. (eq type 'plain-text))
  3657. ;; If one object is found, the function
  3658. ;; has to recurse into every object.
  3659. (throw 'found 'objects))
  3660. ((not (memq type org-element-greater-elements))
  3661. ;; If one regular element is found, the
  3662. ;; function has to recurse, at least,
  3663. ;; into every element it encounters.
  3664. (and (not (eq category 'elements))
  3665. (setq category 'elements)))))
  3666. types)
  3667. category)))
  3668. ;; Compute properties for affiliated keywords if necessary.
  3669. (--affiliated-alist
  3670. (and with-affiliated
  3671. (mapcar (lambda (kwd)
  3672. (cons kwd (intern (concat ":" (downcase kwd)))))
  3673. org-element-affiliated-keywords)))
  3674. --acc
  3675. --walk-tree
  3676. (--walk-tree
  3677. (function
  3678. (lambda (--data)
  3679. ;; Recursively walk DATA. INFO, if non-nil, is a plist
  3680. ;; holding contextual information.
  3681. (let ((--type (org-element-type --data)))
  3682. (cond
  3683. ((not --data))
  3684. ;; Ignored element in an export context.
  3685. ((and info (memq --data (plist-get info :ignore-list))))
  3686. ;; List of elements or objects.
  3687. ((not --type) (mapc --walk-tree --data))
  3688. ;; Unconditionally enter parse trees.
  3689. ((eq --type 'org-data)
  3690. (mapc --walk-tree (org-element-contents --data)))
  3691. (t
  3692. ;; Check if TYPE is matching among TYPES. If so,
  3693. ;; apply FUN to --DATA and accumulate return value
  3694. ;; into --ACC (or exit if FIRST-MATCH is non-nil).
  3695. (when (memq --type types)
  3696. (let ((result (funcall fun --data)))
  3697. (cond ((not result))
  3698. (first-match (throw '--map-first-match result))
  3699. (t (push result --acc)))))
  3700. ;; If --DATA has a secondary string that can contain
  3701. ;; objects with their type among TYPES, look into it.
  3702. (when (and (eq --category 'objects) (not (stringp --data)))
  3703. (let ((sec-prop
  3704. (assq --type org-element-secondary-value-alist)))
  3705. (when sec-prop
  3706. (funcall --walk-tree
  3707. (org-element-property (cdr sec-prop) --data)))))
  3708. ;; If --DATA has any affiliated keywords and
  3709. ;; WITH-AFFILIATED is non-nil, look for objects in
  3710. ;; them.
  3711. (when (and with-affiliated
  3712. (eq --category 'objects)
  3713. (memq --type org-element-all-elements))
  3714. (mapc (lambda (kwd-pair)
  3715. (let ((kwd (car kwd-pair))
  3716. (value (org-element-property
  3717. (cdr kwd-pair) --data)))
  3718. ;; Pay attention to the type of value.
  3719. ;; Preserve order for multiple keywords.
  3720. (cond
  3721. ((not value))
  3722. ((and (member kwd org-element-multiple-keywords)
  3723. (member kwd org-element-dual-keywords))
  3724. (mapc (lambda (line)
  3725. (funcall --walk-tree (cdr line))
  3726. (funcall --walk-tree (car line)))
  3727. (reverse value)))
  3728. ((member kwd org-element-multiple-keywords)
  3729. (mapc (lambda (line) (funcall --walk-tree line))
  3730. (reverse value)))
  3731. ((member kwd org-element-dual-keywords)
  3732. (funcall --walk-tree (cdr value))
  3733. (funcall --walk-tree (car value)))
  3734. (t (funcall --walk-tree value)))))
  3735. --affiliated-alist))
  3736. ;; Determine if a recursion into --DATA is possible.
  3737. (cond
  3738. ;; --TYPE is explicitly removed from recursion.
  3739. ((memq --type no-recursion))
  3740. ;; --DATA has no contents.
  3741. ((not (org-element-contents --data)))
  3742. ;; Looking for greater elements but --DATA is simply
  3743. ;; an element or an object.
  3744. ((and (eq --category 'greater-elements)
  3745. (not (memq --type org-element-greater-elements))))
  3746. ;; Looking for elements but --DATA is an object.
  3747. ((and (eq --category 'elements)
  3748. (memq --type org-element-all-objects)))
  3749. ;; In any other case, map contents.
  3750. (t (mapc --walk-tree (org-element-contents --data)))))))))))
  3751. (catch '--map-first-match
  3752. (funcall --walk-tree data)
  3753. ;; Return value in a proper order.
  3754. (nreverse --acc))))
  3755. (put 'org-element-map 'lisp-indent-function 2)
  3756. ;; The following functions are internal parts of the parser.
  3757. ;;
  3758. ;; The first one, `org-element--parse-elements' acts at the element's
  3759. ;; level.
  3760. ;;
  3761. ;; The second one, `org-element--parse-objects' applies on all objects
  3762. ;; of a paragraph or a secondary string. It uses
  3763. ;; `org-element--get-next-object-candidates' to optimize the search of
  3764. ;; the next object in the buffer.
  3765. ;;
  3766. ;; More precisely, that function looks for every allowed object type
  3767. ;; first. Then, it discards failed searches, keeps further matches,
  3768. ;; and searches again types matched behind point, for subsequent
  3769. ;; calls. Thus, searching for a given type fails only once, and every
  3770. ;; object is searched only once at top level (but sometimes more for
  3771. ;; nested types).
  3772. (defun org-element--parse-elements
  3773. (beg end special structure granularity visible-only acc)
  3774. "Parse elements between BEG and END positions.
  3775. SPECIAL prioritize some elements over the others. It can be set
  3776. to `first-section', `section' `item' or `table-row'.
  3777. When value is `item', STRUCTURE will be used as the current list
  3778. structure.
  3779. GRANULARITY determines the depth of the recursion. See
  3780. `org-element-parse-buffer' for more information.
  3781. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3782. elements.
  3783. Elements are accumulated into ACC."
  3784. (save-excursion
  3785. (goto-char beg)
  3786. ;; Visible only: skip invisible parts at the beginning of the
  3787. ;; element.
  3788. (when (and visible-only (org-invisible-p2))
  3789. (goto-char (min (1+ (org-find-visible)) end)))
  3790. ;; When parsing only headlines, skip any text before first one.
  3791. (when (and (eq granularity 'headline) (not (org-at-heading-p)))
  3792. (org-with-limited-levels (outline-next-heading)))
  3793. ;; Main loop start.
  3794. (while (< (point) end)
  3795. ;; Find current element's type and parse it accordingly to
  3796. ;; its category.
  3797. (let* ((element (org-element--current-element
  3798. end granularity special structure))
  3799. (type (org-element-type element))
  3800. (cbeg (org-element-property :contents-begin element)))
  3801. (goto-char (org-element-property :end element))
  3802. ;; Visible only: skip invisible parts between siblings.
  3803. (when (and visible-only (org-invisible-p2))
  3804. (goto-char (min (1+ (org-find-visible)) end)))
  3805. ;; Fill ELEMENT contents by side-effect.
  3806. (cond
  3807. ;; If element has no contents, don't modify it.
  3808. ((not cbeg))
  3809. ;; Greater element: parse it between `contents-begin' and
  3810. ;; `contents-end'. Make sure GRANULARITY allows the
  3811. ;; recursion, or ELEMENT is a headline, in which case going
  3812. ;; inside is mandatory, in order to get sub-level headings.
  3813. ((and (memq type org-element-greater-elements)
  3814. (or (memq granularity '(element object nil))
  3815. (and (eq granularity 'greater-element)
  3816. (eq type 'section))
  3817. (eq type 'headline)))
  3818. (org-element--parse-elements
  3819. cbeg (org-element-property :contents-end element)
  3820. ;; Possibly switch to a special mode.
  3821. (case type
  3822. (headline 'section)
  3823. (plain-list 'item)
  3824. (property-drawer 'node-property)
  3825. (table 'table-row))
  3826. (and (memq type '(item plain-list))
  3827. (org-element-property :structure element))
  3828. granularity visible-only element))
  3829. ;; ELEMENT has contents. Parse objects inside, if
  3830. ;; GRANULARITY allows it.
  3831. ((memq granularity '(object nil))
  3832. (org-element--parse-objects
  3833. cbeg (org-element-property :contents-end element) element
  3834. (org-element-restriction type))))
  3835. (org-element-adopt-elements acc element)))
  3836. ;; Return result.
  3837. acc))
  3838. (defun org-element--parse-objects (beg end acc restriction)
  3839. "Parse objects between BEG and END and return recursive structure.
  3840. Objects are accumulated in ACC.
  3841. RESTRICTION is a list of object successors which are allowed in
  3842. the current object."
  3843. (let ((candidates 'initial))
  3844. (save-excursion
  3845. (save-restriction
  3846. (narrow-to-region beg end)
  3847. (goto-char (point-min))
  3848. (while (and (not (eobp))
  3849. (setq candidates
  3850. (org-element--get-next-object-candidates
  3851. restriction candidates)))
  3852. (let ((next-object
  3853. (let ((pos (apply 'min (mapcar 'cdr candidates))))
  3854. (save-excursion
  3855. (goto-char pos)
  3856. (funcall (intern (format "org-element-%s-parser"
  3857. (car (rassq pos candidates)))))))))
  3858. ;; 1. Text before any object. Untabify it.
  3859. (let ((obj-beg (org-element-property :begin next-object)))
  3860. (unless (= (point) obj-beg)
  3861. (setq acc
  3862. (org-element-adopt-elements
  3863. acc
  3864. (replace-regexp-in-string
  3865. "\t" (make-string tab-width ? )
  3866. (buffer-substring-no-properties (point) obj-beg))))))
  3867. ;; 2. Object...
  3868. (let ((obj-end (org-element-property :end next-object))
  3869. (cont-beg (org-element-property :contents-begin next-object)))
  3870. ;; Fill contents of NEXT-OBJECT by side-effect, if it has
  3871. ;; a recursive type.
  3872. (when (and cont-beg
  3873. (memq (car next-object) org-element-recursive-objects))
  3874. (org-element--parse-objects
  3875. cont-beg (org-element-property :contents-end next-object)
  3876. next-object (org-element-restriction next-object)))
  3877. (setq acc (org-element-adopt-elements acc next-object))
  3878. (goto-char obj-end))))
  3879. ;; 3. Text after last object. Untabify it.
  3880. (unless (eobp)
  3881. (setq acc
  3882. (org-element-adopt-elements
  3883. acc
  3884. (replace-regexp-in-string
  3885. "\t" (make-string tab-width ? )
  3886. (buffer-substring-no-properties (point) end)))))
  3887. ;; Result.
  3888. acc))))
  3889. (defun org-element--get-next-object-candidates (restriction objects)
  3890. "Return an alist of candidates for the next object.
  3891. RESTRICTION is a list of object types, as symbols. Only
  3892. candidates with such types are looked after.
  3893. OBJECTS is the previous candidates alist. If it is set to
  3894. `initial', no search has been done before, and all symbols in
  3895. RESTRICTION should be looked after.
  3896. Return value is an alist whose CAR is the object type and CDR its
  3897. beginning position."
  3898. (delq
  3899. nil
  3900. (if (eq objects 'initial)
  3901. ;; When searching for the first time, look for every successor
  3902. ;; allowed in RESTRICTION.
  3903. (mapcar
  3904. (lambda (res)
  3905. (funcall (intern (format "org-element-%s-successor" res))))
  3906. restriction)
  3907. ;; Focus on objects returned during last search. Keep those
  3908. ;; still after point. Search again objects before it.
  3909. (mapcar
  3910. (lambda (obj)
  3911. (if (>= (cdr obj) (point)) obj
  3912. (let* ((type (car obj))
  3913. (succ (or (cdr (assq type org-element-object-successor-alist))
  3914. type)))
  3915. (and succ
  3916. (funcall (intern (format "org-element-%s-successor" succ)))))))
  3917. objects))))
  3918. ;;; Towards A Bijective Process
  3919. ;;
  3920. ;; The parse tree obtained with `org-element-parse-buffer' is really
  3921. ;; a snapshot of the corresponding Org buffer. Therefore, it can be
  3922. ;; interpreted and expanded into a string with canonical Org syntax.
  3923. ;; Hence `org-element-interpret-data'.
  3924. ;;
  3925. ;; The function relies internally on
  3926. ;; `org-element--interpret-affiliated-keywords'.
  3927. ;;;###autoload
  3928. (defun org-element-interpret-data (data &optional pseudo-objects)
  3929. "Interpret DATA as Org syntax.
  3930. DATA is a parse tree, an element, an object or a secondary string
  3931. to interpret.
  3932. Optional argument PSEUDO-OBJECTS is a list of symbols defining
  3933. new types that should be treated as objects. An unknown type not
  3934. belonging to this list is seen as a pseudo-element instead. Both
  3935. pseudo-objects and pseudo-elements are transparent entities, i.e.
  3936. only their contents are interpreted.
  3937. Return Org syntax as a string."
  3938. (org-element--interpret-data-1 data nil pseudo-objects))
  3939. (defun org-element--interpret-data-1 (data parent pseudo-objects)
  3940. "Interpret DATA as Org syntax.
  3941. DATA is a parse tree, an element, an object or a secondary string
  3942. to interpret. PARENT is used for recursive calls. It contains
  3943. the element or object containing data, or nil. PSEUDO-OBJECTS
  3944. are list of symbols defining new element or object types.
  3945. Unknown types that don't belong to this list are treated as
  3946. pseudo-elements instead.
  3947. Return Org syntax as a string."
  3948. (let* ((type (org-element-type data))
  3949. ;; Find interpreter for current object or element. If it
  3950. ;; doesn't exist (e.g. this is a pseudo object or element),
  3951. ;; return contents, if any.
  3952. (interpret
  3953. (let ((fun (intern (format "org-element-%s-interpreter" type))))
  3954. (if (fboundp fun) fun (lambda (data contents) contents))))
  3955. (results
  3956. (cond
  3957. ;; Secondary string.
  3958. ((not type)
  3959. (mapconcat
  3960. (lambda (obj)
  3961. (org-element--interpret-data-1 obj parent pseudo-objects))
  3962. data ""))
  3963. ;; Full Org document.
  3964. ((eq type 'org-data)
  3965. (mapconcat
  3966. (lambda (obj)
  3967. (org-element--interpret-data-1 obj parent pseudo-objects))
  3968. (org-element-contents data) ""))
  3969. ;; Plain text: return it.
  3970. ((stringp data) data)
  3971. ;; Element or object without contents.
  3972. ((not (org-element-contents data)) (funcall interpret data nil))
  3973. ;; Element or object with contents.
  3974. (t
  3975. (funcall interpret data
  3976. ;; Recursively interpret contents.
  3977. (mapconcat
  3978. (lambda (obj)
  3979. (org-element--interpret-data-1 obj data pseudo-objects))
  3980. (org-element-contents
  3981. (if (not (memq type '(paragraph verse-block)))
  3982. data
  3983. ;; Fix indentation of elements containing
  3984. ;; objects. We ignore `table-row' elements
  3985. ;; as they are one line long anyway.
  3986. (org-element-normalize-contents
  3987. data
  3988. ;; When normalizing first paragraph of an
  3989. ;; item or a footnote-definition, ignore
  3990. ;; first line's indentation.
  3991. (and (eq type 'paragraph)
  3992. (equal data (car (org-element-contents parent)))
  3993. (memq (org-element-type parent)
  3994. '(footnote-definition item))))))
  3995. ""))))))
  3996. (if (memq type '(org-data plain-text nil)) results
  3997. ;; Build white spaces. If no `:post-blank' property is
  3998. ;; specified, assume its value is 0.
  3999. (let ((post-blank (or (org-element-property :post-blank data) 0)))
  4000. (if (or (memq type org-element-all-objects)
  4001. (memq type pseudo-objects))
  4002. (concat results (make-string post-blank ?\s))
  4003. (concat
  4004. (org-element--interpret-affiliated-keywords data)
  4005. (org-element-normalize-string results)
  4006. (make-string post-blank ?\n)))))))
  4007. (defun org-element--interpret-affiliated-keywords (element)
  4008. "Return ELEMENT's affiliated keywords as Org syntax.
  4009. If there is no affiliated keyword, return the empty string."
  4010. (let ((keyword-to-org
  4011. (function
  4012. (lambda (key value)
  4013. (let (dual)
  4014. (when (member key org-element-dual-keywords)
  4015. (setq dual (cdr value) value (car value)))
  4016. (concat "#+" key
  4017. (and dual
  4018. (format "[%s]" (org-element-interpret-data dual)))
  4019. ": "
  4020. (if (member key org-element-parsed-keywords)
  4021. (org-element-interpret-data value)
  4022. value)
  4023. "\n"))))))
  4024. (mapconcat
  4025. (lambda (prop)
  4026. (let ((value (org-element-property prop element))
  4027. (keyword (upcase (substring (symbol-name prop) 1))))
  4028. (when value
  4029. (if (or (member keyword org-element-multiple-keywords)
  4030. ;; All attribute keywords can have multiple lines.
  4031. (string-match "^ATTR_" keyword))
  4032. (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
  4033. (reverse value)
  4034. "")
  4035. (funcall keyword-to-org keyword value)))))
  4036. ;; List all ELEMENT's properties matching an attribute line or an
  4037. ;; affiliated keyword, but ignore translated keywords since they
  4038. ;; cannot belong to the property list.
  4039. (loop for prop in (nth 1 element) by 'cddr
  4040. when (let ((keyword (upcase (substring (symbol-name prop) 1))))
  4041. (or (string-match "^ATTR_" keyword)
  4042. (and
  4043. (member keyword org-element-affiliated-keywords)
  4044. (not (assoc keyword
  4045. org-element-keyword-translation-alist)))))
  4046. collect prop)
  4047. "")))
  4048. ;; Because interpretation of the parse tree must return the same
  4049. ;; number of blank lines between elements and the same number of white
  4050. ;; space after objects, some special care must be given to white
  4051. ;; spaces.
  4052. ;;
  4053. ;; The first function, `org-element-normalize-string', ensures any
  4054. ;; string different from the empty string will end with a single
  4055. ;; newline character.
  4056. ;;
  4057. ;; The second function, `org-element-normalize-contents', removes
  4058. ;; global indentation from the contents of the current element.
  4059. (defun org-element-normalize-string (s)
  4060. "Ensure string S ends with a single newline character.
  4061. If S isn't a string return it unchanged. If S is the empty
  4062. string, return it. Otherwise, return a new string with a single
  4063. newline character at its end."
  4064. (cond
  4065. ((not (stringp s)) s)
  4066. ((string= "" s) "")
  4067. (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
  4068. (replace-match "\n" nil nil s)))))
  4069. (defun org-element-normalize-contents (element &optional ignore-first)
  4070. "Normalize plain text in ELEMENT's contents.
  4071. ELEMENT must only contain plain text and objects.
  4072. If optional argument IGNORE-FIRST is non-nil, ignore first line's
  4073. indentation to compute maximal common indentation.
  4074. Return the normalized element that is element with global
  4075. indentation removed from its contents. The function assumes that
  4076. indentation is not done with TAB characters."
  4077. (let* ((min-ind most-positive-fixnum)
  4078. find-min-ind ; For byte-compiler.
  4079. (find-min-ind
  4080. (function
  4081. ;; Return minimal common indentation within BLOB. This is
  4082. ;; done by walking recursively BLOB and updating MIN-IND
  4083. ;; along the way. FIRST-FLAG is non-nil when the first
  4084. ;; string hasn't been seen yet. It is required as this
  4085. ;; string is the only one whose indentation doesn't happen
  4086. ;; after a newline character.
  4087. (lambda (blob first-flag)
  4088. (dolist (object (org-element-contents blob))
  4089. (when (and first-flag (stringp object))
  4090. (setq first-flag nil)
  4091. (string-match "\\`\\( *\\)" object)
  4092. (let ((len (length (match-string 1 object))))
  4093. ;; An indentation of zero means no string will be
  4094. ;; modified. Quit the process.
  4095. (if (zerop len) (throw 'zero (setq min-ind 0))
  4096. (setq min-ind (min len min-ind)))))
  4097. (cond
  4098. ((stringp object)
  4099. (dolist (line (delq "" (cdr (org-split-string object " *\n"))))
  4100. (setq min-ind (min (org-get-indentation line) min-ind))))
  4101. ((memq (org-element-type object) org-element-recursive-objects)
  4102. (funcall find-min-ind object first-flag))))))))
  4103. ;; Find minimal indentation in ELEMENT.
  4104. (catch 'zero (funcall find-min-ind element (not ignore-first)))
  4105. (if (or (zerop min-ind) (= min-ind most-positive-fixnum)) element
  4106. ;; Build ELEMENT back, replacing each string with the same
  4107. ;; string minus common indentation.
  4108. (let* (build ; For byte compiler.
  4109. (build
  4110. (function
  4111. (lambda (blob first-flag)
  4112. ;; Return BLOB with all its strings indentation
  4113. ;; shortened from MIN-IND white spaces. FIRST-FLAG
  4114. ;; is non-nil when the first string hasn't been seen
  4115. ;; yet.
  4116. (setcdr (cdr blob)
  4117. (mapcar
  4118. #'(lambda (object)
  4119. (when (and first-flag (stringp object))
  4120. (setq first-flag nil)
  4121. (setq object
  4122. (replace-regexp-in-string
  4123. (format "\\` \\{%d\\}" min-ind)
  4124. "" object)))
  4125. (cond
  4126. ((stringp object)
  4127. (replace-regexp-in-string
  4128. (format "\n \\{%d\\}" min-ind) "\n" object))
  4129. ((memq (org-element-type object)
  4130. org-element-recursive-objects)
  4131. (funcall build object first-flag))
  4132. (t object)))
  4133. (org-element-contents blob)))
  4134. blob))))
  4135. (funcall build element (not ignore-first))))))
  4136. ;;; Cache
  4137. ;;
  4138. ;; Implement a caching mechanism for `org-element-at-point' and
  4139. ;; `org-element-context', which see.
  4140. ;;
  4141. ;; A single public function is provided: `org-element-cache-reset'.
  4142. ;;
  4143. ;; Cache is enabled by default, but can be disabled globally with
  4144. ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
  4145. ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
  4146. ;; can be tweaked to control caching behaviour.
  4147. ;;
  4148. ;; Internally, parsed elements are stored in an AVL tree,
  4149. ;; `org-element--cache'. This tree is updated lazily: whenever
  4150. ;; a change happens to the buffer, a synchronization request is
  4151. ;; registered in `org-element--cache-sync-requests' (see
  4152. ;; `org-element--cache-submit-request'). During idle time, requests
  4153. ;; are processed by `org-element--cache-sync'. Synchronization also
  4154. ;; happens when an element is required from the cache. In this case,
  4155. ;; the process stops as soon as the needed element is up-to-date.
  4156. ;;
  4157. ;; A synchronization request can only apply on a synchronized part of
  4158. ;; the cache. Therefore, the cache is updated at least to the
  4159. ;; location where the new request applies. Thus, requests are ordered
  4160. ;; from left to right and all elements starting before the first
  4161. ;; request are correct. This property is used by functions like
  4162. ;; `org-element--cache-find' to retrieve elements in the part of the
  4163. ;; cache that can be trusted.
  4164. ;;
  4165. ;; A request applies to every element, starting from its original
  4166. ;; location (or key, see below). When a request is processed, it
  4167. ;; moves forward and may collide the next one. In this case, both
  4168. ;; requests are merged into a new one that starts from that element.
  4169. ;; As a consequence, the whole synchronization complexity does not
  4170. ;; depend on the number of pending requests, but on the number of
  4171. ;; elements the very first request will be applied on.
  4172. ;;
  4173. ;; Elements cannot be accessed through their beginning position, which
  4174. ;; may or may not be up-to-date. Instead, each element in the tree is
  4175. ;; associated to a key, obtained with `org-element--cache-key'. This
  4176. ;; mechanism is robust enough to preserve total order among elements
  4177. ;; even when the tree is only partially synchronized.
  4178. ;;
  4179. ;; Objects contained in an element are stored in a hash table,
  4180. ;; `org-element--cache-objects'.
  4181. (defvar org-element-use-cache t
  4182. "Non nil when Org parser should cache its results.
  4183. This is mostly for debugging purpose.")
  4184. (defvar org-element-cache-sync-idle-time 0.4
  4185. "Length, in seconds, of idle time before syncing cache.")
  4186. (defvar org-element-cache-sync-duration (seconds-to-time 0.04)
  4187. "Maximum duration, as a time value, for a cache synchronization.
  4188. If the synchronization is not over after this delay, the process
  4189. pauses and resumes after `org-element-cache-sync-break'
  4190. seconds.")
  4191. (defvar org-element-cache-sync-break (seconds-to-time 0.2)
  4192. "Duration, as a time value, of the pause between synchronizations.
  4193. See `org-element-cache-sync-duration' for more information.")
  4194. ;;;; Data Structure
  4195. (defvar org-element--cache nil
  4196. "AVL tree used to cache elements.
  4197. Each node of the tree contains an element. Comparison is done
  4198. with `org-element--cache-compare'. This cache is used in
  4199. `org-element-at-point'.")
  4200. (defvar org-element--cache-objects nil
  4201. "Hash table used as to cache objects.
  4202. Key is an element, as returned by `org-element-at-point', and
  4203. value is an alist where each association is:
  4204. \(POS CANDIDATES . OBJECTS)
  4205. where POS is a buffer position, CANDIDATES is the last know list
  4206. of successors (see `org-element--get-next-object-candidates') in
  4207. container starting at POS and OBJECTS is a list of objects known
  4208. to live within that container, from farthest to closest.
  4209. In the following example, \\alpha, bold object and \\beta start
  4210. at, respectively, positions 1, 7 and 8,
  4211. \\alpha *\\beta*
  4212. If the paragraph is completely parsed, OBJECTS-DATA will be
  4213. \((1 nil BOLD-OBJECT ENTITY-OBJECT)
  4214. \(8 nil ENTITY-OBJECT))
  4215. whereas in a partially parsed paragraph, it could be
  4216. \((1 ((entity . 1) (bold . 7)) ENTITY-OBJECT))
  4217. This cache is used in `org-element-context'.")
  4218. (defvar org-element--cache-sync-requests nil
  4219. "List of pending synchronization requests.
  4220. A request is a vector with the following pattern:
  4221. \[NEXT END OFFSET PARENT PHASE]
  4222. Processing a synchronization request consists in three phases:
  4223. 0. Delete modified elements,
  4224. 1. Fill missing area in cache,
  4225. 2. Shift positions and re-parent elements after the changes.
  4226. During phase 0, NEXT is the key of the first element to be
  4227. removed and END is buffer position delimiting the modifications.
  4228. Every element starting between these are removed. PARENT is an
  4229. element to be removed. Every element contained in it will also
  4230. be removed.
  4231. During phase 1, NEXT is the key of the next known element in
  4232. cache. Parse buffer between that element and the one before it
  4233. in order to determine the parent of the next element. Set PARENT
  4234. to the element containing NEXT.
  4235. During phase 2, NEXT is the key of the next element to shift in
  4236. the parse tree. All elements starting from this one have their
  4237. properties relatives to buffer positions shifted by integer
  4238. OFFSET and, if they belong to element PARENT, are adopted by it.
  4239. PHASE specifies the phase number, as an integer.")
  4240. (defvar org-element--cache-sync-timer nil
  4241. "Timer used for cache synchronization.")
  4242. (defvar org-element--cache-sync-keys nil
  4243. "Hash table used to store keys during synchronization.
  4244. See `org-element--cache-key' for more information.")
  4245. (defsubst org-element--cache-key (element)
  4246. "Return a unique key for ELEMENT in cache tree.
  4247. Keys are used to keep a total order among elements in the cache.
  4248. Comparison is done with `org-element--cache-key-less-p'.
  4249. When no synchronization is taking place, a key is simply the
  4250. beginning position of the element, or that position plus one in
  4251. the case of an first item (respectively row) in
  4252. a list (respectively a table).
  4253. During a synchronization, the key is the one the element had when
  4254. the cache was synchronized for the last time. Elements added to
  4255. cache during the synchronization get a new key generated with
  4256. `org-element--cache-generate-key'.
  4257. Such keys are stored in `org-element--cache-sync-keys'. The hash
  4258. table is cleared once the synchronization is complete."
  4259. (or (gethash element org-element--cache-sync-keys)
  4260. (let* ((begin (org-element-property :begin element))
  4261. ;; Increase beginning position of items (respectively
  4262. ;; table rows) by one, so the first item can get
  4263. ;; a different key from its parent list (respectively
  4264. ;; table).
  4265. (key (if (memq (org-element-type element) '(item table-row))
  4266. (1+ begin)
  4267. begin)))
  4268. (if org-element--cache-sync-requests
  4269. (puthash element key org-element--cache-sync-keys)
  4270. key))))
  4271. (defconst org-element--cache-default-key (ash most-positive-fixnum -1)
  4272. "Default value for a new key level.
  4273. See `org-element--cache-generate-key' for more information.")
  4274. (defun org-element--cache-generate-key (lower upper)
  4275. "Generate a key between LOWER and UPPER.
  4276. LOWER and UPPER are integers or lists, possibly empty.
  4277. If LOWER and UPPER are equals, return LOWER. Otherwise, return
  4278. a unique key, as an integer or a list of integers, according to
  4279. the following rules:
  4280. - LOWER and UPPER are compared level-wise until values differ.
  4281. - If, at a given level, LOWER and UPPER differ from more than
  4282. 2, the new key shares all the levels above with LOWER and
  4283. gets a new level. Its value is the mean between LOWER and
  4284. UPPER.
  4285. \(1 2) + (1 4) --> (1 3)
  4286. - If LOWER has no value to compare with, it is assumed that its
  4287. value is 0:
  4288. \(1 1) + (1 1 2) --> (1 1 1)
  4289. Likewise, if UPPER is short of levels, the current value is
  4290. `most-positive-fixnum'.
  4291. - If they differ from only one, the new key inherits from
  4292. current LOWER lever and has a new level at the value
  4293. `org-element--cache-default-key'.
  4294. \(1 2) + (1 3) --> (1 2 org-element--cache-default-key)
  4295. - If the key is only one level long, it is returned as an
  4296. integer.
  4297. \(1 2) + (3 2) --> 2"
  4298. (if (equal lower upper) lower
  4299. (let ((lower (if (integerp lower) (list lower) lower))
  4300. (upper (if (integerp upper) (list upper) upper))
  4301. key)
  4302. (catch 'exit
  4303. (while (and lower upper)
  4304. (let ((lower-level (car lower))
  4305. (upper-level (car upper)))
  4306. (cond
  4307. ((= lower-level upper-level)
  4308. (push lower-level key)
  4309. (setq lower (cdr lower) upper (cdr upper)))
  4310. ((= (- upper-level lower-level) 1)
  4311. (push lower-level key)
  4312. (setq lower (cdr lower))
  4313. (while (and lower (= (car lower) most-positive-fixnum))
  4314. (push most-positive-fixnum key)
  4315. (setq lower (cdr lower)))
  4316. (push (if lower
  4317. (let ((n (car lower)))
  4318. (+ (ash (if (zerop (mod n 2)) n (1+ n)) -1)
  4319. org-element--cache-default-key))
  4320. org-element--cache-default-key)
  4321. key)
  4322. (throw 'exit t))
  4323. (t
  4324. (push (let ((n (car lower)))
  4325. (+ (ash (if (zerop (mod n 2)) n (1+ n)) -1)
  4326. (ash (car upper) -1)))
  4327. key)
  4328. (throw 'exit t)))))
  4329. (cond
  4330. ((not lower)
  4331. (while (and upper (zerop (car upper)))
  4332. (push 0 key)
  4333. (setq upper (cdr upper)))
  4334. ;; (n) is equivalent to (n 0 0 0 0 ...) so we want to avoid
  4335. ;; ending on a sequence of 0.
  4336. (if (= (car upper) 1)
  4337. (progn (push 0 key)
  4338. (push org-element--cache-default-key key))
  4339. (push (if upper (ash (car upper) -1) org-element--cache-default-key)
  4340. key)))
  4341. ((not upper)
  4342. (while (and lower (= (car lower) most-positive-fixnum))
  4343. (push most-positive-fixnum key)
  4344. (setq lower (cdr lower)))
  4345. (push (if lower
  4346. (let ((n (car lower)))
  4347. (+ (ash (if (zerop (mod n 2)) n (1+ n)) -1)
  4348. org-element--cache-default-key))
  4349. org-element--cache-default-key)
  4350. key))))
  4351. ;; Ensure we don't return a list with a single element.
  4352. (if (cdr key) (nreverse key) (car key)))))
  4353. (defsubst org-element--cache-key-less-p (a b)
  4354. "Non-nil if key A is less than key B.
  4355. A and B are either integers or lists of integers, as returned by
  4356. `org-element--cache-key'."
  4357. (if (integerp a) (if (integerp b) (< a b) (<= a (car b)))
  4358. (if (integerp b) (< (car a) b)
  4359. (catch 'exit
  4360. (while (and a b)
  4361. (cond ((car-less-than-car a b) (throw 'exit t))
  4362. ((car-less-than-car b a) (throw 'exit nil))
  4363. (t (setq a (cdr a) b (cdr b)))))
  4364. ;; If A is empty, either keys are equal (B is also empty) or
  4365. ;; B is less than A (B is longer). Therefore return nil.
  4366. ;;
  4367. ;; If A is not empty, B is necessarily empty and A is less
  4368. ;; than B (A is longer). Therefore, return a non-nil value.
  4369. a))))
  4370. (defun org-element--cache-compare (a b)
  4371. "Non-nil when element A is located before element B."
  4372. (org-element--cache-key-less-p (org-element--cache-key a)
  4373. (org-element--cache-key b)))
  4374. (defsubst org-element--cache-root ()
  4375. "Return root value in cache.
  4376. This function assumes `org-element--cache' is a valid AVL tree."
  4377. (avl-tree--node-left (avl-tree--dummyroot org-element--cache)))
  4378. ;;;; Tools
  4379. (defsubst org-element--cache-active-p ()
  4380. "Non-nil when cache is active in current buffer."
  4381. (and org-element-use-cache
  4382. (or (derived-mode-p 'org-mode) orgstruct-mode)))
  4383. (defun org-element--cache-find (pos &optional side)
  4384. "Find element in cache starting at POS or before.
  4385. POS refers to a buffer position.
  4386. When optional argument SIDE is non-nil, the function checks for
  4387. elements starting at or past POS instead. If SIDE is `both', the
  4388. function returns a cons cell where car is the first element
  4389. starting at or before POS and cdr the first element starting
  4390. after POS.
  4391. The function can only find elements in the synchronized part of
  4392. the cache."
  4393. (let ((limit (and org-element--cache-sync-requests
  4394. (aref (car org-element--cache-sync-requests) 0)))
  4395. (node (org-element--cache-root))
  4396. lower upper)
  4397. (while node
  4398. (let* ((element (avl-tree--node-data node))
  4399. (begin (org-element-property :begin element)))
  4400. (cond
  4401. ((and limit
  4402. (not (org-element--cache-key-less-p
  4403. (org-element--cache-key element) limit)))
  4404. (setq node (avl-tree--node-left node)))
  4405. ((> begin pos)
  4406. (setq upper element
  4407. node (avl-tree--node-left node)))
  4408. ((< begin pos)
  4409. (setq lower element
  4410. node (avl-tree--node-right node)))
  4411. ;; We found an element in cache starting at POS. If `side'
  4412. ;; is `both' we also want the next one in order to generate
  4413. ;; a key in-between.
  4414. ;;
  4415. ;; If the element is the first row or item in a table or
  4416. ;; a plain list, we always return the table or the plain
  4417. ;; list.
  4418. ;;
  4419. ;; In any other case, we return the element found.
  4420. ((eq side 'both)
  4421. (setq lower element)
  4422. (setq node (avl-tree--node-right node)))
  4423. ((and (memq (org-element-type element) '(item table-row))
  4424. (let ((parent (org-element-property :parent element)))
  4425. (and (= (org-element-property :begin element)
  4426. (org-element-property :contents-begin parent))
  4427. (setq node nil
  4428. lower parent
  4429. upper parent)))))
  4430. (t
  4431. (setq node nil
  4432. lower element
  4433. upper element)))))
  4434. (case side
  4435. (both (cons lower upper))
  4436. ((nil) lower)
  4437. (otherwise upper))))
  4438. (defun org-element--cache-put (element &optional data)
  4439. "Store ELEMENT in current buffer's cache, if allowed.
  4440. When optional argument DATA is non-nil, assume is it object data
  4441. relative to ELEMENT and store it in the objects cache."
  4442. (cond ((not (org-element--cache-active-p)) nil)
  4443. ((not data)
  4444. (when org-element--cache-sync-requests
  4445. ;; During synchronization, first build an appropriate key
  4446. ;; for the new element so `avl-tree-enter' can insert it at
  4447. ;; the right spot in the cache.
  4448. (let ((keys (org-element--cache-find
  4449. (org-element-property :begin element) 'both)))
  4450. (puthash element
  4451. (org-element--cache-generate-key
  4452. (and (car keys) (org-element--cache-key (car keys)))
  4453. (cond ((cdr keys) (org-element--cache-key (cdr keys)))
  4454. (org-element--cache-sync-requests
  4455. (aref (car org-element--cache-sync-requests) 0))))
  4456. org-element--cache-sync-keys)))
  4457. (avl-tree-enter org-element--cache element))
  4458. ;; Headlines are not stored in cache, so objects in titles are
  4459. ;; not stored either.
  4460. ((eq (org-element-type element) 'headline) nil)
  4461. (t (puthash element data org-element--cache-objects))))
  4462. ;;;; Synchronization
  4463. (defsubst org-element--cache-set-timer (buffer)
  4464. "Set idle timer for cache synchronization in BUFFER."
  4465. (when org-element--cache-sync-timer
  4466. (cancel-timer org-element--cache-sync-timer))
  4467. (setq org-element--cache-sync-timer
  4468. (run-with-idle-timer
  4469. (let ((idle (current-idle-time)))
  4470. (if idle (time-add idle org-element-cache-sync-break)
  4471. org-element-cache-sync-idle-time))
  4472. nil
  4473. #'org-element--cache-sync
  4474. buffer)))
  4475. (defsubst org-element--cache-interrupt-p (time-limit)
  4476. "Non-nil when synchronization process should be interrupted.
  4477. TIME-LIMIT is a time value or nil."
  4478. (and time-limit
  4479. (or (input-pending-p)
  4480. (time-less-p time-limit (current-time)))))
  4481. (defsubst org-element--cache-shift-positions (element offset &optional props)
  4482. "Shift ELEMENT properties relative to buffer positions by OFFSET.
  4483. Properties containing buffer positions are `:begin', `:end',
  4484. `:contents-begin', `:contents-end' and `:structure'. When
  4485. optional argument PROPS is a list of keywords, only shift
  4486. properties provided in that list.
  4487. Properties are modified by side-effect."
  4488. (let ((properties (nth 1 element)))
  4489. ;; Shift `:structure' property for the first plain list only: it
  4490. ;; is the only one that really matters and it prevents from
  4491. ;; shifting it more than once.
  4492. (when (and (or (not props) (memq :structure props))
  4493. (eq (org-element-type element) 'plain-list)
  4494. (not (eq (org-element-type (plist-get properties :parent))
  4495. 'item)))
  4496. (dolist (item (plist-get properties :structure))
  4497. (incf (car item) offset)
  4498. (incf (nth 6 item) offset)))
  4499. (dolist (key '(:begin :contents-begin :contents-end :end :post-affiliated))
  4500. (let ((value (and (or (not props) (memq key props))
  4501. (plist-get properties key))))
  4502. (and value (plist-put properties key (+ offset value)))))))
  4503. (defun org-element--cache-sync (buffer &optional threshold)
  4504. "Synchronize cache with recent modification in BUFFER.
  4505. When optional argument THRESHOLD is non-nil, do the
  4506. synchronization for all elements starting before or at threshold,
  4507. then exit. Otherwise, synchronize cache for as long as
  4508. `org-element-cache-sync-duration' or until Emacs leaves idle
  4509. state."
  4510. (when (buffer-live-p buffer)
  4511. (with-current-buffer buffer
  4512. (let ((inhibit-quit t) request next)
  4513. (when org-element--cache-sync-timer
  4514. (cancel-timer org-element--cache-sync-timer))
  4515. (catch 'interrupt
  4516. (while org-element--cache-sync-requests
  4517. (setq request (car org-element--cache-sync-requests)
  4518. next (nth 1 org-element--cache-sync-requests))
  4519. (or (org-element--cache-process-request
  4520. request
  4521. (and next (aref next 0))
  4522. threshold
  4523. (and (not threshold)
  4524. (time-add (current-time)
  4525. org-element-cache-sync-duration)))
  4526. (throw 'interrupt t))
  4527. ;; Request processed. Merge current and next offsets and
  4528. ;; transfer phase number and ending position.
  4529. (when next
  4530. (incf (aref next 2) (aref request 2))
  4531. (aset next 1 (aref request 1))
  4532. (aset next 4 (aref request 4)))
  4533. (setq org-element--cache-sync-requests
  4534. (cdr org-element--cache-sync-requests))))
  4535. ;; If more requests are awaiting, set idle timer accordingly.
  4536. ;; Otherwise, reset keys.
  4537. (if org-element--cache-sync-requests
  4538. (org-element--cache-set-timer buffer)
  4539. (clrhash org-element--cache-sync-keys))))))
  4540. (defun org-element--cache-process-request (request next threshold time-limit)
  4541. "Process synchronization REQUEST for all entries before NEXT.
  4542. REQUEST is a vector, built by `org-element--cache-submit-request'.
  4543. NEXT is a cache key, as returned by `org-element--cache-key'.
  4544. When non-nil, THRESHOLD is a buffer position. Synchronization
  4545. stops as soon as a shifted element begins after it.
  4546. When non-nil, TIME-LIMIT is a time value. Synchronization stops
  4547. after this time or when Emacs exits idle state.
  4548. Return nil if the process stops before completing the request,
  4549. t otherwise."
  4550. (catch 'quit
  4551. (when (= (aref request 4) 0)
  4552. ;; Phase 1.
  4553. ;;
  4554. ;; Delete all elements starting after BEG, but not after buffer
  4555. ;; position END or past element with key NEXT.
  4556. ;;
  4557. ;; As an exception, also delete elements starting after
  4558. ;; modifications but included in an element altered by
  4559. ;; modifications (orphans).
  4560. ;;
  4561. ;; At each iteration, we start again at tree root since
  4562. ;; a deletion modifies structure of the balanced tree.
  4563. (catch 'end-phase
  4564. (let ((beg (aref request 0))
  4565. (end (aref request 1))
  4566. (deleted-parent (aref request 3)))
  4567. (while t
  4568. (when (org-element--cache-interrupt-p time-limit)
  4569. (aset request 3 deleted-parent)
  4570. (throw 'quit nil))
  4571. ;; Find first element in cache with key BEG or after it.
  4572. ;; We don't use `org-element--cache-find' because it
  4573. ;; couldn't reach orphaned elements past NEXT. Moreover,
  4574. ;; BEG is a key, not a buffer position.
  4575. (let ((node (org-element--cache-root)) data data-key)
  4576. (while node
  4577. (let* ((element (avl-tree--node-data node))
  4578. (key (org-element--cache-key element)))
  4579. (cond
  4580. ((org-element--cache-key-less-p key beg)
  4581. (setq node (avl-tree--node-right node)))
  4582. ((org-element--cache-key-less-p beg key)
  4583. (setq data element
  4584. data-key key
  4585. node (avl-tree--node-left node)))
  4586. (t (setq data element
  4587. data-key key
  4588. node nil)))))
  4589. (if (not data) (throw 'quit t)
  4590. (let ((pos (org-element-property :begin data)))
  4591. (cond
  4592. ;; Remove orphaned elements.
  4593. ((and deleted-parent
  4594. (let ((up data))
  4595. (while (and
  4596. (setq up (org-element-property :parent up))
  4597. (not (eq up deleted-parent))))
  4598. up))
  4599. (avl-tree-delete org-element--cache data))
  4600. ((or (and next
  4601. (not (org-element--cache-key-less-p data-key
  4602. next)))
  4603. (> pos end))
  4604. (aset request 0 data-key)
  4605. (aset request 1 pos)
  4606. (aset request 4 1)
  4607. (throw 'end-phase nil))
  4608. (t (avl-tree-delete org-element--cache data)
  4609. (when (= (org-element-property :end data) end)
  4610. (setq deleted-parent data)))))))))))
  4611. (when (= (aref request 4) 1)
  4612. ;; Phase 2.
  4613. ;;
  4614. ;; Phase 1 left a hole in the parse tree. Some elements after
  4615. ;; it could have parents within. For example, in the following
  4616. ;; buffer:
  4617. ;;
  4618. ;;
  4619. ;; - item
  4620. ;;
  4621. ;;
  4622. ;; Paragraph1
  4623. ;;
  4624. ;; Paragraph2
  4625. ;;
  4626. ;;
  4627. ;; if we remove a blank line between "item" and "Paragraph1",
  4628. ;; everything down to "Paragraph2" is removed from cache. But
  4629. ;; the paragraph now belongs to the list, and its `:parent'
  4630. ;; property no longer is accurate.
  4631. ;;
  4632. ;; Therefore we need to parse again elements in the hole, or at
  4633. ;; least in its last section, so that we can re-parent
  4634. ;; subsequent elements, during phase 3.
  4635. ;;
  4636. ;; Note that we only need to get the parent from the first
  4637. ;; element in cache after the hole.
  4638. ;;
  4639. ;; Also, this part can be delayed if we don't need to retrieve
  4640. ;; an element after the hole.
  4641. (catch 'end-phase
  4642. ;; Next element will start at its beginning position plus
  4643. ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
  4644. ;; contains the real beginning position of the first element
  4645. ;; to shift and re-parent.
  4646. (when (equal (aref request 0) next) (throw 'quit t))
  4647. (let ((limit (+ (aref request 1) (aref request 2))))
  4648. (when (and threshold (< threshold limit)) (throw 'quit nil))
  4649. (let ((parent (org-element--parse-to limit t time-limit)))
  4650. (if (eq parent 'interrupted) (throw 'quit nil)
  4651. (aset request 3 parent)
  4652. (aset request 4 2)
  4653. (throw 'end-phase nil))))))
  4654. ;; Phase 3.
  4655. ;;
  4656. ;; Shift all elements starting from key START, but before NEXT, by
  4657. ;; OFFSET, and re-parent them when appropriate.
  4658. ;;
  4659. ;; Elements are modified by side-effect so the tree structure
  4660. ;; remains intact.
  4661. ;;
  4662. ;; Once THRESHOLD, if any, is reached, or once there is an input
  4663. ;; pending, exit. Before leaving, the current synchronization
  4664. ;; request is updated.
  4665. (let ((start (aref request 0))
  4666. (offset (aref request 2))
  4667. (parent (aref request 3))
  4668. (node (org-element--cache-root))
  4669. (stack (list nil))
  4670. (leftp t)
  4671. exit-flag)
  4672. ;; No re-parenting nor shifting planned: request is over.
  4673. (when (and (not parent) (zerop offset)) (throw 'quit t))
  4674. (while node
  4675. (let* ((data (avl-tree--node-data node))
  4676. (key (org-element--cache-key data)))
  4677. (if (and leftp (avl-tree--node-left node)
  4678. (not (org-element--cache-key-less-p key start)))
  4679. (progn (push node stack)
  4680. (setq node (avl-tree--node-left node)))
  4681. (unless (org-element--cache-key-less-p key start)
  4682. ;; We reached NEXT. Request is complete.
  4683. (when (equal key next) (throw 'quit t))
  4684. ;; Handle interruption request. Update current request.
  4685. (when (or exit-flag (org-element--cache-interrupt-p time-limit))
  4686. (aset request 0 key)
  4687. (aset request 3 parent)
  4688. (throw 'quit nil))
  4689. ;; Shift element.
  4690. (unless (zerop offset)
  4691. (org-element--cache-shift-positions data offset)
  4692. ;; Shift associated objects data, if any.
  4693. (dolist (object-data (gethash data org-element--cache-objects))
  4694. (incf (car object-data) offset)
  4695. (dolist (successor (nth 1 object-data))
  4696. (incf (cdr successor) offset))
  4697. (dolist (object (cddr object-data))
  4698. (org-element--cache-shift-positions object offset))))
  4699. (let ((begin (org-element-property :begin data)))
  4700. ;; Re-parent it.
  4701. (while (and parent
  4702. (<= (org-element-property :end parent) begin))
  4703. (setq parent (org-element-property :parent parent)))
  4704. (cond (parent (org-element-put-property data :parent parent))
  4705. ((zerop offset) (throw 'quit t)))
  4706. ;; Cache is up-to-date past THRESHOLD. Request
  4707. ;; interruption.
  4708. (when (and threshold (> begin threshold)) (setq exit-flag t))))
  4709. (setq node (if (setq leftp (avl-tree--node-right node))
  4710. (avl-tree--node-right node)
  4711. (pop stack))))))
  4712. ;; We reached end of tree: synchronization complete.
  4713. t)))
  4714. (defun org-element--parse-to (pos &optional syncp time-limit)
  4715. "Parse elements in current section, down to POS.
  4716. Start parsing from the closest between the last known element in
  4717. cache or headline above. Return the smallest element containing
  4718. POS.
  4719. When optional argument SYNCP is non-nil, return the parent of the
  4720. element containing POS instead. In that case, it is also
  4721. possible to provide TIME-LIMIT, which is a time value specifying
  4722. when the parsing should stop. The function returns `interrupted'
  4723. if the process stopped before finding the expected result."
  4724. (catch 'exit
  4725. (org-with-wide-buffer
  4726. (goto-char pos)
  4727. (let* ((cached (and (org-element--cache-active-p)
  4728. (org-element--cache-find pos nil)))
  4729. (begin (org-element-property :begin cached))
  4730. element next)
  4731. (cond
  4732. ;; Nothing in cache before point: start parsing from first
  4733. ;; element following headline above, or first element in
  4734. ;; buffer.
  4735. ((not cached)
  4736. (when (org-with-limited-levels (outline-previous-heading))
  4737. (forward-line))
  4738. (skip-chars-forward " \r\t\n")
  4739. (beginning-of-line))
  4740. ;; Cache returned exact match: return it.
  4741. ((= pos begin)
  4742. (throw 'exit (if syncp (org-element-property :parent cached) cached)))
  4743. ;; There's a headline between cached value and POS: cached
  4744. ;; value is invalid. Start parsing from first element
  4745. ;; following the headline.
  4746. ((re-search-backward
  4747. (org-with-limited-levels org-outline-regexp-bol) begin t)
  4748. (forward-line)
  4749. (skip-chars-forward " \r\t\n")
  4750. (beginning-of-line))
  4751. ;; Check if CACHED or any of its ancestors contain point.
  4752. ;;
  4753. ;; If there is such an element, we inspect it in order to know
  4754. ;; if we return it or if we need to parse its contents.
  4755. ;; Otherwise, we just start parsing from current location,
  4756. ;; which is right after the top-most element containing
  4757. ;; CACHED.
  4758. ;;
  4759. ;; As a special case, if POS is at the end of the buffer, we
  4760. ;; want to return the innermost element ending there.
  4761. ;;
  4762. ;; Also, if we find an ancestor and discover that we need to
  4763. ;; parse its contents, make sure we don't start from
  4764. ;; `:contents-begin', as we would otherwise go past CACHED
  4765. ;; again. Instead, in that situation, we will resume parsing
  4766. ;; from NEXT, which is located after CACHED or its higher
  4767. ;; ancestor not containing point.
  4768. (t
  4769. (let ((up cached)
  4770. (pos (if (= (point-max) pos) (1- pos) pos)))
  4771. (goto-char (or (org-element-property :contents-begin cached) begin))
  4772. (while (let ((end (org-element-property :end up)))
  4773. (and (<= end pos)
  4774. (goto-char end)
  4775. (setq up (org-element-property :parent up)))))
  4776. (cond ((not up))
  4777. ((eobp) (setq element up))
  4778. (t (setq element up next (point)))))))
  4779. ;; Parse successively each element until we reach POS.
  4780. (let ((end (or (org-element-property :end element)
  4781. (save-excursion
  4782. (org-with-limited-levels (outline-next-heading))
  4783. (point))))
  4784. (parent element)
  4785. special-flag)
  4786. (while t
  4787. (when syncp
  4788. (cond ((= (point) pos) (throw 'exit parent))
  4789. ((org-element--cache-interrupt-p time-limit)
  4790. (throw 'exit 'interrupted))))
  4791. (unless element
  4792. (setq element (org-element--current-element
  4793. end 'element special-flag
  4794. (org-element-property :structure parent)))
  4795. (org-element-put-property element :parent parent)
  4796. (org-element--cache-put element))
  4797. (let ((elem-end (org-element-property :end element))
  4798. (type (org-element-type element)))
  4799. (cond
  4800. ;; Skip any element ending before point. Also skip
  4801. ;; element ending at point (unless it is also the end of
  4802. ;; buffer) since we're sure that another element begins
  4803. ;; after it.
  4804. ((and (<= elem-end pos) (/= (point-max) elem-end))
  4805. (goto-char elem-end))
  4806. ;; A non-greater element contains point: return it.
  4807. ((not (memq type org-element-greater-elements))
  4808. (throw 'exit element))
  4809. ;; Otherwise, we have to decide if ELEMENT really
  4810. ;; contains POS. In that case we start parsing from
  4811. ;; contents' beginning.
  4812. ;;
  4813. ;; If POS is at contents' beginning but it is also at
  4814. ;; the beginning of the first item in a list or a table.
  4815. ;; In that case, we need to create an anchor for that
  4816. ;; list or table, so return it.
  4817. ;;
  4818. ;; Also, if POS is at the end of the buffer, no element
  4819. ;; can start after it, but more than one may end there.
  4820. ;; Arbitrarily, we choose to return the innermost of
  4821. ;; such elements.
  4822. ((let ((cbeg (org-element-property :contents-begin element))
  4823. (cend (org-element-property :contents-end element)))
  4824. (when (or syncp
  4825. (and cbeg cend
  4826. (or (< cbeg pos)
  4827. (and (= cbeg pos)
  4828. (not (memq type '(plain-list table)))))
  4829. (or (> cend pos)
  4830. (and (= cend pos) (= (point-max) pos)))))
  4831. (goto-char (or next cbeg))
  4832. (setq next nil
  4833. special-flag (case type
  4834. (plain-list 'item)
  4835. (property-drawer 'node-property)
  4836. (table 'table-row))
  4837. parent element
  4838. end cend))))
  4839. ;; Otherwise, return ELEMENT as it is the smallest
  4840. ;; element containing POS.
  4841. (t (throw 'exit element))))
  4842. (setq element nil)))))))
  4843. ;;;; Staging Buffer Changes
  4844. (defconst org-element--cache-opening-line
  4845. (concat "^[ \t]*\\(?:"
  4846. "#\\+BEGIN[:_]" "\\|"
  4847. "\\\\begin{[A-Za-z0-9]+\\*?}" "\\|"
  4848. ":\\S-+:[ \t]*$"
  4849. "\\)")
  4850. "Regexp matching an element opening line.
  4851. When such a line is modified, modifications may propagate after
  4852. modified area. In that situation, every element between that
  4853. area and next section is removed from cache.")
  4854. (defconst org-element--cache-closing-line
  4855. (concat "^[ \t]*\\(?:"
  4856. "#\\+END\\(?:_\\|:?[ \t]*$\\)" "\\|"
  4857. "\\\\end{[A-Za-z0-9]+\\*?}[ \t]*$" "\\|"
  4858. ":END:[ \t]*$"
  4859. "\\)")
  4860. "Regexp matching an element closing line.
  4861. When such a line is modified, modifications may propagate before
  4862. modified area. In that situation, every element between that
  4863. area and previous section is removed from cache.")
  4864. (defvar org-element--cache-change-warning nil
  4865. "Non-nil when a sensitive line is about to be changed.
  4866. It is a symbol among nil, t and `headline'.")
  4867. (defun org-element--cache-before-change (beg end)
  4868. "Request extension of area going to be modified if needed.
  4869. BEG and END are the beginning and end of the range of changed
  4870. text. See `before-change-functions' for more information."
  4871. (let ((inhibit-quit t))
  4872. ;; Make sure buffer positions in cache are correct until END.
  4873. (save-match-data
  4874. (org-element--cache-sync (current-buffer) end)
  4875. (org-with-wide-buffer
  4876. (goto-char beg)
  4877. (beginning-of-line)
  4878. (let ((top (point))
  4879. (bottom (save-excursion (goto-char end) (line-end-position)))
  4880. (sensitive-re
  4881. ;; A sensitive line is a headline or a block (or drawer,
  4882. ;; or latex-environment) boundary. Inserting one can
  4883. ;; modify buffer drastically both above and below that
  4884. ;; line, possibly making cache invalid. Therefore, we
  4885. ;; need to pay attention to changes happening to them.
  4886. (concat
  4887. "\\(" (org-with-limited-levels org-outline-regexp-bol) "\\)" "\\|"
  4888. org-element--cache-closing-line "\\|"
  4889. org-element--cache-opening-line)))
  4890. (setq org-element--cache-change-warning
  4891. (cond ((not (re-search-forward sensitive-re bottom t)) nil)
  4892. ((and (match-beginning 1)
  4893. (progn (goto-char bottom)
  4894. (or (not (re-search-backward sensitive-re
  4895. (match-end 1) t))
  4896. (match-beginning 1))))
  4897. 'headline)
  4898. (t))))))))
  4899. (defun org-element--cache-after-change (beg end pre)
  4900. "Update buffer modifications for current buffer.
  4901. BEG and END are the beginning and end of the range of changed
  4902. text, and the length in bytes of the pre-change text replaced by
  4903. that range. See `after-change-functions' for more information."
  4904. (let ((inhibit-quit t))
  4905. (when (org-element--cache-active-p)
  4906. (org-with-wide-buffer
  4907. (goto-char beg)
  4908. (beginning-of-line)
  4909. (let ((top (point))
  4910. (bottom (save-excursion (goto-char end) (line-end-position))))
  4911. (org-with-limited-levels
  4912. (save-match-data
  4913. ;; Determine if modified area needs to be extended,
  4914. ;; according to both previous and current state. We make
  4915. ;; a special case for headline editing: if a headline is
  4916. ;; modified but not removed, do not extend.
  4917. (when (let ((previous-state org-element--cache-change-warning)
  4918. (sensitive-re
  4919. (concat "\\(" org-outline-regexp-bol "\\)" "\\|"
  4920. org-element--cache-closing-line "\\|"
  4921. org-element--cache-opening-line))
  4922. (case-fold-search t))
  4923. (cond ((eq previous-state t))
  4924. ((not (re-search-forward sensitive-re bottom t))
  4925. (eq previous-state 'headline))
  4926. ((match-beginning 1)
  4927. (or (not (eq previous-state 'headline))
  4928. (and (progn (goto-char bottom)
  4929. (re-search-backward
  4930. sensitive-re (match-end 1) t))
  4931. (not (match-beginning 1)))))
  4932. (t)))
  4933. ;; Effectively extend modified area.
  4934. (setq top (progn (goto-char top)
  4935. (when (outline-previous-heading) (forward-line))
  4936. (point)))
  4937. (setq bottom (progn (goto-char bottom)
  4938. (if (outline-next-heading) (1- (point))
  4939. (point)))))))
  4940. ;; Store synchronization request.
  4941. (let ((offset (- end beg pre)))
  4942. (org-element--cache-submit-request top (- bottom offset) offset))))
  4943. ;; Activate a timer to process the request during idle time.
  4944. (org-element--cache-set-timer (current-buffer)))))
  4945. (defun org-element--cache-submit-request (beg end offset)
  4946. "Submit a new cache synchronization request for current buffer.
  4947. BEG and END are buffer positions delimiting the minimal area
  4948. where cache data should be removed. OFFSET is the size of the
  4949. change, as an integer."
  4950. (let ((first-element
  4951. ;; Find the position of the first element in cache to remove.
  4952. ;;
  4953. ;; Partially modified elements will be removed during request
  4954. ;; processing. As an exception, greater elements around the
  4955. ;; changes that are robust to contents modifications are
  4956. ;; preserved.
  4957. ;;
  4958. ;; We look just before BEG because an element ending at BEG
  4959. ;; needs to be removed too.
  4960. (let* ((elements (org-element--cache-find (1- beg) 'both))
  4961. (before (car elements))
  4962. (after (cdr elements)))
  4963. (if (not before) after
  4964. (let ((up before))
  4965. (while (setq up (org-element-property :parent up))
  4966. (if (and (memq (org-element-type up)
  4967. '(center-block
  4968. drawer dynamic-block inlinetask
  4969. property-drawer quote-block special-block))
  4970. (<= (org-element-property :contents-begin up) beg)
  4971. (> (org-element-property :contents-end up) end))
  4972. ;; UP is a greater element that is wrapped around
  4973. ;; the changes. We only need to extend its
  4974. ;; ending boundaries and those of all its
  4975. ;; parents.
  4976. (while up
  4977. (org-element--cache-shift-positions
  4978. up offset '(:contents-end :end))
  4979. (setq up (org-element-property :parent up)))
  4980. (setq before up)))
  4981. ;; We're at top level element containing ELEMENT: if
  4982. ;; it's altered by buffer modifications, it is first
  4983. ;; element in cache to be removed. Otherwise, that
  4984. ;; first element is the following one.
  4985. (if (< (org-element-property :end before) beg) after before))))))
  4986. (cond
  4987. ;; Changes happened before the first known element. Shift the
  4988. ;; rest of the cache.
  4989. ((and first-element (> (org-element-property :begin first-element) end))
  4990. (push (vector (org-element--cache-key first-element) nil offset nil 2)
  4991. org-element--cache-sync-requests))
  4992. ;; There is at least an element to remove. Find position past
  4993. ;; every element containing END.
  4994. (first-element
  4995. (if (> (org-element-property :end first-element) end)
  4996. (setq end (org-element-property :end first-element))
  4997. (let ((element (org-element--cache-find end)))
  4998. (setq end (org-element-property :end element))
  4999. (let ((up element))
  5000. (while (and (setq up (org-element-property :parent up))
  5001. (>= (org-element-property :begin up) beg))
  5002. (setq end (org-element-property :end up))))))
  5003. (push (vector (org-element--cache-key first-element) end offset nil 0)
  5004. org-element--cache-sync-requests))
  5005. ;; No element to remove. No need to re-parent either. Simply
  5006. ;; shift additional elements, if any, by OFFSET.
  5007. (org-element--cache-sync-requests
  5008. (incf (aref (car org-element--cache-sync-requests) 2) offset)))))
  5009. ;;;; Public Functions
  5010. ;;;###autoload
  5011. (defun org-element-cache-reset (&optional all)
  5012. "Reset cache in current buffer.
  5013. When optional argument ALL is non-nil, reset cache in all Org
  5014. buffers. This function will do nothing if
  5015. `org-element-use-cache' is nil."
  5016. (interactive "P")
  5017. (dolist (buffer (if all (buffer-list) (list (current-buffer))))
  5018. (with-current-buffer buffer
  5019. (when (org-element--cache-active-p)
  5020. (org-set-local 'org-element--cache
  5021. (avl-tree-create #'org-element--cache-compare))
  5022. (org-set-local 'org-element--cache-objects
  5023. (make-hash-table :weakness 'key :test #'eq))
  5024. (org-set-local 'org-element--cache-sync-keys
  5025. (make-hash-table :weakness 'key :test #'eq))
  5026. (org-set-local 'org-element--cache-change-warning nil)
  5027. (org-set-local 'org-element--cache-sync-requests nil)
  5028. (org-set-local 'org-element--cache-sync-timer nil)
  5029. (add-hook 'before-change-functions
  5030. #'org-element--cache-before-change nil t)
  5031. (add-hook 'after-change-functions
  5032. #'org-element--cache-after-change nil t)))))
  5033. ;;; The Toolbox
  5034. ;;
  5035. ;; The first move is to implement a way to obtain the smallest element
  5036. ;; containing point. This is the job of `org-element-at-point'. It
  5037. ;; basically jumps back to the beginning of section containing point
  5038. ;; and proceed, one element after the other, with
  5039. ;; `org-element--current-element' until the container is found. Note:
  5040. ;; When using `org-element-at-point', secondary values are never
  5041. ;; parsed since the function focuses on elements, not on objects.
  5042. ;;
  5043. ;; At a deeper level, `org-element-context' lists all elements and
  5044. ;; objects containing point.
  5045. ;;
  5046. ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
  5047. ;; internally by navigation and manipulation tools.
  5048. ;;;###autoload
  5049. (defun org-element-at-point ()
  5050. "Determine closest element around point.
  5051. Return value is a list like (TYPE PROPS) where TYPE is the type
  5052. of the element and PROPS a plist of properties associated to the
  5053. element.
  5054. Possible types are defined in `org-element-all-elements'.
  5055. Properties depend on element or object type, but always include
  5056. `:begin', `:end', `:parent' and `:post-blank' properties.
  5057. As a special case, if point is at the very beginning of the first
  5058. item in a list or sub-list, returned element will be that list
  5059. instead of the item. Likewise, if point is at the beginning of
  5060. the first row of a table, returned element will be the table
  5061. instead of the first row.
  5062. When point is at the end of the buffer, return the innermost
  5063. element ending there."
  5064. (org-with-wide-buffer
  5065. (let ((origin (point)))
  5066. (end-of-line)
  5067. (skip-chars-backward " \r\t\n")
  5068. (cond
  5069. ;; Within blank lines at the beginning of buffer, return nil.
  5070. ((bobp) nil)
  5071. ;; Within blank lines right after a headline, return that
  5072. ;; headline.
  5073. ((org-with-limited-levels (org-at-heading-p))
  5074. (beginning-of-line)
  5075. (org-element-headline-parser (point-max) t))
  5076. ;; Otherwise parse until we find element containing ORIGIN.
  5077. (t
  5078. (when (org-element--cache-active-p)
  5079. (if (not org-element--cache) (org-element-cache-reset)
  5080. (org-element--cache-sync (current-buffer) origin)))
  5081. (org-element--parse-to origin))))))
  5082. ;;;###autoload
  5083. (defun org-element-context (&optional element)
  5084. "Return closest element or object around point.
  5085. Return value is a list like (TYPE PROPS) where TYPE is the type
  5086. of the element or object and PROPS a plist of properties
  5087. associated to it.
  5088. Possible types are defined in `org-element-all-elements' and
  5089. `org-element-all-objects'. Properties depend on element or
  5090. object type, but always include `:begin', `:end', `:parent' and
  5091. `:post-blank'.
  5092. Optional argument ELEMENT, when non-nil, is the closest element
  5093. containing point, as returned by `org-element-at-point'.
  5094. Providing it allows for quicker computation."
  5095. (catch 'objects-forbidden
  5096. (org-with-wide-buffer
  5097. (let* ((origin (point))
  5098. (element (or element (org-element-at-point)))
  5099. (type (org-element-type element)))
  5100. ;; If point is inside an element containing objects or
  5101. ;; a secondary string, narrow buffer to the container and
  5102. ;; proceed with parsing. Otherwise, return ELEMENT.
  5103. (cond
  5104. ;; At a parsed affiliated keyword, check if we're inside main
  5105. ;; or dual value.
  5106. ((let ((post (org-element-property :post-affiliated element)))
  5107. (and post (< origin post)))
  5108. (beginning-of-line)
  5109. (let ((case-fold-search t)) (looking-at org-element--affiliated-re))
  5110. (cond
  5111. ((not (member-ignore-case (match-string 1)
  5112. org-element-parsed-keywords))
  5113. (throw 'objects-forbidden element))
  5114. ((< (match-end 0) origin)
  5115. (narrow-to-region (match-end 0) (line-end-position)))
  5116. ((and (match-beginning 2)
  5117. (>= origin (match-beginning 2))
  5118. (< origin (match-end 2)))
  5119. (narrow-to-region (match-beginning 2) (match-end 2)))
  5120. (t (throw 'objects-forbidden element)))
  5121. ;; Also change type to retrieve correct restrictions.
  5122. (setq type 'keyword))
  5123. ;; At an item, objects can only be located within tag, if any.
  5124. ((eq type 'item)
  5125. (let ((tag (org-element-property :tag element)))
  5126. (if (not tag) (throw 'objects-forbidden element)
  5127. (beginning-of-line)
  5128. (search-forward tag (line-end-position))
  5129. (goto-char (match-beginning 0))
  5130. (if (and (>= origin (point)) (< origin (match-end 0)))
  5131. (narrow-to-region (point) (match-end 0))
  5132. (throw 'objects-forbidden element)))))
  5133. ;; At an headline or inlinetask, objects are in title.
  5134. ((memq type '(headline inlinetask))
  5135. (goto-char (org-element-property :begin element))
  5136. (skip-chars-forward "*")
  5137. (if (and (> origin (point)) (< origin (line-end-position)))
  5138. (narrow-to-region (point) (line-end-position))
  5139. (throw 'objects-forbidden element)))
  5140. ;; At a paragraph, a table-row or a verse block, objects are
  5141. ;; located within their contents.
  5142. ((memq type '(paragraph table-row verse-block))
  5143. (let ((cbeg (org-element-property :contents-begin element))
  5144. (cend (org-element-property :contents-end element)))
  5145. ;; CBEG is nil for table rules.
  5146. (if (and cbeg cend (>= origin cbeg)
  5147. (or (< origin cend) (and (= origin cend) (eobp))))
  5148. (narrow-to-region cbeg cend)
  5149. (throw 'objects-forbidden element))))
  5150. ;; At a parsed keyword, objects are located within value.
  5151. ((eq type 'keyword)
  5152. (if (not (member (org-element-property :key element)
  5153. org-element-document-properties))
  5154. (throw 'objects-forbidden element)
  5155. (beginning-of-line)
  5156. (search-forward ":")
  5157. (if (and (>= origin (point)) (< origin (line-end-position)))
  5158. (narrow-to-region (point) (line-end-position))
  5159. (throw 'objects-forbidden element))))
  5160. ;; At a planning line, if point is at a timestamp, return it,
  5161. ;; otherwise, return element.
  5162. ((eq type 'planning)
  5163. (dolist (p '(:closed :deadline :scheduled))
  5164. (let ((timestamp (org-element-property p element)))
  5165. (when (and timestamp
  5166. (<= (org-element-property :begin timestamp) origin)
  5167. (> (org-element-property :end timestamp) origin))
  5168. (throw 'objects-forbidden timestamp))))
  5169. ;; All other locations cannot contain objects: bail out.
  5170. (throw 'objects-forbidden element))
  5171. (t (throw 'objects-forbidden element)))
  5172. (goto-char (point-min))
  5173. (let* ((restriction (org-element-restriction type))
  5174. (parent element)
  5175. (candidates 'initial)
  5176. (cache (cond ((not (org-element--cache-active-p)) nil)
  5177. (org-element--cache-objects
  5178. (gethash element org-element--cache-objects))
  5179. (t (org-element-cache-reset) nil)))
  5180. objects-data next)
  5181. (prog1
  5182. (catch 'exit
  5183. (while t
  5184. ;; Get list of next object candidates in CANDIDATES.
  5185. ;; When entering for the first time PARENT, grab it
  5186. ;; from cache, if available, or compute it. Then,
  5187. ;; for each subsequent iteration in PARENT, always
  5188. ;; compute it since we're beyond cache anyway.
  5189. (unless next
  5190. (let* ((key
  5191. ;; Increase key of objects contained in
  5192. ;; table cells by one so they cannot get
  5193. ;; the same key as the cell itself.
  5194. (if (eq (org-element-type parent) 'table-cell)
  5195. (1+ (point))
  5196. (point)))
  5197. (data (assq key cache)))
  5198. (if data (setq candidates (nth 1 (setq objects-data data)))
  5199. (push (setq objects-data (list key 'initial))
  5200. cache))))
  5201. (when (or next (eq 'initial candidates))
  5202. (setq candidates
  5203. (org-element--get-next-object-candidates
  5204. restriction candidates))
  5205. (setcar (cdr objects-data) candidates))
  5206. ;; Compare ORIGIN with next object starting position,
  5207. ;; if any.
  5208. ;;
  5209. ;; If ORIGIN is lesser or if there is no object
  5210. ;; following, look for a previous object that might
  5211. ;; contain it in cache. If there is no cache, we
  5212. ;; didn't miss any object so simply return PARENT.
  5213. ;;
  5214. ;; If ORIGIN is greater or equal, parse next
  5215. ;; candidate for further processing.
  5216. (let ((closest
  5217. (and candidates
  5218. (rassq (apply #'min (mapcar #'cdr candidates))
  5219. candidates))))
  5220. (if (or (not closest) (> (cdr closest) origin))
  5221. (catch 'found
  5222. (dolist (obj (cddr objects-data) (throw 'exit parent))
  5223. (when (<= (org-element-property :begin obj) origin)
  5224. (let ((end (org-element-property :end obj)))
  5225. (cond
  5226. ((> end origin) (throw 'found (setq next obj)))
  5227. ((and (= end origin) (= (point-max) end))
  5228. (org-element-put-property obj :parent parent)
  5229. (throw 'exit obj))
  5230. (t (throw 'exit parent)))))))
  5231. (goto-char (cdr closest))
  5232. (setq next
  5233. (funcall (intern (format "org-element-%s-parser"
  5234. (car closest)))))
  5235. (push next (cddr objects-data))))
  5236. ;; Process NEXT to know if we need to skip it, return
  5237. ;; it or move into it.
  5238. (let ((cbeg (org-element-property :contents-begin next))
  5239. (cend (org-element-property :contents-end next))
  5240. (obj-end (org-element-property :end next)))
  5241. (cond
  5242. ;; ORIGIN is after NEXT, so skip it.
  5243. ((<= obj-end origin) (goto-char obj-end))
  5244. ;; ORIGIN is within a non-recursive next or
  5245. ;; at an object boundaries: Return that object.
  5246. ((or (not cbeg) (< origin cbeg) (>= origin cend))
  5247. (org-element-put-property next :parent parent)
  5248. (throw 'exit next))
  5249. ;; Otherwise, move into NEXT and reset flags as we
  5250. ;; shift parent.
  5251. (t (goto-char cbeg)
  5252. (narrow-to-region (point) cend)
  5253. (org-element-put-property next :parent parent)
  5254. (setq parent next
  5255. restriction (org-element-restriction next)
  5256. next nil
  5257. objects-data nil
  5258. candidates 'initial))))))
  5259. ;; Store results in cache, if applicable.
  5260. (org-element--cache-put element cache)))))))
  5261. (defun org-element-nested-p (elem-A elem-B)
  5262. "Non-nil when elements ELEM-A and ELEM-B are nested."
  5263. (let ((beg-A (org-element-property :begin elem-A))
  5264. (beg-B (org-element-property :begin elem-B))
  5265. (end-A (org-element-property :end elem-A))
  5266. (end-B (org-element-property :end elem-B)))
  5267. (or (and (>= beg-A beg-B) (<= end-A end-B))
  5268. (and (>= beg-B beg-A) (<= end-B end-A)))))
  5269. (defun org-element-swap-A-B (elem-A elem-B)
  5270. "Swap elements ELEM-A and ELEM-B.
  5271. Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
  5272. end of ELEM-A."
  5273. (goto-char (org-element-property :begin elem-A))
  5274. ;; There are two special cases when an element doesn't start at bol:
  5275. ;; the first paragraph in an item or in a footnote definition.
  5276. (let ((specialp (not (bolp))))
  5277. ;; Only a paragraph without any affiliated keyword can be moved at
  5278. ;; ELEM-A position in such a situation. Note that the case of
  5279. ;; a footnote definition is impossible: it cannot contain two
  5280. ;; paragraphs in a row because it cannot contain a blank line.
  5281. (if (and specialp
  5282. (or (not (eq (org-element-type elem-B) 'paragraph))
  5283. (/= (org-element-property :begin elem-B)
  5284. (org-element-property :contents-begin elem-B))))
  5285. (error "Cannot swap elements"))
  5286. ;; In a special situation, ELEM-A will have no indentation. We'll
  5287. ;; give it ELEM-B's (which will in, in turn, have no indentation).
  5288. (let* ((ind-B (when specialp
  5289. (goto-char (org-element-property :begin elem-B))
  5290. (org-get-indentation)))
  5291. (beg-A (org-element-property :begin elem-A))
  5292. (end-A (save-excursion
  5293. (goto-char (org-element-property :end elem-A))
  5294. (skip-chars-backward " \r\t\n")
  5295. (point-at-eol)))
  5296. (beg-B (org-element-property :begin elem-B))
  5297. (end-B (save-excursion
  5298. (goto-char (org-element-property :end elem-B))
  5299. (skip-chars-backward " \r\t\n")
  5300. (point-at-eol)))
  5301. ;; Store overlays responsible for visibility status. We
  5302. ;; also need to store their boundaries as they will be
  5303. ;; removed from buffer.
  5304. (overlays
  5305. (cons
  5306. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  5307. (overlays-in beg-A end-A))
  5308. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  5309. (overlays-in beg-B end-B))))
  5310. ;; Get contents.
  5311. (body-A (buffer-substring beg-A end-A))
  5312. (body-B (delete-and-extract-region beg-B end-B)))
  5313. (goto-char beg-B)
  5314. (when specialp
  5315. (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
  5316. (org-indent-to-column ind-B))
  5317. (insert body-A)
  5318. ;; Restore ex ELEM-A overlays.
  5319. (let ((offset (- beg-B beg-A)))
  5320. (mapc (lambda (ov)
  5321. (move-overlay
  5322. (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
  5323. (car overlays))
  5324. (goto-char beg-A)
  5325. (delete-region beg-A end-A)
  5326. (insert body-B)
  5327. ;; Restore ex ELEM-B overlays.
  5328. (mapc (lambda (ov)
  5329. (move-overlay
  5330. (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
  5331. (cdr overlays)))
  5332. (goto-char (org-element-property :end elem-B)))))
  5333. (defun org-element-remove-indentation (s &optional n)
  5334. "Remove maximum common indentation in string S and return it.
  5335. When optional argument N is a positive integer, remove exactly
  5336. that much characters from indentation, if possible, or return
  5337. S as-is otherwise. Unlike to `org-remove-indentation', this
  5338. function doesn't call `untabify' on S."
  5339. (catch 'exit
  5340. (with-temp-buffer
  5341. (insert s)
  5342. (goto-char (point-min))
  5343. ;; Find maximum common indentation, if not specified.
  5344. (setq n (or n
  5345. (let ((min-ind (point-max)))
  5346. (save-excursion
  5347. (while (re-search-forward "^[ \t]*\\S-" nil t)
  5348. (let ((ind (1- (current-column))))
  5349. (if (zerop ind) (throw 'exit s)
  5350. (setq min-ind (min min-ind ind))))))
  5351. min-ind)))
  5352. (if (zerop n) s
  5353. ;; Remove exactly N indentation, but give up if not possible.
  5354. (while (not (eobp))
  5355. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  5356. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  5357. ((< ind n) (throw 'exit s))
  5358. (t (org-indent-line-to (- ind n))))
  5359. (forward-line)))
  5360. (buffer-string)))))
  5361. (provide 'org-element)
  5362. ;; Local variables:
  5363. ;; generated-autoload-file: "org-loaddefs.el"
  5364. ;; End:
  5365. ;;; org-element.el ends here