ob-octave.el 9.5 KB

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