ob-ruby.el 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. ;;; ob-ruby.el --- org-babel functions for ruby evaluation
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating ruby source code.
  20. ;;; Requirements:
  21. ;; - ruby and irb executables :: http://www.ruby-lang.org/
  22. ;;
  23. ;; - ruby-mode :: Can be installed through ELPA, or from
  24. ;; http://github.com/eschulte/rinari/raw/master/util/ruby-mode.el
  25. ;;
  26. ;; - inf-ruby mode :: Can be installed through ELPA, or from
  27. ;; http://github.com/eschulte/rinari/raw/master/util/inf-ruby.el
  28. ;;; Code:
  29. (require 'ob)
  30. (require 'ob-ref)
  31. (require 'ob-comint)
  32. (eval-when-compile
  33. (require 'cl))
  34. (declare-function run-ruby "inf-ruby" (&optional command name))
  35. (add-to-list 'org-babel-tangle-lang-exts '("ruby" . "rb"))
  36. (defvar org-babel-default-header-args:ruby '())
  37. (defun org-babel-expand-body:ruby (body params &optional processed-params)
  38. "Expand BODY according to PARAMS, return the expanded body."
  39. (require 'inf-ruby)
  40. (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
  41. (concat
  42. (mapconcat ;; define any variables
  43. (lambda (pair)
  44. (format "%s=%s"
  45. (car pair)
  46. (org-babel-ruby-var-to-ruby (cdr pair))))
  47. vars "\n") "\n" body "\n")))
  48. (defun org-babel-execute:ruby (body params)
  49. "Execute a block of Ruby code with org-babel. This function is
  50. called by `org-babel-execute-src-block'."
  51. (message "executing Ruby source code block")
  52. (let* ((processed-params (org-babel-process-params params))
  53. (session (org-babel-ruby-initiate-session (first processed-params)))
  54. (result-params (nth 2 processed-params))
  55. (result-type (nth 3 processed-params))
  56. (full-body (org-babel-expand-body:ruby
  57. body params processed-params))
  58. (result (org-babel-ruby-evaluate
  59. session full-body result-type result-params)))
  60. (or (cdr (assoc :file params))
  61. (org-babel-reassemble-table
  62. result
  63. (org-babel-pick-name (nth 4 processed-params)
  64. (cdr (assoc :colnames params)))
  65. (org-babel-pick-name (nth 5 processed-params)
  66. (cdr (assoc :rownames params)))))))
  67. (defun org-babel-prep-session:ruby (session params)
  68. "Prepare SESSION according to the header arguments specified in PARAMS."
  69. ;; (message "params=%S" params) ;; debugging
  70. (let* ((session (org-babel-ruby-initiate-session session))
  71. (vars (org-babel-ref-variables params))
  72. (var-lines (mapcar ;; define any variables
  73. (lambda (pair)
  74. (format "%s=%s"
  75. (car pair)
  76. (org-babel-ruby-var-to-ruby (cdr pair))))
  77. vars)))
  78. (org-babel-comint-in-buffer session
  79. (sit-for .5) (goto-char (point-max))
  80. (mapc (lambda (var)
  81. (insert var) (comint-send-input nil t)
  82. (org-babel-comint-wait-for-output session)
  83. (sit-for .1) (goto-char (point-max))) var-lines))
  84. session))
  85. (defun org-babel-load-session:ruby (session body params)
  86. "Load BODY into SESSION."
  87. (save-window-excursion
  88. (let ((buffer (org-babel-prep-session:ruby session params)))
  89. (with-current-buffer buffer
  90. (goto-char (process-mark (get-buffer-process (current-buffer))))
  91. (insert (org-babel-chomp body)))
  92. buffer)))
  93. ;; helper functions
  94. (defun org-babel-ruby-var-to-ruby (var)
  95. "Convert an elisp var into a string of ruby source code
  96. specifying a var of the same value."
  97. (if (listp var)
  98. (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
  99. (format "%S" var)))
  100. (defun org-babel-ruby-table-or-string (results)
  101. "If RESULTS look like a table, then convert them into an
  102. Emacs-lisp table, otherwise return the results as a string."
  103. (message "converting %S" results)
  104. (org-babel-read
  105. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  106. (org-babel-read
  107. (concat "'"
  108. (replace-regexp-in-string
  109. "\\[" "(" (replace-regexp-in-string
  110. "\\]" ")" (replace-regexp-in-string
  111. ", " " " (replace-regexp-in-string
  112. "'" "\"" results))))))
  113. results)))
  114. (defun org-babel-ruby-initiate-session (&optional session params)
  115. "If there is not a current inferior-process-buffer in SESSION
  116. then create one. Return the initialized session."
  117. (require 'inf-ruby)
  118. (unless (string= session "none")
  119. (let ((session-buffer (save-window-excursion
  120. (run-ruby nil session) (current-buffer))))
  121. (if (org-babel-comint-buffer-livep session-buffer)
  122. (progn (sit-for .25) session-buffer)
  123. (sit-for .5)
  124. (org-babel-ruby-initiate-session session)))))
  125. (defvar org-babel-ruby-last-value-eval "_"
  126. "When evaluated by Ruby this returns the return value of the last statement.")
  127. (defvar org-babel-ruby-pp-last-value-eval "require 'pp'; pp(_)"
  128. "When evaluated by Ruby this pretty prints value of the last statement.")
  129. (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
  130. "Used to indicate that evaluation is has completed.")
  131. (defvar org-babel-ruby-wrapper-method
  132. "
  133. def main()
  134. %s
  135. end
  136. results = main()
  137. File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
  138. ")
  139. (defvar org-babel-ruby-pp-wrapper-method
  140. "
  141. require 'pp'
  142. def main()
  143. %s
  144. end
  145. results = main()
  146. File.open('%s', 'w') do |f|
  147. $stdout = f
  148. pp results
  149. end
  150. ")
  151. (defun org-babel-ruby-evaluate
  152. (buffer body &optional result-type result-params)
  153. "Pass BODY to the Ruby process in BUFFER. If RESULT-TYPE equals
  154. 'output then return a list of the outputs of the statements in
  155. BODY, if RESULT-TYPE equals 'value then return the value of the
  156. last statement in BODY, as elisp."
  157. (if (not buffer)
  158. ;; external process evaluation
  159. (save-excursion
  160. (case result-type
  161. (output
  162. (with-temp-buffer
  163. (insert body)
  164. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  165. (org-babel-shell-command-on-region
  166. (point-min) (point-max) "ruby" 'current-buffer 'replace)
  167. (buffer-string)))
  168. (value
  169. (let* ((tmp-file (make-temp-file "ruby-functional-results"))
  170. exit-code
  171. (stderr
  172. (with-temp-buffer
  173. (insert (format (if (member "pp" result-params)
  174. org-babel-ruby-pp-wrapper-method
  175. org-babel-ruby-wrapper-method)
  176. body tmp-file))
  177. (setq exit-code
  178. (org-babel-shell-command-on-region
  179. (point-min) (point-max) "ruby"
  180. nil 'replace (current-buffer)))
  181. (buffer-string))))
  182. (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
  183. (let ((raw (with-temp-buffer
  184. (insert-file-contents
  185. (org-babel-maybe-remote-file tmp-file))
  186. (buffer-string))))
  187. (if (or (member "code" result-params)
  188. (member "pp" result-params))
  189. raw
  190. (org-babel-ruby-table-or-string raw)))))))
  191. ;; comint session evaluation
  192. (let* ((full-body
  193. (mapconcat
  194. #'org-babel-chomp
  195. (list body (if (member "pp" result-params)
  196. org-babel-ruby-pp-last-value-eval
  197. org-babel-ruby-last-value-eval)
  198. org-babel-ruby-eoe-indicator) "\n"))
  199. (raw (org-babel-comint-with-output
  200. (buffer org-babel-ruby-eoe-indicator t full-body)
  201. (insert full-body) (comint-send-input nil t)))
  202. (results (cdr (member
  203. org-babel-ruby-eoe-indicator
  204. (reverse (mapcar #'org-babel-ruby-read-string
  205. (mapcar #'org-babel-trim raw)))))))
  206. (case result-type
  207. (output (mapconcat #'identity (reverse (cdr results)) "\n"))
  208. (value
  209. (if (or (member "code" result-params) (member "pp" result-params))
  210. (car results)
  211. (org-babel-ruby-table-or-string (car results))))))))
  212. (defun org-babel-ruby-read-string (string)
  213. "Strip \\\"s from around a ruby string."
  214. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  215. (match-string 1 string)
  216. string))
  217. (provide 'ob-ruby)
  218. ;; arch-tag: 3e9726db-4520-49e2-b263-e8f571ac88f5
  219. ;;; ob-ruby.el ends here