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