Merge branch 'master' of orgmode.org:org-mode
[org-mode.git] / lisp / ob-ruby.el
blobbef8fbafe9e7f5879d5be2da964acd0be51fb0e0
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 result
72 (org-babel-pick-name (cdr (assoc :colname-names params))
73 (cdr (assoc :colnames params)))
74 (org-babel-pick-name (cdr (assoc :rowname-names params))
75 (cdr (assoc :rownames params))))))
77 (defun org-babel-prep-session:ruby (session params)
78 "Prepare SESSION according to the header arguments specified in PARAMS."
79 ;; (message "params=%S" params) ;; debugging
80 (let* ((session (org-babel-ruby-initiate-session session))
81 (var-lines (org-babel-variable-assignments:ruby params)))
82 (org-babel-comint-in-buffer session
83 (sit-for .5) (goto-char (point-max))
84 (mapc (lambda (var)
85 (insert var) (comint-send-input nil t)
86 (org-babel-comint-wait-for-output session)
87 (sit-for .1) (goto-char (point-max))) var-lines))
88 session))
90 (defun org-babel-load-session:ruby (session body params)
91 "Load BODY into SESSION."
92 (save-window-excursion
93 (let ((buffer (org-babel-prep-session:ruby session params)))
94 (with-current-buffer buffer
95 (goto-char (process-mark (get-buffer-process (current-buffer))))
96 (insert (org-babel-chomp body)))
97 buffer)))
99 ;; helper functions
101 (defun org-babel-variable-assignments:ruby (params)
102 "Return list of ruby statements assigning the block's variables."
103 (mapcar
104 (lambda (pair)
105 (format "%s=%s"
106 (car pair)
107 (org-babel-ruby-var-to-ruby (cdr pair))))
108 (mapcar #'cdr (org-babel-get-header params :var))))
110 (defun org-babel-ruby-var-to-ruby (var)
111 "Convert VAR into a ruby variable.
112 Convert an elisp value into a string of ruby source code
113 specifying a variable of the same value."
114 (if (listp var)
115 (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
116 (format "%S" var)))
118 (defun org-babel-ruby-table-or-string (results)
119 "Convert RESULTS into an appropriate elisp value.
120 If RESULTS look like a table, then convert them into an
121 Emacs-lisp table, otherwise return the results as a string."
122 (org-babel-script-escape results))
124 (defun org-babel-ruby-initiate-session (&optional session params)
125 "Initiate a ruby session.
126 If there is not a current inferior-process-buffer in SESSION
127 then create one. Return the initialized session."
128 (unless (string= session "none")
129 (require 'inf-ruby)
130 (let ((session-buffer (save-window-excursion
131 (run-ruby nil session) (current-buffer))))
132 (if (org-babel-comint-buffer-livep session-buffer)
133 (progn (sit-for .25) session-buffer)
134 (sit-for .5)
135 (org-babel-ruby-initiate-session session)))))
137 (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
138 "String to indicate that evaluation has completed.")
139 (defvar org-babel-ruby-f-write
140 "File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
141 (defvar org-babel-ruby-pp-f-write
142 "File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
143 (defvar org-babel-ruby-wrapper-method
145 def main()
148 results = main()
149 File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
151 (defvar org-babel-ruby-pp-wrapper-method
153 require 'pp'
154 def main()
157 results = main()
158 File.open('%s', 'w') do |f|
159 $stdout = f
160 pp results
164 (defun org-babel-ruby-evaluate
165 (buffer body &optional result-type result-params)
166 "Pass BODY to the Ruby process in BUFFER.
167 If RESULT-TYPE equals 'output then return a list of the outputs
168 of the statements in BODY, if RESULT-TYPE equals 'value then
169 return the value of the last statement in BODY, as elisp."
170 (if (not buffer)
171 ;; external process evaluation
172 (case result-type
173 (output (org-babel-eval org-babel-ruby-command body))
174 (value (let ((tmp-file (org-babel-temp-file "ruby-")))
175 (org-babel-eval
176 org-babel-ruby-command
177 (format (if (member "pp" result-params)
178 org-babel-ruby-pp-wrapper-method
179 org-babel-ruby-wrapper-method)
180 body (org-babel-process-file-name tmp-file 'noquote)))
181 ((lambda (raw)
182 (if (or (member "code" result-params)
183 (member "pp" result-params))
185 (org-babel-ruby-table-or-string raw)))
186 (org-babel-eval-read-file tmp-file)))))
187 ;; comint session evaluation
188 (case result-type
189 (output
190 (mapconcat
191 #'identity
192 (butlast
193 (split-string
194 (mapconcat
195 #'org-babel-trim
196 (butlast
197 (org-babel-comint-with-output
198 (buffer org-babel-ruby-eoe-indicator t body)
199 (mapc
200 (lambda (line)
201 (insert (org-babel-chomp line)) (comint-send-input nil t))
202 (list body org-babel-ruby-eoe-indicator))
203 (comint-send-input nil t)) 2)
204 "\n") "[\r\n]")) "\n"))
205 (value
206 ((lambda (results)
207 (if (or (member "code" result-params) (member "pp" result-params))
208 results
209 (org-babel-ruby-table-or-string results)))
210 (let* ((tmp-file (org-babel-temp-file "ruby-"))
211 (ppp (or (member "code" result-params)
212 (member "pp" result-params))))
213 (org-babel-comint-with-output
214 (buffer org-babel-ruby-eoe-indicator t body)
215 (when ppp (insert "require 'pp';") (comint-send-input nil t))
216 (mapc
217 (lambda (line)
218 (insert (org-babel-chomp line)) (comint-send-input nil t))
219 (append
220 (list body)
221 (if (not ppp)
222 (list (format org-babel-ruby-f-write
223 (org-babel-process-file-name tmp-file 'noquote)))
224 (list
225 "results=_" "require 'pp'" "orig_out = $stdout"
226 (format org-babel-ruby-pp-f-write
227 (org-babel-process-file-name tmp-file 'noquote))))
228 (list org-babel-ruby-eoe-indicator)))
229 (comint-send-input nil t))
230 (org-babel-eval-read-file tmp-file)))))))
232 (defun org-babel-ruby-read-string (string)
233 "Strip \\\"s from around a ruby string."
234 (if (string-match "^\"\\([^\000]+\\)\"$" string)
235 (match-string 1 string)
236 string))
238 (provide 'ob-ruby)
242 ;;; ob-ruby.el ends here