ob-ruby.el 9.2 KB

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