org-element.el 135 KB

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