Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make emacs treat #ifdef and #endif like '{' and '}'?

I'd like emacs to treat "#ifdef" exactly like "{" and "#endif" like "}" in relation to indentation. Like so:

#ifdef __linux__
    #include <sys/socket.h>
#endif

int func(void)
{
    int foo = 0;

    #ifdef DO_STUFF
        foo = do_stuff();
    #endif

    return foo;
}

instead of:

#ifdef __linux__
#include <sys/socket.h>
#endif

int func(void)
{
    int foo = 0;

#ifdef DO_STUFF
    foo = do_stuff();
#endif

    return foo;
}

Messing around with "cpp-macro" doesn't do the trick. How would I do it? Thanks!

like image 581
colding Avatar asked Feb 02 '12 22:02

colding


People also ask

How do I change EMAC mode?

You can explicitly select a new major mode by using an M-x command. Take the name of the mode and add -mode to get the name of the command to select that mode (e.g., M-x lisp-mode enters Lisp mode).

How do I run Emacs commands?

You can run any Emacs command by name using M-x , whether or not any keys are bound to it. If you use M-x to run a command which also has a key binding, it displays a message to tell you about the key binding, before running the command.

How do I insert files into Emacs?

If you want to insert the contents of another file into the current buffer, place the cursor at the desired insertion point, and type Control-X-I. Emacs will ask you for the name of the file you wish to insert. You may also insert text by cutting it from one place, and pasting it at the insertion point.


2 Answers

You can use this el file for emacs : http://www.emacswiki.org/emacs/ppindent.el

You can find many info about emacs indentation here : http://www.emacswiki.org/emacs/IndentingC

like image 184
ClemPi Avatar answered Oct 08 '22 01:10

ClemPi


Preprocessor comments are were supposed to start in the first column, so emacs is correct there, however these days compilers typically allow them to be indented. (See Indenting #defines)

That said, see Indent preprocessor directives as C code in emacs for a discussion about this. Infact, I might try and close this question as a duplicate of that.

I agree with some of the comments on that issue, in that it is a mistake to think of the preprocessor as being block or lexically scoped, so it is actually harmful to indent it in the same way as you do with the regular C code.

like image 42
Arafangion Avatar answered Oct 08 '22 00:10

Arafangion