Fixed.
[riece] / lisp / riece-ruby.el
1 ;;; riece-ruby.el --- interact with Ruby interpreter
2 ;; Copyright (C) 1998-2005 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
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:
30 ;; 
31 ;; (riece-ruby-execute "t1" "sleep 30"); returns immediately
32 ;; => nil
33 ;; (riece-ruby-execute "t2" "1 + 1")
34 ;; => nil
35 ;; (riece-ruby-execute "t3" "\"")
36 ;; => nil
37 ;;
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")
45
46 ;;; Code:
47
48 (defvar riece-ruby-command "ruby"
49   "Command name for Ruby interpreter.")
50
51 (defvar riece-ruby-process nil)
52
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)
58
59 (defvar riece-ruby-output-handler-alist nil)
60 (defvar riece-ruby-exit-handler-alist nil)
61
62 (defun riece-ruby-substitute-variables (program variable value)
63   (setq program (copy-sequence program))
64   (let ((pointer program))
65     (while pointer
66       (setq pointer (memq variable program))
67       (if pointer
68           (setcar pointer value)))
69     program))
70
71 (defun riece-ruby-escape-data (data)
72   (let ((index 0))
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) "")
77                   nil nil data)
78             index (+ (match-end 0)
79                      (* (- (match-end 0) (match-beginning 0)) 2))))
80     data))
81
82 (defun riece-ruby-unescape-data (data)
83   (let ((index 0))
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) "\""))
87                   nil nil data)
88             index (- (match-end 0) 2)))
89     data))
90
91 (defun riece-ruby-send-eval (name program)
92   (let* ((string (riece-ruby-escape-data program))
93          (length (- (length string) 998))
94          (index 0)
95          data)
96     (while (< index length)
97       (setq data (cons (substring string index (setq index (+ index 998)))
98                        data)))
99     (setq data (cons (substring string index) data)
100           data (nreverse data))
101     (save-excursion
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"))
115     (while data
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")))
120     
121 (defun riece-ruby-send-poll (name)
122   (save-excursion
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")))
136
137 (defun riece-ruby-filter (process input)
138   (save-excursion
139     (set-buffer (process-buffer process))
140     (goto-char (point-max))
141     (insert input)
142     (goto-char (process-mark process))
143     (beginning-of-line)
144     (while (looking-at ".*\r?\n")
145       (if (looking-at "OK\\( \\(.*\\)\\)?\r")
146           (progn
147             (if riece-ruby-input
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")
154             (progn
155               (setq riece-ruby-input nil
156                     riece-ruby-response
157                     (list 'ERR (string-to-number (match-string 2))
158                           (match-string 3))
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)))
167                     (if entry
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)))
172                       (if entry
173                           (funcall (cdr entry))))))))))
174       (forward-line))
175     (set-marker (process-mark process) (point-marker))))
176
177 (defun riece-ruby-sentinel (process status)
178   (kill-buffer (process-buffer process)))
179
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*")
185                          riece-ruby-command
186                          (expand-file-name
187                           "rubyserv.rb"
188                           (file-name-directory
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))
192   (save-excursion
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))))
198
199 (defun riece-ruby-inspect (name)
200   (save-excursion
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
207           riece-ruby-data
208           riece-ruby-status)))
209
210 (provide 'riece-ruby)
211
212 ;;; riece-ruby.el ends here