org-element.el 149 KB

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