简体   繁体   English

使用emacs C代码自动换行符的结构格式

[英]Format of structs using emacs c code auto-newline

I have recently started using emacs for editing C source, and have been using the auto-newline feature of cc-mode (c-toggle-auto-newline). 我最近开始使用emacs来编辑C源代码,并且一直在使用cc-mode的自动换行功能(c-toggle-auto-newline)。 This works well for constructs like functions and if/else statements, but seems to act strangely when a closing brace should be followed by a semi-colon. 这对于诸如函数和if / else语句之类的构造效果很好,但是当在右括号后加分号时,似乎表现得很奇怪。

Using auto-newline in GNU Emacs 23.3 I get: 在GNU Emacs 23.3中使用自动换行符,我得到:

struct foo
{
    int x;
}
    ;

char int[2] =
  {
    0, 1
  }
  ;

I would like to instead get: 我想得到:

struct foo {
    int x;
};

char int[2] = { 0, 1 };

How can I get the closing semi-colon to remain on the same line as the closing brace? 我怎样才能使结束分号保持与结束括号在同一行?

I don't think you can go around this problem with auto newline on. 我认为启用自动换行功能无法解决此问题。 It's not a greatly thought-through feature, it simply inserts newlines after certain characters ( ; , { , etc.). 这不是一个经过深思熟虑的功能,它只是在某些字符( ;{等)之后插入换行符。 But seriously, how hard is it to press and enter key? 但是说真的,按和输入键有多难? Any automation is always error-prone. 任何自动化总是容易出错的。

You can customize the "cleanup" behavior when auto-newline is turned on. 打开自动换行符后,您可以自定义“清除”行为。 This is controlled by the contents of the c-cleanup-list variable. 这由c-cleanup-list变量的内容控制。 (View help for this within Emacs by entering Ch v c-cleanup-list . (通过输入Ch v c-cleanup-list在Emacs中查看有关此问题的帮助。

Specifically, adding defun-close-semi to c-cleanup-list will solve your problem. 具体来说,将defun-close-semi添加到c-cleanup-list将解决您的问题。

If you're already defining a custom style within your ~/.emacs file, then you can probably figure out how to do this. 如果您已经在~/.emacs文件中定义了自定义样式,则可能可以找出方法。 Otherwise, the easiest way to change this setting is via Customize . 否则,更改此设置的最简单方法是通过Customize In the help buffer (displayed when you ran Ch v c-cleanup-list ), the last line will have a link to customize this variable. 在帮助缓冲区(运行Ch v c-cleanup-list )中,最后一行将具有自定义此变量的链接。

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

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