ob-octave.el 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ;;; ob-octave.el --- org-babel functions for octave and matlab evaluation
  2. ;; Copyright (C) Dan Davison
  3. ;; Author: Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;;; Requirements:
  24. ;; octave
  25. ;; octave-mode.el and octave-inf.el come with GNU emacs
  26. ;;; Code:
  27. (require 'ob)
  28. (require 'octave-inf)
  29. (defvar org-babel-octave-shell-command "octave -q"
  30. "Shell command to use to run octave as an external process.")
  31. (defun org-babel-expand-body:octave (body params &optional processed-params)
  32. "Expand BODY according to PARAMS, return the expanded body."
  33. (let ((vars (second (or processed-params (org-babel-process-params params)))))
  34. (concat
  35. ;; prepend code to define all arguments passed to the code block
  36. ;; (may not be appropriate for all languages)
  37. (mapconcat
  38. (lambda (pair)
  39. (format "%s=%s"
  40. (car pair)
  41. (org-babel-octave-var-to-octave (cdr pair))))
  42. vars "\n") "\n" body "\n")))
  43. (defun org-babel-execute:octave (body params &optional matlabp)
  44. "Execute a block of octave code with org-babel."
  45. (message (format "executing %s source code block" (if matlabp "matlab" "octave")))
  46. (let* ((processed-params (org-babel-process-params params))
  47. ;; set the session if the session variable is non-nil
  48. (session (funcall (intern (format "org-babel-%s-initiate-session" lang))
  49. (first processed-params) params))
  50. (vars (second processed-params))
  51. (result-params (third processed-params))
  52. (result-type (fourth processed-params))
  53. (out-file (cdr (assoc :file params)))
  54. (augmented-body (org-babel-expand-body:octave body params processed-params))
  55. (result (org-babel-octave-evaluate session augmented-body result-type matlabp)))
  56. (or out-file
  57. (org-babel-reassemble-table
  58. result
  59. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  60. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params)))))))
  61. (defun org-babel-octave-var-to-octave (var)
  62. "Convert an emacs-lisp variable into an octave variable.
  63. Converts an emacs-lisp variable into a string of octave code
  64. specifying a variable of the same value."
  65. (if (listp var)
  66. (concat "[" (mapconcat #'org-babel-octave-var-to-octave var ", ") "]")
  67. (format "%S" var)))
  68. (defun org-babel-prep-session:octave (session params &optional matlabp)
  69. "Prepare SESSION according to the header arguments specified in PARAMS."
  70. (let* ((session (org-babel-octave-initiate-session session params matlabp))
  71. (vars (org-babel-ref-variables params))
  72. (var-lines (mapcar
  73. (lambda (pair)
  74. (format "%s=%s"
  75. (car pair)
  76. (org-babel-octave-var-to-octave (cdr pair))))
  77. vars)))
  78. (org-babel-comint-in-buffer session
  79. (mapc (lambda (var)
  80. (end-of-line 1) (insert var) (comint-send-input nil t)
  81. (org-babel-comint-wait-for-output session)) var-lines))
  82. session))
  83. (defun org-babel-octave-initiate-session (&optional session params matlabp)
  84. "Create an octave inferior process buffer. If there is not a
  85. current inferior-process-buffer in SESSION then create. Return
  86. the initialized session."
  87. (unless (string= session "none")
  88. (let ((session (or session (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
  89. (if (org-babel-comint-buffer-livep session) session
  90. (save-window-excursion
  91. (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
  92. (run-octave))
  93. (rename-buffer (if (bufferp session) (buffer-name session)
  94. (if (stringp session) session (buffer-name)))) (current-buffer))))))
  95. (defvar org-babel-octave-wrapper-method
  96. "%s
  97. if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
  98. else, save -ascii %s ans
  99. end")
  100. (defvar org-babel-octave-eoe-indicator "\'org_babel_eoe\'")
  101. (defvar org-babel-octave-eoe-output "ans = org_babel_eoe")
  102. (defun org-babel-octave-evaluate (session body result-type lang)
  103. "Pass BODY to the octave process in SESSION. If RESULT-TYPE
  104. equals 'output then return the outputs of the statements in BODY,
  105. if RESULT-TYPE equals 'value then return the value of the last
  106. statement in BODY, as elisp."
  107. (if session
  108. (org-babel-octave-evaluate-session session body result-type matlabp)
  109. (org-babel-octave-evaluate-external-process body result-type matlabp)))
  110. (defun org-babel-octave-evaluate-external-process (body result-type matlabp)
  111. "Evaluate BODY in an external octave process."
  112. (let ((cmd (if matlabp org-babel-matlab-shell-command org-babel-octave-shell-command)))
  113. (save-excursion
  114. (case result-type
  115. (output
  116. (with-temp-buffer
  117. (insert body)
  118. (org-babel-shell-command-on-region (point-min) (point-max) cmd 'current-buffer 'replace)
  119. (buffer-string)))
  120. (value
  121. (let* ((tmp-file (make-temp-file "org-babel-results-")) exit-code
  122. (stderr
  123. (with-temp-buffer
  124. (insert (format org-babel-octave-wrapper-method body tmp-file tmp-file))
  125. (setq exit-code (org-babel-shell-command-on-region
  126. (point-min) (point-max) cmd nil 'replace (current-buffer)))
  127. (buffer-string))))
  128. (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
  129. (org-babel-octave-import-elisp-from-file (org-babel-maybe-remote-file tmp-file))))))))
  130. (defun org-babel-octave-evaluate-session (session body result-type &optional matlabp)
  131. "Evaluate BODY in SESSION."
  132. (let* ((tmp-file (make-temp-file "org-babel-results-"))
  133. (wait-file (make-temp-file "org-babel-matlab-emacs-link-wait-signal-"))
  134. (full-body
  135. (case result-type
  136. (output
  137. (mapconcat
  138. #'org-babel-chomp
  139. (list body org-babel-octave-eoe-indicator) "\n"))
  140. (value
  141. (if (and matlabp org-babel-matlab-with-emacs-link)
  142. (concat
  143. (format org-babel-matlab-emacs-link-wrapper-method
  144. body tmp-file tmp-file wait-file) "\n")
  145. (mapconcat
  146. #'org-babel-chomp
  147. (list (format org-babel-octave-wrapper-method body tmp-file tmp-file)
  148. org-babel-octave-eoe-indicator) "\n")))))
  149. (raw (if (and matlabp org-babel-matlab-with-emacs-link)
  150. (save-window-excursion
  151. (with-temp-buffer
  152. (insert full-body)
  153. (write-region "" 'ignored wait-file nil nil nil 'excl)
  154. (matlab-shell-run-region (point-min) (point-max))
  155. (message "Waiting for Matlab Emacs Link")
  156. (while (file-exists-p wait-file) (sit-for 0.01))
  157. "")) ;; matlab-shell-run-region doesn't seem to
  158. ;; make *matlab* buffer contents easily
  159. ;; available, so :results output currently
  160. ;; won't work
  161. (org-babel-comint-with-output
  162. (session
  163. (if matlabp
  164. org-babel-octave-eoe-indicator
  165. org-babel-octave-eoe-output)
  166. t full-body)
  167. (insert full-body) (comint-send-input nil t)))) results)
  168. (case result-type
  169. (value
  170. (org-babel-octave-import-elisp-from-file (org-babel-maybe-remote-file tmp-file)))
  171. (output
  172. (progn
  173. (setq results
  174. (if matlabp
  175. (cdr (reverse (delq "" (mapcar #'org-babel-octave-read-string
  176. (mapcar #'org-babel-trim raw)))))
  177. (cdr (member org-babel-octave-eoe-output
  178. (reverse (mapcar #'org-babel-octave-read-string
  179. (mapcar #'org-babel-trim raw)))))))
  180. (mapconcat #'identity (reverse results) "\n"))))))
  181. (defun org-babel-octave-import-elisp-from-file (file-name)
  182. "Import data from FILE-NAME. This removes initial blank and
  183. comment lines and then calls `org-babel-import-elisp-from-file'."
  184. (let ((temp-file (make-temp-file "org-babel-results-")) beg end)
  185. (with-temp-file temp-file
  186. (insert-file-contents file-name)
  187. (re-search-forward "^[ \t]*[^# \t]" nil t)
  188. (if (< (setq beg (point-min))
  189. (setq end (point-at-bol)))
  190. (delete-region beg end)))
  191. (org-babel-import-elisp-from-file temp-file)))
  192. (defun org-babel-octave-read-string (string)
  193. "Strip \\\"s from around octave string"
  194. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  195. (match-string 1 string)
  196. string))
  197. (provide 'ob-octave)
  198. ;;; ob-octave.el ends here