org-element.el 193 KB

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