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: unterminated string meets end of file" "exited")
51 (defvar riece-ruby-command "ruby"
52 "Command name for Ruby interpreter.")
54 (defvar riece-ruby-process nil)
56 (defvar riece-ruby-lock nil)
57 (defvar riece-ruby-response nil)
58 (defvar riece-ruby-data nil)
59 (defvar riece-ruby-input nil)
60 (defvar riece-ruby-status nil)
62 (defvar riece-ruby-output-handler-alist nil)
63 (defvar riece-ruby-exit-handler-alist nil)
65 (defun riece-ruby-substitute-variables (program variable value)
66 (setq program (copy-sequence program))
67 (let ((pointer program))
69 (setq pointer (memq variable program))
71 (setcar pointer value)))
74 (defun riece-ruby-escape-data (data)
76 (while (string-match "[%\r\n]+" data index)
77 (setq data (replace-match
78 (mapconcat (lambda (c) (format "%%%02X" c))
79 (match-string 0 data) "")
81 index (+ (match-end 0)
82 (* (- (match-end 0) (match-beginning 0)) 2))))
85 (defun riece-ruby-unescape-data (data)
87 (while (string-match "%\\([0-9A-F][0-9A-F]\\)" data index)
88 (setq data (replace-match
89 (read (concat "\"\\x" (match-string 1 data) "\""))
91 index (- (match-end 0) 2)))
94 (defun riece-ruby-send-eval (name program)
95 (let* ((string (riece-ruby-escape-data program))
96 (length (- (length string) 998))
99 (while (< index length)
100 (setq data (cons (substring string index (setq index (+ index 998)))
102 (setq data (cons (substring string index) data)
103 data (nreverse data))
105 (set-buffer (process-buffer riece-ruby-process))
106 (make-local-variable 'riece-ruby-lock)
107 (setq riece-ruby-lock nil)
108 (make-local-variable 'riece-ruby-response)
109 (setq riece-ruby-response nil)
110 (make-local-variable 'riece-ruby-data)
111 (setq riece-ruby-data nil)
112 (make-local-variable 'riece-ruby-input)
113 (setq riece-ruby-input nil)
114 (make-local-variable 'riece-ruby-status)
115 (setq riece-ruby-status nil))
116 (process-send-string riece-ruby-process
117 (concat "EVAL " name "\r\n"))
119 (process-send-string riece-ruby-process
120 (concat "D " (car data) "\r\n"))
121 (setq data (cdr data)))
122 (process-send-string riece-ruby-process "END\r\n")))
124 (defun riece-ruby-send-poll (name)
126 (set-buffer (process-buffer riece-ruby-process))
127 (make-local-variable 'riece-ruby-lock)
128 (setq riece-ruby-lock nil)
129 (make-local-variable 'riece-ruby-response)
130 (setq riece-ruby-response nil)
131 (make-local-variable 'riece-ruby-data)
132 (setq riece-ruby-data nil)
133 (make-local-variable 'riece-ruby-input)
134 (setq riece-ruby-input nil)
135 (make-local-variable 'riece-ruby-status)
136 (setq riece-ruby-status nil))
137 (process-send-string riece-ruby-process
138 (concat "POLL " name "\r\n")))
140 (defun riece-ruby-filter (process input)
142 (set-buffer (process-buffer process))
143 (goto-char (point-max))
145 (goto-char (process-mark process))
147 (while (looking-at ".*\r?\n")
148 (if (looking-at "OK\\( \\(.*\\)\\)?\r")
151 (setq riece-ruby-data (mapconcat #'riece-ruby-unescape-data
152 riece-ruby-input "")))
153 (setq riece-ruby-input nil
154 riece-ruby-response (list 'OK (match-string 2))
155 riece-ruby-lock nil))
156 (if (looking-at "ERR \\([0-9]+\\)\\( \\(.*\\)\\)?\r")
158 (setq riece-ruby-input nil
160 (list 'ERR (string-to-number (match-string 2))
162 riece-ruby-lock nil))
163 (if (looking-at "D \\(.*\\)\r")
164 (setq riece-ruby-input (cons (match-string 1) riece-ruby-input))
165 (if (looking-at "S program \\(.*\\)\r")
166 (setq riece-ruby-status (match-string 1))
167 (if (looking-at "# output \\(.*\\) \\(.*\\)\r")
168 (let ((entry (assoc (match-string 1)
169 riece-ruby-output-handler-alist)))
171 (funcall (cdr entry) (match-string 2))))
172 (if (looking-at "# exit \\(.*\\)\r")
173 (let ((entry (assoc (match-string 1)
174 riece-ruby-exit-handler-alist)))
176 (funcall (cdr entry))))))))))
178 (set-marker (process-mark process) (point-marker))))
180 (defun riece-ruby-sentinel (process status)
181 (kill-buffer (process-buffer process)))
183 (defun riece-ruby-execute (name program)
184 (unless (and riece-ruby-process
185 (eq (process-status riece-ruby-process) 'run))
186 (setq riece-ruby-process
187 (start-process "riece-ruby" (generate-new-buffer " *Ruby*")
192 (symbol-file 'riece-ruby-execute)))))
193 (set-process-filter riece-ruby-process #'riece-ruby-filter)
194 (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel))
196 (set-buffer (process-buffer riece-ruby-process))
197 (setq riece-ruby-lock t)
198 (riece-ruby-send-eval name program)
199 (while riece-ruby-lock
200 (accept-process-output riece-ruby-process))))
202 (defun riece-ruby-inspect (name)
204 (set-buffer (process-buffer riece-ruby-process))
205 (setq riece-ruby-lock t)
206 (riece-ruby-send-poll name)
207 (while (null riece-ruby-response)
208 (accept-process-output riece-ruby-process))
209 (list riece-ruby-response
213 (provide 'riece-ruby)
215 ;;; riece-ruby.el ends here