I dry-coded this (didn't run, didn't check for syntax errors), but hopefully it gets the idea across. It looks like you're mainly concerned with replacing names with class-colored versions of those same names, and you have two functions for doing the name-replacement in slightly different ways. Since the two functions are so similar, I made them into a single function implementation.
Now that I see what you're trying to accomplish, I think there's something smarter that can be done to iterate through
chatMessage and alternate grabbing whitespace and non-whitespace. I'll think about how to do that tomorrow when I have time.
Lua Code:
-- Gets a name from the given string after stripping out punctuation.
local function getName(s)
local stripped = s:gsub("'s$", ''):gsub('%p', '') -- strip possessive then punctuation
local name, realm = UnitName(stripped)
local fullName = (realm and realm ~= "") and name .. '-' .. realm or name
if fullName == stripped then
-- "stripped" isn't a unit ID or GUID.
return fullName, name, realm
end
return nil
end
--[[
Extracts a full name (relative to the player's realm) from the given string
Returns:
unit ID: a unit ID that can be passed to unit functions, or nil
fullName: "name-realm", or "name" if the realm is the same as the player's realm, or nil.
--]]
local function extractFullName(s)
local fullName = getName(s)
return fullName, fullname
end
--[[
Extracts a simple name (no realm), or "you" or "your", from the given string.
Returns:
unit ID: a unit ID that can be passed to unit functions, or nil
name: "name" with no realm information, or nil.
--]]
local playerName = UnitName("player")
local function extractShortName(s)
local fullName, name = getName(s)
if fullName then
return fullName, name
end
local stripped = s:gsub("'s$", ''):gsub('%p', '') -- strip possessive then punctuation
local lowered = stripped:lower()
if lowered == "you" or lowered == "your" then
return playerName, stripped
end
return nil
end
-- Replaces name with class-colored names in chatMessage.
local hexColorNames
do
local coloredMessage = {}
function hexColorNames(chatMessage, modChatColor)
extractNameFunc = extractNameFunc or extractFullName
wipe(coloredMessage)
for word in gmatch(chatMessage, "%S+") do
local unit, name = extractNameFunc(word)
if unit and name then
local _, class = UnitClass(unit)
if class then
local coloredName = "|c" .. RAID_CLASS_COLORS[class].colorStr .. name .. modChatColor
word = word:gsub(name, coloredName)
end
end
table.insert(coloredMessage, word)
end
return table.concat(coloredMessage, ' ')
end
end
local function modHexColorNames(chatMessage, modChatColor)
return hexColorNames(chatMessage, modChatColor, extractShortName)
end