Merge branch 'maint'
[org-mode.git] / lisp / ob-ruby.el
blobaf528314393305682b3d274984d89e336a846d99
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 (defcustom org-babel-ruby-hline-to "nil"
54 "Replace hlines in incoming tables with this when translating to ruby."
55 :group 'org-babel
56 :version "24.4"
57 :package-version '(Org . "8.0")
58 :type 'string)
60 (defcustom org-babel-ruby-nil-to 'hline
61 "Replace 'nil' in ruby tables with this before returning."
62 :group 'org-babel
63 :version "24.4"
64 :package-version '(Org . "8.0")
65 :type 'string)
69 (defun org-babel-execute:ruby (body params)
70 "Execute a block of Ruby code with Babel.
71 This function is called by `org-babel-execute-src-block'."
72 (let* ((session (org-babel-ruby-initiate-session
73 (cdr (assoc :session params))))
74 (result-params (cdr (assoc :result-params params)))
75 (result-type (cdr (assoc :result-type params)))
76 (full-body (org-babel-expand-body:generic
77 body params (org-babel-variable-assignments:ruby params)))
78 (result (if (member "xmp" result-params)
79 (with-temp-buffer
80 (require 'rcodetools)
81 (insert full-body)
82 (xmp (cdr (assoc :xmp-option params)))
83 (buffer-string))
84 (org-babel-ruby-evaluate
85 session full-body result-type result-params))))
86 (org-babel-reassemble-table
87 (org-babel-result-cond result-params
88 result
89 (org-babel-ruby-table-or-string result))
90 (org-babel-pick-name (cdr (assoc :colname-names params))
91 (cdr (assoc :colnames params)))
92 (org-babel-pick-name (cdr (assoc :rowname-names params))
93 (cdr (assoc :rownames params))))))
95 (defun org-babel-prep-session:ruby (session params)
96 "Prepare SESSION according to the header arguments specified in PARAMS."
97 ;; (message "params=%S" params) ;; debugging
98 (let* ((session (org-babel-ruby-initiate-session session))
99 (var-lines (org-babel-variable-assignments:ruby params)))
100 (org-babel-comint-in-buffer session
101 (sit-for .5) (goto-char (point-max))
102 (mapc (lambda (var)
103 (insert var) (comint-send-input nil t)
104 (org-babel-comint-wait-for-output session)
105 (sit-for .1) (goto-char (point-max))) var-lines))
106 session))
108 (defun org-babel-load-session:ruby (session body params)
109 "Load BODY into SESSION."
110 (save-window-excursion
111 (let ((buffer (org-babel-prep-session:ruby session params)))
112 (with-current-buffer buffer
113 (goto-char (process-mark (get-buffer-process (current-buffer))))
114 (insert (org-babel-chomp body)))
115 buffer)))
117 ;; helper functions
119 (defun org-babel-variable-assignments:ruby (params)
120 "Return list of ruby statements assigning the block's variables."
121 (mapcar
122 (lambda (pair)
123 (format "%s=%s"
124 (car pair)
125 (org-babel-ruby-var-to-ruby (cdr pair))))
126 (mapcar #'cdr (org-babel-get-header params :var))))
128 (defun org-babel-ruby-var-to-ruby (var)
129 "Convert VAR into a ruby variable.
130 Convert an elisp value into a string of ruby source code
131 specifying a variable of the same value."
132 (if (listp var)
133 (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
134 (if (equal var 'hline)
135 org-babel-ruby-hline-to
136 (format "%S" var))))
138 (defun org-babel-ruby-table-or-string (results)
139 "Convert RESULTS into an appropriate elisp value.
140 If RESULTS look like a table, then convert them into an
141 Emacs-lisp table, otherwise return the results as a string."
142 ((lambda (res)
143 (if (listp res)
144 (mapcar (lambda (el) (if (equal el 'nil)
145 org-babel-ruby-nil-to el))
146 res)
147 res))
148 (org-babel-script-escape results)))
150 (defun org-babel-ruby-initiate-session (&optional session params)
151 "Initiate a ruby session.
152 If there is not a current inferior-process-buffer in SESSION
153 then create one. Return the initialized session."
154 (unless (string= session "none")
155 (require 'inf-ruby)
156 (let ((session-buffer (save-window-excursion
157 (run-ruby nil session) (current-buffer))))
158 (if (org-babel-comint-buffer-livep session-buffer)
159 (progn (sit-for .25) session-buffer)
160 (sit-for .5)
161 (org-babel-ruby-initiate-session session)))))
163 (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
164 "String to indicate that evaluation has completed.")
165 (defvar org-babel-ruby-f-write
166 "File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
167 (defvar org-babel-ruby-pp-f-write
168 "File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
169 (defvar org-babel-ruby-wrapper-method
171 def main()
174 results = main()
175 File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
177 (defvar org-babel-ruby-pp-wrapper-method
179 require 'pp'
180 def main()
183 results = main()
184 File.open('%s', 'w') do |f|
185 $stdout = f
186 pp results
190 (defun org-babel-ruby-evaluate
191 (buffer body &optional result-type result-params)
192 "Pass BODY to the Ruby process in BUFFER.
193 If RESULT-TYPE equals 'output then return a list of the outputs
194 of the statements in BODY, if RESULT-TYPE equals 'value then
195 return the value of the last statement in BODY, as elisp."
196 (if (not buffer)
197 ;; external process evaluation
198 (case result-type
199 (output (org-babel-eval org-babel-ruby-command body))
200 (value (let ((tmp-file (org-babel-temp-file "ruby-")))
201 (org-babel-eval
202 org-babel-ruby-command
203 (format (if (member "pp" result-params)
204 org-babel-ruby-pp-wrapper-method
205 org-babel-ruby-wrapper-method)
206 body (org-babel-process-file-name tmp-file 'noquote)))
207 ((lambda (raw)
208 (if (or (member "code" result-params)
209 (member "pp" result-params))
211 (org-babel-ruby-table-or-string raw)))
212 (org-babel-eval-read-file tmp-file)))))
213 ;; comint session evaluation
214 (case result-type
215 (output
216 (mapconcat
217 #'identity
218 (butlast
219 (split-string
220 (mapconcat
221 #'org-babel-trim
222 (butlast
223 (org-babel-comint-with-output
224 (buffer org-babel-ruby-eoe-indicator t body)
225 (mapc
226 (lambda (line)
227 (insert (org-babel-chomp line)) (comint-send-input nil t))
228 (list body org-babel-ruby-eoe-indicator))
229 (comint-send-input nil t)) 2)
230 "\n") "[\r\n]")) "\n"))
231 (value
232 (let* ((tmp-file (org-babel-temp-file "ruby-"))
233 (ppp (or (member "code" result-params)
234 (member "pp" result-params))))
235 (org-babel-comint-with-output
236 (buffer org-babel-ruby-eoe-indicator t body)
237 (when ppp (insert "require 'pp';") (comint-send-input nil t))
238 (mapc
239 (lambda (line)
240 (insert (org-babel-chomp line)) (comint-send-input nil t))
241 (append
242 (list body)
243 (if (not ppp)
244 (list (format org-babel-ruby-f-write
245 (org-babel-process-file-name tmp-file 'noquote)))
246 (list
247 "results=_" "require 'pp'" "orig_out = $stdout"
248 (format org-babel-ruby-pp-f-write
249 (org-babel-process-file-name tmp-file 'noquote))))
250 (list org-babel-ruby-eoe-indicator)))
251 (comint-send-input nil t))
252 (org-babel-eval-read-file tmp-file))))))
254 (defun org-babel-ruby-read-string (string)
255 "Strip \\\"s from around a ruby string."
256 (if (string-match "^\"\\([^\000]+\\)\"$" string)
257 (match-string 1 string)
258 string))
260 (provide 'ob-ruby)
264 ;;; ob-ruby.el ends here