ob-ruby.el 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. ;;; ob-ruby.el --- Babel Functions for Ruby -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2021 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 :: https://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* ((command (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. (if (functionp command)
  143. (funcall command)
  144. command)
  145. (or session "ruby")
  146. (unless session
  147. (inf-ruby-buffer)))
  148. (current-buffer)))))
  149. (if (org-babel-comint-buffer-livep session-buffer)
  150. (progn (sit-for .25) session-buffer)
  151. (sit-for .5)
  152. (org-babel-ruby-initiate-session session)))))
  153. (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
  154. "String to indicate that evaluation has completed.")
  155. (defvar org-babel-ruby-f-write
  156. "File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
  157. (defvar org-babel-ruby-pp-f-write
  158. "File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
  159. (defvar org-babel-ruby-wrapper-method
  160. "
  161. def main()
  162. %s
  163. end
  164. results = main()
  165. File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
  166. ")
  167. (defvar org-babel-ruby-pp-wrapper-method
  168. "
  169. require 'pp'
  170. def main()
  171. %s
  172. end
  173. results = main()
  174. File.open('%s', 'w') do |f|
  175. $stdout = f
  176. pp results
  177. end
  178. ")
  179. (defun org-babel-ruby-evaluate
  180. (buffer body &optional result-type result-params)
  181. "Pass BODY to the Ruby process in BUFFER.
  182. If RESULT-TYPE equals `output' then return a list of the outputs
  183. of the statements in BODY, if RESULT-TYPE equals `value' then
  184. return the value of the last statement in BODY, as elisp."
  185. (if (not buffer)
  186. ;; external process evaluation
  187. (pcase result-type
  188. (`output (org-babel-eval org-babel-ruby-command body))
  189. (`value (let ((tmp-file (org-babel-temp-file "ruby-")))
  190. (org-babel-eval
  191. org-babel-ruby-command
  192. (format (if (member "pp" result-params)
  193. org-babel-ruby-pp-wrapper-method
  194. org-babel-ruby-wrapper-method)
  195. body (org-babel-process-file-name tmp-file 'noquote)))
  196. (org-babel-eval-read-file tmp-file))))
  197. ;; comint session evaluation
  198. (pcase result-type
  199. (`output
  200. (let ((eoe-string (format "puts \"%s\"" org-babel-ruby-eoe-indicator)))
  201. ;; Force the session to be ready before the actual session
  202. ;; code is run. There is some problem in comint that will
  203. ;; sometimes show the prompt after the input has already
  204. ;; been inserted and that throws off the extraction of the
  205. ;; result for Babel.
  206. (org-babel-comint-with-output
  207. (buffer org-babel-ruby-eoe-indicator t eoe-string)
  208. (insert eoe-string) (comint-send-input nil t))
  209. ;; Now we can start the evaluation.
  210. (mapconcat
  211. #'identity
  212. (butlast
  213. (split-string
  214. (mapconcat
  215. #'org-trim
  216. (org-babel-comint-with-output
  217. (buffer org-babel-ruby-eoe-indicator t body)
  218. (mapc
  219. (lambda (line)
  220. (insert (org-babel-chomp line)) (comint-send-input nil t))
  221. (list "conf.echo=false;_org_prompt_mode=conf.prompt_mode;conf.prompt_mode=:NULL"
  222. body
  223. "conf.prompt_mode=_org_prompt_mode;conf.echo=true"
  224. eoe-string)))
  225. "\n") "[\r\n]") 4) "\n")))
  226. (`value
  227. (let* ((tmp-file (org-babel-temp-file "ruby-"))
  228. (ppp (or (member "code" result-params)
  229. (member "pp" result-params))))
  230. (org-babel-comint-with-output
  231. (buffer org-babel-ruby-eoe-indicator t body)
  232. (when ppp (insert "require 'pp';") (comint-send-input nil t))
  233. (mapc
  234. (lambda (line)
  235. (insert (org-babel-chomp line)) (comint-send-input nil t))
  236. (append
  237. (list body)
  238. (if (not ppp)
  239. (list (format org-babel-ruby-f-write
  240. (org-babel-process-file-name tmp-file 'noquote)))
  241. (list
  242. "results=_" "require 'pp'" "orig_out = $stdout"
  243. (format org-babel-ruby-pp-f-write
  244. (org-babel-process-file-name tmp-file 'noquote))))
  245. (list org-babel-ruby-eoe-indicator)))
  246. (comint-send-input nil t))
  247. (org-babel-eval-read-file tmp-file))))))
  248. (provide 'ob-ruby)
  249. ;;; ob-ruby.el ends here