org-babel-octave.el 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. ;;; org-babel-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 'org-babel)
  28. (require 'octave-inf)
  29. (org-babel-add-interpreter "octave")
  30. (add-to-list 'org-babel-tangle-langs '("octave" "m" "#!/usr/bin/env octave"))
  31. (defvar org-babel-octave-shell-command "octave -q"
  32. "Shell command to use to run octave as an external process.")
  33. (defun org-babel-expand-body:octave (body params &optional processed-params)
  34. (let ((vars (second (or processed-params (org-babel-process-params params)))))
  35. (concat
  36. ;; prepend code to define all arguments passed to the code block
  37. ;; (may not be appropriate for all languages)
  38. (mapconcat
  39. (lambda (pair)
  40. (format "%s=%s"
  41. (car pair)
  42. (org-babel-octave-var-to-octave (cdr pair))))
  43. vars "\n") "\n" body "\n")))
  44. (defun org-babel-execute:octave (body params &optional matlabp)
  45. "Execute a block of octave code with org-babel."
  46. (message (format "executing %s source code block" (if matlabp "matlab" "octave")))
  47. (let* ((processed-params (org-babel-process-params params))
  48. ;; set the session if the session variable is non-nil
  49. (session (funcall (intern (format "org-babel-%s-initiate-session" lang))
  50. (first processed-params) params))
  51. (vars (second processed-params))
  52. (result-params (third processed-params))
  53. (result-type (fourth processed-params))
  54. (out-file (cdr (assoc :file params)))
  55. (augmented-body (org-babel-expand-body:octave body params processed-params))
  56. (result (org-babel-octave-evaluate session augmented-body result-type matlabp)))
  57. (or out-file
  58. (org-babel-reassemble-table
  59. result
  60. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  61. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params)))))))
  62. (defun org-babel-octave-var-to-octave (var)
  63. "Convert an emacs-lisp variable into an octave variable.
  64. Converts an emacs-lisp variable into a string of octave code
  65. specifying a variable of the same value."
  66. (if (listp var)
  67. (concat "[" (mapconcat #'org-babel-octave-var-to-octave var ", ") "]")
  68. (format "%S" var)))
  69. (defun org-babel-prep-session:octave (session params &optional matlabp)
  70. "Prepare SESSION according to the header arguments specified in PARAMS."
  71. (let* ((session (org-babel-octave-initiate-session session params matlabp))
  72. (vars (org-babel-ref-variables params))
  73. (var-lines (mapcar
  74. (lambda (pair)
  75. (format "%s=%s"
  76. (car pair)
  77. (org-babel-octave-var-to-octave (cdr pair))))
  78. vars)))
  79. (org-babel-comint-in-buffer session
  80. (mapc (lambda (var)
  81. (move-end-of-line 1) (insert var) (comint-send-input nil t)
  82. (org-babel-comint-wait-for-output session)) var-lines))
  83. session))
  84. (defun org-babel-octave-initiate-session (&optional session params matlabp)
  85. "Create octave inferior process buffer.
  86. If there is not a current inferior-process-buffer in SESSION
  87. then create. Return the initialized session."
  88. (unless (string= session "none")
  89. (let ((session (or session (if matlabp "*Inferior Matlab*" "*Inferior Octave*"))))
  90. (if (org-babel-comint-buffer-livep session) session
  91. (save-window-excursion
  92. (if matlabp (unless org-babel-matlab-with-emacs-link (matlab-shell))
  93. (run-octave))
  94. (rename-buffer (if (bufferp session) (buffer-name session)
  95. (if (stringp session) session (buffer-name)))) (current-buffer))))))
  96. (defvar org-babel-octave-wrapper-method
  97. "%s
  98. if ischar(ans)
  99. fid = fopen('%s', 'w')
  100. fprintf(fid, '%%s', ans)
  101. fprintf(fid, '\\n')
  102. fclose(fid)
  103. else
  104. save -ascii %s ans
  105. end")
  106. (defvar org-babel-octave-eoe-indicator "\'org_babel_eoe\'")
  107. (defvar org-babel-octave-eoe-output "ans = org_babel_eoe")
  108. (defun org-babel-octave-evaluate (session body result-type lang)
  109. "Pass BODY to the octave process.
  110. If RESULT-TYPE equals 'output then return the outputs of the
  111. statements in BODY, if RESULT-TYPE equals 'value then return the
  112. value of the last statement in BODY, as elisp."
  113. (if session
  114. (org-babel-octave-evaluate-session session body result-type matlabp)
  115. (org-babel-octave-evaluate-external-process body result-type matlabp)))
  116. (defun org-babel-octave-evaluate-external-process (body result-type matlabp)
  117. (let ((cmd (if matlabp org-babel-matlab-shell-command org-babel-octave-shell-command)))
  118. (save-excursion
  119. (case result-type
  120. (output
  121. (with-temp-buffer
  122. (insert body)
  123. (org-babel-shell-command-on-region (point-min) (point-max) cmd 'current-buffer 'replace)
  124. (buffer-string)))
  125. (value
  126. (let* ((tmp-file (make-temp-file "org-babel-results-")) exit-code
  127. (stderr
  128. (with-temp-buffer
  129. (insert (format org-babel-octave-wrapper-method body tmp-file tmp-file))
  130. (setq exit-code (org-babel-shell-command-on-region
  131. (point-min) (point-max) cmd nil 'replace (current-buffer)))
  132. (buffer-string))))
  133. (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
  134. (org-babel-octave-import-elisp-from-file (org-babel-maybe-remote-file tmp-file))))))))
  135. (defun org-babel-octave-evaluate-session (session body result-type &optional matlabp)
  136. (let* ((tmp-file (make-temp-file "org-babel-results-"))
  137. (wait-file (make-temp-file "org-babel-matlab-emacs-link-wait-signal-"))
  138. (full-body
  139. (case result-type
  140. (output
  141. (mapconcat
  142. #'org-babel-chomp
  143. (list body org-babel-octave-eoe-indicator) "\n"))
  144. (value
  145. (if (and matlabp org-babel-matlab-with-emacs-link)
  146. (concat
  147. (format org-babel-matlab-emacs-link-wrapper-method
  148. body tmp-file tmp-file wait-file) "\n")
  149. (mapconcat
  150. #'org-babel-chomp
  151. (list (format org-babel-octave-wrapper-method body tmp-file tmp-file)
  152. org-babel-octave-eoe-indicator) "\n")))))
  153. (raw (if (and matlabp org-babel-matlab-with-emacs-link)
  154. (save-window-excursion
  155. (with-temp-buffer
  156. (insert full-body)
  157. (write-region "" 'ignored wait-file nil nil nil 'excl)
  158. (matlab-shell-run-region (point-min) (point-max))
  159. (message "Waiting for Matlab Emacs Link")
  160. (while (file-exists-p wait-file) (sit-for 0.01))
  161. "")) ;; matlab-shell-run-region doesn't seem to
  162. ;; make *matlab* buffer contents easily
  163. ;; available, so :results output currently
  164. ;; won't work
  165. (org-babel-comint-with-output session
  166. (if matlabp org-babel-octave-eoe-indicator org-babel-octave-eoe-output) t
  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 written to file by octave.
  183. This removes initial blank and comment lines and then calls
  184. `org-babel-import-elisp-from-file'."
  185. (let ((temp-file (make-temp-file "org-babel-results-")) beg end)
  186. (with-temp-file temp-file
  187. (insert-file-contents file-name)
  188. (re-search-forward "^[ \t]*[^# \t]" nil t)
  189. (if (< (setq beg (point-min))
  190. (setq end (point-at-bol)))
  191. (delete-region beg end)))
  192. (org-babel-import-elisp-from-file temp-file)))
  193. (defun org-babel-octave-read-string (string)
  194. "Strip \\\"s from around octave string"
  195. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  196. (match-string 1 string)
  197. string))
  198. (provide 'org-babel-octave)
  199. ;;; org-babel-octave.el ends here