org-element.el 154 KB

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