org-element.el 154 KB

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