wto*_*tok 6 c emacs formatting indentation c99
我正在写一个C99程序并经常使用复合文字.但是,Emacs似乎没有正确地缩进它们.
例如,这是一个简单的函数,手工缩进:
SDL_Point
random_point_within_rect(SDL_Rect r)
{
return (SDL_Point) {
.x = (r.x + (rand() % r.w)),
.y = (r.y + (rand() % r.h)),
};
}
Run Code Online (Sandbox Code Playgroud)
以下是我使用时的外观M-x 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)),
};
}
Run Code Online (Sandbox Code Playgroud)
我的假设是,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;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以让Emacs处理复合文字,如结构文字,或者其他一些方法来修复缩进?
谢谢.
小智 0
我针对这个确切的问题提交了一份错误报告;我相信 emacs 将您的列表视为一系列语句,并将逗号视为逗号运算符。
您可以通过放置来规避该问题;在你的行的末尾。您还可以使用未命名的结构初始值设定项,即
return (SDL_Point){r.x + rand()%r.w, r.y + rand()%r.h}
Run Code Online (Sandbox Code Playgroud)