org-element.el 146 KB

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