ob-octave.el 9.6 KB

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