emacs的c++ 11模式或设置
C++11 mode or settings for emacs?
我正在运行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++代码中字体锁定的两个最紧迫的痛点是
-
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"
)有帮助。
- 是否允许使用带有"w+"模式的 freopen 进行标准设置?
- 如何在使用 MPFR 时在提升多精度中设置舍入模式
- 直接显示 - 无法使用设置模式设置静止引脚
- 基于 std::vector <string>的内容动态设置顺序的模式
- 将 GtkTreeView 中的单元格设置为以编程方式编辑模式
- 将终端设置为原始模式:仅在按下下一个键后显示字符
- 如何自动将流模式设置回默认值
- 如何使用适用于 Mac 和 Windows 10 的 vscode 设置同步插件配置C++智能感知模式
- 将焦点设置在MFC视图中托管的无模式对话框上
- 哪些VS2010 C项目设置导致执行器需要兼容模式
- OpenCV安卓人像模式全屏设置
- 在Android上为OpenCV设置相机场景模式
- 如何设置从非阻塞模式的服务器客户端接收消息的超时
- 在全屏模式下设置控制台字体大小
- 如何在工厂设计模式中设置派生产品类的数据成员
- Qt:无法设置拖动模式
- 如果从文件流中读取数据,gSOAP为什么将stdin模式设置为二进制
- 将1d矩阵模式设置为等于2d矩阵
- emacs的c++ 11模式或设置
- 为什么在设置模式时,使用 getline 会导致"Debug assertion failed"消息?