org-element.el 135 KB

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