* lisp/simple.el (blink-matching-open): Use syntax-class.
[emacs.git] / lisp / org / ob-ruby.el
blobe557c80ef1a6eaa8c2985fddf46b92e0f9c21616
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.01
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-expand-body:ruby (body params &optional processed-params)
56 "Expand BODY according to PARAMS, return the expanded body."
57 (require 'inf-ruby)
58 (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
59 (concat
60 (mapconcat ;; define any variables
61 (lambda (pair)
62 (format "%s=%s"
63 (car pair)
64 (org-babel-ruby-var-to-ruby (cdr pair))))
65 vars "\n") "\n" body "\n")))
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* ((processed-params (org-babel-process-params params))
71 (session (org-babel-ruby-initiate-session (first processed-params)))
72 (result-params (nth 2 processed-params))
73 (result-type (nth 3 processed-params))
74 (full-body (org-babel-expand-body:ruby
75 body params processed-params))
76 (result (org-babel-ruby-evaluate
77 session full-body result-type result-params)))
78 (or (cdr (assoc :file params))
79 (org-babel-reassemble-table
80 result
81 (org-babel-pick-name (nth 4 processed-params)
82 (cdr (assoc :colnames params)))
83 (org-babel-pick-name (nth 5 processed-params)
84 (cdr (assoc :rownames params)))))))
86 (defun org-babel-prep-session:ruby (session params)
87 "Prepare SESSION according to the header arguments specified in PARAMS."
88 ;; (message "params=%S" params) ;; debugging
89 (let* ((session (org-babel-ruby-initiate-session session))
90 (vars (org-babel-ref-variables params))
91 (var-lines (mapcar ;; define any variables
92 (lambda (pair)
93 (format "%s=%s"
94 (car pair)
95 (org-babel-ruby-var-to-ruby (cdr pair))))
96 vars)))
97 (org-babel-comint-in-buffer session
98 (sit-for .5) (goto-char (point-max))
99 (mapc (lambda (var)
100 (insert var) (comint-send-input nil t)
101 (org-babel-comint-wait-for-output session)
102 (sit-for .1) (goto-char (point-max))) var-lines))
103 session))
105 (defun org-babel-load-session:ruby (session body params)
106 "Load BODY into SESSION."
107 (save-window-excursion
108 (let ((buffer (org-babel-prep-session:ruby session params)))
109 (with-current-buffer buffer
110 (goto-char (process-mark (get-buffer-process (current-buffer))))
111 (insert (org-babel-chomp body)))
112 buffer)))
114 ;; helper functions
116 (defun org-babel-ruby-var-to-ruby (var)
117 "Convert VAR into a ruby variable.
118 Convert an elisp value into a string of ruby source code
119 specifying a variable of the same value."
120 (if (listp var)
121 (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
122 (format "%S" var)))
124 (defun org-babel-ruby-table-or-string (results)
125 "Convert RESULTS into an appropriate elisp value.
126 If RESULTS look like a table, then convert them into an
127 Emacs-lisp table, otherwise return the results as a string."
128 (org-babel-read
129 (if (and (stringp results) (string-match "^\\[.+\\]$" results))
130 (org-babel-read
131 (concat "'"
132 (replace-regexp-in-string
133 "\\[" "(" (replace-regexp-in-string
134 "\\]" ")" (replace-regexp-in-string
135 ", " " " (replace-regexp-in-string
136 "'" "\"" results))))))
137 results)))
139 (defun org-babel-ruby-initiate-session (&optional session params)
140 "Initiate a ruby session.
141 If there is not a current inferior-process-buffer in SESSION
142 then create one. Return the initialized session."
143 (require 'inf-ruby)
144 (unless (string= session "none")
145 (let ((session-buffer (save-window-excursion
146 (run-ruby nil session) (current-buffer))))
147 (if (org-babel-comint-buffer-livep session-buffer)
148 (progn (sit-for .25) session-buffer)
149 (sit-for .5)
150 (org-babel-ruby-initiate-session session)))))
152 (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
153 "String to indicate that evaluation has completed.")
154 (defvar org-babel-ruby-f-write
155 "File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
156 (defvar org-babel-ruby-pp-f-write
157 "File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
158 (defvar org-babel-ruby-wrapper-method
160 def main()
163 results = main()
164 File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
166 (defvar org-babel-ruby-pp-wrapper-method
168 require 'pp'
169 def main()
172 results = main()
173 File.open('%s', 'w') do |f|
174 $stdout = f
175 pp results
179 (defun org-babel-ruby-evaluate
180 (buffer body &optional result-type result-params)
181 "Pass BODY to the Ruby process in BUFFER.
182 If RESULT-TYPE equals 'output then return a list of the outputs
183 of the statements in BODY, if RESULT-TYPE equals 'value then
184 return the value of the last statement in BODY, as elisp."
185 (if (not buffer)
186 ;; external process evaluation
187 (case result-type
188 (output (org-babel-eval org-babel-ruby-command body))
189 (value (let ((tmp-file (make-temp-file "org-babel-ruby-results-")))
190 (org-babel-eval org-babel-ruby-command
191 (format (if (member "pp" result-params)
192 org-babel-ruby-pp-wrapper-method
193 org-babel-ruby-wrapper-method)
194 body tmp-file))
195 ((lambda (raw)
196 (if (or (member "code" result-params)
197 (member "pp" result-params))
199 (org-babel-ruby-table-or-string raw)))
200 (org-babel-eval-read-file tmp-file)))))
201 ;; comint session evaluation
202 (case result-type
203 (output
204 (mapconcat
205 #'identity
206 (butlast
207 (split-string
208 (mapconcat
209 #'org-babel-trim
210 (butlast
211 (org-babel-comint-with-output
212 (buffer org-babel-ruby-eoe-indicator t body)
213 (mapc
214 (lambda (line)
215 (insert (org-babel-chomp line)) (comint-send-input nil t))
216 (list body org-babel-ruby-eoe-indicator))
217 (comint-send-input nil t)) 2)
218 "\n") "[\r\n]")) "\n"))
219 (value
220 ((lambda (results)
221 (if (or (member "code" result-params) (member "pp" result-params))
222 results
223 (org-babel-ruby-table-or-string results)))
224 (let* ((tmp-file (make-temp-file "org-babel-ruby-results-"))
225 (ppp (or (member "code" result-params)
226 (member "pp" result-params))))
227 (org-babel-comint-with-output
228 (buffer org-babel-ruby-eoe-indicator t body)
229 (when ppp (insert "require 'pp';") (comint-send-input nil t))
230 (mapc
231 (lambda (line)
232 (insert (org-babel-chomp line)) (comint-send-input nil t))
233 (append
234 (list body)
235 (if (not ppp)
236 (list (format org-babel-ruby-f-write tmp-file))
237 (list
238 "results=_" "require 'pp'" "orig_out = $stdout"
239 (format org-babel-ruby-pp-f-write tmp-file)))
240 (list org-babel-ruby-eoe-indicator)))
241 (comint-send-input nil t))
242 (org-babel-eval-read-file tmp-file)))))))
244 (defun org-babel-ruby-read-string (string)
245 "Strip \\\"s from around a ruby string."
246 (if (string-match "^\"\\([^\000]+\\)\"$" string)
247 (match-string 1 string)
248 string))
250 (provide 'ob-ruby)
252 ;; arch-tag: 3e9726db-4520-49e2-b263-e8f571ac88f5
254 ;;; ob-ruby.el ends here