ob-octave.el 9.8 KB

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