ob-octave.el 9.5 KB

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