org-element.el 174 KB

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