Index: binaries/data/mods/public/gui/common/functions_utility.js =================================================================== --- binaries/data/mods/public/gui/common/functions_utility.js +++ binaries/data/mods/public/gui/common/functions_utility.js @@ -3,6 +3,14 @@ */ var g_LastNickNotification = -1; +/** + * Used to track autocompleted names to try next autocompletion if found multiples + */ +var g_LastAutoCompleteBufferPosition = 0; +var g_LastAutoCompleteText = ""; +var g_SameAutoCompleteTries = 0; +var g_LastAutoCompleteNewText = ""; + // Get list of XML files in pathname with recursion, excepting those starting with _ function getXMLFileList(pathname) { @@ -140,7 +148,7 @@ return Engine.ConfigDB_GetValue("user", "playername.multiplayer") || Engine.GetSystemUsername(); } -function tryAutoComplete(text, autoCompleteList) +function tryAutoComplete(text, autoCompleteList, tries) { if (!text.length) return text; @@ -153,19 +161,35 @@ if (!lastWord.length) return text; + let firstFound = ""; for (var word of autoCompleteList) { if (word.toLowerCase().indexOf(lastWord.toLowerCase()) != 0) continue; + else + { + --tries; + if (firstFound == "") + firstFound = word; + } + if (tries < 0) break; + } - text = wordSplit.join(" "); - if (text.length > 0) - text += " "; + if (firstFound == "") + return text; - text += word; - break; + // wrap search to start, cause tries could not complete to 0, means there are no more matches as tries in list + if (tries >= 0) + { + g_SameAutoCompleteTries = 1; + word = firstFound; } - return text; + + text = wordSplit.join(" "); + if (text.length > 0) + text += " "; + + return text + word; } function autoCompleteNick(guiObject, playernames) @@ -175,11 +199,23 @@ return; let bufferPosition = guiObject.buffer_position; - let textTillBufferPosition = text.substring(0, bufferPosition); - let newText = tryAutoComplete(textTillBufferPosition, playernames); - - guiObject.caption = newText + text.substring(bufferPosition); - guiObject.buffer_position = bufferPosition + (newText.length - textTillBufferPosition.length); + let sameTry = g_LastAutoCompleteNewText == text; + if (!sameTry) + { + g_LastAutoCompleteBufferPosition = bufferPosition; + g_LastAutoCompleteText = text; + g_LastAutoCompleteNewText = ""; + g_SameAutoCompleteTries = 0; + } + + let textTillBufferPosition = sameTry ? g_LastAutoCompleteText.substring(0, g_LastAutoCompleteBufferPosition) : text.substring(0, bufferPosition); + let newText = tryAutoComplete(textTillBufferPosition, playernames, g_SameAutoCompleteTries++); + + guiObject.caption = newText + (sameTry ? g_LastAutoCompleteText.substring(g_LastAutoCompleteBufferPosition) : text.substring(bufferPosition)); + if (g_LastAutoCompleteNewText == "" || sameTry) + g_LastAutoCompleteNewText = guiObject.caption; + guiObject.buffer_position = (sameTry ? g_LastAutoCompleteBufferPosition : bufferPosition) + (newText.length - textTillBufferPosition.length); + return; } function clearChatMessages()