ob-octave.el 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. ;;; ob-octave.el --- Babel Functions for Octave and Matlab -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. ;; Author: Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; URL: https://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 'org-macs)
  23. (org-assert-version)
  24. (require 'ob)
  25. (require 'org-macs)
  26. (declare-function matlab-shell "ext:matlab-mode")
  27. (declare-function matlab-shell-run-region "ext:matlab-mode")
  28. (defvar org-babel-default-header-args:matlab '())
  29. (defvar org-babel-default-header-args:octave '())
  30. (defvar org-babel-matlab-shell-command "matlab -nosplash"
  31. "Shell command to run matlab as an external process.")
  32. (defvar org-babel-octave-shell-command "octave -q"
  33. "Shell command to run octave as an external process.")
  34. (defvar org-babel-matlab-with-emacs-link nil
  35. "If non-nil use matlab-shell-run-region for session evaluation.
  36. This will use EmacsLink if (matlab-with-emacs-link) evaluates
  37. to a non-nil value.")
  38. (defvar org-babel-matlab-emacs-link-wrapper-method
  39. "%s
  40. if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
  41. else, save -ascii %s ans
  42. end
  43. delete('%s')
  44. ")
  45. (defvar org-babel-octave-wrapper-method
  46. "%s
  47. if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
  48. else, dlmwrite('%s', ans, '\\t')
  49. end")
  50. (defvar org-babel-octave-eoe-indicator "'org_babel_eoe'")
  51. (defvar org-babel-octave-eoe-output "ans = org_babel_eoe")
  52. (defun org-babel-execute:matlab (body params)
  53. "Execute a block of matlab code with Babel."
  54. (org-babel-execute:octave body params 'matlab))
  55. (defun org-babel-execute:octave (body params &optional matlabp)
  56. "Execute a block of octave code with Babel."
  57. (let* ((session
  58. (funcall (intern (format "org-babel-%s-initiate-session"
  59. (if matlabp "matlab" "octave")))
  60. (cdr (assq :session params)) params))
  61. (result-type (cdr (assq :result-type params)))
  62. (full-body
  63. (org-babel-expand-body:generic
  64. body params (org-babel-variable-assignments:octave params)))
  65. (gfx-file (ignore-errors (org-babel-graphical-output-file params)))
  66. (result (org-babel-octave-evaluate
  67. session
  68. (if gfx-file
  69. (mapconcat 'identity
  70. (list
  71. "set (0, \"defaultfigurevisible\", \"off\");"
  72. full-body
  73. (format "print -dpng %s" gfx-file))
  74. "\n")
  75. full-body)
  76. result-type matlabp)))
  77. (if gfx-file
  78. nil
  79. (org-babel-reassemble-table
  80. result
  81. (org-babel-pick-name
  82. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  83. (org-babel-pick-name
  84. (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))))
  85. (defun org-babel-prep-session:matlab (session params)
  86. "Prepare SESSION according to PARAMS."
  87. (org-babel-prep-session:octave session params 'matlab))
  88. (defun org-babel-variable-assignments:octave (params)
  89. "Return list of octave statements assigning the block's variables."
  90. (mapcar
  91. (lambda (pair)
  92. (format "%s=%s;"
  93. (car pair)
  94. (org-babel-octave-var-to-octave (cdr pair))))
  95. (org-babel--get-vars params)))
  96. (defalias 'org-babel-variable-assignments:matlab
  97. 'org-babel-variable-assignments:octave)
  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. (cond
  106. ((stringp var)
  107. (format "'%s'" var))
  108. (t
  109. (format "%s" var)))))
  110. (defun org-babel-prep-session:octave (session params &optional matlabp)
  111. "Prepare SESSION according to the header arguments specified in PARAMS."
  112. (let* ((session (org-babel-octave-initiate-session session params matlabp))
  113. (var-lines (org-babel-variable-assignments:octave params)))
  114. (org-babel-comint-in-buffer session
  115. (mapc (lambda (var)
  116. (end-of-line 1) (insert var) (comint-send-input nil t)
  117. (org-babel-comint-wait-for-output session))
  118. 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. (pcase 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. (pcase 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))))
  208. results)
  209. (pcase result-type
  210. (`value
  211. (org-babel-octave-import-elisp-from-file tmp-file))
  212. (`output
  213. (setq results
  214. (if matlabp
  215. (cdr (reverse (delq "" (mapcar #'org-strip-quotes
  216. (mapcar #'org-trim raw)))))
  217. (cdr (member org-babel-octave-eoe-output
  218. (reverse (mapcar #'org-strip-quotes
  219. (mapcar #'org-trim raw)))))))
  220. (mapconcat #'identity (reverse results) "\n")))))
  221. (defun org-babel-octave-import-elisp-from-file (file-name)
  222. "Import data from FILE-NAME.
  223. This removes initial blank and comment lines and then calls
  224. `org-babel-import-elisp-from-file'."
  225. (let ((temp-file (org-babel-temp-file "octave-matlab-")) beg end)
  226. (with-temp-file temp-file
  227. (insert-file-contents file-name)
  228. (re-search-forward "^[ \t]*[^# \t]" nil t)
  229. (when (< (setq beg (point-min))
  230. (setq end (line-beginning-position)))
  231. (delete-region beg end)))
  232. (org-babel-import-elisp-from-file temp-file '(16))))
  233. (provide 'ob-octave)
  234. ;;; ob-octave.el ends here