ob-ruby.el 9.1 KB

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