Skip to main content

The Easy Way to Set up Emacs for Clojure Development on OS X

I had mixed results following The clojure-doc tutorial for Emacs, and found I had to do a few modifications. Following the recipe is easy:

I was not able to easily get Aquamacs to work. Frankly, I lost interest after the initial try, and just went with the emacs build for OSX. I might mess with Aquamacs at some future point, but for now, let's just stick with Emacs proper.



Install Emacs from brew

First, set up Emacs from brew:


$ brew install emacs --cocoa --srgb
$ brew linkapps Emacs



NOTE If you already have Emacs in your Applications folder, the second brew command will fail. To make sure we're on the same page, just rename Emacs.app to something else, like EmacsCocoa.app or whatever, then re-run the second brew command to link the Emacs distribution you just installed with brew to your Applications folder. After it successfully runs, you'll have a link in your Applications folder called, "Emacs". Clicking it will open the GUI version of Emacs.



Add / Modify init.el

Next: Check to see if you have: ~/.emacs.d/init.el. If you do, open it. If you don't, create it (directory too, if it is't in your home directory (mind the leading dot..). Inside the file init.el enter, or add the following:


(require 'package)
(add-to-list 'package-archives
             '("melpa-stable" . "http://melpa-stable.milkbox.net/packages/"))

(package-initialize)

(defvar my-packages '(better-defaults
                      projectile
                      clojure-mode
                      cider))

(dolist (p my-packages)
  (unless (package-installed-p p)
    (package-install p)))

Essentially, what this does is first add an additional package repository, then install the packages for using Clojure, if they aren't already installed. If you happen to be editing the file in Emacs, after you've got it entered, evaluate the buffer:


M-x eval-buffer
Remembering, of course that on the Mac, the Meta-key is OPTION.



Create a Basic Leiningen Project to test with

With this done, you'll need a basic project to test with. The one they use is as good as anything else, so, using leiningen:


$ lein new command-line-args
$ cd command-line-args



Modify project.clj

This should create something like the following project.clj file:


(defproject command-line-args "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.6.0"]])
Note the last line with the keyword :dependencies. We need to add a line below it, so modify project.clj to the following:

(defproject command-line-args "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.6.0"]]
  :profiles {:dev {:plugins [[cider/cider-nrepl "0.8.1"]]}})
I found that clojure-doc was a bit behind the times, showing a cider-nrepl as 0.7.0. While this won't keep things from working, you'll get a warning as to what version of cider is current, and how the version number for cider-repl doesn't match it. As of this writing, version 0.8.1 is current.



Create a Launch Script for GUI Emacs

The next bit will save you some aggrivation, and make life easier. There's an issue with starting Emacs from the Applications folder, and then opening a project. The correct PATH is not transferred, and if you try to run cider it will tell you as much (it won't be able to find lein). So, what we're going to do is create an executable shell script to launch the GUI version of Emacs, from the command line (which will need to be cd'd into our Clojure Projects directory when we launch it). I found it was necessary to do this, despite the "fix" they gave on clojure-docs (which didn't work). They don't offer this in their Emacs tutorial, but work with me, you'll be glad to have everything run the way it's supposed to. So, here is the script I created. I named it emacsw and it uses the exact installation of Emacs that brew created:


#!/bin/sh
/usr/local/Cellar/emacs/24.4/Emacs.app/Contents/MacOS/Emacs "$@"
Save it to /usr/local/bin (I saved it as emacsw as I said), so, /usr/local/bin/emacsw. Now, in the terminal, (assuming your in the directory /usr/local/bin) enter the follwing:

$ chmod a+x emacsw
The script launches the GUI version of Emacs, adding to it whatever file parameter you enter after it. The chmod command makes the script executable.



Test Launch GUI Emacs with the script from within your Project Folder, with a File Argument

Now using the terminal, cd into your projects directory, close Emacs if you have it open, and then enter:


$ emacsw project.clj
This should open GUI Emacs with your projects clj file loaded.

Ensure that everything works by attempting to start Cider Repl

Now to test that everything is working correctly. With your Emacs window selected, enter:


M-x cider-jack-in
Likely there will be a long delay while everything boots up. So long as it doesn't error out, give it some time. If everything worked out, you should see a clojure repl buffer open in Emacs.



NOW, go check out the tutorial on clojure-docs..

Finally, you're at a point where the TUTORIAL from clojure-doc should work just fine. Go check it out, it's got other stuff that's important, will walk you through the basics of developing with Emacs and Cider, etc. Happy Coding.

Comments

Popular posts from this blog

Java application opening then immediately closing on OSX

I have had several occasion's where Java apps would open, and then immediately close.  Often, I don't have the time to dig into the reason, as they aren't crucial.  So, I just move on. Recently, I was maddened by this happening to an application ( see other blog post on Kindle Previewer ) I really needed to use, so I had to get into the trench.  After cracking open the app, and rooting through the various files, I did some spot-checks of the java version used to build the jars ( 2 bytes at offsets 6 & 7 ), as well as had a look at the info.plist, which had the JVMVersion key set at 1.5+. Since Oracle took over Java, Apple has essentially abandoned it to them, and hasn't done an update I *think* since version 6. All of Apple's Tooling for Java stops with JVMVersion key's of 6* and 6+.  The short answer to all this is, if something was built using Apple's AppBundler tools, and you have updated Java on your Mac to 7 or above, you're probably SOL to g...

Boost on OS X using the brew boost keg

I was installing boost today using the *brew keg*, which shows it's for the latest version, 1.55.0, but I kept running into errors. --HEAD --with-mpi In the case of #1, it kept puking every time it tried to grab boost from svn. In the case of #2, no matter what I did, including this option kept giving a warning about Python support soon to be deprecated. And yes, I tried the --without-python` option. It just wasn't happening. I don't know if it's a problem with the keg, or a problem with this particular build of Boost. Finally, I was successfully able to get things to build using: brew install boost --c++11 --with-icu --without-python --without-single