org-element.el 171 KB

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