org-element.el 156 KB

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