ob-octave.el 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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)
  37. "Expand BODY according to PARAMS, return the expanded body."
  38. (org-babel-expand-body:octave body params))
  39. (defun org-babel-expand-body:octave (body params)
  40. "Expand BODY according to PARAMS, return the expanded body."
  41. (mapconcat
  42. #'identity
  43. (append (org-babel-octave-variable-assignments 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 (org-babel-expand-body:octave body params))
  78. (result (org-babel-octave-evaluate
  79. session augmented-body result-type matlabp)))
  80. (or out-file
  81. (org-babel-reassemble-table
  82. result
  83. (org-babel-pick-name
  84. (nth 4 processed-params) (cdr (assoc :colnames params)))
  85. (org-babel-pick-name
  86. (nth 5 processed-params) (cdr (assoc :rownames params)))))))
  87. (defun org-babel-prep-session:matlab (session params)
  88. "Prepare SESSION according to PARAMS."
  89. (org-babel-prep-session:octave session params 'matlab))
  90. (defun org-babel-octave-variable-assignments (params)
  91. "Return list of octave statements assigning the block's variables"
  92. (mapcar
  93. (lambda (pair)
  94. (format "%s=%s"
  95. (car pair)
  96. (org-babel-octave-var-to-octave (cdr pair))))
  97. (mapcar #'cdr (org-babel-get-header params :var))))
  98. (defun org-babel-octave-var-to-octave (var)
  99. "Convert an emacs-lisp value 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. (if (listp (car var)) "; " ",")) "]")
  105. (format "%s" (or var "nil"))))
  106. (defun org-babel-prep-session:octave (session params &optional matlabp)
  107. "Prepare SESSION according to the header arguments specified in PARAMS."
  108. (let* ((session (org-babel-octave-initiate-session session params matlabp))
  109. (var-lines (org-babel-octave-variable-assignments params)))
  110. (org-babel-comint-in-buffer session
  111. (mapc (lambda (var)
  112. (end-of-line 1) (insert var) (comint-send-input nil t)
  113. (org-babel-comint-wait-for-output session)) var-lines))
  114. session))
  115. (defun org-babel-matlab-initiate-session (&optional session params)
  116. "Create a matlab inferior process buffer.
  117. If there is not a current inferior-process-buffer in SESSION then
  118. create. Return the initialized session."
  119. (org-babel-octave-initiate-session session params 'matlab))
  120. (defun org-babel-octave-initiate-session (&optional session params matlabp)
  121. "Create an octave inferior process buffer.
  122. If there is not a current inferior-process-buffer in SESSION then
  123. create. Return the initialized session."
  124. (if matlabp (require 'matlab) (require 'octave-inf))
  125. (unless (string= session "none")
  126. (let ((session (or session
  127. (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
  128. (if (org-babel-comint-buffer-livep session) session
  129. (save-window-excursion
  130. (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
  131. (run-octave))
  132. (rename-buffer (if (bufferp session) (buffer-name session)
  133. (if (stringp session) session (buffer-name))))
  134. (current-buffer))))))
  135. (defun org-babel-octave-evaluate
  136. (session body result-type &optional matlabp)
  137. "Pass BODY to the octave process in SESSION.
  138. If RESULT-TYPE equals 'output then return the outputs of the
  139. statements in BODY, if RESULT-TYPE equals 'value then return the
  140. value of the last statement in BODY, as elisp."
  141. (if session
  142. (org-babel-octave-evaluate-session session body result-type matlabp)
  143. (org-babel-octave-evaluate-external-process body result-type matlabp)))
  144. (defun org-babel-octave-evaluate-external-process (body result-type matlabp)
  145. "Evaluate BODY in an external octave process."
  146. (let ((cmd (if matlabp
  147. org-babel-matlab-shell-command
  148. org-babel-octave-shell-command)))
  149. (case result-type
  150. (output (org-babel-eval cmd body))
  151. (value (let ((tmp-file (org-babel-temp-file "octave-")))
  152. (org-babel-eval
  153. cmd
  154. (format org-babel-octave-wrapper-method body
  155. (org-babel-process-file-name tmp-file 'noquote)
  156. (org-babel-process-file-name tmp-file 'noquote)))
  157. (org-babel-octave-import-elisp-from-file tmp-file))))))
  158. (defun org-babel-octave-evaluate-session
  159. (session body result-type &optional matlabp)
  160. "Evaluate BODY in SESSION."
  161. (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
  162. (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
  163. (full-body
  164. (case result-type
  165. (output
  166. (mapconcat
  167. #'org-babel-chomp
  168. (list body org-babel-octave-eoe-indicator) "\n"))
  169. (value
  170. (if (and matlabp org-babel-matlab-with-emacs-link)
  171. (concat
  172. (format org-babel-matlab-emacs-link-wrapper-method
  173. body
  174. (org-babel-process-file-name tmp-file 'noquote)
  175. (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
  176. (mapconcat
  177. #'org-babel-chomp
  178. (list (format org-babel-octave-wrapper-method
  179. body
  180. (org-babel-process-file-name tmp-file 'noquote)
  181. (org-babel-process-file-name tmp-file 'noquote))
  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 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.
  219. This removes initial blank and comment lines and then calls
  220. `org-babel-import-elisp-from-file'."
  221. (let ((temp-file (org-babel-temp-file "octave-matlab-")) 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 '(16))))
  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