Emacs Lisp: Overlay Highlighting

By Xah Lee. Date: . Last updated: .

emacs: introduction to overlays for highlighting

a stackoverflow question.

in clojure mode, want to highlight lines starting with “;-;”.


here's my answer.

You can use overlay for this. You'll need to search for the `;-;` to end of line, then put overlay to it.

Here's full code.

(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))
(defun xah-color-special-comment ()
  "bold text beging with ;-; to end of line."
  (let (p1 p2)
      (goto-char (point-min))
      (while (search-forward ";-;" )
        (left-char 3)
        (setq p1 (point) p2 (line-end-position))
        (goto-char p2)
        (xah-make-overlay-bold-region p1 p2)))))

(add-hook 'clojure-mode-hook 'xah-color-special-comment)
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs