org-element.el 151 KB

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