org-element.el 166 KB

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