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