Skip to content

Instantly share code, notes, and snippets.

@syohex
Created November 21, 2012 01:03
Show Gist options
  • Select an option

  • Save syohex/4122386 to your computer and use it in GitHub Desktop.

Select an option

Save syohex/4122386 to your computer and use it in GitHub Desktop.
wrapper hi-lock.el for easy to use highlight word or regexp
(eval-when-compile
(require 'cl))
(require 'hi-lock)
(require 'thingatpt)
(defgroup easy-highlight nil
"Easy to use `hi-lock'"
:group 'faces
:prefix "easy-highlight:")
(defvar easy-highlight:face-list (copy-list hi-lock-face-defaults)
"Default face list")
(defvar easy-highlight:highlighted (make-hash-table))
(defun easy-highlight:next-face ()
(when (null easy-highlight:face-list)
(setq easy-highlight (copy-list hi-lock-face-defaults)))
(pop easy-highlight:face-list))
(defun easy-highlight:regexp (regexp)
(interactive
(list
(read-regexp "Regexp" (thing-at-point 'word))))
(let ((face (easy-highlight:next-face)))
(if (gethash regexp easy-highlight:highlighted)
(error (format "'%s' is already highlighted" regexp))
(puthash regexp t easy-highlight:highlighted))
(hi-lock-face-buffer regexp face)))
(defun easy-highlight:words (words-str)
(interactive
(list
(read-string "Highlighted Words: ")))
(loop for word in (split-string words-str)
do
(easy-highlight:regexp word)))
(defun easy-highlight:highlighted-regexps ()
(let ((regexps (loop for re being the hash-keys in easy-highlight:highlighted
collect re)))
(unless regexps
(error "No regexps highlighted"))
regexps))
(defun easy-highlight:clear (str)
(interactive
(list
(completing-read "Clear Regexp: "
(easy-highlight:highlighted-regexps) nil t
nil nil (thing-at-point 'word))))
(hi-lock-unface-buffer str))
(defun easy-highlight:clear-all ()
(interactive)
(loop for re being the hash-keys in easy-highlight:highlighted
do
(hi-lock-unface-buffer re))
(clrhash easy-highlight:highlighted))
(provide 'easy-highlight)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment