简体   繁体   English

使用“ const”关键字为c ++突出显示emacs语法

[英]emacs syntax highlighting for c++ using the “const” keyword

I am having an issue with syntax highlighting in emacs when writing c++ using the const keyword. 使用const关键字编写c ++时,emacs中的语法高亮显示出现问题。 A MWE is the following 以下是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;
}

Referencing the above code, the issue is the highlighting for the Class type and for the name of the object instantiated. 参考上面的代码,问题在于突出显示了Class类型和实例化对象的名称。 It is not highlighting them as another specific keyword type, it is just leaving them the neutral text color. 它不是将它们突出显示为另一种特定的关键字类型,而只是使它们保持中性文本颜色。 (note that the highlighting in the actual code above is NOT what I see in emacs...) (请注意,上面实际代码中的突出显示不是我在emacs中看到的内容...)

I tried commenting out my `.emacs' file to see if any code in there was messing it up, but that didn't change anything. 我尝试注释掉我的.emacs文件,以查看其中是否有任何代码将其弄乱了,但这并没有改变任何东西。

My version of emacs is 23.3.1 and I'm running it in Ubuntu 12.04. 我的emacs版本是23.3.1,并且正在Ubuntu 12.04中运行它。 My `.emacs' file is 我的.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")))

I just tried the same thing with my own emacs (v 24.2.1 on Windows). 我只是用自己的emacs(在Windows上为v.24.2.1)尝试了相同的操作。 I'm seeing the variable identifiers colored correctly, but not the MyClass on the first and third declaration. 我看到变量标识符的颜色正确,但是在第一个和第三个声明上没有MyClass So that's an improvement. 所以这是一个改进。

As an experiment, I tried moving the "const" to the front of the line in lines one and three. 作为实验,我尝试将“ const”移动到第一和第三行的开头。 It colorized that correctly. 它正确地着色了。 This is the more normal ordering for "const", although I'm not a fan of it . 这是“ const”的更正常的排序方式, 尽管我不喜欢它 Still, if you do things a different way than most folks do, you have to expect things like this. 但是,如果您以与大多数人不同的方式来做事情,则您必须期望这样的事情。 :-( :-(

I'd suggest upgrading your emacs to 24 or later for the better C++ syntax coloring. 我建议将emacs升级到24或更高版本,以获得更好的C ++语法着色。 If you still don't like it, the elisp code for C++ mode is in lisp/cc-mode.el, so you can fix it yourself . 如果您仍然不喜欢它,那么C ++模式的elisp代码位于lisp / cc-mode.el中,因此您可以自己修复它

Another alternative if you aren't up to emacs Lisp coding is that you can try to download and install the latest version of cc-mode.el directly from the project site . 如果您不了解emacs Lisp编码的另一种选择是,您可以尝试直接从项目站点下载并安装最新版本的cc-mode.el。 According to the info there, the current mode should work unchanged on your version of emacs (I'd believe that when I see it). 根据那里的信息,当前模式应该在您的emacs版本上保持不变(我相信当我看到它时)。 If the latest version still doesn't work right there either, their bug line is at bug-cc-mode@gnu.org . 如果最新版本仍然无法正常运行,则其错误行位于bug-cc-mode@gnu.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM