       ))
   ;; Set the default shell if not already set
   (when (null py-which-shell)
-    (py-toggle-shells py-default-interpreter))
-  )
+    (py-toggle-shells (py-choose-shell))))
+
+
+(defun jpython-mode ()
+  "Major mode for editing JPython/Jython files.
+This is a simple wrapper around `python-mode'.
+It runs `jpython-mode-hook' then calls `python-mode.'
+It is added to `interpreter-mode-alist' and `py-choose-shell'.
+"
+  (interactive)
+  (python-mode)
+  (py-toggle-shells 'jpython)
+  (when jpython-mode-hook
+      (run-hooks 'jpython-mode-hook)))
+
+
+;; It's handy to add recognition of Python files to the
+;; interpreter-mode-alist and to auto-mode-alist.  With the former, we
+;; can specify different `derived-modes' based on the #! line, but
+;; with the latter, we can't.  So we just won't add them if they're
+;; already added.
+(let ((modes '(("jpython" . jpython-mode)
+	       ("jython" . jpython-mode)
+	       ("python" . python-mode))))
+  (while modes
+    (when (not (assoc (car modes) interpreter-mode-alist))
+      (push modes interpreter-mode-alist))
+    (setq modes (cdr modes))))
+
+(when (not (or (rassq 'python-mode auto-mode-alist)
+	       (rassq 'jpython-mode auto-mode-alist)))
+  (push '("\\.py$" . python-mode) auto-mode-alist))
+
 
  

	(insert-buffer-substring cur start end)
	;; Set the shell either to the #! line command, or to the
	;; py-which-shell buffer local variable.
	(goto-char (point-min))
	(if (looking-at "^#!\\s *\\(.*\\)$")
	    (setq shell (match-string 1))
	  ;; No useable #! line
	  (setq shell py-which-shell))))
    (cond
     ;; always run the code in its own asynchronous subprocess
     (async

				    (point-max) 'move))))
    (nreverse index-alist)))



(defun py-choose-shell-by-shebang ()
  "Choose CPython or JPython mode by looking at #! on the first line.
Returns the appropriate mode function.
Used by `py-choose-shell', and similar to but distinct from
`set-auto-mode', though it uses `auto-mode-interpreter-regexp' (if available)."
  ;; look for an interpreter specified in the first line
  ;; similar to set-auto-mode (files.el)
  (let* ((re (if (boundp 'auto-mode-interpreter-regexp)
		 auto-mode-interpreter-regexp
	       ;; stolen from Emacs 21.2
	       "#![ \t]?\\([^ \t\n]*/bin/env[ \t]\\)?\\([^ \t\n]+\\)"))
	 (interpreter (save-excursion
			(goto-char (point-min))
			(if (looking-at re)
			    (match-string 2)
			  "")))
	 elt)
    ;; Map interpreter name to a mode.
    (setq elt (assoc (file-name-nondirectory interpreter)
		     py-shell-alist))
    (and elt (caddr elt))))



(defun py-choose-shell-by-import ()
  "Choose CPython or JPython mode based imports.
If a file imports any packages in `py-jpython-packages', within
`py-import-check-point-max' characters from the start of the file,
return `jpython', otherwise return nil."
  (let (mode)
    (save-excursion
      (goto-char (point-min))
      (while (and (not mode)
		  (search-forward-regexp
		   "^\\(\\(from\\)\\|\\(import\\)\\) \\([^ \t\n.]+\\)"
		   py-import-check-point-max t))
	(setq mode (and (member (match-string 4) py-jpython-packages)
			'jpython
			))))
    mode))


(defun py-choose-shell ()
  "Choose CPython or JPython mode. Returns the appropriate mode function.
This does the following:
 - look for an interpreter with `py-choose-shell-by-shebang'
 - examine imports using `py-choose-shell-by-import'
 - default to the variable `py-default-interpreter'"
  (interactive)
  (or (py-choose-shell-by-shebang)
      (py-choose-shell-by-import)
      py-default-interpreter
;      'cpython ;; don't use to py-default-interpreter, because default
;               ;; is only way to choose CPython
      ))


;;;###autoload
