C++ 11模式或emacs的设置?

Jos*_*vin 38 c++ emacs elisp syntax-highlighting c++11

我正在运行Emacs 23.3.1(Ubuntu,Oneiric包),emacs似乎不了解任何新的C++ 11关键字,constexpr,thread_local等.它也不明白'>>'是现在允许在模板参数或新的"枚举类"语法中使用.某处有更新或替代模块吗?或者失败了,有些设置让emacs在同一时间更加友好?

And*_*ler 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)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 这是太棒了!这次真是万分感谢.从现在开始我将使用它.您是否考虑过将此贡献回项目? (5认同)
  • 好的,我在`cc-mode.el`中看到了这个错误.与此同时,Alan回复了我的邮件,称已将少量C++ 11功能添加到(尚未发布的)存储库分支"C++ 11-0-1"中.我向他提供了帮助,他同意了.然而,他还警告我CC模式并不容易理解......所以可能需要一些时间才能将相当实用的钩子代码翻译成真实的东西. (2认同)

And*_*ler 13

根据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)
Run Code Online (Sandbox Code Playgroud)

在用户定义的字符串文字的上述实现中,分隔符标记分别标记为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)
Run Code Online (Sandbox Code Playgroud)

玩得开心.


Lud*_*Lud 9

看一下包:Emacs的"Modern C++"字体锁.它也可以在Melpa找到.

语法突出显示对"Modern C++"的支持 - 直到C++ 17和技术规范.该软件包旨在提供C++语言的简单亮点而不依赖.

建议在c ++模式主模式下使用它以进一步突出显示(用户定义的类型,函数等)和缩进.

我是这个小模式的维护者.任何反馈都表示赞赏.


小智 5

用这个取代Andreas的浮点正则表达式将改善浮点数的升华.

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

希望能帮助别人.


Ism*_*eno 4

我检查了主干版本,cc-mode尚未更新,据我所知没有其他选择。如果你真的想要它,但又不想弄脏你的手,你应该花钱请人为你实现它......

  • 做一个 kickstarter 项目:p (2认同)