Unescape functions moved and renamed from org-protocol.el
[org-mode.git] / lisp / ob-octave.el
blob92f16beca7d91709c1062a01ba0daefd9b17af55
1 ;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.4
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/>.
25 ;;; Commentary:
27 ;;; Requirements:
29 ;; octave
30 ;; octave-mode.el and octave-inf.el come with GNU emacs
32 ;;; Code:
33 (require 'ob)
34 (require 'ob-ref)
35 (require 'ob-comint)
36 (require 'ob-eval)
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
53 to a non-nil value.")
55 (defvar org-babel-matlab-emacs-link-wrapper-method
56 "%s
57 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
58 else, save -ascii %s ans
59 end
60 delete('%s')
62 (defvar org-babel-octave-wrapper-method
63 "%s
64 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
65 else, dlmwrite('%s', ans, '\\t')
66 end")
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."
78 (let* ((session
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)))
86 (full-body
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)))
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))))))
98 (defun org-babel-prep-session:matlab (session params)
99 "Prepare SESSION according to PARAMS."
100 (org-babel-prep-session:octave session params 'matlab))
102 (defun org-babel-variable-assignments:octave (params)
103 "Return list of octave statements assigning the block's variables"
104 (mapcar
105 (lambda (pair)
106 (format "%s=%s"
107 (car pair)
108 (org-babel-octave-var-to-octave (cdr pair))))
109 (mapcar #'cdr (org-babel-get-header params :var))))
111 (defalias 'org-babel-variable-assignments:matlab
112 'org-babel-variable-assignments:octave)
114 (defun org-babel-octave-var-to-octave (var)
115 "Convert an emacs-lisp value into an octave variable.
116 Converts an emacs-lisp variable into a string of octave code
117 specifying a variable of the same value."
118 (if (listp var)
119 (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
120 (if (listp (car var)) "; " ",")) "]")
121 (format "%s" (or var "nil"))))
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))
126 (var-lines (org-babel-variable-assignments:octave params)))
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))
133 (defun org-babel-matlab-initiate-session (&optional session params)
134 "Create a matlab inferior process buffer.
135 If there is not a current inferior-process-buffer in SESSION then
136 create. Return the initialized session."
137 (org-babel-octave-initiate-session session params 'matlab))
139 (defun org-babel-octave-initiate-session (&optional session params matlabp)
140 "Create an octave inferior process buffer.
141 If there is not a current inferior-process-buffer in SESSION then
142 create. Return the initialized session."
143 (if matlabp (require 'matlab) (require 'octave-inf))
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))))))
155 (defun org-babel-octave-evaluate
156 (session body result-type &optional matlabp)
157 "Pass BODY to the octave process in SESSION.
158 If RESULT-TYPE equals 'output then return the outputs of the
159 statements in BODY, if RESULT-TYPE equals 'value then return the
160 value 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)))
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))
172 (value (let ((tmp-file (org-babel-temp-file "octave-")))
173 (org-babel-eval
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))))))
180 (defun org-babel-octave-evaluate-session
181 (session body result-type &optional matlabp)
182 "Evaluate BODY in SESSION."
183 (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
184 (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
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
195 body
196 (org-babel-process-file-name tmp-file 'noquote)
197 (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
198 (mapconcat
199 #'org-babel-chomp
200 (list (format org-babel-octave-wrapper-method
201 body
202 (org-babel-process-file-name tmp-file 'noquote)
203 (org-babel-process-file-name tmp-file 'noquote))
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
226 (org-babel-octave-import-elisp-from-file tmp-file))
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"))))))
240 (defun org-babel-octave-import-elisp-from-file (file-name)
241 "Import data from FILE-NAME.
242 This removes initial blank and comment lines and then calls
243 `org-babel-import-elisp-from-file'."
244 (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
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)))
251 (org-babel-import-elisp-from-file temp-file '(16))))
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))
259 (provide 'ob-octave)
261 ;; arch-tag: d8e5f68b-ba13-440a-a495-b653e989e704
263 ;;; ob-octave.el ends here