How to set emacs operator color? - emacs

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?

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

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.

Related

Highlight function calls but not first in quoted list in emacs

In emacs lisp mode, I want to highlight all function calls one color, and all quoted symbols another color. For example, in (foo 1 2 'bar), foo should be highlighted color 1 and 'bar should be highlighted color 2.
I was able to do this with the code below, however, it is also highlighting the first symbol in a list when the list is quoted. For example, in '(nil a b c), nil should not be highlighted as a function call, and all items in that list should be highlighted as quoted symbols (color 2).
(defface font-lock-func-face
'((nil (:foreground "#6fc2ef"))
(t (:bold t :italic t)))
"Font Lock mode face used for function calls."
:group 'font-lock-highlighting-faces)
(defface font-lock-quoted-face
'((nil (:foreground "#e1a3ee"))
(t (:bold t :italic t)))
"Font Lock mode face used for function calls."
:group 'font-lock-highlighting-faces)
(font-lock-add-keywords
'emacs-lisp-mode
'(("(\\s-*\\(\\_<\\(?:\\sw\\|\\s_\\)+\\)\\_>"
1 'font-lock-func-face)))
(font-lock-add-keywords
'emacs-lisp-mode
'(("'[-a-zA-Z_][-a-zA-Z0-9_]*\\>" 0 'font-lock-quoted-face)))
There's also an image of what I'm looking at here: http://imgur.com/Iluku05
First part of the answer: The package lisp-extra-font-lock highlights, among else, quoted expressions.
It also highlights:
variables in the parameter lists of functions and variables bound by let, pcase etc. It distinguish between normal local variables and global variables defined by a defvar by using different colors.
Back-quoted expressions (and un-highlights all ,- and ,#-escaped subexpressions)
For example:
Second part of the answer:
If you use the lisp-extra-font-lock package, your rule for highlighting functions work as you posted it, if you add it as the last rule, after you have enabled the package. That way it will not overwrite any other highlight:
(font-lock-add-keywords
'emacs-lisp-mode
'(("(\\s-*\\(\\_<\\(?:\\sw\\|\\s_\\)+\\)\\_>"
1 'font-lock-func-face))
'append) ;; <-- Add after all other rules

How do you color specific letters in emacs?

In a new and hopefully very simple emacs mode I would like every instance of 'a' to be #0000FF and 'b' to be #DF0101. Thus far I haven't found a way to do this despite lots of googling. Ideally it would happen in realtime, as you typed.
http://www.gnu.org/savannah-checkouts/gnu/emacs/manual/html_node/emacs/Highlight-Interactively.html
http://www.gnu.org/savannah-checkouts/gnu/emacs/manual/html_node/emacs/Font-Lock.html#Font-Lock
I use this for my Navi minor mode:
;; Adding Navi to the syntax highlighting of emacs mode.
First to make new faces, with their colors, in my case green for the success font for letter "t", and cyan for "Navi" and "navi".
If needed, read about font-lock.
(make-face 'font-lock-Navi-face)
(set-face-foreground 'font-lock-Navi-face "cyan")
(make-face 'font-lock-success-face)
(set-face-attribute 'font-lock-success-face nil :foreground "green")
Now to add the 'keywords' (regexp) to which to attach:
(defun add-custom-keywords()
"adds a few keywords for emacs mode"
;
(font-lock-add-keywords nil
'(
("Navi\\|navi" . 'font-lock-Navi-face)
;; here you can see that I highlight the letter "t" in " t " when spaced,
;; or with a parenthesis\newline around it
("\\s-t\\s-\\|\\s-t)\\|\\s-t\n" . 'font-lock-success-face)
)
)
)
You can replace Navi ("or" is here "\\|") or navi with simply your letter, "a" or "b", so "a\\|b" and then give the face to it.
; This is the hook to activate when the mode is triggered
(add-hook 'emacs-lisp-mode-hook 'add-custom-keywords)
The last part ensures that this font will be "real-time", and every time you open the file.
add-font-lock-keywords is for user customizations, mostly. If you're writing the mode yourself, it's much better to just set font-lock-defaults with an appropriate value.
Check sample-mode on the emacswiki for some example.

Org-mode strike-through color

Strike-through texts (like this: +text+) in Org-mode are black by default. I want to make them gray. The problem is, I can't find the place to customize it. I've tried M=x describe-face, and the result is "default face", which is puzzling. Doesn't Org-mode have a place to configure the strike-through color?
Customize the org-emphasis-alist variable with M-x customize-variable. Find the list entry where the "marker character" is + and choose the "Font-lock-face" option in the "Value menu" popup. Input the value of a face of your choosing, whose exact look you can customize the usual way, for example with M-x customize-face.
Or, more succinctly:
(require 'cl) ; for delete*
(setq org-emphasis-alist
(cons '("+" '(:strike-through t :foreground "gray"))
(delete* "+" org-emphasis-alist :key 'car :test 'equal)))

How to customize emacs in python mode to highlight operators?

I'm setting up emacs to be my python IDE, and I've found plenty of material online that explain auto-completion, among a variety of other features. What I can't figure out, though, is how to get the syntax highlighter to do its highlighting magic on operators.
How can I customize my emacs in python mode to make + - different colors? I'd also like it to make integers, floats, and parentheses different colors as well.
i actually have something like this setup for my programming modes. this defines 2 separate faces, one for operators and one for the "end statement" symbol (not so useful in python, obviously). you can modify the "\\([][|!.+=&/%*,<>(){}:^~-]+\\)" regex to match the operators you are interested in and customize the faces to be the color you want.
(defvar font-lock-operator-face 'font-lock-operator-face)
(defface font-lock-operator-face
'((((type tty) (class color)) nil)
(((class color) (background light))
(:foreground "dark red"))
(t nil))
"Used for operators."
:group 'font-lock-faces)
(defvar font-lock-end-statement-face 'font-lock-end-statement-face)
(defface font-lock-end-statement-face
'((((type tty) (class color)) nil)
(((class color) (background light))
(:foreground "DarkSlateBlue"))
(t nil))
"Used for end statement symbols."
:group 'font-lock-faces)
(defvar font-lock-operator-keywords
'(("\\([][|!.+=&/%*,<>(){}:^~-]+\\)" 1 font-lock-operator-face)
(";" 0 font-lock-end-statement-face)))
then, you just enable this by adding a hook to the relevant modes (this example assumes you are using "python-mode"):
(add-hook 'python-mode-hook
'(lambda ()
(font-lock-add-keywords nil font-lock-operator-keywords t))
t t)
Put the following in your ~/.emacs file:
;
; Python operator and integer highlighting
; Integers are given font lock's "constant" face since that's unused in python
; Operators, brackets are given "widget-inactive-face" for convenience to end-user
;
(font-lock-add-keywords 'python-mode
'(("\\<\\(object\\|str\\|else\\|except\\|finally\\|try\\|\\)\\>" 0 py-builtins-face) ; adds object and str and fixes it so that keywords that often appear with : are assigned as builtin-face
("\\<[\\+-]?[0-9]+\\(.[0-9]+\\)?\\>" 0 'font-lock-constant-face) ; FIXME: negative or positive prefixes do not highlight to this regexp but does to one below
("\\([][{}()~^<>:=,.\\+*/%-]\\)" 0 'widget-inactive-face)))
Adding my own answer since I couldn't get the others working (as of emacs 25.3.1, 2017).
The following elisp can be used to highlight operators:
;; Operator Fonts
(defface font-lock-operator-face
'((t (:foreground "#8b8bcd"))) "Basic face for operator." :group 'basic-faces)
;; C-Like
(dolist (mode-iter '(c-mode c++-mode glsl-mode java-mode javascript-mode rust-mode))
(font-lock-add-keywords mode-iter
'(("\\([~^&\|!<>=,.\\+*/%-]\\)" 0 'font-lock-operator-face keep))))
;; Scripting
(dolist (mode-iter '(python-mode lua-mode))
(font-lock-add-keywords mode-iter
'(("\\([#~^&\|!<>:=,.\\+*/%-]\\)" 0 'font-lock-operator-face keep))))

Set color for floating point values in c-mode under Emacs using Color-Theme?

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