Update the default value of riece-url-regexp.
[riece] / lisp / riece-ruby.el
index 6eff56b..0b7ae64 100644 (file)
@@ -19,8 +19,8 @@
 
 ;; 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., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
 ;; => "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:
 
@@ -52,6 +52,7 @@
 
 (defgroup riece-ruby nil
   "Interact with Ruby interpreter."
+  :prefix "riece-"
   :group 'riece)
 
 (defcustom riece-ruby-command "ruby"
@@ -106,6 +107,8 @@ Local to the process buffer.")
   "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.
@@ -139,8 +142,7 @@ Use `riece-ruby-set-property' to set this variable.")
     data))
 
 (defun riece-ruby-reset-process-buffer ()
-  (save-excursion
-    (set-buffer (process-buffer riece-ruby-process))
+  (with-current-buffer (process-buffer riece-ruby-process)
     (buffer-disable-undo)
     (make-local-variable 'riece-ruby-response)
     (setq riece-ruby-response nil)
@@ -177,8 +179,7 @@ Use `riece-ruby-set-property' to set this variable.")
                       (concat "EXIT " name "\r\n")))
 
 (defun riece-ruby-filter (process input)
-  (save-excursion
-    (set-buffer (process-buffer process))
+  (with-current-buffer (process-buffer process)
     (goto-char (point-max))
     (insert input)
     (goto-char (point-min))
@@ -196,7 +197,7 @@ Use `riece-ruby-set-property' to set this variable.")
            (progn
              (setq riece-ruby-escaped-data nil
                    riece-ruby-response
-                   (list 'ERR (string-to-number (match-string 2))
+                   (list 'ERR (string-to-number (match-string 1))
                          (match-string 3))
                    riece-ruby-lock nil))
          (if (looking-at "D \\(.*\\)\r")
@@ -212,11 +213,9 @@ Use `riece-ruby-set-property' to set this variable.")
                      (riece-ruby-run-exit-handler
                       (cdr (car riece-ruby-status-alist)))))
              (if (looking-at "# output \\([^ ]*\\) \\(.*\\)\r")
-                 (let ((entry (assoc (match-string 1)
-                                     riece-ruby-output-handler-alist)))
-                   (if entry
-                       (riece-debug-with-backtrace
-                         (funcall (cdr entry) (car entry) (match-string 2)))))
+                 (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))
@@ -224,13 +223,28 @@ Use `riece-ruby-set-property' to set this variable.")
 
 (defun riece-ruby-run-exit-handler (name)
   (let ((entry (assoc name riece-ruby-exit-handler-alist)))
-    (if entry
-       (progn
-         (setq riece-ruby-exit-handler-alist
-               (delq entry riece-ruby-exit-handler-alist))
-         (riece-debug-with-backtrace
-           (funcall (cdr entry) (car entry)))
-         (riece-ruby-clear name)))))
+    (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)))
@@ -245,20 +259,14 @@ Return a string name assigned by the server."
          (coding-system-for-read 'binary))
       (setq riece-ruby-process
            (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)
+                  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)))
-  (save-excursion
-    (set-buffer (process-buffer riece-ruby-process))
+  (with-current-buffer (process-buffer riece-ruby-process)
     (riece-ruby-reset-process-buffer)
     (make-local-variable 'riece-ruby-lock)
     (setq riece-ruby-lock t)
@@ -276,8 +284,7 @@ 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))
+  (with-current-buffer (process-buffer riece-ruby-process)
     (riece-ruby-reset-process-buffer)
     (make-local-variable 'riece-ruby-lock)
     (setq riece-ruby-lock t)
@@ -292,8 +299,7 @@ The caddr is status from the server."
   "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))
+  (with-current-buffer (process-buffer riece-ruby-process)
     (riece-ruby-reset-process-buffer)
     (make-local-variable 'riece-ruby-lock)
     (setq riece-ruby-lock t)
@@ -328,11 +334,24 @@ is specified.  Otherwise, it should be called explicitly."
   "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)))
+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