Commits

Meikel Brandmeyer committed 62b0d51

Rework handling of Repl binding

Also add *unchecked-math* for 1.3

Comments (0)

Files changed (1)

server/src/main/clojure/vimclojure/repl.clj

   (: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 add-binding
+  [bindings sym]
+  (if-let [v (resolve sym)]
+    (assoc bindings v (safe-var-get v))
+    bindings))
+
+(def bindable-vars
+  `[*warn-on-reflection* *print-meta* *print-length*
+    *print-level* *compile-path* *command-line-args*
+    *unchecked-math* *1 *2 *3 *e
+    ; VimClojure specific.
+    *print-pretty*])
 
 (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)))
+   {:id        id
+    :ns        namespace
+    :test-out  nil
+    :line      1
+    :bindings  (-> (reduce add-binding {} bindable-vars)
+                 (assoc #'*compile-path* (System/getProperty
+                                           "clojure.compile.path"
+                                           "classes")))}))
 
 (defn start
   "Start a new Repl and register it in the system."
                    (the-repl :line)
                    line)]
     (with-bindings
-      {Compiler/LINE          (Integer. (.intValue line)) ; #64: Unbox to ensure int.
-       Compiler/SOURCE        (.getName (java.io.File. file))
-       Compiler/SOURCE_PATH   file
-       #'*in*                 (make-reader *in* line)
-       #'*ns*                 (if nspace nspace (the-repl :ns))
-       #'*warn-on-reflection* (the-repl :warn-on-reflection)
-       #'*print-meta*         (the-repl :print-meta)
-       #'*print-length*       (the-repl :print-length)
-       #'*print-level*        (the-repl :print-level)
-       #'*compile-path*       (the-repl :compile-path)
-       #'*command-line-args*  (the-repl :command-line-args)
-       #'*1                   (the-repl :expr1)
-       #'*2                   (the-repl :expr2)
-       #'*3                   (the-repl :expr3)
-       #'*e                   (the-repl :exception)
-       #'vimclojure.repl/*print-pretty* (the-repl :print-pretty)
-       #'clojure.test/*test-out* (if-let [test-out (the-repl :test-out)]
-                                   test-out
-                                   *out*)}
+      (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
-                   (struct-map
-                     repl
-                     :id                 id
-                     :ns                 *ns*
-                     :warn-on-reflection *warn-on-reflection*
-                     :print-meta         *print-meta*
-                     :print-length       *print-length*
-                     :print-level        *print-level*
-                     :compile-path       *compile-path*
-                     :command-line-args  *command-line-args*
-                     :expr1              *1
-                     :expr2              *2
-                     :expr3              *3
-                     :exception          *e
-                     :print-pretty       vimclojure.repl/*print-pretty*
-                     :test-out           (let [test-out clojure.test/*test-out*]
-                                           (when-not (identical? test-out *out*)
-                                             test-out))
-                     :line               (dec (.getLineNumber *in*))))))))))
+                   {: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