Elisp: Overlay Highlighting
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.
Text Property vs Text Overlay
Emacs has 2 mechanisms for coloring, Elisp: Text Properties and text overlay.
- Vast majority of coloring in emacs are done with text properties.
- Text properties is usually used for static coloring.
- Text properties is usually used when there a lot coloring, such as programing language syntax.
- When text are copied by user or in elisp, the text properties come with it by default.
- Font Lock mode stores coloring info in text properties, and vast majority of programing language modes use Elisp: Font Lock Mode to color text.
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
'face 'highlight
'face 'font-lock-warning-face
- or any of the builtin face. See Elisp: Font Face