Calaf
Calaf

Reputation: 10837

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?

Upvotes: 1

Views: 632

Answers (4)

Arnab Chakraborty
Arnab Chakraborty

Reputation: 13

A simpler method could be to use something like this in the first line of the file:

-*- eval: (progn (highlight-regexp "^!.+$" 'hi-blue) (highlight-regexp "---" 'hi-yellow)) -*-

Upvotes: 0

Eugen Dück
Eugen Dück

Reputation: 302

https://www.gnu.org/software/emacs/manual/html_node/emacs/Highlight-Interactively.html

C-x w i

Extract regexp/face pairs from comments in the current buffer (hi-lock-find-patterns).

Thus, you can enter patterns interactively with highlight-regexp, store them into the file with hi-lock-write-interactive-patterns, edit them (perhaps including different faces for different parenthesized parts of the match), and finally use this command (hi-lock-find-patterns) to have Hi Lock highlight the edited patterns.

Upvotes: 0

Alan Shutko
Alan Shutko

Reputation: 380

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.

Upvotes: 1

lawlist
lawlist

Reputation: 13457

How about the possibility of creating a function that has a hook relating to the file you want to load -- e.g., a text-mode-hook or perhaps a specific file hook (if something like that exists)?

;; M-x ae-hi-lock-features

(global-hi-lock-mode 1)

(defface af-bold-yellow-box '((t  (:background  "black" 
                                   :foreground  "yellow"
                                   :underline "red"
                                   :height 200
                                   :bold t
                                  )))  "yellow-box-face")

(defun z-hi-lock-quizzes ()
  ;; this next line is necessary to correct sloppy hi-locking
  (if (not hi-lock-mode) 
      (progn (hi-lock-mode -1) 
             (hi-lock-mode  1)) 
    (hi-lock-mode) 
    (hi-lock-mode))
  (highlight-regexp "^%-\\*-mode:LaTeX.*$" (quote hi-conceal-content));
  (highlight-regexp "^%-@-(.+$"            (quote hi-lock-page-break));
  (highlight-regexp "food"            (quote af-bold-yellow-box));
)

(defun ae-hi-lock-features ()
   (interactive)
   (z-hi-lock-quizzes)
;;   ... call other functions ...
)

(add-hook 'text-mode-hook 'ae-hi-lock-features)

Upvotes: 0

Related Questions