Skip to content

Commit

Permalink
Clean up the indentation config logic
Browse files Browse the repository at this point in the history
* Converted the type of `clojure-indent-style` to symbol for consistency
with Emacs configuration practices.
* Removed some legacy code related to clojure-defun-style-default-indent.
  • Loading branch information
bbatsov committed Jan 5, 2019
1 parent 5dc098d commit a4ed7a4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* [#489](https://github.com/clojure-emacs/clojure-mode/issues/489): Inserting parens before comment form doesn't move point.
* [#500](https://github.com/clojure-emacs/clojure-mode/pull/500): Fix project.el integration.

### Changes

* Change the accepted values of `clojure-indent-style` from keywords to symbols.

## 5.9.1 (2018-08-27)

* [#485](https://github.com/clojure-emacs/clojure-mode/issues/485): Fix a regression in `end-f-defun`.
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ want multi-line docstrings to be indented at all (which is pretty common in most
The indentation of function forms is configured by the variable
`clojure-indent-style`. It takes three possible values:

- `:always-align` (the default)
- `always-align` (the default)

```clj
(some-function
Expand All @@ -124,7 +124,7 @@ The indentation of function forms is configured by the variable
2)
```

- `:always-indent`
- `always-indent`

```clj
(some-function
Expand All @@ -136,7 +136,7 @@ The indentation of function forms is configured by the variable
2)
```

- `:align-arguments`
- `align-arguments`

```clj
(some-function
Expand All @@ -148,6 +148,9 @@ The indentation of function forms is configured by the variable
2)
```

**Note:** Prior to clojure-mode 5.10 the configuration options for `clojure-indent-style` used to be
keywords, but now they are symbols. Keywords will still be supported at least until clojure-mode 6.

#### Indentation of macro forms

The indentation of special forms and macros with bodies is controlled via
Expand Down
42 changes: 23 additions & 19 deletions clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
;; Artur Malabarba <[email protected]>
;; URL: http://github.com/clojure-emacs/clojure-mode
;; Keywords: languages clojure clojurescript lisp
;; Version: 5.10.0-snapshot
;; Version: 5.10.0
;; Package-Requires: ((emacs "25.1"))

;; This file is not part of GNU Emacs.
Expand Down Expand Up @@ -94,7 +94,7 @@
"Face used to font-lock Clojure character literals."
:package-version '(clojure-mode . "3.0.0"))

(defcustom clojure-indent-style :always-align
(defcustom clojure-indent-style 'always-align
"Indentation style to use for function forms and macro forms.
There are two cases of interest configured by this variable.
Expand All @@ -112,7 +112,7 @@ already use special indentation rules.
The possible values for this variable are keywords indicating how
to indent function forms.
`:always-align' - Follow the same rules as `lisp-mode'. All
`always-align' - Follow the same rules as `lisp-mode'. All
args are vertically aligned with the first arg in case (A),
and vertically aligned with the function name in case (B).
For instance:
Expand All @@ -122,30 +122,27 @@ to indent function forms.
merge
some-coll)
`:always-indent' - All args are indented like a macro body.
`always-indent' - All args are indented like a macro body.
(reduce merge
some-coll)
(reduce
merge
some-coll)
`:align-arguments' - Case (A) is indented like `lisp', and
`align-arguments' - Case (A) is indented like `lisp', and
case (B) is indented like a macro body.
(reduce merge
some-coll)
(reduce
merge
some-coll)"
:safe #'keywordp
:type '(choice (const :tag "Same as `lisp-mode'" :always-align)
(const :tag "Indent like a macro body" :always-indent)
:safe #'symbolp
:type '(choice (const :tag "Same as `lisp-mode'" 'always-align)
(const :tag "Indent like a macro body" 'always-indent)
(const :tag "Indent like a macro body unless first arg is on the same line"
:align-arguments))
'align-arguments))
:package-version '(clojure-mode . "5.2.0"))

(define-obsolete-variable-alias 'clojure-defun-style-default-indent
'clojure-indent-style "5.2.0")

(defcustom clojure-use-backtracking-indent t
"When non-nil, enable context sensitive indentation."
:type 'boolean
Expand Down Expand Up @@ -1370,6 +1367,10 @@ spec."
(let ((function (thing-at-point 'symbol)))
(clojure--get-indent-method function))))

(defun clojure--keyword-to-symbol (keyword)
"Convert KEYWORD to symbol."
(intern (substring (symbol-name keyword) 1)))

(defun clojure--normal-indent (last-sexp indent-mode)
"Return the normal indentation column for a sexp.
Point should be after the open paren of the _enclosing_ sexp, and
Expand All @@ -1395,19 +1396,22 @@ accepted by `clojure-indent-style'."
;; Here we have reached the start of the enclosing sexp (point is now at
;; the function name), so the behaviour depends on INDENT-MODE and on
;; whether there's also an argument on this line (case A or B).
(let ((case-a ; The meaning of case-a is explained in `clojure-indent-style'.
(let ((indent-mode (if (keywordp indent-mode)
;; needed for backwards compatibility
;; as before clojure-mode 5.10 indent-mode was a keyword
(clojure--keyword-to-symbol indent-mode)
indent-mode))
(case-a ; The meaning of case-a is explained in `clojure-indent-style'.
(and last-sexp-start
(< last-sexp-start (line-end-position)))))
(cond
;; For compatibility with the old `clojure-defun-style-default-indent', any
;; value other than these 3 is equivalent to `always-body'.
((not (memq indent-mode '(:always-align :align-arguments nil)))
((eq indent-mode 'always-indent)
(+ (current-column) lisp-body-indent -1))
;; There's an arg after the function name, so align with it.
(case-a (goto-char last-sexp-start)
(current-column))
;; Not same line.
((eq indent-mode :align-arguments)
((eq indent-mode 'align-arguments)
(+ (current-column) lisp-body-indent -1))
;; Finally, just align with the function name.
(t (current-column)))))))
Expand Down Expand Up @@ -1479,7 +1483,7 @@ This function also returns nil meaning don't specify the indentation."
(+ lisp-body-indent containing-form-column))
;; Further non-special args, align with the arg above.
((> pos (1+ method))
(clojure--normal-indent last-sexp :always-align))
(clojure--normal-indent last-sexp 'always-align))
;; Special arg. Rigidly indent with a large indentation.
(t
(+ (* 2 lisp-body-indent) containing-form-column)))))
Expand All @@ -1493,7 +1497,7 @@ This function also returns nil meaning don't specify the indentation."
(cond
;; Preserve useful alignment of :require (and friends) in `ns' forms.
((and function (string-match "^:" function))
(clojure--normal-indent last-sexp :always-align))
(clojure--normal-indent last-sexp 'always-align))
;; This should be identical to the :defn above.
((and function
(string-match "\\`\\(?:\\S +/\\)?\\(def[a-z]*\\|with-\\)"
Expand Down
10 changes: 5 additions & 5 deletions test/clojure-mode-indentation-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ values of customisable variables."
(let ((fname (intern (format "indentation/%s" description))))
`(ert-deftest ,fname ()
(let* ((after ,after)
(clojure-indent-style :always-align)
(clojure-indent-style 'always-align)
(expected-cursor-pos (1+ (s-index-of "|" after)))
(expected-state (delete ?| after))
,@var-bindings)
Expand Down Expand Up @@ -238,14 +238,14 @@ values of customisable variables."
(declare (indent 1))
(when (stringp style)
(setq forms (cons style forms))
(setq style :always-align))
(setq style 'always-align))
`(ert-deftest ,(intern (format "test-backtracking-%s" name)) ()
(progn
,@(mapcar (lambda (form)
`(with-temp-buffer
(clojure-mode)
(insert "\n" ,(replace-regexp-in-string "\n +" "\n " form))
(let ((clojure-indent-style ,style))
(let ((clojure-indent-style (quote ,style)))
(indent-region (point-min) (point-max)))
(should (equal (buffer-string)
,(concat "\n" form)))))
Expand Down Expand Up @@ -463,7 +463,7 @@ x
3))")

(def-full-indent-test align-arguments
:align-arguments
'align-arguments
"(some-function
10
1
Expand All @@ -473,7 +473,7 @@ x
2)")

(def-full-indent-test always-indent
:always-indent
'always-indent
"(some-function
10
1
Expand Down

0 comments on commit a4ed7a4

Please sign in to comment.