babel: marking the safe value of org-export-babel-evaluate
[org-mode/org-tableheadings.git] / lisp / ob-ruby.el
blob5f06d4cf21c90751385f5fdbb858cff52d3e7294
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: 0.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 org-babel. This function is
69 called by `org-babel-execute-src-block'."
70 (message "executing Ruby source code block")
71 (let* ((processed-params (org-babel-process-params params))
72 (session (org-babel-ruby-initiate-session (first processed-params)))
73 (result-params (nth 2 processed-params))
74 (result-type (nth 3 processed-params))
75 (full-body (org-babel-expand-body:ruby
76 body params processed-params))
77 (result (org-babel-ruby-evaluate
78 session full-body result-type result-params)))
79 (or (cdr (assoc :file params))
80 (org-babel-reassemble-table
81 result
82 (org-babel-pick-name (nth 4 processed-params)
83 (cdr (assoc :colnames params)))
84 (org-babel-pick-name (nth 5 processed-params)
85 (cdr (assoc :rownames params)))))))
87 (defun org-babel-prep-session:ruby (session params)
88 "Prepare SESSION according to the header arguments specified in PARAMS."
89 ;; (message "params=%S" params) ;; debugging
90 (let* ((session (org-babel-ruby-initiate-session session))
91 (vars (org-babel-ref-variables params))
92 (var-lines (mapcar ;; define any variables
93 (lambda (pair)
94 (format "%s=%s"
95 (car pair)
96 (org-babel-ruby-var-to-ruby (cdr pair))))
97 vars)))
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))
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)))
115 ;; helper functions
117 (defun org-babel-ruby-var-to-ruby (var)
118 "Convert an elisp var into a string of ruby source code
119 specifying a var 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 "If RESULTS look like a table, then convert them into an
126 Emacs-lisp table, otherwise return the results as a string."
127 (org-babel-read
128 (if (and (stringp results) (string-match "^\\[.+\\]$" results))
129 (org-babel-read
130 (concat "'"
131 (replace-regexp-in-string
132 "\\[" "(" (replace-regexp-in-string
133 "\\]" ")" (replace-regexp-in-string
134 ", " " " (replace-regexp-in-string
135 "'" "\"" results))))))
136 results)))
138 (defun org-babel-ruby-initiate-session (&optional session params)
139 "If there is not a current inferior-process-buffer in SESSION
140 then create one. Return the initialized session."
141 (require 'inf-ruby)
142 (unless (string= session "none")
143 (let ((session-buffer (save-window-excursion
144 (run-ruby nil session) (current-buffer))))
145 (if (org-babel-comint-buffer-livep session-buffer)
146 (progn (sit-for .25) session-buffer)
147 (sit-for .5)
148 (org-babel-ruby-initiate-session session)))))
150 (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
151 "Used to indicate that evaluation is has completed.")
152 (defvar org-babel-ruby-f-write
153 "File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
154 (defvar org-babel-ruby-pp-f-write
155 "File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
156 (defvar org-babel-ruby-wrapper-method
158 def main()
161 results = main()
162 File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
164 (defvar org-babel-ruby-pp-wrapper-method
166 require 'pp'
167 def main()
170 results = main()
171 File.open('%s', 'w') do |f|
172 $stdout = f
173 pp results
177 (defun org-babel-ruby-evaluate
178 (buffer body &optional result-type result-params)
179 "Pass BODY to the Ruby process in BUFFER. If RESULT-TYPE equals
180 'output then return a list of the outputs of the statements in
181 BODY, if RESULT-TYPE equals 'value then return the value of the
182 last statement in BODY, as elisp."
183 (if (not buffer)
184 ;; external process evaluation
185 (case result-type
186 (output (org-babel-eval org-babel-ruby-command body))
187 (value (let ((tmp-file (make-temp-file "org-babel-ruby-results-")))
188 (org-babel-eval org-babel-ruby-command
189 (format (if (member "pp" result-params)
190 org-babel-ruby-pp-wrapper-method
191 org-babel-ruby-wrapper-method)
192 body tmp-file))
193 ((lambda (raw)
194 (if (or (member "code" result-params)
195 (member "pp" result-params))
197 (org-babel-ruby-table-or-string raw)))
198 (org-babel-eval-read-file tmp-file)))))
199 ;; comint session evaluation
200 (case result-type
201 (output
202 (mapconcat
203 #'identity
204 (butlast
205 (split-string
206 (mapconcat
207 #'org-babel-trim
208 (butlast
209 (org-babel-comint-with-output
210 (buffer org-babel-ruby-eoe-indicator t body)
211 (mapc
212 (lambda (line)
213 (insert (org-babel-chomp line)) (comint-send-input nil t))
214 (list body org-babel-ruby-eoe-indicator))
215 (comint-send-input nil t)) 2)
216 "\n") "[\r\n]")) "\n"))
217 (value
218 ((lambda (results)
219 (if (or (member "code" result-params) (member "pp" result-params))
220 results
221 (org-babel-ruby-table-or-string results)))
222 (let* ((tmp-file (make-temp-file "org-babel-ruby-results-"))
223 (ppp (or (member "code" result-params)
224 (member "pp" result-params))))
225 (org-babel-comint-with-output
226 (buffer org-babel-ruby-eoe-indicator t body)
227 (when ppp (insert "require 'pp';") (comint-send-input nil t))
228 (mapc
229 (lambda (line)
230 (insert (org-babel-chomp line)) (comint-send-input nil t))
231 (append
232 (list body)
233 (if (not ppp)
234 (list (format org-babel-ruby-f-write tmp-file))
235 (list
236 "results=_" "require 'pp'" "orig_out = $stdout"
237 (format org-babel-ruby-pp-f-write tmp-file)))
238 (list org-babel-ruby-eoe-indicator)))
239 (comint-send-input nil t))
240 (org-babel-eval-read-file tmp-file)))))))
242 (defun org-babel-ruby-read-string (string)
243 "Strip \\\"s from around a ruby string."
244 (if (string-match "^\"\\([^\000]+\\)\"$" string)
245 (match-string 1 string)
246 string))
248 (provide 'ob-ruby)
250 ;; arch-tag: 3e9726db-4520-49e2-b263-e8f571ac88f5
252 ;;; ob-ruby.el ends here