Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
86 lines (76 sloc) 1.9 KB
(ns ajax-repl
(:use compojure)
(:use [clojure.main :only (repl-exception)]))
(def repl-scripts
(include-js "/js/jquery.js"
(def try-ruby
(link-to "" "Try Ruby!"))
(def credits
[:p "With javascript based on _why's: " try-ruby])
(defn show-repl
"HTML for the REPL index page"
[:title "Clojure REPL"]
(include-css "css/site.css")
[:h1 "Clojure REPL"]
(defn repl-eval
"Evaluate an expression, but catch and print errors and set *1 etc. vars."
(let [value (eval expr)]
(print value)
(set! *3 *2)
(set! *2 *1)
(set! *1 value))
(catch Throwable e
(println (repl-exception e))
(set! *e e))))
(defmacro capture-vars
"Given a list of var names, return a map of name/value pairs."
[& names]
(fn [m n] (assoc m `'~n n))
(defn eval-with-session
"Evaluate a command, keeping useful REPL state in the session."
[session expr]
(binding [*ns* *ns*
*1 nil
*2 nil
*3 nil
*e nil]
(in-ns (@session :ns 'user))
(doseq [[name value] (@session :vars)]
(var-set (eval `(var ~name)) value))
(repl-eval expr)
(alter session merge
{:ns (symbol (str *ns*))
:vars (capture-vars *e *1 *2 *3)}))))
(defn eval-repl-cmd
"Evaluate a command and return the result."
[session cmd]
(eval-with-session session (read-string cmd))))
(defservlet repl-servlet
(GET "/"
(POST "/repl"
(eval-repl-cmd session (params :cmd)))
(GET "/*"
(or (serve-file (route :*))
(ANY "*"