1 ;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
3 ;; Copyright (C) 2010 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 (defun org-babel-expand-body:matlab
(body params
&optional processed-params
)
51 "Expand BODY according to PARAMS, return the expanded body."
52 (org-babel-expand-body:octave body params processed-params
))
53 (defun org-babel-expand-body:octave
(body params
&optional processed-params
)
54 "Expand BODY according to PARAMS, return the expanded body."
55 (let ((vars (nth 1 (or processed-params
(org-babel-process-params params
)))))
61 (org-babel-octave-var-to-octave (cdr pair
))))
62 vars
"\n") "\n" body
"\n")))
64 (defvar org-babel-matlab-with-emacs-link nil
65 "If non-nil use matlab-shell-run-region for session evaluation.
66 This will use EmacsLink if (matlab-with-emacs-link) evaluates
69 (defvar org-babel-matlab-emacs-link-wrapper-method
71 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
72 else, save -ascii %s ans
76 (defvar org-babel-octave-wrapper-method
78 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
79 else, save -ascii %s ans
82 (defvar org-babel-octave-eoe-indicator
"\'org_babel_eoe\'")
84 (defvar org-babel-octave-eoe-output
"ans = org_babel_eoe")
86 (defun org-babel-execute:matlab
(body params
)
87 "Execute a block of matlab code with Babel."
89 (org-babel-execute:octave body params
'matlab
))
90 (defun org-babel-execute:octave
(body params
&optional matlabp
)
91 "Execute a block of octave code with Babel."
92 (let* ((processed-params (org-babel-process-params params
))
94 (funcall (intern (format "org-babel-%s-initiate-session"
95 (if matlabp
"matlab" "octave")))
96 (nth 0 processed-params
) params
))
97 (vars (nth 1 processed-params
))
98 (result-params (nth 2 processed-params
))
99 (result-type (nth 3 processed-params
))
100 (out-file (cdr (assoc :file params
)))
102 (org-babel-expand-body:octave body params processed-params
))
103 (result (org-babel-octave-evaluate
104 session augmented-body result-type matlabp
)))
106 (org-babel-reassemble-table
109 (nth 4 processed-params
) (cdr (assoc :colnames params
)))
111 (nth 5 processed-params
) (cdr (assoc :rownames params
)))))))
113 (defun org-babel-prep-session:matlab
(session params
)
114 "Prepare SESSION according to PARAMS."
116 (org-babel-prep-session:octave session params
'matlab
))
117 (defun org-babel-octave-var-to-octave (var)
118 "Convert an emacs-lisp value into an octave variable.
119 Converts an emacs-lisp variable into a string of octave code
120 specifying a variable of the same value."
122 (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
", ") "]")
125 (defun org-babel-prep-session:octave
(session params
&optional matlabp
)
126 "Prepare SESSION according to the header arguments specified in PARAMS."
127 (let* ((session (org-babel-octave-initiate-session session params matlabp
))
128 (vars (org-babel-ref-variables params
))
133 (org-babel-octave-var-to-octave (cdr pair
))))
135 (org-babel-comint-in-buffer session
137 (end-of-line 1) (insert var
) (comint-send-input nil t
)
138 (org-babel-comint-wait-for-output session
)) var-lines
))
141 (defun org-babel-matlab-initiate-session (&optional session params
)
142 "Create a matlab inferior process buffer.
143 If there is not a current inferior-process-buffer in SESSION then
144 create. Return the initialized session."
146 (org-babel-octave-initiate-session session params
'matlab
))
147 (defun org-babel-octave-initiate-session (&optional session params matlabp
)
148 "Create an octave inferior process buffer.
149 If there is not a current inferior-process-buffer in SESSION then
150 create. Return the initialized session."
151 (require 'octave-inf
)
152 (unless (string= session
"none")
153 (let ((session (or session
154 (if matlabp
"*Inferior Matlab*" "*Inferior Octave*"))))
155 (if (org-babel-comint-buffer-livep session
) session
156 (save-window-excursion
157 (if matlabp
(unless org-babel-matlab-with-emacs-link
(matlab-shell))
159 (rename-buffer (if (bufferp session
) (buffer-name session
)
160 (if (stringp session
) session
(buffer-name))))
161 (current-buffer))))))
163 (defun org-babel-octave-evaluate
164 (session body result-type lang
&optional matlabp
)
165 "Pass BODY to the octave process in SESSION.
166 If RESULT-TYPE equals 'output then return the outputs of the
167 statements in BODY, if RESULT-TYPE equals 'value then return the
168 value of the last statement in BODY, as elisp."
170 (org-babel-octave-evaluate-session session body result-type matlabp
)
171 (org-babel-octave-evaluate-external-process body result-type matlabp
)))
173 (defun org-babel-octave-evaluate-external-process (body result-type matlabp
)
174 "Evaluate BODY in an external octave process."
175 (let ((cmd (if matlabp
176 org-babel-matlab-shell-command
177 org-babel-octave-shell-command
)))
179 (output (org-babel-eval cmd body
))
180 (value (let ((tmp-file (make-temp-file "org-babel-results-")))
183 (format org-babel-octave-wrapper-method body tmp-file tmp-file
))
184 (org-babel-eval-read-file tmp-file
))))))
186 (defun org-babel-octave-evaluate-session
187 (session body result-type
&optional matlabp
)
188 "Evaluate BODY in SESSION."
189 (let* ((tmp-file (make-temp-file "org-babel-results-"))
190 (wait-file (make-temp-file "org-babel-matlab-emacs-link-wait-signal-"))
196 (list body org-babel-octave-eoe-indicator
) "\n"))
198 (if (and matlabp org-babel-matlab-with-emacs-link
)
200 (format org-babel-matlab-emacs-link-wrapper-method
201 body tmp-file tmp-file wait-file
) "\n")
204 (list (format org-babel-octave-wrapper-method
205 body tmp-file tmp-file
)
206 org-babel-octave-eoe-indicator
) "\n")))))
207 (raw (if (and matlabp org-babel-matlab-with-emacs-link
)
208 (save-window-excursion
211 (write-region "" 'ignored wait-file nil nil nil
'excl
)
212 (matlab-shell-run-region (point-min) (point-max))
213 (message "Waiting for Matlab Emacs Link")
214 (while (file-exists-p wait-file
) (sit-for 0.01))
215 "")) ;; matlab-shell-run-region doesn't seem to
216 ;; make *matlab* buffer contents easily
217 ;; available, so :results output currently
219 (org-babel-comint-with-output
222 org-babel-octave-eoe-indicator
223 org-babel-octave-eoe-output
)
225 (insert full-body
) (comint-send-input nil t
)))) results
)
228 (org-babel-octave-import-elisp-from-file
229 (org-babel-maybe-remote-file tmp-file
)))
234 (cdr (reverse (delq "" (mapcar
235 #'org-babel-octave-read-string
236 (mapcar #'org-babel-trim raw
)))))
237 (cdr (member org-babel-octave-eoe-output
239 #'org-babel-octave-read-string
240 (mapcar #'org-babel-trim raw
)))))))
241 (mapconcat #'identity
(reverse results
) "\n"))))))
243 (defun org-babel-octave-import-elisp-from-file (file-name)
244 "Import data from FILE-NAME.
245 This removes initial blank and comment lines and then calls
246 `org-babel-import-elisp-from-file'."
247 (let ((temp-file (make-temp-file "org-babel-results-")) beg end
)
248 (with-temp-file temp-file
249 (insert-file-contents file-name
)
250 (re-search-forward "^[ \t]*[^# \t]" nil t
)
251 (if (< (setq beg
(point-min))
252 (setq end
(point-at-bol)))
253 (delete-region beg end
)))
254 (org-babel-import-elisp-from-file temp-file
)))
256 (defun org-babel-octave-read-string (string)
257 "Strip \\\"s from around octave string"
258 (if (string-match "^\"\\([^\000]+\\)\"$" string
)
259 (match-string 1 string
)
264 ;; arch-tag: d8e5f68b-ba13-440a-a495-b653e989e704
266 ;;; ob-octave.el ends here