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 (defvar riece-ruby-command "ruby"
28 "Command name for Ruby interpreter.")
30 (defvar riece-ruby-process nil)
32 (defvar riece-ruby-lock nil)
33 (defvar riece-ruby-response nil)
34 (defvar riece-ruby-data nil)
35 (defvar riece-ruby-input nil)
36 (defvar riece-ruby-status nil)
38 (defvar riece-ruby-output-handler-alist nil)
39 (defvar riece-ruby-exit-handler-alist nil)
41 (defun riece-ruby-substitute-variables (program variable value)
42 (setq program (copy-sequence program))
43 (let ((pointer program))
45 (setq pointer (memq variable program))
47 (setcar pointer value)))
50 (defun riece-ruby-escape-data (data)
52 (while (string-match "[%\r\n]+" data index)
53 (setq data (replace-match
54 (mapconcat (lambda (c) (format "%%%02X" c))
55 (match-string 0 data) "")
57 index (+ (match-end 0)
58 (* (- (match-end 0) (match-beginning 0)) 2))))
61 (defun riece-ruby-unescape-data (data)
63 (while (string-match "%\\([0-9A-F][0-9A-F]\\)" data index)
64 (setq data (replace-match
65 (read (concat "\"\\x" (match-string 1 data) "\""))
67 index (- (match-end 0) 2)))
70 (defun riece-ruby-send-eval (name program)
71 (let* ((string (riece-ruby-escape-data program))
72 (length (- (length string) 998))
75 (while (< index length)
76 (setq data (cons (substring string index (setq index (+ index 998)))
78 (setq data (cons (substring string index) data)
81 (set-buffer (process-buffer riece-ruby-process))
82 (make-local-variable 'riece-ruby-lock)
83 (setq riece-ruby-lock nil)
84 (make-local-variable 'riece-ruby-response)
85 (setq riece-ruby-response nil)
86 (make-local-variable 'riece-ruby-data)
87 (setq riece-ruby-data nil)
88 (make-local-variable 'riece-ruby-input)
89 (setq riece-ruby-input nil)
90 (make-local-variable 'riece-ruby-status)
91 (setq riece-ruby-status nil))
92 (process-send-string riece-ruby-process
93 (concat "EVAL " name "\r\n"))
95 (process-send-string riece-ruby-process
96 (concat "D " (car data) "\r\n"))
97 (setq data (cdr data)))
98 (process-send-string riece-ruby-process "END\r\n")))
100 (defun riece-ruby-send-poll (name)
102 (set-buffer (process-buffer riece-ruby-process))
103 (make-local-variable 'riece-ruby-lock)
104 (setq riece-ruby-lock nil)
105 (make-local-variable 'riece-ruby-response)
106 (setq riece-ruby-response nil)
107 (make-local-variable 'riece-ruby-data)
108 (setq riece-ruby-data nil)
109 (make-local-variable 'riece-ruby-input)
110 (setq riece-ruby-input nil)
111 (make-local-variable 'riece-ruby-status)
112 (setq riece-ruby-status nil))
113 (process-send-string riece-ruby-process
114 (concat "POLL " name "\r\n")))
116 (defun riece-ruby-filter (process input)
118 (set-buffer (process-buffer process))
119 (goto-char (point-max))
121 (goto-char (process-mark process))
123 (while (looking-at ".*\r?\n")
124 (if (looking-at "OK\\( \\(.*\\)\\)?\r")
127 (setq riece-ruby-data (mapconcat #'riece-ruby-unescape-data
128 riece-ruby-input "")))
129 (setq riece-ruby-input nil
130 riece-ruby-response (list 'OK (match-string 2))
131 riece-ruby-lock nil))
132 (if (looking-at "ERR \\([0-9]+\\)\\( \\(.*\\)\\)?\r")
134 (setq riece-ruby-input nil
136 (list 'ERR (string-to-number (match-string 2))
138 riece-ruby-lock nil))
139 (if (looking-at "D \\(.*\\)\r")
140 (setq riece-ruby-input (cons (match-string 1) riece-ruby-input))
141 (if (looking-at "S program \\(.*\\)\r")
142 (setq riece-ruby-status (match-string 1))
143 (if (looking-at "# output \\(.*\\) \\(.*\\)\r")
144 (let ((entry (assoc (match-string 1)
145 riece-ruby-output-handler-alist)))
147 (funcall (cdr entry) (match-string 2))))
148 (if (looking-at "# exit \\(.*\\)\r")
149 (let ((entry (assoc (match-string 1)
150 riece-ruby-exit-handler-alist)))
152 (funcall (cdr entry))))))))))
154 (set-marker (process-mark process) (point-marker))))
156 (defun riece-ruby-sentinel (process status)
157 (kill-buffer (process-buffer process)))
159 (defun riece-ruby-execute (name program)
160 (unless (and riece-ruby-process
161 (eq (process-status riece-ruby-process) 'run))
162 (setq riece-ruby-process
163 (start-process "riece-ruby" (generate-new-buffer " *Ruby*")
168 (symbol-file 'riece-ruby-execute)))))
169 (set-process-filter riece-ruby-process #'riece-ruby-filter)
170 (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel))
172 (set-buffer (process-buffer riece-ruby-process))
173 (setq riece-ruby-lock t)
174 (riece-ruby-send-eval name program)
175 (while riece-ruby-lock
176 (accept-process-output riece-ruby-process))))
178 (defun riece-ruby-inspect (name)
180 (set-buffer (process-buffer riece-ruby-process))
181 (setq riece-ruby-lock t)
182 (riece-ruby-send-poll name)
183 (while (null riece-ruby-response)
184 (accept-process-output riece-ruby-process))
185 (list riece-ruby-response
189 (provide 'riece-ruby)
191 ;;; riece-ruby.el ends here