1 ;;; ob-ruby.el --- Babel Functions for Ruby -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 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
40 (eval-when-compile (require 'cl
))
42 (declare-function org-trim
"org" (s &optional keep-lead
))
43 (declare-function run-ruby
"ext:inf-ruby" (&optional command name
))
44 (declare-function xmp
"ext:rcodetools" (&optional option
))
46 (defvar org-babel-tangle-lang-exts
)
47 (add-to-list 'org-babel-tangle-lang-exts
'("ruby" .
"rb"))
49 (defvar org-babel-default-header-args
:ruby
'())
51 (defvar org-babel-ruby-command
"ruby"
52 "Name of command to use for executing ruby code.")
54 (defcustom org-babel-ruby-hline-to
"nil"
55 "Replace hlines in incoming tables with this when translating to ruby."
58 :package-version
'(Org .
"8.0")
61 (defcustom org-babel-ruby-nil-to
'hline
62 "Replace nil in ruby tables with this before returning."
65 :package-version
'(Org .
"8.0")
68 (defun org-babel-execute:ruby
(body params
)
69 "Execute a block of Ruby code with Babel.
70 This function is called by `org-babel-execute-src-block'."
71 (let* ((session (org-babel-ruby-initiate-session
72 (cdr (assoc :session params
))))
73 (result-params (cdr (assoc :result-params params
)))
74 (result-type (cdr (assoc :result-type params
)))
75 (full-body (org-babel-expand-body:generic
76 body params
(org-babel-variable-assignments:ruby params
)))
77 (result (if (member "xmp" result-params
)
81 (xmp (cdr (assoc :xmp-option params
)))
83 (org-babel-ruby-evaluate
84 session full-body result-type result-params
))))
85 (org-babel-reassemble-table
86 (org-babel-result-cond result-params
88 (org-babel-ruby-table-or-string result
))
89 (org-babel-pick-name (cdr (assoc :colname-names params
))
90 (cdr (assoc :colnames params
)))
91 (org-babel-pick-name (cdr (assoc :rowname-names params
))
92 (cdr (assoc :rownames params
))))))
94 (defun org-babel-prep-session:ruby
(session params
)
95 "Prepare SESSION according to the header arguments specified in PARAMS."
96 ;; (message "params=%S" params) ;; debugging
97 (let* ((session (org-babel-ruby-initiate-session session
))
98 (var-lines (org-babel-variable-assignments:ruby params
)))
99 (org-babel-comint-in-buffer session
100 (sit-for .5) (goto-char (point-max))
102 (insert var
) (comint-send-input nil t
)
103 (org-babel-comint-wait-for-output session
)
104 (sit-for .1) (goto-char (point-max))) var-lines
))
107 (defun org-babel-load-session:ruby
(session body params
)
108 "Load BODY into SESSION."
109 (save-window-excursion
110 (let ((buffer (org-babel-prep-session:ruby session params
)))
111 (with-current-buffer buffer
112 (goto-char (process-mark (get-buffer-process (current-buffer))))
113 (insert (org-babel-chomp body
)))
118 (defun org-babel-variable-assignments:ruby
(params)
119 "Return list of ruby statements assigning the block's variables."
124 (org-babel-ruby-var-to-ruby (cdr pair
))))
125 (org-babel--get-vars params
)))
127 (defun org-babel-ruby-var-to-ruby (var)
128 "Convert VAR into a ruby variable.
129 Convert an elisp value into a string of ruby source code
130 specifying a variable of the same value."
132 (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var
", ") "]")
133 (if (equal var
'hline
)
134 org-babel-ruby-hline-to
137 (defun org-babel-ruby-table-or-string (results)
138 "Convert RESULTS into an appropriate elisp value.
139 If RESULTS look like a table, then convert them into an
140 Emacs-lisp table, otherwise return the results as a string."
141 (let ((res (org-babel-script-escape results
)))
143 (mapcar (lambda (el) (if (equal el
'nil
)
144 org-babel-ruby-nil-to el
))
148 (defun org-babel-ruby-initiate-session (&optional session _params
)
149 "Initiate a ruby session.
150 If there is not a current inferior-process-buffer in SESSION
151 then create one. Return the initialized session."
152 (unless (string= session
"none")
154 (let ((session-buffer (save-window-excursion
155 (run-ruby nil session
) (current-buffer))))
156 (if (org-babel-comint-buffer-livep session-buffer
)
157 (progn (sit-for .25) session-buffer
)
159 (org-babel-ruby-initiate-session session
)))))
161 (defvar org-babel-ruby-eoe-indicator
":org_babel_ruby_eoe"
162 "String to indicate that evaluation has completed.")
163 (defvar org-babel-ruby-f-write
164 "File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
165 (defvar org-babel-ruby-pp-f-write
166 "File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
167 (defvar org-babel-ruby-wrapper-method
173 File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
175 (defvar org-babel-ruby-pp-wrapper-method
182 File.open('%s', 'w') do |f|
188 (defun org-babel-ruby-evaluate
189 (buffer body
&optional result-type result-params
)
190 "Pass BODY to the Ruby process in BUFFER.
191 If RESULT-TYPE equals `output' then return a list of the outputs
192 of the statements in BODY, if RESULT-TYPE equals `value' then
193 return the value of the last statement in BODY, as elisp."
195 ;; external process evaluation
197 (output (org-babel-eval org-babel-ruby-command body
))
198 (value (let ((tmp-file (org-babel-temp-file "ruby-")))
200 org-babel-ruby-command
201 (format (if (member "pp" result-params
)
202 org-babel-ruby-pp-wrapper-method
203 org-babel-ruby-wrapper-method
)
204 body
(org-babel-process-file-name tmp-file
'noquote
)))
205 (org-babel-eval-read-file tmp-file
))))
206 ;; comint session evaluation
209 (let ((eoe-string (format "puts \"%s\"" org-babel-ruby-eoe-indicator
)))
210 ;; Force the session to be ready before the actual session
211 ;; code is run. There is some problem in comint that will
212 ;; sometimes show the prompt after the the input has already
213 ;; been inserted and that throws off the extraction of the
215 (org-babel-comint-with-output
216 (buffer org-babel-ruby-eoe-indicator t eoe-string
)
217 (insert eoe-string
) (comint-send-input nil t
))
218 ;; Now we can start the evaluation.
225 (org-babel-comint-with-output
226 (buffer org-babel-ruby-eoe-indicator t body
)
229 (insert (org-babel-chomp line
)) (comint-send-input nil t
))
230 (list "conf.echo=false;_org_prompt_mode=conf.prompt_mode;conf.prompt_mode=:NULL"
232 "conf.prompt_mode=_org_prompt_mode;conf.echo=true"
234 "\n") "[\r\n]") 4) "\n")))
236 (let* ((tmp-file (org-babel-temp-file "ruby-"))
237 (ppp (or (member "code" result-params
)
238 (member "pp" result-params
))))
239 (org-babel-comint-with-output
240 (buffer org-babel-ruby-eoe-indicator t body
)
241 (when ppp
(insert "require 'pp';") (comint-send-input nil t
))
244 (insert (org-babel-chomp line
)) (comint-send-input nil t
))
248 (list (format org-babel-ruby-f-write
249 (org-babel-process-file-name tmp-file
'noquote
)))
251 "results=_" "require 'pp'" "orig_out = $stdout"
252 (format org-babel-ruby-pp-f-write
253 (org-babel-process-file-name tmp-file
'noquote
))))
254 (list org-babel-ruby-eoe-indicator
)))
255 (comint-send-input nil t
))
256 (org-babel-eval-read-file tmp-file
))))))
258 (defun org-babel-ruby-read-string (string)
259 "Strip \\\"s from around a ruby string."
260 (if (string-match "^\"\\([^\000]+\\)\"$" string
)
261 (match-string 1 string
)
268 ;;; ob-ruby.el ends here