org-agenda.el (org-agenda-write): Allow writing to an .org file.
[org-mode.git] / lisp / ob-octave.el
blobc2a3abb5fb1bb4128d91aeffa796101171ae5f47
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) (require 'octave-inf))
155 (unless (string= session "none")
156 (let ((session (or session
157 (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
158 (if (org-babel-comint-buffer-livep session) session
159 (save-window-excursion
160 (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
161 (run-octave))
162 (rename-buffer (if (bufferp session) (buffer-name session)
163 (if (stringp session) session (buffer-name))))
164 (current-buffer))))))
166 (defun org-babel-octave-evaluate
167 (session body result-type &optional matlabp)
168 "Pass BODY to the octave process in SESSION.
169 If RESULT-TYPE equals 'output then return the outputs of the
170 statements in BODY, if RESULT-TYPE equals 'value then return the
171 value of the last statement in BODY, as elisp."
172 (if session
173 (org-babel-octave-evaluate-session session body result-type matlabp)
174 (org-babel-octave-evaluate-external-process body result-type matlabp)))
176 (defun org-babel-octave-evaluate-external-process (body result-type matlabp)
177 "Evaluate BODY in an external octave process."
178 (let ((cmd (if matlabp
179 org-babel-matlab-shell-command
180 org-babel-octave-shell-command)))
181 (case result-type
182 (output (org-babel-eval cmd body))
183 (value (let ((tmp-file (org-babel-temp-file "octave-")))
184 (org-babel-eval
186 (format org-babel-octave-wrapper-method body
187 (org-babel-process-file-name tmp-file 'noquote)
188 (org-babel-process-file-name tmp-file 'noquote)))
189 (org-babel-octave-import-elisp-from-file tmp-file))))))
191 (defun org-babel-octave-evaluate-session
192 (session body result-type &optional matlabp)
193 "Evaluate BODY in SESSION."
194 (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
195 (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
196 (full-body
197 (case result-type
198 (output
199 (mapconcat
200 #'org-babel-chomp
201 (list body org-babel-octave-eoe-indicator) "\n"))
202 (value
203 (if (and matlabp org-babel-matlab-with-emacs-link)
204 (concat
205 (format org-babel-matlab-emacs-link-wrapper-method
206 body
207 (org-babel-process-file-name tmp-file 'noquote)
208 (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
209 (mapconcat
210 #'org-babel-chomp
211 (list (format org-babel-octave-wrapper-method
212 body
213 (org-babel-process-file-name tmp-file 'noquote)
214 (org-babel-process-file-name tmp-file 'noquote))
215 org-babel-octave-eoe-indicator) "\n")))))
216 (raw (if (and matlabp org-babel-matlab-with-emacs-link)
217 (save-window-excursion
218 (with-temp-buffer
219 (insert full-body)
220 (write-region "" 'ignored wait-file nil nil nil 'excl)
221 (matlab-shell-run-region (point-min) (point-max))
222 (message "Waiting for Matlab Emacs Link")
223 (while (file-exists-p wait-file) (sit-for 0.01))
224 "")) ;; matlab-shell-run-region doesn't seem to
225 ;; make *matlab* buffer contents easily
226 ;; available, so :results output currently
227 ;; won't work
228 (org-babel-comint-with-output
229 (session
230 (if matlabp
231 org-babel-octave-eoe-indicator
232 org-babel-octave-eoe-output)
233 t full-body)
234 (insert full-body) (comint-send-input nil t)))) results)
235 (case result-type
236 (value
237 (org-babel-octave-import-elisp-from-file tmp-file))
238 (output
239 (progn
240 (setq results
241 (if matlabp
242 (cdr (reverse (delq "" (mapcar
243 #'org-babel-octave-read-string
244 (mapcar #'org-babel-trim raw)))))
245 (cdr (member org-babel-octave-eoe-output
246 (reverse (mapcar
247 #'org-babel-octave-read-string
248 (mapcar #'org-babel-trim raw)))))))
249 (mapconcat #'identity (reverse results) "\n"))))))
251 (defun org-babel-octave-import-elisp-from-file (file-name)
252 "Import data from FILE-NAME.
253 This removes initial blank and comment lines and then calls
254 `org-babel-import-elisp-from-file'."
255 (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
256 (with-temp-file temp-file
257 (insert-file-contents file-name)
258 (re-search-forward "^[ \t]*[^# \t]" nil t)
259 (if (< (setq beg (point-min))
260 (setq end (point-at-bol)))
261 (delete-region beg end)))
262 (org-babel-import-elisp-from-file temp-file '(16))))
264 (defun org-babel-octave-read-string (string)
265 "Strip \\\"s from around octave string."
266 (if (string-match "^\"\\([^\000]+\\)\"$" string)
267 (match-string 1 string)
268 string))
270 (defun org-babel-octave-graphical-output-file (params)
271 "Name of file to which maxima should send graphical output."
272 (and (member "graphics" (cdr (assq :result-params params)))
273 (cdr (assq :file params))))
275 (provide 'ob-octave)
279 ;;; ob-octave.el ends here