File: riece-ruby.el

package info (click to toggle)
riece 8.0.0-1.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,068 kB
  • ctags: 1,310
  • sloc: lisp: 13,235; sh: 898; ruby: 257; makefile: 105
file content (394 lines) | stat: -rw-r--r-- 14,289 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
;;; riece-ruby.el --- interact with Ruby interpreter -*- lexical-binding: t -*-
;; Copyright (C) 1998-2005 Daiki Ueno

;; Author: Daiki Ueno <ueno@unixuser.org>
;; Created: 1998-09-28
;; Keywords: IRC, riece, Ruby

;; This file is part of Riece.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; riece-ruby.el is a library to interact with Ruby interpreter.
;; It supports concurrent execution of Ruby programs in a single
;; session.  For example:
;; 
;; (riece-ruby-execute "sleep 30"); returns immediately
;; => "rubyserv0"
;;
;; (riece-ruby-execute "1 + 1")
;; => "rubyserv1"
;;
;; (riece-ruby-execute "\"")
;; => "rubyserv2"
;;
;; (riece-ruby-inspect "rubyserv0")
;; => ((OK nil) nil (("running")))
;;
;; (riece-ruby-inspect "rubyserv1")
;; => ((OK nil) "2" (("finished")))
;;
;; (riece-ruby-inspect "rubyserv2")
;; => ((OK nil) "(eval):1: unterminated string meets end of file" (("exited")))

;;; Code:

(require 'riece-debug)

(defgroup riece-ruby nil
  "Interact with Ruby interpreter."
  :prefix "riece-"
  :group 'riece)

(defcustom riece-ruby-command "ruby"
  "Command name for Ruby interpreter."
  :type 'string
  :group 'riece-ruby)

(defcustom riece-ruby-out-file (expand-file-name "riece-ruby.out"
						 riece-directory)
  "A file which records stdout of Ruby programs."
  :type 'string
  :group 'riece-ruby)

(defcustom riece-ruby-err-file (expand-file-name "riece-ruby.err"
						 riece-directory)
  "A file which records stderr of Ruby programs."
  :type 'string
  :group 'riece-ruby)

(defcustom riece-ruby-log-file (expand-file-name "riece-ruby.log"
						 riece-directory)
  "A file used to logging."
  :type 'string
  :group 'riece-ruby)

(defvar riece-ruby-server-program "server.rb"
  "The server program file.  If the filename is not absolute, it is
assumed that the file is in the same directory of this file.")

(defvar riece-ruby-server-program-arguments (list "-o" riece-ruby-out-file
						  "-e" riece-ruby-err-file
						  "-l" riece-ruby-log-file)
  "Command line arguments passed to `riece-ruby-server-program'.")

(defvar riece-ruby-process nil
  "Process object of Ruby interpreter.")

(defvar riece-ruby-lock nil
  "Lock for waiting server response.
Local to the process buffer.")
(defvar riece-ruby-response nil
  "The server response.
Local to the process buffer.")
(defvar riece-ruby-data nil
  "Data from server.
Local to the process buffer.")
(defvar riece-ruby-escaped-data nil
  "Escaped data from server.  This variable is cleared every time
server response arrives.
Local to the process buffer.")
(defvar riece-ruby-status-alist nil
  "Status from server.
Local to the process buffer.")

(defvar riece-ruby-output-queue-alist nil
  "An alist mapping from program name to output data.")
(defvar riece-ruby-output-handler-alist nil
  "An alist mapping from program name to output handler.
Output handlers are called every time \"# output\" line arrives.
Use `riece-ruby-set-output-handler' to set this variable.")
(defvar riece-ruby-exit-handler-alist nil
  "An alist mapping from program name to exit handler.
Exit handlers are called once when \"# exit\" line arrives.
Use `riece-ruby-set-exit-handler' to set this variable.")
(defvar riece-ruby-property-alist nil
  "An alist mapping from program name to the property list.
Use `riece-ruby-set-property' to set this variable.")

(defun riece-ruby-escape-data (data)
  (let ((index 0))
    (while (string-match "[%\r\n]+" data index)
      (setq data (replace-match
		  (mapconcat (lambda (c) (format "%%%02X" c))
			     (match-string 0 data) "")
		  nil nil data)
	    index (+ (match-end 0)
		     (* (- (match-end 0) (match-beginning 0)) 2))))
    data))

(defun riece-ruby-unescape-data (data)
  (let ((index 0))
    (while (string-match "%\\([0-9A-F][0-9A-F]\\)" data index)
      (setq data (replace-match
		  (read (concat "\"\\x" (match-string 1 data) "\""))
		  nil nil data)
	    index (- (match-end 0) 2)))
    data))

(defun riece-ruby-reset-process-buffer ()
  (with-current-buffer (process-buffer riece-ruby-process)
    (buffer-disable-undo)
    (make-local-variable 'riece-ruby-response)
    (setq riece-ruby-response nil)
    (make-local-variable 'riece-ruby-data)
    (setq riece-ruby-data nil)
    (make-local-variable 'riece-ruby-escaped-data)
    (setq riece-ruby-escaped-data nil)
    (make-local-variable 'riece-ruby-status-alist)
    (setq riece-ruby-status-alist nil)))

(defun riece-ruby-send-eval (program)
  (let* ((string (riece-ruby-escape-data program))
	 (length (- (length string) 998))
	 (index 0)
	 data)
    (while (< index length)
      (setq data (cons (substring string index (setq index (+ index 998)))
		       data)))
    (setq data (cons (substring string index) data)
	  data (nreverse data))
    (process-send-string riece-ruby-process "EVAL\r\n")
    (while data
      (process-send-string riece-ruby-process
			   (concat "D " (car data) "\r\n"))
      (setq data (cdr data)))
    (process-send-string riece-ruby-process "END\r\n")))

(defun riece-ruby-send-poll (name)
  (process-send-string riece-ruby-process
		       (concat "POLL " name "\r\n")))

(defun riece-ruby-send-exit (name)
  (process-send-string riece-ruby-process
		       (concat "EXIT " name "\r\n")))

(defun riece-ruby-filter (process input)
  (with-current-buffer (process-buffer process)
    (goto-char (point-max))
    (insert input)
    (goto-char (point-min))
    (beginning-of-line)
    (while (looking-at ".*\r\n")
      (if (looking-at "OK\\( \\(.*\\)\\)?\r")
	  (progn
	    (if riece-ruby-escaped-data
		(setq riece-ruby-data (mapconcat #'riece-ruby-unescape-data
						 riece-ruby-escaped-data "")))
	    (setq riece-ruby-escaped-data nil
		  riece-ruby-response (list 'OK (match-string 2))
		  riece-ruby-lock nil))
	(if (looking-at "ERR \\([0-9]+\\)\\( \\(.*\\)\\)?\r")
	    (progn
	      (setq riece-ruby-escaped-data nil
		    riece-ruby-response
		    (list 'ERR (string-to-number (match-string 1))
			  (match-string 3))
		    riece-ruby-lock nil))
	  (if (looking-at "D \\(.*\\)\r")
	      (setq riece-ruby-escaped-data (cons (match-string 1)
						  riece-ruby-escaped-data))
	    (if (looking-at "S \\([^ ]*\\) \\(.*\\)\r")
		(progn
		  (setq riece-ruby-status-alist (cons (cons (match-string 1)
							    (match-string 2))
						      riece-ruby-status-alist))
		  (if (member (car (car riece-ruby-status-alist))
			      '("finished" "exited"))
		      (riece-ruby-run-exit-handler
		       (cdr (car riece-ruby-status-alist)))))
	      (if (looking-at "# output \\([^ ]*\\) \\(.*\\)\r")
		  (riece-ruby-run-output-handler (match-string 1)
						 (match-string 2)
						 (current-time))
		(if (looking-at "# exit \\(.*\\)\r")
		    (riece-ruby-run-exit-handler (match-string 1))))))))
      (forward-line))
    (delete-region (point-min) (point))))

(defun riece-ruby-run-exit-handler (name)
  (let ((entry (assoc name riece-ruby-exit-handler-alist)))
    (when entry
      (setq riece-ruby-exit-handler-alist
	    (delq entry riece-ruby-exit-handler-alist))
      (riece-funcall-ignore-errors (if (symbolp (cdr entry))
				       (symbol-name (cdr entry))
				     (format "%s-exit-handler" name))
				   (cdr entry) (car entry))
      (riece-ruby-clear name))))

(defun riece-ruby-run-output-handler (name output time)
  (let ((handler-entry (assoc name riece-ruby-output-handler-alist))
	(entry (assoc name riece-ruby-output-queue-alist)))
    (if handler-entry
	(riece-funcall-ignore-errors (if (symbolp (cdr handler-entry))
					 (symbol-name (cdr handler-entry))
				       (format "%s-output-handler" name))
				     (cdr handler-entry) name output time)
      (if entry
	  (setcdr entry (cons (cons output time) (cdr entry)))
	(setq riece-ruby-output-queue-alist
	      (cons (list name (cons output time))
		    riece-ruby-output-queue-alist))))))

(defun riece-ruby-sentinel (process _status)
  (kill-buffer (process-buffer process)))

(defun riece-ruby-execute (program)
  "Schedule an execution of a Ruby PROGRAM.
Return a string name assigned by the server."
  (unless (and riece-ruby-process
	       (eq (process-status riece-ruby-process) 'run))
    (let (selective-display
	  (coding-system-for-write 'binary)
	  (coding-system-for-read 'binary))
      (setq riece-ruby-process
	    (apply #'start-process "riece-ruby" (generate-new-buffer " *Ruby*")
		   riece-ruby-command
		   (expand-file-name riece-ruby-server-program
				     riece-data-directory)
		   riece-ruby-server-program-arguments))
      (riece-set-process-query-on-exit-flag riece-ruby-process nil)
      (set-process-filter riece-ruby-process #'riece-ruby-filter)
      (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel)))
  (with-current-buffer (process-buffer riece-ruby-process)
    (riece-ruby-reset-process-buffer)
    (make-local-variable 'riece-ruby-lock)
    (setq riece-ruby-lock t)
    (riece-ruby-send-eval program)
    (while riece-ruby-lock
      (accept-process-output riece-ruby-process))
    (if (eq (car riece-ruby-response) 'ERR)
	(error "Couldn't execute: %S" (cdr riece-ruby-response)))
    (cdr (assoc "name" riece-ruby-status-alist))))

(defun riece-ruby-inspect (name)
  "Inspect a result of program execution distinguished by NAME.
Return a three element list.
The car is protocol response line which looks like:
  \(ERR 103 \"Not implemented\").
The cadr is data from the server, that is, the result of the program.
The caddr is status from the server."
  (with-current-buffer (process-buffer riece-ruby-process)
    (riece-ruby-reset-process-buffer)
    (make-local-variable 'riece-ruby-lock)
    (setq riece-ruby-lock t)
    (riece-ruby-send-poll name)
    (while riece-ruby-lock
      (accept-process-output riece-ruby-process))
    (list riece-ruby-response
	  riece-ruby-data
	  riece-ruby-status-alist)))

(defun riece-ruby-clear (name)
  "Clear a result of program execution distinguished by NAME.
Note that riece-ruby-clear is automatically called iff an exit-handler
is specified.  Otherwise, it should be called explicitly."
  (with-current-buffer (process-buffer riece-ruby-process)
    (riece-ruby-reset-process-buffer)
    (make-local-variable 'riece-ruby-lock)
    (setq riece-ruby-lock t)
    (riece-ruby-send-exit name)
    (while riece-ruby-lock
      (accept-process-output riece-ruby-process)))
  (let ((entry (assoc name riece-ruby-property-alist)))
    (if entry
	(delq entry riece-ruby-property-alist))))

(defun riece-ruby-set-exit-handler (name handler)
  "Set an exit-handler HANDLER for the program distinguished by NAME.
An exit-handler is called when the program is finished or exited abnormally.
An exit-handler is called with an argument same as NAME.
Note that riece-ruby-clear is automatically called iff an exit-handler
is specified.  Otherwise, it should be called explicitly."
  (let ((entry (assoc name riece-ruby-exit-handler-alist)))
    (if handler
	(progn
	  (if entry
	      (setcdr entry handler)
	    (setq riece-ruby-exit-handler-alist
		  (cons (cons name handler)
			riece-ruby-exit-handler-alist)))
	  ;;check if the program already exited
	  (riece-ruby-inspect name))
      (if entry
	  (setq riece-ruby-exit-handler-alist
		(delq entry riece-ruby-exit-handler-alist))))))

(defun riece-ruby-set-output-handler (name handler)
  "Set an output-handler HANDLER for the program distinguished by NAME.
An output-handler is called when the program sends any output by using
`output' method in the Ruby program.
An output-handler is called with three argument.  The first argument
is the same as NAME.  The second argument is the output string.  The
third argument is the timestamp of the output event."
  (let ((entry (assoc name riece-ruby-output-handler-alist))
	queue-entry pointer)
    (if handler
	(progn
	  (when (setq queue-entry (assoc name riece-ruby-output-queue-alist))
	    (setq pointer (nreverse (cdr queue-entry))
		  riece-ruby-output-queue-alist
		  (delq queue-entry riece-ruby-output-queue-alist))
	    (while pointer
	      (riece-funcall-ignore-errors (if (symbolp handler)
					       (symbol-name handler)
					     (format "%s-output-handler" name))
					   handler name (car (car pointer))
					   (cdr (car pointer)))
	      (setq pointer (cdr pointer))))
	  (if entry
	      (setcdr entry handler)
	    (setq riece-ruby-output-handler-alist
		  (cons (cons name handler)
			riece-ruby-output-handler-alist))))
      (if entry
	  (setq riece-ruby-output-handler-alist
		(delq entry riece-ruby-output-handler-alist))))))

(defun riece-ruby-set-property (name property value)
  "Set given PROPERTY/VALUE pair to the program distinguished by NAME."
  (let ((entry (assoc name riece-ruby-property-alist))
	property-entry)
    (unless entry
      (setq entry (list name)
	    riece-ruby-property-alist (cons entry riece-ruby-property-alist)))
    (if (setq property-entry (assoc property (cdr entry)))
	(setcdr property-entry value)
      (setcdr entry (cons (cons property value) (cdr entry))))))

(defun riece-ruby-property (name property)
  "Return the value of PROPERTY set to the program distinguished by NAME."
  (cdr (assoc property (cdr (assoc name riece-ruby-property-alist)))))

(defun riece-ruby-substitute-variables (program alist)
  "Substitute symbols in PROGRAM by looking up ALIST.
Return a string concatenating elements in PROGRAM."
  (setq program (copy-sequence program))
  (while alist
    (let ((pointer program))
      (while pointer
	(setq pointer (memq (car (car alist)) program))
	(if pointer
	    (setcar pointer (cdr (car alist))))))
    (setq alist (cdr alist)))
  (apply #'concat program))

(provide 'riece-ruby)

;;; riece-ruby.el ends here