org-e-odt: Internalize filter
[org-mode.git] / lisp / ob-octave.el
blob73f25eca155d6fdf96475bbc97e34d2ca896a669
1 ;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
3 ;; Copyright (C) 2010-2012 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 (require 'ob-ref)
34 (require 'ob-comint)
35 (require 'ob-eval)
36 (eval-when-compile (require 'cl))
38 (declare-function matlab-shell "ext:matlab-mode")
39 (declare-function matlab-shell-run-region "ext:matlab-mode")
41 (defvar org-babel-default-header-args:matlab '())
42 (defvar org-babel-default-header-args:octave '())
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.")
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.")
54 (defvar org-babel-matlab-emacs-link-wrapper-method
55 "%s
56 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
57 else, save -ascii %s ans
58 end
59 delete('%s')
61 (defvar org-babel-octave-wrapper-method
62 "%s
63 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
64 else, dlmwrite('%s', ans, '\\t')
65 end")
67 (defvar org-babel-octave-eoe-indicator "\'org_babel_eoe\'")
69 (defvar org-babel-octave-eoe-output "ans = org_babel_eoe")
71 (defun org-babel-execute:matlab (body params)
72 "Execute a block of matlab code with Babel."
73 (org-babel-execute:octave body params 'matlab))
75 (defun org-babel-execute:octave (body params &optional matlabp)
76 "Execute a block of octave code with Babel."
77 (let* ((session
78 (funcall (intern (format "org-babel-%s-initiate-session"
79 (if matlabp "matlab" "octave")))
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)))
84 (out-file (cdr (assoc :file params)))
85 (full-body
86 (org-babel-expand-body:generic
87 body params (org-babel-variable-assignments:octave params)))
88 (result (org-babel-octave-evaluate
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)
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)))))))
108 (defun org-babel-prep-session:matlab (session params)
109 "Prepare SESSION according to PARAMS."
110 (org-babel-prep-session:octave session params 'matlab))
112 (defun org-babel-variable-assignments:octave (params)
113 "Return list of octave statements assigning the block's variables."
114 (mapcar
115 (lambda (pair)
116 (format "%s=%s;"
117 (car pair)
118 (org-babel-octave-var-to-octave (cdr pair))))
119 (mapcar #'cdr (org-babel-get-header params :var))))
121 (defalias 'org-babel-variable-assignments:matlab
122 'org-babel-variable-assignments:octave)
124 (defun org-babel-octave-var-to-octave (var)
125 "Convert an emacs-lisp value into an octave variable.
126 Converts an emacs-lisp variable into a string of octave code
127 specifying a variable of the same value."
128 (if (listp var)
129 (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
130 (if (listp (car var)) "; " ",")) "]")
131 (cond
132 ((stringp var)
133 (format "\'%s\'" var))
135 (format "%s" var)))))
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))
140 (var-lines (org-babel-variable-assignments:octave params)))
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))
147 (defun org-babel-matlab-initiate-session (&optional session params)
148 "Create a matlab inferior process buffer.
149 If there is not a current inferior-process-buffer in SESSION then
150 create. Return the initialized session."
151 (org-babel-octave-initiate-session session params 'matlab))
153 (defun org-babel-octave-initiate-session (&optional session params matlabp)
154 "Create an octave inferior process buffer.
155 If there is not a current inferior-process-buffer in SESSION then
156 create. Return the initialized session."
157 (if matlabp (require 'matlab) (require 'octave-inf))
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))))))
169 (defun org-babel-octave-evaluate
170 (session body result-type &optional matlabp)
171 "Pass BODY to the octave process in SESSION.
172 If RESULT-TYPE equals 'output then return the outputs of the
173 statements in BODY, if RESULT-TYPE equals 'value then return the
174 value 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)))
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))
186 (value (let ((tmp-file (org-babel-temp-file "octave-")))
187 (org-babel-eval
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))))))
194 (defun org-babel-octave-evaluate-session
195 (session body result-type &optional matlabp)
196 "Evaluate BODY in SESSION."
197 (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
198 (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
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
209 body
210 (org-babel-process-file-name tmp-file 'noquote)
211 (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
212 (mapconcat
213 #'org-babel-chomp
214 (list (format org-babel-octave-wrapper-method
215 body
216 (org-babel-process-file-name tmp-file 'noquote)
217 (org-babel-process-file-name tmp-file 'noquote))
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
228 ;; make *matlab* buffer contents easily
229 ;; available, so :results output currently
230 ;; won't work
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
240 (org-babel-octave-import-elisp-from-file tmp-file))
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"))))))
254 (defun org-babel-octave-import-elisp-from-file (file-name)
255 "Import data from FILE-NAME.
256 This removes initial blank and comment lines and then calls
257 `org-babel-import-elisp-from-file'."
258 (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
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)))
265 (org-babel-import-elisp-from-file temp-file '(16))))
267 (defun org-babel-octave-read-string (string)
268 "Strip \\\"s from around octave string."
269 (if (string-match "^\"\\([^\000]+\\)\"$" string)
270 (match-string 1 string)
271 string))
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))))
278 (provide 'ob-octave)
282 ;;; ob-octave.el ends here