12

My java-mode in emacs wants to indent function arguments like this:

someLongFunctionName(
                     argumentNumberOne,
                     argumentNumberTwo,
                     argumentNumberThree,
                     argumentNumberFour
                     );

There are two problems here. Firstly, it wants to line up the start of the arguments with the end of the function name. Secondly, it wants to treat the the closet paren as if it were an argument, and thus lines it up with all other arguments. I don't like either of those behaviors.

I would much rather it indent my code like this:

someLongFunctionName(
    argumentNumberOne,
    argumentNumberTwo,
    argumentNumberThree,
    argumentNumberFour
);

c-mode does a much better job of this by default, and I would like to carry over the behavior to java-mode if possible.

I still need to learn how the emacs indentation engine works, and at the moment I don't honestly really even know that much lisp. Those two learning exercises are definitely on my plate, but at the moment a quick copy-paste solution would be pretty awesome.

fieldtensor
  • 3,972
  • 4
  • 27
  • 43

1 Answers1

14

Try this

(defun my-indent-setup ()
  (c-set-offset 'arglist-intro '+))
(add-hook 'java-mode-hook 'my-indent-setup)

From http://www.emacswiki.org/emacs/IndentingC

Rohan Monga
  • 1,759
  • 1
  • 19
  • 31
  • Awesome, that worked. I can't even venture a guess as to why there's a space after the word arglist-intro and before the closing quote, or why there's a + after it all; but it works. It's still indenting the closing paren in a silly way, but I suppose I can live with that, c-mode does the closing paren silly as well, and I've been living with that for years. Anyways, thanks a million; marking this as answered. – fieldtensor Aug 05 '11 at 07:07
  • 1
    heh, don't know much lisp but I usually trust emacswiki, you're welcome :D – Rohan Monga Aug 05 '11 at 07:52
  • Interestingly, that's not a closing quote. It's a quote on the +, so that it can be passed in as a name rather than evaluated right away. – db48x Aug 07 '11 at 05:51
  • 9
    @rutski Strings are delimited with double quotes in Lisp. It is the same as in Java. `'+` is an abbreviation for `(quote +)`. Same for `'arglist-intro` which means `(quote arglist-intro)`. SO does the syntax highlighting wrong. It appears as a SQL string but it is no string in Lisp. – ceving Jun 20 '13 at 15:38
  • Where do you type that into? – JW. Apr 14 '14 at 20:34
  • 2
    Further to my previous comment - I have found a brilliantly detailed answer here: http://stackoverflow.com/a/1365821/205814 – JW. Apr 14 '14 at 23:55
  • here you can see why `'+` btw, and what other options are available: https://www.emacswiki.org/emacs/IndentingC#toc13 – Emmanuel Touzery Nov 24 '16 at 13:25