Tuesday, October 18, 2011

Using emacs and leiningen on windows for clojure: pt. 1

I have three books on clojure currently, Stuart Halloway's Programming Clojure (1st edition), Amit Rathore's Clojure in Action, and Michael Fogus and Chris Houser's The Joy of Clojure.  Unfortunately, none of these books really tell you how to get started.  They talk about the language of course, but setting up your development environment is not really covered in any of these books.

There is the pretty good reference site on getting started with any of the major IDE's available for clojure.  I decided I would learn emacs, since it's something I've been meaning to do for a long time.  I have used Eclipse counter clockwise plugin, but for some reason, I just didn't really feel like using it.  Likewise, I didn't like how Enclojure forced you to use maven for your clojure project.  I didn't play with Idea's La Clojure plugin, so it may be worth investigating.  And for quick down and dirty experimenting, the clojure shell for jEdit really isn't that bad.

But as I mentioned, I wanted to use emacs.  Because I have a brand new motherboard that uses UEFI, I have had problems finding a linux distro I can install on it (I tried Mint and Sabayon, and neither of them liked my new hardware).  So I decided to run emacs on Windows.  Also, I might be able to help people who are also using emacs on windows, and run into trouble when the documentation clearly only gives instructions for linux users.


Before I go too much further into emacs, let's install a different piece of software for your clojure development which we will use with emacs:  leiningen.

Setting up leiningen

Leiningen is a project management tool for clojure, much like Maven is for java (at least until polyglot maven comes out).  Indeed, leiningen uses some maven for dependency management behind the scenes.  But instead of using XML as the declarative language for your project, you use clojure itself!  Leiningen offers many nifty features, including but not limited to:

  1. Dependency Management
  2. Can perform AOT compiling if indicated
  3. Can launch a REPL
  4. Can start a swank session

For now though, let's just install leiningen.  It's a little easier for linux users since every distribution I know of comes with either wget or curl.  For windows, you'll need to grab one of them.  I used curl myself.  Both are just .exe files, and they don't have or require installers.  Just make sure you put the .exe file somewhere in the system's %PATH%

Once you have wget or curl in %PATH%, you can install leiningen.  Grab the stable leiningen for windows, and put the  lein.bat file somewhere in %PATH% again.  Once this is done, run the following command in a console:

lein self-install

If you are behind a proxy, make sure you read the directions on the leiningen site about how to specify your proxy.  Otherwise, we are done for now.



Setting up Emacs

So the first step is to grab the latest version of emacs for windows, which you can get here:


I used the Sep. 3rd version at the time of this writing, so it should work.  Once you downloaded the zip file, unzip it.  If you want to add emacs to your start menu, go to the \bin folder, and click on the addpm icon.  This will create a link in your Start menu for you.  Check to see if you have the following folder:

C:\Users\your_user_name\AppData\Roaming\.emacs.d

If not, create it.  Now open up emacs itself.  In emacs, opening a file is called "visiting" a file, and you can do so  by typing C-x C-f.  That means type the control key and x key at the same time, then the Control key and the f key at the same time.  At the bottom, you will see a little path.  Go to your .emacs.d folder, and at the end, type in:  init.el

 Mine looks like this:


As you are typing, you can hit the Tab key to auto-complete.  Once you have the path, hit enter.  This will create a new buffer for you that you can now type in. Now I recommend that you install the emacs starter kit.  This creates some easy defaults for your emacs environment.  It has become really easy to do with the 24 version of emacs.  Copy this, and paste it into the emacs buffer you have.


(require 'package)
(add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/") t)
(package-initialize)

To paste in emacs, you use the C-y key combination.  Emacs has something called the kill-buffer which is the equivalent of a clipboard history.  Once you hit C-y once, you can use the M-y command to cycle through your next most recent cut/copy item from the buffer.

Once you have the above command pasted into the buffer, use the C-x C-s command to save the file.  You can also try to evaluate the code snippet above by pasting it into the *scratch* buffer, though I had problems with this.  You can cycle through the buffers either by clicking on the grey bar at the bottom (in the screenshot above, where it says *GNU Emacs*), in the menu bar, you can get a list of all the buffers by click on the buffers entry, or you can use a command like this:

M-x buffer-menu

The M is the meta key, and on most systems is your Alt key.  So the above says, "type Alt and x at the same time, then enter the command buffer-menu, and hit enter".  The M-x command is quite common, so get used to it.  If you are wondering where you type this in, it goes in that very bottom row...the same one you used to type in the path to init.el.  This region is called the minibuffer, and it's where a lot of your interactive work goes.  If you decide to cancel a command, you can do so be entering C-g.

What I recommend you do now is to close emacs, and start it up again.  As I understand it, init.el has initialization code for emacs, and so when emacs restarts, it will call the code that was pasted in above.  What that code does is add a package repository for some other emacs goodies that we need to install.

First, we need to update a list of the available packages to the most recent version.  You can do this with the  M-x package-refresh-contents command.  The next thing we'll need to do is actually display the packages you can choose from.  This is accomplished with the M-x package-list-packages command.  There are two ways you can install packages.  The first is to use your arrow keys (or if you are in adventuresome mood, enter the C-h help tutorial command, and look at the key combinations to move the cursor around), and at the very left most edge, you can install a package by hitting the 'i' key when you are at the selected row.



I'll show you how to change the theme color later :)

Once you have selected i to install the packages you want (or u to unselect a previously selected one), then you can use the command M-x menu-package-execute to perform the selected action.  You'll be prompted if you really want to install or not.

For this series of blog posts, you'll want to install at a minimum these packages:

clojure-mode
elein
slime
slime-repl

And that's it for installing emacs.  Come back for the next installment on how to set up a simple leiningen project, perform leiningen tasks from within emacs, and setting up a swank server to serve up your clojure REPL :)



1 comment:

  1. hi, nice post. I'm also just getting started with Clojure, and haven't been using Emacs very long.

    If you make changes to your customization file (init.el is one of them) you can reload it without restarting Emacs. Just M-x load-file and navigate to the file you want to reload.

    ReplyDelete