ob-ruby.el 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. ;;; ob-ruby.el --- org-babel functions for ruby evaluation
  2. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://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 <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating ruby source code.
  19. ;;; Requirements:
  20. ;; - ruby and irb executables :: http://www.ruby-lang.org/
  21. ;;
  22. ;; - ruby-mode :: Can be installed through ELPA, or from
  23. ;; http://github.com/eschulte/rinari/raw/master/util/ruby-mode.el
  24. ;;
  25. ;; - inf-ruby mode :: Can be installed through ELPA, or from
  26. ;; http://github.com/eschulte/rinari/raw/master/util/inf-ruby.el
  27. ;;; Code:
  28. (require 'ob)
  29. (require 'ob-ref)
  30. (require 'ob-comint)
  31. (require 'ob-eval)
  32. (eval-when-compile (require 'cl))
  33. (declare-function run-ruby "ext:inf-ruby" (&optional command name))
  34. (declare-function xmp "ext:rcodetools" (&optional option))
  35. (defvar org-babel-tangle-lang-exts)
  36. (add-to-list 'org-babel-tangle-lang-exts '("ruby" . "rb"))
  37. (defvar org-babel-default-header-args:ruby '())
  38. (defvar org-babel-ruby-command "ruby"
  39. "Name of command to use for executing ruby code.")
  40. (defun org-babel-execute:ruby (body params)
  41. "Execute a block of Ruby code with Babel.
  42. This function is called by `org-babel-execute-src-block'."
  43. (let* ((session (org-babel-ruby-initiate-session
  44. (cdr (assoc :session params))))
  45. (result-params (cdr (assoc :result-params params)))
  46. (result-type (cdr (assoc :result-type params)))
  47. (full-body (org-babel-expand-body:generic
  48. body params (org-babel-variable-assignments:ruby params)))
  49. (result (if (member "xmp" result-params)
  50. (with-temp-buffer
  51. (require 'rcodetools)
  52. (insert full-body)
  53. (xmp (cdr (assoc :xmp-option params)))
  54. (buffer-string))
  55. (org-babel-ruby-evaluate
  56. session full-body result-type result-params))))
  57. (org-babel-reassemble-table
  58. result
  59. (org-babel-pick-name (cdr (assoc :colname-names params))
  60. (cdr (assoc :colnames params)))
  61. (org-babel-pick-name (cdr (assoc :rowname-names params))
  62. (cdr (assoc :rownames params))))))
  63. (defun org-babel-prep-session:ruby (session params)
  64. "Prepare SESSION according to the header arguments specified in PARAMS."
  65. ;; (message "params=%S" params) ;; debugging
  66. (let* ((session (org-babel-ruby-initiate-session session))
  67. (var-lines (org-babel-variable-assignments:ruby params)))
  68. (org-babel-comint-in-buffer session
  69. (sit-for .5) (goto-char (point-max))
  70. (mapc (lambda (var)
  71. (insert var) (comint-send-input nil t)
  72. (org-babel-comint-wait-for-output session)
  73. (sit-for .1) (goto-char (point-max))) var-lines))
  74. session))
  75. (defun org-babel-load-session:ruby (session body params)
  76. "Load BODY into SESSION."
  77. (save-window-excursion
  78. (let ((buffer (org-babel-prep-session:ruby session params)))
  79. (with-current-buffer buffer
  80. (goto-char (process-mark (get-buffer-process (current-buffer))))
  81. (insert (org-babel-chomp body)))
  82. buffer)))
  83. ;; helper functions
  84. (defun org-babel-variable-assignments:ruby (params)
  85. "Return list of ruby statements assigning the block's variables"
  86. (mapcar
  87. (lambda (pair)
  88. (format "%s=%s"
  89. (car pair)
  90. (org-babel-ruby-var-to-ruby (cdr pair))))
  91. (mapcar #'cdr (org-babel-get-header params :var))))
  92. (defun org-babel-ruby-var-to-ruby (var)
  93. "Convert VAR into a ruby variable.
  94. Convert an elisp value into a string of ruby source code
  95. specifying a variable of the same value."
  96. (if (listp var)
  97. (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
  98. (format "%S" var)))
  99. (defun org-babel-ruby-table-or-string (results)
  100. "Convert RESULTS into an appropriate elisp value.
  101. If RESULTS look like a table, then convert them into an
  102. Emacs-lisp table, otherwise return the results as a string."
  103. (org-babel-script-escape results))
  104. (defun org-babel-ruby-initiate-session (&optional session params)
  105. "Initiate a ruby session.
  106. If there is not a current inferior-process-buffer in SESSION
  107. then create one. Return the initialized session."
  108. (unless (string= session "none")
  109. (require 'inf-ruby)
  110. (let ((session-buffer (save-window-excursion
  111. (run-ruby nil session) (current-buffer))))
  112. (if (org-babel-comint-buffer-livep session-buffer)
  113. (progn (sit-for .25) session-buffer)
  114. (sit-for .5)
  115. (org-babel-ruby-initiate-session session)))))
  116. (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
  117. "String to indicate that evaluation has completed.")
  118. (defvar org-babel-ruby-f-write
  119. "File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
  120. (defvar org-babel-ruby-pp-f-write
  121. "File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
  122. (defvar org-babel-ruby-wrapper-method
  123. "
  124. def main()
  125. %s
  126. end
  127. results = main()
  128. File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
  129. ")
  130. (defvar org-babel-ruby-pp-wrapper-method
  131. "
  132. require 'pp'
  133. def main()
  134. %s
  135. end
  136. results = main()
  137. File.open('%s', 'w') do |f|
  138. $stdout = f
  139. pp results
  140. end
  141. ")
  142. (defun org-babel-ruby-evaluate
  143. (buffer body &optional result-type result-params)
  144. "Pass BODY to the Ruby process in BUFFER.
  145. If RESULT-TYPE equals 'output then return a list of the outputs
  146. of the statements in BODY, if RESULT-TYPE equals 'value then
  147. return the value of the last statement in BODY, as elisp."
  148. (if (not buffer)
  149. ;; external process evaluation
  150. (case result-type
  151. (output (org-babel-eval org-babel-ruby-command body))
  152. (value (let ((tmp-file (org-babel-temp-file "ruby-")))
  153. (org-babel-eval
  154. org-babel-ruby-command
  155. (format (if (member "pp" result-params)
  156. org-babel-ruby-pp-wrapper-method
  157. org-babel-ruby-wrapper-method)
  158. body (org-babel-process-file-name tmp-file 'noquote)))
  159. ((lambda (raw)
  160. (if (or (member "code" result-params)
  161. (member "pp" result-params))
  162. raw
  163. (org-babel-ruby-table-or-string raw)))
  164. (org-babel-eval-read-file tmp-file)))))
  165. ;; comint session evaluation
  166. (case result-type
  167. (output
  168. (mapconcat
  169. #'identity
  170. (butlast
  171. (split-string
  172. (mapconcat
  173. #'org-babel-trim
  174. (butlast
  175. (org-babel-comint-with-output
  176. (buffer org-babel-ruby-eoe-indicator t body)
  177. (mapc
  178. (lambda (line)
  179. (insert (org-babel-chomp line)) (comint-send-input nil t))
  180. (list body org-babel-ruby-eoe-indicator))
  181. (comint-send-input nil t)) 2)
  182. "\n") "[\r\n]")) "\n"))
  183. (value
  184. ((lambda (results)
  185. (if (or (member "code" result-params) (member "pp" result-params))
  186. results
  187. (org-babel-ruby-table-or-string results)))
  188. (let* ((tmp-file (org-babel-temp-file "ruby-"))
  189. (ppp (or (member "code" result-params)
  190. (member "pp" result-params))))
  191. (org-babel-comint-with-output
  192. (buffer org-babel-ruby-eoe-indicator t body)
  193. (when ppp (insert "require 'pp';") (comint-send-input nil t))
  194. (mapc
  195. (lambda (line)
  196. (insert (org-babel-chomp line)) (comint-send-input nil t))
  197. (append
  198. (list body)
  199. (if (not ppp)
  200. (list (format org-babel-ruby-f-write
  201. (org-babel-process-file-name tmp-file 'noquote)))
  202. (list
  203. "results=_" "require 'pp'" "orig_out = $stdout"
  204. (format org-babel-ruby-pp-f-write
  205. (org-babel-process-file-name tmp-file 'noquote))))
  206. (list org-babel-ruby-eoe-indicator)))
  207. (comint-send-input nil t))
  208. (org-babel-eval-read-file tmp-file)))))))
  209. (defun org-babel-ruby-read-string (string)
  210. "Strip \\\"s from around a ruby string."
  211. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  212. (match-string 1 string)
  213. string))
  214. (provide 'ob-ruby)
  215. ;;; ob-ruby.el ends here