diff options
-rw-r--r-- | main.lua | 45 |
1 files changed, 33 insertions, 12 deletions
@@ -57,6 +57,23 @@ local function strip(str) return (str:gsub('^%s*(.-)%s*$', '%1')) end +local function utf8_seek(str, pos, n) + local step = n > 0 and 1 or -1 + local test = n > 0 + and function() return pos > #str end + or function() return pos <= 1 end + + while n ~= 0 and not test() do + repeat + pos = pos + step + until test() or bit.band(str:byte(pos), 0xc0) ~= 0x80 + + n = n - step + end + + return pos +end + local function read_json_file(fn) local f = io.open(script_dir .. '/' .. fn, 'r') if not f then @@ -553,7 +570,7 @@ local function update_search_matches() update_search_osd() end -local function search_input_text(event) +local function search_input_char(event) if event.event ~= 'down' and event.event ~= 'repeat' then return end @@ -561,30 +578,32 @@ local function search_input_text(event) local menu = menus[depth] local bc, ac = split_search_text() menu.search_text = bc .. event.key_text .. ac - menu.search_cursor = menu.search_cursor + 1 + menu.search_cursor = menu.search_cursor + #event.key_text update_search_matches() end local function search_input_bs() local menu = menus[depth] - if menu.search_cursor == 1 then + if menu.search_cursor <= 1 then return end - local bc, ac = split_search_text() - menu.search_text = bc:sub(1, -2) .. ac - menu.search_cursor = menu.search_cursor - 1 + local pos = utf8_seek(menu.search_text, menu.search_cursor, -1) + menu.search_text = menu.search_text:sub(1, pos - 1) .. + menu.search_text:sub(menu.search_cursor) + menu.search_cursor = pos update_search_matches() end local function search_input_del() local menu = menus[depth] - if menu.search_cursor == #menu.search_text + 1 then + if menu.search_cursor > #menu.search_text then return end - local bc, ac = split_search_text() - menu.search_text = bc .. ac:sub(2) + menu.search_text = menu.search_text:sub(1, menu.search_cursor - 1) .. + menu.search_text:sub( + utf8_seek(menu.search_text, menu.search_cursor, 1)) update_search_matches() end @@ -600,11 +619,13 @@ local function set_search_cursor(pos) end local function search_cursor_left() - set_search_cursor(menus[depth].search_cursor - 1) + local menu = menus[depth] + set_search_cursor(utf8_seek(menu.search_text, menu.search_cursor, -1)) end local function search_cursor_right() - set_search_cursor(menus[depth].search_cursor + 1) + local menu = menus[depth] + set_search_cursor(utf8_seek(menu.search_text, menu.search_cursor, 1)) end local function search_cursor_start() @@ -674,7 +695,7 @@ end function bind_search_keys() unbind_keys() - bind_key('ANY_UNICODE', search_input_text, {complex=true}) + bind_key('ANY_UNICODE', search_input_char, {complex=true}) bind_key('BS', search_input_bs, {repeatable=true}) bind_key('DEL', search_input_del, {repeatable=true}) |