org-element.el 140 KB

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