org-element.el 174 KB

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