ob-octave.el 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. ;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
  2. ;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. ;; Author: Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Requirements:
  19. ;; octave
  20. ;; octave-mode.el and octave-inf.el come with GNU emacs
  21. ;;; Code:
  22. (require 'ob)
  23. (eval-when-compile (require 'cl))
  24. (declare-function matlab-shell "ext:matlab-mode")
  25. (declare-function matlab-shell-run-region "ext:matlab-mode")
  26. (defvar org-babel-default-header-args:matlab '())
  27. (defvar org-babel-default-header-args:octave '())
  28. (defvar org-babel-matlab-shell-command "matlab -nosplash"
  29. "Shell command to run matlab as an external process.")
  30. (defvar org-babel-octave-shell-command "octave -q"
  31. "Shell command to run octave as an external process.")
  32. (defvar org-babel-matlab-with-emacs-link nil
  33. "If non-nil use matlab-shell-run-region for session evaluation.
  34. This will use EmacsLink if (matlab-with-emacs-link) evaluates
  35. to a non-nil value.")
  36. (defvar org-babel-matlab-emacs-link-wrapper-method
  37. "%s
  38. if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
  39. else, save -ascii %s ans
  40. end
  41. delete('%s')
  42. ")
  43. (defvar org-babel-octave-wrapper-method
  44. "%s
  45. if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
  46. else, dlmwrite('%s', ans, '\\t')
  47. end")
  48. (defvar org-babel-octave-eoe-indicator "\'org_babel_eoe\'")
  49. (defvar org-babel-octave-eoe-output "ans = org_babel_eoe")
  50. (defun org-babel-execute:matlab (body params)
  51. "Execute a block of matlab code with Babel."
  52. (org-babel-execute:octave body params 'matlab))
  53. (defun org-babel-execute:octave (body params &optional matlabp)
  54. "Execute a block of octave code with Babel."
  55. (let* ((session
  56. (funcall (intern (format "org-babel-%s-initiate-session"
  57. (if matlabp "matlab" "octave")))
  58. (cdr (assoc :session params)) params))
  59. (vars (mapcar #'cdr (org-babel-get-header params :var)))
  60. (result-params (cdr (assoc :result-params params)))
  61. (result-type (cdr (assoc :result-type params)))
  62. (out-file (cdr (assoc :file params)))
  63. (full-body
  64. (org-babel-expand-body:generic
  65. body params (org-babel-variable-assignments:octave params)))
  66. (gfx-file (ignore-errors (org-babel-graphical-output-file params)))
  67. (result (org-babel-octave-evaluate
  68. session
  69. (if gfx-file
  70. (mapconcat 'identity
  71. (list
  72. "set (0, \"defaultfigurevisible\", \"off\");"
  73. full-body
  74. (format "print -dpng %s" gfx-file))
  75. "\n")
  76. full-body)
  77. result-type matlabp)))
  78. (if gfx-file
  79. nil
  80. (org-babel-reassemble-table
  81. result
  82. (org-babel-pick-name
  83. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  84. (org-babel-pick-name
  85. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))))
  86. (defun org-babel-prep-session:matlab (session params)
  87. "Prepare SESSION according to PARAMS."
  88. (org-babel-prep-session:octave session params 'matlab))
  89. (defun org-babel-variable-assignments:octave (params)
  90. "Return list of octave statements assigning the block's variables."
  91. (mapcar
  92. (lambda (pair)
  93. (format "%s=%s;"
  94. (car pair)
  95. (org-babel-octave-var-to-octave (cdr pair))))
  96. (mapcar #'cdr (org-babel-get-header params :var))))
  97. (defalias 'org-babel-variable-assignments:matlab
  98. 'org-babel-variable-assignments:octave)
  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. (cond
  107. ((stringp var)
  108. (format "\'%s\'" var))
  109. (t
  110. (format "%s" var)))))
  111. (defun org-babel-prep-session:octave (session params &optional matlabp)
  112. "Prepare SESSION according to the header arguments specified in PARAMS."
  113. (let* ((session (org-babel-octave-initiate-session session params matlabp))
  114. (var-lines (org-babel-variable-assignments:octave params)))
  115. (org-babel-comint-in-buffer session
  116. (mapc (lambda (var)
  117. (end-of-line 1) (insert var) (comint-send-input nil t)
  118. (org-babel-comint-wait-for-output session)) var-lines))
  119. session))
  120. (defun org-babel-matlab-initiate-session (&optional session params)
  121. "Create a matlab inferior process buffer.
  122. If there is not a current inferior-process-buffer in SESSION then
  123. create. Return the initialized session."
  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.
  127. If there is not a current inferior-process-buffer in SESSION then
  128. create. Return the initialized session."
  129. (if matlabp (require 'matlab) (or (require 'octave-inf nil 'noerror)
  130. (require 'octave)))
  131. (unless (string= session "none")
  132. (let ((session (or session
  133. (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
  134. (if (org-babel-comint-buffer-livep session) session
  135. (save-window-excursion
  136. (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
  137. (run-octave))
  138. (rename-buffer (if (bufferp session) (buffer-name session)
  139. (if (stringp session) session (buffer-name))))
  140. (current-buffer))))))
  141. (defun org-babel-octave-evaluate
  142. (session body result-type &optional matlabp)
  143. "Pass BODY to the octave process in SESSION.
  144. If RESULT-TYPE equals `output' then return the outputs of the
  145. statements in BODY, if RESULT-TYPE equals `value' then return the
  146. value of the last statement in BODY, as elisp."
  147. (if session
  148. (org-babel-octave-evaluate-session session body result-type matlabp)
  149. (org-babel-octave-evaluate-external-process body result-type matlabp)))
  150. (defun org-babel-octave-evaluate-external-process (body result-type matlabp)
  151. "Evaluate BODY in an external octave process."
  152. (let ((cmd (if matlabp
  153. org-babel-matlab-shell-command
  154. org-babel-octave-shell-command)))
  155. (case result-type
  156. (output (org-babel-eval cmd body))
  157. (value (let ((tmp-file (org-babel-temp-file "octave-")))
  158. (org-babel-eval
  159. cmd
  160. (format org-babel-octave-wrapper-method body
  161. (org-babel-process-file-name tmp-file 'noquote)
  162. (org-babel-process-file-name tmp-file 'noquote)))
  163. (org-babel-octave-import-elisp-from-file tmp-file))))))
  164. (defun org-babel-octave-evaluate-session
  165. (session body result-type &optional matlabp)
  166. "Evaluate BODY in SESSION."
  167. (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
  168. (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
  169. (full-body
  170. (case result-type
  171. (output
  172. (mapconcat
  173. #'org-babel-chomp
  174. (list body org-babel-octave-eoe-indicator) "\n"))
  175. (value
  176. (if (and matlabp org-babel-matlab-with-emacs-link)
  177. (concat
  178. (format org-babel-matlab-emacs-link-wrapper-method
  179. body
  180. (org-babel-process-file-name tmp-file 'noquote)
  181. (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
  182. (mapconcat
  183. #'org-babel-chomp
  184. (list (format org-babel-octave-wrapper-method
  185. body
  186. (org-babel-process-file-name tmp-file 'noquote)
  187. (org-babel-process-file-name tmp-file 'noquote))
  188. org-babel-octave-eoe-indicator) "\n")))))
  189. (raw (if (and matlabp org-babel-matlab-with-emacs-link)
  190. (save-window-excursion
  191. (with-temp-buffer
  192. (insert full-body)
  193. (write-region "" 'ignored wait-file nil nil nil 'excl)
  194. (matlab-shell-run-region (point-min) (point-max))
  195. (message "Waiting for Matlab Emacs Link")
  196. (while (file-exists-p wait-file) (sit-for 0.01))
  197. "")) ;; matlab-shell-run-region doesn't seem to
  198. ;; make *matlab* buffer contents easily
  199. ;; available, so :results output currently
  200. ;; won't work
  201. (org-babel-comint-with-output
  202. (session
  203. (if matlabp
  204. org-babel-octave-eoe-indicator
  205. org-babel-octave-eoe-output)
  206. t full-body)
  207. (insert full-body) (comint-send-input nil t)))) results)
  208. (case result-type
  209. (value
  210. (org-babel-octave-import-elisp-from-file tmp-file))
  211. (output
  212. (progn
  213. (setq results
  214. (if matlabp
  215. (cdr (reverse (delq "" (mapcar
  216. #'org-babel-octave-read-string
  217. (mapcar #'org-babel-trim raw)))))
  218. (cdr (member org-babel-octave-eoe-output
  219. (reverse (mapcar
  220. #'org-babel-octave-read-string
  221. (mapcar #'org-babel-trim raw)))))))
  222. (mapconcat #'identity (reverse results) "\n"))))))
  223. (defun org-babel-octave-import-elisp-from-file (file-name)
  224. "Import data from FILE-NAME.
  225. This removes initial blank and comment lines and then calls
  226. `org-babel-import-elisp-from-file'."
  227. (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
  228. (with-temp-file temp-file
  229. (insert-file-contents file-name)
  230. (re-search-forward "^[ \t]*[^# \t]" nil t)
  231. (if (< (setq beg (point-min))
  232. (setq end (point-at-bol)))
  233. (delete-region beg end)))
  234. (org-babel-import-elisp-from-file temp-file '(16))))
  235. (defun org-babel-octave-read-string (string)
  236. "Strip \\\"s from around octave string."
  237. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  238. (match-string 1 string)
  239. string))
  240. (provide 'ob-octave)
  241. ;;; ob-octave.el ends here