如何让Emacs用我的代码缩进我的//注释?

时间:2023-01-24 23:06:49

I don't know much about Emacs, but after some googling, I edited my .emacs file to be as follows:

我对Emacs了解不多,但经过一些谷歌搜索后,我编辑了我的.emacs文件如下:

(setq c-default-style "bsd" c-basic-offset 4)

My goal was to get Allman-style indenting with 4-spaced tabs. It works as expected, but now my // comments aren't indented with my code. Before I changed this, when I would type //, it would get auto-indented to be in line with the rest of the code in the function. How can I get Emacs to auto-indent // comments?

我的目标是通过4个间距的标签获得Allman风格的缩进。它按预期工作,但现在我的//注释没有缩进我的代码。在我更改之前,当我输入//时,它将自动缩进以与函数中的其余代码一致。如何让Emacs自动缩进//评论?

I've tried adding c-indent-comments-syntactically-p 1 to the above .emacs file, but that didn't change it...

我已经尝试将c-indent-comments-syntactically-p 1添加到上面的.emacs文件中,但是没有改变它...

For example:

int main()
{
    // I'd like this line to be auto-indented to match the block
    for (int i = 0; i < 10; ++i)
    {
        // And this line to be auto-indented to match the block
        doStuff();
    }
}

Currently, TAB does not indent my // comment, and it doesn't automatically indent either.

目前,TAB不会缩进我的//注释,也不会自动缩进。

3 个解决方案

#1


8  

You can inspect and change the value of the current indent by placing point on the concerned line and pressing C-c C-o. Adjust the relevant symbols to your liking.

您可以通过在相关线上放置点并按C-c C-o来检查和更改当前缩进的值。根据自己的喜好调整相关符号。

This wont be permanent. Use direct invocation of the function c-set-offset in your .emacs to make the changes globally.

这不会永久。使用.emacs中函数c-set-offset的直接调用来全局进行更改。

Simple example:

int main() {
  // 
}

This is my default indent. After moving the cursor to line 2 I see that the relevant symbol is comment-intro.

这是我的默认缩进。将光标移动到第2行后,我看到相关的符号是注释介绍。

Using:

(c-set-offset 'comment-intro 6)

I get:

int main() {
        //
}

Offset accumulates across symbols:

偏移累积符号:

int main() {
        //
  {
          //
  }
}

#2


1  

Hitting TAB still indents your comment line, right? Then what you're missing is the automatic recognition of the double-slash triggering the autoindentation. That is implemented by (c-electric-slash). First be sure that slash is still bound to that command: C-h k / (help on keystroke slash). Then, assuming the documentation for c-electric-slash shows up, read it to figure out under what conditions it's inhibited.

点击TAB仍会缩进评论行,对吧?那么你所缺少的是自动识别触发自动压缩的双斜线。这是通过(c-electric-slash)实现的。首先要确保斜杠仍然绑定到该命令:C-h k /(帮助键击斜杠)。然后,假设c-electric-slash的文档显示出来,请阅读它以确定它在什么条件下被禁止。

#3


0  

If you want to put your comment on the same alignement of your code scope, just add the folllowing line to your ~/.emacs file:

如果您想将注释放在代码范围的相同对齐中,只需将以下行添加到〜/ .emacs文件中:

(c-set-offset 'comment-intro 0)

#1


8  

You can inspect and change the value of the current indent by placing point on the concerned line and pressing C-c C-o. Adjust the relevant symbols to your liking.

您可以通过在相关线上放置点并按C-c C-o来检查和更改当前缩进的值。根据自己的喜好调整相关符号。

This wont be permanent. Use direct invocation of the function c-set-offset in your .emacs to make the changes globally.

这不会永久。使用.emacs中函数c-set-offset的直接调用来全局进行更改。

Simple example:

int main() {
  // 
}

This is my default indent. After moving the cursor to line 2 I see that the relevant symbol is comment-intro.

这是我的默认缩进。将光标移动到第2行后,我看到相关的符号是注释介绍。

Using:

(c-set-offset 'comment-intro 6)

I get:

int main() {
        //
}

Offset accumulates across symbols:

偏移累积符号:

int main() {
        //
  {
          //
  }
}

#2


1  

Hitting TAB still indents your comment line, right? Then what you're missing is the automatic recognition of the double-slash triggering the autoindentation. That is implemented by (c-electric-slash). First be sure that slash is still bound to that command: C-h k / (help on keystroke slash). Then, assuming the documentation for c-electric-slash shows up, read it to figure out under what conditions it's inhibited.

点击TAB仍会缩进评论行,对吧?那么你所缺少的是自动识别触发自动压缩的双斜线。这是通过(c-electric-slash)实现的。首先要确保斜杠仍然绑定到该命令:C-h k /(帮助键击斜杠)。然后,假设c-electric-slash的文档显示出来,请阅读它以确定它在什么条件下被禁止。

#3


0  

If you want to put your comment on the same alignement of your code scope, just add the folllowing line to your ~/.emacs file:

如果您想将注释放在代码范围的相同对齐中,只需将以下行添加到〜/ .emacs文件中:

(c-set-offset 'comment-intro 0)