org-babel.el 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. (defun org-babel-pop-to-session-maybe ()
  34. "Detect if this is context for a org-babel src-block and if so
  35. then run `org-babel-pop-to-session'."
  36. (interactive)
  37. (let ((info (org-babel-get-src-block-info)))
  38. (if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil)))
  39. (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
  40. (defvar org-babel-default-header-args '((:session . "none"))
  41. "Default arguments to use when evaluating a source block.")
  42. (defvar org-babel-default-inline-header-args '((:results . "silent") (:exports . "results"))
  43. "Default arguments to use when evaluating an inline source block.")
  44. (defvar org-babel-src-block-regexp nil
  45. "Regexp used to test when inside of a org-babel src-block")
  46. (defvar org-babel-inline-src-block-regexp nil
  47. "Regexp used to test when on an inline org-babel src-block")
  48. (defun org-babel-named-src-block-regexp-for-name (name)
  49. "Regexp used to match named src block."
  50. (concat "#\\+srcname:[ \t]*" (regexp-quote name) "[ \t\n]*"
  51. org-babel-src-block-regexp))
  52. (defun org-babel-set-interpreters (var value)
  53. (set-default var value)
  54. (setq org-babel-src-block-regexp
  55. (concat "#\\+begin_src \\("
  56. (mapconcat 'regexp-quote value "\\|")
  57. "\\)[ \t]*"
  58. "\\([ \t]+\\([^\n]+\\)\\)?\n" ;; match header arguments
  59. "\\([^\000]+?\\)#\\+end_src"))
  60. (setq org-babel-inline-src-block-regexp
  61. (concat "src_\\("
  62. (mapconcat 'regexp-quote value "\\|")
  63. "\\)"
  64. "\\(\\|\\[\\(.*\\)\\]\\)"
  65. "{\\([^\n]+\\)}")))
  66. (defun org-babel-add-interpreter (interpreter)
  67. "Add INTERPRETER to `org-babel-interpreters' and update
  68. `org-babel-src-block-regexp' appropriately."
  69. (unless (member interpreter org-babel-interpreters)
  70. (setq org-babel-interpreters (cons interpreter org-babel-interpreters))
  71. ;; (add-to-list 'org-babel-session-defaults (cons interpreter (format "org-babel-%s" interpreter)))
  72. (org-babel-set-interpreters 'org-babel-interpreters org-babel-interpreters)))
  73. (defcustom org-babel-interpreters '()
  74. "Interpreters allows for evaluation tags.
  75. This is a list of program names (as strings) that can evaluate code and
  76. insert the output into an Org-mode buffer. Valid choices are
  77. R Evaluate R code
  78. emacs-lisp Evaluate Emacs Lisp code and display the result
  79. sh Pass command to the shell and display the result
  80. perl The perl interpreter
  81. python The python interpreter
  82. ruby The ruby interpreter
  83. The source block regexp `org-babel-src-block-regexp' is updated
  84. when a new interpreter is added to this list through the
  85. customize interface. To add interpreters to this variable from
  86. lisp code use the `org-babel-add-interpreter' function."
  87. :group 'org-babel
  88. :set 'org-babel-set-interpreters
  89. :type '(set :greedy t
  90. (const "R")
  91. (const "emacs-lisp")
  92. (const "sh")
  93. (const "perl")
  94. (const "python")
  95. (const "ruby")))
  96. ;;; functions
  97. (defun org-babel-pop-to-session (&optional arg info)
  98. "Pop to the session of the current source-code block. If
  99. called with a prefix argument then evaluate the header arguments
  100. for the source block before entering the session. Copy the body
  101. of the source block to the kill ring."
  102. (interactive)
  103. (let* ((info (or info (org-babel-get-src-block-info)))
  104. (lang (first info))
  105. (body (second info))
  106. (params (third info))
  107. (session (cdr (assoc :session params))))
  108. (unless (member lang org-babel-interpreters)
  109. (error "Language is not in `org-babel-interpreters': %s" lang))
  110. ;; copy body to the kill ring
  111. (with-temp-buffer (insert (org-babel-trim body)) (copy-region-as-kill (point-min) (point-max)))
  112. ;; if called with a prefix argument, then process header arguments
  113. (if arg (funcall (intern (concat "org-babel-prep-session:" lang)) session params))
  114. ;; just to the session using pop-to-buffer
  115. (pop-to-buffer (funcall (intern (format "org-babel-%s-initiate-session" lang)) session))
  116. (move-end-of-line 1)))
  117. (defun org-babel-execute-src-block (&optional arg info params)
  118. "Execute the current source code block, and dump the results
  119. into the buffer immediately following the block. Results are
  120. commented by `org-toggle-fixed-width-section'. With optional
  121. prefix don't dump results into buffer but rather return the
  122. results in raw elisp (this is useful for automated execution of a
  123. source block).
  124. Optionally supply a value for INFO in the form returned by
  125. `org-babel-get-src-block-info'.
  126. Optionally supply a value for PARAMS which will be merged with
  127. the header arguments specified at the source code block."
  128. (interactive)
  129. (let* ((info (or info (org-babel-get-src-block-info)))
  130. (lang (first info))
  131. (body (second info))
  132. (params (org-combine-plists params (third info)))
  133. (result-params (split-string (or (cdr (assoc :results params)) "")))
  134. (result-type (cond ((member "output" result-params) 'output)
  135. ((member "value" result-params) 'value)
  136. (t 'value)))
  137. (cmd (intern (concat "org-babel-execute:" lang)))
  138. result)
  139. ;; (message (format "params=%S" params)) ;; debugging
  140. (unless (member lang org-babel-interpreters)
  141. (error "Language is not in `org-babel-interpreters': %s" lang))
  142. (when arg (setq result-params (cons "silent" result-params)))
  143. (setq result (org-babel-process-result (funcall cmd body params) result-type))
  144. (org-babel-insert-result result result-params)
  145. (case result-type (output nil) (value result))))
  146. (defun org-babel-process-result (result result-type)
  147. "This doesn't do anything currently.
  148. You can see below the various fragments of results-processing
  149. code that were present in the language-specific files. Out of
  150. those fragments, I've moved the
  151. org-babel-python-table-or-results and
  152. org-babel-import-elisp-from-file functionality into the
  153. org-babel-*-evaluate functions. I think those should only be
  154. used in the :results value case, as in the 'output case we are
  155. not concerned with creating elisp versions of results.
  156. The rest of the functionality below, concerned with vectorising
  157. or scalarising results is commented out, has not yet been
  158. replaced, and may need to be reinstated in this function. "
  159. result)
  160. ;; ;; ruby
  161. ;; (if (member "scalar" result-params)
  162. ;; results
  163. ;; (case result-type ;; process results based on the result-type
  164. ;; ('output (let ((tmp-file (make-temp-file "org-babel-ruby")))
  165. ;; (with-temp-file tmp-file (insert results))
  166. ;; (org-babel-import-elisp-from-file tmp-file)))
  167. ;; ('value (org-babel-ruby-table-or-results results))))))
  168. ;; python
  169. ;; (if (member "scalar" result-params)
  170. ;; results
  171. ;; (setq result (case result-type ;; process results based on the result-type
  172. ;; ('output (let ((tmp-file (make-temp-file "org-babel-python")))
  173. ;; (with-temp-file tmp-file (insert results))
  174. ;; (org-babel-import-elisp-from-file tmp-file)))
  175. ;; ('value (org-babel-python-table-or-results results))))
  176. ;; (if (and (member "vector" results) (not (listp results)))
  177. ;; (list (list results))
  178. ;; results))))
  179. ;; ;; sh
  180. ;; (if (member "scalar" result-params)
  181. ;; results
  182. ;; (setq results (let ((tmp-file (make-temp-file "org-babel-shell")))
  183. ;; (with-temp-file tmp-file (insert results))
  184. ;; (org-babel-import-elisp-from-file tmp-file)))
  185. ;; (if (and (member "vector" results) (not (listp results)))
  186. ;; (list (list results))
  187. ;; results))))
  188. ;; ;; R
  189. ;; (setq results (if (member "scalar" result-params)
  190. ;; results
  191. ;; (let ((tmp-file (make-temp-file "org-babel-R")))
  192. ;; (with-temp-file tmp-file (insert results))
  193. ;; (org-babel-import-elisp-from-file tmp-file))))
  194. ;; (if (and (member "vector" result-params) (not (listp results)))
  195. ;; (list (list results))
  196. ;; results))))
  197. ;; ;; rest of org-babel-execute-src-block
  198. ;; ;; possibly force result into a vector
  199. ;; (if (and (not (listp result)) (cdr (assoc :results params))
  200. ;; (member "vector" (split-string (cdr (assoc :results params)))))
  201. ;; (setq result (list result)))
  202. ;; result))
  203. (defun org-babel-eval-buffer (&optional arg)
  204. "Replace EVAL snippets in the entire buffer."
  205. (interactive "P")
  206. (save-excursion
  207. (goto-char (point-min))
  208. (while (re-search-forward org-babel-regexp nil t)
  209. (org-babel-eval-src-block arg))))
  210. (defun org-babel-eval-subtree (&optional arg)
  211. "Replace EVAL snippets in the entire subtree."
  212. (interactive "P")
  213. (save-excursion
  214. (org-narrow-to-subtree)
  215. (org-babel-eval-buffer)
  216. (widen)))
  217. (defun org-babel-get-src-block-name ()
  218. "Return the name of the current source block if one exists"
  219. (let ((case-fold-search t))
  220. (save-excursion
  221. (goto-char (org-babel-where-is-src-block-head))
  222. (if (save-excursion (forward-line -1)
  223. (looking-at "#\\+srcname:[ \f\t\n\r\v]*\\([^ \f\t\n\r\v]+\\)"))
  224. (org-babel-clean-text-properties (match-string 1))))))
  225. (defun org-babel-get-src-block-info ()
  226. "Return the information of the current source block as a list
  227. of the following form. (language body header-arguments-alist)"
  228. (let ((case-fold-search t) head)
  229. (if (setq head (org-babel-where-is-src-block-head))
  230. (save-excursion (goto-char head) (org-babel-parse-src-block-match))
  231. (if (save-excursion ;; inline source block
  232. (re-search-backward "[ \f\t\n\r\v]" nil t)
  233. (forward-char 1)
  234. (looking-at org-babel-inline-src-block-regexp))
  235. (org-babel-parse-inline-src-block-match)
  236. nil)))) ;; indicate that no source block was found
  237. (defmacro org-babel-map-source-blocks (file &rest body)
  238. "Evaluate BODY forms on each source-block in FILE."
  239. (declare (indent 1))
  240. `(save-window-excursion
  241. (find-file ,file) (goto-char (point-min))
  242. (while (re-search-forward org-babel-src-block-regexp nil t)
  243. (goto-char (match-beginning 0))
  244. (save-match-data ,@body)
  245. (goto-char (match-end 0)))))
  246. (defun org-babel-parse-src-block-match ()
  247. (list (org-babel-clean-text-properties (match-string 1))
  248. (org-babel-strip-protective-commas (org-babel-clean-text-properties (match-string 4)))
  249. (org-combine-plists org-babel-default-header-args
  250. (org-babel-parse-header-arguments
  251. (org-babel-clean-text-properties (or (match-string 3) ""))))))
  252. (defun org-babel-parse-inline-src-block-match ()
  253. (list (org-babel-clean-text-properties (match-string 1))
  254. (org-babel-strip-protective-commas (org-babel-clean-text-properties (match-string 4)))
  255. (org-combine-plists org-babel-default-inline-header-args
  256. (org-babel-parse-header-arguments
  257. (org-babel-clean-text-properties (or (match-string 3) ""))))))
  258. (defun org-babel-parse-header-arguments (arg-string)
  259. "Parse a string of header arguments returning an alist."
  260. (delq nil
  261. (mapcar
  262. (lambda (arg)
  263. (if (string-match "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)" arg)
  264. (cons (intern (concat ":" (match-string 1 arg)))
  265. (org-babel-chomp (match-string 2 arg)))
  266. (cons (intern (concat ":" arg)) nil)))
  267. (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t))))
  268. (defun org-babel-where-is-src-block-head ()
  269. "Return the point at the beginning of the current source
  270. block. Specifically at the beginning of the #+BEGIN_SRC line.
  271. If the point is not on a source block then return nil."
  272. (let ((initial (point)) top bottom)
  273. (or
  274. (save-excursion ;; on a #+srcname: line
  275. (beginning-of-line 1)
  276. (and (looking-at "#\\+srcname") (forward-line 1)
  277. (looking-at org-babel-src-block-regexp)
  278. (point)))
  279. (save-excursion ;; on a #+begin_src line
  280. (beginning-of-line 1)
  281. (and (looking-at org-babel-src-block-regexp)
  282. (point)))
  283. (save-excursion ;; inside a src block
  284. (and
  285. (re-search-backward "#\\+begin_src" nil t) (setq top (point))
  286. (re-search-forward "#\\+end_src" nil t) (setq bottom (point))
  287. (< top initial) (< initial bottom)
  288. (goto-char top) (looking-at org-babel-src-block-regexp)
  289. (point))))))
  290. (defun org-babel-goto-named-source-block (&optional name)
  291. "Go to a named source-code block."
  292. (interactive "ssource-block name: ")
  293. (let ((point (org-babel-find-named-block name)))
  294. (if point
  295. ;; taken from `org-open-at-point'
  296. (progn (goto-char point) (org-show-context))
  297. (message "source-code block '%s' not found in this buffer" name))))
  298. (defun org-babel-find-named-block (name)
  299. "Find a named source-code block.
  300. Return the location of the source block identified by
  301. #+srcname NAME, or nil if no such block exists. Set match data
  302. according to org-babel-named-src-block-regexp."
  303. (save-excursion
  304. (let ((case-fold-search t)
  305. (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
  306. (goto-char (point-min))
  307. (when (or (re-search-forward regexp nil t)
  308. (re-search-backward regexp nil t))
  309. (match-beginning 0)))))
  310. (defun org-babel-find-named-result (name)
  311. "Return the location of the result named NAME in the current
  312. buffer or nil if no such result exists."
  313. (save-excursion
  314. (goto-char (point-min))
  315. (when (re-search-forward (concat "#\\+resname:[ \t]*" (regexp-quote name)) nil t)
  316. (move-beginning-of-line 1) (point))))
  317. (defun org-babel-where-is-src-block-result ()
  318. "Return the point at the beginning of the result of the current
  319. source block. Specifically at the beginning of the #+RESNAME:
  320. line. If no result exists for this block then create a
  321. #+RESNAME: line following the source block."
  322. (save-excursion
  323. (goto-char (org-babel-where-is-src-block-head))
  324. (let ((name (org-babel-get-src-block-name)) end head)
  325. (or (and name (message name) (org-babel-find-named-result name))
  326. (and (re-search-forward "#\\+end_src" nil t)
  327. (progn (move-end-of-line 1)
  328. (if (eobp) (insert "\n") (forward-char 1))
  329. (setq end (point))
  330. (or (progn ;; either an unnamed #+resname: line already exists
  331. (re-search-forward "[^ \f\t\n\r\v]" nil t)
  332. (move-beginning-of-line 1) (looking-at "#\\+resname:"))
  333. (progn ;; or we need to back up and make one ourselves
  334. (goto-char end) (open-line 2) (forward-char 1)
  335. (insert (concat "#+resname:" (if name (concat " " name))))
  336. (move-beginning-of-line 1) t)))
  337. (point))))))
  338. (defun org-babel-insert-result (result &optional insert)
  339. "Insert RESULT into the current buffer after the end of the
  340. current source block. With optional argument INSERT controls
  341. insertion of results in the org-mode file. INSERT can take the
  342. following values...
  343. t ------ the default options, simply insert the results after the
  344. source block
  345. replace - insert results after the source block replacing any
  346. previously inserted results
  347. silent -- no results are inserted"
  348. (if (stringp result)
  349. (progn
  350. (setq result (org-babel-clean-text-properties result))
  351. (if (member "file" insert) (setq result (org-babel-result-to-file result))))
  352. (unless (listp result) (setq result (format "%S" result))))
  353. (if (and insert (member "replace" insert)) (org-babel-remove-result))
  354. (if (= (length result) 0)
  355. (message "no result returned by source block")
  356. (if (and insert (member "silent" insert))
  357. (progn (message (replace-regexp-in-string "%" "%%" (format "%S" result))) result)
  358. (when (and (stringp result) ;; ensure results end in a newline
  359. (not (or (string-equal (substring result -1) "\n")
  360. (string-equal (substring result -1) "\r"))))
  361. (setq result (concat result "\n")))
  362. (save-excursion
  363. (goto-char (org-babel-where-is-src-block-result)) (forward-line 1)
  364. (if (stringp result) ;; assume the result is a table if it's not a string
  365. (if (member "file" insert)
  366. (insert result)
  367. (org-babel-examplize-region (point) (progn (insert result) (point))))
  368. (progn
  369. (insert
  370. (concat (orgtbl-to-orgtbl
  371. (if (consp (car result)) result (list result))
  372. '(:fmt (lambda (cell) (format "%S" cell)))) "\n"))
  373. (forward-line -1)
  374. (org-cycle))))
  375. (message "finished"))))
  376. (defun org-babel-result-to-org-string (result)
  377. "Return RESULT as a string in org-mode format. This function
  378. relies on `org-babel-insert-result'."
  379. (with-temp-buffer (org-babel-insert-result result) (buffer-string)))
  380. (defun org-babel-remove-result ()
  381. "Remove the result of the current source block."
  382. (interactive)
  383. (save-excursion
  384. (goto-char (org-babel-where-is-src-block-result)) (forward-line 1)
  385. (delete-region (point) (org-babel-result-end))))
  386. (defun org-babel-result-end ()
  387. "Return the point at the end of the current set of results"
  388. (save-excursion
  389. (if (org-at-table-p)
  390. (org-table-end)
  391. (while (if (looking-at "\\(: \\|\\[\\[\\)")
  392. (progn (while (looking-at "\\(: \\|\\[\\[\\)")
  393. (forward-line 1)) t))
  394. (forward-line 1))
  395. (forward-line -1)
  396. (point))))
  397. (defun org-babel-result-to-file (result)
  398. "Return an `org-mode' link with the path being the value or
  399. RESULT, and the display being the `file-name-nondirectory' if
  400. non-nil."
  401. (let ((name (file-name-nondirectory result)))
  402. (concat "[[" result (if name (concat "][" name "]]") "]]"))))
  403. (defun org-babel-examplize-region (beg end)
  404. "Comment out region using the ': ' org example quote."
  405. (interactive "*r")
  406. (let ((size (abs (- (line-number-at-pos end)
  407. (line-number-at-pos beg)))))
  408. (if (= size 0)
  409. (let ((result (buffer-substring beg end)))
  410. (delete-region beg end)
  411. (insert (concat ": " result)))
  412. (save-excursion
  413. (goto-char beg)
  414. (dotimes (n size)
  415. (move-beginning-of-line 1) (insert ": ") (forward-line 1))))))
  416. (defun org-babel-clean-text-properties (text)
  417. "Strip all properties from text return."
  418. (set-text-properties 0 (length text) nil text) text)
  419. (defun org-babel-strip-protective-commas (body)
  420. "Strip protective commas from bodies of source blocks."
  421. (replace-regexp-in-string "^,#" "#" body))
  422. (defun org-babel-read (cell)
  423. "Convert the string value of CELL to a number if appropriate.
  424. Otherwise if cell looks like a list (meaning it starts with a
  425. '(') then read it as lisp, otherwise return it unmodified as a
  426. string.
  427. This is taken almost directly from `org-read-prop'."
  428. (if (and (stringp cell) (not (equal cell "")))
  429. (or (org-babel-number-p cell)
  430. (if (or (equal "(" (substring cell 0 1))
  431. (equal "'" (substring cell 0 2)))
  432. (read cell)
  433. (progn (set-text-properties 0 (length cell) nil cell) cell)))
  434. cell))
  435. (defun org-babel-number-p (string)
  436. "Return t if STRING represents a number"
  437. (if (string-match "^[[:digit:]]*\\.?[[:digit:]]*$" string)
  438. (string-to-number string)))
  439. (defun org-babel-import-elisp-from-file (file-name)
  440. "Read the results located at FILE-NAME into an elisp table. If
  441. the table is trivial, then return it as a scalar."
  442. (let (result)
  443. (with-temp-buffer
  444. (condition-case nil
  445. (progn
  446. (org-table-import file-name nil)
  447. (delete-file file-name)
  448. (setq result (mapcar (lambda (row)
  449. (mapcar #'org-babel-string-read row))
  450. (org-table-to-lisp))))
  451. (error nil))
  452. (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
  453. (if (consp (car result))
  454. (if (null (cdr (car result)))
  455. (caar result)
  456. result)
  457. (car result))
  458. result))))
  459. (defun org-babel-string-read (cell)
  460. "Strip nested \"s from around strings in exported R values."
  461. (org-babel-read (or (and (stringp cell)
  462. (string-match "\\\"\\(.+\\)\\\"" cell)
  463. (match-string 1 cell))
  464. cell)))
  465. (defun org-babel-reverse-string (string)
  466. (apply 'string (reverse (string-to-list string))))
  467. (defun org-babel-chomp (string &optional regexp)
  468. "Remove any trailing space or carriage returns characters from
  469. STRING. Default regexp used is \"[ \f\t\n\r\v]\" but can be
  470. overwritten by specifying a regexp as a second argument."
  471. (while (and (> (length string) 0) (string-match "[ \f\t\n\r\v]" (substring string -1)))
  472. (setq string (substring string 0 -1)))
  473. string)
  474. (defun org-babel-trim (string &optional regexp)
  475. "Like `org-babel-chomp' only it runs on both the front and back of the string"
  476. (org-babel-chomp (org-babel-reverse-string
  477. (org-babel-chomp (org-babel-reverse-string string) regexp)) regexp))
  478. (provide 'org-babel)
  479. ;;; org-babel.el ends here