Luca Martini
Luca Martini

Reputation: 1474

Emacs indent template class/functions

Does anybody know how to inhibit emacs from indenting the name of functions or classes after a template clause?

Currenty the result is:

template <typename T>
    class A {
    /* ... */
    };

where I would like to have:

template <typename T>
class A {
/* ... */
};

Thank you very much for your help.

EDIT 1 I'm using c++-mode with java as indent style for c++. I customized the c-offset-alist in this way:

(custom-set-variables 
;;
'(c-offsets-alist (quote ((case-label . +) (innamespace . 0))))

Upvotes: 6

Views: 2087

Answers (4)

D&#233;j&#224; vu
D&#233;j&#224; vu

Reputation: 28850

Go to the class line and hit TAB to perform the (unsolicited) indentation.
Then press Control-CControl-Oto display the indent mode fortopmost-intro-cont`

Press ENTER, then you can change the indent number (3 to 0 for instance).

At the end of your .emacs you can set permanently that instruction:

  (c-set-offset 'topmost-intro-cont 0 nil)

Upvotes: 9

jwernerny
jwernerny

Reputation: 7048

A couple of different things to check:

I've seen similar issues when editing C++ .h files in C-mode instead of C++-mode. By default, .h files are C-mode, not C++-mode. [You can check this by looking for "C++" or "C" in parenthesis at the bottom of your window.] You can setup emacs to always open .h files as C++ by using the following in your .emacs

(setq auto-mode-alist (append '(("\\.h\\'"   . c++-mode)
                )
                  auto-mode-alist
                  ))

The other thing to check is how you have setup your c-default-style. The info page for "CC Mode" goes into a lot more detail on all of the possibilities.

Upvotes: 1

darioo
darioo

Reputation: 47213

There are different styles for indentation for Emacs' C++ mode. Quoting EmacsWiki:

A partial list of the better known C styles:

  • “gnu”: The default style for GNU projects
  • “k&r”: What Kernighan and Ritchie, the authors of C used in their book
  • “bsd”: What BSD developers use, aka “Allman style” after Eric Allman.
  • “stroustrup”: What Stroustrup, the author of C++ used in his book
  • “linux”: What the Linux developers use for kernel development
  • “python”: What Python developers use for extension modules
  • “java”: The default style for java-mode (see below)
  • “user”: When you want to define your own style

The c-default-style variable is what you need to change. Perhaps one of them will be what you need. Don't have Emacs right now, so I can't check them out.

Upvotes: 4

Thunder Rabbit
Thunder Rabbit

Reputation: 5459

I don't know, but I imagine your mode makes a difference. In what mode are you editing? I assume c++-mode cause you have c++ as a tag.

For me, in c++-mode, it turned out like this:

template <typename T>
class A {
   /* ... */
};

With the comments indented, but class A not indented.

Upvotes: 1

Related Questions