org-element.el 175 KB

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