Saturday 19 November 2011

QuickBits-10: Highlight a word in Emacs buffer

If you have used Source Insight for code browsing, you would have used its word hightlight feature.You can highlight the word under cursor, in your file you are browsing using shift+f8.

You can do the same in Emacs, by adding this to your .emacs file


 
(defun is-word-highlighted ()
  (interactive)
  (let ((face (or (get-char-property (point) 'read-face-name)
                  (get-char-property (point) 'face))))
   (if (facep face) (if (face-equal face "hi-yellow") t nil) nil)))

(defun toggle-highlight-word ()
(interactive)
(setq sym (current-word))
(if (is-word-highlighted) (unhighlight-regexp sym) (highlight-regexp sym)) 
)

(global-set-key [S-f8] 'toggle-highlight-word)

No comments:

Post a Comment