ob-ruby.el 8.6 KB

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