2022-06-03 22:14:53 +01:00
|
|
|
-- helpers for the search bar (C-f)
|
|
|
|
|
2022-07-13 00:08:24 +01:00
|
|
|
function Text.draw_search_bar(State)
|
|
|
|
local h = State.line_height+2
|
2022-06-03 22:14:53 +01:00
|
|
|
local y = App.screen.height-h
|
|
|
|
love.graphics.setColor(0.9,0.9,0.9)
|
|
|
|
love.graphics.rectangle('fill', 0, y-10, App.screen.width-1, h+8)
|
|
|
|
love.graphics.setColor(0.6,0.6,0.6)
|
|
|
|
love.graphics.line(0, y-10, App.screen.width-1, y-10)
|
|
|
|
love.graphics.setColor(1,1,1)
|
|
|
|
love.graphics.rectangle('fill', 20, y-6, App.screen.width-40, h+2, 2,2)
|
|
|
|
love.graphics.setColor(0.6,0.6,0.6)
|
|
|
|
love.graphics.rectangle('line', 20, y-6, App.screen.width-40, h+2, 2,2)
|
2022-07-12 07:03:27 +01:00
|
|
|
App.color(Text_color)
|
2022-07-13 00:08:24 +01:00
|
|
|
App.screen.print(State.search_term, 25,y-5)
|
|
|
|
if State.search_text == nil then
|
|
|
|
State.search_text = App.newText(love.graphics.getFont(), State.search_term)
|
2022-06-03 22:14:53 +01:00
|
|
|
end
|
2022-07-26 02:17:04 +01:00
|
|
|
Text.draw_cursor(State, 25+App.width(State.search_text),y-5)
|
2022-06-03 22:14:53 +01:00
|
|
|
end
|
|
|
|
|
2022-07-13 01:02:53 +01:00
|
|
|
function Text.search_next(State)
|
2022-08-12 04:09:59 +01:00
|
|
|
-- search current line from cursor
|
2022-08-12 06:17:21 +01:00
|
|
|
local pos = find(State.lines[State.cursor1.line].data, State.search_term, State.cursor1.pos)
|
|
|
|
if pos then
|
|
|
|
State.cursor1.pos = pos
|
2022-06-03 22:14:53 +01:00
|
|
|
end
|
|
|
|
if pos == nil then
|
2022-08-12 04:09:59 +01:00
|
|
|
-- search lines below cursor
|
2022-07-13 01:02:53 +01:00
|
|
|
for i=State.cursor1.line+1,#State.lines do
|
2022-08-12 06:17:21 +01:00
|
|
|
pos = find(State.lines[i].data, State.search_term)
|
|
|
|
if pos then
|
2022-09-03 22:13:22 +01:00
|
|
|
State.cursor1 = {line=i, pos=pos}
|
2022-08-12 06:17:21 +01:00
|
|
|
break
|
2022-06-03 22:14:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if pos == nil then
|
|
|
|
-- wrap around
|
2022-07-13 01:02:53 +01:00
|
|
|
for i=1,State.cursor1.line-1 do
|
2022-08-12 06:17:21 +01:00
|
|
|
pos = find(State.lines[i].data, State.search_term)
|
2022-06-03 22:14:53 +01:00
|
|
|
if pos then
|
2022-09-03 22:13:22 +01:00
|
|
|
State.cursor1 = {line=i, pos=pos}
|
2022-06-03 22:14:53 +01:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-08-12 04:09:59 +01:00
|
|
|
if pos == nil then
|
|
|
|
-- search current line until cursor
|
2022-08-12 06:17:21 +01:00
|
|
|
pos = find(State.lines[State.cursor1.line].data, State.search_term)
|
|
|
|
if pos and pos < State.cursor1.pos then
|
|
|
|
State.cursor1.pos = pos
|
2022-08-12 04:09:59 +01:00
|
|
|
end
|
|
|
|
end
|
2022-06-03 22:14:53 +01:00
|
|
|
if pos == nil then
|
2022-07-13 01:02:53 +01:00
|
|
|
State.cursor1.line = State.search_backup.cursor.line
|
|
|
|
State.cursor1.pos = State.search_backup.cursor.pos
|
|
|
|
State.screen_top1.line = State.search_backup.screen_top.line
|
|
|
|
State.screen_top1.pos = State.search_backup.screen_top.pos
|
2022-06-03 22:14:53 +01:00
|
|
|
end
|
2022-07-13 01:02:53 +01:00
|
|
|
if Text.lt1(State.cursor1, State.screen_top1) or Text.lt1(State.screen_bottom1, State.cursor1) then
|
|
|
|
State.screen_top1.line = State.cursor1.line
|
2022-08-18 18:22:48 +01:00
|
|
|
local pos = Text.pos_at_start_of_screen_line(State, State.cursor1)
|
2022-07-13 01:02:53 +01:00
|
|
|
State.screen_top1.pos = pos
|
2022-06-03 22:14:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-13 01:02:53 +01:00
|
|
|
function Text.search_previous(State)
|
2022-08-12 06:23:04 +01:00
|
|
|
-- search current line before cursor
|
2022-08-12 04:14:21 +01:00
|
|
|
local pos = rfind(State.lines[State.cursor1.line].data, State.search_term, State.cursor1.pos-1)
|
2022-06-03 22:14:53 +01:00
|
|
|
if pos then
|
2022-07-13 01:02:53 +01:00
|
|
|
State.cursor1.pos = pos
|
2022-06-03 22:14:53 +01:00
|
|
|
end
|
|
|
|
if pos == nil then
|
2022-08-12 06:23:04 +01:00
|
|
|
-- search lines above cursor
|
2022-07-13 01:02:53 +01:00
|
|
|
for i=State.cursor1.line-1,1,-1 do
|
|
|
|
pos = rfind(State.lines[i].data, State.search_term)
|
2022-06-03 22:14:53 +01:00
|
|
|
if pos then
|
2022-09-03 22:13:22 +01:00
|
|
|
State.cursor1 = {line=i, pos=pos}
|
2022-06-03 22:14:53 +01:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if pos == nil then
|
|
|
|
-- wrap around
|
2022-07-13 01:02:53 +01:00
|
|
|
for i=#State.lines,State.cursor1.line+1,-1 do
|
|
|
|
pos = rfind(State.lines[i].data, State.search_term)
|
2022-06-03 22:14:53 +01:00
|
|
|
if pos then
|
2022-09-03 22:13:22 +01:00
|
|
|
State.cursor1 = {line=i, pos=pos}
|
2022-06-03 22:14:53 +01:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-08-12 06:23:04 +01:00
|
|
|
if pos == nil then
|
|
|
|
-- search current line after cursor
|
|
|
|
pos = rfind(State.lines[State.cursor1.line].data, State.search_term)
|
|
|
|
if pos and pos > State.cursor1.pos then
|
|
|
|
State.cursor1.pos = pos
|
|
|
|
end
|
|
|
|
end
|
2022-06-03 22:14:53 +01:00
|
|
|
if pos == nil then
|
2022-07-13 01:02:53 +01:00
|
|
|
State.cursor1.line = State.search_backup.cursor.line
|
|
|
|
State.cursor1.pos = State.search_backup.cursor.pos
|
|
|
|
State.screen_top1.line = State.search_backup.screen_top.line
|
|
|
|
State.screen_top1.pos = State.search_backup.screen_top.pos
|
2022-06-03 22:14:53 +01:00
|
|
|
end
|
2022-07-13 01:02:53 +01:00
|
|
|
if Text.lt1(State.cursor1, State.screen_top1) or Text.lt1(State.screen_bottom1, State.cursor1) then
|
|
|
|
State.screen_top1.line = State.cursor1.line
|
2022-08-18 18:22:48 +01:00
|
|
|
local pos = Text.pos_at_start_of_screen_line(State, State.cursor1)
|
2022-07-13 01:02:53 +01:00
|
|
|
State.screen_top1.pos = pos
|
2022-06-03 22:14:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-03 22:13:22 +01:00
|
|
|
function find(s, pat, i, plain)
|
2022-08-12 06:17:21 +01:00
|
|
|
if s == nil then return end
|
2022-09-03 22:13:22 +01:00
|
|
|
return s:find(pat, i, plain)
|
2022-08-12 06:17:21 +01:00
|
|
|
end
|
|
|
|
|
2022-09-03 22:13:22 +01:00
|
|
|
function rfind(s, pat, i, plain)
|
2022-08-12 06:17:21 +01:00
|
|
|
if s == nil then return end
|
2022-06-03 22:14:53 +01:00
|
|
|
local rs = s:reverse()
|
|
|
|
local rpat = pat:reverse()
|
|
|
|
if i == nil then i = #s end
|
|
|
|
local ri = #s - i + 1
|
2022-09-03 22:13:22 +01:00
|
|
|
local rendpos = rs:find(rpat, ri, plain)
|
2022-06-03 22:14:53 +01:00
|
|
|
if rendpos == nil then return nil end
|
|
|
|
local endpos = #s - rendpos + 1
|
|
|
|
assert (endpos >= #pat)
|
|
|
|
return endpos-#pat+1
|
|
|
|
end
|