From 009c7abb609931449457ea1250f7acccb66a217a Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 23 Jun 2022 11:09:49 -0700 Subject: extract a function --- select.lua | 3 +-- text.lua | 29 +++++++++++++---------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/select.lua b/select.lua index 5659015..219fe3e 100644 --- a/select.lua +++ b/select.lua @@ -135,8 +135,7 @@ function Text.delete_selection_without_undo() Cursor1.pos = minp Selection1 = {} -- delete everything between min (inclusive) and max (exclusive) - Lines[minl].fragments = nil - Lines[minl].screen_line_starting_pos = nil + Text.clear_cache(Lines[minl]) local min_offset = Text.offset(Lines[minl].data, minp) local max_offset = Text.offset(Lines[maxl].data, maxp) if minl == maxl then diff --git a/text.lua b/text.lua index 0ed46f8..23e6e05 100644 --- a/text.lua +++ b/text.lua @@ -155,8 +155,7 @@ end function Text.insert_at_cursor(t) local byte_offset = Text.offset(Lines[Cursor1.line].data, Cursor1.pos) Lines[Cursor1.line].data = string.sub(Lines[Cursor1.line].data, 1, byte_offset-1)..t..string.sub(Lines[Cursor1.line].data, byte_offset) - Lines[Cursor1.line].fragments = nil - Lines[Cursor1.line].screen_line_starting_pos = nil + Text.clear_cache(Lines[Cursor1.line]) Cursor1.pos = Cursor1.pos+1 end @@ -202,8 +201,7 @@ function Text.keychord_pressed(chord) else Lines[Cursor1.line].data = string.sub(Lines[Cursor1.line].data, 1, byte_start-1) end - Lines[Cursor1.line].fragments = nil - Lines[Cursor1.line].screen_line_starting_pos = nil + Text.clear_cache(Lines[Cursor1.line]) Cursor1.pos = Cursor1.pos-1 end elseif Cursor1.line > 1 then @@ -214,8 +212,7 @@ function Text.keychord_pressed(chord) -- join lines Cursor1.pos = utf8.len(Lines[Cursor1.line-1].data)+1 Lines[Cursor1.line-1].data = Lines[Cursor1.line-1].data..Lines[Cursor1.line].data - Lines[Cursor1.line-1].fragments = nil - Lines[Cursor1.line-1].screen_line_starting_pos = nil + Text.clear_cache(Lines[Cursor1.line-1]) table.remove(Lines, Cursor1.line) end Cursor1.line = Cursor1.line-1 @@ -250,8 +247,7 @@ function Text.keychord_pressed(chord) else Lines[Cursor1.line].data = string.sub(Lines[Cursor1.line].data, 1, byte_start-1) end - Lines[Cursor1.line].fragments = nil - Lines[Cursor1.line].screen_line_starting_pos = nil + Text.clear_cache(Lines[Cursor1.line]) -- no change to Cursor1.pos end elseif Cursor1.line < #Lines then @@ -260,8 +256,7 @@ function Text.keychord_pressed(chord) else -- join lines Lines[Cursor1.line].data = Lines[Cursor1.line].data..Lines[Cursor1.line+1].data - Lines[Cursor1.line].fragments = nil - Lines[Cursor1.line].screen_line_starting_pos = nil + Text.clear_cache(Lines[Cursor1.line]) table.remove(Lines, Cursor1.line+1) end end @@ -354,10 +349,8 @@ function Text.insert_return() local byte_offset = Text.offset(Lines[Cursor1.line].data, Cursor1.pos) table.insert(Lines, Cursor1.line+1, {mode='text', data=string.sub(Lines[Cursor1.line].data, byte_offset)}) Lines[Cursor1.line].data = string.sub(Lines[Cursor1.line].data, 1, byte_offset-1) - Lines[Cursor1.line].fragments = nil - Lines[Cursor1.line].screen_line_starting_pos = nil - Lines[Cursor1.line+1].fragments = nil - Lines[Cursor1.line+1].screen_line_starting_pos = nil + Text.clear_cache(Lines[Cursor1.line]) + Text.clear_cache(Lines[Cursor1.line+1]) Cursor1.line = Cursor1.line+1 Cursor1.pos = 1 end @@ -883,9 +876,13 @@ function Text.redraw_all() --? print('clearing fragments') for _,line in ipairs(Lines) do line.y = nil - line.fragments = nil - line.screen_line_starting_pos = nil + Text.clear_cache(line) end end +function Text.clear_cache(line) + line.fragments = nil + line.screen_line_starting_pos = nil +end + return Text -- cgit 1.4.1-2-gfad0