From e2696fcc72d134e70c0636ff2c7a0c79631c3166 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 3 Jun 2022 14:37:32 -0700 Subject: [PATCH] more precise search highlighting --- select.lua | 3 +++ text.lua | 8 ++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/select.lua b/select.lua index 499c309..3d8999e 100644 --- a/select.lua +++ b/select.lua @@ -51,6 +51,8 @@ function Text.clip_selection(line_index, apos, bpos) end -- draw highlight for line corresponding to (lo,hi) given an approximate x,y and pos on the same screen line +-- Creates text objects every time, so use this sparingly. +-- Returns some intermediate computation useful elsewhere. function Text.draw_highlight(line, x,y, pos, lo,hi) if lo then local lo_offset = utf8.offset(line.data, lo) @@ -71,6 +73,7 @@ function Text.draw_highlight(line, x,y, pos, lo,hi) love.graphics.setColor(0.7,0.7,0.9) love.graphics.rectangle('fill', x+lo_px,y, text_width,Line_height) love.graphics.setColor(0,0,0) + return lo_px end end diff --git a/text.lua b/text.lua index 1398896..30f000f 100644 --- a/text.lua +++ b/text.lua @@ -62,13 +62,9 @@ function Text.draw(line, line_width, line_index) if pos <= Cursor1.pos and pos + frag_len > Cursor1.pos then if Search_term then if Lines[Cursor1.line].data:sub(Cursor1.pos, Cursor1.pos+utf8.len(Search_term)-1) == Search_term then - if Search_text == nil then - Search_text = App.newText(love.graphics.getFont(), Search_term) - end - love.graphics.setColor(0.7,1,1) - love.graphics.rectangle('fill', x,y, App.width(Search_text),Line_height) + local lo_px = Text.draw_highlight(line, x,y, pos, Cursor1.pos, Cursor1.pos+utf8.len(Search_term)) love.graphics.setColor(0,0,0) - love.graphics.print(Search_term, x,y) + love.graphics.print(Search_term, x+lo_px,y) end else Text.draw_cursor(x+Text.x(frag, Cursor1.pos-pos+1), y)