I want to highlight somehow symbol under cursor, is there a way to customize font for that current symbol? Ideally I want to add underline with some color to it, or maybe change bg color of text, is there a package for that or specific default font?
hlt-unhighlight-symbol highlight and unhighlight the symbol at point or under the mouse pointer.
(They are bound to
C-x X h s and
C-x X u s, by default. All commands of the library are bound in keymap
hlt-map, which is on prefix key
C-x X by default.)
Normally, these commands provide a highlighting background color by default (changed each time you invoke them). But when called from Lisp they accept a
FACE argument and use that face.
So you can easily define your own command that uses an underline of a given color, for which you are prompted:
(defun my-highlight-symbol-with-face (symbol color all-buffers-p) "Highlight occurrences of SYMBOL using an underline of COLOR. The symbol at point is used by default, or the symbol under the mouse pointer if the command is invoked using the mouse. Highlight over the region if active, or the buffer otherwise. With a prefix arg, use all buffers that are visible or iconified. \(This first unhighlights occurrences, to prevent stacking up multiple highlighting on the same occurrences.)" (interactive (save-excursion (when (listp last-nonmenu-event) (mouse-set-point last-nonmenu-event)) (let ((symb (symbol-at-point)) colr) (unless symb (error "No symbol %s" (if (listp last-nonmenu-event) "under mouse pointer" "at point"))) (setq colr (read-color "Color: ")) (list symb colr current-prefix-arg)))) (let ((face `(:underline (:color ,color)))) (hlt-highlight-symbol symbol all-buffers-p face)))
(You could also specify an underlining style in the code, or prompt for it. By default, the style is a straight line.)
hlt-unhighlight-symbol to unhighlight this highlighting.
(defvar hi-last-symbol nil) (defun hilight-current-symbol (&optional arg) (interactive) (let ((sym (thing-at-point 'symbol))) (cond ;; sym + last and not equal ((and sym hi-last-symbol (not (string= hi-last-symbol sym))) ;; unhilite the last one (hi-lock-unface-buffer (format "\\_<%s\\_>" (regexp-quote hi-last-symbol))) ;; highlight this one (hi-lock-face-buffer (format "\\_<%s\\_>" (regexp-quote sym))) (setq hi-last-symbol sym)) ;; sym + no last ((and sym ;we have symbol (null hi-last-symbol)) ;; highlight this one (hi-lock-face-buffer (format "\\_<%s\\_>" (regexp-quote sym))) (setq hi-last-symbol sym)) ;; no sym and last ((and (null sym) (not (null hi-last-symbol))) (hi-lock-unface-buffer (format "\\_<%s\\_>" (regexp-quote hi-last-symbol))) (setq hi-last-symbol sym))))) (add-hook 'post-command-hook 'hilight-current-symbol)
The highlight-symbol package is a simple way to do exactly this. It provides
highlight-symbol which will toggle the highlighting of the symbol under the point and
highlight-symbol-mode which will continuously update the highlighting as the point moves.
The default is to change the background color. To underline instead use
(set-face-attribute 'highlight-symbol-face nil :background nil :underline t)