org-element.el 130 KB

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