Use singleton method for preparing empty environment.
[riece] / lisp / riece-ruby.el
index 46d6a74..8a3c66b 100644 (file)
@@ -3,7 +3,7 @@
 
 ;; Author: Daiki Ueno <ueno@unixuser.org>
 ;; Created: 1998-09-28
-;; Keywords: IRC, riece
+;; Keywords: IRC, riece, Ruby
 
 ;; This file is part of Riece.
 
@@ -24,7 +24,7 @@
 
 ;;; Commentary:
 
-;; riece-ruby.el is a library to interact with the Ruby interpreter.
+;; riece-ruby.el is a library to interact with Ruby interpreter.
 ;; It supports concurrent execution of Ruby programs in a single
 ;; session.  For example:
 ;; 
 ;; => "rubyserv2"
 ;;
 ;; (riece-ruby-inspect "rubyserv0")
-;; => ((OK nil) nil "running")
+;; => ((OK nil) nil (("running")))
 ;;
 ;; (riece-ruby-inspect "rubyserv1")
-;; => ((OK nil) "2" "finished")
+;; => ((OK nil) "2" (("finished")))
 ;;
 ;; (riece-ruby-inspect "rubyserv2")
-;; => ((OK nil) "(eval):1: unterminated string meets end of file" "exited")
+;; => ((OK nil) "(eval):1: unterminated string meets end of file" (("exited")))
 
 ;;; Code:
 
+(require 'riece-debug)
+
 (defgroup riece-ruby nil
-  "Interact with the Ruby interpreter."
+  "Interact with Ruby interpreter."
   :group 'riece)
 
 (defcustom riece-ruby-command "ruby"
   :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 the Ruby interpreter.")
+  "Process object of Ruby interpreter.")
 
 (defvar riece-ruby-lock nil
   "Lock for waiting server response.
@@ -93,17 +118,6 @@ Use `riece-ruby-set-exit-handler' to set this variable.")
   "An alist mapping from program name to the property list.
 Use `riece-ruby-set-property' to set this variable.")
 
-(defun riece-ruby-substitute-variables (program alist)
-  (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))
-
 (defun riece-ruby-escape-data (data)
   (let ((index 0))
     (while (string-match "[%\r\n]+" data index)
@@ -167,7 +181,7 @@ Use `riece-ruby-set-property' to set this variable.")
     (set-buffer (process-buffer process))
     (goto-char (point-max))
     (insert input)
-    (goto-char (process-mark process))
+    (goto-char (point-min))
     (beginning-of-line)
     (while (looking-at ".*\r\n")
       (if (looking-at "OK\\( \\(.*\\)\\)?\r")
@@ -201,11 +215,12 @@ Use `riece-ruby-set-property' to set this variable.")
                  (let ((entry (assoc (match-string 1)
                                      riece-ruby-output-handler-alist)))
                    (if entry
-                       (funcall (cdr entry) (car entry) (match-string 2))))
+                       (riece-debug-with-backtrace
+                         (funcall (cdr entry) (car entry) (match-string 2)))))
                (if (looking-at "# exit \\(.*\\)\r")
                    (riece-ruby-run-exit-handler (match-string 1))))))))
       (forward-line))
-    (set-marker (process-mark process) (point-marker))))
+    (delete-region (point-min) (point))))
 
 (defun riece-ruby-run-exit-handler (name)
   (let ((entry (assoc name riece-ruby-exit-handler-alist)))
@@ -213,28 +228,32 @@ Use `riece-ruby-set-property' to set this variable.")
        (progn
          (setq riece-ruby-exit-handler-alist
                (delq entry riece-ruby-exit-handler-alist))
-         (funcall (cdr entry) (car entry))
+         (riece-debug-with-backtrace
+           (funcall (cdr entry) (car entry)))
          (riece-ruby-clear name)))))
 
 (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
-           (start-process "riece-ruby" (generate-new-buffer " *Ruby*")
-                          riece-ruby-command
-                          (if (file-name-absolute-p riece-ruby-server-program)
-                              riece-ruby-server-program
-                            (expand-file-name
-                             riece-ruby-server-program
-                             (file-name-directory
-                              (locate-library
-                               (symbol-file 'riece-ruby-execute)))))))
+           (apply #'start-process "riece-ruby" (generate-new-buffer " *Ruby*")
+                  riece-ruby-command
+                  (if (file-name-absolute-p riece-ruby-server-program)
+                      riece-ruby-server-program
+                    (expand-file-name
+                     riece-ruby-server-program
+                     (file-name-directory
+                      (locate-library
+                       (symbol-file 'riece-ruby-execute)))))
+                  riece-ruby-server-program-arguments))
       (process-kill-without-query riece-ruby-process)
       (set-process-filter riece-ruby-process #'riece-ruby-filter)
       (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel)))
@@ -251,6 +270,12 @@ Use `riece-ruby-set-property' to set this variable.")
     (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."
   (save-excursion
     (set-buffer (process-buffer riece-ruby-process))
     (riece-ruby-reset-process-buffer)
@@ -264,6 +289,9 @@ Use `riece-ruby-set-property' to set this variable.")
          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."
   (save-excursion
     (set-buffer (process-buffer riece-ruby-process))
     (riece-ruby-reset-process-buffer)
@@ -277,6 +305,11 @@ Use `riece-ruby-set-property' to set this variable.")
        (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
@@ -292,6 +325,11 @@ Use `riece-ruby-set-property' to set this variable.")
                (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 two argument.  The first argument is
+the same as NAME.  The second argument is output string."
   (let ((entry (assoc name riece-ruby-output-handler-alist)))
     (if handler
        (progn
@@ -305,6 +343,7 @@ Use `riece-ruby-set-property' to set this variable.")
                (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
@@ -315,8 +354,22 @@ Use `riece-ruby-set-property' to set this variable.")
       (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