Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I configure emacs in java mode so that it doesn't automatically align method parameters

In emacs, when I type:

public void foo(String one,
    String two) {

It tabifies like this:

public void foo(String one,
                String two) {

I'd rather it didn't, and just aligned parameters like other line continuations. How can I configure it not to do this?

like image 642
Brian Duff Avatar asked Sep 02 '09 02:09

Brian Duff


1 Answers

This comes from the Info manual for Emacs CC Mode, using GNU Emacs 23.1 on Windows:

  1. Start building your Java class that's not indenting properly. In your case, exactly what you've typed above.
  2. Move your cursor to the start of the line that's not indenting properly. In your case, "String two) {".
  3. Hit C-c C-s (c-show-syntactic-information) to ask Emacs what syntax element it thinks you're looking at. In your case, it'll say something like ((arglist-cont-nonempty n m)).
  4. Use C-c C-o (c-set-offset) to tell it you want to change the indentation level for this syntactic element.
  5. It defaults to what it thinks that syntactic element is, e.g., arglist-cont-nonempty. Just hit RET if that default is correct.
  6. Now it wants to know what expression to use to calculate the offset. In your case, the default is an elisp expression. Delete that, and just use a single plus sign + instead.
  7. Test it out to make sure it's working correctly: Hit TAB a bunch on different lines, or M-x indent-region or similar.
  8. To make it permanent, add this to your .emacs file:

(setq c-offsets-alist '((arglist-cont-nonempty . +)))

like image 59
Chris Jones Avatar answered Oct 26 '22 00:10

Chris Jones