X-Git-Url: https://cgit.sxemacs.org/?a=blobdiff_plain;f=lisp%2Friece-ruby.el;h=f8a839126c344dbaa82600bd892e861d20a4fcf5;hb=4f82ec8c6e92a26b20ca6376b50774b60b7a6619;hp=62da69dbe4df4eb60b6bac370e7416f8a7227fc2;hpb=a4f3d45e0df3895aeb6322e8d8bf4271251c3137;p=riece diff --git a/lisp/riece-ruby.el b/lisp/riece-ruby.el index 62da69d..f8a8391 100644 --- a/lisp/riece-ruby.el +++ b/lisp/riece-ruby.el @@ -3,7 +3,7 @@ ;; Author: Daiki Ueno ;; 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: ;; @@ -49,7 +49,7 @@ ;;; Code: (defgroup riece-ruby nil - "Interact with the Ruby interpreter." + "Interact with Ruby interpreter." :group 'riece) (defcustom riece-ruby-command "ruby" @@ -62,7 +62,7 @@ assumed that the file is in the same directory of this file.") (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 +93,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) @@ -188,7 +177,7 @@ Use `riece-ruby-set-property' to set this variable.") (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)) @@ -197,11 +186,11 @@ Use `riece-ruby-set-property' to set this variable.") '("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 (car entry) (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)) @@ -211,13 +200,17 @@ Use `riece-ruby-set-property' to set this variable.") (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)) (funcall (cdr entry) (car entry)) - (setq riece-ruby-exit-handler-alist (delq 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 @@ -233,6 +226,7 @@ Use `riece-ruby-set-property' to set this variable.") (file-name-directory (locate-library (symbol-file 'riece-ruby-execute))))))) + (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))) (save-excursion @@ -248,50 +242,80 @@ 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) (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 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) (make-local-variable 'riece-ruby-lock) (setq riece-ruby-lock t) (riece-ruby-send-exit name) - (while (null riece-ruby-response) + (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 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) + "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 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) + "Set given PROPERTY/VALUE pair to the program distinguished by NAME." (let ((entry (assoc name riece-ruby-property-alist)) property-entry) (unless entry @@ -302,8 +326,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