org-element.el 212 KB

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