More tweaks of skeleton documentation wrt \n behavior at bol/eol.
[bpt/emacs.git] / lisp / org / ob-ruby.el
CommitLineData
86fbb8ca
CD
1;;; ob-ruby.el --- org-babel functions for ruby evaluation
2
ba318903 3;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
86fbb8ca
CD
4
5;; Author: Eric Schulte
6;; Keywords: literate programming, reproducible research
7;; Homepage: http://orgmode.org
86fbb8ca
CD
8
9;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24;;; Commentary:
25
26;; Org-Babel support for evaluating ruby source code.
27
28;;; Requirements:
29
30;; - ruby and irb executables :: http://www.ruby-lang.org/
14e1337f 31;;
86fbb8ca
CD
32;; - ruby-mode :: Can be installed through ELPA, or from
33;; http://github.com/eschulte/rinari/raw/master/util/ruby-mode.el
14e1337f 34;;
86fbb8ca
CD
35;; - inf-ruby mode :: Can be installed through ELPA, or from
36;; http://github.com/eschulte/rinari/raw/master/util/inf-ruby.el
37
38;;; Code:
39(require 'ob)
86fbb8ca
CD
40(eval-when-compile (require 'cl))
41
42(declare-function run-ruby "ext:inf-ruby" (&optional command name))
3ab2c837 43(declare-function xmp "ext:rcodetools" (&optional option))
86fbb8ca 44
3ab2c837 45(defvar org-babel-tangle-lang-exts)
86fbb8ca
CD
46(add-to-list 'org-babel-tangle-lang-exts '("ruby" . "rb"))
47
48(defvar org-babel-default-header-args:ruby '())
49
50(defvar org-babel-ruby-command "ruby"
51 "Name of command to use for executing ruby code.")
52
271672fa
BG
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)
59
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")
73d3db82 65 :type 'symbol)
271672fa 66
86fbb8ca
CD
67(defun org-babel-execute:ruby (body params)
68 "Execute a block of Ruby code with Babel.
69This function is called by `org-babel-execute-src-block'."
afe98dfa
CD
70 (let* ((session (org-babel-ruby-initiate-session
71 (cdr (assoc :session params))))
72 (result-params (cdr (assoc :result-params params)))
73 (result-type (cdr (assoc :result-type params)))
74 (full-body (org-babel-expand-body:generic
75 body params (org-babel-variable-assignments:ruby params)))
3ab2c837
BG
76 (result (if (member "xmp" result-params)
77 (with-temp-buffer
8223b1d2
BG
78 (require 'rcodetools)
79 (insert full-body)
80 (xmp (cdr (assoc :xmp-option params)))
81 (buffer-string))
3ab2c837 82 (org-babel-ruby-evaluate
8223b1d2 83 session full-body result-type result-params))))
3ab2c837 84 (org-babel-reassemble-table
271672fa
BG
85 (org-babel-result-cond result-params
86 result
87 (org-babel-ruby-table-or-string result))
3ab2c837
BG
88 (org-babel-pick-name (cdr (assoc :colname-names params))
89 (cdr (assoc :colnames params)))
90 (org-babel-pick-name (cdr (assoc :rowname-names params))
91 (cdr (assoc :rownames params))))))
86fbb8ca
CD
92
93(defun org-babel-prep-session:ruby (session params)
94 "Prepare SESSION according to the header arguments specified in PARAMS."
95 ;; (message "params=%S" params) ;; debugging
96 (let* ((session (org-babel-ruby-initiate-session session))
afe98dfa 97 (var-lines (org-babel-variable-assignments:ruby params)))
86fbb8ca
CD
98 (org-babel-comint-in-buffer session
99 (sit-for .5) (goto-char (point-max))
100 (mapc (lambda (var)
101 (insert var) (comint-send-input nil t)
102 (org-babel-comint-wait-for-output session)
103 (sit-for .1) (goto-char (point-max))) var-lines))
104 session))
105
106(defun org-babel-load-session:ruby (session body params)
107 "Load BODY into SESSION."
108 (save-window-excursion
109 (let ((buffer (org-babel-prep-session:ruby session params)))
110 (with-current-buffer buffer
111 (goto-char (process-mark (get-buffer-process (current-buffer))))
112 (insert (org-babel-chomp body)))
113 buffer)))
114
115;; helper functions
116
afe98dfa 117(defun org-babel-variable-assignments:ruby (params)
8223b1d2 118 "Return list of ruby statements assigning the block's variables."
afe98dfa
CD
119 (mapcar
120 (lambda (pair)
121 (format "%s=%s"
122 (car pair)
123 (org-babel-ruby-var-to-ruby (cdr pair))))
124 (mapcar #'cdr (org-babel-get-header params :var))))
125
86fbb8ca
CD
126(defun org-babel-ruby-var-to-ruby (var)
127 "Convert VAR into a ruby variable.
128Convert an elisp value into a string of ruby source code
129specifying a variable of the same value."
130 (if (listp var)
131 (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
271672fa
BG
132 (if (equal var 'hline)
133 org-babel-ruby-hline-to
134 (format "%S" var))))
86fbb8ca
CD
135
136(defun org-babel-ruby-table-or-string (results)
137 "Convert RESULTS into an appropriate elisp value.
138If RESULTS look like a table, then convert them into an
139Emacs-lisp table, otherwise return the results as a string."
666ffc7e
SM
140 (let ((res (org-babel-script-escape results)))
141 (if (listp res)
142 (mapcar (lambda (el) (if (equal el 'nil)
143 org-babel-ruby-nil-to el))
144 res)
145 res)))
86fbb8ca
CD
146
147(defun org-babel-ruby-initiate-session (&optional session params)
148 "Initiate a ruby session.
149If there is not a current inferior-process-buffer in SESSION
150then create one. Return the initialized session."
86fbb8ca 151 (unless (string= session "none")
e66ba1df 152 (require 'inf-ruby)
86fbb8ca
CD
153 (let ((session-buffer (save-window-excursion
154 (run-ruby nil session) (current-buffer))))
155 (if (org-babel-comint-buffer-livep session-buffer)
156 (progn (sit-for .25) session-buffer)
157 (sit-for .5)
158 (org-babel-ruby-initiate-session session)))))
159
160(defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
161 "String to indicate that evaluation has completed.")
162(defvar org-babel-ruby-f-write
163 "File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
164(defvar org-babel-ruby-pp-f-write
165 "File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
166(defvar org-babel-ruby-wrapper-method
167 "
168def main()
169%s
170end
171results = main()
172File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
173")
174(defvar org-babel-ruby-pp-wrapper-method
175 "
176require 'pp'
177def main()
178%s
179end
180results = main()
181File.open('%s', 'w') do |f|
182 $stdout = f
183 pp results
184end
185")
186
187(defun org-babel-ruby-evaluate
188 (buffer body &optional result-type result-params)
189 "Pass BODY to the Ruby process in BUFFER.
190If RESULT-TYPE equals 'output then return a list of the outputs
191of the statements in BODY, if RESULT-TYPE equals 'value then
192return the value of the last statement in BODY, as elisp."
193 (if (not buffer)
194 ;; external process evaluation
195 (case result-type
196 (output (org-babel-eval org-babel-ruby-command body))
afe98dfa
CD
197 (value (let ((tmp-file (org-babel-temp-file "ruby-")))
198 (org-babel-eval
199 org-babel-ruby-command
200 (format (if (member "pp" result-params)
201 org-babel-ruby-pp-wrapper-method
202 org-babel-ruby-wrapper-method)
203 body (org-babel-process-file-name tmp-file 'noquote)))
666ffc7e
SM
204 (let ((raw (org-babel-eval-read-file tmp-file)))
205 (if (or (member "code" result-params)
206 (member "pp" result-params))
207 raw
208 (org-babel-ruby-table-or-string raw))))))
86fbb8ca
CD
209 ;; comint session evaluation
210 (case result-type
211 (output
212 (mapconcat
213 #'identity
214 (butlast
215 (split-string
216 (mapconcat
217 #'org-babel-trim
218 (butlast
219 (org-babel-comint-with-output
220 (buffer org-babel-ruby-eoe-indicator t body)
221 (mapc
222 (lambda (line)
223 (insert (org-babel-chomp line)) (comint-send-input nil t))
224 (list body org-babel-ruby-eoe-indicator))
225 (comint-send-input nil t)) 2)
226 "\n") "[\r\n]")) "\n"))
227 (value
271672fa
BG
228 (let* ((tmp-file (org-babel-temp-file "ruby-"))
229 (ppp (or (member "code" result-params)
230 (member "pp" result-params))))
231 (org-babel-comint-with-output
232 (buffer org-babel-ruby-eoe-indicator t body)
233 (when ppp (insert "require 'pp';") (comint-send-input nil t))
234 (mapc
235 (lambda (line)
236 (insert (org-babel-chomp line)) (comint-send-input nil t))
237 (append
238 (list body)
239 (if (not ppp)
240 (list (format org-babel-ruby-f-write
241 (org-babel-process-file-name tmp-file 'noquote)))
242 (list
243 "results=_" "require 'pp'" "orig_out = $stdout"
244 (format org-babel-ruby-pp-f-write
245 (org-babel-process-file-name tmp-file 'noquote))))
246 (list org-babel-ruby-eoe-indicator)))
247 (comint-send-input nil t))
248 (org-babel-eval-read-file tmp-file))))))
86fbb8ca
CD
249
250(defun org-babel-ruby-read-string (string)
251 "Strip \\\"s from around a ruby string."
252 (if (string-match "^\"\\([^\000]+\\)\"$" string)
253 (match-string 1 string)
254 string))
255
256(provide 'ob-ruby)
257
5b409b39 258
86fbb8ca
CD
259
260;;; ob-ruby.el ends here