ob-core.el 120 KB

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