I need to have non-ASCII chars highlighted in my latex-mode buffers. So I added this lines in my .emacs:

(add-hook 'LaTeX-mode-hook
          '(lambda () (highlight-regexp "[^[:ascii:]]" 'custom-invalid)))
(add-hook 'latex-mode-hook
          '(lambda () (highlight-regexp "[^[:ascii:]]" 'custom-invalid)))
(add-hook 'tex-mode-hook
          '(lambda () (highlight-regexp "[^[:ascii:]]" 'custom-invalid)))

that produce something like: enter image description here

Now, some scripts of mine do reset regexp higlights (I need to do that) with the code:

(when (bound-and-true-p hi-lock-mode)
    (hi-lock-mode -1))

My question is: what's the best way to keep this regexp highlighted in my buffer? I'd prefer something that works only in latex-mode. I figured out to use something like a change hook.


You can add font-lock keywords that highlights whatever you want to be highlighted. For example:

(defun my-add-nonascii-highlighting ()
  (font-lock-add-keywords nil
                          '(("[^[:ascii:]]" (0 'custom-invalid)))))

(add-hook 'LaTeX-mode-hook #'my-add-nonascii-highlighting)
(add-hook 'latex-mode-hook #'my-add-nonascii-highlighting)
(add-hook 'tex-mode-hook   #'my-add-nonascii-highlighting)

Not too sure what you're asking.

But if you use library highlight-chars.el to highlight such chars then you can turn on/off the highlighting provided:

  • in the current buffer only (i.e., locally),
  • globally whenever font-lock-mode is turned on, or
  • automatically whenever a buffer is in a given major mode

And if you use library Highlight (highlight.el) then you have even more control over the highlighting:

  • You can use an overlay or text property
  • The highlighting can be independent of font-lock-mode or controlled by font-lock-mode
  • You can make the highlighting persist across Emacs sessions
  • I'm asking to have non-ASCII chars always highlighted (in latex-mode). I'll study your answer. – Gabriele Nicolardi Jun 20 '18 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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