 	    ))
       (set-buffer curbuf))))
 
-(defun py-execute-buffer ()
++
+;;; Subprocess commands
+
+;;;###autoload
+(defun py-shell ()
+  "Start an interactive Python interpreter in another window.
+This is like Shell mode, except that Python is running in the window
+instead of a shell.  See the `Interactive Shell' and `Shell Mode'
+sections of the Emacs manual for details, especially for the key
+bindings active in the `*Python*' buffer.
+
+See the docs for variable `py-scroll-buffer' for info on scrolling
+behavior in the process window.
+
+Warning: Don't use an interactive Python if you change sys.ps1 or
+sys.ps2 from their default values, or if you're running code that
+prints `>>> ' or `... ' at the start of a line.  `python-mode' can't
+distinguish your output from Python's output, and assumes that `>>> '
+at the start of a line is a prompt from Python.  Similarly, the Emacs
+Shell mode code assumes that both `>>> ' and `... ' at the start of a
+line are Python prompts.  Bad things can happen if you fool either
+mode.
+
+Warning:  If you do any editing *in* the process buffer *while* the
+buffer is accepting output from Python, do NOT attempt to `undo' the
+changes.  Some of the output (nowhere near the parts you changed!) may
+be lost if you do.  This appears to be an Emacs bug, an unfortunate
+interaction between undo and process filters; the same problem exists in
+non-Python process buffers using the default (Emacs-supplied) process
+filter."
+  ;; BAW - should undo be disabled in the python process buffer, if
+  ;; this bug still exists?
+  (interactive)
+  (require 'comint)
+  (switch-to-buffer-other-window (make-comint "Python" py-python-command "-i"))
+  (make-local-variable 'comint-prompt-regexp)
+  (setq comint-prompt-regexp "^>>> \\|^[.][.][.] ")
+  (set-process-filter (get-buffer-process (current-buffer)) 'py-process-filter)
+  (set-syntax-table py-mode-syntax-table)
+  (local-set-key [tab] 'self-insert-command))
+
++
+(defun py-clear-queue ()
+  "Clear the queue of temporary files waiting to execute."
+  (interactive)
+  (let ((n (length py-file-queue)))
+    (mapcar 'delete-file py-file-queue)
+    (setq py-file-queue nil)
+    (message "%d pending files de-queued." n)))
+
+(defun py-execute-region (start end &optional async)
+  "Execute the the region in a Python interpreter.
+The region is first copied into a temporary file (in the directory
+`py-temp-directory').  If there is no Python interpreter shell
+running, this file is executed synchronously using
+`shell-command-on-region'.  If the program is long running, use an
+optional \\[universal-argument] to run the command asynchronously in
+its own buffer.
+
+If the Python interpreter shell is running, the region is execfile()'d
+in that shell.  If you try to execute regions too quickly,
+`python-mode' will queue them up and execute them one at a time when
+it sees a `>>> ' prompt from Python.  Each time this happens, the
+process buffer is popped into a window (if it's not already in some
+window) so you can see it, and a comment of the form
+
+    \t## working on region in file <name>...
+
+is inserted at the end.  See also the command `py-clear-queue'."
+  (interactive "r\nP")
+  (or (< start end)
+      (error "Region is empty"))
+  (let* ((proc (get-process "Python"))
+	 (temp (make-temp-name "python"))
+	 (file (concat (file-name-as-directory py-temp-directory) temp))
+	 (outbuf "*Python Output*"))
+    (write-region start end file nil 'nomsg)
+    (cond
+     ;; always run the code in it's own asynchronous subprocess
+     (async
+      (let* ((buf (generate-new-buffer-name "*Python Output*")))
+	(start-process "Python" buf py-python-command "-u" file)
+	(pop-to-buffer buf)
+	))
+     ;; if the Python interpreter shell is running, queue it up for
+     ;; execution there.
+     (proc
+      ;; use the existing python shell
+      (if (not py-file-queue)
+	  (py-execute-file proc file)
+	(push file py-file-queue)
+	(message "File %s queued for execution" file))
+      )
+     (t
+      ;; otherwise either run it synchronously in a subprocess
+      (shell-command-on-region start end py-python-command outbuf)
+      ))))
+
+;; Code execution command
+(defun py-execute-buffer (&optional async)
   "Send the contents of the buffer to a Python interpreter.
 If there is a *Python* process buffer it is used.  If a clipping
 restriction is in effect, only the accessible portion of the buffer is
 sent.  A trailing newline will be supplied if needed.
 
 See the `\\[py-execute-region]' docs for an account of some subtleties."
-  (interactive)
-  (py-execute-region (point-min) (point-max)))
-
+  (interactive "P")
+  (py-execute-region (point-min) (point-max) async))
 
  


;; Author: 1995-1997 Barry A. Warsaw
;;         1992-1994 Tim Peters
;; Maintainer:    python-mode@python.org
;; Created:       Feb 1992
;; Version:       3.0
;; Keywords: python languages oop

(defconst py-version "3.0"
  "`python-mode' version number.")

  (py-safe (while py-file-queue
	     (py-delete-file-silently (car py-file-queue))
	     (setq py-file-queue (cdr py-file-queue)))))

(defun py-current-defun ()
  ;; tell add-log.el how to find the current function/method/variable
