From 9892bc5d7c924ce284003a35c00914352713eb42 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 25 May 2022 15:25:10 -0700 Subject: extract couple of functions --- text.lua | 76 +++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/text.lua b/text.lua index a0cec1c..7ff7baf 100644 --- a/text.lua +++ b/text.lua @@ -769,41 +769,9 @@ function Text.keychord_pressed(chord) Text.insert_at_cursor('\t') save_to_disk(Lines, Filename) elseif chord == 'left' then - assert(Lines[Cursor1.line].mode == 'text') - if Cursor1.pos > 1 then - Cursor1.pos = Cursor1.pos-1 - else - local new_cursor_line = Cursor1.line - while new_cursor_line > 1 do - new_cursor_line = new_cursor_line-1 - if Lines[new_cursor_line].mode == 'text' then - Cursor1.line = new_cursor_line - Cursor1.pos = utf8.len(Lines[Cursor1.line].data) + 1 - break - end - end - if Cursor1.line < Screen_top1.line then - Screen_top1.line = Cursor1.line - end - end + Text.left() elseif chord == 'right' then - assert(Lines[Cursor1.line].mode == 'text') - if Cursor1.pos <= utf8.len(Lines[Cursor1.line].data) then - Cursor1.pos = Cursor1.pos+1 - else - local new_cursor_line = Cursor1.line - while new_cursor_line <= #Lines-1 do - new_cursor_line = new_cursor_line+1 - if Lines[new_cursor_line].mode == 'text' then - Cursor1.line = new_cursor_line - Cursor1.pos = 1 - break - end - end - if Cursor1.line > Screen_bottom1.line then - Screen_top1.line = Cursor1.line - end - end + Text.right() elseif chord == 'home' then Cursor1.pos = 1 elseif chord == 'end' then @@ -957,6 +925,46 @@ function Text.keychord_pressed(chord) end end +function Text.left() + assert(Lines[Cursor1.line].mode == 'text') + if Cursor1.pos > 1 then + Cursor1.pos = Cursor1.pos-1 + else + local new_cursor_line = Cursor1.line + while new_cursor_line > 1 do + new_cursor_line = new_cursor_line-1 + if Lines[new_cursor_line].mode == 'text' then + Cursor1.line = new_cursor_line + Cursor1.pos = utf8.len(Lines[Cursor1.line].data) + 1 + break + end + end + if Cursor1.line < Screen_top1.line then + Screen_top1.line = Cursor1.line + end + end +end + +function Text.right() + assert(Lines[Cursor1.line].mode == 'text') + if Cursor1.pos <= utf8.len(Lines[Cursor1.line].data) then + Cursor1.pos = Cursor1.pos+1 + else + local new_cursor_line = Cursor1.line + while new_cursor_line <= #Lines-1 do + new_cursor_line = new_cursor_line+1 + if Lines[new_cursor_line].mode == 'text' then + Cursor1.line = new_cursor_line + Cursor1.pos = 1 + break + end + end + if Cursor1.line > Screen_bottom1.line then + Screen_top1.line = Cursor1.line + end + end +end + function Text.pos_at_start_of_cursor_screen_line() if Lines[Cursor1.line].screen_line_starting_pos == nil then return 1,1 -- cgit 1.4.1-2-gfad0