diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2022-06-03 07:48:41 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2022-06-03 07:48:41 -0700 |
commit | 7301f3964c518858ba6e3ffc2404baee1734639b (patch) | |
tree | 41d9799f7bf69ee9c5b8e492e5858c0f1270e868 | |
parent | 03499f7449e607e20bee81ba42213e39e308dfff (diff) | |
download | text.love-7301f3964c518858ba6e3ffc2404baee1734639b.tar.gz |
up arrow to search previous
-rw-r--r-- | main.lua | 2 | ||||
-rw-r--r-- | text.lua | 50 |
2 files changed, 52 insertions, 0 deletions
diff --git a/main.lua b/main.lua index a00790d..584dde4 100644 --- a/main.lua +++ b/main.lua @@ -255,6 +255,8 @@ function App.keychord_pressed(chord) elseif chord == 'down' then Cursor1.pos = Cursor1.pos+1 Text.search_next() + elseif chord == 'up' then + Text.search_previous() end return elseif chord == 'C-f' then diff --git a/text.lua b/text.lua index 5c468b2..3c3bb9d 100644 --- a/text.lua +++ b/text.lua @@ -163,6 +163,56 @@ function Text.search_next() end end +function Text.search_previous() + -- search current line + local pos = rfind(Lines[Cursor1.line].data, Search_term, Cursor1.pos) + if pos then + Cursor1.pos = pos + end + if pos == nil then + for i=Cursor1.line-1,1,-1 do + pos = rfind(Lines[i].data, Search_term) + if pos then + Cursor1.line = i + Cursor1.pos = pos + break + end + end + end + if pos == nil then + -- wrap around + for i=#Lines,Cursor1.line+1,-1 do + pos = rfind(Lines[i].data, Search_term) + if pos then + Cursor1.line = i + Cursor1.pos = pos + break + end + end + end + if pos == nil then + Cursor1.line = Search_backup_cursor1.line + Cursor1.pos = Search_backup_cursor1.pos + end + if Text.lt1(Cursor1, Screen_top1) or Text.lt1(Screen_bottom1, Cursor1) then + Screen_top1.line = Cursor1.line + local _, pos = Text.pos_at_start_of_cursor_screen_line() + Screen_top1.pos = pos + end +end + +function rfind(s, pat, i) + local rs = s:reverse() + local rpat = pat:reverse() + if i == nil then i = #s end + local ri = #s - i + 1 + local rendpos = rs:find(rpat, ri) + if rendpos == nil then return nil end + local endpos = #s - rendpos + 1 + assert (endpos >= #pat) + return endpos-#pat+1 +end + -- Return any intersection of the region from Selection1 to Cursor1 with the -- region between {line=line_index, pos=apos} and {line=line_index, pos=bpos}. -- apos must be less than bpos. However Selection1 and Cursor1 can be in any order. |