Fixed.
[riece] / lisp / riece-ruby.el
index da209ca..2792b22 100644 (file)
 
 ;;; Code:
 
-(defvar riece-ruby-command "ruby"
-  "Command name for Ruby interpreter.")
+(defgroup riece-ruby nil
+  "Interact with the Ruby interpreter."
+  :group 'riece)
 
-(defvar riece-ruby-server-program "server.rb")
+(defcustom riece-ruby-command "ruby"
+  "Command name for Ruby interpreter."
+  :type 'string
+  :group 'riece-ruby)
 
-(defvar riece-ruby-process nil)
+(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-lock nil)
-(defvar riece-ruby-response nil)
-(defvar riece-ruby-data nil)
-(defvar riece-ruby-escaped-data nil)
-(defvar riece-ruby-status-alist nil)
+(defvar riece-ruby-process nil
+  "Process object of the Ruby interpreter.")
 
-(defvar riece-ruby-output-handler-alist nil)
-(defvar riece-ruby-exit-handler-alist nil)
+(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.")
 
-(defun riece-ruby-substitute-variables (program variable value)
+(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.")
+
+(defvar riece-ruby-enabled nil)
+
+(defconst riece-ruby-description
+  "Evaluate an input string as Ruby program.")
+
+(defun riece-ruby-substitute-variables (program alist)
   (setq program (copy-sequence program))
-  (let ((pointer program))
-    (while pointer
-      (setq pointer (memq variable program))
-      (if pointer
-         (setcar pointer value)))
-    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))
          (if (looking-at "D \\(.*\\)\r")
              (setq riece-ruby-escaped-data (cons (match-string 1)
                                                  riece-ruby-escaped-data))
-           (if (looking-at "S \\(.*\\) \\(.*\\)\r")
+           (if (looking-at "S \\([^ ]*\\) \\(.*\\)\r")
                (progn
                  (setq riece-ruby-status-alist (cons (cons (match-string 1)
                                                            (match-string 2))
                              '("finished" "exited"))
                      (riece-ruby-run-exit-handler
                       (cdr (car riece-ruby-status-alist)))))
-             (if (looking-at "# output \\(.*\\) \\(.*\\)\r")
+             (if (looking-at "# output \\([^ ]*\\) \\(.*\\)\r")
                  (let ((entry (assoc (match-string 1)
                                      riece-ruby-output-handler-alist)))
                    (if entry
-                       (funcall (cdr entry) (match-string 2))))
+                       (funcall (cdr entry) (car entry) (match-string 2))))
                (if (looking-at "# exit \\(.*\\)\r")
                    (riece-ruby-run-exit-handler (match-string 1))))))))
       (forward-line))
   (let ((entry (assoc name riece-ruby-exit-handler-alist)))
     (if entry
        (progn
-         (funcall (cdr entry))
-         (setq riece-ruby-exit-handler-alist (delq entry))))))
+         (setq riece-ruby-exit-handler-alist
+               (delq entry riece-ruby-exit-handler-alist))
+         (funcall (cdr entry) (car entry))))))
 
 (defun riece-ruby-sentinel (process status)
   (kill-buffer (process-buffer process)))
                             (expand-file-name
                              riece-ruby-server-program
                              (file-name-directory
-                              (symbol-file 'riece-ruby-execute))))))
+                              (locate-library
+                               (symbol-file 'riece-ruby-execute)))))))
       (set-process-filter riece-ruby-process #'riece-ruby-filter)
       (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel)))
   (save-excursion
     (make-local-variable 'riece-ruby-lock)
     (setq riece-ruby-lock t)
     (riece-ruby-send-poll name)
-    (while (null riece-ruby-response)
+    (while riece-ruby-lock
       (accept-process-output riece-ruby-process))
     (list riece-ruby-response
          riece-ruby-data
     (make-local-variable 'riece-ruby-lock)
     (setq riece-ruby-lock t)
     (riece-ruby-send-exit name)
-    (while (null riece-ruby-response)
-      (accept-process-output riece-ruby-process))))
+    (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)
   (let ((entry (assoc name riece-ruby-exit-handler-alist)))
-    (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)))
+    (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)
   (let ((entry (assoc name riece-ruby-output-handler-alist)))
-    (if entry
-       (setcdr entry handler)
-      (setq riece-ruby-output-handler-alist
-           (cons (cons name handler)
-                 riece-ruby-output-handler-alist)))))
+    (if handler
+       (progn
+         (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)
+  (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)
+  (cdr (assoc property (cdr (assoc name riece-ruby-property-alist)))))
+
+(defun riece-ruby-exit-handler (name)
+  (riece-ruby-inspect name)
+  (let* ((data (copy-sequence riece-ruby-data))
+        (length (length data))
+        (index 0))
+    (while (< index length)
+      (if (eq (aref data index) ?\n)
+         (aset data index " ")))
+    (riece-send-string
+     (format "NOTICE %s :%s\r\n"
+            (riece-identity-prefix
+             (riece-ruby-property name 'riece-ruby-target))
+            data))
+    (riece-display-message
+     (riece-make-message (riece-current-nickname)
+                        (riece-ruby-property name 'riece-ruby-target)
+                        data
+                        'notice))
+    (riece-ruby-clear name)))
+
+(defun riece-ruby-display-message-function (message)
+  (if (and riece-ruby-enabled
+          (riece-message-own-p message)
+          (string-match "^,ruby\\s-+" (riece-message-text message)))
+      (let ((name (riece-ruby-execute
+                  (substring (riece-message-text message)
+                             (match-end 0)))))
+       (riece-ruby-set-property name
+                                'riece-ruby-target
+                                (riece-message-target message))
+       (riece-ruby-set-exit-handler name
+                                    #'riece-ruby-exit-handler))))
+
+(defun riece-ruby-insinuate ()
+  (add-hook 'riece-after-display-message-functions
+           'riece-ruby-display-message-function))
+
+(defun riece-ruby-enable ()
+  (setq riece-ruby-enabled t))
+
+(defun riece-ruby-disable ()
+  (setq riece-ruby-enabled nil))
 
 (provide 'riece-ruby)