Skip to content

Commit

Permalink
Change F3/Shift+F3 to directly find next/previous occurrence
Browse files Browse the repository at this point in the history
of current selected text when find text not initialized, issue #862.
  • Loading branch information
zufuliu committed Oct 12, 2024
1 parent 666f766 commit 8d1ce64
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 68 deletions.
45 changes: 45 additions & 0 deletions src/Edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4595,6 +4595,51 @@ extern int iFindReplaceOption;
extern int iFindReplaceOpacityLevel;
extern int iSelectOption;

void EditSaveSelectionAsFindText(EDITFINDREPLACE *lpefr, int menu, bool findSelection) noexcept {
if (!findSelection && (iSelectOption & SelectOption_CopySelectionAsFindText) == 0) {
return;
}
Sci_Position cchSelection = SciCall_GetSelTextLength();
if (cchSelection == 0 && findSelection) {
EditSelectWord();
cchSelection = SciCall_GetSelTextLength();
}

if (cchSelection > 0 && cchSelection < NP2_FIND_REPLACE_LIMIT) {
char mszSelection[NP2_FIND_REPLACE_LIMIT];

SciCall_GetSelText(mszSelection);
mszSelection[cchSelection] = 0; // zero terminate

const UINT cpEdit = SciCall_GetCodePage();
strcpy(lpefr->szFind, mszSelection);

if (cpEdit != SC_CP_UTF8) {
WCHAR wszBuf[NP2_FIND_REPLACE_LIMIT];
MultiByteToWideChar(cpEdit, 0, mszSelection, -1, wszBuf, COUNTOF(wszBuf));
WideCharToMultiByte(CP_UTF8, 0, wszBuf, -1, lpefr->szFindUTF8, COUNTOF(lpefr->szFindUTF8), nullptr, nullptr);
} else {
strcpy(lpefr->szFindUTF8, mszSelection);
}

lpefr->fuFlags &= SCFIND_REGEXP - 1; // clear all regex flags
lpefr->option &= ~FindReplaceOption_TransformBackslash;

switch (menu) {
case IDM_EDIT_SAVEFIND:
break;

case CMD_FINDNEXTSEL:
EditFindNext(lpefr, false);
break;

case CMD_FINDPREVSEL:
EditFindPrev(lpefr, false);
break;
}
}
}

static void FindReplaceSetFont(HWND hwnd, bool monospaced, HFONT *hFontFindReplaceEdit) noexcept {
HWND hwndFind = GetDlgItem(hwnd, IDC_FINDTEXT);
HWND hwndRepl = GetDlgItem(hwnd, IDC_REPLACETEXT);
Expand Down
1 change: 1 addition & 0 deletions src/Edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ void EditGetExcerpt(LPWSTR lpszExcerpt, DWORD cchExcerpt) noexcept;

void EditSelectWord() noexcept;
void EditSelectLines(bool currentBlock, bool lineSelection) noexcept;
void EditSaveSelectionAsFindText(EDITFINDREPLACE *lpefr, int menu, bool findSelection) noexcept;
HWND EditFindReplaceDlg(HWND hwnd, EDITFINDREPLACE *lpefr, bool bReplace) noexcept;
void EditFindNext(const EDITFINDREPLACE *lpefr, bool fExtendSelection) noexcept;
void EditFindPrev(const EDITFINDREPLACE *lpefr, bool fExtendSelection) noexcept;
Expand Down
97 changes: 29 additions & 68 deletions src/Notepad4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3807,38 +3807,38 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) {
break;
}

if (StrIsEmpty(efrData.szFind)) {
if (LOWORD(wParam) != IDM_EDIT_REPLACENEXT) {
if (StrIsEmpty(efrData.szFind) && LOWORD(wParam) != IDM_EDIT_REPLACENEXT) {
EditSaveSelectionAsFindText(&efrData, IDM_EDIT_SAVEFIND, false);
if (StrIsEmpty(efrData.szFind)) {
SendWMCommand(hwnd, IDM_EDIT_FIND);
} else {
SendWMCommand(hwnd, IDM_EDIT_REPLACE);
}
} else {
switch (LOWORD(wParam)) {
case IDM_EDIT_FINDNEXT:
EditFindNext(&efrData, false);
break;

case IDM_EDIT_FINDPREV:
EditFindPrev(&efrData, false);
break;
}
}

case IDM_EDIT_REPLACENEXT:
if (bReplaceInitialized) {
EditReplace(hwndEdit, &efrData);
} else {
SendWMCommand(hwnd, IDM_EDIT_REPLACE);
}
break;
switch (LOWORD(wParam)) {
case IDM_EDIT_FINDNEXT:
EditFindNext(&efrData, false);
break;

case IDM_EDIT_SELTONEXT:
EditFindNext(&efrData, true);
break;
case IDM_EDIT_FINDPREV:
EditFindPrev(&efrData, false);
break;

case IDM_EDIT_SELTOPREV:
EditFindPrev(&efrData, true);
break;
case IDM_EDIT_REPLACENEXT:
if (bReplaceInitialized && StrNotEmpty(efrData.szFind)) {
EditReplace(hwndEdit, &efrData);
} else {
SendWMCommand(hwnd, IDM_EDIT_REPLACE);
}
break;

case IDM_EDIT_SELTONEXT:
EditFindNext(&efrData, true);
break;

case IDM_EDIT_SELTOPREV:
EditFindPrev(&efrData, true);
break;
}
break;

Expand Down Expand Up @@ -4631,48 +4631,9 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) {

case CMD_FINDNEXTSEL:
case CMD_FINDPREVSEL:
case IDM_EDIT_SAVEFIND: {
Sci_Position cchSelection = SciCall_GetSelTextLength();
if (cchSelection == 0) {
SendWMCommand(hwnd, IDM_EDIT_SELECTWORD);
cchSelection = SciCall_GetSelTextLength();
}

if (cchSelection > 0 && cchSelection < NP2_FIND_REPLACE_LIMIT) {
char mszSelection[NP2_FIND_REPLACE_LIMIT];

SciCall_GetSelText(mszSelection);
mszSelection[cchSelection] = 0; // zero terminate

const UINT cpEdit = SciCall_GetCodePage();
strcpy(efrData.szFind, mszSelection);

if (cpEdit != SC_CP_UTF8) {
WCHAR wszBuf[NP2_FIND_REPLACE_LIMIT];
MultiByteToWideChar(cpEdit, 0, mszSelection, -1, wszBuf, COUNTOF(wszBuf));
WideCharToMultiByte(CP_UTF8, 0, wszBuf, -1, efrData.szFindUTF8, COUNTOF(efrData.szFindUTF8), nullptr, nullptr);
} else {
strcpy(efrData.szFindUTF8, mszSelection);
}

efrData.fuFlags &= SCFIND_REGEXP - 1; // clear all regex flags
efrData.option &= ~FindReplaceOption_TransformBackslash;

switch (LOWORD(wParam)) {
case IDM_EDIT_SAVEFIND:
break;

case CMD_FINDNEXTSEL:
EditFindNext(&efrData, false);
break;

case CMD_FINDPREVSEL:
EditFindPrev(&efrData, false);
break;
}
}
}
break;
case IDM_EDIT_SAVEFIND:
EditSaveSelectionAsFindText(&efrData, LOWORD(wParam), true);
break;

case CMD_INCLINELIMIT:
case CMD_DECLINELIMIT:
Expand Down

0 comments on commit 8d1ce64

Please sign in to comment.