Error formatting macro: pagetree: java.lang.NullPointerException
Skip to end of metadata
Go to start of metadata
You are viewing an old version of this page. View the current version. Compare with Current  |   View Page History

If you are new to Emacs, you may find the Emacs Starter Kit helpful.

If you already have your own Emacs configuration, you will need to get package.el in order to install the necessary elisp libraries. If you are using Emacs 24 or the Emacs Starter Kit you will already have it installed. You'll also need to add Marmalade as a package source. (This repo is added by the Emacs Starter Kit since 2011-04-12.)

  • Download package.el to your ~/.emacs.d/ directory (create it if necessary).
  • Make sure that your ~/.emacs file contains the following or equivalent:
  • Reload your ~/.emacs file or restart Emacs.

When you have package.el available (either from Emacs Starter Kit or following the above steps), install clojure-mode:

  • Run the command M-x package-list-packages.
  • Select clojure-mode (press i), and then install the selected package (press x). There will be some compilation warnings; these are normal and do not affect the installation process.

Open or create a Clojure .clj file, and you should find that clojure-mode enabled since built-in functions and forms are syntax-highlighted.

To evaluate expressions, there are multiple approaches:

  • inferior-lisp: a simple mode that will start a Clojure instance as a subprocess and then communicates with its REPL via stdin/stdout pipes.
  • SLIME and Swank Clojure: a rich, complex mode that provides various commands to communicate with an external Clojure instance via a socket.

In both cases, you need to launch a Clojure instance with the correct classpath settings. This is most commonly done using a build tool. For instructions see the Build Tools section of Getting Started.

inferior-lisp

The simplest way to eval expressions at a Clojure REPL is to set Clojure to be your inferior-lisp program (inferior as in a subprocess, not as in worse-than) as follows:

  • M-x describe-variable inferior-lisp-program
  • Customize the variable to point to a command that launches a Clojure REPL. You can use an absolute path, or a relative path that will be interpreted relative to the current directory in emacs. For example:
    • java -cp path/to/clojure.jar clojure.main for a basic repl (no rlwrap/jline needed)
    • lein repl for a Leiningen project
    • cake repl for a Cake project
    • mvn clojure:repl for a Maven project
    • script/repl for the labrepl tutorial project.
  • M-x inferior-lisp (or C-c C-z)
  • Use C-x C-e at the end of an expression to evaluate it, showing the result in the inferior-lisp buffer. C-c C-l will load the current file. C-M-x can evaluate a whole def form.

SLIME and Swank Clojure

There is also swank-clojure, which provides a richer level of interaction using SLIME.

Note that having old versions of SLIME either manually installed or installed using a system-wide package manager like apt-get may cause issues.

  • Go back to the packages list with M-x package-list-packages
  • Choose slime-repl. This will install all the elisp code needed. There will be some compilation failures; these are normal and do not affect the installation process.
  • Use your project's build tool to launch a swank server. For example:
  • Connect to the server using M-x slime-connect.
  • Use C-x C-e at the end of an expression to evaluate it, showing the result in the inferior-lisp buffer. C-c C-l will load the current file. C-M-x can evaluate a whole def form.
  • When a file has been loaded use C-c TAB to auto-complete a symbol. C-c M-p can be used to switch to the namespace of the current file.

For more key bindings and other details see the swank-clojure readme.

There is a elisp package called swank-clojure.el that used to be bundled with Swank Clojure. It provided ways to launch a swank server from within emacs – a task which is now more reliable to perform using a build tool, possibly in combination with a tool such as Durendal (also available in the Marmalade repo).

Labels: