org-element.el 150 KB

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