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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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."
58 (defcustom riece-ruby-command "ruby"
59 "Command name for Ruby interpreter."
63 (defcustom riece-ruby-out-file (expand-file-name "riece-ruby.out"
65 "A file which records stdout of Ruby programs."
69 (defcustom riece-ruby-err-file (expand-file-name "riece-ruby.err"
71 "A file which records stderr of Ruby programs."
75 (defcustom riece-ruby-log-file (expand-file-name "riece-ruby.log"
77 "A file used to logging."
81 (defvar riece-ruby-server-program "server.rb"
82 "The server program file. If the filename is not absolute, it is
83 assumed that the file is in the same directory of this file.")
85 (defvar riece-ruby-server-program-arguments (list "-o" riece-ruby-out-file
86 "-e" riece-ruby-err-file
87 "-l" riece-ruby-log-file)
88 "Command line arguments passed to `riece-ruby-server-program'.")
90 (defvar riece-ruby-process nil
91 "Process object of Ruby interpreter.")
93 (defvar riece-ruby-lock nil
94 "Lock for waiting server response.
95 Local to the process buffer.")
96 (defvar riece-ruby-response nil
98 Local to the process buffer.")
99 (defvar riece-ruby-data nil
101 Local to the process buffer.")
102 (defvar riece-ruby-escaped-data nil
103 "Escaped data from server. This variable is cleared every time
104 server response arrives.
105 Local to the process buffer.")
106 (defvar riece-ruby-status-alist nil
108 Local to the process buffer.")
110 (defvar riece-ruby-output-queue-alist nil
111 "An alist mapping from program name to output data.")
112 (defvar riece-ruby-output-handler-alist nil
113 "An alist mapping from program name to output handler.
114 Output handlers are called every time \"# output\" line arrives.
115 Use `riece-ruby-set-output-handler' to set this variable.")
116 (defvar riece-ruby-exit-handler-alist nil
117 "An alist mapping from program name to exit handler.
118 Exit handlers are called once when \"# exit\" line arrives.
119 Use `riece-ruby-set-exit-handler' to set this variable.")
120 (defvar riece-ruby-property-alist nil
121 "An alist mapping from program name to the property list.
122 Use `riece-ruby-set-property' to set this variable.")
124 (defun riece-ruby-escape-data (data)
126 (while (string-match "[%\r\n]+" data index)
127 (setq data (replace-match
128 (mapconcat (lambda (c) (format "%%%02X" c))
129 (match-string 0 data) "")
131 index (+ (match-end 0)
132 (* (- (match-end 0) (match-beginning 0)) 2))))
135 (defun riece-ruby-unescape-data (data)
137 (while (string-match "%\\([0-9A-F][0-9A-F]\\)" data index)
138 (setq data (replace-match
139 (read (concat "\"\\x" (match-string 1 data) "\""))
141 index (- (match-end 0) 2)))
144 (defun riece-ruby-reset-process-buffer ()
146 (set-buffer (process-buffer riece-ruby-process))
147 (buffer-disable-undo)
148 (make-local-variable 'riece-ruby-response)
149 (setq riece-ruby-response nil)
150 (make-local-variable 'riece-ruby-data)
151 (setq riece-ruby-data nil)
152 (make-local-variable 'riece-ruby-escaped-data)
153 (setq riece-ruby-escaped-data nil)
154 (make-local-variable 'riece-ruby-status-alist)
155 (setq riece-ruby-status-alist nil)))
157 (defun riece-ruby-send-eval (program)
158 (let* ((string (riece-ruby-escape-data program))
159 (length (- (length string) 998))
162 (while (< index length)
163 (setq data (cons (substring string index (setq index (+ index 998)))
165 (setq data (cons (substring string index) data)
166 data (nreverse data))
167 (process-send-string riece-ruby-process "EVAL\r\n")
169 (process-send-string riece-ruby-process
170 (concat "D " (car data) "\r\n"))
171 (setq data (cdr data)))
172 (process-send-string riece-ruby-process "END\r\n")))
174 (defun riece-ruby-send-poll (name)
175 (process-send-string riece-ruby-process
176 (concat "POLL " name "\r\n")))
178 (defun riece-ruby-send-exit (name)
179 (process-send-string riece-ruby-process
180 (concat "EXIT " name "\r\n")))
182 (defun riece-ruby-filter (process input)
184 (set-buffer (process-buffer process))
185 (goto-char (point-max))
187 (goto-char (point-min))
189 (while (looking-at ".*\r\n")
190 (if (looking-at "OK\\( \\(.*\\)\\)?\r")
192 (if riece-ruby-escaped-data
193 (setq riece-ruby-data (mapconcat #'riece-ruby-unescape-data
194 riece-ruby-escaped-data "")))
195 (setq riece-ruby-escaped-data nil
196 riece-ruby-response (list 'OK (match-string 2))
197 riece-ruby-lock nil))
198 (if (looking-at "ERR \\([0-9]+\\)\\( \\(.*\\)\\)?\r")
200 (setq riece-ruby-escaped-data nil
202 (list 'ERR (string-to-number (match-string 1))
204 riece-ruby-lock nil))
205 (if (looking-at "D \\(.*\\)\r")
206 (setq riece-ruby-escaped-data (cons (match-string 1)
207 riece-ruby-escaped-data))
208 (if (looking-at "S \\([^ ]*\\) \\(.*\\)\r")
210 (setq riece-ruby-status-alist (cons (cons (match-string 1)
212 riece-ruby-status-alist))
213 (if (member (car (car riece-ruby-status-alist))
214 '("finished" "exited"))
215 (riece-ruby-run-exit-handler
216 (cdr (car riece-ruby-status-alist)))))
217 (if (looking-at "# output \\([^ ]*\\) \\(.*\\)\r")
218 (riece-ruby-run-output-handler (match-string 1)
221 (if (looking-at "# exit \\(.*\\)\r")
222 (riece-ruby-run-exit-handler (match-string 1))))))))
224 (delete-region (point-min) (point))))
226 (defun riece-ruby-run-exit-handler (name)
227 (let ((entry (assoc name riece-ruby-exit-handler-alist)))
229 (setq riece-ruby-exit-handler-alist
230 (delq entry riece-ruby-exit-handler-alist))
231 (riece-funcall-ignore-errors (if (symbolp (cdr entry))
232 (symbol-name (cdr entry))
233 (format "%s-exit-handler" name))
234 (cdr entry) (car entry))
235 (riece-ruby-clear name))))
237 (defun riece-ruby-run-output-handler (name output time)
238 (let ((handler-entry (assoc name riece-ruby-output-handler-alist))
239 (entry (assoc name riece-ruby-output-queue-alist)))
241 (riece-funcall-ignore-errors (if (symbolp (cdr handler-entry))
242 (symbol-name (cdr handler-entry))
243 (format "%s-output-handler" name))
244 (cdr handler-entry) name output time)
246 (setcdr entry (cons (cons output time) (cdr entry)))
247 (setq riece-ruby-output-queue-alist
248 (cons (list name (cons output time))
249 riece-ruby-output-queue-alist))))))
251 (defun riece-ruby-sentinel (process status)
252 (kill-buffer (process-buffer process)))
254 (defun riece-ruby-execute (program)
255 "Schedule an execution of a Ruby PROGRAM.
256 Return a string name assigned by the server."
257 (unless (and riece-ruby-process
258 (eq (process-status riece-ruby-process) 'run))
259 (let (selective-display
260 (coding-system-for-write 'binary)
261 (coding-system-for-read 'binary))
262 (setq riece-ruby-process
263 (apply #'start-process "riece-ruby" (generate-new-buffer " *Ruby*")
265 (expand-file-name riece-ruby-server-program
266 riece-data-directory)
267 riece-ruby-server-program-arguments))
268 (process-kill-without-query riece-ruby-process)
269 (set-process-filter riece-ruby-process #'riece-ruby-filter)
270 (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel)))
272 (set-buffer (process-buffer riece-ruby-process))
273 (riece-ruby-reset-process-buffer)
274 (make-local-variable 'riece-ruby-lock)
275 (setq riece-ruby-lock t)
276 (riece-ruby-send-eval program)
277 (while riece-ruby-lock
278 (accept-process-output riece-ruby-process))
279 (if (eq (car riece-ruby-response) 'ERR)
280 (error "Couldn't execute: %S" (cdr riece-ruby-response)))
281 (cdr (assoc "name" riece-ruby-status-alist))))
283 (defun riece-ruby-inspect (name)
284 "Inspect a result of program execution distinguished by NAME.
285 Return a three element list.
286 The car is protocol response line which looks like:
287 \(ERR 103 \"Not implemented\").
288 The cadr is data from the server, that is, the result of the program.
289 The caddr is status from the server."
291 (set-buffer (process-buffer riece-ruby-process))
292 (riece-ruby-reset-process-buffer)
293 (make-local-variable 'riece-ruby-lock)
294 (setq riece-ruby-lock t)
295 (riece-ruby-send-poll name)
296 (while riece-ruby-lock
297 (accept-process-output riece-ruby-process))
298 (list riece-ruby-response
300 riece-ruby-status-alist)))
302 (defun riece-ruby-clear (name)
303 "Clear a result of program execution distinguished by NAME.
304 Note that riece-ruby-clear is automatically called iff an exit-handler
305 is specified. Otherwise, it should be called explicitly."
307 (set-buffer (process-buffer riece-ruby-process))
308 (riece-ruby-reset-process-buffer)
309 (make-local-variable 'riece-ruby-lock)
310 (setq riece-ruby-lock t)
311 (riece-ruby-send-exit name)
312 (while riece-ruby-lock
313 (accept-process-output riece-ruby-process)))
314 (let ((entry (assoc name riece-ruby-property-alist)))
316 (delq entry riece-ruby-property-alist))))
318 (defun riece-ruby-set-exit-handler (name handler)
319 "Set an exit-handler HANDLER for the program distinguished by NAME.
320 An exit-handler is called when the program is finished or exited abnormally.
321 An exit-handler is called with an argument same as NAME.
322 Note that riece-ruby-clear is automatically called iff an exit-handler
323 is specified. Otherwise, it should be called explicitly."
324 (let ((entry (assoc name riece-ruby-exit-handler-alist)))
328 (setcdr entry handler)
329 (setq riece-ruby-exit-handler-alist
330 (cons (cons name handler)
331 riece-ruby-exit-handler-alist)))
332 ;;check if the program already exited
333 (riece-ruby-inspect name))
335 (setq riece-ruby-exit-handler-alist
336 (delq entry riece-ruby-exit-handler-alist))))))
338 (defun riece-ruby-set-output-handler (name handler)
339 "Set an output-handler HANDLER for the program distinguished by NAME.
340 An output-handler is called when the program sends any output by using
341 `output' method in the Ruby program.
342 An output-handler is called with three argument. The first argument
343 is the same as NAME. The second argument is the output string. The
344 third argument is the timestamp of the output event."
345 (let ((entry (assoc name riece-ruby-output-handler-alist))
349 (when (setq queue-entry (assoc name riece-ruby-output-queue-alist))
350 (setq pointer (nreverse (cdr queue-entry))
351 riece-ruby-output-queue-alist
352 (delq queue-entry riece-ruby-output-queue-alist))
354 (riece-funcall-ignore-errors (if (symbolp handler)
355 (symbol-name handler)
356 (format "%s-output-handler" name))
357 handler name (car (car pointer))
359 (setq pointer (cdr pointer))))
361 (setcdr entry handler)
362 (setq riece-ruby-output-handler-alist
363 (cons (cons name handler)
364 riece-ruby-output-handler-alist))))
366 (setq riece-ruby-output-handler-alist
367 (delq entry riece-ruby-output-handler-alist))))))
369 (defun riece-ruby-set-property (name property value)
370 "Set given PROPERTY/VALUE pair to the program distinguished by NAME."
371 (let ((entry (assoc name riece-ruby-property-alist))
374 (setq entry (list name)
375 riece-ruby-property-alist (cons entry riece-ruby-property-alist)))
376 (if (setq property-entry (assoc property (cdr entry)))
377 (setcdr property-entry value)
378 (setcdr entry (cons (cons property value) (cdr entry))))))
380 (defun riece-ruby-property (name property)
381 "Return the value of PROPERTY set to the program distinguished by NAME."
382 (cdr (assoc property (cdr (assoc name riece-ruby-property-alist)))))
384 (defun riece-ruby-substitute-variables (program alist)
385 "Substitute symbols in PROGRAM by looking up ALIST.
386 Return a string concatenating elements in PROGRAM."
387 (setq program (copy-sequence program))
389 (let ((pointer program))
391 (setq pointer (memq (car (car alist)) program))
393 (setcar pointer (cdr (car alist))))))
394 (setq alist (cdr alist)))
395 (apply #'concat program))
397 (provide 'riece-ruby)
399 ;;; riece-ruby.el ends here