org-element.el 176 KB

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