How to configure emacs in python mode to highlight statements?

I create emacs to be my python IDE, and I found a lot of materials on the web that explain automatic completion, among many other features. However, I cannot figure out how to make syntax highlighting do my highlighting for statements.

How can I configure emacs in python mode to make + - different colors? I would also like to make integers, floats and parentheses in different colors.

+5
source share
3 answers

Put the following in the ~ / .emacs file:

;
; Python operator and integer highlighting
; Integers are given font lock "constant" face since that 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)))
+3
source

- . , " " ( python, ). "\\([][|!.+=&/%*,<>(){}:^~-]+\\)", , .

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

, ( , "python-mode" ):

(add-hook 'python-mode-hook
                  '(lambda ()
                     (font-lock-add-keywords nil font-lock-operator-keywords t))
                  t t)
+5

Adding my own answer, as I could not get the rest to work (starting with emacs 25.3.1, 2017).

The following elisp can be used to highlight statements:

;; 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))))
0
source

All Articles