From c06e744feb17ba90caa7ac92f668cfeb37060c67 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 16 May 2022 21:10:35 -0700 Subject: [PATCH] speed up some obvious common cases This is probably not worth the effort. First sign of trouble, back it out. --- main.lua | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/main.lua b/main.lua index 5392742..742c048 100644 --- a/main.lua +++ b/main.lua @@ -546,13 +546,13 @@ function keychord_pressed(chord) if cursor_line > 1 then local old_x = cursor_x(lines[cursor_line], cursor_pos) cursor_line = cursor_line-1 - cursor_pos = nearest_cursor_pos(lines[cursor_line], old_x) + cursor_pos = nearest_cursor_pos(lines[cursor_line], old_x, cursor_pos) end elseif chord == 'down' then if cursor_line < #lines then local old_x = cursor_x(lines[cursor_line], cursor_pos) cursor_line = cursor_line+1 - cursor_pos = nearest_cursor_pos(lines[cursor_line], old_x) + cursor_pos = nearest_cursor_pos(lines[cursor_line], old_x, cursor_pos) end elseif chord == 'delete' then if cursor_pos <= #lines[cursor_line] then @@ -712,11 +712,24 @@ function cursor_x(line, cursor_pos) return text_before_cursor:getWidth() end -function nearest_cursor_pos(line, x) +function nearest_cursor_pos(line, x, hint) if x == 0 then return 1 end + local max_x = cursor_x(line, #line+1) + if x > max_x then + return #line+1 + end + local currx = cursor_x(line, hint) + if currx > x-2 and currx < x+2 then + return hint + end local left, right = 1, #line+1 + if currx > x then + right = hint + else + left = hint + end while left < right-1 do local curr = math.floor((left+right)/2) local currx = cursor_x(line, curr)