From f79dd4824c72ab93e90bdbf1d3fa130f7fb3c97d Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 18 Aug 2022 00:19:07 -0700 Subject: [PATCH 1/8] simplify --- text.lua | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/text.lua b/text.lua index c38dc1e..4a585b7 100644 --- a/text.lua +++ b/text.lua @@ -498,10 +498,7 @@ function Text.down(State) end else -- move down one screen line in current line - local scroll_down = false - if Text.le1(State.screen_bottom1, State.cursor1) then - scroll_down = true - end + 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) new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index+1] From a14f1096b6b11940bd677000eb41bc8ba6e9b559 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 18 Aug 2022 09:51:43 -0700 Subject: [PATCH 2/8] extract a variable --- text.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/text.lua b/text.lua index 4a585b7..ff98140 100644 --- a/text.lua +++ b/text.lua @@ -870,9 +870,10 @@ function Text.to2(State, loc1) return {line=loc1.line, screen_line=1, screen_pos=1} end local result = {line=loc1.line, screen_line=1} + local line_cache = State.line_cache[loc1.line] Text.populate_screen_line_starting_pos(State, loc1.line) - for i=#State.line_cache[loc1.line].screen_line_starting_pos,1,-1 do - local spos = State.line_cache[loc1.line].screen_line_starting_pos[i] + 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 result.screen_line = i result.screen_pos = loc1.pos - spos + 1 From 3c043105034e5f0d2df7fd01bc04abb11ad277fb Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 18 Aug 2022 10:09:20 -0700 Subject: [PATCH 3/8] subsection headings in a long switch --- edit.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/edit.lua b/edit.lua index a0293ab..011f6b9 100644 --- a/edit.lua +++ b/edit.lua @@ -338,6 +338,7 @@ function edit.keychord_pressed(State, chord, key) State.search_term = '' State.search_backup = {cursor={line=State.cursor1.line, pos=State.cursor1.pos}, screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos}} assert(State.search_text == nil) + -- zoom elseif chord == 'C-=' then edit.update_font_settings(State, State.font_height+2) Text.redraw_all(State) @@ -347,6 +348,7 @@ function edit.keychord_pressed(State, chord, key) elseif chord == 'C-0' then edit.update_font_settings(State, 20) Text.redraw_all(State) + -- undo elseif chord == 'C-z' then for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll local event = undo_event(State) From cf8d9774eac577b4cd4fdb77cbb27b6b46004756 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 18 Aug 2022 10:28:06 -0700 Subject: [PATCH 4/8] drop some obsolete args --- search.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/search.lua b/search.lua index e613ed7..01a832b 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, State.left, State.right) + local pos = Text.pos_at_start_of_cursor_screen_line(State) 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, State.left, State.right) + local pos = Text.pos_at_start_of_cursor_screen_line(State) State.screen_top1.pos = pos end end From 1d3c9f47085ee5d9cfa37d988d53f2dfead0195a Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 18 Aug 2022 10:22:48 -0700 Subject: [PATCH 5/8] 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 From edcd3d7a9a255bc7a02dad4a28c797fd7406010b Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 18 Aug 2022 12:07:50 -0700 Subject: [PATCH 6/8] dead code --- text.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/text.lua b/text.lua index 9c4a009..3c91361 100644 --- a/text.lua +++ b/text.lua @@ -525,8 +525,6 @@ 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_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) end From 0bf34a9ce043b04ba99fc5236f69fb724705cc52 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 18 Aug 2022 12:09:50 -0700 Subject: [PATCH 7/8] spurious args --- text.lua | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/text.lua b/text.lua index 3c91361..071b52d 100644 --- a/text.lua +++ b/text.lua @@ -281,43 +281,43 @@ function Text.keychord_pressed(State, chord) record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)}) --== shortcuts that move the cursor elseif chord == 'left' then - Text.left(State, State.left, State.right) + Text.left(State) State.selection1 = {} elseif chord == 'right' then - Text.right(State, State.left, State.right) + Text.right(State) State.selection1 = {} elseif chord == 'S-left' then if State.selection1.line == nil then State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos} end - Text.left(State, State.left, State.right) + Text.left(State) elseif chord == 'S-right' then if State.selection1.line == nil then State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos} end - Text.right(State, State.left, State.right) + Text.right(State) -- C- hotkeys reserved for drawings, so we'll use M- elseif chord == 'M-left' then - Text.word_left(State, State.left, State.right) + Text.word_left(State) State.selection1 = {} elseif chord == 'M-right' then - Text.word_right(State, State.left, State.right) + Text.word_right(State) State.selection1 = {} elseif chord == 'M-S-left' then if State.selection1.line == nil then State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos} end - Text.word_left(State, State.left, State.right) + Text.word_left(State) elseif chord == 'M-S-right' then if State.selection1.line == nil then State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos} end - Text.word_right(State, State.left, State.right) + Text.word_right(State) elseif chord == 'home' then Text.start_of_line(State) State.selection1 = {} elseif chord == 'end' then - Text.end_of_line(State, State.left, State.right) + Text.end_of_line(State) State.selection1 = {} elseif chord == 'S-home' then if State.selection1.line == nil then @@ -328,39 +328,39 @@ function Text.keychord_pressed(State, chord) if State.selection1.line == nil then State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos} end - Text.end_of_line(State, State.left, State.right) + Text.end_of_line(State) elseif chord == 'up' then - Text.up(State, State.left, State.right) + Text.up(State) State.selection1 = {} elseif chord == 'down' then - Text.down(State, State.left, State.right) + Text.down(State) State.selection1 = {} elseif chord == 'S-up' then if State.selection1.line == nil then State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos} end - Text.up(State, State.left, State.right) + Text.up(State) elseif chord == 'S-down' then if State.selection1.line == nil then State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos} end - Text.down(State, State.left, State.right) + Text.down(State) elseif chord == 'pageup' then - Text.pageup(State, State.left, State.right) + Text.pageup(State) State.selection1 = {} elseif chord == 'pagedown' then - Text.pagedown(State, State.left, State.right) + Text.pagedown(State) State.selection1 = {} elseif chord == 'S-pageup' then if State.selection1.line == nil then State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos} end - Text.pageup(State, State.left, State.right) + Text.pageup(State) elseif chord == 'S-pagedown' then if State.selection1.line == nil then State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos} end - Text.pagedown(State, State.left, State.right) + Text.pagedown(State) end end From 72866ec0adb9e744e40bc5e1eba027f5dd32d41b Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 18 Aug 2022 13:04:04 -0700 Subject: [PATCH 8/8] get rid of some ridiculous code I guess I wrote it before I settled into the idiom of: * first change cursor * then scroll if necessary --- text.lua | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/text.lua b/text.lua index 071b52d..451ab51 100644 --- a/text.lua +++ b/text.lua @@ -442,36 +442,26 @@ function Text.up(State) local screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos --? print(#screen_line_starting_pos) screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos] ---? print('previous screen line starts at pos '..tostring(screen_line_starting_pos)..' of its line') - if State.screen_top1.line > State.cursor1.line then - State.screen_top1.line = State.cursor1.line - State.screen_top1.pos = screen_line_starting_pos ---? print('pos of top of screen is also '..tostring(State.screen_top1.pos)..' of the same line') - end local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos) local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset) State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1 break end end - if State.cursor1.line < State.screen_top1.line then - State.screen_top1.line = State.cursor1.line - end else -- move up one screen line in current line ---? print('cursor is NOT at first screen line of its line') assert(screen_line_index > 1) 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)) - if State.screen_top1.line == State.cursor1.line and State.screen_top1.pos == screen_line_starting_pos then - State.screen_top1.pos = new_screen_line_starting_pos ---? print('also setting pos of top of screen to '..tostring(State.screen_top1.pos)) - end local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos) local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset) State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1 --? print('cursor pos is now '..tostring(State.cursor1.pos)) end + if Text.lt1(State.cursor1, State.screen_top1) then + local top2 = Text.to2(State, State.screen_top1) + top2 = Text.previous_screen_line(State, top2) + State.screen_top1 = Text.to1(State, top2) + end end function Text.down(State)