org-element.el 137 KB

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