ob-octave.el 10 KB

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