emacs的c++ 11模式或设置

C++11 mode or settings for emacs?

本文关键字:设置 模式 c++ emacs      更新时间:2023-10-16

我正在运行Emacs 23.3.1 (Ubuntu, Oneiric包),Emacs似乎不理解任何新的c++ 11关键字,constexpr, thread_local等。此外,它不理解'>>'现在允许在模板参数中使用,或者新的'enum类'语法。是否有更新的或可替代的模块?或者,如果做不到这一点,在此同时,一些设置可以使emacs对c++ 11更友好?

嗯,我用的是24.1。一些c++ 98关键字丢失了,所有新的c++ 11关键字都丢失了。它甚至不会对数字常量进行排序。好像c++-mode已经有十年没有更新了。

我使用下面的代码很长一段时间了,最近添加了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)

希望这对你有帮助。

根据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的速度。请注意,用户定义字符串字面量的regexp并没有从某处"窃取";所以我希望它们能起作用。欢迎提出任何意见。

如果您想将整个字面值字符串格式化为font-lock-string-face(包括分隔符),只需将三个regexp替换为一个。像这样:

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

玩。

看一下Emacs的"Modern c++ " font-lock包。也可以在Melpa上使用。

语法高亮显示支持"现代c++"-直到c++ 17和技术规范。这个包旨在提供一个简单的c++语言的高亮显示,不依赖。

建议与c++-mode主模式一起使用用于额外的突出显示(用户定义的类型,函数等)和缩进。

我是这个次要模式的维护者。

用此替换Andreas的浮点regexp将改善浮点的高亮显示。

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

希望对大家有所帮助。

我检查了主干版本,cc-mode还没有更新,我猜没有其他选择。如果你真的想要它,但不想弄脏你的手,你应该花钱让别人为你实现它…

对于我来说,现代c++代码中字体锁定的两个最紧迫的痛点是

  1. auto被突出显示为关键字(而不是类型),因此下面的标识符通常不会作为变量声明突出显示,并且
  2. 当呈现一些代码(例如,尝试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")有帮助。