Skip to content

Latest commit

 

History

History
519 lines (439 loc) · 23.2 KB

README.org

File metadata and controls

519 lines (439 loc) · 23.2 KB

Clojure layer

img/clojure.png img/cider.png

Table of Contents

Description

This layer adds support for Clojure language using CIDER.

Features:

Install

Layer

To use this configuration layer, add it to your ~/.spacemacs. You will need to add clojure to the existing dotspacemacs-configuration-layers list in this file.

Pretty Symbols

Pretty symbols for anonymous functions, set literals and partial, like (λ [a] (+ a 5)), ƒ(+ % 5), ∈{2 4 6} and Ƥ.

To enable this feature, add the following snippet to the dotspacemacs/user-config section of your ~/.spacemacs file:

(setq clojure-enable-fancify-symbols t)

Or set this variable when loading the configuration layer:

(setq-default dotspacemacs-configuration-layers
'((clojure :variables clojure-enable-fancify-symbols t)))

Enabling sayid or clj-refactor

The packages sayid (Clojure debugger) and clj-refactor (automatic refactorings) are disabled by default. To enable them, add a `:variables` option when enabling the `clojure` layer, specifying `clojure-enable-sayid`, or `clojure-enable-clj-refactor`, or both.

In your Spacemacs configuration:

;; before
dotspacemacs-configuration-layers
'(...
  clojure
  )

;; after
dotspacemacs-configuration-layers
'(...
  (clojure :variables
           clojure-enable-sayid t
           clojure-enable-clj-refactor t)
  )

Enabling either of these packages will cause extra nREPL middleware to be injected when jacking in CIDER.

Starting Clojure manually (outside of Emacs)

CIDER communicates with your Clojure process through nREPL, and for CIDER to function correctly extra nREPL middleware needs to be present (cider/cider-nrepl). The same is true for clj-refactor (refactor-nrepl), and for sayid (com.billpiel/sayid).

When starting the Clojure process through cider (`cider-jack-in` and friends) this will be handled automatically, and so most users should be able to just run SPC m s i to connect to the CIDER REPL and skip the rest of this section.

If you are running an older version of CIDER (0.10 or older), or if you are starting the Clojure process yourself outside of Emacs, then you need to make sure the necessary dependencies are present, and the necessary nREPL middlewares are enabled.

Quick Start with boot

  • Install boot (see https://github.com/boot-clj/boot#user-content-install)
  • Create a file ~/.boot/profile.boot with the following content:
    (require 'boot.repl)
    
    (swap! boot.repl/*default-dependencies* conj
           ;; When running an older version of CIDER (pre 0.18), use the
           ;; version that best matches M-x cider-version. For versions since
           ;; 0.18.0 use whatever version is the most recent.
           '[cider/cider-nrepl "0.19.0-SNAPSHOT"]
    
           ;; Only necessary when using clj-refactor
           '[refactor-nrepl "2.4.0"]
    
           ;; Only necessary when using sayid
           '[com.billpiel/sayid "0.0.17"])
    
    (swap! boot.repl/*default-middleware* conj
           'cider.nrepl/cider-middleware
           'refactor-nrepl.middleware/wrap-refactor
           'com.billpiel.sayid.nrepl-middleware/wrap-sayid)
        

Quick Start with lein

  • Install lein version 2.5.2 or newer (see https://leiningen.org/#install)
  • Create a file ~/.lein/profiles.clj with the following content:
    {:repl
     {:plugins [;; When running an older version of CIDER (pre 0.18), use the
                ;; version that best matches M-x cider-version. For versions since
                ;; 0.18.0 use whatever version is the most recent.
                [cider/cider-nrepl "0.19.0-SNAPSHOT"]
    
                ;; Only necessary when using clj-refactor
                [refactor-nrepl "2.4.0"]
    
                ;; Only necessary when using sayid
                [com.billpiel/sayid "0.0.17"]]
    
      :dependencies [[nrepl "0.4.5"]]
    
      :repl-options
      {:nrepl-middleware [cider.nrepl/cider-middleware
                          refactor-nrepl.middleware/wrap-refactor ;; clj-refactor
                          com.billpiel.sayid.nrepl-middleware/wrap-sayid ;; sayid
                          ]}}}
        

More details

More info regarding installation of nREPL middleware can be found here:

Usage

Cheatsheet

This layers installs the clojure-cheatsheet package which embeds this useful cheatsheet into Emacs.

Type SPC m h c to display the cheatsheet then type in some terms (space separated) to narrow down the list. For example, try typing in sort map to see some functions that deal with sorting maps.

Structuraly safe editing

This layer adds support for evil-cleverparens which allows to safely edit lisp code by keeping the s-expressions balanced.

By default this mode is not activated. You can turn it on locally on the active buffer with SPC m T s (s for safe).

To turn it on automatically for all clojure buffers call the following function in your dotspacemacs/user-config function:

(spacemacs/toggle-evil-safe-lisp-structural-editing-on-register-hook-clojure-mode)

or to enable it for all supported modes:

(spacemacs/toggle-evil-safe-lisp-structural-editing-on-register-hooks)

When enabled the symbol 🆂 should be displayed in the mode-line.

Key bindings

Working with clojure files (barfage, slurpage & more)

Spacemacs comes with a special lisp-state for working with lisp code that supports slurpage, barfage and more tools you’ll likely want when working with lisp.

As this state works the same for all files, the documentation is in global DOCUMENTATION.org. In general, use SPC k to interact with the lisp-state.

Leader

Documentation

Key bindingDescription
SPC m h acider apropos
SPC m h cclojure cheatsheet
SPC m h gcider grimoire
SPC m h hcider doc
SPC m h jcider javadoc
SPC m h ncider browse namespace
SPC m h Ncider browse all namespaces

Evaluation

Key bindingDescription
SPC m e ;eval sexp and show result as comment
SPC m e beval buffer
SPC m e eeval last sexp
SPC m e feval function at point
SPC m e reval region
SPC m e mcider macroexpand 1
SPC m e Mcider macroexpand all
SPC m e pprint last sexp (clojure interaction mode only)
SPC m e Peval last sexp and pretty print result in separate buffer
SPC m e weval last sexp and replace with result

Goto

Key bindingDescription
SPC m g bgo back
SPC m g Cbrowse classpath
SPC m g ggoto var
SPC m g egoto error
SPC m g ngoto namespace
SPC m g rgoto resource
SPC m g sbrowse spec
SPC m g Sbrowse all specs

REPL

Key bindingDescription
SPC m ,handle shortcut (cider-repl-handle-shortcut)
SPC m s bsend and eval buffer in REPL
SPC m s Bsend and eval buffer and switch to REPL in insert state
SPC m s cconnect to REPL (cider-connect) or clear repl buffer (cider-repl-clear-buffer)
SPC m s Cclear REPL (cider-find-and-clear-repl-output)
SPC m s esend and eval last sexp in REPL
SPC m s Esend and eval last sexp and switch to REPL in insert state
SPC m s fsend and eval function in REPL
SPC m s Fsend and eval function and switch to REPL in insert state
SPC m s istart REPL (cider-jack-in)
SPC m s Istart clojurescript REPL (cider-jack-in-clojurescript)
SPC m s nsend and eval ns form in REPL
SPC m s Nsend and eval ns form and switch to REPL in insert state
SPC m s qkill REPL (cider-quit)
SPC m s oswitch to other repl instance (cider-repl-switch-to-other)
SPC m s rsend and eval region in REPL
SPC m s Rsend and eval region and switch to REPL in insert state
SPC m s sswitch to REPL or jump to last file or last clj buffer from repl (cider-repl)
SPC m s xrefresh REPL
SPC m s Xrestart REPL

Tests

Key bindingDescription
SPC m t arun all tests in namespace
SPC m t rre-run test failures for namespace
SPC m t trun test at point

Toggles

Key bindingDescription
SPC m T etoggle englighten mode
SPC m T ftoggle REPL font-locking
SPC m T itoggle indentation style
SPC m T ptoggle REPL pretty-printing
SPC m T ttoggle auto test mode

Debugging

Key bindingDescription
SPC m d !reload traces and clear sayid workspace
SPC m d binstrument expression at point
SPC m d cclear workspace trace log
SPC m d edisplay last stacktrace
SPC m d Eone time display of value at cursor
SPC m d fquery form at point
SPC m d hshow sayid help (key bindings may not be accurate)
SPC m d iinspect expression at point
SPC m d rreload namespaces
SPC m d sshow what is currently traced
SPC m d Sshow what is currently traced in current namespace
SPC m d t btrace current file’s namespace
SPC m d t ddisable existing trace on current function
SPC m d t Ddisable existing trace on all functions
SPC m d t eenable existing trace on current function
SPC m d t Eenable existing trace on all functions
SPC m d t Kremove all traces
SPC m d t ncreate inner trace on function
SPC m d t ocreate outer trace on function
SPC m d t ptrace namespaces by regex
SPC m d t rremove trace on function
SPC m d t yrecursively trace every namespace in given dir
SPC m d vinspect expression at point
SPC m d Vset the view
SPC m d wopen sayid workspace window
SPC m d xclear workspace traces and log

Refactoring

The following refactoring key bindings are enabled by default in clojure-mode:

Key bindingDescription
SPC m r c icycle between if and if-not forms
SPC m r c pcycle privacy of defn and def forms
SPC m r c (convert coll to list
SPC m r c 'convert coll to quoted list
SPC m r c {convert coll to map
SPC m r c #convert coll to set
SPC m r c [convert coll to vector
SPC m r t frewrite the following form to use the -> (thread first) macro.
SPC m r t lrewrite the following form to use the ->> (thread last) macro.
SPC m r t hthread another form into the surrounding threading macro
SPC m r u aunwind all steps of surrounding threading macro
SPC m r u wunwind threading macro one step at a time

The following refactorings require cljr-refactor to be enabled and generally depend on a connected CIDER session.

Key bindingDescription
SPC m r ?describe refactoring
SPC m r a dadd declaration
SPC m r a iadd import to ns
SPC m r a madd missing libspec
SPC m r a padd project dependency
SPC m r a radd require to ns
SPC m r a uadd use to ns
SPC m r c :toggle between keyword and string
SPC m r c nclean ns
SPC m r d kdestructure keys
SPC m r e cextract constant
SPC m r e dextract definition
SPC m r e fextract function
SPC m r e lexpand let
SPC m r f ufind usages
SPC m r f ecreate fn from example
SPC m r h dhotload dependency
SPC m r i lintroduce let
SPC m r i sinline symbol
SPC m r m fmove form
SPC m r m lmove to let
SPC m r p cproject clean
SPC m r p fpromote function
SPC m r r dremove debug fns
SPC m r r frename file
SPC m r r lremove let
SPC m r r rremove unused requires
SPC m r r srename symbol
SPC m r r ureplace use
SPC m r s nsort ns
SPC m r s psort project dependencies
SPC m r s rstop referring
SPC m r s cshow changelog
SPC m r u pupdate project dependencies

Reformatting

Key bindingDescription
SPC m f b or SPC m =reformat current buffer
SPC m f lrealign current form

Profiling

Key bindingDescription
SPC m p +profile samples
SPC m p cclear profile
SPC m p ntoggle profile ns
SPC m p sprofile summary
SPC m p Ssummary for all
SPC m p ttoggle profile
SPC m p vis variable profiled

CIDER Buffers

In general, q should always quit the popped up buffer.

cider-repl-mode

Key bindingDescription
C-jnext input
C-kprevious input

stacktrace-mode

Key bindingDescription
C-jnext cause
C-kprevious cause
TABcycle current cause
0cycle all causes
1cycle cause 1
2cycle cause 2
3cycle cause 3
4cycle cause 4
5cycle cause 5
atoggle all
ctoggle clj
dtoggle duplicates
Jtoggle java
rtoggle repl
Ttoggle tooling

inspector-mode

Key bindingDescription
TABnext inspectable object
Shift-TABprevious inspectable object
RETinspect object
Lpop to the parent object
nnext page in paginated view
Nprevious page in paginated view
rrefresh
sset a new page size

test-report-mode

Key bindingDescription
C-jnext result
C-kprevious result
RETjump to test
dediff test result
eshow stacktrace
rrerun failed tests
trun test
Trun tests

Sayid Buffers

sayid-mode

Key bindingDescription
Shift-Backspaceforward buffer state
enterpop to function
ddef value to $s/*
fquery for calls to function
Fquery to calls to function with modifier
ishow only this instance
Ishow only this instance with modifier
L or Backspaceprevious buffer state
njump to next call
Njump to previous call
Ppretty print value
Cclear workspace trace log
egenerate instance expression and put in kill ring
Hdisplay help (key bindings may not be accurate)
wshow full workspace trace
C-s vtoggle view
C-s Vset view

sayid-traced-mode

Key bindingDescription
backspacego back to trace overview
enterdrill into ns at point
eenable trace
Eenable all traces
ddisable trace
Ddisable all traces
hdisplay help (key bindings may not be accurate)
iapply inner trace to function at point
oapply outer trace to function at point
rremove trace at point

sayid-pprint

Key bindingDescription
entershow path in minibuffer
ienter child node
oenter parent node
nenter next sibling node
penter previous sibling node

Development Notes

Indentation

With a new functionality of Cider to read the custom indentation rules from the var’s metadata, it is better for consistency reasons to not add the custom indentation rules to Spacemacs, but to add them to the metadata of those specific vars.