Indent preprocessor directives as C code in emacs

Emacs does not have processor backing code by default. I know that he has historical roots that are already outdated.

However, with code with lots of #ifdef unindented, it's hard to read.

So, I would like to indent emacs automatically, give me something like this:

void myfunc() { int foo; #ifdef BAR printf(foo); #endif return foo; } 

Instead of what I am getting now:

 void myfunc() { int foo; #ifdef BAR printf(foo); #endif return foo; } 

Any conclusions on this issue you crack hackers :)?

+4
source share
1 answer

You can simply tell Emacs to add an offset to the lines of the preprocessor.

  • Place the cursor ( point ) in the preprocessor line
  • then press Cc Co (control-c control-o)
  • the minibuffer should say Syntactic symbol to change:
  • type cpp-macro , press Enter
  • Enter a new offset (number - usually 0 )

Then a TAB on each preprocessor line should correctly indent. (or Mx indent-region ...).

For changes to be permanent, you can, for example, add the necessary lines to your .emacs file.
An easy way to copy a previously entered command is cx ESC ESC and use the arrow keys to find the command (c-set-offset ...) Elisp.

It should be

 (c-set-offset (quote cpp-macro) 0 nil) 
+12
source

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


All Articles