From 1d3c9f47085ee5d9cfa37d988d53f2dfead0195a Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 18 Aug 2022 10:22:48 -0700 Subject: [PATCH] generalize a function --- search.lua | 4 ++-- select.lua | 2 +- text.lua | 19 ++++++++++--------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/search.lua b/search.lua index 01a832b..bd28d58 100644 --- a/search.lua +++ b/search.lua @@ -62,7 +62,7 @@ function Text.search_next(State) end if Text.lt1(State.cursor1, State.screen_top1) or Text.lt1(State.screen_bottom1, State.cursor1) then State.screen_top1.line = State.cursor1.line - local pos = Text.pos_at_start_of_cursor_screen_line(State) + local pos = Text.pos_at_start_of_screen_line(State, State.cursor1) State.screen_top1.pos = pos end end @@ -110,7 +110,7 @@ function Text.search_previous(State) end if Text.lt1(State.cursor1, State.screen_top1) or Text.lt1(State.screen_bottom1, State.cursor1) then State.screen_top1.line = State.cursor1.line - local pos = Text.pos_at_start_of_cursor_screen_line(State) + local pos = Text.pos_at_start_of_screen_line(State, State.cursor1) State.screen_top1.pos = pos end end diff --git a/select.lua b/select.lua index c5794e1..bae9504 100644 --- a/select.lua +++ b/select.lua @@ -130,7 +130,7 @@ function Text.delete_selection_without_undo(State) State.cursor1.pos = minp if Text.lt1(State.cursor1, State.screen_top1) then State.screen_top1.line = State.cursor1.line - State.screen_top1.pos = Text.pos_at_start_of_cursor_screen_line(State) + State.screen_top1.pos = Text.pos_at_start_of_screen_line(State, State.cursor1) end State.selection1 = {} -- delete everything between min (inclusive) and max (exclusive) diff --git a/text.lua b/text.lua index ff98140..9c4a009 100644 --- a/text.lua +++ b/text.lua @@ -426,7 +426,7 @@ end function Text.up(State) assert(State.lines[State.cursor1.line].mode == 'text') --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos) - local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_cursor_screen_line(State) + local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1) if screen_line_starting_pos == 1 then --? print('cursor is at first screen line of its line') -- line is done; skip to previous text line @@ -500,7 +500,7 @@ function Text.down(State) -- move down one screen line in current line local scroll_down = Text.le1(State.screen_bottom1, State.cursor1) --? print('cursor is NOT at final screen line of its line') - local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_cursor_screen_line(State) + local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1) new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index+1] --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos)) local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos) @@ -525,7 +525,7 @@ end function Text.end_of_line(State) State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1 - local botpos = Text.pos_at_start_of_cursor_screen_line(State) + local botpos = Text.pos_at_start_of_screen_line(State, State.cursor1) local botline1 = {line=State.cursor1.line, pos=botpos} if Text.cursor_out_of_screen(State) then Text.snap_cursor_to_bottom_of_screen(State) @@ -636,11 +636,12 @@ function Text.right_without_scroll(State) end end -function Text.pos_at_start_of_cursor_screen_line(State) - Text.populate_screen_line_starting_pos(State, State.cursor1.line) - for i=#State.line_cache[State.cursor1.line].screen_line_starting_pos,1,-1 do - local spos = State.line_cache[State.cursor1.line].screen_line_starting_pos[i] - if spos <= State.cursor1.pos then +function Text.pos_at_start_of_screen_line(State, loc1) + Text.populate_screen_line_starting_pos(State, loc1.line) + local line_cache = State.line_cache[loc1.line] + for i=#line_cache.screen_line_starting_pos,1,-1 do + local spos = line_cache.screen_line_starting_pos[i] + if spos <= loc1.pos then return spos,i end end @@ -984,7 +985,7 @@ function Text.cursor_out_of_screen(State) return State.cursor_y == nil -- this approach is cheaper and almost works, except on the final screen -- where file ends above bottom of screen ---? local botpos = Text.pos_at_start_of_cursor_screen_line(State) +--? local botpos = Text.pos_at_start_of_screen_line(State, State.cursor1) --? local botline1 = {line=State.cursor1.line, pos=botpos} --? return Text.lt1(State.screen_bottom1, botline1) end