   (:require
     clojure.test)
   (:use
-     [vimclojure.util :only (stream->seq pretty-print pretty-print-causetrace)])
+    [vimclojure.util :only [safe-var-get stream->seq
+                            pretty-print pretty-print-causetrace]])
   (:import
-     (clojure.lang Var Compiler LineNumberingPushbackReader)))
+    clojure.lang.Var
+    clojure.lang.Compiler
+    clojure.lang.LineNumberingPushbackReader))
 
 (def
   ^{:dynamic true :doc

  *print-pretty*
  false)

(defstruct
  #^{:doc
  "The structure for the Repl interface. Holds the state of a Repl between
  invokations. The members correspond to the Vars as bound be with-binding."}
  repl
  :id :ns :warn-on-reflection :print-meta :print-length :print-level
  :compile-path :command-line-args :expr1 :expr2 :expr3 :exception :test-out
  :line)

(defn make-repl
  "Create a new Repl."
  ([id] (make-repl id (the-ns 'user)))
  ([id namespace]
   (struct-map repl
               :id                 id
               :ns                 namespace
               :warn-on-reflection *warn-on-reflection*
               :print-meta         *print-meta*
               :print-length       *print-length*
               :print-level        *print-level*
               :compile-path       (System/getProperty
                                     "clojure.compile.path"
                                     "classes")
               :command-line-args  nil
               :expr1              nil
               :expr2              nil
               :expr3              nil
               :exception          nil
               :print-pretty       vimclojure.repl/*print-pretty*
               :test-out           nil
               :line               0)))

(defn start
  "Start a new Repl and register it in the system."

                   (the-repl :line)
                   line)]
    (with-bindings
      (merge (:bindings the-repl)
             ; #64: Unbox to ensure int.
             {Compiler/LINE        (Integer. (.intValue line))
              Compiler/SOURCE      (.getName (java.io.File. file))
              Compiler/SOURCE_PATH file
              #'*in*               (make-reader *in* line)
              #'*ns*               (if nspace nspace (:ns the-repl))
              #'clojure.test/*test-out* (if-let [test-out (the-repl :test-out)]
                                          test-out
                                          *out*)})
      (try
        (thunk)
        (finally
          (when (not= id -1)
            (swap! *repls* assoc id
                   {:id        id
                    :ns        *ns*
                    :test-out  (let [test-out clojure.test/*test-out*]
                                 (when-not (identical? test-out *out*)
                                   test-out))
                    :line      (dec (.getLineNumber *in*))
                    :bindings  (reduce add-binding {} bindable-vars)})))))))

(defmacro with-repl
  "Executes body in the context of the Repl with the given id. id may be -1
