if Cursor_pane.col < 1 then return end
clear_all_search_terms()
local pane = Surface[Cursor_pane.col][Cursor_pane.row]
if pane == nil then
return
end
print('search previous', Cursor_pane.col, Cursor_pane.row, pane.cursor1.line, pane.cursor1.pos)
local old_cursor_in_cursor_pane = {line=pane.cursor1.line, pos=pane.cursor1.pos}
-- scan current pane up from cursor
if search_previous_in_pane(Surface[Cursor_pane.col][Cursor_pane.row]) then
print('found in same pane', pane.cursor1.line, pane.cursor1.pos)
return
end
pane.cursor1 = old_cursor_in_cursor_pane
-- scan current column down from current pane
for current_pane_index=Cursor_pane.row-1,1,-1 do
local pane = Surface[Cursor_pane.col][current_pane_index]
pane.cursor1 = {line=#pane.lines, pos=#pane.lines[#pane.lines].data+1}
if search_previous_in_pane(pane) then
Cursor_pane.row = current_pane_index
print('found in same column', Cursor_pane.col, Cursor_pane.row, pane.cursor1.line, pane.cursor1.pos)
return
end
end
local current_column_index = 1 + (Cursor_pane.col-2)%#Surface -- (i-1)%#Surface in the presence of 1-indexing
-- scan columns past current, looping around
while true do
print('column', current_column_index)
for current_pane_index = #Surface[current_column_index],1,-1 do
local pane = Surface[current_column_index][current_pane_index]
pane.cursor1 = {line=#pane.lines, pos=#pane.lines[#pane.lines].data+1}
if search_previous_in_pane(pane) then
Cursor_pane = {col=current_column_index, row=current_pane_index}
print('found', Cursor_pane.col, Cursor_pane.row, pane.cursor1.line, pane.cursor1.pos)
return
end
end
-- loop update
current_column_index = 1 + (current_column_index-2)%#Surface -- i = (i-1)%#Surface in the presence of 1-indexing
-- termination check
if current_column_index == Cursor_pane.col then
break
end
end
-- scan current column from bottom current pane
for current_pane_index=#Surface[Cursor_pane.col],Cursor_pane.row+1,-1 do
print('same column', current_pane_index)
if search_previous_in_pane(Surface[Cursor_pane.col][current_pane_index]) then
Cursor_pane.row = current_pane_index
print('found in same column', Cursor_pane.col, Cursor_pane.row, pane.cursor1.line, pane.cursor1.pos)
return
end
end
-- finally, scan the cursor pane from bottom until cursor
local pane = Surface[Cursor_pane.col][Cursor_pane.row]
local old_cursor = pane.cursor1
pane.cursor1 = {line=#pane.lines, pos=#pane.lines[#pane.lines].data+1}
if search_previous_in_pane(pane) then
if Text.lt1(old_cursor, pane.cursor1) then
return
end
end
-- nothing found
pane.cursor1 = old_cursor_in_cursor_pane
end
-- returns whether it found an occurrence
function search_previous_in_pane(pane)
pane.search_term = Display_settings.search_term
pane.search_text = Display_settings.search_text
pane.search_backup = {cursor={line=pane.cursor1.line, pos=pane.cursor1.pos}, screen_top={line=pane.screen_top1.line, pos=pane.screen_top1.pos}}
for i=1,#pane.lines do
if pane.line_cache[i] == nil then
pane.line_cache[i] = {}
end
end
if Text.search_previous(pane) then
if Text.lt1(pane.cursor1, pane.search_backup.cursor) then
-- select this occurrence
return true
end
-- Otherwise cursor wrapped around. Skip this pane.
end
-- Clean up this pane before moving on to the previous one.
pane.search_term = nil
pane.search_text = nil
pane.cursor1.line = pane.search_backup.cursor.line
pane.cursor1.pos = pane.search_backup.cursor.pos
pane.screen_top1.line = pane.search_backup.screen_top.line
pane.screen_top1.pos = pane.search_backup.screen_top.pos
pane.search_backup = nil