org-element.el 165 KB

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