ob-ruby.el 9.2 KB

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