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-execute "t1" "sleep 30")
29 ;; (riece-ruby-execute "t2" "1 + 1")
31 ;; (riece-ruby-execute "t3" "\"")
33 ;; (riece-ruby-inspect "t1")
34 ;; => ((OK nil) nil "running")
36 ;; (riece-ruby-inspect "t2")
37 ;; => ((OK nil) "2" "finished")
39 ;; (riece-ruby-inspect "t3")
40 ;; => ((OK nil) "(eval):1:in `dispatch_eval': compile error
41 ;; (eval):1: unterminated string meets end of file" "exited")
46 (defvar riece-ruby-command "ruby"
47 "Command name for Ruby interpreter.")
49 (defvar riece-ruby-process nil)
51 (defvar riece-ruby-lock nil)
52 (defvar riece-ruby-response nil)
53 (defvar riece-ruby-data nil)
54 (defvar riece-ruby-input nil)
55 (defvar riece-ruby-status nil)
57 (defvar riece-ruby-output-handler-alist nil)
58 (defvar riece-ruby-exit-handler-alist nil)
60 (defun riece-ruby-substitute-variables (program variable value)
61 (setq program (copy-sequence program))
62 (let ((pointer program))
64 (setq pointer (memq variable program))
66 (setcar pointer value)))
69 (defun riece-ruby-escape-data (data)
71 (while (string-match "[%\r\n]+" data index)
72 (setq data (replace-match
73 (mapconcat (lambda (c) (format "%%%02X" c))
74 (match-string 0 data) "")
76 index (+ (match-end 0)
77 (* (- (match-end 0) (match-beginning 0)) 2))))
80 (defun riece-ruby-unescape-data (data)
82 (while (string-match "%\\([0-9A-F][0-9A-F]\\)" data index)
83 (setq data (replace-match
84 (read (concat "\"\\x" (match-string 1 data) "\""))
86 index (- (match-end 0) 2)))
89 (defun riece-ruby-send-eval (name program)
90 (let* ((string (riece-ruby-escape-data program))
91 (length (- (length string) 998))
94 (while (< index length)
95 (setq data (cons (substring string index (setq index (+ index 998)))
97 (setq data (cons (substring string index) data)
100 (set-buffer (process-buffer riece-ruby-process))
101 (make-local-variable 'riece-ruby-lock)
102 (setq riece-ruby-lock nil)
103 (make-local-variable 'riece-ruby-response)
104 (setq riece-ruby-response nil)
105 (make-local-variable 'riece-ruby-data)
106 (setq riece-ruby-data nil)
107 (make-local-variable 'riece-ruby-input)
108 (setq riece-ruby-input nil)
109 (make-local-variable 'riece-ruby-status)
110 (setq riece-ruby-status nil))
111 (process-send-string riece-ruby-process
112 (concat "EVAL " name "\r\n"))
114 (process-send-string riece-ruby-process
115 (concat "D " (car data) "\r\n"))
116 (setq data (cdr data)))
117 (process-send-string riece-ruby-process "END\r\n")))
119 (defun riece-ruby-send-poll (name)
121 (set-buffer (process-buffer riece-ruby-process))
122 (make-local-variable 'riece-ruby-lock)
123 (setq riece-ruby-lock nil)
124 (make-local-variable 'riece-ruby-response)
125 (setq riece-ruby-response nil)
126 (make-local-variable 'riece-ruby-data)
127 (setq riece-ruby-data nil)
128 (make-local-variable 'riece-ruby-input)
129 (setq riece-ruby-input nil)
130 (make-local-variable 'riece-ruby-status)
131 (setq riece-ruby-status nil))
132 (process-send-string riece-ruby-process
133 (concat "POLL " name "\r\n")))
135 (defun riece-ruby-filter (process input)
137 (set-buffer (process-buffer process))
138 (goto-char (point-max))
140 (goto-char (process-mark process))
142 (while (looking-at ".*\r?\n")
143 (if (looking-at "OK\\( \\(.*\\)\\)?\r")
146 (setq riece-ruby-data (mapconcat #'riece-ruby-unescape-data
147 riece-ruby-input "")))
148 (setq riece-ruby-input nil
149 riece-ruby-response (list 'OK (match-string 2))
150 riece-ruby-lock nil))
151 (if (looking-at "ERR \\([0-9]+\\)\\( \\(.*\\)\\)?\r")
153 (setq riece-ruby-input nil
155 (list 'ERR (string-to-number (match-string 2))
157 riece-ruby-lock nil))
158 (if (looking-at "D \\(.*\\)\r")
159 (setq riece-ruby-input (cons (match-string 1) riece-ruby-input))
160 (if (looking-at "S program \\(.*\\)\r")
161 (setq riece-ruby-status (match-string 1))
162 (if (looking-at "# output \\(.*\\) \\(.*\\)\r")
163 (let ((entry (assoc (match-string 1)
164 riece-ruby-output-handler-alist)))
166 (funcall (cdr entry) (match-string 2))))
167 (if (looking-at "# exit \\(.*\\)\r")
168 (let ((entry (assoc (match-string 1)
169 riece-ruby-exit-handler-alist)))
171 (funcall (cdr entry))))))))))
173 (set-marker (process-mark process) (point-marker))))
175 (defun riece-ruby-sentinel (process status)
176 (kill-buffer (process-buffer process)))
178 (defun riece-ruby-execute (name program)
179 (unless (and riece-ruby-process
180 (eq (process-status riece-ruby-process) 'run))
181 (setq riece-ruby-process
182 (start-process "riece-ruby" (generate-new-buffer " *Ruby*")
187 (symbol-file 'riece-ruby-execute)))))
188 (set-process-filter riece-ruby-process #'riece-ruby-filter)
189 (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel))
191 (set-buffer (process-buffer riece-ruby-process))
192 (setq riece-ruby-lock t)
193 (riece-ruby-send-eval name program)
194 (while riece-ruby-lock
195 (accept-process-output riece-ruby-process))))
197 (defun riece-ruby-inspect (name)
199 (set-buffer (process-buffer riece-ruby-process))
200 (setq riece-ruby-lock t)
201 (riece-ruby-send-poll name)
202 (while (null riece-ruby-response)
203 (accept-process-output riece-ruby-process))
204 (list riece-ruby-response
208 (provide 'riece-ruby)
210 ;;; riece-ruby.el ends here