org-element.el 149 KB

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