org-element.el 198 KB

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