org-element.el 142 KB

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