Clojure is a very interesting language. It’s a lisp for the JVM, and Emacs is very well suited as a Clojure IDE. In this post I’ll add some customizations that will enhance the Clojure experience in Emacs.
After completing this part I’ll have a setup that let me edit Clojure code in a nice and effective way with help from clojure-mode, clj-refactor and smartparens. I can compile and evaluate code, as well as running tests with help from Cider and company-mode gives me autocompletion powered by Cider. I won’t go into details about these packages. They all have good documentation. Read it!
(require 'setup-package) (package-require 'clojure-mode) (package-require 'clj-refactor) (package-require 'cider) (package-require 'smartparens) (package-require 'rainbow-delimiters) (package-require 'yasnippet) (add-hook 'cider-mode-hook (lambda () (eldoc-mode 1) (company-mode 1) (setq nrepl-hide-special-buffers t))) (add-hook 'cider-repl-mode-hook #'company-mode) (add-hook 'clojure-mode-hook (lambda () (clj-refactor-mode 1) (cljr-add-keybindings-with-prefix "C-c C-m") (smartparens-strict-mode 1) (sp-use-smartparens-bindings) (rainbow-delimiters-mode 1) (yas/minor-mode 1))) (provide 'setup-clojure)
What I’ve done here is to require a bunch of packages. In addition to the ones already mentioned I’ve added rainbow-delimiters and yasnippet, which is nice to have, but not crucial.
Next up is three hooks. Hooks are functions that are called when a given event occurs. In this case the events are modes that become enabled. These are more or less copied from the usage section of the different packages. For
cider-mode I enable
company-mode and hide some special buffers.
eldoc-mode gives me information about function arguments, while
company-mode provides autocompletion.
The first hook defines a
lambda which contains all functions to execute for that hook. The next one “points” directly to an existing function. This is better as that hooks currently consist of just one function. I could have rewritten the first hook as several such statements; one for each line inside the lambda.
The final hook is the one for
clojure-mode. I enable clj-refactoring which provides a lot of helpful functions for refactoring code. Take a look at the documentation! It will make your life easier. Then I bind its prefix key to the default/recommended value
C-c C-m. Smartparens makes working with parens a lot easier. Once you get used to barfing, slurping and killing expressions you will not go back to manual editing.
This is a pretty minimal setup for Clojure development, but I think the most important parts are taken care of.
My .emacs.d at this point can be found in my GitHub repository.