Commit 6b156fb8a29add9a96f582e8e231c87a517b7354

Authored by DarkGod
1 parent 7a3ee623

Pressing tab when typing a player name as the first word of a sentence will try to autocomplete it

... ... @@ -78,6 +78,8 @@ function _M:init(chat, on_end)
78 78 self.chat:setCurrentTarget(false, self.chat.last_whispers[found])
79 79 self:updateTitle(self:getTitle())
80 80 end
  81 + else
  82 + self:autoComplete()
81 83 end
82 84 end)
83 85 end
... ... @@ -136,6 +138,35 @@ function _M:checkTarget(text)
136 138 end
137 139 end
138 140
  141 +function _M:autoComplete()
  142 + local text, text_len = self.c_box.text, self.c_box.text:len()
  143 +
  144 + local matches = {}
  145 + for k, v in pairs(self.chat.channels[self.chat.cur_channel].users) do
  146 + if k:sub(1, text_len) == text then
  147 + matches[#matches+1] = k
  148 + end
  149 + end
  150 + if #matches == 1 then
  151 + self.c_box:setText(matches[1])
  152 + elseif #matches > 1 then
  153 + -- Find the longest common substring and complete it
  154 + local substring = matches[1]:sub(#text+1)
  155 + for i=2,#matches do
  156 + local min_len = math.min(#matches[i]-#text, #substring)
  157 + for j=1,min_len do
  158 + if substring:sub(j, j) ~= matches[i]:sub(#text+j, #text+j) then
  159 + substring = substring:sub(1, util.bound(j-1, 0))
  160 + break
  161 + end
  162 + end
  163 + if #substring > 0 then
  164 + self.c_box:setText(text .. substring)
  165 + end
  166 + end
  167 + end
  168 +end
  169 +
139 170 function _M:okclick()
140 171 local text = self.c_box.text
141 172
... ...