org-element.el 214 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034
  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. Decode any encoding.
  2759. ;; Expand any 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. (org-link-unescape
  2771. (replace-regexp-in-string
  2772. "[ \t]*\n[ \t]*" " "
  2773. (match-string-no-properties 1)))))
  2774. ;; Determine TYPE of link and set PATH accordingly. According
  2775. ;; to RFC 3986, remove whitespaces from URI in external links.
  2776. ;; In internal ones, treat indentation as a single space.
  2777. (cond
  2778. ;; File type.
  2779. ((or (file-name-absolute-p raw-link)
  2780. (string-match "\\`\\.\\.?/" raw-link))
  2781. (setq type "file")
  2782. (setq path raw-link))
  2783. ;; Explicit type (http, irc, bbdb...).
  2784. ((string-match org-link-types-re raw-link)
  2785. (setq type (match-string 1 raw-link))
  2786. (setq path (substring raw-link (match-end 0))))
  2787. ;; Code-ref type: PATH is the name of the reference.
  2788. ((and (string-match-p "\\`(" raw-link)
  2789. (string-match-p ")\\'" raw-link))
  2790. (setq type "coderef")
  2791. (setq path (substring raw-link 1 -1)))
  2792. ;; Custom-id type: PATH is the name of the custom id.
  2793. ((= (string-to-char raw-link) ?#)
  2794. (setq type "custom-id")
  2795. (setq path (substring raw-link 1)))
  2796. ;; Fuzzy type: Internal link either matches a target, an
  2797. ;; headline name or nothing. PATH is the target or
  2798. ;; headline's name.
  2799. (t
  2800. (setq type "fuzzy")
  2801. (setq path raw-link))))
  2802. ;; Type 3: Plain link, e.g., https://orgmode.org
  2803. ((looking-at org-plain-link-re)
  2804. (setq format 'plain)
  2805. (setq raw-link (match-string-no-properties 0))
  2806. (setq type (match-string-no-properties 1))
  2807. (setq link-end (match-end 0))
  2808. (setq path (match-string-no-properties 2)))
  2809. ;; Type 4: Angular link, e.g., <https://orgmode.org>. Unlike to
  2810. ;; bracket links, follow RFC 3986 and remove any extra
  2811. ;; whitespace in URI.
  2812. ((looking-at org-angle-link-re)
  2813. (setq format 'angle)
  2814. (setq type (match-string-no-properties 1))
  2815. (setq link-end (match-end 0))
  2816. (setq raw-link
  2817. (buffer-substring-no-properties
  2818. (match-beginning 1) (match-end 2)))
  2819. (setq path (replace-regexp-in-string
  2820. "[ \t]*\n[ \t]*" "" (match-string-no-properties 2))))
  2821. (t (throw 'no-object nil)))
  2822. ;; In any case, deduce end point after trailing white space from
  2823. ;; LINK-END variable.
  2824. (save-excursion
  2825. (setq post-blank
  2826. (progn (goto-char link-end) (skip-chars-forward " \t")))
  2827. (setq end (point)))
  2828. ;; Special "file" type link processing. Extract opening
  2829. ;; application and search option, if any. Also normalize URI.
  2830. (when (string-match "\\`file\\(?:\\+\\(.+\\)\\)?\\'" type)
  2831. (setq application (match-string 1 type) type "file")
  2832. (when (string-match "::\\(.*\\)\\'" path)
  2833. (setq search-option (match-string 1 path))
  2834. (setq path (replace-match "" nil nil path)))
  2835. (setq path (replace-regexp-in-string "\\`///*\\(.:\\)?/" "\\1/" path)))
  2836. ;; Translate link, if `org-link-translation-function' is set.
  2837. (let ((trans (and (functionp org-link-translation-function)
  2838. (funcall org-link-translation-function type path))))
  2839. (when trans
  2840. (setq type (car trans))
  2841. (setq path (cdr trans))))
  2842. (list 'link
  2843. (list :type type
  2844. :path path
  2845. :format format
  2846. :raw-link (or raw-link path)
  2847. :application application
  2848. :search-option search-option
  2849. :begin begin
  2850. :end end
  2851. :contents-begin contents-begin
  2852. :contents-end contents-end
  2853. :post-blank post-blank)))))
  2854. (defun org-element-link-interpreter (link contents)
  2855. "Interpret LINK object as Org syntax.
  2856. CONTENTS is the contents of the object, or nil."
  2857. (let ((type (org-element-property :type link))
  2858. (path (org-element-property :path link)))
  2859. (if (string= type "radio") path
  2860. (let ((fmt (pcase (org-element-property :format link)
  2861. ;; Links with contents and internal links have to
  2862. ;; use bracket syntax. Ignore `:format' in these
  2863. ;; cases. This is also the default syntax when the
  2864. ;; property is not defined, e.g., when the object
  2865. ;; was crafted by the user.
  2866. ((guard contents)
  2867. (format "[[%%s][%s]]"
  2868. ;; Since this is going to be used as
  2869. ;; a format string, escape percent signs
  2870. ;; in description.
  2871. (replace-regexp-in-string "%" "%%" contents)))
  2872. ((or `bracket
  2873. `nil
  2874. (guard (member type '("coderef" "custom-id" "fuzzy"))))
  2875. "[[%s]]")
  2876. ;; Otherwise, just obey to `:format'.
  2877. (`angle "<%s>")
  2878. (`plain "%s")
  2879. (f (error "Wrong `:format' value: %s" f)))))
  2880. (format fmt
  2881. (pcase type
  2882. ("coderef" (format "(%s)" path))
  2883. ("custom-id" (concat "#" path))
  2884. ("file"
  2885. (let ((app (org-element-property :application link))
  2886. (opt (org-element-property :search-option link)))
  2887. (concat type (and app (concat "+" app)) ":"
  2888. path
  2889. (and opt (concat "::" opt)))))
  2890. ("fuzzy" path)
  2891. (_ (concat type ":" path))))))))
  2892. ;;;; Macro
  2893. (defun org-element-macro-parser ()
  2894. "Parse macro at point, if any.
  2895. When at a macro, return a list whose car is `macro' and cdr
  2896. a plist with `:key', `:args', `:begin', `:end', `:value' and
  2897. `:post-blank' as keywords. Otherwise, return nil.
  2898. Assume point is at the macro."
  2899. (save-excursion
  2900. (when (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\((\\([^\000]*?\\))\\)?}}}")
  2901. (let ((begin (point))
  2902. (key (downcase (match-string-no-properties 1)))
  2903. (value (match-string-no-properties 0))
  2904. (post-blank (progn (goto-char (match-end 0))
  2905. (skip-chars-forward " \t")))
  2906. (end (point))
  2907. (args (pcase (match-string-no-properties 3)
  2908. (`nil nil)
  2909. (a (org-macro-extract-arguments
  2910. (replace-regexp-in-string
  2911. "[ \t\r\n]+" " " (org-trim a)))))))
  2912. (list 'macro
  2913. (list :key key
  2914. :value value
  2915. :args args
  2916. :begin begin
  2917. :end end
  2918. :post-blank post-blank))))))
  2919. (defun org-element-macro-interpreter (macro _)
  2920. "Interpret MACRO object as Org syntax."
  2921. (format "{{{%s%s}}}"
  2922. (org-element-property :key macro)
  2923. (pcase (org-element-property :args macro)
  2924. (`nil "")
  2925. (args (format "(%s)" (apply #'org-macro-escape-arguments args))))))
  2926. ;;;; Radio-target
  2927. (defun org-element-radio-target-parser ()
  2928. "Parse radio target at point, if any.
  2929. When at a radio target, return a list whose car is `radio-target'
  2930. and cdr a plist with `:begin', `:end', `:contents-begin',
  2931. `:contents-end', `:value' and `:post-blank' as keywords.
  2932. Otherwise, return nil.
  2933. Assume point is at the radio target."
  2934. (save-excursion
  2935. (when (looking-at org-radio-target-regexp)
  2936. (let ((begin (point))
  2937. (contents-begin (match-beginning 1))
  2938. (contents-end (match-end 1))
  2939. (value (match-string-no-properties 1))
  2940. (post-blank (progn (goto-char (match-end 0))
  2941. (skip-chars-forward " \t")))
  2942. (end (point)))
  2943. (list 'radio-target
  2944. (list :begin begin
  2945. :end end
  2946. :contents-begin contents-begin
  2947. :contents-end contents-end
  2948. :post-blank post-blank
  2949. :value value))))))
  2950. (defun org-element-radio-target-interpreter (_ contents)
  2951. "Interpret target object as Org syntax.
  2952. CONTENTS is the contents of the object."
  2953. (concat "<<<" contents ">>>"))
  2954. ;;;; Statistics Cookie
  2955. (defun org-element-statistics-cookie-parser ()
  2956. "Parse statistics cookie at point, if any.
  2957. When at a statistics cookie, return a list whose car is
  2958. `statistics-cookie', and cdr a plist with `:begin', `:end',
  2959. `:value' and `:post-blank' keywords. Otherwise, return nil.
  2960. Assume point is at the beginning of the statistics-cookie."
  2961. (save-excursion
  2962. (when (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
  2963. (let* ((begin (point))
  2964. (value (buffer-substring-no-properties
  2965. (match-beginning 0) (match-end 0)))
  2966. (post-blank (progn (goto-char (match-end 0))
  2967. (skip-chars-forward " \t")))
  2968. (end (point)))
  2969. (list 'statistics-cookie
  2970. (list :begin begin
  2971. :end end
  2972. :value value
  2973. :post-blank post-blank))))))
  2974. (defun org-element-statistics-cookie-interpreter (statistics-cookie _)
  2975. "Interpret STATISTICS-COOKIE object as Org syntax."
  2976. (org-element-property :value statistics-cookie))
  2977. ;;;; Strike-Through
  2978. (defun org-element-strike-through-parser ()
  2979. "Parse strike-through object at point, if any.
  2980. When at a strike-through object, return a list whose car is
  2981. `strike-through' and cdr is a plist with `:begin', `:end',
  2982. `:contents-begin' and `:contents-end' and `:post-blank' keywords.
  2983. Otherwise, return nil.
  2984. Assume point is at the first plus sign marker."
  2985. (save-excursion
  2986. (unless (bolp) (backward-char 1))
  2987. (when (looking-at org-emph-re)
  2988. (let ((begin (match-beginning 2))
  2989. (contents-begin (match-beginning 4))
  2990. (contents-end (match-end 4))
  2991. (post-blank (progn (goto-char (match-end 2))
  2992. (skip-chars-forward " \t")))
  2993. (end (point)))
  2994. (list 'strike-through
  2995. (list :begin begin
  2996. :end end
  2997. :contents-begin contents-begin
  2998. :contents-end contents-end
  2999. :post-blank post-blank))))))
  3000. (defun org-element-strike-through-interpreter (_ contents)
  3001. "Interpret strike-through object as Org syntax.
  3002. CONTENTS is the contents of the object."
  3003. (format "+%s+" contents))
  3004. ;;;; Subscript
  3005. (defun org-element-subscript-parser ()
  3006. "Parse subscript at point, if any.
  3007. When at a subscript object, return a list whose car is
  3008. `subscript' and cdr a plist with `:begin', `:end',
  3009. `:contents-begin', `:contents-end', `:use-brackets-p' and
  3010. `:post-blank' as keywords. Otherwise, return nil.
  3011. Assume point is at the underscore."
  3012. (save-excursion
  3013. (unless (bolp) (backward-char))
  3014. (when (looking-at org-match-substring-regexp)
  3015. (let ((bracketsp (match-beginning 4))
  3016. (begin (match-beginning 2))
  3017. (contents-begin (or (match-beginning 4)
  3018. (match-beginning 3)))
  3019. (contents-end (or (match-end 4) (match-end 3)))
  3020. (post-blank (progn (goto-char (match-end 0))
  3021. (skip-chars-forward " \t")))
  3022. (end (point)))
  3023. (list 'subscript
  3024. (list :begin begin
  3025. :end end
  3026. :use-brackets-p bracketsp
  3027. :contents-begin contents-begin
  3028. :contents-end contents-end
  3029. :post-blank post-blank))))))
  3030. (defun org-element-subscript-interpreter (subscript contents)
  3031. "Interpret SUBSCRIPT object as Org syntax.
  3032. CONTENTS is the contents of the object."
  3033. (format
  3034. (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
  3035. contents))
  3036. ;;;; Superscript
  3037. (defun org-element-superscript-parser ()
  3038. "Parse superscript at point, if any.
  3039. When at a superscript object, return a list whose car is
  3040. `superscript' and cdr a plist with `:begin', `:end',
  3041. `:contents-begin', `:contents-end', `:use-brackets-p' and
  3042. `:post-blank' as keywords. Otherwise, return nil.
  3043. Assume point is at the caret."
  3044. (save-excursion
  3045. (unless (bolp) (backward-char))
  3046. (when (looking-at org-match-substring-regexp)
  3047. (let ((bracketsp (match-beginning 4))
  3048. (begin (match-beginning 2))
  3049. (contents-begin (or (match-beginning 4)
  3050. (match-beginning 3)))
  3051. (contents-end (or (match-end 4) (match-end 3)))
  3052. (post-blank (progn (goto-char (match-end 0))
  3053. (skip-chars-forward " \t")))
  3054. (end (point)))
  3055. (list 'superscript
  3056. (list :begin begin
  3057. :end end
  3058. :use-brackets-p bracketsp
  3059. :contents-begin contents-begin
  3060. :contents-end contents-end
  3061. :post-blank post-blank))))))
  3062. (defun org-element-superscript-interpreter (superscript contents)
  3063. "Interpret SUPERSCRIPT object as Org syntax.
  3064. CONTENTS is the contents of the object."
  3065. (format
  3066. (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
  3067. contents))
  3068. ;;;; Table Cell
  3069. (defun org-element-table-cell-parser ()
  3070. "Parse table cell at point.
  3071. Return a list whose car is `table-cell' and cdr is a plist
  3072. containing `:begin', `:end', `:contents-begin', `:contents-end'
  3073. and `:post-blank' keywords."
  3074. (looking-at "[ \t]*\\(.*?\\)[ \t]*\\(?:|\\|$\\)")
  3075. (let* ((begin (match-beginning 0))
  3076. (end (match-end 0))
  3077. (contents-begin (match-beginning 1))
  3078. (contents-end (match-end 1)))
  3079. (list 'table-cell
  3080. (list :begin begin
  3081. :end end
  3082. :contents-begin contents-begin
  3083. :contents-end contents-end
  3084. :post-blank 0))))
  3085. (defun org-element-table-cell-interpreter (_ contents)
  3086. "Interpret table-cell element as Org syntax.
  3087. CONTENTS is the contents of the cell, or nil."
  3088. (concat " " contents " |"))
  3089. ;;;; Target
  3090. (defun org-element-target-parser ()
  3091. "Parse target at point, if any.
  3092. When at a target, return a list whose car is `target' and cdr
  3093. a plist with `:begin', `:end', `:value' and `:post-blank' as
  3094. keywords. Otherwise, return nil.
  3095. Assume point is at the target."
  3096. (save-excursion
  3097. (when (looking-at org-target-regexp)
  3098. (let ((begin (point))
  3099. (value (match-string-no-properties 1))
  3100. (post-blank (progn (goto-char (match-end 0))
  3101. (skip-chars-forward " \t")))
  3102. (end (point)))
  3103. (list 'target
  3104. (list :begin begin
  3105. :end end
  3106. :value value
  3107. :post-blank post-blank))))))
  3108. (defun org-element-target-interpreter (target _)
  3109. "Interpret TARGET object as Org syntax."
  3110. (format "<<%s>>" (org-element-property :value target)))
  3111. ;;;; Timestamp
  3112. (defconst org-element--timestamp-regexp
  3113. (concat org-ts-regexp-both
  3114. "\\|"
  3115. "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  3116. "\\|"
  3117. "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
  3118. "Regexp matching any timestamp type object.")
  3119. (defun org-element-timestamp-parser ()
  3120. "Parse time stamp at point, if any.
  3121. When at a time stamp, return a list whose car is `timestamp', and
  3122. cdr a plist with `:type', `:raw-value', `:year-start',
  3123. `:month-start', `:day-start', `:hour-start', `:minute-start',
  3124. `:year-end', `:month-end', `:day-end', `:hour-end',
  3125. `:minute-end', `:repeater-type', `:repeater-value',
  3126. `:repeater-unit', `:warning-type', `:warning-value',
  3127. `:warning-unit', `:begin', `:end' and `:post-blank' keywords.
  3128. Otherwise, return nil.
  3129. Assume point is at the beginning of the timestamp."
  3130. (when (looking-at-p org-element--timestamp-regexp)
  3131. (save-excursion
  3132. (let* ((begin (point))
  3133. (activep (eq (char-after) ?<))
  3134. (raw-value
  3135. (progn
  3136. (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
  3137. (match-string-no-properties 0)))
  3138. (date-start (match-string-no-properties 1))
  3139. (date-end (match-string 3))
  3140. (diaryp (match-beginning 2))
  3141. (post-blank (progn (goto-char (match-end 0))
  3142. (skip-chars-forward " \t")))
  3143. (end (point))
  3144. (time-range
  3145. (and (not diaryp)
  3146. (string-match
  3147. "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
  3148. date-start)
  3149. (cons (string-to-number (match-string 2 date-start))
  3150. (string-to-number (match-string 3 date-start)))))
  3151. (type (cond (diaryp 'diary)
  3152. ((and activep (or date-end time-range)) 'active-range)
  3153. (activep 'active)
  3154. ((or date-end time-range) 'inactive-range)
  3155. (t 'inactive)))
  3156. (repeater-props
  3157. (and (not diaryp)
  3158. (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
  3159. raw-value)
  3160. (list
  3161. :repeater-type
  3162. (let ((type (match-string 1 raw-value)))
  3163. (cond ((equal "++" type) 'catch-up)
  3164. ((equal ".+" type) 'restart)
  3165. (t 'cumulate)))
  3166. :repeater-value (string-to-number (match-string 2 raw-value))
  3167. :repeater-unit
  3168. (pcase (string-to-char (match-string 3 raw-value))
  3169. (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (_ 'year)))))
  3170. (warning-props
  3171. (and (not diaryp)
  3172. (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value)
  3173. (list
  3174. :warning-type (if (match-string 1 raw-value) 'first 'all)
  3175. :warning-value (string-to-number (match-string 2 raw-value))
  3176. :warning-unit
  3177. (pcase (string-to-char (match-string 3 raw-value))
  3178. (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (_ 'year)))))
  3179. year-start month-start day-start hour-start minute-start year-end
  3180. month-end day-end hour-end minute-end)
  3181. ;; Parse date-start.
  3182. (unless diaryp
  3183. (let ((date (org-parse-time-string date-start t)))
  3184. (setq year-start (nth 5 date)
  3185. month-start (nth 4 date)
  3186. day-start (nth 3 date)
  3187. hour-start (nth 2 date)
  3188. minute-start (nth 1 date))))
  3189. ;; Compute date-end. It can be provided directly in time-stamp,
  3190. ;; or extracted from time range. Otherwise, it defaults to the
  3191. ;; same values as date-start.
  3192. (unless diaryp
  3193. (let ((date (and date-end (org-parse-time-string date-end t))))
  3194. (setq year-end (or (nth 5 date) year-start)
  3195. month-end (or (nth 4 date) month-start)
  3196. day-end (or (nth 3 date) day-start)
  3197. hour-end (or (nth 2 date) (car time-range) hour-start)
  3198. minute-end (or (nth 1 date) (cdr time-range) minute-start))))
  3199. (list 'timestamp
  3200. (nconc (list :type type
  3201. :raw-value raw-value
  3202. :year-start year-start
  3203. :month-start month-start
  3204. :day-start day-start
  3205. :hour-start hour-start
  3206. :minute-start minute-start
  3207. :year-end year-end
  3208. :month-end month-end
  3209. :day-end day-end
  3210. :hour-end hour-end
  3211. :minute-end minute-end
  3212. :begin begin
  3213. :end end
  3214. :post-blank post-blank)
  3215. repeater-props
  3216. warning-props))))))
  3217. (defun org-element-timestamp-interpreter (timestamp _)
  3218. "Interpret TIMESTAMP object as Org syntax."
  3219. (let* ((repeat-string
  3220. (concat
  3221. (pcase (org-element-property :repeater-type timestamp)
  3222. (`cumulate "+") (`catch-up "++") (`restart ".+"))
  3223. (let ((val (org-element-property :repeater-value timestamp)))
  3224. (and val (number-to-string val)))
  3225. (pcase (org-element-property :repeater-unit timestamp)
  3226. (`hour "h") (`day "d") (`week "w") (`month "m") (`year "y"))))
  3227. (warning-string
  3228. (concat
  3229. (pcase (org-element-property :warning-type timestamp)
  3230. (`first "--") (`all "-"))
  3231. (let ((val (org-element-property :warning-value timestamp)))
  3232. (and val (number-to-string val)))
  3233. (pcase (org-element-property :warning-unit timestamp)
  3234. (`hour "h") (`day "d") (`week "w") (`month "m") (`year "y"))))
  3235. (build-ts-string
  3236. ;; Build an Org timestamp string from TIME. ACTIVEP is
  3237. ;; non-nil when time stamp is active. If WITH-TIME-P is
  3238. ;; non-nil, add a time part. HOUR-END and MINUTE-END
  3239. ;; specify a time range in the timestamp. REPEAT-STRING is
  3240. ;; the repeater string, if any.
  3241. (lambda (time activep &optional with-time-p hour-end minute-end)
  3242. (let ((ts (format-time-string
  3243. (funcall (if with-time-p #'cdr #'car)
  3244. org-time-stamp-formats)
  3245. time)))
  3246. (when (and hour-end minute-end)
  3247. (string-match "[012]?[0-9]:[0-5][0-9]" ts)
  3248. (setq ts
  3249. (replace-match
  3250. (format "\\&-%02d:%02d" hour-end minute-end)
  3251. nil nil ts)))
  3252. (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
  3253. (dolist (s (list repeat-string warning-string))
  3254. (when (org-string-nw-p s)
  3255. (setq ts (concat (substring ts 0 -1)
  3256. " "
  3257. s
  3258. (substring ts -1)))))
  3259. ;; Return value.
  3260. ts)))
  3261. (type (org-element-property :type timestamp)))
  3262. (pcase type
  3263. ((or `active `inactive)
  3264. (let* ((minute-start (org-element-property :minute-start timestamp))
  3265. (minute-end (org-element-property :minute-end timestamp))
  3266. (hour-start (org-element-property :hour-start timestamp))
  3267. (hour-end (org-element-property :hour-end timestamp))
  3268. (time-range-p (and hour-start hour-end minute-start minute-end
  3269. (or (/= hour-start hour-end)
  3270. (/= minute-start minute-end)))))
  3271. (funcall
  3272. build-ts-string
  3273. (encode-time 0
  3274. (or minute-start 0)
  3275. (or hour-start 0)
  3276. (org-element-property :day-start timestamp)
  3277. (org-element-property :month-start timestamp)
  3278. (org-element-property :year-start timestamp))
  3279. (eq type 'active)
  3280. (and hour-start minute-start)
  3281. (and time-range-p hour-end)
  3282. (and time-range-p minute-end))))
  3283. ((or `active-range `inactive-range)
  3284. (let ((minute-start (org-element-property :minute-start timestamp))
  3285. (minute-end (org-element-property :minute-end timestamp))
  3286. (hour-start (org-element-property :hour-start timestamp))
  3287. (hour-end (org-element-property :hour-end timestamp)))
  3288. (concat
  3289. (funcall
  3290. build-ts-string (encode-time
  3291. 0
  3292. (or minute-start 0)
  3293. (or hour-start 0)
  3294. (org-element-property :day-start timestamp)
  3295. (org-element-property :month-start timestamp)
  3296. (org-element-property :year-start timestamp))
  3297. (eq type 'active-range)
  3298. (and hour-start minute-start))
  3299. "--"
  3300. (funcall build-ts-string
  3301. (encode-time 0
  3302. (or minute-end 0)
  3303. (or hour-end 0)
  3304. (org-element-property :day-end timestamp)
  3305. (org-element-property :month-end timestamp)
  3306. (org-element-property :year-end timestamp))
  3307. (eq type 'active-range)
  3308. (and hour-end minute-end)))))
  3309. (_ (org-element-property :raw-value timestamp)))))
  3310. ;;;; Underline
  3311. (defun org-element-underline-parser ()
  3312. "Parse underline object at point, if any.
  3313. When at an underline object, return a list whose car is
  3314. `underline' and cdr is a plist with `:begin', `:end',
  3315. `:contents-begin' and `:contents-end' and `:post-blank' keywords.
  3316. Otherwise, return nil.
  3317. Assume point is at the first underscore marker."
  3318. (save-excursion
  3319. (unless (bolp) (backward-char 1))
  3320. (when (looking-at org-emph-re)
  3321. (let ((begin (match-beginning 2))
  3322. (contents-begin (match-beginning 4))
  3323. (contents-end (match-end 4))
  3324. (post-blank (progn (goto-char (match-end 2))
  3325. (skip-chars-forward " \t")))
  3326. (end (point)))
  3327. (list 'underline
  3328. (list :begin begin
  3329. :end end
  3330. :contents-begin contents-begin
  3331. :contents-end contents-end
  3332. :post-blank post-blank))))))
  3333. (defun org-element-underline-interpreter (_ contents)
  3334. "Interpret underline object as Org syntax.
  3335. CONTENTS is the contents of the object."
  3336. (format "_%s_" contents))
  3337. ;;;; Verbatim
  3338. (defun org-element-verbatim-parser ()
  3339. "Parse verbatim object at point, if any.
  3340. When at a verbatim object, return a list whose car is `verbatim'
  3341. and cdr is a plist with `:value', `:begin', `:end' and
  3342. `:post-blank' keywords. Otherwise, return nil.
  3343. Assume point is at the first equal sign marker."
  3344. (save-excursion
  3345. (unless (bolp) (backward-char 1))
  3346. (when (looking-at org-verbatim-re)
  3347. (let ((begin (match-beginning 2))
  3348. (value (match-string-no-properties 4))
  3349. (post-blank (progn (goto-char (match-end 2))
  3350. (skip-chars-forward " \t")))
  3351. (end (point)))
  3352. (list 'verbatim
  3353. (list :value value
  3354. :begin begin
  3355. :end end
  3356. :post-blank post-blank))))))
  3357. (defun org-element-verbatim-interpreter (verbatim _)
  3358. "Interpret VERBATIM object as Org syntax."
  3359. (format "=%s=" (org-element-property :value verbatim)))
  3360. ;;; Parsing Element Starting At Point
  3361. ;;
  3362. ;; `org-element--current-element' is the core function of this section.
  3363. ;; It returns the Lisp representation of the element starting at
  3364. ;; point.
  3365. ;;
  3366. ;; `org-element--current-element' makes use of special modes. They
  3367. ;; are activated for fixed element chaining (e.g., `plain-list' >
  3368. ;; `item') or fixed conditional element chaining (e.g., `headline' >
  3369. ;; `section'). Special modes are: `first-section', `item',
  3370. ;; `node-property', `section' and `table-row'.
  3371. (defun org-element--current-element (limit &optional granularity mode structure)
  3372. "Parse the element starting at point.
  3373. Return value is a list like (TYPE PROPS) where TYPE is the type
  3374. of the element and PROPS a plist of properties associated to the
  3375. element.
  3376. Possible types are defined in `org-element-all-elements'.
  3377. LIMIT bounds the search.
  3378. Optional argument GRANULARITY determines the depth of the
  3379. recursion. Allowed values are `headline', `greater-element',
  3380. `element', `object' or nil. When it is broader than `object' (or
  3381. nil), secondary values will not be parsed, since they only
  3382. contain objects.
  3383. Optional argument MODE, when non-nil, can be either
  3384. `first-section', `section', `planning', `item', `node-property'
  3385. and `table-row'.
  3386. If STRUCTURE isn't provided but MODE is set to `item', it will be
  3387. computed.
  3388. This function assumes point is always at the beginning of the
  3389. element it has to parse."
  3390. (save-excursion
  3391. (let ((case-fold-search t)
  3392. ;; Determine if parsing depth allows for secondary strings
  3393. ;; parsing. It only applies to elements referenced in
  3394. ;; `org-element-secondary-value-alist'.
  3395. (raw-secondary-p (and granularity (not (eq granularity 'object)))))
  3396. (cond
  3397. ;; Item.
  3398. ((eq mode 'item)
  3399. (org-element-item-parser limit structure raw-secondary-p))
  3400. ;; Table Row.
  3401. ((eq mode 'table-row) (org-element-table-row-parser limit))
  3402. ;; Node Property.
  3403. ((eq mode 'node-property) (org-element-node-property-parser limit))
  3404. ;; Headline.
  3405. ((org-with-limited-levels (org-at-heading-p))
  3406. (org-element-headline-parser limit raw-secondary-p))
  3407. ;; Sections (must be checked after headline).
  3408. ((eq mode 'section) (org-element-section-parser limit))
  3409. ((eq mode 'first-section)
  3410. (org-element-section-parser
  3411. (or (save-excursion (org-with-limited-levels (outline-next-heading)))
  3412. limit)))
  3413. ;; Planning.
  3414. ((and (eq mode 'planning)
  3415. (eq ?* (char-after (line-beginning-position 0)))
  3416. (looking-at org-planning-line-re))
  3417. (org-element-planning-parser limit))
  3418. ;; Property drawer.
  3419. ((and (memq mode '(planning property-drawer))
  3420. (eq ?* (char-after (line-beginning-position
  3421. (if (eq mode 'planning) 0 -1))))
  3422. (looking-at org-property-drawer-re))
  3423. (org-element-property-drawer-parser limit))
  3424. ;; When not at bol, point is at the beginning of an item or
  3425. ;; a footnote definition: next item is always a paragraph.
  3426. ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
  3427. ;; Clock.
  3428. ((looking-at org-clock-line-re) (org-element-clock-parser limit))
  3429. ;; Inlinetask.
  3430. ((org-at-heading-p)
  3431. (org-element-inlinetask-parser limit raw-secondary-p))
  3432. ;; From there, elements can have affiliated keywords.
  3433. (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
  3434. (cond
  3435. ;; Jumping over affiliated keywords put point off-limits.
  3436. ;; Parse them as regular keywords.
  3437. ((and (cdr affiliated) (>= (point) limit))
  3438. (goto-char (car affiliated))
  3439. (org-element-keyword-parser limit nil))
  3440. ;; LaTeX Environment.
  3441. ((looking-at org-element--latex-begin-environment)
  3442. (org-element-latex-environment-parser limit affiliated))
  3443. ;; Drawer and Property Drawer.
  3444. ((looking-at org-drawer-regexp)
  3445. (org-element-drawer-parser limit affiliated))
  3446. ;; Fixed Width
  3447. ((looking-at "[ \t]*:\\( \\|$\\)")
  3448. (org-element-fixed-width-parser limit affiliated))
  3449. ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
  3450. ;; Keywords.
  3451. ((looking-at "[ \t]*#")
  3452. (goto-char (match-end 0))
  3453. (cond
  3454. ((looking-at "\\(?: \\|$\\)")
  3455. (beginning-of-line)
  3456. (org-element-comment-parser limit affiliated))
  3457. ((looking-at "\\+BEGIN_\\(\\S-+\\)")
  3458. (beginning-of-line)
  3459. (funcall (pcase (upcase (match-string 1))
  3460. ("CENTER" #'org-element-center-block-parser)
  3461. ("COMMENT" #'org-element-comment-block-parser)
  3462. ("EXAMPLE" #'org-element-example-block-parser)
  3463. ("EXPORT" #'org-element-export-block-parser)
  3464. ("QUOTE" #'org-element-quote-block-parser)
  3465. ("SRC" #'org-element-src-block-parser)
  3466. ("VERSE" #'org-element-verse-block-parser)
  3467. (_ #'org-element-special-block-parser))
  3468. limit
  3469. affiliated))
  3470. ((looking-at "\\+CALL:")
  3471. (beginning-of-line)
  3472. (org-element-babel-call-parser limit affiliated))
  3473. ((looking-at "\\+BEGIN:? ")
  3474. (beginning-of-line)
  3475. (org-element-dynamic-block-parser limit affiliated))
  3476. ((looking-at "\\+\\S-+:")
  3477. (beginning-of-line)
  3478. (org-element-keyword-parser limit affiliated))
  3479. (t
  3480. (beginning-of-line)
  3481. (org-element-paragraph-parser limit affiliated))))
  3482. ;; Footnote Definition.
  3483. ((looking-at org-footnote-definition-re)
  3484. (org-element-footnote-definition-parser limit affiliated))
  3485. ;; Horizontal Rule.
  3486. ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
  3487. (org-element-horizontal-rule-parser limit affiliated))
  3488. ;; Diary Sexp.
  3489. ((looking-at "%%(")
  3490. (org-element-diary-sexp-parser limit affiliated))
  3491. ;; Table.
  3492. ((or (looking-at "[ \t]*|")
  3493. ;; There is no strict definition of a table.el
  3494. ;; table. Try to prevent false positive while being
  3495. ;; quick.
  3496. (let ((rule-regexp "[ \t]*\\+\\(-+\\+\\)+[ \t]*$")
  3497. (next (line-beginning-position 2)))
  3498. (and (looking-at rule-regexp)
  3499. (save-excursion
  3500. (forward-line)
  3501. (re-search-forward "^[ \t]*\\($\\|[^|]\\)" limit t)
  3502. (and (> (line-beginning-position) next)
  3503. (org-match-line rule-regexp))))))
  3504. (org-element-table-parser limit affiliated))
  3505. ;; List.
  3506. ((looking-at (org-item-re))
  3507. (org-element-plain-list-parser
  3508. limit affiliated
  3509. (or structure (org-element--list-struct limit))))
  3510. ;; Default element: Paragraph.
  3511. (t (org-element-paragraph-parser limit affiliated)))))))))
  3512. ;; Most elements can have affiliated keywords. When looking for an
  3513. ;; element beginning, we want to move before them, as they belong to
  3514. ;; that element, and, in the meantime, collect information they give
  3515. ;; into appropriate properties. Hence the following function.
  3516. (defun org-element--collect-affiliated-keywords (limit)
  3517. "Collect affiliated keywords from point down to LIMIT.
  3518. Return a list whose CAR is the position at the first of them and
  3519. CDR a plist of keywords and values and move point to the
  3520. beginning of the first line after them.
  3521. As a special case, if element doesn't start at the beginning of
  3522. the line (e.g., a paragraph starting an item), CAR is current
  3523. position of point and CDR is nil."
  3524. (if (not (bolp)) (list (point))
  3525. (let ((case-fold-search t)
  3526. (origin (point))
  3527. ;; RESTRICT is the list of objects allowed in parsed
  3528. ;; keywords value.
  3529. (restrict (org-element-restriction 'keyword))
  3530. output)
  3531. (while (and (< (point) limit) (looking-at org-element--affiliated-re))
  3532. (let* ((raw-kwd (upcase (match-string 1)))
  3533. ;; Apply translation to RAW-KWD. From there, KWD is
  3534. ;; the official keyword.
  3535. (kwd (or (cdr (assoc raw-kwd
  3536. org-element-keyword-translation-alist))
  3537. raw-kwd))
  3538. ;; Find main value for any keyword.
  3539. (value
  3540. (save-match-data
  3541. (org-trim
  3542. (buffer-substring-no-properties
  3543. (match-end 0) (line-end-position)))))
  3544. ;; PARSEDP is non-nil when keyword should have its
  3545. ;; value parsed.
  3546. (parsedp (member kwd org-element-parsed-keywords))
  3547. ;; If KWD is a dual keyword, find its secondary
  3548. ;; value. Maybe parse it.
  3549. (dualp (member kwd org-element-dual-keywords))
  3550. (dual-value
  3551. (and dualp
  3552. (let ((sec (match-string-no-properties 2)))
  3553. (if (or (not sec) (not parsedp)) sec
  3554. (save-match-data
  3555. (org-element--parse-objects
  3556. (match-beginning 2) (match-end 2) nil restrict))))))
  3557. ;; Attribute a property name to KWD.
  3558. (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
  3559. ;; Now set final shape for VALUE.
  3560. (when parsedp
  3561. (setq value
  3562. (org-element--parse-objects
  3563. (match-end 0)
  3564. (progn (end-of-line) (skip-chars-backward " \t") (point))
  3565. nil restrict)))
  3566. (when dualp
  3567. (setq value (and (or value dual-value) (cons value dual-value))))
  3568. (when (or (member kwd org-element-multiple-keywords)
  3569. ;; Attributes can always appear on multiple lines.
  3570. (string-match "^ATTR_" kwd))
  3571. (setq value (cons value (plist-get output kwd-sym))))
  3572. ;; Eventually store the new value in OUTPUT.
  3573. (setq output (plist-put output kwd-sym value))
  3574. ;; Move to next keyword.
  3575. (forward-line)))
  3576. ;; If affiliated keywords are orphaned: move back to first one.
  3577. ;; They will be parsed as a paragraph.
  3578. (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
  3579. ;; Return value.
  3580. (cons origin output))))
  3581. ;;; The Org Parser
  3582. ;;
  3583. ;; The two major functions here are `org-element-parse-buffer', which
  3584. ;; parses Org syntax inside the current buffer, taking into account
  3585. ;; region, narrowing, or even visibility if specified, and
  3586. ;; `org-element-parse-secondary-string', which parses objects within
  3587. ;; a given string.
  3588. ;;
  3589. ;; The (almost) almighty `org-element-map' allows applying a function
  3590. ;; on elements or objects matching some type, and accumulating the
  3591. ;; resulting values. In an export situation, it also skips unneeded
  3592. ;; parts of the parse tree.
  3593. (defun org-element-parse-buffer (&optional granularity visible-only)
  3594. "Recursively parse the buffer and return structure.
  3595. If narrowing is in effect, only parse the visible part of the
  3596. buffer.
  3597. Optional argument GRANULARITY determines the depth of the
  3598. recursion. It can be set to the following symbols:
  3599. `headline' Only parse headlines.
  3600. `greater-element' Don't recurse into greater elements except
  3601. headlines and sections. Thus, elements
  3602. parsed are the top-level ones.
  3603. `element' Parse everything but objects and plain text.
  3604. `object' Parse the complete buffer (default).
  3605. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3606. elements.
  3607. An element or object is represented as a list with the
  3608. pattern (TYPE PROPERTIES CONTENTS), where :
  3609. TYPE is a symbol describing the element or object. See
  3610. `org-element-all-elements' and `org-element-all-objects' for an
  3611. exhaustive list of such symbols. One can retrieve it with
  3612. `org-element-type' function.
  3613. PROPERTIES is the list of attributes attached to the element or
  3614. object, as a plist. Although most of them are specific to the
  3615. element or object type, all types share `:begin', `:end',
  3616. `:post-blank' and `:parent' properties, which respectively
  3617. refer to buffer position where the element or object starts,
  3618. ends, the number of white spaces or blank lines after it, and
  3619. the element or object containing it. Properties values can be
  3620. obtained by using `org-element-property' function.
  3621. CONTENTS is a list of elements, objects or raw strings
  3622. contained in the current element or object, when applicable.
  3623. One can access them with `org-element-contents' function.
  3624. The Org buffer has `org-data' as type and nil as properties.
  3625. `org-element-map' function can be used to find specific elements
  3626. or objects within the parse tree.
  3627. This function assumes that current major mode is `org-mode'."
  3628. (save-excursion
  3629. (goto-char (point-min))
  3630. (org-skip-whitespace)
  3631. (org-element--parse-elements
  3632. (point-at-bol) (point-max)
  3633. ;; Start in `first-section' mode so text before the first
  3634. ;; headline belongs to a section.
  3635. 'first-section nil granularity visible-only (list 'org-data nil))))
  3636. (defun org-element-parse-secondary-string (string restriction &optional parent)
  3637. "Recursively parse objects in STRING and return structure.
  3638. RESTRICTION is a symbol limiting the object types that will be
  3639. looked after.
  3640. Optional argument PARENT, when non-nil, is the element or object
  3641. containing the secondary string. It is used to set correctly
  3642. `:parent' property within the string.
  3643. If STRING is the empty string or nil, return nil."
  3644. (cond
  3645. ((not string) nil)
  3646. ((equal string "") nil)
  3647. (t (let ((local-variables (buffer-local-variables)))
  3648. (with-temp-buffer
  3649. (dolist (v local-variables)
  3650. (ignore-errors
  3651. (if (symbolp v) (makunbound v)
  3652. (set (make-local-variable (car v)) (cdr v)))))
  3653. ;; Transferring local variables may put the temporary buffer
  3654. ;; into a read-only state. Make sure we can insert STRING.
  3655. (let ((inhibit-read-only t)) (insert string))
  3656. ;; Prevent "Buffer *temp* modified; kill anyway?".
  3657. (restore-buffer-modified-p nil)
  3658. (org-element--parse-objects
  3659. (point-min) (point-max) nil restriction parent))))))
  3660. (defun org-element-map
  3661. (data types fun &optional info first-match no-recursion with-affiliated)
  3662. "Map a function on selected elements or objects.
  3663. DATA is a parse tree, an element, an object, a string, or a list
  3664. of such constructs. TYPES is a symbol or list of symbols of
  3665. elements or objects types (see `org-element-all-elements' and
  3666. `org-element-all-objects' for a complete list of types). FUN is
  3667. the function called on the matching element or object. It has to
  3668. accept one argument: the element or object itself.
  3669. When optional argument INFO is non-nil, it should be a plist
  3670. holding export options. In that case, parts of the parse tree
  3671. not exportable according to that property list will be skipped.
  3672. When optional argument FIRST-MATCH is non-nil, stop at the first
  3673. match for which FUN doesn't return nil, and return that value.
  3674. Optional argument NO-RECURSION is a symbol or a list of symbols
  3675. representing elements or objects types. `org-element-map' won't
  3676. enter any recursive element or object whose type belongs to that
  3677. list. Though, FUN can still be applied on them.
  3678. When optional argument WITH-AFFILIATED is non-nil, FUN will also
  3679. apply to matching objects within parsed affiliated keywords (see
  3680. `org-element-parsed-keywords').
  3681. Nil values returned from FUN do not appear in the results.
  3682. Examples:
  3683. ---------
  3684. Assuming TREE is a variable containing an Org buffer parse tree,
  3685. the following example will return a flat list of all `src-block'
  3686. and `example-block' elements in it:
  3687. (org-element-map tree \\='(example-block src-block) #\\='identity)
  3688. The following snippet will find the first headline with a level
  3689. of 1 and a \"phone\" tag, and will return its beginning position:
  3690. (org-element-map tree \\='headline
  3691. (lambda (hl)
  3692. (and (= (org-element-property :level hl) 1)
  3693. (member \"phone\" (org-element-property :tags hl))
  3694. (org-element-property :begin hl)))
  3695. nil t)
  3696. The next example will return a flat list of all `plain-list' type
  3697. elements in TREE that are not a sub-list themselves:
  3698. (org-element-map tree \\='plain-list #\\='identity nil nil \\='plain-list)
  3699. Eventually, this example will return a flat list of all `bold'
  3700. type objects containing a `latex-snippet' type object, even
  3701. looking into captions:
  3702. (org-element-map tree \\='bold
  3703. (lambda (b)
  3704. (and (org-element-map b \\='latex-snippet #\\='identity nil t) b))
  3705. nil nil nil t)"
  3706. ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
  3707. (let* ((types (if (listp types) types (list types)))
  3708. (no-recursion (if (listp no-recursion) no-recursion
  3709. (list no-recursion)))
  3710. ;; Recursion depth is determined by --CATEGORY.
  3711. (--category
  3712. (catch :--found
  3713. (let ((category 'greater-elements)
  3714. (all-objects (cons 'plain-text org-element-all-objects)))
  3715. (dolist (type types category)
  3716. (cond ((memq type all-objects)
  3717. ;; If one object is found, the function has
  3718. ;; to recurse into every object.
  3719. (throw :--found 'objects))
  3720. ((not (memq type org-element-greater-elements))
  3721. ;; If one regular element is found, the
  3722. ;; function has to recurse, at least, into
  3723. ;; every element it encounters.
  3724. (and (not (eq category 'elements))
  3725. (setq category 'elements))))))))
  3726. --acc)
  3727. (letrec ((--walk-tree
  3728. (lambda (--data)
  3729. ;; Recursively walk DATA. INFO, if non-nil, is a plist
  3730. ;; holding contextual information.
  3731. (let ((--type (org-element-type --data)))
  3732. (cond
  3733. ((not --data))
  3734. ;; Ignored element in an export context.
  3735. ((and info (memq --data (plist-get info :ignore-list))))
  3736. ;; List of elements or objects.
  3737. ((not --type) (mapc --walk-tree --data))
  3738. ;; Unconditionally enter parse trees.
  3739. ((eq --type 'org-data)
  3740. (mapc --walk-tree (org-element-contents --data)))
  3741. (t
  3742. ;; Check if TYPE is matching among TYPES. If so,
  3743. ;; apply FUN to --DATA and accumulate return value
  3744. ;; into --ACC (or exit if FIRST-MATCH is non-nil).
  3745. (when (memq --type types)
  3746. (let ((result (funcall fun --data)))
  3747. (cond ((not result))
  3748. (first-match (throw :--map-first-match result))
  3749. (t (push result --acc)))))
  3750. ;; If --DATA has a secondary string that can contain
  3751. ;; objects with their type among TYPES, look inside.
  3752. (when (and (eq --category 'objects) (not (stringp --data)))
  3753. (dolist (p (cdr (assq --type
  3754. org-element-secondary-value-alist)))
  3755. (funcall --walk-tree (org-element-property p --data))))
  3756. ;; If --DATA has any parsed affiliated keywords and
  3757. ;; WITH-AFFILIATED is non-nil, look for objects in
  3758. ;; them.
  3759. (when (and with-affiliated
  3760. (eq --category 'objects)
  3761. (eq (org-element-class --data) 'element))
  3762. (dolist (kwd-pair org-element--parsed-properties-alist)
  3763. (let ((kwd (car kwd-pair))
  3764. (value (org-element-property (cdr kwd-pair) --data)))
  3765. ;; Pay attention to the type of parsed
  3766. ;; keyword. In particular, preserve order for
  3767. ;; multiple keywords.
  3768. (cond
  3769. ((not value))
  3770. ((member kwd org-element-dual-keywords)
  3771. (if (member kwd org-element-multiple-keywords)
  3772. (dolist (line (reverse value))
  3773. (funcall --walk-tree (cdr line))
  3774. (funcall --walk-tree (car line)))
  3775. (funcall --walk-tree (cdr value))
  3776. (funcall --walk-tree (car value))))
  3777. ((member kwd org-element-multiple-keywords)
  3778. (mapc --walk-tree (reverse value)))
  3779. (t (funcall --walk-tree value))))))
  3780. ;; Determine if a recursion into --DATA is possible.
  3781. (cond
  3782. ;; --TYPE is explicitly removed from recursion.
  3783. ((memq --type no-recursion))
  3784. ;; --DATA has no contents.
  3785. ((not (org-element-contents --data)))
  3786. ;; Looking for greater elements but --DATA is
  3787. ;; simply an element or an object.
  3788. ((and (eq --category 'greater-elements)
  3789. (not (memq --type org-element-greater-elements))))
  3790. ;; Looking for elements but --DATA is an object.
  3791. ((and (eq --category 'elements)
  3792. (eq (org-element-class --data) 'object)))
  3793. ;; In any other case, map contents.
  3794. (t (mapc --walk-tree (org-element-contents --data))))))))))
  3795. (catch :--map-first-match
  3796. (funcall --walk-tree data)
  3797. ;; Return value in a proper order.
  3798. (nreverse --acc)))))
  3799. (put 'org-element-map 'lisp-indent-function 2)
  3800. ;; The following functions are internal parts of the parser.
  3801. ;;
  3802. ;; The first one, `org-element--parse-elements' acts at the element's
  3803. ;; level.
  3804. ;;
  3805. ;; The second one, `org-element--parse-objects' applies on all objects
  3806. ;; of a paragraph or a secondary string. It calls
  3807. ;; `org-element--object-lex' to find the next object in the current
  3808. ;; container.
  3809. (defsubst org-element--next-mode (type parentp)
  3810. "Return next special mode according to TYPE, or nil.
  3811. TYPE is a symbol representing the type of an element or object
  3812. containing next element if PARENTP is non-nil, or before it
  3813. otherwise. Modes can be either `first-section', `item',
  3814. `node-property', `planning', `property-drawer', `section',
  3815. `table-row' or nil."
  3816. (if parentp
  3817. (pcase type
  3818. (`headline 'section)
  3819. (`inlinetask 'planning)
  3820. (`plain-list 'item)
  3821. (`property-drawer 'node-property)
  3822. (`section 'planning)
  3823. (`table 'table-row))
  3824. (pcase type
  3825. (`item 'item)
  3826. (`node-property 'node-property)
  3827. (`planning 'property-drawer)
  3828. (`table-row 'table-row))))
  3829. (defun org-element--parse-elements
  3830. (beg end mode structure granularity visible-only acc)
  3831. "Parse elements between BEG and END positions.
  3832. MODE prioritizes some elements over the others. It can be set to
  3833. `first-section', `section', `planning', `item', `node-property'
  3834. or `table-row'.
  3835. When value is `item', STRUCTURE will be used as the current list
  3836. structure.
  3837. GRANULARITY determines the depth of the recursion. See
  3838. `org-element-parse-buffer' for more information.
  3839. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3840. elements.
  3841. Elements are accumulated into ACC."
  3842. (save-excursion
  3843. (goto-char beg)
  3844. ;; Visible only: skip invisible parts at the beginning of the
  3845. ;; element.
  3846. (when (and visible-only (org-invisible-p2))
  3847. (goto-char (min (1+ (org-find-visible)) end)))
  3848. ;; When parsing only headlines, skip any text before first one.
  3849. (when (and (eq granularity 'headline) (not (org-at-heading-p)))
  3850. (org-with-limited-levels (outline-next-heading)))
  3851. (let (elements)
  3852. (while (< (point) end)
  3853. ;; Find current element's type and parse it accordingly to
  3854. ;; its category.
  3855. (let* ((element (org-element--current-element
  3856. end granularity mode structure))
  3857. (type (org-element-type element))
  3858. (cbeg (org-element-property :contents-begin element)))
  3859. (goto-char (org-element-property :end element))
  3860. ;; Visible only: skip invisible parts between siblings.
  3861. (when (and visible-only (org-invisible-p2))
  3862. (goto-char (min (1+ (org-find-visible)) end)))
  3863. ;; Fill ELEMENT contents by side-effect.
  3864. (cond
  3865. ;; If element has no contents, don't modify it.
  3866. ((not cbeg))
  3867. ;; Greater element: parse it between `contents-begin' and
  3868. ;; `contents-end'. Make sure GRANULARITY allows the
  3869. ;; recursion, or ELEMENT is a headline, in which case going
  3870. ;; inside is mandatory, in order to get sub-level headings.
  3871. ((and (memq type org-element-greater-elements)
  3872. (or (memq granularity '(element object nil))
  3873. (and (eq granularity 'greater-element)
  3874. (eq type 'section))
  3875. (eq type 'headline)))
  3876. (org-element--parse-elements
  3877. cbeg (org-element-property :contents-end element)
  3878. ;; Possibly switch to a special mode.
  3879. (org-element--next-mode type t)
  3880. (and (memq type '(item plain-list))
  3881. (org-element-property :structure element))
  3882. granularity visible-only element))
  3883. ;; ELEMENT has contents. Parse objects inside, if
  3884. ;; GRANULARITY allows it.
  3885. ((memq granularity '(object nil))
  3886. (org-element--parse-objects
  3887. cbeg (org-element-property :contents-end element) element
  3888. (org-element-restriction type))))
  3889. (push (org-element-put-property element :parent acc) elements)
  3890. ;; Update mode.
  3891. (setq mode (org-element--next-mode type nil))))
  3892. ;; Return result.
  3893. (apply #'org-element-set-contents acc (nreverse elements)))))
  3894. (defun org-element--object-lex (restriction)
  3895. "Return next object in current buffer or nil.
  3896. RESTRICTION is a list of object types, as symbols, that should be
  3897. looked after. This function assumes that the buffer is narrowed
  3898. to an appropriate container (e.g., a paragraph)."
  3899. (if (memq 'table-cell restriction) (org-element-table-cell-parser)
  3900. (let* ((start (point))
  3901. (limit
  3902. ;; Object regexp sometimes needs to have a peek at
  3903. ;; a character ahead. Therefore, when there is a hard
  3904. ;; limit, make it one more than the true beginning of the
  3905. ;; radio target.
  3906. (save-excursion
  3907. (cond ((not org-target-link-regexp) nil)
  3908. ((not (memq 'link restriction)) nil)
  3909. ((progn
  3910. (unless (bolp) (forward-char -1))
  3911. (not (re-search-forward org-target-link-regexp nil t)))
  3912. nil)
  3913. ;; Since we moved backward, we do not want to
  3914. ;; match again an hypothetical 1-character long
  3915. ;; radio link before us. Realizing that this can
  3916. ;; only happen if such a radio link starts at
  3917. ;; beginning of line, we prevent this here.
  3918. ((and (= start (1+ (line-beginning-position)))
  3919. (= start (match-end 1)))
  3920. (and (re-search-forward org-target-link-regexp nil t)
  3921. (1+ (match-beginning 1))))
  3922. (t (1+ (match-beginning 1))))))
  3923. found)
  3924. (save-excursion
  3925. (while (and (not found)
  3926. (re-search-forward org-element--object-regexp limit 'move))
  3927. (goto-char (match-beginning 0))
  3928. (let ((result (match-string 0)))
  3929. (setq found
  3930. (cond
  3931. ((string-prefix-p "call_" result t)
  3932. (and (memq 'inline-babel-call restriction)
  3933. (org-element-inline-babel-call-parser)))
  3934. ((string-prefix-p "src_" result t)
  3935. (and (memq 'inline-src-block restriction)
  3936. (org-element-inline-src-block-parser)))
  3937. (t
  3938. (pcase (char-after)
  3939. (?^ (and (memq 'superscript restriction)
  3940. (org-element-superscript-parser)))
  3941. (?_ (or (and (memq 'subscript restriction)
  3942. (org-element-subscript-parser))
  3943. (and (memq 'underline restriction)
  3944. (org-element-underline-parser))))
  3945. (?* (and (memq 'bold restriction)
  3946. (org-element-bold-parser)))
  3947. (?/ (and (memq 'italic restriction)
  3948. (org-element-italic-parser)))
  3949. (?~ (and (memq 'code restriction)
  3950. (org-element-code-parser)))
  3951. (?= (and (memq 'verbatim restriction)
  3952. (org-element-verbatim-parser)))
  3953. (?+ (and (memq 'strike-through restriction)
  3954. (org-element-strike-through-parser)))
  3955. (?@ (and (memq 'export-snippet restriction)
  3956. (org-element-export-snippet-parser)))
  3957. (?{ (and (memq 'macro restriction)
  3958. (org-element-macro-parser)))
  3959. (?$ (and (memq 'latex-fragment restriction)
  3960. (org-element-latex-fragment-parser)))
  3961. (?<
  3962. (if (eq (aref result 1) ?<)
  3963. (or (and (memq 'radio-target restriction)
  3964. (org-element-radio-target-parser))
  3965. (and (memq 'target restriction)
  3966. (org-element-target-parser)))
  3967. (or (and (memq 'timestamp restriction)
  3968. (org-element-timestamp-parser))
  3969. (and (memq 'link restriction)
  3970. (org-element-link-parser)))))
  3971. (?\\
  3972. (if (eq (aref result 1) ?\\)
  3973. (and (memq 'line-break restriction)
  3974. (org-element-line-break-parser))
  3975. (or (and (memq 'entity restriction)
  3976. (org-element-entity-parser))
  3977. (and (memq 'latex-fragment restriction)
  3978. (org-element-latex-fragment-parser)))))
  3979. (?\[
  3980. (if (eq (aref result 1) ?\[)
  3981. (and (memq 'link restriction)
  3982. (org-element-link-parser))
  3983. (or (and (memq 'footnote-reference restriction)
  3984. (org-element-footnote-reference-parser))
  3985. (and (memq 'timestamp restriction)
  3986. (org-element-timestamp-parser))
  3987. (and (memq 'statistics-cookie restriction)
  3988. (org-element-statistics-cookie-parser)))))
  3989. ;; This is probably a plain link.
  3990. (_ (and (memq 'link restriction)
  3991. (org-element-link-parser)))))))
  3992. (or (eobp) (forward-char))))
  3993. (cond (found)
  3994. (limit (forward-char -1)
  3995. (org-element-link-parser)) ;radio link
  3996. (t nil))))))
  3997. (defun org-element--parse-objects (beg end acc restriction &optional parent)
  3998. "Parse objects between BEG and END and return recursive structure.
  3999. Objects are accumulated in ACC. RESTRICTION is a list of object
  4000. successors which are allowed in the current object.
  4001. ACC becomes the parent for all parsed objects. However, if ACC
  4002. is nil (i.e., a secondary string is being parsed) and optional
  4003. argument PARENT is non-nil, use it as the parent for all objects.
  4004. Eventually, if both ACC and PARENT are nil, the common parent is
  4005. the list of objects itself."
  4006. (save-excursion
  4007. (save-restriction
  4008. (narrow-to-region beg end)
  4009. (goto-char (point-min))
  4010. (let (next-object contents)
  4011. (while (and (not (eobp))
  4012. (setq next-object (org-element--object-lex restriction)))
  4013. ;; Text before any object.
  4014. (let ((obj-beg (org-element-property :begin next-object)))
  4015. (unless (= (point) obj-beg)
  4016. (let ((text (buffer-substring-no-properties (point) obj-beg)))
  4017. (push (if acc (org-element-put-property text :parent acc) text)
  4018. contents))))
  4019. ;; Object...
  4020. (let ((obj-end (org-element-property :end next-object))
  4021. (cont-beg (org-element-property :contents-begin next-object)))
  4022. (when acc (org-element-put-property next-object :parent acc))
  4023. (push (if cont-beg
  4024. ;; Fill contents of NEXT-OBJECT if possible.
  4025. (org-element--parse-objects
  4026. cont-beg
  4027. (org-element-property :contents-end next-object)
  4028. next-object
  4029. (org-element-restriction next-object))
  4030. next-object)
  4031. contents)
  4032. (goto-char obj-end)))
  4033. ;; Text after last object.
  4034. (unless (eobp)
  4035. (let ((text (buffer-substring-no-properties (point) end)))
  4036. (push (if acc (org-element-put-property text :parent acc) text)
  4037. contents)))
  4038. ;; Result. Set appropriate parent.
  4039. (if acc (apply #'org-element-set-contents acc (nreverse contents))
  4040. (let* ((contents (nreverse contents))
  4041. (parent (or parent contents)))
  4042. (dolist (datum contents contents)
  4043. (org-element-put-property datum :parent parent))))))))
  4044. ;;; Towards A Bijective Process
  4045. ;;
  4046. ;; The parse tree obtained with `org-element-parse-buffer' is really
  4047. ;; a snapshot of the corresponding Org buffer. Therefore, it can be
  4048. ;; interpreted and expanded into a string with canonical Org syntax.
  4049. ;; Hence `org-element-interpret-data'.
  4050. ;;
  4051. ;; The function relies internally on
  4052. ;; `org-element--interpret-affiliated-keywords'.
  4053. ;;;###autoload
  4054. (defun org-element-interpret-data (data)
  4055. "Interpret DATA as Org syntax.
  4056. DATA is a parse tree, an element, an object or a secondary string
  4057. to interpret. Return Org syntax as a string."
  4058. (letrec ((fun
  4059. (lambda (data parent)
  4060. (let* ((type (org-element-type data))
  4061. ;; Find interpreter for current object or
  4062. ;; element. If it doesn't exist (e.g. this is
  4063. ;; a pseudo object or element), return contents,
  4064. ;; if any.
  4065. (interpret
  4066. (let ((fun (intern
  4067. (format "org-element-%s-interpreter" type))))
  4068. (if (fboundp fun) fun (lambda (_ contents) contents))))
  4069. (results
  4070. (cond
  4071. ;; Secondary string.
  4072. ((not type)
  4073. (mapconcat (lambda (obj) (funcall fun obj parent))
  4074. data
  4075. ""))
  4076. ;; Full Org document.
  4077. ((eq type 'org-data)
  4078. (mapconcat (lambda (obj) (funcall fun obj parent))
  4079. (org-element-contents data)
  4080. ""))
  4081. ;; Plain text: return it.
  4082. ((stringp data) data)
  4083. ;; Element or object without contents.
  4084. ((not (org-element-contents data))
  4085. (funcall interpret data nil))
  4086. ;; Element or object with contents.
  4087. (t
  4088. (funcall
  4089. interpret
  4090. data
  4091. ;; Recursively interpret contents.
  4092. (mapconcat
  4093. (lambda (datum) (funcall fun datum data))
  4094. (org-element-contents
  4095. (if (not (memq type '(paragraph verse-block)))
  4096. data
  4097. ;; Fix indentation of elements containing
  4098. ;; objects. We ignore `table-row'
  4099. ;; elements as they are one line long
  4100. ;; anyway.
  4101. (org-element-normalize-contents
  4102. data
  4103. ;; When normalizing first paragraph of
  4104. ;; an item or a footnote-definition,
  4105. ;; ignore first line's indentation.
  4106. (and (eq type 'paragraph)
  4107. (memq (org-element-type parent)
  4108. '(footnote-definition item))
  4109. (eq data (car (org-element-contents parent)))
  4110. (eq (org-element-property :pre-blank parent)
  4111. 0)))))
  4112. ""))))))
  4113. (if (memq type '(org-data plain-text nil)) results
  4114. ;; Build white spaces. If no `:post-blank' property
  4115. ;; is specified, assume its value is 0.
  4116. (let ((blank (or (org-element-property :post-blank data) 0)))
  4117. (if (eq (org-element-class data parent) 'object)
  4118. (concat results (make-string blank ?\s))
  4119. (concat (org-element--interpret-affiliated-keywords data)
  4120. (org-element-normalize-string results)
  4121. (make-string blank ?\n)))))))))
  4122. (funcall fun data nil)))
  4123. (defun org-element--interpret-affiliated-keywords (element)
  4124. "Return ELEMENT's affiliated keywords as Org syntax.
  4125. If there is no affiliated keyword, return the empty string."
  4126. (let ((keyword-to-org
  4127. (function
  4128. (lambda (key value)
  4129. (let (dual)
  4130. (when (member key org-element-dual-keywords)
  4131. (setq dual (cdr value) value (car value)))
  4132. (concat "#+" (downcase key)
  4133. (and dual
  4134. (format "[%s]" (org-element-interpret-data dual)))
  4135. ": "
  4136. (if (member key org-element-parsed-keywords)
  4137. (org-element-interpret-data value)
  4138. value)
  4139. "\n"))))))
  4140. (mapconcat
  4141. (lambda (prop)
  4142. (let ((value (org-element-property prop element))
  4143. (keyword (upcase (substring (symbol-name prop) 1))))
  4144. (when value
  4145. (if (or (member keyword org-element-multiple-keywords)
  4146. ;; All attribute keywords can have multiple lines.
  4147. (string-match "^ATTR_" keyword))
  4148. (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
  4149. (reverse value)
  4150. "")
  4151. (funcall keyword-to-org keyword value)))))
  4152. ;; List all ELEMENT's properties matching an attribute line or an
  4153. ;; affiliated keyword, but ignore translated keywords since they
  4154. ;; cannot belong to the property list.
  4155. (cl-loop for prop in (nth 1 element) by 'cddr
  4156. when (let ((keyword (upcase (substring (symbol-name prop) 1))))
  4157. (or (string-match "^ATTR_" keyword)
  4158. (and
  4159. (member keyword org-element-affiliated-keywords)
  4160. (not (assoc keyword
  4161. org-element-keyword-translation-alist)))))
  4162. collect prop)
  4163. "")))
  4164. ;; Because interpretation of the parse tree must return the same
  4165. ;; number of blank lines between elements and the same number of white
  4166. ;; space after objects, some special care must be given to white
  4167. ;; spaces.
  4168. ;;
  4169. ;; The first function, `org-element-normalize-string', ensures any
  4170. ;; string different from the empty string will end with a single
  4171. ;; newline character.
  4172. ;;
  4173. ;; The second function, `org-element-normalize-contents', removes
  4174. ;; global indentation from the contents of the current element.
  4175. (defun org-element-normalize-string (s)
  4176. "Ensure string S ends with a single newline character.
  4177. If S isn't a string return it unchanged. If S is the empty
  4178. string, return it. Otherwise, return a new string with a single
  4179. newline character at its end."
  4180. (cond
  4181. ((not (stringp s)) s)
  4182. ((string= "" s) "")
  4183. (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
  4184. (replace-match "\n" nil nil s)))))
  4185. (defun org-element-normalize-contents (element &optional ignore-first)
  4186. "Normalize plain text in ELEMENT's contents.
  4187. ELEMENT must only contain plain text and objects.
  4188. If optional argument IGNORE-FIRST is non-nil, ignore first line's
  4189. indentation to compute maximal common indentation.
  4190. Return the normalized element that is element with global
  4191. indentation removed from its contents."
  4192. (letrec ((find-min-ind
  4193. ;; Return minimal common indentation within BLOB. This is
  4194. ;; done by walking recursively BLOB and updating MIN-IND
  4195. ;; along the way. FIRST-FLAG is non-nil when the next
  4196. ;; object is expected to be a string that doesn't start
  4197. ;; with a newline character. It happens for strings at
  4198. ;; the beginnings of the contents or right after a line
  4199. ;; break.
  4200. (lambda (blob first-flag min-ind)
  4201. (dolist (datum (org-element-contents blob) min-ind)
  4202. (when first-flag
  4203. (setq first-flag nil)
  4204. (cond
  4205. ;; Objects cannot start with spaces: in this
  4206. ;; case, indentation is 0.
  4207. ((not (stringp datum)) (throw :zero 0))
  4208. ((not (string-match
  4209. "\\`\\([ \t]+\\)\\([^ \t\n]\\|\n\\|\\'\\)" datum))
  4210. (throw :zero 0))
  4211. ((equal (match-string 2 datum) "\n")
  4212. (put-text-property
  4213. (match-beginning 1) (match-end 1) 'org-ind 'empty datum))
  4214. (t
  4215. (let ((i (string-width (match-string 1 datum))))
  4216. (put-text-property
  4217. (match-beginning 1) (match-end 1) 'org-ind i datum)
  4218. (setq min-ind (min i min-ind))))))
  4219. (cond
  4220. ((stringp datum)
  4221. (let ((s 0))
  4222. (while (string-match
  4223. "\n\\([ \t]*\\)\\([^ \t\n]\\|\n\\|\\'\\)" datum s)
  4224. (setq s (match-end 1))
  4225. (cond
  4226. ((equal (match-string 1 datum) "")
  4227. (unless (member (match-string 2 datum) '("" "\n"))
  4228. (throw :zero 0)))
  4229. ((equal (match-string 2 datum) "\n")
  4230. (put-text-property (match-beginning 1) (match-end 1)
  4231. 'org-ind 'empty datum))
  4232. (t
  4233. (let ((i (string-width (match-string 1 datum))))
  4234. (put-text-property (match-beginning 1) (match-end 1)
  4235. 'org-ind i datum)
  4236. (setq min-ind (min i min-ind))))))))
  4237. ((eq (org-element-type datum) 'line-break)
  4238. (setq first-flag t))
  4239. ((memq (org-element-type datum) org-element-recursive-objects)
  4240. (setq min-ind
  4241. (funcall find-min-ind datum first-flag min-ind)))))))
  4242. (min-ind
  4243. (catch :zero
  4244. (funcall find-min-ind
  4245. element (not ignore-first) most-positive-fixnum))))
  4246. (if (or (zerop min-ind) (= min-ind most-positive-fixnum)) element
  4247. ;; Build ELEMENT back, replacing each string with the same
  4248. ;; string minus common indentation.
  4249. (letrec ((build
  4250. (lambda (datum)
  4251. ;; Return DATUM with all its strings indentation
  4252. ;; shortened from MIN-IND white spaces.
  4253. (setcdr
  4254. (cdr datum)
  4255. (mapcar
  4256. (lambda (object)
  4257. (cond
  4258. ((stringp object)
  4259. (with-temp-buffer
  4260. (insert object)
  4261. (let ((s (point-min)))
  4262. (while (setq s (text-property-not-all
  4263. s (point-max) 'org-ind nil))
  4264. (goto-char s)
  4265. (let ((i (get-text-property s 'org-ind)))
  4266. (delete-region s (progn
  4267. (skip-chars-forward " \t")
  4268. (point)))
  4269. (when (integerp i) (indent-to (- i min-ind))))))
  4270. (buffer-string)))
  4271. ((memq (org-element-type object)
  4272. org-element-recursive-objects)
  4273. (funcall build object))
  4274. (t object)))
  4275. (org-element-contents datum)))
  4276. datum)))
  4277. (funcall build element)))))
  4278. ;;; Cache
  4279. ;;
  4280. ;; Implement a caching mechanism for `org-element-at-point' and
  4281. ;; `org-element-context', which see.
  4282. ;;
  4283. ;; A single public function is provided: `org-element-cache-reset'.
  4284. ;;
  4285. ;; Cache is enabled by default, but can be disabled globally with
  4286. ;; `org-element-use-cache'. `org-element-cache-sync-idle-time',
  4287. ;; org-element-cache-sync-duration' and `org-element-cache-sync-break'
  4288. ;; can be tweaked to control caching behavior.
  4289. ;;
  4290. ;; Internally, parsed elements are stored in an AVL tree,
  4291. ;; `org-element--cache'. This tree is updated lazily: whenever
  4292. ;; a change happens to the buffer, a synchronization request is
  4293. ;; registered in `org-element--cache-sync-requests' (see
  4294. ;; `org-element--cache-submit-request'). During idle time, requests
  4295. ;; are processed by `org-element--cache-sync'. Synchronization also
  4296. ;; happens when an element is required from the cache. In this case,
  4297. ;; the process stops as soon as the needed element is up-to-date.
  4298. ;;
  4299. ;; A synchronization request can only apply on a synchronized part of
  4300. ;; the cache. Therefore, the cache is updated at least to the
  4301. ;; location where the new request applies. Thus, requests are ordered
  4302. ;; from left to right and all elements starting before the first
  4303. ;; request are correct. This property is used by functions like
  4304. ;; `org-element--cache-find' to retrieve elements in the part of the
  4305. ;; cache that can be trusted.
  4306. ;;
  4307. ;; A request applies to every element, starting from its original
  4308. ;; location (or key, see below). When a request is processed, it
  4309. ;; moves forward and may collide the next one. In this case, both
  4310. ;; requests are merged into a new one that starts from that element.
  4311. ;; As a consequence, the whole synchronization complexity does not
  4312. ;; depend on the number of pending requests, but on the number of
  4313. ;; elements the very first request will be applied on.
  4314. ;;
  4315. ;; Elements cannot be accessed through their beginning position, which
  4316. ;; may or may not be up-to-date. Instead, each element in the tree is
  4317. ;; associated to a key, obtained with `org-element--cache-key'. This
  4318. ;; mechanism is robust enough to preserve total order among elements
  4319. ;; even when the tree is only partially synchronized.
  4320. (defvar org-element-use-cache nil
  4321. "Non-nil when Org parser should cache its results.
  4322. WARNING: for the time being, using cache sometimes triggers
  4323. freezes. Therefore, it is disabled by default. Activate it if
  4324. you want to help debugging the issue.")
  4325. (defvar org-element-cache-sync-idle-time 0.6
  4326. "Length, in seconds, of idle time before syncing cache.")
  4327. (defvar org-element-cache-sync-duration 0.04
  4328. "Maximum duration, as a time value, for a cache synchronization.
  4329. If the synchronization is not over after this delay, the process
  4330. pauses and resumes after `org-element-cache-sync-break'
  4331. seconds.")
  4332. (defvar org-element-cache-sync-break 0.3
  4333. "Duration, as a time value, of the pause between synchronizations.
  4334. See `org-element-cache-sync-duration' for more information.")
  4335. ;;;; Data Structure
  4336. (defvar org-element--cache nil
  4337. "AVL tree used to cache elements.
  4338. Each node of the tree contains an element. Comparison is done
  4339. with `org-element--cache-compare'. This cache is used in
  4340. `org-element-at-point'.")
  4341. (defvar org-element--cache-sync-requests nil
  4342. "List of pending synchronization requests.
  4343. A request is a vector with the following pattern:
  4344. \[NEXT BEG END OFFSET PARENT PHASE]
  4345. Processing a synchronization request consists of three phases:
  4346. 0. Delete modified elements,
  4347. 1. Fill missing area in cache,
  4348. 2. Shift positions and re-parent elements after the changes.
  4349. During phase 0, NEXT is the key of the first element to be
  4350. removed, BEG and END is buffer position delimiting the
  4351. modifications. Elements starting between them (inclusive) are
  4352. removed. So are elements whose parent is removed. PARENT, when
  4353. non-nil, is the parent of the first element to be removed.
  4354. During phase 1, NEXT is the key of the next known element in
  4355. cache and BEG its beginning position. Parse buffer between that
  4356. element and the one before it in order to determine the parent of
  4357. the next element. Set PARENT to the element containing NEXT.
  4358. During phase 2, NEXT is the key of the next element to shift in
  4359. the parse tree. All elements starting from this one have their
  4360. properties relatives to buffer positions shifted by integer
  4361. OFFSET and, if they belong to element PARENT, are adopted by it.
  4362. PHASE specifies the phase number, as an integer.")
  4363. (defvar org-element--cache-sync-timer nil
  4364. "Timer used for cache synchronization.")
  4365. (defvar org-element--cache-sync-keys nil
  4366. "Hash table used to store keys during synchronization.
  4367. See `org-element--cache-key' for more information.")
  4368. (defsubst org-element--cache-key (element)
  4369. "Return a unique key for ELEMENT in cache tree.
  4370. Keys are used to keep a total order among elements in the cache.
  4371. Comparison is done with `org-element--cache-key-less-p'.
  4372. When no synchronization is taking place, a key is simply the
  4373. beginning position of the element, or that position plus one in
  4374. the case of an first item (respectively row) in
  4375. a list (respectively a table).
  4376. During a synchronization, the key is the one the element had when
  4377. the cache was synchronized for the last time. Elements added to
  4378. cache during the synchronization get a new key generated with
  4379. `org-element--cache-generate-key'.
  4380. Such keys are stored in `org-element--cache-sync-keys'. The hash
  4381. table is cleared once the synchronization is complete."
  4382. (or (gethash element org-element--cache-sync-keys)
  4383. (let* ((begin (org-element-property :begin element))
  4384. ;; Increase beginning position of items (respectively
  4385. ;; table rows) by one, so the first item can get
  4386. ;; a different key from its parent list (respectively
  4387. ;; table).
  4388. (key (if (memq (org-element-type element) '(item table-row))
  4389. (1+ begin)
  4390. begin)))
  4391. (if org-element--cache-sync-requests
  4392. (puthash element key org-element--cache-sync-keys)
  4393. key))))
  4394. (defun org-element--cache-generate-key (lower upper)
  4395. "Generate a key between LOWER and UPPER.
  4396. LOWER and UPPER are fixnums or lists of same, possibly empty.
  4397. If LOWER and UPPER are equals, return LOWER. Otherwise, return
  4398. a unique key, as an integer or a list of integers, according to
  4399. the following rules:
  4400. - LOWER and UPPER are compared level-wise until values differ.
  4401. - If, at a given level, LOWER and UPPER differ from more than
  4402. 2, the new key shares all the levels above with LOWER and
  4403. gets a new level. Its value is the mean between LOWER and
  4404. UPPER:
  4405. (1 2) + (1 4) --> (1 3)
  4406. - If LOWER has no value to compare with, it is assumed that its
  4407. value is `most-negative-fixnum'. E.g.,
  4408. (1 1) + (1 1 2)
  4409. is equivalent to
  4410. (1 1 m) + (1 1 2)
  4411. where m is `most-negative-fixnum'. Likewise, if UPPER is
  4412. short of levels, the current value is `most-positive-fixnum'.
  4413. - If they differ from only one, the new key inherits from
  4414. current LOWER level and fork it at the next level. E.g.,
  4415. (2 1) + (3 3)
  4416. is equivalent to
  4417. (2 1) + (2 M)
  4418. where M is `most-positive-fixnum'.
  4419. - If the key is only one level long, it is returned as an
  4420. integer:
  4421. (1 2) + (3 2) --> 2
  4422. When they are not equals, the function assumes that LOWER is
  4423. lesser than UPPER, per `org-element--cache-key-less-p'."
  4424. (if (equal lower upper) lower
  4425. (let ((lower (if (integerp lower) (list lower) lower))
  4426. (upper (if (integerp upper) (list upper) upper))
  4427. skip-upper key)
  4428. (catch 'exit
  4429. (while t
  4430. (let ((min (or (car lower) most-negative-fixnum))
  4431. (max (cond (skip-upper most-positive-fixnum)
  4432. ((car upper))
  4433. (t most-positive-fixnum))))
  4434. (if (< (1+ min) max)
  4435. (let ((mean (+ (ash min -1) (ash max -1) (logand min max 1))))
  4436. (throw 'exit (if key (nreverse (cons mean key)) mean)))
  4437. (when (and (< min max) (not skip-upper))
  4438. ;; When at a given level, LOWER and UPPER differ from
  4439. ;; 1, ignore UPPER altogether. Instead create a key
  4440. ;; between LOWER and the greatest key with the same
  4441. ;; prefix as LOWER so far.
  4442. (setq skip-upper t))
  4443. (push min key)
  4444. (setq lower (cdr lower) upper (cdr upper)))))))))
  4445. (defsubst org-element--cache-key-less-p (a b)
  4446. "Non-nil if key A is less than key B.
  4447. A and B are either integers or lists of integers, as returned by
  4448. `org-element--cache-key'."
  4449. (if (integerp a) (if (integerp b) (< a b) (<= a (car b)))
  4450. (if (integerp b) (< (car a) b)
  4451. (catch 'exit
  4452. (while (and a b)
  4453. (cond ((car-less-than-car a b) (throw 'exit t))
  4454. ((car-less-than-car b a) (throw 'exit nil))
  4455. (t (setq a (cdr a) b (cdr b)))))
  4456. ;; If A is empty, either keys are equal (B is also empty) and
  4457. ;; we return nil, or A is lesser than B (B is longer) and we
  4458. ;; return a non-nil value.
  4459. ;;
  4460. ;; If A is not empty, B is necessarily empty and A is greater
  4461. ;; than B (A is longer). Therefore, return nil.
  4462. (and (null a) b)))))
  4463. (defun org-element--cache-compare (a b)
  4464. "Non-nil when element A is located before element B."
  4465. (org-element--cache-key-less-p (org-element--cache-key a)
  4466. (org-element--cache-key b)))
  4467. (defsubst org-element--cache-root ()
  4468. "Return root value in cache.
  4469. This function assumes `org-element--cache' is a valid AVL tree."
  4470. (avl-tree--node-left (avl-tree--dummyroot org-element--cache)))
  4471. ;;;; Tools
  4472. (defsubst org-element--cache-active-p ()
  4473. "Non-nil when cache is active in current buffer."
  4474. (and org-element-use-cache
  4475. org-element--cache
  4476. (derived-mode-p 'org-mode)))
  4477. (defun org-element--cache-find (pos &optional side)
  4478. "Find element in cache starting at POS or before.
  4479. POS refers to a buffer position.
  4480. When optional argument SIDE is non-nil, the function checks for
  4481. elements starting at or past POS instead. If SIDE is `both', the
  4482. function returns a cons cell where car is the first element
  4483. starting at or before POS and cdr the first element starting
  4484. after POS.
  4485. The function can only find elements in the synchronized part of
  4486. the cache."
  4487. (let ((limit (and org-element--cache-sync-requests
  4488. (aref (car org-element--cache-sync-requests) 0)))
  4489. (node (org-element--cache-root))
  4490. lower upper)
  4491. (while node
  4492. (let* ((element (avl-tree--node-data node))
  4493. (begin (org-element-property :begin element)))
  4494. (cond
  4495. ((and limit
  4496. (not (org-element--cache-key-less-p
  4497. (org-element--cache-key element) limit)))
  4498. (setq node (avl-tree--node-left node)))
  4499. ((> begin pos)
  4500. (setq upper element
  4501. node (avl-tree--node-left node)))
  4502. ((< begin pos)
  4503. (setq lower element
  4504. node (avl-tree--node-right node)))
  4505. ;; We found an element in cache starting at POS. If `side'
  4506. ;; is `both' we also want the next one in order to generate
  4507. ;; a key in-between.
  4508. ;;
  4509. ;; If the element is the first row or item in a table or
  4510. ;; a plain list, we always return the table or the plain
  4511. ;; list.
  4512. ;;
  4513. ;; In any other case, we return the element found.
  4514. ((eq side 'both)
  4515. (setq lower element)
  4516. (setq node (avl-tree--node-right node)))
  4517. ((and (memq (org-element-type element) '(item table-row))
  4518. (let ((parent (org-element-property :parent element)))
  4519. (and (= (org-element-property :begin element)
  4520. (org-element-property :contents-begin parent))
  4521. (setq node nil
  4522. lower parent
  4523. upper parent)))))
  4524. (t
  4525. (setq node nil
  4526. lower element
  4527. upper element)))))
  4528. (pcase side
  4529. (`both (cons lower upper))
  4530. (`nil lower)
  4531. (_ upper))))
  4532. (defun org-element--cache-put (element)
  4533. "Store ELEMENT in current buffer's cache, if allowed."
  4534. (when (org-element--cache-active-p)
  4535. (when org-element--cache-sync-requests
  4536. ;; During synchronization, first build an appropriate key for
  4537. ;; the new element so `avl-tree-enter' can insert it at the
  4538. ;; right spot in the cache.
  4539. (let ((keys (org-element--cache-find
  4540. (org-element-property :begin element) 'both)))
  4541. (puthash element
  4542. (org-element--cache-generate-key
  4543. (and (car keys) (org-element--cache-key (car keys)))
  4544. (cond ((cdr keys) (org-element--cache-key (cdr keys)))
  4545. (org-element--cache-sync-requests
  4546. (aref (car org-element--cache-sync-requests) 0))))
  4547. org-element--cache-sync-keys)))
  4548. (avl-tree-enter org-element--cache element)))
  4549. (defsubst org-element--cache-remove (element)
  4550. "Remove ELEMENT from cache.
  4551. Assume ELEMENT belongs to cache and that a cache is active."
  4552. (avl-tree-delete org-element--cache element))
  4553. ;;;; Synchronization
  4554. (defsubst org-element--cache-set-timer (buffer)
  4555. "Set idle timer for cache synchronization in BUFFER."
  4556. (when org-element--cache-sync-timer
  4557. (cancel-timer org-element--cache-sync-timer))
  4558. (setq org-element--cache-sync-timer
  4559. (run-with-idle-timer
  4560. (let ((idle (current-idle-time)))
  4561. (if idle (org-time-add idle org-element-cache-sync-break)
  4562. org-element-cache-sync-idle-time))
  4563. nil
  4564. #'org-element--cache-sync
  4565. buffer)))
  4566. (defsubst org-element--cache-interrupt-p (time-limit)
  4567. "Non-nil when synchronization process should be interrupted.
  4568. TIME-LIMIT is a time value or nil."
  4569. (and time-limit
  4570. (or (input-pending-p)
  4571. (org-time-less-p time-limit nil))))
  4572. (defsubst org-element--cache-shift-positions (element offset &optional props)
  4573. "Shift ELEMENT properties relative to buffer positions by OFFSET.
  4574. Properties containing buffer positions are `:begin', `:end',
  4575. `:contents-begin', `:contents-end' and `:structure'. When
  4576. optional argument PROPS is a list of keywords, only shift
  4577. properties provided in that list.
  4578. Properties are modified by side-effect."
  4579. (let ((properties (nth 1 element)))
  4580. ;; Shift `:structure' property for the first plain list only: it
  4581. ;; is the only one that really matters and it prevents from
  4582. ;; shifting it more than once.
  4583. (when (and (or (not props) (memq :structure props))
  4584. (eq (org-element-type element) 'plain-list)
  4585. (not (eq (org-element-type (plist-get properties :parent))
  4586. 'item)))
  4587. (dolist (item (plist-get properties :structure))
  4588. (cl-incf (car item) offset)
  4589. (cl-incf (nth 6 item) offset)))
  4590. (dolist (key '(:begin :contents-begin :contents-end :end :post-affiliated))
  4591. (let ((value (and (or (not props) (memq key props))
  4592. (plist-get properties key))))
  4593. (and value (plist-put properties key (+ offset value)))))))
  4594. (defun org-element--cache-sync (buffer &optional threshold future-change)
  4595. "Synchronize cache with recent modification in BUFFER.
  4596. When optional argument THRESHOLD is non-nil, do the
  4597. synchronization for all elements starting before or at threshold,
  4598. then exit. Otherwise, synchronize cache for as long as
  4599. `org-element-cache-sync-duration' or until Emacs leaves idle
  4600. state.
  4601. FUTURE-CHANGE, when non-nil, is a buffer position where changes
  4602. not registered yet in the cache are going to happen. It is used
  4603. in `org-element--cache-submit-request', where cache is partially
  4604. updated before current modification are actually submitted."
  4605. (when (buffer-live-p buffer)
  4606. (with-current-buffer buffer
  4607. (let ((inhibit-quit t) request next)
  4608. (when org-element--cache-sync-timer
  4609. (cancel-timer org-element--cache-sync-timer))
  4610. (catch 'interrupt
  4611. (while org-element--cache-sync-requests
  4612. (setq request (car org-element--cache-sync-requests)
  4613. next (nth 1 org-element--cache-sync-requests))
  4614. (org-element--cache-process-request
  4615. request
  4616. (and next (aref next 0))
  4617. threshold
  4618. (and (not threshold)
  4619. (org-time-add nil
  4620. org-element-cache-sync-duration))
  4621. future-change)
  4622. ;; Request processed. Merge current and next offsets and
  4623. ;; transfer ending position.
  4624. (when next
  4625. (cl-incf (aref next 3) (aref request 3))
  4626. (aset next 2 (aref request 2)))
  4627. (setq org-element--cache-sync-requests
  4628. (cdr org-element--cache-sync-requests))))
  4629. ;; If more requests are awaiting, set idle timer accordingly.
  4630. ;; Otherwise, reset keys.
  4631. (if org-element--cache-sync-requests
  4632. (org-element--cache-set-timer buffer)
  4633. (clrhash org-element--cache-sync-keys))))))
  4634. (defun org-element--cache-process-request
  4635. (request next threshold time-limit future-change)
  4636. "Process synchronization REQUEST for all entries before NEXT.
  4637. REQUEST is a vector, built by `org-element--cache-submit-request'.
  4638. NEXT is a cache key, as returned by `org-element--cache-key'.
  4639. When non-nil, THRESHOLD is a buffer position. Synchronization
  4640. stops as soon as a shifted element begins after it.
  4641. When non-nil, TIME-LIMIT is a time value. Synchronization stops
  4642. after this time or when Emacs exits idle state.
  4643. When non-nil, FUTURE-CHANGE is a buffer position where changes
  4644. not registered yet in the cache are going to happen. See
  4645. `org-element--cache-submit-request' for more information.
  4646. Throw `interrupt' if the process stops before completing the
  4647. request."
  4648. (catch 'quit
  4649. (when (= (aref request 5) 0)
  4650. ;; Phase 0.
  4651. ;;
  4652. ;; Delete all elements starting after BEG, but not after buffer
  4653. ;; position END or past element with key NEXT. Also delete
  4654. ;; elements contained within a previously removed element
  4655. ;; (stored in `last-container').
  4656. ;;
  4657. ;; At each iteration, we start again at tree root since
  4658. ;; a deletion modifies structure of the balanced tree.
  4659. (catch 'end-phase
  4660. (while t
  4661. (when (org-element--cache-interrupt-p time-limit)
  4662. (throw 'interrupt nil))
  4663. ;; Find first element in cache with key BEG or after it.
  4664. (let ((beg (aref request 0))
  4665. (end (aref request 2))
  4666. (node (org-element--cache-root))
  4667. data data-key last-container)
  4668. (while node
  4669. (let* ((element (avl-tree--node-data node))
  4670. (key (org-element--cache-key element)))
  4671. (cond
  4672. ((org-element--cache-key-less-p key beg)
  4673. (setq node (avl-tree--node-right node)))
  4674. ((org-element--cache-key-less-p beg key)
  4675. (setq data element
  4676. data-key key
  4677. node (avl-tree--node-left node)))
  4678. (t (setq data element
  4679. data-key key
  4680. node nil)))))
  4681. (if data
  4682. (let ((pos (org-element-property :begin data)))
  4683. (if (if (or (not next)
  4684. (org-element--cache-key-less-p data-key next))
  4685. (<= pos end)
  4686. (and last-container
  4687. (let ((up data))
  4688. (while (and up (not (eq up last-container)))
  4689. (setq up (org-element-property :parent up)))
  4690. up)))
  4691. (progn (when (and (not last-container)
  4692. (> (org-element-property :end data)
  4693. end))
  4694. (setq last-container data))
  4695. (org-element--cache-remove data))
  4696. (aset request 0 data-key)
  4697. (aset request 1 pos)
  4698. (aset request 5 1)
  4699. (throw 'end-phase nil)))
  4700. ;; No element starting after modifications left in
  4701. ;; cache: further processing is futile.
  4702. (throw 'quit t))))))
  4703. (when (= (aref request 5) 1)
  4704. ;; Phase 1.
  4705. ;;
  4706. ;; Phase 0 left a hole in the cache. Some elements after it
  4707. ;; could have parents within. For example, in the following
  4708. ;; buffer:
  4709. ;;
  4710. ;; - item
  4711. ;;
  4712. ;;
  4713. ;; Paragraph1
  4714. ;;
  4715. ;; Paragraph2
  4716. ;;
  4717. ;; if we remove a blank line between "item" and "Paragraph1",
  4718. ;; everything down to "Paragraph2" is removed from cache. But
  4719. ;; the paragraph now belongs to the list, and its `:parent'
  4720. ;; property no longer is accurate.
  4721. ;;
  4722. ;; Therefore we need to parse again elements in the hole, or at
  4723. ;; least in its last section, so that we can re-parent
  4724. ;; subsequent elements, during phase 2.
  4725. ;;
  4726. ;; Note that we only need to get the parent from the first
  4727. ;; element in cache after the hole.
  4728. ;;
  4729. ;; When next key is lesser or equal to the current one, delegate
  4730. ;; phase 1 processing to next request in order to preserve key
  4731. ;; order among requests.
  4732. (let ((key (aref request 0)))
  4733. (when (and next (not (org-element--cache-key-less-p key next)))
  4734. (let ((next-request (nth 1 org-element--cache-sync-requests)))
  4735. (aset next-request 0 key)
  4736. (aset next-request 1 (aref request 1))
  4737. (aset next-request 5 1))
  4738. (throw 'quit t)))
  4739. ;; Next element will start at its beginning position plus
  4740. ;; offset, since it hasn't been shifted yet. Therefore, LIMIT
  4741. ;; contains the real beginning position of the first element to
  4742. ;; shift and re-parent.
  4743. (let ((limit (+ (aref request 1) (aref request 3))))
  4744. (cond ((and threshold (> limit threshold)) (throw 'interrupt nil))
  4745. ((and future-change (>= limit future-change))
  4746. ;; Changes are going to happen around this element and
  4747. ;; they will trigger another phase 1 request. Skip the
  4748. ;; current one.
  4749. (aset request 5 2))
  4750. (t
  4751. (let ((parent (org-element--parse-to limit t time-limit)))
  4752. (aset request 4 parent)
  4753. (aset request 5 2))))))
  4754. ;; Phase 2.
  4755. ;;
  4756. ;; Shift all elements starting from key START, but before NEXT, by
  4757. ;; OFFSET, and re-parent them when appropriate.
  4758. ;;
  4759. ;; Elements are modified by side-effect so the tree structure
  4760. ;; remains intact.
  4761. ;;
  4762. ;; Once THRESHOLD, if any, is reached, or once there is an input
  4763. ;; pending, exit. Before leaving, the current synchronization
  4764. ;; request is updated.
  4765. (let ((start (aref request 0))
  4766. (offset (aref request 3))
  4767. (parent (aref request 4))
  4768. (node (org-element--cache-root))
  4769. (stack (list nil))
  4770. (leftp t)
  4771. exit-flag)
  4772. ;; No re-parenting nor shifting planned: request is over.
  4773. (when (and (not parent) (zerop offset)) (throw 'quit t))
  4774. (while node
  4775. (let* ((data (avl-tree--node-data node))
  4776. (key (org-element--cache-key data)))
  4777. (if (and leftp (avl-tree--node-left node)
  4778. (not (org-element--cache-key-less-p key start)))
  4779. (progn (push node stack)
  4780. (setq node (avl-tree--node-left node)))
  4781. (unless (org-element--cache-key-less-p key start)
  4782. ;; We reached NEXT. Request is complete.
  4783. (when (equal key next) (throw 'quit t))
  4784. ;; Handle interruption request. Update current request.
  4785. (when (or exit-flag (org-element--cache-interrupt-p time-limit))
  4786. (aset request 0 key)
  4787. (aset request 4 parent)
  4788. (throw 'interrupt nil))
  4789. ;; Shift element.
  4790. (unless (zerop offset)
  4791. (org-element--cache-shift-positions data offset))
  4792. (let ((begin (org-element-property :begin data)))
  4793. ;; Update PARENT and re-parent DATA, only when
  4794. ;; necessary. Propagate new structures for lists.
  4795. (while (and parent
  4796. (<= (org-element-property :end parent) begin))
  4797. (setq parent (org-element-property :parent parent)))
  4798. (cond ((and (not parent) (zerop offset)) (throw 'quit nil))
  4799. ((and parent
  4800. (let ((p (org-element-property :parent data)))
  4801. (or (not p)
  4802. (< (org-element-property :begin p)
  4803. (org-element-property :begin parent)))))
  4804. (org-element-put-property data :parent parent)
  4805. (let ((s (org-element-property :structure parent)))
  4806. (when (and s (org-element-property :structure data))
  4807. (org-element-put-property data :structure s)))))
  4808. ;; Cache is up-to-date past THRESHOLD. Request
  4809. ;; interruption.
  4810. (when (and threshold (> begin threshold)) (setq exit-flag t))))
  4811. (setq node (if (setq leftp (avl-tree--node-right node))
  4812. (avl-tree--node-right node)
  4813. (pop stack))))))
  4814. ;; We reached end of tree: synchronization complete.
  4815. t)))
  4816. (defun org-element--parse-to (pos &optional syncp time-limit)
  4817. "Parse elements in current section, down to POS.
  4818. Start parsing from the closest between the last known element in
  4819. cache or headline above. Return the smallest element containing
  4820. POS.
  4821. When optional argument SYNCP is non-nil, return the parent of the
  4822. element containing POS instead. In that case, it is also
  4823. possible to provide TIME-LIMIT, which is a time value specifying
  4824. when the parsing should stop. The function throws `interrupt' if
  4825. the process stopped before finding the expected result."
  4826. (catch 'exit
  4827. (org-with-wide-buffer
  4828. (goto-char pos)
  4829. (let* ((cached (and (org-element--cache-active-p)
  4830. (org-element--cache-find pos nil)))
  4831. (begin (org-element-property :begin cached))
  4832. element next mode)
  4833. (cond
  4834. ;; Nothing in cache before point: start parsing from first
  4835. ;; element following headline above, or first element in
  4836. ;; buffer.
  4837. ((not cached)
  4838. (when (org-with-limited-levels (outline-previous-heading))
  4839. (setq mode 'planning)
  4840. (forward-line))
  4841. (skip-chars-forward " \r\t\n")
  4842. (beginning-of-line))
  4843. ;; Cache returned exact match: return it.
  4844. ((= pos begin)
  4845. (throw 'exit (if syncp (org-element-property :parent cached) cached)))
  4846. ;; There's a headline between cached value and POS: cached
  4847. ;; value is invalid. Start parsing from first element
  4848. ;; following the headline.
  4849. ((re-search-backward
  4850. (org-with-limited-levels org-outline-regexp-bol) begin t)
  4851. (forward-line)
  4852. (skip-chars-forward " \r\t\n")
  4853. (beginning-of-line)
  4854. (setq mode 'planning))
  4855. ;; Check if CACHED or any of its ancestors contain point.
  4856. ;;
  4857. ;; If there is such an element, we inspect it in order to know
  4858. ;; if we return it or if we need to parse its contents.
  4859. ;; Otherwise, we just start parsing from current location,
  4860. ;; which is right after the top-most element containing
  4861. ;; CACHED.
  4862. ;;
  4863. ;; As a special case, if POS is at the end of the buffer, we
  4864. ;; want to return the innermost element ending there.
  4865. ;;
  4866. ;; Also, if we find an ancestor and discover that we need to
  4867. ;; parse its contents, make sure we don't start from
  4868. ;; `:contents-begin', as we would otherwise go past CACHED
  4869. ;; again. Instead, in that situation, we will resume parsing
  4870. ;; from NEXT, which is located after CACHED or its higher
  4871. ;; ancestor not containing point.
  4872. (t
  4873. (let ((up cached)
  4874. (pos (if (= (point-max) pos) (1- pos) pos)))
  4875. (goto-char (or (org-element-property :contents-begin cached) begin))
  4876. (while (let ((end (org-element-property :end up)))
  4877. (and (<= end pos)
  4878. (goto-char end)
  4879. (setq up (org-element-property :parent up)))))
  4880. (cond ((not up))
  4881. ((eobp) (setq element up))
  4882. (t (setq element up next (point)))))))
  4883. ;; Parse successively each element until we reach POS.
  4884. (let ((end (or (org-element-property :end element)
  4885. (save-excursion
  4886. (org-with-limited-levels (outline-next-heading))
  4887. (point))))
  4888. (parent element))
  4889. (while t
  4890. (when syncp
  4891. (cond ((= (point) pos) (throw 'exit parent))
  4892. ((org-element--cache-interrupt-p time-limit)
  4893. (throw 'interrupt nil))))
  4894. (unless element
  4895. (setq element (org-element--current-element
  4896. end 'element mode
  4897. (org-element-property :structure parent)))
  4898. (org-element-put-property element :parent parent)
  4899. (org-element--cache-put element))
  4900. (let ((elem-end (org-element-property :end element))
  4901. (type (org-element-type element)))
  4902. (cond
  4903. ;; Skip any element ending before point. Also skip
  4904. ;; element ending at point (unless it is also the end of
  4905. ;; buffer) since we're sure that another element begins
  4906. ;; after it.
  4907. ((and (<= elem-end pos) (/= (point-max) elem-end))
  4908. (goto-char elem-end)
  4909. (setq mode (org-element--next-mode type nil)))
  4910. ;; A non-greater element contains point: return it.
  4911. ((not (memq type org-element-greater-elements))
  4912. (throw 'exit element))
  4913. ;; Otherwise, we have to decide if ELEMENT really
  4914. ;; contains POS. In that case we start parsing from
  4915. ;; contents' beginning.
  4916. ;;
  4917. ;; If POS is at contents' beginning but it is also at
  4918. ;; the beginning of the first item in a list or a table.
  4919. ;; In that case, we need to create an anchor for that
  4920. ;; list or table, so return it.
  4921. ;;
  4922. ;; Also, if POS is at the end of the buffer, no element
  4923. ;; can start after it, but more than one may end there.
  4924. ;; Arbitrarily, we choose to return the innermost of
  4925. ;; such elements.
  4926. ((let ((cbeg (org-element-property :contents-begin element))
  4927. (cend (org-element-property :contents-end element)))
  4928. (when (or syncp
  4929. (and cbeg cend
  4930. (or (< cbeg pos)
  4931. (and (= cbeg pos)
  4932. (not (memq type '(plain-list table)))))
  4933. (or (> cend pos)
  4934. (and (= cend pos) (= (point-max) pos)))))
  4935. (goto-char (or next cbeg))
  4936. (setq next nil
  4937. mode (org-element--next-mode type t)
  4938. parent element
  4939. end cend))))
  4940. ;; Otherwise, return ELEMENT as it is the smallest
  4941. ;; element containing POS.
  4942. (t (throw 'exit element))))
  4943. (setq element nil)))))))
  4944. ;;;; Staging Buffer Changes
  4945. (defconst org-element--cache-sensitive-re
  4946. (concat
  4947. org-outline-regexp-bol "\\|"
  4948. "\\\\end{[A-Za-z0-9*]+}[ \t]*$" "\\|"
  4949. "^[ \t]*\\(?:"
  4950. "#\\+\\(?:BEGIN[:_]\\|END\\(?:_\\|:?[ \t]*$\\)\\)" "\\|"
  4951. "\\\\begin{[A-Za-z0-9*]+}" "\\|"
  4952. ":\\(?:\\w\\|[-_]\\)+:[ \t]*$"
  4953. "\\)")
  4954. "Regexp matching a sensitive line, structure wise.
  4955. A sensitive line is a headline, inlinetask, block, drawer, or
  4956. latex-environment boundary. When such a line is modified,
  4957. structure changes in the document may propagate in the whole
  4958. section, possibly making cache invalid.")
  4959. (defvar org-element--cache-change-warning nil
  4960. "Non-nil when a sensitive line is about to be changed.
  4961. It is a symbol among nil, t and `headline'.")
  4962. (defun org-element--cache-before-change (beg end)
  4963. "Request extension of area going to be modified if needed.
  4964. BEG and END are the beginning and end of the range of changed
  4965. text. See `before-change-functions' for more information."
  4966. (when (org-element--cache-active-p)
  4967. (org-with-wide-buffer
  4968. (goto-char beg)
  4969. (beginning-of-line)
  4970. (let ((bottom (save-excursion (goto-char end) (line-end-position))))
  4971. (setq org-element--cache-change-warning
  4972. (save-match-data
  4973. (if (and (org-with-limited-levels (org-at-heading-p))
  4974. (= (line-end-position) bottom))
  4975. 'headline
  4976. (let ((case-fold-search t))
  4977. (re-search-forward
  4978. org-element--cache-sensitive-re bottom t)))))))))
  4979. (defun org-element--cache-after-change (beg end pre)
  4980. "Update buffer modifications for current buffer.
  4981. BEG and END are the beginning and end of the range of changed
  4982. text, and the length in bytes of the pre-change text replaced by
  4983. that range. See `after-change-functions' for more information."
  4984. (when (org-element--cache-active-p)
  4985. (org-with-wide-buffer
  4986. (goto-char beg)
  4987. (beginning-of-line)
  4988. (save-match-data
  4989. (let ((top (point))
  4990. (bottom (save-excursion (goto-char end) (line-end-position))))
  4991. ;; Determine if modified area needs to be extended, according
  4992. ;; to both previous and current state. We make a special
  4993. ;; case for headline editing: if a headline is modified but
  4994. ;; not removed, do not extend.
  4995. (when (pcase org-element--cache-change-warning
  4996. (`t t)
  4997. (`headline
  4998. (not (and (org-with-limited-levels (org-at-heading-p))
  4999. (= (line-end-position) bottom))))
  5000. (_
  5001. (let ((case-fold-search t))
  5002. (re-search-forward
  5003. org-element--cache-sensitive-re bottom t))))
  5004. ;; Effectively extend modified area.
  5005. (org-with-limited-levels
  5006. (setq top (progn (goto-char top)
  5007. (when (outline-previous-heading) (forward-line))
  5008. (point)))
  5009. (setq bottom (progn (goto-char bottom)
  5010. (if (outline-next-heading) (1- (point))
  5011. (point))))))
  5012. ;; Store synchronization request.
  5013. (let ((offset (- end beg pre)))
  5014. (org-element--cache-submit-request top (- bottom offset) offset)))))
  5015. ;; Activate a timer to process the request during idle time.
  5016. (org-element--cache-set-timer (current-buffer))))
  5017. (defun org-element--cache-for-removal (beg end offset)
  5018. "Return first element to remove from cache.
  5019. BEG and END are buffer positions delimiting buffer modifications.
  5020. OFFSET is the size of the changes.
  5021. Returned element is usually the first element in cache containing
  5022. any position between BEG and END. As an exception, greater
  5023. elements around the changes that are robust to contents
  5024. modifications are preserved and updated according to the
  5025. changes."
  5026. (let* ((elements (org-element--cache-find (1- beg) 'both))
  5027. (before (car elements))
  5028. (after (cdr elements)))
  5029. (if (not before) after
  5030. (let ((up before)
  5031. (robust-flag t))
  5032. (while up
  5033. (if (let ((type (org-element-type up)))
  5034. (and (or (memq type '(center-block dynamic-block quote-block
  5035. special-block))
  5036. ;; Drawers named "PROPERTIES" are probably
  5037. ;; a properties drawer being edited. Force
  5038. ;; parsing to check if editing is over.
  5039. (and (eq type 'drawer)
  5040. (not (string=
  5041. (org-element-property :drawer-name up)
  5042. "PROPERTIES"))))
  5043. (let ((cbeg (org-element-property :contents-begin up)))
  5044. (and cbeg
  5045. (<= cbeg beg)
  5046. (> (org-element-property :contents-end up) end)))))
  5047. ;; UP is a robust greater element containing changes.
  5048. ;; We only need to extend its ending boundaries.
  5049. (org-element--cache-shift-positions
  5050. up offset '(:contents-end :end))
  5051. (setq before up)
  5052. (when robust-flag (setq robust-flag nil)))
  5053. (setq up (org-element-property :parent up)))
  5054. ;; We're at top level element containing ELEMENT: if it's
  5055. ;; altered by buffer modifications, it is first element in
  5056. ;; cache to be removed. Otherwise, that first element is the
  5057. ;; following one.
  5058. ;;
  5059. ;; As a special case, do not remove BEFORE if it is a robust
  5060. ;; container for current changes.
  5061. (if (or (< (org-element-property :end before) beg) robust-flag) after
  5062. before)))))
  5063. (defun org-element--cache-submit-request (beg end offset)
  5064. "Submit a new cache synchronization request for current buffer.
  5065. BEG and END are buffer positions delimiting the minimal area
  5066. where cache data should be removed. OFFSET is the size of the
  5067. change, as an integer."
  5068. (let ((next (car org-element--cache-sync-requests))
  5069. delete-to delete-from)
  5070. (if (and next
  5071. (zerop (aref next 5))
  5072. (> (setq delete-to (+ (aref next 2) (aref next 3))) end)
  5073. (<= (setq delete-from (aref next 1)) end))
  5074. ;; Current changes can be merged with first sync request: we
  5075. ;; can save a partial cache synchronization.
  5076. (progn
  5077. (cl-incf (aref next 3) offset)
  5078. ;; If last change happened within area to be removed, extend
  5079. ;; boundaries of robust parents, if any. Otherwise, find
  5080. ;; first element to remove and update request accordingly.
  5081. (if (> beg delete-from)
  5082. (let ((up (aref next 4)))
  5083. (while up
  5084. (org-element--cache-shift-positions
  5085. up offset '(:contents-end :end))
  5086. (setq up (org-element-property :parent up))))
  5087. (let ((first (org-element--cache-for-removal beg delete-to offset)))
  5088. (when first
  5089. (aset next 0 (org-element--cache-key first))
  5090. (aset next 1 (org-element-property :begin first))
  5091. (aset next 4 (org-element-property :parent first))))))
  5092. ;; Ensure cache is correct up to END. Also make sure that NEXT,
  5093. ;; if any, is no longer a 0-phase request, thus ensuring that
  5094. ;; phases are properly ordered. We need to provide OFFSET as
  5095. ;; optional parameter since current modifications are not known
  5096. ;; yet to the otherwise correct part of the cache (i.e, before
  5097. ;; the first request).
  5098. (when next (org-element--cache-sync (current-buffer) end beg))
  5099. (let ((first (org-element--cache-for-removal beg end offset)))
  5100. (if first
  5101. (push (let ((beg (org-element-property :begin first))
  5102. (key (org-element--cache-key first)))
  5103. (cond
  5104. ;; When changes happen before the first known
  5105. ;; element, re-parent and shift the rest of the
  5106. ;; cache.
  5107. ((> beg end) (vector key beg nil offset nil 1))
  5108. ;; Otherwise, we find the first non robust
  5109. ;; element containing END. All elements between
  5110. ;; FIRST and this one are to be removed.
  5111. ((let ((first-end (org-element-property :end first)))
  5112. (and (> first-end end)
  5113. (vector key beg first-end offset first 0))))
  5114. (t
  5115. (let* ((element (org-element--cache-find end))
  5116. (end (org-element-property :end element))
  5117. (up element))
  5118. (while (and (setq up (org-element-property :parent up))
  5119. (>= (org-element-property :begin up) beg))
  5120. (setq end (org-element-property :end up)
  5121. element up))
  5122. (vector key beg end offset element 0)))))
  5123. org-element--cache-sync-requests)
  5124. ;; No element to remove. No need to re-parent either.
  5125. ;; Simply shift additional elements, if any, by OFFSET.
  5126. (when org-element--cache-sync-requests
  5127. (cl-incf (aref (car org-element--cache-sync-requests) 3)
  5128. offset)))))))
  5129. ;;;; Public Functions
  5130. ;;;###autoload
  5131. (defun org-element-cache-reset (&optional all)
  5132. "Reset cache in current buffer.
  5133. When optional argument ALL is non-nil, reset cache in all Org
  5134. buffers."
  5135. (interactive "P")
  5136. (dolist (buffer (if all (buffer-list) (list (current-buffer))))
  5137. (with-current-buffer buffer
  5138. (when (and org-element-use-cache (derived-mode-p 'org-mode))
  5139. (setq-local org-element--cache
  5140. (avl-tree-create #'org-element--cache-compare))
  5141. (setq-local org-element--cache-sync-keys
  5142. (make-hash-table :weakness 'key :test #'eq))
  5143. (setq-local org-element--cache-change-warning nil)
  5144. (setq-local org-element--cache-sync-requests nil)
  5145. (setq-local org-element--cache-sync-timer nil)
  5146. (add-hook 'before-change-functions
  5147. #'org-element--cache-before-change nil t)
  5148. (add-hook 'after-change-functions
  5149. #'org-element--cache-after-change nil t)))))
  5150. ;;;###autoload
  5151. (defun org-element-cache-refresh (pos)
  5152. "Refresh cache at position POS."
  5153. (when (org-element--cache-active-p)
  5154. (org-element--cache-sync (current-buffer) pos)
  5155. (org-element--cache-submit-request pos pos 0)
  5156. (org-element--cache-set-timer (current-buffer))))
  5157. ;;; The Toolbox
  5158. ;;
  5159. ;; The first move is to implement a way to obtain the smallest element
  5160. ;; containing point. This is the job of `org-element-at-point'. It
  5161. ;; basically jumps back to the beginning of section containing point
  5162. ;; and proceed, one element after the other, with
  5163. ;; `org-element--current-element' until the container is found. Note:
  5164. ;; When using `org-element-at-point', secondary values are never
  5165. ;; parsed since the function focuses on elements, not on objects.
  5166. ;;
  5167. ;; At a deeper level, `org-element-context' lists all elements and
  5168. ;; objects containing point.
  5169. ;;
  5170. ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
  5171. ;; internally by navigation and manipulation tools.
  5172. ;;;###autoload
  5173. (defun org-element-at-point ()
  5174. "Determine closest element around point.
  5175. Return value is a list like (TYPE PROPS) where TYPE is the type
  5176. of the element and PROPS a plist of properties associated to the
  5177. element.
  5178. Possible types are defined in `org-element-all-elements'.
  5179. Properties depend on element or object type, but always include
  5180. `:begin', `:end', `:parent' and `:post-blank' properties.
  5181. As a special case, if point is at the very beginning of the first
  5182. item in a list or sub-list, returned element will be that list
  5183. instead of the item. Likewise, if point is at the beginning of
  5184. the first row of a table, returned element will be the table
  5185. instead of the first row.
  5186. When point is at the end of the buffer, return the innermost
  5187. element ending there."
  5188. (org-with-wide-buffer
  5189. (let ((origin (point)))
  5190. (end-of-line)
  5191. (skip-chars-backward " \r\t\n")
  5192. (cond
  5193. ;; Within blank lines at the beginning of buffer, return nil.
  5194. ((bobp) nil)
  5195. ;; Within blank lines right after a headline, return that
  5196. ;; headline.
  5197. ((org-with-limited-levels (org-at-heading-p))
  5198. (beginning-of-line)
  5199. (org-element-headline-parser (point-max) t))
  5200. ;; Otherwise parse until we find element containing ORIGIN.
  5201. (t
  5202. (when (org-element--cache-active-p)
  5203. (if (not org-element--cache) (org-element-cache-reset)
  5204. (org-element--cache-sync (current-buffer) origin)))
  5205. (org-element--parse-to origin))))))
  5206. ;;;###autoload
  5207. (defun org-element-context (&optional element)
  5208. "Return smallest element or object around point.
  5209. Return value is a list like (TYPE PROPS) where TYPE is the type
  5210. of the element or object and PROPS a plist of properties
  5211. associated to it.
  5212. Possible types are defined in `org-element-all-elements' and
  5213. `org-element-all-objects'. Properties depend on element or
  5214. object type, but always include `:begin', `:end', `:parent' and
  5215. `:post-blank'.
  5216. As a special case, if point is right after an object and not at
  5217. the beginning of any other object, return that object.
  5218. Optional argument ELEMENT, when non-nil, is the closest element
  5219. containing point, as returned by `org-element-at-point'.
  5220. Providing it allows for quicker computation."
  5221. (catch 'objects-forbidden
  5222. (org-with-wide-buffer
  5223. (let* ((pos (point))
  5224. (element (or element (org-element-at-point)))
  5225. (type (org-element-type element))
  5226. (post (org-element-property :post-affiliated element)))
  5227. ;; If point is inside an element containing objects or
  5228. ;; a secondary string, narrow buffer to the container and
  5229. ;; proceed with parsing. Otherwise, return ELEMENT.
  5230. (cond
  5231. ;; At a parsed affiliated keyword, check if we're inside main
  5232. ;; or dual value.
  5233. ((and post (< pos post))
  5234. (beginning-of-line)
  5235. (let ((case-fold-search t)) (looking-at org-element--affiliated-re))
  5236. (cond
  5237. ((not (member-ignore-case (match-string 1)
  5238. org-element-parsed-keywords))
  5239. (throw 'objects-forbidden element))
  5240. ((< (match-end 0) pos)
  5241. (narrow-to-region (match-end 0) (line-end-position)))
  5242. ((and (match-beginning 2)
  5243. (>= pos (match-beginning 2))
  5244. (< pos (match-end 2)))
  5245. (narrow-to-region (match-beginning 2) (match-end 2)))
  5246. (t (throw 'objects-forbidden element)))
  5247. ;; Also change type to retrieve correct restrictions.
  5248. (setq type 'keyword))
  5249. ;; At an item, objects can only be located within tag, if any.
  5250. ((eq type 'item)
  5251. (let ((tag (org-element-property :tag element)))
  5252. (if (or (not tag) (/= (line-beginning-position) post))
  5253. (throw 'objects-forbidden element)
  5254. (beginning-of-line)
  5255. (search-forward tag (line-end-position))
  5256. (goto-char (match-beginning 0))
  5257. (if (and (>= pos (point)) (< pos (match-end 0)))
  5258. (narrow-to-region (point) (match-end 0))
  5259. (throw 'objects-forbidden element)))))
  5260. ;; At an headline or inlinetask, objects are in title.
  5261. ((memq type '(headline inlinetask))
  5262. (let ((case-fold-search nil))
  5263. (goto-char (org-element-property :begin element))
  5264. (looking-at org-complex-heading-regexp)
  5265. (let ((end (match-end 4)))
  5266. (if (not end) (throw 'objects-forbidden element)
  5267. (goto-char (match-beginning 4))
  5268. (when (looking-at org-comment-string)
  5269. (goto-char (match-end 0)))
  5270. (if (>= (point) end) (throw 'objects-forbidden element)
  5271. (narrow-to-region (point) end))))))
  5272. ;; At a paragraph, a table-row or a verse block, objects are
  5273. ;; located within their contents.
  5274. ((memq type '(paragraph table-row verse-block))
  5275. (let ((cbeg (org-element-property :contents-begin element))
  5276. (cend (org-element-property :contents-end element)))
  5277. ;; CBEG is nil for table rules.
  5278. (if (and cbeg cend (>= pos cbeg)
  5279. (or (< pos cend) (and (= pos cend) (eobp))))
  5280. (narrow-to-region cbeg cend)
  5281. (throw 'objects-forbidden element))))
  5282. (t (throw 'objects-forbidden element)))
  5283. (goto-char (point-min))
  5284. (let ((restriction (org-element-restriction type))
  5285. (parent element)
  5286. last)
  5287. (catch 'exit
  5288. (while t
  5289. (let ((next (org-element--object-lex restriction)))
  5290. (when next (org-element-put-property next :parent parent))
  5291. ;; Process NEXT, if any, in order to know if we need to
  5292. ;; skip it, return it or move into it.
  5293. (if (or (not next) (> (org-element-property :begin next) pos))
  5294. (throw 'exit (or last parent))
  5295. (let ((end (org-element-property :end next))
  5296. (cbeg (org-element-property :contents-begin next))
  5297. (cend (org-element-property :contents-end next)))
  5298. (cond
  5299. ;; Skip objects ending before point. Also skip
  5300. ;; objects ending at point unless it is also the
  5301. ;; end of buffer, since we want to return the
  5302. ;; innermost object.
  5303. ((and (<= end pos) (/= (point-max) end))
  5304. (goto-char end)
  5305. ;; For convenience, when object ends at POS,
  5306. ;; without any space, store it in LAST, as we
  5307. ;; will return it if no object starts here.
  5308. (when (and (= end pos)
  5309. (not (memq (char-before) '(?\s ?\t))))
  5310. (setq last next)))
  5311. ;; If POS is within a container object, move into
  5312. ;; that object.
  5313. ((and cbeg cend
  5314. (>= pos cbeg)
  5315. (or (< pos cend)
  5316. ;; At contents' end, if there is no
  5317. ;; space before point, also move into
  5318. ;; object, for consistency with
  5319. ;; convenience feature above.
  5320. (and (= pos cend)
  5321. (or (= (point-max) pos)
  5322. (not (memq (char-before pos)
  5323. '(?\s ?\t)))))))
  5324. (goto-char cbeg)
  5325. (narrow-to-region (point) cend)
  5326. (setq parent next)
  5327. (setq restriction (org-element-restriction next)))
  5328. ;; Otherwise, return NEXT.
  5329. (t (throw 'exit next)))))))))))))
  5330. (defun org-element-lineage (datum &optional types with-self)
  5331. "List all ancestors of a given element or object.
  5332. DATUM is an object or element.
  5333. Return ancestors from the closest to the farthest. When optional
  5334. argument TYPES is a list of symbols, return the first element or
  5335. object in the lineage whose type belongs to that list instead.
  5336. When optional argument WITH-SELF is non-nil, lineage includes
  5337. DATUM itself as the first element, and TYPES, if provided, also
  5338. apply to it.
  5339. When DATUM is obtained through `org-element-context' or
  5340. `org-element-at-point', only ancestors from its section can be
  5341. found. There is no such limitation when DATUM belongs to a full
  5342. parse tree."
  5343. (let ((up (if with-self datum (org-element-property :parent datum)))
  5344. ancestors)
  5345. (while (and up (not (memq (org-element-type up) types)))
  5346. (unless types (push up ancestors))
  5347. (setq up (org-element-property :parent up)))
  5348. (if types up (nreverse ancestors))))
  5349. (defun org-element-nested-p (elem-A elem-B)
  5350. "Non-nil when elements ELEM-A and ELEM-B are nested."
  5351. (let ((beg-A (org-element-property :begin elem-A))
  5352. (beg-B (org-element-property :begin elem-B))
  5353. (end-A (org-element-property :end elem-A))
  5354. (end-B (org-element-property :end elem-B)))
  5355. (or (and (>= beg-A beg-B) (<= end-A end-B))
  5356. (and (>= beg-B beg-A) (<= end-B end-A)))))
  5357. (defun org-element-swap-A-B (elem-A elem-B)
  5358. "Swap elements ELEM-A and ELEM-B.
  5359. Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
  5360. end of ELEM-A."
  5361. (goto-char (org-element-property :begin elem-A))
  5362. ;; There are two special cases when an element doesn't start at bol:
  5363. ;; the first paragraph in an item or in a footnote definition.
  5364. (let ((specialp (not (bolp))))
  5365. ;; Only a paragraph without any affiliated keyword can be moved at
  5366. ;; ELEM-A position in such a situation. Note that the case of
  5367. ;; a footnote definition is impossible: it cannot contain two
  5368. ;; paragraphs in a row because it cannot contain a blank line.
  5369. (when (and specialp
  5370. (or (not (eq (org-element-type elem-B) 'paragraph))
  5371. (/= (org-element-property :begin elem-B)
  5372. (org-element-property :contents-begin elem-B))))
  5373. (error "Cannot swap elements"))
  5374. ;; In a special situation, ELEM-A will have no indentation. We'll
  5375. ;; give it ELEM-B's (which will in, in turn, have no indentation).
  5376. (let* ((ind-B (when specialp
  5377. (goto-char (org-element-property :begin elem-B))
  5378. (current-indentation)))
  5379. (beg-A (org-element-property :begin elem-A))
  5380. (end-A (save-excursion
  5381. (goto-char (org-element-property :end elem-A))
  5382. (skip-chars-backward " \r\t\n")
  5383. (point-at-eol)))
  5384. (beg-B (org-element-property :begin elem-B))
  5385. (end-B (save-excursion
  5386. (goto-char (org-element-property :end elem-B))
  5387. (skip-chars-backward " \r\t\n")
  5388. (point-at-eol)))
  5389. ;; Store inner overlays responsible for visibility status.
  5390. ;; We also need to store their boundaries as they will be
  5391. ;; removed from buffer.
  5392. (overlays
  5393. (cons
  5394. (delq nil
  5395. (mapcar (lambda (o)
  5396. (and (>= (overlay-start o) beg-A)
  5397. (<= (overlay-end o) end-A)
  5398. (list o (overlay-start o) (overlay-end o))))
  5399. (overlays-in beg-A end-A)))
  5400. (delq nil
  5401. (mapcar (lambda (o)
  5402. (and (>= (overlay-start o) beg-B)
  5403. (<= (overlay-end o) end-B)
  5404. (list o (overlay-start o) (overlay-end o))))
  5405. (overlays-in beg-B end-B)))))
  5406. ;; Get contents.
  5407. (body-A (buffer-substring beg-A end-A))
  5408. (body-B (delete-and-extract-region beg-B end-B)))
  5409. (goto-char beg-B)
  5410. (when specialp
  5411. (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
  5412. (indent-to-column ind-B))
  5413. (insert body-A)
  5414. ;; Restore ex ELEM-A overlays.
  5415. (let ((offset (- beg-B beg-A)))
  5416. (dolist (o (car overlays))
  5417. (move-overlay (car o) (+ (nth 1 o) offset) (+ (nth 2 o) offset)))
  5418. (goto-char beg-A)
  5419. (delete-region beg-A end-A)
  5420. (insert body-B)
  5421. ;; Restore ex ELEM-B overlays.
  5422. (dolist (o (cdr overlays))
  5423. (move-overlay (car o) (- (nth 1 o) offset) (- (nth 2 o) offset))))
  5424. (goto-char (org-element-property :end elem-B)))))
  5425. (provide 'org-element)
  5426. ;; Local variables:
  5427. ;; generated-autoload-file: "org-loaddefs.el"
  5428. ;; End:
  5429. ;;; org-element.el ends here