org-element.el 147 KB

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