org-element.el 156 KB

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