Skip to end of metadata
Go to start of metadata
Labels:
  1. Mar 29, 2011

    (I wanted to edit the original page, but somehow I couldn't; I spent 10 minutes looking for an edit link or button and then I gave up; someone please integrate this with the rest.)

    There are three main ways to work with Clojure from Emacs:

    1. Inferior-mode : a simple mode that will start a VM as a subprocess and then
    communicates with its repl via stdin/stdout pipes.

    • Functionality is minimal (you can send expressions to eval and do a little
      bit more), but...
    • Due to its simplicity this is always working. Also, all output goes to a
      single place (it's just like a REPL).
    • This is how the core developers work, because often support for the other
      method of connecting lags behind a littlebit.

    Advantages: simple; working with all versions of Clojure (including
    the very bleeding edge).

    Disadvantages: limited number of commands; simplistic output (results
    of expressions is intermixed with stdout output); it's easy to kill
    the REPL buffer by accident and that kills your VM subprocess.

    Setup: you typically need to write a script that will start you
    clojure VM, and configure Emacs to tell it where to run this script.

    2. SLIME: a rich, complex mode that provides various commands to
    communicate with an external VM via a socket. The VM is started
    independently, and loaded with a package called swank-clojure,
    which opens a server that listens on port 4005. This server
    interprets an asynchronous protocol by SLIME ("emacs-rex") which
    consists of simple s-expressions. swank-clojure implements many of
    the commands in the emacs-rex protocol, and provides SLIME with
    appropriate responses. SLIME is an Emacs package which provides
    UI/workflow for various LISP targets, and it is able to connect to
    the VM with a TCP socket (via its slime-connect command).

    • About 70% of SLIME functionality is implemented by Swank-Clojure.
      Evaluating expressions, bringing up documentation, browsing
      (a-propos) of symbols from packages, completion, list and killing
      threads, etc. The "who calls" functions aren't there, and a few
      others.
    • Swank-clojure sometimes breaks on newer versions, and can be
      difficult to debug quickly if you're not familiar with its code.
      On the other hand, it does get fixed eventually--people are using
      it--and on relatively "recent" versions it works well.
    • Setup involves: installing and configuring SLIME, installing and
      starting the Swank-Clojure server in your VM. (See below).
    • Where SLIME places the output of commands can be confusing if you
      don't know how it works:

    1. Return values get rendered in the Emacs minibuffer

    2. stdout of the main thread goes to the slime-repl buffer

    3. stdout of all the other threads go to the console of
    the VM, because they inherit System/out

    4. Side-effects from converting return values to string
    (i.e., from lazy seqs, e.g., (map println (range 10)))
    appear in the minibuffer too, interspersed with the
    return values.

    Advantages: rich set of commands, asynchronous execution;
    buffer/workflow management.

    Disadvantages: swank-clojure support sometimes lags behind the latest;
    more complicated installation; can be confusing to interpret output.

    Setup: You don't really need to use fancy package management if you
    use the following simple method:

    Make sure you're using the versions above, which are usually in a
    known working state. The minimal setup in Emacs for making this work
    is:

    Note: clojure-mode is independent of SLIME, and provides editing
    functionality in Emacs only, has nothing to do with SLIME; you only
    need it to make editing easier.

    Note(2): you need the slime-repl add-on for Swank-Clojure to work
    properly.

    3. nREPL and others: a server for sending asynchronous eval requests
    via a socket is being worked on (nREPL). The purpose of it is to have
    other GUIs than Emacs share a common server connection method to a
    running VM. It does not yet provide a common package of utilities that
    you can invoke, its definition is limited to asynchronous evaluations.
    A common library of backend tools is being worked on, and it is hoped
    that correspond Emacs support will arise to take advantage of that
    library, to replace SLIME with more Clojure-specific support.

    (See mailing-list on 'nREPL' for details and a long thread.)

  2. Apr 08, 2011

    Hi Anthony,

    I'm having trouble following these instructions. I have GNU Emacs 22.1.1 (i586-suse-linux-gnu). Whether I start with my normal .emacs_d or technomancy-emacs-starter-kit-4ba4630/init.el, loading package.el and/or package-maint.el gives me the error message "unknown keyword :risky", and presumably as a result, the command M-x package-list-packages does not get defined. I would upgrade to Emacs 24 if it had been released! Any suggestions welcome. Thanks.

    David

  3. Aug 27, 2011

    I've spent quite a bit of time trying to get this setup. The nice folks on #clojure irc.freenode.net helped me out. A lot of blog posts are outdated. These details are for a slime/swank setup. I may not be 100% correct on my descriptions, so apologies for that in advance.

    General overview:

    emacs->slime   <-> swank->swank-clojure->clojure

    My understanding is that emacs talks to a swank server via the slime library. Swank can communicate to a number of lisps via a back end, one of which is clojure via the swank-clojure backend.

    You should install the package manager as described at the top of this page. I used the emacs-starter-kit so all I needed to do was run (in emacs):

    M-x package-install, scroll down to clojure-mode and then press i (for install) then x (to execute). y (yes) I want to install clojure-mode.

    I installed leiningen (links are further up on the page as to how to do this). I personally used Homebrew since I'm on OSX (via terminal): brew install leiningen

    Then, as of the time of writing (via terminal):
    lein plugin install swank-clojure 1.3.2

    Create a new project (via terminal):
    lein new <projectname>

    Open a file in the lein created clojure project in emacs and M-x clojure-jack-in

    Switch to the swank buffer. If you get an error as I did about not being able to find lein, add this to your dotemacs:
    (setenv "PATH" (concat "/usr/local/bin:" (getenv "PATH")))
    where /usr/local/bin is the path to lein. Because you're appending the path to lein to the start of the existing PATH, you will still need to include the colon. Restart emacs and repeat the jack-in process.

    The following paragraph may not be 100% correct, but I think the gist is correct

    clojure-jack-in is defined in clojure-mode. This tells lein to start the swank server using swank-clojure, and grabs a known working copy of SLIME from the swank-clojure jar file that's downloaded when you install swank-clojure as a lein plugin. Meanwhile, lein walks the filesystem from the current working directory looking for the project.clj and will setup the correct class paths. Clojure-mode then loads the copy of SLIME it received from the swank jar into emacs and uses it to connect to the now running swank server.

    The general process of the instructions at the top of this page are correct. A further outline can be found on the swank-clojure github readme, linked earlier on in this page. Hopefully this comment will shed a little more light on some of the magic that happens.

  4. Oct 07, 2011

    Thanks for the effort and the instructions given above. As a new entrant into clojure and to some extent to Emacs, I struggled a bit to install and get them working and had to refer many resources in web.

    To help people like me starting out in "Emacs + Clojure + Swank + Leiningen", I am putting together detailed set of instructions building on top of what is in the page above:

    DETAILED INSTRUCTIONS - for EMACS + CLOJURE :

    (emphasis on Windows - will apply to other OSs also):

    Emacs set up:

     1. Follow Emacs Starter Kit page (http://github.com/technomancy/emacs-starter-kit) to pick-up latest precompiled binary of Emacs from http://code.google.com/p/emacs-for-windows/updates/list. [If you have earlier versions that v24 installed, skip this and copy "package.el" as given in the top of the page instructions. I used v24. Cant say about previous versions.]

    2. Unzip the contents to folder of your choice.

    3. Add the Emacs directory path to your Path variable (e.g., C:\Emacs, C:\Emacs\bin) in the Environment variables section (My Computer >> Properties >> Advanced >> Environment Variables >> System Variables) - Google for more if you need more instruction on this step

    4. Launch and Test Emacs by typing "runemacs" by opening command prompt by typing cmd in Start >> Run or by clicking Command prompt icon in the task bar

    Emacs Starter kit set up:

     1. Original instruction in Starter Kit page and on top of the page: Add Marmalade as a package archive source in ~/.emacs.d/init.el

    There are couple of points which might confuse a newbie.

    1.1.An empty emacs.d directory gets created when you install emacs in your home directory. So look in your home directory for this folder.

    1.1.1 for Unix ~/. is the home. For windows XP --> "C:\Documents and Settings\Administrator\Application Data" is the home folder.

    1.2 Package dependencies need to be added to init.el and .emacs file. Top of the page instruction says .emacs file. Emacs Starter kit page and the Issue # 20 - http://code.google.com/p/marmalade/issues/detail?id=20 say init.el.

    1.2.1 Either of them alone did not work for me. I created both. BEWARE: Don’t waste time looking for these files if yours is a fresh installation. Both these files need to be created manually afresh.

    In init.el add:

    (require 'package)

    (add-to-list 'package-archives

                 '("marmalade" . "http://marmalade-repo.org/packages/") t)

    (package-initialize)

    In .emacs file add the contents given in top of the page.

    1.3 Next run ‘M-x: "package-refresh-contents"’. (M is Alt key in windows). Again, first time this needs to be manually installed. Emacs tab completion did not show the command for me. Just type and press enter.

    1.3.1  Then run “M-x package-install <hit enter> starter-kit <hit enter>”

     Clojure mode set up:

     Install clojure-mode by pressing M-x package-install and choose clojure-mode.

     Install Leiningen:

     Leiningen is required for swank clojure and SLIME set up.

     1. Download windows or OS specific distribution pre-complied binaries from - as of 7th Oct, 2011, I downloaded 1.6.1-windows from http://github.com/technomancy/leiningen

    2. Unzip in a any folder (e.g.,C:\lein)

    2.1 WARNING: Git has some certificate checking issue which was fixed by Phil which is included in this download itself. DO NOT Download the separate ".bat" file as given in the Leiningen site.

    (Additional info: Not necessary to read if you don't want to bother: http://github.com/blog/738-sidejack-prevention-phase-2-ssl-everywhere#comment-9002 and http://github.com/blog/738-sidejack-prevention-phase-2-ssl-everywhere#comment-9002)

    3. Include the "lein" directory (C:\lein, C:\lein\bin) in windows PATH.

    3.1 Download "wget.exe" to C:\windows (This automatically includes in the path)

    4. Move to lein directory and do a "lein self-install"

    5. Next do a "lein deps" from "C:\lein" where the project.clj is available. THIS IS IMPORTANT. Many instructions/ tutorials don’t say this explicitly.

    6. Test lein by creating a new test project by running

    6.1 "lein new test"

     Install Swank-clojure

    (ref: [http://github.com/technomancy/swank-clojure/blob/master/README.md#readme):
    ]

    7. run “lein plugin install swank-clojure 1.3.2” (ref: swank-clojure site)

    8. Test by "lein swank" to see if the server is started in local.

     This will install swank-clojure

     Run Emacs with swank/SLIME:

     9. Create a new project in a location where you want. Refer to steps 6, 6.1

    10. Type "runemacs" in command prompt from the project folder.  This will launch emacs.

    11. Type M-x in Emacs

    12. Select/ type "clojure-jack-in"

    13. Swank server will be started and you will be in SLIME/ swank mode.

     Happy hacking...!
    1. Nov 10, 2011

      Muthu,

      I am struggling probably much more than you do.

      I have followed all you indications to install emacs, lein and SLIME/swank. When at the end I runemacs, type M-x, type "clojure-jack-in" I get [No match]. What am I doing wrong?

      1. Nov 15, 2011

        Jose

        Did you get to solve the problem?

        If you have advanced as far as installing swank, I am not sure why this occurs?

        Were you able to start a swank sever using lein swank?

        How does your .emacs file look like?

        Muthu

        1. Nov 15, 2011

          Muthu,

          Thanks for you answer.

          For now I have given up with emacs. I went over the no match issue, but had some other kind of error.

          This has been a VERY LONG adventure in frustration atter frustration for an older user of Visual Lisp an Visual Basic. I have hack some code here and there and know that clojure  can do exactly what I want. In my case the clojure code is not a problem but it is becoming mission impossible to find an IDE that you can install, code and compile without having to install and configure a bounch  of other things like lein, swank, slime etc ... etc...

          I'll keep on trying until the come out with the clojure install for windows with all the goodies inside.

          Thanks again,

          Jose

           

          1. Nov 16, 2011

            Hi Jose

            In that case, did you try Clooj? It is supposed to be a easy-to-setup and simple editor. I haven't tried yet. EDIT: I just downloaded it. It's just a jar file and looks like zero installation.

            I think it is worth a try.

            http://dev.clojure.org/display/doc/getting+started+with+Clooj

            Regards

            Muthu

            =====

            1. Nov 16, 2011

              On my pilgrimage through all the available deveopment platforms I think I did try it, but I am going to give a second look again.

              Thanks. Will keep you posted,

              Jose 

      2. Dec 22, 2011

        You might have a look at the radically simplified setup (requires lein):
        http://groups.google.com/group/clojure/browse_thread/thread/91d4f13090afb876/97f8d88255d58798

        and the more extensive blog post (and video) here:
        http://technomancy.us/149

        The clojure-mode in ELPA / package.el is outdated and currently does not contain M-x clojure-jack-in
        I simply cloned clojure-mode from github into ~/.emacs.d and reference it in ~/.emacs like so:

         

        (add-to-list 'load-path "~/.emacs.d/")
        (require 'clojure-mode)

        Then continue with steps 2 and 3.
        It really doesn't take longer than a few minutes.

         

         

         

        1. Feb 17, 2012

          Can we get this information clearly flagged at the top of the page, please? It's not clear from anywhere in the text of this page (or, in fact, anywhere else that I can see) that tromey.com/elpa is deprecated.  I wasted a couple of hours last night in part because of this.

          There's a further source of confusion in that if you're on emacs23, you need to grab a specific version of package.el - from http://github.com/technomancy/package.el.  If you follow the clojure-mode link in the body of this page, the README you end up at has apparently the right link, but it's directly to a blob in a git repo, so it's not abundantly clear that it's a link that's maintained to point at the correct version.

          It also seems that if you're going to use package.el, you must use the Marmalade repository.

  5. Jan 19, 2012

    In the section about where SLIME output goes, there is this rule:

    3. stdout of all the other threads go to the console of
    the VM, because they inherit System/out

     

    Is it possible to view or redirect the console of the VM? It would be nice to be view output of things happening on other threads!

     

    Thanks!

    Andy