Permalink
Browse files

Merge pull request #128 from blais/master

Added ClojureScript support.
  • Loading branch information...
2 parents 08972e8 + 41f39e5 commit 57844d3953538a02b6040ed8af7a1d486183194b @technomancy technomancy committed May 25, 2012
Showing with 55 additions and 1 deletion.
  1. +8 −1 src/swank/commands/basic.clj
  2. +47 −0 src/swank/commands/cljs.clj
@@ -72,7 +72,14 @@
(defslimefn interactive-eval [string]
(with-emacs-package
- (pr-str (first (eval-region string)))))
+ (pr-str
+ (let [value (first (eval-region string))]
+ ;; If the result is a seq, consume it here instead of getting evaluated
+ ;; from pr-str to allow side-effects to go to the repl.
+ (if (instance? clojure.lang.LazySeq value)
+ (doall value)
+ value)
+ ))))
(defslimefn listener-eval [form]
(with-emacs-package
@@ -0,0 +1,47 @@
+(ns ^{:doc "Support for sending evaluation of forms into a ClojureScript repl."}
+ swank.commands.cljs
+ (:use [swank.core :only (with-emacs-package)]
+ [swank.commands :onlny (defslimefn)])
+ (:require [cljs.repl :as repl]
+ [cljs.compiler :as comp])
+ )
+
+(def cljs-targets "A mapping of registered repl environments which can be used as targets." (atom {}))
+
+(defn register-repl
+ "Register a new REPL environment for interactive-eval-with-target to dispatch to."
+ [key env]
+ (swap! cljs-targets assoc key env))
+
+(defn eval-in-cljs
+ "Evaluate the given string in the provided ClojureScript repl environment."
+ [env form-string]
+ (binding [comp/*cljs-ns* comp/*cljs-ns*]
+ (let [form (read-string form-string),
+ ;; Note: the following is lifted from cljs.repl.browser; FIXME: we
+ ;; should add support there to do this without a repl thread.
+ context {:context :statement
+ :locals {}
+ :ns (@comp/namespaces comp/*cljs-ns*)}]
+
+ (repl/evaluate-form env context "<swank-cljs-repl>" form)
+ )))
+
+(defslimefn ^{:doc "Evaluate a Clojure form in a ClojureScript environment."}
+ interactive-eval-with-target [target form-string]
+ (let [env (get @cljs-targets target)]
+ (if env
+ (eval-in-cljs env form-string)
+ (throw (Exception.
+ (format "Emacs eval abort; eval target '%s' not found" target)))
+ )))
+
+
+;; Notes:
+;;
+;; You will need an Emacs customization that overrides
+;; slime-interactive-eval tocall (swank:interactive-eval-with-target) instead of
+;; (swank:interactive-eval), such as is provided in clojure-mode.el.
+;;
+;; Also, before you can eval to a target, you will need your VM to have a repl
+;; instance registered via 'register-repl' (e.g. browser repl).

0 comments on commit 57844d3

Please sign in to comment.