ob-ruby.el 9.3 KB

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