org-element.el 157 KB

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