ob-octave.el 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
  2. ;; Copyright (C) 2010-2011 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 full-body result-type matlabp)))
  71. (org-babel-reassemble-table
  72. result
  73. (org-babel-pick-name
  74. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  75. (org-babel-pick-name
  76. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  77. (defun org-babel-prep-session:matlab (session params)
  78. "Prepare SESSION according to PARAMS."
  79. (org-babel-prep-session:octave session params 'matlab))
  80. (defun org-babel-variable-assignments:octave (params)
  81. "Return list of octave statements assigning the block's variables"
  82. (mapcar
  83. (lambda (pair)
  84. (format "%s=%s;"
  85. (car pair)
  86. (org-babel-octave-var-to-octave (cdr pair))))
  87. (mapcar #'cdr (org-babel-get-header params :var))))
  88. (defalias 'org-babel-variable-assignments:matlab
  89. 'org-babel-variable-assignments:octave)
  90. (defun org-babel-octave-var-to-octave (var)
  91. "Convert an emacs-lisp value into an octave variable.
  92. Converts an emacs-lisp variable into a string of octave code
  93. specifying a variable of the same value."
  94. (if (listp var)
  95. (concat "[" (mapconcat #'org-babel-octave-var-to-octave var
  96. (if (listp (car var)) "; " ",")) "]")
  97. (cond
  98. ((stringp var)
  99. (format "\'%s\'" var))
  100. (t
  101. (format "%s" var)))))
  102. (defun org-babel-prep-session:octave (session params &optional matlabp)
  103. "Prepare SESSION according to the header arguments specified in PARAMS."
  104. (let* ((session (org-babel-octave-initiate-session session params matlabp))
  105. (var-lines (org-babel-variable-assignments:octave params)))
  106. (org-babel-comint-in-buffer session
  107. (mapc (lambda (var)
  108. (end-of-line 1) (insert var) (comint-send-input nil t)
  109. (org-babel-comint-wait-for-output session)) var-lines))
  110. session))
  111. (defun org-babel-matlab-initiate-session (&optional session params)
  112. "Create a matlab inferior process buffer.
  113. If there is not a current inferior-process-buffer in SESSION then
  114. create. Return the initialized session."
  115. (org-babel-octave-initiate-session session params 'matlab))
  116. (defun org-babel-octave-initiate-session (&optional session params matlabp)
  117. "Create an octave inferior process buffer.
  118. If there is not a current inferior-process-buffer in SESSION then
  119. create. Return the initialized session."
  120. (if matlabp (require 'matlab) (require 'octave-inf))
  121. (unless (string= session "none")
  122. (let ((session (or session
  123. (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
  124. (if (org-babel-comint-buffer-livep session) session
  125. (save-window-excursion
  126. (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
  127. (run-octave))
  128. (rename-buffer (if (bufferp session) (buffer-name session)
  129. (if (stringp session) session (buffer-name))))
  130. (current-buffer))))))
  131. (defun org-babel-octave-evaluate
  132. (session body result-type &optional matlabp)
  133. "Pass BODY to the octave process in SESSION.
  134. If RESULT-TYPE equals 'output then return the outputs of the
  135. statements in BODY, if RESULT-TYPE equals 'value then return the
  136. value of the last statement in BODY, as elisp."
  137. (if session
  138. (org-babel-octave-evaluate-session session body result-type matlabp)
  139. (org-babel-octave-evaluate-external-process body result-type matlabp)))
  140. (defun org-babel-octave-evaluate-external-process (body result-type matlabp)
  141. "Evaluate BODY in an external octave process."
  142. (let ((cmd (if matlabp
  143. org-babel-matlab-shell-command
  144. org-babel-octave-shell-command)))
  145. (case result-type
  146. (output (org-babel-eval cmd body))
  147. (value (let ((tmp-file (org-babel-temp-file "octave-")))
  148. (org-babel-eval
  149. cmd
  150. (format org-babel-octave-wrapper-method body
  151. (org-babel-process-file-name tmp-file 'noquote)
  152. (org-babel-process-file-name tmp-file 'noquote)))
  153. (org-babel-octave-import-elisp-from-file tmp-file))))))
  154. (defun org-babel-octave-evaluate-session
  155. (session body result-type &optional matlabp)
  156. "Evaluate BODY in SESSION."
  157. (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
  158. (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
  159. (full-body
  160. (case result-type
  161. (output
  162. (mapconcat
  163. #'org-babel-chomp
  164. (list body org-babel-octave-eoe-indicator) "\n"))
  165. (value
  166. (if (and matlabp org-babel-matlab-with-emacs-link)
  167. (concat
  168. (format org-babel-matlab-emacs-link-wrapper-method
  169. body
  170. (org-babel-process-file-name tmp-file 'noquote)
  171. (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
  172. (mapconcat
  173. #'org-babel-chomp
  174. (list (format org-babel-octave-wrapper-method
  175. body
  176. (org-babel-process-file-name tmp-file 'noquote)
  177. (org-babel-process-file-name tmp-file 'noquote))
  178. org-babel-octave-eoe-indicator) "\n")))))
  179. (raw (if (and matlabp org-babel-matlab-with-emacs-link)
  180. (save-window-excursion
  181. (with-temp-buffer
  182. (insert full-body)
  183. (write-region "" 'ignored wait-file nil nil nil 'excl)
  184. (matlab-shell-run-region (point-min) (point-max))
  185. (message "Waiting for Matlab Emacs Link")
  186. (while (file-exists-p wait-file) (sit-for 0.01))
  187. "")) ;; matlab-shell-run-region doesn't seem to
  188. ;; make *matlab* buffer contents easily
  189. ;; available, so :results output currently
  190. ;; won't work
  191. (org-babel-comint-with-output
  192. (session
  193. (if matlabp
  194. org-babel-octave-eoe-indicator
  195. org-babel-octave-eoe-output)
  196. t full-body)
  197. (insert full-body) (comint-send-input nil t)))) results)
  198. (case result-type
  199. (value
  200. (org-babel-octave-import-elisp-from-file tmp-file))
  201. (output
  202. (progn
  203. (setq results
  204. (if matlabp
  205. (cdr (reverse (delq "" (mapcar
  206. #'org-babel-octave-read-string
  207. (mapcar #'org-babel-trim raw)))))
  208. (cdr (member org-babel-octave-eoe-output
  209. (reverse (mapcar
  210. #'org-babel-octave-read-string
  211. (mapcar #'org-babel-trim raw)))))))
  212. (mapconcat #'identity (reverse results) "\n"))))))
  213. (defun org-babel-octave-import-elisp-from-file (file-name)
  214. "Import data from FILE-NAME.
  215. This removes initial blank and comment lines and then calls
  216. `org-babel-import-elisp-from-file'."
  217. (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
  218. (with-temp-file temp-file
  219. (insert-file-contents file-name)
  220. (re-search-forward "^[ \t]*[^# \t]" nil t)
  221. (if (< (setq beg (point-min))
  222. (setq end (point-at-bol)))
  223. (delete-region beg end)))
  224. (org-babel-import-elisp-from-file temp-file '(16))))
  225. (defun org-babel-octave-read-string (string)
  226. "Strip \\\"s from around octave string"
  227. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  228. (match-string 1 string)
  229. string))
  230. (provide 'ob-octave)
  231. ;;; ob-octave.el ends here