org-babel: file output for ruby and python
[rgr-org-mode.git] / contrib / babel / lisp / langs / org-babel-ruby.el
blob508429141257dc77c48fb2e0ac66ac30fd459065
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' via multiple-value-bind."
52 (message "executing Ruby source code block")
53 (let* ((full-body (concat
54 (mapconcat ;; define any variables
55 (lambda (pair)
56 (format "%s=%s"
57 (car pair)
58 (org-babel-ruby-var-to-ruby (cdr pair))))
59 vars "\n") "\n" body "\n")) ;; then the source block body
60 (session (org-babel-ruby-initiate-session session))
61 (result (org-babel-ruby-evaluate session full-body result-type)))
62 (or (cdr (assoc :file params)) result)))
64 (defun org-babel-prep-session:ruby (session params)
65 "Prepare SESSION according to the header arguments specified in PARAMS."
66 ;; (message "params=%S" params) ;; debugging
67 (let* ((session (org-babel-ruby-initiate-session session))
68 (vars (org-babel-ref-variables params))
69 (var-lines (mapcar ;; define any variables
70 (lambda (pair)
71 (format "%s=%s"
72 (car pair)
73 (org-babel-ruby-var-to-ruby (cdr pair))))
74 vars)))
75 ;; (message "vars=%S" vars) ;; debugging
76 (org-babel-comint-in-buffer session
77 (sit-for .5) (goto-char (point-max))
78 (mapc (lambda (var)
79 (insert var) (comint-send-input nil t)
80 (org-babel-comint-wait-for-output session)
81 (sit-for .1) (goto-char (point-max))) var-lines))))
83 ;; helper functions
85 (defun org-babel-ruby-var-to-ruby (var)
86 "Convert an elisp var into a string of ruby source code
87 specifying a var of the same value."
88 (if (listp var)
89 (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
90 (format "%S" var)))
92 (defun org-babel-ruby-table-or-string (results)
93 "If the results look like a table, then convert them into an
94 Emacs-lisp table, otherwise return the results as a string."
95 (org-babel-read
96 (if (and (stringp results) (string-match "^\\[.+\\]$" results))
97 (org-babel-read
98 (replace-regexp-in-string
99 "\\[" "(" (replace-regexp-in-string
100 "\\]" ")" (replace-regexp-in-string
101 ", " " " (replace-regexp-in-string
102 "'" "\"" results)))))
103 results)))
105 (defun org-babel-ruby-initiate-session (&optional session)
106 "If there is not a current inferior-process-buffer in SESSION
107 then create. Return the initialized session."
108 (unless (string= session "none")
109 (let ((session-buffer (save-window-excursion (run-ruby nil session) (current-buffer))))
110 (if (org-babel-comint-buffer-livep session-buffer)
111 session-buffer
112 (sit-for .5)
113 (org-babel-ruby-initiate-session session)))))
115 (defvar org-babel-ruby-last-value-eval "_"
116 "When evaluated by Ruby this returns the return value of the last statement.")
117 (defvar org-babel-ruby-pp-last-value-eval "require 'pp'; pp(_)"
118 "When evaluated by Ruby this pretty prints value of the last statement.")
119 (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
120 "Used to indicate that evaluation is has completed.")
121 (defvar org-babel-ruby-wrapper-method
123 def main()
126 results = main()
127 File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
129 (defvar org-babel-ruby-pp-wrapper-method
131 require 'pp'
132 def main()
135 results = main()
136 File.open('%s', 'w') do |f|
137 $stdout = f
138 pp results
142 (defun org-babel-ruby-evaluate (buffer body &optional result-type)
143 "Pass BODY to the Ruby process in BUFFER. If RESULT-TYPE equals
144 'output then return a list of the outputs of the statements in
145 BODY, if RESULT-TYPE equals 'value then return the value of the
146 last statement in BODY, as elisp."
147 (if (not session)
148 ;; external process evaluation
149 (save-window-excursion
150 (case result-type
151 (output
152 (with-temp-buffer
153 (insert body)
154 ;; (message "buffer=%s" (buffer-string)) ;; debugging
155 (shell-command-on-region (point-min) (point-max) "ruby" 'replace)
156 (buffer-string)))
157 (value
158 (let ((tmp-file (make-temp-file "ruby-functional-results")))
159 (with-temp-buffer
160 (insert (format (if (member "pp" result-params)
161 org-babel-ruby-pp-wrapper-method
162 org-babel-ruby-wrapper-method) body tmp-file))
163 ;; (message "buffer=%s" (buffer-string)) ;; debugging
164 (shell-command-on-region (point-min) (point-max) "ruby"))
165 (let ((raw (with-temp-buffer (insert-file-contents tmp-file)
166 (buffer-string))))
167 (if (or (member "code" result-params) (member "pp" result-params))
169 (org-babel-ruby-table-or-string raw)))))))
170 ;; comint session evaluation
171 (let* ((full-body
172 (mapconcat
173 #'org-babel-chomp
174 (list body (if (member "pp" result-params)
175 org-babel-ruby-pp-last-value-eval
176 org-babel-ruby-last-value-eval)
177 org-babel-ruby-eoe-indicator) "\n"))
178 (raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
179 (insert full-body) (comint-send-input nil t)))
180 (results (cdr (member org-babel-ruby-eoe-indicator
181 (reverse (mapcar #'org-babel-ruby-read-string
182 (mapcar #'org-babel-trim raw)))))))
183 (case result-type
184 (output (mapconcat #'identity (reverse (cdr results)) "\n"))
185 (value
186 (if (or (member "code" result-params) (member "pp" result-params))
187 (car results)
188 (org-babel-ruby-table-or-string (car results))))))))
190 (defun org-babel-ruby-read-string (string)
191 "Strip \\\"s from around ruby string"
192 (if (string-match "\"\\([^\000]+\\)\"" string)
193 (match-string 1 string)
194 string))
196 (provide 'org-babel-ruby)
197 ;;; org-babel-ruby.el ends here