org-element.el 152 KB

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