org-element.el 174 KB

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