23

在 Emacs 中,如何强制规定最大行长,例如 80 个字符?我希望它在我的代码中插入正确的换行符,就像fill-paragraph文本一样,如果可能的话,正确插入行的第二部分。

这里有一个小例子:

LongNameType<PrettyLong, AlsoLong> doSomethingWithLongFunctionName(int a, int b);
foo();

如果我真的这样做fill-paragraph,它会变成:

LongNameType<PrettyLong, AlsoLong>
doSomethingWithLongFunctionName(int a, int b); foo();

而我更喜欢这个:

LongNameType<PrettyLong, AlsoLong>
  doSomethingWithLongFunctionName(int a, int b);
foo();
4

7 回答 7

5

许多软件包会警告您行长限制。就个人而言,我使用wide-column,它会根据当前列更改光标颜色。

于 2009-04-13T18:15:25.917 回答
4

fill-paragraph 和 auto-fill-mode 故意不包装代码。有太多的方法可以做到这一点,它可能会出错。他们会包装评论,但这对你没有帮助。

我曾经做过的唯一方法是明确地将我希望文本中断的位置。然后自动缩进应该把虚线放在正确的位置。

您是否正在尝试重排大量现有代码?或者尝试对您现在正在编写的代码进行自动填充工作?

于 2009-04-07T13:10:28.113 回答
1

Not really an emacser, but what happens if you turn on auto-fill-mode while in c++-mode?

C++ mode should give you auto-indent, and auto-fill-mode gives you line-wrapping....

于 2009-04-07T01:54:10.657 回答
1

I use modeline-posn package. It highlights column number in the modeline if it's greater than specified value.

于 2009-04-07T02:07:22.280 回答
1

您应该查看 Emacs 的许多“垂直线”库之一。有些库始终在整个缓冲区上保留一条垂直突出显示线(不是您真正想要的),但其他库始终将垂直突出显示放在固定列上,这并不是您真正想要的,但您可以立即看到当你应该换行时。

于 2009-04-17T00:52:48.010 回答
0

尝试

'(c-max-one-liner-length 80)

'(fill-column 80)
'(c-ignore-auto-fill (quote (string cpp)))

希望能帮助到你。

于 2009-05-15T12:43:45.513 回答
0

您可以使用更高级的clang-format包。

  • 你必须安装 clang-fromat 以及它的 emacs 包。
  • 将此添加到您的 .emacs(setq clang-format-style "file")或添加到custom-set-variables '(clang-format-style "file").
  • 生成您的模板样式,clang-format -style=gnu -dump-config > .clang-format然后将其放入项目的根目录中。
  • 根据需要自定义 .clang-format 并更改ColumnLimit: 120为 80 或任何您想要的值。

这将使用 clang-format 工具强制列限制。

参考: ClangFormatStyleOptions。 相关问题

于 2020-12-12T14:08:03.150 回答