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