org-element.el 138 KB

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