org-element.el 197 KB

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