简体   繁体   English

如何让Emacs正确缩进C99复合文字?

[英]How can I make Emacs properly indent C99 compound literals?

I'm writing a C99 program and using compound literals quite often. 我正在写一个C99程序并经常使用复合文字。 However, Emacs doesn't seem to indent them properly. 但是,Emacs似乎没有正确地缩进它们。

For example, here's a simple function, indented by hand: 例如,这是一个简单的函数,手工缩进:

SDL_Point
random_point_within_rect(SDL_Rect r)
{
    return (SDL_Point) {
        .x = (r.x + (rand() % r.w)),
        .y = (r.y + (rand() % r.h)),
    };
}

And here is how that looks when I use Mx indent-region : 以下是我使用Mx indent-region时的外观:

SDL_Point
random_point_within_rect(SDL_Rect r)
{
    return (SDL_Point) {
        .x = (r.x + (rand() % r.w)),
            .y = (r.y + (rand() % r.h)),
            };
}

My hypothesis is that Emacs doesn't recognise the braces in (type) { ... } as being the same sort of thing as in type x = { ... } , since this, which has exactly the same contents in the braces, works fine: 我的假设是,Emacs不认识(type) { ... }的大括号与type x = { ... }相同,因为这在括号中具有完全相同的内容, 工作良好:

SDL_Point
random_point_within_rect(SDL_Rect r)
{
    SDL_Point p = {
        .x = (r.x + (rand() % r.w)),
        .y = (r.y + (rand() % r.h)),
    };
    return p;
}

Is there a way I can get Emacs to treat compound literals like struct literals, or some other way to fix the indentation here? 有没有办法可以让Emacs处理复合文字,如结构文字,或其他一些方法来修复缩进?

Thanks. 谢谢。

I filed a bug report for that exact problem; 我为这个确切的问题提交了一份错误报告; I believe emacs is treating your list as a series of statements, and commas as comma operators. 我相信emacs将您的列表视为一系列语句,将逗号视为逗号运算符。

You can circumvent the problem by putting ; 你可以通过放置来规避问题; at the end of your lines. 在你的行结束。 You can also use unnamed struct initializers, ie 您还可以使用未命名的struct初始化器,即

return (SDL_Point){r.x + rand()%r.w, r.y + rand()%r.h}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM