ob-octave.el 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. ;;; ob-octave.el --- Babel Functions for Octave and Matlab -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2021 Free Software Foundation, Inc.
  3. ;; Author: Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: 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 'ob)
  23. (require 'org-macs)
  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 (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))
  116. var-lines))
  117. session))
  118. (defun org-babel-matlab-initiate-session (&optional session params)
  119. "Create a matlab inferior process buffer.
  120. If there is not a current inferior-process-buffer in SESSION then
  121. create. Return the initialized session."
  122. (org-babel-octave-initiate-session session params 'matlab))
  123. (defun org-babel-octave-initiate-session (&optional session _params matlabp)
  124. "Create an octave inferior process buffer.
  125. If there is not a current inferior-process-buffer in SESSION then
  126. create. Return the initialized session."
  127. (if matlabp (require 'matlab) (or (require 'octave-inf nil 'noerror)
  128. (require 'octave)))
  129. (unless (string= session "none")
  130. (let ((session (or session
  131. (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
  132. (if (org-babel-comint-buffer-livep session) session
  133. (save-window-excursion
  134. (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
  135. (run-octave))
  136. (rename-buffer (if (bufferp session) (buffer-name session)
  137. (if (stringp session) session (buffer-name))))
  138. (current-buffer))))))
  139. (defun org-babel-octave-evaluate
  140. (session body result-type &optional matlabp)
  141. "Pass BODY to the octave process in SESSION.
  142. If RESULT-TYPE equals `output' then return the outputs of the
  143. statements in BODY, if RESULT-TYPE equals `value' then return the
  144. value of the last statement in BODY, as elisp."
  145. (if session
  146. (org-babel-octave-evaluate-session session body result-type matlabp)
  147. (org-babel-octave-evaluate-external-process body result-type matlabp)))
  148. (defun org-babel-octave-evaluate-external-process (body result-type matlabp)
  149. "Evaluate BODY in an external octave process."
  150. (let ((cmd (if matlabp
  151. org-babel-matlab-shell-command
  152. org-babel-octave-shell-command)))
  153. (pcase result-type
  154. (`output (org-babel-eval cmd body))
  155. (`value (let ((tmp-file (org-babel-temp-file "octave-")))
  156. (org-babel-eval
  157. cmd
  158. (format org-babel-octave-wrapper-method body
  159. (org-babel-process-file-name tmp-file 'noquote)
  160. (org-babel-process-file-name tmp-file 'noquote)))
  161. (org-babel-octave-import-elisp-from-file tmp-file))))))
  162. (defun org-babel-octave-evaluate-session
  163. (session body result-type &optional matlabp)
  164. "Evaluate BODY in SESSION."
  165. (let* ((tmp-file (org-babel-temp-file (if matlabp "matlab-" "octave-")))
  166. (wait-file (org-babel-temp-file "matlab-emacs-link-wait-signal-"))
  167. (full-body
  168. (pcase result-type
  169. (`output
  170. (mapconcat
  171. #'org-babel-chomp
  172. (list body org-babel-octave-eoe-indicator) "\n"))
  173. (`value
  174. (if (and matlabp org-babel-matlab-with-emacs-link)
  175. (concat
  176. (format org-babel-matlab-emacs-link-wrapper-method
  177. body
  178. (org-babel-process-file-name tmp-file 'noquote)
  179. (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
  180. (mapconcat
  181. #'org-babel-chomp
  182. (list (format org-babel-octave-wrapper-method
  183. body
  184. (org-babel-process-file-name tmp-file 'noquote)
  185. (org-babel-process-file-name tmp-file 'noquote))
  186. org-babel-octave-eoe-indicator) "\n")))))
  187. (raw (if (and matlabp org-babel-matlab-with-emacs-link)
  188. (save-window-excursion
  189. (with-temp-buffer
  190. (insert full-body)
  191. (write-region "" 'ignored wait-file nil nil nil 'excl)
  192. (matlab-shell-run-region (point-min) (point-max))
  193. (message "Waiting for Matlab Emacs Link")
  194. (while (file-exists-p wait-file) (sit-for 0.01))
  195. "")) ;; matlab-shell-run-region doesn't seem to
  196. ;; make *matlab* buffer contents easily
  197. ;; available, so :results output currently
  198. ;; won't work
  199. (org-babel-comint-with-output
  200. (session
  201. (if matlabp
  202. org-babel-octave-eoe-indicator
  203. org-babel-octave-eoe-output)
  204. t full-body)
  205. (insert full-body) (comint-send-input nil t))))
  206. results)
  207. (pcase result-type
  208. (`value
  209. (org-babel-octave-import-elisp-from-file tmp-file))
  210. (`output
  211. (setq results
  212. (if matlabp
  213. (cdr (reverse (delq "" (mapcar #'org-strip-quotes
  214. (mapcar #'org-trim raw)))))
  215. (cdr (member org-babel-octave-eoe-output
  216. (reverse (mapcar #'org-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. (when (< (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