ob-octave.el 9.9 KB

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