如何在 Emacs 中强制最大行长度?

发布于 2024-07-16 17:06:10 字数 566 浏览 14 评论 0原文

在 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();

In Emacs, how can I enforce a maximum line length of, say, 80 characters? I want it to insert proper line breaks in my code, much like fill-paragraph for text, if possible, with the correct insertion of the second part of the line.

Here a little example:

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

If I actually do fill-paragraph it becomes:

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

whereas I'd prefer this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

冬天旳寂寞 2024-07-23 17:06:11

并不是真正的 emacser,但是如果您在 自动填充模式 中打开 < a href="http://www.emacswiki.org/emacs/CPlusPlusMode" rel="nofollow noreferrer">c++-模式?

C++ 模式应该为您提供自动缩进,自动填充模式为您提供换行......

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....

青丝拂面 2024-07-23 17:06:11

我使用 modeline-posn 包。 如果模型行中的列号大于指定值,它会突出显示该列号。

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

最单纯的乌龟 2024-07-23 17:06:11

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

You should check out one of the many "vertical line" libraries for Emacs. Some keep a vertical highlight line over the entire buffer at point at all times (not really what you want) but other libraries put the vertical highlight on a fix column at all times, which is not really what you want, but you can immediately see when you ought to be wrapping lines.

寒尘 2024-07-23 17:06:11

尝试

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

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

希望有帮助。

Try

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

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

Hope it helps.

尛丟丟 2024-07-23 17:06:11

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

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

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

参考:
ClangFormatStyleOptions。
相关问题

You could use the more advanced clang-format package.

  • You have to install clang-fromat along with it's emacs package.
  • Add this to your .emacs (setq clang-format-style "file") or add to custom-set-variables '(clang-format-style "file").
  • Generate your template style by clang-format -style=gnu -dump-config > .clang-format then place into your project's root.
  • Customize .clang-format as you like and change ColumnLimit: 120 to 80 or whatever value you want.

That would force column limit using clang-format tool.

Reference:
ClangFormatStyleOptions.
Related question

隱形的亼 2024-07-23 17:06:10

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

There are a number of packages which warn you of line length limits. Personally, I use wide-column, which changes the cursor color depending on its current column.

删除会话 2024-07-23 17:06:10

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

我这样做的唯一方法是明确地将文本放置在我想要文本中断的位置。 然后自动缩进应该将断线放在正确的位置。

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

fill-paragraph and auto-fill-mode deliberately don't wrap code. There are just too many ways to do it and it'd probably get it wrong. They will wrap comments, but that doesn't help you here.

The only way I've ever done it to to explicitly put the where I want the text to break. Then the auto-indent should put the broken line in the right place.

Are you trying to reflow a large body of existing code? Or trying to have auto-fill work on code you are writing now?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文