org-babel-octave.el 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 (org-babel-process-params 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, 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. (full-body
  138. (case result-type
  139. (output
  140. (mapconcat
  141. #'org-babel-chomp
  142. (list body org-babel-octave-eoe-indicator) "\n"))
  143. (value
  144. (mapconcat
  145. #'org-babel-chomp
  146. (list (format org-babel-octave-wrapper-method body tmp-file tmp-file) org-babel-octave-eoe-indicator) "\n"))))
  147. (raw (if (and matlabp org-babel-matlab-with-emacs-link)
  148. (save-window-excursion
  149. (with-temp-buffer
  150. (insert full-body)
  151. (matlab-shell-run-region (point-min) (point-max))
  152. "")) ;; matlab-shell-run-region doesn't seem to
  153. ;; make *matlab* buffer contents easily
  154. ;; available, so :results output currently
  155. ;; won't work
  156. (org-babel-comint-with-output session
  157. (if matlabp org-babel-octave-eoe-indicator org-babel-octave-eoe-output) t
  158. (insert full-body) (comint-send-input nil t)))) results)
  159. (case result-type
  160. (value
  161. (org-babel-octave-import-elisp-from-file (org-babel-maybe-remote-file tmp-file)))
  162. (output
  163. (progn
  164. (setq results
  165. (if matlabp
  166. (cdr (reverse (delq "" (mapcar #'org-babel-octave-read-string
  167. (mapcar #'org-babel-trim raw)))))
  168. (cdr (member org-babel-octave-eoe-output
  169. (reverse (mapcar #'org-babel-octave-read-string
  170. (mapcar #'org-babel-trim raw)))))))
  171. (mapconcat #'identity (reverse results) "\n"))))))
  172. (defun org-babel-octave-import-elisp-from-file (file-name)
  173. "Import data written to file by octave.
  174. This removes initial blank and comment lines and then calls
  175. `org-babel-import-elisp-from-file'."
  176. (let ((temp-file (make-temp-file "org-babel-results-")) beg end)
  177. (with-temp-file temp-file
  178. (insert-file-contents file-name)
  179. (re-search-forward "^[ \t]*[^# \t]" nil t)
  180. (if (< (setq beg (point-min))
  181. (setq end (point-at-bol)))
  182. (delete-region beg end)))
  183. (org-babel-import-elisp-from-file temp-file)))
  184. (defun org-babel-octave-read-string (string)
  185. "Strip \\\"s from around octave string"
  186. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  187. (match-string 1 string)
  188. string))
  189. (provide 'org-babel-octave)
  190. ;;; org-babel-octave.el ends here