ob-ruby.el 8.3 KB

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