1 ;;; riece-ruby.el --- interact with Ruby interpreter
2 ;; Copyright (C) 1998-2005 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: IRC, riece
8 ;; This file is part of Riece.
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; riece-ruby.el is a library to interact with the Ruby interpreter.
28 ;; It supports concurrent execution of Ruby programs in a single
29 ;; session. For example:
31 ;; (riece-ruby-execute "t1" "sleep 30"); returns immediately
33 ;; (riece-ruby-execute "t2" "1 + 1")
35 ;; (riece-ruby-execute "t3" "\"")
38 ;; (riece-ruby-inspect "t1")
39 ;; => ((OK nil) nil "running")
40 ;; (riece-ruby-inspect "t2")
41 ;; => ((OK nil) "2" "finished")
42 ;; (riece-ruby-inspect "t3")
43 ;; => ((OK nil) "(eval):1:in `dispatch_eval': compile error
44 ;; (eval):1: unterminated string meets end of file" "exited")
48 (defvar riece-ruby-command "ruby"
49 "Command name for Ruby interpreter.")
51 (defvar riece-ruby-process nil)
53 (defvar riece-ruby-lock nil)
54 (defvar riece-ruby-response nil)
55 (defvar riece-ruby-data nil)
56 (defvar riece-ruby-input nil)
57 (defvar riece-ruby-status nil)
59 (defvar riece-ruby-output-handler-alist nil)
60 (defvar riece-ruby-exit-handler-alist nil)
62 (defun riece-ruby-substitute-variables (program variable value)
63 (setq program (copy-sequence program))
64 (let ((pointer program))
66 (setq pointer (memq variable program))
68 (setcar pointer value)))
71 (defun riece-ruby-escape-data (data)
73 (while (string-match "[%\r\n]+" data index)
74 (setq data (replace-match
75 (mapconcat (lambda (c) (format "%%%02X" c))
76 (match-string 0 data) "")
78 index (+ (match-end 0)
79 (* (- (match-end 0) (match-beginning 0)) 2))))
82 (defun riece-ruby-unescape-data (data)
84 (while (string-match "%\\([0-9A-F][0-9A-F]\\)" data index)
85 (setq data (replace-match
86 (read (concat "\"\\x" (match-string 1 data) "\""))
88 index (- (match-end 0) 2)))
91 (defun riece-ruby-send-eval (name program)
92 (let* ((string (riece-ruby-escape-data program))
93 (length (- (length string) 998))
96 (while (< index length)
97 (setq data (cons (substring string index (setq index (+ index 998)))
99 (setq data (cons (substring string index) data)
100 data (nreverse data))
102 (set-buffer (process-buffer riece-ruby-process))
103 (make-local-variable 'riece-ruby-lock)
104 (setq riece-ruby-lock nil)
105 (make-local-variable 'riece-ruby-response)
106 (setq riece-ruby-response nil)
107 (make-local-variable 'riece-ruby-data)
108 (setq riece-ruby-data nil)
109 (make-local-variable 'riece-ruby-input)
110 (setq riece-ruby-input nil)
111 (make-local-variable 'riece-ruby-status)
112 (setq riece-ruby-status nil))
113 (process-send-string riece-ruby-process
114 (concat "EVAL " name "\r\n"))
116 (process-send-string riece-ruby-process
117 (concat "D " (car data) "\r\n"))
118 (setq data (cdr data)))
119 (process-send-string riece-ruby-process "END\r\n")))
121 (defun riece-ruby-send-poll (name)
123 (set-buffer (process-buffer riece-ruby-process))
124 (make-local-variable 'riece-ruby-lock)
125 (setq riece-ruby-lock nil)
126 (make-local-variable 'riece-ruby-response)
127 (setq riece-ruby-response nil)
128 (make-local-variable 'riece-ruby-data)
129 (setq riece-ruby-data nil)
130 (make-local-variable 'riece-ruby-input)
131 (setq riece-ruby-input nil)
132 (make-local-variable 'riece-ruby-status)
133 (setq riece-ruby-status nil))
134 (process-send-string riece-ruby-process
135 (concat "POLL " name "\r\n")))
137 (defun riece-ruby-filter (process input)
139 (set-buffer (process-buffer process))
140 (goto-char (point-max))
142 (goto-char (process-mark process))
144 (while (looking-at ".*\r?\n")
145 (if (looking-at "OK\\( \\(.*\\)\\)?\r")
148 (setq riece-ruby-data (mapconcat #'riece-ruby-unescape-data
149 riece-ruby-input "")))
150 (setq riece-ruby-input nil
151 riece-ruby-response (list 'OK (match-string 2))
152 riece-ruby-lock nil))
153 (if (looking-at "ERR \\([0-9]+\\)\\( \\(.*\\)\\)?\r")
155 (setq riece-ruby-input nil
157 (list 'ERR (string-to-number (match-string 2))
159 riece-ruby-lock nil))
160 (if (looking-at "D \\(.*\\)\r")
161 (setq riece-ruby-input (cons (match-string 1) riece-ruby-input))
162 (if (looking-at "S program \\(.*\\)\r")
163 (setq riece-ruby-status (match-string 1))
164 (if (looking-at "# output \\(.*\\) \\(.*\\)\r")
165 (let ((entry (assoc (match-string 1)
166 riece-ruby-output-handler-alist)))
168 (funcall (cdr entry) (match-string 2))))
169 (if (looking-at "# exit \\(.*\\)\r")
170 (let ((entry (assoc (match-string 1)
171 riece-ruby-exit-handler-alist)))
173 (funcall (cdr entry))))))))))
175 (set-marker (process-mark process) (point-marker))))
177 (defun riece-ruby-sentinel (process status)
178 (kill-buffer (process-buffer process)))
180 (defun riece-ruby-execute (name program)
181 (unless (and riece-ruby-process
182 (eq (process-status riece-ruby-process) 'run))
183 (setq riece-ruby-process
184 (start-process "riece-ruby" (generate-new-buffer " *Ruby*")
189 (symbol-file 'riece-ruby-execute)))))
190 (set-process-filter riece-ruby-process #'riece-ruby-filter)
191 (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel))
193 (set-buffer (process-buffer riece-ruby-process))
194 (setq riece-ruby-lock t)
195 (riece-ruby-send-eval name program)
196 (while riece-ruby-lock
197 (accept-process-output riece-ruby-process))))
199 (defun riece-ruby-inspect (name)
201 (set-buffer (process-buffer riece-ruby-process))
202 (setq riece-ruby-lock t)
203 (riece-ruby-send-poll name)
204 (while (null riece-ruby-response)
205 (accept-process-output riece-ruby-process))
206 (list riece-ruby-response
210 (provide 'riece-ruby)
212 ;;; riece-ruby.el ends here