I wrote the following code to highlight dollar signs in AUCTeX buffers in different colors, but then I found that it's even highlighting dollar signs in comments, which was unintended, but I am starting to like it. But now just for curiosity, I wonder if that can be avoided.
(defun my-LaTeX-mode-dollars ()
(font-lock-add-keywords
nil
`((,(rx "$") (0 'success t)))
t))
(add-hook 'LaTeX-mode-hook 'my-LaTeX-mode-dollars)
From the documentation of
font-lock-keywords
:In other words, if you drop the
t
after 'success, it will no longer fontify dollar signs in comments and strings.EDIT:
Apparently, the above solution is not sufficient in this situation, probably because dollar signs have been colored using another face earlier.
One way that might work is to not pass the HOW parameter (currently
t
) tofont-lock-add-keywords
. This means that they should be added to the end of the list. However, this might cause other things to stop working.If we need a bigger hammer, you can write a bit more advanced rule that inspects the current fontification, and decides what to do upon this. For example, the following is used by Emacs to add a warning face to parentheses placed at column 0 in strings:
A third way to do this is to replace the regexp
(rx "$")
with the name of function that could search for$
and check that it appears in the correct context. One example of such font-lock rules can be found in the standard Emacs packagecwarn
.