org-element.el 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323
  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
  1528. ;; Skip non-existent or incomplete drawer.
  1529. ((save-excursion
  1530. (beginning-of-line)
  1531. (and (looking-at "[ \t]*:\\S-")
  1532. (or (not (looking-at org-drawer-regexp))
  1533. (not (save-excursion
  1534. (re-search-forward
  1535. "^[ \t]*:END:" limit t)))))))
  1536. ;; Stop at comments.
  1537. ((save-excursion
  1538. (beginning-of-line)
  1539. (not (looking-at "[ \t]*#\\S-"))) nil)
  1540. ;; Skip incomplete dynamic blocks.
  1541. ((save-excursion
  1542. (beginning-of-line)
  1543. (looking-at "[ \t]*#\\+BEGIN: "))
  1544. (not (save-excursion
  1545. (re-search-forward
  1546. "^[ \t]*\\+END:" limit t))))
  1547. ;; Skip incomplete blocks.
  1548. ((save-excursion
  1549. (beginning-of-line)
  1550. (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)"))
  1551. (not (save-excursion
  1552. (re-search-forward
  1553. (concat "^[ \t]*#\\+END_"
  1554. (match-string 1))
  1555. limit t))))
  1556. ;; Skip ill-formed keywords.
  1557. ((not (save-excursion
  1558. (beginning-of-line)
  1559. (looking-at "[ \t]*#\\+\\S-+:"))))))
  1560. (re-search-forward org-element-paragraph-separate limit 'm))
  1561. (if (eobp) (point) (goto-char (line-beginning-position)))))
  1562. (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
  1563. (forward-line)
  1564. (point)))
  1565. (end (progn (skip-chars-forward " \r\t\n" limit)
  1566. (if (eobp) (point) (point-at-bol)))))
  1567. (list 'paragraph
  1568. ;; If paragraph has no affiliated keywords, it may not begin
  1569. ;; at beginning of line if it starts an item.
  1570. (nconc
  1571. (list :begin (if (cadr keywords) begin contents-begin)
  1572. :end end
  1573. :contents-begin contents-begin
  1574. :contents-end contents-end
  1575. :post-blank (count-lines before-blank end))
  1576. (cadr keywords))))))
  1577. (defun org-element-paragraph-interpreter (paragraph contents)
  1578. "Interpret PARAGRAPH element as Org syntax.
  1579. CONTENTS is the contents of the element."
  1580. contents)
  1581. ;;;; Planning
  1582. (defun org-element-planning-parser (limit)
  1583. "Parse a planning.
  1584. LIMIT bounds the search.
  1585. Return a list whose CAR is `planning' and CDR is a plist
  1586. containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
  1587. and `:post-blank' keywords."
  1588. (save-excursion
  1589. (let* ((case-fold-search nil)
  1590. (begin (point))
  1591. (post-blank (let ((before-blank (progn (forward-line) (point))))
  1592. (skip-chars-forward " \r\t\n" limit)
  1593. (unless (eobp) (beginning-of-line))
  1594. (count-lines before-blank (point))))
  1595. (end (point))
  1596. closed deadline scheduled)
  1597. (goto-char begin)
  1598. (while (re-search-forward org-keyword-time-not-clock-regexp
  1599. (line-end-position) t)
  1600. (goto-char (match-end 1))
  1601. (org-skip-whitespace)
  1602. (let ((time (buffer-substring-no-properties
  1603. (1+ (point)) (1- (match-end 0))))
  1604. (keyword (match-string 1)))
  1605. (cond ((equal keyword org-closed-string) (setq closed time))
  1606. ((equal keyword org-deadline-string) (setq deadline time))
  1607. (t (setq scheduled time)))))
  1608. (list 'planning
  1609. (list :closed closed
  1610. :deadline deadline
  1611. :scheduled scheduled
  1612. :begin begin
  1613. :end end
  1614. :post-blank post-blank)))))
  1615. (defun org-element-planning-interpreter (planning contents)
  1616. "Interpret PLANNING element as Org syntax.
  1617. CONTENTS is nil."
  1618. (mapconcat
  1619. 'identity
  1620. (delq nil
  1621. (list (let ((closed (org-element-property :closed planning)))
  1622. (when closed (concat org-closed-string " [" closed "]")))
  1623. (let ((deadline (org-element-property :deadline planning)))
  1624. (when deadline (concat org-deadline-string " <" deadline ">")))
  1625. (let ((scheduled (org-element-property :scheduled planning)))
  1626. (when scheduled
  1627. (concat org-scheduled-string " <" scheduled ">")))))
  1628. " "))
  1629. ;;;; Property Drawer
  1630. (defun org-element-property-drawer-parser (limit)
  1631. "Parse a property drawer.
  1632. LIMIT bounds the search.
  1633. Return a list whose CAR is `property-drawer' and CDR is a plist
  1634. containing `:begin', `:end', `:hiddenp', `:contents-begin',
  1635. `:contents-end', `:properties' and `:post-blank' keywords.
  1636. Assume point is at the beginning of the property drawer."
  1637. (save-excursion
  1638. (let ((case-fold-search t)
  1639. (begin (point))
  1640. (prop-begin (progn (forward-line) (point)))
  1641. (hidden (org-invisible-p2))
  1642. (properties
  1643. (let (val)
  1644. (while (not (looking-at "^[ \t]*:END:"))
  1645. (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
  1646. (push (cons (org-match-string-no-properties 1)
  1647. (org-trim
  1648. (buffer-substring-no-properties
  1649. (match-end 0) (point-at-eol))))
  1650. val))
  1651. (forward-line))
  1652. val))
  1653. (prop-end (progn (re-search-forward "^[ \t]*:END:" limit t)
  1654. (point-at-bol)))
  1655. (pos-before-blank (progn (forward-line) (point)))
  1656. (end (progn (skip-chars-forward " \r\t\n" limit)
  1657. (if (eobp) (point) (point-at-bol)))))
  1658. (list 'property-drawer
  1659. (list :begin begin
  1660. :end end
  1661. :hiddenp hidden
  1662. :properties properties
  1663. :post-blank (count-lines pos-before-blank end))))))
  1664. (defun org-element-property-drawer-interpreter (property-drawer contents)
  1665. "Interpret PROPERTY-DRAWER element as Org syntax.
  1666. CONTENTS is nil."
  1667. (let ((props (org-element-property :properties property-drawer)))
  1668. (concat
  1669. ":PROPERTIES:\n"
  1670. (mapconcat (lambda (p)
  1671. (format org-property-format (format ":%s:" (car p)) (cdr p)))
  1672. (nreverse props) "\n")
  1673. "\n:END:")))
  1674. ;;;; Quote Section
  1675. (defun org-element-quote-section-parser (limit)
  1676. "Parse a quote section.
  1677. LIMIT bounds the search.
  1678. Return a list whose CAR is `quote-section' and CDR is a plist
  1679. containing `:begin', `:end', `:value' and `:post-blank' keywords.
  1680. Assume point is at beginning of the section."
  1681. (save-excursion
  1682. (let* ((begin (point))
  1683. (end (progn (org-with-limited-levels (outline-next-heading))
  1684. (point)))
  1685. (pos-before-blank (progn (skip-chars-backward " \r\t\n")
  1686. (forward-line)
  1687. (point)))
  1688. (value (buffer-substring-no-properties begin pos-before-blank)))
  1689. (list 'quote-section
  1690. (list :begin begin
  1691. :end end
  1692. :value value
  1693. :post-blank (count-lines pos-before-blank end))))))
  1694. (defun org-element-quote-section-interpreter (quote-section contents)
  1695. "Interpret QUOTE-SECTION element as Org syntax.
  1696. CONTENTS is nil."
  1697. (org-element-property :value quote-section))
  1698. ;;;; Src Block
  1699. (defun org-element-src-block-parser (limit)
  1700. "Parse a src block.
  1701. LIMIT bounds the search.
  1702. Return a list whose CAR is `src-block' and CDR is a plist
  1703. containing `:language', `:switches', `:parameters', `:begin',
  1704. `:end', `:hiddenp', `:number-lines', `:retain-labels',
  1705. `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
  1706. `:post-blank' keywords.
  1707. Assume point is at the beginning of the block."
  1708. (let ((case-fold-search t))
  1709. (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC" limit t)))
  1710. ;; Incomplete block: parse it as a comment.
  1711. (org-element-paragraph-parser limit)
  1712. (let ((contents-end (match-beginning 0)))
  1713. (save-excursion
  1714. (let* ((keywords (org-element--collect-affiliated-keywords))
  1715. ;; Get beginning position.
  1716. (begin (car keywords))
  1717. ;; Get language as a string.
  1718. (language
  1719. (progn
  1720. (looking-at
  1721. (concat "^[ \t]*#\\+BEGIN_SRC"
  1722. "\\(?: +\\(\\S-+\\)\\)?"
  1723. "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
  1724. "\\(.*\\)[ \t]*$"))
  1725. (org-match-string-no-properties 1)))
  1726. ;; Get switches.
  1727. (switches (org-match-string-no-properties 2))
  1728. ;; Get parameters.
  1729. (parameters (org-match-string-no-properties 3))
  1730. ;; Switches analysis
  1731. (number-lines (cond ((not switches) nil)
  1732. ((string-match "-n\\>" switches) 'new)
  1733. ((string-match "+n\\>" switches) 'continued)))
  1734. (preserve-indent (and switches (string-match "-i\\>" switches)))
  1735. (label-fmt (and switches
  1736. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  1737. (match-string 1 switches)))
  1738. ;; Should labels be retained in (or stripped from)
  1739. ;; src blocks?
  1740. (retain-labels
  1741. (or (not switches)
  1742. (not (string-match "-r\\>" switches))
  1743. (and number-lines (string-match "-k\\>" switches))))
  1744. ;; What should code-references use - labels or
  1745. ;; line-numbers?
  1746. (use-labels
  1747. (or (not switches)
  1748. (and retain-labels (not (string-match "-k\\>" switches)))))
  1749. ;; Get visibility status.
  1750. (hidden (progn (forward-line) (org-invisible-p2)))
  1751. ;; Retrieve code.
  1752. (value (buffer-substring-no-properties (point) contents-end))
  1753. (pos-before-blank (progn (goto-char contents-end)
  1754. (forward-line)
  1755. (point)))
  1756. ;; Get position after ending blank lines.
  1757. (end (progn (skip-chars-forward " \r\t\n" limit)
  1758. (if (eobp) (point) (point-at-bol)))))
  1759. (list 'src-block
  1760. (nconc
  1761. (list :language language
  1762. :switches (and (org-string-nw-p switches)
  1763. (org-trim switches))
  1764. :parameters (and (org-string-nw-p parameters)
  1765. (org-trim parameters))
  1766. :begin begin
  1767. :end end
  1768. :number-lines number-lines
  1769. :preserve-indent preserve-indent
  1770. :retain-labels retain-labels
  1771. :use-labels use-labels
  1772. :label-fmt label-fmt
  1773. :hiddenp hidden
  1774. :value value
  1775. :post-blank (count-lines pos-before-blank end))
  1776. (cadr keywords)))))))))
  1777. (defun org-element-src-block-interpreter (src-block contents)
  1778. "Interpret SRC-BLOCK element as Org syntax.
  1779. CONTENTS is nil."
  1780. (let ((lang (org-element-property :language src-block))
  1781. (switches (org-element-property :switches src-block))
  1782. (params (org-element-property :parameters src-block))
  1783. (value (let ((val (org-element-property :value src-block)))
  1784. (cond
  1785. (org-src-preserve-indentation val)
  1786. ((zerop org-edit-src-content-indentation)
  1787. (org-remove-indentation val))
  1788. (t
  1789. (let ((ind (make-string
  1790. org-edit-src-content-indentation 32)))
  1791. (replace-regexp-in-string
  1792. "\\(^\\)[ \t]*\\S-" ind
  1793. (org-remove-indentation val) nil nil 1)))))))
  1794. (concat (format "#+BEGIN_SRC%s\n"
  1795. (concat (and lang (concat " " lang))
  1796. (and switches (concat " " switches))
  1797. (and params (concat " " params))))
  1798. value
  1799. "#+END_SRC")))
  1800. ;;;; Table
  1801. (defun org-element-table-parser (limit)
  1802. "Parse a table at point.
  1803. LIMIT bounds the search.
  1804. Return a list whose CAR is `table' and CDR is a plist containing
  1805. `:begin', `:end', `:tblfm', `:type', `:contents-begin',
  1806. `:contents-end', `:value' and `:post-blank' keywords.
  1807. Assume point is at the beginning of the table."
  1808. (save-excursion
  1809. (let* ((case-fold-search t)
  1810. (table-begin (point))
  1811. (type (if (org-at-table.el-p) 'table.el 'org))
  1812. (keywords (org-element--collect-affiliated-keywords))
  1813. (begin (car keywords))
  1814. (table-end (goto-char (marker-position (org-table-end t))))
  1815. (tblfm (let (acc)
  1816. (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
  1817. (push (org-match-string-no-properties 1) acc)
  1818. (forward-line))
  1819. acc))
  1820. (pos-before-blank (point))
  1821. (end (progn (skip-chars-forward " \r\t\n" limit)
  1822. (if (eobp) (point) (point-at-bol)))))
  1823. (list 'table
  1824. (nconc
  1825. (list :begin begin
  1826. :end end
  1827. :type type
  1828. :tblfm tblfm
  1829. ;; Only `org' tables have contents. `table.el' tables
  1830. ;; use a `:value' property to store raw table as
  1831. ;; a string.
  1832. :contents-begin (and (eq type 'org) table-begin)
  1833. :contents-end (and (eq type 'org) table-end)
  1834. :value (and (eq type 'table.el)
  1835. (buffer-substring-no-properties
  1836. table-begin table-end))
  1837. :post-blank (count-lines pos-before-blank end))
  1838. (cadr keywords))))))
  1839. (defun org-element-table-interpreter (table contents)
  1840. "Interpret TABLE element as Org syntax.
  1841. CONTENTS is nil."
  1842. (if (eq (org-element-property :type table) 'table.el)
  1843. (org-remove-indentation (org-element-property :value table))
  1844. (concat (with-temp-buffer (insert contents)
  1845. (org-table-align)
  1846. (buffer-string))
  1847. (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
  1848. (reverse (org-element-property :tblfm table))
  1849. "\n"))))
  1850. ;;;; Table Row
  1851. (defun org-element-table-row-parser (limit)
  1852. "Parse table row at point.
  1853. LIMIT bounds the search.
  1854. Return a list whose CAR is `table-row' and CDR is a plist
  1855. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1856. `:type' and `:post-blank' keywords."
  1857. (save-excursion
  1858. (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
  1859. (begin (point))
  1860. ;; A table rule has no contents. In that case, ensure
  1861. ;; CONTENTS-BEGIN matches CONTENTS-END.
  1862. (contents-begin (and (eq type 'standard)
  1863. (search-forward "|")
  1864. (point)))
  1865. (contents-end (and (eq type 'standard)
  1866. (progn
  1867. (end-of-line)
  1868. (skip-chars-backward " \t")
  1869. (point))))
  1870. (end (progn (forward-line) (point))))
  1871. (list 'table-row
  1872. (list :type type
  1873. :begin begin
  1874. :end end
  1875. :contents-begin contents-begin
  1876. :contents-end contents-end
  1877. :post-blank 0)))))
  1878. (defun org-element-table-row-interpreter (table-row contents)
  1879. "Interpret TABLE-ROW element as Org syntax.
  1880. CONTENTS is the contents of the table row."
  1881. (if (eq (org-element-property :type table-row) 'rule) "|-"
  1882. (concat "| " contents)))
  1883. ;;;; Verse Block
  1884. (defun org-element-verse-block-parser (limit)
  1885. "Parse a verse block.
  1886. LIMIT bounds the search.
  1887. Return a list whose CAR is `verse-block' and CDR is a plist
  1888. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1889. `:hiddenp' and `:post-blank' keywords.
  1890. Assume point is at beginning of the block."
  1891. (let ((case-fold-search t))
  1892. (if (not (save-excursion
  1893. (re-search-forward "^[ \t]*#\\+END_VERSE" limit t)))
  1894. ;; Incomplete block: parse it as a comment.
  1895. (org-element-paragraph-parser limit)
  1896. (let ((contents-end (match-beginning 0)))
  1897. (save-excursion
  1898. (let* ((keywords (org-element--collect-affiliated-keywords))
  1899. (begin (car keywords))
  1900. (hidden (progn (forward-line) (org-invisible-p2)))
  1901. (contents-begin (point))
  1902. (pos-before-blank (progn (goto-char contents-end)
  1903. (forward-line)
  1904. (point)))
  1905. (end (progn (skip-chars-forward " \r\t\n" limit)
  1906. (if (eobp) (point) (point-at-bol)))))
  1907. (list 'verse-block
  1908. (nconc
  1909. (list :begin begin
  1910. :end end
  1911. :contents-begin contents-begin
  1912. :contents-end contents-end
  1913. :hiddenp hidden
  1914. :post-blank (count-lines pos-before-blank end))
  1915. (cadr keywords)))))))))
  1916. (defun org-element-verse-block-interpreter (verse-block contents)
  1917. "Interpret VERSE-BLOCK element as Org syntax.
  1918. CONTENTS is verse block contents."
  1919. (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
  1920. ;;; Objects
  1921. ;;
  1922. ;; Unlike to elements, interstices can be found between objects.
  1923. ;; That's why, along with the parser, successor functions are provided
  1924. ;; for each object. Some objects share the same successor (i.e. `code'
  1925. ;; and `verbatim' objects).
  1926. ;;
  1927. ;; A successor must accept a single argument bounding the search. It
  1928. ;; will return either a cons cell whose CAR is the object's type, as
  1929. ;; a symbol, and CDR the position of its next occurrence, or nil.
  1930. ;;
  1931. ;; Successors follow the naming convention:
  1932. ;; org-element-NAME-successor, where NAME is the name of the
  1933. ;; successor, as defined in `org-element-all-successors'.
  1934. ;;
  1935. ;; Some object types (i.e. `italic') are recursive. Restrictions on
  1936. ;; object types they can contain will be specified in
  1937. ;; `org-element-object-restrictions'.
  1938. ;;
  1939. ;; Adding a new type of object is simple. Implement a successor,
  1940. ;; a parser, and an interpreter for it, all following the naming
  1941. ;; convention. Register type in `org-element-all-objects' and
  1942. ;; successor in `org-element-all-successors'. Maybe tweak
  1943. ;; restrictions about it, and that's it.
  1944. ;;;; Bold
  1945. (defun org-element-bold-parser ()
  1946. "Parse bold object at point.
  1947. Return a list whose CAR is `bold' and CDR is a plist with
  1948. `:begin', `:end', `:contents-begin' and `:contents-end' and
  1949. `:post-blank' keywords.
  1950. Assume point is at the first star marker."
  1951. (save-excursion
  1952. (unless (bolp) (backward-char 1))
  1953. (looking-at org-emph-re)
  1954. (let ((begin (match-beginning 2))
  1955. (contents-begin (match-beginning 4))
  1956. (contents-end (match-end 4))
  1957. (post-blank (progn (goto-char (match-end 2))
  1958. (skip-chars-forward " \t")))
  1959. (end (point)))
  1960. (list 'bold
  1961. (list :begin begin
  1962. :end end
  1963. :contents-begin contents-begin
  1964. :contents-end contents-end
  1965. :post-blank post-blank)))))
  1966. (defun org-element-bold-interpreter (bold contents)
  1967. "Interpret BOLD object as Org syntax.
  1968. CONTENTS is the contents of the object."
  1969. (format "*%s*" contents))
  1970. (defun org-element-text-markup-successor (limit)
  1971. "Search for the next text-markup object.
  1972. LIMIT bounds the search.
  1973. Return value is a cons cell whose CAR is a symbol among `bold',
  1974. `italic', `underline', `strike-through', `code' and `verbatim'
  1975. and CDR is beginning position."
  1976. (save-excursion
  1977. (unless (bolp) (backward-char))
  1978. (when (re-search-forward org-emph-re limit t)
  1979. (let ((marker (match-string 3)))
  1980. (cons (cond
  1981. ((equal marker "*") 'bold)
  1982. ((equal marker "/") 'italic)
  1983. ((equal marker "_") 'underline)
  1984. ((equal marker "+") 'strike-through)
  1985. ((equal marker "~") 'code)
  1986. ((equal marker "=") 'verbatim)
  1987. (t (error "Unknown marker at %d" (match-beginning 3))))
  1988. (match-beginning 2))))))
  1989. ;;;; Code
  1990. (defun org-element-code-parser ()
  1991. "Parse code object at point.
  1992. Return a list whose CAR is `code' and CDR is a plist with
  1993. `:value', `:begin', `:end' and `:post-blank' keywords.
  1994. Assume point is at the first tilde marker."
  1995. (save-excursion
  1996. (unless (bolp) (backward-char 1))
  1997. (looking-at org-emph-re)
  1998. (let ((begin (match-beginning 2))
  1999. (value (org-match-string-no-properties 4))
  2000. (post-blank (progn (goto-char (match-end 2))
  2001. (skip-chars-forward " \t")))
  2002. (end (point)))
  2003. (list 'code
  2004. (list :value value
  2005. :begin begin
  2006. :end end
  2007. :post-blank post-blank)))))
  2008. (defun org-element-code-interpreter (code contents)
  2009. "Interpret CODE object as Org syntax.
  2010. CONTENTS is nil."
  2011. (format "~%s~" (org-element-property :value code)))
  2012. ;;;; Entity
  2013. (defun org-element-entity-parser ()
  2014. "Parse entity at point.
  2015. Return a list whose CAR is `entity' and CDR a plist with
  2016. `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
  2017. `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
  2018. keywords.
  2019. Assume point is at the beginning of the entity."
  2020. (save-excursion
  2021. (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
  2022. (let* ((value (org-entity-get (match-string 1)))
  2023. (begin (match-beginning 0))
  2024. (bracketsp (string= (match-string 2) "{}"))
  2025. (post-blank (progn (goto-char (match-end 1))
  2026. (when bracketsp (forward-char 2))
  2027. (skip-chars-forward " \t")))
  2028. (end (point)))
  2029. (list 'entity
  2030. (list :name (car value)
  2031. :latex (nth 1 value)
  2032. :latex-math-p (nth 2 value)
  2033. :html (nth 3 value)
  2034. :ascii (nth 4 value)
  2035. :latin1 (nth 5 value)
  2036. :utf-8 (nth 6 value)
  2037. :begin begin
  2038. :end end
  2039. :use-brackets-p bracketsp
  2040. :post-blank post-blank)))))
  2041. (defun org-element-entity-interpreter (entity contents)
  2042. "Interpret ENTITY object as Org syntax.
  2043. CONTENTS is nil."
  2044. (concat "\\"
  2045. (org-element-property :name entity)
  2046. (when (org-element-property :use-brackets-p entity) "{}")))
  2047. (defun org-element-latex-or-entity-successor (limit)
  2048. "Search for the next latex-fragment or entity object.
  2049. LIMIT bounds the search.
  2050. Return value is a cons cell whose CAR is `entity' or
  2051. `latex-fragment' and CDR is beginning position."
  2052. (save-excursion
  2053. (let ((matchers (plist-get org-format-latex-options :matchers))
  2054. ;; ENTITY-RE matches both LaTeX commands and Org entities.
  2055. (entity-re
  2056. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
  2057. (when (re-search-forward
  2058. (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
  2059. matchers "\\|")
  2060. "\\|" entity-re)
  2061. limit t)
  2062. (goto-char (match-beginning 0))
  2063. (if (looking-at entity-re)
  2064. ;; Determine if it's a real entity or a LaTeX command.
  2065. (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
  2066. (match-beginning 0))
  2067. ;; No entity nor command: point is at a LaTeX fragment.
  2068. ;; Determine its type to get the correct beginning position.
  2069. (cons 'latex-fragment
  2070. (catch 'return
  2071. (mapc (lambda (e)
  2072. (when (looking-at (nth 1 (assoc e org-latex-regexps)))
  2073. (throw 'return
  2074. (match-beginning
  2075. (nth 2 (assoc e org-latex-regexps))))))
  2076. matchers)
  2077. (point))))))))
  2078. ;;;; Export Snippet
  2079. (defun org-element-export-snippet-parser ()
  2080. "Parse export snippet at point.
  2081. Return a list whose CAR is `export-snippet' and CDR a plist with
  2082. `:begin', `:end', `:back-end', `:value' and `:post-blank' as
  2083. keywords.
  2084. Assume point is at the beginning of the snippet."
  2085. (save-excursion
  2086. (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
  2087. (let* ((begin (match-beginning 0))
  2088. (back-end (org-match-string-no-properties 1))
  2089. (value (buffer-substring-no-properties
  2090. (point)
  2091. (progn (re-search-forward "@@" nil t) (match-beginning 0))))
  2092. (post-blank (skip-chars-forward " \t"))
  2093. (end (point)))
  2094. (list 'export-snippet
  2095. (list :back-end back-end
  2096. :value value
  2097. :begin begin
  2098. :end end
  2099. :post-blank post-blank)))))
  2100. (defun org-element-export-snippet-interpreter (export-snippet contents)
  2101. "Interpret EXPORT-SNIPPET object as Org syntax.
  2102. CONTENTS is nil."
  2103. (format "@@%s:%s@@"
  2104. (org-element-property :back-end export-snippet)
  2105. (org-element-property :value export-snippet)))
  2106. (defun org-element-export-snippet-successor (limit)
  2107. "Search for the next export-snippet object.
  2108. LIMIT bounds the search.
  2109. Return value is a cons cell whose CAR is `export-snippet' and CDR
  2110. its beginning position."
  2111. (save-excursion
  2112. (let (beg)
  2113. (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
  2114. (setq beg (match-beginning 0))
  2115. (re-search-forward "@@" limit t))
  2116. (cons 'export-snippet beg)))))
  2117. ;;;; Footnote Reference
  2118. (defun org-element-footnote-reference-parser ()
  2119. "Parse footnote reference at point.
  2120. Return a list whose CAR is `footnote-reference' and CDR a plist
  2121. with `:label', `:type', `:inline-definition', `:begin', `:end'
  2122. and `:post-blank' as keywords."
  2123. (save-excursion
  2124. (looking-at org-footnote-re)
  2125. (let* ((begin (point))
  2126. (label (or (org-match-string-no-properties 2)
  2127. (org-match-string-no-properties 3)
  2128. (and (match-string 1)
  2129. (concat "fn:" (org-match-string-no-properties 1)))))
  2130. (type (if (or (not label) (match-string 1)) 'inline 'standard))
  2131. (inner-begin (match-end 0))
  2132. (inner-end
  2133. (let ((count 1))
  2134. (forward-char)
  2135. (while (and (> count 0) (re-search-forward "[][]" nil t))
  2136. (if (equal (match-string 0) "[") (incf count) (decf count)))
  2137. (1- (point))))
  2138. (post-blank (progn (goto-char (1+ inner-end))
  2139. (skip-chars-forward " \t")))
  2140. (end (point))
  2141. (footnote-reference
  2142. (list 'footnote-reference
  2143. (list :label label
  2144. :type type
  2145. :begin begin
  2146. :end end
  2147. :post-blank post-blank))))
  2148. (org-element-put-property
  2149. footnote-reference :inline-definition
  2150. (and (eq type 'inline)
  2151. (org-element-parse-secondary-string
  2152. (buffer-substring inner-begin inner-end)
  2153. (org-element-restriction 'footnote-reference)
  2154. footnote-reference))))))
  2155. (defun org-element-footnote-reference-interpreter (footnote-reference contents)
  2156. "Interpret FOOTNOTE-REFERENCE object as Org syntax.
  2157. CONTENTS is nil."
  2158. (let ((label (or (org-element-property :label footnote-reference) "fn:"))
  2159. (def
  2160. (let ((inline-def
  2161. (org-element-property :inline-definition footnote-reference)))
  2162. (if (not inline-def) ""
  2163. (concat ":" (org-element-interpret-data inline-def))))))
  2164. (format "[%s]" (concat label def))))
  2165. (defun org-element-footnote-reference-successor (limit)
  2166. "Search for the next footnote-reference object.
  2167. LIMIT bounds the search.
  2168. Return value is a cons cell whose CAR is `footnote-reference' and
  2169. CDR is beginning position."
  2170. (save-excursion
  2171. (catch 'exit
  2172. (while (re-search-forward org-footnote-re limit t)
  2173. (save-excursion
  2174. (let ((beg (match-beginning 0))
  2175. (count 1))
  2176. (backward-char)
  2177. (while (re-search-forward "[][]" limit t)
  2178. (if (equal (match-string 0) "[") (incf count) (decf count))
  2179. (when (zerop count)
  2180. (throw 'exit (cons 'footnote-reference beg))))))))))
  2181. ;;;; Inline Babel Call
  2182. (defun org-element-inline-babel-call-parser ()
  2183. "Parse inline babel call at point.
  2184. Return a list whose CAR is `inline-babel-call' and CDR a plist
  2185. with `:begin', `:end', `:info' and `:post-blank' as keywords.
  2186. Assume point is at the beginning of the babel call."
  2187. (save-excursion
  2188. (unless (bolp) (backward-char))
  2189. (looking-at org-babel-inline-lob-one-liner-regexp)
  2190. (let ((info (save-match-data (org-babel-lob-get-info)))
  2191. (begin (match-end 1))
  2192. (post-blank (progn (goto-char (match-end 0))
  2193. (skip-chars-forward " \t")))
  2194. (end (point)))
  2195. (list 'inline-babel-call
  2196. (list :begin begin
  2197. :end end
  2198. :info info
  2199. :post-blank post-blank)))))
  2200. (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
  2201. "Interpret INLINE-BABEL-CALL object as Org syntax.
  2202. CONTENTS is nil."
  2203. (let* ((babel-info (org-element-property :info inline-babel-call))
  2204. (main-source (car babel-info))
  2205. (post-options (nth 1 babel-info)))
  2206. (concat "call_"
  2207. (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
  2208. ;; Remove redundant square brackets.
  2209. (replace-match
  2210. (match-string 1 main-source) nil nil main-source)
  2211. main-source)
  2212. (and post-options (format "[%s]" post-options)))))
  2213. (defun org-element-inline-babel-call-successor (limit)
  2214. "Search for the next inline-babel-call object.
  2215. LIMIT bounds the search.
  2216. Return value is a cons cell whose CAR is `inline-babel-call' and
  2217. CDR is beginning position."
  2218. (save-excursion
  2219. ;; Use a simplified version of
  2220. ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
  2221. (when (re-search-forward
  2222. "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
  2223. limit t)
  2224. (cons 'inline-babel-call (match-beginning 0)))))
  2225. ;;;; Inline Src Block
  2226. (defun org-element-inline-src-block-parser ()
  2227. "Parse inline source block at point.
  2228. LIMIT bounds the search.
  2229. Return a list whose CAR is `inline-src-block' and CDR a plist
  2230. with `:begin', `:end', `:language', `:value', `:parameters' and
  2231. `:post-blank' as keywords.
  2232. Assume point is at the beginning of the inline src block."
  2233. (save-excursion
  2234. (unless (bolp) (backward-char))
  2235. (looking-at org-babel-inline-src-block-regexp)
  2236. (let ((begin (match-beginning 1))
  2237. (language (org-match-string-no-properties 2))
  2238. (parameters (org-match-string-no-properties 4))
  2239. (value (org-match-string-no-properties 5))
  2240. (post-blank (progn (goto-char (match-end 0))
  2241. (skip-chars-forward " \t")))
  2242. (end (point)))
  2243. (list 'inline-src-block
  2244. (list :language language
  2245. :value value
  2246. :parameters parameters
  2247. :begin begin
  2248. :end end
  2249. :post-blank post-blank)))))
  2250. (defun org-element-inline-src-block-interpreter (inline-src-block contents)
  2251. "Interpret INLINE-SRC-BLOCK object as Org syntax.
  2252. CONTENTS is nil."
  2253. (let ((language (org-element-property :language inline-src-block))
  2254. (arguments (org-element-property :parameters inline-src-block))
  2255. (body (org-element-property :value inline-src-block)))
  2256. (format "src_%s%s{%s}"
  2257. language
  2258. (if arguments (format "[%s]" arguments) "")
  2259. body)))
  2260. (defun org-element-inline-src-block-successor (limit)
  2261. "Search for the next inline-babel-call element.
  2262. LIMIT bounds the search.
  2263. Return value is a cons cell whose CAR is `inline-babel-call' and
  2264. CDR is beginning position."
  2265. (save-excursion
  2266. (when (re-search-forward org-babel-inline-src-block-regexp limit t)
  2267. (cons 'inline-src-block (match-beginning 1)))))
  2268. ;;;; Italic
  2269. (defun org-element-italic-parser ()
  2270. "Parse italic object at point.
  2271. Return a list whose CAR is `italic' and CDR is a plist with
  2272. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2273. `:post-blank' keywords.
  2274. Assume point is at the first slash marker."
  2275. (save-excursion
  2276. (unless (bolp) (backward-char 1))
  2277. (looking-at org-emph-re)
  2278. (let ((begin (match-beginning 2))
  2279. (contents-begin (match-beginning 4))
  2280. (contents-end (match-end 4))
  2281. (post-blank (progn (goto-char (match-end 2))
  2282. (skip-chars-forward " \t")))
  2283. (end (point)))
  2284. (list 'italic
  2285. (list :begin begin
  2286. :end end
  2287. :contents-begin contents-begin
  2288. :contents-end contents-end
  2289. :post-blank post-blank)))))
  2290. (defun org-element-italic-interpreter (italic contents)
  2291. "Interpret ITALIC object as Org syntax.
  2292. CONTENTS is the contents of the object."
  2293. (format "/%s/" contents))
  2294. ;;;; Latex Fragment
  2295. (defun org-element-latex-fragment-parser ()
  2296. "Parse latex fragment at point.
  2297. Return a list whose CAR is `latex-fragment' and CDR a plist with
  2298. `:value', `:begin', `:end', and `:post-blank' as keywords.
  2299. Assume point is at the beginning of the latex fragment."
  2300. (save-excursion
  2301. (let* ((begin (point))
  2302. (substring-match
  2303. (catch 'exit
  2304. (mapc (lambda (e)
  2305. (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
  2306. (when (or (looking-at latex-regexp)
  2307. (and (not (bobp))
  2308. (save-excursion
  2309. (backward-char)
  2310. (looking-at latex-regexp))))
  2311. (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
  2312. (plist-get org-format-latex-options :matchers))
  2313. ;; None found: it's a macro.
  2314. (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
  2315. 0))
  2316. (value (match-string-no-properties substring-match))
  2317. (post-blank (progn (goto-char (match-end substring-match))
  2318. (skip-chars-forward " \t")))
  2319. (end (point)))
  2320. (list 'latex-fragment
  2321. (list :value value
  2322. :begin begin
  2323. :end end
  2324. :post-blank post-blank)))))
  2325. (defun org-element-latex-fragment-interpreter (latex-fragment contents)
  2326. "Interpret LATEX-FRAGMENT object as Org syntax.
  2327. CONTENTS is nil."
  2328. (org-element-property :value latex-fragment))
  2329. ;;;; Line Break
  2330. (defun org-element-line-break-parser ()
  2331. "Parse line break at point.
  2332. Return a list whose CAR is `line-break', and CDR a plist with
  2333. `:begin', `:end' and `:post-blank' keywords.
  2334. Assume point is at the beginning of the line break."
  2335. (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
  2336. (defun org-element-line-break-interpreter (line-break contents)
  2337. "Interpret LINE-BREAK object as Org syntax.
  2338. CONTENTS is nil."
  2339. "\\\\")
  2340. (defun org-element-line-break-successor (limit)
  2341. "Search for the next line-break object.
  2342. LIMIT bounds the search.
  2343. Return value is a cons cell whose CAR is `line-break' and CDR is
  2344. beginning position."
  2345. (save-excursion
  2346. (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
  2347. (goto-char (match-beginning 1)))))
  2348. ;; A line break can only happen on a non-empty line.
  2349. (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
  2350. (cons 'line-break beg)))))
  2351. ;;;; Link
  2352. (defun org-element-link-parser ()
  2353. "Parse link at point.
  2354. Return a list whose CAR is `link' and CDR a plist with `:type',
  2355. `:path', `:raw-link', `:begin', `:end', `:contents-begin',
  2356. `:contents-end' and `:post-blank' as keywords.
  2357. Assume point is at the beginning of the link."
  2358. (save-excursion
  2359. (let ((begin (point))
  2360. end contents-begin contents-end link-end post-blank path type
  2361. raw-link link)
  2362. (cond
  2363. ;; Type 1: Text targeted from a radio target.
  2364. ((and org-target-link-regexp (looking-at org-target-link-regexp))
  2365. (setq type "radio"
  2366. link-end (match-end 0)
  2367. path (org-match-string-no-properties 0)))
  2368. ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
  2369. ((looking-at org-bracket-link-regexp)
  2370. (setq contents-begin (match-beginning 3)
  2371. contents-end (match-end 3)
  2372. link-end (match-end 0)
  2373. ;; RAW-LINK is the original link.
  2374. raw-link (org-match-string-no-properties 1)
  2375. link (org-translate-link
  2376. (org-link-expand-abbrev
  2377. (org-link-unescape raw-link))))
  2378. ;; Determine TYPE of link and set PATH accordingly.
  2379. (cond
  2380. ;; File type.
  2381. ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
  2382. (setq type "file" path link))
  2383. ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
  2384. ((string-match org-link-re-with-space3 link)
  2385. (setq type (match-string 1 link) path (match-string 2 link)))
  2386. ;; Id type: PATH is the id.
  2387. ((string-match "^id:\\([-a-f0-9]+\\)" link)
  2388. (setq type "id" path (match-string 1 link)))
  2389. ;; Code-ref type: PATH is the name of the reference.
  2390. ((string-match "^(\\(.*\\))$" link)
  2391. (setq type "coderef" path (match-string 1 link)))
  2392. ;; Custom-id type: PATH is the name of the custom id.
  2393. ((= (aref link 0) ?#)
  2394. (setq type "custom-id" path (substring link 1)))
  2395. ;; Fuzzy type: Internal link either matches a target, an
  2396. ;; headline name or nothing. PATH is the target or
  2397. ;; headline's name.
  2398. (t (setq type "fuzzy" path link))))
  2399. ;; Type 3: Plain link, i.e. http://orgmode.org
  2400. ((looking-at org-plain-link-re)
  2401. (setq raw-link (org-match-string-no-properties 0)
  2402. type (org-match-string-no-properties 1)
  2403. path (org-match-string-no-properties 2)
  2404. link-end (match-end 0)))
  2405. ;; Type 4: Angular link, i.e. <http://orgmode.org>
  2406. ((looking-at org-angle-link-re)
  2407. (setq raw-link (buffer-substring-no-properties
  2408. (match-beginning 1) (match-end 2))
  2409. type (org-match-string-no-properties 1)
  2410. path (org-match-string-no-properties 2)
  2411. link-end (match-end 0))))
  2412. ;; In any case, deduce end point after trailing white space from
  2413. ;; LINK-END variable.
  2414. (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
  2415. end (point))
  2416. (list 'link
  2417. (list :type type
  2418. :path path
  2419. :raw-link (or raw-link path)
  2420. :begin begin
  2421. :end end
  2422. :contents-begin contents-begin
  2423. :contents-end contents-end
  2424. :post-blank post-blank)))))
  2425. (defun org-element-link-interpreter (link contents)
  2426. "Interpret LINK object as Org syntax.
  2427. CONTENTS is the contents of the object, or nil."
  2428. (let ((type (org-element-property :type link))
  2429. (raw-link (org-element-property :raw-link link)))
  2430. (if (string= type "radio") raw-link
  2431. (format "[[%s]%s]"
  2432. raw-link
  2433. (if contents (format "[%s]" contents) "")))))
  2434. (defun org-element-link-successor (limit)
  2435. "Search for the next link object.
  2436. LIMIT bounds the search.
  2437. Return value is a cons cell whose CAR is `link' and CDR is
  2438. beginning position."
  2439. (save-excursion
  2440. (let ((link-regexp
  2441. (if (not org-target-link-regexp) org-any-link-re
  2442. (concat org-any-link-re "\\|" org-target-link-regexp))))
  2443. (when (re-search-forward link-regexp limit t)
  2444. (cons 'link (match-beginning 0))))))
  2445. ;;;; Macro
  2446. (defun org-element-macro-parser ()
  2447. "Parse macro at point.
  2448. Return a list whose CAR is `macro' and CDR a plist with `:key',
  2449. `:args', `:begin', `:end', `:value' and `:post-blank' as
  2450. keywords.
  2451. Assume point is at the macro."
  2452. (save-excursion
  2453. (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
  2454. (let ((begin (point))
  2455. (key (downcase (org-match-string-no-properties 1)))
  2456. (value (org-match-string-no-properties 0))
  2457. (post-blank (progn (goto-char (match-end 0))
  2458. (skip-chars-forward " \t")))
  2459. (end (point))
  2460. (args (let ((args (org-match-string-no-properties 3)) args2)
  2461. (when args
  2462. (setq args (org-split-string args ","))
  2463. (while args
  2464. (while (string-match "\\\\\\'" (car args))
  2465. ;; Repair bad splits.
  2466. (setcar (cdr args) (concat (substring (car args) 0 -1)
  2467. "," (nth 1 args)))
  2468. (pop args))
  2469. (push (pop args) args2))
  2470. (mapcar 'org-trim (nreverse args2))))))
  2471. (list 'macro
  2472. (list :key key
  2473. :value value
  2474. :args args
  2475. :begin begin
  2476. :end end
  2477. :post-blank post-blank)))))
  2478. (defun org-element-macro-interpreter (macro contents)
  2479. "Interpret MACRO object as Org syntax.
  2480. CONTENTS is nil."
  2481. (org-element-property :value macro))
  2482. (defun org-element-macro-successor (limit)
  2483. "Search for the next macro object.
  2484. LIMIT bounds the search.
  2485. Return value is cons cell whose CAR is `macro' and CDR is
  2486. beginning position."
  2487. (save-excursion
  2488. (when (re-search-forward
  2489. "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
  2490. limit t)
  2491. (cons 'macro (match-beginning 0)))))
  2492. ;;;; Radio-target
  2493. (defun org-element-radio-target-parser ()
  2494. "Parse radio target at point.
  2495. Return a list whose CAR is `radio-target' and CDR a plist with
  2496. `:begin', `:end', `:contents-begin', `:contents-end', `:value'
  2497. and `:post-blank' as keywords.
  2498. Assume point is at the radio target."
  2499. (save-excursion
  2500. (looking-at org-radio-target-regexp)
  2501. (let ((begin (point))
  2502. (contents-begin (match-beginning 1))
  2503. (contents-end (match-end 1))
  2504. (value (org-match-string-no-properties 1))
  2505. (post-blank (progn (goto-char (match-end 0))
  2506. (skip-chars-forward " \t")))
  2507. (end (point)))
  2508. (list 'radio-target
  2509. (list :begin begin
  2510. :end end
  2511. :contents-begin contents-begin
  2512. :contents-end contents-end
  2513. :post-blank post-blank
  2514. :value value)))))
  2515. (defun org-element-radio-target-interpreter (target contents)
  2516. "Interpret TARGET object as Org syntax.
  2517. CONTENTS is the contents of the object."
  2518. (concat "<<<" contents ">>>"))
  2519. (defun org-element-radio-target-successor (limit)
  2520. "Search for the next radio-target object.
  2521. LIMIT bounds the search.
  2522. Return value is a cons cell whose CAR is `radio-target' and CDR
  2523. is beginning position."
  2524. (save-excursion
  2525. (when (re-search-forward org-radio-target-regexp limit t)
  2526. (cons 'radio-target (match-beginning 0)))))
  2527. ;;;; Statistics Cookie
  2528. (defun org-element-statistics-cookie-parser ()
  2529. "Parse statistics cookie at point.
  2530. Return a list whose CAR is `statistics-cookie', and CDR a plist
  2531. with `:begin', `:end', `:value' and `:post-blank' keywords.
  2532. Assume point is at the beginning of the statistics-cookie."
  2533. (save-excursion
  2534. (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
  2535. (let* ((begin (point))
  2536. (value (buffer-substring-no-properties
  2537. (match-beginning 0) (match-end 0)))
  2538. (post-blank (progn (goto-char (match-end 0))
  2539. (skip-chars-forward " \t")))
  2540. (end (point)))
  2541. (list 'statistics-cookie
  2542. (list :begin begin
  2543. :end end
  2544. :value value
  2545. :post-blank post-blank)))))
  2546. (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
  2547. "Interpret STATISTICS-COOKIE object as Org syntax.
  2548. CONTENTS is nil."
  2549. (org-element-property :value statistics-cookie))
  2550. (defun org-element-statistics-cookie-successor (limit)
  2551. "Search for the next statistics cookie object.
  2552. LIMIT bounds the search.
  2553. Return value is a cons cell whose CAR is `statistics-cookie' and
  2554. CDR is beginning position."
  2555. (save-excursion
  2556. (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
  2557. (cons 'statistics-cookie (match-beginning 0)))))
  2558. ;;;; Strike-Through
  2559. (defun org-element-strike-through-parser ()
  2560. "Parse strike-through object at point.
  2561. Return a list whose CAR is `strike-through' and CDR is a plist
  2562. with `:begin', `:end', `:contents-begin' and `:contents-end' and
  2563. `:post-blank' keywords.
  2564. Assume point is at the first plus sign marker."
  2565. (save-excursion
  2566. (unless (bolp) (backward-char 1))
  2567. (looking-at org-emph-re)
  2568. (let ((begin (match-beginning 2))
  2569. (contents-begin (match-beginning 4))
  2570. (contents-end (match-end 4))
  2571. (post-blank (progn (goto-char (match-end 2))
  2572. (skip-chars-forward " \t")))
  2573. (end (point)))
  2574. (list 'strike-through
  2575. (list :begin begin
  2576. :end end
  2577. :contents-begin contents-begin
  2578. :contents-end contents-end
  2579. :post-blank post-blank)))))
  2580. (defun org-element-strike-through-interpreter (strike-through contents)
  2581. "Interpret STRIKE-THROUGH object as Org syntax.
  2582. CONTENTS is the contents of the object."
  2583. (format "+%s+" contents))
  2584. ;;;; Subscript
  2585. (defun org-element-subscript-parser ()
  2586. "Parse subscript at point.
  2587. Return a list whose CAR is `subscript' and CDR a plist with
  2588. `:begin', `:end', `:contents-begin', `:contents-end',
  2589. `:use-brackets-p' and `:post-blank' as keywords.
  2590. Assume point is at the underscore."
  2591. (save-excursion
  2592. (unless (bolp) (backward-char))
  2593. (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
  2594. t
  2595. (not (looking-at org-match-substring-regexp))))
  2596. (begin (match-beginning 2))
  2597. (contents-begin (or (match-beginning 5)
  2598. (match-beginning 3)))
  2599. (contents-end (or (match-end 5) (match-end 3)))
  2600. (post-blank (progn (goto-char (match-end 0))
  2601. (skip-chars-forward " \t")))
  2602. (end (point)))
  2603. (list 'subscript
  2604. (list :begin begin
  2605. :end end
  2606. :use-brackets-p bracketsp
  2607. :contents-begin contents-begin
  2608. :contents-end contents-end
  2609. :post-blank post-blank)))))
  2610. (defun org-element-subscript-interpreter (subscript contents)
  2611. "Interpret SUBSCRIPT object as Org syntax.
  2612. CONTENTS is the contents of the object."
  2613. (format
  2614. (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
  2615. contents))
  2616. (defun org-element-sub/superscript-successor (limit)
  2617. "Search for the next sub/superscript object.
  2618. LIMIT bounds the search.
  2619. Return value is a cons cell whose CAR is either `subscript' or
  2620. `superscript' and CDR is beginning position."
  2621. (save-excursion
  2622. (when (re-search-forward org-match-substring-regexp limit t)
  2623. (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
  2624. (match-beginning 2)))))
  2625. ;;;; Superscript
  2626. (defun org-element-superscript-parser ()
  2627. "Parse superscript at point.
  2628. Return a list whose CAR is `superscript' and CDR a plist with
  2629. `:begin', `:end', `:contents-begin', `:contents-end',
  2630. `:use-brackets-p' and `:post-blank' as keywords.
  2631. Assume point is at the caret."
  2632. (save-excursion
  2633. (unless (bolp) (backward-char))
  2634. (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
  2635. (not (looking-at org-match-substring-regexp))))
  2636. (begin (match-beginning 2))
  2637. (contents-begin (or (match-beginning 5)
  2638. (match-beginning 3)))
  2639. (contents-end (or (match-end 5) (match-end 3)))
  2640. (post-blank (progn (goto-char (match-end 0))
  2641. (skip-chars-forward " \t")))
  2642. (end (point)))
  2643. (list 'superscript
  2644. (list :begin begin
  2645. :end end
  2646. :use-brackets-p bracketsp
  2647. :contents-begin contents-begin
  2648. :contents-end contents-end
  2649. :post-blank post-blank)))))
  2650. (defun org-element-superscript-interpreter (superscript contents)
  2651. "Interpret SUPERSCRIPT object as Org syntax.
  2652. CONTENTS is the contents of the object."
  2653. (format
  2654. (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
  2655. contents))
  2656. ;;;; Table Cell
  2657. (defun org-element-table-cell-parser ()
  2658. "Parse table cell at point.
  2659. Return a list whose CAR is `table-cell' and CDR is a plist
  2660. containing `:begin', `:end', `:contents-begin', `:contents-end'
  2661. and `:post-blank' keywords."
  2662. (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
  2663. (let* ((begin (match-beginning 0))
  2664. (end (match-end 0))
  2665. (contents-begin (match-beginning 1))
  2666. (contents-end (match-end 1)))
  2667. (list 'table-cell
  2668. (list :begin begin
  2669. :end end
  2670. :contents-begin contents-begin
  2671. :contents-end contents-end
  2672. :post-blank 0))))
  2673. (defun org-element-table-cell-interpreter (table-cell contents)
  2674. "Interpret TABLE-CELL element as Org syntax.
  2675. CONTENTS is the contents of the cell, or nil."
  2676. (concat " " contents " |"))
  2677. (defun org-element-table-cell-successor (limit)
  2678. "Search for the next table-cell object.
  2679. LIMIT bounds the search.
  2680. Return value is a cons cell whose CAR is `table-cell' and CDR is
  2681. beginning position."
  2682. (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
  2683. ;;;; Target
  2684. (defun org-element-target-parser ()
  2685. "Parse target at point.
  2686. Return a list whose CAR is `target' and CDR a plist with
  2687. `:begin', `:end', `:value' and `:post-blank' as keywords.
  2688. Assume point is at the target."
  2689. (save-excursion
  2690. (looking-at org-target-regexp)
  2691. (let ((begin (point))
  2692. (value (org-match-string-no-properties 1))
  2693. (post-blank (progn (goto-char (match-end 0))
  2694. (skip-chars-forward " \t")))
  2695. (end (point)))
  2696. (list 'target
  2697. (list :begin begin
  2698. :end end
  2699. :value value
  2700. :post-blank post-blank)))))
  2701. (defun org-element-target-interpreter (target contents)
  2702. "Interpret TARGET object as Org syntax.
  2703. CONTENTS is nil."
  2704. (format "<<%s>>" (org-element-property :value target)))
  2705. (defun org-element-target-successor (limit)
  2706. "Search for the next target object.
  2707. LIMIT bounds the search.
  2708. Return value is a cons cell whose CAR is `target' and CDR is
  2709. beginning position."
  2710. (save-excursion
  2711. (when (re-search-forward org-target-regexp limit t)
  2712. (cons 'target (match-beginning 0)))))
  2713. ;;;; Timestamp
  2714. (defun org-element-timestamp-parser ()
  2715. "Parse time stamp at point.
  2716. Return a list whose CAR is `timestamp', and CDR a plist with
  2717. `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
  2718. Assume point is at the beginning of the timestamp."
  2719. (save-excursion
  2720. (let* ((begin (point))
  2721. (activep (eq (char-after) ?<))
  2722. (main-value
  2723. (progn
  2724. (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
  2725. (match-string-no-properties 1)))
  2726. (range-end (match-string-no-properties 3))
  2727. (type (cond ((match-string 2) 'diary)
  2728. ((and activep range-end) 'active-range)
  2729. (activep 'active)
  2730. (range-end 'inactive-range)
  2731. (t 'inactive)))
  2732. (post-blank (progn (goto-char (match-end 0))
  2733. (skip-chars-forward " \t")))
  2734. (end (point)))
  2735. (list 'timestamp
  2736. (list :type type
  2737. :value main-value
  2738. :range-end range-end
  2739. :begin begin
  2740. :end end
  2741. :post-blank post-blank)))))
  2742. (defun org-element-timestamp-interpreter (timestamp contents)
  2743. "Interpret TIMESTAMP object as Org syntax.
  2744. CONTENTS is nil."
  2745. (let ((type (org-element-property :type timestamp) ))
  2746. (concat
  2747. (format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>")
  2748. (org-element-property :value timestamp))
  2749. (let ((range-end (org-element-property :range-end timestamp)))
  2750. (when range-end
  2751. (concat "--"
  2752. (format (if (eq type 'inactive-range) "[%s]" "<%s>")
  2753. range-end)))))))
  2754. (defun org-element-timestamp-successor (limit)
  2755. "Search for the next timestamp object.
  2756. LIMIT bounds the search.
  2757. Return value is a cons cell whose CAR is `timestamp' and CDR is
  2758. beginning position."
  2759. (save-excursion
  2760. (when (re-search-forward
  2761. (concat org-ts-regexp-both
  2762. "\\|"
  2763. "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  2764. "\\|"
  2765. "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
  2766. limit t)
  2767. (cons 'timestamp (match-beginning 0)))))
  2768. ;;;; Underline
  2769. (defun org-element-underline-parser ()
  2770. "Parse underline object at point.
  2771. Return a list whose CAR is `underline' and CDR is a plist with
  2772. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2773. `:post-blank' keywords.
  2774. Assume point is at the first underscore marker."
  2775. (save-excursion
  2776. (unless (bolp) (backward-char 1))
  2777. (looking-at org-emph-re)
  2778. (let ((begin (match-beginning 2))
  2779. (contents-begin (match-beginning 4))
  2780. (contents-end (match-end 4))
  2781. (post-blank (progn (goto-char (match-end 2))
  2782. (skip-chars-forward " \t")))
  2783. (end (point)))
  2784. (list 'underline
  2785. (list :begin begin
  2786. :end end
  2787. :contents-begin contents-begin
  2788. :contents-end contents-end
  2789. :post-blank post-blank)))))
  2790. (defun org-element-underline-interpreter (underline contents)
  2791. "Interpret UNDERLINE object as Org syntax.
  2792. CONTENTS is the contents of the object."
  2793. (format "_%s_" contents))
  2794. ;;;; Verbatim
  2795. (defun org-element-verbatim-parser ()
  2796. "Parse verbatim object at point.
  2797. Return a list whose CAR is `verbatim' and CDR is a plist with
  2798. `:value', `:begin', `:end' and `:post-blank' keywords.
  2799. Assume point is at the first equal sign marker."
  2800. (save-excursion
  2801. (unless (bolp) (backward-char 1))
  2802. (looking-at org-emph-re)
  2803. (let ((begin (match-beginning 2))
  2804. (value (org-match-string-no-properties 4))
  2805. (post-blank (progn (goto-char (match-end 2))
  2806. (skip-chars-forward " \t")))
  2807. (end (point)))
  2808. (list 'verbatim
  2809. (list :value value
  2810. :begin begin
  2811. :end end
  2812. :post-blank post-blank)))))
  2813. (defun org-element-verbatim-interpreter (verbatim contents)
  2814. "Interpret VERBATIM object as Org syntax.
  2815. CONTENTS is nil."
  2816. (format "=%s=" (org-element-property :value verbatim)))
  2817. ;;; Parsing Element Starting At Point
  2818. ;;
  2819. ;; `org-element--current-element' is the core function of this section.
  2820. ;; It returns the Lisp representation of the element starting at
  2821. ;; point.
  2822. ;;
  2823. ;; `org-element--current-element' makes use of special modes. They
  2824. ;; are activated for fixed element chaining (i.e. `plain-list' >
  2825. ;; `item') or fixed conditional element chaining (i.e. `headline' >
  2826. ;; `section'). Special modes are: `first-section', `section',
  2827. ;; `quote-section', `item' and `table-row'.
  2828. (defun org-element--current-element
  2829. (limit &optional granularity special structure)
  2830. "Parse the element starting at point.
  2831. LIMIT bounds the search.
  2832. Return value is a list like (TYPE PROPS) where TYPE is the type
  2833. of the element and PROPS a plist of properties associated to the
  2834. element.
  2835. Possible types are defined in `org-element-all-elements'.
  2836. Optional argument GRANULARITY determines the depth of the
  2837. recursion. Allowed values are `headline', `greater-element',
  2838. `element', `object' or nil. When it is broader than `object' (or
  2839. nil), secondary values will not be parsed, since they only
  2840. contain objects.
  2841. Optional argument SPECIAL, when non-nil, can be either
  2842. `first-section', `section', `quote-section', `table-row' and
  2843. `item'.
  2844. If STRUCTURE isn't provided but SPECIAL is set to `item', it will
  2845. be computed.
  2846. This function assumes point is always at the beginning of the
  2847. element it has to parse."
  2848. (save-excursion
  2849. ;; If point is at an affiliated keyword, try moving to the
  2850. ;; beginning of the associated element. If none is found, the
  2851. ;; keyword is orphaned and will be treated as plain text.
  2852. (when (looking-at org-element--affiliated-re)
  2853. (let ((opoint (point)))
  2854. (while (looking-at org-element--affiliated-re) (forward-line))
  2855. (when (looking-at "[ \t]*$") (goto-char opoint))))
  2856. (let ((case-fold-search t)
  2857. ;; Determine if parsing depth allows for secondary strings
  2858. ;; parsing. It only applies to elements referenced in
  2859. ;; `org-element-secondary-value-alist'.
  2860. (raw-secondary-p (and granularity (not (eq granularity 'object)))))
  2861. (cond
  2862. ;; Item.
  2863. ((eq special 'item)
  2864. (org-element-item-parser limit structure raw-secondary-p))
  2865. ;; Quote Section.
  2866. ((eq special 'quote-section) (org-element-quote-section-parser limit))
  2867. ;; Table Row.
  2868. ((eq special 'table-row) (org-element-table-row-parser limit))
  2869. ;; Headline.
  2870. ((org-with-limited-levels (org-at-heading-p))
  2871. (org-element-headline-parser limit raw-secondary-p))
  2872. ;; Section (must be checked after headline).
  2873. ((eq special 'section) (org-element-section-parser limit))
  2874. ((eq special 'first-section)
  2875. (org-element-section-parser
  2876. (or (save-excursion (org-with-limited-levels (outline-next-heading)))
  2877. limit)))
  2878. ;; Planning and Clock.
  2879. ((and (looking-at org-planning-or-clock-line-re))
  2880. (if (equal (match-string 1) org-clock-string)
  2881. (org-element-clock-parser limit)
  2882. (org-element-planning-parser limit)))
  2883. ;; Inlinetask.
  2884. ((org-at-heading-p)
  2885. (org-element-inlinetask-parser limit raw-secondary-p))
  2886. ;; LaTeX Environment.
  2887. ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
  2888. (if (save-excursion
  2889. (re-search-forward
  2890. (format "[ \t]*\\\\end{%s}[ \t]*"
  2891. (regexp-quote (match-string 1)))
  2892. nil t))
  2893. (org-element-latex-environment-parser limit)
  2894. (org-element-paragraph-parser limit)))
  2895. ;; Drawer and Property Drawer.
  2896. ((looking-at org-drawer-regexp)
  2897. (let ((name (match-string 1)))
  2898. (cond
  2899. ((not (save-excursion
  2900. (re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
  2901. (org-element-paragraph-parser limit))
  2902. ((equal "PROPERTIES" name)
  2903. (org-element-property-drawer-parser limit))
  2904. (t (org-element-drawer-parser limit)))))
  2905. ;; Fixed Width
  2906. ((looking-at "[ \t]*:\\( \\|$\\)")
  2907. (org-element-fixed-width-parser limit))
  2908. ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
  2909. ;; Keywords.
  2910. ((looking-at "[ \t]*#")
  2911. (goto-char (match-end 0))
  2912. (cond ((looking-at "\\(?: \\|$\\)")
  2913. (beginning-of-line)
  2914. (org-element-comment-parser limit))
  2915. ((looking-at "\\+BEGIN_\\(\\S-+\\)")
  2916. (beginning-of-line)
  2917. (let ((parser (assoc (upcase (match-string 1))
  2918. org-element-block-name-alist)))
  2919. (if parser (funcall (cdr parser) limit)
  2920. (org-element-special-block-parser limit))))
  2921. ((looking-at "\\+CALL:")
  2922. (beginning-of-line)
  2923. (org-element-babel-call-parser limit))
  2924. ((looking-at "\\+BEGIN:? ")
  2925. (beginning-of-line)
  2926. (org-element-dynamic-block-parser limit))
  2927. ((looking-at "\\+\\S-+:")
  2928. (beginning-of-line)
  2929. (org-element-keyword-parser limit))
  2930. (t
  2931. (beginning-of-line)
  2932. (org-element-paragraph-parser limit))))
  2933. ;; Footnote Definition.
  2934. ((looking-at org-footnote-definition-re)
  2935. (org-element-footnote-definition-parser limit))
  2936. ;; Horizontal Rule.
  2937. ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
  2938. (org-element-horizontal-rule-parser limit))
  2939. ;; Table.
  2940. ((org-at-table-p t) (org-element-table-parser limit))
  2941. ;; List.
  2942. ((looking-at (org-item-re))
  2943. (org-element-plain-list-parser limit (or structure (org-list-struct))))
  2944. ;; Default element: Paragraph.
  2945. (t (org-element-paragraph-parser limit))))))
  2946. ;; Most elements can have affiliated keywords. When looking for an
  2947. ;; element beginning, we want to move before them, as they belong to
  2948. ;; that element, and, in the meantime, collect information they give
  2949. ;; into appropriate properties. Hence the following function.
  2950. ;;
  2951. ;; Usage of optional arguments may not be obvious at first glance:
  2952. ;;
  2953. ;; - TRANS-LIST is used to polish keywords names that have evolved
  2954. ;; during Org history. In example, even though =result= and
  2955. ;; =results= coexist, we want to have them under the same =result=
  2956. ;; property. It's also true for "srcname" and "name", where the
  2957. ;; latter seems to be preferred nowadays (thus the "name" property).
  2958. ;;
  2959. ;; - CONSED allows to regroup multi-lines keywords under the same
  2960. ;; property, while preserving their own identity. This is mostly
  2961. ;; used for "attr_latex" and al.
  2962. ;;
  2963. ;; - PARSED prepares a keyword value for export. This is useful for
  2964. ;; "caption". Objects restrictions for such keywords are defined in
  2965. ;; `org-element-object-restrictions'.
  2966. ;;
  2967. ;; - DUALS is used to take care of keywords accepting a main and an
  2968. ;; optional secondary values. For example "results" has its
  2969. ;; source's name as the main value, and may have an hash string in
  2970. ;; optional square brackets as the secondary one.
  2971. ;;
  2972. ;; A keyword may belong to more than one category.
  2973. (defun org-element--collect-affiliated-keywords
  2974. (&optional key-re trans-list consed parsed duals)
  2975. "Collect affiliated keywords before point.
  2976. Optional argument KEY-RE is a regexp matching keywords, which
  2977. puts matched keyword in group 1. It defaults to
  2978. `org-element--affiliated-re'.
  2979. TRANS-LIST is an alist where key is the keyword and value the
  2980. property name it should be translated to, without the colons. It
  2981. defaults to `org-element-keyword-translation-alist'.
  2982. CONSED is a list of strings. Any keyword belonging to that list
  2983. will have its value consed. The check is done after keyword
  2984. translation. It defaults to `org-element-multiple-keywords'.
  2985. PARSED is a list of strings. Any keyword member of this list
  2986. will have its value parsed. The check is done after keyword
  2987. translation. If a keyword is a member of both CONSED and PARSED,
  2988. it's value will be a list of parsed strings. It defaults to
  2989. `org-element-parsed-keywords'.
  2990. DUALS is a list of strings. Any keyword member of this list can
  2991. have two parts: one mandatory and one optional. Its value is
  2992. a cons cell whose CAR is the former, and the CDR the latter. If
  2993. a keyword is a member of both PARSED and DUALS, both values will
  2994. be parsed. It defaults to `org-element-dual-keywords'.
  2995. Return a list whose CAR is the position at the first of them and
  2996. CDR a plist of keywords and values."
  2997. (save-excursion
  2998. (let ((case-fold-search t)
  2999. (key-re (or key-re org-element--affiliated-re))
  3000. (trans-list (or trans-list org-element-keyword-translation-alist))
  3001. (consed (or consed org-element-multiple-keywords))
  3002. (parsed (or parsed org-element-parsed-keywords))
  3003. (duals (or duals org-element-dual-keywords))
  3004. ;; RESTRICT is the list of objects allowed in parsed
  3005. ;; keywords value.
  3006. (restrict (org-element-restriction 'keyword))
  3007. output)
  3008. (unless (bobp)
  3009. (while (and (not (bobp)) (progn (forward-line -1) (looking-at key-re)))
  3010. (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
  3011. ;; Apply translation to RAW-KWD. From there, KWD is
  3012. ;; the official keyword.
  3013. (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
  3014. ;; Find main value for any keyword.
  3015. (value
  3016. (save-match-data
  3017. (org-trim
  3018. (buffer-substring-no-properties
  3019. (match-end 0) (point-at-eol)))))
  3020. ;; If KWD is a dual keyword, find its secondary
  3021. ;; value. Maybe parse it.
  3022. (dual-value
  3023. (and (member kwd duals)
  3024. (let ((sec (org-match-string-no-properties 3)))
  3025. (if (or (not sec) (not (member kwd parsed))) sec
  3026. (org-element-parse-secondary-string sec restrict)))))
  3027. ;; Attribute a property name to KWD.
  3028. (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
  3029. ;; Now set final shape for VALUE.
  3030. (when (member kwd parsed)
  3031. (setq value (org-element-parse-secondary-string value restrict)))
  3032. (when (member kwd duals)
  3033. ;; VALUE is mandatory. Set it to nil if there is none.
  3034. (setq value (and value (cons value dual-value))))
  3035. ;; Attributes are always consed.
  3036. (when (or (member kwd consed) (string-match "^ATTR_" kwd))
  3037. (setq value (cons value (plist-get output kwd-sym))))
  3038. ;; Eventually store the new value in OUTPUT.
  3039. (setq output (plist-put output kwd-sym value))))
  3040. (unless (looking-at key-re) (forward-line 1)))
  3041. (list (point) output))))
  3042. ;;; The Org Parser
  3043. ;;
  3044. ;; The two major functions here are `org-element-parse-buffer', which
  3045. ;; parses Org syntax inside the current buffer, taking into account
  3046. ;; region, narrowing, or even visibility if specified, and
  3047. ;; `org-element-parse-secondary-string', which parses objects within
  3048. ;; a given string.
  3049. ;;
  3050. ;; The (almost) almighty `org-element-map' allows to apply a function
  3051. ;; on elements or objects matching some type, and accumulate the
  3052. ;; resulting values. In an export situation, it also skips unneeded
  3053. ;; parts of the parse tree.
  3054. (defun org-element-parse-buffer (&optional granularity visible-only)
  3055. "Recursively parse the buffer and return structure.
  3056. If narrowing is in effect, only parse the visible part of the
  3057. buffer.
  3058. Optional argument GRANULARITY determines the depth of the
  3059. recursion. It can be set to the following symbols:
  3060. `headline' Only parse headlines.
  3061. `greater-element' Don't recurse into greater elements excepted
  3062. headlines and sections. Thus, elements
  3063. parsed are the top-level ones.
  3064. `element' Parse everything but objects and plain text.
  3065. `object' Parse the complete buffer (default).
  3066. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3067. elements.
  3068. Assume buffer is in Org mode."
  3069. (save-excursion
  3070. (goto-char (point-min))
  3071. (org-skip-whitespace)
  3072. (org-element--parse-elements
  3073. (point-at-bol) (point-max)
  3074. ;; Start in `first-section' mode so text before the first
  3075. ;; headline belongs to a section.
  3076. 'first-section nil granularity visible-only (list 'org-data nil))))
  3077. (defun org-element-parse-secondary-string (string restriction &optional parent)
  3078. "Recursively parse objects in STRING and return structure.
  3079. RESTRICTION is a symbol limiting the object types that will be
  3080. looked after.
  3081. Optional argument PARENT, when non-nil, is the element or object
  3082. containing the secondary string. It is used to set correctly
  3083. `:parent' property within the string."
  3084. (with-temp-buffer
  3085. (insert string)
  3086. (let ((secondary (org-element--parse-objects
  3087. (point-min) (point-max) nil restriction)))
  3088. (mapc (lambda (obj) (org-element-put-property obj :parent parent))
  3089. secondary))))
  3090. (defun org-element-map (data types fun &optional info first-match no-recursion)
  3091. "Map a function on selected elements or objects.
  3092. DATA is the parsed tree, as returned by, i.e,
  3093. `org-element-parse-buffer'. TYPES is a symbol or list of symbols
  3094. of elements or objects types. FUN is the function called on the
  3095. matching element or object. It must accept one arguments: the
  3096. element or object itself.
  3097. When optional argument INFO is non-nil, it should be a plist
  3098. holding export options. In that case, parts of the parse tree
  3099. not exportable according to that property list will be skipped.
  3100. When optional argument FIRST-MATCH is non-nil, stop at the first
  3101. match for which FUN doesn't return nil, and return that value.
  3102. Optional argument NO-RECURSION is a symbol or a list of symbols
  3103. representing elements or objects types. `org-element-map' won't
  3104. enter any recursive element or object whose type belongs to that
  3105. list. Though, FUN can still be applied on them.
  3106. Nil values returned from FUN do not appear in the results."
  3107. ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
  3108. (unless (listp types) (setq types (list types)))
  3109. (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
  3110. ;; Recursion depth is determined by --CATEGORY.
  3111. (let* ((--category
  3112. (catch 'found
  3113. (let ((category 'greater-elements))
  3114. (mapc (lambda (type)
  3115. (cond ((or (memq type org-element-all-objects)
  3116. (eq type 'plain-text))
  3117. ;; If one object is found, the function
  3118. ;; has to recurse into every object.
  3119. (throw 'found 'objects))
  3120. ((not (memq type org-element-greater-elements))
  3121. ;; If one regular element is found, the
  3122. ;; function has to recurse, at lest, into
  3123. ;; every element it encounters.
  3124. (and (not (eq category 'elements))
  3125. (setq category 'elements)))))
  3126. types)
  3127. category)))
  3128. --acc
  3129. --walk-tree
  3130. (--walk-tree
  3131. (function
  3132. (lambda (--data)
  3133. ;; Recursively walk DATA. INFO, if non-nil, is a plist
  3134. ;; holding contextual information.
  3135. (let ((--type (org-element-type --data)))
  3136. (cond
  3137. ((not --data))
  3138. ;; Ignored element in an export context.
  3139. ((and info (memq --data (plist-get info :ignore-list))))
  3140. ;; Secondary string: only objects can be found there.
  3141. ((not --type)
  3142. (when (eq --category 'objects) (mapc --walk-tree --data)))
  3143. ;; Unconditionally enter parse trees.
  3144. ((eq --type 'org-data)
  3145. (mapc --walk-tree (org-element-contents --data)))
  3146. (t
  3147. ;; Check if TYPE is matching among TYPES. If so,
  3148. ;; apply FUN to --DATA and accumulate return value
  3149. ;; into --ACC (or exit if FIRST-MATCH is non-nil).
  3150. (when (memq --type types)
  3151. (let ((result (funcall fun --data)))
  3152. (cond ((not result))
  3153. (first-match (throw '--map-first-match result))
  3154. (t (push result --acc)))))
  3155. ;; If --DATA has a secondary string that can contain
  3156. ;; objects with their type among TYPES, look into it.
  3157. (when (eq --category 'objects)
  3158. (let ((sec-prop
  3159. (assq --type org-element-secondary-value-alist)))
  3160. (when sec-prop
  3161. (funcall --walk-tree
  3162. (org-element-property (cdr sec-prop) --data)))))
  3163. ;; Determine if a recursion into --DATA is possible.
  3164. (cond
  3165. ;; --TYPE is explicitly removed from recursion.
  3166. ((memq --type no-recursion))
  3167. ;; --DATA has no contents.
  3168. ((not (org-element-contents --data)))
  3169. ;; Looking for greater elements but --DATA is simply
  3170. ;; an element or an object.
  3171. ((and (eq --category 'greater-elements)
  3172. (not (memq --type org-element-greater-elements))))
  3173. ;; Looking for elements but --DATA is an object.
  3174. ((and (eq --category 'elements)
  3175. (memq --type org-element-all-objects)))
  3176. ;; In any other case, map contents.
  3177. (t (mapc --walk-tree (org-element-contents --data)))))))))))
  3178. (catch '--map-first-match
  3179. (funcall --walk-tree data)
  3180. ;; Return value in a proper order.
  3181. (nreverse --acc))))
  3182. ;; The following functions are internal parts of the parser.
  3183. ;;
  3184. ;; The first one, `org-element--parse-elements' acts at the element's
  3185. ;; level.
  3186. ;;
  3187. ;; The second one, `org-element--parse-objects' applies on all objects
  3188. ;; of a paragraph or a secondary string. It uses
  3189. ;; `org-element--get-next-object-candidates' to optimize the search of
  3190. ;; the next object in the buffer.
  3191. ;;
  3192. ;; More precisely, that function looks for every allowed object type
  3193. ;; first. Then, it discards failed searches, keeps further matches,
  3194. ;; and searches again types matched behind point, for subsequent
  3195. ;; calls. Thus, searching for a given type fails only once, and every
  3196. ;; object is searched only once at top level (but sometimes more for
  3197. ;; nested types).
  3198. (defun org-element--parse-elements
  3199. (beg end special structure granularity visible-only acc)
  3200. "Parse elements between BEG and END positions.
  3201. SPECIAL prioritize some elements over the others. It can be set
  3202. to `first-section', `quote-section', `section' `item' or
  3203. `table-row'.
  3204. When value is `item', STRUCTURE will be used as the current list
  3205. structure.
  3206. GRANULARITY determines the depth of the recursion. See
  3207. `org-element-parse-buffer' for more information.
  3208. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3209. elements.
  3210. Elements are accumulated into ACC."
  3211. (save-excursion
  3212. (goto-char beg)
  3213. ;; When parsing only headlines, skip any text before first one.
  3214. (when (and (eq granularity 'headline) (not (org-at-heading-p)))
  3215. (org-with-limited-levels (outline-next-heading)))
  3216. ;; Main loop start.
  3217. (while (< (point) end)
  3218. ;; Find current element's type and parse it accordingly to
  3219. ;; its category.
  3220. (let* ((element (org-element--current-element
  3221. end granularity special structure))
  3222. (type (org-element-type element))
  3223. (cbeg (org-element-property :contents-begin element)))
  3224. (goto-char (org-element-property :end element))
  3225. ;; Fill ELEMENT contents by side-effect.
  3226. (cond
  3227. ;; If VISIBLE-ONLY is true and element is hidden or if it has
  3228. ;; no contents, don't modify it.
  3229. ((or (and visible-only (org-element-property :hiddenp element))
  3230. (not cbeg)))
  3231. ;; Greater element: parse it between `contents-begin' and
  3232. ;; `contents-end'. Make sure GRANULARITY allows the
  3233. ;; recursion, or ELEMENT is an headline, in which case going
  3234. ;; inside is mandatory, in order to get sub-level headings.
  3235. ((and (memq type org-element-greater-elements)
  3236. (or (memq granularity '(element object nil))
  3237. (and (eq granularity 'greater-element)
  3238. (eq type 'section))
  3239. (eq type 'headline)))
  3240. (org-element--parse-elements
  3241. cbeg (org-element-property :contents-end element)
  3242. ;; Possibly switch to a special mode.
  3243. (case type
  3244. (headline
  3245. (if (org-element-property :quotedp element) 'quote-section
  3246. 'section))
  3247. (plain-list 'item)
  3248. (table 'table-row))
  3249. (org-element-property :structure element)
  3250. granularity visible-only element))
  3251. ;; ELEMENT has contents. Parse objects inside, if
  3252. ;; GRANULARITY allows it.
  3253. ((memq granularity '(object nil))
  3254. (org-element--parse-objects
  3255. cbeg (org-element-property :contents-end element) element
  3256. (org-element-restriction type))))
  3257. (org-element-adopt-element acc element t)))
  3258. ;; Return result.
  3259. acc))
  3260. (defun org-element--parse-objects (beg end acc restriction)
  3261. "Parse objects between BEG and END and return recursive structure.
  3262. Objects are accumulated in ACC.
  3263. RESTRICTION is a list of object types which are allowed in the
  3264. current object."
  3265. (let (candidates)
  3266. (save-excursion
  3267. (goto-char beg)
  3268. (while (and (< (point) end)
  3269. (setq candidates (org-element--get-next-object-candidates
  3270. end restriction candidates)))
  3271. (let ((next-object
  3272. (let ((pos (apply 'min (mapcar 'cdr candidates))))
  3273. (save-excursion
  3274. (goto-char pos)
  3275. (funcall (intern (format "org-element-%s-parser"
  3276. (car (rassq pos candidates)))))))))
  3277. ;; 1. Text before any object. Untabify it.
  3278. (let ((obj-beg (org-element-property :begin next-object)))
  3279. (unless (= (point) obj-beg)
  3280. (setq acc
  3281. (org-element-adopt-element
  3282. acc
  3283. (replace-regexp-in-string
  3284. "\t" (make-string tab-width ? )
  3285. (buffer-substring-no-properties (point) obj-beg)) t))))
  3286. ;; 2. Object...
  3287. (let ((obj-end (org-element-property :end next-object))
  3288. (cont-beg (org-element-property :contents-begin next-object)))
  3289. ;; Fill contents of NEXT-OBJECT by side-effect, if it has
  3290. ;; a recursive type.
  3291. (when (and cont-beg
  3292. (memq (car next-object) org-element-recursive-objects))
  3293. (save-restriction
  3294. (narrow-to-region
  3295. cont-beg
  3296. (org-element-property :contents-end next-object))
  3297. (org-element--parse-objects
  3298. (point-min) (point-max) next-object
  3299. (org-element-restriction next-object))))
  3300. (setq acc (org-element-adopt-element acc next-object t))
  3301. (goto-char obj-end))))
  3302. ;; 3. Text after last object. Untabify it.
  3303. (unless (= (point) end)
  3304. (setq acc
  3305. (org-element-adopt-element
  3306. acc
  3307. (replace-regexp-in-string
  3308. "\t" (make-string tab-width ? )
  3309. (buffer-substring-no-properties (point) end)) t)))
  3310. ;; Result.
  3311. acc)))
  3312. (defun org-element--get-next-object-candidates (limit restriction objects)
  3313. "Return an alist of candidates for the next object.
  3314. LIMIT bounds the search, and RESTRICTION narrows candidates to
  3315. some object types.
  3316. Return value is an alist whose CAR is position and CDR the object
  3317. type, as a symbol.
  3318. OBJECTS is the previous candidates alist."
  3319. (let (next-candidates types-to-search)
  3320. ;; If no previous result, search every object type in RESTRICTION.
  3321. ;; Otherwise, keep potential candidates (old objects located after
  3322. ;; point) and ask to search again those which had matched before.
  3323. (if (not objects) (setq types-to-search restriction)
  3324. (mapc (lambda (obj)
  3325. (if (< (cdr obj) (point)) (push (car obj) types-to-search)
  3326. (push obj next-candidates)))
  3327. objects))
  3328. ;; Call the appropriate successor function for each type to search
  3329. ;; and accumulate matches.
  3330. (mapc
  3331. (lambda (type)
  3332. (let* ((successor-fun
  3333. (intern
  3334. (format "org-element-%s-successor"
  3335. (or (cdr (assq type org-element-object-successor-alist))
  3336. type))))
  3337. (obj (funcall successor-fun limit)))
  3338. (and obj (push obj next-candidates))))
  3339. types-to-search)
  3340. ;; Return alist.
  3341. next-candidates))
  3342. ;;; Towards A Bijective Process
  3343. ;;
  3344. ;; The parse tree obtained with `org-element-parse-buffer' is really
  3345. ;; a snapshot of the corresponding Org buffer. Therefore, it can be
  3346. ;; interpreted and expanded into a string with canonical Org syntax.
  3347. ;; Hence `org-element-interpret-data'.
  3348. ;;
  3349. ;; The function relies internally on
  3350. ;; `org-element--interpret-affiliated-keywords'.
  3351. (defun org-element-interpret-data (data &optional parent)
  3352. "Interpret DATA as Org syntax.
  3353. DATA is a parse tree, an element, an object or a secondary string
  3354. to interpret.
  3355. Optional argument PARENT is used for recursive calls. It contains
  3356. the element or object containing data, or nil.
  3357. Return Org syntax as a string."
  3358. (let* ((type (org-element-type data))
  3359. (results
  3360. (cond
  3361. ;; Secondary string.
  3362. ((not type)
  3363. (mapconcat
  3364. (lambda (obj) (org-element-interpret-data obj parent))
  3365. data ""))
  3366. ;; Full Org document.
  3367. ((eq type 'org-data)
  3368. (mapconcat
  3369. (lambda (obj) (org-element-interpret-data obj parent))
  3370. (org-element-contents data) ""))
  3371. ;; Plain text.
  3372. ((stringp data) data)
  3373. ;; Element/Object without contents.
  3374. ((not (org-element-contents data))
  3375. (funcall (intern (format "org-element-%s-interpreter" type))
  3376. data nil))
  3377. ;; Element/Object with contents.
  3378. (t
  3379. (let* ((greaterp (memq type org-element-greater-elements))
  3380. (objectp (and (not greaterp)
  3381. (memq type org-element-recursive-objects)))
  3382. (contents
  3383. (mapconcat
  3384. (lambda (obj) (org-element-interpret-data obj data))
  3385. (org-element-contents
  3386. (if (or greaterp objectp) data
  3387. ;; Elements directly containing objects must
  3388. ;; have their indentation normalized first.
  3389. (org-element-normalize-contents
  3390. data
  3391. ;; When normalizing first paragraph of an
  3392. ;; item or a footnote-definition, ignore
  3393. ;; first line's indentation.
  3394. (and (eq type 'paragraph)
  3395. (equal data (car (org-element-contents parent)))
  3396. (memq (org-element-type parent)
  3397. '(footnote-definiton item))))))
  3398. "")))
  3399. (funcall (intern (format "org-element-%s-interpreter" type))
  3400. data
  3401. (if greaterp (org-element-normalize-contents contents)
  3402. contents)))))))
  3403. (if (memq type '(org-data plain-text nil)) results
  3404. ;; Build white spaces. If no `:post-blank' property is
  3405. ;; specified, assume its value is 0.
  3406. (let ((post-blank (or (org-element-property :post-blank data) 0)))
  3407. (if (memq type org-element-all-objects)
  3408. (concat results (make-string post-blank 32))
  3409. (concat
  3410. (org-element--interpret-affiliated-keywords data)
  3411. (org-element-normalize-string results)
  3412. (make-string post-blank 10)))))))
  3413. (defun org-element--interpret-affiliated-keywords (element)
  3414. "Return ELEMENT's affiliated keywords as Org syntax.
  3415. If there is no affiliated keyword, return the empty string."
  3416. (let ((keyword-to-org
  3417. (function
  3418. (lambda (key value)
  3419. (let (dual)
  3420. (when (member key org-element-dual-keywords)
  3421. (setq dual (cdr value) value (car value)))
  3422. (concat "#+" key
  3423. (and dual
  3424. (format "[%s]" (org-element-interpret-data dual)))
  3425. ": "
  3426. (if (member key org-element-parsed-keywords)
  3427. (org-element-interpret-data value)
  3428. value)
  3429. "\n"))))))
  3430. (mapconcat
  3431. (lambda (prop)
  3432. (let ((value (org-element-property prop element))
  3433. (keyword (upcase (substring (symbol-name prop) 1))))
  3434. (when value
  3435. (if (or (member keyword org-element-multiple-keywords)
  3436. ;; All attribute keywords can have multiple lines.
  3437. (string-match "^ATTR_" keyword))
  3438. (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
  3439. value
  3440. "")
  3441. (funcall keyword-to-org keyword value)))))
  3442. ;; List all ELEMENT's properties matching an attribute line or an
  3443. ;; affiliated keyword, but ignore translated keywords since they
  3444. ;; cannot belong to the property list.
  3445. (loop for prop in (nth 1 element) by 'cddr
  3446. when (let ((keyword (upcase (substring (symbol-name prop) 1))))
  3447. (or (string-match "^ATTR_" keyword)
  3448. (and
  3449. (member keyword org-element-affiliated-keywords)
  3450. (not (assoc keyword
  3451. org-element-keyword-translation-alist)))))
  3452. collect prop)
  3453. "")))
  3454. ;; Because interpretation of the parse tree must return the same
  3455. ;; number of blank lines between elements and the same number of white
  3456. ;; space after objects, some special care must be given to white
  3457. ;; spaces.
  3458. ;;
  3459. ;; The first function, `org-element-normalize-string', ensures any
  3460. ;; string different from the empty string will end with a single
  3461. ;; newline character.
  3462. ;;
  3463. ;; The second function, `org-element-normalize-contents', removes
  3464. ;; global indentation from the contents of the current element.
  3465. (defun org-element-normalize-string (s)
  3466. "Ensure string S ends with a single newline character.
  3467. If S isn't a string return it unchanged. If S is the empty
  3468. string, return it. Otherwise, return a new string with a single
  3469. newline character at its end."
  3470. (cond
  3471. ((not (stringp s)) s)
  3472. ((string= "" s) "")
  3473. (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
  3474. (replace-match "\n" nil nil s)))))
  3475. (defun org-element-normalize-contents (element &optional ignore-first)
  3476. "Normalize plain text in ELEMENT's contents.
  3477. ELEMENT must only contain plain text and objects.
  3478. If optional argument IGNORE-FIRST is non-nil, ignore first line's
  3479. indentation to compute maximal common indentation.
  3480. Return the normalized element that is element with global
  3481. indentation removed from its contents. The function assumes that
  3482. indentation is not done with TAB characters."
  3483. (let* (ind-list ; for byte-compiler
  3484. collect-inds ; for byte-compiler
  3485. (collect-inds
  3486. (function
  3487. ;; Return list of indentations within BLOB. This is done by
  3488. ;; walking recursively BLOB and updating IND-LIST along the
  3489. ;; way. FIRST-FLAG is non-nil when the first string hasn't
  3490. ;; been seen yet. It is required as this string is the only
  3491. ;; one whose indentation doesn't happen after a newline
  3492. ;; character.
  3493. (lambda (blob first-flag)
  3494. (mapc
  3495. (lambda (object)
  3496. (when (and first-flag (stringp object))
  3497. (setq first-flag nil)
  3498. (string-match "\\`\\( *\\)" object)
  3499. (let ((len (length (match-string 1 object))))
  3500. ;; An indentation of zero means no string will be
  3501. ;; modified. Quit the process.
  3502. (if (zerop len) (throw 'zero (setq ind-list nil))
  3503. (push len ind-list))))
  3504. (cond
  3505. ((stringp object)
  3506. (let ((start 0))
  3507. ;; Avoid matching blank or empty lines.
  3508. (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
  3509. (not (equal (match-string 2 object) " ")))
  3510. (setq start (match-end 0))
  3511. (push (length (match-string 1 object)) ind-list))))
  3512. ((memq (org-element-type object) org-element-recursive-objects)
  3513. (funcall collect-inds object first-flag))))
  3514. (org-element-contents blob))))))
  3515. ;; Collect indentation list in ELEMENT. Possibly remove first
  3516. ;; value if IGNORE-FIRST is non-nil.
  3517. (catch 'zero (funcall collect-inds element (not ignore-first)))
  3518. (if (not ind-list) element
  3519. ;; Build ELEMENT back, replacing each string with the same
  3520. ;; string minus common indentation.
  3521. (let* (build ; For byte compiler.
  3522. (build
  3523. (function
  3524. (lambda (blob mci first-flag)
  3525. ;; Return BLOB with all its strings indentation
  3526. ;; shortened from MCI white spaces. FIRST-FLAG is
  3527. ;; non-nil when the first string hasn't been seen
  3528. ;; yet.
  3529. (setcdr (cdr blob)
  3530. (mapcar
  3531. (lambda (object)
  3532. (when (and first-flag (stringp object))
  3533. (setq first-flag nil)
  3534. (setq object
  3535. (replace-regexp-in-string
  3536. (format "\\` \\{%d\\}" mci) "" object)))
  3537. (cond
  3538. ((stringp object)
  3539. (replace-regexp-in-string
  3540. (format "\n \\{%d\\}" mci) "\n" object))
  3541. ((memq (org-element-type object)
  3542. org-element-recursive-objects)
  3543. (funcall build object mci first-flag))
  3544. (t object)))
  3545. (org-element-contents blob)))
  3546. blob))))
  3547. (funcall build element (apply 'min ind-list) (not ignore-first))))))
  3548. ;;; The Toolbox
  3549. ;;
  3550. ;; The first move is to implement a way to obtain the smallest element
  3551. ;; containing point. This is the job of `org-element-at-point'. It
  3552. ;; basically jumps back to the beginning of section containing point
  3553. ;; and moves, element after element, with
  3554. ;; `org-element--current-element' until the container is found. Note:
  3555. ;; When using `org-element-at-point', secondary values are never
  3556. ;; parsed since the function focuses on elements, not on objects.
  3557. ;;
  3558. ;; At a deeper level, `org-element-context' lists all elements and
  3559. ;; objects containing point.
  3560. ;;
  3561. ;; `org-element-nested-p' and `org-element-swap-A-B' may be used
  3562. ;; internally by navigation and manipulation tools.
  3563. ;;;###autoload
  3564. (defun org-element-at-point (&optional keep-trail)
  3565. "Determine closest element around point.
  3566. Return value is a list like (TYPE PROPS) where TYPE is the type
  3567. of the element and PROPS a plist of properties associated to the
  3568. element.
  3569. Possible types are defined in `org-element-all-elements'.
  3570. Properties depend on element or object type, but always
  3571. include :begin, :end, :parent and :post-blank properties.
  3572. As a special case, if point is at the very beginning of a list or
  3573. sub-list, returned element will be that list instead of the first
  3574. item. In the same way, if point is at the beginning of the first
  3575. row of a table, returned element will be the table instead of the
  3576. first row.
  3577. If optional argument KEEP-TRAIL is non-nil, the function returns
  3578. a list of of elements leading to element at point. The list's
  3579. CAR is always the element at point. Following positions contain
  3580. element's siblings, then parents, siblings of parents, until the
  3581. first element of current section."
  3582. (org-with-wide-buffer
  3583. ;; If at an headline, parse it. It is the sole element that
  3584. ;; doesn't require to know about context. Be sure to disallow
  3585. ;; secondary string parsing, though.
  3586. (if (org-with-limited-levels (org-at-heading-p))
  3587. (progn
  3588. (beginning-of-line)
  3589. (if (not keep-trail) (org-element-headline-parser (point-max) t)
  3590. (list (org-element-headline-parser (point-max) t))))
  3591. ;; Otherwise move at the beginning of the section containing
  3592. ;; point.
  3593. (let ((origin (point))
  3594. (end (save-excursion
  3595. (org-with-limited-levels (outline-next-heading)) (point)))
  3596. element type special-flag trail struct prevs parent)
  3597. (org-with-limited-levels
  3598. (if (org-with-limited-levels (org-before-first-heading-p))
  3599. (goto-char (point-min))
  3600. (org-back-to-heading)
  3601. (forward-line)))
  3602. (org-skip-whitespace)
  3603. (beginning-of-line)
  3604. ;; Parse successively each element, skipping those ending
  3605. ;; before original position.
  3606. (catch 'exit
  3607. (while t
  3608. (setq element
  3609. (org-element--current-element end 'element special-flag struct)
  3610. type (car element))
  3611. (org-element-put-property element :parent parent)
  3612. (when keep-trail (push element trail))
  3613. (cond
  3614. ;; 1. Skip any element ending before point. Also skip
  3615. ;; element ending at point when we're sure that another
  3616. ;; element has started.
  3617. ((let ((elem-end (org-element-property :end element)))
  3618. (when (or (< elem-end origin)
  3619. (and (= elem-end origin) (/= elem-end end)))
  3620. (goto-char elem-end))))
  3621. ;; 2. An element containing point is always the element at
  3622. ;; point.
  3623. ((not (memq type org-element-greater-elements))
  3624. (throw 'exit (if keep-trail trail element)))
  3625. ;; 3. At any other greater element type, if point is
  3626. ;; within contents, move into it.
  3627. (t
  3628. (let ((cbeg (org-element-property :contents-begin element))
  3629. (cend (org-element-property :contents-end element)))
  3630. (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
  3631. ;; Create an anchor for tables and plain lists:
  3632. ;; when point is at the very beginning of these
  3633. ;; elements, ignoring affiliated keywords,
  3634. ;; target them instead of their contents.
  3635. (and (= cbeg origin) (memq type '(plain-list table)))
  3636. ;; When point is at contents end, do not move
  3637. ;; into elements with an explicit ending, but
  3638. ;; return that element instead.
  3639. (and (= cend origin)
  3640. (memq type
  3641. '(center-block
  3642. drawer dynamic-block inlinetask item
  3643. plain-list quote-block special-block))))
  3644. (throw 'exit (if keep-trail trail element))
  3645. (setq parent element)
  3646. (case type
  3647. (plain-list
  3648. (setq special-flag 'item
  3649. struct (org-element-property :structure element)))
  3650. (table (setq special-flag 'table-row))
  3651. (otherwise (setq special-flag nil)))
  3652. (setq end cend)
  3653. (goto-char cbeg)))))))))))
  3654. (defun org-element-context ()
  3655. "Return closest element or object around point.
  3656. Return value is a list like (TYPE PROPS) where TYPE is the type
  3657. of the element or object and PROPS a plist of properties
  3658. associated to it.
  3659. Possible types are defined in `org-element-all-elements' and
  3660. `org-element-all-objects'. Properties depend on element or
  3661. object type, but always include :begin, :end, :parent
  3662. and :post-blank properties."
  3663. (org-with-wide-buffer
  3664. (let* ((origin (point))
  3665. (element (org-element-at-point))
  3666. (type (car element))
  3667. end)
  3668. ;; Check if point is inside an element containing objects or at
  3669. ;; a secondary string. In that case, move to beginning of the
  3670. ;; element or secondary string and set END to the other side.
  3671. (if (not (or (and (eq type 'item)
  3672. (let ((tag (org-element-property :tag element)))
  3673. (and tag
  3674. (progn
  3675. (beginning-of-line)
  3676. (search-forward tag (point-at-eol))
  3677. (goto-char (match-beginning 0))
  3678. (and (>= origin (point))
  3679. (<= origin
  3680. ;; `1+' is required so some
  3681. ;; successors can match
  3682. ;; properly their object.
  3683. (setq end (1+ (match-end 0)))))))))
  3684. (and (memq type '(headline inlinetask))
  3685. (progn (beginning-of-line)
  3686. (skip-chars-forward "* ")
  3687. (setq end (point-at-eol))))
  3688. (and (memq type '(paragraph table-cell verse-block))
  3689. (let ((cbeg (org-element-property
  3690. :contents-begin element))
  3691. (cend (org-element-property
  3692. :contents-end element)))
  3693. (and (>= origin cbeg)
  3694. (<= origin cend)
  3695. (progn (goto-char cbeg) (setq end cend)))))))
  3696. element
  3697. (let ((restriction (org-element-restriction element))
  3698. (parent element)
  3699. candidates)
  3700. (catch 'exit
  3701. (while (setq candidates (org-element--get-next-object-candidates
  3702. end restriction candidates))
  3703. (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
  3704. candidates)))
  3705. ;; If ORIGIN is before next object in element, there's
  3706. ;; no point in looking further.
  3707. (if (> (cdr closest-cand) origin) (throw 'exit element)
  3708. (let* ((object
  3709. (progn (goto-char (cdr closest-cand))
  3710. (funcall (intern (format "org-element-%s-parser"
  3711. (car closest-cand))))))
  3712. (cbeg (org-element-property :contents-begin object))
  3713. (cend (org-element-property :contents-end object)))
  3714. (cond
  3715. ;; ORIGIN is after OBJECT, so skip it.
  3716. ((< (org-element-property :end object) origin)
  3717. (goto-char (org-element-property :end object)))
  3718. ;; ORIGIN is within a non-recursive object or at an
  3719. ;; object boundaries: Return that object.
  3720. ((or (not cbeg) (> cbeg origin) (< cend origin))
  3721. (throw 'exit
  3722. (org-element-put-property object :parent parent)))
  3723. ;; Otherwise, move within current object and restrict
  3724. ;; search to the end of its contents.
  3725. (t (goto-char cbeg)
  3726. (org-element-put-property object :parent parent)
  3727. (setq parent object end cend)))))))
  3728. parent))))))
  3729. (defsubst org-element-nested-p (elem-A elem-B)
  3730. "Non-nil when elements ELEM-A and ELEM-B are nested."
  3731. (let ((beg-A (org-element-property :begin elem-A))
  3732. (beg-B (org-element-property :begin elem-B))
  3733. (end-A (org-element-property :end elem-A))
  3734. (end-B (org-element-property :end elem-B)))
  3735. (or (and (>= beg-A beg-B) (<= end-A end-B))
  3736. (and (>= beg-B beg-A) (<= end-B end-A)))))
  3737. (defun org-element-swap-A-B (elem-A elem-B)
  3738. "Swap elements ELEM-A and ELEM-B.
  3739. Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
  3740. end of ELEM-A."
  3741. (goto-char (org-element-property :begin elem-A))
  3742. ;; There are two special cases when an element doesn't start at bol:
  3743. ;; the first paragraph in an item or in a footnote definition.
  3744. (let ((specialp (not (bolp))))
  3745. ;; Only a paragraph without any affiliated keyword can be moved at
  3746. ;; ELEM-A position in such a situation. Note that the case of
  3747. ;; a footnote definition is impossible: it cannot contain two
  3748. ;; paragraphs in a row because it cannot contain a blank line.
  3749. (if (and specialp
  3750. (or (not (eq (org-element-type elem-B) 'paragraph))
  3751. (/= (org-element-property :begin elem-B)
  3752. (org-element-property :contents-begin elem-B))))
  3753. (error "Cannot swap elements"))
  3754. ;; In a special situation, ELEM-A will have no indentation. We'll
  3755. ;; give it ELEM-B's (which will in, in turn, have no indentation).
  3756. (let* ((ind-B (when specialp
  3757. (goto-char (org-element-property :begin elem-B))
  3758. (org-get-indentation)))
  3759. (beg-A (org-element-property :begin elem-A))
  3760. (end-A (save-excursion
  3761. (goto-char (org-element-property :end elem-A))
  3762. (skip-chars-backward " \r\t\n")
  3763. (point-at-eol)))
  3764. (beg-B (org-element-property :begin elem-B))
  3765. (end-B (save-excursion
  3766. (goto-char (org-element-property :end elem-B))
  3767. (skip-chars-backward " \r\t\n")
  3768. (point-at-eol)))
  3769. ;; Store overlays responsible for visibility status. We
  3770. ;; also need to store their boundaries as they will be
  3771. ;; removed from buffer.
  3772. (overlays
  3773. (cons
  3774. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  3775. (overlays-in beg-A end-A))
  3776. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  3777. (overlays-in beg-B end-B))))
  3778. ;; Get contents.
  3779. (body-A (buffer-substring beg-A end-A))
  3780. (body-B (delete-and-extract-region beg-B end-B)))
  3781. (goto-char beg-B)
  3782. (when specialp
  3783. (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
  3784. (org-indent-to-column ind-B))
  3785. (insert body-A)
  3786. ;; Restore ex ELEM-A overlays.
  3787. (let ((offset (- beg-B beg-A)))
  3788. (mapc (lambda (ov)
  3789. (move-overlay
  3790. (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
  3791. (car overlays))
  3792. (goto-char beg-A)
  3793. (delete-region beg-A end-A)
  3794. (insert body-B)
  3795. ;; Restore ex ELEM-B overlays.
  3796. (mapc (lambda (ov)
  3797. (move-overlay
  3798. (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
  3799. (cdr overlays)))
  3800. (goto-char (org-element-property :end elem-B)))))
  3801. (provide 'org-element)
  3802. ;;; org-element.el ends here