org-element.el 175 KB

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