org-element.el 154 KB

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