Tag: clojure

After installing the CoffeeScript brush for SyntaxHighlighter Evolved, I wanted a Clojure brush. Rather than install another plugin, I created a combined plugin, with a generic name so I can add more semi-popular languages to it, called SyntaxHighlighter Evolved: Brush Pack. I’m planning on putting it in the WordPress Plugin Directory.

Not bad. The :keywords and curly braces, which aren’t a part of Common Lisp or Scheme, are neatly highlighted. The code sample above, by the way, gets the port from an environment variable, which is how Heroku makes it easy for applications on different platforms to adapt to their environment.

I’ve known of Incanter, a powerful statistics library for Clojure, for a while now. A couple of days ago I set out to get it running nicely, and was successful. Here are the steps I took:

Installing Leiningen

I used Leiningen because I knew it made packages easy to install, and while it is described more often as a build tool than as a package manager, it has a repl command, so I can use it for experimenting with libraries, too. It was at least as convenient as I expected. I’m going to continue using it to set up repl environments.

That’s it! It uses the Java installation that comes with OS X. I installed this on Snow Leopard, but these instructions should work on Leopard as well. If you replace aquamacs-emacs-starter-kit with emacs-starter-kit, all of the instructions in this blog post should work on other platforms, with some other minor changes.

Opening a Clojure repl with access to Leiningen

To start a new Leiningen project, I went to my home directory and ran:

This creates a directory called incanterplayground with project.clj and a few other files. Only project.clj will need to be modified to get an Inanter repl environment up and running. I opened project.clj and added the repository and dependency information for Incanter:

And then, following the instructions in Leiningen’s README file, I ran lein deps and lein repl in the project directory:

I wasn’t planning on using swank or nailgun. I haven’t been using emacs, and nailgun doesn’t sound very appealing to me, mainly because when I looked at using it with JRuby a year or two ago, it came with security warnings so strong that they seemed to be telling me you don’t want to use this. When I read the above message, though, I felt like I should give one of the two a try. Before I even tried importing incanter, I started working on getting emacs and lein-swank running.

Getting the lein-swank plugin running

The lein-swank plugin is a server, and therefore doesn’t require emacs to be up and running to run. So I tackled getting lein-swank running, and I put off installing SLIME, which I didn’t know how to use, until afterward. I added a dev-dependencies line to my Leiningen project file. Here is project.clj with this added:

I then ran lein deps and lein swank:

(Note: I made this tutorial while running through these steps for the second time, so lein deps is using files that have already been downloaded. The first time you run it, lein deps will show the download and installation progress.)

This indicates that the swank server is running, and is ready to be connected to by emacs.

Getting emacs running and installing packages with ELPA

I used to use emacs, and I had installed Carbon Emacs to try out org-mode. I didn’t check to see if ELPA was already installed, but rather, ran the directions on the ELPA website. This involved taking a piece of code and evaluating it. Then I went to the ELPA package manager, selected the packages listed in the lein-swank installation instructions, and ran the install. I saw a lot of warnings go by.

When I got done, I restarted emacs, and tried running M-x slime-connect. The command, however, wasn’t installed. I searched for help, and found a post from a user who had the same problem. Someone replied to the post saying that he probably had a bad installation of ELPA. At this point I punted and decided to try aquamacs instead. After installing aquamacs, and running it, I got the idea to find an emacs profile to use that would hopefully have ELPA already working. I searched for an aquamacs emacs profile and found aquamacs-emacs-starter-kit.

I downloaded it to my github directory, rather than to my Emacs preferences directory.

I then added these three lines to the end of ~/Library/Preferences/Aquamacs Emacs/Preferences.el (which just had comments in it):

After this, I restarted Aquamacs, saw a ton of warnings fly by, waited it for it to finish what it was doing, and restarted Aquamacs again. I saw more warnings fly by, and restarted Aquamacs again. I may have restarted Aquamacs a third time before it stopped showing a lot of warnings. It takes a little time for aquamacs-emacs-starter-kit to work its magic.

Then I installed the required packages by typing M-x package-list-packages, selecting the packages that I needed or wanted that weren’t already installed, and typing x to install them. I made sure that clojure, clojure-test, paredit, slime, slime-repl, and swank-clojure were selected before hitting x. After pressing x, I waited while some warnings flew by, and restarted Aquamacs again. Then I tried typing M-x slime and then pressing tab to see what the autocompletion showed. There were a bunch of results, so I felt confident that I had successfully installed SLIME. Indeed I had.

Running SLIME

Finally, I ran SLIME. I still had lein-swank running from a previous step, so I typed M-x slime-connect and chose the default host and port. After a second or two, I saw a silly message indicating that it worked.

I poked around the commands, first using tab completion on M-x, and then looking at the the keyboard bindings in the project.clj buffer with C-h b. I opened a new file with C-x C-f playground.clj, and started evaluating expressions in there. You need to be in a window that’s in the slime mode; the *scratch* buffer will evaluate with emacs lisp if you type C-x C-e in it.

Here’s an example session:

If you go to the end of the first line and type C-x C-e, SLIME should print out #’user/x in the status line. If it works, you know it’s evaluating Clojure and not emacs lisp. Go to the end of the second line and type C-x C-e, and then to the end of the last line and type C-x C-e. It prints out 6. This way I know that it’s running them in the same Clojure session. Pretty cool, huh?

Printing out a graph with Incanter

I found this introductory incanter code, pasted it into playground.clj, and started evaluating it.

After I ran C-x C-e on the first line, a new dock icon with a Java logo appeared. I switched back to Aquamacs, went to the end of the second line, and typed C-x C-e again. Three lines showed up in the emacs status bar, describing an object that was returned. I clicked the new dock icon, and voila! A graph showed up.