babel: implement org-babel-load-session:* for R, clojure, gnuplot, python, ruby and sh
[rgr-org-mode.git] / contrib / babel / lisp / langs / org-babel-ruby.el
blob27de42f4a815c0d67b21a50d088779747af7829f
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
8 ;; Version: 0.01
10 ;;; License:
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)
15 ;; any later version.
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.
27 ;;; Commentary:
29 ;; Org-Babel support for evaluating ruby source code.
31 ;;; Requirements:
33 ;; - ruby and irb executables :: http://www.ruby-lang.org/
34 ;;
35 ;; - ruby-mode :: Can be installed through ELPA, or from
36 ;; http://github.com/eschulte/rinari/raw/master/util/ruby-mode.el
37 ;;
38 ;; - inf-ruby mode :: Can be installed through ELPA, or from
39 ;; http://github.com/eschulte/rinari/raw/master/util/inf-ruby.el
41 ;;; Code:
42 (require 'org-babel)
43 (require 'inf-ruby)
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))
58 (full-body (concat
59 (mapconcat ;; define any variables
60 (lambda (pair)
61 (format "%s=%s"
62 (car pair)
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
74 (lambda (pair)
75 (format "%s=%s"
76 (car pair)
77 (org-babel-ruby-var-to-ruby (cdr pair))))
78 vars)))
79 (org-babel-comint-in-buffer session
80 (sit-for .5) (goto-char (point-max))
81 (mapc (lambda (var)
82 (insert var) (comint-send-input nil t)
83 (org-babel-comint-wait-for-output session)
84 (sit-for .1) (goto-char (point-max))) var-lines))
85 session))
87 (defun org-babel-load-session:ruby (session body params)
88 "Load BODY into SESSION."
89 (save-window-excursion
90 (let ((buffer (org-babel-prep-session:ruby session params)))
91 (with-current-buffer buffer
92 (goto-char (process-mark (get-buffer-process (current-buffer))))
93 (insert (org-babel-chomp body)))
94 buffer)))
96 ;; helper functions
98 (defun org-babel-ruby-var-to-ruby (var)
99 "Convert an elisp var into a string of ruby source code
100 specifying a var of the same value."
101 (if (listp var)
102 (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
103 (format "%S" var)))
105 (defun org-babel-ruby-table-or-string (results)
106 "If the results look like a table, then convert them into an
107 Emacs-lisp table, otherwise return the results as a string."
108 (org-babel-read
109 (if (and (stringp results) (string-match "^\\[.+\\]$" results))
110 (org-babel-read
111 (replace-regexp-in-string
112 "\\[" "(" (replace-regexp-in-string
113 "\\]" ")" (replace-regexp-in-string
114 ", " " " (replace-regexp-in-string
115 "'" "\"" results)))))
116 results)))
118 (defun org-babel-ruby-initiate-session (&optional session)
119 "If there is not a current inferior-process-buffer in SESSION
120 then create. Return the initialized session."
121 (unless (string= session "none")
122 (let ((session-buffer (save-window-excursion (run-ruby nil session) (current-buffer))))
123 (if (org-babel-comint-buffer-livep session-buffer)
124 session-buffer
125 (sit-for .5)
126 (org-babel-ruby-initiate-session session)))))
128 (defvar org-babel-ruby-last-value-eval "_"
129 "When evaluated by Ruby this returns the return value of the last statement.")
130 (defvar org-babel-ruby-pp-last-value-eval "require 'pp'; pp(_)"
131 "When evaluated by Ruby this pretty prints value of the last statement.")
132 (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
133 "Used to indicate that evaluation is has completed.")
134 (defvar org-babel-ruby-wrapper-method
136 def main()
139 results = main()
140 File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
142 (defvar org-babel-ruby-pp-wrapper-method
144 require 'pp'
145 def main()
148 results = main()
149 File.open('%s', 'w') do |f|
150 $stdout = f
151 pp results
155 (defun org-babel-ruby-evaluate (buffer body &optional result-type)
156 "Pass BODY to the Ruby process in BUFFER. If RESULT-TYPE equals
157 'output then return a list of the outputs of the statements in
158 BODY, if RESULT-TYPE equals 'value then return the value of the
159 last statement in BODY, as elisp."
160 (if (not session)
161 ;; external process evaluation
162 (save-window-excursion
163 (case result-type
164 (output
165 (with-temp-buffer
166 (insert body)
167 ;; (message "buffer=%s" (buffer-string)) ;; debugging
168 (shell-command-on-region (point-min) (point-max) "ruby" 'replace)
169 (buffer-string)))
170 (value
171 (let ((tmp-file (make-temp-file "ruby-functional-results")))
172 (with-temp-buffer
173 (insert (format (if (member "pp" result-params)
174 org-babel-ruby-pp-wrapper-method
175 org-babel-ruby-wrapper-method) body tmp-file))
176 ;; (message "buffer=%s" (buffer-string)) ;; debugging
177 (shell-command-on-region (point-min) (point-max) "ruby"))
178 (let ((raw (with-temp-buffer (insert-file-contents tmp-file)
179 (buffer-string))))
180 (if (or (member "code" result-params) (member "pp" result-params))
182 (org-babel-ruby-table-or-string raw)))))))
183 ;; comint session evaluation
184 (let* ((full-body
185 (mapconcat
186 #'org-babel-chomp
187 (list body (if (member "pp" result-params)
188 org-babel-ruby-pp-last-value-eval
189 org-babel-ruby-last-value-eval)
190 org-babel-ruby-eoe-indicator) "\n"))
191 (raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
192 (insert full-body) (comint-send-input nil t)))
193 (results (cdr (member org-babel-ruby-eoe-indicator
194 (reverse (mapcar #'org-babel-ruby-read-string
195 (mapcar #'org-babel-trim raw)))))))
196 (case result-type
197 (output (mapconcat #'identity (reverse (cdr results)) "\n"))
198 (value
199 (if (or (member "code" result-params) (member "pp" result-params))
200 (car results)
201 (org-babel-ruby-table-or-string (car results))))))))
203 (defun org-babel-ruby-read-string (string)
204 "Strip \\\"s from around ruby string"
205 (if (string-match "\"\\([^\000]+\\)\"" string)
206 (match-string 1 string)
207 string))
209 (provide 'org-babel-ruby)
210 ;;; org-babel-ruby.el ends here