Skip to content

Commit 3d8296e

Browse files
authored
Make indentation of special arguments customisable (#582)
Allow control of the indentation of special arguments through a defcustom. - introduces clojure-special-arg-indent-factor, which is used as a factor of lisp-body-ident to indent special arguments. Defaults to 2, the currently hard coded value for this.
1 parent cccddea commit 3d8296e

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [#571](https://github.com/clojure-emacs/clojure-mode/issues/571): Remove `project.el` integration.
1212
* [#574](https://github.com/clojure-emacs/clojure-mode/issues/574): Remove `clojure-view-grimoire` command.
1313
* Stop `clojure-sort-ns` from calling `redisplay`
14+
* Add 'clojure-special-arg-indent-factor' to control special argument indentation.
1415

1516
## 5.12.0 (2020-08-13)
1617

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,24 @@ For instructions on how to write these specifications, see
223223
[this document](https://docs.cider.mx/cider/indent_spec.html).
224224
The only difference is that you're allowed to use lists instead of vectors.
225225

226+
The indentation of special arguments is controlled by
227+
`clojure-special-arg-indent-factor`, which by default indents special arguments
228+
a further `lisp-body-indent` when compared to ordinary arguments.
229+
230+
An example of the default formatting is:
231+
232+
```clojure
233+
(defrecord MyRecord
234+
[my-field])
235+
```
236+
237+
Setting `clojure-special-arg-indent-factor` to 1, results in:
238+
239+
```clojure
240+
(defrecord MyRecord
241+
[my-field])
242+
```
243+
226244
### Indentation of Comments
227245

228246
`clojure-mode` differentiates between comments like `;`, `;;`, etc.

clojure-mode.el

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,13 @@ will align the values like this:
11421142
:safe #'listp
11431143
:type '(repeat string))
11441144

1145+
(defcustom clojure-special-arg-indent-factor
1146+
2
1147+
"Factor of the 'lisp-body-indent' used to indent special arguments."
1148+
:package-version '(clojure-mode . "5.13")
1149+
:type 'integer
1150+
:safe 'integerp)
1151+
11451152
(defvar clojure--beginning-of-reader-conditional-regexp
11461153
"#\\?@(\\|#\\?("
11471154
"Regexp denoting the beginning of a reader conditional.")
@@ -1509,7 +1516,8 @@ This function also returns nil meaning don't specify the indentation."
15091516
(clojure--normal-indent last-sexp 'always-align))
15101517
;; Special arg. Rigidly indent with a large indentation.
15111518
(t
1512-
(+ (* 2 lisp-body-indent) containing-form-column)))))
1519+
(+ (* clojure-special-arg-indent-factor lisp-body-indent)
1520+
containing-form-column)))))
15131521
(`:defn
15141522
(+ lisp-body-indent containing-form-column))
15151523
((pred functionp)

test/clojure-mode-indentation-test.el

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,51 @@ DESCRIPTION is a string with the description of the spec."
233233
(ala/bala top
234234
|one)"))
235235

236+
(describe "specify an indentation for symbol"
237+
(put-clojure-indent 'cala 1)
238+
239+
(when-indenting-with-point-it "should handle a symbol with ns"
240+
"
241+
(cala top
242+
|one)"
243+
"
244+
(cala top
245+
|one)")
246+
(when-indenting-with-point-it "should handle special arguments"
247+
"
248+
(cala
249+
|top
250+
one)"
251+
"
252+
(cala
253+
|top
254+
one)"))
255+
(describe "should respect special argument indentation"
256+
:var (clojure-special-arg-indent-factor)
257+
(before-each
258+
(setq clojure-special-arg-indent-factor 1))
259+
(after-each
260+
(setq clojure-special-arg-indent-factor 2))
261+
262+
(put-clojure-indent 'cala 1)
263+
264+
(when-indenting-with-point-it "should handle a symbol with ns"
265+
"
266+
(cala top
267+
|one)"
268+
"
269+
(cala top
270+
|one)")
271+
(when-indenting-with-point-it "should handle special arguments"
272+
"
273+
(cala
274+
|top
275+
one)"
276+
"
277+
(cala
278+
|top
279+
one)"))
280+
236281
(describe "we can pass a lambda to explicitly set the column"
237282
(put-clojure-indent 'arsymbol (lambda (indent-point state) 0))
238283

0 commit comments

Comments
 (0)