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