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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/663588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:35:07  来源:igfitidea点击:

Emacs C++-mode incorrect indentation?

c++emacsindentation

提问by Idan K

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

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

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

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

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

回答by Alex B

I have the following in my .emacs file:

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

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

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 ifit will say substatement-open.

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

回答by justinhj

This is mine... this matches the default setup for visual studio.

这是我的……这与 Visual Studio 的默认设置相匹配。

(defun my-c-mode-common-hook ()
 ;; my customizations for all of c-mode, c++-mode, objc-mode, java-mode
 (c-set-offset 'substatement-open 0)
 ;; other customizations can go here

 (setq c++-tab-always-indent t)
 (setq c-basic-offset 4)                  ;; Default is 2
 (setq c-indent-level 4)                  ;; Default is 2

 (setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60))
 (setq tab-width 4)
 (setq indent-tabs-mode t)  ; use spaces only if nil
 )

(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

回答by Colin

Short answer: Put this line into your .emacs file:

简短回答:将此行放入您的 .emacs 文件中:

(c-set-offset 'substatement-open 0)

Long answer: ...

长答案:...

For those of us who are new to emacs-lisp, there is a pretty simple method at http://www.cs.cmu.edu/:

对于我们这些刚接触 emacs-lisp 的人来说,http: //www.cs.cmu.edu/ 上有一个非常简单的方法:

  • Go to the line you want to indent

  • Type C-C C-O (that's the letter "O", not zero)

  • Press Enterto accept the default suggestion

  • Type "0" (that's a zero) for no extra indentation, press Enter

  • Type Tabto reindent the line.

  • Future "{" in this situation will have the correct tab setting, until you restart emacs.

  • 转到要缩进的行

  • 键入 CC CO(即字母“O”,而不是零)

  • Enter接受默认建议

  • 键入“0”(即零)表示没有额外的缩进,按Enter

  • 键入Tab以重新缩进该行。

  • 在这种情况下,未来的“{”将具有正确的选项卡设置,直到您重新启动 emacs。

The nice thing about this method, is that you can actually seethe lisp code that you want to change. You can put in your .emacs file:

这种方法的好处是,您实际上可以看到要更改的 lisp 代码。您可以放入 .emacs 文件:

(c-set-offset 'SYNTACTIC-SYMBOL OFFSET)

Additionally, you may want to check out the program AStyleto automatically format C++ source outside of emacs.

此外,您可能需要查看程序AStyle以在 e​​macs 之外自动格式化 C++ 源代码。

回答by chen bin

The accepted answer is actually wrong. Emacswiki won't help.

接受的答案实际上是错误的。Emacswiki 不会有帮助。

Insert following code into ~/.emacs:

将以下代码插入 ~/.emacs:

(defun fix-c-indent-offset-according-to-syntax-context (key val)
  ;; remove the old element
  (setq c-offsets-alist (delq (assoc key c-offsets-alist) c-offsets-alist))
  ;; new value
  (add-to-list 'c-offsets-alist '(key . val)))

(add-hook 'c-mode-common-hook
          (lambda ()
            (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
              ;; indent
              (fix-c-indent-offset-according-to-syntax-context 'substatement-open 0))
            ))

See http://blog.binchen.org/posts/ccjava-code-indentation-in-emacs.htmlfor technical details.

有关技术详细信息,请参阅http://blog.binchen.org/posts/ccjava-code-indentation-in-emacs.html

The key issue is c-set-offset is not reliableto detect syntax context any more (Emacs24.3.1). So the only reliable way is to analyze the original emacs code. The detailed steps to hack the code is listed in my article, basically you need read the function c-indent-linewhich is defined in /usr/share/emacs/24.3/lisp/progmodes/cc-cmds.el

关键问题是 c-set-offset不再可靠地检测语法上下文(Emacs24.3.1)。所以唯一可靠的方法是分析原始的emacs代码。我的文章中列出了破解代码的详细步骤,基本上你需要阅读/usr/share/emacs/24.3/lisp/progmodes/cc-cmds.el 中定义的函数c-indent-line

Some people complained that my setup does not work. Actually it works in all stableversions of Emacs (23.4, 24.3, 24.4) if you don't change default setup (The indention has different profiles, my setup is based on default profile).

有些人抱怨我的设置不起作用。实际上,如果您不更改默认设置(缩进具有不同的配置文件,我的设置基于默认配置文件),则它适用于所有稳定版本的 Emacs(23.4、24.3、24.4)。

My key point is, on this specific issue, you need read the code.

我的重点是,在这个特定问题上,您需要阅读代码。

回答by Mekk

Before tuning individual offsets, configure preferable style. I use stroustrup, you my try some others, see http://www.emacswiki.org/emacs/IndentingC#toc1for names and http://en.wikipedia.org/wiki/Indent_style#Variant:_Stroustrupfor examples

在调整单个偏移量之前,配置首选样式。我使用 stroustrup,你我尝试其他一些,参见http://www.emacswiki.org/emacs/IndentingC#toc1名称和http://en.wikipedia.org/wiki/Indent_style#Variant:_Stroustrup示例

M-x customize-variable c-default-style (and set your preferred for C++_

Mx 自定义变量 c-default-style(并设置您的首选 C++_