1

I am having an issue with syntax highlighting in emacs when writing c++ using the const keyword. A MWE is the following

#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. 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...)

I tried commenting out my `.emacs' file to see if any code in there was messing it up, but that didn't change anything.

My version of emacs is 23.3.1 and I'm running it in Ubuntu 12.04. My `.emacs' file is

(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")))
nick
  • 319
  • 1
  • 4
  • 18
  • Perhaps consider defining your keywords separately so that whenever they appear when using this particular mode, they will be highlighted in the manner you desire. However, that would not be as fancy as using a special circumstance regexp function that only highlights keywords when certain complex patterns exist. Here is an example of defining your own keywords (see the answer): http://stackoverflow.com/questions/17981738/face-font-in-c-mode-when-adding-new-keyword – lawlist Nov 27 '13 at 16:40

1 Answers1

1

I just tried the same thing with my own emacs (v 24.2.1 on Windows). I'm seeing the variable identifiers colored correctly, but not the MyClass on the first and third declaration. So that's an improvement.

As an experiment, I tried moving the "const" to the front of the line in lines one and three. It colorized that correctly. This is the more normal ordering for "const", although I'm not a fan of it. 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. 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.

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. According to the info there, the current mode should work unchanged on your version of emacs (I'd believe that when I see it). If the latest version still doesn't work right there either, their bug line is at bug-cc-mode@gnu.org.

Community
  • 1
  • 1
T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • I tried upgrading to emacs 24.2.1 and I had an issue using `\' to escape the newline character when writing directives for openMP. – nick Nov 26 '13 at 20:48
  • @nick - Well, another alternative would be to try to just upgrade your cc-mode.el to the one from 24. There could well be version dependencies in there though, or dependencies on other lisp file updates, so that might not work. – T.E.D. Nov 26 '13 at 21:05
  • you mentioned I can edit the cc-mode.el file? Is that a straightforward task? if so, how would I go about it? If not, can you point me to some resources that might explain more about it? – nick Nov 27 '13 at 01:49
  • @nick - I've done it in the past myself (for ada-mode.el, but the principle should be the same). Editing and trying out changes is easy as pie. However, you may or may not find emacs-lisp and the logic used inside the lexing code of a mode file a bit of a challenge. I happened to already know Lisp and lexers fairly well before I ever set foot in that code. Learning new languages with a really challenging task can be quite a chore. You can at least look around inside the file and see if you can make heads or tails of it first though. right? – T.E.D. Nov 27 '13 at 14:17
  • Added more info to the answer, including a link to the Gnu Emacs lisp manual, and some info I discovered about getting the mode directly from the maintainers. – T.E.D. Nov 27 '13 at 14:26
  • this is great! Thanks for the info. Given that they claim the .el file will work on previous editions, I will give that a shot first and see. I will post the result one way or another. The resources you added are much appreciated. – nick Nov 27 '13 at 21:07