0

I would like to be able to highlight a particular phrase (in my case: @dakrone) in every emacs buffer, regardless of the mode (it's fine if it doesn't work in fundamental-mode). I know of something like:

(defun eos/add-watchwords ()
  "Highlight FIXME and TODO in code"
  (font-lock-add-keywords
   nil '(("\\<\\(TODO\\(?:(.*)\\)?:?\\)\\>"  1 'warning prepend)
         ("\\<\\(FIXME\\(?:(.*)\\)?:?\\)\\>" 1 'error prepend))))

(add-hook 'prog-mode-hook #'eos/add-watchwords)

But I haven't gotten something like that to be applied globally instead of having to manually add every mode I want to highlight in. Is there a way do do this?

1
  • Not all modes use font-lock; e.g., org-agenda-mode uses text-properties. Font-lock and text-properties do not play well together. A third option is to use overlays.
    – lawlist
    Commented Jan 8, 2018 at 17:51

2 Answers 2

1

You can use library highlight.el to highlight any text in any buffer, using either text properties or overlays. The highlighting can be independent of font-locking or be controlled by font-lock-mode.

There are many ways to highlight, and your question is not very specific. See Highlight Library for more information.

0

You can use after-change-major-mode-hook to generalize modes, eg:

(defun eos/add-watchwords ()
  "Highlight FIXME and TODO in code"
  (font-lock-add-keywords
   nil '(("\\<\\(TODO\\(?:(.*)\\)?:?\\)\\>"  1 'warning prepend)
         ("\\<\\(FIXME\\(?:(.*)\\)?:?\\)\\>" 1 'error prepend))))

(add-hook 'after-change-major-mode-hook
  (lambda ()
    (when (derived-mode-p 'prog-mode)
      (eos/add-watchwords))))

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.