Friday, February 3, 2012

Using emacs and leiningen on Windows for clojure pt. 3: Getting SLIME'd

jinspector.clj In the last post, I made a booboo when I made the dependencies for our project.  The logback groupID is actually ch.qos.logback, not just qos.logback.  So make sure you change your project.clj file accordingly.


(defproject MyFirstCljProject "1.0.0-SNAPSHOT"
  :description "FIXME: write description"
  :dependencies [[org.clojure/clojure "1.3.0"]
                 [org.jboss.netty/netty "3.2.6.Final"]
                 [ch.qos.logback/logback-core "0.9.30"]
                 [ch.qos.logback/logback-classic "0.9.30"]
                 [org.slf4j/slf4j-api "1.6.3"]])


Let's add our first source file.  Notice in your MyFirstCljProject, there's a src directory, and under that, there is a MyFirstCljProject directory.  Leiningen is kind of like Maven in that the src directory is the root folder for your package.  Leiningen, by default through the new command, created your top level MyFirstCljProject directory.  This is the first level element to your clojure package.  Still confused?

Let's say you wanted to create a namespace like:

MyFirstCljProject.jinspector

That means you would find a  ../src/MyFirstCljProject/jinspector.clj file.  As another example, imagine you wanted a namespace of tools.networking.netty-client.  When you have - in the namespace, you MUST have an underscore in the actual name of the file.  So you would have a folder path of:

../src/tools/networking/netty_client.clj.

So now that we've got some namespacing under our belt, let's actually write that file.  Let's create our own version of a Java class inspector.  There is a clojure-contrib.repl-utils which does what I'll do here, but this is just for illustrative purposes.  Open a file in emacs by using C-x C-f, and opening the C:/users/your_name/MyFirstCljProject/src/MyFirstCljProject/jinspector.clj


(ns MyFirstCljProject.jinspector)


(defn inspect
  [ x ]
  (let [cl (class? x) x (.getClass x)
        fields (.getFields cl)
        pfields (.getDeclaredFields cl)
        methods (.getMethods cl)
        pmethods (.getDeclaredMethods cl)
        p (.getSuperclass cl) ]
    (doseq [ x (concat fields members) ]
      (println x))))

Now that we have our method, let's actually try and use it...from SLIME!  The first thing we have to do is actually start our project.  Now, I've had some trouble using the swank-clojure command, 'clojure-jack-in'.  This command is supposed to allow you to automatically connect to a leiningen project with a slime interface.  Unfortunately, I haven't been able to get it to work.

However, you can start it manually.  While your jinspector.clj buffer is the active one, do a M-x elein-run-task, and when it asks you for the task, enter 'swank'.   Once you do that, you should have something like this:


Notice how the new buffer says "Connection opened on localhost port 4005"?  That's our queue that we can use slime to connect to the swank-clojure plugin.  You can do that with the M-x slime-connect command.  It will ask you for an IP address (use the default 127.0.0.1...that's your local machine), and also hit enter again to use the default port of 4005.  Once you do that, it should look like this:


Now we can begin playing with the clojure REPL!!


1 comment:

  1. clojure-jack-in had a problem on Windows: it required a sh shell in the environment path. Starting from Oct 31, 2011 this should be fixed. For a concise step by step setup check:

    http://sourceforge.net/apps/wordpress/codesounding/2011/09/29/installing-emacs-24-and-clojure-mode-on-windows-7-step-by-step/

    ReplyDelete