org-exp.el (org-export-push-to-kill-ring): Minor fix to docstring
[org-mode.git] / lisp / ob-octave.el
blob3d9095483c07853df3a0938f3ef5998ac22989bf
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 (format "%s" (or var "nil"))))
122 (defun org-babel-prep-session:octave (session params &optional matlabp)
123 "Prepare SESSION according to the header arguments specified in PARAMS."
124 (let* ((session (org-babel-octave-initiate-session session params matlabp))
125 (var-lines (org-babel-variable-assignments:octave params)))
126 (org-babel-comint-in-buffer session
127 (mapc (lambda (var)
128 (end-of-line 1) (insert var) (comint-send-input nil t)
129 (org-babel-comint-wait-for-output session)) var-lines))
130 session))
132 (defun org-babel-matlab-initiate-session (&optional session params)
133 "Create a matlab inferior process buffer.
134 If there is not a current inferior-process-buffer in SESSION then
135 create. Return the initialized session."
136 (org-babel-octave-initiate-session session params 'matlab))
138 (defun org-babel-octave-initiate-session (&optional session params matlabp)
139 "Create an octave inferior process buffer.
140 If there is not a current inferior-process-buffer in SESSION then
141 create. Return the initialized session."
142 (if matlabp (require 'matlab) (require 'octave-inf))
143 (unless (string= session "none")
144 (let ((session (or session
145 (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
146 (if (org-babel-comint-buffer-livep session) session
147 (save-window-excursion
148 (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
149 (run-octave))
150 (rename-buffer (if (bufferp session) (buffer-name session)
151 (if (stringp session) session (buffer-name))))
152 (current-buffer))))))
154 (defun org-babel-octave-evaluate
155 (session body result-type &optional matlabp)
156 "Pass BODY to the octave process in SESSION.
157 If RESULT-TYPE equals 'output then return the outputs of the
158 statements in BODY, if RESULT-TYPE equals 'value then return the
159 value of the last statement in BODY, as elisp."
160 (if session
161 (org-babel-octave-evaluate-session session body result-type matlabp)
162 (org-babel-octave-evaluate-external-process body result-type matlabp)))
164 (defun org-babel-octave-evaluate-external-process (body result-type matlabp)
165 "Evaluate BODY in an external octave process."
166 (let ((cmd (if matlabp
167 org-babel-matlab-shell-command
168 org-babel-octave-shell-command)))
169 (case result-type
170 (output (org-babel-eval cmd body))
171 (value (let ((tmp-file (org-babel-temp-file "octave-")))
172 (org-babel-eval
174 (format org-babel-octave-wrapper-method body
175 (org-babel-process-file-name tmp-file 'noquote)
176 (org-babel-process-file-name tmp-file 'noquote)))
177 (org-babel-octave-import-elisp-from-file tmp-file))))))
179 (defun org-babel-octave-evaluate-session
180 (session body result-type &optional matlabp)
181 "Evaluate BODY in SESSION."
182 (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
183 (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
184 (full-body
185 (case result-type
186 (output
187 (mapconcat
188 #'org-babel-chomp
189 (list body org-babel-octave-eoe-indicator) "\n"))
190 (value
191 (if (and matlabp org-babel-matlab-with-emacs-link)
192 (concat
193 (format org-babel-matlab-emacs-link-wrapper-method
194 body
195 (org-babel-process-file-name tmp-file 'noquote)
196 (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
197 (mapconcat
198 #'org-babel-chomp
199 (list (format org-babel-octave-wrapper-method
200 body
201 (org-babel-process-file-name tmp-file 'noquote)
202 (org-babel-process-file-name tmp-file 'noquote))
203 org-babel-octave-eoe-indicator) "\n")))))
204 (raw (if (and matlabp org-babel-matlab-with-emacs-link)
205 (save-window-excursion
206 (with-temp-buffer
207 (insert full-body)
208 (write-region "" 'ignored wait-file nil nil nil 'excl)
209 (matlab-shell-run-region (point-min) (point-max))
210 (message "Waiting for Matlab Emacs Link")
211 (while (file-exists-p wait-file) (sit-for 0.01))
212 "")) ;; matlab-shell-run-region doesn't seem to
213 ;; make *matlab* buffer contents easily
214 ;; available, so :results output currently
215 ;; won't work
216 (org-babel-comint-with-output
217 (session
218 (if matlabp
219 org-babel-octave-eoe-indicator
220 org-babel-octave-eoe-output)
221 t full-body)
222 (insert full-body) (comint-send-input nil t)))) results)
223 (case result-type
224 (value
225 (org-babel-octave-import-elisp-from-file tmp-file))
226 (output
227 (progn
228 (setq results
229 (if matlabp
230 (cdr (reverse (delq "" (mapcar
231 #'org-babel-octave-read-string
232 (mapcar #'org-babel-trim raw)))))
233 (cdr (member org-babel-octave-eoe-output
234 (reverse (mapcar
235 #'org-babel-octave-read-string
236 (mapcar #'org-babel-trim raw)))))))
237 (mapconcat #'identity (reverse results) "\n"))))))
239 (defun org-babel-octave-import-elisp-from-file (file-name)
240 "Import data from FILE-NAME.
241 This removes initial blank and comment lines and then calls
242 `org-babel-import-elisp-from-file'."
243 (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
244 (with-temp-file temp-file
245 (insert-file-contents file-name)
246 (re-search-forward "^[ \t]*[^# \t]" nil t)
247 (if (< (setq beg (point-min))
248 (setq end (point-at-bol)))
249 (delete-region beg end)))
250 (org-babel-import-elisp-from-file temp-file '(16))))
252 (defun org-babel-octave-read-string (string)
253 "Strip \\\"s from around octave string"
254 (if (string-match "^\"\\([^\000]+\\)\"$" string)
255 (match-string 1 string)
256 string))
258 (provide 'ob-octave)
262 ;;; ob-octave.el ends here