1
votes

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")))
1
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): stackoverflow.com/questions/17981738/… - lawlist

1 Answers

1
votes

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 [email protected].