summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChouser <chouser@n01se.net>2010-10-27 23:08:04 -0400
committerStuart Halloway <stu@thinkrelevance.com>2010-11-05 08:42:47 -0700
commiteef9686ddc1ceb412c6c9890a912f8e67f27ab5d (patch)
tree5c27bfe16cbdc296596f43d561a2d7fbae1df93e
parent0898ad3ea4e4f051ae03c9e6a87a8cda3f9fa3de (diff)
Add set-break-handler! and thread-stopper, refs CLJ-460
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
-rw-r--r--src/clj/clojure/repl.clj20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/clj/clojure/repl.clj b/src/clj/clojure/repl.clj
index 77ef9dad..8b44d05b 100644
--- a/src/clj/clojure/repl.clj
+++ b/src/clj/clojure/repl.clj
@@ -151,3 +151,23 @@ str-or-pattern."
(+ 2 (- (count (.getStackTrace cause))
(count st))))))))))
+;; ----------------------------------------------------------------------
+;; Handle Ctrl-C keystrokes
+
+(defn thread-stopper
+ "Returns a function that takes one arg and uses that as an exception message
+ to stop the given thread. Defaults to the current thread"
+ ([] (thread-stopper (Thread/currentThread)))
+ ([thread] (fn [msg] (.stop thread (Error. msg)))))
+
+(defn set-break-handler!
+ "Register INT signal handler. After calling this, Ctrl-C will cause
+ the given function f to be called with a single argument, the signal.
+ Uses thread-stopper if no function given."
+ ([] (set-break-handler! (thread-stopper)))
+ ([f]
+ (sun.misc.Signal/handle
+ (sun.misc.Signal. "INT")
+ (proxy [sun.misc.SignalHandler] []
+ (handle [signal]
+ (f (str "-- caught signal " signal)))))))