org-element.el 148 KB

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