r/emacs•
Salaadas

I would like to indent preprocessor directives with the same level of the code. How can I?

Hello, I would like to make Emacs automatically indent preprocessor directives in C++ like the following:

```

#if defined(__linux__) || defined(__APPLE__)

#include <pthread.h>

#elif defined(_WIN32)

#include "Platform_Windows.h"

#endif

struct Mutex

{

#if defined(__linux__) || defined(__APPLE__)

pthread_mutex_t csection;

#elif defined(_WIN32)

CRITICAL_SECTION csection;

#endif

bool debug_is_initialized = false; // @Incomplete: #ifdef this out.

};

struct Condition

{

#if defined(__linux__) || defined(__APPLE__)

pthread_cond_t event;

Mutex event_mutex;

bool condition_has_been_signalled = false;

#elif defined(_WIN32)

HANDLE event;

#endif

};

```

Now, I'm fully aware of the debate on whether preprocessors should be indented or not, but that's another topic for another day. How I can best do the above using electric indent from emacs? I have tried searching for the solution to this for the past days and I couldn't figure it out.

Thanks!

Posted Apr 15, 2025, 7:28 PM UTC

30
Loading comments…