0

My question is similar to this one and that one: I would like to add highlighting of some characters that are not highlighted in some programming modes, like python-mode and fortran-mode.

So, following Lindydancer's answer to the first question cited above, I try the following in my init file:

(defun python-fortran-common-hook ()
    (font-lock-add-keywords nil
                '(;
                  ("+" (0 font-lock-negation-char-face))
                  ("-" (0 font-lock-negation-char-face))
                  ("*" (0 font-lock-negation-char-face))
                  ("/" (0 font-lock-negation-char-face))
                  ("=" (0 font-lock-negation-char-face))
                  ("<" (0 font-lock-negation-char-face))
                  (">" (0 font-lock-negation-char-face))
                  ("," (0 font-lock-negation-char-face))
                  (";" (0 font-lock-negation-char-face))
                  (":" (0 font-lock-negation-char-face))
                  )))
(add-hook 'python-mode-hook #'python-fortran-common-hook)
(add-hook 'fortran-mode-hook #'python-fortran-common-hook)

The problem is that for some reason this does not work (the characters I want to highlight still have the default highlighting), and my question is why.

Note 1: I tried the same with an add-hook form, and it works. But I would greatly appreciate to have the above form, not to have to write and add-hook for each mode that I want to have this highlighting. I am aware of prog-mode, but there are modes in it for which I don't want this highlighting.

Note 2: Of course, if there is a better way to achieve what I want, I would be glad to hear about it.

1 Answer 1

1

I just tried this, for me it works straight out of the box. Try to restart your Emacs, as it's easy to end up with intermediate versions of font-lock rules in hooks and other places when trying out new rules.

(Just one minor note. * has a special meaning in regexp:s so you should write "\\*" instead of "*".)

If you don't want to write this for each mode, you can use something like (even though I don't think you gain much from it):

(dolist (hook '(python-mode-hook fortran-mode-hook))
  (add-hook hook #'python-fortran-common-hook))

When it comes to if you can do this better, you could join all rules into one using the regexp [...] construct:

("[-+*/=<>,;:]" (0 font-lock-negation-char-face))

Note: If you want - to be interpreted literally when put inside square brackets [ and ], you have to put it just after the openning bracket [ or just before the closing one ], otherwise it is interpreted specially. Have a look also here if you need ], \ or ^ literally inside square brackets.

3
  • Thanks a lot, also for the improvements you provide. I will just edit your answer about the - inside brackets (small detail). Concerning my code, I have just realized that it indeed works for python-mode and fortran-mode, but not for lisp-mode and shell-script-mode... Any idea why?
    – Giuseppe
    Commented Aug 24, 2017 at 12:27
  • I found why it did not work for shell-script-mode: I had placed "shell-script-mode-hook'", which does not exists. The correct hook is sh-mode-hook` (shell-script-mode is an alias for sh-mode). It also worked for lisp-mode, I just did not notice at first that the lisp file I was testing it on was in emacs-lisp-mode and not in lisp-mode.
    – Giuseppe
    Commented Aug 24, 2017 at 13:09
  • Glad I could help. Thanks for correcting the code I posted with regards to the minus sign, it's things like that that happens when posting untested code... Commented Aug 24, 2017 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.