ob-ruby.el 9.5 KB

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