org-element.el 149 KB

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