1 ;;; org-babel-ruby.el --- org-babel functions for ruby evaluation
3 ;; Copyright (C) 2009 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
12 ;; This program 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, or (at your option)
17 ;; This program 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Org-Babel support for evaluating ruby source code.
33 ;; - ruby and irb executables :: http://www.ruby-lang.org/
35 ;; - ruby-mode :: Can be installed through ELPA, or from
36 ;; http://github.com/eschulte/rinari/raw/master/util/ruby-mode.el
38 ;; - inf-ruby mode :: Can be installed through ELPA, or from
39 ;; http://github.com/eschulte/rinari/raw/master/util/inf-ruby.el
45 (org-babel-add-interpreter "ruby")
47 (add-to-list 'org-babel-tangle-langs
'("ruby" "rb" "#!/usr/bin/env ruby"))
49 (defun org-babel-execute:ruby
(body params
)
50 "Execute a block of Ruby code with org-babel. This function is
51 called by `org-babel-execute-src-block'."
52 (message "executing Ruby source code block")
53 (let* ((processed-params (org-babel-process-params params
))
54 (session (org-babel-ruby-initiate-session (first processed-params
)))
55 (vars (second processed-params
))
56 (result-params (third processed-params
))
57 (result-type (fourth processed-params
))
59 (mapconcat ;; define any variables
63 (org-babel-ruby-var-to-ruby (cdr pair
))))
64 vars
"\n") "\n" body
"\n")) ;; then the source block body
65 (result (org-babel-ruby-evaluate session full-body result-type
)))
66 (or (cdr (assoc :file params
)) result
)))
68 (defun org-babel-prep-session:ruby
(session params
)
69 "Prepare SESSION according to the header arguments specified in PARAMS."
70 ;; (message "params=%S" params) ;; debugging
71 (let* ((session (org-babel-ruby-initiate-session session
))
72 (vars (org-babel-ref-variables params
))
73 (var-lines (mapcar ;; define any variables
77 (org-babel-ruby-var-to-ruby (cdr pair
))))
79 ;; (message "vars=%S" vars) ;; debugging
80 (org-babel-comint-in-buffer session
81 (sit-for .5) (goto-char (point-max))
83 (insert var
) (comint-send-input nil t
)
84 (org-babel-comint-wait-for-output session
)
85 (sit-for .1) (goto-char (point-max))) var-lines
))))
89 (defun org-babel-ruby-var-to-ruby (var)
90 "Convert an elisp var into a string of ruby source code
91 specifying a var of the same value."
93 (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var
", ") "]")
96 (defun org-babel-ruby-table-or-string (results)
97 "If the results look like a table, then convert them into an
98 Emacs-lisp table, otherwise return the results as a string."
100 (if (and (stringp results
) (string-match "^\\[.+\\]$" results
))
102 (replace-regexp-in-string
103 "\\[" "(" (replace-regexp-in-string
104 "\\]" ")" (replace-regexp-in-string
105 ", " " " (replace-regexp-in-string
106 "'" "\"" results
)))))
109 (defun org-babel-ruby-initiate-session (&optional session
)
110 "If there is not a current inferior-process-buffer in SESSION
111 then create. Return the initialized session."
112 (unless (string= session
"none")
113 (let ((session-buffer (save-window-excursion (run-ruby nil session
) (current-buffer))))
114 (if (org-babel-comint-buffer-livep session-buffer
)
117 (org-babel-ruby-initiate-session session
)))))
119 (defvar org-babel-ruby-last-value-eval
"_"
120 "When evaluated by Ruby this returns the return value of the last statement.")
121 (defvar org-babel-ruby-pp-last-value-eval
"require 'pp'; pp(_)"
122 "When evaluated by Ruby this pretty prints value of the last statement.")
123 (defvar org-babel-ruby-eoe-indicator
":org_babel_ruby_eoe"
124 "Used to indicate that evaluation is has completed.")
125 (defvar org-babel-ruby-wrapper-method
131 File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
133 (defvar org-babel-ruby-pp-wrapper-method
140 File.open('%s', 'w') do |f|
146 (defun org-babel-ruby-evaluate (buffer body
&optional result-type
)
147 "Pass BODY to the Ruby process in BUFFER. If RESULT-TYPE equals
148 'output then return a list of the outputs of the statements in
149 BODY, if RESULT-TYPE equals 'value then return the value of the
150 last statement in BODY, as elisp."
152 ;; external process evaluation
153 (save-window-excursion
158 ;; (message "buffer=%s" (buffer-string)) ;; debugging
159 (shell-command-on-region (point-min) (point-max) "ruby" 'replace
)
162 (let ((tmp-file (make-temp-file "ruby-functional-results")))
164 (insert (format (if (member "pp" result-params
)
165 org-babel-ruby-pp-wrapper-method
166 org-babel-ruby-wrapper-method
) body tmp-file
))
167 ;; (message "buffer=%s" (buffer-string)) ;; debugging
168 (shell-command-on-region (point-min) (point-max) "ruby"))
169 (let ((raw (with-temp-buffer (insert-file-contents tmp-file
)
171 (if (or (member "code" result-params
) (member "pp" result-params
))
173 (org-babel-ruby-table-or-string raw
)))))))
174 ;; comint session evaluation
178 (list body
(if (member "pp" result-params
)
179 org-babel-ruby-pp-last-value-eval
180 org-babel-ruby-last-value-eval
)
181 org-babel-ruby-eoe-indicator
) "\n"))
182 (raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
183 (insert full-body
) (comint-send-input nil t
)))
184 (results (cdr (member org-babel-ruby-eoe-indicator
185 (reverse (mapcar #'org-babel-ruby-read-string
186 (mapcar #'org-babel-trim raw
)))))))
188 (output (mapconcat #'identity
(reverse (cdr results
)) "\n"))
190 (if (or (member "code" result-params
) (member "pp" result-params
))
192 (org-babel-ruby-table-or-string (car results
))))))))
194 (defun org-babel-ruby-read-string (string)
195 "Strip \\\"s from around ruby string"
196 (if (string-match "\"\\([^\000]+\\)\"" string
)
197 (match-string 1 string
)
200 (provide 'org-babel-ruby
)
201 ;;; org-babel-ruby.el ends here