Elisp: Overlay Highlighting

By Xah Lee. Date: . Last updated: .

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")
    (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))

Emacs has 2 mechanisms for coloring:

  1. text property: a region of text can have a property list, and this properly list specifies the color, font, size, etc.
  2. text overlay: create an “overlay” object, and this overlay can be placed on a region of text. You can think of it as a covering layer.

Vast majority of coloring in emacs are done with text properties. This is faster, and suitable for lots of coloring, such as syntax coloring of a programing language. 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 font lock mode to color text.

[see Elisp: Text Properties]

[see Elisp: Font Lock Mode Basics]

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

(info "(elisp) Overlays")

Writing Major Mode

  1. How to Write a Emacs Major Mode for Syntax Coloring
  2. Elisp: Font Lock Mode Basics
  3. Elisp: How to Define Face
  4. Elisp: How to Color Comment in Major Mode
  5. Elisp: How to Write Comment Command in Major Mode
  6. Elisp: How to Write Your Own Comment Command from Scratch
  7. Elisp: How to Write Keyword Completion Command
  8. Elisp: How to Create Keymap for Major Mode
  9. Elisp: Abbrev, Template, for Major Mode
  10. Elisp: Text Properties
  11. Elisp: Overlay Highlighting
  12. Emacs: Lookup Google, Dictionary, Documentation
  13. Elisp: Syntax Table Tutorial

  1. Elisp: How to Name Your Major Mode
  2. Elisp: What's “feature”?
  3. Elisp: require, load, load-file, autoload, feature

Syntax Table

  1. Elisp: Syntax Table Tutorial
  2. Elisp: How to Find Syntax of a Character?
  3. Elisp: How to Modify Syntax Table Temporarily
  4. Elisp: How to Determine If Cursor is Inside String or Comment
  5. Elisp: Regex Patterns and Syntax Table
  6. Elisp: Find Matching Bracket Character
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.