How can I make Emacs properly indent C99?

I write a C99 program and often use complex literals. However, Emacs does not seem to back down properly.

For example, here is a simple function with manual indentation:

SDL_Point random_point_within_rect(SDL_Rect r) { return (SDL_Point) { .x = (rx + (rand() % rw)), .y = (ry + (rand() % rh)), }; } 

And this is how it looks when I use Mx indent-region :

 SDL_Point random_point_within_rect(SDL_Rect r) { return (SDL_Point) { .x = (rx + (rand() % rw)), .y = (ry + (rand() % rh)), }; } 

My hypothesis is that Emacs does not recognize curly braces in (type) { ... } as the same things as type x = { ... } , since it that has exactly the same content in curly brackets, works fine:

 SDL_Point random_point_within_rect(SDL_Rect r) { SDL_Point p = { .x = (rx + (rand() % rw)), .y = (ry + (rand() % rh)), }; return p; } 

Is there a way to get Emacs to process complex literals, such as string literals, or some other way to fix the indent here?

Thanks.

+6
source share
1 answer

I wrote a bug report for this exact problem; I believe that emacs treats your list as a series of statements, and commas as comma operators.

You can get around the problem by setting; at the end of your lines. You can also use initializers of an unnamed structure, i.e.

 return (SDL_Point){rx + rand()%rw, ry + rand()%rh} 
0
source

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


All Articles