Skip to content

Commit 75f7e80

Browse files
committed
Implemented find on same line for backwards searches
1 parent a023355 commit 75f7e80

File tree

5 files changed

+100
-10
lines changed

5 files changed

+100
-10
lines changed

Finding.cpp

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ bool NotFound (CFindInfo & FindInfo)
5454
FindInfo.m_bAgain ? " again." : " .");
5555
::UMessageBox (strMsg, MB_ICONINFORMATION);
5656
FindInfo.m_iStartColumn = -1;
57+
FindInfo.m_iLastLineSearched = -1;
58+
FindInfo.m_MatchesOnLine.clear ();
5759
return false;
5860
} // end of NotFound
5961

@@ -72,6 +74,8 @@ CFindDlg dlg (FindInfo.m_strFindStringList);
7274
if (!FindInfo.m_bAgain || FindInfo.m_strFindStringList.IsEmpty ())
7375
{
7476
FindInfo.m_iStartColumn = -1; // return consistent column number
77+
FindInfo.m_iLastLineSearched = -1;
78+
FindInfo.m_MatchesOnLine.clear ();
7579

7680
if (!FindInfo.m_strFindStringList.IsEmpty ())
7781
dlg.m_strFindText = FindInfo.m_strFindStringList.GetHead ();
@@ -118,7 +122,7 @@ CFindDlg dlg (FindInfo.m_strFindStringList);
118122
else
119123
{
120124
if (FindInfo.m_bRepeatOnSameLine)
121-
FindInfo.m_iStartColumn++; // skip previous match
125+
FindInfo.m_iStartColumn = FindInfo.m_iEndColumn; // skip previous match
122126
else
123127
FindInfo.m_iStartColumn = -1; // return consistent column number
124128

@@ -150,10 +154,6 @@ CFindDlg dlg (FindInfo.m_strFindStringList);
150154
return NotFound (FindInfo);
151155
}
152156

153-
// re-initiate the search - this will set up the POSITION parameter, if it wants to
154-
155-
// (*pInitiateSearch) (pObject, FindInfo);
156-
157157
// loop until end of text, or text found
158158

159159
CString strLine;
@@ -217,6 +217,87 @@ CString strStatus = TFormat ("Finding: %s", (LPCTSTR) FindInfo.m_strFindStringLi
217217
}
218218
} // end of having a progress control
219219

220+
// if searching backwards, and doing more than one, find all matches on this line
221+
222+
if (!FindInfo.m_bForwards && FindInfo.m_bRepeatOnSameLine)
223+
{
224+
225+
if (FindInfo.m_nCurrentLine != FindInfo.m_iLastLineSearched)
226+
{
227+
FindInfo.m_iLastLineSearched = FindInfo.m_nCurrentLine;
228+
FindInfo.m_MatchesOnLine.clear (); // no matches yet
229+
int iStartCol = 0; // start at start of line
230+
231+
// if case-insensitive search wanted, force this line to lower case
232+
if (!FindInfo.m_bMatchCase && !FindInfo.m_bRegexp )
233+
strLine.MakeLower ();
234+
235+
// loop until we run out of matches
236+
while (true)
237+
{
238+
239+
if (FindInfo.m_bRegexp )
240+
{
241+
if (regexec (FindInfo.m_regexp, strLine, iStartCol))
242+
{
243+
FindInfo.m_MatchesOnLine.push_back (
244+
pair <int, int> (FindInfo.m_regexp->m_vOffsets [0],
245+
FindInfo.m_regexp->m_vOffsets [1]));
246+
iStartCol = FindInfo.m_regexp->m_vOffsets [1];
247+
if (iStartCol >= strLine.GetLength ())
248+
break;
249+
} // end of regexp matched
250+
else
251+
break; // no match, done searching
252+
253+
} // end regexp
254+
else
255+
{
256+
if ((iStartCol = strLine.Find (strFindString, iStartCol)) != -1)
257+
{
258+
// work out ending column
259+
int iEndCol = iStartCol + strFindString.GetLength ();
260+
FindInfo.m_MatchesOnLine.push_back (pair <int, int> (iStartCol, iEndCol));
261+
iStartCol = iEndCol;
262+
if (iStartCol >= strLine.GetLength ())
263+
break;
264+
} // end of found
265+
else
266+
break; // no match, done searching
267+
268+
} // end not regexp
269+
270+
271+
} // end of while we found something
272+
273+
274+
} // end of different line to last time
275+
276+
277+
// if m_MatchesOnLine is not empty we had a match (either this time or last time)
278+
279+
if (!FindInfo.m_MatchesOnLine.empty ())
280+
{
281+
// get last match
282+
pair<int, int> result = FindInfo.m_MatchesOnLine.back ();
283+
// don't want it any more
284+
FindInfo.m_MatchesOnLine.pop_back ();
285+
// copy first and last column
286+
FindInfo.m_iStartColumn = result.first;
287+
FindInfo.m_iEndColumn = result.second;
288+
// all done!
289+
WrapUpFind (FindInfo);
290+
return true; // found it!
291+
} // end if found
292+
else
293+
{
294+
// nothing found, try previous line
295+
FindInfo.m_nCurrentLine--;
296+
continue; // skip other testing
297+
} // end if not found
298+
299+
} // end of searching backwards with repeat on same line
300+
220301
// if text found on this line, then we have done it!
221302

222303
if (FindInfo.m_bRegexp )
@@ -233,14 +314,14 @@ CString strStatus = TFormat ("Finding: %s", (LPCTSTR) FindInfo.m_strFindStringLi
233314
} // end of regular expression
234315
else
235316
{ // not regular expression
317+
236318
// if case-insensitive search wanted, force this line to lower case
237319
if (!FindInfo.m_bMatchCase)
238320
strLine.MakeLower ();
239321
if ((FindInfo.m_iStartColumn = strLine.Find (strFindString, maximum (FindInfo.m_iStartColumn, 0))) != -1)
240322
{
241323
// work out ending column
242-
FindInfo.m_iEndColumn = FindInfo.m_iStartColumn +
243-
FindInfo.m_strFindStringList.GetHead ().GetLength ();
324+
FindInfo.m_iEndColumn = FindInfo.m_iStartColumn + strFindString.GetLength ();
244325
WrapUpFind (FindInfo);
245326
return true; // found it!
246327
} // end of found

dialogs/world_prefs/genpropertypage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ void CGenPropertyPage::LoadList (void)
10581058

10591059
m_cTreeCtrl.DeleteAllItems ();
10601060
// since all is deleted, we don't have any groups any more
1061-
m_GroupsMap.erase (m_GroupsMap.begin (), m_GroupsMap.end ());
1061+
m_GroupsMap.clear ();
10621062

10631063

10641064
CString strObjectName;

doc.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4373,9 +4373,15 @@ long CMUSHclientDoc::GetLastLine (void)
43734373
// our "last found" line needs adjusting
43744374

43754375
m_DisplayFindInfo.m_nCurrentLine -= JUMP_SIZE;
4376+
m_DisplayFindInfo.m_iLastLineSearched -= JUMP_SIZE;
43764377

4378+
// has last found line has disappeared off the front of the buffer?
43774379
if (m_DisplayFindInfo.m_nCurrentLine < 0)
4380+
{
43784381
m_DisplayFindInfo.m_nCurrentLine = 0;
4382+
m_DisplayFindInfo.m_iLastLineSearched = -1;
4383+
}
4384+
43794385

43804386
} // end of CMUSHclientDoc::RemoveChunk
43814387

sendvw.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,8 +1362,8 @@ ASSERT_VALID(pDoc);
13621362

13631363
pDoc->m_DisplayFindInfo.m_bAgain = bAgain;
13641364

1365-
// if finding forwards, we can find multiple instances on the same line
1366-
pDoc->m_DisplayFindInfo.m_bRepeatOnSameLine = pDoc->m_DisplayFindInfo.m_bForwards;
1365+
// we can find multiple instances on the same line
1366+
pDoc->m_DisplayFindInfo.m_bRepeatOnSameLine = true;
13671367

13681368
bool found = FindRoutine (pDoc, // passed back to callback routines
13691369
pDoc->m_DisplayFindInfo, // finding structure

stdafx.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ class CFindInfo : public CObject
260260
m_iControlColumns = 0;
261261
m_regexp = NULL;
262262
m_bRepeatOnSameLine = false;
263+
m_iLastLineSearched = -1;
263264
}; // constructor
264265

265266
~CFindInfo () { delete m_regexp; };
@@ -281,6 +282,8 @@ class CFindInfo : public CObject
281282
t_regexp * m_regexp; // compiled regular expression
282283
CStringList m_strFindStringList; // previous things we found
283284
bool m_bRepeatOnSameLine; // keep trying to match on same line
285+
list<pair<int, int> > m_MatchesOnLine; // list of matches on this line (for reverse searching)
286+
int m_iLastLineSearched; // last line searched to populate m_MatchesOnLine
284287
};
285288

286289
// prototype for "get next line" callback for find routine

0 commit comments

Comments
 (0)