org-element.el 212 KB

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