Skip to content

Commit

Permalink
Fix removing multibyte characters in prompt, fixes #974
Browse files Browse the repository at this point in the history
This change does what i've said in #974, works well for me.
  • Loading branch information
necauqua committed Sep 7, 2018
1 parent d0afcaa commit d5b80ad
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions lib/awful/prompt.lua
Expand Up @@ -115,7 +115,6 @@ local io = io
local table = table
local math = math
local ipairs = ipairs
local pcall = pcall
local capi =
{
selection = selection
Expand Down Expand Up @@ -839,7 +838,12 @@ function prompt.run(args, textbox, exe_callback, completion_callback,
elseif key == "End" then
cur_pos = #command + 1
elseif key == "BackSpace" then
if cur_pos > 1 then
local byte = 128
-- unicode continuation bytes begin with bits 1 and 0
-- so a continuation byte is one in range [128;192)
-- not a bitshift of any kind for max compatibility
while cur_pos > 1 and byte > 127 and byte < 192 do
byte = command:sub(cur_pos - 1, cur_pos):byte()
command = command:sub(1, cur_pos - 2) .. command:sub(cur_pos)
cur_pos = cur_pos - 1
end
Expand Down Expand Up @@ -885,21 +889,7 @@ function prompt.run(args, textbox, exe_callback, completion_callback,
selectall = nil
end

local success = pcall(update)
while not success do
-- TODO UGLY HACK TODO
-- Setting the text failed. Most likely reason is that the user
-- entered a multibyte character and pressed backspace which only
-- removed the last byte. Let's remove another byte.
if cur_pos <= 1 then
-- No text left?!
break
end

command = command:sub(1, cur_pos - 2) .. command:sub(cur_pos)
cur_pos = cur_pos - 1
success = pcall(update)
end
update()

if changed_callback then
changed_callback(command)
Expand Down

0 comments on commit d5b80ad

Please sign in to comment.