ob-ruby.el 8.4 KB

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