ob-ruby.el 8.0 KB

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