Default indentation of list subforms
guns opened this issue · 6 comments
From an email, shared with permission:
On Fri 13 Dec 2013 at 04:46:33PM +0100, Michael Sappler wrote:
In my software team, some people use Emacs and some use Vim for
clojure development and the auto-indentation produces different
results. Basically I am asking if vim-clojure-static can be made to
behave a like clojure-mode of emacs.The case is documented here: https://github.com/clojure-emacs/clojure-mode#indentation-options
vim-clojure-static does this:
(my-foo-fn a b c)and emacs clojure-mode this:
(my-foo-fn a b c)
It appears to me that emacs is indenting incorrectly in this case. The
documentation you linked reads:
… "defun" indentation …
i. Known special forms (e.g. case, try, etc)
ii. Nearly all "core" macros that ship as part of Clojure itself
iii. Userland macros (and any other form?) that are locally
registered via put-clojure-indent, define-clojure-indent (helpers
for adding mappings to clojure-indent-function)
The difference between defun indentation and list indentation is that
subforms are indented two spaces to signify that the outer form is a
function call. Lists of things:
'(a
b
c)are aligned to the opening paren to signify that it is data.
Since you are calling my-foo-fn in your example, the proper
indentation style is defun-indent. The documentation indicates that
you can add custom symbols to the set of defun-indented symbols via
put-clojure-indent and define-clojure-indent, so this is not a
shortcoming in clojure-emacs.
IMO, defaulting to defun-indent instead of list-indent for
paren-delimited compound forms is a sensible choice in Clojure as
PersistentLists are most often created with the list function due to
the inconvenience of unquoting forms.
This is not necessarily true in CL or Scheme, so the opposite default is
probably more suitable for those languages.
Now, in the context presented, you are proposing to change the default
indentation of newline-separated paren-delimited forms to list-indent.
I do not think this is a wise change for the reasons above, but I am
always open to being convinced otherwise.
cf. a similar issue regarding defun-indent for vectors:
Reply from Michael Sappler, shared with permission:
What would you suggest for making both editors auto indentation feature
consistent? Should Emacs change?
Eclipse Counterclockwise also uses the same indentation as Vim.
Should Emacs change?
I don't think clojure-emacs is wrong in this instance, but I disagree with their default.
If you find my argument compelling, it appears that you can change the default indent of lists to defun-indent using the following:
(defcustom clojure-defun-style-default-indent nil
"Default indenting of function and macro forms using defun rules unless
otherwise defined via `put-clojure-indent`, `define-clojure-indent`, etc."
:type 'boolean
:group 'clojure
:safe 'booleanp)I am unfamiliar with Emacs, so please let me know if this is the correct toggle.
The difference between defun indentation and list indentation is that
subforms are indented two spaces to signify that the outer form is a
function call.
I interpret this differently--defun-indent is intended for things that are meant to be indented like defun: that is: macros where the first line is special and the rest in a & body argument. I don't believe regular function calls should be treated the same way.
Ah, this is very interesting. Tracing my exposure to lisp indenting conventions reveal that my opinions on indenting subforms correspond exactly with this document: http://www.ccs.neu.edu/home/dorai/scmindent/
I remember using this script back years ago when I was first hacking through SICP with vim and I needed (or thought I needed) a Scheme indenting script. Presumably, I formed my opinions through its use.
For reasons unknown, this style of subform indenting is also the way Vim's builtin lispindent works. Meikel Brandmeyer then presumably used the builtin lispindent function as a guide for writing the Clojure indent script.
I glanced back at my copy of SICP, and sure enough, the few times the second sexp is on the next line (and not a define), it is indented to the same column as the first symbol.
BTW, I miscontrued the use of defun-indent in Emacs. The lispindent in Vim is not as granular: there is only normal indenting, then "special" indenting for both macros and functions according to the guidelines in this commit:
The metric for inclusion in this set of "special" symbols has never corresponded cleanly with macro vs function, at least in Vim indenting conventions.
While Emacs obviously has tradition on its side, Vim has inherited a slightly different tradition from an unknown source. I am unwilling to change the default, since this will likely annoy existing users (like Meikel, who I inherited the script from).
However, since clojure-mode has an option to make indenting more vim-like, I will add an option to make regular subform indenting work like Emacs.
Okay, I added a new option: g:clojure_align_subforms. I think this should address your concerns. If there is anything else that you think could be more flexible to achieve harmony with clojure-mode, please let me know!
Thank you that solves the issue for our team.
You are probably right that changing the default will annoy existing users, but I would argue that in the long run this could save a lot of unnecessary diffs in projects that use mixed editors.