org-element.el 152 KB

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