1 ;;; riece-ruby.el --- interact with Ruby interpreter
2 ;; Copyright (C) 1998-2005 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: IRC, riece
8 ;; This file is part of Riece.
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)
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.
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.
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:
31 ;; (riece-ruby-execute "t1" "sleep 30"); returns immediately
33 ;; (riece-ruby-execute "t2" "1 + 1")
35 ;; (riece-ruby-execute "t3" "\"")
37 ;; (riece-ruby-inspect "t1")
38 ;; => ((OK nil) nil "running")
40 ;; (riece-ruby-inspect "t2")
41 ;; => ((OK nil) "2" "finished")
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")
49 (defvar riece-ruby-command "ruby"
50 "Command name for Ruby interpreter.")
52 (defvar riece-ruby-process nil)
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)
60 (defvar riece-ruby-output-handler-alist nil)
61 (defvar riece-ruby-exit-handler-alist nil)
63 (defun riece-ruby-substitute-variables (program variable value)
64 (setq program (copy-sequence program))
65 (let ((pointer program))
67 (setq pointer (memq variable program))
69 (setcar pointer value)))
72 (defun riece-ruby-escape-data (data)
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) "")
79 index (+ (match-end 0)
80 (* (- (match-end 0) (match-beginning 0)) 2))))
83 (defun riece-ruby-unescape-data (data)
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) "\""))
89 index (- (match-end 0) 2)))
92 (defun riece-ruby-send-eval (name program)
93 (let* ((string (riece-ruby-escape-data program))
94 (length (- (length string) 998))
97 (while (< index length)
98 (setq data (cons (substring string index (setq index (+ index 998)))
100 (setq data (cons (substring string index) data)
101 data (nreverse data))
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"))
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")))
122 (defun riece-ruby-send-poll (name)
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")))
138 (defun riece-ruby-filter (process input)
140 (set-buffer (process-buffer process))
141 (goto-char (point-max))
143 (goto-char (process-mark process))
145 (while (looking-at ".*\r?\n")
146 (if (looking-at "OK\\( \\(.*\\)\\)?\r")
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")
156 (setq riece-ruby-input nil
158 (list 'ERR (string-to-number (match-string 2))
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)))
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)))
174 (funcall (cdr entry))))))))))
176 (set-marker (process-mark process) (point-marker))))
178 (defun riece-ruby-sentinel (process status)
179 (kill-buffer (process-buffer process)))
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*")
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))
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))))
200 (defun riece-ruby-inspect (name)
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
211 (provide 'riece-ruby)
213 ;;; riece-ruby.el ends here