[求助] indent-region 怎么设置行宽?

最近写一个C++项目,因为某些原因,需要120行宽,但是indent-region格式化的时候,默认是80行宽。

(setq-default fill-column 120)

设置fill-column,查看这个变量确实生效了,但是indent-region的时候,还是按80行宽换行,求助社区。

clang-format 然后 tab的时候调用此程序

可以具体点吗?调整哪个程序?

indent-region的文档,

With no prefix argument, the command chooses one of these methods and
indents all the lines with it:

  1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
     beginning of each line in the region that does not already begin
     with it.
  2) If `indent-region-function' is non-nil, call that function
     to indent the region.

我猜 fill-prefix是nil,所以应该是第二条规则起作用。其默认值是c-indent-region。它又调用了c-indent-line.

建议你给个具体的例子和详细的重现步骤,否则真的不好回答。

我很久不用C++了,这是之前我写的文章 C/C++/Java code indentation in Emacs | Chen's blog , 给出了分析c-indent-line的技术细节。

你也可以到 emacs stackexhange 上 搜一下同类问题。

我这个解决方案是依赖外部程序 clang-format

不是emacs的原生解决方案

就是通过按键调用 命令行程序 完整代码格式化

非常感谢提供思路,但是还是没有找到解决方案。

问题是这样的:

   0 int function_name_is_very_very_very_very_very_very_very_very_very_long(int a, int b, int c, int d, int e) {
   1   if (d + e < 0) {
   2     function_name_is_very_very_very_very_very_very_very_very_very_long(a, b, c, d, e);
   3   }
   4   return a + b + c;
   5 }

这个是原始代码,函数名字超过80个字符,调用函数的地方也超过80字符,选中这个region之后,调用indent-region 得到的效果是这样的:

   0 int function_name_is_very_very_very_very_very_very_very_very_very_long(
   1     int a, int b, int c, int d, int e) {
   2   if (d + e < 0) {
   3     function_name_is_very_very_very_very_very_very_very_very_very_long(a, b, c,
   4                                                                        d, e);
   5   }
   6   return a + b + c;
   7 }

我期望indent-region以120个字符作为行宽,即不超过120个字符不换行。 我的环境fill-prefix确实是nil,indent-region-function 是和major mode相关的,我的对应的是lsp-format-region。但是没用找到相关设置相关变量的地方。

格式化规则在哪里定义呢,比如哪里可以设置行宽?我的lsp-format似乎和你这个类似。

clang-format有配置文件 你可以google一下

非常感谢! 在项目根目录下新建.clang-format文件解决该问题;

$ cat .clang-format
ColumnLimit:    120

参考链接: https://my.oschina.net/u/4369588/blog/4401497

虽然解决了 毕竟依赖外部程序 emacs 肯定也可以搞定 等大神hack

试了你的例子,我没有任何问题。我已经很久不用C++了,以前的设置全忘了,不过在我的配置里,有这样的代码,

;; avoid default "gnu" style, use more popular one
(setq c-default-style '((java-mode . "java")
                        (awk-mode . "awk")
                        (other . "linux")))

看注释可能和c-default-style有关。