org-element.el 143 KB

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