ob-octave.el 9.6 KB

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