org-element.el 152 KB

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