From 6ea830502ffe715d5b1362ed4a52d97b1032be6d Mon Sep 17 00:00:00 2001 From: Rainer Kottenhoff Date: Thu, 22 Mar 2018 19:11:51 +0100 Subject: [PATCH] + fix: Search Next (F3) handling (ver II) --- src/Edit.c | 33 +++---- src/Helpers.h | 26 +++--- src/Notepad3.c | 227 +++++++++++++++++++++++++++---------------------- src/Notepad3.h | 3 + 4 files changed, 159 insertions(+), 130 deletions(-) diff --git a/src/Edit.c b/src/Edit.c index d0764e6d4..5ff5ebef3 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -4409,9 +4409,13 @@ void __fastcall EscapeWildcards(char* szFind2, LPCEDITFINDREPLACE lpefr) int __fastcall EditGetFindStrg(HWND hwnd, LPCEDITFINDREPLACE lpefr, LPSTR szFind, int cchCnt) { UNUSED(hwnd); - if (!StringCchLenA(lpefr->szFind, COUNTOF(lpefr->szFind))) { return 0; } - - StringCchCopyA(szFind, cchCnt, lpefr->szFind); + if (StringCchLenA(lpefr->szFind, COUNTOF(lpefr->szFind))) { + StringCchCopyA(szFind, cchCnt, lpefr->szFind); + } + else { + GetFindPatternMB(szFind, cchCnt); + } + if (!StringCchLenA(szFind, cchCnt)) { return 0; } BOOL bIsRegEx = (lpefr->fuFlags & SCFIND_REGEXP); if (lpefr->bTransformBS || bIsRegEx) { @@ -4770,19 +4774,15 @@ INT_PTR CALLBACK EditFindReplaceDlgProcW(HWND hwnd,UINT umsg,WPARAM wParam,LPARA else if (cchSelection <= 1) { // nothing is selected in the editor: // if first time you bring up find/replace dialog, - // copy content from File-MRU or clipboard to find box - GetFindPattern(tchBuf, FNDRPL_BUFFER); - if (tchBuf[0] == L'\0') { - // empty string in File-MRU list - char* pClip = EditGetClipboardText(hwnd, FALSE, NULL, NULL); - if (pClip) { - int len = lstrlenA(pClip); - if (len > 0 && len < FNDRPL_BUFFER) { - lpszSelection = GlobalAlloc(GPTR, len + 1); - StringCchCopyNA(lpszSelection, len + 1, pClip, len); - } - LocalFree(pClip); + // copy content clipboard to find box + char* pClip = EditGetClipboardText(hwnd, FALSE, NULL, NULL); + if (pClip) { + int len = lstrlenA(pClip); + if (len > 0 && len < FNDRPL_BUFFER) { + lpszSelection = GlobalAlloc(GPTR, len + 1); + StringCchCopyNA(lpszSelection, len + 1, pClip, len); } + LocalFree(pClip); } } @@ -4801,6 +4801,9 @@ INT_PTR CALLBACK EditFindReplaceDlgProcW(HWND hwnd,UINT umsg,WPARAM wParam,LPARA GlobalFree(lpszSelection); } else { + if (tchBuf[0] == L'\0') { + GetFindPattern(tchBuf, FNDRPL_BUFFER); + } if (tchBuf[0] == L'\0') { MRU_Enum(g_pMRUfind, 0, tchBuf, COUNTOF(tchBuf)); } diff --git a/src/Helpers.h b/src/Helpers.h index 4bf1c009b..0eb47db0d 100644 --- a/src/Helpers.h +++ b/src/Helpers.h @@ -67,14 +67,14 @@ __inline BOOL IniSetInt(LPCWSTR lpSection, LPCWSTR lpName, int i) { int IniSectionGetString(LPCWSTR, LPCWSTR, LPCWSTR, LPWSTR, int); int IniSectionGetInt(LPCWSTR, LPCWSTR, int); UINT IniSectionGetUInt(LPCWSTR, LPCWSTR, UINT); -__inline BOOL IniSectionGetBool(LPCWSTR lpCachedIniSection, LPCWSTR lpName, BOOL bDefault) { +__forceinline BOOL IniSectionGetBool(LPCWSTR lpCachedIniSection, LPCWSTR lpName, BOOL bDefault) { return (IniSectionGetInt(lpCachedIniSection, lpName, ((bDefault) ? 1 : 0)) ? TRUE : FALSE); } BOOL IniSectionSetString(LPWSTR,LPCWSTR,LPCWSTR); -__inline BOOL IniSectionSetInt(LPWSTR lpCachedIniSection,LPCWSTR lpName, DocPos i) { +__forceinline BOOL IniSectionSetInt(LPWSTR lpCachedIniSection,LPCWSTR lpName, DocPos i) { WCHAR tch[32]={L'\0'}; StringCchPrintf(tch,COUNTOF(tch),L"%i",i); return IniSectionSetString(lpCachedIniSection,lpName,tch); } -__inline BOOL IniSectionSetBool(LPWSTR lpCachedIniSection, LPCWSTR lpName, BOOL b) { +__forceinline BOOL IniSectionSetBool(LPWSTR lpCachedIniSection, LPCWSTR lpName, BOOL b) { return IniSectionSetInt(lpCachedIniSection, lpName, (b ? 1 : 0)); } @@ -298,8 +298,8 @@ WCHAR* _StrCutIW(WCHAR*,const WCHAR*); #endif //==== StrSafe lstrlen() ======================================================= -inline int StringCchLenA(LPCSTR s,size_t n) { size_t len; HRESULT hr = StringCchLengthA(s,n,&len); return (SUCCEEDED(hr) ? (int)len : 0); } -inline int StringCchLenW(LPCWSTR s,size_t n) { size_t len; HRESULT hr = StringCchLengthW(s,n,&len); return (SUCCEEDED(hr) ? (int)len : 0); } +__forceinline int StringCchLenA(LPCSTR s,size_t n) { size_t len; HRESULT hr = StringCchLengthA(s,n,&len); return (SUCCEEDED(hr) ? (int)len : 0); } +__forceinline int StringCchLenW(LPCWSTR s,size_t n) { size_t len; HRESULT hr = StringCchLengthW(s,n,&len); return (SUCCEEDED(hr) ? (int)len : 0); } #if defined(UNICODE) || defined(_UNICODE) #define StringCchLen(s,n) StringCchLenW((s),(n)) @@ -308,7 +308,7 @@ inline int StringCchLenW(LPCWSTR s,size_t n) { size_t len; HRESULT hr = StringCc #endif //==== StrSafe lstrcmp(),lstrcmpi() ============================================= -inline int _StringCchCmpNA(PCNZCH s1,int l1,PCNZCH s2,int l2) +__forceinline int _StringCchCmpNA(PCNZCH s1,int l1,PCNZCH s2,int l2) { return (CompareStringA(LOCALE_INVARIANT,0,s1,(l1 >= 0 ? StringCchLenA(s1,l1) : -1), s2,(l2 >= 0 ? StringCchLenA(s2,l2) : -1)) - CSTR_EQUAL); @@ -316,7 +316,7 @@ inline int _StringCchCmpNA(PCNZCH s1,int l1,PCNZCH s2,int l2) #define StringCchCompareNA(s1,l1,s2,l2) _StringCchCmpNA((s1),(l1),(s2),(l2)) #define StringCchCompareXA(s1,s2) _StringCchCmpNA((s1),-1,(s2),-1) -inline int _StringCchCmpINA(PCNZCH s1,int l1,PCNZCH s2,int l2) +__forceinline int _StringCchCmpINA(PCNZCH s1,int l1,PCNZCH s2,int l2) { return (CompareStringA(LOCALE_INVARIANT,NORM_IGNORECASE,s1,(l1 >= 0 ? StringCchLenA(s1,l1) : -1), s2,(l2 >= 0 ? StringCchLenA(s2,l2) : -1)) - CSTR_EQUAL); @@ -324,14 +324,14 @@ inline int _StringCchCmpINA(PCNZCH s1,int l1,PCNZCH s2,int l2) #define StringCchCompareINA(s1,l1,s2,l2) _StringCchCmpINA((s1),(l1),(s2),(l2)) #define StringCchCompareIXA(s1,s2) _StringCchCmpINA((s1),-1,(s2),-1) -inline int _StringCchCmpNW(PCNZWCH s1,int l1,PCNZWCH s2,int l2) { +__forceinline int _StringCchCmpNW(PCNZWCH s1,int l1,PCNZWCH s2,int l2) { return (CompareStringW(LOCALE_INVARIANT,0,s1,(l1 >= 0 ? StringCchLenW(s1,l1) : -1), s2,(l2 >= 0 ? StringCchLenW(s2,l2) : -1)) - CSTR_EQUAL); } #define StringCchCompareNW(s1,l1,s2,l2) _StringCchCmpNW((s1),(l1),(s2),(l2)) #define StringCchCompareXW(s1,s2) _StringCchCmpNW((s1),-1,(s2),-1) -inline int _StringCchCmpINW(PCNZWCH s1,int l1,PCNZWCH s2,int l2) { +__forceinline int _StringCchCmpINW(PCNZWCH s1,int l1,PCNZWCH s2,int l2) { return (CompareStringW(LOCALE_INVARIANT,NORM_IGNORECASE,s1,(l1 >= 0 ? StringCchLenW(s1,l1) : -1), s2,(l2 >= 0 ? StringCchLenW(s2,l2) : -1)) - CSTR_EQUAL); } @@ -358,10 +358,10 @@ void UrlUnescapeEx(LPWSTR, LPWSTR, DWORD*); // including and linking against pathcch.lib // api-ms-win-core-path-l1-1-0.dll library : Minimum supported client is Windows 8 :-/ // so switch back to previous (deprecated) methods: -inline HRESULT PathCchAppend(PWSTR p,size_t l,PCWSTR a) { UNUSED(l); return (PathAppend(p,a) ? S_OK : E_FAIL); } -inline HRESULT PathCchCanonicalize(PWSTR p,size_t l,PCWSTR a) { UNUSED(l); return (PathCanonicalize(p,a) ? S_OK : E_FAIL); } -inline HRESULT PathCchRenameExtension(PWSTR p,size_t l,PCWSTR a) { UNUSED(l); return (PathRenameExtension(p,a) ? S_OK : E_FAIL); } -inline HRESULT PathCchRemoveFileSpec(PWSTR p,size_t l) { UNUSED(l); return (PathRemoveFileSpec(p) ? S_OK : E_FAIL); } +__forceinline HRESULT PathCchAppend(PWSTR p,size_t l,PCWSTR a) { UNUSED(l); return (PathAppend(p,a) ? S_OK : E_FAIL); } +__forceinline HRESULT PathCchCanonicalize(PWSTR p,size_t l,PCWSTR a) { UNUSED(l); return (PathCanonicalize(p,a) ? S_OK : E_FAIL); } +__forceinline HRESULT PathCchRenameExtension(PWSTR p,size_t l,PCWSTR a) { UNUSED(l); return (PathRenameExtension(p,a) ? S_OK : E_FAIL); } +__forceinline HRESULT PathCchRemoveFileSpec(PWSTR p,size_t l) { UNUSED(l); return (PathRemoveFileSpec(p) ? S_OK : E_FAIL); } // special Drag and Drop Handling diff --git a/src/Notepad3.c b/src/Notepad3.c index 4bb5a3895..d4a5599be 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -3777,48 +3777,6 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) break; - case IDM_EDIT_FIND: - if (!IsWindow(g_hwndDlgFindReplace)) { - bFindReplCopySelOrClip = TRUE; - g_hwndDlgFindReplace = EditFindReplaceDlg(g_hwndEdit, &g_efrData, FALSE); - } - else { - bFindReplCopySelOrClip = (GetForegroundWindow() != g_hwndDlgFindReplace); - if (GetDlgItem(g_hwndDlgFindReplace,IDC_REPLACE)) { - SendMessage(g_hwndDlgFindReplace,WM_COMMAND,MAKELONG(IDMSG_SWITCHTOFIND,1),0); - DestroyWindow(g_hwndDlgFindReplace); - g_hwndDlgFindReplace = EditFindReplaceDlg(g_hwndEdit,&g_efrData,FALSE); - } - else { - SetForegroundWindow(g_hwndDlgFindReplace); - PostMessage(g_hwndDlgFindReplace,WM_NEXTDLGCTL,(WPARAM)(GetDlgItem(g_hwndDlgFindReplace,IDC_FINDTEXT)),1); - } - UpdateStatusbar(); - } - break; - - - case IDM_EDIT_REPLACE: - if (!IsWindow(g_hwndDlgFindReplace)) { - bFindReplCopySelOrClip = TRUE; - g_hwndDlgFindReplace = EditFindReplaceDlg(g_hwndEdit, &g_efrData, TRUE); - } - else { - bFindReplCopySelOrClip = (GetForegroundWindow() != g_hwndDlgFindReplace); - if (!GetDlgItem(g_hwndDlgFindReplace,IDC_REPLACE)) { - SendMessage(g_hwndDlgFindReplace,WM_COMMAND,MAKELONG(IDMSG_SWITCHTOREPLACE,1),0); - DestroyWindow(g_hwndDlgFindReplace); - g_hwndDlgFindReplace = EditFindReplaceDlg(g_hwndEdit,&g_efrData,TRUE); - } - else { - SetForegroundWindow(g_hwndDlgFindReplace); - PostMessage(g_hwndDlgFindReplace,WM_NEXTDLGCTL,(WPARAM)(GetDlgItem(g_hwndDlgFindReplace, IDC_FINDTEXT)),1); - } - UpdateStatusbar(); - } - break; - - // Main Bookmark Functions case BME_EDIT_BOOKMARKNEXT: { @@ -3886,6 +3844,49 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) break; + + case IDM_EDIT_FIND: + if (!IsWindow(g_hwndDlgFindReplace)) { + bFindReplCopySelOrClip = TRUE; + g_hwndDlgFindReplace = EditFindReplaceDlg(g_hwndEdit, &g_efrData, FALSE); + } + else { + bFindReplCopySelOrClip = (GetForegroundWindow() != g_hwndDlgFindReplace); + if (GetDlgItem(g_hwndDlgFindReplace, IDC_REPLACE)) { + SendMessage(g_hwndDlgFindReplace, WM_COMMAND, MAKELONG(IDMSG_SWITCHTOFIND, 1), 0); + DestroyWindow(g_hwndDlgFindReplace); + g_hwndDlgFindReplace = EditFindReplaceDlg(g_hwndEdit, &g_efrData, FALSE); + } + else { + SetForegroundWindow(g_hwndDlgFindReplace); + PostMessage(g_hwndDlgFindReplace, WM_NEXTDLGCTL, (WPARAM)(GetDlgItem(g_hwndDlgFindReplace, IDC_FINDTEXT)), 1); + } + UpdateStatusbar(); + } + break; + + + case IDM_EDIT_REPLACE: + if (!IsWindow(g_hwndDlgFindReplace)) { + bFindReplCopySelOrClip = TRUE; + g_hwndDlgFindReplace = EditFindReplaceDlg(g_hwndEdit, &g_efrData, TRUE); + } + else { + bFindReplCopySelOrClip = (GetForegroundWindow() != g_hwndDlgFindReplace); + if (!GetDlgItem(g_hwndDlgFindReplace, IDC_REPLACE)) { + SendMessage(g_hwndDlgFindReplace, WM_COMMAND, MAKELONG(IDMSG_SWITCHTOREPLACE, 1), 0); + DestroyWindow(g_hwndDlgFindReplace); + g_hwndDlgFindReplace = EditFindReplaceDlg(g_hwndEdit, &g_efrData, TRUE); + } + else { + SetForegroundWindow(g_hwndDlgFindReplace); + PostMessage(g_hwndDlgFindReplace, WM_NEXTDLGCTL, (WPARAM)(GetDlgItem(g_hwndDlgFindReplace, IDC_FINDTEXT)), 1); + } + UpdateStatusbar(); + } + break; + + case IDM_EDIT_FINDNEXT: case IDM_EDIT_FINDPREV: case IDM_EDIT_REPLACENEXT: @@ -3895,7 +3896,7 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) if (SciCall_GetTextLength() == 0) break; - if (!strlen(g_efrData.szFind)) + if (IsFindPatternEmpty() && !StringCchLenA(g_efrData.szFind, COUNTOF(g_efrData.szFind))) { if (LOWORD(wParam) != IDM_EDIT_REPLACENEXT) SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_FIND,1),0); @@ -3932,6 +3933,59 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) } break; + + case CMD_FINDNEXTSEL: + case CMD_FINDPREVSEL: + case IDM_EDIT_SAVEFIND: + { + DocPos cchSelection = SciCall_GetSelText(NULL); + + if (1 >= cchSelection) + { + SendMessage(hwnd, WM_COMMAND, MAKELONG(IDM_EDIT_SELECTWORD, 1), 0); + cchSelection = SciCall_GetSelText(NULL); + } + + if ((1 < cchSelection) && (cchSelection < FNDRPL_BUFFER)) + { + char mszSelection[FNDRPL_BUFFER]; + SciCall_GetSelText(mszSelection); + + // Check lpszSelection and truncate newlines + char *lpsz = StrChrA(mszSelection, '\n'); + if (lpsz) *lpsz = '\0'; + + lpsz = StrChrA(mszSelection, '\r'); + if (lpsz) *lpsz = '\0'; + + StringCchCopyA(g_efrData.szFind, COUNTOF(g_efrData.szFind), mszSelection); + g_efrData.fuFlags &= (~(SCFIND_REGEXP | SCFIND_POSIX)); + g_efrData.bTransformBS = FALSE; + + WCHAR wszBuf[FNDRPL_BUFFER]; + MultiByteToWideCharStrg(Encoding_SciCP, mszSelection, wszBuf); + MRU_Add(g_pMRUfind, wszBuf, 0, 0, NULL); + SetFindPattern(wszBuf); + + switch (LOWORD(wParam)) { + + case IDM_EDIT_SAVEFIND: + break; + + case CMD_FINDNEXTSEL: + EditFindNext(g_hwndEdit, &g_efrData, FALSE, FALSE); + break; + + case CMD_FINDPREVSEL: + EditFindPrev(g_hwndEdit, &g_efrData, FALSE, FALSE); + break; + } + } + } + break; + + + case IDM_EDIT_COMPLETEWORD: EditCompleteWord(g_hwndEdit, TRUE); break; @@ -4790,56 +4844,6 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) break; - case CMD_FINDNEXTSEL: - case CMD_FINDPREVSEL: - case IDM_EDIT_SAVEFIND: - { - DocPos cchSelection = SciCall_GetSelText(NULL); - - if (1 >= cchSelection) - { - SendMessage(hwnd,WM_COMMAND,MAKELONG(IDM_EDIT_SELECTWORD,1),0); - cchSelection = SciCall_GetSelText(NULL); - } - - if ((1 < cchSelection) && (cchSelection < FNDRPL_BUFFER)) - { - char mszSelection[FNDRPL_BUFFER]; - SciCall_GetSelText(mszSelection); - - // Check lpszSelection and truncate newlines - char *lpsz = StrChrA(mszSelection,'\n'); - if (lpsz) *lpsz = '\0'; - - lpsz = StrChrA(mszSelection,'\r'); - if (lpsz) *lpsz = '\0'; - - StringCchCopyA(g_efrData.szFind,COUNTOF(g_efrData.szFind),mszSelection); - g_efrData.fuFlags &= (~(SCFIND_REGEXP|SCFIND_POSIX)); - g_efrData.bTransformBS = FALSE; - - WCHAR wszBuf[FNDRPL_BUFFER]; - MultiByteToWideCharStrg(Encoding_SciCP, mszSelection, wszBuf); - SetFindPattern(wszBuf); - - switch (LOWORD(wParam)) { - - case IDM_EDIT_SAVEFIND: - break; - - case CMD_FINDNEXTSEL: - EditFindNext(g_hwndEdit,&g_efrData,FALSE,FALSE); - break; - - case CMD_FINDPREVSEL: - EditFindPrev(g_hwndEdit,&g_efrData,FALSE,FALSE); - break; - } - } - } - break; - - case CMD_INCLINELIMIT: case CMD_DECLINELIMIT: if (!bMarkLongLines) @@ -5764,23 +5768,35 @@ LRESULT MsgNotify(HWND hwnd,WPARAM wParam,LPARAM lParam) } + +//============================================================================= +// +// Set/Get FindPattern() +// +static WCHAR sCurrentFindPattern[FNDRPL_BUFFER] = { L'\0' }; + +bool IsFindPatternEmpty() +{ + return (StringCchLenW(sCurrentFindPattern, COUNTOF(sCurrentFindPattern)) == 0); +} + //============================================================================= // // SetFindPattern() // -static WCHAR sCurrentFindPattern[FNDRPL_BUFFER] = { L'\0' }; - void SetFindPattern(LPCWSTR wchFindPattern) { - //if (wchFindPattern) { - // WideCharToMultiByteStrg(Encoding_SciCP, wchFindPattern, g_efrData.szFind); - //} - //else { - // g_efrData.szFindUTF8[0] = g_efrData.szFind[0] = '\0'; - //} StringCchCopyW(sCurrentFindPattern, COUNTOF(sCurrentFindPattern), (wchFindPattern ? wchFindPattern : L"")); } +//============================================================================= +// +// SetFindPatternMB() +// +void SetFindPatternMB(LPCSTR chFindPattern) +{ + MultiByteToWideChar(Encoding_SciCP, 0, chFindPattern, -1, sCurrentFindPattern, COUNTOF(sCurrentFindPattern)); +} //============================================================================= // @@ -5788,10 +5804,17 @@ void SetFindPattern(LPCWSTR wchFindPattern) // void GetFindPattern(LPWSTR wchFindPattern, size_t bufferSize) { - //MultiByteToWideChar(Encoding_SciCP, 0, g_efrData.szFind, -1, wchFindPattern, (int)bufferSize); StringCchCopyW(wchFindPattern, bufferSize, sCurrentFindPattern); } +//============================================================================= +// +// GetFindPatternMB() +// +void GetFindPatternMB(LPSTR chFindPattern, size_t bufferSize) +{ + WideCharToMultiByte(Encoding_SciCP, 0, sCurrentFindPattern, -1, chFindPattern, (int)bufferSize, NULL, NULL); +} //============================================================================= diff --git a/src/Notepad3.h b/src/Notepad3.h index 97bae39c2..6e839df07 100644 --- a/src/Notepad3.h +++ b/src/Notepad3.h @@ -162,8 +162,11 @@ void RestoreAction(int,DoAction); int UndoRedoActionMap(int,UndoRedoSelection_t*); void OpenHotSpotURL(DocPos, BOOL); +bool IsFindPatternEmpty(); void SetFindPattern(LPCWSTR); +void SetFindPatternMB(LPCSTR); void GetFindPattern(LPWSTR, size_t); +void GetFindPatternMB(LPSTR, size_t); BOOL FileIO(BOOL,LPCWSTR,BOOL,BOOL,int*,int*,BOOL*,BOOL*,BOOL*,BOOL*,BOOL); BOOL FileLoad(BOOL,BOOL,BOOL,BOOL,BOOL,LPCWSTR);