org-element.el 150 KB

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