org-babel.el 47 KB

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