erc-highlight-nicknames.el: review

This commit is contained in:
Jeremy Baxter 2024-07-18 19:32:50 +12:00
parent 72240ee488
commit a168c92c18

View file

@ -88,8 +88,7 @@
(require 'erc) (require 'erc)
(require 'erc-button) (require 'erc-button)
(defvar erc-highlight-facec 16) (defconst erc-highlight-faces
(defvar erc-highlight-faces
'(fg:erc-color-face0 '(fg:erc-color-face0
fg:erc-color-face1 fg:erc-color-face1
fg:erc-color-face2 fg:erc-color-face2
@ -109,13 +108,13 @@
(defface erc-highlight-nick-base-face (defface erc-highlight-nick-base-face
'((t nil)) '((t nil))
"Base face used for highlighting nicks in erc. (Before the nick "Base face used for highlighting nicknames in ERC before the
color is added)" color is added."
:group 'erc-faces) :group 'erc-faces)
(defvar erc-highlight-face-table (defvar erc-highlight-face-table
(make-hash-table :test 'equal) (make-hash-table :test 'equal)
"The hash table that contains unique erc nickname faces.") "Hash table containing unique ERC nickname faces.")
(defun hexcolor-luminance (color) (defun hexcolor-luminance (color)
"Returns the luminance of color COLOR. COLOR is a string \(e.g. "Returns the luminance of color COLOR. COLOR is a string \(e.g.
@ -138,9 +137,7 @@ between 0 and 255."
(- 65535 r) (- 65535 g) (- 65535 b)))) (- 65535 r) (- 65535 g) (- 65535 b))))
(defun erc-highlight-nicknames () (defun erc-highlight-nicknames ()
"Searches for nicknames and highlights them. Uses the first "Highlight nicknames using a random face from `erc-highlight-faces'."
twelve digits of the MD5 message digest of the nickname as
color (#rrrrggggbbbb)."
(with-syntax-table erc-button-syntax-table (with-syntax-table erc-button-syntax-table
(let (bounds bound-start bound-end word color new-nick-face) (let (bounds bound-start bound-end word color new-nick-face)
(goto-char (point-min)) (goto-char (point-min))
@ -153,13 +150,15 @@ color (#rrrrggggbbbb)."
(when (erc-get-server-user word) (when (erc-get-server-user word)
(setq new-nick-face (gethash word erc-highlight-face-table)) (setq new-nick-face (gethash word erc-highlight-face-table))
(unless new-nick-face (unless new-nick-face
(setq new-nick-face (nth (random erc-highlight-facec) erc-highlight-faces)) (setq new-nick-face
(nth (random (length erc-highlight-faces))
erc-highlight-faces))
(setq color (face-foreground new-nick-face)) (setq color (face-foreground new-nick-face))
(if (equal (cdr (assoc 'background-mode (frame-parameters))) 'dark) (if (equal (cdr (assoc 'background-mode (frame-parameters))) 'dark)
;; if too dark for background ;; if too dark for background
(when (< (hexcolor-luminance color) 85) (when (< (hexcolor-luminance color) 85)
(setq color (invert-color color))) (setq color (invert-color color)))
;; if to bright for background ;; if too bright for background
(when (> (hexcolor-luminance color) 170) (when (> (hexcolor-luminance color) 170)
(setq color (invert-color color)))) (setq color (invert-color color))))
(copy-face 'erc-highlight-nick-base-face new-nick-face) (copy-face 'erc-highlight-nick-base-face new-nick-face)
@ -170,7 +169,7 @@ color (#rrrrggggbbbb)."
)))) ))))
(define-erc-module highlight-nicknames nil (define-erc-module highlight-nicknames nil
"Search through the buffer for nicknames, and highlight." "Search through the buffer for nicknames, and highlight them."
((add-hook 'erc-insert-modify-hook 'erc-highlight-nicknames t)) ((add-hook 'erc-insert-modify-hook 'erc-highlight-nicknames t))
((remove-hook 'erc-insert-modify-hook 'erc-highlight-nicknames))) ((remove-hook 'erc-insert-modify-hook 'erc-highlight-nicknames)))