5b21239d426eb9ef9856d9ee071e16ef76047210
[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 ;;
34 ;; (riece-ruby-execute "t2" "1 + 1")
35 ;; => nil
36 ;;
37 ;; (riece-ruby-execute "t3" "\"")
38 ;; => nil
39 ;;
40 ;; (riece-ruby-inspect "t1")
41 ;; => ((OK nil) nil "running")
42 ;;
43 ;; (riece-ruby-inspect "t2")
44 ;; => ((OK nil) "2" "finished")
45 ;;
46 ;; (riece-ruby-inspect "t3")
47 ;; => ((OK nil) "(eval):1: unterminated string meets end of file" "exited")
48
49 ;;; Code:
50
51 (defvar riece-ruby-command "ruby"
52   "Command name for Ruby interpreter.")
53
54 (defvar riece-ruby-process nil)
55
56 (defvar riece-ruby-lock nil)
57 (defvar riece-ruby-response nil)
58 (defvar riece-ruby-data nil)
59 (defvar riece-ruby-input nil)
60 (defvar riece-ruby-status nil)
61
62 (defvar riece-ruby-output-handler-alist nil)
63 (defvar riece-ruby-exit-handler-alist nil)
64
65 (defun riece-ruby-substitute-variables (program variable value)
66   (setq program (copy-sequence program))
67   (let ((pointer program))
68     (while pointer
69       (setq pointer (memq variable program))
70       (if pointer
71           (setcar pointer value)))
72     program))
73
74 (defun riece-ruby-escape-data (data)
75   (let ((index 0))
76     (while (string-match "[%\r\n]+" data index)
77       (setq data (replace-match
78                   (mapconcat (lambda (c) (format "%%%02X" c))
79                              (match-string 0 data) "")
80                   nil nil data)
81             index (+ (match-end 0)
82                      (* (- (match-end 0) (match-beginning 0)) 2))))
83     data))
84
85 (defun riece-ruby-unescape-data (data)
86   (let ((index 0))
87     (while (string-match "%\\([0-9A-F][0-9A-F]\\)" data index)
88       (setq data (replace-match
89                   (read (concat "\"\\x" (match-string 1 data) "\""))
90                   nil nil data)
91             index (- (match-end 0) 2)))
92     data))
93
94 (defun riece-ruby-send-eval (name program)
95   (let* ((string (riece-ruby-escape-data program))
96          (length (- (length string) 998))
97          (index 0)
98          data)
99     (while (< index length)
100       (setq data (cons (substring string index (setq index (+ index 998)))
101                        data)))
102     (setq data (cons (substring string index) data)
103           data (nreverse data))
104     (save-excursion
105       (set-buffer (process-buffer riece-ruby-process))
106       (make-local-variable 'riece-ruby-lock)
107       (setq riece-ruby-lock nil)
108       (make-local-variable 'riece-ruby-response)
109       (setq riece-ruby-response nil)
110       (make-local-variable 'riece-ruby-data)
111       (setq riece-ruby-data nil)
112       (make-local-variable 'riece-ruby-input)
113       (setq riece-ruby-input nil)
114       (make-local-variable 'riece-ruby-status)
115       (setq riece-ruby-status nil))
116     (process-send-string riece-ruby-process
117                          (concat "EVAL " name "\r\n"))
118     (while data
119       (process-send-string riece-ruby-process
120                            (concat "D " (car data) "\r\n"))
121       (setq data (cdr data)))
122     (process-send-string riece-ruby-process "END\r\n")))
123     
124 (defun riece-ruby-send-poll (name)
125   (save-excursion
126     (set-buffer (process-buffer riece-ruby-process))
127     (make-local-variable 'riece-ruby-lock)
128     (setq riece-ruby-lock nil)
129     (make-local-variable 'riece-ruby-response)
130     (setq riece-ruby-response nil)
131     (make-local-variable 'riece-ruby-data)
132     (setq riece-ruby-data nil)
133     (make-local-variable 'riece-ruby-input)
134     (setq riece-ruby-input nil)
135     (make-local-variable 'riece-ruby-status)
136     (setq riece-ruby-status nil))
137   (process-send-string riece-ruby-process
138                        (concat "POLL " name "\r\n")))
139
140 (defun riece-ruby-filter (process input)
141   (save-excursion
142     (set-buffer (process-buffer process))
143     (goto-char (point-max))
144     (insert input)
145     (goto-char (process-mark process))
146     (beginning-of-line)
147     (while (looking-at ".*\r?\n")
148       (if (looking-at "OK\\( \\(.*\\)\\)?\r")
149           (progn
150             (if riece-ruby-input
151                 (setq riece-ruby-data (mapconcat #'riece-ruby-unescape-data
152                                                  riece-ruby-input "")))
153             (setq riece-ruby-input nil
154                   riece-ruby-response (list 'OK (match-string 2))
155                   riece-ruby-lock nil))
156         (if (looking-at "ERR \\([0-9]+\\)\\( \\(.*\\)\\)?\r")
157             (progn
158               (setq riece-ruby-input nil
159                     riece-ruby-response
160                     (list 'ERR (string-to-number (match-string 2))
161                           (match-string 3))
162                     riece-ruby-lock nil))
163           (if (looking-at "D \\(.*\\)\r")
164               (setq riece-ruby-input (cons (match-string 1) riece-ruby-input))
165             (if (looking-at "S program \\(.*\\)\r")
166                 (setq riece-ruby-status (match-string 1))
167               (if (looking-at "# output \\(.*\\) \\(.*\\)\r")
168                   (let ((entry (assoc (match-string 1)
169                                       riece-ruby-output-handler-alist)))
170                     (if entry
171                         (funcall (cdr entry) (match-string 2))))
172                 (if (looking-at "# exit \\(.*\\)\r")
173                     (let ((entry (assoc (match-string 1)
174                                         riece-ruby-exit-handler-alist)))
175                       (if entry
176                           (funcall (cdr entry))))))))))
177       (forward-line))
178     (set-marker (process-mark process) (point-marker))))
179
180 (defun riece-ruby-sentinel (process status)
181   (kill-buffer (process-buffer process)))
182
183 (defun riece-ruby-execute (name program)
184   (unless (and riece-ruby-process
185                (eq (process-status riece-ruby-process) 'run))
186     (setq riece-ruby-process
187           (start-process "riece-ruby" (generate-new-buffer " *Ruby*")
188                          riece-ruby-command
189                          (expand-file-name
190                           "rubyserv.rb"
191                           (file-name-directory
192                            (symbol-file 'riece-ruby-execute)))))
193     (set-process-filter riece-ruby-process #'riece-ruby-filter)
194     (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel))
195   (save-excursion
196     (set-buffer (process-buffer riece-ruby-process))
197     (setq riece-ruby-lock t)
198     (riece-ruby-send-eval name program)
199     (while riece-ruby-lock
200       (accept-process-output riece-ruby-process))))
201
202 (defun riece-ruby-inspect (name)
203   (save-excursion
204     (set-buffer (process-buffer riece-ruby-process))
205     (setq riece-ruby-lock t)
206     (riece-ruby-send-poll name)
207     (while (null riece-ruby-response)
208       (accept-process-output riece-ruby-process))
209     (list riece-ruby-response
210           riece-ruby-data
211           riece-ruby-status)))
212
213 (provide 'riece-ruby)
214
215 ;;; riece-ruby.el ends here