Created
March 11, 2009 12:16
-
-
Save olaugh/77438 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import '(org.eclipse.swt SWT)) | |
(import '(org.eclipse.swt.widgets Display Shell Widget)) | |
(import '(org.eclipse.swt.graphics Font)) | |
(import '(org.eclipse.swt.events ControlAdapter)) | |
(import '(org.eclipse.swt.custom StyleRange StyledText VerifyKeyListener)) | |
(def *display* (new Display)) | |
(def *shell* (new Shell *display*)) | |
(def *repl* (new StyledText *shell* SWT/BORDER)) | |
(def +prompt+ "REPL> ") | |
(def +backspace+ 8) | |
(def +enter+ 13) | |
(defn append-to-repl-in-style [text {:keys [fg style] :or {style SWT/NORMAL}}] | |
(let [start (.getCharCount *repl*) | |
length (.length text) | |
style-range (new StyleRange start length fg nil style)] | |
(.append *repl* text) | |
(.setStyleRange *repl* style-range) | |
(.setCaretOffset *repl* (.getCharCount *repl*)))) | |
(defmacro color [name] | |
(let [long-name# (str "SWT/COLOR_" (.toUpperCase name))] | |
`(.getSystemColor *display* ~(symbol long-name#)))) | |
(defn append-repl-prompt [] | |
(append-to-repl-in-style +prompt+ {:fg (color "blue") :style SWT/BOLD})) | |
(defn append-repl-result [text] | |
(append-to-repl-in-style (str "\n" text "\n") {:fg (color "red")})) | |
(defn verify-key [event] | |
(let [key (.keyCode event) | |
line-number (dec (.getLineCount *repl*)) | |
offset (.getOffsetAtLine *repl* line-number) | |
length (- (.getCharCount *repl*) offset)] | |
(cond (= key +backspace+) (when (>= (.length +prompt+) length) | |
(set! (.doit event) false)) | |
(= key +enter+) (let [line (.getText *repl* | |
(+ offset (.length +prompt+)) | |
(dec (.getCharCount *repl*))) | |
result (str (try (load-string line) | |
(catch Exception e e)))] | |
(set! (.doit event) false) | |
(append-repl-result result) | |
(append-repl-prompt))))) | |
(defn repl-key-listener [] | |
(proxy [VerifyKeyListener] [] | |
(verifyKey [event] (verify-key event)))) | |
(defn shell-resized [event] | |
(.setSize *repl* (.getSize *shell*))) | |
(defn shell-listener [] | |
(proxy [ControlAdapter] [] | |
(controlResized [event] (shell-resized event)))) | |
(doto *repl* | |
(.setSize (.getSize *shell*)) | |
(.setFont (new Font *display* "bitstream vera sans mono" 12 SWT/NORMAL)) | |
(.addVerifyKeyListener (repl-key-listener))) | |
(append-repl-prompt) | |
(doto *shell* | |
(.pack) | |
(.open) | |
(.setText "REPL Test") | |
(.addControlListener (shell-listener))) | |
(loop [] | |
(if (.isDisposed *shell*) | |
(.dispose *display*) | |
(do | |
(when (not (.readAndDispatch *display*)) | |
(.sleep *display*)) | |
(recur)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment