org-element.el 155 KB

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