ob-ruby.el 8.8 KB

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