Release 7.3
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-octave.el
blobd6affecd74d456e0f282cf4861e0feb8dcd26882
1 ;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.3
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Requirements:
29 ;; octave
30 ;; octave-mode.el and octave-inf.el come with GNU emacs
32 ;;; Code:
33 (require 'ob)
34 (require 'ob-ref)
35 (require 'ob-comint)
36 (require 'ob-eval)
37 (eval-when-compile (require 'cl))
39 (declare-function matlab-shell "ext:matlab-mode")
40 (declare-function matlab-shell-run-region "ext:matlab-mode")
42 (defvar org-babel-default-header-args:matlab '())
43 (defvar org-babel-default-header-args:octave '())
45 (defvar org-babel-matlab-shell-command "matlab -nosplash"
46 "Shell command to run matlab as an external process.")
47 (defvar org-babel-octave-shell-command "octave -q"
48 "Shell command to run octave as an external process.")
50 (defvar org-babel-matlab-with-emacs-link nil
51 "If non-nil use matlab-shell-run-region for session evaluation.
52 This will use EmacsLink if (matlab-with-emacs-link) evaluates
53 to a non-nil value.")
55 (defvar org-babel-matlab-emacs-link-wrapper-method
56 "%s
57 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
58 else, save -ascii %s ans
59 end
60 delete('%s')
62 (defvar org-babel-octave-wrapper-method
63 "%s
64 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
65 else, dlmwrite('%s', ans, '\\t')
66 end")
68 (defvar org-babel-octave-eoe-indicator "\'org_babel_eoe\'")
70 (defvar org-babel-octave-eoe-output "ans = org_babel_eoe")
72 (defun org-babel-execute:matlab (body params)
73 "Execute a block of matlab code with Babel."
74 (org-babel-execute:octave body params 'matlab))
76 (defun org-babel-execute:octave (body params &optional matlabp)
77 "Execute a block of octave code with Babel."
78 (let* ((session
79 (funcall (intern (format "org-babel-%s-initiate-session"
80 (if matlabp "matlab" "octave")))
81 (cdr (assoc :session params)) params))
82 (vars (mapcar #'cdr (org-babel-get-header params :var)))
83 (result-params (cdr (assoc :result-params params)))
84 (result-type (cdr (assoc :result-type params)))
85 (out-file (cdr (assoc :file params)))
86 (full-body
87 (org-babel-expand-body:generic
88 body params (org-babel-variable-assignments:octave params)))
89 (result (org-babel-octave-evaluate
90 session full-body result-type matlabp)))
91 (or out-file
92 (org-babel-reassemble-table
93 result
94 (org-babel-pick-name
95 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
96 (org-babel-pick-name
97 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))))
99 (defun org-babel-prep-session:matlab (session params)
100 "Prepare SESSION according to PARAMS."
101 (org-babel-prep-session:octave session params 'matlab))
103 (defun org-babel-variable-assignments:octave (params)
104 "Return list of octave statements assigning the block's variables"
105 (mapcar
106 (lambda (pair)
107 (format "%s=%s"
108 (car pair)
109 (org-babel-octave-var-to-octave (cdr pair))))
110 (mapcar #'cdr (org-babel-get-header params :var))))
112 (defalias 'org-babel-variable-assignments:matlab
113 'org-babel-variable-assignments:octave)
115 (defun org-babel-octave-var-to-octave (var)
116 "Convert an emacs-lisp value into an octave variable.
117 Converts an emacs-lisp variable into a string of octave code
118 specifying a variable of the same value."
119 (if (listp var)
120 (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
121 (if (listp (car var)) "; " ",")) "]")
122 (format "%s" (or var "nil"))))
124 (defun org-babel-prep-session:octave (session params &optional matlabp)
125 "Prepare SESSION according to the header arguments specified in PARAMS."
126 (let* ((session (org-babel-octave-initiate-session session params matlabp))
127 (var-lines (org-babel-variable-assignments:octave params)))
128 (org-babel-comint-in-buffer session
129 (mapc (lambda (var)
130 (end-of-line 1) (insert var) (comint-send-input nil t)
131 (org-babel-comint-wait-for-output session)) var-lines))
132 session))
134 (defun org-babel-matlab-initiate-session (&optional session params)
135 "Create a matlab inferior process buffer.
136 If there is not a current inferior-process-buffer in SESSION then
137 create. Return the initialized session."
138 (org-babel-octave-initiate-session session params 'matlab))
140 (defun org-babel-octave-initiate-session (&optional session params matlabp)
141 "Create an octave inferior process buffer.
142 If there is not a current inferior-process-buffer in SESSION then
143 create. Return the initialized session."
144 (if matlabp (require 'matlab) (require 'octave-inf))
145 (unless (string= session "none")
146 (let ((session (or session
147 (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
148 (if (org-babel-comint-buffer-livep session) session
149 (save-window-excursion
150 (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
151 (run-octave))
152 (rename-buffer (if (bufferp session) (buffer-name session)
153 (if (stringp session) session (buffer-name))))
154 (current-buffer))))))
156 (defun org-babel-octave-evaluate
157 (session body result-type &optional matlabp)
158 "Pass BODY to the octave process in SESSION.
159 If RESULT-TYPE equals 'output then return the outputs of the
160 statements in BODY, if RESULT-TYPE equals 'value then return the
161 value of the last statement in BODY, as elisp."
162 (if session
163 (org-babel-octave-evaluate-session session body result-type matlabp)
164 (org-babel-octave-evaluate-external-process body result-type matlabp)))
166 (defun org-babel-octave-evaluate-external-process (body result-type matlabp)
167 "Evaluate BODY in an external octave process."
168 (let ((cmd (if matlabp
169 org-babel-matlab-shell-command
170 org-babel-octave-shell-command)))
171 (case result-type
172 (output (org-babel-eval cmd body))
173 (value (let ((tmp-file (org-babel-temp-file "octave-")))
174 (org-babel-eval
176 (format org-babel-octave-wrapper-method body
177 (org-babel-process-file-name tmp-file 'noquote)
178 (org-babel-process-file-name tmp-file 'noquote)))
179 (org-babel-octave-import-elisp-from-file tmp-file))))))
181 (defun org-babel-octave-evaluate-session
182 (session body result-type &optional matlabp)
183 "Evaluate BODY in SESSION."
184 (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
185 (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
186 (full-body
187 (case result-type
188 (output
189 (mapconcat
190 #'org-babel-chomp
191 (list body org-babel-octave-eoe-indicator) "\n"))
192 (value
193 (if (and matlabp org-babel-matlab-with-emacs-link)
194 (concat
195 (format org-babel-matlab-emacs-link-wrapper-method
196 body
197 (org-babel-process-file-name tmp-file 'noquote)
198 (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
199 (mapconcat
200 #'org-babel-chomp
201 (list (format org-babel-octave-wrapper-method
202 body
203 (org-babel-process-file-name tmp-file 'noquote)
204 (org-babel-process-file-name tmp-file 'noquote))
205 org-babel-octave-eoe-indicator) "\n")))))
206 (raw (if (and matlabp org-babel-matlab-with-emacs-link)
207 (save-window-excursion
208 (with-temp-buffer
209 (insert full-body)
210 (write-region "" 'ignored wait-file nil nil nil 'excl)
211 (matlab-shell-run-region (point-min) (point-max))
212 (message "Waiting for Matlab Emacs Link")
213 (while (file-exists-p wait-file) (sit-for 0.01))
214 "")) ;; matlab-shell-run-region doesn't seem to
215 ;; make *matlab* buffer contents easily
216 ;; available, so :results output currently
217 ;; won't work
218 (org-babel-comint-with-output
219 (session
220 (if matlabp
221 org-babel-octave-eoe-indicator
222 org-babel-octave-eoe-output)
223 t full-body)
224 (insert full-body) (comint-send-input nil t)))) results)
225 (case result-type
226 (value
227 (org-babel-octave-import-elisp-from-file tmp-file))
228 (output
229 (progn
230 (setq results
231 (if matlabp
232 (cdr (reverse (delq "" (mapcar
233 #'org-babel-octave-read-string
234 (mapcar #'org-babel-trim raw)))))
235 (cdr (member org-babel-octave-eoe-output
236 (reverse (mapcar
237 #'org-babel-octave-read-string
238 (mapcar #'org-babel-trim raw)))))))
239 (mapconcat #'identity (reverse results) "\n"))))))
241 (defun org-babel-octave-import-elisp-from-file (file-name)
242 "Import data from FILE-NAME.
243 This removes initial blank and comment lines and then calls
244 `org-babel-import-elisp-from-file'."
245 (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
246 (with-temp-file temp-file
247 (insert-file-contents file-name)
248 (re-search-forward "^[ \t]*[^# \t]" nil t)
249 (if (< (setq beg (point-min))
250 (setq end (point-at-bol)))
251 (delete-region beg end)))
252 (org-babel-import-elisp-from-file temp-file '(16))))
254 (defun org-babel-octave-read-string (string)
255 "Strip \\\"s from around octave string"
256 (if (string-match "^\"\\([^\000]+\\)\"$" string)
257 (match-string 1 string)
258 string))
260 (provide 'ob-octave)
262 ;; arch-tag: d8e5f68b-ba13-440a-a495-b653e989e704
264 ;;; ob-octave.el ends here