emacsemacs24

Persistent colors in an emacs text buffer


After highlighting text in an emacs buffer using a regexp (1), it's easy enough to write the setting in the file (2), but I am missing a third step for persistence.

(1) Set

Doing M-s h r (highlight-regexp) and, say, \{.*\} followed by italic will highlight everything between curly braces in that style.

(2) Write

Subsequently calling C-x w b (hi-lock-write-interactive-patterns) will write the string

# Hi-lock: (("\\{.*\\}" (0 (quote italic) t)))

in the buffer, after asking for the comment string (I used #).

(3) Re-use

What is the third step needed to make this highlighting persistent, i.e., to make it survive saving/loading the file from disk?


Solution

  • If you C-h f hi-lock-write-interactive-pattern, you'll see in the help buffer a link to hi-lock.el. Often Lisp libraries have some usage information at the beginning of the file and it's handy to check.

    In this case, it tells how to make it persistent:

    ;;    To enable the use of patterns found in files (presumably placed
    ;;    there by hi-lock) include the following in your init file:
    ;;
    ;;    (setq hi-lock-file-patterns-policy 'ask)
    ;;
    ;;    If you get tired of being asked each time a file is loaded replace
    ;;    `ask' with a function that returns t if patterns should be read.