How to customize emacs in python mode to highlight operators? - emacs

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

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 to set emacs operator color?

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.

problems with emacs color themes, set-fontset and font-lock

I use the following to ensure that lambda symbols etc are displayed using a specific font
(set-face-attribute 'default nil :family "PragmataPro")
(set-face-attribute 'default nil :height 130)
;; over-ride "fontset-defult" for all unicode characters
(set-fontset-font "fontset-default" 'unicode "PragmataPro")
And use a pretty lambda font-lock when working in scheme,
(defun pretty-lambdas ()
(font-lock-add-keywords nil
'(("(\\(lambda\\)\\>" (0 (prog1 ()
(compose-region (match-beginning 1)
(match-end 1)
?λ)))))))
this works fine when using the default emacs theme, but if i change theme M-x load-theme: - the λ becomes some kind of "eastern" symbol, even though the character glyph for λ is provided by the face.
Any ideas? Is the load theme over-riding something?
-- EDIT --
I though the problem has solved itself, with the regular face it is now fine, but it still happens with italics.

Emacs AucTex Latex syntax prevents monospaced font

My emacs (Aquamacs with AucTex) changes font size (in e.g. LaTeX mode) to show the syntax - like this:
Unfortunately this ruins the point of a monospaced font - e.g. my comments do not align. How do I solve this problem?
For the specific example of sections, chapters, etc., add the following to your .emacs:
(setq font-latex-fontify-sectioning 'color)
Edit
Here is the config I usually use to customise the AUCTeX formatting:
;; Only change sectioning colour
(setq font-latex-fontify-sectioning 'color)
;; super-/sub-script on baseline
(setq font-latex-script-display (quote (nil)))
;; Do not change super-/sub-script font
(custom-set-faces
'(font-latex-subscript-face ((t nil)))
'(font-latex-superscript-face ((t nil)))
)
;; Exclude bold/italic from keywords
(setq font-latex-deactivated-keyword-classes
'("italic-command" "bold-command" "italic-declaration" "bold-declaration"))
If you find a solution to this, the beers are on me. The best I've been able to come up with so far is to put the following in my .emacs somewhere and run the function after loading a mode that does this (org-mode does it too).
(defun fix-fonts ()
(interactive)
(mapc
(lambda (face)
(set-face-attribute face nil
;; :family (if (string= system-type "darwin")
;; "Menlo"
;; "Inconsolata")
:width 'normal
:height 1.0
:weight 'normal
:underline nil
:slant 'normal))
(remove 'default (face-list))))
I don't do the family thing anymore, because I didn't have time to figure out a good way to programatically get it right and it doesn't seem to matter, but your mileage might vary. Also, I don't set anything on the "default" font because some of the other values are relative and need that fixed reference point.

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)