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

Passing Functions and Lambdas into Functions with Ruby

Ruby's New Style of Lambda Functions f = ->( m ) { p m } f.call( 1 ) #=> 1 Which of course means the same thing as: f = lambda { |n| p n } f.call(1) #=> 1 Ruby Proc Objects p = Proc.new { |n| p n + 2 } p.call(2) #=> 4 Using a Function as a Closure in Ruby def domo( k ) ->(m) { p m + k } end z = domo( 5 ) z.call( 5 ) #=> 10 Function :domo takes a single parameter. Within :domo , we create a lambda that takes a single parameter, and adds that parameter to the value :domo takes in as its parameter. Then, we assign z to be the result of the lambda in :domo with its 'k' parameter loaded with 5. When z is called, we pass (another) 5 to it. This parameter loads the lambdas n parameter. The lambda executes, essentially adding n (5) + k (5) and yielding the result of 10. The thing about closures such as this is, we can load the initial value of the lambda to be whatever we want it to be when assigning the function :domo '...

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

OS X Process-Fu 101

OS X is of course a variety of BSD Unix, not Linux. Often is the case where Linux commands either don't work, or don't give the same results on OS X as they do on Linux. For example, a basic netstat will give you a long list of the processes making network connections on your box. On Linux, various command line options can help you drill down from there, to get the answers to more specific queries. On OS X, those same queries don't give you the answers you're seeking. This isn't a compare and contrast article between Linux and OSX. Instead, I'm just noting various commands that can give answers to some basic questions. On OS X, for example, to get an answer to the question: "What process is running on port 50224?" we would use: sudo lsof -Pn | grep 50224 This will give you a list of everything connected to port 50224, sans any kind of headings. A much more abbreviated command that will give you essentially the same things, but with column...