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

Codeigniter vs. Kohana Database access speeds

I was doing some basic profiling for a project in which I needed the fastest raw speed I could get with database queries. I'm a fan of Codeigniter for projects that are suitable for it, but had heard from some that Kohana was faster, so I decided to do a very basic comparison of the two. I was using MAMP for OSX, and created a very small db, with a table that had 3 fields: (id), (first), and (last). The data sample was also very small, only a few records. The basic query I tested was a "SELECT * FROM [table]". There is of course nothing remotely scientific about this. It was just a quick ad dirty, very limited comparison. Take it as such. Versions used: CI2.1.3, Kohana 3.3.0. Codeigniter I really like Codeigniter (CI). But, one thing that is very evident from their own profiling functions, is that CI is a bit of a memory hog! Essentially, the same Controller function running in CI takes approximately 10X more memory than in Kohana! This in itself is not ...

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}...

Fun with Meteor, React, and React-Bootstrap

React-Bootstrap is pretty cool. I decided to play with it a bit.  Here are the basics. In an already set up Meteor project (set up for React), it is added thus: npm install --save react-bootstrap Once this is done, you also need to add a bootstrap library. It could either be the twitter bootstrap meteor package, or you can link to it. For the purpose of my demo, I just grabbed a couple links from the React-Bootstrap site that they had handy for pulling in from a CMS: index.html <head> <!-- Latest compiled and minified CSS --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css" rel="stylesheet"> </link> <!-- Optional theme --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap-theme.min.css" rel="stylesheet"> </link> </head> Now, let's make a layout, and then create a component tha...