org-element.el 150 KB

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