ob-octave.el 9.8 KB

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