1 ;;; ob-octave.el --- Babel Functions for Octave and Matlab -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2016 Free Software Foundation, Inc.
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/>.
29 ;; octave-mode.el and octave-inf.el come with GNU emacs
33 (eval-when-compile (require 'cl
))
35 (declare-function matlab-shell
"ext:matlab-mode")
36 (declare-function matlab-shell-run-region
"ext:matlab-mode")
37 (declare-function org-trim
"org" (s &optional keep-lead
))
39 (defvar org-babel-default-header-args
:matlab
'())
40 (defvar org-babel-default-header-args
:octave
'())
42 (defvar org-babel-matlab-shell-command
"matlab -nosplash"
43 "Shell command to run matlab as an external process.")
44 (defvar org-babel-octave-shell-command
"octave -q"
45 "Shell command to run octave as an external process.")
47 (defvar org-babel-matlab-with-emacs-link nil
48 "If non-nil use matlab-shell-run-region for session evaluation.
49 This will use EmacsLink if (matlab-with-emacs-link) evaluates
52 (defvar org-babel-matlab-emacs-link-wrapper-method
54 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
55 else, save -ascii %s ans
59 (defvar org-babel-octave-wrapper-method
61 if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
62 else, dlmwrite('%s', ans, '\\t')
65 (defvar org-babel-octave-eoe-indicator
"'org_babel_eoe'")
67 (defvar org-babel-octave-eoe-output
"ans = org_babel_eoe")
69 (defun org-babel-execute:matlab
(body params
)
70 "Execute a block of matlab code with Babel."
71 (org-babel-execute:octave body params
'matlab
))
73 (defun org-babel-execute:octave
(body params
&optional matlabp
)
74 "Execute a block of octave code with Babel."
76 (funcall (intern (format "org-babel-%s-initiate-session"
77 (if matlabp
"matlab" "octave")))
78 (cdr (assoc :session params
)) params
))
79 (result-type (cdr (assoc :result-type params
)))
81 (org-babel-expand-body:generic
82 body params
(org-babel-variable-assignments:octave params
)))
83 (gfx-file (ignore-errors (org-babel-graphical-output-file params
)))
84 (result (org-babel-octave-evaluate
89 "set (0, \"defaultfigurevisible\", \"off\");"
91 (format "print -dpng %s" gfx-file
))
94 result-type matlabp
)))
97 (org-babel-reassemble-table
100 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
102 (cdr (assoc :rowname-names params
)) (cdr (assoc :rownames params
)))))))
104 (defun org-babel-prep-session:matlab
(session params
)
105 "Prepare SESSION according to PARAMS."
106 (org-babel-prep-session:octave session params
'matlab
))
108 (defun org-babel-variable-assignments:octave
(params)
109 "Return list of octave statements assigning the block's variables."
114 (org-babel-octave-var-to-octave (cdr pair
))))
115 (org-babel--get-vars params
)))
117 (defalias 'org-babel-variable-assignments
:matlab
118 'org-babel-variable-assignments
:octave
)
120 (defun org-babel-octave-var-to-octave (var)
121 "Convert an emacs-lisp value into an octave variable.
122 Converts an emacs-lisp variable into a string of octave code
123 specifying a variable of the same value."
125 (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
126 (if (listp (car var
)) "; " ",")) "]")
131 (format "%s" var
)))))
133 (defun org-babel-prep-session:octave
(session params
&optional matlabp
)
134 "Prepare SESSION according to the header arguments specified in PARAMS."
135 (let* ((session (org-babel-octave-initiate-session session params matlabp
))
136 (var-lines (org-babel-variable-assignments:octave params
)))
137 (org-babel-comint-in-buffer session
139 (end-of-line 1) (insert var
) (comint-send-input nil t
)
140 (org-babel-comint-wait-for-output session
)) var-lines
))
143 (defun org-babel-matlab-initiate-session (&optional session params
)
144 "Create a matlab inferior process buffer.
145 If there is not a current inferior-process-buffer in SESSION then
146 create. Return the initialized session."
147 (org-babel-octave-initiate-session session params
'matlab
))
149 (defun org-babel-octave-initiate-session (&optional session _params matlabp
)
150 "Create an octave inferior process buffer.
151 If there is not a current inferior-process-buffer in SESSION then
152 create. Return the initialized session."
153 (if matlabp
(require 'matlab
) (or (require 'octave-inf nil
'noerror
)
155 (unless (string= session
"none")
156 (let ((session (or session
157 (if matlabp
"*Inferior Matlab*" "*Inferior Octave*"))))
158 (if (org-babel-comint-buffer-livep session
) session
159 (save-window-excursion
160 (if matlabp
(unless org-babel-matlab-with-emacs-link
(matlab-shell))
162 (rename-buffer (if (bufferp session
) (buffer-name session
)
163 (if (stringp session
) session
(buffer-name))))
164 (current-buffer))))))
166 (defun org-babel-octave-evaluate
167 (session body result-type
&optional matlabp
)
168 "Pass BODY to the octave process in SESSION.
169 If RESULT-TYPE equals `output' then return the outputs of the
170 statements in BODY, if RESULT-TYPE equals `value' then return the
171 value of the last statement in BODY, as elisp."
173 (org-babel-octave-evaluate-session session body result-type matlabp
)
174 (org-babel-octave-evaluate-external-process body result-type matlabp
)))
176 (defun org-babel-octave-evaluate-external-process (body result-type matlabp
)
177 "Evaluate BODY in an external octave process."
178 (let ((cmd (if matlabp
179 org-babel-matlab-shell-command
180 org-babel-octave-shell-command
)))
182 (output (org-babel-eval cmd body
))
183 (value (let ((tmp-file (org-babel-temp-file "octave-")))
186 (format org-babel-octave-wrapper-method body
187 (org-babel-process-file-name tmp-file
'noquote
)
188 (org-babel-process-file-name tmp-file
'noquote
)))
189 (org-babel-octave-import-elisp-from-file tmp-file
))))))
191 (defun org-babel-octave-evaluate-session
192 (session body result-type
&optional matlabp
)
193 "Evaluate BODY in SESSION."
194 (let* ((tmp-file (org-babel-temp-file (if matlabp
"matlab-" "octave-")))
195 (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
201 (list body org-babel-octave-eoe-indicator
) "\n"))
203 (if (and matlabp org-babel-matlab-with-emacs-link
)
205 (format org-babel-matlab-emacs-link-wrapper-method
207 (org-babel-process-file-name tmp-file
'noquote
)
208 (org-babel-process-file-name tmp-file
'noquote
) wait-file
) "\n")
211 (list (format org-babel-octave-wrapper-method
213 (org-babel-process-file-name tmp-file
'noquote
)
214 (org-babel-process-file-name tmp-file
'noquote
))
215 org-babel-octave-eoe-indicator
) "\n")))))
216 (raw (if (and matlabp org-babel-matlab-with-emacs-link
)
217 (save-window-excursion
220 (write-region "" 'ignored wait-file nil nil nil
'excl
)
221 (matlab-shell-run-region (point-min) (point-max))
222 (message "Waiting for Matlab Emacs Link")
223 (while (file-exists-p wait-file
) (sit-for 0.01))
224 "")) ;; matlab-shell-run-region doesn't seem to
225 ;; make *matlab* buffer contents easily
226 ;; available, so :results output currently
228 (org-babel-comint-with-output
231 org-babel-octave-eoe-indicator
232 org-babel-octave-eoe-output
)
234 (insert full-body
) (comint-send-input nil t
)))) results
)
237 (org-babel-octave-import-elisp-from-file tmp-file
))
242 (cdr (reverse (delq "" (mapcar
243 #'org-babel-octave-read-string
244 (mapcar #'org-trim raw
)))))
245 (cdr (member org-babel-octave-eoe-output
247 #'org-babel-octave-read-string
248 (mapcar #'org-trim raw
)))))))
249 (mapconcat #'identity
(reverse results
) "\n"))))))
251 (defun org-babel-octave-import-elisp-from-file (file-name)
252 "Import data from FILE-NAME.
253 This removes initial blank and comment lines and then calls
254 `org-babel-import-elisp-from-file'."
255 (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end
)
256 (with-temp-file temp-file
257 (insert-file-contents file-name
)
258 (re-search-forward "^[ \t]*[^# \t]" nil t
)
259 (if (< (setq beg
(point-min))
260 (setq end
(point-at-bol)))
261 (delete-region beg end
)))
262 (org-babel-import-elisp-from-file temp-file
'(16))))
264 (defun org-babel-octave-read-string (string)
265 "Strip \\\"s from around octave string."
266 (if (string-match "^\"\\([^\000]+\\)\"$" string
)
267 (match-string 1 string
)
274 ;;; ob-octave.el ends here