org-element.el 175 KB

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