ob-ruby.el 8.1 KB

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