Emacs cc模式缩进问题与C ++ 0x枚举类 [英] Emacs cc-mode indentation problem with C++0x enum class
问题描述
Emacs cc模式似乎尚未识别C ++ 0x中引入的类型安全枚举类
。我得到的结果是一个双缩进第二,第三,等枚举:
枚举类Color {
蓝色,
Red,
Orange,
Green
};
我想要的是:
枚举类Color {
Blue,
Red,
Orange,
Green
};
您可以推荐一个好的命令添加到 .emacs
这将使cc模式处理 enum
?同样的方式处理 enum
p>
这是问题:
<假设关键词是单个词。添加对
enum_class
而不是枚举类
的支持只是更改几个正则表达式的问题。 而Emacs把它当作类。解决这个问题的正确方法是教授Emacs这是一个枚举。但这超出了答案的范围。
这是黑客:
现有缩进在这种情况下表现不同。 (可用于修改此 gist 中的代码。)
(defun inside-class-enum-p(pos)
检查POS是否位于C ++ \enum class\的括号内。
(ignore-errors
(save-excursion
(goto-char pos)
(up-list -1)
(backward-sexp 1)
\\ t] + class [\ t] + [^}] +))))
(defun align-enum-class(langelem)
(if(inside- enum-p(c-langelem-pos langelem))
0
(c-lineup-topmost-intro-cont langelem)))
-closing-brace(langelem)
(if(inside-class-enum-p(c-langelem-pos langelem))
' -
'+))
(defun fix-enum-class()
设置`c ++ - 模式'来更好地处理\class enum\。
(add-to-list'c-offsets- alist'(top-int-cont。align-enum-class))
(add-to-list'c-offsets-alist
'括号)))
(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模式缩进问题与C ++ 0x枚举类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!