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, Ruby
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 Ruby interpreter.
28 ;; It supports concurrent execution of Ruby programs in a single
29 ;; session. For example:
31 ;; (riece-ruby-execute "sleep 30"); returns immediately
34 ;; (riece-ruby-execute "1 + 1")
37 ;; (riece-ruby-execute "\"")
40 ;; (riece-ruby-inspect "rubyserv0")
41 ;; => ((OK nil) nil (("running")))
43 ;; (riece-ruby-inspect "rubyserv1")
44 ;; => ((OK nil) "2" (("finished")))
46 ;; (riece-ruby-inspect "rubyserv2")
47 ;; => ((OK nil) "(eval):1: unterminated string meets end of file" (("exited")))
51 (require 'riece-debug)
53 (defgroup riece-ruby nil
54 "Interact with Ruby interpreter."
57 (defcustom riece-ruby-command "ruby"
58 "Command name for Ruby interpreter."
62 (defcustom riece-ruby-out-file (expand-file-name "riece-ruby.out"
64 "A file which records stdout of Ruby programs."
68 (defcustom riece-ruby-err-file (expand-file-name "riece-ruby.err"
70 "A file which records stderr of Ruby programs."
74 (defcustom riece-ruby-log-file (expand-file-name "riece-ruby.log"
76 "A file used to logging."
80 (defvar riece-ruby-server-program "server.rb"
81 "The server program file. If the filename is not absolute, it is
82 assumed that the file is in the same directory of this file.")
84 (defvar riece-ruby-server-program-arguments (list "-o" riece-ruby-out-file
85 "-e" riece-ruby-err-file
86 "-l" riece-ruby-log-file)
87 "Command line arguments passed to `riece-ruby-server-program'.")
89 (defvar riece-ruby-process nil
90 "Process object of Ruby interpreter.")
92 (defvar riece-ruby-lock nil
93 "Lock for waiting server response.
94 Local to the process buffer.")
95 (defvar riece-ruby-response nil
97 Local to the process buffer.")
98 (defvar riece-ruby-data nil
100 Local to the process buffer.")
101 (defvar riece-ruby-escaped-data nil
102 "Escaped data from server. This variable is cleared every time
103 server response arrives.
104 Local to the process buffer.")
105 (defvar riece-ruby-status-alist nil
107 Local to the process buffer.")
109 (defvar riece-ruby-output-queue-alist nil
110 "An alist mapping from program name to output data.")
111 (defvar riece-ruby-output-handler-alist nil
112 "An alist mapping from program name to output handler.
113 Output handlers are called every time \"# output\" line arrives.
114 Use `riece-ruby-set-output-handler' to set this variable.")
115 (defvar riece-ruby-exit-handler-alist nil
116 "An alist mapping from program name to exit handler.
117 Exit handlers are called once when \"# exit\" line arrives.
118 Use `riece-ruby-set-exit-handler' to set this variable.")
119 (defvar riece-ruby-property-alist nil
120 "An alist mapping from program name to the property list.
121 Use `riece-ruby-set-property' to set this variable.")
123 (defun riece-ruby-escape-data (data)
125 (while (string-match "[%\r\n]+" data index)
126 (setq data (replace-match
127 (mapconcat (lambda (c) (format "%%%02X" c))
128 (match-string 0 data) "")
130 index (+ (match-end 0)
131 (* (- (match-end 0) (match-beginning 0)) 2))))
134 (defun riece-ruby-unescape-data (data)
136 (while (string-match "%\\([0-9A-F][0-9A-F]\\)" data index)
137 (setq data (replace-match
138 (read (concat "\"\\x" (match-string 1 data) "\""))
140 index (- (match-end 0) 2)))
143 (defun riece-ruby-reset-process-buffer ()
145 (set-buffer (process-buffer riece-ruby-process))
146 (buffer-disable-undo)
147 (make-local-variable 'riece-ruby-response)
148 (setq riece-ruby-response nil)
149 (make-local-variable 'riece-ruby-data)
150 (setq riece-ruby-data nil)
151 (make-local-variable 'riece-ruby-escaped-data)
152 (setq riece-ruby-escaped-data nil)
153 (make-local-variable 'riece-ruby-status-alist)
154 (setq riece-ruby-status-alist nil)))
156 (defun riece-ruby-send-eval (program)
157 (let* ((string (riece-ruby-escape-data program))
158 (length (- (length string) 998))
161 (while (< index length)
162 (setq data (cons (substring string index (setq index (+ index 998)))
164 (setq data (cons (substring string index) data)
165 data (nreverse data))
166 (process-send-string riece-ruby-process "EVAL\r\n")
168 (process-send-string riece-ruby-process
169 (concat "D " (car data) "\r\n"))
170 (setq data (cdr data)))
171 (process-send-string riece-ruby-process "END\r\n")))
173 (defun riece-ruby-send-poll (name)
174 (process-send-string riece-ruby-process
175 (concat "POLL " name "\r\n")))
177 (defun riece-ruby-send-exit (name)
178 (process-send-string riece-ruby-process
179 (concat "EXIT " name "\r\n")))
181 (defun riece-ruby-filter (process input)
183 (set-buffer (process-buffer process))
184 (goto-char (point-max))
186 (goto-char (point-min))
188 (while (looking-at ".*\r\n")
189 (if (looking-at "OK\\( \\(.*\\)\\)?\r")
191 (if riece-ruby-escaped-data
192 (setq riece-ruby-data (mapconcat #'riece-ruby-unescape-data
193 riece-ruby-escaped-data "")))
194 (setq riece-ruby-escaped-data nil
195 riece-ruby-response (list 'OK (match-string 2))
196 riece-ruby-lock nil))
197 (if (looking-at "ERR \\([0-9]+\\)\\( \\(.*\\)\\)?\r")
199 (setq riece-ruby-escaped-data nil
201 (list 'ERR (string-to-number (match-string 1))
203 riece-ruby-lock nil))
204 (if (looking-at "D \\(.*\\)\r")
205 (setq riece-ruby-escaped-data (cons (match-string 1)
206 riece-ruby-escaped-data))
207 (if (looking-at "S \\([^ ]*\\) \\(.*\\)\r")
209 (setq riece-ruby-status-alist (cons (cons (match-string 1)
211 riece-ruby-status-alist))
212 (if (member (car (car riece-ruby-status-alist))
213 '("finished" "exited"))
214 (riece-ruby-run-exit-handler
215 (cdr (car riece-ruby-status-alist)))))
216 (if (looking-at "# output \\([^ ]*\\) \\(.*\\)\r")
217 (riece-ruby-run-output-handler (match-string 1)
220 (if (looking-at "# exit \\(.*\\)\r")
221 (riece-ruby-run-exit-handler (match-string 1))))))))
223 (delete-region (point-min) (point))))
225 (defun riece-ruby-run-exit-handler (name)
226 (let ((entry (assoc name riece-ruby-exit-handler-alist)))
228 (setq riece-ruby-exit-handler-alist
229 (delq entry riece-ruby-exit-handler-alist))
230 (riece-funcall-ignore-errors (if (symbolp (cdr entry))
231 (symbol-name (cdr entry))
232 (format "%s-exit-handler" name))
233 (cdr entry) (car entry))
234 (riece-ruby-clear name))))
236 (defun riece-ruby-run-output-handler (name output time)
237 (let ((handler-entry (assoc name riece-ruby-output-handler-alist))
238 (entry (assoc name riece-ruby-output-queue-alist)))
240 (riece-funcall-ignore-errors (if (symbolp (cdr handler-entry))
241 (symbol-name (cdr handler-entry))
242 (format "%s-output-handler" name))
243 (cdr handler-entry) name output time)
245 (setcdr entry (cons output (cdr entry)))
246 (setq riece-ruby-output-queue-alist
247 (cons (list name (cons output time))
248 riece-ruby-output-queue-alist))))))
250 (defun riece-ruby-sentinel (process status)
251 (kill-buffer (process-buffer process)))
253 (defun riece-ruby-execute (program)
254 "Schedule an execution of a Ruby PROGRAM.
255 Return a string name assigned by the server."
256 (unless (and riece-ruby-process
257 (eq (process-status riece-ruby-process) 'run))
258 (let (selective-display
259 (coding-system-for-write 'binary)
260 (coding-system-for-read 'binary))
261 (setq riece-ruby-process
262 (apply #'start-process "riece-ruby" (generate-new-buffer " *Ruby*")
264 (expand-file-name riece-ruby-server-program
265 riece-data-directory)
266 riece-ruby-server-program-arguments))
267 (process-kill-without-query riece-ruby-process)
268 (set-process-filter riece-ruby-process #'riece-ruby-filter)
269 (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel)))
271 (set-buffer (process-buffer riece-ruby-process))
272 (riece-ruby-reset-process-buffer)
273 (make-local-variable 'riece-ruby-lock)
274 (setq riece-ruby-lock t)
275 (riece-ruby-send-eval program)
276 (while riece-ruby-lock
277 (accept-process-output riece-ruby-process))
278 (if (eq (car riece-ruby-response) 'ERR)
279 (error "Couldn't execute: %S" (cdr riece-ruby-response)))
280 (cdr (assoc "name" riece-ruby-status-alist))))
282 (defun riece-ruby-inspect (name)
283 "Inspect a result of program execution distinguished by NAME.
284 Return a three element list.
285 The car is protocol response line which looks like:
286 \(ERR 103 \"Not implemented\").
287 The cadr is data from the server, that is, the result of the program.
288 The caddr is status from the server."
290 (set-buffer (process-buffer riece-ruby-process))
291 (riece-ruby-reset-process-buffer)
292 (make-local-variable 'riece-ruby-lock)
293 (setq riece-ruby-lock t)
294 (riece-ruby-send-poll name)
295 (while riece-ruby-lock
296 (accept-process-output riece-ruby-process))
297 (list riece-ruby-response
299 riece-ruby-status-alist)))
301 (defun riece-ruby-clear (name)
302 "Clear a result of program execution distinguished by NAME.
303 Note that riece-ruby-clear is automatically called iff an exit-handler
304 is specified. Otherwise, it should be called explicitly."
306 (set-buffer (process-buffer riece-ruby-process))
307 (riece-ruby-reset-process-buffer)
308 (make-local-variable 'riece-ruby-lock)
309 (setq riece-ruby-lock t)
310 (riece-ruby-send-exit name)
311 (while riece-ruby-lock
312 (accept-process-output riece-ruby-process)))
313 (let ((entry (assoc name riece-ruby-property-alist)))
315 (delq entry riece-ruby-property-alist))))
317 (defun riece-ruby-set-exit-handler (name handler)
318 "Set an exit-handler HANDLER for the program distinguished by NAME.
319 An exit-handler is called when the program is finished or exited abnormally.
320 An exit-handler is called with an argument same as NAME.
321 Note that riece-ruby-clear is automatically called iff an exit-handler
322 is specified. Otherwise, it should be called explicitly."
323 (let ((entry (assoc name riece-ruby-exit-handler-alist)))
327 (setcdr entry handler)
328 (setq riece-ruby-exit-handler-alist
329 (cons (cons name handler)
330 riece-ruby-exit-handler-alist)))
331 ;;check if the program already exited
332 (riece-ruby-inspect name))
334 (setq riece-ruby-exit-handler-alist
335 (delq entry riece-ruby-exit-handler-alist))))))
337 (defun riece-ruby-set-output-handler (name handler)
338 "Set an output-handler HANDLER for the program distinguished by NAME.
339 An output-handler is called when the program sends any output by using
340 `output' method in the Ruby program.
341 An output-handler is called with three argument. The first argument
342 is the same as NAME. The second argument is the output string. The
343 third argument is the timestamp of the output event."
344 (let ((entry (assoc name riece-ruby-output-handler-alist))
348 (when (setq queue-entry (assoc name riece-ruby-output-queue-alist))
349 (setq pointer (nreverse (cdr queue-entry))
350 riece-ruby-output-queue-alist
351 (delq queue-entry riece-ruby-output-queue-alist))
353 (riece-funcall-ignore-errors (if (symbolp handler)
354 (symbol-name handler)
355 (format "%s-output-handler" name))
356 handler name (car (car pointer))
358 (setq pointer (cdr pointer))))
360 (setcdr entry handler)
361 (setq riece-ruby-output-handler-alist
362 (cons (cons name handler)
363 riece-ruby-output-handler-alist))))
365 (setq riece-ruby-output-handler-alist
366 (delq entry riece-ruby-output-handler-alist))))))
368 (defun riece-ruby-set-property (name property value)
369 "Set given PROPERTY/VALUE pair to the program distinguished by NAME."
370 (let ((entry (assoc name riece-ruby-property-alist))
373 (setq entry (list name)
374 riece-ruby-property-alist (cons entry riece-ruby-property-alist)))
375 (if (setq property-entry (assoc property (cdr entry)))
376 (setcdr property-entry value)
377 (setcdr entry (cons (cons property value) (cdr entry))))))
379 (defun riece-ruby-property (name property)
380 "Return the value of PROPERTY set to the program distinguished by NAME."
381 (cdr (assoc property (cdr (assoc name riece-ruby-property-alist)))))
383 (defun riece-ruby-substitute-variables (program alist)
384 "Substitute symbols in PROGRAM by looking up ALIST.
385 Return a string concatenating elements in PROGRAM."
386 (setq program (copy-sequence program))
388 (let ((pointer program))
390 (setq pointer (memq (car (car alist)) program))
392 (setcar pointer (cdr (car alist))))))
393 (setq alist (cdr alist)))
394 (apply #'concat program))
396 (provide 'riece-ruby)
398 ;;; riece-ruby.el ends here