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