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-inspect "t1")
32 ;; => ((OK nil) nil "running")
34 ;; (riece-ruby-inspect "t2")
35 ;; => ((OK nil) "2" "finished")
39 (defvar riece-ruby-command "ruby"
40 "Command name for Ruby interpreter.")
42 (defvar riece-ruby-process nil)
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)
50 (defvar riece-ruby-output-handler-alist nil)
51 (defvar riece-ruby-exit-handler-alist nil)
53 (defun riece-ruby-substitute-variables (program variable value)
54 (setq program (copy-sequence program))
55 (let ((pointer program))
57 (setq pointer (memq variable program))
59 (setcar pointer value)))
62 (defun riece-ruby-escape-data (data)
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) "")
69 index (+ (match-end 0)
70 (* (- (match-end 0) (match-beginning 0)) 2))))
73 (defun riece-ruby-unescape-data (data)
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) "\""))
79 index (- (match-end 0) 2)))
82 (defun riece-ruby-send-eval (name program)
83 (let* ((string (riece-ruby-escape-data program))
84 (length (- (length string) 998))
87 (while (< index length)
88 (setq data (cons (substring string index (setq index (+ index 998)))
90 (setq data (cons (substring string index) data)
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"))
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")))
112 (defun riece-ruby-send-poll (name)
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")))
128 (defun riece-ruby-filter (process input)
130 (set-buffer (process-buffer process))
131 (goto-char (point-max))
133 (goto-char (process-mark process))
135 (while (looking-at ".*\r?\n")
136 (if (looking-at "OK\\( \\(.*\\)\\)?\r")
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")
146 (setq riece-ruby-input nil
148 (list 'ERR (string-to-number (match-string 2))
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)))
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)))
164 (funcall (cdr entry))))))))))
166 (set-marker (process-mark process) (point-marker))))
168 (defun riece-ruby-sentinel (process status)
169 (kill-buffer (process-buffer process)))
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*")
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))
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))))
190 (defun riece-ruby-inspect (name)
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
201 (provide 'riece-ruby)
203 ;;; riece-ruby.el ends here