I’m following the Douglas Crockford’s code convention, but I can’t get the correct identation in JS mode in Emacs. I tried to customize the indent options of the mode, tried another modes like js3, but nothing seems to work.
When I have parenthesis, and I have to break the expression, Emacs indent like this:
this.offices.each(this.addOfficesToMap,
this);
While the convention that I’m following, says that I should leave just 4 spaces when an expression is broken up. So the indentation should look like:
this.offices.each(this.addOfficesToMap,
this);
Any idea of how I can change the indentation on broken up expressions?
The behaviour you want to change is hard-coded into a function called
js--proper-indentation. An inelegant fix to your problem would be to replace the function in your .emacs:I have modified three lines of code towards the bottom of the function. If you want your indentation to be 8 chars instead of 4, change the
(forward-char 4)line accordingly.Note that
js--proper-indentation(and the js library) requires the cl.el library, but that usingeval-after-loadmucks this up. So you need to explicitly requireclin your .emacs for this to work.Note that this ‘solution’ hard codes a 4 space indentation only for the situation you indicate, and does not handle nested code at all. But knowing the point in the code that deals with your situation should at least point you towards the bit that needs work for a more sophisticated solution.