org-element.el 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494
  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. ;; Match first line with a loose regexp since it might as
  1201. ;; well be an ill-defined keyword.
  1202. (value (progn
  1203. (looking-at "#\\+? ?")
  1204. (buffer-substring-no-properties
  1205. (match-end 0) (progn (forward-line) (point)))))
  1206. (com-end
  1207. ;; Get comments ending.
  1208. (progn
  1209. (while (and (< (point) limit)
  1210. (looking-at "\\(\\(# ?\\)[^+]\\|[ \t]*#\\+\\( \\|$\\)\\)"))
  1211. ;; Accumulate lines without leading hash and plus sign
  1212. ;; if any. First whitespace is also ignored.
  1213. (setq value
  1214. (concat value
  1215. (buffer-substring-no-properties
  1216. (or (match-end 2) (match-end 3))
  1217. (progn (forward-line) (point))))))
  1218. (point)))
  1219. (end (progn (goto-char com-end)
  1220. (skip-chars-forward " \r\t\n" limit)
  1221. (if (eobp) (point) (point-at-bol)))))
  1222. (list 'comment
  1223. (nconc
  1224. (list :begin begin
  1225. :end end
  1226. :value value
  1227. :post-blank (count-lines com-end end))
  1228. (cadr keywords))))))
  1229. (defun org-element-comment-interpreter (comment contents)
  1230. "Interpret COMMENT element as Org syntax.
  1231. CONTENTS is nil."
  1232. (replace-regexp-in-string "^" "#+ " (org-element-property :value comment)))
  1233. ;;;; Comment Block
  1234. (defun org-element-comment-block-parser (limit)
  1235. "Parse an export block.
  1236. LIMIT bounds the search.
  1237. Return a list whose CAR is `comment-block' and CDR is a plist
  1238. containing `:begin', `:end', `:hiddenp', `:value' and
  1239. `:post-blank' keywords.
  1240. Assume point is at comment block beginning."
  1241. (let ((case-fold-search t))
  1242. (if (not (save-excursion
  1243. (re-search-forward "^[ \t]*#\\+END_COMMENT" limit t)))
  1244. ;; Incomplete block: parse it as a comment.
  1245. (org-element-comment-parser limit)
  1246. (let ((contents-end (match-beginning 0)))
  1247. (save-excursion
  1248. (let* ((keywords (org-element--collect-affiliated-keywords))
  1249. (begin (car keywords))
  1250. (contents-begin (progn (forward-line) (point)))
  1251. (hidden (org-invisible-p2))
  1252. (pos-before-blank (progn (goto-char contents-end)
  1253. (forward-line)
  1254. (point)))
  1255. (end (progn (skip-chars-forward " \r\t\n" limit)
  1256. (if (eobp) (point) (point-at-bol))))
  1257. (value (buffer-substring-no-properties
  1258. contents-begin contents-end)))
  1259. (list 'comment-block
  1260. (nconc
  1261. (list :begin begin
  1262. :end end
  1263. :value value
  1264. :hiddenp hidden
  1265. :post-blank (count-lines pos-before-blank end))
  1266. (cadr keywords)))))))))
  1267. (defun org-element-comment-block-interpreter (comment-block contents)
  1268. "Interpret COMMENT-BLOCK element as Org syntax.
  1269. CONTENTS is nil."
  1270. (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
  1271. (org-remove-indentation (org-element-property :value comment-block))))
  1272. ;;;; Example Block
  1273. (defun org-element-example-block-parser (limit)
  1274. "Parse an example block.
  1275. LIMIT bounds the search.
  1276. Return a list whose CAR is `example-block' and CDR is a plist
  1277. containing `:begin', `:end', `:number-lines', `:preserve-indent',
  1278. `:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
  1279. `:switches', `:value' and `:post-blank' keywords."
  1280. (let ((case-fold-search t))
  1281. (if (not (save-excursion
  1282. (re-search-forward "^[ \t]*#\\+END_EXAMPLE" limit t)))
  1283. ;; Incomplete block: parse it as a comment.
  1284. (org-element-comment-parser limit)
  1285. (let ((contents-end (match-beginning 0)))
  1286. (save-excursion
  1287. (let* ((switches
  1288. (progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
  1289. (org-match-string-no-properties 1)))
  1290. ;; Switches analysis
  1291. (number-lines (cond ((not switches) nil)
  1292. ((string-match "-n\\>" switches) 'new)
  1293. ((string-match "+n\\>" switches) 'continued)))
  1294. (preserve-indent (and switches (string-match "-i\\>" switches)))
  1295. ;; Should labels be retained in (or stripped from) example
  1296. ;; blocks?
  1297. (retain-labels
  1298. (or (not switches)
  1299. (not (string-match "-r\\>" switches))
  1300. (and number-lines (string-match "-k\\>" switches))))
  1301. ;; What should code-references use - labels or
  1302. ;; line-numbers?
  1303. (use-labels
  1304. (or (not switches)
  1305. (and retain-labels (not (string-match "-k\\>" switches)))))
  1306. (label-fmt (and switches
  1307. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  1308. (match-string 1 switches)))
  1309. ;; Standard block parsing.
  1310. (keywords (org-element--collect-affiliated-keywords))
  1311. (begin (car keywords))
  1312. (contents-begin (progn (forward-line) (point)))
  1313. (hidden (org-invisible-p2))
  1314. (value (buffer-substring-no-properties contents-begin contents-end))
  1315. (pos-before-blank (progn (goto-char contents-end)
  1316. (forward-line)
  1317. (point)))
  1318. (end (progn (skip-chars-forward " \r\t\n" limit)
  1319. (if (eobp) (point) (point-at-bol)))))
  1320. (list 'example-block
  1321. (nconc
  1322. (list :begin begin
  1323. :end end
  1324. :value value
  1325. :switches switches
  1326. :number-lines number-lines
  1327. :preserve-indent preserve-indent
  1328. :retain-labels retain-labels
  1329. :use-labels use-labels
  1330. :label-fmt label-fmt
  1331. :hiddenp hidden
  1332. :post-blank (count-lines pos-before-blank end))
  1333. (cadr keywords)))))))))
  1334. (defun org-element-example-block-interpreter (example-block contents)
  1335. "Interpret EXAMPLE-BLOCK element as Org syntax.
  1336. CONTENTS is nil."
  1337. (let ((switches (org-element-property :switches example-block)))
  1338. (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
  1339. (org-remove-indentation
  1340. (org-element-property :value example-block))
  1341. "#+END_EXAMPLE")))
  1342. ;;;; Export Block
  1343. (defun org-element-export-block-parser (limit)
  1344. "Parse an export block.
  1345. LIMIT bounds the search.
  1346. Return a list whose CAR is `export-block' and CDR is a plist
  1347. containing `:begin', `:end', `:type', `:hiddenp', `:value' and
  1348. `:post-blank' keywords.
  1349. Assume point is at export-block beginning."
  1350. (let* ((case-fold-search t)
  1351. (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1352. (upcase (org-match-string-no-properties 1)))))
  1353. (if (not (save-excursion
  1354. (re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
  1355. ;; Incomplete block: parse it as a comment.
  1356. (org-element-comment-parser limit)
  1357. (let ((contents-end (match-beginning 0)))
  1358. (save-excursion
  1359. (let* ((keywords (org-element--collect-affiliated-keywords))
  1360. (begin (car keywords))
  1361. (contents-begin (progn (forward-line) (point)))
  1362. (hidden (org-invisible-p2))
  1363. (pos-before-blank (progn (goto-char contents-end)
  1364. (forward-line)
  1365. (point)))
  1366. (end (progn (skip-chars-forward " \r\t\n" limit)
  1367. (if (eobp) (point) (point-at-bol))))
  1368. (value (buffer-substring-no-properties contents-begin
  1369. contents-end)))
  1370. (list 'export-block
  1371. (nconc
  1372. (list :begin begin
  1373. :end end
  1374. :type type
  1375. :value value
  1376. :hiddenp hidden
  1377. :post-blank (count-lines pos-before-blank end))
  1378. (cadr keywords)))))))))
  1379. (defun org-element-export-block-interpreter (export-block contents)
  1380. "Interpret EXPORT-BLOCK element as Org syntax.
  1381. CONTENTS is nil."
  1382. (let ((type (org-element-property :type export-block)))
  1383. (concat (format "#+BEGIN_%s\n" type)
  1384. (org-element-property :value export-block)
  1385. (format "#+END_%s" type))))
  1386. ;;;; Fixed-width
  1387. (defun org-element-fixed-width-parser (limit)
  1388. "Parse a fixed-width section.
  1389. LIMIT bounds the search.
  1390. Return a list whose CAR is `fixed-width' and CDR is a plist
  1391. containing `:begin', `:end', `:value' and `:post-blank' keywords.
  1392. Assume point is at the beginning of the fixed-width area."
  1393. (save-excursion
  1394. (let* ((keywords (org-element--collect-affiliated-keywords))
  1395. (begin (car keywords))
  1396. value
  1397. (end-area
  1398. (progn
  1399. (while (and (< (point) limit)
  1400. (looking-at "[ \t]*:\\( \\|$\\)"))
  1401. ;; Accumulate text without starting colons.
  1402. (setq value
  1403. (concat value
  1404. (buffer-substring-no-properties
  1405. (match-end 0) (point-at-eol))
  1406. "\n"))
  1407. (forward-line))
  1408. (point)))
  1409. (end (progn (skip-chars-forward " \r\t\n" limit)
  1410. (if (eobp) (point) (point-at-bol)))))
  1411. (list 'fixed-width
  1412. (nconc
  1413. (list :begin begin
  1414. :end end
  1415. :value value
  1416. :post-blank (count-lines end-area end))
  1417. (cadr keywords))))))
  1418. (defun org-element-fixed-width-interpreter (fixed-width contents)
  1419. "Interpret FIXED-WIDTH element as Org syntax.
  1420. CONTENTS is nil."
  1421. (replace-regexp-in-string
  1422. "^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
  1423. ;;;; Horizontal Rule
  1424. (defun org-element-horizontal-rule-parser (limit)
  1425. "Parse an horizontal rule.
  1426. LIMIT bounds the search.
  1427. Return a list whose CAR is `horizontal-rule' and CDR is a plist
  1428. containing `:begin', `:end' and `:post-blank' keywords."
  1429. (save-excursion
  1430. (let* ((keywords (org-element--collect-affiliated-keywords))
  1431. (begin (car keywords))
  1432. (post-hr (progn (forward-line) (point)))
  1433. (end (progn (skip-chars-forward " \r\t\n" limit)
  1434. (if (eobp) (point) (point-at-bol)))))
  1435. (list 'horizontal-rule
  1436. (nconc
  1437. (list :begin begin
  1438. :end end
  1439. :post-blank (count-lines post-hr end))
  1440. (cadr keywords))))))
  1441. (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
  1442. "Interpret HORIZONTAL-RULE element as Org syntax.
  1443. CONTENTS is nil."
  1444. "-----")
  1445. ;;;; Keyword
  1446. (defun org-element-keyword-parser (limit)
  1447. "Parse a keyword at point.
  1448. LIMIT bounds the search.
  1449. Return a list whose CAR is `keyword' and CDR is a plist
  1450. containing `:key', `:value', `:begin', `:end' and `:post-blank'
  1451. keywords."
  1452. (save-excursion
  1453. (let* ((case-fold-search t)
  1454. (begin (point))
  1455. (key (progn (looking-at "[ \t]*#\\+\\(\\S-+\\):")
  1456. (upcase (org-match-string-no-properties 1))))
  1457. (value (org-trim (buffer-substring-no-properties
  1458. (match-end 0) (point-at-eol))))
  1459. (pos-before-blank (progn (forward-line) (point)))
  1460. (end (progn (skip-chars-forward " \r\t\n" limit)
  1461. (if (eobp) (point) (point-at-bol)))))
  1462. (list 'keyword
  1463. (list :key key
  1464. :value value
  1465. :begin begin
  1466. :end end
  1467. :post-blank (count-lines pos-before-blank end))))))
  1468. (defun org-element-keyword-interpreter (keyword contents)
  1469. "Interpret KEYWORD element as Org syntax.
  1470. CONTENTS is nil."
  1471. (format "#+%s: %s"
  1472. (org-element-property :key keyword)
  1473. (org-element-property :value keyword)))
  1474. ;;;; Latex Environment
  1475. (defun org-element-latex-environment-parser (limit)
  1476. "Parse a LaTeX environment.
  1477. LIMIT bounds the search.
  1478. Return a list whose CAR is `latex-environment' and CDR is a plist
  1479. containing `:begin', `:end', `:value' and `:post-blank'
  1480. keywords.
  1481. Assume point is at the beginning of the latex environment."
  1482. (save-excursion
  1483. (let* ((case-fold-search t)
  1484. (code-begin (point))
  1485. (keywords (org-element--collect-affiliated-keywords))
  1486. (begin (car keywords))
  1487. (env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
  1488. (regexp-quote (match-string 1))))
  1489. (code-end
  1490. (progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env) limit t)
  1491. (forward-line)
  1492. (point)))
  1493. (value (buffer-substring-no-properties code-begin code-end))
  1494. (end (progn (skip-chars-forward " \r\t\n" limit)
  1495. (if (eobp) (point) (point-at-bol)))))
  1496. (list 'latex-environment
  1497. (nconc
  1498. (list :begin begin
  1499. :end end
  1500. :value value
  1501. :post-blank (count-lines code-end end))
  1502. (cadr keywords))))))
  1503. (defun org-element-latex-environment-interpreter (latex-environment contents)
  1504. "Interpret LATEX-ENVIRONMENT element as Org syntax.
  1505. CONTENTS is nil."
  1506. (org-element-property :value latex-environment))
  1507. ;;;; Paragraph
  1508. (defun org-element-paragraph-parser (limit)
  1509. "Parse a paragraph.
  1510. LIMIT bounds the search.
  1511. Return a list whose CAR is `paragraph' and CDR is a plist
  1512. containing `:begin', `:end', `:contents-begin' and
  1513. `:contents-end' and `:post-blank' keywords.
  1514. Assume point is at the beginning of the paragraph."
  1515. (save-excursion
  1516. (let* ((contents-begin (point))
  1517. (keywords (org-element--collect-affiliated-keywords))
  1518. (begin (car keywords))
  1519. (before-blank
  1520. (progn (end-of-line)
  1521. (if (re-search-forward org-element-paragraph-separate
  1522. limit
  1523. 'm)
  1524. (goto-char (match-beginning 0))
  1525. (point))))
  1526. (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
  1527. (forward-line)
  1528. (point)))
  1529. (end (progn (skip-chars-forward " \r\t\n" limit)
  1530. (if (eobp) (point) (point-at-bol)))))
  1531. (list 'paragraph
  1532. ;; If paragraph has no affiliated keywords, it may not begin
  1533. ;; at beginning of line if it starts an item.
  1534. (nconc
  1535. (list :begin (if (cadr keywords) begin contents-begin)
  1536. :end end
  1537. :contents-begin contents-begin
  1538. :contents-end contents-end
  1539. :post-blank (count-lines before-blank end))
  1540. (cadr keywords))))))
  1541. (defun org-element-paragraph-interpreter (paragraph contents)
  1542. "Interpret PARAGRAPH element as Org syntax.
  1543. CONTENTS is the contents of the element."
  1544. contents)
  1545. ;;;; Planning
  1546. (defun org-element-planning-parser (limit)
  1547. "Parse a planning.
  1548. LIMIT bounds the search.
  1549. Return a list whose CAR is `planning' and CDR is a plist
  1550. containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
  1551. and `:post-blank' keywords."
  1552. (save-excursion
  1553. (let* ((case-fold-search nil)
  1554. (begin (point))
  1555. (post-blank (let ((before-blank (progn (forward-line) (point))))
  1556. (skip-chars-forward " \r\t\n" limit)
  1557. (unless (eobp) (beginning-of-line))
  1558. (count-lines before-blank (point))))
  1559. (end (point))
  1560. closed deadline scheduled)
  1561. (goto-char begin)
  1562. (while (re-search-forward org-keyword-time-not-clock-regexp
  1563. (line-end-position) t)
  1564. (goto-char (match-end 1))
  1565. (org-skip-whitespace)
  1566. (let ((time (buffer-substring-no-properties
  1567. (1+ (point)) (1- (match-end 0))))
  1568. (keyword (match-string 1)))
  1569. (cond ((equal keyword org-closed-string) (setq closed time))
  1570. ((equal keyword org-deadline-string) (setq deadline time))
  1571. (t (setq scheduled time)))))
  1572. (list 'planning
  1573. (list :closed closed
  1574. :deadline deadline
  1575. :scheduled scheduled
  1576. :begin begin
  1577. :end end
  1578. :post-blank post-blank)))))
  1579. (defun org-element-planning-interpreter (planning contents)
  1580. "Interpret PLANNING element as Org syntax.
  1581. CONTENTS is nil."
  1582. (mapconcat
  1583. 'identity
  1584. (delq nil
  1585. (list (let ((closed (org-element-property :closed planning)))
  1586. (when closed (concat org-closed-string " [" closed "]")))
  1587. (let ((deadline (org-element-property :deadline planning)))
  1588. (when deadline (concat org-deadline-string " <" deadline ">")))
  1589. (let ((scheduled (org-element-property :scheduled planning)))
  1590. (when scheduled
  1591. (concat org-scheduled-string " <" scheduled ">")))))
  1592. " "))
  1593. ;;;; Property Drawer
  1594. (defun org-element-property-drawer-parser (limit)
  1595. "Parse a property drawer.
  1596. LIMIT bounds the search.
  1597. Return a list whose CAR is `property-drawer' and CDR is a plist
  1598. containing `:begin', `:end', `:hiddenp', `:contents-begin',
  1599. `:contents-end', `:properties' and `:post-blank' keywords.
  1600. Assume point is at the beginning of the property drawer."
  1601. (save-excursion
  1602. (let ((case-fold-search t)
  1603. (begin (point))
  1604. (prop-begin (progn (forward-line) (point)))
  1605. (hidden (org-invisible-p2))
  1606. (properties
  1607. (let (val)
  1608. (while (not (looking-at "^[ \t]*:END:"))
  1609. (when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
  1610. (push (cons (org-match-string-no-properties 1)
  1611. (org-trim
  1612. (buffer-substring-no-properties
  1613. (match-end 0) (point-at-eol))))
  1614. val))
  1615. (forward-line))
  1616. val))
  1617. (prop-end (progn (re-search-forward "^[ \t]*:END:" limit t)
  1618. (point-at-bol)))
  1619. (pos-before-blank (progn (forward-line) (point)))
  1620. (end (progn (skip-chars-forward " \r\t\n" limit)
  1621. (if (eobp) (point) (point-at-bol)))))
  1622. (list 'property-drawer
  1623. (list :begin begin
  1624. :end end
  1625. :hiddenp hidden
  1626. :properties properties
  1627. :post-blank (count-lines pos-before-blank end))))))
  1628. (defun org-element-property-drawer-interpreter (property-drawer contents)
  1629. "Interpret PROPERTY-DRAWER element as Org syntax.
  1630. CONTENTS is nil."
  1631. (let ((props (org-element-property :properties property-drawer)))
  1632. (concat
  1633. ":PROPERTIES:\n"
  1634. (mapconcat (lambda (p)
  1635. (format org-property-format (format ":%s:" (car p)) (cdr p)))
  1636. (nreverse props) "\n")
  1637. "\n:END:")))
  1638. ;;;; Quote Section
  1639. (defun org-element-quote-section-parser (limit)
  1640. "Parse a quote section.
  1641. LIMIT bounds the search.
  1642. Return a list whose CAR is `quote-section' and CDR is a plist
  1643. containing `:begin', `:end', `:value' and `:post-blank' keywords.
  1644. Assume point is at beginning of the section."
  1645. (save-excursion
  1646. (let* ((begin (point))
  1647. (end (progn (org-with-limited-levels (outline-next-heading))
  1648. (point)))
  1649. (pos-before-blank (progn (skip-chars-backward " \r\t\n")
  1650. (forward-line)
  1651. (point)))
  1652. (value (buffer-substring-no-properties begin pos-before-blank)))
  1653. (list 'quote-section
  1654. (list :begin begin
  1655. :end end
  1656. :value value
  1657. :post-blank (count-lines pos-before-blank end))))))
  1658. (defun org-element-quote-section-interpreter (quote-section contents)
  1659. "Interpret QUOTE-SECTION element as Org syntax.
  1660. CONTENTS is nil."
  1661. (org-element-property :value quote-section))
  1662. ;;;; Src Block
  1663. (defun org-element-src-block-parser (limit)
  1664. "Parse a src block.
  1665. LIMIT bounds the search.
  1666. Return a list whose CAR is `src-block' and CDR is a plist
  1667. containing `:language', `:switches', `:parameters', `:begin',
  1668. `:end', `:hiddenp', `:number-lines', `:retain-labels',
  1669. `:use-labels', `:label-fmt', `:preserve-indent', `:value' and
  1670. `:post-blank' keywords.
  1671. Assume point is at the beginning of the block."
  1672. (let ((case-fold-search t))
  1673. (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC" limit t)))
  1674. ;; Incomplete block: parse it as a comment.
  1675. (org-element-comment-parser limit)
  1676. (let ((contents-end (match-beginning 0)))
  1677. (save-excursion
  1678. (let* ((keywords (org-element--collect-affiliated-keywords))
  1679. ;; Get beginning position.
  1680. (begin (car keywords))
  1681. ;; Get language as a string.
  1682. (language
  1683. (progn
  1684. (looking-at
  1685. (concat "^[ \t]*#\\+BEGIN_SRC"
  1686. "\\(?: +\\(\\S-+\\)\\)?"
  1687. "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
  1688. "\\(.*\\)[ \t]*$"))
  1689. (org-match-string-no-properties 1)))
  1690. ;; Get switches.
  1691. (switches (org-match-string-no-properties 2))
  1692. ;; Get parameters.
  1693. (parameters (org-match-string-no-properties 3))
  1694. ;; Switches analysis
  1695. (number-lines (cond ((not switches) nil)
  1696. ((string-match "-n\\>" switches) 'new)
  1697. ((string-match "+n\\>" switches) 'continued)))
  1698. (preserve-indent (and switches (string-match "-i\\>" switches)))
  1699. (label-fmt (and switches
  1700. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  1701. (match-string 1 switches)))
  1702. ;; Should labels be retained in (or stripped from)
  1703. ;; src blocks?
  1704. (retain-labels
  1705. (or (not switches)
  1706. (not (string-match "-r\\>" switches))
  1707. (and number-lines (string-match "-k\\>" switches))))
  1708. ;; What should code-references use - labels or
  1709. ;; line-numbers?
  1710. (use-labels
  1711. (or (not switches)
  1712. (and retain-labels (not (string-match "-k\\>" switches)))))
  1713. ;; Get visibility status.
  1714. (hidden (progn (forward-line) (org-invisible-p2)))
  1715. ;; Retrieve code.
  1716. (value (buffer-substring-no-properties (point) contents-end))
  1717. (pos-before-blank (progn (goto-char contents-end)
  1718. (forward-line)
  1719. (point)))
  1720. ;; Get position after ending blank lines.
  1721. (end (progn (skip-chars-forward " \r\t\n" limit)
  1722. (if (eobp) (point) (point-at-bol)))))
  1723. (list 'src-block
  1724. (nconc
  1725. (list :language language
  1726. :switches (and (org-string-nw-p switches)
  1727. (org-trim switches))
  1728. :parameters (and (org-string-nw-p parameters)
  1729. (org-trim parameters))
  1730. :begin begin
  1731. :end end
  1732. :number-lines number-lines
  1733. :preserve-indent preserve-indent
  1734. :retain-labels retain-labels
  1735. :use-labels use-labels
  1736. :label-fmt label-fmt
  1737. :hiddenp hidden
  1738. :value value
  1739. :post-blank (count-lines pos-before-blank end))
  1740. (cadr keywords)))))))))
  1741. (defun org-element-src-block-interpreter (src-block contents)
  1742. "Interpret SRC-BLOCK element as Org syntax.
  1743. CONTENTS is nil."
  1744. (let ((lang (org-element-property :language src-block))
  1745. (switches (org-element-property :switches src-block))
  1746. (params (org-element-property :parameters src-block))
  1747. (value (let ((val (org-element-property :value src-block)))
  1748. (cond
  1749. (org-src-preserve-indentation val)
  1750. ((zerop org-edit-src-content-indentation)
  1751. (org-remove-indentation val))
  1752. (t
  1753. (let ((ind (make-string
  1754. org-edit-src-content-indentation 32)))
  1755. (replace-regexp-in-string
  1756. "\\(^\\)[ \t]*\\S-" ind
  1757. (org-remove-indentation val) nil nil 1)))))))
  1758. (concat (format "#+BEGIN_SRC%s\n"
  1759. (concat (and lang (concat " " lang))
  1760. (and switches (concat " " switches))
  1761. (and params (concat " " params))))
  1762. value
  1763. "#+END_SRC")))
  1764. ;;;; Table
  1765. (defun org-element-table-parser (limit)
  1766. "Parse a table at point.
  1767. LIMIT bounds the search.
  1768. Return a list whose CAR is `table' and CDR is a plist containing
  1769. `:begin', `:end', `:tblfm', `:type', `:contents-begin',
  1770. `:contents-end', `:value' and `:post-blank' keywords.
  1771. Assume point is at the beginning of the table."
  1772. (save-excursion
  1773. (let* ((case-fold-search t)
  1774. (table-begin (point))
  1775. (type (if (org-at-table.el-p) 'table.el 'org))
  1776. (keywords (org-element--collect-affiliated-keywords))
  1777. (begin (car keywords))
  1778. (table-end (goto-char (marker-position (org-table-end t))))
  1779. (tblfm (let (acc)
  1780. (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
  1781. (push (org-match-string-no-properties 1) acc)
  1782. (forward-line))
  1783. acc))
  1784. (pos-before-blank (point))
  1785. (end (progn (skip-chars-forward " \r\t\n" limit)
  1786. (if (eobp) (point) (point-at-bol)))))
  1787. (list 'table
  1788. (nconc
  1789. (list :begin begin
  1790. :end end
  1791. :type type
  1792. :tblfm tblfm
  1793. ;; Only `org' tables have contents. `table.el' tables
  1794. ;; use a `:value' property to store raw table as
  1795. ;; a string.
  1796. :contents-begin (and (eq type 'org) table-begin)
  1797. :contents-end (and (eq type 'org) table-end)
  1798. :value (and (eq type 'table.el)
  1799. (buffer-substring-no-properties
  1800. table-begin table-end))
  1801. :post-blank (count-lines pos-before-blank end))
  1802. (cadr keywords))))))
  1803. (defun org-element-table-interpreter (table contents)
  1804. "Interpret TABLE element as Org syntax.
  1805. CONTENTS is nil."
  1806. (if (eq (org-element-property :type table) 'table.el)
  1807. (org-remove-indentation (org-element-property :value table))
  1808. (concat (with-temp-buffer (insert contents)
  1809. (org-table-align)
  1810. (buffer-string))
  1811. (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
  1812. (reverse (org-element-property :tblfm table))
  1813. "\n"))))
  1814. ;;;; Table Row
  1815. (defun org-element-table-row-parser (limit)
  1816. "Parse table row at point.
  1817. LIMIT bounds the search.
  1818. Return a list whose CAR is `table-row' and CDR is a plist
  1819. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1820. `:type' and `:post-blank' keywords."
  1821. (save-excursion
  1822. (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
  1823. (begin (point))
  1824. ;; A table rule has no contents. In that case, ensure
  1825. ;; CONTENTS-BEGIN matches CONTENTS-END.
  1826. (contents-begin (and (eq type 'standard)
  1827. (search-forward "|")
  1828. (point)))
  1829. (contents-end (and (eq type 'standard)
  1830. (progn
  1831. (end-of-line)
  1832. (skip-chars-backward " \t")
  1833. (point))))
  1834. (end (progn (forward-line) (point))))
  1835. (list 'table-row
  1836. (list :type type
  1837. :begin begin
  1838. :end end
  1839. :contents-begin contents-begin
  1840. :contents-end contents-end
  1841. :post-blank 0)))))
  1842. (defun org-element-table-row-interpreter (table-row contents)
  1843. "Interpret TABLE-ROW element as Org syntax.
  1844. CONTENTS is the contents of the table row."
  1845. (if (eq (org-element-property :type table-row) 'rule) "|-"
  1846. (concat "| " contents)))
  1847. ;;;; Verse Block
  1848. (defun org-element-verse-block-parser (limit)
  1849. "Parse a verse block.
  1850. LIMIT bounds the search.
  1851. Return a list whose CAR is `verse-block' and CDR is a plist
  1852. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1853. `:hiddenp' and `:post-blank' keywords.
  1854. Assume point is at beginning of the block."
  1855. (let ((case-fold-search t))
  1856. (if (not (save-excursion
  1857. (re-search-forward "^[ \t]*#\\+END_VERSE" limit t)))
  1858. ;; Incomplete block: parse it as a comment.
  1859. (org-element-comment-parser limit)
  1860. (let ((contents-end (match-beginning 0)))
  1861. (save-excursion
  1862. (let* ((keywords (org-element--collect-affiliated-keywords))
  1863. (begin (car keywords))
  1864. (hidden (progn (forward-line) (org-invisible-p2)))
  1865. (contents-begin (point))
  1866. (pos-before-blank (progn (goto-char contents-end)
  1867. (forward-line)
  1868. (point)))
  1869. (end (progn (skip-chars-forward " \r\t\n" limit)
  1870. (if (eobp) (point) (point-at-bol)))))
  1871. (list 'verse-block
  1872. (nconc
  1873. (list :begin begin
  1874. :end end
  1875. :contents-begin contents-begin
  1876. :contents-end contents-end
  1877. :hiddenp hidden
  1878. :post-blank (count-lines pos-before-blank end))
  1879. (cadr keywords)))))))))
  1880. (defun org-element-verse-block-interpreter (verse-block contents)
  1881. "Interpret VERSE-BLOCK element as Org syntax.
  1882. CONTENTS is verse block contents."
  1883. (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
  1884. ;;; Objects
  1885. ;;
  1886. ;; Unlike to elements, interstices can be found between objects.
  1887. ;; That's why, along with the parser, successor functions are provided
  1888. ;; for each object. Some objects share the same successor (i.e. `code'
  1889. ;; and `verbatim' objects).
  1890. ;;
  1891. ;; A successor must accept a single argument bounding the search. It
  1892. ;; will return either a cons cell whose CAR is the object's type, as
  1893. ;; a symbol, and CDR the position of its next occurrence, or nil.
  1894. ;;
  1895. ;; Successors follow the naming convention:
  1896. ;; org-element-NAME-successor, where NAME is the name of the
  1897. ;; successor, as defined in `org-element-all-successors'.
  1898. ;;
  1899. ;; Some object types (i.e. `emphasis') are recursive. Restrictions on
  1900. ;; object types they can contain will be specified in
  1901. ;; `org-element-object-restrictions'.
  1902. ;;
  1903. ;; Adding a new type of object is simple. Implement a successor,
  1904. ;; a parser, and an interpreter for it, all following the naming
  1905. ;; convention. Register type in `org-element-all-objects' and
  1906. ;; successor in `org-element-all-successors'. Maybe tweak
  1907. ;; restrictions about it, and that's it.
  1908. ;;;; Bold
  1909. (defun org-element-bold-parser ()
  1910. "Parse bold object at point.
  1911. Return a list whose CAR is `bold' and CDR is a plist with
  1912. `:begin', `:end', `:contents-begin' and `:contents-end' and
  1913. `:post-blank' keywords.
  1914. Assume point is at the first star marker."
  1915. (save-excursion
  1916. (unless (bolp) (backward-char 1))
  1917. (looking-at org-emph-re)
  1918. (let ((begin (match-beginning 2))
  1919. (contents-begin (match-beginning 4))
  1920. (contents-end (match-end 4))
  1921. (post-blank (progn (goto-char (match-end 2))
  1922. (skip-chars-forward " \t")))
  1923. (end (point)))
  1924. (list 'bold
  1925. (list :begin begin
  1926. :end end
  1927. :contents-begin contents-begin
  1928. :contents-end contents-end
  1929. :post-blank post-blank)))))
  1930. (defun org-element-bold-interpreter (bold contents)
  1931. "Interpret BOLD object as Org syntax.
  1932. CONTENTS is the contents of the object."
  1933. (format "*%s*" contents))
  1934. (defun org-element-text-markup-successor (limit)
  1935. "Search for the next text-markup object.
  1936. LIMIT bounds the search.
  1937. LIMIT bounds the search.
  1938. Return value is a cons cell whose CAR is a symbol among `bold',
  1939. `italic', `underline', `strike-through', `code' and `verbatim'
  1940. and CDR is beginning position."
  1941. (save-excursion
  1942. (unless (bolp) (backward-char))
  1943. (when (re-search-forward org-emph-re limit t)
  1944. (let ((marker (match-string 3)))
  1945. (cons (cond
  1946. ((equal marker "*") 'bold)
  1947. ((equal marker "/") 'italic)
  1948. ((equal marker "_") 'underline)
  1949. ((equal marker "+") 'strike-through)
  1950. ((equal marker "~") 'code)
  1951. ((equal marker "=") 'verbatim)
  1952. (t (error "Unknown marker at %d" (match-beginning 3))))
  1953. (match-beginning 2))))))
  1954. ;;;; Code
  1955. (defun org-element-code-parser ()
  1956. "Parse code object at point.
  1957. Return a list whose CAR is `code' and CDR is a plist with
  1958. `:value', `:begin', `:end' and `:post-blank' keywords.
  1959. Assume point is at the first tilde marker."
  1960. (save-excursion
  1961. (unless (bolp) (backward-char 1))
  1962. (looking-at org-emph-re)
  1963. (let ((begin (match-beginning 2))
  1964. (value (org-match-string-no-properties 4))
  1965. (post-blank (progn (goto-char (match-end 2))
  1966. (skip-chars-forward " \t")))
  1967. (end (point)))
  1968. (list 'code
  1969. (list :value value
  1970. :begin begin
  1971. :end end
  1972. :post-blank post-blank)))))
  1973. (defun org-element-code-interpreter (code contents)
  1974. "Interpret CODE object as Org syntax.
  1975. CONTENTS is nil."
  1976. (format "~%s~" (org-element-property :value code)))
  1977. ;;;; Entity
  1978. (defun org-element-entity-parser ()
  1979. "Parse entity at point.
  1980. Return a list whose CAR is `entity' and CDR a plist with
  1981. `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
  1982. `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
  1983. keywords.
  1984. Assume point is at the beginning of the entity."
  1985. (save-excursion
  1986. (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
  1987. (let* ((value (org-entity-get (match-string 1)))
  1988. (begin (match-beginning 0))
  1989. (bracketsp (string= (match-string 2) "{}"))
  1990. (post-blank (progn (goto-char (match-end 1))
  1991. (when bracketsp (forward-char 2))
  1992. (skip-chars-forward " \t")))
  1993. (end (point)))
  1994. (list 'entity
  1995. (list :name (car value)
  1996. :latex (nth 1 value)
  1997. :latex-math-p (nth 2 value)
  1998. :html (nth 3 value)
  1999. :ascii (nth 4 value)
  2000. :latin1 (nth 5 value)
  2001. :utf-8 (nth 6 value)
  2002. :begin begin
  2003. :end end
  2004. :use-brackets-p bracketsp
  2005. :post-blank post-blank)))))
  2006. (defun org-element-entity-interpreter (entity contents)
  2007. "Interpret ENTITY object as Org syntax.
  2008. CONTENTS is nil."
  2009. (concat "\\"
  2010. (org-element-property :name entity)
  2011. (when (org-element-property :use-brackets-p entity) "{}")))
  2012. (defun org-element-latex-or-entity-successor (limit)
  2013. "Search for the next latex-fragment or entity object.
  2014. LIMIT bounds the search.
  2015. Return value is a cons cell whose CAR is `entity' or
  2016. `latex-fragment' and CDR is beginning position."
  2017. (save-excursion
  2018. (let ((matchers (plist-get org-format-latex-options :matchers))
  2019. ;; ENTITY-RE matches both LaTeX commands and Org entities.
  2020. (entity-re
  2021. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
  2022. (when (re-search-forward
  2023. (concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
  2024. matchers "\\|")
  2025. "\\|" entity-re)
  2026. limit t)
  2027. (goto-char (match-beginning 0))
  2028. (if (looking-at entity-re)
  2029. ;; Determine if it's a real entity or a LaTeX command.
  2030. (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
  2031. (match-beginning 0))
  2032. ;; No entity nor command: point is at a LaTeX fragment.
  2033. ;; Determine its type to get the correct beginning position.
  2034. (cons 'latex-fragment
  2035. (catch 'return
  2036. (mapc (lambda (e)
  2037. (when (looking-at (nth 1 (assoc e org-latex-regexps)))
  2038. (throw 'return
  2039. (match-beginning
  2040. (nth 2 (assoc e org-latex-regexps))))))
  2041. matchers)
  2042. (point))))))))
  2043. ;;;; Export Snippet
  2044. (defun org-element-export-snippet-parser ()
  2045. "Parse export snippet at point.
  2046. Return a list whose CAR is `export-snippet' and CDR a plist with
  2047. `:begin', `:end', `:back-end', `:value' and `:post-blank' as
  2048. keywords.
  2049. Assume point is at the beginning of the snippet."
  2050. (save-excursion
  2051. (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
  2052. (let* ((begin (match-beginning 0))
  2053. (back-end (org-match-string-no-properties 1))
  2054. (value (buffer-substring-no-properties
  2055. (point)
  2056. (progn (re-search-forward "@@" nil t) (match-beginning 0))))
  2057. (post-blank (skip-chars-forward " \t"))
  2058. (end (point)))
  2059. (list 'export-snippet
  2060. (list :back-end back-end
  2061. :value value
  2062. :begin begin
  2063. :end end
  2064. :post-blank post-blank)))))
  2065. (defun org-element-export-snippet-interpreter (export-snippet contents)
  2066. "Interpret EXPORT-SNIPPET object as Org syntax.
  2067. CONTENTS is nil."
  2068. (format "@@%s:%s@@"
  2069. (org-element-property :back-end export-snippet)
  2070. (org-element-property :value export-snippet)))
  2071. (defun org-element-export-snippet-successor (limit)
  2072. "Search for the next export-snippet object.
  2073. LIMIT bounds the search.
  2074. Return value is a cons cell whose CAR is `export-snippet' and CDR
  2075. its beginning position."
  2076. (save-excursion
  2077. (let (beg)
  2078. (when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
  2079. (setq beg (match-beginning 0))
  2080. (re-search-forward "@@" limit t))
  2081. (cons 'export-snippet beg)))))
  2082. ;;;; Footnote Reference
  2083. (defun org-element-footnote-reference-parser ()
  2084. "Parse footnote reference at point.
  2085. Return a list whose CAR is `footnote-reference' and CDR a plist
  2086. with `:label', `:type', `:inline-definition', `:begin', `:end'
  2087. and `:post-blank' as keywords."
  2088. (save-excursion
  2089. (looking-at org-footnote-re)
  2090. (let* ((begin (point))
  2091. (label (or (org-match-string-no-properties 2)
  2092. (org-match-string-no-properties 3)
  2093. (and (match-string 1)
  2094. (concat "fn:" (org-match-string-no-properties 1)))))
  2095. (type (if (or (not label) (match-string 1)) 'inline 'standard))
  2096. (inner-begin (match-end 0))
  2097. (inner-end
  2098. (let ((count 1))
  2099. (forward-char)
  2100. (while (and (> count 0) (re-search-forward "[][]" nil t))
  2101. (if (equal (match-string 0) "[") (incf count) (decf count)))
  2102. (1- (point))))
  2103. (post-blank (progn (goto-char (1+ inner-end))
  2104. (skip-chars-forward " \t")))
  2105. (end (point))
  2106. (footnote-reference
  2107. (list 'footnote-reference
  2108. (list :label label
  2109. :type type
  2110. :begin begin
  2111. :end end
  2112. :post-blank post-blank))))
  2113. (org-element-put-property
  2114. footnote-reference :inline-definition
  2115. (and (eq type 'inline)
  2116. (org-element-parse-secondary-string
  2117. (buffer-substring inner-begin inner-end)
  2118. (org-element-restriction 'footnote-reference)
  2119. footnote-reference))))))
  2120. (defun org-element-footnote-reference-interpreter (footnote-reference contents)
  2121. "Interpret FOOTNOTE-REFERENCE object as Org syntax.
  2122. CONTENTS is nil."
  2123. (let ((label (or (org-element-property :label footnote-reference) "fn:"))
  2124. (def
  2125. (let ((inline-def
  2126. (org-element-property :inline-definition footnote-reference)))
  2127. (if (not inline-def) ""
  2128. (concat ":" (org-element-interpret-data inline-def))))))
  2129. (format "[%s]" (concat label def))))
  2130. (defun org-element-footnote-reference-successor (limit)
  2131. "Search for the next footnote-reference object.
  2132. LIMIT bounds the search.
  2133. Return value is a cons cell whose CAR is `footnote-reference' and
  2134. CDR is beginning position."
  2135. (save-excursion
  2136. (catch 'exit
  2137. (while (re-search-forward org-footnote-re limit t)
  2138. (save-excursion
  2139. (let ((beg (match-beginning 0))
  2140. (count 1))
  2141. (backward-char)
  2142. (while (re-search-forward "[][]" limit t)
  2143. (if (equal (match-string 0) "[") (incf count) (decf count))
  2144. (when (zerop count)
  2145. (throw 'exit (cons 'footnote-reference beg))))))))))
  2146. ;;;; Inline Babel Call
  2147. (defun org-element-inline-babel-call-parser ()
  2148. "Parse inline babel call at point.
  2149. Return a list whose CAR is `inline-babel-call' and CDR a plist
  2150. with `:begin', `:end', `:info' and `:post-blank' as keywords.
  2151. Assume point is at the beginning of the babel call."
  2152. (save-excursion
  2153. (unless (bolp) (backward-char))
  2154. (looking-at org-babel-inline-lob-one-liner-regexp)
  2155. (let ((info (save-match-data (org-babel-lob-get-info)))
  2156. (begin (match-end 1))
  2157. (post-blank (progn (goto-char (match-end 0))
  2158. (skip-chars-forward " \t")))
  2159. (end (point)))
  2160. (list 'inline-babel-call
  2161. (list :begin begin
  2162. :end end
  2163. :info info
  2164. :post-blank post-blank)))))
  2165. (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
  2166. "Interpret INLINE-BABEL-CALL object as Org syntax.
  2167. CONTENTS is nil."
  2168. (let* ((babel-info (org-element-property :info inline-babel-call))
  2169. (main-source (car babel-info))
  2170. (post-options (nth 1 babel-info)))
  2171. (concat "call_"
  2172. (if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
  2173. ;; Remove redundant square brackets.
  2174. (replace-match
  2175. (match-string 1 main-source) nil nil main-source)
  2176. main-source)
  2177. (and post-options (format "[%s]" post-options)))))
  2178. (defun org-element-inline-babel-call-successor (limit)
  2179. "Search for the next inline-babel-call object.
  2180. LIMIT bounds the search.
  2181. Return value is a cons cell whose CAR is `inline-babel-call' and
  2182. CDR is beginning position."
  2183. (save-excursion
  2184. ;; Use a simplified version of
  2185. ;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
  2186. (when (re-search-forward
  2187. "\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
  2188. limit t)
  2189. (cons 'inline-babel-call (match-beginning 0)))))
  2190. ;;;; Inline Src Block
  2191. (defun org-element-inline-src-block-parser ()
  2192. "Parse inline source block at point.
  2193. LIMIT bounds the search.
  2194. Return a list whose CAR is `inline-src-block' and CDR a plist
  2195. with `:begin', `:end', `:language', `:value', `:parameters' and
  2196. `:post-blank' as keywords.
  2197. Assume point is at the beginning of the inline src block."
  2198. (save-excursion
  2199. (unless (bolp) (backward-char))
  2200. (looking-at org-babel-inline-src-block-regexp)
  2201. (let ((begin (match-beginning 1))
  2202. (language (org-match-string-no-properties 2))
  2203. (parameters (org-match-string-no-properties 4))
  2204. (value (org-match-string-no-properties 5))
  2205. (post-blank (progn (goto-char (match-end 0))
  2206. (skip-chars-forward " \t")))
  2207. (end (point)))
  2208. (list 'inline-src-block
  2209. (list :language language
  2210. :value value
  2211. :parameters parameters
  2212. :begin begin
  2213. :end end
  2214. :post-blank post-blank)))))
  2215. (defun org-element-inline-src-block-interpreter (inline-src-block contents)
  2216. "Interpret INLINE-SRC-BLOCK object as Org syntax.
  2217. CONTENTS is nil."
  2218. (let ((language (org-element-property :language inline-src-block))
  2219. (arguments (org-element-property :parameters inline-src-block))
  2220. (body (org-element-property :value inline-src-block)))
  2221. (format "src_%s%s{%s}"
  2222. language
  2223. (if arguments (format "[%s]" arguments) "")
  2224. body)))
  2225. (defun org-element-inline-src-block-successor (limit)
  2226. "Search for the next inline-babel-call element.
  2227. LIMIT bounds the search.
  2228. Return value is a cons cell whose CAR is `inline-babel-call' and
  2229. CDR is beginning position."
  2230. (save-excursion
  2231. (when (re-search-forward org-babel-inline-src-block-regexp limit t)
  2232. (cons 'inline-src-block (match-beginning 1)))))
  2233. ;;;; Italic
  2234. (defun org-element-italic-parser ()
  2235. "Parse italic object at point.
  2236. Return a list whose CAR is `italic' and CDR is a plist with
  2237. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2238. `:post-blank' keywords.
  2239. Assume point is at the first slash marker."
  2240. (save-excursion
  2241. (unless (bolp) (backward-char 1))
  2242. (looking-at org-emph-re)
  2243. (let ((begin (match-beginning 2))
  2244. (contents-begin (match-beginning 4))
  2245. (contents-end (match-end 4))
  2246. (post-blank (progn (goto-char (match-end 2))
  2247. (skip-chars-forward " \t")))
  2248. (end (point)))
  2249. (list 'italic
  2250. (list :begin begin
  2251. :end end
  2252. :contents-begin contents-begin
  2253. :contents-end contents-end
  2254. :post-blank post-blank)))))
  2255. (defun org-element-italic-interpreter (italic contents)
  2256. "Interpret ITALIC object as Org syntax.
  2257. CONTENTS is the contents of the object."
  2258. (format "/%s/" contents))
  2259. ;;;; Latex Fragment
  2260. (defun org-element-latex-fragment-parser ()
  2261. "Parse latex fragment at point.
  2262. Return a list whose CAR is `latex-fragment' and CDR a plist with
  2263. `:value', `:begin', `:end', and `:post-blank' as keywords.
  2264. Assume point is at the beginning of the latex fragment."
  2265. (save-excursion
  2266. (let* ((begin (point))
  2267. (substring-match
  2268. (catch 'exit
  2269. (mapc (lambda (e)
  2270. (let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
  2271. (when (or (looking-at latex-regexp)
  2272. (and (not (bobp))
  2273. (save-excursion
  2274. (backward-char)
  2275. (looking-at latex-regexp))))
  2276. (throw 'exit (nth 2 (assoc e org-latex-regexps))))))
  2277. (plist-get org-format-latex-options :matchers))
  2278. ;; None found: it's a macro.
  2279. (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
  2280. 0))
  2281. (value (match-string-no-properties substring-match))
  2282. (post-blank (progn (goto-char (match-end substring-match))
  2283. (skip-chars-forward " \t")))
  2284. (end (point)))
  2285. (list 'latex-fragment
  2286. (list :value value
  2287. :begin begin
  2288. :end end
  2289. :post-blank post-blank)))))
  2290. (defun org-element-latex-fragment-interpreter (latex-fragment contents)
  2291. "Interpret LATEX-FRAGMENT object as Org syntax.
  2292. CONTENTS is nil."
  2293. (org-element-property :value latex-fragment))
  2294. ;;;; Line Break
  2295. (defun org-element-line-break-parser ()
  2296. "Parse line break at point.
  2297. Return a list whose CAR is `line-break', and CDR a plist with
  2298. `:begin', `:end' and `:post-blank' keywords.
  2299. Assume point is at the beginning of the line break."
  2300. (list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
  2301. (defun org-element-line-break-interpreter (line-break contents)
  2302. "Interpret LINE-BREAK object as Org syntax.
  2303. CONTENTS is nil."
  2304. "\\\\")
  2305. (defun org-element-line-break-successor (limit)
  2306. "Search for the next line-break object.
  2307. LIMIT bounds the search.
  2308. Return value is a cons cell whose CAR is `line-break' and CDR is
  2309. beginning position."
  2310. (save-excursion
  2311. (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
  2312. (goto-char (match-beginning 1)))))
  2313. ;; A line break can only happen on a non-empty line.
  2314. (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
  2315. (cons 'line-break beg)))))
  2316. ;;;; Link
  2317. (defun org-element-link-parser ()
  2318. "Parse link at point.
  2319. Return a list whose CAR is `link' and CDR a plist with `:type',
  2320. `:path', `:raw-link', `:begin', `:end', `:contents-begin',
  2321. `:contents-end' and `:post-blank' as keywords.
  2322. Assume point is at the beginning of the link."
  2323. (save-excursion
  2324. (let ((begin (point))
  2325. end contents-begin contents-end link-end post-blank path type
  2326. raw-link link)
  2327. (cond
  2328. ;; Type 1: Text targeted from a radio target.
  2329. ((and org-target-link-regexp (looking-at org-target-link-regexp))
  2330. (setq type "radio"
  2331. link-end (match-end 0)
  2332. path (org-match-string-no-properties 0)))
  2333. ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
  2334. ((looking-at org-bracket-link-regexp)
  2335. (setq contents-begin (match-beginning 3)
  2336. contents-end (match-end 3)
  2337. link-end (match-end 0)
  2338. ;; RAW-LINK is the original link.
  2339. raw-link (org-match-string-no-properties 1)
  2340. link (org-translate-link
  2341. (org-link-expand-abbrev
  2342. (org-link-unescape raw-link))))
  2343. ;; Determine TYPE of link and set PATH accordingly.
  2344. (cond
  2345. ;; File type.
  2346. ((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
  2347. (setq type "file" path link))
  2348. ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
  2349. ((string-match org-link-re-with-space3 link)
  2350. (setq type (match-string 1 link) path (match-string 2 link)))
  2351. ;; Id type: PATH is the id.
  2352. ((string-match "^id:\\([-a-f0-9]+\\)" link)
  2353. (setq type "id" path (match-string 1 link)))
  2354. ;; Code-ref type: PATH is the name of the reference.
  2355. ((string-match "^(\\(.*\\))$" link)
  2356. (setq type "coderef" path (match-string 1 link)))
  2357. ;; Custom-id type: PATH is the name of the custom id.
  2358. ((= (aref link 0) ?#)
  2359. (setq type "custom-id" path (substring link 1)))
  2360. ;; Fuzzy type: Internal link either matches a target, an
  2361. ;; headline name or nothing. PATH is the target or
  2362. ;; headline's name.
  2363. (t (setq type "fuzzy" path link))))
  2364. ;; Type 3: Plain link, i.e. http://orgmode.org
  2365. ((looking-at org-plain-link-re)
  2366. (setq raw-link (org-match-string-no-properties 0)
  2367. type (org-match-string-no-properties 1)
  2368. path (org-match-string-no-properties 2)
  2369. link-end (match-end 0)))
  2370. ;; Type 4: Angular link, i.e. <http://orgmode.org>
  2371. ((looking-at org-angle-link-re)
  2372. (setq raw-link (buffer-substring-no-properties
  2373. (match-beginning 1) (match-end 2))
  2374. type (org-match-string-no-properties 1)
  2375. path (org-match-string-no-properties 2)
  2376. link-end (match-end 0))))
  2377. ;; In any case, deduce end point after trailing white space from
  2378. ;; LINK-END variable.
  2379. (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
  2380. end (point))
  2381. (list 'link
  2382. (list :type type
  2383. :path path
  2384. :raw-link (or raw-link path)
  2385. :begin begin
  2386. :end end
  2387. :contents-begin contents-begin
  2388. :contents-end contents-end
  2389. :post-blank post-blank)))))
  2390. (defun org-element-link-interpreter (link contents)
  2391. "Interpret LINK object as Org syntax.
  2392. CONTENTS is the contents of the object, or nil."
  2393. (let ((type (org-element-property :type link))
  2394. (raw-link (org-element-property :raw-link link)))
  2395. (if (string= type "radio") raw-link
  2396. (format "[[%s]%s]"
  2397. raw-link
  2398. (if contents (format "[%s]" contents) "")))))
  2399. (defun org-element-link-successor (limit)
  2400. "Search for the next link object.
  2401. LIMIT bounds the search.
  2402. Return value is a cons cell whose CAR is `link' and CDR is
  2403. beginning position."
  2404. (save-excursion
  2405. (let ((link-regexp
  2406. (if (not org-target-link-regexp) org-any-link-re
  2407. (concat org-any-link-re "\\|" org-target-link-regexp))))
  2408. (when (re-search-forward link-regexp limit t)
  2409. (cons 'link (match-beginning 0))))))
  2410. ;;;; Macro
  2411. (defun org-element-macro-parser ()
  2412. "Parse macro at point.
  2413. Return a list whose CAR is `macro' and CDR a plist with `:key',
  2414. `:args', `:begin', `:end', `:value' and `:post-blank' as
  2415. keywords.
  2416. Assume point is at the macro."
  2417. (save-excursion
  2418. (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
  2419. (let ((begin (point))
  2420. (key (downcase (org-match-string-no-properties 1)))
  2421. (value (org-match-string-no-properties 0))
  2422. (post-blank (progn (goto-char (match-end 0))
  2423. (skip-chars-forward " \t")))
  2424. (end (point))
  2425. (args (let ((args (org-match-string-no-properties 3)) args2)
  2426. (when args
  2427. (setq args (org-split-string args ","))
  2428. (while args
  2429. (while (string-match "\\\\\\'" (car args))
  2430. ;; Repair bad splits.
  2431. (setcar (cdr args) (concat (substring (car args) 0 -1)
  2432. "," (nth 1 args)))
  2433. (pop args))
  2434. (push (pop args) args2))
  2435. (mapcar 'org-trim (nreverse args2))))))
  2436. (list 'macro
  2437. (list :key key
  2438. :value value
  2439. :args args
  2440. :begin begin
  2441. :end end
  2442. :post-blank post-blank)))))
  2443. (defun org-element-macro-interpreter (macro contents)
  2444. "Interpret MACRO object as Org syntax.
  2445. CONTENTS is nil."
  2446. (org-element-property :value macro))
  2447. (defun org-element-macro-successor (limit)
  2448. "Search for the next macro object.
  2449. LIMIT bounds the search.
  2450. Return value is cons cell whose CAR is `macro' and CDR is
  2451. beginning position."
  2452. (save-excursion
  2453. (when (re-search-forward
  2454. "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
  2455. limit t)
  2456. (cons 'macro (match-beginning 0)))))
  2457. ;;;; Radio-target
  2458. (defun org-element-radio-target-parser ()
  2459. "Parse radio target at point.
  2460. Return a list whose CAR is `radio-target' and CDR a plist with
  2461. `:begin', `:end', `:contents-begin', `:contents-end', `:value'
  2462. and `:post-blank' as keywords.
  2463. Assume point is at the radio target."
  2464. (save-excursion
  2465. (looking-at org-radio-target-regexp)
  2466. (let ((begin (point))
  2467. (contents-begin (match-beginning 1))
  2468. (contents-end (match-end 1))
  2469. (value (org-match-string-no-properties 1))
  2470. (post-blank (progn (goto-char (match-end 0))
  2471. (skip-chars-forward " \t")))
  2472. (end (point)))
  2473. (list 'radio-target
  2474. (list :begin begin
  2475. :end end
  2476. :contents-begin contents-begin
  2477. :contents-end contents-end
  2478. :post-blank post-blank
  2479. :value value)))))
  2480. (defun org-element-radio-target-interpreter (target contents)
  2481. "Interpret TARGET object as Org syntax.
  2482. CONTENTS is the contents of the object."
  2483. (concat "<<<" contents ">>>"))
  2484. (defun org-element-radio-target-successor (limit)
  2485. "Search for the next radio-target object.
  2486. LIMIT bounds the search.
  2487. Return value is a cons cell whose CAR is `radio-target' and CDR
  2488. is beginning position."
  2489. (save-excursion
  2490. (when (re-search-forward org-radio-target-regexp limit t)
  2491. (cons 'radio-target (match-beginning 0)))))
  2492. ;;;; Statistics Cookie
  2493. (defun org-element-statistics-cookie-parser ()
  2494. "Parse statistics cookie at point.
  2495. Return a list whose CAR is `statistics-cookie', and CDR a plist
  2496. with `:begin', `:end', `:value' and `:post-blank' keywords.
  2497. Assume point is at the beginning of the statistics-cookie."
  2498. (save-excursion
  2499. (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
  2500. (let* ((begin (point))
  2501. (value (buffer-substring-no-properties
  2502. (match-beginning 0) (match-end 0)))
  2503. (post-blank (progn (goto-char (match-end 0))
  2504. (skip-chars-forward " \t")))
  2505. (end (point)))
  2506. (list 'statistics-cookie
  2507. (list :begin begin
  2508. :end end
  2509. :value value
  2510. :post-blank post-blank)))))
  2511. (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
  2512. "Interpret STATISTICS-COOKIE object as Org syntax.
  2513. CONTENTS is nil."
  2514. (org-element-property :value statistics-cookie))
  2515. (defun org-element-statistics-cookie-successor (limit)
  2516. "Search for the next statistics cookie object.
  2517. LIMIT bounds the search.
  2518. Return value is a cons cell whose CAR is `statistics-cookie' and
  2519. CDR is beginning position."
  2520. (save-excursion
  2521. (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
  2522. (cons 'statistics-cookie (match-beginning 0)))))
  2523. ;;;; Strike-Through
  2524. (defun org-element-strike-through-parser ()
  2525. "Parse strike-through object at point.
  2526. Return a list whose CAR is `strike-through' and CDR is a plist
  2527. with `:begin', `:end', `:contents-begin' and `:contents-end' and
  2528. `:post-blank' keywords.
  2529. Assume point is at the first plus sign marker."
  2530. (save-excursion
  2531. (unless (bolp) (backward-char 1))
  2532. (looking-at org-emph-re)
  2533. (let ((begin (match-beginning 2))
  2534. (contents-begin (match-beginning 4))
  2535. (contents-end (match-end 4))
  2536. (post-blank (progn (goto-char (match-end 2))
  2537. (skip-chars-forward " \t")))
  2538. (end (point)))
  2539. (list 'strike-through
  2540. (list :begin begin
  2541. :end end
  2542. :contents-begin contents-begin
  2543. :contents-end contents-end
  2544. :post-blank post-blank)))))
  2545. (defun org-element-strike-through-interpreter (strike-through contents)
  2546. "Interpret STRIKE-THROUGH object as Org syntax.
  2547. CONTENTS is the contents of the object."
  2548. (format "+%s+" contents))
  2549. ;;;; Subscript
  2550. (defun org-element-subscript-parser ()
  2551. "Parse subscript at point.
  2552. Return a list whose CAR is `subscript' and CDR a plist with
  2553. `:begin', `:end', `:contents-begin', `:contents-end',
  2554. `:use-brackets-p' and `:post-blank' as keywords.
  2555. Assume point is at the underscore."
  2556. (save-excursion
  2557. (unless (bolp) (backward-char))
  2558. (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
  2559. t
  2560. (not (looking-at org-match-substring-regexp))))
  2561. (begin (match-beginning 2))
  2562. (contents-begin (or (match-beginning 5)
  2563. (match-beginning 3)))
  2564. (contents-end (or (match-end 5) (match-end 3)))
  2565. (post-blank (progn (goto-char (match-end 0))
  2566. (skip-chars-forward " \t")))
  2567. (end (point)))
  2568. (list 'subscript
  2569. (list :begin begin
  2570. :end end
  2571. :use-brackets-p bracketsp
  2572. :contents-begin contents-begin
  2573. :contents-end contents-end
  2574. :post-blank post-blank)))))
  2575. (defun org-element-subscript-interpreter (subscript contents)
  2576. "Interpret SUBSCRIPT object as Org syntax.
  2577. CONTENTS is the contents of the object."
  2578. (format
  2579. (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
  2580. contents))
  2581. (defun org-element-sub/superscript-successor (limit)
  2582. "Search for the next sub/superscript object.
  2583. LIMIT bounds the search.
  2584. Return value is a cons cell whose CAR is either `subscript' or
  2585. `superscript' and CDR is beginning position."
  2586. (save-excursion
  2587. (when (re-search-forward org-match-substring-regexp limit t)
  2588. (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
  2589. (match-beginning 2)))))
  2590. ;;;; Superscript
  2591. (defun org-element-superscript-parser ()
  2592. "Parse superscript at point.
  2593. Return a list whose CAR is `superscript' and CDR a plist with
  2594. `:begin', `:end', `:contents-begin', `:contents-end',
  2595. `:use-brackets-p' and `:post-blank' as keywords.
  2596. Assume point is at the caret."
  2597. (save-excursion
  2598. (unless (bolp) (backward-char))
  2599. (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
  2600. (not (looking-at org-match-substring-regexp))))
  2601. (begin (match-beginning 2))
  2602. (contents-begin (or (match-beginning 5)
  2603. (match-beginning 3)))
  2604. (contents-end (or (match-end 5) (match-end 3)))
  2605. (post-blank (progn (goto-char (match-end 0))
  2606. (skip-chars-forward " \t")))
  2607. (end (point)))
  2608. (list 'superscript
  2609. (list :begin begin
  2610. :end end
  2611. :use-brackets-p bracketsp
  2612. :contents-begin contents-begin
  2613. :contents-end contents-end
  2614. :post-blank post-blank)))))
  2615. (defun org-element-superscript-interpreter (superscript contents)
  2616. "Interpret SUPERSCRIPT object as Org syntax.
  2617. CONTENTS is the contents of the object."
  2618. (format
  2619. (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
  2620. contents))
  2621. ;;;; Table Cell
  2622. (defun org-element-table-cell-parser ()
  2623. "Parse table cell at point.
  2624. Return a list whose CAR is `table-cell' and CDR is a plist
  2625. containing `:begin', `:end', `:contents-begin', `:contents-end'
  2626. and `:post-blank' keywords."
  2627. (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
  2628. (let* ((begin (match-beginning 0))
  2629. (end (match-end 0))
  2630. (contents-begin (match-beginning 1))
  2631. (contents-end (match-end 1)))
  2632. (list 'table-cell
  2633. (list :begin begin
  2634. :end end
  2635. :contents-begin contents-begin
  2636. :contents-end contents-end
  2637. :post-blank 0))))
  2638. (defun org-element-table-cell-interpreter (table-cell contents)
  2639. "Interpret TABLE-CELL element as Org syntax.
  2640. CONTENTS is the contents of the cell, or nil."
  2641. (concat " " contents " |"))
  2642. (defun org-element-table-cell-successor (limit)
  2643. "Search for the next table-cell object.
  2644. LIMIT bounds the search.
  2645. Return value is a cons cell whose CAR is `table-cell' and CDR is
  2646. beginning position."
  2647. (when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
  2648. ;;;; Target
  2649. (defun org-element-target-parser ()
  2650. "Parse target at point.
  2651. Return a list whose CAR is `target' and CDR a plist with
  2652. `:begin', `:end', `:value' and `:post-blank' as keywords.
  2653. Assume point is at the target."
  2654. (save-excursion
  2655. (looking-at org-target-regexp)
  2656. (let ((begin (point))
  2657. (value (org-match-string-no-properties 1))
  2658. (post-blank (progn (goto-char (match-end 0))
  2659. (skip-chars-forward " \t")))
  2660. (end (point)))
  2661. (list 'target
  2662. (list :begin begin
  2663. :end end
  2664. :value value
  2665. :post-blank post-blank)))))
  2666. (defun org-element-target-interpreter (target contents)
  2667. "Interpret TARGET object as Org syntax.
  2668. CONTENTS is nil."
  2669. (format "<<%s>>" (org-element-property :value target)))
  2670. (defun org-element-target-successor (limit)
  2671. "Search for the next target object.
  2672. LIMIT bounds the search.
  2673. Return value is a cons cell whose CAR is `target' and CDR is
  2674. beginning position."
  2675. (save-excursion
  2676. (when (re-search-forward org-target-regexp limit t)
  2677. (cons 'target (match-beginning 0)))))
  2678. ;;;; Timestamp
  2679. (defun org-element-timestamp-parser ()
  2680. "Parse time stamp at point.
  2681. Return a list whose CAR is `timestamp', and CDR a plist with
  2682. `:type', `:begin', `:end', `:value' and `:post-blank' keywords.
  2683. Assume point is at the beginning of the timestamp."
  2684. (save-excursion
  2685. (let* ((begin (point))
  2686. (activep (eq (char-after) ?<))
  2687. (main-value
  2688. (progn
  2689. (looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
  2690. (match-string-no-properties 1)))
  2691. (range-end (match-string-no-properties 3))
  2692. (type (cond ((match-string 2) 'diary)
  2693. ((and activep range-end) 'active-range)
  2694. (activep 'active)
  2695. (range-end 'inactive-range)
  2696. (t 'inactive)))
  2697. (post-blank (progn (goto-char (match-end 0))
  2698. (skip-chars-forward " \t")))
  2699. (end (point)))
  2700. (list 'timestamp
  2701. (list :type type
  2702. :value main-value
  2703. :range-end range-end
  2704. :begin begin
  2705. :end end
  2706. :post-blank post-blank)))))
  2707. (defun org-element-timestamp-interpreter (timestamp contents)
  2708. "Interpret TIMESTAMP object as Org syntax.
  2709. CONTENTS is nil."
  2710. (let ((type (org-element-property :type timestamp) ))
  2711. (concat
  2712. (format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>")
  2713. (org-element-property :value timestamp))
  2714. (let ((range-end (org-element-property :range-end timestamp)))
  2715. (when range-end
  2716. (concat "--"
  2717. (format (if (eq type 'inactive-range) "[%s]" "<%s>")
  2718. range-end)))))))
  2719. (defun org-element-timestamp-successor (limit)
  2720. "Search for the next timestamp object.
  2721. LIMIT bounds the search.
  2722. Return value is a cons cell whose CAR is `timestamp' and CDR is
  2723. beginning position."
  2724. (save-excursion
  2725. (when (re-search-forward
  2726. (concat org-ts-regexp-both
  2727. "\\|"
  2728. "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  2729. "\\|"
  2730. "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
  2731. limit t)
  2732. (cons 'timestamp (match-beginning 0)))))
  2733. ;;;; Underline
  2734. (defun org-element-underline-parser ()
  2735. "Parse underline object at point.
  2736. Return a list whose CAR is `underline' and CDR is a plist with
  2737. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2738. `:post-blank' keywords.
  2739. Assume point is at the first underscore marker."
  2740. (save-excursion
  2741. (unless (bolp) (backward-char 1))
  2742. (looking-at org-emph-re)
  2743. (let ((begin (match-beginning 2))
  2744. (contents-begin (match-beginning 4))
  2745. (contents-end (match-end 4))
  2746. (post-blank (progn (goto-char (match-end 2))
  2747. (skip-chars-forward " \t")))
  2748. (end (point)))
  2749. (list 'underline
  2750. (list :begin begin
  2751. :end end
  2752. :contents-begin contents-begin
  2753. :contents-end contents-end
  2754. :post-blank post-blank)))))
  2755. (defun org-element-underline-interpreter (underline contents)
  2756. "Interpret UNDERLINE object as Org syntax.
  2757. CONTENTS is the contents of the object."
  2758. (format "_%s_" contents))
  2759. ;;;; Verbatim
  2760. (defun org-element-verbatim-parser ()
  2761. "Parse verbatim object at point.
  2762. Return a list whose CAR is `verbatim' and CDR is a plist with
  2763. `:value', `:begin', `:end' and `:post-blank' keywords.
  2764. Assume point is at the first equal sign marker."
  2765. (save-excursion
  2766. (unless (bolp) (backward-char 1))
  2767. (looking-at org-emph-re)
  2768. (let ((begin (match-beginning 2))
  2769. (value (org-match-string-no-properties 4))
  2770. (post-blank (progn (goto-char (match-end 2))
  2771. (skip-chars-forward " \t")))
  2772. (end (point)))
  2773. (list 'verbatim
  2774. (list :value value
  2775. :begin begin
  2776. :end end
  2777. :post-blank post-blank)))))
  2778. (defun org-element-verbatim-interpreter (verbatim contents)
  2779. "Interpret VERBATIM object as Org syntax.
  2780. CONTENTS is nil."
  2781. (format "=%s=" (org-element-property :value verbatim)))
  2782. ;;; Parsing Element Starting At Point
  2783. ;;
  2784. ;; `org-element--current-element' is the core function of this section.
  2785. ;; It returns the Lisp representation of the element starting at
  2786. ;; point.
  2787. ;;
  2788. ;; `org-element--current-element' makes use of special modes. They
  2789. ;; are activated for fixed element chaining (i.e. `plain-list' >
  2790. ;; `item') or fixed conditional element chaining (i.e. `headline' >
  2791. ;; `section'). Special modes are: `first-section', `section',
  2792. ;; `quote-section', `item' and `table-row'.
  2793. (defun org-element--current-element
  2794. (limit &optional granularity special structure)
  2795. "Parse the element starting at point.
  2796. LIMIT bounds the search.
  2797. Return value is a list like (TYPE PROPS) where TYPE is the type
  2798. of the element and PROPS a plist of properties associated to the
  2799. element.
  2800. Possible types are defined in `org-element-all-elements'.
  2801. Optional argument GRANULARITY determines the depth of the
  2802. recursion. Allowed values are `headline', `greater-element',
  2803. `element', `object' or nil. When it is broader than `object' (or
  2804. nil), secondary values will not be parsed, since they only
  2805. contain objects.
  2806. Optional argument SPECIAL, when non-nil, can be either
  2807. `first-section', `section', `quote-section', `table-row' and
  2808. `item'.
  2809. If STRUCTURE isn't provided but SPECIAL is set to `item', it will
  2810. be computed.
  2811. This function assumes point is always at the beginning of the
  2812. element it has to parse."
  2813. (save-excursion
  2814. ;; If point is at an affiliated keyword, try moving to the
  2815. ;; beginning of the associated element. If none is found, the
  2816. ;; keyword is orphaned and will be treated as plain text.
  2817. (when (looking-at org-element--affiliated-re)
  2818. (let ((opoint (point)))
  2819. (while (looking-at org-element--affiliated-re) (forward-line))
  2820. (when (looking-at "[ \t]*$") (goto-char opoint))))
  2821. (let ((case-fold-search t)
  2822. ;; Determine if parsing depth allows for secondary strings
  2823. ;; parsing. It only applies to elements referenced in
  2824. ;; `org-element-secondary-value-alist'.
  2825. (raw-secondary-p (and granularity (not (eq granularity 'object)))))
  2826. (cond
  2827. ;; Item.
  2828. ((eq special 'item)
  2829. (org-element-item-parser limit structure raw-secondary-p))
  2830. ;; Quote Section.
  2831. ((eq special 'quote-section) (org-element-quote-section-parser limit))
  2832. ;; Table Row.
  2833. ((eq special 'table-row) (org-element-table-row-parser limit))
  2834. ;; Headline.
  2835. ((org-with-limited-levels (org-at-heading-p))
  2836. (org-element-headline-parser limit raw-secondary-p))
  2837. ;; Section (must be checked after headline).
  2838. ((eq special 'section) (org-element-section-parser limit))
  2839. ((eq special 'first-section)
  2840. (org-element-section-parser
  2841. (or (save-excursion (org-with-limited-levels (outline-next-heading)))
  2842. limit)))
  2843. ;; Planning and Clock.
  2844. ((and (looking-at org-planning-or-clock-line-re))
  2845. (if (equal (match-string 1) org-clock-string)
  2846. (org-element-clock-parser limit)
  2847. (org-element-planning-parser limit)))
  2848. ;; Inlinetask.
  2849. ((org-at-heading-p)
  2850. (org-element-inlinetask-parser limit raw-secondary-p))
  2851. ;; LaTeX Environment.
  2852. ((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
  2853. (if (save-excursion
  2854. (re-search-forward
  2855. (format "[ \t]*\\\\end{%s}[ \t]*"
  2856. (regexp-quote (match-string 1)))
  2857. nil t))
  2858. (org-element-latex-environment-parser limit)
  2859. (org-element-paragraph-parser limit)))
  2860. ;; Drawer and Property Drawer.
  2861. ((looking-at org-drawer-regexp)
  2862. (let ((name (match-string 1)))
  2863. (cond
  2864. ((not (save-excursion
  2865. (re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
  2866. (org-element-paragraph-parser limit))
  2867. ((equal "PROPERTIES" name)
  2868. (org-element-property-drawer-parser limit))
  2869. (t (org-element-drawer-parser limit)))))
  2870. ;; Fixed Width
  2871. ((looking-at "[ \t]*:\\( \\|$\\)")
  2872. (org-element-fixed-width-parser limit))
  2873. ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
  2874. ;; Keywords.
  2875. ((looking-at "[ \t]*#\\+")
  2876. (goto-char (match-end 0))
  2877. (cond ((looking-at "$\\| ")
  2878. (beginning-of-line)
  2879. (org-element-comment-parser limit))
  2880. ((looking-at "BEGIN_\\(\\S-+\\)")
  2881. (beginning-of-line)
  2882. (let ((parser (assoc (upcase (match-string 1))
  2883. org-element-block-name-alist)))
  2884. (if parser (funcall (cdr parser) limit)
  2885. (org-element-special-block-parser limit))))
  2886. ((looking-at "CALL")
  2887. (beginning-of-line)
  2888. (org-element-babel-call-parser limit))
  2889. ((looking-at "BEGIN:? ")
  2890. (beginning-of-line)
  2891. (org-element-dynamic-block-parser limit))
  2892. ((looking-at "\\S-+:")
  2893. (beginning-of-line)
  2894. (org-element-keyword-parser limit))
  2895. ;; Ill-formed syntax is considered as a comment.
  2896. (t
  2897. (beginning-of-line)
  2898. (org-element-comment-parser limit))))
  2899. ;; Comments.
  2900. ((eq (char-after) ?#) (org-element-comment-parser limit))
  2901. ;; Footnote Definition.
  2902. ((looking-at org-footnote-definition-re)
  2903. (org-element-footnote-definition-parser limit))
  2904. ;; Horizontal Rule.
  2905. ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
  2906. (org-element-horizontal-rule-parser limit))
  2907. ;; Table.
  2908. ((org-at-table-p t) (org-element-table-parser limit))
  2909. ;; List.
  2910. ((looking-at (org-item-re))
  2911. (org-element-plain-list-parser limit (or structure (org-list-struct))))
  2912. ;; Default element: Paragraph.
  2913. (t (org-element-paragraph-parser limit))))))
  2914. ;; Most elements can have affiliated keywords. When looking for an
  2915. ;; element beginning, we want to move before them, as they belong to
  2916. ;; that element, and, in the meantime, collect information they give
  2917. ;; into appropriate properties. Hence the following function.
  2918. ;;
  2919. ;; Usage of optional arguments may not be obvious at first glance:
  2920. ;;
  2921. ;; - TRANS-LIST is used to polish keywords names that have evolved
  2922. ;; during Org history. In example, even though =result= and
  2923. ;; =results= coexist, we want to have them under the same =result=
  2924. ;; property. It's also true for "srcname" and "name", where the
  2925. ;; latter seems to be preferred nowadays (thus the "name" property).
  2926. ;;
  2927. ;; - CONSED allows to regroup multi-lines keywords under the same
  2928. ;; property, while preserving their own identity. This is mostly
  2929. ;; used for "attr_latex" and al.
  2930. ;;
  2931. ;; - PARSED prepares a keyword value for export. This is useful for
  2932. ;; "caption". Objects restrictions for such keywords are defined in
  2933. ;; `org-element-object-restrictions'.
  2934. ;;
  2935. ;; - DUALS is used to take care of keywords accepting a main and an
  2936. ;; optional secondary values. For example "results" has its
  2937. ;; source's name as the main value, and may have an hash string in
  2938. ;; optional square brackets as the secondary one.
  2939. ;;
  2940. ;; A keyword may belong to more than one category.
  2941. (defun org-element--collect-affiliated-keywords
  2942. (&optional key-re trans-list consed parsed duals)
  2943. "Collect affiliated keywords before point.
  2944. Optional argument KEY-RE is a regexp matching keywords, which
  2945. puts matched keyword in group 1. It defaults to
  2946. `org-element--affiliated-re'.
  2947. TRANS-LIST is an alist where key is the keyword and value the
  2948. property name it should be translated to, without the colons. It
  2949. defaults to `org-element-keyword-translation-alist'.
  2950. CONSED is a list of strings. Any keyword belonging to that list
  2951. will have its value consed. The check is done after keyword
  2952. translation. It defaults to `org-element-multiple-keywords'.
  2953. PARSED is a list of strings. Any keyword member of this list
  2954. will have its value parsed. The check is done after keyword
  2955. translation. If a keyword is a member of both CONSED and PARSED,
  2956. it's value will be a list of parsed strings. It defaults to
  2957. `org-element-parsed-keywords'.
  2958. DUALS is a list of strings. Any keyword member of this list can
  2959. have two parts: one mandatory and one optional. Its value is
  2960. a cons cell whose CAR is the former, and the CDR the latter. If
  2961. a keyword is a member of both PARSED and DUALS, both values will
  2962. be parsed. It defaults to `org-element-dual-keywords'.
  2963. Return a list whose CAR is the position at the first of them and
  2964. CDR a plist of keywords and values."
  2965. (save-excursion
  2966. (let ((case-fold-search t)
  2967. (key-re (or key-re org-element--affiliated-re))
  2968. (trans-list (or trans-list org-element-keyword-translation-alist))
  2969. (consed (or consed org-element-multiple-keywords))
  2970. (parsed (or parsed org-element-parsed-keywords))
  2971. (duals (or duals org-element-dual-keywords))
  2972. ;; RESTRICT is the list of objects allowed in parsed
  2973. ;; keywords value.
  2974. (restrict (org-element-restriction 'keyword))
  2975. output)
  2976. (unless (bobp)
  2977. (while (and (not (bobp)) (progn (forward-line -1) (looking-at key-re)))
  2978. (let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
  2979. ;; Apply translation to RAW-KWD. From there, KWD is
  2980. ;; the official keyword.
  2981. (kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
  2982. ;; Find main value for any keyword.
  2983. (value
  2984. (save-match-data
  2985. (org-trim
  2986. (buffer-substring-no-properties
  2987. (match-end 0) (point-at-eol)))))
  2988. ;; If KWD is a dual keyword, find its secondary
  2989. ;; value. Maybe parse it.
  2990. (dual-value
  2991. (and (member kwd duals)
  2992. (let ((sec (org-match-string-no-properties 3)))
  2993. (if (or (not sec) (not (member kwd parsed))) sec
  2994. (org-element-parse-secondary-string sec restrict)))))
  2995. ;; Attribute a property name to KWD.
  2996. (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
  2997. ;; Now set final shape for VALUE.
  2998. (when (member kwd parsed)
  2999. (setq value (org-element-parse-secondary-string value restrict)))
  3000. (when (member kwd duals)
  3001. ;; VALUE is mandatory. Set it to nil if there is none.
  3002. (setq value (and value (cons value dual-value))))
  3003. ;; Attributes are always consed.
  3004. (when (or (member kwd consed) (string-match "^ATTR_" kwd))
  3005. (setq value (cons value (plist-get output kwd-sym))))
  3006. ;; Eventually store the new value in OUTPUT.
  3007. (setq output (plist-put output kwd-sym value))))
  3008. (unless (looking-at key-re) (forward-line 1)))
  3009. (list (point) output))))
  3010. ;;; The Org Parser
  3011. ;;
  3012. ;; The two major functions here are `org-element-parse-buffer', which
  3013. ;; parses Org syntax inside the current buffer, taking into account
  3014. ;; region, narrowing, or even visibility if specified, and
  3015. ;; `org-element-parse-secondary-string', which parses objects within
  3016. ;; a given string.
  3017. ;;
  3018. ;; The (almost) almighty `org-element-map' allows to apply a function
  3019. ;; on elements or objects matching some type, and accumulate the
  3020. ;; resulting values. In an export situation, it also skips unneeded
  3021. ;; parts of the parse tree.
  3022. (defun org-element-parse-buffer (&optional granularity visible-only)
  3023. "Recursively parse the buffer and return structure.
  3024. If narrowing is in effect, only parse the visible part of the
  3025. buffer.
  3026. Optional argument GRANULARITY determines the depth of the
  3027. recursion. It can be set to the following symbols:
  3028. `headline' Only parse headlines.
  3029. `greater-element' Don't recurse into greater elements excepted
  3030. headlines and sections. Thus, elements
  3031. parsed are the top-level ones.
  3032. `element' Parse everything but objects and plain text.
  3033. `object' Parse the complete buffer (default).
  3034. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3035. elements.
  3036. Assume buffer is in Org mode."
  3037. (save-excursion
  3038. (goto-char (point-min))
  3039. (org-skip-whitespace)
  3040. (org-element--parse-elements
  3041. (point-at-bol) (point-max)
  3042. ;; Start in `first-section' mode so text before the first
  3043. ;; headline belongs to a section.
  3044. 'first-section nil granularity visible-only (list 'org-data nil))))
  3045. (defun org-element-parse-secondary-string (string restriction &optional parent)
  3046. "Recursively parse objects in STRING and return structure.
  3047. RESTRICTION is a symbol limiting the object types that will be
  3048. looked after.
  3049. Optional argument PARENT, when non-nil, is the element or object
  3050. containing the secondary string. It is used to set correctly
  3051. `:parent' property within the string."
  3052. (with-temp-buffer
  3053. (insert string)
  3054. (let ((secondary (org-element--parse-objects
  3055. (point-min) (point-max) nil restriction)))
  3056. (mapc (lambda (obj) (org-element-put-property obj :parent parent))
  3057. secondary))))
  3058. (defun org-element-map (data types fun &optional info first-match no-recursion)
  3059. "Map a function on selected elements or objects.
  3060. DATA is the parsed tree, as returned by, i.e,
  3061. `org-element-parse-buffer'. TYPES is a symbol or list of symbols
  3062. of elements or objects types. FUN is the function called on the
  3063. matching element or object. It must accept one arguments: the
  3064. element or object itself.
  3065. When optional argument INFO is non-nil, it should be a plist
  3066. holding export options. In that case, parts of the parse tree
  3067. not exportable according to that property list will be skipped.
  3068. When optional argument FIRST-MATCH is non-nil, stop at the first
  3069. match for which FUN doesn't return nil, and return that value.
  3070. Optional argument NO-RECURSION is a symbol or a list of symbols
  3071. representing elements or objects types. `org-element-map' won't
  3072. enter any recursive element or object whose type belongs to that
  3073. list. Though, FUN can still be applied on them.
  3074. Nil values returned from FUN do not appear in the results."
  3075. ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
  3076. (unless (listp types) (setq types (list types)))
  3077. (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
  3078. ;; Recursion depth is determined by --CATEGORY.
  3079. (let* ((--category
  3080. (catch 'found
  3081. (let ((category 'greater-elements))
  3082. (mapc (lambda (type)
  3083. (cond ((or (memq type org-element-all-objects)
  3084. (eq type 'plain-text))
  3085. ;; If one object is found, the function
  3086. ;; has to recurse into every object.
  3087. (throw 'found 'objects))
  3088. ((not (memq type org-element-greater-elements))
  3089. ;; If one regular element is found, the
  3090. ;; function has to recurse, at lest, into
  3091. ;; every element it encounters.
  3092. (and (not (eq category 'elements))
  3093. (setq category 'elements)))))
  3094. types)
  3095. category)))
  3096. --acc
  3097. --walk-tree
  3098. (--walk-tree
  3099. (function
  3100. (lambda (--data)
  3101. ;; Recursively walk DATA. INFO, if non-nil, is a plist
  3102. ;; holding contextual information.
  3103. (let ((--type (org-element-type --data)))
  3104. (cond
  3105. ((not --data))
  3106. ;; Ignored element in an export context.
  3107. ((and info (memq --data (plist-get info :ignore-list))))
  3108. ;; Secondary string: only objects can be found there.
  3109. ((not --type)
  3110. (when (eq --category 'objects) (mapc --walk-tree --data)))
  3111. ;; Unconditionally enter parse trees.
  3112. ((eq --type 'org-data)
  3113. (mapc --walk-tree (org-element-contents --data)))
  3114. (t
  3115. ;; Check if TYPE is matching among TYPES. If so,
  3116. ;; apply FUN to --DATA and accumulate return value
  3117. ;; into --ACC (or exit if FIRST-MATCH is non-nil).
  3118. (when (memq --type types)
  3119. (let ((result (funcall fun --data)))
  3120. (cond ((not result))
  3121. (first-match (throw '--map-first-match result))
  3122. (t (push result --acc)))))
  3123. ;; If --DATA has a secondary string that can contain
  3124. ;; objects with their type among TYPES, look into it.
  3125. (when (eq --category 'objects)
  3126. (let ((sec-prop
  3127. (assq --type org-element-secondary-value-alist)))
  3128. (when sec-prop
  3129. (funcall --walk-tree
  3130. (org-element-property (cdr sec-prop) --data)))))
  3131. ;; Determine if a recursion into --DATA is possible.
  3132. (cond
  3133. ;; --TYPE is explicitly removed from recursion.
  3134. ((memq --type no-recursion))
  3135. ;; --DATA has no contents.
  3136. ((not (org-element-contents --data)))
  3137. ;; Looking for greater elements but --DATA is simply
  3138. ;; an element or an object.
  3139. ((and (eq --category 'greater-elements)
  3140. (not (memq --type org-element-greater-elements))))
  3141. ;; Looking for elements but --DATA is an object.
  3142. ((and (eq --category 'elements)
  3143. (memq --type org-element-all-objects)))
  3144. ;; In any other case, map contents.
  3145. (t (mapc --walk-tree (org-element-contents --data)))))))))))
  3146. (catch '--map-first-match
  3147. (funcall --walk-tree data)
  3148. ;; Return value in a proper order.
  3149. (nreverse --acc))))
  3150. ;; The following functions are internal parts of the parser.
  3151. ;;
  3152. ;; The first one, `org-element--parse-elements' acts at the element's
  3153. ;; level.
  3154. ;;
  3155. ;; The second one, `org-element--parse-objects' applies on all objects
  3156. ;; of a paragraph or a secondary string. It uses
  3157. ;; `org-element--get-next-object-candidates' to optimize the search of
  3158. ;; the next object in the buffer.
  3159. ;;
  3160. ;; More precisely, that function looks for every allowed object type
  3161. ;; first. Then, it discards failed searches, keeps further matches,
  3162. ;; and searches again types matched behind point, for subsequent
  3163. ;; calls. Thus, searching for a given type fails only once, and every
  3164. ;; object is searched only once at top level (but sometimes more for
  3165. ;; nested types).
  3166. (defun org-element--parse-elements
  3167. (beg end special structure granularity visible-only acc)
  3168. "Parse elements between BEG and END positions.
  3169. SPECIAL prioritize some elements over the others. It can be set
  3170. to `first-section', `quote-section', `section' `item' or
  3171. `table-row'.
  3172. When value is `item', STRUCTURE will be used as the current list
  3173. structure.
  3174. GRANULARITY determines the depth of the recursion. See
  3175. `org-element-parse-buffer' for more information.
  3176. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3177. elements.
  3178. Elements are accumulated into ACC."
  3179. (save-excursion
  3180. (goto-char beg)
  3181. ;; When parsing only headlines, skip any text before first one.
  3182. (when (and (eq granularity 'headline) (not (org-at-heading-p)))
  3183. (org-with-limited-levels (outline-next-heading)))
  3184. ;; Main loop start.
  3185. (while (< (point) end)
  3186. ;; Find current element's type and parse it accordingly to
  3187. ;; its category.
  3188. (let* ((element (org-element--current-element
  3189. end granularity special structure))
  3190. (type (org-element-type element))
  3191. (cbeg (org-element-property :contents-begin element)))
  3192. (goto-char (org-element-property :end element))
  3193. ;; Fill ELEMENT contents by side-effect.
  3194. (cond
  3195. ;; If VISIBLE-ONLY is true and element is hidden or if it has
  3196. ;; no contents, don't modify it.
  3197. ((or (and visible-only (org-element-property :hiddenp element))
  3198. (not cbeg)))
  3199. ;; Greater element: parse it between `contents-begin' and
  3200. ;; `contents-end'. Make sure GRANULARITY allows the
  3201. ;; recursion, or ELEMENT is an headline, in which case going
  3202. ;; inside is mandatory, in order to get sub-level headings.
  3203. ((and (memq type org-element-greater-elements)
  3204. (or (memq granularity '(element object nil))
  3205. (and (eq granularity 'greater-element)
  3206. (eq type 'section))
  3207. (eq type 'headline)))
  3208. (org-element--parse-elements
  3209. cbeg (org-element-property :contents-end element)
  3210. ;; Possibly switch to a special mode.
  3211. (case type
  3212. (headline
  3213. (if (org-element-property :quotedp element) 'quote-section
  3214. 'section))
  3215. (plain-list 'item)
  3216. (table 'table-row))
  3217. (org-element-property :structure element)
  3218. granularity visible-only element))
  3219. ;; ELEMENT has contents. Parse objects inside, if
  3220. ;; GRANULARITY allows it.
  3221. ((memq granularity '(object nil))
  3222. (org-element--parse-objects
  3223. cbeg (org-element-property :contents-end element) element
  3224. (org-element-restriction type))))
  3225. (org-element-adopt-element acc element t)))
  3226. ;; Return result.
  3227. acc))
  3228. (defun org-element--parse-objects (beg end acc restriction)
  3229. "Parse objects between BEG and END and return recursive structure.
  3230. Objects are accumulated in ACC.
  3231. RESTRICTION is a list of object types which are allowed in the
  3232. current object."
  3233. (let (candidates)
  3234. (save-excursion
  3235. (goto-char beg)
  3236. (while (and (< (point) end)
  3237. (setq candidates (org-element--get-next-object-candidates
  3238. end restriction candidates)))
  3239. (let ((next-object
  3240. (let ((pos (apply 'min (mapcar 'cdr candidates))))
  3241. (save-excursion
  3242. (goto-char pos)
  3243. (funcall (intern (format "org-element-%s-parser"
  3244. (car (rassq pos candidates)))))))))
  3245. ;; 1. Text before any object. Untabify it.
  3246. (let ((obj-beg (org-element-property :begin next-object)))
  3247. (unless (= (point) obj-beg)
  3248. (setq acc
  3249. (org-element-adopt-element
  3250. acc
  3251. (replace-regexp-in-string
  3252. "\t" (make-string tab-width ? )
  3253. (buffer-substring-no-properties (point) obj-beg)) t))))
  3254. ;; 2. Object...
  3255. (let ((obj-end (org-element-property :end next-object))
  3256. (cont-beg (org-element-property :contents-begin next-object)))
  3257. ;; Fill contents of NEXT-OBJECT by side-effect, if it has
  3258. ;; a recursive type.
  3259. (when (and cont-beg
  3260. (memq (car next-object) org-element-recursive-objects))
  3261. (save-restriction
  3262. (narrow-to-region
  3263. cont-beg
  3264. (org-element-property :contents-end next-object))
  3265. (org-element--parse-objects
  3266. (point-min) (point-max) next-object
  3267. (org-element-restriction next-object))))
  3268. (setq acc (org-element-adopt-element acc next-object t))
  3269. (goto-char obj-end))))
  3270. ;; 3. Text after last object. Untabify it.
  3271. (unless (= (point) end)
  3272. (setq acc
  3273. (org-element-adopt-element
  3274. acc
  3275. (replace-regexp-in-string
  3276. "\t" (make-string tab-width ? )
  3277. (buffer-substring-no-properties (point) end)) t)))
  3278. ;; Result.
  3279. acc)))
  3280. (defun org-element--get-next-object-candidates (limit restriction objects)
  3281. "Return an alist of candidates for the next object.
  3282. LIMIT bounds the search, and RESTRICTION narrows candidates to
  3283. some object types.
  3284. Return value is an alist whose CAR is position and CDR the object
  3285. type, as a symbol.
  3286. OBJECTS is the previous candidates alist."
  3287. (let (next-candidates types-to-search)
  3288. ;; If no previous result, search every object type in RESTRICTION.
  3289. ;; Otherwise, keep potential candidates (old objects located after
  3290. ;; point) and ask to search again those which had matched before.
  3291. (if (not objects) (setq types-to-search restriction)
  3292. (mapc (lambda (obj)
  3293. (if (< (cdr obj) (point)) (push (car obj) types-to-search)
  3294. (push obj next-candidates)))
  3295. objects))
  3296. ;; Call the appropriate successor function for each type to search
  3297. ;; and accumulate matches.
  3298. (mapc
  3299. (lambda (type)
  3300. (let* ((successor-fun
  3301. (intern
  3302. (format "org-element-%s-successor"
  3303. (or (cdr (assq type org-element-object-successor-alist))
  3304. type))))
  3305. (obj (funcall successor-fun limit)))
  3306. (and obj (push obj next-candidates))))
  3307. types-to-search)
  3308. ;; Return alist.
  3309. next-candidates))
  3310. ;;; Towards A Bijective Process
  3311. ;;
  3312. ;; The parse tree obtained with `org-element-parse-buffer' is really
  3313. ;; a snapshot of the corresponding Org buffer. Therefore, it can be
  3314. ;; interpreted and expanded into a string with canonical Org syntax.
  3315. ;; Hence `org-element-interpret-data'.
  3316. ;;
  3317. ;; The function relies internally on
  3318. ;; `org-element--interpret-affiliated-keywords'.
  3319. (defun org-element-interpret-data (data &optional parent)
  3320. "Interpret DATA as Org syntax.
  3321. DATA is a parse tree, an element, an object or a secondary string
  3322. to interpret.
  3323. Optional argument PARENT is used for recursive calls. It contains
  3324. the element or object containing data, or nil.
  3325. Return Org syntax as a string."
  3326. (let* ((type (org-element-type data))
  3327. (results
  3328. (cond
  3329. ;; Secondary string.
  3330. ((not type)
  3331. (mapconcat
  3332. (lambda (obj) (org-element-interpret-data obj parent))
  3333. data ""))
  3334. ;; Full Org document.
  3335. ((eq type 'org-data)
  3336. (mapconcat
  3337. (lambda (obj) (org-element-interpret-data obj parent))
  3338. (org-element-contents data) ""))
  3339. ;; Plain text.
  3340. ((stringp data) data)
  3341. ;; Element/Object without contents.
  3342. ((not (org-element-contents data))
  3343. (funcall (intern (format "org-element-%s-interpreter" type))
  3344. data nil))
  3345. ;; Element/Object with contents.
  3346. (t
  3347. (let* ((greaterp (memq type org-element-greater-elements))
  3348. (objectp (and (not greaterp)
  3349. (memq type org-element-recursive-objects)))
  3350. (contents
  3351. (mapconcat
  3352. (lambda (obj) (org-element-interpret-data obj data))
  3353. (org-element-contents
  3354. (if (or greaterp objectp) data
  3355. ;; Elements directly containing objects must
  3356. ;; have their indentation normalized first.
  3357. (org-element-normalize-contents
  3358. data
  3359. ;; When normalizing first paragraph of an
  3360. ;; item or a footnote-definition, ignore
  3361. ;; first line's indentation.
  3362. (and (eq type 'paragraph)
  3363. (equal data (car (org-element-contents parent)))
  3364. (memq (org-element-type parent)
  3365. '(footnote-definiton item))))))
  3366. "")))
  3367. (funcall (intern (format "org-element-%s-interpreter" type))
  3368. data
  3369. (if greaterp (org-element-normalize-contents contents)
  3370. contents)))))))
  3371. (if (memq type '(org-data plain-text nil)) results
  3372. ;; Build white spaces. If no `:post-blank' property is
  3373. ;; specified, assume its value is 0.
  3374. (let ((post-blank (or (org-element-property :post-blank data) 0)))
  3375. (if (memq type org-element-all-objects)
  3376. (concat results (make-string post-blank 32))
  3377. (concat
  3378. (org-element--interpret-affiliated-keywords data)
  3379. (org-element-normalize-string results)
  3380. (make-string post-blank 10)))))))
  3381. (defun org-element--interpret-affiliated-keywords (element)
  3382. "Return ELEMENT's affiliated keywords as Org syntax.
  3383. If there is no affiliated keyword, return the empty string."
  3384. (let ((keyword-to-org
  3385. (function
  3386. (lambda (key value)
  3387. (let (dual)
  3388. (when (member key org-element-dual-keywords)
  3389. (setq dual (cdr value) value (car value)))
  3390. (concat "#+" key
  3391. (and dual
  3392. (format "[%s]" (org-element-interpret-data dual)))
  3393. ": "
  3394. (if (member key org-element-parsed-keywords)
  3395. (org-element-interpret-data value)
  3396. value)
  3397. "\n"))))))
  3398. (mapconcat
  3399. (lambda (prop)
  3400. (let ((value (org-element-property prop element))
  3401. (keyword (upcase (substring (symbol-name prop) 1))))
  3402. (when value
  3403. (if (or (member keyword org-element-multiple-keywords)
  3404. ;; All attribute keywords can have multiple lines.
  3405. (string-match "^ATTR_" keyword))
  3406. (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
  3407. value
  3408. "")
  3409. (funcall keyword-to-org keyword value)))))
  3410. ;; List all ELEMENT's properties matching an attribute line or an
  3411. ;; affiliated keyword, but ignore translated keywords since they
  3412. ;; cannot belong to the property list.
  3413. (loop for prop in (nth 1 element) by 'cddr
  3414. when (let ((keyword (upcase (substring (symbol-name prop) 1))))
  3415. (or (string-match "^ATTR_" keyword)
  3416. (and
  3417. (member keyword org-element-affiliated-keywords)
  3418. (not (assoc keyword
  3419. org-element-keyword-translation-alist)))))
  3420. collect prop)
  3421. "")))
  3422. ;; Because interpretation of the parse tree must return the same
  3423. ;; number of blank lines between elements and the same number of white
  3424. ;; space after objects, some special care must be given to white
  3425. ;; spaces.
  3426. ;;
  3427. ;; The first function, `org-element-normalize-string', ensures any
  3428. ;; string different from the empty string will end with a single
  3429. ;; newline character.
  3430. ;;
  3431. ;; The second function, `org-element-normalize-contents', removes
  3432. ;; global indentation from the contents of the current element.
  3433. (defun org-element-normalize-string (s)
  3434. "Ensure string S ends with a single newline character.
  3435. If S isn't a string return it unchanged. If S is the empty
  3436. string, return it. Otherwise, return a new string with a single
  3437. newline character at its end."
  3438. (cond
  3439. ((not (stringp s)) s)
  3440. ((string= "" s) "")
  3441. (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
  3442. (replace-match "\n" nil nil s)))))
  3443. (defun org-element-normalize-contents (element &optional ignore-first)
  3444. "Normalize plain text in ELEMENT's contents.
  3445. ELEMENT must only contain plain text and objects.
  3446. If optional argument IGNORE-FIRST is non-nil, ignore first line's
  3447. indentation to compute maximal common indentation.
  3448. Return the normalized element that is element with global
  3449. indentation removed from its contents. The function assumes that
  3450. indentation is not done with TAB characters."
  3451. (let* (ind-list ; for byte-compiler
  3452. collect-inds ; for byte-compiler
  3453. (collect-inds
  3454. (function
  3455. ;; Return list of indentations within BLOB. This is done by
  3456. ;; walking recursively BLOB and updating IND-LIST along the
  3457. ;; way. FIRST-FLAG is non-nil when the first string hasn't
  3458. ;; been seen yet. It is required as this string is the only
  3459. ;; one whose indentation doesn't happen after a newline
  3460. ;; character.
  3461. (lambda (blob first-flag)
  3462. (mapc
  3463. (lambda (object)
  3464. (when (and first-flag (stringp object))
  3465. (setq first-flag nil)
  3466. (string-match "\\`\\( *\\)" object)
  3467. (let ((len (length (match-string 1 object))))
  3468. ;; An indentation of zero means no string will be
  3469. ;; modified. Quit the process.
  3470. (if (zerop len) (throw 'zero (setq ind-list nil))
  3471. (push len ind-list))))
  3472. (cond
  3473. ((stringp object)
  3474. (let ((start 0))
  3475. ;; Avoid matching blank or empty lines.
  3476. (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
  3477. (not (equal (match-string 2 object) " ")))
  3478. (setq start (match-end 0))
  3479. (push (length (match-string 1 object)) ind-list))))
  3480. ((memq (org-element-type object) org-element-recursive-objects)
  3481. (funcall collect-inds object first-flag))))
  3482. (org-element-contents blob))))))
  3483. ;; Collect indentation list in ELEMENT. Possibly remove first
  3484. ;; value if IGNORE-FIRST is non-nil.
  3485. (catch 'zero (funcall collect-inds element (not ignore-first)))
  3486. (if (not ind-list) element
  3487. ;; Build ELEMENT back, replacing each string with the same
  3488. ;; string minus common indentation.
  3489. (let* (build ; For byte compiler.
  3490. (build
  3491. (function
  3492. (lambda (blob mci first-flag)
  3493. ;; Return BLOB with all its strings indentation
  3494. ;; shortened from MCI white spaces. FIRST-FLAG is
  3495. ;; non-nil when the first string hasn't been seen
  3496. ;; yet.
  3497. (setcdr (cdr blob)
  3498. (mapcar
  3499. (lambda (object)
  3500. (when (and first-flag (stringp object))
  3501. (setq first-flag nil)
  3502. (setq object
  3503. (replace-regexp-in-string
  3504. (format "\\` \\{%d\\}" mci) "" object)))
  3505. (cond
  3506. ((stringp object)
  3507. (replace-regexp-in-string
  3508. (format "\n \\{%d\\}" mci) "\n" object))
  3509. ((memq (org-element-type object)
  3510. org-element-recursive-objects)
  3511. (funcall build object mci first-flag))
  3512. (t object)))
  3513. (org-element-contents blob)))
  3514. blob))))
  3515. (funcall build element (apply 'min ind-list) (not ignore-first))))))
  3516. ;;; The Toolbox
  3517. ;;
  3518. ;; The first move is to implement a way to obtain the smallest element
  3519. ;; containing point. This is the job of `org-element-at-point'. It
  3520. ;; basically jumps back to the beginning of section containing point
  3521. ;; and moves, element after element, with
  3522. ;; `org-element--current-element' until the container is found.
  3523. ;;
  3524. ;; At a deeper level, `org-element-context' lists all elements and
  3525. ;; objects containing point.
  3526. ;;
  3527. ;; Note: When using `org-element-at-point', secondary values are never
  3528. ;; parsed since the function focuses on elements, not on objects.
  3529. (defun org-element-at-point (&optional keep-trail)
  3530. "Determine closest element around point.
  3531. Return value is a list like (TYPE PROPS) where TYPE is the type
  3532. of the element and PROPS a plist of properties associated to the
  3533. element.
  3534. Possible types are defined in `org-element-all-elements'.
  3535. Properties depend on element or object type, but always
  3536. include :begin, :end, :parent and :post-blank properties.
  3537. As a special case, if point is at the very beginning of a list or
  3538. sub-list, returned element will be that list instead of the first
  3539. item. In the same way, if point is at the beginning of the first
  3540. row of a table, returned element will be the table instead of the
  3541. first row.
  3542. If optional argument KEEP-TRAIL is non-nil, the function returns
  3543. a list of of elements leading to element at point. The list's
  3544. CAR is always the element at point. Following positions contain
  3545. element's siblings, then parents, siblings of parents, until the
  3546. first element of current section."
  3547. (org-with-wide-buffer
  3548. ;; If at an headline, parse it. It is the sole element that
  3549. ;; doesn't require to know about context. Be sure to disallow
  3550. ;; secondary string parsing, though.
  3551. (if (org-with-limited-levels (org-at-heading-p))
  3552. (progn
  3553. (beginning-of-line)
  3554. (if (not keep-trail) (org-element-headline-parser (point-max) t)
  3555. (list (org-element-headline-parser (point-max) t))))
  3556. ;; Otherwise move at the beginning of the section containing
  3557. ;; point.
  3558. (let ((origin (point))
  3559. (end (save-excursion
  3560. (org-with-limited-levels (outline-next-heading)) (point)))
  3561. element type special-flag trail struct prevs parent)
  3562. (org-with-limited-levels
  3563. (if (org-with-limited-levels (org-before-first-heading-p))
  3564. (goto-char (point-min))
  3565. (org-back-to-heading)
  3566. (forward-line)))
  3567. (org-skip-whitespace)
  3568. (beginning-of-line)
  3569. ;; Parse successively each element, skipping those ending
  3570. ;; before original position.
  3571. (catch 'exit
  3572. (while t
  3573. (setq element
  3574. (org-element--current-element end 'element special-flag struct)
  3575. type (car element))
  3576. (org-element-put-property element :parent parent)
  3577. (when keep-trail (push element trail))
  3578. (cond
  3579. ;; 1. Skip any element ending before point or at point
  3580. ;; because the following element has started. On the
  3581. ;; other hand, if the element ends at point and that
  3582. ;; point is also the end of the buffer, do not skip it.
  3583. ((let ((end (org-element-property :end element)))
  3584. (when (or (< end origin)
  3585. (and (= end origin) (/= (point-max) end)))
  3586. (if (> (point-max) end) (goto-char end)
  3587. (throw 'exit (if keep-trail trail element))))))
  3588. ;; 2. An element containing point is always the element at
  3589. ;; point.
  3590. ((not (memq type org-element-greater-elements))
  3591. (throw 'exit (if keep-trail trail element)))
  3592. ;; 3. At any other greater element type, if point is
  3593. ;; within contents, move into it. Otherwise, return
  3594. ;; that element. As a special case, when ORIGIN is
  3595. ;; contents end and is also the end of the buffer, try
  3596. ;; to move inside the greater element to find the end
  3597. ;; of the innermost element.
  3598. (t
  3599. (let ((cbeg (org-element-property :contents-begin element))
  3600. (cend (org-element-property :contents-end element)))
  3601. (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
  3602. (and (= cend origin) (/= (point-max) origin))
  3603. (and (= cbeg origin) (memq type '(plain-list table))))
  3604. (throw 'exit (if keep-trail trail element))
  3605. (setq parent element)
  3606. (case type
  3607. (plain-list
  3608. (setq special-flag 'item
  3609. struct (org-element-property :structure element)))
  3610. (table (setq special-flag 'table-row))
  3611. (otherwise (setq special-flag nil)))
  3612. (setq end cend)
  3613. (goto-char cbeg)))))))))))
  3614. (defun org-element-context ()
  3615. "Return closest element or object around point.
  3616. Return value is a list like (TYPE PROPS) where TYPE is the type
  3617. of the element or object and PROPS a plist of properties
  3618. associated to it.
  3619. Possible types are defined in `org-element-all-elements' and
  3620. `org-element-all-objects'. Properties depend on element or
  3621. object type, but always include :begin, :end, :parent
  3622. and :post-blank properties."
  3623. (org-with-wide-buffer
  3624. (let* ((origin (point))
  3625. (element (org-element-at-point))
  3626. (type (car element))
  3627. end)
  3628. ;; Check if point is inside an element containing objects or at
  3629. ;; a secondary string. In that case, move to beginning of the
  3630. ;; element or secondary string and set END to the other side.
  3631. (if (not (or (and (eq type 'item)
  3632. (let ((tag (org-element-property :tag element)))
  3633. (and tag
  3634. (progn
  3635. (beginning-of-line)
  3636. (search-forward tag (point-at-eol))
  3637. (goto-char (match-beginning 0))
  3638. (and (>= origin (point))
  3639. (<= origin
  3640. ;; `1+' is required so some
  3641. ;; successors can match
  3642. ;; properly their object.
  3643. (setq end (1+ (match-end 0)))))))))
  3644. (and (memq type '(headline inlinetask))
  3645. (progn (beginning-of-line)
  3646. (skip-chars-forward "* ")
  3647. (setq end (point-at-eol))))
  3648. (and (memq type '(paragraph table-cell verse-block))
  3649. (let ((cbeg (org-element-property
  3650. :contents-begin element))
  3651. (cend (org-element-property
  3652. :contents-end element)))
  3653. (and (>= origin cbeg)
  3654. (<= origin cend)
  3655. (progn (goto-char cbeg) (setq end cend)))))))
  3656. element
  3657. (let ((restriction (org-element-restriction element))
  3658. (parent element)
  3659. candidates)
  3660. (catch 'exit
  3661. (while (setq candidates (org-element--get-next-object-candidates
  3662. end restriction candidates))
  3663. (let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
  3664. candidates)))
  3665. ;; If ORIGIN is before next object in element, there's
  3666. ;; no point in looking further.
  3667. (if (> (cdr closest-cand) origin) (throw 'exit element)
  3668. (let* ((object
  3669. (progn (goto-char (cdr closest-cand))
  3670. (funcall (intern (format "org-element-%s-parser"
  3671. (car closest-cand))))))
  3672. (cbeg (org-element-property :contents-begin object))
  3673. (cend (org-element-property :contents-end object)))
  3674. (cond
  3675. ;; ORIGIN is after OBJECT, so skip it.
  3676. ((< (org-element-property :end object) origin)
  3677. (goto-char (org-element-property :end object)))
  3678. ;; ORIGIN is within a non-recursive object or at an
  3679. ;; object boundaries: Return that object.
  3680. ((or (not cbeg) (> cbeg origin) (< cend origin))
  3681. (throw 'exit
  3682. (org-element-put-property object :parent parent)))
  3683. ;; Otherwise, move within current object and restrict
  3684. ;; search to the end of its contents.
  3685. (t (goto-char cbeg)
  3686. (org-element-put-property object :parent parent)
  3687. (setq parent object end cend)))))))
  3688. parent))))))
  3689. ;; Once the local structure around point is well understood, it's easy
  3690. ;; to implement some replacements for `forward-paragraph'
  3691. ;; `backward-paragraph', namely `org-element-forward' and
  3692. ;; `org-element-backward'.
  3693. ;;
  3694. ;; Also, `org-transpose-elements' mimics the behaviour of
  3695. ;; `transpose-words', at the element's level, whereas
  3696. ;; `org-element-drag-forward', `org-element-drag-backward', and
  3697. ;; `org-element-up' generalize, respectively, functions
  3698. ;; `org-subtree-down', `org-subtree-up' and `outline-up-heading'.
  3699. ;;
  3700. ;; `org-element-unindent-buffer' will, as its name almost suggests,
  3701. ;; smartly remove global indentation from buffer, making it possible
  3702. ;; to use Org indent mode on a file created with hard indentation.
  3703. ;;
  3704. ;; `org-element-nested-p' and `org-element-swap-A-B' are used
  3705. ;; internally by some of the previously cited tools.
  3706. (defsubst org-element-nested-p (elem-A elem-B)
  3707. "Non-nil when elements ELEM-A and ELEM-B are nested."
  3708. (let ((beg-A (org-element-property :begin elem-A))
  3709. (beg-B (org-element-property :begin elem-B))
  3710. (end-A (org-element-property :end elem-A))
  3711. (end-B (org-element-property :end elem-B)))
  3712. (or (and (>= beg-A beg-B) (<= end-A end-B))
  3713. (and (>= beg-B beg-A) (<= end-B end-A)))))
  3714. (defun org-element-swap-A-B (elem-A elem-B)
  3715. "Swap elements ELEM-A and ELEM-B.
  3716. Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
  3717. end of ELEM-A."
  3718. (goto-char (org-element-property :begin elem-A))
  3719. ;; There are two special cases when an element doesn't start at bol:
  3720. ;; the first paragraph in an item or in a footnote definition.
  3721. (let ((specialp (not (bolp))))
  3722. ;; Only a paragraph without any affiliated keyword can be moved at
  3723. ;; ELEM-A position in such a situation. Note that the case of
  3724. ;; a footnote definition is impossible: it cannot contain two
  3725. ;; paragraphs in a row because it cannot contain a blank line.
  3726. (if (and specialp
  3727. (or (not (eq (org-element-type elem-B) 'paragraph))
  3728. (/= (org-element-property :begin elem-B)
  3729. (org-element-property :contents-begin elem-B))))
  3730. (error "Cannot swap elements"))
  3731. ;; In a special situation, ELEM-A will have no indentation. We'll
  3732. ;; give it ELEM-B's (which will in, in turn, have no indentation).
  3733. (let* ((ind-B (when specialp
  3734. (goto-char (org-element-property :begin elem-B))
  3735. (org-get-indentation)))
  3736. (beg-A (org-element-property :begin elem-A))
  3737. (end-A (save-excursion
  3738. (goto-char (org-element-property :end elem-A))
  3739. (skip-chars-backward " \r\t\n")
  3740. (point-at-eol)))
  3741. (beg-B (org-element-property :begin elem-B))
  3742. (end-B (save-excursion
  3743. (goto-char (org-element-property :end elem-B))
  3744. (skip-chars-backward " \r\t\n")
  3745. (point-at-eol)))
  3746. ;; Store overlays responsible for visibility status. We
  3747. ;; also need to store their boundaries as they will be
  3748. ;; removed from buffer.
  3749. (overlays
  3750. (cons
  3751. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  3752. (overlays-in beg-A end-A))
  3753. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  3754. (overlays-in beg-B end-B))))
  3755. ;; Get contents.
  3756. (body-A (buffer-substring beg-A end-A))
  3757. (body-B (delete-and-extract-region beg-B end-B)))
  3758. (goto-char beg-B)
  3759. (when specialp
  3760. (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
  3761. (org-indent-to-column ind-B))
  3762. (insert body-A)
  3763. ;; Restore ex ELEM-A overlays.
  3764. (mapc (lambda (ov)
  3765. (move-overlay
  3766. (car ov)
  3767. (+ (nth 1 ov) (- beg-B beg-A))
  3768. (+ (nth 2 ov) (- beg-B beg-A))))
  3769. (car overlays))
  3770. (goto-char beg-A)
  3771. (delete-region beg-A end-A)
  3772. (insert body-B)
  3773. ;; Restore ex ELEM-B overlays.
  3774. (mapc (lambda (ov)
  3775. (move-overlay (car ov)
  3776. (+ (nth 1 ov) (- beg-A beg-B))
  3777. (+ (nth 2 ov) (- beg-A beg-B))))
  3778. (cdr overlays))
  3779. (goto-char (org-element-property :end elem-B)))))
  3780. (defun org-element-forward ()
  3781. "Move forward by one element.
  3782. Move to the next element at the same level, when possible."
  3783. (interactive)
  3784. (cond ((eobp) (error "Cannot move further down"))
  3785. ((org-with-limited-levels (org-at-heading-p))
  3786. (let ((origin (point)))
  3787. (org-forward-same-level 1)
  3788. (unless (org-with-limited-levels (org-at-heading-p))
  3789. (goto-char origin)
  3790. (error "Cannot move further down"))))
  3791. (t
  3792. (let* ((elem (org-element-at-point))
  3793. (end (org-element-property :end elem))
  3794. (parent (org-element-property :parent elem)))
  3795. (if (and parent (= (org-element-property :contents-end parent) end))
  3796. (goto-char (org-element-property :end parent))
  3797. (goto-char end))))))
  3798. (defun org-element-backward ()
  3799. "Move backward by one element.
  3800. Move to the previous element at the same level, when possible."
  3801. (interactive)
  3802. (if (org-with-limited-levels (org-at-heading-p))
  3803. ;; At an headline, move to the previous one, if any, or stay
  3804. ;; here.
  3805. (let ((origin (point)))
  3806. (org-backward-same-level 1)
  3807. (unless (org-with-limited-levels (org-at-heading-p))
  3808. (goto-char origin)
  3809. (error "Cannot move further up")))
  3810. (let* ((trail (org-element-at-point 'keep-trail))
  3811. (elem (car trail))
  3812. (prev-elem (nth 1 trail))
  3813. (beg (org-element-property :begin elem)))
  3814. (cond
  3815. ;; Move to beginning of current element if point isn't there
  3816. ;; already.
  3817. ((/= (point) beg) (goto-char beg))
  3818. ((not prev-elem) (error "Cannot move further up"))
  3819. (t (goto-char (org-element-property :begin prev-elem)))))))
  3820. (defun org-element-up ()
  3821. "Move to upper element."
  3822. (interactive)
  3823. (if (org-with-limited-levels (org-at-heading-p))
  3824. (unless (org-up-heading-safe) (error "No surrounding element"))
  3825. (let* ((elem (org-element-at-point))
  3826. (parent (org-element-property :parent elem)))
  3827. (if parent (goto-char (org-element-property :begin parent))
  3828. (if (org-with-limited-levels (org-before-first-heading-p))
  3829. (error "No surrounding element")
  3830. (org-with-limited-levels (org-back-to-heading)))))))
  3831. (defun org-element-down ()
  3832. "Move to inner element."
  3833. (interactive)
  3834. (let ((element (org-element-at-point)))
  3835. (cond
  3836. ((memq (org-element-type element) '(plain-list table))
  3837. (goto-char (org-element-property :contents-begin element))
  3838. (forward-char))
  3839. ((memq (org-element-type element) org-element-greater-elements)
  3840. ;; If contents are hidden, first disclose them.
  3841. (when (org-element-property :hiddenp element) (org-cycle))
  3842. (goto-char (org-element-property :contents-begin element)))
  3843. (t (error "No inner element")))))
  3844. (defun org-element-drag-backward ()
  3845. "Move backward element at point."
  3846. (interactive)
  3847. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  3848. (let* ((trail (org-element-at-point 'keep-trail))
  3849. (elem (car trail))
  3850. (prev-elem (nth 1 trail)))
  3851. ;; Error out if no previous element or previous element is
  3852. ;; a parent of the current one.
  3853. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  3854. (error "Cannot drag element backward")
  3855. (let ((pos (point)))
  3856. (org-element-swap-A-B prev-elem elem)
  3857. (goto-char (+ (org-element-property :begin prev-elem)
  3858. (- pos (org-element-property :begin elem)))))))))
  3859. (defun org-element-drag-forward ()
  3860. "Move forward element at point."
  3861. (interactive)
  3862. (let* ((pos (point))
  3863. (elem (org-element-at-point)))
  3864. (when (= (point-max) (org-element-property :end elem))
  3865. (error "Cannot drag element forward"))
  3866. (goto-char (org-element-property :end elem))
  3867. (let ((next-elem (org-element-at-point)))
  3868. (when (or (org-element-nested-p elem next-elem)
  3869. (and (eq (org-element-type next-elem) 'headline)
  3870. (not (eq (org-element-type elem) 'headline))))
  3871. (goto-char pos)
  3872. (error "Cannot drag element forward"))
  3873. ;; Compute new position of point: it's shifted by NEXT-ELEM
  3874. ;; body's length (without final blanks) and by the length of
  3875. ;; blanks between ELEM and NEXT-ELEM.
  3876. (let ((size-next (- (save-excursion
  3877. (goto-char (org-element-property :end next-elem))
  3878. (skip-chars-backward " \r\t\n")
  3879. (forward-line)
  3880. ;; Small correction if buffer doesn't end
  3881. ;; with a newline character.
  3882. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  3883. (org-element-property :begin next-elem)))
  3884. (size-blank (- (org-element-property :end elem)
  3885. (save-excursion
  3886. (goto-char (org-element-property :end elem))
  3887. (skip-chars-backward " \r\t\n")
  3888. (forward-line)
  3889. (point)))))
  3890. (org-element-swap-A-B elem next-elem)
  3891. (goto-char (+ pos size-next size-blank))))))
  3892. (defun org-element-mark-element ()
  3893. "Put point at beginning of this element, mark at end.
  3894. Interactively, if this command is repeated or (in Transient Mark
  3895. mode) if the mark is active, it marks the next element after the
  3896. ones already marked."
  3897. (interactive)
  3898. (let (deactivate-mark)
  3899. (if (or (and (eq last-command this-command) (mark t))
  3900. (and transient-mark-mode mark-active))
  3901. (set-mark
  3902. (save-excursion
  3903. (goto-char (mark))
  3904. (goto-char (org-element-property :end (org-element-at-point)))))
  3905. (let ((element (org-element-at-point)))
  3906. (end-of-line)
  3907. (push-mark (org-element-property :end element) t t)
  3908. (goto-char (org-element-property :begin element))))))
  3909. (defun org-narrow-to-element ()
  3910. "Narrow buffer to current element."
  3911. (interactive)
  3912. (let ((elem (org-element-at-point)))
  3913. (cond
  3914. ((eq (car elem) 'headline)
  3915. (narrow-to-region
  3916. (org-element-property :begin elem)
  3917. (org-element-property :end elem)))
  3918. ((memq (car elem) org-element-greater-elements)
  3919. (narrow-to-region
  3920. (org-element-property :contents-begin elem)
  3921. (org-element-property :contents-end elem)))
  3922. (t
  3923. (narrow-to-region
  3924. (org-element-property :begin elem)
  3925. (org-element-property :end elem))))))
  3926. (defun org-element-transpose ()
  3927. "Transpose current and previous elements, keeping blank lines between.
  3928. Point is moved after both elements."
  3929. (interactive)
  3930. (org-skip-whitespace)
  3931. (let ((end (org-element-property :end (org-element-at-point))))
  3932. (org-element-drag-backward)
  3933. (goto-char end)))
  3934. (defun org-element-unindent-buffer ()
  3935. "Un-indent the visible part of the buffer.
  3936. Relative indentation (between items, inside blocks, etc.) isn't
  3937. modified."
  3938. (interactive)
  3939. (unless (eq major-mode 'org-mode)
  3940. (error "Cannot un-indent a buffer not in Org mode"))
  3941. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  3942. unindent-tree ; For byte-compiler.
  3943. (unindent-tree
  3944. (function
  3945. (lambda (contents)
  3946. (mapc
  3947. (lambda (element)
  3948. (if (memq (org-element-type element) '(headline section))
  3949. (funcall unindent-tree (org-element-contents element))
  3950. (save-excursion
  3951. (save-restriction
  3952. (narrow-to-region
  3953. (org-element-property :begin element)
  3954. (org-element-property :end element))
  3955. (org-do-remove-indentation)))))
  3956. (reverse contents))))))
  3957. (funcall unindent-tree (org-element-contents parse-tree))))
  3958. (provide 'org-element)
  3959. ;;; org-element.el ends here