* calendar/todo-mode.el: Fix two bugs.
[bpt/emacs.git] / lisp / org / ob-octave.el
CommitLineData
86fbb8ca
CD
1;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
2
ba318903 3;; Copyright (C) 2010-2014 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)
86fbb8ca
CD
33(eval-when-compile (require 'cl))
34
35(declare-function matlab-shell "ext:matlab-mode")
36(declare-function matlab-shell-run-region "ext:matlab-mode")
37
38(defvar org-babel-default-header-args:matlab '())
39(defvar org-babel-default-header-args:octave '())
40
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.")
45
86fbb8ca
CD
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.")
50
51(defvar org-babel-matlab-emacs-link-wrapper-method
8223b1d2 52 "%s
86fbb8ca
CD
53if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
54else, save -ascii %s ans
55end
56delete('%s')
57")
58(defvar org-babel-octave-wrapper-method
59 "%s
60if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
afe98dfa 61else, dlmwrite('%s', ans, '\\t')
86fbb8ca
CD
62end")
63
64(defvar org-babel-octave-eoe-indicator "\'org_babel_eoe\'")
65
66(defvar org-babel-octave-eoe-output "ans = org_babel_eoe")
67
68(defun org-babel-execute:matlab (body params)
69 "Execute a block of matlab code with Babel."
86fbb8ca 70 (org-babel-execute:octave body params 'matlab))
afe98dfa 71
86fbb8ca
CD
72(defun org-babel-execute:octave (body params &optional matlabp)
73 "Execute a block of octave code with Babel."
afe98dfa 74 (let* ((session
86fbb8ca
CD
75 (funcall (intern (format "org-babel-%s-initiate-session"
76 (if matlabp "matlab" "octave")))
afe98dfa
CD
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)))
86fbb8ca 81 (out-file (cdr (assoc :file params)))
afe98dfa
CD
82 (full-body
83 (org-babel-expand-body:generic
84 body params (org-babel-variable-assignments:octave params)))
86fbb8ca 85 (result (org-babel-octave-evaluate
e66ba1df
BG
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)))))))
86fbb8ca
CD
104
105(defun org-babel-prep-session:matlab (session params)
106 "Prepare SESSION according to PARAMS."
86fbb8ca 107 (org-babel-prep-session:octave session params 'matlab))
afe98dfa
CD
108
109(defun org-babel-variable-assignments:octave (params)
8223b1d2 110 "Return list of octave statements assigning the block's variables."
afe98dfa
CD
111 (mapcar
112 (lambda (pair)
3ab2c837 113 (format "%s=%s;"
afe98dfa
CD
114 (car pair)
115 (org-babel-octave-var-to-octave (cdr pair))))
116 (mapcar #'cdr (org-babel-get-header params :var))))
117
118(defalias 'org-babel-variable-assignments:matlab
119 'org-babel-variable-assignments:octave)
120
86fbb8ca
CD
121(defun org-babel-octave-var-to-octave (var)
122 "Convert an emacs-lisp value into an octave variable.
123Converts an emacs-lisp variable into a string of octave code
124specifying a variable of the same value."
125 (if (listp var)
afe98dfa
CD
126 (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
127 (if (listp (car var)) "; " ",")) "]")
e66ba1df
BG
128 (cond
129 ((stringp var)
130 (format "\'%s\'" var))
131 (t
132 (format "%s" var)))))
86fbb8ca
CD
133
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))
afe98dfa 137 (var-lines (org-babel-variable-assignments:octave params)))
86fbb8ca
CD
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))
143
144(defun org-babel-matlab-initiate-session (&optional session params)
145 "Create a matlab inferior process buffer.
146If there is not a current inferior-process-buffer in SESSION then
8223b1d2 147create. Return the initialized session."
86fbb8ca 148 (org-babel-octave-initiate-session session params 'matlab))
afe98dfa 149
86fbb8ca
CD
150(defun org-babel-octave-initiate-session (&optional session params matlabp)
151 "Create an octave inferior process buffer.
152If there is not a current inferior-process-buffer in SESSION then
8223b1d2 153create. Return the initialized session."
271672fa
BG
154 (if matlabp (require 'matlab) (or (require 'octave-inf nil 'noerror)
155 (require 'octave)))
86fbb8ca
CD
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))))))
166
167(defun org-babel-octave-evaluate
afe98dfa 168 (session body result-type &optional matlabp)
86fbb8ca
CD
169 "Pass BODY to the octave process in SESSION.
170If RESULT-TYPE equals 'output then return the outputs of the
171statements in BODY, if RESULT-TYPE equals 'value then return the
172value 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)))
176
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))
afe98dfa 184 (value (let ((tmp-file (org-babel-temp-file "octave-")))
86fbb8ca
CD
185 (org-babel-eval
186 cmd
afe98dfa
CD
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))))))
86fbb8ca
CD
191
192(defun org-babel-octave-evaluate-session
193 (session body result-type &optional matlabp)
194 "Evaluate BODY in SESSION."
afe98dfa
CD
195 (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
196 (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
86fbb8ca
CD
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
afe98dfa
CD
207 body
208 (org-babel-process-file-name tmp-file 'noquote)
209 (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
86fbb8ca
CD
210 (mapconcat
211 #'org-babel-chomp
212 (list (format org-babel-octave-wrapper-method
afe98dfa
CD
213 body
214 (org-babel-process-file-name tmp-file 'noquote)
215 (org-babel-process-file-name tmp-file 'noquote))
86fbb8ca
CD
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
8223b1d2
BG
226 ;; make *matlab* buffer contents easily
227 ;; available, so :results output currently
228 ;; won't work
86fbb8ca
CD
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
afe98dfa 238 (org-babel-octave-import-elisp-from-file tmp-file))
86fbb8ca
CD
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"))))))
251
252(defun org-babel-octave-import-elisp-from-file (file-name)
253 "Import data from FILE-NAME.
254This removes initial blank and comment lines and then calls
255`org-babel-import-elisp-from-file'."
afe98dfa 256 (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
86fbb8ca
CD
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)))
afe98dfa 263 (org-babel-import-elisp-from-file temp-file '(16))))
86fbb8ca
CD
264
265(defun org-babel-octave-read-string (string)
8223b1d2 266 "Strip \\\"s from around octave string."
86fbb8ca
CD
267 (if (string-match "^\"\\([^\000]+\\)\"$" string)
268 (match-string 1 string)
269 string))
270
e66ba1df
BG
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))))
275
86fbb8ca
CD
276(provide 'ob-octave)
277
5b409b39 278
86fbb8ca
CD
279
280;;; ob-octave.el ends here