Correcting up cut/paste mistakes made in manual conflict resolution.
[rgr-org-mode.git] / lisp / org-babel.el
blob60c60b3761e521a295bce4e8cbd811bfeed9aaff
1 ;;; org-babel.el --- facilitating communication between programming languages and people
3 ;; Copyright (C) 2009 Eric Schulte, Dan Davison
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;;; License:
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; See org-babel.org in the parent directory for more information
31 ;;; Code:
32 (require 'org)
34 (defun org-babel-execute-src-block-maybe ()
35 "Detect if this is context for a org-babel src-block and if so
36 then run `org-babel-execute-src-block'."
37 (interactive)
38 (let ((info (org-babel-get-src-block-info)))
39 (if info (progn (org-babel-execute-src-block current-prefix-arg info) t) nil)))
41 (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-src-block-maybe)
43 (defun org-babel-pop-to-session-maybe ()
44 "Detect if this is context for a org-babel src-block and if so
45 then run `org-babel-pop-to-session'."
46 (interactive)
47 (let ((info (org-babel-get-src-block-info)))
48 (if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil)))
50 (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
52 (defvar org-babel-default-header-args '((:session . "none") (:results . "replace"))
53 "Default arguments to use when evaluating a source block.")
55 (defvar org-babel-default-inline-header-args '((:results . "silent") (:exports . "results"))
56 "Default arguments to use when evaluating an inline source block.")
58 (defvar org-babel-src-block-regexp nil
59 "Regexp used to test when inside of a org-babel src-block")
61 (defvar org-babel-inline-src-block-regexp nil
62 "Regexp used to test when on an inline org-babel src-block")
64 (defun org-babel-named-src-block-regexp-for-name (name)
65 "Regexp used to match named src block."
66 (concat "#\\+srcname:[ \t]*" (regexp-quote name) "[ \t\n]*"
67 org-babel-src-block-regexp))
69 (defun org-babel-set-interpreters (var value)
70 (set-default var value)
71 (setq org-babel-src-block-regexp
72 (concat "#\\+begin_src \\("
73 (mapconcat 'regexp-quote value "\\|")
74 "\\)[ \t]*"
75 "\\([ \t]+\\([^\n]+\\)\\)?\n" ;; match header arguments
76 "\\([^\000]+?\\)#\\+end_src"))
77 (setq org-babel-inline-src-block-regexp
78 (concat "src_\\("
79 (mapconcat 'regexp-quote value "\\|")
80 "\\)"
81 "\\(\\|\\[\\(.*\\)\\]\\)"
82 "{\\([^\n]+\\)}")))
84 (defun org-babel-add-interpreter (interpreter)
85 "Add INTERPRETER to `org-babel-interpreters' and update
86 `org-babel-src-block-regexp' appropriately."
87 (unless (member interpreter org-babel-interpreters)
88 (setq org-babel-interpreters (cons interpreter org-babel-interpreters))
89 ;; (add-to-list 'org-babel-session-defaults (cons interpreter (format "org-babel-%s" interpreter)))
90 (org-babel-set-interpreters 'org-babel-interpreters org-babel-interpreters)))
92 (defcustom org-babel-interpreters '()
93 "Interpreters allows for evaluation tags.
94 This is a list of program names (as strings) that can evaluate code and
95 insert the output into an Org-mode buffer. Valid choices are
97 R Evaluate R code
98 emacs-lisp Evaluate Emacs Lisp code and display the result
99 sh Pass command to the shell and display the result
100 perl The perl interpreter
101 python The python interpreter
102 ruby The ruby interpreter
104 The source block regexp `org-babel-src-block-regexp' is updated
105 when a new interpreter is added to this list through the
106 customize interface. To add interpreters to this variable from
107 lisp code use the `org-babel-add-interpreter' function."
108 :group 'org-babel
109 :set 'org-babel-set-interpreters
110 :type '(set :greedy t
111 (const "R")
112 (const "emacs-lisp")
113 (const "sh")
114 (const "perl")
115 (const "python")
116 (const "ruby")))
118 ;;; functions
119 (defun org-babel-pop-to-session (&optional arg info)
120 "Pop to the session of the current source-code block. If
121 called with a prefix argument then evaluate the header arguments
122 for the source block before entering the session. Copy the body
123 of the source block to the kill ring."
124 (interactive)
125 (let* ((info (or info (org-babel-get-src-block-info)))
126 (lang (first info))
127 (body (second info))
128 (params (third info))
129 (session (cdr (assoc :session params))))
130 (unless (member lang org-babel-interpreters)
131 (error "Language is not in `org-babel-interpreters': %s" lang))
132 ;; copy body to the kill ring
133 (with-temp-buffer (insert (org-babel-trim body)) (copy-region-as-kill (point-min) (point-max)))
134 ;; if called with a prefix argument, then process header arguments
135 (if arg (funcall (intern (concat "org-babel-prep-session:" lang)) session params))
136 ;; just to the session using pop-to-buffer
137 (pop-to-buffer (funcall (intern (format "org-babel-%s-initiate-session" lang)) session))
138 (move-end-of-line 1)))
140 (defun org-babel-execute-src-block (&optional arg info params)
141 "Execute the current source code block, and dump the results
142 into the buffer immediately following the block. Results are
143 commented by `org-toggle-fixed-width-section'. With optional
144 prefix don't dump results into buffer but rather return the
145 results in raw elisp (this is useful for automated execution of a
146 source block).
148 Optionally supply a value for INFO in the form returned by
149 `org-babel-get-src-block-info'.
151 Optionally supply a value for PARAMS which will be merged with
152 the header arguments specified at the source code block."
153 (interactive)
154 (let* ((info (or info (org-babel-get-src-block-info)))
155 (lang (first info))
156 (body (second info))
157 (params (org-babel-merge-params
158 (third info) (org-babel-get-src-block-function-args) params))
159 (processed-params (org-babel-process-params params))
160 (result-params (third processed-params))
161 (result-type (fourth processed-params))
162 (cmd (intern (concat "org-babel-execute:" lang)))
163 result)
164 ;; (message (format "params=%S" params)) ;; debugging
165 (unless (member lang org-babel-interpreters)
166 (error "Language is not in `org-babel-interpreters': %s" lang))
167 (when arg (setq result-params (cons "silent" result-params)))
168 (setq result
169 (org-babel-process-result
170 (multiple-value-bind (session vars result-params result-type) processed-params
171 (funcall cmd body params)) result-type))
172 (org-babel-insert-result result result-params)
173 (case result-type (output nil) (value result))))
175 (defun org-babel-process-result (result result-type)
176 "This doesn't do anything currently.
178 You can see below the various fragments of results-processing
179 code that were present in the language-specific files. Out of
180 those fragments, I've moved the
181 org-babel-python-table-or-results and
182 org-babel-import-elisp-from-file functionality into the
183 org-babel-*-evaluate functions. I think those should only be
184 used in the :results value case, as in the 'output case we are
185 not concerned with creating elisp versions of results.
187 The rest of the functionality below, concerned with vectorising
188 or scalarising results is commented out, has not yet been
189 replaced, and may need to be reinstated in this function. "
191 result)
192 ;; ;; ruby
193 ;; (if (member "scalar" result-params)
194 ;; results
195 ;; (case result-type ;; process results based on the result-type
196 ;; ('output (let ((tmp-file (make-temp-file "org-babel-ruby")))
197 ;; (with-temp-file tmp-file (insert results))
198 ;; (org-babel-import-elisp-from-file tmp-file)))
199 ;; ('value (org-babel-ruby-table-or-results results))))))
201 ;; python
202 ;; (if (member "scalar" result-params)
203 ;; results
204 ;; (setq result (case result-type ;; process results based on the result-type
205 ;; ('output (let ((tmp-file (make-temp-file "org-babel-python")))
206 ;; (with-temp-file tmp-file (insert results))
207 ;; (org-babel-import-elisp-from-file tmp-file)))
208 ;; ('value (org-babel-python-table-or-results results))))
209 ;; (if (and (member "vector" results) (not (listp results)))
210 ;; (list (list results))
211 ;; results))))
214 ;; ;; sh
215 ;; (if (member "scalar" result-params)
216 ;; results
217 ;; (setq results (let ((tmp-file (make-temp-file "org-babel-shell")))
218 ;; (with-temp-file tmp-file (insert results))
219 ;; (org-babel-import-elisp-from-file tmp-file)))
220 ;; (if (and (member "vector" results) (not (listp results)))
221 ;; (list (list results))
222 ;; results))))
224 ;; ;; R
225 ;; (setq results (if (member "scalar" result-params)
226 ;; results
227 ;; (let ((tmp-file (make-temp-file "org-babel-R")))
228 ;; (with-temp-file tmp-file (insert results))
229 ;; (org-babel-import-elisp-from-file tmp-file))))
230 ;; (if (and (member "vector" result-params) (not (listp results)))
231 ;; (list (list results))
232 ;; results))))
235 ;; ;; rest of org-babel-execute-src-block
237 ;; ;; possibly force result into a vector
238 ;; (if (and (not (listp result)) (cdr (assoc :results params))
239 ;; (member "vector" (split-string (cdr (assoc :results params)))))
240 ;; (setq result (list result)))
241 ;; result))
243 (defun org-babel-execute-buffer (&optional arg)
244 "Replace EVAL snippets in the entire buffer."
245 (interactive "P")
246 (save-excursion
247 (goto-char (point-min))
248 (while (re-search-forward org-babel-src-block-regexp nil t)
249 (goto-char (match-beginning 0))
250 (org-babel-execute-src-block arg)
251 (goto-char (match-end 0)))))
253 (defun org-babel-execute-subtree (&optional arg)
254 "Replace EVAL snippets in the entire subtree."
255 (interactive "P")
256 (save-excursion
257 (org-narrow-to-subtree)
258 (org-babel-execute-buffer)
259 (widen)))
261 (defun org-babel-get-src-block-name ()
262 "Return the name of the current source block if one exists.
264 This function is analogous to org-babel-lob-get-info. For both
265 functions, after they are called, (match-string 1) matches the
266 function name, and (match-string 2) matches the function
267 arguments inside the parentheses. I think perhaps these functions
268 should be renamed to bring out this similarity, perhaps involving
269 the word 'call'."
270 (let ((case-fold-search t)
271 (head (org-babel-where-is-src-block-head)))
272 (if head
273 (save-excursion
274 (goto-char head)
275 (if (save-excursion
276 (forward-line -1)
277 (looking-at "#\\+srcname:[ \f\t\n\r\v]*\\([^ \f\t\n\r\v]+\\)\(\\(.*\\)\)"))
278 (org-babel-clean-text-properties (match-string 1)))))))
280 (defun org-babel-get-src-block-info ()
281 "Return the information of the current source block as a list
282 of the following form. (language body header-arguments-alist)"
283 (let ((case-fold-search t) head)
284 (if (setq head (org-babel-where-is-src-block-head))
285 (save-excursion (goto-char head) (org-babel-parse-src-block-match))
286 (if (save-excursion ;; inline source block
287 (re-search-backward "[ \f\t\n\r\v]" nil t)
288 (forward-char 1)
289 (looking-at org-babel-inline-src-block-regexp))
290 (org-babel-parse-inline-src-block-match)
291 nil)))) ;; indicate that no source block was found
293 (defun org-babel-get-src-block-function-args ()
294 (when (org-babel-get-src-block-name)
295 (mapcar (lambda (ref) (cons :var ref))
296 (split-string (org-babel-clean-text-properties (match-string 2))
297 ",[ \f\t\n\r\v]*"))))
299 (defmacro org-babel-map-source-blocks (file &rest body)
300 "Evaluate BODY forms on each source-block in FILE."
301 (declare (indent 1))
302 `(save-window-excursion
303 (find-file ,file) (goto-char (point-min))
304 (while (re-search-forward org-babel-src-block-regexp nil t)
305 (goto-char (match-beginning 0))
306 (save-match-data ,@body)
307 (goto-char (match-end 0)))))
309 (defun org-babel-parse-src-block-match ()
310 (let* ((lang (org-babel-clean-text-properties (match-string 1)))
311 (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
312 (list lang
313 (org-babel-strip-protective-comas (org-babel-clean-text-properties (match-string 4)))
314 (org-babel-merge-params
315 org-babel-default-header-args
316 (if (boundp lang-headers) (eval lang-headers) nil)
317 (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 3) "")))))))
319 (defun org-babel-parse-inline-src-block-match ()
320 (let* ((lang (org-babel-clean-text-properties (match-string 1)))
321 (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
322 (list lang
323 (org-babel-strip-protective-comas (org-babel-clean-text-properties (match-string 4)))
324 (org-combine-plists
325 org-babel-default-inline-header-args
326 (if (boundp lang-headers) (eval lang-headers) nil)
327 (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 3) "")))))))
329 (defun org-babel-parse-header-arguments (arg-string)
330 "Parse a string of header arguments returning an alist."
331 (if (> (length arg-string) 0)
332 (delq nil
333 (mapcar
334 (lambda (arg)
335 (if (string-match "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)" arg)
336 (cons (intern (concat ":" (match-string 1 arg)))
337 (org-babel-chomp (match-string 2 arg)))
338 (cons (intern (concat ":" arg)) nil)))
339 (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t)))))
341 (defun org-babel-process-params (params)
342 "Parse params and resolve references.
344 Return a list (session vars result-params result-type). These are
345 made available to the org-babel-execute:LANG functions via
346 multiple-value-bind."
347 (let* ((session (cdr (assoc :session params)))
348 (vars (org-babel-ref-variables params))
349 (result-params (split-string (or (cdr (assoc :results params)) "")))
350 (result-type (cond ((member "output" result-params) 'output)
351 ((member "value" result-params) 'value)
352 (t 'value))))
353 (list session vars result-params result-type)))
355 (defun org-babel-where-is-src-block-head ()
356 "Return the point at the beginning of the current source
357 block. Specifically at the beginning of the #+BEGIN_SRC line.
358 If the point is not on a source block then return nil."
359 (let ((initial (point)) top bottom)
361 (save-excursion ;; on a #+srcname: line
362 (beginning-of-line 1)
363 (and (looking-at "#\\+srcname") (forward-line 1)
364 (looking-at org-babel-src-block-regexp)
365 (point)))
366 (save-excursion ;; on a #+begin_src line
367 (beginning-of-line 1)
368 (and (looking-at org-babel-src-block-regexp)
369 (point)))
370 (save-excursion ;; inside a src block
371 (and
372 (re-search-backward "#\\+begin_src" nil t) (setq top (point))
373 (re-search-forward "#\\+end_src" nil t) (setq bottom (point))
374 (< top initial) (< initial bottom)
375 (goto-char top) (looking-at org-babel-src-block-regexp)
376 (point))))))
378 (defun org-babel-goto-named-source-block (&optional name)
379 "Go to a named source-code block."
380 (interactive "ssource-block name: ")
381 (let ((point (org-babel-find-named-block name)))
382 (if point
383 ;; taken from `org-open-at-point'
384 (progn (goto-char point) (org-show-context))
385 (message "source-code block '%s' not found in this buffer" name))))
387 (defun org-babel-find-named-block (name)
388 "Find a named source-code block.
389 Return the location of the source block identified by
390 #+srcname NAME, or nil if no such block exists. Set match data
391 according to org-babel-named-src-block-regexp."
392 (save-excursion
393 (let ((case-fold-search t)
394 (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
395 (goto-char (point-min))
396 (when (or (re-search-forward regexp nil t)
397 (re-search-backward regexp nil t))
398 (match-beginning 0)))))
400 (defun org-babel-find-named-result (name)
401 "Return the location of the result named NAME in the current
402 buffer or nil if no such result exists."
403 (save-excursion
404 (goto-char (point-min))
405 (when (re-search-forward (concat "#\\+resname:[ \t]*" (regexp-quote name)) nil t)
406 (move-beginning-of-line 1) (point))))
408 (defun org-babel-where-is-src-block-result ()
409 "Return the point at the beginning of the result of the current
410 source block. Specifically at the beginning of the #+RESNAME:
411 line. If no result exists for this block then create a
412 #+RESNAME: line following the source block."
413 (save-excursion
414 (let* ((on-lob-line (progn (beginning-of-line 1)
415 (looking-at org-babel-lob-one-liner-regexp)))
416 (name (if on-lob-line (org-babel-lob-get-info) (org-babel-get-src-block-name)))
417 end head)
418 (unless on-lob-line (goto-char (org-babel-where-is-src-block-head)))
419 (or (and name (message name) (org-babel-find-named-result name))
420 (and (or on-lob-line (re-search-forward "#\\+end_src" nil t))
421 (progn (move-end-of-line 1)
422 (if (eobp) (insert "\n") (forward-char 1))
423 (setq end (point))
424 (or (progn ;; either an unnamed #+resname: line already exists
425 (re-search-forward "[^ \f\t\n\r\v]" nil t)
426 (move-beginning-of-line 1) (looking-at "#\\+resname:"))
427 (progn ;; or we need to back up and make one ourselves
428 (goto-char end) (open-line 2) (forward-char 1)
429 (insert (concat "#+resname:" (if name (concat " " name))))
430 (move-beginning-of-line 1) t)))
431 (point))))))
433 (defun org-babel-insert-result (result &optional insert)
434 "Insert RESULT into the current buffer after the end of the
435 current source block. With optional argument INSERT controls
436 insertion of results in the org-mode file. INSERT can take the
437 following values...
439 t ------ the default options, simply insert the results after the
440 source block
442 replace - insert results after the source block replacing any
443 previously inserted results
445 silent -- no results are inserted"
446 (if (stringp result)
447 (progn
448 (setq result (org-babel-clean-text-properties result))
449 (if (member "file" insert) (setq result (org-babel-result-to-file result))))
450 (unless (listp result) (setq result (format "%S" result))))
451 (if (and insert (member "replace" insert)) (org-babel-remove-result))
452 (if (= (length result) 0)
453 (if (member "value" result-params)
454 (message "No result returned by source block")
455 (message "Source block produced no output"))
456 (if (and insert (member "silent" insert))
457 (progn (message (replace-regexp-in-string "%" "%%" (format "%S" result))) result)
458 (when (and (stringp result) ;; ensure results end in a newline
459 (not (or (string-equal (substring result -1) "\n")
460 (string-equal (substring result -1) "\r"))))
461 (setq result (concat result "\n")))
462 (save-excursion
463 (goto-char (org-babel-where-is-src-block-result)) (forward-line 1)
464 (if (stringp result) ;; assume the result is a table if it's not a string
465 (if (member "file" insert)
466 (insert result)
467 (org-babel-examplize-region (point) (progn (insert result) (point))))
468 (progn
469 (insert
470 (concat (orgtbl-to-orgtbl
471 (if (consp (car result)) result (list result))
472 '(:fmt (lambda (cell) (format "%S" cell)))) "\n"))
473 (forward-line -1)
474 (org-cycle))))
475 (message "finished"))))
477 (defun org-babel-result-to-org-string (result)
478 "Return RESULT as a string in org-mode format. This function
479 relies on `org-babel-insert-result'."
480 (with-temp-buffer (org-babel-insert-result result) (buffer-string)))
482 (defun org-babel-remove-result ()
483 "Remove the result of the current source block."
484 (interactive)
485 (save-excursion
486 (goto-char (org-babel-where-is-src-block-result)) (forward-line 1)
487 (delete-region (point) (org-babel-result-end))))
489 (defun org-babel-result-end ()
490 "Return the point at the end of the current set of results"
491 (save-excursion
492 (if (org-at-table-p)
493 (org-table-end)
494 (while (if (looking-at "\\(: \\|\\[\\[\\)")
495 (progn (while (looking-at "\\(: \\|\\[\\[\\)")
496 (forward-line 1)) t))
497 (forward-line 1))
498 (forward-line -1)
499 (point))))
501 (defun org-babel-result-to-file (result)
502 "Return an `org-mode' link with the path being the value or
503 RESULT, and the display being the `file-name-nondirectory' if
504 non-nil."
505 (let ((name (file-name-nondirectory result)))
506 (concat "[[file:" result (if name (concat "][" name "]]") "]]"))))
508 (defun org-babel-examplize-region (beg end)
509 "Comment out region using the ': ' org example quote."
510 (interactive "*r")
511 (let ((size (abs (- (line-number-at-pos end)
512 (line-number-at-pos beg)))))
513 (if (= size 0)
514 (let ((result (buffer-substring beg end)))
515 (delete-region beg end)
516 (insert (concat ": " result)))
517 (save-excursion
518 (goto-char beg)
519 (dotimes (n size)
520 (move-beginning-of-line 1) (insert ": ") (forward-line 1))))))
522 (defun org-babel-merge-params (&rest plists)
523 "Combine all parameter association lists in PLISTS. Later
524 elements of PLISTS override the values of previous element. This
525 takes into account some special considerations for certain
526 parameters when merging lists."
527 (let (params results vars var ref)
528 (mapc (lambda (plist)
529 (mapc (lambda (pair)
530 (case (car pair)
531 (:var
532 ;; we want only one specification per variable
533 (when (string-match "^\\([^= \f\t\n\r\v]+\\)=\\([^\f\n\r\v]+\\)$" (cdr pair))
534 ;; TODO: When is this not true? Can there be whitespace around the '='?
535 (setq var (intern (match-string 1 (cdr pair)))
536 ref (match-string 2 (cdr pair))
537 vars (cons (cons var ref) (assq-delete-all var vars)))))
538 (:results
539 ;; maintain list of unique :results specifications
540 (setq results (org-uniquify (append (split-string (cdr pair)) results))))
542 ;; replace: this covers e.g. :session
543 (setq params (cons pair (assq-delete-all (car pair) params))))))
544 plist))
545 plists)
546 (setq vars (mapcar (lambda (pair) (format "%s=%s" (car pair) (cdr pair))) vars))
547 (while vars (setq params (cons (cons :var (pop vars)) params)))
548 (cons (cons :results (mapconcat 'identity results " ")) params)))
550 (defun org-babel-clean-text-properties (text)
551 "Strip all properties from text return."
552 (set-text-properties 0 (length text) nil text) text)
554 (defun org-babel-strip-protective-commas (body)
555 "Strip protective commas from bodies of source blocks."
556 (replace-regexp-in-string "^,#" "#" body))
558 (defun org-babel-read (cell)
559 "Convert the string value of CELL to a number if appropriate.
560 Otherwise if cell looks like lisp (meaning it starts with a
561 \"(\" or a \"'\") then read it as lisp, otherwise return it
562 unmodified as a string.
564 This is taken almost directly from `org-read-prop'."
565 (if (and (stringp cell) (not (equal cell "")))
566 (or (org-babel-number-p cell)
567 (if (or (equal "(" (substring cell 0 1))
568 (equal "'" (substring cell 0 1)))
569 (read cell)
570 (progn (set-text-properties 0 (length cell) nil cell) cell)))
571 cell))
573 (defun org-babel-number-p (string)
574 "Return t if STRING represents a number"
575 (if (string-match "^[[:digit:]]*\\.?[[:digit:]]*$" string)
576 (string-to-number string)))
578 (defun org-babel-import-elisp-from-file (file-name)
579 "Read the results located at FILE-NAME into an elisp table. If
580 the table is trivial, then return it as a scalar."
581 (let (result)
582 (with-temp-buffer
583 (condition-case nil
584 (progn
585 (org-table-import file-name nil)
586 (delete-file file-name)
587 (setq result (mapcar (lambda (row)
588 (mapcar #'org-babel-string-read row))
589 (org-table-to-lisp))))
590 (error nil))
591 (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
592 (if (consp (car result))
593 (if (null (cdr (car result)))
594 (caar result)
595 result)
596 (car result))
597 result))))
599 (defun org-babel-string-read (cell)
600 "Strip nested \"s from around strings in exported R values."
601 (org-babel-read (or (and (stringp cell)
602 (string-match "\\\"\\(.+\\)\\\"" cell)
603 (match-string 1 cell))
604 cell)))
606 (defun org-babel-reverse-string (string)
607 (apply 'string (reverse (string-to-list string))))
609 (defun org-babel-chomp (string &optional regexp)
610 "Remove any trailing space or carriage returns characters from
611 STRING. Default regexp used is \"[ \f\t\n\r\v]\" but can be
612 overwritten by specifying a regexp as a second argument."
613 (while (and (> (length string) 0) (string-match "[ \f\t\n\r\v]" (substring string -1)))
614 (setq string (substring string 0 -1)))
615 string)
617 (defun org-babel-trim (string &optional regexp)
618 "Like `org-babel-chomp' only it runs on both the front and back of the string"
619 (org-babel-chomp (org-babel-reverse-string
620 (org-babel-chomp (org-babel-reverse-string string) regexp)) regexp))
622 (provide 'org-babel)
623 ;;; org-babel.el ends here