* riece-ruby.el: Implement add-on API.
[riece] / lisp / riece-ruby.el
index f8f4f35..fc11dce 100644 (file)
@@ -93,6 +93,11 @@ 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.")
 
+(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))
   (while alist
@@ -211,9 +216,9 @@ Use `riece-ruby-set-property' to set this variable.")
   (let ((entry (assoc name riece-ruby-exit-handler-alist)))
     (if entry
        (progn
-         (funcall (cdr entry) (car entry))
          (setq riece-ruby-exit-handler-alist
-               (delq entry 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)))
@@ -265,28 +270,42 @@ Use `riece-ruby-set-property' to set this variable.")
   (save-excursion
     (set-buffer (process-buffer riece-ruby-process))
     (riece-ruby-reset-process-buffer)
-    (riece-ruby-send-exit name))
+    (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)
   (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 name)))
+    (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))
@@ -301,6 +320,49 @@ Use `riece-ruby-set-property' to set this variable.")
 (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)
 
 ;;; riece-ruby.el ends here