org-element.el 156 KB

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