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
34 ;; (riece-ruby-execute "t2" "1 + 1")
37 ;; (riece-ruby-execute "t3" "\"")
40 ;; (riece-ruby-inspect "t1")
41 ;; => ((OK nil) nil "running")
43 ;; (riece-ruby-inspect "t2")
44 ;; => ((OK nil) "2" "finished")
46 ;; (riece-ruby-inspect "t3")
47 ;; => ((OK nil) "(eval):1:in `dispatch_eval': compile error
48 ;; (eval):1: unterminated string meets end of file" "exited")
52 (defvar riece-ruby-command "ruby"
53 "Command name for Ruby interpreter.")
55 (defvar riece-ruby-process nil)
57 (defvar riece-ruby-lock nil)
58 (defvar riece-ruby-response nil)
59 (defvar riece-ruby-data nil)
60 (defvar riece-ruby-input nil)
61 (defvar riece-ruby-status nil)
63 (defvar riece-ruby-output-handler-alist nil)
64 (defvar riece-ruby-exit-handler-alist nil)
66 (defun riece-ruby-substitute-variables (program variable value)
67 (setq program (copy-sequence program))
68 (let ((pointer program))
70 (setq pointer (memq variable program))
72 (setcar pointer value)))
75 (defun riece-ruby-escape-data (data)
77 (while (string-match "[%\r\n]+" data index)
78 (setq data (replace-match
79 (mapconcat (lambda (c) (format "%%%02X" c))
80 (match-string 0 data) "")
82 index (+ (match-end 0)
83 (* (- (match-end 0) (match-beginning 0)) 2))))
86 (defun riece-ruby-unescape-data (data)
88 (while (string-match "%\\([0-9A-F][0-9A-F]\\)" data index)
89 (setq data (replace-match
90 (read (concat "\"\\x" (match-string 1 data) "\""))
92 index (- (match-end 0) 2)))
95 (defun riece-ruby-send-eval (name program)
96 (let* ((string (riece-ruby-escape-data program))
97 (length (- (length string) 998))
100 (while (< index length)
101 (setq data (cons (substring string index (setq index (+ index 998)))
103 (setq data (cons (substring string index) data)
104 data (nreverse data))
106 (set-buffer (process-buffer riece-ruby-process))
107 (make-local-variable 'riece-ruby-lock)
108 (setq riece-ruby-lock nil)
109 (make-local-variable 'riece-ruby-response)
110 (setq riece-ruby-response nil)
111 (make-local-variable 'riece-ruby-data)
112 (setq riece-ruby-data nil)
113 (make-local-variable 'riece-ruby-input)
114 (setq riece-ruby-input nil)
115 (make-local-variable 'riece-ruby-status)
116 (setq riece-ruby-status nil))
117 (process-send-string riece-ruby-process
118 (concat "EVAL " name "\r\n"))
120 (process-send-string riece-ruby-process
121 (concat "D " (car data) "\r\n"))
122 (setq data (cdr data)))
123 (process-send-string riece-ruby-process "END\r\n")))
125 (defun riece-ruby-send-poll (name)
127 (set-buffer (process-buffer riece-ruby-process))
128 (make-local-variable 'riece-ruby-lock)
129 (setq riece-ruby-lock nil)
130 (make-local-variable 'riece-ruby-response)
131 (setq riece-ruby-response nil)
132 (make-local-variable 'riece-ruby-data)
133 (setq riece-ruby-data nil)
134 (make-local-variable 'riece-ruby-input)
135 (setq riece-ruby-input nil)
136 (make-local-variable 'riece-ruby-status)
137 (setq riece-ruby-status nil))
138 (process-send-string riece-ruby-process
139 (concat "POLL " name "\r\n")))
141 (defun riece-ruby-filter (process input)
143 (set-buffer (process-buffer process))
144 (goto-char (point-max))
146 (goto-char (process-mark process))
148 (while (looking-at ".*\r?\n")
149 (if (looking-at "OK\\( \\(.*\\)\\)?\r")
152 (setq riece-ruby-data (mapconcat #'riece-ruby-unescape-data
153 riece-ruby-input "")))
154 (setq riece-ruby-input nil
155 riece-ruby-response (list 'OK (match-string 2))
156 riece-ruby-lock nil))
157 (if (looking-at "ERR \\([0-9]+\\)\\( \\(.*\\)\\)?\r")
159 (setq riece-ruby-input nil
161 (list 'ERR (string-to-number (match-string 2))
163 riece-ruby-lock nil))
164 (if (looking-at "D \\(.*\\)\r")
165 (setq riece-ruby-input (cons (match-string 1) riece-ruby-input))
166 (if (looking-at "S program \\(.*\\)\r")
167 (setq riece-ruby-status (match-string 1))
168 (if (looking-at "# output \\(.*\\) \\(.*\\)\r")
169 (let ((entry (assoc (match-string 1)
170 riece-ruby-output-handler-alist)))
172 (funcall (cdr entry) (match-string 2))))
173 (if (looking-at "# exit \\(.*\\)\r")
174 (let ((entry (assoc (match-string 1)
175 riece-ruby-exit-handler-alist)))
177 (funcall (cdr entry))))))))))
179 (set-marker (process-mark process) (point-marker))))
181 (defun riece-ruby-sentinel (process status)
182 (kill-buffer (process-buffer process)))
184 (defun riece-ruby-execute (name program)
185 (unless (and riece-ruby-process
186 (eq (process-status riece-ruby-process) 'run))
187 (setq riece-ruby-process
188 (start-process "riece-ruby" (generate-new-buffer " *Ruby*")
193 (symbol-file 'riece-ruby-execute)))))
194 (set-process-filter riece-ruby-process #'riece-ruby-filter)
195 (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel))
197 (set-buffer (process-buffer riece-ruby-process))
198 (setq riece-ruby-lock t)
199 (riece-ruby-send-eval name program)
200 (while riece-ruby-lock
201 (accept-process-output riece-ruby-process))))
203 (defun riece-ruby-inspect (name)
205 (set-buffer (process-buffer riece-ruby-process))
206 (setq riece-ruby-lock t)
207 (riece-ruby-send-poll name)
208 (while (null riece-ruby-response)
209 (accept-process-output riece-ruby-process))
210 (list riece-ruby-response
214 (provide 'riece-ruby)
216 ;;; riece-ruby.el ends here