1 ;;; ob-ruby.el --- org-babel functions for ruby evaluation
3 ;; Copyright (C) 2009-2013 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 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 (defun org-babel-execute:ruby
(body params
)
54 "Execute a block of Ruby code with Babel.
55 This function is called by `org-babel-execute-src-block'."
56 (let* ((session (org-babel-ruby-initiate-session
57 (cdr (assoc :session params
))))
58 (result-params (cdr (assoc :result-params params
)))
59 (result-type (cdr (assoc :result-type params
)))
60 (full-body (org-babel-expand-body:generic
61 body params
(org-babel-variable-assignments:ruby params
)))
62 (result (if (member "xmp" result-params
)
66 (xmp (cdr (assoc :xmp-option params
)))
68 (org-babel-ruby-evaluate
69 session full-body result-type result-params
))))
70 (org-babel-reassemble-table
71 (org-babel-result-cond result-params
73 (org-babel-ruby-table-or-string result
))
74 (org-babel-pick-name (cdr (assoc :colname-names params
))
75 (cdr (assoc :colnames params
)))
76 (org-babel-pick-name (cdr (assoc :rowname-names params
))
77 (cdr (assoc :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))
87 (insert var
) (comint-send-input nil t
)
88 (org-babel-comint-wait-for-output session
)
89 (sit-for .1) (goto-char (point-max))) var-lines
))
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
)))
103 (defun org-babel-variable-assignments:ruby
(params)
104 "Return list of ruby statements assigning the block's variables."
109 (org-babel-ruby-var-to-ruby (cdr pair
))))
110 (mapcar #'cdr
(org-babel-get-header params
:var
))))
112 (defun org-babel-ruby-var-to-ruby (var)
113 "Convert VAR into a ruby variable.
114 Convert an elisp value into a string of ruby source code
115 specifying a variable of the same value."
117 (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var
", ") "]")
120 (defun org-babel-ruby-table-or-string (results)
121 "Convert RESULTS into an appropriate elisp value.
122 If RESULTS look like a table, then convert them into an
123 Emacs-lisp table, otherwise return the results as a string."
124 (org-babel-script-escape results
))
126 (defun org-babel-ruby-initiate-session (&optional session params
)
127 "Initiate a ruby session.
128 If there is not a current inferior-process-buffer in SESSION
129 then create one. Return the initialized session."
130 (unless (string= session
"none")
132 (let ((session-buffer (save-window-excursion
133 (run-ruby nil session
) (current-buffer))))
134 (if (org-babel-comint-buffer-livep session-buffer
)
135 (progn (sit-for .25) session-buffer
)
137 (org-babel-ruby-initiate-session session
)))))
139 (defvar org-babel-ruby-eoe-indicator
":org_babel_ruby_eoe"
140 "String to indicate that evaluation has completed.")
141 (defvar org-babel-ruby-f-write
142 "File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
143 (defvar org-babel-ruby-pp-f-write
144 "File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
145 (defvar org-babel-ruby-wrapper-method
151 File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
153 (defvar org-babel-ruby-pp-wrapper-method
160 File.open('%s', 'w') do |f|
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."
173 ;; external process evaluation
175 (output (org-babel-eval org-babel-ruby-command body
))
176 (value (let ((tmp-file (org-babel-temp-file "ruby-")))
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
)))
184 (if (or (member "code" result-params
)
185 (member "pp" result-params
))
187 (org-babel-ruby-table-or-string raw
)))
188 (org-babel-eval-read-file tmp-file
)))))
189 ;; comint session evaluation
199 (org-babel-comint-with-output
200 (buffer org-babel-ruby-eoe-indicator t body
)
203 (insert (org-babel-chomp line
)) (comint-send-input nil t
))
204 (list body org-babel-ruby-eoe-indicator
))
205 (comint-send-input nil t
)) 2)
206 "\n") "[\r\n]")) "\n"))
208 (let* ((tmp-file (org-babel-temp-file "ruby-"))
209 (ppp (or (member "code" result-params
)
210 (member "pp" result-params
))))
211 (org-babel-comint-with-output
212 (buffer org-babel-ruby-eoe-indicator t body
)
213 (when ppp
(insert "require 'pp';") (comint-send-input nil t
))
216 (insert (org-babel-chomp line
)) (comint-send-input nil t
))
220 (list (format org-babel-ruby-f-write
221 (org-babel-process-file-name tmp-file
'noquote
)))
223 "results=_" "require 'pp'" "orig_out = $stdout"
224 (format org-babel-ruby-pp-f-write
225 (org-babel-process-file-name tmp-file
'noquote
))))
226 (list org-babel-ruby-eoe-indicator
)))
227 (comint-send-input nil t
))
228 (org-babel-eval-read-file tmp-file
))))))
230 (defun org-babel-ruby-read-string (string)
231 "Strip \\\"s from around a ruby string."
232 (if (string-match "^\"\\([^\000]+\\)\"$" string
)
233 (match-string 1 string
)
240 ;;; ob-ruby.el ends here