Merge from emacs-23; up to 2010-06-09T17:54:28Z!albinus@detlef.
[bpt/emacs.git] / lisp / org / ob-octave.el
CommitLineData
86fbb8ca
CD
1;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
2
73b0cd50 3;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
86fbb8ca
CD
4
5;; Author: Dan Davison
6;; Keywords: literate programming, reproducible research
7;; Homepage: http://orgmode.org
acedf35c 8;; Version: 7.4
86fbb8ca
CD
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs 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 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs 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.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26
27;;; Requirements:
28
29;; octave
30;; octave-mode.el and octave-inf.el come with GNU emacs
31
32;;; Code:
33(require 'ob)
34(require 'ob-ref)
35(require 'ob-comint)
36(require 'ob-eval)
37(eval-when-compile (require 'cl))
38
39(declare-function matlab-shell "ext:matlab-mode")
40(declare-function matlab-shell-run-region "ext:matlab-mode")
41
42(defvar org-babel-default-header-args:matlab '())
43(defvar org-babel-default-header-args:octave '())
44
45(defvar org-babel-matlab-shell-command "matlab -nosplash"
46 "Shell command to run matlab as an external process.")
47(defvar org-babel-octave-shell-command "octave -q"
48 "Shell command to run octave as an external process.")
49
86fbb8ca
CD
50(defvar org-babel-matlab-with-emacs-link nil
51 "If non-nil use matlab-shell-run-region for session evaluation.
52 This will use EmacsLink if (matlab-with-emacs-link) evaluates
53 to a non-nil value.")
54
55(defvar org-babel-matlab-emacs-link-wrapper-method
56 "%s
57if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
58else, save -ascii %s ans
59end
60delete('%s')
61")
62(defvar org-babel-octave-wrapper-method
63 "%s
64if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
afe98dfa 65else, dlmwrite('%s', ans, '\\t')
86fbb8ca
CD
66end")
67
68(defvar org-babel-octave-eoe-indicator "\'org_babel_eoe\'")
69
70(defvar org-babel-octave-eoe-output "ans = org_babel_eoe")
71
72(defun org-babel-execute:matlab (body params)
73 "Execute a block of matlab code with Babel."
86fbb8ca 74 (org-babel-execute:octave body params 'matlab))
afe98dfa 75
86fbb8ca
CD
76(defun org-babel-execute:octave (body params &optional matlabp)
77 "Execute a block of octave code with Babel."
afe98dfa 78 (let* ((session
86fbb8ca
CD
79 (funcall (intern (format "org-babel-%s-initiate-session"
80 (if matlabp "matlab" "octave")))
afe98dfa
CD
81 (cdr (assoc :session params)) params))
82 (vars (mapcar #'cdr (org-babel-get-header params :var)))
83 (result-params (cdr (assoc :result-params params)))
84 (result-type (cdr (assoc :result-type params)))
86fbb8ca 85 (out-file (cdr (assoc :file params)))
afe98dfa
CD
86 (full-body
87 (org-babel-expand-body:generic
88 body params (org-babel-variable-assignments:octave params)))
86fbb8ca 89 (result (org-babel-octave-evaluate
afe98dfa 90 session full-body result-type matlabp)))
86fbb8ca
CD
91 (or out-file
92 (org-babel-reassemble-table
93 result
94 (org-babel-pick-name
afe98dfa 95 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
86fbb8ca 96 (org-babel-pick-name
afe98dfa 97 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))))
86fbb8ca
CD
98
99(defun org-babel-prep-session:matlab (session params)
100 "Prepare SESSION according to PARAMS."
86fbb8ca 101 (org-babel-prep-session:octave session params 'matlab))
afe98dfa
CD
102
103(defun org-babel-variable-assignments:octave (params)
104 "Return list of octave statements assigning the block's variables"
105 (mapcar
106 (lambda (pair)
107 (format "%s=%s"
108 (car pair)
109 (org-babel-octave-var-to-octave (cdr pair))))
110 (mapcar #'cdr (org-babel-get-header params :var))))
111
112(defalias 'org-babel-variable-assignments:matlab
113 'org-babel-variable-assignments:octave)
114
86fbb8ca
CD
115(defun org-babel-octave-var-to-octave (var)
116 "Convert an emacs-lisp value into an octave variable.
117Converts an emacs-lisp variable into a string of octave code
118specifying a variable of the same value."
119 (if (listp var)
afe98dfa
CD
120 (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
121 (if (listp (car var)) "; " ",")) "]")
122 (format "%s" (or var "nil"))))
86fbb8ca
CD
123
124(defun org-babel-prep-session:octave (session params &optional matlabp)
125 "Prepare SESSION according to the header arguments specified in PARAMS."
126 (let* ((session (org-babel-octave-initiate-session session params matlabp))
afe98dfa 127 (var-lines (org-babel-variable-assignments:octave params)))
86fbb8ca
CD
128 (org-babel-comint-in-buffer session
129 (mapc (lambda (var)
130 (end-of-line 1) (insert var) (comint-send-input nil t)
131 (org-babel-comint-wait-for-output session)) var-lines))
132 session))
133
134(defun org-babel-matlab-initiate-session (&optional session params)
135 "Create a matlab inferior process buffer.
136If there is not a current inferior-process-buffer in SESSION then
137create. Return the initialized session."
86fbb8ca 138 (org-babel-octave-initiate-session session params 'matlab))
afe98dfa 139
86fbb8ca
CD
140(defun org-babel-octave-initiate-session (&optional session params matlabp)
141 "Create an octave inferior process buffer.
142If there is not a current inferior-process-buffer in SESSION then
143create. Return the initialized session."
afe98dfa 144 (if matlabp (require 'matlab) (require 'octave-inf))
86fbb8ca
CD
145 (unless (string= session "none")
146 (let ((session (or session
147 (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
148 (if (org-babel-comint-buffer-livep session) session
149 (save-window-excursion
150 (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
151 (run-octave))
152 (rename-buffer (if (bufferp session) (buffer-name session)
153 (if (stringp session) session (buffer-name))))
154 (current-buffer))))))
155
156(defun org-babel-octave-evaluate
afe98dfa 157 (session body result-type &optional matlabp)
86fbb8ca
CD
158 "Pass BODY to the octave process in SESSION.
159If RESULT-TYPE equals 'output then return the outputs of the
160statements in BODY, if RESULT-TYPE equals 'value then return the
161value of the last statement in BODY, as elisp."
162 (if session
163 (org-babel-octave-evaluate-session session body result-type matlabp)
164 (org-babel-octave-evaluate-external-process body result-type matlabp)))
165
166(defun org-babel-octave-evaluate-external-process (body result-type matlabp)
167 "Evaluate BODY in an external octave process."
168 (let ((cmd (if matlabp
169 org-babel-matlab-shell-command
170 org-babel-octave-shell-command)))
171 (case result-type
172 (output (org-babel-eval cmd body))
afe98dfa 173 (value (let ((tmp-file (org-babel-temp-file "octave-")))
86fbb8ca
CD
174 (org-babel-eval
175 cmd
afe98dfa
CD
176 (format org-babel-octave-wrapper-method body
177 (org-babel-process-file-name tmp-file 'noquote)
178 (org-babel-process-file-name tmp-file 'noquote)))
179 (org-babel-octave-import-elisp-from-file tmp-file))))))
86fbb8ca
CD
180
181(defun org-babel-octave-evaluate-session
182 (session body result-type &optional matlabp)
183 "Evaluate BODY in SESSION."
afe98dfa
CD
184 (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
185 (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
86fbb8ca
CD
186 (full-body
187 (case result-type
188 (output
189 (mapconcat
190 #'org-babel-chomp
191 (list body org-babel-octave-eoe-indicator) "\n"))
192 (value
193 (if (and matlabp org-babel-matlab-with-emacs-link)
194 (concat
195 (format org-babel-matlab-emacs-link-wrapper-method
afe98dfa
CD
196 body
197 (org-babel-process-file-name tmp-file 'noquote)
198 (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
86fbb8ca
CD
199 (mapconcat
200 #'org-babel-chomp
201 (list (format org-babel-octave-wrapper-method
afe98dfa
CD
202 body
203 (org-babel-process-file-name tmp-file 'noquote)
204 (org-babel-process-file-name tmp-file 'noquote))
86fbb8ca
CD
205 org-babel-octave-eoe-indicator) "\n")))))
206 (raw (if (and matlabp org-babel-matlab-with-emacs-link)
207 (save-window-excursion
208 (with-temp-buffer
209 (insert full-body)
210 (write-region "" 'ignored wait-file nil nil nil 'excl)
211 (matlab-shell-run-region (point-min) (point-max))
212 (message "Waiting for Matlab Emacs Link")
213 (while (file-exists-p wait-file) (sit-for 0.01))
214 "")) ;; matlab-shell-run-region doesn't seem to
215 ;; make *matlab* buffer contents easily
216 ;; available, so :results output currently
217 ;; won't work
218 (org-babel-comint-with-output
219 (session
220 (if matlabp
221 org-babel-octave-eoe-indicator
222 org-babel-octave-eoe-output)
223 t full-body)
224 (insert full-body) (comint-send-input nil t)))) results)
225 (case result-type
226 (value
afe98dfa 227 (org-babel-octave-import-elisp-from-file tmp-file))
86fbb8ca
CD
228 (output
229 (progn
230 (setq results
231 (if matlabp
232 (cdr (reverse (delq "" (mapcar
233 #'org-babel-octave-read-string
234 (mapcar #'org-babel-trim raw)))))
235 (cdr (member org-babel-octave-eoe-output
236 (reverse (mapcar
237 #'org-babel-octave-read-string
238 (mapcar #'org-babel-trim raw)))))))
239 (mapconcat #'identity (reverse results) "\n"))))))
240
241(defun org-babel-octave-import-elisp-from-file (file-name)
242 "Import data from FILE-NAME.
243This removes initial blank and comment lines and then calls
244`org-babel-import-elisp-from-file'."
afe98dfa 245 (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
86fbb8ca
CD
246 (with-temp-file temp-file
247 (insert-file-contents file-name)
248 (re-search-forward "^[ \t]*[^# \t]" nil t)
249 (if (< (setq beg (point-min))
250 (setq end (point-at-bol)))
251 (delete-region beg end)))
afe98dfa 252 (org-babel-import-elisp-from-file temp-file '(16))))
86fbb8ca
CD
253
254(defun org-babel-octave-read-string (string)
255 "Strip \\\"s from around octave string"
256 (if (string-match "^\"\\([^\000]+\\)\"$" string)
257 (match-string 1 string)
258 string))
259
260(provide 'ob-octave)
261
86fbb8ca
CD
262
263;;; ob-octave.el ends here