2

How can I set font color for operators? I'm programing in C++, and I would like operators such as '+', '=', '!=', '<<' and such be colored as I wish.

I tried move the cursor onto an operator and 'M-x customize-face' but it takes me to 'all faces' by default. Which is the one I should edit?

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
Diaz
  • 957
  • 2
  • 13
  • 22

2 Answers2

3

I believe this is what you're looking for.

;;       * the name of our face *
(defface font-lock-operator-face
  '((((class color)
       :background "darkseagreen2")))
  "Basic face for highlighting."
  :group 'basic-faces)

;; You'll have a hard time missing these colors
(set-face-foreground 'font-lock-operator-face "red")
(set-face-background 'font-lock-operator-face "blue")

(font-lock-add-keywords 'c++-mode
  '(("\\(~^&\|!<>:=,.\\+*/%-]\\)" 0 'font-lock-operator-face)))
PythonNut
  • 6,182
  • 1
  • 25
  • 41
0

By default, operators are not font-locked in my version of c++-mode (Emacs 24.3 default). You can get the face under the cursor with C-u C-x =. To add font locking to operators you can add then this way:

(font-lock-add-keywords 'c++-mode
  '(("\\(~^<>:=,.\\+*/%-]\\)" 0 'highlight)))

The regex, and face may be customized. I am not a regex ninja, so the operators highlighted are very simplistic.

PythonNut
  • 6,182
  • 1
  • 25
  • 41
  • That didn't work for me. Emacs was initialized normally but nothing appeared different. I would guess that 'highlight just happened to make it white, the same as normal characters? – Diaz Feb 21 '14 at 01:37
  • Anyway I could specify the color I like? – Diaz Feb 21 '14 at 01:37
  • Sure, define a face and set it to the color you want with (set-face-foreground). – PythonNut Feb 21 '14 at 01:52
  • I'm totally new to emacs, so could you please explain it a little? Since I have no idea what commands like 'fond-lock-add-keywords' implies, and if it won't work I can't find a way to approach it. Thanks! – Diaz Feb 21 '14 at 02:19