org-element.el 148 KB

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