Emacs C++ 模式缩进不正确?

2022-01-02 00:00:00 indentation c++ emacs

我正在使用 c++ 模式运行 emacs 23 并且有一些缩进问题.假设我有这个代码:

I'm running emacs 23 with c++-mode and having some indentation problems. Suppose I have this code:

void foo()
{
   if (cond)
     { <---
        int i;
        ...
     } <---
}

这似乎是自动缩进的默认行为.但是我想改变它,所以它会是这样的:

This seems to be the default behavior of the automatic indentation. However I'd like to change it so it'll be like this:

void foo()
{
   if (cond)
   {
      int i;
      ...
   }
}

有没有办法通过配置 C++ 模式或我的 .emacs 文件轻松地做到这一点?

Is there a way to do this easily by configuring c++ mode or my .emacs file?

推荐答案

我的 .emacs 文件中有以下内容:

I have the following in my .emacs file:

(defun my-c++-mode-hook ()
  (setq c-basic-offset 4)
  (c-set-offset 'substatement-open 0))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)

您可以通过在任何行上按 [ctrl-c ctrl-s] 来确定要编辑的偏移量.在 if 后面带大括号的第一行,它会说 substatement-open.

You can determine which offset to edit by hitting [ctrl-c ctrl-s] on any line. On the first line with a brace after the if it will say substatement-open.

相关文章