ob-ruby.el 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. ;;; ob-ruby.el --- Babel Functions for Ruby -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2015 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. (defcustom org-babel-ruby-hline-to "nil"
  38. "Replace hlines in incoming tables with this when translating to ruby."
  39. :group 'org-babel
  40. :version "24.4"
  41. :package-version '(Org . "8.0")
  42. :type 'string)
  43. (defcustom org-babel-ruby-nil-to 'hline
  44. "Replace nil in ruby tables with this before returning."
  45. :group 'org-babel
  46. :version "24.4"
  47. :package-version '(Org . "8.0")
  48. :type 'symbol)
  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* ((session (org-babel-ruby-initiate-session
  53. (cdr (assoc :session params))))
  54. (result-params (cdr (assoc :result-params params)))
  55. (result-type (cdr (assoc :result-type params)))
  56. (full-body (org-babel-expand-body:generic
  57. body params (org-babel-variable-assignments:ruby params)))
  58. (result (if (member "xmp" result-params)
  59. (with-temp-buffer
  60. (require 'rcodetools)
  61. (insert full-body)
  62. (xmp (cdr (assoc :xmp-option params)))
  63. (buffer-string))
  64. (org-babel-ruby-evaluate
  65. session full-body result-type result-params))))
  66. (org-babel-reassemble-table
  67. (org-babel-result-cond result-params
  68. result
  69. (org-babel-ruby-table-or-string result))
  70. (org-babel-pick-name (cdr (assoc :colname-names params))
  71. (cdr (assoc :colnames params)))
  72. (org-babel-pick-name (cdr (assoc :rowname-names params))
  73. (cdr (assoc :rownames params))))))
  74. (defun org-babel-prep-session:ruby (session params)
  75. "Prepare SESSION according to the header arguments specified in PARAMS."
  76. ;; (message "params=%S" params) ;; debugging
  77. (let* ((session (org-babel-ruby-initiate-session session))
  78. (var-lines (org-babel-variable-assignments:ruby params)))
  79. (org-babel-comint-in-buffer session
  80. (sit-for .5) (goto-char (point-max))
  81. (mapc (lambda (var)
  82. (insert var) (comint-send-input nil t)
  83. (org-babel-comint-wait-for-output session)
  84. (sit-for .1) (goto-char (point-max))) var-lines))
  85. session))
  86. (defun org-babel-load-session:ruby (session body params)
  87. "Load BODY into SESSION."
  88. (save-window-excursion
  89. (let ((buffer (org-babel-prep-session:ruby session params)))
  90. (with-current-buffer buffer
  91. (goto-char (process-mark (get-buffer-process (current-buffer))))
  92. (insert (org-babel-chomp body)))
  93. buffer)))
  94. ;; helper functions
  95. (defun org-babel-variable-assignments:ruby (params)
  96. "Return list of ruby statements assigning the block's variables."
  97. (mapcar
  98. (lambda (pair)
  99. (format "%s=%s"
  100. (car pair)
  101. (org-babel-ruby-var-to-ruby (cdr pair))))
  102. (mapcar #'cdr (org-babel-get-header params :var))))
  103. (defun org-babel-ruby-var-to-ruby (var)
  104. "Convert VAR into a ruby variable.
  105. Convert an elisp value into a string of ruby source code
  106. specifying a variable of the same value."
  107. (if (listp var)
  108. (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
  109. (if (equal var 'hline)
  110. org-babel-ruby-hline-to
  111. (format "%S" var))))
  112. (defun org-babel-ruby-table-or-string (results)
  113. "Convert RESULTS into an appropriate elisp value.
  114. If RESULTS look like a table, then convert them into an
  115. Emacs-lisp table, otherwise return the results as a string."
  116. (let ((res (org-babel-script-escape results)))
  117. (if (listp res)
  118. (mapcar (lambda (el) (if (equal el 'nil)
  119. org-babel-ruby-nil-to el))
  120. res)
  121. res)))
  122. (defun org-babel-ruby-initiate-session (&optional session _params)
  123. "Initiate a ruby session.
  124. If there is not a current inferior-process-buffer in SESSION
  125. then create one. Return the initialized session."
  126. (unless (string= session "none")
  127. (require 'inf-ruby)
  128. (let ((session-buffer (save-window-excursion
  129. (run-ruby nil session) (current-buffer))))
  130. (if (org-babel-comint-buffer-livep session-buffer)
  131. (progn (sit-for .25) session-buffer)
  132. (sit-for .5)
  133. (org-babel-ruby-initiate-session session)))))
  134. (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
  135. "String to indicate that evaluation has completed.")
  136. (defvar org-babel-ruby-f-write
  137. "File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
  138. (defvar org-babel-ruby-pp-f-write
  139. "File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
  140. (defvar org-babel-ruby-wrapper-method
  141. "
  142. def main()
  143. %s
  144. end
  145. results = main()
  146. File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
  147. ")
  148. (defvar org-babel-ruby-pp-wrapper-method
  149. "
  150. require 'pp'
  151. def main()
  152. %s
  153. end
  154. results = main()
  155. File.open('%s', 'w') do |f|
  156. $stdout = f
  157. pp results
  158. end
  159. ")
  160. (defun org-babel-ruby-evaluate
  161. (buffer body &optional result-type result-params)
  162. "Pass BODY to the Ruby process in BUFFER.
  163. If RESULT-TYPE equals `output' then return a list of the outputs
  164. of the statements in BODY, if RESULT-TYPE equals `value' then
  165. return the value of the last statement in BODY, as elisp."
  166. (if (not buffer)
  167. ;; external process evaluation
  168. (case result-type
  169. (output (org-babel-eval org-babel-ruby-command body))
  170. (value (let ((tmp-file (org-babel-temp-file "ruby-")))
  171. (org-babel-eval
  172. org-babel-ruby-command
  173. (format (if (member "pp" result-params)
  174. org-babel-ruby-pp-wrapper-method
  175. org-babel-ruby-wrapper-method)
  176. body (org-babel-process-file-name tmp-file 'noquote)))
  177. (org-babel-eval-read-file tmp-file))))
  178. ;; comint session evaluation
  179. (case result-type
  180. (output
  181. (let ((eoe-string (format "puts \"%s\"" org-babel-ruby-eoe-indicator)))
  182. ;; Force the session to be ready before the actual session
  183. ;; code is run. There is some problem in comint that will
  184. ;; sometimes show the prompt after the the input has already
  185. ;; been inserted and that throws off the extraction of the
  186. ;; result for Babel.
  187. (org-babel-comint-with-output
  188. (buffer org-babel-ruby-eoe-indicator t eoe-string)
  189. (insert eoe-string) (comint-send-input nil t))
  190. ;; Now we can start the evaluation.
  191. (mapconcat
  192. #'identity
  193. (butlast
  194. (split-string
  195. (mapconcat
  196. #'org-babel-trim
  197. (org-babel-comint-with-output
  198. (buffer org-babel-ruby-eoe-indicator t body)
  199. (mapc
  200. (lambda (line)
  201. (insert (org-babel-chomp line)) (comint-send-input nil t))
  202. (list "conf.echo=false;_org_prompt_mode=conf.prompt_mode;conf.prompt_mode=:NULL"
  203. body
  204. "conf.prompt_mode=_org_prompt_mode;conf.echo=true"
  205. eoe-string)))
  206. "\n") "[\r\n]") 4) "\n")))
  207. (value
  208. (let* ((tmp-file (org-babel-temp-file "ruby-"))
  209. (ppp (or (member "code" result-params)
  210. (member "pp" result-params))))
  211. (org-babel-comint-with-output
  212. (buffer org-babel-ruby-eoe-indicator t body)
  213. (when ppp (insert "require 'pp';") (comint-send-input nil t))
  214. (mapc
  215. (lambda (line)
  216. (insert (org-babel-chomp line)) (comint-send-input nil t))
  217. (append
  218. (list body)
  219. (if (not ppp)
  220. (list (format org-babel-ruby-f-write
  221. (org-babel-process-file-name tmp-file 'noquote)))
  222. (list
  223. "results=_" "require 'pp'" "orig_out = $stdout"
  224. (format org-babel-ruby-pp-f-write
  225. (org-babel-process-file-name tmp-file 'noquote))))
  226. (list org-babel-ruby-eoe-indicator)))
  227. (comint-send-input nil t))
  228. (org-babel-eval-read-file tmp-file))))))
  229. (defun org-babel-ruby-read-string (string)
  230. "Strip \\\"s from around a ruby string."
  231. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  232. (match-string 1 string)
  233. string))
  234. (provide 'ob-ruby)
  235. ;;; ob-ruby.el ends here