org-element.el 132 KB

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