3
votes

I would like for certain non-alphanumeric characters (= + / < > etc.) to be highlighted in emacs while editing any language.

Using the answer to a different question on this site, I found this solution for highlighting '%' while editing Fortran:

(font-lock-add-keywords 'f90-mode
        '(("%" . font-lock-keyword-face)))

but this is only one symbol in one language. I can achieve what I want by polluting my .emacs file:

(font-lock-add-keywords 'c++-mode
        '(("=" . font-lock-keyword-face)))

(font-lock-add-keywords 'java-mode
        '(("=" . font-lock-keyword-face)))

(font-lock-add-keywords 'python-mode
        '(("=" . font-lock-keyword-face)))

and so on, but this introduces a bug in which the last few lines of any file do not get any syntax highlighting at all.

How can I, across all language modes, enable syntax highlighting for a certain set of non-alphanumeric characters?

1

1 Answers

3
votes

Use library highlight-chars.el (the code is here).

You can highlight any set of characters, using any faces. You can define the set using any number of entries of any of these kinds:

  • string of chars
  • range of successive chars (Emacs characters are integers)
  • character class (e.g. [:nonascii:], [:space:])
  • character set (e.g. iso-8859-1 or lao)

You can also exclude any set of characters from such highlighting.

You can thus, for example, highlight all characters in character set greek-iso8859-7 except the character GREEK SMALL LETTER LAMBDA. Or highlight all characters in class [:space:] (whitespace) except tab. Or all Unicode characters in the range ?\u2190 through ?\u21ff (mathematical arrows) except ?\u21b6, ?\u21b7, ?\u21ba, and ?\u21bb (curved arrows)...