ob-ruby.el 8.1 KB

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