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