Elisp: Overlay Highlighting

By Xah Lee. Date: . Last updated: .

What is Text Overlay

Text Overlay is a “overlay” object on a region of text, usually used to color text. You can think of it as a covering layer. This overlay can be freely moved around, not fixed to the text.

Overlays (ELISP Manual)

Text Property vs Text Overlay

Emacs has 2 mechanisms for coloring, Elisp: Text Properties and text overlay.

Overlay is useful for smaller number of coloring, or on and off coloring, such as temporary highlight of words, or current line.

Example, Text Overlay

Here's example of using overlay mechanism to highlight text.

(defun xah-make-overlay-bold-region (Begin End)
  "make the region bold, using overlay.
Version 2016-11-01"
  (interactive "r")
  (progn
    (overlay-put (make-overlay Begin End) 'face 'bold)
    (setq mark-active nil)))

(defun xah-remove-overlays-region (Begin End)
  "Remove overlays.
Version 2016-11-01"
  (interactive "r")
  (remove-overlays Begin End))

instead of 'face 'bold, you can use

Elisp, font lock, syntax coloring