1 ;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
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/>.
30 ;; octave-mode.el and octave-inf.el come with GNU emacs
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
55 (defvar org-babel-matlab-emacs-link-wrapper-method
57 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
58 else, save -ascii %s ans
62 (defvar org-babel-octave-wrapper-method
64 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
65 else, dlmwrite('%s', ans, '\\t')
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."
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
)))
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
)))
92 (org-babel-reassemble-table
95 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
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"
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."
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
130 (end-of-line 1) (insert var
) (comint-send-input nil t
)
131 (org-babel-comint-wait-for-output session
)) var-lines
))
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))
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."
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
)))
172 (output (org-babel-eval cmd body
))
173 (value (let ((tmp-file (org-babel-temp-file "octave-")))
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-"))
191 (list body org-babel-octave-eoe-indicator
) "\n"))
193 (if (and matlabp org-babel-matlab-with-emacs-link
)
195 (format org-babel-matlab-emacs-link-wrapper-method
197 (org-babel-process-file-name tmp-file
'noquote
)
198 (org-babel-process-file-name tmp-file
'noquote
) wait-file
) "\n")
201 (list (format org-babel-octave-wrapper-method
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
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
218 (org-babel-comint-with-output
221 org-babel-octave-eoe-indicator
222 org-babel-octave-eoe-output
)
224 (insert full-body
) (comint-send-input nil t
)))) results
)
227 (org-babel-octave-import-elisp-from-file tmp-file
))
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
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
)
263 ;;; ob-octave.el ends here