ob-core.el 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170
  1. ;;; ob-core.el --- working with code blocks in org-mode
  2. ;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
  3. ;; Authors: Eric Schulte
  4. ;; Dan Davison
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: http://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Code:
  19. (eval-when-compile
  20. (require 'cl))
  21. (require 'cl-lib)
  22. (require 'ob-eval)
  23. (require 'org-macs)
  24. (require 'org-compat)
  25. (defconst org-babel-exeext
  26. (if (memq system-type '(windows-nt cygwin))
  27. ".exe"
  28. nil))
  29. (defvar org-babel-call-process-region-original nil)
  30. (defvar org-babel-library-of-babel)
  31. (defvar org-edit-src-content-indentation)
  32. (defvar org-src-lang-modes)
  33. (defvar org-src-preserve-indentation)
  34. (declare-function outline-show-all "outline" ())
  35. (declare-function org-get-indentation "org" (&optional line))
  36. (declare-function org-remove-indentation "org" (code &optional n))
  37. (declare-function org-mark-ring-push "org" (&optional pos buffer))
  38. (declare-function tramp-compat-make-temp-file "tramp-compat"
  39. (filename &optional dir-flag))
  40. (declare-function tramp-dissect-file-name "tramp" (name &optional nodefault))
  41. (declare-function tramp-file-name-user "tramp" (vec))
  42. (declare-function tramp-file-name-host "tramp" (vec))
  43. (declare-function with-parsed-tramp-file-name "tramp" (filename var &rest body))
  44. (declare-function org-edit-src-code "org-src" (&optional code edit-buffer-name))
  45. (declare-function org-edit-src-exit "org-src" ())
  46. (declare-function org-get-indentation "org" (&optional line))
  47. (declare-function org-in-regexp "org" (regexp &optional nlines visually))
  48. (declare-function org-open-at-point "org" (&optional in-emacs reference-buffer))
  49. (declare-function org-save-outline-visibility "org-macs" (use-markers &rest body))
  50. (declare-function org-outline-overlay-data "org" (&optional use-markers))
  51. (declare-function org-set-outline-overlay-data "org" (data))
  52. (declare-function org-narrow-to-subtree "org" ())
  53. (declare-function org-split-string "org" (string &optional separators))
  54. (declare-function org-entry-get "org"
  55. (pom property &optional inherit literal-nil))
  56. (declare-function org-indent-line "org" ())
  57. (declare-function org-make-options-regexp "org" (kwds &optional extra))
  58. (declare-function org-do-remove-indentation "org" (&optional n))
  59. (declare-function org-next-block "org" (arg &optional backward block-regexp))
  60. (declare-function org-previous-block "org" (arg &optional block-regexp))
  61. (declare-function org-show-context "org" (&optional key))
  62. (declare-function org-at-table-p "org" (&optional table-type))
  63. (declare-function org-cycle "org" (&optional arg))
  64. (declare-function org-uniquify "org" (list))
  65. (declare-function org-current-level "org" ())
  66. (declare-function org-table-import "org-table" (file arg))
  67. (declare-function org-add-hook "org-compat"
  68. (hook function &optional append local))
  69. (declare-function org-table-align "org-table" ())
  70. (declare-function org-table-end "org-table" (&optional table-type))
  71. (declare-function orgtbl-to-generic "org-table" (table params))
  72. (declare-function orgtbl-to-orgtbl "org-table" (table params))
  73. (declare-function org-babel-tangle-comment-links "ob-tangle" (&optional info))
  74. (declare-function org-babel-ref-split-args "ob-ref" (arg-string))
  75. (declare-function org-babel-ref-parse "ob-ref" (assignment))
  76. (declare-function org-babel-ref-resolve "ob-ref" (ref))
  77. (declare-function org-babel-ref-goto-headline-id "ob-ref" (id))
  78. (declare-function org-babel-ref-headline-body "ob-ref" ())
  79. (declare-function org-babel-lob-execute-maybe "ob-lob" ())
  80. (declare-function org-number-sequence "org-compat" (from &optional to inc))
  81. (declare-function org-at-item-p "org-list" ())
  82. (declare-function org-list-to-generic "org-list" (LIST PARAMS))
  83. (declare-function org-list-to-lisp "org-list" (&optional delete))
  84. (declare-function org-list-struct "org-list" ())
  85. (declare-function org-list-prevs-alist "org-list" (struct))
  86. (declare-function org-list-get-list-end "org-list" (item struct prevs))
  87. (declare-function org-completing-read "org" (&rest args))
  88. (declare-function org-escape-code-in-region "org-src" (beg end))
  89. (declare-function org-unescape-code-in-string "org-src" (s))
  90. (declare-function org-table-to-lisp "org-table" (&optional txt))
  91. (declare-function org-reverse-string "org" (string))
  92. (declare-function org-element-context "org-element" (&optional element))
  93. (declare-function org-element-type "org-element" (element))
  94. (declare-function org-element-at-point "org-element" ())
  95. (declare-function org-element-normalize-string "org-element" (s))
  96. (declare-function org-element-property "org-element" (property element))
  97. (declare-function org-macro-escape-arguments "org-macro" (&rest args))
  98. (defgroup org-babel nil
  99. "Code block evaluation and management in `org-mode' documents."
  100. :tag "Babel"
  101. :group 'org)
  102. (defcustom org-confirm-babel-evaluate t
  103. "Confirm before evaluation.
  104. \\<org-mode-map>\
  105. Require confirmation before interactively evaluating code
  106. blocks in Org-mode buffers. The default value of this variable
  107. is t, meaning confirmation is required for any code block
  108. evaluation. This variable can be set to nil to inhibit any
  109. future confirmation requests. This variable can also be set to a
  110. function which takes two arguments the language of the code block
  111. and the body of the code block. Such a function should then
  112. return a non-nil value if the user should be prompted for
  113. execution or nil if no prompt is required.
  114. Warning: Disabling confirmation may result in accidental
  115. evaluation of potentially harmful code. It may be advisable
  116. remove code block execution from \\[org-ctrl-c-ctrl-c] \
  117. as further protection
  118. against accidental code block evaluation. The
  119. `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can be used to
  120. remove code block execution from the \\[org-ctrl-c-ctrl-c] keybinding."
  121. :group 'org-babel
  122. :version "24.1"
  123. :type '(choice boolean function))
  124. ;; don't allow this variable to be changed through file settings
  125. (put 'org-confirm-babel-evaluate 'safe-local-variable (lambda (x) (eq x t)))
  126. (defcustom org-babel-no-eval-on-ctrl-c-ctrl-c nil
  127. "\\<org-mode-map>\
  128. Remove code block evaluation from the \\[org-ctrl-c-ctrl-c] key binding."
  129. :group 'org-babel
  130. :version "24.1"
  131. :type 'boolean)
  132. (defcustom org-babel-results-keyword "RESULTS"
  133. "Keyword used to name results generated by code blocks.
  134. It should be \"RESULTS\". However any capitalization may be
  135. used."
  136. :group 'org-babel
  137. :version "24.4"
  138. :package-version '(Org . "8.0")
  139. :type 'string
  140. :safe (lambda (v)
  141. (and (stringp v)
  142. (eq (compare-strings "RESULTS" nil nil v nil nil t)
  143. t))))
  144. (defcustom org-babel-noweb-wrap-start "<<"
  145. "String used to begin a noweb reference in a code block.
  146. See also `org-babel-noweb-wrap-end'."
  147. :group 'org-babel
  148. :type 'string)
  149. (defcustom org-babel-noweb-wrap-end ">>"
  150. "String used to end a noweb reference in a code block.
  151. See also `org-babel-noweb-wrap-start'."
  152. :group 'org-babel
  153. :type 'string)
  154. (defcustom org-babel-inline-result-wrap "=%s="
  155. "Format string used to wrap inline results.
  156. This string must include a \"%s\" which will be replaced by the results."
  157. :group 'org-babel
  158. :type 'string)
  159. (put 'org-babel-inline-result-wrap
  160. 'safe-local-variable
  161. (lambda (value)
  162. (and (stringp value)
  163. (string-match-p "%s" value))))
  164. (defun org-babel-noweb-wrap (&optional regexp)
  165. (concat org-babel-noweb-wrap-start
  166. (or regexp "\\([^ \t\n].+?[^ \t]\\|[^ \t\n]\\)")
  167. org-babel-noweb-wrap-end))
  168. (defvar org-babel-src-name-regexp
  169. "^[ \t]*#\\+name:[ \t]*"
  170. "Regular expression used to match a source name line.")
  171. (defvar org-babel-multi-line-header-regexp
  172. "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
  173. "Regular expression used to match multi-line header arguments.")
  174. (defvar org-babel-src-block-regexp
  175. (concat
  176. ;; (1) indentation (2) lang
  177. "^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
  178. ;; (3) switches
  179. "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
  180. ;; (4) header arguments
  181. "\\([^\n]*\\)\n"
  182. ;; (5) body
  183. "\\([^\000]*?\n\\)??[ \t]*#\\+end_src")
  184. "Regexp used to identify code blocks.")
  185. (defun org-babel--get-vars (params)
  186. "Return the babel variable assignments in PARAMS.
  187. PARAMS is a quasi-alist of header args, whcih may contain
  188. multiple entries for the key `:var'. This function returns a
  189. list of the cdr of all the `:var' entries."
  190. (mapcar #'cdr
  191. (cl-remove-if-not (lambda (x) (eq (car x) :var)) params)))
  192. (defvar org-babel-exp-reference-buffer nil
  193. "Buffer containing original contents of the exported buffer.
  194. This is used by Babel to resolve references in source blocks.
  195. Its value is dynamically bound during export.")
  196. (defun org-babel-check-confirm-evaluate (info)
  197. "Check whether INFO allows code block evaluation.
  198. Returns nil if evaluation is disallowed, t if it is
  199. unconditionally allowed, and the symbol `query' if the user
  200. should be asked whether to allow evaluation."
  201. (let* ((headers (nth 2 info))
  202. (eval (or (cdr (assq :eval headers))
  203. (when (assq :noeval headers) "no")))
  204. (eval-no (member eval '("no" "never")))
  205. (export org-babel-exp-reference-buffer)
  206. (eval-no-export (and export (member eval '("no-export" "never-export"))))
  207. (noeval (or eval-no eval-no-export))
  208. (query (or (equal eval "query")
  209. (and export (equal eval "query-export"))
  210. (if (functionp org-confirm-babel-evaluate)
  211. (save-excursion
  212. (goto-char (nth 5 info))
  213. (funcall org-confirm-babel-evaluate
  214. ;; language, code block body
  215. (nth 0 info) (nth 1 info)))
  216. org-confirm-babel-evaluate))))
  217. (cond
  218. (noeval nil)
  219. (query 'query)
  220. (t t))))
  221. (defun org-babel-check-evaluate (info)
  222. "Check if code block INFO should be evaluated.
  223. Do not query the user, but do display an informative message if
  224. evaluation is blocked. Returns non-nil if evaluation is not blocked."
  225. (let ((evalp (org-babel-check-confirm-evaluate info)))
  226. (when (null evalp)
  227. (message "Evaluation of this %s code-block%sis disabled."
  228. (nth 0 info)
  229. (let ((name (nth 4 info))) (if name (format " (%s) " name) ""))))
  230. evalp))
  231. ;; Dynamically scoped for asynchronous export.
  232. (defvar org-babel-confirm-evaluate-answer-no)
  233. (defun org-babel-confirm-evaluate (info)
  234. "Confirm evaluation of the code block INFO.
  235. This query can also be suppressed by setting the value of
  236. `org-confirm-babel-evaluate' to nil, in which case all future
  237. interactive code block evaluations will proceed without any
  238. confirmation from the user.
  239. Note disabling confirmation may result in accidental evaluation
  240. of potentially harmful code.
  241. The variable `org-babel-confirm-evaluate-answer-no' is used by
  242. the async export process, which requires a non-interactive
  243. environment, to override this check."
  244. (let* ((evalp (org-babel-check-confirm-evaluate info))
  245. (lang (nth 0 info))
  246. (name (nth 4 info))
  247. (name-string (if name (format " (%s) " name) " ")))
  248. (pcase evalp
  249. (`nil nil)
  250. (`t t)
  251. (`query (or
  252. (and (not (org-bound-and-true-p
  253. org-babel-confirm-evaluate-answer-no))
  254. (yes-or-no-p
  255. (format "Evaluate this %s code block%son your system? "
  256. lang name-string)))
  257. (progn
  258. (message "Evaluation of this %s code-block%sis aborted."
  259. lang name-string)
  260. nil)))
  261. (x (error "Unexpected value `%s' from `org-babel-check-confirm-evaluate'" x)))))
  262. ;;;###autoload
  263. (defun org-babel-execute-safely-maybe ()
  264. (unless org-babel-no-eval-on-ctrl-c-ctrl-c
  265. (org-babel-execute-maybe)))
  266. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-safely-maybe)
  267. ;;;###autoload
  268. (defun org-babel-execute-maybe ()
  269. (interactive)
  270. (or (org-babel-execute-src-block-maybe)
  271. (org-babel-lob-execute-maybe)))
  272. (defmacro org-babel-when-in-src-block (&rest body)
  273. "Execute BODY if point is in a source block and return t.
  274. Otherwise do nothing and return nil."
  275. `(if (memq (org-element-type (org-element-context))
  276. '(inline-src-block src-block))
  277. (progn
  278. ,@body
  279. t)
  280. nil))
  281. (defun org-babel-execute-src-block-maybe ()
  282. "Conditionally execute a source block.
  283. Detect if this is context for a Babel src-block and if so
  284. then run `org-babel-execute-src-block'."
  285. (interactive)
  286. (org-babel-when-in-src-block
  287. (org-babel-eval-wipe-error-buffer)
  288. (org-babel-execute-src-block current-prefix-arg)))
  289. ;;;###autoload
  290. (defun org-babel-view-src-block-info ()
  291. "Display information on the current source block.
  292. This includes header arguments, language and name, and is largely
  293. a window into the `org-babel-get-src-block-info' function."
  294. (interactive)
  295. (let ((info (org-babel-get-src-block-info 'light))
  296. (full (lambda (it) (> (length it) 0)))
  297. (printf (lambda (fmt &rest args) (princ (apply #'format fmt args)))))
  298. (when info
  299. (with-help-window (help-buffer)
  300. (let ((name (nth 4 info))
  301. (lang (nth 0 info))
  302. (switches (nth 3 info))
  303. (header-args (nth 2 info)))
  304. (when name (funcall printf "Name: %s\n" name))
  305. (when lang (funcall printf "Lang: %s\n" lang))
  306. (funcall printf "Properties:\n")
  307. (funcall printf "\t:header-args \t%s\n" (org-entry-get (point) "header-args" t))
  308. (funcall printf "\t:header-args:%s \t%s\n" lang (org-entry-get (point) (concat "header-args:" lang) t))
  309. (when (funcall full switches) (funcall printf "Switches: %s\n" switches))
  310. (funcall printf "Header Arguments:\n")
  311. (dolist (pair (sort header-args
  312. (lambda (a b) (string< (symbol-name (car a))
  313. (symbol-name (car b))))))
  314. (when (funcall full (format "%s" (cdr pair)))
  315. (funcall printf "\t%S%s\t%s\n"
  316. (car pair)
  317. (if (> (length (format "%S" (car pair))) 7) "" "\t")
  318. (cdr pair)))))))))
  319. ;;;###autoload
  320. (defun org-babel-expand-src-block-maybe ()
  321. "Conditionally expand a source block.
  322. Detect if this is context for a org-babel src-block and if so
  323. then run `org-babel-expand-src-block'."
  324. (interactive)
  325. (org-babel-when-in-src-block
  326. (org-babel-expand-src-block current-prefix-arg)))
  327. ;;;###autoload
  328. (defun org-babel-load-in-session-maybe ()
  329. "Conditionally load a source block in a session.
  330. Detect if this is context for a org-babel src-block and if so
  331. then run `org-babel-load-in-session'."
  332. (interactive)
  333. (org-babel-when-in-src-block
  334. (org-babel-load-in-session current-prefix-arg)))
  335. (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
  336. ;;;###autoload
  337. (defun org-babel-pop-to-session-maybe ()
  338. "Conditionally pop to a session.
  339. Detect if this is context for a org-babel src-block and if so
  340. then run `org-babel-switch-to-session'."
  341. (interactive)
  342. (org-babel-when-in-src-block
  343. (org-babel-switch-to-session current-prefix-arg)))
  344. (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
  345. (defconst org-babel-common-header-args-w-values
  346. '((cache . ((no yes)))
  347. (cmdline . :any)
  348. (colnames . ((nil no yes)))
  349. (comments . ((no link yes org both noweb)))
  350. (dir . :any)
  351. (eval . ((yes no no-export strip-export never-export eval never
  352. query)))
  353. (exports . ((code results both none)))
  354. (epilogue . :any)
  355. (file . :any)
  356. (file-desc . :any)
  357. (file-ext . :any)
  358. (hlines . ((no yes)))
  359. (mkdirp . ((yes no)))
  360. (no-expand)
  361. (noeval)
  362. (noweb . ((yes no tangle no-export strip-export)))
  363. (noweb-ref . :any)
  364. (noweb-sep . :any)
  365. (output-dir . :any)
  366. (padline . ((yes no)))
  367. (post . :any)
  368. (prologue . :any)
  369. (results . ((file list vector table scalar verbatim)
  370. (raw html latex org code pp drawer)
  371. (replace silent none append prepend)
  372. (output value)))
  373. (rownames . ((no yes)))
  374. (sep . :any)
  375. (session . :any)
  376. (shebang . :any)
  377. (tangle . ((tangle yes no :any)))
  378. (tangle-mode . ((#o755 #o555 #o444 :any)))
  379. (var . :any)
  380. (wrap . :any)))
  381. (defconst org-babel-header-arg-names
  382. (mapcar #'car org-babel-common-header-args-w-values)
  383. "Common header arguments used by org-babel.
  384. Note that individual languages may define their own language
  385. specific header arguments as well.")
  386. (defconst org-babel-safe-header-args
  387. '(:cache :colnames :comments :exports :epilogue :hlines :noeval
  388. :noweb :noweb-ref :noweb-sep :padline :prologue :rownames
  389. :sep :session :tangle :wrap
  390. (:eval . ("never" "query"))
  391. (:results . (lambda (str) (not (string-match "file" str)))))
  392. "A list of safe header arguments for babel source blocks.
  393. The list can have entries of the following forms:
  394. - :ARG -> :ARG is always a safe header arg
  395. - (:ARG . (VAL1 VAL2 ...)) -> :ARG is safe as a header arg if it is
  396. `equal' to one of the VALs.
  397. - (:ARG . FN) -> :ARG is safe as a header arg if the function FN
  398. returns non-nil. FN is passed one
  399. argument, the value of the header arg
  400. (as a string).")
  401. (defmacro org-babel-header-args-safe-fn (safe-list)
  402. "Return a function that determines whether a list of header args are safe.
  403. Intended usage is:
  404. \(put \\='org-babel-default-header-args \\='safe-local-variable
  405. (org-babel-header-args-safe-p org-babel-safe-header-args)
  406. This allows org-babel languages to extend the list of safe values for
  407. their `org-babel-default-header-args:foo' variable.
  408. For the format of SAFE-LIST, see `org-babel-safe-header-args'."
  409. `(lambda (value)
  410. (and (listp value)
  411. (cl-every
  412. (lambda (pair)
  413. (and (consp pair)
  414. (org-babel-one-header-arg-safe-p pair ,safe-list)))
  415. value))))
  416. (defvar org-babel-default-header-args
  417. '((:session . "none") (:results . "replace") (:exports . "code")
  418. (:cache . "no") (:noweb . "no") (:hlines . "no") (:tangle . "no"))
  419. "Default arguments to use when evaluating a source block.")
  420. (put 'org-babel-default-header-args 'safe-local-variable
  421. (org-babel-header-args-safe-fn org-babel-safe-header-args))
  422. (defvar org-babel-default-inline-header-args
  423. '((:session . "none") (:results . "replace")
  424. (:exports . "results") (:hlines . "yes"))
  425. "Default arguments to use when evaluating an inline source block.")
  426. (put 'org-babel-default-inline-header-args 'safe-local-variable
  427. (org-babel-header-args-safe-fn org-babel-safe-header-args))
  428. (defconst org-babel-name-regexp
  429. (format "^[ \t]*#\\+%s:[ \t]*"
  430. ;; FIXME: TBLNAME is for backward compatibility.
  431. (regexp-opt '("NAME" "TBLNAME")))
  432. "Regexp matching a NAME keyword.")
  433. (defconst org-babel-result-regexp
  434. (format "^[ \t]*#\\+%s\\(?:\\[\\(?:%S\\)?\\([[:alnum:]]+\\)\\]\\)?:[ \t]*"
  435. org-babel-results-keyword
  436. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>")
  437. "Regular expression used to match result lines.
  438. If the results are associated with a hash key then the hash will
  439. be saved in match group 1.")
  440. (defconst org-babel-result-w-name-regexp
  441. (concat org-babel-result-regexp "\\(?9:[^ \t\n\r\v\f]+\\)")
  442. "Regexp matching a RESULTS keyword with a name.
  443. Name is saved in match group 9.")
  444. (defvar org-babel-min-lines-for-block-output 10
  445. "The minimum number of lines for block output.
  446. If number of lines of output is equal to or exceeds this
  447. value, the output is placed in a #+begin_example...#+end_example
  448. block. Otherwise the output is marked as literal by inserting
  449. colons at the starts of the lines. This variable only takes
  450. effect if the :results output option is in effect.")
  451. (defvar org-babel-noweb-error-all-langs nil
  452. "Raise errors when noweb references don't resolve.
  453. Also see `org-babel-noweb-error-langs' to control noweb errors on
  454. a language by language bases.")
  455. (defvar org-babel-noweb-error-langs nil
  456. "Languages for which Babel will raise literate programming errors.
  457. List of languages for which errors should be raised when the
  458. source code block satisfying a noweb reference in this language
  459. can not be resolved. Also see `org-babel-noweb-error-all-langs'
  460. to raise errors for all languages.")
  461. (defvar org-babel-hash-show 4
  462. "Number of initial characters to show of a hidden results hash.")
  463. (defvar org-babel-hash-show-time nil
  464. "Non-nil means show the time the code block was evaluated in the result hash.")
  465. (defvar org-babel-after-execute-hook nil
  466. "Hook for functions to be called after `org-babel-execute-src-block'")
  467. (defun org-babel-named-src-block-regexp-for-name (&optional name)
  468. "This generates a regexp used to match a src block named NAME.
  469. If NAME is nil, match any name. Matched name is then put in
  470. match group 9. Other match groups are defined in
  471. `org-babel-src-block-regexp'."
  472. (concat org-babel-src-name-regexp
  473. (concat (if name (regexp-quote name) "\\(?9:.*?\\)") "[ \t]*" )
  474. "\\(?:\n[ \t]*#\\+\\S-+:.*\\)*?"
  475. "\n"
  476. (substring org-babel-src-block-regexp 1)))
  477. (defun org-babel-named-data-regexp-for-name (name)
  478. "This generates a regexp used to match data named NAME."
  479. (concat org-babel-name-regexp (regexp-quote name) "[ \t]*$"))
  480. ;;; functions
  481. (defvar call-process-region)
  482. (defvar org-babel-current-src-block-location nil
  483. "Marker pointing to the src block currently being executed.
  484. This may also point to a call line or an inline code block. If
  485. multiple blocks are being executed (e.g., in chained execution
  486. through use of the :var header argument) this marker points to
  487. the outer-most code block.")
  488. (defvar *this*)
  489. (defun org-babel-get-src-block-info (&optional light datum)
  490. "Extract information from a source block or inline source block.
  491. Optional argument LIGHT does not resolve remote variable
  492. references; a process which could likely result in the execution
  493. of other code blocks.
  494. By default, consider the block at point. However, when optional
  495. argument DATUM is provided, extract information from that parsed
  496. object instead.
  497. Return nil if point is not on a source block. Otherwise, return
  498. a list with the following pattern:
  499. \(language body header-arguments-alist switches name block-head)"
  500. (let* ((datum (or datum (org-element-context)))
  501. (type (org-element-type datum))
  502. (inline (eq type 'inline-src-block)))
  503. (when (memq type '(inline-src-block src-block))
  504. (let* ((lang (org-element-property :language datum))
  505. (lang-headers (intern
  506. (concat "org-babel-default-header-args:" lang)))
  507. (name (org-element-property :name datum))
  508. (info
  509. (list
  510. lang
  511. ;; Normalize contents. In particular, remove spurious
  512. ;; indentation and final newline character.
  513. (let* ((value (org-element-property :value datum))
  514. (body (if (and (> (length value) 1)
  515. (string-match-p "\n\\'" value))
  516. (substring value 0 -1)
  517. value)))
  518. (cond (inline
  519. ;; Newline characters and indentation in an
  520. ;; inline src-block are not meaningful, since
  521. ;; they could come from some paragraph
  522. ;; filling. Treat them as a white space.
  523. (replace-regexp-in-string "\n[ \t]*" " " body))
  524. ((or org-src-preserve-indentation
  525. (org-element-property :preserve-indent datum))
  526. body)
  527. (t (org-remove-indentation body))))
  528. (apply #'org-babel-merge-params
  529. (if inline org-babel-default-inline-header-args
  530. org-babel-default-header-args)
  531. (and (boundp lang-headers) (symbol-value lang-headers))
  532. (append
  533. ;; If DATUM is provided, make sure we get node
  534. ;; properties applicable to its location within
  535. ;; the document.
  536. (org-with-wide-buffer
  537. (when datum
  538. (goto-char (org-element-property :begin datum)))
  539. (org-babel-params-from-properties lang))
  540. (mapcar #'org-babel-parse-header-arguments
  541. (cons
  542. (org-element-property :parameters datum)
  543. (org-element-property :header datum)))))
  544. (or (org-element-property :switches datum) "")
  545. name
  546. (org-element-property (if inline :begin :post-affiliated)
  547. datum))))
  548. (unless light
  549. (setf (nth 2 info) (org-babel-process-params (nth 2 info))))
  550. (setf (nth 2 info) (org-babel-generate-file-param name (nth 2 info)))
  551. info))))
  552. ;;;###autoload
  553. (defun org-babel-execute-src-block (&optional arg info params)
  554. "Execute the current source code block.
  555. Insert the results of execution into the buffer. Source code
  556. execution and the collection and formatting of results can be
  557. controlled through a variety of header arguments.
  558. With prefix argument ARG, force re-execution even if an existing
  559. result cached in the buffer would otherwise have been returned.
  560. Optionally supply a value for INFO in the form returned by
  561. `org-babel-get-src-block-info'.
  562. Optionally supply a value for PARAMS which will be merged with
  563. the header arguments specified at the front of the source code
  564. block."
  565. (interactive)
  566. (let* ((org-babel-current-src-block-location
  567. (or org-babel-current-src-block-location
  568. (nth 5 info)
  569. (org-babel-where-is-src-block-head)))
  570. (info (if info (copy-tree info) (org-babel-get-src-block-info))))
  571. (cl-callf org-babel-merge-params (nth 2 info) params)
  572. (when (org-babel-check-evaluate info)
  573. (cl-callf org-babel-process-params (nth 2 info))
  574. (let* ((params (nth 2 info))
  575. (cachep (and (not arg) (cdr (assoc :cache params))
  576. (string= "yes" (cdr (assoc :cache params)))))
  577. (new-hash (when cachep (org-babel-sha1-hash info)))
  578. (old-hash (when cachep (org-babel-current-result-hash)))
  579. (cache-current-p (and (not arg) new-hash
  580. (equal new-hash old-hash))))
  581. (cond
  582. (cache-current-p
  583. (save-excursion ;; return cached result
  584. (goto-char (org-babel-where-is-src-block-result nil info))
  585. (forward-line)
  586. (skip-chars-forward " \t")
  587. (let ((result (org-babel-read-result)))
  588. (message (replace-regexp-in-string
  589. "%" "%%" (format "%S" result)))
  590. result)))
  591. ((org-babel-confirm-evaluate info)
  592. (let* ((lang (nth 0 info))
  593. (result-params (cdr (assoc :result-params params)))
  594. (body (setf (nth 1 info)
  595. (if (org-babel-noweb-p params :eval)
  596. (org-babel-expand-noweb-references info)
  597. (nth 1 info))))
  598. (dir (cdr (assoc :dir params)))
  599. (default-directory
  600. (or (and dir (file-name-as-directory (expand-file-name dir)))
  601. default-directory))
  602. (org-babel-call-process-region-original ;; for tramp handler
  603. (or (org-bound-and-true-p
  604. org-babel-call-process-region-original)
  605. (symbol-function 'call-process-region)))
  606. result cmd)
  607. (unwind-protect
  608. (let ((call-process-region
  609. (lambda (&rest args)
  610. (apply 'org-babel-tramp-handle-call-process-region
  611. args))))
  612. (let ((lang-check
  613. (lambda (f)
  614. (let ((f (intern (concat "org-babel-execute:" f))))
  615. (when (fboundp f) f)))))
  616. (setq cmd
  617. (or (funcall lang-check lang)
  618. (funcall lang-check
  619. (symbol-name
  620. (cdr (assoc lang org-src-lang-modes))))
  621. (error "No org-babel-execute function for %s!"
  622. lang))))
  623. (message "executing %s code block%s..."
  624. (capitalize lang)
  625. (if (nth 4 info) (format " (%s)" (nth 4 info)) ""))
  626. (if (member "none" result-params)
  627. (progn
  628. (funcall cmd body params)
  629. (message "result silenced")
  630. (setq result nil))
  631. (setq result
  632. (let ((result (funcall cmd body params)))
  633. (if (and (eq (cdr (assoc :result-type params))
  634. 'value)
  635. (or (member "vector" result-params)
  636. (member "table" result-params))
  637. (not (listp result)))
  638. (list (list result)) result)))
  639. ;; If non-empty result and :file then write to :file.
  640. (when (cdr (assoc :file params))
  641. (when result
  642. (with-temp-file (cdr (assoc :file params))
  643. (insert
  644. (org-babel-format-result
  645. result (cdr (assoc :sep (nth 2 info)))))))
  646. (setq result (cdr (assoc :file params))))
  647. ;; Possibly perform post process provided its appropriate.
  648. (when (cdr (assoc :post params))
  649. (let ((*this* (if (cdr (assoc :file params))
  650. (org-babel-result-to-file
  651. (cdr (assoc :file params))
  652. (when (assoc :file-desc params)
  653. (or (cdr (assoc :file-desc params))
  654. result)))
  655. result)))
  656. (setq result (org-babel-ref-resolve
  657. (cdr (assoc :post params))))
  658. (when (cdr (assoc :file params))
  659. (setq result-params
  660. (remove "file" result-params)))))
  661. (org-babel-insert-result
  662. result result-params info new-hash lang))
  663. (run-hooks 'org-babel-after-execute-hook)
  664. result)
  665. (setq call-process-region
  666. 'org-babel-call-process-region-original)))))))))
  667. (defun org-babel-expand-body:generic (body params &optional var-lines)
  668. "Expand BODY with PARAMS.
  669. Expand a block of code with org-babel according to its header
  670. arguments. This generic implementation of body expansion is
  671. called for languages which have not defined their own specific
  672. org-babel-expand-body:lang function."
  673. (let ((pro (cdr (assoc :prologue params)))
  674. (epi (cdr (assoc :epilogue params))))
  675. (mapconcat #'identity
  676. (append (when pro (list pro))
  677. var-lines
  678. (list body)
  679. (when epi (list epi)))
  680. "\n")))
  681. ;;;###autoload
  682. (defun org-babel-expand-src-block (&optional _arg info params)
  683. "Expand the current source code block.
  684. Expand according to the source code block's header
  685. arguments and pop open the results in a preview buffer."
  686. (interactive)
  687. (let* ((info (or info (org-babel-get-src-block-info)))
  688. (lang (nth 0 info))
  689. (params (setf (nth 2 info)
  690. (sort (org-babel-merge-params (nth 2 info) params)
  691. (lambda (el1 el2) (string< (symbol-name (car el1))
  692. (symbol-name (car el2)))))))
  693. (body (setf (nth 1 info)
  694. (if (org-babel-noweb-p params :eval)
  695. (org-babel-expand-noweb-references info) (nth 1 info))))
  696. (expand-cmd (intern (concat "org-babel-expand-body:" lang)))
  697. (assignments-cmd (intern (concat "org-babel-variable-assignments:"
  698. lang)))
  699. (expanded
  700. (if (fboundp expand-cmd) (funcall expand-cmd body params)
  701. (org-babel-expand-body:generic
  702. body params (and (fboundp assignments-cmd)
  703. (funcall assignments-cmd params))))))
  704. (if (org-called-interactively-p 'any)
  705. (org-edit-src-code
  706. expanded (concat "*Org-Babel Preview " (buffer-name) "[ " lang " ]*"))
  707. expanded)))
  708. (defun org-babel-edit-distance (s1 s2)
  709. "Return the edit (levenshtein) distance between strings S1 S2."
  710. (let* ((l1 (length s1))
  711. (l2 (length s2))
  712. (dist (vconcat (mapcar (lambda (_) (make-vector (1+ l2) nil))
  713. (number-sequence 1 (1+ l1)))))
  714. (in (lambda (i j) (aref (aref dist i) j))))
  715. (setf (aref (aref dist 0) 0) 0)
  716. (dolist (j (number-sequence 1 l2))
  717. (setf (aref (aref dist 0) j) j))
  718. (dolist (i (number-sequence 1 l1))
  719. (setf (aref (aref dist i) 0) i)
  720. (dolist (j (number-sequence 1 l2))
  721. (setf (aref (aref dist i) j)
  722. (min
  723. (1+ (funcall in (1- i) j))
  724. (1+ (funcall in i (1- j)))
  725. (+ (if (equal (aref s1 (1- i)) (aref s2 (1- j))) 0 1)
  726. (funcall in (1- i) (1- j)))))))
  727. (funcall in l1 l2)))
  728. (defun org-babel-combine-header-arg-lists (original &rest others)
  729. "Combine a number of lists of header argument names and arguments."
  730. (let ((results (copy-sequence original)))
  731. (dolist (new-list others)
  732. (dolist (arg-pair new-list)
  733. (let ((header (car arg-pair)))
  734. (setq results
  735. (cons arg-pair (cl-remove-if
  736. (lambda (pair) (equal header (car pair)))
  737. results))))))
  738. results))
  739. ;;;###autoload
  740. (defun org-babel-check-src-block ()
  741. "Check for misspelled header arguments in the current code block."
  742. (interactive)
  743. ;; TODO: report malformed code block
  744. ;; TODO: report incompatible combinations of header arguments
  745. ;; TODO: report uninitialized variables
  746. (let ((too-close 2) ;; <- control closeness to report potential match
  747. (names (mapcar #'symbol-name org-babel-header-arg-names)))
  748. (dolist (header (mapcar (lambda (arg) (substring (symbol-name (car arg)) 1))
  749. (and (org-babel-where-is-src-block-head)
  750. (org-babel-parse-header-arguments
  751. (org-no-properties
  752. (match-string 4))))))
  753. (dolist (name names)
  754. (when (and (not (string= header name))
  755. (<= (org-babel-edit-distance header name) too-close)
  756. (not (member header names)))
  757. (error "Supplied header \"%S\" is suspiciously close to \"%S\""
  758. header name))))
  759. (message "No suspicious header arguments found.")))
  760. ;;;###autoload
  761. (defun org-babel-insert-header-arg (&optional header-arg value)
  762. "Insert a header argument selecting from lists of common args and values."
  763. (interactive)
  764. (let* ((info (org-babel-get-src-block-info 'light))
  765. (lang (car info))
  766. (begin (nth 5 info))
  767. (lang-headers (intern (concat "org-babel-header-args:" lang)))
  768. (headers (org-babel-combine-header-arg-lists
  769. org-babel-common-header-args-w-values
  770. (when (boundp lang-headers) (eval lang-headers))))
  771. (header-arg (or header-arg
  772. (completing-read
  773. "Header Arg: "
  774. (mapcar
  775. (lambda (header-spec) (symbol-name (car header-spec)))
  776. headers))))
  777. (vals (cdr (assoc (intern header-arg) headers)))
  778. (value (or value
  779. (cond
  780. ((eq vals :any)
  781. (read-from-minibuffer "value: "))
  782. ((listp vals)
  783. (mapconcat
  784. (lambda (group)
  785. (let ((arg (completing-read
  786. "Value: "
  787. (cons "default"
  788. (mapcar #'symbol-name group)))))
  789. (if (and arg (not (string= "default" arg)))
  790. (concat arg " ")
  791. "")))
  792. vals ""))))))
  793. (save-excursion
  794. (goto-char begin)
  795. (goto-char (point-at-eol))
  796. (unless (= (char-before (point)) ?\ ) (insert " "))
  797. (insert ":" header-arg) (when value (insert " " value)))))
  798. ;; Add support for completing-read insertion of header arguments after ":"
  799. (defun org-babel-header-arg-expand ()
  800. "Call `org-babel-enter-header-arg-w-completion' in appropriate contexts."
  801. (when (and (equal (char-before) ?\:) (org-babel-where-is-src-block-head))
  802. (org-babel-enter-header-arg-w-completion (match-string 2))))
  803. (defun org-babel-enter-header-arg-w-completion (&optional lang)
  804. "Insert header argument appropriate for LANG with completion."
  805. (let* ((lang-headers-var (intern (concat "org-babel-header-args:" lang)))
  806. (lang-headers (when (boundp lang-headers-var) (eval lang-headers-var)))
  807. (headers-w-values (org-babel-combine-header-arg-lists
  808. org-babel-common-header-args-w-values lang-headers))
  809. (headers (mapcar #'symbol-name (mapcar #'car headers-w-values)))
  810. (header (org-completing-read "Header Arg: " headers))
  811. (args (cdr (assoc (intern header) headers-w-values)))
  812. (arg (when (and args (listp args))
  813. (org-completing-read
  814. (format "%s: " header)
  815. (mapcar #'symbol-name (apply #'append args))))))
  816. (insert (concat header " " (or arg "")))
  817. (cons header arg)))
  818. (add-hook 'org-tab-first-hook 'org-babel-header-arg-expand)
  819. ;;;###autoload
  820. (defun org-babel-load-in-session (&optional _arg info)
  821. "Load the body of the current source-code block.
  822. Evaluate the header arguments for the source block before
  823. entering the session. After loading the body this pops open the
  824. session."
  825. (interactive)
  826. (let* ((info (or info (org-babel-get-src-block-info)))
  827. (lang (nth 0 info))
  828. (params (nth 2 info))
  829. (body (if (not info)
  830. (user-error "No src code block at point")
  831. (setf (nth 1 info)
  832. (if (org-babel-noweb-p params :eval)
  833. (org-babel-expand-noweb-references info)
  834. (nth 1 info)))))
  835. (session (cdr (assoc :session params)))
  836. (dir (cdr (assoc :dir params)))
  837. (default-directory
  838. (or (and dir (file-name-as-directory dir)) default-directory))
  839. (cmd (intern (concat "org-babel-load-session:" lang))))
  840. (unless (fboundp cmd)
  841. (error "No org-babel-load-session function for %s!" lang))
  842. (pop-to-buffer (funcall cmd session body params))
  843. (end-of-line 1)))
  844. ;;;###autoload
  845. (defun org-babel-initiate-session (&optional arg info)
  846. "Initiate session for current code block.
  847. If called with a prefix argument then resolve any variable
  848. references in the header arguments and assign these variables in
  849. the session. Copy the body of the code block to the kill ring."
  850. (interactive "P")
  851. (let* ((info (or info (org-babel-get-src-block-info (not arg))))
  852. (lang (nth 0 info))
  853. (body (nth 1 info))
  854. (params (nth 2 info))
  855. (session (cdr (assoc :session params)))
  856. (dir (cdr (assoc :dir params)))
  857. (default-directory
  858. (or (and dir (file-name-as-directory dir)) default-directory))
  859. (init-cmd (intern (format "org-babel-%s-initiate-session" lang)))
  860. (prep-cmd (intern (concat "org-babel-prep-session:" lang))))
  861. (if (and (stringp session) (string= session "none"))
  862. (error "This block is not using a session!"))
  863. (unless (fboundp init-cmd)
  864. (error "No org-babel-initiate-session function for %s!" lang))
  865. (with-temp-buffer (insert (org-babel-trim body))
  866. (copy-region-as-kill (point-min) (point-max)))
  867. (when arg
  868. (unless (fboundp prep-cmd)
  869. (error "No org-babel-prep-session function for %s!" lang))
  870. (funcall prep-cmd session params))
  871. (funcall init-cmd session params)))
  872. ;;;###autoload
  873. (defun org-babel-switch-to-session (&optional arg info)
  874. "Switch to the session of the current code block.
  875. Uses `org-babel-initiate-session' to start the session. If called
  876. with a prefix argument then this is passed on to
  877. `org-babel-initiate-session'."
  878. (interactive "P")
  879. (pop-to-buffer (org-babel-initiate-session arg info))
  880. (end-of-line 1))
  881. (defalias 'org-babel-pop-to-session 'org-babel-switch-to-session)
  882. (defvar org-src-window-setup)
  883. ;;;###autoload
  884. (defun org-babel-switch-to-session-with-code (&optional arg _info)
  885. "Switch to code buffer and display session."
  886. (interactive "P")
  887. (let ((swap-windows
  888. (lambda ()
  889. (let ((other-window-buffer (window-buffer (next-window))))
  890. (set-window-buffer (next-window) (current-buffer))
  891. (set-window-buffer (selected-window) other-window-buffer))
  892. (other-window 1)))
  893. (info (org-babel-get-src-block-info))
  894. (org-src-window-setup 'reorganize-frame))
  895. (save-excursion
  896. (org-babel-switch-to-session arg info))
  897. (org-edit-src-code)
  898. (funcall swap-windows)))
  899. ;;;###autoload
  900. (defmacro org-babel-do-in-edit-buffer (&rest body)
  901. "Evaluate BODY in edit buffer if there is a code block at point.
  902. Return t if a code block was found at point, nil otherwise."
  903. `(let ((org-src-window-setup 'switch-invisibly))
  904. (when (and (org-babel-where-is-src-block-head)
  905. (org-edit-src-code))
  906. (unwind-protect (progn ,@body)
  907. (org-edit-src-exit))
  908. t)))
  909. (def-edebug-spec org-babel-do-in-edit-buffer (body))
  910. (defun org-babel-do-key-sequence-in-edit-buffer (key)
  911. "Read key sequence and execute the command in edit buffer.
  912. Enter a key sequence to be executed in the language major-mode
  913. edit buffer. For example, TAB will alter the contents of the
  914. Org-mode code block according to the effect of TAB in the
  915. language major-mode buffer. For languages that support
  916. interactive sessions, this can be used to send code from the Org
  917. buffer to the session for evaluation using the native major-mode
  918. evaluation mechanisms."
  919. (interactive "kEnter key-sequence to execute in edit buffer: ")
  920. (org-babel-do-in-edit-buffer
  921. (call-interactively
  922. (key-binding (or key (read-key-sequence nil))))))
  923. (defvar org-bracket-link-regexp)
  924. (defun org-babel-active-location-p ()
  925. (memq (org-element-type (save-match-data (org-element-context)))
  926. '(babel-call inline-babel-call inline-src-block src-block)))
  927. ;;;###autoload
  928. (defun org-babel-open-src-block-result (&optional re-run)
  929. "If `point' is on a src block then open the results of the
  930. source code block, otherwise return nil. With optional prefix
  931. argument RE-RUN the source-code block is evaluated even if
  932. results already exist."
  933. (interactive "P")
  934. (let ((info (org-babel-get-src-block-info 'light)))
  935. (when info
  936. (save-excursion
  937. ;; go to the results, if there aren't any then run the block
  938. (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
  939. (progn (org-babel-execute-src-block)
  940. (org-babel-where-is-src-block-result))))
  941. (end-of-line 1)
  942. (while (looking-at "[\n\r\t\f ]") (forward-char 1))
  943. ;; open the results
  944. (if (looking-at org-bracket-link-regexp)
  945. ;; file results
  946. (org-open-at-point)
  947. (let ((r (org-babel-format-result
  948. (org-babel-read-result) (cdr (assoc :sep (nth 2 info))))))
  949. (pop-to-buffer (get-buffer-create "*Org-Babel Results*"))
  950. (delete-region (point-min) (point-max))
  951. (insert r)))
  952. t))))
  953. ;;;###autoload
  954. (defmacro org-babel-map-src-blocks (file &rest body)
  955. "Evaluate BODY forms on each source-block in FILE.
  956. If FILE is nil evaluate BODY forms on source blocks in current
  957. buffer. During evaluation of BODY the following local variables
  958. are set relative to the currently matched code block.
  959. full-block ------- string holding the entirety of the code block
  960. beg-block -------- point at the beginning of the code block
  961. end-block -------- point at the end of the matched code block
  962. lang ------------- string holding the language of the code block
  963. beg-lang --------- point at the beginning of the lang
  964. end-lang --------- point at the end of the lang
  965. switches --------- string holding the switches
  966. beg-switches ----- point at the beginning of the switches
  967. end-switches ----- point at the end of the switches
  968. header-args ------ string holding the header-args
  969. beg-header-args -- point at the beginning of the header-args
  970. end-header-args -- point at the end of the header-args
  971. body ------------- string holding the body of the code block
  972. beg-body --------- point at the beginning of the body
  973. end-body --------- point at the end of the body"
  974. (declare (indent 1))
  975. (let ((tempvar (make-symbol "file")))
  976. `(let* ((case-fold-search t)
  977. (,tempvar ,file)
  978. (visited-p (or (null ,tempvar)
  979. (get-file-buffer (expand-file-name ,tempvar))))
  980. (point (point)) to-be-removed)
  981. (save-window-excursion
  982. (when ,tempvar (find-file ,tempvar))
  983. (setq to-be-removed (current-buffer))
  984. (goto-char (point-min))
  985. (while (re-search-forward org-babel-src-block-regexp nil t)
  986. (when (org-babel-active-location-p)
  987. (goto-char (match-beginning 0))
  988. (let ((full-block (match-string 0))
  989. (beg-block (match-beginning 0))
  990. (end-block (match-end 0))
  991. (lang (match-string 2))
  992. (beg-lang (match-beginning 2))
  993. (end-lang (match-end 2))
  994. (switches (match-string 3))
  995. (beg-switches (match-beginning 3))
  996. (end-switches (match-end 3))
  997. (header-args (match-string 4))
  998. (beg-header-args (match-beginning 4))
  999. (end-header-args (match-end 4))
  1000. (body (match-string 5))
  1001. (beg-body (match-beginning 5))
  1002. (end-body (match-end 5)))
  1003. ;; Silence byte-compiler in case `body' doesn't use all
  1004. ;; those variables.
  1005. (ignore full-block beg-block end-block lang
  1006. beg-lang end-lang switches beg-switches
  1007. end-switches header-args beg-header-args
  1008. end-header-args body beg-body end-body)
  1009. ,@body
  1010. (goto-char end-block)))))
  1011. (unless visited-p (kill-buffer to-be-removed))
  1012. (goto-char point))))
  1013. (def-edebug-spec org-babel-map-src-blocks (form body))
  1014. ;;;###autoload
  1015. (defmacro org-babel-map-inline-src-blocks (file &rest body)
  1016. "Evaluate BODY forms on each inline source block in FILE.
  1017. If FILE is nil evaluate BODY forms on source blocks in current
  1018. buffer."
  1019. (declare (indent 1) (debug (form body)))
  1020. (org-with-gensyms (datum end point tempvar to-be-removed visitedp)
  1021. `(let* ((case-fold-search t)
  1022. (,tempvar ,file)
  1023. (,visitedp (or (null ,tempvar)
  1024. (get-file-buffer (expand-file-name ,tempvar))))
  1025. (,point (point))
  1026. ,to-be-removed)
  1027. (save-window-excursion
  1028. (when ,tempvar (find-file ,tempvar))
  1029. (setq ,to-be-removed (current-buffer))
  1030. (goto-char (point-min))
  1031. (while (re-search-forward "src_\\S-" nil t)
  1032. (let ((,datum (save-match-data (org-element-context))))
  1033. (when (eq (org-element-type ,datum) 'inline-src-block)
  1034. (goto-char (match-beginning 0))
  1035. (let ((,end (copy-marker (org-element-property :end ,datum))))
  1036. ,@body
  1037. (goto-char ,end)
  1038. (set-marker ,end nil))))))
  1039. (unless ,visitedp (kill-buffer ,to-be-removed))
  1040. (goto-char ,point))))
  1041. ;;;###autoload
  1042. (defmacro org-babel-map-call-lines (file &rest body)
  1043. "Evaluate BODY forms on each call line in FILE.
  1044. If FILE is nil evaluate BODY forms on source blocks in current
  1045. buffer."
  1046. (declare (indent 1) (debug (form body)))
  1047. (org-with-gensyms (datum end point tempvar to-be-removed visitedp)
  1048. `(let* ((case-fold-search t)
  1049. (,tempvar ,file)
  1050. (,visitedp (or (null ,tempvar)
  1051. (get-file-buffer (expand-file-name ,tempvar))))
  1052. (,point (point))
  1053. ,to-be-removed)
  1054. (save-window-excursion
  1055. (when ,tempvar (find-file ,tempvar))
  1056. (setq ,to-be-removed (current-buffer))
  1057. (goto-char (point-min))
  1058. (while (re-search-forward "call_\\S-\\|^[ \t]*#\\+CALL:" nil t)
  1059. (let ((,datum (save-match-data (org-element-context))))
  1060. (when (memq (org-element-type ,datum)
  1061. '(babel-call inline-babel-call))
  1062. (goto-char (match-beginning 0))
  1063. (let ((,end (copy-marker (org-element-property :end ,datum))))
  1064. ,@body
  1065. (goto-char ,end)
  1066. (set-marker ,end nil))))))
  1067. (unless ,visitedp (kill-buffer ,to-be-removed))
  1068. (goto-char ,point))))
  1069. ;;;###autoload
  1070. (defmacro org-babel-map-executables (file &rest body)
  1071. "Evaluate BODY forms on each active Babel code in FILE.
  1072. If FILE is nil evaluate BODY forms on source blocks in current
  1073. buffer."
  1074. (declare (indent 1) (debug (form body)))
  1075. (org-with-gensyms (datum end point tempvar to-be-removed visitedp)
  1076. `(let* ((case-fold-search t)
  1077. (,tempvar ,file)
  1078. (,visitedp (or (null ,tempvar)
  1079. (get-file-buffer (expand-file-name ,tempvar))))
  1080. (,point (point))
  1081. ,to-be-removed)
  1082. (save-window-excursion
  1083. (when ,tempvar (find-file ,tempvar))
  1084. (setq ,to-be-removed (current-buffer))
  1085. (goto-char (point-min))
  1086. (while (re-search-forward
  1087. "\\(call\\|src\\)_\\|^[ \t]*#\\+\\(BEGIN_SRC\\|CALL:\\)" nil t)
  1088. (let ((,datum (save-match-data (org-element-context))))
  1089. (when (memq (org-element-type ,datum)
  1090. '(babel-call inline-babel-call inline-src-block
  1091. src-block))
  1092. (goto-char (match-beginning 0))
  1093. (let ((,end (copy-marker (org-element-property :end ,datum))))
  1094. ,@body
  1095. (goto-char ,end)
  1096. (set-marker ,end nil))))))
  1097. (unless ,visitedp (kill-buffer ,to-be-removed))
  1098. (goto-char ,point))))
  1099. ;;;###autoload
  1100. (defun org-babel-execute-buffer (&optional arg)
  1101. "Execute source code blocks in a buffer.
  1102. Call `org-babel-execute-src-block' on every source block in
  1103. the current buffer."
  1104. (interactive "P")
  1105. (org-babel-eval-wipe-error-buffer)
  1106. (org-save-outline-visibility t
  1107. (org-babel-map-executables nil
  1108. (if (memq (org-element-type (org-element-context))
  1109. '(babel-call inline-babel-call))
  1110. (org-babel-lob-execute-maybe)
  1111. (org-babel-execute-src-block arg)))))
  1112. ;;;###autoload
  1113. (defun org-babel-execute-subtree (&optional arg)
  1114. "Execute source code blocks in a subtree.
  1115. Call `org-babel-execute-src-block' on every source block in
  1116. the current subtree."
  1117. (interactive "P")
  1118. (save-restriction
  1119. (save-excursion
  1120. (org-narrow-to-subtree)
  1121. (org-babel-execute-buffer arg)
  1122. (widen))))
  1123. ;;;###autoload
  1124. (defun org-babel-sha1-hash (&optional info)
  1125. "Generate an sha1 hash based on the value of info."
  1126. (interactive)
  1127. (let ((print-level nil)
  1128. (info (or info (org-babel-get-src-block-info))))
  1129. (setf (nth 2 info)
  1130. (sort (copy-sequence (nth 2 info))
  1131. (lambda (a b) (string< (car a) (car b)))))
  1132. (let* ((rm (lambda (lst)
  1133. (dolist (p '("replace" "silent" "none"
  1134. "append" "prepend"))
  1135. (setq lst (remove p lst)))
  1136. lst))
  1137. (norm (lambda (arg)
  1138. (let ((v (if (and (listp (cdr arg)) (null (cddr arg)))
  1139. (copy-sequence (cdr arg))
  1140. (cdr arg))))
  1141. (when (and v (not (and (sequencep v)
  1142. (not (consp v))
  1143. (= (length v) 0))))
  1144. (cond
  1145. ((and (listp v) ; lists are sorted
  1146. (member (car arg) '(:result-params)))
  1147. (sort (funcall rm v) #'string<))
  1148. ((and (stringp v) ; strings are sorted
  1149. (member (car arg) '(:results :exports)))
  1150. (mapconcat #'identity (sort (funcall rm (split-string v))
  1151. #'string<) " "))
  1152. (t v))))))
  1153. ;; expanded body
  1154. (lang (nth 0 info))
  1155. (params (nth 2 info))
  1156. (body (if (org-babel-noweb-p params :eval)
  1157. (org-babel-expand-noweb-references info) (nth 1 info)))
  1158. (expand-cmd (intern (concat "org-babel-expand-body:" lang)))
  1159. (assignments-cmd (intern (concat "org-babel-variable-assignments:"
  1160. lang)))
  1161. (expanded
  1162. (if (fboundp expand-cmd) (funcall expand-cmd body params)
  1163. (org-babel-expand-body:generic
  1164. body params (and (fboundp assignments-cmd)
  1165. (funcall assignments-cmd params))))))
  1166. (let* ((it (format "%s-%s"
  1167. (mapconcat
  1168. #'identity
  1169. (delq nil (mapcar (lambda (arg)
  1170. (let ((normalized (funcall norm arg)))
  1171. (when normalized
  1172. (format "%S" normalized))))
  1173. (nth 2 info))) ":")
  1174. expanded))
  1175. (hash (sha1 it)))
  1176. (when (org-called-interactively-p 'interactive) (message hash))
  1177. hash))))
  1178. (defun org-babel-current-result-hash (&optional info)
  1179. "Return the current in-buffer hash."
  1180. (let ((result (org-babel-where-is-src-block-result nil info)))
  1181. (when result
  1182. (org-with-wide-buffer
  1183. (goto-char result)
  1184. (looking-at org-babel-result-regexp)
  1185. (match-string-no-properties 1)))))
  1186. (defun org-babel-set-current-result-hash (hash info)
  1187. "Set the current in-buffer hash to HASH."
  1188. (org-with-wide-buffer
  1189. (goto-char (org-babel-where-is-src-block-result nil info))
  1190. (looking-at org-babel-result-regexp)
  1191. (goto-char (match-beginning 1))
  1192. (mapc #'delete-overlay (overlays-at (point)))
  1193. (forward-char org-babel-hash-show)
  1194. (mapc #'delete-overlay (overlays-at (point)))
  1195. (replace-match hash nil nil nil 1)
  1196. (beginning-of-line)
  1197. (org-babel-hide-hash)))
  1198. (defun org-babel-hide-hash ()
  1199. "Hide the hash in the current results line.
  1200. Only the initial `org-babel-hash-show' characters of the hash
  1201. will remain visible."
  1202. (add-to-invisibility-spec '(org-babel-hide-hash . t))
  1203. (save-excursion
  1204. (when (and (re-search-forward org-babel-result-regexp nil t)
  1205. (match-string 1))
  1206. (let* ((start (match-beginning 1))
  1207. (hide-start (+ org-babel-hash-show start))
  1208. (end (match-end 1))
  1209. (hash (match-string 1))
  1210. ov1 ov2)
  1211. (setq ov1 (make-overlay start hide-start))
  1212. (setq ov2 (make-overlay hide-start end))
  1213. (overlay-put ov2 'invisible 'org-babel-hide-hash)
  1214. (overlay-put ov1 'babel-hash hash)))))
  1215. (defun org-babel-hide-all-hashes ()
  1216. "Hide the hash in the current buffer.
  1217. Only the initial `org-babel-hash-show' characters of each hash
  1218. will remain visible. This function should be called as part of
  1219. the `org-mode-hook'."
  1220. (save-excursion
  1221. (while (and (not org-babel-hash-show-time)
  1222. (re-search-forward org-babel-result-regexp nil t))
  1223. (goto-char (match-beginning 0))
  1224. (org-babel-hide-hash)
  1225. (goto-char (match-end 0)))))
  1226. (add-hook 'org-mode-hook 'org-babel-hide-all-hashes)
  1227. (defun org-babel-hash-at-point (&optional point)
  1228. "Return the value of the hash at POINT.
  1229. \\<org-mode-map>\
  1230. The hash is also added as the last element of the kill ring.
  1231. This can be called with \\[org-ctrl-c-ctrl-c]."
  1232. (interactive)
  1233. (let ((hash (car (delq nil (mapcar
  1234. (lambda (ol) (overlay-get ol 'babel-hash))
  1235. (overlays-at (or point (point))))))))
  1236. (when hash (kill-new hash) (message hash))))
  1237. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
  1238. (defun org-babel-result-hide-spec ()
  1239. "Hide portions of results lines.
  1240. Add `org-babel-hide-result' as an invisibility spec for hiding
  1241. portions of results lines."
  1242. (add-to-invisibility-spec '(org-babel-hide-result . t)))
  1243. (add-hook 'org-mode-hook 'org-babel-result-hide-spec)
  1244. (defvar org-babel-hide-result-overlays nil
  1245. "Overlays hiding results.")
  1246. (defun org-babel-result-hide-all ()
  1247. "Fold all results in the current buffer."
  1248. (interactive)
  1249. (org-babel-show-result-all)
  1250. (save-excursion
  1251. (while (re-search-forward org-babel-result-regexp nil t)
  1252. (save-excursion (goto-char (match-beginning 0))
  1253. (org-babel-hide-result-toggle-maybe)))))
  1254. (defun org-babel-show-result-all ()
  1255. "Unfold all results in the current buffer."
  1256. (mapc 'delete-overlay org-babel-hide-result-overlays)
  1257. (setq org-babel-hide-result-overlays nil))
  1258. ;;;###autoload
  1259. (defun org-babel-hide-result-toggle-maybe ()
  1260. "Toggle visibility of result at point."
  1261. (interactive)
  1262. (let ((case-fold-search t))
  1263. (if (save-excursion
  1264. (beginning-of-line 1)
  1265. (looking-at org-babel-result-regexp))
  1266. (progn (org-babel-hide-result-toggle)
  1267. t) ;; to signal that we took action
  1268. nil))) ;; to signal that we did not
  1269. (defun org-babel-hide-result-toggle (&optional force)
  1270. "Toggle the visibility of the current result."
  1271. (interactive)
  1272. (save-excursion
  1273. (beginning-of-line)
  1274. (if (re-search-forward org-babel-result-regexp nil t)
  1275. (let ((start (progn (beginning-of-line 2) (- (point) 1)))
  1276. (end (progn
  1277. (while (looking-at org-babel-multi-line-header-regexp)
  1278. (forward-line 1))
  1279. (goto-char (- (org-babel-result-end) 1)) (point)))
  1280. ov)
  1281. (if (memq t (mapcar (lambda (overlay)
  1282. (eq (overlay-get overlay 'invisible)
  1283. 'org-babel-hide-result))
  1284. (overlays-at start)))
  1285. (if (or (not force) (eq force 'off))
  1286. (mapc (lambda (ov)
  1287. (when (member ov org-babel-hide-result-overlays)
  1288. (setq org-babel-hide-result-overlays
  1289. (delq ov org-babel-hide-result-overlays)))
  1290. (when (eq (overlay-get ov 'invisible)
  1291. 'org-babel-hide-result)
  1292. (delete-overlay ov)))
  1293. (overlays-at start)))
  1294. (setq ov (make-overlay start end))
  1295. (overlay-put ov 'invisible 'org-babel-hide-result)
  1296. ;; make the block accessible to isearch
  1297. (overlay-put
  1298. ov 'isearch-open-invisible
  1299. (lambda (ov)
  1300. (when (member ov org-babel-hide-result-overlays)
  1301. (setq org-babel-hide-result-overlays
  1302. (delq ov org-babel-hide-result-overlays)))
  1303. (when (eq (overlay-get ov 'invisible)
  1304. 'org-babel-hide-result)
  1305. (delete-overlay ov))))
  1306. (push ov org-babel-hide-result-overlays)))
  1307. (error "Not looking at a result line"))))
  1308. ;; org-tab-after-check-for-cycling-hook
  1309. (add-hook 'org-tab-first-hook 'org-babel-hide-result-toggle-maybe)
  1310. ;; Remove overlays when changing major mode
  1311. (add-hook 'org-mode-hook
  1312. (lambda () (org-add-hook 'change-major-mode-hook
  1313. 'org-babel-show-result-all 'append 'local)))
  1314. (defvar org-file-properties)
  1315. (defun org-babel-params-from-properties (&optional lang)
  1316. "Retrieve parameters specified as properties.
  1317. Return a list of association lists of source block params
  1318. specified in the properties of the current outline entry."
  1319. (save-match-data
  1320. (list
  1321. ;; DEPRECATED header arguments specified as separate property at
  1322. ;; point of definition.
  1323. (org-babel-parse-multiple-vars
  1324. (delq nil
  1325. (mapcar
  1326. (lambda (header)
  1327. (let* ((arg (symbol-name (car header)))
  1328. (val (org-entry-get (point) arg t)))
  1329. (and val
  1330. (cons (intern (concat ":" arg))
  1331. (org-babel-read val)))))
  1332. (org-babel-combine-header-arg-lists
  1333. org-babel-common-header-args-w-values
  1334. (let ((sym (intern (concat "org-babel-header-args:" lang))))
  1335. (and (boundp sym) (symbol-value sym)))))))
  1336. ;; header arguments specified with the header-args property at
  1337. ;; point of call.
  1338. (org-babel-parse-header-arguments
  1339. (org-entry-get org-babel-current-src-block-location
  1340. "header-args"
  1341. 'inherit))
  1342. (and lang ; language-specific header arguments at point of call
  1343. (org-babel-parse-header-arguments
  1344. (org-entry-get org-babel-current-src-block-location
  1345. (concat "header-args:" lang)
  1346. 'inherit))))))
  1347. (defun org-babel-balanced-split (string alts)
  1348. "Split STRING on instances of ALTS.
  1349. ALTS is a cons of two character options where each option may be
  1350. either the numeric code of a single character or a list of
  1351. character alternatives. For example to split on balanced
  1352. instances of \"[ \t]:\" set ALTS to ((32 9) . 58)."
  1353. (let* ((matches (lambda (ch spec) (if (listp spec) (member ch spec) (equal spec ch))))
  1354. (matched (lambda (ch last)
  1355. (if (consp alts)
  1356. (and (funcall matches ch (cdr alts))
  1357. (funcall matches last (car alts)))
  1358. (funcall matches ch alts))))
  1359. (balance 0) (last 0)
  1360. quote partial lst)
  1361. (mapc (lambda (ch) ; split on [], (), "" balanced instances of [ \t]:
  1362. (setq balance (+ balance
  1363. (cond ((or (equal 91 ch) (equal 40 ch)) 1)
  1364. ((or (equal 93 ch) (equal 41 ch)) -1)
  1365. (t 0))))
  1366. (when (and (equal 34 ch) (not (equal 92 last)))
  1367. (setq quote (not quote)))
  1368. (setq partial (cons ch partial))
  1369. (when (and (= balance 0) (not quote) (funcall matched ch last))
  1370. (setq lst (cons (apply #'string (nreverse
  1371. (if (consp alts)
  1372. (cddr partial)
  1373. (cdr partial))))
  1374. lst))
  1375. (setq partial nil))
  1376. (setq last ch))
  1377. (string-to-list string))
  1378. (nreverse (cons (apply #'string (nreverse partial)) lst))))
  1379. (defun org-babel-join-splits-near-ch (ch list)
  1380. "Join splits where \"=\" is on either end of the split."
  1381. (let ((last= (lambda (str) (= ch (aref str (1- (length str))))))
  1382. (first= (lambda (str) (= ch (aref str 0)))))
  1383. (reverse
  1384. (cl-reduce (lambda (acc el)
  1385. (let ((head (car acc)))
  1386. (if (and head (or (funcall last= head) (funcall first= el)))
  1387. (cons (concat head el) (cdr acc))
  1388. (cons el acc))))
  1389. list :initial-value nil))))
  1390. (defun org-babel-parse-header-arguments (arg-string)
  1391. "Parse a string of header arguments returning an alist."
  1392. (when (> (length arg-string) 0)
  1393. (org-babel-parse-multiple-vars
  1394. (delq nil
  1395. (mapcar
  1396. (lambda (arg)
  1397. (if (string-match
  1398. "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)"
  1399. arg)
  1400. (cons (intern (match-string 1 arg))
  1401. (org-babel-read (org-babel-chomp (match-string 2 arg))))
  1402. (cons (intern (org-babel-chomp arg)) nil)))
  1403. (let ((raw (org-babel-balanced-split arg-string '((32 9) . 58))))
  1404. (cons (car raw) (mapcar (lambda (r) (concat ":" r)) (cdr raw)))))))))
  1405. (defun org-babel-parse-multiple-vars (header-arguments)
  1406. "Expand multiple variable assignments behind a single :var keyword.
  1407. This allows expression of multiple variables with one :var as
  1408. shown below.
  1409. #+PROPERTY: var foo=1, bar=2"
  1410. (let (results)
  1411. (mapc (lambda (pair)
  1412. (if (eq (car pair) :var)
  1413. (mapcar (lambda (v) (push (cons :var (org-babel-trim v)) results))
  1414. (org-babel-join-splits-near-ch
  1415. 61 (org-babel-balanced-split (cdr pair) 32)))
  1416. (push pair results)))
  1417. header-arguments)
  1418. (nreverse results)))
  1419. (defun org-babel-process-params (params)
  1420. "Expand variables in PARAMS and add summary parameters."
  1421. (let* ((processed-vars (mapcar (lambda (el)
  1422. (if (consp el)
  1423. el
  1424. (org-babel-ref-parse el)))
  1425. (org-babel--get-vars params)))
  1426. (vars-and-names (if (and (assoc :colname-names params)
  1427. (assoc :rowname-names params))
  1428. (list processed-vars)
  1429. (org-babel-disassemble-tables
  1430. processed-vars
  1431. (cdr (assoc :hlines params))
  1432. (cdr (assoc :colnames params))
  1433. (cdr (assoc :rownames params)))))
  1434. (raw-result (or (cdr (assoc :results params)) ""))
  1435. (result-params (append
  1436. (split-string (if (stringp raw-result)
  1437. raw-result
  1438. (eval raw-result)))
  1439. (cdr (assoc :result-params params)))))
  1440. (append
  1441. (mapcar (lambda (var) (cons :var var)) (car vars-and-names))
  1442. (list
  1443. (cons :colname-names (or (cdr (assoc :colname-names params))
  1444. (cadr vars-and-names)))
  1445. (cons :rowname-names (or (cdr (assoc :rowname-names params))
  1446. (caddr vars-and-names)))
  1447. (cons :result-params result-params)
  1448. (cons :result-type (cond ((member "output" result-params) 'output)
  1449. ((member "value" result-params) 'value)
  1450. (t 'value))))
  1451. (cl-remove-if
  1452. (lambda (x) (memq (car x) '(:colname-names :rowname-names :result-params
  1453. :result-type :var)))
  1454. params))))
  1455. ;; row and column names
  1456. (defun org-babel-del-hlines (table)
  1457. "Remove all `hline's from TABLE."
  1458. (remq 'hline table))
  1459. (defun org-babel-get-colnames (table)
  1460. "Return the column names of TABLE.
  1461. Return a cons cell, the `car' of which contains the TABLE less
  1462. colnames, and the `cdr' of which contains a list of the column
  1463. names."
  1464. (if (equal 'hline (nth 1 table))
  1465. (cons (cddr table) (car table))
  1466. (cons (cdr table) (car table))))
  1467. (defun org-babel-get-rownames (table)
  1468. "Return the row names of TABLE.
  1469. Return a cons cell, the `car' of which contains the TABLE less
  1470. rownames, and the `cdr' of which contains a list of the rownames.
  1471. Note: this function removes any hlines in TABLE."
  1472. (let* ((table (org-babel-del-hlines table))
  1473. (rownames (funcall (lambda ()
  1474. (let ((tp table))
  1475. (mapcar
  1476. (lambda (_row)
  1477. (prog1
  1478. (pop (car tp))
  1479. (setq tp (cdr tp))))
  1480. table))))))
  1481. (cons table rownames)))
  1482. (defun org-babel-put-colnames (table colnames)
  1483. "Add COLNAMES to TABLE if they exist."
  1484. (if colnames (apply 'list colnames 'hline table) table))
  1485. (defun org-babel-put-rownames (table rownames)
  1486. "Add ROWNAMES to TABLE if they exist."
  1487. (if rownames
  1488. (mapcar (lambda (row)
  1489. (if (listp row)
  1490. (cons (or (pop rownames) "") row)
  1491. row)) table)
  1492. table))
  1493. (defun org-babel-pick-name (names selector)
  1494. "Select one out of an alist of row or column names.
  1495. SELECTOR can be either a list of names in which case those names
  1496. will be returned directly, or an index into the list NAMES in
  1497. which case the indexed names will be return."
  1498. (if (listp selector)
  1499. selector
  1500. (when names
  1501. (if (and selector (symbolp selector) (not (equal t selector)))
  1502. (cdr (assoc selector names))
  1503. (if (integerp selector)
  1504. (nth (- selector 1) names)
  1505. (cdr (car (last names))))))))
  1506. (defun org-babel-disassemble-tables (vars hlines colnames rownames)
  1507. "Parse tables for further processing.
  1508. Process the variables in VARS according to the HLINES,
  1509. ROWNAMES and COLNAMES header arguments. Return a list consisting
  1510. of the vars, cnames and rnames."
  1511. (let (cnames rnames)
  1512. (list
  1513. (mapcar
  1514. (lambda (var)
  1515. (when (listp (cdr var))
  1516. (when (and (not (equal colnames "no"))
  1517. (or colnames (and (equal (nth 1 (cdr var)) 'hline)
  1518. (not (member 'hline (cddr (cdr var)))))))
  1519. (let ((both (org-babel-get-colnames (cdr var))))
  1520. (setq cnames (cons (cons (car var) (cdr both))
  1521. cnames))
  1522. (setq var (cons (car var) (car both)))))
  1523. (when (and rownames (not (equal rownames "no")))
  1524. (let ((both (org-babel-get-rownames (cdr var))))
  1525. (setq rnames (cons (cons (car var) (cdr both))
  1526. rnames))
  1527. (setq var (cons (car var) (car both)))))
  1528. (when (and hlines (not (equal hlines "yes")))
  1529. (setq var (cons (car var) (org-babel-del-hlines (cdr var))))))
  1530. var)
  1531. vars)
  1532. (reverse cnames) (reverse rnames))))
  1533. (defun org-babel-reassemble-table (table colnames rownames)
  1534. "Add column and row names to a table.
  1535. Given a TABLE and set of COLNAMES and ROWNAMES add the names
  1536. to the table for reinsertion to org-mode."
  1537. (if (listp table)
  1538. (let ((table (if (and rownames (= (length table) (length rownames)))
  1539. (org-babel-put-rownames table rownames) table)))
  1540. (if (and colnames (listp (car table)) (= (length (car table))
  1541. (length colnames)))
  1542. (org-babel-put-colnames table colnames) table))
  1543. table))
  1544. (defun org-babel-where-is-src-block-head (&optional src-block)
  1545. "Find where the current source block begins.
  1546. If optional argument SRC-BLOCK is `src-block' type element, find
  1547. its current beginning instead.
  1548. Return the point at the beginning of the current source block.
  1549. Specifically at the beginning of the #+BEGIN_SRC line. Also set
  1550. match-data relatively to `org-babel-src-block-regexp', which see.
  1551. If the point is not on a source block then return nil."
  1552. (let ((element (or src-block (org-element-at-point))))
  1553. (when (eq (org-element-type element) 'src-block)
  1554. (let ((end (org-element-property :end element)))
  1555. (org-with-wide-buffer
  1556. ;; Ensure point is not on a blank line after the block.
  1557. (beginning-of-line)
  1558. (skip-chars-forward " \r\t\n" end)
  1559. (when (< (point) end)
  1560. (prog1 (goto-char (org-element-property :post-affiliated element))
  1561. (looking-at org-babel-src-block-regexp))))))))
  1562. ;;;###autoload
  1563. (defun org-babel-goto-src-block-head ()
  1564. "Go to the beginning of the current code block."
  1565. (interactive)
  1566. (let ((head (org-babel-where-is-src-block-head)))
  1567. (if head (goto-char head) (error "Not currently in a code block"))))
  1568. ;;;###autoload
  1569. (defun org-babel-goto-named-src-block (name)
  1570. "Go to a named source-code block."
  1571. (interactive
  1572. (let ((completion-ignore-case t)
  1573. (case-fold-search t)
  1574. (all-block-names (org-babel-src-block-names)))
  1575. (list (completing-read
  1576. "source-block name: " all-block-names nil t
  1577. (let* ((context (org-element-context))
  1578. (type (org-element-type context)))
  1579. (cond
  1580. ((and (memq type '(inline-src-block src-block)) ;<<noweb>>
  1581. (org-in-regexp (org-babel-noweb-wrap))))
  1582. ((memq type '(babel-call inline-babel-call)) ;#+CALL:
  1583. (org-element-property :call context))
  1584. ((org-element-property :results context)) ;#+RESULTS:
  1585. ((let ((symbol (thing-at-point 'symbol))) ;Symbol.
  1586. (and (member-ignore-case symbol all-block-names)
  1587. symbol)))
  1588. (t "")))))))
  1589. (let ((point (org-babel-find-named-block name)))
  1590. (if point
  1591. ;; Taken from `org-open-at-point'.
  1592. (progn (org-mark-ring-push) (goto-char point) (org-show-context))
  1593. (message "source-code block `%s' not found in this buffer" name))))
  1594. (defun org-babel-find-named-block (name)
  1595. "Find a named source-code block.
  1596. Return the location of the source block identified by source
  1597. NAME, or nil if no such block exists. Set match data according
  1598. to `org-babel-named-src-block-regexp'."
  1599. (save-excursion
  1600. (goto-char (point-min))
  1601. (ignore-errors
  1602. (org-next-block 1 nil (org-babel-named-src-block-regexp-for-name name)))))
  1603. (defun org-babel-src-block-names (&optional file)
  1604. "Returns the names of source blocks in FILE or the current buffer."
  1605. (when file (find-file file))
  1606. (save-excursion
  1607. (goto-char (point-min))
  1608. (let ((re (org-babel-named-src-block-regexp-for-name))
  1609. names)
  1610. (while (ignore-errors (org-next-block 1 nil re))
  1611. (push (org-match-string-no-properties 9) names))
  1612. names)))
  1613. ;;;###autoload
  1614. (defun org-babel-goto-named-result (name)
  1615. "Go to a named result."
  1616. (interactive
  1617. (let ((completion-ignore-case t))
  1618. (list (completing-read "Source-block name: "
  1619. (org-babel-result-names) nil t))))
  1620. (let ((point (org-babel-find-named-result name)))
  1621. (if point
  1622. ;; taken from `org-open-at-point'
  1623. (progn (goto-char point) (org-show-context))
  1624. (message "result `%s' not found in this buffer" name))))
  1625. (defun org-babel-find-named-result (name)
  1626. "Find a named result.
  1627. Return the location of the result named NAME in the current
  1628. buffer or nil if no such result exists."
  1629. (save-excursion
  1630. (goto-char (point-min))
  1631. (let ((case-fold-search t)
  1632. (re (format "^[ \t]*#\\+%s.*?:[ \t]*%s[ \t]*$"
  1633. org-babel-results-keyword
  1634. (regexp-quote name))))
  1635. (catch :found
  1636. (while (re-search-forward re nil t)
  1637. (let ((element (org-element-at-point)))
  1638. (when (or (eq (org-element-type element) 'keyword)
  1639. (< (point)
  1640. (org-element-property :post-affiliated element)))
  1641. (throw :found (line-beginning-position)))))))))
  1642. (defun org-babel-result-names (&optional file)
  1643. "Returns the names of results in FILE or the current buffer."
  1644. (save-excursion
  1645. (when file (find-file file)) (goto-char (point-min))
  1646. (let ((case-fold-search t) names)
  1647. (while (re-search-forward org-babel-result-w-name-regexp nil t)
  1648. (setq names (cons (match-string-no-properties 9) names)))
  1649. names)))
  1650. ;;;###autoload
  1651. (defun org-babel-next-src-block (&optional arg)
  1652. "Jump to the next source block.
  1653. With optional prefix argument ARG, jump forward ARG many source blocks."
  1654. (interactive "p")
  1655. (org-next-block arg nil org-babel-src-block-regexp))
  1656. ;;;###autoload
  1657. (defun org-babel-previous-src-block (&optional arg)
  1658. "Jump to the previous source block.
  1659. With optional prefix argument ARG, jump backward ARG many source blocks."
  1660. (interactive "p")
  1661. (org-previous-block arg org-babel-src-block-regexp))
  1662. (defvar org-babel-load-languages)
  1663. ;;;###autoload
  1664. (defun org-babel-mark-block ()
  1665. "Mark current src block."
  1666. (interactive)
  1667. (let ((head (org-babel-where-is-src-block-head)))
  1668. (when head
  1669. (save-excursion
  1670. (goto-char head)
  1671. (looking-at org-babel-src-block-regexp))
  1672. (push-mark (match-end 5) nil t)
  1673. (goto-char (match-beginning 5)))))
  1674. (defun org-babel-demarcate-block (&optional arg)
  1675. "Wrap or split the code in the region or on the point.
  1676. When called from inside of a code block the current block is
  1677. split. When called from outside of a code block a new code block
  1678. is created. In both cases if the region is demarcated and if the
  1679. region is not active then the point is demarcated."
  1680. (interactive "P")
  1681. (let* ((info (org-babel-get-src-block-info 'light))
  1682. (start (org-babel-where-is-src-block-head))
  1683. (block (and start (match-string 0)))
  1684. (headers (and start (match-string 4)))
  1685. (stars (concat (make-string (or (org-current-level) 1) ?*) " "))
  1686. (lower-case-p (and block
  1687. (let (case-fold-search)
  1688. (org-string-match-p "#\\+begin_src" block)))))
  1689. (if info
  1690. (mapc
  1691. (lambda (place)
  1692. (save-excursion
  1693. (goto-char place)
  1694. (let ((lang (nth 0 info))
  1695. (indent (make-string (org-get-indentation) ?\s)))
  1696. (when (string-match "^[[:space:]]*$"
  1697. (buffer-substring (point-at-bol)
  1698. (point-at-eol)))
  1699. (delete-region (point-at-bol) (point-at-eol)))
  1700. (insert (concat
  1701. (if (looking-at "^") "" "\n")
  1702. indent (funcall (if lower-case-p 'downcase 'upcase) "#+end_src\n")
  1703. (if arg stars indent) "\n"
  1704. indent (funcall (if lower-case-p 'downcase 'upcase) "#+begin_src ")
  1705. lang
  1706. (if (> (length headers) 1)
  1707. (concat " " headers) headers)
  1708. (if (looking-at "[\n\r]")
  1709. ""
  1710. (concat "\n" (make-string (current-column) ? )))))))
  1711. (move-end-of-line 2))
  1712. (sort (if (org-region-active-p) (list (mark) (point)) (list (point))) #'>))
  1713. (let ((start (point))
  1714. (lang (completing-read
  1715. "Lang: "
  1716. (mapcar #'symbol-name
  1717. (delete-dups
  1718. (append (mapcar #'car org-babel-load-languages)
  1719. (mapcar (lambda (el) (intern (car el)))
  1720. org-src-lang-modes))))))
  1721. (body (delete-and-extract-region
  1722. (if (org-region-active-p) (mark) (point)) (point))))
  1723. (insert (concat (if (looking-at "^") "" "\n")
  1724. (if arg (concat stars "\n") "")
  1725. (funcall (if lower-case-p 'downcase 'upcase) "#+begin_src ")
  1726. lang "\n"
  1727. body
  1728. (if (or (= (length body) 0)
  1729. (string-match "[\r\n]$" body)) "" "\n")
  1730. (funcall (if lower-case-p 'downcase 'upcase) "#+end_src\n")))
  1731. (goto-char start) (move-end-of-line 1)))))
  1732. (defun org-babel--insert-results-keyword (name hash)
  1733. "Insert RESULTS keyword with NAME value at point.
  1734. If NAME is nil, results are anonymous. HASH is a string used as
  1735. the results hash, or nil. Leave point before the keyword."
  1736. (save-excursion (insert "\n")) ;open line to indent.
  1737. (org-indent-line)
  1738. (delete-char 1)
  1739. (insert (concat "#+" org-babel-results-keyword
  1740. (cond ((not hash) nil)
  1741. (org-babel-hash-show-time
  1742. (format "[%s %s]"
  1743. (format-time-string "<%Y-%m-%d %H:%M:%S>")
  1744. hash))
  1745. (t (format "[%s]" hash)))
  1746. ":"
  1747. (when name (concat " " name))
  1748. "\n\n"))
  1749. (beginning-of-line -1)
  1750. (when hash (org-babel-hide-hash)))
  1751. (defun org-babel--clear-results-maybe (hash)
  1752. "Clear results when hash doesn't match HASH.
  1753. When results hash does not match HASH, remove RESULTS keyword at
  1754. point, along with related contents. Do nothing if HASH is nil.
  1755. Return a non-nil value if results were cleared. In this case,
  1756. leave point where new results should be inserted."
  1757. (when hash
  1758. (looking-at org-babel-result-regexp)
  1759. (unless (string= (match-string 1) hash)
  1760. (let* ((e (org-element-at-point))
  1761. (post (copy-marker (org-element-property :post-affiliated e))))
  1762. ;; Delete contents.
  1763. (delete-region post
  1764. (save-excursion
  1765. (goto-char (org-element-property :end e))
  1766. (skip-chars-backward " \t\n")
  1767. (line-beginning-position 2)))
  1768. ;; Delete RESULT keyword. However, if RESULTS keyword is
  1769. ;; orphaned, ignore this part. The deletion above already
  1770. ;; took care of it.
  1771. (unless (= (point) post)
  1772. (delete-region (line-beginning-position)
  1773. (line-beginning-position 2)))
  1774. (goto-char post)
  1775. (set-marker post nil)))
  1776. t))
  1777. (defun org-babel-where-is-src-block-result (&optional insert _info hash)
  1778. "Find where the current source block results begin.
  1779. Return the point at the beginning of the result of the current
  1780. source block, specifically at the beginning of the results line.
  1781. If no result exists for this block return nil, unless optional
  1782. argument INSERT is non-nil. In this case, create a results line
  1783. following the source block and return the position at its
  1784. beginning. In the case of inline code, remove the results part
  1785. instead.
  1786. If optional argument HASH is a string, remove contents related to
  1787. RESULTS keyword if its hash is different. Then update the latter
  1788. to HASH."
  1789. (let ((context (org-element-context)))
  1790. (catch :found
  1791. (org-with-wide-buffer
  1792. (pcase (org-element-type context)
  1793. ((or `inline-babel-call `inline-src-block)
  1794. ;; Results for inline objects are located right after them.
  1795. ;; There is no RESULTS line to insert either.
  1796. (let ((limit (org-element-property
  1797. :contents-end (org-element-property :parent context))))
  1798. (goto-char (org-element-property :end context))
  1799. (skip-chars-forward " \t\n" limit)
  1800. (throw :found
  1801. (and
  1802. (< (point) limit)
  1803. (let ((result (org-element-context)))
  1804. (and (eq (org-element-type result) 'macro)
  1805. (string= (org-element-property :key result)
  1806. "results")
  1807. (if (not insert) (point)
  1808. (delete-region
  1809. (point)
  1810. (progn
  1811. (goto-char (org-element-property :end result))
  1812. (skip-chars-backward " \t")
  1813. (point)))
  1814. (point))))))))
  1815. ((or `babel-call `src-block)
  1816. (let* ((name (org-element-property :name context))
  1817. (named-results (and name (org-babel-find-named-result name))))
  1818. (goto-char (or named-results (org-element-property :end context)))
  1819. (cond
  1820. ;; Existing results named after the current source.
  1821. (named-results
  1822. (when (org-babel--clear-results-maybe hash)
  1823. (org-babel--insert-results-keyword name hash))
  1824. (throw :found (point)))
  1825. ;; Named results expect but none to be found.
  1826. (name)
  1827. ;; No possible anonymous results at the very end of
  1828. ;; buffer.
  1829. ((eobp))
  1830. ;; Check if next element is an anonymous result below
  1831. ;; the current block.
  1832. ((let* ((next (org-element-at-point))
  1833. (end (save-excursion
  1834. (goto-char
  1835. (org-element-property :post-affiliated next))
  1836. (line-end-position)))
  1837. (empty-result-re (concat org-babel-result-regexp "$"))
  1838. (case-fold-search t))
  1839. (re-search-forward empty-result-re end t))
  1840. (beginning-of-line)
  1841. (when (org-babel--clear-results-maybe hash)
  1842. (org-babel--insert-results-keyword nil hash))
  1843. (throw :found (point))))))
  1844. ;; Ignore other elements.
  1845. (_ (throw :found nil))))
  1846. ;; No result found. Insert a RESULTS keyword below element, if
  1847. ;; appropriate. In this case, ensure there is an empty line
  1848. ;; after the previous element.
  1849. (when insert
  1850. (save-excursion
  1851. (goto-char (min (org-element-property :end context) (point-max)))
  1852. (skip-chars-backward " \t\n")
  1853. (forward-line)
  1854. (cond ((not (bolp)) (insert "\n\n"))
  1855. ((or (eobp)
  1856. (= (org-element-property :post-blank context) 0))
  1857. (insert "\n"))
  1858. (t (forward-line)))
  1859. (org-babel--insert-results-keyword
  1860. (org-element-property :name context) hash)
  1861. (point))))))
  1862. (defun org-babel-read-element (element)
  1863. "Read ELEMENT into emacs-lisp.
  1864. Return nil if ELEMENT cannot be read."
  1865. (org-with-wide-buffer
  1866. (goto-char (org-element-property :post-affiliated element))
  1867. (pcase (org-element-type element)
  1868. (`fixed-width
  1869. (let ((v (org-babel-trim (org-element-property :value element))))
  1870. (or (org-babel-number-p v) v)))
  1871. (`table (org-babel-read-table))
  1872. (`plain-list (org-babel-read-list))
  1873. (`example-block
  1874. (let ((v (org-element-property :value element)))
  1875. (if (or org-src-preserve-indentation
  1876. (org-element-property :preserve-indent element))
  1877. v
  1878. (org-remove-indentation v))))
  1879. (`export-block
  1880. (org-remove-indentation (org-element-property :value element)))
  1881. (`paragraph
  1882. ;; Treat paragraphs containing a single link specially.
  1883. (skip-chars-forward " \t")
  1884. (if (and (looking-at org-bracket-link-regexp)
  1885. (save-excursion
  1886. (goto-char (match-end 0))
  1887. (skip-chars-forward " \r\t\n")
  1888. (<= (org-element-property :end element)
  1889. (point))))
  1890. (org-babel-read-link)
  1891. (buffer-substring-no-properties
  1892. (org-element-property :contents-begin element)
  1893. (org-element-property :contents-end element))))
  1894. ((or `center-block `quote-block `verse-block `special-block)
  1895. (org-remove-indentation
  1896. (buffer-substring-no-properties
  1897. (org-element-property :contents-begin element)
  1898. (org-element-property :contents-end element))))
  1899. (_ nil))))
  1900. (defun org-babel-read-result ()
  1901. "Read the result at point into emacs-lisp."
  1902. (and (not (save-excursion
  1903. (beginning-of-line)
  1904. (looking-at-p "[ \t]*$")))
  1905. (org-babel-read-element (org-element-at-point))))
  1906. (defun org-babel-read-table ()
  1907. "Read the table at point into emacs-lisp."
  1908. (mapcar (lambda (row)
  1909. (if (and (symbolp row) (equal row 'hline)) row
  1910. (mapcar (lambda (el) (org-babel-read el 'inhibit-lisp-eval)) row)))
  1911. (org-table-to-lisp)))
  1912. (defun org-babel-read-list ()
  1913. "Read the list at point into emacs-lisp."
  1914. (mapcar (lambda (el) (org-babel-read el 'inhibit-lisp-eval))
  1915. (cdr (org-list-to-lisp))))
  1916. (defvar org-link-types-re)
  1917. (defun org-babel-read-link ()
  1918. "Read the link at point into emacs-lisp.
  1919. If the path of the link is a file path it is expanded using
  1920. `expand-file-name'."
  1921. (let* ((case-fold-search t)
  1922. (raw (and (looking-at org-bracket-link-regexp)
  1923. (org-no-properties (match-string 1))))
  1924. (type (and (string-match org-link-types-re raw)
  1925. (match-string 1 raw))))
  1926. (cond
  1927. ((not type) (expand-file-name raw))
  1928. ((string= type "file")
  1929. (and (string-match "file\\(.*\\):\\(.+\\)" raw)
  1930. (expand-file-name (match-string 2 raw))))
  1931. (t raw))))
  1932. (defun org-babel-format-result (result &optional sep)
  1933. "Format RESULT for writing to file."
  1934. (let ((echo-res (lambda (r) (if (stringp r) r (format "%S" r)))))
  1935. (if (listp result)
  1936. ;; table result
  1937. (orgtbl-to-generic
  1938. result (list :sep (or sep "\t") :fmt echo-res))
  1939. ;; scalar result
  1940. (funcall echo-res result))))
  1941. (defun org-babel-insert-result (result &optional result-params info hash lang)
  1942. "Insert RESULT into the current buffer.
  1943. By default RESULT is inserted after the end of the current source
  1944. block. The RESULT of an inline source block usually will be
  1945. wrapped inside a `results' macro and placed on the same line as
  1946. the inline source block. The macro is stripped upon export.
  1947. Multiline and non-scalar RESULTS from inline source blocks are
  1948. not allowed. With optional argument RESULT-PARAMS controls
  1949. insertion of results in the Org mode file. RESULT-PARAMS can
  1950. take the following values:
  1951. replace - (default option) insert results after the source block
  1952. or inline source block replacing any previously
  1953. inserted results.
  1954. silent -- no results are inserted into the Org-mode buffer but
  1955. the results are echoed to the minibuffer and are
  1956. ingested by Emacs (a potentially time consuming
  1957. process).
  1958. file ---- the results are interpreted as a file path, and are
  1959. inserted into the buffer using the Org-mode file syntax.
  1960. list ---- the results are interpreted as an Org-mode list.
  1961. raw ----- results are added directly to the Org-mode file. This
  1962. is a good option if you code block will output org-mode
  1963. formatted text.
  1964. drawer -- results are added directly to the Org-mode file as with
  1965. \"raw\", but are wrapped in a RESULTS drawer or results
  1966. macro, allowing them to later be replaced or removed
  1967. automatically.
  1968. org ----- results are added inside of a \"src_org{}\" or \"#+BEGIN_SRC
  1969. org\" block depending on whether the current source block is
  1970. inline or not. They are not comma-escaped when inserted,
  1971. but Org syntax here will be discarded when exporting the
  1972. file.
  1973. html ---- results are added inside of a #+BEGIN_EXPORT HTML block
  1974. or html export snippet depending on whether the current
  1975. source block is inline or not. This is a good option
  1976. if your code block will output html formatted text.
  1977. latex --- results are added inside of a #+BEGIN_EXPORT LATEX
  1978. block or latex export snippet depending on whether the
  1979. current source block is inline or not. This is a good
  1980. option if your code block will output latex formatted
  1981. text.
  1982. code ---- the results are extracted in the syntax of the source
  1983. code of the language being evaluated and are added
  1984. inside of a source block with the source-code language
  1985. set appropriately. Also, source block inlining is
  1986. preserved in this case. Note this relies on the
  1987. optional LANG argument.
  1988. list ---- the results are rendered as a list. This option not
  1989. allowed for inline src blocks.
  1990. table --- the results are rendered as a table. This option not
  1991. allowed for inline src blocks.
  1992. INFO may provide the values of these header arguments (in the
  1993. `header-arguments-alist' see the docstring for
  1994. `org-babel-get-src-block-info'):
  1995. :file --- the name of the file to which output should be written.
  1996. :wrap --- the effect is similar to `latex' in RESULT-PARAMS but
  1997. using the argument supplied to specify the export block
  1998. or snippet type."
  1999. (cond ((stringp result)
  2000. (setq result (org-no-properties result))
  2001. (when (member "file" result-params)
  2002. (setq result (org-babel-result-to-file
  2003. result (when (assoc :file-desc (nth 2 info))
  2004. (or (cdr (assoc :file-desc (nth 2 info)))
  2005. result))))))
  2006. ((listp result))
  2007. (t (setq result (format "%S" result))))
  2008. (if (and result-params (member "silent" result-params))
  2009. (progn (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
  2010. result)
  2011. (let ((inline (let ((context (org-element-context)))
  2012. (and (memq (org-element-type context)
  2013. '(inline-babel-call inline-src-block))
  2014. context))))
  2015. (when inline
  2016. (let ((warning
  2017. (or (and (member "table" result-params) "`:results table'")
  2018. (and (listp result) "list result")
  2019. (and (string-match-p "\n." result) "multiline result")
  2020. (and (member "list" result-params) "`:results list'"))))
  2021. (when warning
  2022. (user-error "Inline error: %s cannot be used" warning))))
  2023. (save-excursion
  2024. (let* ((visible-beg (point-min-marker))
  2025. (visible-end (copy-marker (point-max) t))
  2026. (inline (let ((context (org-element-context)))
  2027. (and (memq (org-element-type context)
  2028. '(inline-babel-call inline-src-block))
  2029. context)))
  2030. (existing-result (org-babel-where-is-src-block-result t nil hash))
  2031. (results-switches (cdr (assq :results_switches (nth 2 info))))
  2032. ;; When results exist outside of the current visible
  2033. ;; region of the buffer, be sure to widen buffer to
  2034. ;; update them.
  2035. (outside-scope (and existing-result
  2036. (buffer-narrowed-p)
  2037. (or (> visible-beg existing-result)
  2038. (<= visible-end existing-result))))
  2039. beg end indent)
  2040. ;; Ensure non-inline results end in a newline.
  2041. (when (and (org-string-nw-p result)
  2042. (not inline)
  2043. (not (string-equal (substring result -1) "\n")))
  2044. (setq result (concat result "\n")))
  2045. (unwind-protect
  2046. (progn
  2047. (when outside-scope (widen))
  2048. (if existing-result (goto-char existing-result)
  2049. (goto-char (org-element-property :end inline))
  2050. (skip-chars-backward " \t"))
  2051. (unless inline
  2052. (setq indent (org-get-indentation))
  2053. (forward-line 1))
  2054. (setq beg (point))
  2055. (cond
  2056. (inline
  2057. ;; Make sure new results are separated from the
  2058. ;; source code by one space.
  2059. (unless existing-result
  2060. (insert " ")
  2061. (setq beg (point))))
  2062. ((member "replace" result-params)
  2063. (delete-region (point) (org-babel-result-end)))
  2064. ((member "append" result-params)
  2065. (goto-char (org-babel-result-end)) (setq beg (point-marker)))
  2066. ((member "prepend" result-params))) ; already there
  2067. (setq results-switches
  2068. (if results-switches (concat " " results-switches) ""))
  2069. (let ((wrap (lambda (start finish &optional no-escape no-newlines
  2070. inline-start inline-finish)
  2071. (when inline
  2072. (setq start inline-start)
  2073. (setq finish inline-finish)
  2074. (setq no-newlines t))
  2075. (goto-char end)
  2076. (insert (concat finish (unless no-newlines "\n")))
  2077. (goto-char beg)
  2078. (insert (concat start (unless no-newlines "\n")))
  2079. (unless no-escape
  2080. (org-escape-code-in-region (min (point) end) end))
  2081. (goto-char end)
  2082. (unless no-newlines (goto-char (point-at-eol)))
  2083. (setq end (point-marker))))
  2084. (tabulablep
  2085. (lambda (r)
  2086. ;; Non-nil when result R can be turned into
  2087. ;; a table.
  2088. (and (listp r)
  2089. (null (cdr (last r)))
  2090. (cl-every
  2091. (lambda (e) (or (atom e) (null (cdr (last e)))))
  2092. result)))))
  2093. ;; insert results based on type
  2094. (cond
  2095. ;; Do nothing for an empty result.
  2096. ((null result))
  2097. ;; Insert a list if preferred.
  2098. ((member "list" result-params)
  2099. (insert
  2100. (org-babel-trim
  2101. (org-list-to-generic
  2102. (cons 'unordered
  2103. (mapcar
  2104. (lambda (e)
  2105. (list (if (stringp e) e (format "%S" e))))
  2106. (if (listp result) result
  2107. (split-string result "\n" t))))
  2108. '(:splicep nil :istart "- " :iend "\n")))
  2109. "\n"))
  2110. ;; Try hard to print RESULT as a table. Give up if
  2111. ;; it contains an improper list.
  2112. ((funcall tabulablep result)
  2113. (goto-char beg)
  2114. (insert (concat (orgtbl-to-orgtbl
  2115. (if (cl-every
  2116. (lambda (e)
  2117. (or (eq e 'hline) (listp e)))
  2118. result)
  2119. result
  2120. (list result))
  2121. nil)
  2122. "\n"))
  2123. (goto-char beg)
  2124. (when (org-at-table-p) (org-table-align))
  2125. (goto-char (org-table-end)))
  2126. ;; Print verbatim a list that cannot be turned into
  2127. ;; a table.
  2128. ((listp result) (insert (format "%s\n" result)))
  2129. ((member "file" result-params)
  2130. (when inline
  2131. (setq result (org-macro-escape-arguments result)))
  2132. (insert result))
  2133. ((and inline (not (member "raw" result-params)))
  2134. (insert (org-macro-escape-arguments
  2135. (org-babel-chomp result "\n"))))
  2136. (t (goto-char beg) (insert result)))
  2137. (setq end (point-marker))
  2138. ;; possibly wrap result
  2139. (cond
  2140. ((assoc :wrap (nth 2 info))
  2141. (let ((name (or (cdr (assoc :wrap (nth 2 info))) "RESULTS")))
  2142. (funcall wrap (concat "#+BEGIN_" name)
  2143. (concat "#+END_" (car (org-split-string name)))
  2144. nil nil (concat "{{{results(@@" name ":") "@@)}}}")))
  2145. ((member "html" result-params)
  2146. (funcall wrap "#+BEGIN_EXPORT html" "#+END_EXPORT" nil nil
  2147. "{{{results(@@html:" "@@)}}}"))
  2148. ((member "latex" result-params)
  2149. (funcall wrap "#+BEGIN_EXPORT latex" "#+END_EXPORT" nil nil
  2150. "{{{results(@@latex:" "@@)}}}"))
  2151. ((member "org" result-params)
  2152. (goto-char beg) (if (org-at-table-p) (org-cycle))
  2153. (funcall wrap "#+BEGIN_SRC org" "#+END_SRC" nil nil
  2154. "{{{results(src_org{" "})}}}"))
  2155. ((member "code" result-params)
  2156. (let ((lang (or lang "none")))
  2157. (funcall wrap (format "#+BEGIN_SRC %s%s" lang results-switches)
  2158. "#+END_SRC" nil nil
  2159. (format "{{{results(src_%s[%s]{" lang results-switches)
  2160. "})}}}")))
  2161. ((member "raw" result-params)
  2162. (goto-char beg) (if (org-at-table-p) (org-cycle)))
  2163. ((or (member "drawer" result-params)
  2164. ;; Stay backward compatible with <7.9.2
  2165. (member "wrap" result-params))
  2166. (goto-char beg) (if (org-at-table-p) (org-cycle))
  2167. (funcall wrap ":RESULTS:" ":END:" 'no-escape nil
  2168. "{{{results(" ")}}}"))
  2169. ((and inline (member "file" result-params))
  2170. (funcall wrap nil nil nil nil "{{{results(" ")}}}"))
  2171. ((and (not (funcall tabulablep result))
  2172. (not (member "file" result-params)))
  2173. (let ((org-babel-inline-result-wrap
  2174. ;; Hard code {{{results(...)}}} on top of customization.
  2175. (format "{{{results(%s)}}}"
  2176. org-babel-inline-result-wrap)))
  2177. (org-babel-examplify-region beg end results-switches inline)
  2178. (setq end (point))))))
  2179. ;; Possibly indent results in par with #+results line.
  2180. (when (and (not inline) (numberp indent) (> indent 0)
  2181. ;; In this case `table-align' does the work
  2182. ;; for us.
  2183. (not (and (listp result)
  2184. (member "append" result-params))))
  2185. (indent-rigidly beg end indent))
  2186. (if (null result)
  2187. (if (member "value" result-params)
  2188. (message "Code block returned no value.")
  2189. (message "Code block produced no output."))
  2190. (message "Code block evaluation complete.")))
  2191. (when outside-scope (narrow-to-region visible-beg visible-end))
  2192. (set-marker visible-beg nil)
  2193. (set-marker visible-end nil)))))))
  2194. (defun org-babel-remove-result (&optional info keep-keyword)
  2195. "Remove the result of the current source block."
  2196. (interactive)
  2197. (let ((location (org-babel-where-is-src-block-result nil info)))
  2198. (when location
  2199. (save-excursion
  2200. (goto-char location)
  2201. (when (looking-at (concat org-babel-result-regexp ".*$"))
  2202. (delete-region
  2203. (if keep-keyword (1+ (match-end 0)) (1- (match-beginning 0)))
  2204. (progn (forward-line 1) (org-babel-result-end))))))))
  2205. (defun org-babel-remove-inline-result (&optional datum)
  2206. "Remove the result of the current inline-src-block or babel call.
  2207. The result must be wrapped in a `results' macro to be removed.
  2208. Leading white space is trimmed."
  2209. (interactive)
  2210. (let* ((el (or datum (org-element-context))))
  2211. (when (memq (org-element-type el) '(inline-src-block inline-babel-call))
  2212. (org-with-wide-buffer
  2213. (goto-char (org-element-property :end el))
  2214. (skip-chars-backward " \t")
  2215. (let ((result (save-excursion
  2216. (skip-chars-forward
  2217. " \t\n"
  2218. (org-element-property
  2219. :contents-end (org-element-property :parent el)))
  2220. (org-element-context))))
  2221. (when (and (eq (org-element-type result) 'macro)
  2222. (string= (org-element-property :key result) "results"))
  2223. (delete-region ; And leading whitespace.
  2224. (point)
  2225. (progn (goto-char (org-element-property :end result))
  2226. (skip-chars-backward " \t\n")
  2227. (point)))))))))
  2228. (defun org-babel-remove-result-one-or-many (x)
  2229. "Remove the result of the current source block.
  2230. If called with a prefix argument, remove all result blocks
  2231. in the buffer."
  2232. (interactive "P")
  2233. (if x
  2234. (org-babel-map-src-blocks nil (org-babel-remove-result))
  2235. (org-babel-remove-result)))
  2236. (defun org-babel-result-end ()
  2237. "Return the point at the end of the current set of results."
  2238. (save-excursion
  2239. (cond
  2240. ((org-at-table-p) (progn (goto-char (org-table-end)) (point)))
  2241. ((org-at-item-p) (let* ((struct (org-list-struct))
  2242. (prvs (org-list-prevs-alist struct)))
  2243. (org-list-get-list-end (point-at-bol) struct prvs)))
  2244. ((let ((case-fold-search t)) (looking-at "^\\([ \t]*\\):results:"))
  2245. (progn (re-search-forward (concat "^" (match-string 1) ":END:"))
  2246. (forward-char 1) (point)))
  2247. (t
  2248. (let ((case-fold-search t))
  2249. (if (looking-at (concat "[ \t]*#\\+begin_\\([^ \t\n\r]+\\)"))
  2250. (progn (re-search-forward (concat "[ \t]*#\\+end_" (match-string 1))
  2251. nil t)
  2252. (forward-char 1))
  2253. (while (looking-at "[ \t]*\\(: \\|:$\\|\\[\\[\\)")
  2254. (forward-line 1))))
  2255. (point)))))
  2256. (defun org-babel-result-to-file (result &optional description)
  2257. "Convert RESULT into an `org-mode' link with optional DESCRIPTION.
  2258. If the `default-directory' is different from the containing
  2259. file's directory then expand relative links."
  2260. (when (stringp result)
  2261. (format "[[file:%s]%s]"
  2262. (if (and default-directory
  2263. buffer-file-name
  2264. (not (string= (expand-file-name default-directory)
  2265. (expand-file-name
  2266. (file-name-directory buffer-file-name)))))
  2267. (expand-file-name result default-directory)
  2268. result)
  2269. (if description (concat "[" description "]") ""))))
  2270. (defvar org-babel-capitalize-example-region-markers nil
  2271. "Make true to capitalize begin/end example markers inserted by code blocks.")
  2272. (define-obsolete-function-alias
  2273. 'org-babel-examplize-region
  2274. 'org-babel-examplify-region "25.1")
  2275. (defun org-babel-examplify-region (beg end &optional results-switches inline)
  2276. "Comment out region using the inline `==' or `: ' org example quote."
  2277. (interactive "*r")
  2278. (let ((maybe-cap
  2279. (lambda (str)
  2280. (if org-babel-capitalize-example-region-markers (upcase str) str))))
  2281. (if inline
  2282. (save-excursion
  2283. (goto-char beg)
  2284. (insert (format org-babel-inline-result-wrap
  2285. (delete-and-extract-region beg end))))
  2286. (let ((size (count-lines beg end)))
  2287. (save-excursion
  2288. (cond ((= size 0)) ; do nothing for an empty result
  2289. ((< size org-babel-min-lines-for-block-output)
  2290. (goto-char beg)
  2291. (dotimes (n size)
  2292. (beginning-of-line 1) (insert ": ") (forward-line 1)))
  2293. (t
  2294. (goto-char beg)
  2295. (insert (if results-switches
  2296. (format "%s%s\n"
  2297. (funcall maybe-cap "#+begin_example")
  2298. results-switches)
  2299. (funcall maybe-cap "#+begin_example\n")))
  2300. (if (markerp end) (goto-char end) (forward-char (- end beg)))
  2301. (insert (funcall maybe-cap "#+end_example\n")))))))))
  2302. (defun org-babel-update-block-body (new-body)
  2303. "Update the body of the current code block to NEW-BODY."
  2304. (let ((element (org-element-at-point)))
  2305. (unless (eq (org-element-type element) 'src-block)
  2306. (error "Not in a source block"))
  2307. (goto-char (org-babel-where-is-src-block-head element))
  2308. (let* ((ind (org-get-indentation))
  2309. (body-start (line-beginning-position 2))
  2310. (body (org-element-normalize-string
  2311. (if (or org-src-preserve-indentation
  2312. (org-element-property :preserve-indent element))
  2313. new-body
  2314. (with-temp-buffer
  2315. (insert (org-remove-indentation new-body))
  2316. (indent-rigidly
  2317. (point-min)
  2318. (point-max)
  2319. (+ ind org-edit-src-content-indentation))
  2320. (buffer-string))))))
  2321. (delete-region body-start
  2322. (org-with-wide-buffer
  2323. (goto-char (org-element-property :end element))
  2324. (skip-chars-backward " \t\n")
  2325. (line-beginning-position)))
  2326. (goto-char body-start)
  2327. (insert body))))
  2328. (defun org-babel-merge-params (&rest plists)
  2329. "Combine all parameter association lists in PLISTS.
  2330. Later elements of PLISTS override the values of previous elements.
  2331. This takes into account some special considerations for certain
  2332. parameters when merging lists."
  2333. (let* ((results-exclusive-groups
  2334. (mapcar (lambda (group) (mapcar #'symbol-name group))
  2335. (cdr (assoc 'results org-babel-common-header-args-w-values))))
  2336. (exports-exclusive-groups
  2337. (mapcar (lambda (group) (mapcar #'symbol-name group))
  2338. (cdr (assoc 'exports org-babel-common-header-args-w-values))))
  2339. (variable-index 0)
  2340. (e-merge (lambda (exclusive-groups &rest result-params)
  2341. ;; maintain exclusivity of mutually exclusive parameters
  2342. (let (output)
  2343. (mapc (lambda (new-params)
  2344. (mapc (lambda (new-param)
  2345. (mapc (lambda (exclusive-group)
  2346. (when (member new-param exclusive-group)
  2347. (mapcar (lambda (excluded-param)
  2348. (setq output
  2349. (delete
  2350. excluded-param
  2351. output)))
  2352. exclusive-group)))
  2353. exclusive-groups)
  2354. (setq output (org-uniquify
  2355. (cons new-param output))))
  2356. new-params))
  2357. result-params)
  2358. output)))
  2359. params results exports tangle noweb cache vars shebang comments padline
  2360. clearnames)
  2361. (mapc
  2362. (lambda (plist)
  2363. (mapc
  2364. (lambda (pair)
  2365. (case (car pair)
  2366. (:var
  2367. (let ((name (if (listp (cdr pair))
  2368. (cadr pair)
  2369. (and (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*="
  2370. (cdr pair))
  2371. (intern (match-string 1 (cdr pair)))))))
  2372. (if name
  2373. (setq vars
  2374. (append
  2375. (if (member name (mapcar #'car vars))
  2376. (progn
  2377. (push name clearnames)
  2378. (delq nil
  2379. (mapcar
  2380. (lambda (p)
  2381. (unless (equal (car p) name) p))
  2382. vars)))
  2383. vars)
  2384. (list (cons name pair))))
  2385. ;; if no name is given and we already have named variables
  2386. ;; then assign to named variables in order
  2387. (if (and vars (nth variable-index vars))
  2388. (let ((name (car (nth variable-index vars))))
  2389. (push name clearnames) ; clear out colnames
  2390. ; and rownames
  2391. ; for replace vars
  2392. (prog1 (setf (cddr (nth variable-index vars))
  2393. (concat (symbol-name name) "=" (cdr pair)))
  2394. (incf variable-index)))
  2395. (error "Variable \"%s\" must be assigned a default value"
  2396. (cdr pair))))))
  2397. (:results
  2398. (setq results (funcall e-merge results-exclusive-groups
  2399. results
  2400. (split-string
  2401. (let ((r (cdr pair)))
  2402. (if (stringp r) r (eval r)))))))
  2403. (:file
  2404. (when (cdr pair)
  2405. (setq results (funcall e-merge results-exclusive-groups
  2406. results '("file")))
  2407. (unless (or (member "both" exports)
  2408. (member "none" exports)
  2409. (member "code" exports))
  2410. (setq exports (funcall e-merge exports-exclusive-groups
  2411. exports '("results"))))
  2412. (setq params (cons pair (assq-delete-all (car pair) params)))))
  2413. (:file-ext
  2414. (when (cdr pair)
  2415. (setq results (funcall e-merge results-exclusive-groups
  2416. results '("file")))
  2417. (unless (or (member "both" exports)
  2418. (member "none" exports)
  2419. (member "code" exports))
  2420. (setq exports (funcall e-merge exports-exclusive-groups
  2421. exports '("results"))))
  2422. (setq params (cons pair (assq-delete-all (car pair) params)))))
  2423. (:exports
  2424. (setq exports (funcall e-merge exports-exclusive-groups
  2425. exports
  2426. (split-string (or (cdr pair) "")))))
  2427. (:tangle ;; take the latest -- always overwrite
  2428. (setq tangle (or (list (cdr pair)) tangle)))
  2429. (:noweb
  2430. (setq noweb (funcall e-merge
  2431. '(("yes" "no" "tangle" "no-export"
  2432. "strip-export" "eval"))
  2433. noweb
  2434. (split-string (or (cdr pair) "")))))
  2435. (:cache
  2436. (setq cache (funcall e-merge '(("yes" "no")) cache
  2437. (split-string (or (cdr pair) "")))))
  2438. (:padline
  2439. (setq padline (funcall e-merge '(("yes" "no")) padline
  2440. (split-string (or (cdr pair) "")))))
  2441. (:shebang ;; take the latest -- always overwrite
  2442. (setq shebang (or (list (cdr pair)) shebang)))
  2443. (:comments
  2444. (setq comments (funcall e-merge '(("yes" "no")) comments
  2445. (split-string (or (cdr pair) "")))))
  2446. (t ;; replace: this covers e.g. :session
  2447. (setq params (cons pair (assq-delete-all (car pair) params))))))
  2448. plist))
  2449. plists)
  2450. (setq vars (reverse vars))
  2451. (while vars (setq params (cons (cons :var (cddr (pop vars))) params)))
  2452. ;; clear out col-names and row-names for replaced variables
  2453. (mapc
  2454. (lambda (name)
  2455. (mapc
  2456. (lambda (param)
  2457. (when (assoc param params)
  2458. (setf (cdr (assoc param params))
  2459. (cl-remove-if (lambda (pair) (equal (car pair) name))
  2460. (cdr (assoc param params))))
  2461. (setf params (cl-remove-if (lambda (pair) (and (equal (car pair) param)
  2462. (null (cdr pair))))
  2463. params))))
  2464. (list :colname-names :rowname-names)))
  2465. clearnames)
  2466. (mapc
  2467. (lambda (hd)
  2468. (let ((key (intern (concat ":" (symbol-name hd))))
  2469. (val (eval hd)))
  2470. (setf params (cons (cons key (mapconcat 'identity val " ")) params))))
  2471. '(results exports tangle noweb padline cache shebang comments))
  2472. params))
  2473. (defvar org-babel-use-quick-and-dirty-noweb-expansion nil
  2474. "Set to true to use regular expressions to expand noweb references.
  2475. This results in much faster noweb reference expansion but does
  2476. not properly allow code blocks to inherit the \":noweb-ref\"
  2477. header argument from buffer or subtree wide properties.")
  2478. (defun org-babel-noweb-p (params context)
  2479. "Check if PARAMS require expansion in CONTEXT.
  2480. CONTEXT may be one of :tangle, :export or :eval."
  2481. (let* (intersect
  2482. (intersect (lambda (as bs)
  2483. (when as
  2484. (if (member (car as) bs)
  2485. (car as)
  2486. (funcall intersect (cdr as) bs))))))
  2487. (funcall intersect (case context
  2488. (:tangle '("yes" "tangle" "no-export" "strip-export"))
  2489. (:eval '("yes" "no-export" "strip-export" "eval"))
  2490. (:export '("yes")))
  2491. (split-string (or (cdr (assoc :noweb params)) "")))))
  2492. (defun org-babel-expand-noweb-references (&optional info parent-buffer)
  2493. "Expand Noweb references in the body of the current source code block.
  2494. For example the following reference would be replaced with the
  2495. body of the source-code block named `example-block'.
  2496. <<example-block>>
  2497. Note that any text preceding the <<foo>> construct on a line will
  2498. be interposed between the lines of the replacement text. So for
  2499. example if <<foo>> is placed behind a comment, then the entire
  2500. replacement text will also be commented.
  2501. This function must be called from inside of the buffer containing
  2502. the source-code block which holds BODY.
  2503. In addition the following syntax can be used to insert the
  2504. results of evaluating the source-code block named `example-block'.
  2505. <<example-block()>>
  2506. Any optional arguments can be passed to example-block by placing
  2507. the arguments inside the parenthesis following the convention
  2508. defined by `org-babel-lob'. For example
  2509. <<example-block(a=9)>>
  2510. would set the value of argument \"a\" equal to \"9\". Note that
  2511. these arguments are not evaluated in the current source-code
  2512. block but are passed literally to the \"example-block\"."
  2513. (let* ((parent-buffer (or parent-buffer (current-buffer)))
  2514. (info (or info (org-babel-get-src-block-info 'light)))
  2515. (lang (nth 0 info))
  2516. (body (nth 1 info))
  2517. (ob-nww-start org-babel-noweb-wrap-start)
  2518. (ob-nww-end org-babel-noweb-wrap-end)
  2519. (comment (string= "noweb" (cdr (assoc :comments (nth 2 info)))))
  2520. (rx-prefix (concat "\\(" org-babel-src-name-regexp "\\|"
  2521. ":noweb-ref[ \t]+" "\\)"))
  2522. (new-body "")
  2523. (nb-add (lambda (text) (setq new-body (concat new-body text))))
  2524. (c-wrap (lambda (text)
  2525. (with-temp-buffer
  2526. (funcall (intern (concat lang "-mode")))
  2527. (comment-region (point) (progn (insert text) (point)))
  2528. (org-babel-trim (buffer-string)))))
  2529. index source-name evaluate prefix)
  2530. (with-temp-buffer
  2531. (setq-local org-babel-noweb-wrap-start ob-nww-start)
  2532. (setq-local org-babel-noweb-wrap-end ob-nww-end)
  2533. (insert body) (goto-char (point-min))
  2534. (setq index (point))
  2535. (while (and (re-search-forward (org-babel-noweb-wrap) nil t))
  2536. (save-match-data (setf source-name (match-string 1)))
  2537. (save-match-data (setq evaluate (string-match "(.*)" source-name)))
  2538. (save-match-data
  2539. (setq prefix
  2540. (buffer-substring (match-beginning 0)
  2541. (save-excursion
  2542. (beginning-of-line 1) (point)))))
  2543. ;; add interval to new-body (removing noweb reference)
  2544. (goto-char (match-beginning 0))
  2545. (funcall nb-add (buffer-substring index (point)))
  2546. (goto-char (match-end 0))
  2547. (setq index (point))
  2548. (funcall
  2549. nb-add
  2550. (with-current-buffer parent-buffer
  2551. (save-restriction
  2552. (widen)
  2553. (mapconcat ;; Interpose PREFIX between every line.
  2554. #'identity
  2555. (split-string
  2556. (if evaluate
  2557. (let ((raw (org-babel-ref-resolve source-name)))
  2558. (if (stringp raw) raw (format "%S" raw)))
  2559. (or
  2560. ;; Retrieve from the library of babel.
  2561. (nth 2 (assoc (intern source-name)
  2562. org-babel-library-of-babel))
  2563. ;; Return the contents of headlines literally.
  2564. (save-excursion
  2565. (when (org-babel-ref-goto-headline-id source-name)
  2566. (org-babel-ref-headline-body)))
  2567. ;; Find the expansion of reference in this buffer.
  2568. (let ((rx (concat rx-prefix source-name "[ \t\n]"))
  2569. expansion)
  2570. (save-excursion
  2571. (goto-char (point-min))
  2572. (if org-babel-use-quick-and-dirty-noweb-expansion
  2573. (while (re-search-forward rx nil t)
  2574. (let* ((i (org-babel-get-src-block-info 'light))
  2575. (body (org-babel-expand-noweb-references i))
  2576. (sep (or (cdr (assoc :noweb-sep (nth 2 i)))
  2577. "\n"))
  2578. (full (if comment
  2579. (let ((cs (org-babel-tangle-comment-links i)))
  2580. (concat (funcall c-wrap (car cs)) "\n"
  2581. body "\n"
  2582. (funcall c-wrap (cadr cs))))
  2583. body)))
  2584. (setq expansion (cons sep (cons full expansion)))))
  2585. (org-babel-map-src-blocks nil
  2586. (let ((i (org-babel-get-src-block-info 'light)))
  2587. (when (equal (or (cdr (assoc :noweb-ref (nth 2 i)))
  2588. (nth 4 i))
  2589. source-name)
  2590. (let* ((body (org-babel-expand-noweb-references i))
  2591. (sep (or (cdr (assoc :noweb-sep (nth 2 i)))
  2592. "\n"))
  2593. (full (if comment
  2594. (let ((cs (org-babel-tangle-comment-links i)))
  2595. (concat (funcall c-wrap (car cs)) "\n"
  2596. body "\n"
  2597. (funcall c-wrap (cadr cs))))
  2598. body)))
  2599. (setq expansion
  2600. (cons sep (cons full expansion)))))))))
  2601. (and expansion
  2602. (mapconcat #'identity (nreverse (cdr expansion)) "")))
  2603. ;; Possibly raise an error if named block doesn't exist.
  2604. (if (or org-babel-noweb-error-all-langs
  2605. (member lang org-babel-noweb-error-langs))
  2606. (error "%s" (concat
  2607. (org-babel-noweb-wrap source-name)
  2608. "could not be resolved (see "
  2609. "`org-babel-noweb-error-langs')"))
  2610. "")))
  2611. "[\n\r]") (concat "\n" prefix))))))
  2612. (funcall nb-add (buffer-substring index (point-max))))
  2613. new-body))
  2614. (defun org-babel--script-escape-inner (str)
  2615. (let (in-single in-double backslash out)
  2616. (mapc
  2617. (lambda (ch)
  2618. (setq
  2619. out
  2620. (if backslash
  2621. (progn
  2622. (setq backslash nil)
  2623. (cond
  2624. ((and in-single (eq ch ?'))
  2625. ;; Escaped single quote inside single quoted string:
  2626. ;; emit just a single quote, since we've changed the
  2627. ;; outer quotes to double.
  2628. (cons ch out))
  2629. ((eq ch ?\")
  2630. ;; Escaped double quote
  2631. (if in-single
  2632. ;; This should be interpreted as backslash+quote,
  2633. ;; not an escape. Emit a three backslashes
  2634. ;; followed by a quote (because one layer of
  2635. ;; quoting will be stripped by `org-babel-read').
  2636. (append (list ch ?\\ ?\\ ?\\) out)
  2637. ;; Otherwise we are in a double-quoted string. Emit
  2638. ;; a single escaped quote
  2639. (append (list ch ?\\) out)))
  2640. ((eq ch ?\\)
  2641. ;; Escaped backslash: emit a single escaped backslash
  2642. (append (list ?\\ ?\\) out))
  2643. ;; Other: emit a quoted backslash followed by whatever
  2644. ;; the character was (because one layer of quoting will
  2645. ;; be stripped by `org-babel-read').
  2646. (t (append (list ch ?\\ ?\\) out))))
  2647. (case ch
  2648. (?\[ (if (or in-double in-single)
  2649. (cons ?\[ out)
  2650. (cons ?\( out)))
  2651. (?\] (if (or in-double in-single)
  2652. (cons ?\] out)
  2653. (cons ?\) out)))
  2654. (?\{ (if (or in-double in-single)
  2655. (cons ?\{ out)
  2656. (cons ?\( out)))
  2657. (?\} (if (or in-double in-single)
  2658. (cons ?\} out)
  2659. (cons ?\) out)))
  2660. (?, (if (or in-double in-single)
  2661. (cons ?, out) (cons ?\s out)))
  2662. (?\' (if in-double
  2663. (cons ?\' out)
  2664. (setq in-single (not in-single)) (cons ?\" out)))
  2665. (?\" (if in-single
  2666. (append (list ?\" ?\\) out)
  2667. (setq in-double (not in-double)) (cons ?\" out)))
  2668. (?\\ (unless (or in-single in-double)
  2669. (error "Can't handle backslash outside string in `org-babel-script-escape'"))
  2670. (setq backslash t)
  2671. out)
  2672. (t (cons ch out))))))
  2673. (string-to-list str))
  2674. (when (or in-single in-double)
  2675. (error "Unterminated string in `org-babel-script-escape'"))
  2676. (apply #'string (reverse out))))
  2677. (defun org-babel-script-escape (str &optional force)
  2678. "Safely convert tables into elisp lists."
  2679. (unless (stringp str)
  2680. (error "`org-babel-script-escape' expects a string"))
  2681. (let ((escaped
  2682. (cond
  2683. ((and (> (length str) 2)
  2684. (or (and (string-equal "[" (substring str 0 1))
  2685. (string-equal "]" (substring str -1)))
  2686. (and (string-equal "{" (substring str 0 1))
  2687. (string-equal "}" (substring str -1)))
  2688. (and (string-equal "(" (substring str 0 1))
  2689. (string-equal ")" (substring str -1)))))
  2690. (concat "'" (org-babel--script-escape-inner str)))
  2691. ((or force
  2692. (and (> (length str) 2)
  2693. (or (and (string-equal "'" (substring str 0 1))
  2694. (string-equal "'" (substring str -1)))
  2695. ;; We need to pass double-quoted strings
  2696. ;; through the backslash-twiddling bits, even
  2697. ;; though we don't need to change their
  2698. ;; delimiters.
  2699. (and (string-equal "\"" (substring str 0 1))
  2700. (string-equal "\"" (substring str -1))))))
  2701. (org-babel--script-escape-inner str))
  2702. (t str))))
  2703. (condition-case nil (org-babel-read escaped) (error escaped))))
  2704. (defun org-babel-read (cell &optional inhibit-lisp-eval)
  2705. "Convert the string value of CELL to a number if appropriate.
  2706. Otherwise if CELL looks like lisp (meaning it starts with a
  2707. \"(\", \"\\='\", \"\\=`\" or a \"[\") then read and evaluate it as
  2708. lisp, otherwise return it unmodified as a string. Optional
  2709. argument INHIBIT-LISP-EVAL inhibits lisp evaluation for
  2710. situations in which is it not appropriate."
  2711. (if (and (stringp cell) (not (equal cell "")))
  2712. (or (org-babel-number-p cell)
  2713. (if (and (not inhibit-lisp-eval)
  2714. (or (member (substring cell 0 1) '("(" "'" "`" "["))
  2715. (string= cell "*this*")))
  2716. (eval (read cell))
  2717. (if (string= (substring cell 0 1) "\"")
  2718. (read cell)
  2719. (progn (set-text-properties 0 (length cell) nil cell) cell))))
  2720. cell))
  2721. (defun org-babel-number-p (string)
  2722. "If STRING represents a number return its value."
  2723. (if (and (string-match "[0-9]+" string)
  2724. (string-match "^-?[0-9]*\\.?[0-9]*$" string)
  2725. (= (length (substring string (match-beginning 0)
  2726. (match-end 0)))
  2727. (length string)))
  2728. (string-to-number string)))
  2729. (defun org-babel-import-elisp-from-file (file-name &optional separator)
  2730. "Read the results located at FILE-NAME into an elisp table.
  2731. If the table is trivial, then return it as a scalar."
  2732. (let (result)
  2733. (save-window-excursion
  2734. (with-temp-buffer
  2735. (condition-case err
  2736. (progn
  2737. (org-table-import file-name separator)
  2738. (delete-file file-name)
  2739. (setq result (mapcar (lambda (row)
  2740. (mapcar #'org-babel-string-read row))
  2741. (org-table-to-lisp))))
  2742. (error (message "Error reading results: %s" err) nil)))
  2743. (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
  2744. (if (consp (car result))
  2745. (if (null (cdr (car result)))
  2746. (caar result)
  2747. result)
  2748. (car result))
  2749. result))))
  2750. (defun org-babel-string-read (cell)
  2751. "Strip nested \"s from around strings."
  2752. (org-babel-read (or (and (stringp cell)
  2753. (string-match "\\\"\\(.+\\)\\\"" cell)
  2754. (match-string 1 cell))
  2755. cell) t))
  2756. (defun org-babel-chomp (string &optional regexp)
  2757. "Strip a trailing space or carriage return from STRING.
  2758. The default regexp used is \"[ \\f\\t\\n\\r\\v]\" but another one
  2759. can be specified as the REGEXP argument."
  2760. (let ((regexp (or regexp "[ \f\t\n\r\v]")))
  2761. (while (and (> (length string) 0)
  2762. (string-match regexp (substring string -1)))
  2763. (setq string (substring string 0 -1)))
  2764. string))
  2765. (defun org-babel-trim (string &optional regexp)
  2766. "Strip a leading and trailing space or carriage return from STRING.
  2767. Like `org-babel-chomp', but run on both the first and last
  2768. character of the string."
  2769. (org-babel-chomp
  2770. (org-reverse-string
  2771. (org-babel-chomp (org-reverse-string string) regexp)) regexp))
  2772. (defun org-babel-tramp-handle-call-process-region
  2773. (start end program &optional delete buffer display &rest args)
  2774. "Use Tramp to handle `call-process-region'.
  2775. Fixes a bug in `tramp-handle-call-process-region'."
  2776. (if (and (featurep 'tramp) (file-remote-p default-directory))
  2777. (let ((tmpfile (tramp-compat-make-temp-file "")))
  2778. (write-region start end tmpfile)
  2779. (when delete (delete-region start end))
  2780. (unwind-protect
  2781. ;; (apply 'call-process program tmpfile buffer display args)
  2782. ;; bug in tramp
  2783. (apply 'process-file program tmpfile buffer display args)
  2784. (delete-file tmpfile)))
  2785. ;; org-babel-call-process-region-original is the original emacs
  2786. ;; definition. It is in scope from the let binding in
  2787. ;; org-babel-execute-src-block
  2788. (apply org-babel-call-process-region-original
  2789. start end program delete buffer display args)))
  2790. (defun org-babel-local-file-name (file)
  2791. "Return the local name component of FILE."
  2792. (or (file-remote-p file 'localname) file))
  2793. (defun org-babel-process-file-name (name &optional no-quote-p)
  2794. "Prepare NAME to be used in an external process.
  2795. If NAME specifies a remote location, the remote portion of the
  2796. name is removed, since in that case the process will be executing
  2797. remotely. The file name is then processed by `expand-file-name'.
  2798. Unless second argument NO-QUOTE-P is non-nil, the file name is
  2799. additionally processed by `shell-quote-argument'"
  2800. (let ((f (org-babel-local-file-name (expand-file-name name))))
  2801. (if no-quote-p f (shell-quote-argument f))))
  2802. (defvar org-babel-temporary-directory)
  2803. (unless (or noninteractive (boundp 'org-babel-temporary-directory))
  2804. (defvar org-babel-temporary-directory
  2805. (or (and (boundp 'org-babel-temporary-directory)
  2806. (file-exists-p org-babel-temporary-directory)
  2807. org-babel-temporary-directory)
  2808. (make-temp-file "babel-" t))
  2809. "Directory to hold temporary files created to execute code blocks.
  2810. Used by `org-babel-temp-file'. This directory will be removed on
  2811. Emacs shutdown."))
  2812. (defcustom org-babel-remote-temporary-directory "/tmp/"
  2813. "Directory to hold temporary files on remote hosts."
  2814. :group 'org-babel
  2815. :type 'string)
  2816. (defmacro org-babel-result-cond (result-params scalar-form &rest table-forms)
  2817. "Call the code to parse raw string results according to RESULT-PARAMS."
  2818. (declare (indent 1)
  2819. (debug (form form &rest form)))
  2820. (org-with-gensyms (params)
  2821. `(let ((,params ,result-params))
  2822. (unless (member "none" ,params)
  2823. (if (or (member "scalar" ,params)
  2824. (member "verbatim" ,params)
  2825. (member "html" ,params)
  2826. (member "code" ,params)
  2827. (member "pp" ,params)
  2828. (member "file" ,params)
  2829. (and (or (member "output" ,params)
  2830. (member "raw" ,params)
  2831. (member "org" ,params)
  2832. (member "drawer" ,params))
  2833. (not (member "table" ,params))))
  2834. ,scalar-form
  2835. ,@table-forms)))))
  2836. (def-edebug-spec org-babel-result-cond (form form body))
  2837. (defun org-babel-temp-file (prefix &optional suffix)
  2838. "Create a temporary file in the `org-babel-temporary-directory'.
  2839. Passes PREFIX and SUFFIX directly to `make-temp-file' with the
  2840. value of `temporary-file-directory' temporarily set to the value
  2841. of `org-babel-temporary-directory'."
  2842. (if (file-remote-p default-directory)
  2843. (let ((prefix
  2844. (concat (file-remote-p default-directory)
  2845. (expand-file-name
  2846. prefix org-babel-remote-temporary-directory))))
  2847. (make-temp-file prefix nil suffix))
  2848. (let ((temporary-file-directory
  2849. (or (and (boundp 'org-babel-temporary-directory)
  2850. (file-exists-p org-babel-temporary-directory)
  2851. org-babel-temporary-directory)
  2852. temporary-file-directory)))
  2853. (make-temp-file prefix nil suffix))))
  2854. (defun org-babel-remove-temporary-directory ()
  2855. "Remove `org-babel-temporary-directory' on Emacs shutdown."
  2856. (when (and (boundp 'org-babel-temporary-directory)
  2857. (file-exists-p org-babel-temporary-directory))
  2858. ;; taken from `delete-directory' in files.el
  2859. (condition-case nil
  2860. (progn
  2861. (mapc (lambda (file)
  2862. ;; This test is equivalent to
  2863. ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
  2864. ;; but more efficient
  2865. (if (eq t (car (file-attributes file)))
  2866. (delete-directory file)
  2867. (delete-file file)))
  2868. ;; We do not want to delete "." and "..".
  2869. (directory-files org-babel-temporary-directory 'full
  2870. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
  2871. (delete-directory org-babel-temporary-directory))
  2872. (error
  2873. (message "Failed to remove temporary Org-babel directory %s"
  2874. (if (boundp 'org-babel-temporary-directory)
  2875. org-babel-temporary-directory
  2876. "[directory not defined]"))))))
  2877. (add-hook 'kill-emacs-hook 'org-babel-remove-temporary-directory)
  2878. (defun org-babel-one-header-arg-safe-p (pair safe-list)
  2879. "Determine if the PAIR is a safe babel header arg according to SAFE-LIST.
  2880. For the format of SAFE-LIST, see `org-babel-safe-header-args'."
  2881. (and (consp pair)
  2882. (keywordp (car pair))
  2883. (stringp (cdr pair))
  2884. (or
  2885. (memq (car pair) safe-list)
  2886. (let ((entry (assq (car pair) safe-list)))
  2887. (and entry
  2888. (consp entry)
  2889. (cond ((functionp (cdr entry))
  2890. (funcall (cdr entry) (cdr pair)))
  2891. ((listp (cdr entry))
  2892. (member (cdr pair) (cdr entry)))
  2893. (t nil)))))))
  2894. (defun org-babel-generate-file-param (src-name params)
  2895. "Calculate the filename for source block results.
  2896. The directory is calculated from the :output-dir property of the
  2897. source block; if not specified, use the current directory.
  2898. If the source block has a #+NAME and the :file parameter does not
  2899. contain any period characters, then the :file parameter is
  2900. treated as an extension, and the output file name is the
  2901. concatenation of the directory (as calculated above), the block
  2902. name, a period, and the parameter value as a file extension.
  2903. Otherwise, the :file parameter is treated as a full file name,
  2904. and the output file name is the directory (as calculated above)
  2905. plus the parameter value."
  2906. (let* ((file-cons (assq :file params))
  2907. (file-ext-cons (assq :file-ext params))
  2908. (file-ext (cdr-safe file-ext-cons))
  2909. (dir (cdr-safe (assq :output-dir params)))
  2910. fname)
  2911. ;; create the output-dir if it does not exist
  2912. (when dir
  2913. (make-directory dir t))
  2914. (if file-cons
  2915. ;; :file given; add :output-dir if given
  2916. (when dir
  2917. (setcdr file-cons (concat (file-name-as-directory dir) (cdr file-cons))))
  2918. ;; :file not given; compute from name and :file-ext if possible
  2919. (when (and src-name file-ext)
  2920. (if dir
  2921. (setq fname (concat (file-name-as-directory (or dir ""))
  2922. src-name "." file-ext))
  2923. (setq fname (concat src-name "." file-ext)))
  2924. (setq params (cons (cons :file fname) params))))
  2925. params))
  2926. ;;; Used by backends: R, Maxima, Octave.
  2927. (defun org-babel-graphical-output-file (params)
  2928. "File where a babel block should send graphical output, per PARAMS."
  2929. (unless (assq :file params)
  2930. (if (assq :file-ext params)
  2931. (user-error ":file-ext given but no :file generated; did you forget to give a block a #+NAME?")
  2932. (user-error "No :file header argument given; cannot create graphical result.")))
  2933. (and (member "graphics" (cdr (assq :result-params params)))
  2934. (cdr (assq :file params))))
  2935. (provide 'ob-core)
  2936. ;; Local variables:
  2937. ;; generated-autoload-file: "org-loaddefs.el"
  2938. ;; End:
  2939. ;;; ob-core.el ends here