Skip to content

Commit

Permalink
Implement backwards search
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiolo committed Dec 2, 2023
1 parent 7471837 commit dd622ef
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ struct Cell {
}
}

Cell *FindNextSearchMatch(wxString &search, Cell *best, Cell *selected, bool &lastwasselected) {
Cell *FindNextSearchMatch(wxString &search, Cell *best, Cell *selected, bool &lastwasselected, bool &reverse) {
if (sys->casesensitivesearch) {
if (text.t.Find(search) >= 0) {
if (lastwasselected) best = this;
Expand All @@ -423,7 +423,7 @@ struct Cell {
}
}
if (selected == this) lastwasselected = true;
if (grid) best = grid->FindNextSearchMatch(search, best, selected, lastwasselected);
if (grid) best = grid->FindNextSearchMatch(search, best, selected, lastwasselected, reverse);
return best;
}

Expand Down
14 changes: 9 additions & 5 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,11 @@ struct Document {
}

case A_SEARCHNEXT: {
return SearchNext(dc, false, true);
return SearchNext(dc, false, true, false);
}

case A_SEARCHPREV: {
return SearchNext(dc, false, true, true);
}

case A_CASESENSITIVESEARCH: {
Expand All @@ -1190,7 +1194,7 @@ struct Document {
sys->frame->filter->GetValue() :
sys->frame->filter->GetValue().Lower();
sys->frame->SetSearchTextBoxBackgroundColour(false);
this->SearchNext(dc, false, false);
this->SearchNext(dc, false, false, false);
this->Refresh();
return nullptr;
}
Expand Down Expand Up @@ -1230,7 +1234,7 @@ struct Document {
} else {
loopallcellssel(c, true) if (c->text.IsInSearch()) c->AddUndo(this);
selected.g->ReplaceStr(this, replaces, lreplaces, selected);
if (k == A_REPLACEONCEJ) return SearchNext(dc, false, true);
if (k == A_REPLACEONCEJ) return SearchNext(dc, false, true, false);
}
return _(L"Text has been replaced.");
}
Expand Down Expand Up @@ -2023,12 +2027,12 @@ struct Document {
}
}

const wxChar *SearchNext(wxDC &dc, bool focusmatch, bool jump) {
const wxChar *SearchNext(wxDC &dc, bool focusmatch, bool jump, bool reverse) {
if (!sys->searchstring.Len()) return _(L"No search string.");
bool lastsel = true;
if (!rootgrid) return nullptr; //fix crash when opening new doc
Cell *next =
rootgrid->FindNextSearchMatch(sys->searchstring, nullptr, selected.GetCell(), lastsel);
rootgrid->FindNextSearchMatch(sys->searchstring, nullptr, selected.GetCell(), lastsel, reverse);
sys->frame->SetSearchTextBoxBackgroundColour(next);
if (!next) return _(L"No matches for search.");
if (!jump) return nullptr;
Expand Down
8 changes: 6 additions & 2 deletions src/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,12 @@ struct Grid {
return best;
}

Cell *FindNextSearchMatch(wxString &search, Cell *best, Cell *selected, bool &lastwasselected) {
foreachcell(c) best = c->FindNextSearchMatch(search, best, selected, lastwasselected);
Cell *FindNextSearchMatch(wxString &search, Cell *best, Cell *selected, bool &lastwasselected, bool &reverse) {
if (reverse) {
foreachcellrev(c) best = c->FindNextSearchMatch(search, best, selected, lastwasselected, reverse);
} else {
foreachcell(c) best = c->FindNextSearchMatch(search, best, selected, lastwasselected, reverse);
}
return best;
}

Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ enum {
A_LINKIMG,
A_LINKIMGREV,
A_SEARCHNEXT,
A_SEARCHPREV,
A_CUSTCOL,
A_COLCELL,
A_SORT,
Expand Down
5 changes: 3 additions & 2 deletions src/myframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ struct MyFrame : wxFrame {
semenu->AppendCheckItem(A_CASESENSITIVESEARCH, _(L"Case-sensitive search"));
semenu->Check(A_CASESENSITIVESEARCH, sys->casesensitivesearch);
MyAppend(semenu, A_SEARCHNEXT, _(L"&Go To Next Search Result\tF3"));
MyAppend(semenu, A_SEARCHPREV, _(L"Go To &Previous Search Result\tSHIFT+F3"));
MyAppend(semenu, A_REPLACEF, _(L"&Replace\tCTRL+h"));
MyAppend(semenu, A_REPLACEONCE, _(L"Replace in Current &Selection\tCTRL+k"));
MyAppend(semenu, A_REPLACEONCEJ, _(L"Replace in Current Selection && &Jump Next\tCTRL+j"));
Expand Down Expand Up @@ -478,7 +479,7 @@ struct MyFrame : wxFrame {
// xgettext:no-c-format
MyAppend(filtermenu, A_FILTERL, _(L"Show 1% less than the last filter"));
MyAppend(filtermenu, A_FILTERBYCELLBG, _(L"Filter by the same cell color"));
MyAppend(filtermenu, A_FILTERMATCHNEXT, _(L"Go to next filter match\tSHIFT+F3"));
MyAppend(filtermenu, A_FILTERMATCHNEXT, _(L"Go to next filter match\tCTRL+F3"));

wxMenu *viewmenu = new wxMenu();
MyAppend(viewmenu, A_ZOOMIN, _(L"Zoom &In (CTRL+mousewheel)\tCTRL+PGUP"));
Expand Down Expand Up @@ -1066,7 +1067,7 @@ struct MyFrame : wxFrame {
Document *doc = GetCurTab()->doc;
TSCanvas *sw = GetCurTab();
wxClientDC dc(sw);
doc->SearchNext(dc, false, false);
doc->SearchNext(dc, false, false, false);
if (doc->searchfilter) {
doc->SetSearchFilter(sys->searchstring.Len() != 0);
} else
Expand Down
2 changes: 1 addition & 1 deletion src/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ struct System {
newdoc->sw->SetFocus();
newdoc->UpdateFileName();
wxClientDC dc(newdoc->sw);
newdoc->SearchNext(dc, false, false);
newdoc->SearchNext(dc, false, false, false);
}

void Init(const wxString &filename) {
Expand Down

0 comments on commit dd622ef

Please sign in to comment.