Use the default face instead of fancy colors.
[org-mode.git] / lisp / ob-octave.el
blobf8407397801557fcd0fbf660f540192484026458
1 ;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
3 ;; Copyright (C) 2010-2011 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 (require 'ob-ref)
34 (require 'ob-comint)
35 (require 'ob-eval)
36 (eval-when-compile (require 'cl))
38 (declare-function matlab-shell "ext:matlab-mode")
39 (declare-function matlab-shell-run-region "ext:matlab-mode")
41 (defvar org-babel-default-header-args:matlab '())
42 (defvar org-babel-default-header-args:octave '())
44 (defvar org-babel-matlab-shell-command "matlab -nosplash"
45 "Shell command to run matlab as an external process.")
46 (defvar org-babel-octave-shell-command "octave -q"
47 "Shell command to run octave as an external process.")
49 (defvar org-babel-matlab-with-emacs-link nil
50 "If non-nil use matlab-shell-run-region for session evaluation.
51 This will use EmacsLink if (matlab-with-emacs-link) evaluates
52 to a non-nil value.")
54 (defvar org-babel-matlab-emacs-link-wrapper-method
55 "%s
56 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
57 else, save -ascii %s ans
58 end
59 delete('%s')
61 (defvar org-babel-octave-wrapper-method
62 "%s
63 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
64 else, dlmwrite('%s', ans, '\\t')
65 end")
67 (defvar org-babel-octave-eoe-indicator "\'org_babel_eoe\'")
69 (defvar org-babel-octave-eoe-output "ans = org_babel_eoe")
71 (defun org-babel-execute:matlab (body params)
72 "Execute a block of matlab code with Babel."
73 (org-babel-execute:octave body params 'matlab))
75 (defun org-babel-execute:octave (body params &optional matlabp)
76 "Execute a block of octave code with Babel."
77 (let* ((session
78 (funcall (intern (format "org-babel-%s-initiate-session"
79 (if matlabp "matlab" "octave")))
80 (cdr (assoc :session params)) params))
81 (vars (mapcar #'cdr (org-babel-get-header params :var)))
82 (result-params (cdr (assoc :result-params params)))
83 (result-type (cdr (assoc :result-type params)))
84 (out-file (cdr (assoc :file params)))
85 (full-body
86 (org-babel-expand-body:generic
87 body params (org-babel-variable-assignments:octave params)))
88 (result (org-babel-octave-evaluate
89 session full-body result-type matlabp)))
90 (org-babel-reassemble-table
91 result
92 (org-babel-pick-name
93 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
94 (org-babel-pick-name
95 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
97 (defun org-babel-prep-session:matlab (session params)
98 "Prepare SESSION according to PARAMS."
99 (org-babel-prep-session:octave session params 'matlab))
101 (defun org-babel-variable-assignments:octave (params)
102 "Return list of octave statements assigning the block's variables"
103 (mapcar
104 (lambda (pair)
105 (format "%s=%s;"
106 (car pair)
107 (org-babel-octave-var-to-octave (cdr pair))))
108 (mapcar #'cdr (org-babel-get-header params :var))))
110 (defalias 'org-babel-variable-assignments:matlab
111 'org-babel-variable-assignments:octave)
113 (defun org-babel-octave-var-to-octave (var)
114 "Convert an emacs-lisp value into an octave variable.
115 Converts an emacs-lisp variable into a string of octave code
116 specifying a variable of the same value."
117 (if (listp var)
118 (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
119 (if (listp (car var)) "; " ",")) "]")
120 (cond
121 ((stringp var)
122 (format "\'%s\'" var))
124 (format "%s" var)))))
126 (defun org-babel-prep-session:octave (session params &optional matlabp)
127 "Prepare SESSION according to the header arguments specified in PARAMS."
128 (let* ((session (org-babel-octave-initiate-session session params matlabp))
129 (var-lines (org-babel-variable-assignments:octave params)))
130 (org-babel-comint-in-buffer session
131 (mapc (lambda (var)
132 (end-of-line 1) (insert var) (comint-send-input nil t)
133 (org-babel-comint-wait-for-output session)) var-lines))
134 session))
136 (defun org-babel-matlab-initiate-session (&optional session params)
137 "Create a matlab inferior process buffer.
138 If there is not a current inferior-process-buffer in SESSION then
139 create. Return the initialized session."
140 (org-babel-octave-initiate-session session params 'matlab))
142 (defun org-babel-octave-initiate-session (&optional session params matlabp)
143 "Create an octave inferior process buffer.
144 If there is not a current inferior-process-buffer in SESSION then
145 create. Return the initialized session."
146 (if matlabp (require 'matlab) (require 'octave-inf))
147 (unless (string= session "none")
148 (let ((session (or session
149 (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
150 (if (org-babel-comint-buffer-livep session) session
151 (save-window-excursion
152 (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
153 (run-octave))
154 (rename-buffer (if (bufferp session) (buffer-name session)
155 (if (stringp session) session (buffer-name))))
156 (current-buffer))))))
158 (defun org-babel-octave-evaluate
159 (session body result-type &optional matlabp)
160 "Pass BODY to the octave process in SESSION.
161 If RESULT-TYPE equals 'output then return the outputs of the
162 statements in BODY, if RESULT-TYPE equals 'value then return the
163 value of the last statement in BODY, as elisp."
164 (if session
165 (org-babel-octave-evaluate-session session body result-type matlabp)
166 (org-babel-octave-evaluate-external-process body result-type matlabp)))
168 (defun org-babel-octave-evaluate-external-process (body result-type matlabp)
169 "Evaluate BODY in an external octave process."
170 (let ((cmd (if matlabp
171 org-babel-matlab-shell-command
172 org-babel-octave-shell-command)))
173 (case result-type
174 (output (org-babel-eval cmd body))
175 (value (let ((tmp-file (org-babel-temp-file "octave-")))
176 (org-babel-eval
178 (format org-babel-octave-wrapper-method body
179 (org-babel-process-file-name tmp-file 'noquote)
180 (org-babel-process-file-name tmp-file 'noquote)))
181 (org-babel-octave-import-elisp-from-file tmp-file))))))
183 (defun org-babel-octave-evaluate-session
184 (session body result-type &optional matlabp)
185 "Evaluate BODY in SESSION."
186 (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
187 (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
188 (full-body
189 (case result-type
190 (output
191 (mapconcat
192 #'org-babel-chomp
193 (list body org-babel-octave-eoe-indicator) "\n"))
194 (value
195 (if (and matlabp org-babel-matlab-with-emacs-link)
196 (concat
197 (format org-babel-matlab-emacs-link-wrapper-method
198 body
199 (org-babel-process-file-name tmp-file 'noquote)
200 (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
201 (mapconcat
202 #'org-babel-chomp
203 (list (format org-babel-octave-wrapper-method
204 body
205 (org-babel-process-file-name tmp-file 'noquote)
206 (org-babel-process-file-name tmp-file 'noquote))
207 org-babel-octave-eoe-indicator) "\n")))))
208 (raw (if (and matlabp org-babel-matlab-with-emacs-link)
209 (save-window-excursion
210 (with-temp-buffer
211 (insert full-body)
212 (write-region "" 'ignored wait-file nil nil nil 'excl)
213 (matlab-shell-run-region (point-min) (point-max))
214 (message "Waiting for Matlab Emacs Link")
215 (while (file-exists-p wait-file) (sit-for 0.01))
216 "")) ;; matlab-shell-run-region doesn't seem to
217 ;; make *matlab* buffer contents easily
218 ;; available, so :results output currently
219 ;; won't work
220 (org-babel-comint-with-output
221 (session
222 (if matlabp
223 org-babel-octave-eoe-indicator
224 org-babel-octave-eoe-output)
225 t full-body)
226 (insert full-body) (comint-send-input nil t)))) results)
227 (case result-type
228 (value
229 (org-babel-octave-import-elisp-from-file tmp-file))
230 (output
231 (progn
232 (setq results
233 (if matlabp
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
238 (reverse (mapcar
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 (org-babel-temp-file "octave-matlab-")) 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 '(16))))
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)
260 string))
262 (provide 'ob-octave)
266 ;;; ob-octave.el ends here