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