Fix Org ChangeLog entries and remove arch-tag.
[bpt/emacs.git] / lisp / org / ob-octave.el
CommitLineData
86fbb8ca
CD
1;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
2
3ab2c837 3;; Copyright (C) 2010 Free Software Foundation, Inc.
86fbb8ca
CD
4
5;; Author: Dan Davison
6;; Keywords: literate programming, reproducible research
7;; Homepage: http://orgmode.org
3ab2c837 8;; Version: 7.7
86fbb8ca
CD
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25;;; Commentary:
26
27;;; Requirements:
28
29;; octave
30;; octave-mode.el and octave-inf.el come with GNU emacs
31
32;;; Code:
33(require 'ob)
34(require 'ob-ref)
35(require 'ob-comint)
36(require 'ob-eval)
37(eval-when-compile (require 'cl))
38
39(declare-function matlab-shell "ext:matlab-mode")
40(declare-function matlab-shell-run-region "ext:matlab-mode")
41
42(defvar org-babel-default-header-args:matlab '())
43(defvar org-babel-default-header-args:octave '())
44
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.")
49
86fbb8ca
CD
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.")
54
55(defvar org-babel-matlab-emacs-link-wrapper-method
56 "%s
57if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
58else, save -ascii %s ans
59end
60delete('%s')
61")
62(defvar org-babel-octave-wrapper-method
63 "%s
64if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
afe98dfa 65else, dlmwrite('%s', ans, '\\t')
86fbb8ca
CD
66end")
67
68(defvar org-babel-octave-eoe-indicator "\'org_babel_eoe\'")
69
70(defvar org-babel-octave-eoe-output "ans = org_babel_eoe")
71
72(defun org-babel-execute:matlab (body params)
73 "Execute a block of matlab code with Babel."
86fbb8ca 74 (org-babel-execute:octave body params 'matlab))
afe98dfa 75
86fbb8ca
CD
76(defun org-babel-execute:octave (body params &optional matlabp)
77 "Execute a block of octave code with Babel."
afe98dfa 78 (let* ((session
86fbb8ca
CD
79 (funcall (intern (format "org-babel-%s-initiate-session"
80 (if matlabp "matlab" "octave")))
afe98dfa
CD
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)))
86fbb8ca 85 (out-file (cdr (assoc :file params)))
afe98dfa
CD
86 (full-body
87 (org-babel-expand-body:generic
88 body params (org-babel-variable-assignments:octave params)))
86fbb8ca 89 (result (org-babel-octave-evaluate
afe98dfa 90 session full-body result-type matlabp)))
3ab2c837
BG
91 (org-babel-reassemble-table
92 result
93 (org-babel-pick-name
94 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
95 (org-babel-pick-name
96 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
86fbb8ca
CD
97
98(defun org-babel-prep-session:matlab (session params)
99 "Prepare SESSION according to PARAMS."
86fbb8ca 100 (org-babel-prep-session:octave session params 'matlab))
afe98dfa
CD
101
102(defun org-babel-variable-assignments:octave (params)
103 "Return list of octave statements assigning the block's variables"
104 (mapcar
105 (lambda (pair)
3ab2c837 106 (format "%s=%s;"
afe98dfa
CD
107 (car pair)
108 (org-babel-octave-var-to-octave (cdr pair))))
109 (mapcar #'cdr (org-babel-get-header params :var))))
110
111(defalias 'org-babel-variable-assignments:matlab
112 'org-babel-variable-assignments:octave)
113
86fbb8ca
CD
114(defun org-babel-octave-var-to-octave (var)
115 "Convert an emacs-lisp value into an octave variable.
116Converts an emacs-lisp variable into a string of octave code
117specifying a variable of the same value."
118 (if (listp var)
afe98dfa
CD
119 (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
120 (if (listp (car var)) "; " ",")) "]")
121 (format "%s" (or var "nil"))))
86fbb8ca
CD
122
123(defun org-babel-prep-session:octave (session params &optional matlabp)
124 "Prepare SESSION according to the header arguments specified in PARAMS."
125 (let* ((session (org-babel-octave-initiate-session session params matlabp))
afe98dfa 126 (var-lines (org-babel-variable-assignments:octave params)))
86fbb8ca
CD
127 (org-babel-comint-in-buffer session
128 (mapc (lambda (var)
129 (end-of-line 1) (insert var) (comint-send-input nil t)
130 (org-babel-comint-wait-for-output session)) var-lines))
131 session))
132
133(defun org-babel-matlab-initiate-session (&optional session params)
134 "Create a matlab inferior process buffer.
135If there is not a current inferior-process-buffer in SESSION then
136create. Return the initialized session."
86fbb8ca 137 (org-babel-octave-initiate-session session params 'matlab))
afe98dfa 138
86fbb8ca
CD
139(defun org-babel-octave-initiate-session (&optional session params matlabp)
140 "Create an octave inferior process buffer.
141If there is not a current inferior-process-buffer in SESSION then
142create. Return the initialized session."
afe98dfa 143 (if matlabp (require 'matlab) (require 'octave-inf))
86fbb8ca
CD
144 (unless (string= session "none")
145 (let ((session (or session
146 (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
147 (if (org-babel-comint-buffer-livep session) session
148 (save-window-excursion
149 (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
150 (run-octave))
151 (rename-buffer (if (bufferp session) (buffer-name session)
152 (if (stringp session) session (buffer-name))))
153 (current-buffer))))))
154
155(defun org-babel-octave-evaluate
afe98dfa 156 (session body result-type &optional matlabp)
86fbb8ca
CD
157 "Pass BODY to the octave process in SESSION.
158If RESULT-TYPE equals 'output then return the outputs of the
159statements in BODY, if RESULT-TYPE equals 'value then return the
160value of the last statement in BODY, as elisp."
161 (if session
162 (org-babel-octave-evaluate-session session body result-type matlabp)
163 (org-babel-octave-evaluate-external-process body result-type matlabp)))
164
165(defun org-babel-octave-evaluate-external-process (body result-type matlabp)
166 "Evaluate BODY in an external octave process."
167 (let ((cmd (if matlabp
168 org-babel-matlab-shell-command
169 org-babel-octave-shell-command)))
170 (case result-type
171 (output (org-babel-eval cmd body))
afe98dfa 172 (value (let ((tmp-file (org-babel-temp-file "octave-")))
86fbb8ca
CD
173 (org-babel-eval
174 cmd
afe98dfa
CD
175 (format org-babel-octave-wrapper-method body
176 (org-babel-process-file-name tmp-file 'noquote)
177 (org-babel-process-file-name tmp-file 'noquote)))
178 (org-babel-octave-import-elisp-from-file tmp-file))))))
86fbb8ca
CD
179
180(defun org-babel-octave-evaluate-session
181 (session body result-type &optional matlabp)
182 "Evaluate BODY in SESSION."
afe98dfa
CD
183 (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
184 (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
86fbb8ca
CD
185 (full-body
186 (case result-type
187 (output
188 (mapconcat
189 #'org-babel-chomp
190 (list body org-babel-octave-eoe-indicator) "\n"))
191 (value
192 (if (and matlabp org-babel-matlab-with-emacs-link)
193 (concat
194 (format org-babel-matlab-emacs-link-wrapper-method
afe98dfa
CD
195 body
196 (org-babel-process-file-name tmp-file 'noquote)
197 (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
86fbb8ca
CD
198 (mapconcat
199 #'org-babel-chomp
200 (list (format org-babel-octave-wrapper-method
afe98dfa
CD
201 body
202 (org-babel-process-file-name tmp-file 'noquote)
203 (org-babel-process-file-name tmp-file 'noquote))
86fbb8ca
CD
204 org-babel-octave-eoe-indicator) "\n")))))
205 (raw (if (and matlabp org-babel-matlab-with-emacs-link)
206 (save-window-excursion
207 (with-temp-buffer
208 (insert full-body)
209 (write-region "" 'ignored wait-file nil nil nil 'excl)
210 (matlab-shell-run-region (point-min) (point-max))
211 (message "Waiting for Matlab Emacs Link")
212 (while (file-exists-p wait-file) (sit-for 0.01))
213 "")) ;; matlab-shell-run-region doesn't seem to
214 ;; make *matlab* buffer contents easily
215 ;; available, so :results output currently
216 ;; won't work
217 (org-babel-comint-with-output
218 (session
219 (if matlabp
220 org-babel-octave-eoe-indicator
221 org-babel-octave-eoe-output)
222 t full-body)
223 (insert full-body) (comint-send-input nil t)))) results)
224 (case result-type
225 (value
afe98dfa 226 (org-babel-octave-import-elisp-from-file tmp-file))
86fbb8ca
CD
227 (output
228 (progn
229 (setq results
230 (if matlabp
231 (cdr (reverse (delq "" (mapcar
232 #'org-babel-octave-read-string
233 (mapcar #'org-babel-trim raw)))))
234 (cdr (member org-babel-octave-eoe-output
235 (reverse (mapcar
236 #'org-babel-octave-read-string
237 (mapcar #'org-babel-trim raw)))))))
238 (mapconcat #'identity (reverse results) "\n"))))))
239
240(defun org-babel-octave-import-elisp-from-file (file-name)
241 "Import data from FILE-NAME.
242This removes initial blank and comment lines and then calls
243`org-babel-import-elisp-from-file'."
afe98dfa 244 (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
86fbb8ca
CD
245 (with-temp-file temp-file
246 (insert-file-contents file-name)
247 (re-search-forward "^[ \t]*[^# \t]" nil t)
248 (if (< (setq beg (point-min))
249 (setq end (point-at-bol)))
250 (delete-region beg end)))
afe98dfa 251 (org-babel-import-elisp-from-file temp-file '(16))))
86fbb8ca
CD
252
253(defun org-babel-octave-read-string (string)
254 "Strip \\\"s from around octave string"
255 (if (string-match "^\"\\([^\000]+\\)\"$" string)
256 (match-string 1 string)
257 string))
258
259(provide 'ob-octave)
260
5b409b39 261
86fbb8ca
CD
262
263;;; ob-octave.el ends here