org-element.el 147 KB

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