From 741bce84890013e0452a4d8d70579ec731ba1cb5 Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Tue, 12 Dec 2023 19:06:52 -0800 Subject: [PATCH] Forgo excess nick buttonizing on JOINs and QUITs * etc/ERC-NEWS: Mention nick-button deduping. * lisp/erc/erc-button.el (erc-button-highlight-nick-once): New variable to specify commands and numerics for messages that ERC should forgo redundant buttonizing atop, assuming the presence of a "userhost". (erc-button--fallback-cmem-function): Change default value to `erc-button--get-user-from-spkr-prop'. (erc-button--get-user-from-spkr-prop): New function to derive user object from `erc--spkr' "msg prop". The point is to allow `erc-speaker-from-channel-member-function' to return a display name that differs from the speaker's actual nick as recorded by its `erc-server-user' object. (erc-button--get-phantom-cmem): Add `count' parameter. (erc-button-add-nickname-buttons): Stop after first turn if the current command appears in `erc-button-highlight-nick-once'. Pass iteration count to `erc-button--fallback-cmem-function'. (Bug#67677) --- etc/ERC-NEWS | 7 +++++++ lisp/erc/erc-button.el | 31 +++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/etc/ERC-NEWS b/etc/ERC-NEWS index bd8e95fa4ac..1311df3c21a 100644 --- a/etc/ERC-NEWS +++ b/etc/ERC-NEWS @@ -212,6 +212,13 @@ currently most prominent in "/ME" messages, where 'erc-action-face' sits beneath 'erc-input-face', as well as 'erc-my-nick-face' in the speaker portion. +** Fewer nick buttons in QUIT, JOIN, and PART messages. +Common messages that show a nickname followed by a "userhost" often +end up with redundant buttons because the nick reappears in or is the +same as the "~user" portion. ERC now tamps down on this to make +ing around more convenient. To opt out, see the new variable +'erc-button-highlight-nick-once'. + ** Improved interplay between buffer truncation and message logging. While most of these improvements are subtle, some affect everyday use. For example, users of the 'truncate' module may notice that truncation diff --git a/lisp/erc/erc-button.el b/lisp/erc/erc-button.el index e1c10be53f6..8e013c3a0d7 100644 --- a/lisp/erc/erc-button.el +++ b/lisp/erc/erc-button.el @@ -216,6 +216,9 @@ PAR is a number of a regexp grouping whose text will be passed to "URL of the EmacsWiki ELisp area." :type 'string) +(defvar erc-button-highlight-nick-once '(QUIT PART JOIN) + "Messages for which to buttonize only the first nick occurrence.") + (defvar erc-button-keymap (let ((map (make-sparse-keymap))) (define-key map (kbd "RET") #'erc-button-press-button) @@ -382,11 +385,22 @@ be updated at will.") (defvar-local erc-button--phantom-cmems nil) -(defvar erc-button--fallback-cmem-function #'ignore +(defvar erc-button--fallback-cmem-function + #'erc-button--get-user-from-spkr-prop "Function to determine channel member if not found in the usual places. -Called with DOWNCASED-NICK, NICK, and NICK-BOUNDS when +Called with DOWNCASED-NICK, NICK, NICK-BOUNDS, and COUNT when `erc-button-add-nickname-buttons' cannot find a user object for -DOWNCASED-NICK in `erc-channel-users' or `erc-server-users'.") +DOWNCASED-NICK in `erc-channel-users' or `erc-server-users'. +NICK-BOUNDS is a cons of buffer positions, and COUNT is a number +incremented with each visit, starting at 1.") + +(defun erc-button--get-user-from-spkr-prop (_ _ _ count) + "Attempt to obtain an `erc-channel-user' from current \"msg props\". +But only do so when COUNT is 1, meaning this is the first button +candidate in the just-inserted message." + (and-let* (((= 1 count)) + (nick (erc--check-msg-prop 'erc--spkr))) + (gethash nick erc-channel-users))) ;; Historical or fictitious users. As long as these two structs ;; remain superficial "subclasses" with the same slots and defaults, @@ -408,7 +422,7 @@ DOWNCASED-NICK in `erc-channel-users' or `erc-server-users'.") (puthash downcased (cons user cuser) erc-button--phantom-cmems) (cons user cuser))) -(defun erc-button--get-phantom-cmem (down _word _bounds) +(defun erc-button--get-phantom-cmem (down _word _bounds _count) (gethash down erc-button--phantom-cmems)) (define-minor-mode erc-button--phantom-users-mode @@ -446,10 +460,15 @@ retrieve it during buttonizing via (and erc-button-buttonize-nicks erc-button--modify-nick-function))) (erc-button--extract-form form))) + (oncep (if-let ((erc-button-highlight-nick-once) + (c (erc--check-msg-prop 'erc--cmd)) + ((memq c erc-button-highlight-nick-once))) + 1 0)) (seen 0)) (goto-char (point-min)) (while-let - (((erc-forward-word)) + (((or (zerop seen) (zerop oncep))) + ((erc-forward-word)) (bounds (or (and (= 1 (cl-incf seen)) (erc--get-speaker-bounds)) (erc-bounds-of-word-at-point))) (word (buffer-substring-no-properties (car bounds) (cdr bounds))) @@ -459,7 +478,7 @@ retrieve it during buttonizing via (cuser (and erc-channel-users (or (gethash down erc-channel-users) (funcall erc-button--fallback-cmem-function - down word bounds)))) + down word bounds seen)))) (user (or (and cuser (car cuser)) (and erc-server-users (gethash down erc-server-users)))) (data (list word))) -- 2.39.2