4 ;; Author: Paul Kinnucan <paulk@mathworks.com>
5 ;; Maintainer: Paul Kinnucan
6 ;; Keywords: java, tools
8 ;; Copyright (C) 1997, 1998, 2001, 2002, 2003, 2004 Paul Kinnucan.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This package is intended to serve as an interface between Emacs and
28 ;; Pat Neimeyer's BeanShell, a Java source code interpreter (see
29 ;; http://www.beanshell.org/).
31 ;; This package is intended to serve both interactive users and Emacs
32 ;; applications that want to use Java as an extension language for
33 ;; Emacs. To facilitate this dual usage, this package implements the
34 ;; BeanShell interface as an eieio class named bsh. This class is
35 ;; intended to serve as a base class for classes tailored to specific
36 ;; applications. This class defines the following methods:
38 ;; bsh Constructs an instance of the bsh class.
39 ;; bsh-launch Launches the BeanShell and creates an Emacs buffer
40 ;; for interacting with the BeanShell.
41 ;; bsh-eval Sends a Java expression to the BeanShell for evaluation and
42 ;; waits for a response.
43 ;; bsh-eval-r Sends a Java expression to the BeanShell for evaluation
44 ;; and then evaluates the resulting BeanShell output
45 ;; as a Lisp expression.
46 ;; bsh-async-eval Sends a Java expression to the BeanShell for evaluation
47 ;; and sets a listener to evaluate the response as a Lisp
50 ;; bsh-eval and bsh-eval-r synchronous functions. After sending a Java
51 ;; expression to the BeanShell, they suspend Emacs until the BeanShell
52 ;; responds. bsh-async-eval does not suspend Emacs. It does, however,
53 ;; set a listener that evaluates any responses from the BeanShell as
56 ;; See bsh-demo-eval for an example of how to use bsh-async-eval to evaluate a
60 ;; The bsh class also defines fields that specify the path of the
61 ;; BeanShell jar file, the Java vm used to run the BeanShell, a
62 ;; startup classpath and directory, vm arguments, etc. Emacs package
63 ;; developers can easily extend this base class to tailor the BeanShell
64 ;; to specific applications.
65 ;; This package provides one such derived class named
66 ;; bsh-standalone-bsh. This is a BeanShell implementation intended
67 ;; for use by interactive users.
69 ;; To use the standalone BeanShell, download the BeanShell jar file
70 ;; from http://www.beanshell.org/ and install it on your system.
71 ;; Set bsh-jar to the path of the BeanShell jar. Set bsh-vm to
72 ;; the path of a Java vm on your system (this is necessary only
73 ;; if the java vm is not on your system's command path). Execute
74 ;; M-x bsh to start the BeanShell. To terminate the BeanShell,
75 ;; execute M-x bsh-exit.
82 "Customizations for the Emacs inteface to Pat Neimeyer's Java
83 interpreter, the Beanshell."
87 (defcustom bsh-jar "bsh.jar"
88 "Path to the jar file containing the BeanShell classes."
92 ;; (makunbound 'bsh-vm)
94 "Java vm to be used to run the standalone BeanShell. By default,
95 bsh uses the first vm that it finds on the Emacs
96 `exec-path' (usually the same as your system's command
97 path). To specify a vm, select Path and enter the
101 (const :tag "Default" :value nil)
104 (defcustom bsh-classpath nil
105 "Startup classpath for the BeanShell."
107 :type '(repeat (file :tag "Path")))
109 ;; (makunbound 'bsh-startup-timeout)
110 (defcustom bsh-startup-timeout 10
111 "*Length of time Emacs waits for the Beanshell to startup.
112 Increase the value of this variable if you get Lisp errors on
113 BeanShell startup on Unix. Setting this value on some versions of
114 XEmacs, e.g., Windows, seems to cause premature timeouts. If you keep
115 getting timeout errors no matter how large a value you set, try
116 setting this variable to no timeout (nil)."
118 :type '(choice (const :tag "No timeout" :value nil)
119 (number :tag "Length")))
121 ;; (makunbound 'bsh-eval-timeout)
122 (defcustom bsh-eval-timeout 30
123 "*Length of time in seconds Emacs waits for the Beanshell to
124 evaluate a Java expression before giving up and signaling an
125 error. Setting this value on some versions of XEmacs, e.g., Windows,
126 seems to cause premature timeouts. If you keep getting timeout errors
127 no matter how large a value you set, try setting this variable to no
130 :type '(choice (const :tag "No timeout" :value nil)
131 (number :tag "Length")))
133 (defcustom bsh-vm-args nil
134 "*Specify arguments to be passed to the Java vm.
135 This option allows you to specify one or more arguments to be passed
136 to the Java vm that runs the BeanShell. Note that the value of this
137 variable should be a list of strings, each of which represents an
138 argument. When customizing this variable, use a separate text field
141 :type '(repeat (string :tag "Argument")))
143 (defcustom bsh-startup-directory ""
144 "Path of directory in which to start the beanshell.
145 The path may start with a tilde (~) indication your
146 home directory and may include environment variables.
147 If this variable is the null string (the default),
148 the beanshell starts in the directory of the current
154 (defclass bsh-buffer ()
155 ((buffer-name :initarg :buffer-name
159 "Name of buffer used to interact with BeanShell process.")
160 (buffer :initarg :buffer
163 "Buffer used to interact with BeanShell process.")
165 (process :initarg :process
167 "Beanshell process.")
169 (filter :initarg :filter
172 "Function used to propcess buffer output."))
173 "Buffer that displays BeanShell output.")
176 (defmethod initialize-instance ((this bsh-buffer) &rest fields)
177 "Constructor for BeanShell buffer instance."
180 (oset this buffer (get-buffer-create (oref this buffer-name))))
182 (defmethod bsh-buffer-live-p ((this bsh-buffer))
183 "Return t if this buffer has not been killed."
184 (buffer-live-p (oref this buffer)))
186 (defmethod bsh-buffer-display ((this bsh-buffer))
187 "Display this buffer."
188 (pop-to-buffer (oref this buffer-name)))
191 (defclass bsh-comint-buffer (bsh-buffer)
193 "BeanShell buffer that runs in `comint-mode'.")
195 (defmethod initialize-instance ((this bsh-comint-buffer) &rest fields)
196 "Constructor for BeanShell buffer instance."
198 (with-current-buffer (oref this buffer)
200 (setq comint-prompt-regexp "bsh % ")))
202 (defmethod bsh-comint-buffer-exec ((this bsh-comint-buffer) vm vm-args)
203 (let ((win32-start-process-show-window t)
204 (w32-start-process-show-window t)
205 (w32-quote-process-args ?\") ;; Emacs
206 (win32-quote-process-args ?\") ;; XEmacs
207 (windowed-process-io t)
208 (process-connection-type nil)
210 (coding-system-for-read
211 (if (or (member system-type '(cygwin32 cygwin))
212 (eq system-type 'windows-nt))
214 (comint-exec (oref this buffer) "bsh" vm nil vm-args))
216 (oset this process (get-buffer-process (oref this buffer)))
217 (oset this filter (process-filter (oref this process)))
218 (process-kill-without-query (oref this process))
220 (if (eq system-type 'windows-nt)
221 (accept-process-output (oref this process) bsh-startup-timeout 0)
222 (while (accept-process-output (oref this process) bsh-startup-timeout 0))))
225 (defclass bsh-compilation-buffer (bsh-buffer)
227 "Implements a `compilation-mode' buffer for BeanShell output.")
229 (defmethod initialize-instance ((this bsh-compilation-buffer) &rest fields)
230 "Constructor for BeanShell compilation buffer instance."
232 (bsh-compilation-buffer-create-native-buffer this)
237 (lexical-let ((this-buf this))
238 (lambda (process output)
239 (bsh-compilation-buffer-filter this-buf process output))))
241 ;; This buffer does not have its own process.
242 (oset this process nil)
243 (bsh-compilation-buffer-set-mode this))
245 (defmethod bsh-compilation-buffer-create-native-buffer ((this bsh-compilation-buffer))
246 "Creates the native Emacs buffer encapsulated by this eieio object."
247 (oset this buffer-name "*bsh compilation*")
248 (oset this buffer (get-buffer-create (oref this buffer-name))))
250 (defmethod bsh-compilation-buffer-set-mode ((this bsh-compilation-buffer))
251 "Define buffer mode."
252 (let ((thisdir default-directory))
253 (with-current-buffer (oref this buffer)
254 (let ((buf (oref this buffer))
255 ;; Some or all of these variables may not be defined by
256 ;; the various versions of compile.el shipped with Emacs
259 (if (boundp 'compilation-error-regexp-alist)
260 compilation-error-regexp-alist))
262 (if (boundp 'compilation-enter-directory-regexp-alist)
263 compilation-enter-directory-regexp-alist))
265 (if (boundp 'compilation-leave-directory-regexp-alist)
266 compilation-leave-directory-regexp-alist))
268 (if (boundp 'compilation-file-regexp-alist)
269 compilation-file-regexp-alist))
270 (nomessage-regexp-alist
271 (if (boundp 'compilation-nomessage-regexp-alist)
272 compilation-nomessage-regexp-alist))
274 (if (boundp 'compilation-parse-errors-function)
275 compilation-parse-errors-function))
276 (error-message "No further errors"))
278 ;; In case the compilation buffer is current, make sure we get the global
279 ;; values of compilation-error-regexp-alist, etc.
280 (kill-all-local-variables)
282 ;; Clear out the compilation buffer and make it writable.
283 (setq buffer-read-only nil)
284 (buffer-disable-undo (current-buffer))
286 (buffer-enable-undo (current-buffer))
289 (setq buffer-read-only nil)
291 (set (make-local-variable 'compilation-parse-errors-function) parser)
292 (set (make-local-variable 'compilation-error-message) error-message)
293 (set (make-local-variable 'compilation-error-regexp-alist)
296 (if (not (featurep 'xemacs))
298 (set (make-local-variable 'compilation-enter-directory-regexp-alist)
300 (set (make-local-variable 'compilation-leave-directory-regexp-alist)
302 (set (make-local-variable 'compilation-file-regexp-alist)
304 (set (make-local-variable 'compilation-nomessage-regexp-alist)
305 nomessage-regexp-alist)))
307 (setq default-directory thisdir)
308 (setq compilation-directory-stack (list default-directory))))))
310 (defmethod bsh-compilation-buffer-filter ((this bsh-compilation-buffer) proc string)
311 "This filter prints out the result of the process without buffering.
312 The result is inserted as it comes in the compilation buffer."
313 (with-current-buffer (oref this buffer)
314 (let ((end-of-result (string-match ".*bsh % " string))
315 (win (get-buffer-window (oref this buffer)))
316 output len (status " "))
318 ;;Insert the text, advancing the process marker
319 (goto-char (point-max))
322 (setq output (substring string 0 end-of-result))
323 (set-buffer-modified-p nil)
325 ;;Searching backwards for the status code
326 (while (member status '(" " "\r" "\n"))
327 (setq len (length output))
330 (setq status (substring output (- len 1)))
331 (setq output (substring output 0 (- len 1))))
333 (setq status (buffer-substring (- (point-max) 1)
335 (delete-region (- (point-max) 1) (point-max)))))
338 (compilation-handle-exit
340 (if (string= "0" status)
342 (format "exited abnormally with code %s\n"
345 (if (not (featurep 'xemacs))
346 (if compilation-scroll-output
347 (save-selected-window
351 (goto-char (point-max))))
357 ((buffer :initarg :buffer
358 :type bsh-comint-buffer
360 "Buffer used to interact with BeanShell process.")
363 (eval-buffer :initarg :eval-buffer
364 :type (or null bsh-buffer)
367 "Buffer used to display evaluation result.")
369 (eval-filter :initarg :eval-filter
372 "Function used to capture Lisp output from the BeanShell.")
374 (async-filter :initarg :async-filter
377 "Function used to capture and evaluate BeanShell Lisp output.")
379 (redir-filter :initarg :redir-filter
382 "Redirects BeanShell output to eval-buffer.")
384 (java-expr :initarg :java-expr
388 "Last Java expression evaluated in the BeanShell.")
390 (lisp-output :initarg :lisp-output
394 "Lisp output from the BeanShell.")
401 "Path of Java vm used to run the BeanShell.")
403 (vm-args :initarg :vm-args
407 "List of arguments to be passed to the Beanshell vm.")
409 (startup-dir :initarg :startup-dir
413 "Directory in which to start the BeanShell")
418 "Startup classpath for BeanShell")
424 "Path of the BeanShell jar file.")
426 (class-name :initarg :class-name
427 :initform "bsh.Interpreter"
430 "Name of BeanShell class."))
431 "Defines an instance of a BeanShell process.")
433 (defmethod initialize-instance ((this bsh) &rest fields)
434 "Constructor for BeanShell instance."
438 (bsh-create-buffer this)
440 (lexical-let ((this-bsh this))
441 ;; Filter for synchronous evaluations.
444 (lambda (process output)
445 (with-current-buffer (process-buffer process)
446 (bsh-snag-lisp-output this-bsh process output))))
448 ;; Filter for asynchronous Java statement evaluations.
451 (lambda (process output)
452 (with-current-buffer (process-buffer process)
453 (bsh-snag-and-eval-lisp-output this-bsh process output))))
455 ;; Filter for redirecting result of evaluating an expression to the
456 ;; buffer specified by eval-buffer. This is typically used to
457 ;; redirect compiler like output to a compilation-mode buffer.
460 (lambda (process output)
461 (with-current-buffer (process-buffer process)
462 (bsh-redirect-eval-output this-bsh process output))))))
464 (defmethod bsh-create-buffer ((this bsh))
465 "Creates the buffer used by this beanshell instance."
466 (oset this buffer (bsh-comint-buffer "bsh main buffer")))
469 (defmethod bsh-build-classpath-argument ((this bsh))
470 "Convert the list of classes specified by the cp slot
471 to the string form required by the vm."
475 (and (slot-boundp this 'cp) (oref this cp))
476 (list (oref this jar)))
479 (defmethod bsh-running-p ((this bsh))
480 "Return t if this instance of a BeanShell is running; otherwise nil"
481 (let* ((buffer (oref this buffer))
483 (if (slot-boundp buffer 'process)
484 (oref buffer process))))
488 (eq (process-status process) 'run))))
490 (defmethod bsh-get-process ((this bsh))
491 "Return the Lisp object representing the Beanshell process."
492 (if (bsh-running-p this)
493 (oref (oref this buffer) process)))
496 (defmethod bsh-launch ((this bsh) &optional display-buffer)
499 (or (file-exists-p (oref this vm))
500 (executable-find (oref this vm)))
501 nil "Specified vm does not exist: %s" (oref this vm))
504 (file-exists-p (oref this jar))
506 "Specified BeanShell jar filed does not exist: %s" (oref this jar))
509 (if (not (bsh-running-p this))
512 ((not (string= (oref this startup-dir) ""))
513 (expand-file-name (oref this startup-dir)))
515 (file-name-directory (buffer-file-name)))
518 (vm-args (list "-classpath" (bsh-build-classpath-argument this)))
520 (let ((buf (oref this buffer)))
521 (if (bsh-buffer-live-p buf)
523 (bsh-create-buffer this))))
524 (native-buff (oref buffer buffer)))
527 (setq vm-args (append vm-args (oref this vm-args)))
528 (setq vm-args (append vm-args bsh-vm-args))
529 (setq vm-args (append vm-args (list (oref this class-name))))
531 (with-current-buffer native-buff
535 (insert (concat "cd " dir "\n"))
537 (concat (oref this vm) " "
538 (mapconcat (lambda (x) x) vm-args " ") "\n\n"))
540 (setq bsh-the-bsh this))
542 (message "%s" "Starting the BeanShell. Please wait...")
543 (bsh-comint-buffer-exec buffer (oref this vm) vm-args)
546 (bsh-buffer-display buffer)))
548 (message "The BeanShell is already running.")
549 (bsh-buffer-display (oref this buffer)))))
551 (defvar bsh-the-bsh nil
552 "The BeanShell instance associated with the current BeanShell buffer.")
553 (make-variable-buffer-local 'bsh-the-bsh)
556 (defmethod bsh-snag-lisp-output ((this bsh) process output)
557 "Assemble Lisp OUTPUT from the BeanShell."
558 (let ((end-of-output (string-match ".*bsh % " output)))
560 ;; %bsh\n...eval output...%bsh\n
561 ;; This can happen because the beanshell outputs two or more
562 ;; prompts after evaluating some expressions.
563 ;; Thanks to Stephane Nicolas.
564 ;; (if (eq end-of-result 0)
565 ;; (accept-process-output process 0 5))
570 (concat (oref this lisp-output) (substring output 0 end-of-output)))
571 (oset this lisp-output (concat (oref this lisp-output) output))
572 (accept-process-output process bsh-eval-timeout 5))))
574 (defmethod bsh-detect-java-eval-error ((this bsh) bsh-output)
575 (if (string-match "// Error:" bsh-output)
578 "Beanshell expression evaluation error.\n Expression: %s\n Error: %s"
579 (oref this java-expr) bsh-output)
580 (error "Beanshell eval error. See messages buffer for details."))))
583 (defmethod bsh-eval-lisp-output ((this bsh))
584 (if (not (string= (oref this lisp-output) ""))
585 (flet ((format-error-msg (error-symbols)
586 (mapconcat (lambda (s) (symbol-name s)) error-symbols " ")))
587 (condition-case eval-error
588 (eval (read (oref this lisp-output)))
590 (message "Error evaluating Lisp result of Java expression evaluation.")
591 (message " Java expression: %s." (oref this java-expr))
592 (message " Java evaluation result: %s." (oref this lisp-output))
593 ;; The following causes an unreadable object error on XEmacs when
594 ;; trying to load the byte-compiled file:
596 ;; (message " Error from evaluating result as Lisp: %s"
597 ;; (mapconcat (lambda (s) (symbol-name s)) eval-error " ")
598 (message " Error from evaluating result as Lisp: %s"
599 (format-error-msg eval-error))
600 (error "Error evaluating Java expresson. See *Messages* buffer."))))
602 (message "bsh-eval-r error: Beanshell result is null. Cannot evaluate.")
603 (message " Expression: %s" (oref this java-expr)))))
605 (defmethod bsh-snag-and-eval-lisp-output ((this bsh) process output)
606 "Assemble and parse Lisp OUTPUT from the BeanShell PROCESS."
607 (bsh-snag-lisp-output this process output)
608 (bsh-detect-java-eval-error this output)
609 (bsh-eval-lisp-output this))
612 (defmethod bsh-redirect-eval-output ((this bsh) process output)
613 (let ((eval-buffer (oref this eval-buffer)))
615 (funcall (oref eval-buffer filter) process output)
616 ;; Restore the Beanshell's standard output filter when
617 ;; the Beanshell prompt reappears.
618 (if (string-match ".*bsh % " output)
620 (set-process-filter process (oref (oref this buffer) filter))
621 (oset this eval-buffer nil)))))
623 (defmethod bsh-eval ((this bsh) expr &optional eval-return)
624 "Uses the BeanShell Java interpreter to evaluate the Java expression
625 EXPR. This function returns any text output by the Java interpreter's
626 standard out or standard error pipes. If EVAL-RETURN is non-nil, this
627 function returns the result of evaluating the Java output as a Lisp
630 (unless (bsh-running-p this)
633 (when (bsh-running-p this)
634 (oset this java-expr expr)
635 (oset this lisp-output "")
636 (set-process-filter (oref (oref this buffer) process) (oref this eval-filter))
637 ;; (message "Evaluating: %s" expr)
638 (process-send-string (oref (oref this buffer) process) (concat expr "\n"))
640 (if (not (accept-process-output (oref (oref this buffer) process) bsh-eval-timeout))
642 (set-process-filter (oref (oref this buffer) process) (oref (oref this buffer) filter))
643 (error "No reply from BeanShell")))
645 (set-process-filter (oref (oref this buffer) process) (oref (oref this buffer) filter))
647 (bsh-detect-java-eval-error this (oref this lisp-output))
648 ;; (if eval-return (message "Evaluating reply: %s" (oref this lisp-output)))
650 (bsh-eval-lisp-output this)
651 (oref this lisp-output))))
653 (defmethod bsh-eval-r ((this bsh) java-statement)
654 "Convenience method for evaluating Java statements
655 that return Lisp expressions as output. This function
656 invokes bsh-eval with the evaluate-return option set to
658 (bsh-eval this java-statement t))
660 (defmethod bsh-async-eval ((this bsh) expr)
661 "Send the Java statement EXPR to the BeanShell for
662 evaluation. Do not wait for a response."
663 (unless (bsh-running-p this)
666 (oset this lisp-output "")
667 (oset this java-expr expr)
668 (set-process-filter (oref (oref this buffer) process) (oref this async-filter))
670 (when (bsh-running-p this)
671 (process-send-string (oref (oref this buffer) process) (concat expr "\n"))))
673 (defmethod bsh-buffer-eval ((this bsh) expr buffer)
674 "Evaluate EXPR and displays its output in BUFFER. BUFFER
675 must be an instance of `bsh-buffer' class. A typical use for this
676 method is to invoke a Java application whose output is compiler-like,
677 e.g., javac, ant, or checkstyle. In this case, BUFFER would be
678 an instance of a subclass of `bsh-compiler-buffer'."
679 (assert (typep expr 'string))
680 (assert (typep buffer 'bsh-buffer))
681 (unless (bsh-running-p this)
683 (let* ((comint-buffer (oref this buffer))
684 (bsh-process (oref comint-buffer process)))
685 (oset this eval-buffer buffer)
686 (set-process-filter bsh-process (oref this redir-filter))
687 (process-send-string bsh-process expr)))
689 (defmethod bsh-kill-process ((this bsh))
690 "Terminates the BeanShell process."
691 (process-send-string (oref (oref this buffer) process) "exit();\n"))
693 (defun get-bsh(buffer-name)
694 "Get the `bsh' object associated
695 with the buffer named BUFFER-NAME."
697 (buffer (get-buffer buffer-name)))
701 (setq bsh-object bsh-the-bsh)))
705 (defun bsh-demo-eval ()
706 "This command demonstrates use of the BeanShell to
707 execute Java code. It assumes that
708 `bsh-jar' points to the location of the BeanShell
709 jar file on your system and that either `exec-path'
710 or `bsh-vm' point to a Java vm on your system."
714 (file-exists-p (expand-file-name bsh-jar))
717 "Cannot find the BeanShell jar file at "
718 (expand-file-name bsh-jar)
719 ". Type C-h bsh-jar for more info."))
724 (executable-find bsh-vm)
725 (file-exists-p bsh-vm))
727 "The vm specified by bsh-vm does not exist: %s." bsh-vm)
729 (executable-find (if (eq system-type 'windows-nt) "javaw" "java"))
731 "Cannot find a Java vm on exec-path."))
734 (let ((beanshell (get-bsh "*bsh demo*"))
738 "name = JOptionPane.showInputDialog(\"Enter your name.\");"
739 "print(\"(message \\\"Your name is \" + name + \"\\\")\");"
746 ;; Create a BeanShell wrapper object
747 (setq beanshell (bsh "BeanShell"))
749 (oset (oref beanshell buffer) buffer-name "*bsh demo*")
751 ;; Set the wrapper's jar slot (eieio/CLOS speak for field) to
752 ;; point to the location of the BeanShell on the user's
754 (oset beanshell jar (expand-file-name bsh-jar))
756 ;; Set the wrapper's vm slot to point to the location
757 ;; of a vm on the user's system. The wrapper's launch
758 ;; method (see below) uses the specified vm to launch
765 (executable-find bsh-vm)
767 (executable-find (if (eq system-type 'windows-nt) "javaw" "java"))))))
769 (unless (bsh-running-p beanshell)
770 (bsh-launch beanshell)
771 (bsh-async-eval beanshell demo-script))
772 (bsh-async-eval beanshell "demo();")))
776 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
777 ;;; Standalone BeanShell ;;
778 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
780 (defclass bsh-standalone-bsh (bsh)
781 ((the-bsh :type bsh-standalone-bsh
784 "The single instance of the standalone BeanShell."))
785 "BeanShell intended to be used independently of any other
788 (defmethod initialize-instance ((this bsh-standalone-bsh) &rest fields)
789 "Constructor for the standard bsh BeanShell instance."
793 (file-exists-p (expand-file-name bsh-jar))
796 "Cannot find the BeanShell jar file at "
797 (expand-file-name bsh-jar)
798 ". Type C-h bsh-jar for more info."))
803 (executable-find bsh-vm)
804 (file-exists-p bsh-vm))
806 "The vm specified by bsh-vm does not exist: %s." bsh-vm)
808 (executable-find (if (eq system-type 'windows-nt) "javaw" "java"))
810 "Cannot find a Java vm on exec-path."))
813 (oset this jar (expand-file-name bsh-jar))
820 (executable-find bsh-vm)
822 (executable-find (if (eq system-type 'windows-nt) "javaw" "java"))))
824 (oset this cp bsh-classpath)
826 (oset this startup-dir bsh-startup-directory))
829 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
830 ;;; Beanshell commands ;;
831 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
834 "*Starts the standalone version of the BeanShell, a Java interpreter developed
837 (oset-default 'bsh-standalone-bsh the-bsh (bsh-standalone-bsh "Standalone BeanShell"))
838 (bsh-launch (oref bsh-standalone-bsh the-bsh) t))
841 "Closes the standalone version of the BeanShell."
844 (if (slot-boundp 'bsh-standalone-bsh 'the-bsh)
845 (oref 'bsh-standalone-bsh the-bsh))))
846 (if (and bsh (bsh-running-p bsh))
847 (bsh-kill-process bsh)
848 (message "The beanshell is not running"))))
851 (defun bsh-find-bsh-data-directory ()
852 "Return the path of the bsh data directory.
853 Returns the path of the directory containing the documentation directory; nil if the
854 directory cannot be found. If XEmacs, returns the location of
855 the data directory in the XEmacs distribution hierarchy. On all other Emacs versions,
856 the bsh expects to find the documentation
857 in the same directory that contains the bsh.el file."
858 (let ((directory-sep-char ?/)
860 (flet ((find-data-dir
864 (file-name-directory (locate-library "beanshell")))))
865 (if (featurep 'xemacs)
867 (setq dir (locate-data-directory "beanshell"))
869 (setq dir (find-data-dir))))
870 (setq dir (find-data-dir))))
874 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
875 ;;; Beanshell mode ;;
876 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
879 bsh-script-mode java-mode "bsh script"
880 "Major mode for developing Beanshell scripts.
881 \\(bsh-script-mode-map)"
883 (set (make-local-variable 'font-lock-defaults)
884 (if (featurep 'xemacs)
885 (get 'java-mode 'font-lock-defaults)
886 (cdr (assq 'java-mode font-lock-defaults))))
887 (set (make-local-variable 'font-lock-maximum-decoration) t)
888 (set (make-local-variable 'font-lock-multiline) t)
891 (add-to-list 'auto-mode-alist '("\\.bsh\\'" . bsh-script-mode))
894 (defun bsh-script-help ()
895 "Display BeanShell User's Guide."
897 (let* ((bsh-dir (bsh-find-bsh-data-directory))
900 (expand-file-name "bsh-ug.html" bsh-dir))))
903 (file-exists-p bsh-help))
904 (browse-url (concat "file://" bsh-help)
905 (if (boundp 'browse-url-new-window-flag)
906 'browse-url-new-window-flag
907 browse-url-new-window-p))
908 (signal 'error '("Cannot find BeanShell help file.")))))
910 (defcustom bsh-script-menu-definition
912 ["Help" bsh-script-help t])
913 "Definition of menu for BeanShell script buffers."
916 :set '(lambda (sym val)
917 (set-default sym val)
918 ; Define Bsh script menu for FSF Emacs.
919 (if (or (not (featurep 'xemacs))
920 (featurep 'infodock))
921 (easy-menu-define bsh-script-menu
923 "Menu for BeanShell Script Buffer."
925 (if (and (featurep 'xemacs)
926 (eq major-mode 'bsh-script-mode))
927 (bsh-script-insert-menu-in-xemacs-menubar))))
929 (defun bsh-script-insert-menu-in-xemacs-menubar ()
930 "Insert BeanShell script menu in the XEmacs menu bar."
932 (not (featurep 'infodock))
933 (not (memq 'infodock c-emacs-features))
934 (boundp 'current-menubar)
936 (if (fboundp 'add-submenu)
937 (add-submenu nil bsh-script-menu-definition)
938 (add-menu nil "Bsh" (cdr bsh-script-menu-definition)))))
944 ;; $Log: beanshell.el,v $
945 ;; Revision 1.76 2005/01/23 00:02:39 jslopez
946 ;; Set buffer-read-only to nil after invoking compilation-mode, which sets it to t
947 ;; in latest version of Emacs.
949 ;; Revision 1.75 2004/12/22 04:20:14 paulk
950 ;; Fix bsh-exit to handle case where the standalone beanshell is not running.
952 ;; Revision 1.74 2004/10/14 04:32:43 paulk
953 ;; Set process-connection-type to nil (always uses pipes for process I/O)
954 ;; when starting a beanshell comint buffer. This change is intended to
955 ;; avoid code 129 errors that occur on some versions of Linux.
957 ;; Revision 1.73 2004/06/06 04:31:08 paulk
958 ;; Fixed reference to obsolete font-lock variable.
960 ;; Revision 1.72 2004/06/03 02:54:43 paulk
961 ;; Remove unnecessary format clauses in assert forms.
963 ;; Revision 1.71 2004/05/04 03:59:23 paulk
964 ;; Eliminate optional argument to compilation-mode to ensure compatiblity with next release of Emacs.
966 ;; Revision 1.70 2004/04/30 02:38:16 paulk
967 ;; Makes beanshell not depend on compilation- variables being defined.
969 ;; Revision 1.69 2004/03/21 15:05:02 paulk
970 ;; The beanshell redirect filter now restores the standard filter only when the Beanshell prompt reappears. This ensures that the redirect filter redirects the entire output of the previous Beanshell expression evaluation. Thanks to J
\81örg Mensmann.
972 ;; Revision 1.68 2003/06/30 03:52:39 paulk
973 ;; Include bsh-vm-args in the command line that starts the BeanShell, thus fixing a
974 ;; regression error reported by TAKAHASHI Toru.
976 ;; Revision 1.67 2003/06/22 16:06:43 paulk
977 ;; Fix "unreadable object" error when XEmacs tries to load beanshell.elc.
979 ;; Revision 1.66 2003/05/07 04:29:37 paulk
980 ;; Include Lisp error in messages buffer when a Lisp error results from evaluating the result of a Java evaluation as Lisp.
982 ;; Revision 1.65 2003/04/16 04:13:18 paulk
983 ;; Added a require statement for the comint package. This is not necessary for use with the JDEE, which already requires comint, but may be necessary when beanshell.el is used independently of the JDEE.
985 ;; Revision 1.64 2003/04/11 22:51:33 jslopez
986 ;; Fixes typo, orer to oref.
988 ;; Revision 1.63 2003/04/08 03:14:07 paulk
989 ;; Fixes bug that caused the working directory of the compilation buffer to be set incorrectly. Also add methods to bsh-compilation-buffer class that simplify subclassing.
991 ;; Revision 1.62 2003/04/06 07:32:25 paulk
992 ;; Fixed bugs caused by not handling the case when the user has killed a
993 ;; Beanshell buffer. Also added a bsh-create-buffer method to allow
994 ;; derived classes to override the type of buffer used to interact
995 ;; with the BeanShell.
997 ;; Revision 1.61 2003/03/04 10:10:40 paulk
998 ;; Made the following changes to provide generalized support
999 ;; for displaying BeanShell output in a compilation-mode buffer.
1001 ;; - Defined a new eieio class hierarchy that wraps Emacs buffers.
1002 ;; The hierarchy contains three classes: bsh-buffer, the base,
1003 ;; and bsh-comint-buffer and bsh-compilation-buffer class, which
1004 ;; wrap comint-mode and compilation-mode buffers, respectively.
1006 ;; - Added a bsh-buffer-eval method to the bsh class that displays
1007 ;; the results of evaluating a Java expression in a specified
1010 ;; Revision 1.60 2003/02/17 08:09:29 paulk
1011 ;; Refactors beanshell.el so that it supports but does not require the
1012 ;; JDEE. This allows beanshell.el to be used to support other Emacs
1013 ;; applications that want to use Java as an extension language. See
1014 ;; the package commentary for more information.
1016 ;; Revision 1.59 2003/02/15 20:56:53 nsieger
1017 ;; (bsh-eval): Add second optional IGNORE-EMPTY parameter which
1018 ;; suppresses errors if evaluation produces no forms. Part of initial
1019 ;; JUCI checkin, which necessitated this feature.
1021 ;; Revision 1.58 2003/01/27 06:38:49 paulk
1022 ;; Moved error-handling for when jde-get-tools-jar returns a nil value into
1023 ;; jde-get-tools-jar itself.
1025 ;; Revision 1.57 2003/01/20 04:43:47 paulk
1026 ;; Slightly expanded error message for not being able to find the JDK tools jar file.
1028 ;; Revision 1.56 2003/01/09 18:21:14 ahyatt
1029 ;; Beanshell should be started with an expanded classpath
1031 ;; Revision 1.55 2003/01/03 16:35:24 jslopez
1032 ;; Fixes bug that was not adding the ant jar files to the beanshell classpath
1033 ;; when jde-ant-home is not bound. This could happen because the jde-ant
1034 ;; package has not been loaded.
1036 ;; Revision 1.54 2002/12/30 05:09:00 paulk
1037 ;; Define bsh-script-mode for editing BeanShell scripts.
1039 ;; Revision 1.53 2002/12/14 03:54:09 jslopez
1040 ;; Adds all the jar files in the ant/lib directory to the beanshell classpath.
1042 ;; Revision 1.52 2002/12/06 03:47:37 ahyatt
1043 ;; Changes to support Mac OS X, which does not use tools.jar
1045 ;; Revision 1.51 2002/11/14 06:03:34 paulk
1046 ;; Fix regression bug in bsh-exit caused by deferring loading of ant package until used.
1048 ;; Revision 1.50 2002/10/31 05:19:34 paulk
1049 ;; Applied compatibility fix for Mac OS X. Thanks to Andi Vajda <avajda@nanospace.com>.
1051 ;; Revision 1.49 2002/10/22 04:41:19 paulk
1052 ;; Checks whether jde-ant-home is bound. This is necessary because jde-ant is now autoloaded and hence jde-ant-home is not bound until the user executes an ant command.
1054 ;; Revision 1.48 2002/09/16 05:05:59 paulk
1055 ;; Cygwin Emacs compatibility fix. Check for Cygwin Emacs when processing paths. Thanks
1058 ;; Revision 1.47 2002/09/11 03:32:56 paulk
1059 ;; If jde-devel-debug is true, the BeanShell now uses the JDEE classes
1060 ;; in the jde/java/classes directory instead of those in the
1061 ;; jde/java/lib/jde.jar file. This simplifies testing changes
1062 ;; to the JDEE classes.
1064 ;; Revision 1.46 2002/09/10 04:44:31 paulk
1065 ;; The JDEE now waits for the BeanShell startup message
1066 ;; on Windows. Previously, it returned possibly creating
1067 ;; subtle timing bugs.
1071 ;; Revision 1.45 2002/08/27 05:03:30 paulk
1072 ;; Put the JDEE libraries ahead of jde-global-classpath in the beanshell classpath to ensure that the
1073 ;; JDEE versions of these libraries are loaded and not other versions that the user may have installed
1074 ;; in their classpath. Thanks to Andy Piper.
1076 ;; Revision 1.44 2002/06/22 05:52:18 paulk
1077 ;; Fixed minor compilation error.
1079 ;; Revision 1.43 2002/06/18 06:02:51 paulk
1080 ;; XEmacs compatibility fix: allow user to set bsh-startup-timeout
1081 ;; and bsh-eval-timeout to floating point values and to nil
1082 ;; (no timeout) to accommodate an apparent bug in the way
1083 ;; accept-process-output works on the Windows version of XEmacs.
1085 ;; Revision 1.42 2002/06/12 07:04:31 paulk
1086 ;; XEmacs compatibility fix: set win32-quote-process-args wherever
1087 ;; the JDEE sets w32-quote-process-args. This allows use of spaces in
1088 ;; paths passed as arguments to processes (e.g., javac) started by
1091 ;; Revision 1.41 2002/06/11 06:30:37 paulk
1092 ;; Provides support for paths containing spaces as beanshell vm arguments via the following change:
1093 ;; locally set the w32-quote-process-args variable to a quotation mark when launching
1094 ;; the beanshell vm process.
1096 ;; Revision 1.40 2002/02/26 04:35:27 paulk
1097 ;; * Fixes regression bug that calls bsh-eval to fail when ant-home is nil.
1098 ;; * Adds a new eieo class, bsh-compiler, intended to serve as a base class
1099 ;; for compiler-like tools (e.g., javac, ant, checkstyle) based on
1102 ;; Revision 1.39 2002/02/25 20:07:38 jslopez
1103 ;; Remove no buffering filter.
1104 ;; Add get-process method.
1106 ;; Revision 1.38 2002/02/21 12:42:33 jslopez
1107 ;; Fixes bug adding incorrect path for ant.jar in
1108 ;; the beanshell classpath.
1110 ;; Revision 1.37 2002/02/15 17:50:46 jslopez
1111 ;; Removes reference to bsh-eval-comint-filter.
1112 ;; Switch the order of the sit-for and the accept-process-output.
1114 ;; Revision 1.36 2002/02/15 02:48:20 jslopez
1115 ;; Adds a new non buffering filter, this filter is meant to be used
1116 ;; by the compile server and the ant server.
1117 ;; Modifies bsh-eval to support a new filter.
1119 ;; Revision 1.35 2001/12/09 17:17:48 jslopez
1120 ;; Replaces repetitive code with jde-get-global-classpath.
1122 ;; Revision 1.34 2001/11/05 14:18:32 jslopez
1123 ;; Modifies bsh-exit to use two different exit methods.
1124 ;; If Ant Server is enable it uses JdeUtilities.exit()
1125 ;; otherwise it uses the beanshell exit() method.
1127 ;; Revision 1.33 2001/11/05 02:13:59 jslopez
1128 ;; Modified bsh-exit to class jde.util.JdeUtilities.exit() instead
1129 ;; of using the beanshell exit method.
1131 ;; Revision 1.32 2001/10/24 05:27:56 paulk
1132 ;; Updated bsh-internal to use jde-run-get-vm (instead of the obsolete
1133 ;; jde-run-java-vm) to get the path to the vm to run the Beanshell.
1135 ;; Revision 1.31 2001/10/19 09:47:55 paulk
1136 ;; XEmacs compatibility fix: Now correctly converts new lines (^M) on Windows.
1137 ;; Thanks to Andy Piper.
1139 ;; Revision 1.30 2001/09/05 06:18:05 paulk
1140 ;; bsh-internal now uses jde-get-jdk-directory to determine the JDK directory.
1142 ;; Revision 1.29 2001/08/30 04:15:06 paulk
1143 ;; Beanshell now uses jde-jdk-directory instead of jde-bug-jdk-directory (soon to be deprecated) to find the JDK tools.jar file.
1145 ;; Revision 1.28 2001/08/30 01:34:47 paulk
1146 ;; Adds JDK tools.jar file to beanshell classpath. Needed to support compile server. Thanks to Javier Lopez.
1148 ;; Revision 1.27 2001/08/14 06:11:35 paulk
1149 ;; Add bsh-exit, bsh-open-class-browser, and bsh-open-desktop. Thanks to Javier Lopez.
1151 ;; Revision 1.26 2001/07/31 05:11:48 paulk
1154 ;; Revision 1.25 2001/06/13 03:51:44 paulk
1155 ;; Now defines bsh customization group.
1157 ;; Revision 1.24 2001/05/31 05:14:38 paulk
1158 ;; Provide support for per-project caching of class data in the Beanshell. Thanks to Matt Conway.
1160 ;; Revision 1.23 2001/05/19 02:39:18 paulk
1161 ;; Put jde-global-classpath first on the classpath to facilitate debugging of Java code run in hthe Beanshell.
1163 ;; Revision 1.22 2001/04/16 05:33:20 paulk
1164 ;; Normalized paths. Thanks to Nick Sieger.
1166 ;; Revision 1.21 2001/03/21 20:46:34 paulk
1167 ;; Updated bsh-internal to handle case where both jde-global-classpath and CLASSPATH environment variable are nil. Thanks to Toru TAKAHASHI <torutk@alles.or.jp> for reporting this bug and supply an initial version of a fix.
1169 ;; Revision 1.20 2001/03/01 05:01:28 paulk
1170 ;; Adds the customization variable bsh-startup-directory.
1172 ;; Revision 1.19 2001/02/25 04:23:12 paulk
1173 ;; Fixed bug in processing CLASSPATH environment variable.
1175 ;; Revision 1.18 2001/02/03 07:44:26 paulk
1176 ;; Now uses jde-build-classpath to build BeanShell classpath. This allows enviromnent variables in the classpath.
1178 ;; Revision 1.17 2000/10/08 12:55:38 paulk
1179 ;; *** empty log message ***
1181 ;; Revision 1.16 2000/08/10 09:09:47 paulk
1182 ;; Now handles Lisp eval errors gracefully.
1184 ;; Revision 1.15 2000/08/07 05:11:38 paulk
1185 ;; Adds bsh-vm-args variable.
1187 ;; Revision 1.14 2000/08/04 02:51:19 paulk
1188 ;; Added bsh-eval-timeout variable.
1190 ;; Revision 1.13 2000/02/16 04:39:28 paulk
1191 ;; Implemented Cygwin/XEmacs compatiblity fix provided by Fred Hart
1192 ;; <cfhart@Z-TEL.com> in bsh-internal.
1194 ;; Revision 1.12 2000/02/02 05:51:00 paulk
1195 ;; Expanded doc string.
1197 ;; Revision 1.11 2000/01/28 04:28:00 paulk
1198 ;; Fixed startup timing bug that cause commands that use the beanshell to
1199 ;; failt the first time on Unix systems.
1201 ;; Revision 1.10 2000/01/15 08:00:03 paulk
1204 ;; Revision 1.9 1999/11/01 03:13:07 paulk
1207 ;; Revision 1.8 1999/09/17 06:55:26 paulk
1208 ;; Set comint-prompt-regexp to the beanshell prompt.
1209 ;; Fixed bug where Emacs was querying user whether to kill the beanshell
1210 ;; buffer on exit from Emacs.
1212 ;; Revision 1.7 1999/01/15 22:18:41 paulk
1213 ;; Added Andy Piper's NT/XEmacs compatibility changes.
1215 ;; Revision 1.6 1998/12/13 22:10:04 paulk
1216 ;; Add check for chunked traffic between Emacs and the BeanShell.
1218 ;; Revision 1.5 1998/12/09 00:59:43 paulk
1219 ;; Added a startup message for beanshell.
1221 ;; Revision 1.4 1998/11/27 10:07:57 paulk
1222 ;; Use CLASSPATH environment variable if jde-global-classpath is nil.
1224 ;; Revision 1.3 1998/11/22 23:14:28 paulk
1225 ;; Fixed path separator bug.
1227 ;; Revision 1.2 1998/11/22 18:11:56 paulk
1228 ;; Changed path to use jde.jar.
1230 ;; Revision 1.1 1998/10/22 00:07:56 paulk
1235 ;; End of beanshell.el