How to make emacs treat #ifdef and #endif as '{' and '}'?

I would like emacs to handle "#ifdef" just like "{" and "#endif" as "}" relative to indentation. For instance:

#ifdef __linux__ #include <sys/socket.h> #endif int func(void) { int foo = 0; #ifdef DO_STUFF foo = do_stuff(); #endif return foo; } 

instead:

 #ifdef __linux__ #include <sys/socket.h> #endif int func(void) { int foo = 0; #ifdef DO_STUFF foo = do_stuff(); #endif return foo; } 

Communicating with "cpp-macro" does not do the trick. How can I do it? Thanks!

+6
source share
2 answers

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

You can find a lot of information about emacs indentation here: http://www.emacswiki.org/emacs/IndentingC

+4
source

Preprocessor comments were supposed to start in the first column, so emacs is true there, however these days, compilers usually allow backtracking. (See Indentation #defines )

However, see the indent preprocessor directives as emacs C code for a discussion of this. Infact, I could try to close this question as a duplicate of this.

I agree with some comments on this issue, since it is erroneous to think of the preprocessor as a block or lexical scope, so it is actually harmful to backtrack from it as you would with regular C.

+3
source

Source: https://habr.com/ru/post/907524/


All Articles