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

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

React Simplicity

This is just a quick intro to React to show how easy it is on a very basic level. React is often compared to Angular, but the two are very different: Angular is more of a framework , whereas React is more of a library . So, with React, we can make Components, and in so doing, we can intersperse plain Javascript to instill behavior. This article is not showing (or using) best practices, or a recommended structure. It's purpose is only to show how easy the basic mechanics of React are. Let's grab the getting started cli from React's page npm install -g create-react-app create-react-app my-app cd my-app npm start After this is done, and you have the project displayed in your browser, let's experiment. A boiler-plate header we can use for each new class can be as simple as: /src/Foo.js import React, { Component } from 'react'; class Foo extends Component { render(){ return(); } } export default Foo; So, all that we need to change to get st

Screen Scraping in Ruby with Watir and Nokogiri

I was given an interesting challenge to scrape some data from a specific site.  Not to write a completed, packaged solution, but rather just to scrape the data.  The rub being, the site uses Javascript paging, so one couldn't simply use something like Mechanize.  While a self-contained product would require inclusion of V8 (as the Javascript would need to be run and evaluated), to just scrape the data allows making use of whatever is easy and available.  Enter Watir . Watir allows "mechanized/automated" browser control.  Essentially, we can script a browser to go to pages, click links, fill out forms, and what have you.  It's mainstay is in testing, but it's also pretty damned handy in cases where we need some Javascript on a page processed... like in this case.  Keep in mind though, it is literally automating a browser, so you'll see your browser open and navigate to pages, etc. when the script runs.  But, there is also a headless browser option.  This is