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 @@ -7,6 +7,14 @@ "nick": { "soundfile": "audio/interface/ui/chat_alert.ogg", "threshold": 3000 } }; +/** + * Used to track autocompleted names to try next autocompletion if multiples has been found. + */ +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) { @@ -144,7 +152,7 @@ return Engine.ConfigDB_GetValue("user", "playername.multiplayer") || Engine.GetSystemUsername(); } -function tryAutoComplete(text, autoCompleteList) +function tryAutoComplete(text, autoCompleteList, tries) { if (!text.length) return text; @@ -157,19 +165,35 @@ if (!lastWord.length) return text; + let firstFound = ""; for (var word of autoCompleteList) { if (word.toLowerCase().indexOf(lastWord.toLowerCase()) != 0) continue; - text = wordSplit.join(" "); - if (text.length > 0) - text += " "; + --tries; + if (!firstFound) + firstFound = word; - text += word; - break; + if (tries < 0) + break; } - return text; + + if (firstFound == "") + return text; + + // 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; + } + + text = wordSplit.join(" "); + if (text.length > 0) + text += " "; + + return text + word; } function autoCompleteNick(guiObject, playernames) @@ -179,11 +203,23 @@ return; let bufferPosition = guiObject.buffer_position; - let textTillBufferPosition = text.substring(0, bufferPosition); - let newText = tryAutoComplete(textTillBufferPosition, playernames); + 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 + text.substring(bufferPosition); - guiObject.buffer_position = bufferPosition + (newText.length - textTillBufferPosition.length); + 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()