5

If my .emacs is empty or if it contains only the lines

(require 'cc-mode)
(add-to-list 'c-offsets-alist '(annotation-top-cont .0))

(add-hook 'java-mode-hook
          '(lambda () (c-set-offset 'annotation-top-cont 0)))

then instead of having protected aligned with @Override

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

protected comes out instead indented relative to @Override

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

What changes should I make to my .emacs so that the indentation comes out as the top sample above?

Calaf
  • 10,113
  • 15
  • 57
  • 120
  • I think you should consider using Eclipse or some other IDE; it would improve your productivity tremendously. I use emacs a lot, but never for Java. – Andrew Mao Mar 06 '13 at 18:52
  • I agree that Eclipse is terrific. I switch back and forth between Eclipse and emacs (and this issue in the only difference in indentation between the two). At this time I find both necessary, because emacs has a formidable number of features that Eclipse lacks. – Calaf Mar 06 '13 at 19:22

2 Answers2

0

Look to annotation-top-cont style for c-offsets-alist definition. To make no identing you need to set it to 0. More information in CC-mode manual.

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
  • Sorry, Alex, could you check again? annotation-top-cont is not sufficient to solve the problem. – Calaf Mar 06 '13 at 18:25
  • I think, that the problem is that you set c-offset-alist globally, but in documentation states that this variable becomes local if it's set in any fashion. You need to use the `(c-set-offset 'annotation-top-cont 0)` in the `java-mode-hook` function... – Alex Ott Mar 07 '13 at 19:31
  • It's still work-in-progress. I modified the question to include your suggestion. – Calaf Mar 07 '13 at 21:19
0

I found this solution on an EMACS mailing list:

(add-hook 'java-mode-hook
      '(lambda ()
         "Treat Java 1.5 @-style annotations as comments."
         (setq c-comment-start-regexp 
           "\\(@\\|/\\(/\\|[*][*]?\\)\\)")
         (modify-syntax-entry ?@ "< b" 
                  java-mode-syntax-table)))

It treats the @annotations as comments, which will cause it to indent properly.

Source: http://lists.gnu.org/archive/html/help-gnu-emacs/2011-04/msg00262.html

Tomas Skäre
  • 174
  • 5