2011-12-18 54 views
38

我正在运行Emacs 23.3.1(Ubuntu,Oneiric软件包),emacs似乎并不了解任何新的C++ 11关键字,constexpr,thread_local等。它也不理解''>> '现在允许使用模板参数或新的'enum class'语法。有没有更新或替代模块?或者做不到这一点,一些设置使emacs在此期间更友好?C++ 11模式或emacs的设置?

+0

请注意''thread_local'还不被G ++支持:http://gcc.gnu.org/projects/cxx0x.html gcc扩展'__thread'类似,但不调用构造函数或析构函数IIRC。 – bdonlan 2011-12-18 03:37:09

+0

我对vim有同样的问题,它有一些我调整过的语法文件。我认为必须是相同的emacs :) – Geoffroy 2011-12-20 02:13:26

回答

4

我检查了干线版本,cc-mode尚未更新,AFAIK没有其他选择。如果你真的想要它,但不想让你的手变脏,你应该付钱给你实施它...

+2

做一个kickstarter项目:p – 2013-11-15 03:30:41

27

那么,我使用24.1。一些C++ 98关键字缺失,以及所有新的C++ 11关键字。它甚至不能为数字常量赋值。看起来好像C++ - 模式还没有更新十年。

我现在正在使用以下代码很久,并且最近添加了C++ 11关键字。尝试将其放入您的.emacs;它应该填补一些漏洞。

(require 'font-lock) 

(defun --copy-face (new-face face) 
    "Define NEW-FACE from existing FACE." 
    (copy-face face new-face) 
    (eval `(defvar ,new-face nil)) 
    (set new-face new-face)) 

(--copy-face 'font-lock-label-face ; labels, case, public, private, proteced, namespace-tags 
     'font-lock-keyword-face) 
(--copy-face 'font-lock-doc-markup-face ; comment markups such as Javadoc-tags 
     'font-lock-doc-face) 
(--copy-face 'font-lock-doc-string-face ; comment markups 
     'font-lock-comment-face) 

(global-font-lock-mode t) 
(setq font-lock-maximum-decoration t) 


(add-hook 'c++-mode-hook 
     '(lambda() 
     (font-lock-add-keywords 
     nil '(;; complete some fundamental keywords 
      ("\\<\\(void\\|unsigned\\|signed\\|char\\|short\\|bool\\|int\\|long\\|float\\|double\\)\\>" . font-lock-keyword-face) 
      ;; add the new C++11 keywords 
      ("\\<\\(alignof\\|alignas\\|constexpr\\|decltype\\|noexcept\\|nullptr\\|static_assert\\|thread_local\\|override\\|final\\)\\>" . font-lock-keyword-face) 
      ("\\<\\(char[0-9]+_t\\)\\>" . font-lock-keyword-face) 
      ;; PREPROCESSOR_CONSTANT 
      ("\\<[A-Z]+[A-Z_]+\\>" . font-lock-constant-face) 
      ;; hexadecimal numbers 
      ("\\<0[xX][0-9A-Fa-f]+\\>" . font-lock-constant-face) 
      ;; integer/float/scientific numbers 
      ("\\<[\\-+]*[0-9]*\\.?[0-9]+\\([ulUL]+\\|[eE][\\-+]?[0-9]+\\)?\\>" . font-lock-constant-face) 
      ;; user-types (customize!) 
      ("\\<[A-Za-z_]+[A-Za-z_0-9]*_\\(t\\|type\\|ptr\\)\\>" . font-lock-type-face) 
      ("\\<\\(xstring\\|xchar\\)\\>" . font-lock-type-face) 
      )) 
     ) t) 

希望这会有所帮助。

+5

这太棒了!这次真是万分感谢。我将从现在开始使用它。你有没有想过为此项目回馈? – 2012-12-21 16:35:39

+0

也许...但是,谁维护cc模式?第一作者是RMS(1985),最后一个Alan Mackenzie(2003)。我会发邮件给gnu.org;让我们看看他们说什么。 – 2013-05-22 08:10:27

+1

@AndreasSpindler:最后一次编辑绝对不是在2003年 - 最新的源代码签名是在2周前由Alan Mackenzie撰写,请参阅[更新日志](http://cc-mode.hg.sourceforge.net/hgweb/ CC-模式/ cc的模/数)。 – 2013-05-22 19:41:38

12

根据Mike Weller的要求,此处为C++ 11字符串文字(包括用户定义文字)的更新版本。

(add-hook 
'c++-mode-hook 
'(lambda() 
    ;; We could place some regexes into `c-mode-common-hook', but note that their evaluation order 
    ;; matters. 
    (font-lock-add-keywords 
    nil '(;; complete some fundamental keywords 
      ("\\<\\(void\\|unsigned\\|signed\\|char\\|short\\|bool\\|int\\|long\\|float\\|double\\)\\>" . font-lock-keyword-face) 
      ;; namespace names and tags - these are rendered as constants by cc-mode 
      ("\\<\\(\\w+::\\)" . font-lock-function-name-face) 
      ;; new C++11 keywords 
      ("\\<\\(alignof\\|alignas\\|constexpr\\|decltype\\|noexcept\\|nullptr\\|static_assert\\|thread_local\\|override\\|final\\)\\>" . font-lock-keyword-face) 
      ("\\<\\(char16_t\\|char32_t\\)\\>" . font-lock-keyword-face) 
      ;; PREPROCESSOR_CONSTANT, PREPROCESSORCONSTANT 
      ("\\<[A-Z]*_[A-Z_]+\\>" . font-lock-constant-face) 
      ("\\<[A-Z]\\{3,\\}\\>" . font-lock-constant-face) 
      ;; hexadecimal numbers 
      ("\\<0[xX][0-9A-Fa-f]+\\>" . font-lock-constant-face) 
      ;; integer/float/scientific numbers 
      ("\\<[\\-+]*[0-9]*\\.?[0-9]+\\([ulUL]+\\|[eE][\\-+]?[0-9]+\\)?\\>" . font-lock-constant-face) 
      ;; c++11 string literals 
      ;;  L"wide string" 
      ;;  L"wide string with UNICODE codepoint: \u2018" 
      ;;  u8"UTF-8 string", u"UTF-16 string", U"UTF-32 string" 
      ("\\<\\([LuU8]+\\)\".*?\"" 1 font-lock-keyword-face) 
      ;;  R"(user-defined literal)" 
      ;;  R"(a "quot'd" string)" 
      ;;  R"delimiter(The String Data")delimiter" 
      ;;  R"delimiter((a-z))delimiter" is equivalent to "(a-z)" 
      ("\\(\\<[uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(\\)" 1 font-lock-keyword-face t) ; start delimiter 
      ( "\\<[uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(\\(.*?\\))[^\\s-\\\\()]\\{0,16\\}\"" 1 font-lock-string-face t) ; actual string 
      ( "\\<[uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(.*?\\()[^\\s-\\\\()]\\{0,16\\}\"\\)" 1 font-lock-keyword-face t) ; end delimiter 

      ;; user-defined types (rather project-specific) 
      ("\\<[A-Za-z_]+[A-Za-z_0-9]*_\\(type\\|ptr\\)\\>" . font-lock-type-face) 
      ("\\<\\(xstring\\|xchar\\)\\>" . font-lock-type-face) 
      )) 
    ) t) 

在上述实施用户定义的字符串文字的,定界符代码被分别标为font-lock-keyword-face;另一种选择是font-lock-constant-face。这个实现并不像它可能的那样高效;但它工作并不会减慢Emacs的速度。请注意,用户定义的字符串文字的正则表达式并未从某处“被盗”;所以我希望他们工作。欢迎任何评论。

如果您希望将整个文字字符串分成font-lock-string-face(包括分隔符),请将三个正则表达式替换为一个。像这样:

. 
    . 
("\\<\\([uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(.*?)[^\\s-\\\\()]\\{0,16\\}\"\\)\\>" 1 font-lock-string-face t) 

玩得开心。

+0

我喜欢它。但是,它不会正确突出显示多行字符串文字。 ![见此](http://i39.tinypic.com/2z6v5o6.jpg)。 – 2013-06-18 14:39:19

+0

好吧,这是因为'.'与换行符不匹配。用'[[​​:ascii:] [:nonascii:]]替换所有的'。*'*' – 2013-06-18 15:19:16

+0

*'但我不明白为什么它会用红色突出显示一些字符串引号。 [见此](http://i41.tinypic.com/16ld3iw.jpg)。 – 2013-06-18 15:35:12

5

用这个替换Andreas的浮点正则表达式可以提高浮点数的高亮度。

integer/float/scientific literals 
("\\<[-+]?[0-9]*\\.?[0-9]+\\([uUlL]+\\|[eE][-+]?[0-9]+\\)?[fFlL]?\\>" . font-lock-constant-face) 

希望能帮助别人。

7

看看包装:"Modern C++" font-lock for Emacs。它也可在Melpa上获得。

语法突出显示支持“Modern C++” - 直到C++ 17和 Technical Specification。这个包旨在提供一个简单的 突出显示的C++语言而不依赖。

除了C++模式主要模式 以外,还推荐使用它来额外突出显示(用户定义的类型,函数等)和缩进。

我是这个小模式的维护者。任何反馈意见。

0

对我来说,随着现代的C字型锁的两个最紧迫的痛点++代码已经

  1. 事实auto被突出显示作为关键字(而不是一种类型的),因此下面的标识符将通常不是最重要的变量声明,并且当呈现一些代码时(例如,尝试rtags' src/ClangIndexer.cpp),突出显示简单地变得疯狂,然后例如未能突出显示诸如函数定义的顶级构造。

经过一番实验,我得出了一个适合我的解决方案,可以解决这两个问题。

第一种是通过修改lisp/progmodes/cc-langs.el(复制到一个人的load-path然后修改也适用),以除去"auto"

(c-lang-defconst c-modifier-kwds 
    "Keywords that can prefix normal declarations of identifiers 

添加c++-font-lock-extra-types(例如,经由自定义)来实现的。

对于第二个,清空c++-font-lock-extra-types(保留"auto"除外)有帮助。