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!
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).
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With