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

Dead Simple React.js with Meteor

I spent a little time exploring the patterns involved in using React.js with Meteor. It's incredibly easy, it turns out. I'll show some examples here. The setup: meteor add kadira:flow-router npm install react react-dom react-mounter npm install react-addons-pure-render-mixin meteor add react-meteor-data Then of course remove all blaze related meteor packages. Ok, Some basic component patterns: Let's create one that accepts a single argument: Hello.jsx import React from 'react'; export const Hello = ({name}) => ( <div>Hello, {name}</div> ); That's all there is to it. Now, let's see a pattern for a component that takes two arguments. We can see that to add further arguments, we can just tack them on after the first two: TwoArgs.jsx import React from 'react'; export const TwoArgs = ({one, two}) => ( <div> <h2>TwoArgs!</h2> <h3>One is: {one}</h3> <h3>Two is: {two}...

Install current SBCL on OS X

You must have Command Line Tools installed. If you don't , this tutorial is not for you. Google: installation of XCode and Command Line Tools. Normally, I use brew to install things (when it offers a solution), but in this case the keg version was a couple minor version's off. And, there had been sufficient addition's that motivated me to want the current release. So, building from source was the path of least resistance. First, what not to do : The note's caution against using OS X's Terminal , as their make.sh script pukes a shit-ton of text during the build, and according to them, it can slow the build. I did not experience an issue with this, compared to other builds I've done in the past.   BUT , they also say build can be accomplished with other LISP's installed (you must have a lisp installed prior to building). OMFG , unless you want to wait a month of Sunday's, my experience building with CLISP was slower than the Molasses in January.  D...

Using Boost on OS X with Jetbrains' AppCode

So, after a serious pain in the butt getting Boost installed using the homebrew boost keg (see my previous post ), I decided to test things out using AppCode. Obviously, since this is a "3rd party library", a little massaging has to be done to get the libraries and headers found, and it's sufficiently obscure to deserve a post. I will list the procedure using my rig (OS X). You can extrapolate from this to your own kit. Right-click on your project icon, and select, "Project Settings..." , and scroll down to the "Search Paths" heading. About the third or so option down is "Header Search Paths" . Open that, and select either or both your type of build (Debug/Release), and then double click to the right of it under the "value" column. This will open up a window where you can add a path. Click the "+" and enter the path to the location of your copy of Boost's Headers. In my case: /usr/local/Cellar/boost/1....