使用const
关键字编写c++时,emacs中的语法高亮显示出现问题。以下是MWE
#include <iostream>
class MyClass{
pulbic:
void helloWorld();
};
void MyClass::helloWorld(){
std::cout << "Hello, World!\n";
}
int main(){
MyClass const * myClass0; // "MyClass" is not highlighted correctly
MyClass * const myClass1; // "myclass1" is not highlighted correctly
MyClass const * const myClass2; // both not highlighted correctly
return 0;
}
参考上面的代码,问题在于突出显示了Class类型和实例化对象的名称。它不是将它们突出显示为另一种特定的关键字类型,而是使它们保持中性文本颜色。
(请注意,上面实际代码中的突出显示不是我在emacs中看到的内容...)
我试图注释掉我的.emacs文件,以查看其中是否有任何代码将其弄乱了,但这并没有改变任何东西。
我的emacs版本是23.3.1,我正在Ubuntu 12.04中运行它。我的.emacs文件是
(setq backup-by-copying t
backup-directory-alist '(("." . "~/.emacsBkups"))
delete-old-versions t
kept-new-versions 5
kept-old-versions 2
version-control t)
(auto-fill-mode 1)
(setq-default fill-column 80)
(setq LaTeX-item-indent 0)
(setq LaTeX-break-at-separators '(\\\( \\\) \\\[ \\\] \\\{ \\\} "$"))
(setq LaTeX-command-style '(("" "%(PDF)%(latex) -file-line-error %S%(PDFout)")))
(add-to-list 'auto-mode-alist '("\\.Rnw\\'" . Rnw-mode))
(add-to-list 'auto-mode-alist '("\\.Snw\\'" . Rnw-mode))
(setq reftex-file-extensions
'(("Snw" "Rnw" "nw" "tex" ".tex" ".ltx") ("bib" ".bib")))
(setq TeX-file-extensions
'("Snw" "Rnw" "nw" "tex" "sty" "cls" "ltx" "texi" "texinfo"))
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq TeX-PDF-mode t)
(require 'ess-site)
(ess-toggle-underscore nil)
(require 'whitespace)
(setq whitespace-style '(lines face))
(setq whitespace-line-column 80)
(add-hook 'c-mode-hook 'whitespace-mode)
(add-hook 'c++-mode-hook 'whitespace-mode)
(add-hook 'python-mode-hook 'whitespace-mode)
(c-set-offset (quote cpp-macro) 0 nil)
(c-set-offset 'access-label '/)
;; SyncTeX
(setq TeX-source-correlate-mode t)
(setq TeX-source-correlate-method 'synctex)
;;(setq TeX-source-correlate-start-server t)
(load "server")
(unless (server-running-p) (server-start))
'(LaTeX-command "latex -synctex=1")
;; evince viewer
;;(setq TeX-view-program-list '(("Evince" "evince --page-index=%(outpage) %o")))
;;(setq TeX-view-program-selection '((output-pdf "Evince")))
;; okular viewer
(setq TeX-view-program-list '(("Okular" "okular --unique %o#src:%n%b")))
(setq TeX-view-program-selection '((output-pdf "Okular")))
;; okular viewer
;;(setq TeX-view-program-list '(("Okular" "okular --unique %u")))
;;(add-hook 'LaTeX-mode-hook '(lambda ()
;; (add-to-list 'TeX-expand-list
;; '("%u" Okular-make-url))))
;;(defun Okular-make-url () (concat
;; "file://"
;; (expand-file-name (funcall file (TeX-output-extension) t)
;; (file-name-directory (TeX-master-file)))
;; "#src:"
;;'(100)
;; (expand-file-name (TeX-master-directory))
;; "./"
;; (TeX-current-file-name-master-relative)))
;;(setq TeX-view-program-selection '((output-pdf "Okular")))
最佳答案
我只是用自己的emacs(在Windows上为v.24.2.1)尝试了相同的操作。我看到变量标识符的颜色正确,但是第一个和第三个声明上的MyClass
没有。所以这是一个改进。
作为实验,我尝试将“const”移动到第一行和第三行的前面。它正确地着色了。这是constt的更正常的排序although I'm not a fan of it。但是,如果您以与大多数人不同的方式来做事情,则您必须期待这样的事情。 :-(
我建议将emacs升级到24或更高版本,以获得更好的C++语法着色。如果您仍然不喜欢它,则C++模式的elisp代码位于lisp / cc-mode.el中,因此可以fix it yourself。
如果您不了解emacs Lisp编码的另一种选择是,您可以尝试直接从project site下载并安装最新版本的cc-mode.el。根据那里的信息,当前模式应该在您的emacs版本上保持不变(我相信当我看到它时)。如果最新版本仍然无法正常运行,则其错误行位于bug-cc-mode@gnu.org。