1 ;;; ob-ruby.el --- Babel Functions for Ruby -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2017 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Org-Babel support for evaluating ruby source code.
30 ;; - ruby and irb executables :: http://www.ruby-lang.org/
32 ;; - ruby-mode :: Can be installed through ELPA, or from
33 ;; http://github.com/eschulte/rinari/raw/master/util/ruby-mode.el
35 ;; - inf-ruby mode :: Can be installed through ELPA, or from
36 ;; http://github.com/eschulte/rinari/raw/master/util/inf-ruby.el
41 (declare-function org-trim
"org" (s &optional keep-lead
))
42 (declare-function run-ruby
"ext:inf-ruby" (&optional command name
))
43 (declare-function xmp
"ext:rcodetools" (&optional option
))
45 (defvar org-babel-tangle-lang-exts
)
46 (add-to-list 'org-babel-tangle-lang-exts
'("ruby" .
"rb"))
48 (defvar org-babel-default-header-args
:ruby
'())
50 (defvar org-babel-ruby-command
"ruby"
51 "Name of command to use for executing ruby code.")
53 (defcustom org-babel-ruby-hline-to
"nil"
54 "Replace hlines in incoming tables with this when translating to ruby."
57 :package-version
'(Org .
"8.0")
60 (defcustom org-babel-ruby-nil-to
'hline
61 "Replace nil in ruby tables with this before returning."
64 :package-version
'(Org .
"8.0")
67 (defun org-babel-execute:ruby
(body params
)
68 "Execute a block of Ruby code with Babel.
69 This function is called by `org-babel-execute-src-block'."
70 (let* ((session (org-babel-ruby-initiate-session
71 (cdr (assq :session params
))))
72 (result-params (cdr (assq :result-params params
)))
73 (result-type (cdr (assq :result-type params
)))
74 (full-body (org-babel-expand-body:generic
75 body params
(org-babel-variable-assignments:ruby params
)))
76 (result (if (member "xmp" result-params
)
80 (xmp (cdr (assq :xmp-option params
)))
82 (org-babel-ruby-evaluate
83 session full-body result-type result-params
))))
84 (org-babel-reassemble-table
85 (org-babel-result-cond result-params
87 (org-babel-ruby-table-or-string result
))
88 (org-babel-pick-name (cdr (assq :colname-names params
))
89 (cdr (assq :colnames params
)))
90 (org-babel-pick-name (cdr (assq :rowname-names params
))
91 (cdr (assq :rownames params
))))))
93 (defun org-babel-prep-session:ruby
(session params
)
94 "Prepare SESSION according to the header arguments specified in PARAMS."
95 ;; (message "params=%S" params) ;; debugging
96 (let* ((session (org-babel-ruby-initiate-session session
))
97 (var-lines (org-babel-variable-assignments:ruby params
)))
98 (org-babel-comint-in-buffer session
99 (sit-for .5) (goto-char (point-max))
101 (insert var
) (comint-send-input nil t
)
102 (org-babel-comint-wait-for-output session
)
103 (sit-for .1) (goto-char (point-max))) var-lines
))
106 (defun org-babel-load-session:ruby
(session body params
)
107 "Load BODY into SESSION."
108 (save-window-excursion
109 (let ((buffer (org-babel-prep-session:ruby session params
)))
110 (with-current-buffer buffer
111 (goto-char (process-mark (get-buffer-process (current-buffer))))
112 (insert (org-babel-chomp body
)))
117 (defun org-babel-variable-assignments:ruby
(params)
118 "Return list of ruby statements assigning the block's variables."
123 (org-babel-ruby-var-to-ruby (cdr pair
))))
124 (org-babel--get-vars params
)))
126 (defun org-babel-ruby-var-to-ruby (var)
127 "Convert VAR into a ruby variable.
128 Convert an elisp value into a string of ruby source code
129 specifying a variable of the same value."
131 (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var
", ") "]")
133 org-babel-ruby-hline-to
136 (defun org-babel-ruby-table-or-string (results)
137 "Convert RESULTS into an appropriate elisp value.
138 If RESULTS look like a table, then convert them into an
139 Emacs-lisp table, otherwise return the results as a string."
140 (let ((res (org-babel-script-escape results
)))
142 (mapcar (lambda (el) (if (not el
)
143 org-babel-ruby-nil-to el
))
147 (defun org-babel-ruby-initiate-session (&optional session _params
)
148 "Initiate a ruby session.
149 If there is not a current inferior-process-buffer in SESSION
150 then create one. Return the initialized session."
151 (unless (string= session
"none")
153 (let* ((cmd (cdr (assoc inf-ruby-default-implementation
154 inf-ruby-implementations
)))
155 (buffer (get-buffer (format "*%s*" session
)))
156 (session-buffer (or buffer
(save-window-excursion
157 (run-ruby cmd session
)
159 (if (org-babel-comint-buffer-livep session-buffer
)
160 (progn (sit-for .25) session-buffer
)
162 (org-babel-ruby-initiate-session session
)))))
164 (defvar org-babel-ruby-eoe-indicator
":org_babel_ruby_eoe"
165 "String to indicate that evaluation has completed.")
166 (defvar org-babel-ruby-f-write
167 "File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
168 (defvar org-babel-ruby-pp-f-write
169 "File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
170 (defvar org-babel-ruby-wrapper-method
176 File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
178 (defvar org-babel-ruby-pp-wrapper-method
185 File.open('%s', 'w') do |f|
191 (defun org-babel-ruby-evaluate
192 (buffer body
&optional result-type result-params
)
193 "Pass BODY to the Ruby process in BUFFER.
194 If RESULT-TYPE equals `output' then return a list of the outputs
195 of the statements in BODY, if RESULT-TYPE equals `value' then
196 return the value of the last statement in BODY, as elisp."
198 ;; external process evaluation
200 (`output
(org-babel-eval org-babel-ruby-command body
))
201 (`value
(let ((tmp-file (org-babel-temp-file "ruby-")))
203 org-babel-ruby-command
204 (format (if (member "pp" result-params
)
205 org-babel-ruby-pp-wrapper-method
206 org-babel-ruby-wrapper-method
)
207 body
(org-babel-process-file-name tmp-file
'noquote
)))
208 (org-babel-eval-read-file tmp-file
))))
209 ;; comint session evaluation
212 (let ((eoe-string (format "puts \"%s\"" org-babel-ruby-eoe-indicator
)))
213 ;; Force the session to be ready before the actual session
214 ;; code is run. There is some problem in comint that will
215 ;; sometimes show the prompt after the the input has already
216 ;; been inserted and that throws off the extraction of the
218 (org-babel-comint-with-output
219 (buffer org-babel-ruby-eoe-indicator t eoe-string
)
220 (insert eoe-string
) (comint-send-input nil t
))
221 ;; Now we can start the evaluation.
228 (org-babel-comint-with-output
229 (buffer org-babel-ruby-eoe-indicator t body
)
232 (insert (org-babel-chomp line
)) (comint-send-input nil t
))
233 (list "conf.echo=false;_org_prompt_mode=conf.prompt_mode;conf.prompt_mode=:NULL"
235 "conf.prompt_mode=_org_prompt_mode;conf.echo=true"
237 "\n") "[\r\n]") 4) "\n")))
239 (let* ((tmp-file (org-babel-temp-file "ruby-"))
240 (ppp (or (member "code" result-params
)
241 (member "pp" result-params
))))
242 (org-babel-comint-with-output
243 (buffer org-babel-ruby-eoe-indicator t body
)
244 (when ppp
(insert "require 'pp';") (comint-send-input nil t
))
247 (insert (org-babel-chomp line
)) (comint-send-input nil t
))
251 (list (format org-babel-ruby-f-write
252 (org-babel-process-file-name tmp-file
'noquote
)))
254 "results=_" "require 'pp'" "orig_out = $stdout"
255 (format org-babel-ruby-pp-f-write
256 (org-babel-process-file-name tmp-file
'noquote
))))
257 (list org-babel-ruby-eoe-indicator
)))
258 (comint-send-input nil t
))
259 (org-babel-eval-read-file tmp-file
))))))
265 ;;; ob-ruby.el ends here