org-element.el 137 KB

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