开发者

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开发者_如何学C 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))))


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)


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜