ob-octave.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. ;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  3. ;; Author: Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Requirements:
  19. ;; octave
  20. ;; octave-mode.el and octave-inf.el come with GNU emacs
  21. ;;; Code:
  22. (require 'ob)
  23. (require 'ob-ref)
  24. (require 'ob-comint)
  25. (require 'ob-eval)
  26. (eval-when-compile (require 'cl))
  27. (declare-function matlab-shell "ext:matlab-mode")
  28. (declare-function matlab-shell-run-region "ext:matlab-mode")
  29. (defvar org-babel-default-header-args:matlab '())
  30. (defvar org-babel-default-header-args:octave '())
  31. (defvar org-babel-matlab-shell-command "matlab -nosplash"
  32. "Shell command to run matlab as an external process.")
  33. (defvar org-babel-octave-shell-command "octave -q"
  34. "Shell command to run octave as an external process.")
  35. (defvar org-babel-matlab-with-emacs-link nil
  36. "If non-nil use matlab-shell-run-region for session evaluation.
  37. This will use EmacsLink if (matlab-with-emacs-link) evaluates
  38. to a non-nil value.")
  39. (defvar org-babel-matlab-emacs-link-wrapper-method
  40. "%s
  41. if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
  42. else, save -ascii %s ans
  43. end
  44. delete('%s')
  45. ")
  46. (defvar org-babel-octave-wrapper-method
  47. "%s
  48. if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
  49. else, dlmwrite('%s', ans, '\\t')
  50. end")
  51. (defvar org-babel-octave-eoe-indicator "\'org_babel_eoe\'")
  52. (defvar org-babel-octave-eoe-output "ans = org_babel_eoe")
  53. (defun org-babel-execute:matlab (body params)
  54. "Execute a block of matlab code with Babel."
  55. (org-babel-execute:octave body params 'matlab))
  56. (defun org-babel-execute:octave (body params &optional matlabp)
  57. "Execute a block of octave code with Babel."
  58. (let* ((session
  59. (funcall (intern (format "org-babel-%s-initiate-session"
  60. (if matlabp "matlab" "octave")))
  61. (cdr (assoc :session params)) params))
  62. (vars (mapcar #'cdr (org-babel-get-header params :var)))
  63. (result-params (cdr (assoc :result-params params)))
  64. (result-type (cdr (assoc :result-type params)))
  65. (out-file (cdr (assoc :file params)))
  66. (full-body
  67. (org-babel-expand-body:generic
  68. body params (org-babel-variable-assignments:octave params)))
  69. (result (org-babel-octave-evaluate
  70. session
  71. (if (org-babel-octave-graphical-output-file params)
  72. (mapconcat 'identity
  73. (list
  74. "set (0, \"defaultfigurevisible\", \"off\");"
  75. full-body
  76. (format "print -dpng %s" (org-babel-octave-graphical-output-file params)))
  77. "\n")
  78. full-body)
  79. result-type matlabp)))
  80. (if (org-babel-octave-graphical-output-file params)
  81. nil
  82. (org-babel-reassemble-table
  83. result
  84. (org-babel-pick-name
  85. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  86. (org-babel-pick-name
  87. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))))
  88. (defun org-babel-prep-session:matlab (session params)
  89. "Prepare SESSION according to PARAMS."
  90. (org-babel-prep-session:octave session params 'matlab))
  91. (defun org-babel-variable-assignments:octave (params)
  92. "Return list of octave statements assigning the block's variables"
  93. (mapcar
  94. (lambda (pair)
  95. (format "%s=%s;"
  96. (car pair)
  97. (org-babel-octave-var-to-octave (cdr pair))))
  98. (mapcar #'cdr (org-babel-get-header params :var))))
  99. (defalias 'org-babel-variable-assignments:matlab
  100. 'org-babel-variable-assignments:octave)
  101. (defun org-babel-octave-var-to-octave (var)
  102. "Convert an emacs-lisp value into an octave variable.
  103. Converts an emacs-lisp variable into a string of octave code
  104. specifying a variable of the same value."
  105. (if (listp var)
  106. (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
  107. (if (listp (car var)) "; " ",")) "]")
  108. (cond
  109. ((stringp var)
  110. (format "\'%s\'" var))
  111. (t
  112. (format "%s" var)))))
  113. (defun org-babel-prep-session:octave (session params &optional matlabp)
  114. "Prepare SESSION according to the header arguments specified in PARAMS."
  115. (let* ((session (org-babel-octave-initiate-session session params matlabp))
  116. (var-lines (org-babel-variable-assignments:octave params)))
  117. (org-babel-comint-in-buffer session
  118. (mapc (lambda (var)
  119. (end-of-line 1) (insert var) (comint-send-input nil t)
  120. (org-babel-comint-wait-for-output session)) var-lines))
  121. session))
  122. (defun org-babel-matlab-initiate-session (&optional session params)
  123. "Create a matlab inferior process buffer.
  124. If there is not a current inferior-process-buffer in SESSION then
  125. create. Return the initialized session."
  126. (org-babel-octave-initiate-session session params 'matlab))
  127. (defun org-babel-octave-initiate-session (&optional session params matlabp)
  128. "Create an octave inferior process buffer.
  129. If there is not a current inferior-process-buffer in SESSION then
  130. create. Return the initialized session."
  131. (if matlabp (require 'matlab) (require 'octave-inf))
  132. (unless (string= session "none")
  133. (let ((session (or session
  134. (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
  135. (if (org-babel-comint-buffer-livep session) session
  136. (save-window-excursion
  137. (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
  138. (run-octave))
  139. (rename-buffer (if (bufferp session) (buffer-name session)
  140. (if (stringp session) session (buffer-name))))
  141. (current-buffer))))))
  142. (defun org-babel-octave-evaluate
  143. (session body result-type &optional matlabp)
  144. "Pass BODY to the octave process in SESSION.
  145. If RESULT-TYPE equals 'output then return the outputs of the
  146. statements in BODY, if RESULT-TYPE equals 'value then return the
  147. value of the last statement in BODY, as elisp."
  148. (if session
  149. (org-babel-octave-evaluate-session session body result-type matlabp)
  150. (org-babel-octave-evaluate-external-process body result-type matlabp)))
  151. (defun org-babel-octave-evaluate-external-process (body result-type matlabp)
  152. "Evaluate BODY in an external octave process."
  153. (let ((cmd (if matlabp
  154. org-babel-matlab-shell-command
  155. org-babel-octave-shell-command)))
  156. (case result-type
  157. (output (org-babel-eval cmd body))
  158. (value (let ((tmp-file (org-babel-temp-file "octave-")))
  159. (org-babel-eval
  160. cmd
  161. (format org-babel-octave-wrapper-method body
  162. (org-babel-process-file-name tmp-file 'noquote)
  163. (org-babel-process-file-name tmp-file 'noquote)))
  164. (org-babel-octave-import-elisp-from-file tmp-file))))))
  165. (defun org-babel-octave-evaluate-session
  166. (session body result-type &optional matlabp)
  167. "Evaluate BODY in SESSION."
  168. (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
  169. (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
  170. (full-body
  171. (case result-type
  172. (output
  173. (mapconcat
  174. #'org-babel-chomp
  175. (list body org-babel-octave-eoe-indicator) "\n"))
  176. (value
  177. (if (and matlabp org-babel-matlab-with-emacs-link)
  178. (concat
  179. (format org-babel-matlab-emacs-link-wrapper-method
  180. body
  181. (org-babel-process-file-name tmp-file 'noquote)
  182. (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
  183. (mapconcat
  184. #'org-babel-chomp
  185. (list (format org-babel-octave-wrapper-method
  186. body
  187. (org-babel-process-file-name tmp-file 'noquote)
  188. (org-babel-process-file-name tmp-file 'noquote))
  189. org-babel-octave-eoe-indicator) "\n")))))
  190. (raw (if (and matlabp org-babel-matlab-with-emacs-link)
  191. (save-window-excursion
  192. (with-temp-buffer
  193. (insert full-body)
  194. (write-region "" 'ignored wait-file nil nil nil 'excl)
  195. (matlab-shell-run-region (point-min) (point-max))
  196. (message "Waiting for Matlab Emacs Link")
  197. (while (file-exists-p wait-file) (sit-for 0.01))
  198. "")) ;; matlab-shell-run-region doesn't seem to
  199. ;; make *matlab* buffer contents easily
  200. ;; available, so :results output currently
  201. ;; won't work
  202. (org-babel-comint-with-output
  203. (session
  204. (if matlabp
  205. org-babel-octave-eoe-indicator
  206. org-babel-octave-eoe-output)
  207. t full-body)
  208. (insert full-body) (comint-send-input nil t)))) results)
  209. (case result-type
  210. (value
  211. (org-babel-octave-import-elisp-from-file tmp-file))
  212. (output
  213. (progn
  214. (setq results
  215. (if matlabp
  216. (cdr (reverse (delq "" (mapcar
  217. #'org-babel-octave-read-string
  218. (mapcar #'org-babel-trim raw)))))
  219. (cdr (member org-babel-octave-eoe-output
  220. (reverse (mapcar
  221. #'org-babel-octave-read-string
  222. (mapcar #'org-babel-trim raw)))))))
  223. (mapconcat #'identity (reverse results) "\n"))))))
  224. (defun org-babel-octave-import-elisp-from-file (file-name)
  225. "Import data from FILE-NAME.
  226. This removes initial blank and comment lines and then calls
  227. `org-babel-import-elisp-from-file'."
  228. (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
  229. (with-temp-file temp-file
  230. (insert-file-contents file-name)
  231. (re-search-forward "^[ \t]*[^# \t]" nil t)
  232. (if (< (setq beg (point-min))
  233. (setq end (point-at-bol)))
  234. (delete-region beg end)))
  235. (org-babel-import-elisp-from-file temp-file '(16))))
  236. (defun org-babel-octave-read-string (string)
  237. "Strip \\\"s from around octave string"
  238. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  239. (match-string 1 string)
  240. string))
  241. (defun org-babel-octave-graphical-output-file (params)
  242. "Name of file to which maxima should send graphical output."
  243. (and (member "graphics" (cdr (assq :result-params params)))
  244. (cdr (assq :file params))))
  245. (provide 'ob-octave)
  246. ;;; ob-octave.el ends here