Search code examples
emacselispmode

Emacs mode: how to specify that thing in square brackets should be colored


I write a simple emacs mode. How do I explicitly specify that all things in e.g. square brackets should be colored. Must be smth like that:

( (if thing is in square brackets) . font-lock-string-face)

Solution

  • I assume you're writing a major mode, but font-lock-add-keywords works also in minor modes. Check out its documentation with C-h f RET font-lock-add-keywords.

    (define-derived-mode my-mode text-mode "mymode"
      ;; some init code
      (font-lock-add-keywords nil '(("\\[\\(.*\\)\\]"
                                     1 font-lock-warning-face prepend)))
      ;; some more init code
    )