1
votes

I'm using EmacsForMacOsX, v23.3.1, and I wonder how I can change the color for floating point values celsiusFloat = (5.0/9.0); to be a different color than those I get from my current
color-theme-billw theme for integers age = 23;.

I doubt that StackOverflow colours them differently.


EDIT: My initial approach to add a regex for the floating point d*\.d* in cc-mode.el was apparently not the way Emacs work with syntax highlighting (also known as font locking) - further research has led me to the following website: http://www.gnu.org/software/emacs/elisp/html_node/Customizing-Keywords.html

Edit 2: I seem to have found my answer at http://www.emacswiki.org/emacs/AddKeywords and
http://www.gnu.org/software/emacs/manual/html_node/emacs/Font-Lock.html#Font-Lock

(add-hook 'c-mode-hook (lambda () (font-lock-add-keywords nil '(("[0-9]+\\.[0-9]+" 1 font-lock-warning-face t)))))

1
Oh man, a way to color ints vs floats would be SO AWESOME for stupid languages that silently understand numbers based on context. - drysdam
It would, wouldn't it? :) Now if I could just figure out how to do it.. - Zolomon
So far I've managed to find the cc-mode.el - my first attempt will be to add a face for the regex "\d*\.\d*". - Zolomon

1 Answers

0
votes

I found a solution at: http://hbfs.wordpress.com/2010/03/02/adding-keywords-in-emacs/

First:

(make-face 'font-lock-special-macro-face) ;; Create a new face
(set-face-foreground 'font-lock-special-macro-face "pink") ;; Set the colour

Then we proceed to add regular expressions to the list of keywords and associate each regexp with a face:

(defun add-custom-keyw()
  "adds a few special keywords for c and c++ modes"
  ;
  (font-lock-add-keywords nil
   '(
     ("[0-9]+\\.[0-9]+" . 'font-lock-special-macro-face )

     ; more of those would go here
     )
   )
 )

Last we hook it to our mode:

(add-hook 'c-mode-hook 'add-custom-keyw)