Emacs cc-mode缩进问题与C ++ 0x枚举类 [英] Emacs cc-mode indentation problem with C++0x enum class

查看:184
本文介绍了Emacs cc-mode缩进问题与C ++ 0x枚举类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Emacs cc-mode似乎还没有识别C ++ 0x中引入的类型安全的枚举类。我得到的结果是第二,第三等枚举的双重缩进:

 枚举类颜色{
蓝色,
红色,
橙色,
绿色
};

我想要的是:

 枚举类颜色{
蓝色,
红色,
橙色,
绿色
};

你可以推荐一个很好的命令来添加到 .emacs 这将使cc-mode对待枚举类与对待普通旧的枚举

cc-mode依赖于关键字是单词的假设。添加对 enum_class 而不是枚举类的支持只是一个改变几个regexps的问题。



相反,Emacs将其视为一个类。解决这个问题的正确方法是教Emas,这是一个枚举。但是这超出了答案的范围。



这是黑客:



所以我们将修改在这种情况下,现有的缩进行为有所不同。 (此代码可用于修改此要点)。

 
(defun inside-class-enum-p(pos)
检查POS是否在C ++ \\枚举类\\中的大括号内。
(忽略错误
(save-excursion
(goto-char pos)
(up-list -1)
(backward-sexp 1)
返回枚举[\t] +类[\t] + [^}] +))))

(defun align-enum-class(langelem)
(内部类 - 枚举(c-langelem-pos langelem))
0
(c-lineup-topmost-intro-cont langelem))

(defun align-enum-class-closing-brace(langelem)
(if(inside-class-enum-p(c-langelem-pos langelem))
' -
'+))

(defun fix-enum-class()
安装程序`c ++ - 模式'以更好地处理\类枚举\\
(加入列表'c-offsets-alist'(topmost-intro-cont。align-enum-class))
(add-to-list'c-offsets-alist
'(statement-cont。align-enum-class-closing-brace)))

(add-hook'c ++ - mode-hook'fix-enum-class)

这没有经过严格的测试。 ;)



工作原理:



c-offsets-alist 确定语法树中不同位置的缩进。它可以分配常量或函数。



这两个函数可以确定当前位置是否位于 枚举类中{...} 。如果是这种情况,它们返回0或' - ,将cc模式解释为缩进深度。如果不是,它们返回默认值。



inside-class-enum-p 到前一个大括号,并检查它之前的文本是枚举类。


Emacs cc-mode does not appear to yet recognize the type-safe enum class introduced in C++0x. The result I get is a double indentation for second, third, etc enums:

enum class Color {
    Blue,
        Red,
        Orange,
        Green
        };

What I would like is:

enum class Color {
    Blue,
    Red,
    Orange,
    Green
};

Can you recommend a good command to add to .emacs which will make cc-mode treat enum class the same way it treats the plain old enum?

解决方案

This is the problem:

cc-mode relies somewhat on the assumption that keywords are single words. Adding support for enum_class instead of enum class would just be a matter of changing a few regexps.

Instead Emacs treats this as a class. The correct way of solving this would be teaching Emacs that this is an enum. But that's beyond the scope of an answer.

This is the hack:

So we'll modify the existing indentation to behave differently in this one case. (Code available for tinkering in this gist.)

(defun inside-class-enum-p (pos)
  "Checks if POS is within the braces of a C++ \"enum class\"."
  (ignore-errors
    (save-excursion
      (goto-char pos)
      (up-list -1)
      (backward-sexp 1)
      (looking-back "enum[ \t]+class[ \t]+[^}]+"))))

(defun align-enum-class (langelem)
  (if (inside-class-enum-p (c-langelem-pos langelem))
      0
    (c-lineup-topmost-intro-cont langelem)))

(defun align-enum-class-closing-brace (langelem)
  (if (inside-class-enum-p (c-langelem-pos langelem))
      '-
    '+))

(defun fix-enum-class ()
  "Setup `c++-mode' to better handle \"class enum\"."
  (add-to-list 'c-offsets-alist '(topmost-intro-cont . align-enum-class))
  (add-to-list 'c-offsets-alist
               '(statement-cont . align-enum-class-closing-brace)))

(add-hook 'c++-mode-hook 'fix-enum-class)

This is not heavily tested. ;)

How it works:

c-offsets-alist determines the indentation for different positions in the syntax tree. It can be assigned constants or functions.

These two functions find out whether the current position is inside the enum class {...}. If that's the case, they return 0 or '-, which cc-mode interprets as an indentation depth. If it isn't, they return the default value.

inside-class-enum-p simply moves up to the previous brace and checks if the text before it is "enum class".

这篇关于Emacs cc-mode缩进问题与C ++ 0x枚举类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆