org-element.el 155 KB

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