ob-octave.el 10 KB

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