Backport commit 284c470 from Emacs master branch
[org-mode.git] / lisp / ob-octave.el
blobdbd9100cbba5dbfcb9de80d62bd47dd0f4b32519
1 ;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
3 ;; Copyright (C) 2010-2015 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 (gfx-file (ignore-errors (org-babel-graphical-output-file params)))
86 (result (org-babel-octave-evaluate
87 session
88 (if gfx-file
89 (mapconcat 'identity
90 (list
91 "set (0, \"defaultfigurevisible\", \"off\");"
92 full-body
93 (format "print -dpng %s" gfx-file))
94 "\n")
95 full-body)
96 result-type matlabp)))
97 (if gfx-file
98 nil
99 (org-babel-reassemble-table
100 result
101 (org-babel-pick-name
102 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
103 (org-babel-pick-name
104 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))))
106 (defun org-babel-prep-session:matlab (session params)
107 "Prepare SESSION according to PARAMS."
108 (org-babel-prep-session:octave session params 'matlab))
110 (defun org-babel-variable-assignments:octave (params)
111 "Return list of octave statements assigning the block's variables."
112 (mapcar
113 (lambda (pair)
114 (format "%s=%s;"
115 (car pair)
116 (org-babel-octave-var-to-octave (cdr pair))))
117 (mapcar #'cdr (org-babel-get-header params :var))))
119 (defalias 'org-babel-variable-assignments:matlab
120 'org-babel-variable-assignments:octave)
122 (defun org-babel-octave-var-to-octave (var)
123 "Convert an emacs-lisp value into an octave variable.
124 Converts an emacs-lisp variable into a string of octave code
125 specifying a variable of the same value."
126 (if (listp var)
127 (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
128 (if (listp (car var)) "; " ",")) "]")
129 (cond
130 ((stringp var)
131 (format "'%s'" var))
133 (format "%s" var)))))
135 (defun org-babel-prep-session:octave (session params &optional matlabp)
136 "Prepare SESSION according to the header arguments specified in PARAMS."
137 (let* ((session (org-babel-octave-initiate-session session params matlabp))
138 (var-lines (org-babel-variable-assignments:octave params)))
139 (org-babel-comint-in-buffer session
140 (mapc (lambda (var)
141 (end-of-line 1) (insert var) (comint-send-input nil t)
142 (org-babel-comint-wait-for-output session)) var-lines))
143 session))
145 (defun org-babel-matlab-initiate-session (&optional session params)
146 "Create a matlab inferior process buffer.
147 If there is not a current inferior-process-buffer in SESSION then
148 create. Return the initialized session."
149 (org-babel-octave-initiate-session session params 'matlab))
151 (defun org-babel-octave-initiate-session (&optional session params matlabp)
152 "Create an octave inferior process buffer.
153 If there is not a current inferior-process-buffer in SESSION then
154 create. Return the initialized session."
155 (if matlabp (require 'matlab) (or (require 'octave-inf nil 'noerror)
156 (require 'octave)))
157 (unless (string= session "none")
158 (let ((session (or session
159 (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
160 (if (org-babel-comint-buffer-livep session) session
161 (save-window-excursion
162 (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
163 (run-octave))
164 (rename-buffer (if (bufferp session) (buffer-name session)
165 (if (stringp session) session (buffer-name))))
166 (current-buffer))))))
168 (defun org-babel-octave-evaluate
169 (session body result-type &optional matlabp)
170 "Pass BODY to the octave process in SESSION.
171 If RESULT-TYPE equals `output' then return the outputs of the
172 statements in BODY, if RESULT-TYPE equals `value' then return the
173 value of the last statement in BODY, as elisp."
174 (if session
175 (org-babel-octave-evaluate-session session body result-type matlabp)
176 (org-babel-octave-evaluate-external-process body result-type matlabp)))
178 (defun org-babel-octave-evaluate-external-process (body result-type matlabp)
179 "Evaluate BODY in an external octave process."
180 (let ((cmd (if matlabp
181 org-babel-matlab-shell-command
182 org-babel-octave-shell-command)))
183 (case result-type
184 (output (org-babel-eval cmd body))
185 (value (let ((tmp-file (org-babel-temp-file "octave-")))
186 (org-babel-eval
188 (format org-babel-octave-wrapper-method body
189 (org-babel-process-file-name tmp-file 'noquote)
190 (org-babel-process-file-name tmp-file 'noquote)))
191 (org-babel-octave-import-elisp-from-file tmp-file))))))
193 (defun org-babel-octave-evaluate-session
194 (session body result-type &optional matlabp)
195 "Evaluate BODY in SESSION."
196 (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
197 (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
198 (full-body
199 (case result-type
200 (output
201 (mapconcat
202 #'org-babel-chomp
203 (list body org-babel-octave-eoe-indicator) "\n"))
204 (value
205 (if (and matlabp org-babel-matlab-with-emacs-link)
206 (concat
207 (format org-babel-matlab-emacs-link-wrapper-method
208 body
209 (org-babel-process-file-name tmp-file 'noquote)
210 (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
211 (mapconcat
212 #'org-babel-chomp
213 (list (format org-babel-octave-wrapper-method
214 body
215 (org-babel-process-file-name tmp-file 'noquote)
216 (org-babel-process-file-name tmp-file 'noquote))
217 org-babel-octave-eoe-indicator) "\n")))))
218 (raw (if (and matlabp org-babel-matlab-with-emacs-link)
219 (save-window-excursion
220 (with-temp-buffer
221 (insert full-body)
222 (write-region "" 'ignored wait-file nil nil nil 'excl)
223 (matlab-shell-run-region (point-min) (point-max))
224 (message "Waiting for Matlab Emacs Link")
225 (while (file-exists-p wait-file) (sit-for 0.01))
226 "")) ;; matlab-shell-run-region doesn't seem to
227 ;; make *matlab* buffer contents easily
228 ;; available, so :results output currently
229 ;; won't work
230 (org-babel-comint-with-output
231 (session
232 (if matlabp
233 org-babel-octave-eoe-indicator
234 org-babel-octave-eoe-output)
235 t full-body)
236 (insert full-body) (comint-send-input nil t)))) results)
237 (case result-type
238 (value
239 (org-babel-octave-import-elisp-from-file tmp-file))
240 (output
241 (progn
242 (setq results
243 (if matlabp
244 (cdr (reverse (delq "" (mapcar
245 #'org-babel-octave-read-string
246 (mapcar #'org-babel-trim raw)))))
247 (cdr (member org-babel-octave-eoe-output
248 (reverse (mapcar
249 #'org-babel-octave-read-string
250 (mapcar #'org-babel-trim raw)))))))
251 (mapconcat #'identity (reverse results) "\n"))))))
253 (defun org-babel-octave-import-elisp-from-file (file-name)
254 "Import data from FILE-NAME.
255 This removes initial blank and comment lines and then calls
256 `org-babel-import-elisp-from-file'."
257 (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
258 (with-temp-file temp-file
259 (insert-file-contents file-name)
260 (re-search-forward "^[ \t]*[^# \t]" nil t)
261 (if (< (setq beg (point-min))
262 (setq end (point-at-bol)))
263 (delete-region beg end)))
264 (org-babel-import-elisp-from-file temp-file '(16))))
266 (defun org-babel-octave-read-string (string)
267 "Strip \\\"s from around octave string."
268 (if (string-match "^\"\\([^\000]+\\)\"$" string)
269 (match-string 1 string)
270 string))
272 (provide 'ob-octave)
276 ;;; ob-octave.el ends here