From 95b119effcc7635f6fe1f2120c9a6d1502db4964 Mon Sep 17 00:00:00 2001 From: Tor Hveem Date: Mon, 6 Jul 2026 21:10:27 +0200 Subject: [PATCH] colorize_nicks.py v34: fix problem with nicks with - in them --- python/colorize_nicks.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/python/colorize_nicks.py b/python/colorize_nicks.py index d441420e..07660d32 100644 --- a/python/colorize_nicks.py +++ b/python/colorize_nicks.py @@ -25,6 +25,12 @@ # https://github.com/ryoskzypu/weechat_scripts # # History: +# 2026-07-06: xt +# version 34: fix colorization of nicks containing regex-special characters +# (e.g., hyphens) — re.escape() was applied before dictionary lookup, +# causing escaped nick form (e.g., 'flash\-code') to not match unescaped +# dict keys (e.g., 'flash-code'). Keep nick unescaped for lookup/display, +# escape only when building the line-matching regex. # 2025-05-08: ryoskzypu # version 33: add many improvements, features, and fixes # 2023-10-30: Sébastien Helleu @@ -108,7 +114,7 @@ SCRIPT_NAME = 'colorize_nicks' SCRIPT_AUTHOR = 'xt ' -SCRIPT_VERSION = '33' +SCRIPT_VERSION = '34' SCRIPT_LICENSE = 'GPL' SCRIPT_DESC = 'Use the weechat nick colors in the chat area' @@ -443,14 +449,14 @@ def colorize_nicks(buffer, min_len, prefixes, suffixes, has_colors, line): (?P [^ ]+) ''' if (nick := re.search(nicks_rgx, word, flags=re.VERBOSE)) is not None: - nick = re.escape(nick.group('nick')) + nick = nick.group('nick') # If the word is not a known nick and its last character is an option # suffix (e.g. colon ':' or comma ','), try to match the word without it. # This is necessary as 'foo:' is a valid nick, which could be addressed # as 'foo::'. if nick not in colored_nicks[buffer]: - if (suffix := re.search(rf'[{suffixes}]$', nick)) is not None: + if re.search(rf'[{suffixes}]$', nick) is not None: nick = nick[:-1] # Nick exists on buffer. @@ -462,10 +468,11 @@ def colorize_nicks(buffer, min_len, prefixes, suffixes, has_colors, line): nick_color = colored_nicks[buffer][nick]['color'] # Find nick in the line. + nick_re = re.escape(nick) line_rgx = rf''' (?: \A | [ ]) # Boundary (?P [{prefixes}])? # Optional prefix char - (?P {nick}) + (?P {nick_re}) [{suffixes}]? # " suffix char (?: \Z | [ ]) # Boundary '''