org-element.el 154 KB

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