Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-ruby.el --- org-babel functions for ruby evaluation |
2 | ||
3ab2c837 | 3 | ;; Copyright (C) 2009, 2010 Free Software Foundation |
86fbb8ca CD |
4 | |
5 | ;; Author: Eric Schulte | |
6 | ;; Keywords: literate programming, reproducible research | |
7 | ;; Homepage: http://orgmode.org | |
3ab2c837 | 8 | ;; Version: 7.7 |
86fbb8ca CD |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation, either version 3 of the License, or | |
15 | ;; (at your option) any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Org-Babel support for evaluating ruby source code. | |
28 | ||
29 | ;;; Requirements: | |
30 | ||
31 | ;; - ruby and irb executables :: http://www.ruby-lang.org/ | |
acedf35c | 32 | ;; |
86fbb8ca CD |
33 | ;; - ruby-mode :: Can be installed through ELPA, or from |
34 | ;; http://github.com/eschulte/rinari/raw/master/util/ruby-mode.el | |
acedf35c | 35 | ;; |
86fbb8ca CD |
36 | ;; - inf-ruby mode :: Can be installed through ELPA, or from |
37 | ;; http://github.com/eschulte/rinari/raw/master/util/inf-ruby.el | |
38 | ||
39 | ;;; Code: | |
40 | (require 'ob) | |
41 | (require 'ob-ref) | |
42 | (require 'ob-comint) | |
43 | (require 'ob-eval) | |
44 | (eval-when-compile (require 'cl)) | |
45 | ||
46 | (declare-function run-ruby "ext:inf-ruby" (&optional command name)) | |
3ab2c837 | 47 | (declare-function xmp "ext:rcodetools" (&optional option)) |
86fbb8ca | 48 | |
3ab2c837 | 49 | (defvar org-babel-tangle-lang-exts) |
86fbb8ca CD |
50 | (add-to-list 'org-babel-tangle-lang-exts '("ruby" . "rb")) |
51 | ||
52 | (defvar org-babel-default-header-args:ruby '()) | |
53 | ||
54 | (defvar org-babel-ruby-command "ruby" | |
55 | "Name of command to use for executing ruby code.") | |
56 | ||
86fbb8ca CD |
57 | (defun org-babel-execute:ruby (body params) |
58 | "Execute a block of Ruby code with Babel. | |
59 | This function is called by `org-babel-execute-src-block'." | |
afe98dfa CD |
60 | (let* ((session (org-babel-ruby-initiate-session |
61 | (cdr (assoc :session params)))) | |
62 | (result-params (cdr (assoc :result-params params))) | |
63 | (result-type (cdr (assoc :result-type params))) | |
64 | (full-body (org-babel-expand-body:generic | |
65 | body params (org-babel-variable-assignments:ruby params))) | |
3ab2c837 BG |
66 | (result (if (member "xmp" result-params) |
67 | (with-temp-buffer | |
68 | (require 'rcodetools) | |
69 | (insert full-body) | |
70 | (xmp (cdr (assoc :xmp-option params))) | |
71 | (buffer-string)) | |
72 | (org-babel-ruby-evaluate | |
73 | session full-body result-type result-params)))) | |
74 | (org-babel-reassemble-table | |
75 | result | |
76 | (org-babel-pick-name (cdr (assoc :colname-names params)) | |
77 | (cdr (assoc :colnames params))) | |
78 | (org-babel-pick-name (cdr (assoc :rowname-names params)) | |
79 | (cdr (assoc :rownames params)))))) | |
86fbb8ca CD |
80 | |
81 | (defun org-babel-prep-session:ruby (session params) | |
82 | "Prepare SESSION according to the header arguments specified in PARAMS." | |
83 | ;; (message "params=%S" params) ;; debugging | |
84 | (let* ((session (org-babel-ruby-initiate-session session)) | |
afe98dfa | 85 | (var-lines (org-babel-variable-assignments:ruby params))) |
86fbb8ca CD |
86 | (org-babel-comint-in-buffer session |
87 | (sit-for .5) (goto-char (point-max)) | |
88 | (mapc (lambda (var) | |
89 | (insert var) (comint-send-input nil t) | |
90 | (org-babel-comint-wait-for-output session) | |
91 | (sit-for .1) (goto-char (point-max))) var-lines)) | |
92 | session)) | |
93 | ||
94 | (defun org-babel-load-session:ruby (session body params) | |
95 | "Load BODY into SESSION." | |
96 | (save-window-excursion | |
97 | (let ((buffer (org-babel-prep-session:ruby session params))) | |
98 | (with-current-buffer buffer | |
99 | (goto-char (process-mark (get-buffer-process (current-buffer)))) | |
100 | (insert (org-babel-chomp body))) | |
101 | buffer))) | |
102 | ||
103 | ;; helper functions | |
104 | ||
afe98dfa CD |
105 | (defun org-babel-variable-assignments:ruby (params) |
106 | "Return list of ruby statements assigning the block's variables" | |
107 | (mapcar | |
108 | (lambda (pair) | |
109 | (format "%s=%s" | |
110 | (car pair) | |
111 | (org-babel-ruby-var-to-ruby (cdr pair)))) | |
112 | (mapcar #'cdr (org-babel-get-header params :var)))) | |
113 | ||
86fbb8ca CD |
114 | (defun org-babel-ruby-var-to-ruby (var) |
115 | "Convert VAR into a ruby variable. | |
116 | Convert an elisp value into a string of ruby source code | |
117 | specifying a variable of the same value." | |
118 | (if (listp var) | |
119 | (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]") | |
120 | (format "%S" var))) | |
121 | ||
122 | (defun org-babel-ruby-table-or-string (results) | |
123 | "Convert RESULTS into an appropriate elisp value. | |
124 | If RESULTS look like a table, then convert them into an | |
125 | Emacs-lisp table, otherwise return the results as a string." | |
acedf35c | 126 | (org-babel-script-escape results)) |
86fbb8ca CD |
127 | |
128 | (defun org-babel-ruby-initiate-session (&optional session params) | |
129 | "Initiate a ruby session. | |
130 | If there is not a current inferior-process-buffer in SESSION | |
131 | then create one. Return the initialized session." | |
132 | (require 'inf-ruby) | |
133 | (unless (string= session "none") | |
134 | (let ((session-buffer (save-window-excursion | |
135 | (run-ruby nil session) (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 | ||
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 | ||
168 | (defun org-babel-ruby-evaluate | |
169 | (buffer body &optional result-type result-params) | |
170 | "Pass BODY to the Ruby process in BUFFER. | |
171 | If RESULT-TYPE equals 'output then return a list of the outputs | |
172 | of the statements in BODY, if RESULT-TYPE equals 'value then | |
173 | return the value of the last statement in BODY, as elisp." | |
174 | (if (not buffer) | |
175 | ;; external process evaluation | |
176 | (case result-type | |
177 | (output (org-babel-eval org-babel-ruby-command body)) | |
afe98dfa CD |
178 | (value (let ((tmp-file (org-babel-temp-file "ruby-"))) |
179 | (org-babel-eval | |
180 | org-babel-ruby-command | |
181 | (format (if (member "pp" result-params) | |
182 | org-babel-ruby-pp-wrapper-method | |
183 | org-babel-ruby-wrapper-method) | |
184 | body (org-babel-process-file-name tmp-file 'noquote))) | |
86fbb8ca CD |
185 | ((lambda (raw) |
186 | (if (or (member "code" result-params) | |
187 | (member "pp" result-params)) | |
188 | raw | |
189 | (org-babel-ruby-table-or-string raw))) | |
190 | (org-babel-eval-read-file tmp-file))))) | |
191 | ;; comint session evaluation | |
192 | (case result-type | |
193 | (output | |
194 | (mapconcat | |
195 | #'identity | |
196 | (butlast | |
197 | (split-string | |
198 | (mapconcat | |
199 | #'org-babel-trim | |
200 | (butlast | |
201 | (org-babel-comint-with-output | |
202 | (buffer org-babel-ruby-eoe-indicator t body) | |
203 | (mapc | |
204 | (lambda (line) | |
205 | (insert (org-babel-chomp line)) (comint-send-input nil t)) | |
206 | (list body org-babel-ruby-eoe-indicator)) | |
207 | (comint-send-input nil t)) 2) | |
208 | "\n") "[\r\n]")) "\n")) | |
209 | (value | |
210 | ((lambda (results) | |
211 | (if (or (member "code" result-params) (member "pp" result-params)) | |
212 | results | |
213 | (org-babel-ruby-table-or-string results))) | |
afe98dfa | 214 | (let* ((tmp-file (org-babel-temp-file "ruby-")) |
86fbb8ca CD |
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) | |
afe98dfa CD |
226 | (list (format org-babel-ruby-f-write |
227 | (org-babel-process-file-name tmp-file 'noquote))) | |
86fbb8ca CD |
228 | (list |
229 | "results=_" "require 'pp'" "orig_out = $stdout" | |
afe98dfa CD |
230 | (format org-babel-ruby-pp-f-write |
231 | (org-babel-process-file-name tmp-file 'noquote)))) | |
86fbb8ca CD |
232 | (list org-babel-ruby-eoe-indicator))) |
233 | (comint-send-input nil t)) | |
234 | (org-babel-eval-read-file tmp-file))))))) | |
235 | ||
236 | (defun org-babel-ruby-read-string (string) | |
237 | "Strip \\\"s from around a ruby string." | |
238 | (if (string-match "^\"\\([^\000]+\\)\"$" string) | |
239 | (match-string 1 string) | |
240 | string)) | |
241 | ||
242 | (provide 'ob-ruby) | |
243 | ||
5b409b39 | 244 | |
86fbb8ca CD |
245 | |
246 | ;;; ob-ruby.el ends here |