Release 7.3
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-ruby.el
blob9a9f21cde602d723e496842e36b043cf32bd2cc6
1 ;;; ob-ruby.el --- org-babel functions for ruby evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.3
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating ruby source code.
29 ;;; Requirements:
31 ;; - ruby and irb executables :: http://www.ruby-lang.org/
32 ;;
33 ;; - ruby-mode :: Can be installed through ELPA, or from
34 ;; http://github.com/eschulte/rinari/raw/master/util/ruby-mode.el
35 ;;
36 ;; - inf-ruby mode :: Can be installed through ELPA, or from
37 ;; http://github.com/eschulte/rinari/raw/master/util/inf-ruby.el
39 ;;; Code:
40 (require 'ob)
41 (require 'ob-ref)
42 (require 'ob-comint)
43 (require 'ob-eval)
44 (eval-when-compile (require 'cl))
46 (declare-function run-ruby "ext:inf-ruby" (&optional command name))
48 (add-to-list 'org-babel-tangle-lang-exts '("ruby" . "rb"))
50 (defvar org-babel-default-header-args:ruby '())
52 (defvar org-babel-ruby-command "ruby"
53 "Name of command to use for executing ruby code.")
55 (defun org-babel-execute:ruby (body params)
56 "Execute a block of Ruby code with Babel.
57 This function is called by `org-babel-execute-src-block'."
58 (let* ((session (org-babel-ruby-initiate-session
59 (cdr (assoc :session params))))
60 (result-params (cdr (assoc :result-params params)))
61 (result-type (cdr (assoc :result-type params)))
62 (full-body (org-babel-expand-body:generic
63 body params (org-babel-variable-assignments:ruby params)))
64 (result (org-babel-ruby-evaluate
65 session full-body result-type result-params)))
66 (or (cdr (assoc :file params))
67 (org-babel-reassemble-table
68 result
69 (org-babel-pick-name (cdr (assoc :colname-names params))
70 (cdr (assoc :colnames params)))
71 (org-babel-pick-name (cdr (assoc :rowname-names params))
72 (cdr (assoc :rownames params)))))))
74 (defun org-babel-prep-session:ruby (session params)
75 "Prepare SESSION according to the header arguments specified in PARAMS."
76 ;; (message "params=%S" params) ;; debugging
77 (let* ((session (org-babel-ruby-initiate-session session))
78 (var-lines (org-babel-variable-assignments:ruby params)))
79 (org-babel-comint-in-buffer session
80 (sit-for .5) (goto-char (point-max))
81 (mapc (lambda (var)
82 (insert var) (comint-send-input nil t)
83 (org-babel-comint-wait-for-output session)
84 (sit-for .1) (goto-char (point-max))) var-lines))
85 session))
87 (defun org-babel-load-session:ruby (session body params)
88 "Load BODY into SESSION."
89 (save-window-excursion
90 (let ((buffer (org-babel-prep-session:ruby session params)))
91 (with-current-buffer buffer
92 (goto-char (process-mark (get-buffer-process (current-buffer))))
93 (insert (org-babel-chomp body)))
94 buffer)))
96 ;; helper functions
98 (defun org-babel-variable-assignments:ruby (params)
99 "Return list of ruby statements assigning the block's variables"
100 (mapcar
101 (lambda (pair)
102 (format "%s=%s"
103 (car pair)
104 (org-babel-ruby-var-to-ruby (cdr pair))))
105 (mapcar #'cdr (org-babel-get-header params :var))))
107 (defun org-babel-ruby-var-to-ruby (var)
108 "Convert VAR into a ruby variable.
109 Convert an elisp value into a string of ruby source code
110 specifying a variable of the same value."
111 (if (listp var)
112 (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
113 (format "%S" var)))
115 (defun org-babel-ruby-table-or-string (results)
116 "Convert RESULTS into an appropriate elisp value.
117 If RESULTS look like a table, then convert them into an
118 Emacs-lisp table, otherwise return the results as a string."
119 (org-babel-read
120 (if (and (stringp results) (string-match "^\\[.+\\]$" results))
121 (org-babel-read
122 (concat "'"
123 (replace-regexp-in-string
124 "\\[" "(" (replace-regexp-in-string
125 "\\]" ")" (replace-regexp-in-string
126 ", " " " (replace-regexp-in-string
127 "'" "\"" results))))))
128 results)))
130 (defun org-babel-ruby-initiate-session (&optional session params)
131 "Initiate a ruby session.
132 If there is not a current inferior-process-buffer in SESSION
133 then create one. Return the initialized session."
134 (require 'inf-ruby)
135 (unless (string= session "none")
136 (let ((session-buffer (save-window-excursion
137 (run-ruby nil session) (current-buffer))))
138 (if (org-babel-comint-buffer-livep session-buffer)
139 (progn (sit-for .25) session-buffer)
140 (sit-for .5)
141 (org-babel-ruby-initiate-session session)))))
143 (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
144 "String to indicate that evaluation has completed.")
145 (defvar org-babel-ruby-f-write
146 "File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
147 (defvar org-babel-ruby-pp-f-write
148 "File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
149 (defvar org-babel-ruby-wrapper-method
151 def main()
154 results = main()
155 File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
157 (defvar org-babel-ruby-pp-wrapper-method
159 require 'pp'
160 def main()
163 results = main()
164 File.open('%s', 'w') do |f|
165 $stdout = f
166 pp results
170 (defun org-babel-ruby-evaluate
171 (buffer body &optional result-type result-params)
172 "Pass BODY to the Ruby process in BUFFER.
173 If RESULT-TYPE equals 'output then return a list of the outputs
174 of the statements in BODY, if RESULT-TYPE equals 'value then
175 return the value of the last statement in BODY, as elisp."
176 (if (not buffer)
177 ;; external process evaluation
178 (case result-type
179 (output (org-babel-eval org-babel-ruby-command body))
180 (value (let ((tmp-file (org-babel-temp-file "ruby-")))
181 (org-babel-eval
182 org-babel-ruby-command
183 (format (if (member "pp" result-params)
184 org-babel-ruby-pp-wrapper-method
185 org-babel-ruby-wrapper-method)
186 body (org-babel-process-file-name tmp-file 'noquote)))
187 ((lambda (raw)
188 (if (or (member "code" result-params)
189 (member "pp" result-params))
191 (org-babel-ruby-table-or-string raw)))
192 (org-babel-eval-read-file tmp-file)))))
193 ;; comint session evaluation
194 (case result-type
195 (output
196 (mapconcat
197 #'identity
198 (butlast
199 (split-string
200 (mapconcat
201 #'org-babel-trim
202 (butlast
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 body org-babel-ruby-eoe-indicator))
209 (comint-send-input nil t)) 2)
210 "\n") "[\r\n]")) "\n"))
211 (value
212 ((lambda (results)
213 (if (or (member "code" result-params) (member "pp" result-params))
214 results
215 (org-babel-ruby-table-or-string results)))
216 (let* ((tmp-file (org-babel-temp-file "ruby-"))
217 (ppp (or (member "code" result-params)
218 (member "pp" result-params))))
219 (org-babel-comint-with-output
220 (buffer org-babel-ruby-eoe-indicator t body)
221 (when ppp (insert "require 'pp';") (comint-send-input nil t))
222 (mapc
223 (lambda (line)
224 (insert (org-babel-chomp line)) (comint-send-input nil t))
225 (append
226 (list body)
227 (if (not ppp)
228 (list (format org-babel-ruby-f-write
229 (org-babel-process-file-name tmp-file 'noquote)))
230 (list
231 "results=_" "require 'pp'" "orig_out = $stdout"
232 (format org-babel-ruby-pp-f-write
233 (org-babel-process-file-name tmp-file 'noquote))))
234 (list org-babel-ruby-eoe-indicator)))
235 (comint-send-input nil t))
236 (org-babel-eval-read-file tmp-file)))))))
238 (defun org-babel-ruby-read-string (string)
239 "Strip \\\"s from around a ruby string."
240 (if (string-match "^\"\\([^\000]+\\)\"$" string)
241 (match-string 1 string)
242 string))
244 (provide 'ob-ruby)
246 ;; arch-tag: 3e9726db-4520-49e2-b263-e8f571ac88f5
248 ;;; ob-ruby.el ends here