Merge branch 'maint'
[org-mode.git] / lisp / ob-ruby.el
blob20fb418aa1f608af12bf0f8f0c86d612ce7e37ae
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/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating ruby source code.
28 ;;; Requirements:
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
38 ;;; Code:
39 (require 'ob)
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)
63 (with-temp-buffer
64 (require 'rcodetools)
65 (insert full-body)
66 (xmp (cdr (assoc :xmp-option params)))
67 (buffer-string))
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
72 result
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))
86 (mapc (lambda (var)
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))
90 session))
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)))
99 buffer)))
101 ;; helper functions
103 (defun org-babel-variable-assignments:ruby (params)
104 "Return list of ruby statements assigning the block's variables."
105 (mapcar
106 (lambda (pair)
107 (format "%s=%s"
108 (car pair)
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."
116 (if (listp var)
117 (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
118 (format "%S" 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")
131 (require 'inf-ruby)
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)
136 (sit-for .5)
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
147 def main()
150 results = main()
151 File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
153 (defvar org-babel-ruby-pp-wrapper-method
155 require 'pp'
156 def main()
159 results = main()
160 File.open('%s', 'w') do |f|
161 $stdout = f
162 pp results
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."
172 (if (not buffer)
173 ;; external process evaluation
174 (case result-type
175 (output (org-babel-eval org-babel-ruby-command body))
176 (value (let ((tmp-file (org-babel-temp-file "ruby-")))
177 (org-babel-eval
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)))
183 ((lambda (raw)
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
190 (case result-type
191 (output
192 (mapconcat
193 #'identity
194 (butlast
195 (split-string
196 (mapconcat
197 #'org-babel-trim
198 (butlast
199 (org-babel-comint-with-output
200 (buffer org-babel-ruby-eoe-indicator t body)
201 (mapc
202 (lambda (line)
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"))
207 (value
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))
214 (mapc
215 (lambda (line)
216 (insert (org-babel-chomp line)) (comint-send-input nil t))
217 (append
218 (list body)
219 (if (not ppp)
220 (list (format org-babel-ruby-f-write
221 (org-babel-process-file-name tmp-file 'noquote)))
222 (list
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)
234 string))
236 (provide 'ob-ruby)
240 ;;; ob-ruby.el ends here