LrsNate
LrsNate

Reputation: 21

Change java-mode's indentation style in emacs

My emacs is configured with c-default-style as "linux" and c-basic-offset as 4, which I am really happy with and works like a charm with C and PHP. The thing is that when I try to edit some java code it is indented like this:

class Main
{
    public static void main(String args[])
        {
            System.out.println("Hello world!");
        }
}

Which is apparently Emacs's default behaviour for indenting and not the one I set for CC-mode (what bothers me is the indentation of the braces for the main function). I only started using Emacs a few months ago and I'm not that comfortable with Emacs Lisp, but really have no idea why java-mode isn't picking up cc-mode's indent style. Does someone have any clues with this ? Of course, disabling indent-tabs-mode is a solution but I have to say I don't find it satisfactory. I have a feeling this problem really has a simple solution, but I've been looking for hours...

Upvotes: 2

Views: 4328

Answers (2)

abo-abo
abo-abo

Reputation: 20342

Here's what works for me:

(require 'google-c-style)
(add-hook 'c-mode-common-hook
      (lambda()
            (subword-mode)
            (google-set-c-style)
            (google-make-newline-indent)
            (setq c-basic-offset 4)))

You'll need to download https://github.com/google/styleguide/blob/gh-pages/google-c-style.el and put it somewhere in your load path, for instance in ~/.emacs.d/.

Upvotes: 1

leif
leif

Reputation: 2037

Setting the "c style" (rules for indenting C-like languages) is described briefly here. In particular, you can use something like this to select "linux" for java-mode:

(setq c-default-style
      '((java-mode . "linux")))

I believe you need to select the style for each mode separately. java-mode probably isn't picking up on c-default-style as a string for some reason.

Upvotes: 0

Related Questions