org-element.el 214 KB

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