diff options
-rw-r--r-- | src/clj/clojure/repl.clj | 20 |
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))))))) |