org-element.el 149 KB

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