From 10a98ca9505960b3bc21b6f965cb3184111f5e25 Mon Sep 17 00:00:00 2001 From: Olivier Certner Date: Thu, 14 Jan 2021 18:26:38 +0100 Subject: [PATCH 3/4] ERC: Track: Rewrite and rename `erc-track-find-face' For clarification purposes. No functional changes. * lisp/erc/erc-track.el (erc-track-find-face): Rename it to `erc-track-select-face'. Rewrite it so that it is very clear what is the current algorithm, changing the parameters it takes. No functional changes. Performance improvements. Clarify the documentation and remove the part on some faces being lists, which clearly doesn't apply. * lisp/erc/erc-track.el (erc-track-modified-channels): Replace calls to `erc-track-find-face' by calls to `erc-track-select-face', preserving the existing behavior. * lisp/erc/erc-track.el (erc-modified-channels-alist): Change the reference to `erc-track-select-face' in the documentation following the rename. --- lisp/erc/erc-track.el | 81 ++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/lisp/erc/erc-track.el b/lisp/erc/erc-track.el index 3e4e557d78..d63028cfeb 100644 --- a/lisp/erc/erc-track.el +++ b/lisp/erc/erc-track.el @@ -275,7 +275,7 @@ erc-modified-channels-alist is true). For how the face is chosen for a buffer, see -`erc-track-find-face' and `erc-track-priority-faces-only'. For +`erc-track-select-face' and `erc-track-priority-faces-only'. For how buffers are then displayed in the mode line, see `erc-modified-channels-display'.") @@ -733,38 +733,45 @@ erc-modified-channels-remove-buffer (when (called-interactively-p 'interactive) (erc-modified-channels-display))) -(defun erc-track-find-face (faces) - "Return the face to use in the mode line from the faces in FACES. -If `erc-track-faces-priority-list' is set, the one from FACES who -is first in that list will be used. If nothing matches or if -`erc-track-faces-priority-list' is not set, the default mode-line -faces will be used. +(defun erc-track-select-face (cur-face new-faces) + "Return the face to use in the mode line. -If `erc-track-faces-normal-list' is non-nil, use it to produce a -blinking effect that indicates channel activity when the first -element in FACES and the highest-ranking face among the rest of -FACES are both members of `erc-track-faces-normal-list'. +CUR-FACE is the face currently used in the mode line (for the +current buffer). NEW-FACES is the list of new faces that have +just been seen (in the current buffer). -If one of the faces is a list, then it will be ranked according -to its highest-tanking face member. A list of faces including -that member will take priority over just the single member -element." +Initially, the selected face is the one with highest priority in +`erc-track-faces-priority-list' (i.e., the one closest to the +head of the list) among CUR-FACE and NEW-FACES. If nothing +matches (including if `erc-track-faces-priority-list' is not +set), the default mode-line faces will be used (NIL is returned). + +If the selected face is still CUR-FACE (highest priority), and +the highest priority face in NEW-FACES alone is different (which +necessarily means it has lower priority than CUR-FACE), and both +are in `erc-track-faces-normal-list', then the latter is selected +instead. This has the effect of allowing the current mode line +face, if a member of `erc-track-faces-normal-list', to be +replaced by another with lower priority from the new faces, if +that with highest priority in the new ones is also a member of +`erc-track-faces-normal-list'." (let ((choice (catch 'face - (dolist (candidate erc-track-faces-priority-list) - (when (member candidate faces) - (throw 'face candidate))))) - (no-first (and erc-track-faces-normal-list - (catch 'face - (dolist (candidate erc-track-faces-priority-list) - (when (member candidate (cdr faces)) - (throw 'face candidate))))))) - (cond ((null choice) - nil) - ((and (member choice erc-track-faces-normal-list) - (member no-first erc-track-faces-normal-list)) - no-first) - (t - choice)))) + (dolist (candidate erc-track-faces-priority-list) + (when (or (equal candidate cur-face) + (member candidate new-faces)) + (throw 'face candidate)))))) + (when choice + (if (and (equal choice cur-face) + (member choice erc-track-faces-normal-list)) + (let ((only-in-new + (catch 'face + (dolist (candidate erc-track-faces-priority-list) + (when (member candidate new-faces) + (throw 'face candidate)))))) + (if (member only-in-new erc-track-faces-normal-list) + only-in-new + choice)) + choice)))) (defun erc-track-modified-channels () "Hook function for `erc-insert-post-hook' to check if the current @@ -804,17 +811,21 @@ erc-track-modified-channels ;; Add buffer, faces and counts (setq erc-modified-channels-alist (cons (cons (current-buffer) - (cons 1 (erc-track-find-face faces))) + (cons 1 (erc-track-select-mode-line-face + (car faces) + (cdr faces)))) erc-modified-channels-alist)) ;; Else modify the face for the buffer, if necessary. (when faces (let* ((cell (assq (current-buffer) erc-modified-channels-alist)) (old-face (cddr cell)) - (new-face (erc-track-find-face - (if old-face - (cons old-face faces) - faces)))) + (new-face (if old-face + (erc-track-select-mode-line-face old-face + faces) + (erc-track-select-mode-line-face + (car faces) + (cdr faces))))) (setcdr cell (cons (1+ (cadr cell)) new-face))))) ;; And display it (erc-modified-channels-display))) -- 2.30.0