org-element.el 142 KB

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