dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

README.md (20975B)


      1 [![circleci][badge-circleci]][circleci]
      2 [![MELPA][melpa-badge]][melpa-package]
      3 [![MELPA Stable][melpa-stable-badge]][melpa-stable-package]
      4 [![NonGNU ELPA](https://elpa.nongnu.org/nongnu/clojure-mode.svg)](https://elpa.nongnu.org/nongnu/clojure-mode.html)
      5 [![Discord](https://img.shields.io/badge/chat-on%20discord-7289da.svg?sanitize=true)](https://discord.com/invite/nFPpynQPME)
      6 [![License GPL 3][badge-license]][copying]
      7 
      8 # Clojure Mode
      9 
     10 `clojure-mode` is an Emacs major mode that provides font-lock (syntax
     11 highlighting), indentation, navigation and refactoring support for the
     12 [Clojure(Script) programming language](http://clojure.org).
     13 
     14 -----------
     15 
     16 **This documentation tracks the `master` branch of `clojure-mode`. Some of
     17 the features and settings discussed here might not be available in
     18 older releases (including the current stable release). Please, consult
     19 the relevant git tag (e.g. 5.15.1) if you need documentation for a
     20 specific `clojure-mode` release.**
     21 
     22 ## Installation
     23 
     24 Available on the major `package.el` community maintained repos -
     25 [MELPA Stable][] and [MELPA][] repos.
     26 
     27 MELPA Stable is the recommended repo as it has the latest stable
     28 version.  MELPA has a development snapshot for users who don't mind
     29 (infrequent) breakage but don't want to run from a git checkout.
     30 
     31 You can install `clojure-mode` using the following command:
     32 
     33 <kbd>M-x</kbd> `package-install` <kbd>[RET]</kbd> `clojure-mode` <kbd>[RET]</kbd>
     34 
     35 or if you'd rather keep it in your dotfiles:
     36 
     37 ```el
     38 (unless (package-installed-p 'clojure-mode)
     39   (package-install 'clojure-mode))
     40 ```
     41 
     42 If the installation doesn't work try refreshing the package list:
     43 
     44 <kbd>M-x</kbd> `package-refresh-contents`
     45 
     46 ## Bundled major modes
     47 
     48 The `clojure-mode` package actually bundles together several major modes:
     49 
     50 * `clojure-mode` is a major mode for editing Clojure code
     51 * `clojurescript-mode` is a major mode for editing ClojureScript code
     52 * `clojurec-mode` is a major mode for editing `.cljc` source files
     53 
     54 All the major modes derive from `clojure-mode` and provide more or less the same
     55 functionality.  Differences can be found mostly in the font-locking -
     56 e.g. ClojureScript has some built-in constructs that are not present in Clojure.
     57 
     58 The proper major mode is selected automatically based on the extension of the
     59 file you're editing.
     60 
     61 Having separate major modes gives you the flexibility to attach different hooks
     62 to them and to alter their behavior individually (e.g. add extra font-locking
     63 just to `clojurescript-mode`) .
     64 
     65 Note that all modes derive from `clojure-mode`, so things you add to
     66 `clojure-mode-hook` and `clojure-mode-map` will affect all the derived modes as
     67 well.
     68 
     69 ## Configuration
     70 
     71 In the spirit of Emacs, pretty much everything you can think of in `clojure-mode` is configurable.
     72 
     73 To see a list of available configuration options do `M-x customize-group RET clojure`.
     74 
     75 ### Indentation options
     76 
     77 The default indentation rules in `clojure-mode` are derived from the
     78 [community Clojure Style Guide](https://guide.clojure.style).
     79 Please, refer to the guide for the general Clojure indentation rules.
     80 
     81 #### Indentation of docstrings
     82 
     83 By default multi-line docstrings are indented with 2 spaces, as this is a
     84 somewhat common standard in the Clojure community. You can however adjust this
     85 by modifying `clojure-docstring-fill-prefix-width`. Set it to 0 if you don't
     86 want multi-line docstrings to be indented at all (which is pretty common in most lisps).
     87 
     88 #### Indentation of function forms
     89 
     90 The indentation of function forms is configured by the variable
     91 `clojure-indent-style`. It takes three possible values:
     92 
     93 - `always-align` (the default)
     94 
     95 ```clj
     96 (some-function
     97  10
     98  1
     99  2)
    100 (some-function 10
    101                1
    102                2)
    103 ```
    104 
    105 - `always-indent`
    106 
    107 ```clj
    108 (some-function
    109   10
    110   1
    111   2)
    112 (some-function 10
    113   1
    114   2)
    115 ```
    116 
    117 - `align-arguments`
    118 
    119 ```clj
    120 (some-function
    121   10
    122   1
    123   2)
    124 (some-function 10
    125                1
    126                2)
    127 ```
    128 
    129 **Note:** Prior to clojure-mode 5.10, the configuration options for `clojure-indent-style` used to be
    130 keywords, but now they are symbols. Keywords will still be supported at least until clojure-mode 6.
    131 
    132 #### Indentation of macro forms
    133 
    134 The indentation of special forms and macros with bodies is controlled via
    135 `put-clojure-indent`, `define-clojure-indent` and `clojure-backtracking-indent`.
    136 Nearly all special forms and built-in macros with bodies have special indentation
    137 settings in `clojure-mode`. You can add/alter the indentation settings in your
    138 personal config. Let's assume you want to indent `->>` and `->` like this:
    139 
    140 ```clojure
    141 (->> something
    142   ala
    143   bala
    144   portokala)
    145 ```
    146 
    147 You can do so by putting the following in your config:
    148 
    149 ```el
    150 (put-clojure-indent '-> 1)
    151 (put-clojure-indent '->> 1)
    152 ```
    153 
    154 This means that the body of the `->/->>` is after the first argument.
    155 
    156 A more compact way to do the same thing is:
    157 
    158 ```el
    159 (define-clojure-indent
    160   (-> 1)
    161   (->> 1))
    162 ```
    163 
    164 To indent something like a definition (`defn`) you can do something like:
    165 
    166 ``` el
    167 (put-clojure-indent '>defn :defn)
    168 ```
    169 
    170 You can also specify different indentation settings for symbols
    171 prefixed with some ns (or ns alias):
    172 
    173 ```el
    174 (put-clojure-indent 'do 0)
    175 (put-clojure-indent 'my-ns/do 1)
    176 ```
    177 
    178 The bodies of certain more complicated macros and special forms
    179 (e.g. `letfn`, `deftype`, `extend-protocol`, etc) are indented using
    180 a contextual backtracking indentation method, require more sophisticated
    181 indent specifications. Here are a few examples:
    182 
    183 ```el
    184 (define-clojure-indent
    185   (implement '(1 (1)))
    186   (letfn     '(1 ((:defn)) nil))
    187   (proxy     '(2 nil nil (1)))
    188   (reify     '(:defn (1)))
    189   (deftype   '(2 nil nil (1)))
    190   (defrecord '(2 nil nil (1)))
    191   (specify   '(1 (1)))
    192   (specify   '(1 (1))))
    193 ```
    194 
    195 These follow the same rules as the `:style/indent` metadata specified by [cider-nrepl][].
    196 For instructions on how to write these specifications, see
    197 [this document](https://docs.cider.mx/cider/indent_spec.html).
    198 The only difference is that you're allowed to use lists instead of vectors.
    199 
    200 The indentation of [special arguments](https://docs.cider.mx/cider/indent_spec.html#special-arguments) is controlled by
    201 `clojure-special-arg-indent-factor`, which by default indents special arguments
    202 a further `lisp-body-indent` when compared to ordinary arguments.
    203 
    204 An example of the default formatting is:
    205 
    206 ```clojure
    207 (defrecord MyRecord
    208     [my-field])
    209 ```
    210 
    211 Note that `defrecord` has two special arguments, followed by the form's body -
    212 namely the record's name and its fields vector.
    213 
    214 Setting `clojure-special-arg-indent-factor` to 1, results in:
    215 
    216 ```clojure
    217 (defrecord MyRecord
    218   [my-field])
    219 ```
    220 
    221 ### Indentation of Comments
    222 
    223 `clojure-mode` differentiates between comments like `;`, `;;`, etc.
    224 By default `clojure-mode` treats `;` as inline comments and *always* indents those.
    225 You can change this behaviour like this:
    226 
    227 ```emacs-lisp
    228 (add-hook 'clojure-mode-hook (lambda () (setq-local comment-column 0)))
    229 ```
    230 
    231 You might also want to change `comment-add` to 0 in that way, so that Emacs comment
    232 functions (e.g. `comment-region`) would use `;` by default instead of `;;`.
    233 
    234 **Note:** Check out [this section](https://guide.clojure.style/#comments) of the Clojure style guide to understand better the semantics of the different comment levels and why `clojure-mode` treats them differently by default.
    235 
    236 ### Vertical alignment
    237 
    238 You can vertically align sexps with `C-c SPC`. For instance, typing
    239 this combo on the following form:
    240 
    241 ```clj
    242 (def my-map
    243   {:a-key 1
    244    :other-key 2})
    245 ```
    246 
    247 Leads to the following:
    248 
    249 ```clj
    250 (def my-map
    251   {:a-key     1
    252    :other-key 2})
    253 ```
    254 
    255 This can also be done automatically (as part of indentation) by
    256 turning on `clojure-align-forms-automatically`. This way it will
    257 happen whenever you select some code and hit `TAB`.
    258 
    259 ### Font-locking
    260 
    261 `clojure-mode` features static font-locking (syntax highlighting) that you can extend yourself
    262 if needed. As typical for Emacs, it's based on regular expressions. You can find
    263 the default font-locking rules in `clojure-font-lock-keywords`. Here's how you can add font-locking for built-in Clojure functions and vars:
    264 
    265 ``` el
    266 (defvar clojure-built-in-vars
    267   '(;; clojure.core
    268     "accessor" "aclone"
    269     "agent" "agent-errors" "aget" "alength" "alias"
    270     "all-ns" "alter" "alter-meta!" "alter-var-root" "amap"
    271     ;; omitted for brevity
    272     ))
    273 
    274 (defvar clojure-built-in-dynamic-vars
    275   '(;; clojure.test
    276     "*initial-report-counters*" "*load-tests*" "*report-counters*"
    277     "*stack-trace-depth*" "*test-out*" "*testing-contexts*" "*testing-vars*"
    278     ;; clojure.xml
    279     "*current*" "*sb*" "*stack*" "*state*"
    280     ))
    281 
    282 (font-lock-add-keywords 'clojure-mode
    283                         `((,(concat "(\\(?:\.*/\\)?"
    284                                     (regexp-opt clojure-built-in-vars t)
    285                                     "\\>")
    286                            1 font-lock-builtin-face)))
    287 
    288 (font-lock-add-keywords 'clojure-mode
    289                         `((,(concat "\\<"
    290                                     (regexp-opt clojure-built-in-dynamic-vars t)
    291                                     "\\>")
    292                            0 font-lock-builtin-face)))
    293 
    294 ```
    295 
    296 **Note:** The package `clojure-mode-extra-font-locking` provides such additional
    297 font-locking for Clojure built-ins.
    298 
    299 As you might imagine one problem with this font-locking approach is that because
    300 it's based on regular expressions you'll get some false positives here and there
    301 (there's no namespace information, and no way for `clojure-mode` to know what
    302 var a symbol resolves to). That's why `clojure-mode`'s font-locking defaults are
    303 conservative and minimalistic.
    304 
    305 Precise font-locking requires additional data that can obtained from a running
    306 REPL (that's how CIDER's [dynamic font-locking](https://docs.cider.mx/cider/config/syntax_highlighting.html) works) or from static code analysis.
    307 
    308 When it comes to non built-in definitions, `clojure-mode` needs to be manually instructed how to handle the docstrings and highlighting. Here's an example:
    309 
    310 ``` emacs-lisp
    311 (put '>defn 'clojure-doc-string-elt 2)
    312 
    313 (font-lock-add-keywords 'clojure-mode
    314                         `((,(concat "(\\(?:" clojure--sym-regexp "/\\)?"
    315                                     "\\(>defn\\)\\>")
    316                            1 font-lock-keyword-face)))
    317 ```
    318 
    319 **Note:** The `clojure-doc-string-elt` attribute is processed by the function `clojure-font-lock-syntactic-face-function`.
    320 
    321 ## Refactoring support
    322 
    323 The available refactorings were originally created and maintained by the
    324 `clj-refactor.el` team. The ones implemented in Elisp only are gradually migrated
    325 to `clojure-mode`.
    326 
    327 ### Threading macros related features
    328 
    329 `clojure-thread`: Thread another form into the surrounding thread. Both `->>`
    330 and `->` variants are supported.
    331 
    332 <img width="512" src="/doc/clojure-thread.gif">
    333 
    334 `clojure-unwind`: Unwind a threaded expression. Supports both `->>` and `->`.
    335 
    336 <img width="512" src="/doc/clojure-unwind.gif">
    337 
    338 `clojure-thread-first-all`: Introduce the thread first macro (`->`) and rewrite
    339 the entire form. With a prefix argument do not thread the last form.
    340 
    341 <img width="512" src="/doc/clojure-thread-first-all.gif">
    342 
    343 `clojure-thread-last-all`: Introduce the thread last macro and rewrite the
    344 entire form. With a prefix argument do not thread the last form.
    345 
    346 <img width="512" src="/doc/clojure-thread-last-all.gif">
    347 
    348 `clojure-unwind-all`: Fully unwind a threaded expression removing the threading
    349 macro.
    350 
    351 <img width="512" src="/doc/clojure-unwind-all.gif">
    352 
    353 ### Cycling things
    354 
    355 `clojure-cycle-privacy`: Cycle privacy of `def`s or `defn`s. Use metadata
    356 explicitly with setting `clojure-use-metadata-for-privacy` to `t` for `defn`s
    357 too.
    358 
    359 <img width="512" src="/doc/clojure-cycle-privacy.gif">
    360 
    361 `clojure-cycle-not`: Add or remove a `not` form around the current form.
    362 
    363 <img width="512" src="/doc/clojure-cycle-not.gif">
    364 
    365 `clojure-cycle-when`: Find the closest `when` or `when-not` up the syntax tree
    366 and toggle it.
    367 
    368 <img width="512" src="/doc/clojure-cycle-when.gif">
    369 
    370 `clojure-cycle-if`: Find the closest `if` or `if-not` up the syntax tree and
    371 toggle it. Also transpose the `else` and `then` branches, keeping the semantics
    372 the same as before.
    373 
    374 <img width="512" src="/doc/clojure-cycle-if.gif">
    375 
    376 ### Convert collection
    377 
    378 Convert any given collection at point to list, quoted list, map, vector or set.
    379 
    380 ### Let expression
    381 
    382 `clojure-introduce-let`: Introduce a new `let` form. Put the current form into
    383 its binding form with a name provided by the user as a bound name. If called
    384 with a numeric prefix put the let form Nth level up in the form hierarchy.
    385 
    386 <img width="512" src="/doc/clojure-introduce-let.gif">
    387 
    388 `clojure-move-to-let`: Move the current form to the closest `let`'s binding
    389 form. Replace all occurrences of the form in the body of the let.
    390 
    391 <img width="512" src="/doc/clojure-move-to-let.gif">
    392 
    393 `clojure-let-forward-slurp-sexp`: Slurp the next form after the `let` into the
    394 `let`. Replace all occurrences of the bound forms in the form added to the `let`
    395 form. If called with a prefix argument slurp the next n forms.
    396 
    397 <img width="512" src="/doc/clojure-let-forward-slurp-sexp.gif">
    398 
    399 `clojure-let-backward-slurp-sexp`: Slurp the form before the `let` into the
    400 `let`. Replace all occurrences of the bound forms in the form added to the `let`
    401 form. If called with a prefix argument slurp the previous n forms.
    402 
    403 <img width="512" src="/doc/clojure-let-backward-slurp-sexp.gif">
    404 
    405 `paredit-convolute-sexp` is advised to replace occurrences of bound forms with their bound names when convolute is used on a let form.
    406 
    407 ### Rename ns alias
    408 
    409 `clojure-rename-ns-alias`: Rename an alias inside a namespace declaration,
    410 and all of its usages in the buffer
    411 
    412 <img width="512" src="/doc/clojure-rename-ns-alias.gif">
    413 
    414 If there is an active selected region, only rename usages of aliases within the region,
    415 without affecting the namespace declaration.
    416 
    417 <img width="512" src="/doc/clojure-rename-ns-alias-region.gif">
    418 
    419 ### Add arity to a function
    420 
    421 `clojure-add-arity`: Add a new arity to an existing single-arity or multi-arity function.
    422 
    423 <img width="512" src="/doc/clojure-add-arity.gif">
    424 
    425 ## Related packages
    426 
    427 * [clojure-mode-extra-font-locking][] provides additional font-locking
    428 for built-in methods and macros.  The font-locking is pretty
    429 imprecise, because it doesn't take namespaces into account and it
    430 won't font-lock a function at all possible positions in a sexp, but
    431 if you don't mind its imperfections you can easily enable it:
    432 
    433 ```el
    434 (require 'clojure-mode-extra-font-locking)
    435 ```
    436 
    437 The code in `clojure-mode-font-locking` used to be bundled with
    438 `clojure-mode` before version 3.0.
    439 
    440 You can also use the code in this package as a basis for extending the
    441 font-locking further (e.g. functions/macros from more
    442 namespaces). Generally you should avoid adding special font-locking
    443 for things that don't have fairly unique names, as this will result in
    444 plenty of incorrect font-locking. CIDER users should avoid this package,
    445 as CIDER does its own dynamic font-locking, which is namespace-aware
    446 and doesn't produce almost any false positives.
    447 
    448 * [clj-refactor][] provides refactoring support.
    449 
    450 * Enabling `CamelCase` support for editing commands(like
    451 `forward-word`, `backward-word`, etc) in `clojure-mode` is quite
    452 useful since we often have to deal with Java class and method
    453 names. The built-in Emacs minor mode `subword-mode` provides such
    454 functionality:
    455 
    456 ```el
    457 (add-hook 'clojure-mode-hook #'subword-mode)
    458 ```
    459 
    460 * The use of [paredit][] when editing Clojure (or any other Lisp) code
    461 is highly recommended. It helps ensure the structure of your forms is
    462 not compromised and offers a number of operations that work on code
    463 structure at a higher level than just characters and words. To enable
    464 it for Clojure buffers:
    465 
    466 ```el
    467 (add-hook 'clojure-mode-hook #'paredit-mode)
    468 ```
    469 
    470 * [smartparens][] is an excellent
    471   (newer) alternative to paredit. Many Clojure hackers have adopted it
    472   recently and you might want to give it a try as well. To enable
    473   `smartparens` use the following code:
    474 
    475 ```el
    476 (add-hook 'clojure-mode-hook #'smartparens-strict-mode)
    477 ```
    478 
    479 * [RainbowDelimiters][] is a
    480   minor mode which highlights parentheses, brackets, and braces
    481   according to their depth. Each successive level is highlighted in a
    482   different color. This makes it easy to spot matching delimiters,
    483   orient yourself in the code, and tell which statements are at a
    484   given depth. Assuming you've already installed `RainbowDelimiters` you can
    485   enable it like this:
    486 
    487 ```el
    488 (add-hook 'clojure-mode-hook #'rainbow-delimiters-mode)
    489 ```
    490 
    491 * [aggressive-indent-mode][] automatically adjust the indentation of your code,
    492 while you're writing it. Using it together with `clojure-mode` is highly
    493 recommended. Provided you've already installed `aggressive-indent-mode` you can
    494 enable it like this:
    495 
    496 ```el
    497 (add-hook 'clojure-mode-hook #'aggressive-indent-mode)
    498 ```
    499 
    500 ## REPL Interaction
    501 
    502 One of the fundamental aspects of Lisps in general, and Clojure in
    503 particular, is the notion of interactive programming - building your
    504 programs by continuously changing the state of the running Lisp
    505 program (as opposed to doing something more traditional like making a
    506 change and re-running the program afterwards to see the changes in
    507 action). To get the most of clojure-mode you'll have to combine it
    508 with some tool which will allow you to interact with your Clojure program
    509 (a.k.a. process/REPL).
    510 
    511 A number of options exist for connecting to a
    512 running Clojure process and evaluating code interactively.
    513 
    514 ### Basic REPL
    515 
    516 [inf-clojure][] provides basic interaction with a Clojure REPL process.
    517 It's very similar in nature and supported functionality to `inferior-lisp-mode`
    518 for Common Lisp.
    519 
    520 ### CIDER
    521 
    522 [CIDER][] is a powerful Clojure interactive development environment,
    523 similar to SLIME for Common Lisp.
    524 
    525 If you're into Clojure and Emacs you should definitely check it out.
    526 
    527 ## Tutorials
    528 
    529 Tutorials,
    530 targeting Emacs beginners, are available at
    531 [clojure-doc.org](http://clojure-doc.org/articles/tutorials/emacs/) and
    532 [Clojure for the Brave and the True](http://www.braveclojure.com/basic-emacs/).
    533 Keep in mind, however, that they might be out-of-date.
    534 
    535 ## Caveats
    536 
    537 `clojure-mode` is a capable tool, but it's certainly not perfect. This section
    538 lists a couple of general design problems/limitations that might affect your
    539 experience negatively.
    540 
    541 ### General Issues
    542 
    543 `clojure-mode` derives a lot of functionality directly from `lisp-mode` (an Emacs major mode for Common Lisp), which
    544 simplified the initial implementation, but also made it harder to implement
    545 certain functionality. Down the road it'd be nice to fully decouple `clojure-mode`
    546 from `lisp-mode`.
    547 
    548 See [this ticket](https://github.com/clojure-emacs/clojure-mode/issues/270) for a bit more details.
    549 
    550 ### Indentation Performance
    551 
    552 `clojure-mode`'s indentation engine is a bit slow. You can speed things up significantly by disabling `clojure-use-backtracking-indent`, but this will break the indentation of complex forms like `deftype`, `defprotocol`, `reify`, `letfn`, etc.
    553 
    554 We should look into ways to optimize the performance of the backtracking indentation logic. See [this ticket](https://github.com/clojure-emacs/clojure-mode/issues/606) for more details.
    555 
    556 ### Font-locking Implementation
    557 
    558 As mentioned [above](https://github.com/clojure-emacs/clojure-mode#font-locking), the font-locking is implemented in terms of regular expressions which makes it both slow and inaccurate.
    559 
    560 ## Changelog
    561 
    562 An extensive changelog is available [here](CHANGELOG.md).
    563 
    564 ## License
    565 
    566 Copyright © 2007-2022 Jeffrey Chu, Lennart Staflin, Phil Hagelberg, Bozhidar
    567 Batsov, Artur Malabarba, Magnar Sveen and [contributors][].
    568 
    569 Distributed under the GNU General Public License; type <kbd>C-h C-c</kbd> to view it.
    570 
    571 [badge-license]: https://img.shields.io/badge/license-GPL_3-green.svg
    572 [melpa-badge]: http://melpa.org/packages/clojure-mode-badge.svg
    573 [melpa-stable-badge]: http://stable.melpa.org/packages/clojure-mode-badge.svg
    574 [melpa-package]: http://melpa.org/#/clojure-mode
    575 [melpa-stable-package]: http://stable.melpa.org/#/clojure-mode
    576 [COPYING]: http://www.gnu.org/copyleft/gpl.html
    577 [badge-circleci]: https://circleci.com/gh/clojure-emacs/clojure-mode.svg?style=svg
    578 [circleci]: https://circleci.com/gh/clojure-emacs/clojure-mode
    579 [CIDER]: https://github.com/clojure-emacs/cider
    580 [cider-nrepl]: https://github.com/clojure-emacs/cider-nrepl
    581 [inf-clojure]: https://github.com/clojure-emacs/inf-clojure
    582 [contributors]: https://github.com/clojure-emacs/clojure-mode/contributors
    583 [melpa]: http://melpa.org
    584 [melpa stable]: http://stable.melpa.org
    585 [clojure-mode-extra-font-locking]: https://github.com/clojure-emacs/clojure-mode/blob/master/clojure-mode-extra-font-locking.el
    586 [clj-refactor]: https://github.com/clojure-emacs/clj-refactor.el
    587 [paredit]: http://mumble.net/~campbell/emacs/paredit.html
    588 [smartparens]: https://github.com/Fuco1/smartparens
    589 [RainbowDelimiters]: https://github.com/Fanael/rainbow-delimiters
    590 [aggressive-indent-mode]: https://github.com/Malabarba/aggressive-indent-mode