ob-ruby.el 9.1 KB

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