Skip to content

Commit

Permalink
[fu] init-highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
pkryger committed Nov 20, 2024
1 parent 0cf6108 commit 9d30d33
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions modules/init-highlight.el
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
(load (file-name-concat (locate-user-emacs-file "modules") "init-require"))))
(exordium-require 'init-prefs)

(require 'hi-lock)

(use-package highlight
:defer t)

Expand All @@ -45,15 +43,22 @@


;;; Highlight symbol under point automatically after a small delay.

(when exordium-highlight-symbol
(use-package highlight-symbol
;; N.B. the `highlight-symbol' package has not been updated for a while (at
;; the time of writing this comment in 2024 last update was from 2016) and
;; it forcibly defines its own alias for `highlight-symbol-at-point',
;; clobbering the one delivered with Emacs in `hi-lock'. However, the
;; `highlight-symbol' implementation is nicer, as it allows to remove
;; highlighting with the same keybinding. Also the `highlight-symbol-mode'
;; is the mode that is turned on (`hi-lock-mode' is not turned on), so
;; let's keep that implementation, by ensuring load order.
:after (hi-lock)
:diminish highlight-symbol-mode
:hook (prog-mode . highlight-symbol-mode)
:hook ((prog-mode . highlight-symbol-mode)
(prog-mode . highlight-symbol-nav-mode))
:custom
(highlight-symbol-on-navigation-p t)
:config
(highlight-symbol-nav-mode)))
(highlight-symbol-on-navigation-p t)))


;;; Highlight/unhighlight symbol under point using a key.
Expand All @@ -62,22 +67,29 @@
"List of regexps for the currently highlighted symbols.
This variable is buffer-local.")

(defun exordium-highlight-symbol-at-point ()
"Toggle highlighting of occurrences of the symbol under point.
Up to 4 different symbols can be highlighted using different
colors at one time."
(interactive)
(let ((regex (find-tag-default-as-symbol-regexp)))
(cond ((member regex exordium-highlighted-symbols)
;; Remove highlight for this symbol.
(setq exordium-highlighted-symbols (remove regex exordium-highlighted-symbols))
(hi-lock-unface-buffer regex))
(t
;; Add highlight for this symbol.
(setq exordium-highlighted-symbols (cons regex exordium-highlighted-symbols))
(hi-lock-face-symbol-at-point)))))

(global-set-key (kbd "C-c C-SPC") #'exordium-highlight-symbol-at-point)
(use-package hi-lock
:ensure nil
:demand t
:autoload (hi-lock-regexp-okay)
:functions (exordium-highlight-symbol-at-point)
:init
(defun exordium-highlight-symbol-at-point ()
"Toggle highlighting of occurrences of the symbol under point.
Faces from `hi-lock-face-defaults' are used to perform the highlight, so up
to the number of elements in that list of different symbols can
be highlighted using different colors at one time."
(interactive)
(when-let* ((regexp (ignore-errors (hi-lock-regexp-okay
(find-tag-default-as-symbol-regexp)))))
(cond ((member regexp exordium-highlighted-symbols)
;; Remove highlight for this symbol.
(setq exordium-highlighted-symbols (remove regexp exordium-highlighted-symbols))
(hi-lock-unface-buffer regexp))
(t
;; Add highlight for this symbol.
(setq exordium-highlighted-symbols (cons regexp exordium-highlighted-symbols))
(hi-lock-face-symbol-at-point)))))
:bind ("C-c C-SPC" . #'exordium-highlight-symbol-at-point))


;; Highlight color name and Hex values in buffer.
Expand Down

0 comments on commit 9d30d33

Please sign in to comment.