org-element.el 148 KB

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