org-babel.el 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. ;;; org-babel.el --- facilitating communication between programming languages and people
  2. ;; Copyright (C) 2009 Eric Schulte, Dan Davison
  3. ;; Author: Eric Schulte, Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; See org-babel.org in the parent directory for more information
  24. ;;; Code:
  25. (require 'org)
  26. (defun org-babel-execute-src-block-maybe ()
  27. "Detect if this is context for a org-babel src-block and if so
  28. then run `org-babel-execute-src-block'."
  29. (interactive)
  30. (let ((info (org-babel-get-src-block-info)))
  31. (if info (progn (org-babel-execute-src-block current-prefix-arg info) t) nil)))
  32. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-src-block-maybe)
  33. (defadvice org-edit-special (around org-babel-prep-session-for-edit activate)
  34. "Prepare the current source block's session according to it's
  35. header arguments before editing in an org-src buffer. This
  36. function is called when `org-edit-special' is called with a
  37. prefix argument from inside of a source-code block."
  38. (when current-prefix-arg
  39. (let* ((info (org-babel-get-src-block-info))
  40. (lang (first info))
  41. (params (third info))
  42. (session (cdr (assoc :session params))))
  43. (when (and info session) ;; if we are in a source-code block which has a session
  44. (funcall (intern (concat "org-babel-prep-session:" lang)) session params))))
  45. ad-do-it)
  46. (defadvice org-open-at-point (around org-babel-open-at-point activate)
  47. "If `point' is on a source code block, then open that block's
  48. results with `org-babel-open-src-block-results', otherwise defer
  49. to `org-open-at-point'."
  50. (interactive "P")
  51. (or (call-interactively #'org-babel-open-src-block-result) ad-do-it))
  52. (defun org-babel-load-in-session-maybe ()
  53. "Detect if this is context for a org-babel src-block and if so
  54. then run `org-babel-load-in-session'."
  55. (interactive)
  56. (let ((info (org-babel-get-src-block-info)))
  57. (if info (progn (org-babel-load-in-session current-prefix-arg info) t) nil)))
  58. (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
  59. (defun org-babel-pop-to-session-maybe ()
  60. "Detect if this is context for a org-babel src-block and if so
  61. then run `org-babel-pop-to-session'."
  62. (interactive)
  63. (let ((info (org-babel-get-src-block-info)))
  64. (if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil)))
  65. (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
  66. (defconst org-babel-header-arg-names
  67. '(cache cmdline colnames dir exports file noweb results session tangle var)
  68. "Common header arguments used by org-babel. Note that
  69. individual languages may define their own language specific
  70. header arguments as well.")
  71. (defvar org-babel-default-header-args
  72. '((:session . "none") (:results . "replace") (:exports . "code") (:cache . "no") (:noweb . "no"))
  73. "Default arguments to use when evaluating a source block.")
  74. (defvar org-babel-default-inline-header-args
  75. '((:session . "none") (:results . "silent") (:exports . "results"))
  76. "Default arguments to use when evaluating an inline source block.")
  77. (defvar org-babel-src-block-regexp nil
  78. "Regexp used to test when inside of a org-babel src-block")
  79. (defvar org-babel-inline-src-block-regexp nil
  80. "Regexp used to test when on an inline org-babel src-block")
  81. (defvar org-babel-result-regexp
  82. "^[ \t]*#\\+res\\(ults\\|name\\)\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:"
  83. "Regular expression used to match result lines. If the
  84. results are associated with a hash key then the hash will be
  85. saved in the second match data.")
  86. (defvar org-babel-source-name-regexp
  87. "^[ \t]*#\\+\\(srcname\\|source\\|function\\):[ \t]*"
  88. "Regular expression used to match a source name line.")
  89. (defvar org-babel-min-lines-for-block-output 10
  90. "If number of lines of output is equal to or exceeds this
  91. value, the output is placed in a #+begin_example...#+end_example
  92. block. Otherwise the output is marked as literal by inserting
  93. colons at the starts of the lines. This variable only takes
  94. effect if the :results output option is in effect.")
  95. (defvar org-babel-noweb-error-langs nil
  96. "List of language for which errors should be raised when the
  97. source code block satisfying a noweb reference in this language
  98. can not be resolved.")
  99. (defvar org-babel-hash-show 4
  100. "Number of initial characters to show of a hidden results hash.")
  101. (defun org-babel-named-src-block-regexp-for-name (name)
  102. "Regexp used to match named src block."
  103. (concat org-babel-source-name-regexp (regexp-quote name) "[ \t\n]*"
  104. (substring org-babel-src-block-regexp 1)))
  105. (defun org-babel-set-interpreters (var value)
  106. (set-default var value)
  107. (setq org-babel-src-block-regexp
  108. (concat "^[ \t]*#\\+begin_src[ \t]+\\(" ;; (1) lang
  109. (mapconcat 'regexp-quote value "\\|")
  110. "\\)[ \t]*"
  111. "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)" ;; (2) switches
  112. "\\([^\n]*\\)\n" ;; (3) header arguments
  113. "\\([^\000]+?\\)#\\+end_src")) ;; (4) body
  114. (setq org-babel-inline-src-block-regexp
  115. (concat "[ \f\t\n\r\v]\\(src_" ;; (1) replacement target
  116. "\\(" ;; (2) lang
  117. (mapconcat 'regexp-quote value "\\|")
  118. "\\)"
  119. "\\(\\|\\[\\(.*\\)\\]\\)" ;; (3,4) (unused, headers)
  120. "{\\([^\f\n\r\v]+\\)}" ;; (5) body
  121. "\\)")))
  122. (defun org-babel-add-interpreter (interpreter)
  123. "Add INTERPRETER to `org-babel-interpreters' and update
  124. `org-babel-src-block-regexp' appropriately."
  125. (unless (member interpreter org-babel-interpreters)
  126. (setq org-babel-interpreters (cons interpreter org-babel-interpreters))
  127. (org-babel-set-interpreters 'org-babel-interpreters org-babel-interpreters)))
  128. (defcustom org-babel-interpreters '()
  129. "Interpreters allows for evaluation tags.
  130. This is a list of program names (as strings) that can evaluate code and
  131. insert the output into an Org-mode buffer. Valid choices are
  132. R Evaluate R code
  133. emacs-lisp Evaluate Emacs Lisp code and display the result
  134. sh Pass command to the shell and display the result
  135. perl The perl interpreter
  136. python The python interpreter
  137. ruby The ruby interpreter
  138. The source block regexp `org-babel-src-block-regexp' is updated
  139. when a new interpreter is added to this list through the
  140. customize interface. To add interpreters to this variable from
  141. lisp code use the `org-babel-add-interpreter' function."
  142. :group 'org-babel
  143. :set 'org-babel-set-interpreters
  144. :type '(set :greedy t
  145. (const "R")
  146. (const "emacs-lisp")
  147. (const "sh")
  148. (const "perl")
  149. (const "python")
  150. (const "ruby")))
  151. ;;; functions
  152. (defun org-babel-execute-src-block (&optional arg info params)
  153. "Execute the current source code block, and insert the results
  154. into the buffer. Source code execution and the collection and
  155. formatting of results can be controlled through a variety of
  156. header arguments.
  157. Optionally supply a value for INFO in the form returned by
  158. `org-babel-get-src-block-info'.
  159. Optionally supply a value for PARAMS which will be merged with
  160. the header arguments specified at the front of the source code
  161. block."
  162. (interactive)
  163. ;; (message "supplied params=%S" params) ;; debugging
  164. (let* ((info (or info (org-babel-get-src-block-info)))
  165. (lang (first info))
  166. (params (setf (third info)
  167. (sort (org-babel-merge-params (third info) params)
  168. (lambda (el1 el2) (string< (symbol-name (car el1))
  169. (symbol-name (car el2)))))))
  170. (new-hash (if (and (cdr (assoc :cache params))
  171. (string= "yes" (cdr (assoc :cache params)))) (org-babel-sha1-hash info)))
  172. (old-hash (org-babel-result-hash info))
  173. (body (setf (second info)
  174. (if (and (cdr (assoc :noweb params))
  175. (string= "yes" (cdr (assoc :noweb params))))
  176. (org-babel-expand-noweb-references info) (second info))))
  177. (result-params (split-string (or (cdr (assoc :results params)) "")))
  178. (result-type (cond ((member "output" result-params) 'output)
  179. ((member "value" result-params) 'value)
  180. (t 'value)))
  181. (cmd (intern (concat "org-babel-execute:" lang)))
  182. result)
  183. ;; (message "params=%S" params) ;; debugging
  184. (unless (member lang org-babel-interpreters)
  185. (error "Language is not in `org-babel-interpreters': %s" lang))
  186. (if (and (not arg) new-hash (equal new-hash old-hash))
  187. (save-excursion ;; return cached result
  188. (goto-char (org-babel-where-is-src-block-result nil info))
  189. (move-end-of-line 1) (forward-char 1)
  190. (setq result (org-babel-read-result))
  191. (message (replace-regexp-in-string "%" "%%" (format "%S" result))) result)
  192. (setq result (funcall cmd body params))
  193. (if (eq result-type 'value)
  194. (setq result (if (and (or (member "vector" result-params)
  195. (member "table" result-params))
  196. (not (listp result)))
  197. (list (list result))
  198. result)))
  199. (org-babel-insert-result result result-params info new-hash)
  200. result)))
  201. (defun org-babel-load-in-session (&optional arg info)
  202. "Load the body of the current source-code block. Evaluate the
  203. header arguments for the source block before entering the
  204. session. After loading the body this pops open the session."
  205. (interactive)
  206. (let* ((info (or info (org-babel-get-src-block-info)))
  207. (lang (first info))
  208. (body (second info))
  209. (params (third info))
  210. (session (cdr (assoc :session params))))
  211. (unless (member lang org-babel-interpreters)
  212. (error "Language is not in `org-babel-interpreters': %s" lang))
  213. ;; if called with a prefix argument, then process header arguments
  214. (pop-to-buffer (funcall (intern (concat "org-babel-load-session:" lang)) session body params))
  215. (move-end-of-line 1)))
  216. (defun org-babel-pop-to-session (&optional arg info)
  217. "Pop to the session of the current source-code block. If
  218. called with a prefix argument then evaluate the header arguments
  219. for the source block before entering the session. Copy the body
  220. of the source block to the kill ring."
  221. (interactive)
  222. (let* ((info (or info (org-babel-get-src-block-info)))
  223. (lang (first info))
  224. (body (second info))
  225. (params (third info))
  226. (session (cdr (assoc :session params))))
  227. (unless (member lang org-babel-interpreters)
  228. (error "Language is not in `org-babel-interpreters': %s" lang))
  229. ;; copy body to the kill ring
  230. (with-temp-buffer (insert (org-babel-trim body)) (copy-region-as-kill (point-min) (point-max)))
  231. ;; if called with a prefix argument, then process header arguments
  232. (if arg (funcall (intern (concat "org-babel-prep-session:" lang)) session params))
  233. ;; just to the session using pop-to-buffer
  234. (pop-to-buffer (funcall (intern (format "org-babel-%s-initiate-session" lang)) session))
  235. (move-end-of-line 1)))
  236. (defun org-babel-open-src-block-result (&optional re-run)
  237. "If `point' is on a src block then open the results of the
  238. source code block, otherwise return nil. With optional prefix
  239. argument RE-RUN the source-code block is evaluated even if
  240. results already exist."
  241. (interactive "P")
  242. (when (org-babel-get-src-block-info)
  243. (save-excursion
  244. ;; go to the results, if there aren't any then run the block
  245. (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
  246. (progn (org-babel-execute-src-block)
  247. (org-babel-where-is-src-block-result))))
  248. (move-end-of-line 1) (forward-char 1)
  249. ;; open the results
  250. (if (looking-at org-bracket-link-regexp)
  251. ;; file results
  252. (org-open-at-point)
  253. (let ((results (org-babel-read-result)))
  254. (flet ((echo-res (result)
  255. (if (stringp result) result (format "%S" result))))
  256. (pop-to-buffer (get-buffer-create "org-babel-results"))
  257. (delete-region (point-min) (point-max))
  258. (if (listp results)
  259. ;; table result
  260. (insert (orgtbl-to-generic results '(:sep "\t" :fmt echo-res)))
  261. ;; scalar result
  262. (insert (echo-res results))))))
  263. t)))
  264. (defun org-babel-execute-buffer (&optional arg)
  265. "Call `org-babel-execute-src-block' on every source block in
  266. the current buffer."
  267. (interactive "P")
  268. (save-excursion
  269. (goto-char (point-min))
  270. (while (re-search-forward org-babel-src-block-regexp nil t)
  271. (let ((pos-end (match-end 0)))
  272. (goto-char (match-beginning 0))
  273. (org-babel-execute-src-block arg)
  274. (goto-char pos-end)))))
  275. (defun org-babel-execute-subtree (&optional arg)
  276. "Call `org-babel-execute-src-block' on every source block in
  277. the current subtree."
  278. (interactive "P")
  279. (save-excursion
  280. (org-narrow-to-subtree)
  281. (org-babel-execute-buffer)
  282. (widen)))
  283. (defun org-babel-get-src-block-info (&optional header-vars-only)
  284. "Get information of the current source block.
  285. Returns a list
  286. (language body header-arguments-alist switches name function-args).
  287. Unless HEADER-VARS-ONLY is non-nil, any variable
  288. references provided in 'function call style' (i.e. in a
  289. parenthesised argument list following the src block name) are
  290. added to the header-arguments-alist."
  291. (let ((case-fold-search t) head info args)
  292. (if (setq head (org-babel-where-is-src-block-head))
  293. (save-excursion
  294. (goto-char head)
  295. (setq info (org-babel-parse-src-block-match))
  296. (forward-line -1)
  297. (when (looking-at (concat org-babel-source-name-regexp
  298. "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
  299. (setq info (append info (list (org-babel-clean-text-properties (match-string 2)))))
  300. ;; Note that e.g. "name()" and "name( )" result in ((:var . "")).
  301. ;; We maintain that behaviour, and the resulting non-nil sixth
  302. ;; element is relied upon in org-babel-exp-code to detect a functional-style
  303. ;; block in those cases. However, "name" without any
  304. ;; parentheses would result in the same thing, so we
  305. ;; explicitly avoid that.
  306. (if (setq args (match-string 4))
  307. (setq info (append info (list (mapcar (lambda (ref) (cons :var ref))
  308. (org-babel-ref-split-args args))))))
  309. (unless header-vars-only
  310. (setf (third info)
  311. (org-babel-merge-params (sixth info) (third info)))))
  312. info)
  313. (if (save-excursion ;; inline source block
  314. (re-search-backward "[ \f\t\n\r\v]" nil t)
  315. (looking-at org-babel-inline-src-block-regexp))
  316. (org-babel-parse-inline-src-block-match)
  317. nil)))) ;; indicate that no source block was found
  318. (defun org-babel-sha1-hash (&optional info)
  319. (interactive)
  320. (let* ((info (or info (org-babel-get-src-block-info)))
  321. (hash (sha1 (format "%s-%s" (mapconcat (lambda (arg) (format "%S" arg))
  322. (third info) ":")
  323. (second info)))))
  324. (when (interactive-p) (message hash))
  325. hash))
  326. (defun org-babel-result-hash (&optional info)
  327. (org-babel-where-is-src-block-result nil info)
  328. (org-babel-clean-text-properties (match-string 3)))
  329. (defun org-babel-hide-hash ()
  330. "Hide the hash in the current results line. Only the initial
  331. `org-babel-hash-show' characters of the hash will remain
  332. visible."
  333. (org-add-to-invisibility-spec '(org-babel-hide-hash . t))
  334. (save-excursion
  335. (when (and (re-search-forward org-babel-result-regexp nil t)
  336. (match-string 3))
  337. (let* ((start (match-beginning 3))
  338. (hide-start (+ org-babel-hash-show start))
  339. (end (match-end 3))
  340. (hash (match-string 3))
  341. ov1 ov2)
  342. (setq ov1 (org-make-overlay start hide-start))
  343. (setq ov2 (org-make-overlay hide-start end))
  344. (org-overlay-put ov2 'invisible 'org-babel-hide-hash)
  345. (org-overlay-put ov1 'babel-hash hash)))))
  346. (defun org-babel-hide-all-hashes ()
  347. "Hide the hash in the current buffer. Only the initial
  348. `org-babel-hash-show' characters of each hash will remain
  349. visible. This function should be called as part of the
  350. `org-mode-hook'."
  351. (save-excursion
  352. (while (re-search-forward org-babel-result-regexp nil t)
  353. (goto-char (match-beginning 0))
  354. (org-babel-hide-hash)
  355. (goto-char (match-end 0)))))
  356. (add-hook 'org-mode-hook 'org-babel-hide-all-hashes)
  357. (defun org-babel-hash-at-point (&optional point)
  358. "Return the value of the hash at `point'. The hash is also
  359. added as the last element of the kill ring. This can be called
  360. with C-c C-c."
  361. (interactive)
  362. (let ((hash (car (delq nil (mapcar
  363. (lambda (ol) (org-overlay-get ol 'babel-hash))
  364. (org-overlays-at (or point (point))))))))
  365. (when hash (kill-new hash) (message hash))))
  366. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
  367. (defun org-babel-result-hide-spec ()
  368. (org-add-to-invisibility-spec '(org-babel-hide-result . t)))
  369. (add-hook 'org-mode-hook 'org-babel-result-hide-spec)
  370. (defvar org-babel-hide-result-overlays nil
  371. "Overlays hiding results.")
  372. (defun org-babel-result-hide-all ()
  373. "Fold all results in the current buffer."
  374. (interactive)
  375. (org-babel-show-result-all)
  376. (save-excursion
  377. (while (re-search-forward org-babel-result-regexp nil t)
  378. (save-excursion (goto-char (match-beginning 0))
  379. (org-babel-hide-result-toggle-maybe)))))
  380. (defun org-babel-show-result-all ()
  381. "Unfold all results in the current buffer."
  382. (mapc 'org-delete-overlay org-babel-hide-result-overlays)
  383. (setq org-babel-hide-result-overlays nil))
  384. (defun org-babel-hide-result-toggle-maybe ()
  385. "Toggle visibility of result at point."
  386. (interactive)
  387. (let ((case-fold-search t))
  388. (if (save-excursion
  389. (beginning-of-line 1)
  390. (looking-at org-babel-result-regexp))
  391. (progn (org-babel-hide-result-toggle)
  392. t) ;; to signal that we took action
  393. nil))) ;; to signal that we did not
  394. (defun org-babel-hide-result-toggle (&optional force)
  395. "Toggle the visibility of the current result."
  396. (interactive)
  397. (save-excursion
  398. (beginning-of-line)
  399. (if (re-search-forward org-babel-result-regexp nil t)
  400. (let ((start (progn (beginning-of-line 2) (- (point) 1)))
  401. (end (progn (goto-char (- (org-babel-result-end) 1)) (point)))
  402. ov)
  403. (if (memq t (mapcar (lambda (overlay)
  404. (eq (org-overlay-get overlay 'invisible)
  405. 'org-babel-hide-result))
  406. (org-overlays-at start)))
  407. (if (or (not force) (eq force 'off))
  408. (mapc (lambda (ov)
  409. (when (member ov org-babel-hide-result-overlays)
  410. (setq org-babel-hide-result-overlays
  411. (delq ov org-babel-hide-result-overlays)))
  412. (when (eq (org-overlay-get ov 'invisible)
  413. 'org-babel-hide-result)
  414. (org-delete-overlay ov)))
  415. (org-overlays-at start)))
  416. (setq ov (org-make-overlay start end))
  417. (org-overlay-put ov 'invisible 'org-babel-hide-result)
  418. ;; make the block accessible to isearch
  419. (org-overlay-put
  420. ov 'isearch-open-invisible
  421. (lambda (ov)
  422. (when (member ov org-babel-hide-result-overlays)
  423. (setq org-babel-hide-result-overlays
  424. (delq ov org-babel-hide-result-overlays)))
  425. (when (eq (org-overlay-get ov 'invisible)
  426. 'org-babel-hide-result)
  427. (org-delete-overlay ov))))
  428. (push ov org-babel-hide-result-overlays)))
  429. (error "Not looking at a result line"))))
  430. ;; org-tab-after-check-for-cycling-hook
  431. (add-hook 'org-tab-first-hook 'org-babel-hide-result-toggle-maybe)
  432. ;; Remove overlays when changing major mode
  433. (add-hook 'org-mode-hook
  434. (lambda () (org-add-hook 'change-major-mode-hook
  435. 'org-babel-show-result-all 'append 'local)))
  436. (defmacro org-babel-map-source-blocks (file &rest body)
  437. "Evaluate BODY forms on each source-block in FILE."
  438. (declare (indent 1))
  439. `(let ((visited-p (get-buffer (file-name-nondirectory ,file))))
  440. (save-window-excursion
  441. (find-file ,file) (goto-char (point-min))
  442. (while (re-search-forward org-babel-src-block-regexp nil t)
  443. (goto-char (match-beginning 0))
  444. (save-match-data ,@body)
  445. (goto-char (match-end 0))))
  446. (unless visited-p (kill-buffer (file-name-nondirectory file)))))
  447. (defun org-babel-params-from-properties ()
  448. "Return an association list of any source block params which
  449. may be specified in the properties of the current outline entry."
  450. (save-match-data
  451. (delq nil
  452. (mapcar
  453. (lambda (header-arg)
  454. (let ((val (or (condition-case nil
  455. (org-entry-get (point) header-arg t)
  456. (error nil))
  457. (cdr (assoc header-arg org-file-properties)))))
  458. (when val
  459. ;; (message "prop %s=%s" header-arg val) ;; debugging
  460. (cons (intern (concat ":" header-arg)) val))))
  461. (mapcar 'symbol-name org-babel-header-arg-names)))))
  462. (defun org-babel-parse-src-block-match ()
  463. (let* ((lang (org-babel-clean-text-properties (match-string 1)))
  464. (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
  465. (switches (match-string 2))
  466. (body (org-babel-clean-text-properties (match-string 4)))
  467. (preserve-indentation (or org-src-preserve-indentation
  468. (string-match "-i\\>" switches))))
  469. (list lang
  470. ;; get src block body removing properties, protective commas, and indentation
  471. (with-temp-buffer
  472. (save-match-data
  473. (insert (org-babel-strip-protective-commas body))
  474. (unless preserve-indentation (org-do-remove-indentation))
  475. (buffer-string)))
  476. (org-babel-merge-params
  477. org-babel-default-header-args
  478. (org-babel-params-from-properties)
  479. (if (boundp lang-headers) (eval lang-headers) nil)
  480. (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 3) ""))))
  481. switches)))
  482. (defun org-babel-parse-inline-src-block-match ()
  483. (let* ((lang (org-babel-clean-text-properties (match-string 2)))
  484. (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
  485. (list lang
  486. (org-babel-strip-protective-commas (org-babel-clean-text-properties (match-string 5)))
  487. (org-babel-merge-params
  488. org-babel-default-inline-header-args
  489. (org-babel-params-from-properties)
  490. (if (boundp lang-headers) (eval lang-headers) nil)
  491. (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 4) "")))))))
  492. (defun org-babel-parse-header-arguments (arg-string)
  493. "Parse a string of header arguments returning an alist."
  494. (if (> (length arg-string) 0)
  495. (delq nil
  496. (mapcar
  497. (lambda (arg)
  498. (if (string-match "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)" arg)
  499. (cons (intern (concat ":" (match-string 1 arg)))
  500. (let ((raw (org-babel-chomp (match-string 2 arg))))
  501. (if (org-babel-number-p raw) raw (eval (org-babel-read raw)))))
  502. (cons (intern (concat ":" arg)) nil)))
  503. (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t)))))
  504. (defun org-babel-process-params (params)
  505. "Parse params and resolve references.
  506. Return a list (session vars result-params result-type)."
  507. (let* ((session (cdr (assoc :session params)))
  508. (vars (org-babel-ref-variables params))
  509. (result-params (split-string (or (cdr (assoc :results params)) "")))
  510. (result-type (cond ((member "output" result-params) 'output)
  511. ((member "value" result-params) 'value)
  512. (t 'value))))
  513. (list session vars result-params result-type)))
  514. (defun org-babel-where-is-src-block-head ()
  515. "Return the point at the beginning of the current source
  516. block. Specifically at the beginning of the #+BEGIN_SRC line.
  517. If the point is not on a source block then return nil."
  518. (let ((initial (point)) top bottom)
  519. (or
  520. (save-excursion ;; on a source name line
  521. (beginning-of-line 1)
  522. (and (looking-at org-babel-source-name-regexp) (forward-line 1)
  523. (looking-at org-babel-src-block-regexp)
  524. (point)))
  525. (save-excursion ;; on a #+begin_src line
  526. (beginning-of-line 1)
  527. (and (looking-at org-babel-src-block-regexp)
  528. (point)))
  529. (save-excursion ;; inside a src block
  530. (and
  531. (re-search-backward "#\\+begin_src" nil t) (setq top (point))
  532. (re-search-forward "#\\+end_src" nil t) (setq bottom (point))
  533. (< top initial) (< initial bottom)
  534. (goto-char top) (move-beginning-of-line 1)
  535. (looking-at org-babel-src-block-regexp)
  536. (point))))))
  537. (defun org-babel-goto-named-source-block (&optional name)
  538. "Go to a named source-code block."
  539. (interactive "ssource-block name: ")
  540. (let ((point (org-babel-find-named-block name)))
  541. (if point
  542. ;; taken from `org-open-at-point'
  543. (progn (goto-char point) (org-show-context))
  544. (message "source-code block '%s' not found in this buffer" name))))
  545. (defun org-babel-find-named-block (name)
  546. "Find a named source-code block.
  547. Return the location of the source block identified by source
  548. NAME, or nil if no such block exists. Set match data according to
  549. org-babel-named-src-block-regexp."
  550. (save-excursion
  551. (let ((case-fold-search t)
  552. (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
  553. (goto-char (point-min))
  554. (when (or (re-search-forward regexp nil t)
  555. (re-search-backward regexp nil t))
  556. (match-beginning 0)))))
  557. (defun org-babel-find-named-result (name)
  558. "Return the location of the result named NAME in the current
  559. buffer or nil if no such result exists."
  560. (save-excursion
  561. (goto-char (point-min))
  562. (when (re-search-forward
  563. (concat org-babel-result-regexp "[ \t]" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
  564. (move-beginning-of-line 0) (point))))
  565. (defun org-babel-where-is-src-block-result (&optional insert info hash)
  566. "Return the point at the beginning of the result of the current
  567. source block. Specifically at the beginning of the results line.
  568. If no result exists for this block then create a results line
  569. following the source block."
  570. (save-excursion
  571. (let* ((on-lob-line (progn (beginning-of-line 1)
  572. (looking-at org-babel-lob-one-liner-regexp)))
  573. (name (if on-lob-line (first (org-babel-lob-get-info))
  574. (fifth (or info (org-babel-get-src-block-info)))))
  575. (head (unless on-lob-line (org-babel-where-is-src-block-head))) end)
  576. (when head (goto-char head))
  577. (or (and name (org-babel-find-named-result name))
  578. (and (or on-lob-line (re-search-forward "#\\+end_src" nil t))
  579. (progn (move-end-of-line 1)
  580. (if (eobp) (insert "\n") (forward-char 1))
  581. (setq end (point))
  582. (or (and (not name)
  583. (progn ;; unnamed results line already exists
  584. (re-search-forward "[^ \f\t\n\r\v]" nil t)
  585. (move-beginning-of-line 1)
  586. (looking-at (concat org-babel-result-regexp "\n"))))
  587. ;; or (with optional insert) back up and make one ourselves
  588. (when insert
  589. (goto-char end)
  590. (if (looking-at "[\n\r]") (forward-char 1) (insert "\n"))
  591. (insert (concat "#+results" (if hash (concat "["hash"]"))
  592. ":"(if name (concat " " name)) "\n"))
  593. (move-beginning-of-line 0)
  594. (if hash (org-babel-hide-hash)) t)))
  595. (point))))))
  596. (defun org-babel-read-result ()
  597. "Read the result at `point' into emacs-lisp."
  598. (let ((case-fold-search t) result-string)
  599. (cond
  600. ((org-at-table-p) (org-babel-read-table))
  601. ((looking-at org-bracket-link-regexp) (org-babel-read-link))
  602. ((looking-at org-block-regexp) (org-babel-trim (match-string 4)))
  603. ((looking-at ": ")
  604. (setq result-string
  605. (org-babel-trim
  606. (mapconcat (lambda (line) (if (and (> (length line) 1)
  607. (string= ": " (substring line 0 2)))
  608. (substring line 2)
  609. line))
  610. (split-string
  611. (buffer-substring (point) (org-babel-result-end)) "[\r\n]+")
  612. "\n")))
  613. (or (org-babel-number-p result-string) result-string))
  614. ((looking-at org-babel-result-regexp)
  615. (save-excursion (forward-line 1) (org-babel-read-result))))))
  616. (defun org-babel-read-table ()
  617. "Read the table at `point' into emacs-lisp."
  618. (mapcar (lambda (row)
  619. (if (and (symbolp row) (equal row 'hline)) row
  620. (mapcar #'org-babel-read row)))
  621. (org-table-to-lisp)))
  622. (defun org-babel-read-link ()
  623. "Read the link at `point' into emacs-lisp. If the path of the
  624. link is a file path it is expanded using `expand-file-name'."
  625. (let* ((case-fold-search t)
  626. (raw (and (looking-at org-bracket-link-regexp)
  627. (org-babel-clean-text-properties (match-string 1))))
  628. (type (and (string-match org-link-types-re raw)
  629. (match-string 1 raw))))
  630. (cond
  631. ((not type) (expand-file-name raw))
  632. ((string= type "file")
  633. (and (string-match "file\\(.*\\):\\(.+\\)" raw)
  634. (expand-file-name (match-string 2 raw))))
  635. (t raw))))
  636. (defun org-babel-insert-result (result &optional result-params info hash)
  637. "Insert RESULT into the current buffer after the end of the
  638. current source block. With optional argument RESULT-PARAMS
  639. controls insertion of results in the org-mode file.
  640. RESULT-PARAMS can take the following values...
  641. replace - (default option) insert results after the source block
  642. replacing any previously inserted results
  643. silent -- no results are inserted
  644. file ---- the results are interpreted as a file path, and are
  645. inserted into the buffer using the Org-mode file syntax
  646. raw ----- results are added directly to the org-mode file. This
  647. is a good option if you code block will output org-mode
  648. formatted text.
  649. org ----- this is the same as the 'raw' option
  650. html ---- results are added inside of a #+BEGIN_HTML block. This
  651. is a good option if you code block will output html
  652. formatted text.
  653. latex --- results are added inside of a #+BEGIN_LATEX block.
  654. This is a good option if you code block will output
  655. latex formatted text.
  656. code ---- the results are extracted in the syntax of the source
  657. code of the language being evaluated and are added
  658. inside of a #+BEGIN_SRC block with the source-code
  659. language set appropriately."
  660. (if (stringp result)
  661. (progn
  662. (setq result (org-babel-clean-text-properties result))
  663. (when (member "file" result-params)
  664. (setq result (org-babel-result-to-file result))))
  665. (unless (listp result) (setq result (format "%S" result))))
  666. (if (and result-params (member "replace" result-params)
  667. (not (member "silent" result-params)))
  668. (org-babel-remove-result info))
  669. (if (= (length result) 0)
  670. (if (member "value" result-params)
  671. (message "No result returned by source block")
  672. (message "Source block produced no output"))
  673. (if (and result-params (member "silent" result-params))
  674. (progn (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
  675. result)
  676. (when (and (stringp result) ;; ensure results end in a newline
  677. (not (or (string-equal (substring result -1) "\n")
  678. (string-equal (substring result -1) "\r"))))
  679. (setq result (concat result "\n")))
  680. (save-excursion
  681. (let ((existing-result (org-babel-where-is-src-block-result t info hash))
  682. (results-switches (cdr (assoc :results_switches (third info)))))
  683. (when existing-result (goto-char existing-result) (forward-line 1))
  684. (setq results-switches
  685. (if results-switches (concat " " results-switches) ""))
  686. (cond
  687. ;; assume the result is a table if it's not a string
  688. ((not (stringp result))
  689. (insert (concat (orgtbl-to-orgtbl
  690. (if (and (listp (car result))
  691. (listp (cdr (car result))))
  692. result (list result))
  693. '(:fmt (lambda (cell) (format "%s" cell)))) "\n"))
  694. (forward-line -1) (org-cycle))
  695. ((member "file" result-params)
  696. (insert result))
  697. ((member "html" result-params)
  698. (insert (format "#+BEGIN_HTML%s\n%s#+END_HTML\n" results-switches result)))
  699. ((member "latex" result-params)
  700. (insert (format "#+BEGIN_LaTeX%s\n%s#+END_LaTeX\n" results-switches result)))
  701. ((member "code" result-params)
  702. (insert (format "#+BEGIN_SRC %s%s\n%s#+END_SRC\n" lang results-switches result)))
  703. ((or (member "raw" result-params) (member "org" result-params))
  704. (save-excursion (insert result)) (if (org-at-table-p) (org-cycle)))
  705. (t
  706. (org-babel-examplize-region
  707. (point) (progn (insert result) (point)) results-switches)))))
  708. (message "finished"))))
  709. (defun org-babel-result-to-org-string (result)
  710. "Return RESULT as a string in org-mode format. This function
  711. relies on `org-babel-insert-result'."
  712. (with-temp-buffer (org-babel-insert-result result) (buffer-string)))
  713. (defun org-babel-remove-result (&optional info)
  714. "Remove the result of the current source block."
  715. (interactive)
  716. (let ((location (org-babel-where-is-src-block-result nil info)) start)
  717. (when location
  718. (save-excursion
  719. (goto-char location) (setq start (point)) (forward-line 1)
  720. (delete-region start (org-babel-result-end))))))
  721. (defun org-babel-result-end ()
  722. "Return the point at the end of the current set of results"
  723. (save-excursion
  724. (if (org-at-table-p)
  725. (progn (goto-char (org-table-end)) (point))
  726. (let ((case-fold-search t))
  727. (cond
  728. ((looking-at "#\\+begin_latex")
  729. (search-forward "#+end_latex" nil t)
  730. (forward-line 1))
  731. ((looking-at "#\\+begin_html")
  732. (search-forward "#+end_html" nil t)
  733. (forward-line 1))
  734. ((looking-at "#\\+begin_example")
  735. (search-forward "#+end_example" nil t)
  736. (forward-line 1))
  737. ((looking-at "#\\+begin_src")
  738. (search-forward "#+end_src" nil t)
  739. (forward-line 1))
  740. (t (progn (while (looking-at "\\(: \\|\\[\\[\\)")
  741. (forward-line 1))))))
  742. (point))))
  743. (defun org-babel-result-to-file (result)
  744. "Return an `org-mode' link with the path being the value or
  745. RESULT, and the display being the `file-name-nondirectory' if
  746. non-nil."
  747. (concat "[[file:" result "]]"))
  748. (defun org-babel-examplize-region (beg end &optional results-switches)
  749. "Comment out region using the ': ' org example quote."
  750. (interactive "*r")
  751. (let ((size (abs (- (line-number-at-pos end)
  752. (line-number-at-pos beg)))))
  753. (save-excursion
  754. (cond ((= size 0)
  755. (error "This should be impossible: a newline was appended to result if missing"))
  756. ((< size org-babel-min-lines-for-block-output)
  757. (goto-char beg)
  758. (dotimes (n size)
  759. (move-beginning-of-line 1) (insert ": ") (forward-line 1)))
  760. (t
  761. (goto-char beg)
  762. (insert (if results-switches
  763. (format "#+begin_example%s\n" results-switches)
  764. "#+begin_example\n"))
  765. (forward-char (- end beg))
  766. (insert "#+end_example\n"))))))
  767. (defun org-babel-merge-params (&rest plists)
  768. "Combine all parameter association lists in PLISTS. Later
  769. elements of PLISTS override the values of previous element. This
  770. takes into account some special considerations for certain
  771. parameters when merging lists."
  772. (let ((results-exclusive-groups
  773. '(("file" "vector" "table" "scalar" "raw" "org" "html" "latex" "code" "pp")
  774. ("replace" "silent")
  775. ("output" "value")))
  776. (exports-exclusive-groups
  777. '(("code" "results" "both" "none")))
  778. params results exports tangle noweb cache vars var ref shebang comments)
  779. (flet ((e-merge (exclusive-groups &rest result-params)
  780. ;; maintain exclusivity of mutually exclusive parameters
  781. (let (output)
  782. (mapc (lambda (new-params)
  783. (mapc (lambda (new-param)
  784. (mapc (lambda (exclusive-group)
  785. (when (member new-param exclusive-group)
  786. (mapcar (lambda (excluded-param)
  787. (setq output (delete excluded-param output)))
  788. exclusive-group)))
  789. exclusive-groups)
  790. (setq output (org-uniquify (cons new-param output))))
  791. new-params))
  792. result-params)
  793. output)))
  794. (mapc (lambda (plist)
  795. (mapc (lambda (pair)
  796. (case (car pair)
  797. (:var
  798. ;; we want only one specification per variable
  799. (when (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*=[ \t]*\\([^\f\n\r\v]+\\)$" (cdr pair))
  800. ;; TODO: When is this not true?
  801. (setq var (intern (match-string 1 (cdr pair)))
  802. ref (match-string 2 (cdr pair))
  803. vars (cons (cons var ref) (assq-delete-all var vars)))))
  804. (:results
  805. (setq results
  806. (e-merge results-exclusive-groups results (split-string (cdr pair)))))
  807. (:file
  808. (when (cdr pair)
  809. (setq results (e-merge results-exclusive-groups results '("file")))
  810. (unless (or (member "both" exports)
  811. (member "none" exports)
  812. (member "code" exports))
  813. (setq exports (e-merge exports-exclusive-groups exports '("results"))))
  814. (setq params (cons pair (assq-delete-all (car pair) params)))))
  815. (:exports
  816. (setq exports (e-merge exports-exclusive-groups
  817. exports (split-string (cdr pair)))))
  818. (:tangle ;; take the latest -- always overwrite
  819. (setq tangle (or (list (cdr pair)) tangle)))
  820. (:noweb
  821. (setq noweb (e-merge '(("yes" "no"))
  822. noweb (split-string (or (cdr pair) "")))))
  823. (:cache
  824. (setq cache (e-merge '(("yes" "no"))
  825. cache (split-string (or (cdr pair) "")))))
  826. (:shebang ;; take the latest -- always overwrite
  827. (setq shebang (or (list (cdr pair)) shebang)))
  828. (:comments
  829. (setq comments (e-merge '(("yes" "no"))
  830. comments (split-string (or (cdr pair) "")))))
  831. (t ;; replace: this covers e.g. :session
  832. (setq params (cons pair (assq-delete-all (car pair) params))))))
  833. plist))
  834. plists))
  835. (setq vars (mapcar (lambda (pair) (format "%s=%s" (car pair) (cdr pair))) vars))
  836. (while vars (setq params (cons (cons :var (pop vars)) params)))
  837. (cons (cons :comments (mapconcat 'identity comments " "))
  838. (cons (cons :shebang (mapconcat 'identity shebang " "))
  839. (cons (cons :cache (mapconcat 'identity cache " "))
  840. (cons (cons :noweb (mapconcat 'identity noweb " "))
  841. (cons (cons :tangle (mapconcat 'identity tangle " "))
  842. (cons (cons :exports (mapconcat 'identity exports " "))
  843. (cons (cons :results (mapconcat 'identity results " "))
  844. params)))))))))
  845. (defun org-babel-expand-noweb-references (&optional info parent-buffer)
  846. "This function expands Noweb style references in the body of
  847. the current source-code block. For example the following
  848. reference would be replaced with the body of the source-code
  849. block named 'example-block'.
  850. <<example-block>>
  851. Note that any text preceding the <<foo>> construct on a line will
  852. be interposed between the lines of the replacement text. So for
  853. example if <<foo>> is placed behind a comment, then the entire
  854. replacement text will also be commented.
  855. This function must be called from inside of the buffer containing
  856. the source-code block which holds BODY.
  857. In addition the following syntax can be used to insert the
  858. results of evaluating the source-code block named 'example-block'.
  859. <<example-block()>>
  860. Any optional arguments can be passed to example-block by placing
  861. the arguments inside the parenthesis following the convention
  862. defined by `org-babel-lob'. For example
  863. <<example-block(a=9)>>
  864. would set the value of argument \"a\" equal to \"9\". Note that
  865. these arguments are not evaluated in the current source-code
  866. block but are passed literally to the \"example-block\"."
  867. (let* ((parent-buffer (or parent-buffer (current-buffer)))
  868. (info (or info (org-babel-get-src-block-info)))
  869. (lang (first info))
  870. (body (second info))
  871. (new-body "") index source-name evaluate prefix)
  872. (flet ((nb-add (text)
  873. (setq new-body (concat new-body text))))
  874. (with-temp-buffer
  875. (insert body) (goto-char (point-min))
  876. (funcall (intern (concat (or (and (cdr (assoc lang org-src-lang-modes))
  877. (symbol-name
  878. (cdr (assoc lang org-src-lang-modes))))
  879. lang) "-mode")))
  880. (setq index (point))
  881. (while (and (re-search-forward "<<\\(.+?\\)>>" nil t))
  882. (save-match-data (setf source-name (match-string 1)))
  883. (save-match-data (setq evaluate (string-match "\(.*\)" source-name)))
  884. (save-match-data
  885. (setq prefix (buffer-substring (match-beginning 0)
  886. (save-excursion
  887. (move-beginning-of-line 1) (point)))))
  888. ;; add interval to new-body (removing noweb reference)
  889. (goto-char (match-beginning 0))
  890. (nb-add (buffer-substring index (point)))
  891. (goto-char (match-end 0))
  892. (setq index (point))
  893. (nb-add (save-excursion
  894. (set-buffer parent-buffer)
  895. (mapconcat ;; interpose `prefix' between every line
  896. #'identity
  897. (split-string
  898. (if evaluate
  899. (let ((raw (org-babel-ref-resolve-reference
  900. source-name nil)))
  901. (if (stringp raw) raw (format "%S" raw)))
  902. (let ((point (org-babel-find-named-block source-name)))
  903. (if point
  904. (save-excursion
  905. (goto-char point)
  906. (org-babel-trim (org-babel-expand-noweb-references
  907. (org-babel-get-src-block-info))))
  908. ;; optionally raise an error if named
  909. ;; source-block doesn't exist
  910. (if (member lang org-babel-noweb-error-langs)
  911. (error
  912. "<<%s>> could not be resolved (see `org-babel-noweb-error-langs')"
  913. source-name)
  914. "")))) "[\n\r]") (concat "\n" prefix)))))
  915. (nb-add (buffer-substring index (point-max)))))
  916. new-body))
  917. (defun org-babel-clean-text-properties (text)
  918. "Strip all properties from text return."
  919. (set-text-properties 0 (length text) nil text) text)
  920. (defun org-babel-strip-protective-commas (body)
  921. "Strip protective commas from bodies of source blocks."
  922. (replace-regexp-in-string "^,#" "#" body))
  923. (defun org-babel-read (cell)
  924. "Convert the string value of CELL to a number if appropriate.
  925. Otherwise if cell looks like lisp (meaning it starts with a
  926. \"(\" or a \"'\") then read it as lisp, otherwise return it
  927. unmodified as a string.
  928. This is taken almost directly from `org-read-prop'."
  929. (if (and (stringp cell) (not (equal cell "")))
  930. (or (org-babel-number-p cell)
  931. (if (or (equal "(" (substring cell 0 1))
  932. (equal "'" (substring cell 0 1)))
  933. (read cell)
  934. (progn (set-text-properties 0 (length cell) nil cell) cell)))
  935. cell))
  936. (defun org-babel-number-p (string)
  937. "Return t if STRING represents a number"
  938. (if (and (string-match "^-?[[:digit:]]*\\.?[[:digit:]]*$" string)
  939. (= (match-end 0) (length string)))
  940. (string-to-number string)))
  941. (defun org-babel-import-elisp-from-file (file-name)
  942. "Read the results located at FILE-NAME into an elisp table. If
  943. the table is trivial, then return it as a scalar."
  944. (let (result)
  945. (with-temp-buffer
  946. (condition-case nil
  947. (progn
  948. (org-table-import file-name nil)
  949. (delete-file file-name)
  950. (setq result (mapcar (lambda (row)
  951. (mapcar #'org-babel-string-read row))
  952. (org-table-to-lisp))))
  953. (error nil)))
  954. (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
  955. (if (consp (car result))
  956. (if (null (cdr (car result)))
  957. (caar result)
  958. result)
  959. (car result))
  960. result)))
  961. (defun org-babel-string-read (cell)
  962. "Strip nested \"s from around strings in exported R values."
  963. (org-babel-read (or (and (stringp cell)
  964. (string-match "\\\"\\(.+\\)\\\"" cell)
  965. (match-string 1 cell))
  966. cell)))
  967. (defun org-babel-reverse-string (string)
  968. (apply 'string (reverse (string-to-list string))))
  969. (defun org-babel-chomp (string &optional regexp)
  970. "Remove any trailing space or carriage returns characters from
  971. STRING. Default regexp used is \"[ \f\t\n\r\v]\" but can be
  972. overwritten by specifying a regexp as a second argument."
  973. (let ((regexp (or regexp "[ \f\t\n\r\v]")))
  974. (while (and (> (length string) 0) (string-match regexp (substring string -1)))
  975. (setq string (substring string 0 -1)))
  976. string))
  977. (defun org-babel-trim (string &optional regexp)
  978. "Like `org-babel-chomp' only it runs on both the front and back of the string"
  979. (org-babel-chomp (org-babel-reverse-string
  980. (org-babel-chomp (org-babel-reverse-string string) regexp)) regexp))
  981. (provide 'org-babel)
  982. ;;; org-babel.el ends here