Skip to content

Commit

Permalink
Implement "Find All".
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Sep 26, 2020
1 parent 921f24a commit 7a66ddb
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 36 deletions.
3 changes: 2 additions & 1 deletion locale/de/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,8 @@ BEGIN
AUTOCHECKBOX "W&ildcard search",IDC_WILDCARDSEARCH,132,61,74,10,WS_TABSTOP
DEFPUSHBUTTON "&Find Next",IDOK,223,7,60,14
PUSHBUTTON "Find &Previous",IDC_FINDPREV,223,24,60,14
PUSHBUTTON "Close",IDCANCEL,223,41,60,14
PUSHBUTTON "Find &All",IDC_REPLACEALL,223,41,60,14
PUSHBUTTON "Close",IDCANCEL,223,58,60,14
CONTROL "<a>(?)</a>",IDC_REGEXPHELP,"SysLink",0x0,118,73,14,10
CONTROL "<a>(?)</a>",IDC_BACKSLASHHELP,"SysLink",0x0,118,85,14,10
CONTROL "<a>(?)</a>",IDC_WILDCARDHELP,"SysLink",0x0,208,61,14,10
Expand Down
3 changes: 2 additions & 1 deletion locale/it/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,8 @@ BEGIN
AUTOCHECKBOX "W&ildcard search",IDC_WILDCARDSEARCH,132,61,74,10,WS_TABSTOP
DEFPUSHBUTTON "&Find Next",IDOK,223,7,60,14
PUSHBUTTON "Find &Previous",IDC_FINDPREV,223,24,60,14
PUSHBUTTON "Close",IDCANCEL,223,41,60,14
PUSHBUTTON "Find &All",IDC_REPLACEALL,223,41,60,14
PUSHBUTTON "Close",IDCANCEL,223,58,60,14
CONTROL "<a>(?)</a>",IDC_REGEXPHELP,"SysLink",0x0,118,73,14,10
CONTROL "<a>(?)</a>",IDC_BACKSLASHHELP,"SysLink",0x0,118,85,14,10
CONTROL "<a>(?)</a>",IDC_WILDCARDHELP,"SysLink",0x0,208,61,14,10
Expand Down
3 changes: 2 additions & 1 deletion locale/ja/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,8 @@ BEGIN
AUTOCHECKBOX "ワイルドカード(&I)",IDC_WILDCARDSEARCH,132,61,74,10,WS_TABSTOP
DEFPUSHBUTTON "次を検索(&F)",IDOK,223,7,60,14
PUSHBUTTON "前へ検索(&P)",IDC_FINDPREV,223,24,60,14
PUSHBUTTON "閉じる",IDCANCEL,223,41,60,14
PUSHBUTTON "Find &All",IDC_REPLACEALL,223,41,60,14
PUSHBUTTON "閉じる",IDCANCEL,223,58,60,14
CONTROL "<a>(?)</a>",IDC_REGEXPHELP,"SysLink",0x0,118,73,14,10
CONTROL "<a>(?)</a>",IDC_BACKSLASHHELP,"SysLink",0x0,118,85,14,10
CONTROL "<a>(?)</a>",IDC_WILDCARDHELP,"SysLink",0x0,208,61,14,10
Expand Down
3 changes: 2 additions & 1 deletion locale/zh-Hans/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,8 @@ BEGIN
AUTOCHECKBOX "通配符搜索(&I)",IDC_WILDCARDSEARCH,132,61,74,10,WS_TABSTOP
DEFPUSHBUTTON "查找下一个(&F)",IDOK,223,7,60,14
PUSHBUTTON "查找上一个(&P)",IDC_FINDPREV,223,24,60,14
PUSHBUTTON "关闭",IDCANCEL,223,41,60,14
PUSHBUTTON "查找全部(&)",IDC_REPLACEALL,223,41,60,14
PUSHBUTTON "关闭",IDCANCEL,223,58,60,14
CONTROL "<a>(?)</a>",IDC_REGEXPHELP,"SysLink",0x0,118,73,14,10
CONTROL "<a>(?)</a>",IDC_BACKSLASHHELP,"SysLink",0x0,118,85,14,10
CONTROL "<a>(?)</a>",IDC_WILDCARDHELP,"SysLink",0x0,208,61,14,10
Expand Down
3 changes: 2 additions & 1 deletion locale/zh-Hant/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,8 @@ BEGIN
AUTOCHECKBOX "通配符搜尋(&I)",IDC_WILDCARDSEARCH,132,61,74,10,WS_TABSTOP
DEFPUSHBUTTON "尋找下一個(&F)",IDOK,223,7,60,14
PUSHBUTTON "尋找上一個(&P)",IDC_FINDPREV,223,24,60,14
PUSHBUTTON "關閉",IDCANCEL,223,41,60,14
PUSHBUTTON "尋找全部(&)",IDC_REPLACEALL,223,41,60,14
PUSHBUTTON "關閉",IDCANCEL,223,58,60,14
CONTROL "<a>(?)</a>",IDC_REGEXPHELP,"SysLink",0x0,118,73,14,10
CONTROL "<a>(?)</a>",IDC_BACKSLASHHELP,"SysLink",0x0,118,85,14,10
CONTROL "<a>(?)</a>",IDC_WILDCARDHELP,"SysLink",0x0,208,61,14,10
Expand Down
74 changes: 45 additions & 29 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -4980,11 +4980,11 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
hdwp = DeferCtlPos(hdwp, hwnd, IDC_FINDPREV, dx, 0, SWP_NOSIZE);
hdwp = DeferCtlPos(hdwp, hwnd, IDC_SAVEPOSITION, dx, 0, SWP_NOSIZE);
hdwp = DeferCtlPos(hdwp, hwnd, IDC_RESETPOSITION, dx, 0, SWP_NOSIZE);
hdwp = DeferCtlPos(hdwp, hwnd, IDC_REPLACEALL, dx, 0, SWP_NOSIZE);
if (isReplace) {
hdwp = DeferCtlPos(hdwp, hwnd, IDC_REPLACETEXT, dx, 0, SWP_NOMOVE);
hdwp = DeferCtlPos(hdwp, hwnd, IDC_CLEAR_REPLACE, dx, 0, SWP_NOSIZE);
hdwp = DeferCtlPos(hdwp, hwnd, IDC_REPLACE, dx, 0, SWP_NOSIZE);
hdwp = DeferCtlPos(hdwp, hwnd, IDC_REPLACEALL, dx, 0, SWP_NOSIZE);
hdwp = DeferCtlPos(hdwp, hwnd, IDC_REPLACEINSEL, dx, 0, SWP_NOSIZE);
}
EndDeferWindowPos(hdwp);
Expand Down Expand Up @@ -5170,8 +5170,12 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
break;

case IDC_REPLACEALL:
bReplaceInitialized = TRUE;
EditReplaceAll(lpefr->hwnd, lpefr, TRUE);
if (bIsFindDlg) {
EditFindAll(lpefr);
} else {
bReplaceInitialized = TRUE;
EditReplaceAll(lpefr->hwnd, lpefr, TRUE);
}
break;

case IDC_REPLACEINSEL:
Expand Down Expand Up @@ -5593,41 +5597,59 @@ LONG EditMarkAll_ClearEx(int findFlag, Sci_Position iSelCount, LPCSTR pszText) {
return token;
}

void EditMarkAll_Run(const LONG token, const int findFlag, const Sci_Position iSelCount, LPCSTR pszText) {
struct Sci_TextToFind ttf = {{0, SciCall_GetLength()}, pszText, {0, 0}};
void EditMarkAll_Run(BOOL bChanged, int findFlag, Sci_Position iSelCount, LPCSTR pszText) {
if (InterlockedCompareExchange(&editMarkAllStatus.done, 1, 0) == 0) {
InitializeCriticalSection(&editMarkAllStatus.criticalSection);
}
if (!bChanged) {
EnterCriticalSection(&editMarkAllStatus.criticalSection);
bChanged = findFlag != editMarkAllStatus.findFlag
|| iSelCount != editMarkAllStatus.iSelCount
// _stricmp() is not safe for DBCS string.
|| memcmp(pszText, editMarkAllStatus.pszText, iSelCount) != 0;
LeaveCriticalSection(&editMarkAllStatus.criticalSection);
if (!bChanged) {
return;
}
}

const LONG token = EditMarkAll_ClearEx(findFlag, iSelCount, pszText);
struct Sci_TextToFind ttf = { { 0, SciCall_GetLength() }, pszText, { 0, 0 } };

Sci_Position matchCount = 0;
UINT index = 0;
Sci_Position posList[256];
Sci_Position posList[256*2];

BOOL done = FALSE;
while (!done) {
HANDLE timer = WaitableTimer_New(WaitableTimer_DefaultTimeSlot);
while (WaitableTimer_Continue(timer)) {
const Sci_Position pos = SciCall_FindText(findFlag, &ttf);
if (pos == -1 || token != editMarkAllStatus.token) {
const Sci_Position iPos = SciCall_FindText(findFlag, &ttf);
if (iPos == -1 || token != editMarkAllStatus.token) {
done = TRUE;
break;
}
++matchCount;
if (index == COUNTOF(posList)) {
iMatchesCount = matchCount;
SciCall_SetIndicatorCurrent(IndicatorNumber_MarkOccurrence);
for (UINT i = 0; i < index; i++) {
SciCall_IndicatorFillRange(posList[i], iSelCount);
for (UINT i = 0; i < index; i += 2) {
SciCall_IndicatorFillRange(posList[i], posList[i + 1]);
}
index = 0;
}
posList[index++] = pos;
posList[index] = ttf.chrgText.cpMin;
posList[index + 1] = ttf.chrgText.cpMax - ttf.chrgText.cpMin;
ttf.chrg.cpMin = ttf.chrgText.cpMax;
index += 2;
}
CloseHandle(timer);
if (token == editMarkAllStatus.token) {
iMatchesCount = matchCount;
if (index) {
SciCall_SetIndicatorCurrent(IndicatorNumber_MarkOccurrence);
for (UINT i = 0; i < index; i++) {
SciCall_IndicatorFillRange(posList[i], iSelCount);
for (UINT i = 0; i < index; i += 2) {
SciCall_IndicatorFillRange(posList[i], posList[i + 1]);
}
}
UpdateStatusbar();
Expand All @@ -5642,10 +5664,6 @@ void EditMarkAll_Run(const LONG token, const int findFlag, const Sci_Position iS
}

void EditMarkAll(BOOL bChanged, BOOL bMarkOccurrencesMatchCase, BOOL bMarkOccurrencesMatchWords) {
if (InterlockedCompareExchange(&editMarkAllStatus.done, 1, 0) == 0) {
InitializeCriticalSection(&editMarkAllStatus.criticalSection);
}

// get current selection
Sci_Position iSelStart = SciCall_GetSelectionStart();
const Sci_Position iSelEnd = SciCall_GetSelectionEnd();
Expand Down Expand Up @@ -5680,21 +5698,19 @@ void EditMarkAll(BOOL bChanged, BOOL bMarkOccurrencesMatchCase, BOOL bMarkOccurr
}

const int findFlag = (bMarkOccurrencesMatchCase ? SCFIND_MATCHCASE : 0) | (bMarkOccurrencesMatchWords ? SCFIND_WHOLEWORD : 0);
if (!bChanged) {
EnterCriticalSection(&editMarkAllStatus.criticalSection);
bChanged = findFlag != editMarkAllStatus.findFlag
|| iSelCount != editMarkAllStatus.iSelCount
// _stricmp() is not safe for DBCS string.
|| memcmp(pszText, editMarkAllStatus.pszText, iSelCount) != 0;
LeaveCriticalSection(&editMarkAllStatus.criticalSection);
}
if (bChanged) {
const LONG token = EditMarkAll_ClearEx(findFlag, iSelCount, pszText);
EditMarkAll_Run(token, findFlag, iSelCount, pszText);
}
EditMarkAll_Run(bChanged, findFlag, iSelCount, pszText);
NP2HeapFree(pszText);
}

void EditFindAll(LPEDITFINDREPLACE lpefr) {
char szFind2[NP2_FIND_REPLACE_LIMIT];
if (!EditPrepareFind(szFind2, lpefr)) {
return;
}

EditMarkAll_Run(FALSE, lpefr->fuFlags, strlen(szFind2), szFind2);
}

static void ShwowReplaceCount(Sci_Position iCount) {
if (iCount > 0) {
WCHAR tchNum[32];
Expand Down
4 changes: 3 additions & 1 deletion src/Edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ void EditSelectLines(BOOL currentBlock, BOOL lineSelection);
HWND EditFindReplaceDlg(HWND hwnd, LPEDITFINDREPLACE lpefr, BOOL bReplace);
void EditFindNext(LPEDITFINDREPLACE lpefr, BOOL fExtendSelection);
void EditFindPrev(LPEDITFINDREPLACE lpefr, BOOL fExtendSelection);
void EditFindAll(LPEDITFINDREPLACE lpefr);
BOOL EditReplace(HWND hwnd, LPEDITFINDREPLACE lpefr);
BOOL EditReplaceAll(HWND hwnd, LPEDITFINDREPLACE lpefr, BOOL bShowInfo);
BOOL EditReplaceAllInSelection(HWND hwnd, LPEDITFINDREPLACE lpefr, BOOL bShowInfo);
Expand Down Expand Up @@ -214,7 +215,8 @@ LONG EditMarkAll_ClearEx(int findFlag, Sci_Position iSelCount, LPCSTR pszText);
NP2_inline void EditMarkAll_Clear(void) {
EditMarkAll_ClearEx(0, 0, NULL);
}
void EditMarkAll(BOOL bChanged, BOOL bMarkOccurrencesMatchCase, BOOL bMarkOccurrencesMatchWords);
void EditMarkAll_Run(BOOL bChanged, int findFlag, Sci_Position iSelCount, LPCSTR pszText);
void EditMarkAll(BOOL bChanged, BOOL bMarkOccurrencesMatchCase, BOOL bMarkOccurrencesMatchWords);

// auto completion fill-up characters
#define MAX_AUTO_COMPLETION_FILLUP_LENGTH 32 // Only 32 ASCII punctuation
Expand Down
3 changes: 2 additions & 1 deletion src/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,8 @@ BEGIN
AUTOCHECKBOX "W&ildcard search",IDC_WILDCARDSEARCH,132,61,74,10,WS_TABSTOP
DEFPUSHBUTTON "&Find Next",IDOK,223,7,60,14
PUSHBUTTON "Find &Previous",IDC_FINDPREV,223,24,60,14
PUSHBUTTON "Close",IDCANCEL,223,41,60,14
PUSHBUTTON "Find &All",IDC_REPLACEALL,223,41,60,14
PUSHBUTTON "Close",IDCANCEL,223,58,60,14
CONTROL "<a>(?)</a>",IDC_REGEXPHELP,"SysLink",0x0,118,73,14,10
CONTROL "<a>(?)</a>",IDC_BACKSLASHHELP,"SysLink",0x0,118,85,14,10
CONTROL "<a>(?)</a>",IDC_WILDCARDHELP,"SysLink",0x0,208,61,14,10
Expand Down

0 comments on commit 7a66ddb

Please sign in to comment.