From ee86d0c2f8eb21327da3d01d40aeb185c2718ea9 Mon Sep 17 00:00:00 2001 From: Rainer Kottenhoff Date: Sun, 30 Aug 2020 01:09:15 +0200 Subject: [PATCH 1/3] + fix: F/R Dialog: Auto-Esc-Chars handling --- src/Edit.c | 65 ++++----------- src/Helpers.c | 214 ++++++++++++++++++++++---------------------------- src/Helpers.h | 16 ++-- 3 files changed, 118 insertions(+), 177 deletions(-) diff --git a/src/Edit.c b/src/Edit.c index 0bc24cfab..b43927d6d 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -5572,10 +5572,11 @@ static LRESULT CALLBACK EditBoxForPasteFixes(HWND hwnd, UINT uMsg, WPARAM wParam if (pefrData->bAutoEscCtrlChars) { WCHAR wchBuf2[FNDRPL_BUFFER] = { L'\0' }; - SlashW(wchBuf2, COUNTOF(wchBuf2), wchBuf); + SlashCtrlW(wchBuf2, COUNTOF(wchBuf2), wchBuf); SendMessage(hwnd, EM_REPLACESEL, (WPARAM)TRUE, (LPARAM)wchBuf2); } else { + UnSlashCtrlW(wchBuf); SendMessage(hwnd, EM_REPLACESEL, (WPARAM)TRUE, (LPARAM)wchBuf); } } @@ -5680,7 +5681,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd,UINT umsg,WPARAM wParam if (!GetWindowTextLengthW(GetDlgItem(hwnd, IDC_FINDTEXT))) { if (!StrIsEmptyA(sg_pefrData->szFind)) { - SetDlgItemTextMB2W(hwnd, IDC_FINDTEXT, sg_pefrData->szFind); + SetDlgItemTextMB2W(hwnd, IDC_FINDTEXT, sg_pefrData->szFind, sg_pefrData->bAutoEscCtrlChars); } } @@ -5697,7 +5698,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd,UINT umsg,WPARAM wParam } if (!StrIsEmptyA(sg_pefrData->szReplace)) { - SetDlgItemTextMB2W(hwnd, IDC_REPLACETEXT, sg_pefrData->szReplace); + SetDlgItemTextMB2W(hwnd, IDC_REPLACETEXT, sg_pefrData->szReplace, sg_pefrData->bAutoEscCtrlChars); } } @@ -5966,19 +5967,8 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd,UINT umsg,WPARAM wParam DocPos const cchSelection = SciCall_GetSelText(NULL); if ((1 < cchSelection) && (LOWORD(wParam) != IDC_REPLACETEXT)) { - if (sg_pefrData->bAutoEscCtrlChars) { - lpszSelection = AllocMem((cchSelection<<1) + 1, HEAP_ZERO_MEMORY); - char* buf = AllocMem(cchSelection + 1, HEAP_ZERO_MEMORY); - if (buf) { - SciCall_GetSelText(buf); - SlashA(lpszSelection, (cchSelection<<1), buf); - FreeMem(buf); - } - } - else { - lpszSelection = AllocMem(cchSelection + 1, HEAP_ZERO_MEMORY); - SciCall_GetSelText(lpszSelection); - } + lpszSelection = AllocMem(cchSelection + 1, HEAP_ZERO_MEMORY); + SciCall_GetSelText(lpszSelection); } else { // (cchSelection <= 1) // nothing is selected in the editor: @@ -5990,18 +5980,12 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd,UINT umsg,WPARAM wParam } // no recent find pattern: copy content clipboard to find box if (s_tchBuf[0] == L'\0') { - char* const pClip = EditGetClipboardText(Globals.hwndEdit, false, NULL, NULL); + char *const pClip = EditGetClipboardText(Globals.hwndEdit, false, NULL, NULL); if (pClip) { size_t const len = StringCchLenA(pClip, 0); if (len) { - if (sg_pefrData->bAutoEscCtrlChars) { - lpszSelection = AllocMem((len<<1) + 1, HEAP_ZERO_MEMORY); - SlashA(lpszSelection, (len<<1) + 1, pClip); - } - else { - lpszSelection = AllocMem(len + 1, HEAP_ZERO_MEMORY); - StringCchCopyA(lpszSelection, len + 1, pClip); - } + lpszSelection = AllocMem(len + 1, HEAP_ZERO_MEMORY); + StringCchCopyA(lpszSelection, len + 1, pClip); } FreeMem(pClip); } @@ -6009,7 +5993,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd,UINT umsg,WPARAM wParam } if (lpszSelection) { - SetDlgItemTextMB2W(hwnd, IDC_FINDTEXT, lpszSelection); + SetDlgItemTextMB2W(hwnd, IDC_FINDTEXT, lpszSelection, sg_pefrData->bAutoEscCtrlChars); FreeMem(lpszSelection); lpszSelection = NULL; bEditChange = true; @@ -6021,7 +6005,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd,UINT umsg,WPARAM wParam if (s_tchBuf[0] == L'\0') { MRU_Enum(Globals.pMRUfind, 0, s_tchBuf, COUNTOF(s_tchBuf)); } - SetDlgItemText(hwnd, IDC_FINDTEXT, s_tchBuf); + SetDlgItemTextEx(hwnd, IDC_FINDTEXT, s_tchBuf, sg_pefrData->bAutoEscCtrlChars); bEditChange = true; } @@ -6207,24 +6191,9 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd,UINT umsg,WPARAM wParam case IDC_FINDAUTOESCCTRLCHR: { s_SaveAutoEscCtrlChars = IsButtonChecked(hwnd, IDC_FINDAUTOESCCTRLCHR); - if (s_SaveAutoEscCtrlChars) { - char buf[FNDRPL_BUFFER + 1]; - SlashA(buf, COUNTOF(buf), sg_pefrData->szFind); - StringCchCopyA(sg_pefrData->szFind, COUNTOF(sg_pefrData->szFind), buf); - SetDlgItemTextMB2W(hwnd, IDC_FINDTEXT, sg_pefrData->szFind); - if (GetDlgItem(hwnd, IDC_REPLACE)) { - SlashA(buf, COUNTOF(buf), sg_pefrData->szReplace); - StringCchCopyA(sg_pefrData->szReplace, COUNTOF(sg_pefrData->szReplace), buf); - SetDlgItemTextMB2W(hwnd, IDC_REPLACETEXT, sg_pefrData->szReplace); - } - } - else { - UnSlashA(sg_pefrData->szFind, Encoding_SciCP); - SetDlgItemTextMB2W(hwnd, IDC_FINDTEXT, sg_pefrData->szFind); - if (GetDlgItem(hwnd, IDC_REPLACE)) { - UnSlashA(sg_pefrData->szReplace, Encoding_SciCP); - SetDlgItemTextMB2W(hwnd, IDC_REPLACETEXT, sg_pefrData->szReplace); - } + SetDlgItemTextMB2W(hwnd, IDC_FINDTEXT, sg_pefrData->szFind, s_SaveAutoEscCtrlChars); + if (GetDlgItem(hwnd, IDC_REPLACE)) { + SetDlgItemTextMB2W(hwnd, IDC_REPLACETEXT, sg_pefrData->szReplace, s_SaveAutoEscCtrlChars); } _SetSearchFlags(hwnd, sg_pefrData); _DelayMarkAll(hwnd, 50, s_InitialSearchStart); @@ -6310,8 +6279,8 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd,UINT umsg,WPARAM wParam SendDlgItemMessage(hwnd, IDC_REPLACETEXT, CB_ADDSTRING, 0, (LPARAM)s_tchBuf); } - SetDlgItemTextMB2W(hwnd, IDC_FINDTEXT, sg_pefrData->szFind); - SetDlgItemTextMB2W(hwnd, IDC_REPLACETEXT, sg_pefrData->szReplace); + SetDlgItemTextMB2W(hwnd, IDC_FINDTEXT, sg_pefrData->szFind, sg_pefrData->bAutoEscCtrlChars); + SetDlgItemTextMB2W(hwnd, IDC_REPLACETEXT, sg_pefrData->szReplace, sg_pefrData->bAutoEscCtrlChars); if (!s_bSwitchedFindReplace) { SendMessage(hwnd, WM_NEXTDLGCTL, (WPARAM)(GetFocus()), 1); @@ -6436,7 +6405,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd,UINT umsg,WPARAM wParam case IDACC_SAVEFIND: Globals.FindReplaceMatchFoundState = FND_NOP; SendWMCommand(Globals.hwndMain, IDM_EDIT_SAVEFIND); - SetDlgItemTextMB2W(hwnd, IDC_FINDTEXT, sg_pefrData->szFind); + SetDlgItemTextMB2W(hwnd, IDC_FINDTEXT, sg_pefrData->szFind, sg_pefrData->bAutoEscCtrlChars); CheckDlgButton(hwnd, IDC_FINDREGEXP, BST_UNCHECKED); CheckDlgButton(hwnd, IDC_DOT_MATCH_ALL, BST_UNCHECKED); CheckDlgButton(hwnd, IDC_WILDCARDSEARCH, BST_UNCHECKED); diff --git a/src/Helpers.c b/src/Helpers.c index 2828e1dbc..813d50f4f 100644 --- a/src/Helpers.c +++ b/src/Helpers.c @@ -1515,14 +1515,28 @@ size_t FormatNumberStr(LPWSTR lpNumberStr, size_t cch, int fixedWidth) bool SetDlgItemIntEx(HWND hwnd,int nIdItem,UINT uValue) { WCHAR szBuf[64] = { L'\0' }; - StringCchPrintf(szBuf,COUNTOF(szBuf),L"%u",uValue); FormatNumberStr(szBuf, COUNTOF(szBuf), 0); - return(SetDlgItemText(hwnd,nIdItem,szBuf)); } +//============================================================================= +// +// Esc/UnEsc Dialog Item Text +// +UINT SetDlgItemTextEx(HWND hDlg, int nIDDlgItem, LPCWSTR lpString, bool escCtrlChar) +{ + WCHAR wsz[FNDRPL_BUFFER] = { L'\0' }; + if (escCtrlChar) { + SlashCtrlW(wsz, COUNTOF(wsz), lpString); + } else { + StringCchCopy(wsz, COUNTOF(wsz), lpString); + UnSlashCtrlW(wsz); + } + return SetDlgItemTextW(hDlg, nIDDlgItem, wsz); +} + //============================================================================= // // A2W: Convert Dialog Item Text form Unicode to UTF-8 and vice versa @@ -1531,16 +1545,16 @@ UINT GetDlgItemTextW2MB(HWND hDlg, int nIDDlgItem, LPSTR lpString, int nMaxCount { WCHAR wsz[FNDRPL_BUFFER] = { L'\0' }; UINT uRet = GetDlgItemTextW(hDlg, nIDDlgItem, wsz, COUNTOF(wsz)); - ZeroMemory(lpString,nMaxCount); + ZeroMemory(lpString, nMaxCount); WideCharToMultiByte(Encoding_SciCP, 0, wsz, -1, lpString, nMaxCount - 1, NULL, NULL); return uRet; } -UINT SetDlgItemTextMB2W(HWND hDlg, int nIDDlgItem, LPSTR lpString) +UINT SetDlgItemTextMB2W(HWND hDlg, int nIDDlgItem, LPCSTR lpString, bool escCtrlChar) { WCHAR wsz[FNDRPL_BUFFER] = { L'\0' }; MultiByteToWideChar(Encoding_SciCP, 0, lpString, -1, wsz, (int)COUNTOF(wsz)); - return SetDlgItemTextW(hDlg, nIDDlgItem, wsz); + return SetDlgItemTextEx(hDlg, nIDDlgItem, wsz, escCtrlChar); } LRESULT ComboBox_AddStringMB2W(HWND hwnd, LPCSTR lpString) @@ -1678,67 +1692,6 @@ size_t SlashA(LPSTR pchOutput, size_t cchOutLen, LPCSTR pchInput) } -size_t SlashW(LPWSTR pchOutput, size_t cchOutLen, LPCWSTR pchInput) -{ - if (!pchOutput || cchOutLen < 2 || !pchInput) { return 0; } - - size_t i = 0; - size_t k = 0; - size_t const maxcnt = cchOutLen - 2; - while ((pchInput[k] != L'\0') && (i < maxcnt)) - { - switch (pchInput[k]) { - case L'\\': - pchOutput[i++] = L'\\'; - pchOutput[i++] = L'\\'; - break; - case L'\n': - pchOutput[i++] = L'\\'; - pchOutput[i++] = L'n'; - break; - case L'\r': - pchOutput[i++] = L'\\'; - pchOutput[i++] = L'r'; - break; - case L'\t': - pchOutput[i++] = L'\\'; - pchOutput[i++] = L't'; - break; - case L'\f': - pchOutput[i++] = L'\\'; - pchOutput[i++] = L'f'; - break; - case L'\v': - pchOutput[i++] = L'\\'; - pchOutput[i++] = L'v'; - break; - case L'\a': - pchOutput[i++] = L'\\'; - pchOutput[i++] = L'a'; - break; - case L'\b': - pchOutput[i++] = L'\\'; - pchOutput[i++] = L'b'; - break; - case L'\x1B': - pchOutput[i++] = L'\\'; - pchOutput[i++] = L'e'; - break; - default: - pchOutput[i++] = pchInput[k]; - break; - } - ++k; - } - pchOutput[i] = pchInput[k]; - // ensure string end - if (pchInput[k] != L'\0') { - pchOutput[++i] = L'\0'; - } - return i; -} - - /** ****************************************************************************** * * UnSlash functions @@ -1837,8 +1790,67 @@ size_t UnSlashA(LPSTR pchInOut, UINT cpEdit) return (size_t)((ptrdiff_t)(o - sStart)); } -size_t UnSlashW(LPWSTR pchInOut) -{ + +//============================================================================= + +size_t SlashCtrlW(LPWSTR pchOutput, size_t cchOutLen, LPCWSTR pchInput) { + if (!pchOutput || cchOutLen < 2 || !pchInput) { + return 0; + } + + size_t i = 0; + size_t k = 0; + size_t const maxcnt = cchOutLen - 2; + while ((pchInput[k] != L'\0') && (i < maxcnt)) { + switch (pchInput[k]) { + case L'\n': + pchOutput[i++] = L'\\'; + pchOutput[i++] = L'n'; + break; + case L'\r': + pchOutput[i++] = L'\\'; + pchOutput[i++] = L'r'; + break; + case L'\t': + pchOutput[i++] = L'\\'; + pchOutput[i++] = L't'; + break; + case L'\f': + pchOutput[i++] = L'\\'; + pchOutput[i++] = L'f'; + break; + case L'\v': + pchOutput[i++] = L'\\'; + pchOutput[i++] = L'v'; + break; + case L'\a': + pchOutput[i++] = L'\\'; + pchOutput[i++] = L'a'; + break; + case L'\b': + pchOutput[i++] = L'\\'; + pchOutput[i++] = L'b'; + break; + case L'\x1B': + pchOutput[i++] = L'\\'; + pchOutput[i++] = L'e'; + break; + default: + pchOutput[i++] = pchInput[k]; + break; + } + ++k; + } + pchOutput[i] = pchInput[k]; + // ensure string end + if (pchInput[k] != L'\0') { + pchOutput[++i] = L'\0'; + } + return i; +} + + +size_t UnSlashCtrlW(LPWSTR pchInOut) { LPWSTR s = pchInOut; LPWSTR o = pchInOut; LPCWSTR const sStart = pchInOut; @@ -1846,68 +1858,27 @@ size_t UnSlashW(LPWSTR pchInOut) while (*s) { if (*s == '\\') { ++s; - if (*s == L'a') - *o = L'\a'; - else if (*s == L'b') - *o = L'\b'; - else if (*s == L'e') - *o = L'\x1B'; - else if (*s == L'f') - *o = L'\f'; - else if (*s == L'n') + if (*s == L'n') *o = L'\n'; else if (*s == L'r') *o = L'\r'; else if (*s == L't') *o = L'\t'; + else if (*s == L'f') + *o = L'\f'; else if (*s == L'v') *o = L'\v'; - else if (*s == L'"') - *o = L'"'; - else if (*s == L'\\') - *o = L'\\'; - else if (*s == L'x' || *s == L'u') { - bool bShort = (*s == L'x'); - int hex = GetHexDigitW(*(s + 1)); - if (hex >= 0) { - WCHAR val = (WCHAR)hex; - hex = GetHexDigitW(*(++s + 1)); - if (hex >= 0) { - ++s; - val *= 16; - val += (WCHAR)hex; - if (!bShort) { - hex = GetHexDigitW(*(s + 1)); - if (hex >= 0) { - val *= 16; - val += (WCHAR)hex; - hex = GetHexDigitW(*(++s + 1)); - if (hex >= 0) { - ++s; - val *= 16; - val += (WCHAR)hex; - } - } - } - } - - if (val) { - *o = val; - } - else - --o; - } - else - --o; - } - else { - //~*o = '\\'; *++o = *s; // revert + else if (*s == L'a') + *o = L'\a'; + else if (*s == L'b') + *o = L'\b'; + else if (*s == L'e') + *o = L'\x1B'; + else *o = *s; // swallow single '\' - } - } - else + } else { *o = *s; - + } ++o; if (*s) { ++s; @@ -1916,6 +1887,7 @@ size_t UnSlashW(LPWSTR pchInOut) *o = '\0'; return (size_t)((ptrdiff_t)(o - sStart)); } +//============================================================================= size_t UnSlashChar(LPWSTR pchInOut, WCHAR wch) diff --git a/src/Helpers.h b/src/Helpers.h index bfab1962d..1ecb94794 100644 --- a/src/Helpers.h +++ b/src/Helpers.h @@ -306,9 +306,10 @@ void PathFixBackslashes(LPWSTR lpsz); size_t FormatNumberStr(LPWSTR lpNumberStr, size_t cch, int fixedWidth); bool SetDlgItemIntEx(HWND hwnd,int nIdItem,UINT uValue); -UINT GetDlgItemTextW2MB(HWND hDlg,int nIDDlgItem,LPSTR lpString,int nMaxCount); -UINT SetDlgItemTextMB2W(HWND hDlg,int nIDDlgItem,LPSTR lpString); -LRESULT ComboBox_AddStringMB2W(HWND hwnd,LPCSTR lpString); +UINT SetDlgItemTextEx(HWND hDlg, int nIDDlgItem, LPCWSTR lpString, bool escCtrlChar); +UINT GetDlgItemTextW2MB(HWND hDlg, int nIDDlgItem, LPSTR lpString, int nMaxCount); +UINT SetDlgItemTextMB2W(HWND hDlg, int nIDDlgItem, LPCSTR lpString, bool escCtrlChar); +LRESULT ComboBox_AddStringMB2W(HWND hwnd, LPCSTR lpString); /////////////////////////////////////////////////////////////////////// @@ -321,14 +322,13 @@ UINT CodePageFromCharSet(const UINT uCharSet); //==== UnSlash Functions ====================================================== -size_t SlashA(LPSTR pchOutput, size_t cchOutLen, LPCSTR pchInput); -size_t SlashW(LPWSTR pchOutput, size_t cchOutLen, LPCWSTR pchInput); - size_t UnSlashA(LPSTR pchInOut, UINT cpEdit); -size_t UnSlashW(LPWSTR pchInOut); size_t UnSlashChar(LPWSTR pchInOut, WCHAR wch); -void TransformBackslashes(char* pszInput, bool, UINT cpEdit, int* iReplaceMsg); +size_t SlashCtrlW(LPWSTR pchOutput, size_t cchOutLen, LPCWSTR pchInput); +size_t UnSlashCtrlW(LPWSTR pchInOut); + +void TransformBackslashes(char *pszInput, bool, UINT cpEdit, int *iReplaceMsg); void TransformMetaChars(char* pszInput, bool, int iEOLMode); From e360cc3658db68300b8386e54a167818c62123e1 Mon Sep 17 00:00:00 2001 From: Rainer Kottenhoff Date: Sun, 30 Aug 2020 01:12:01 +0200 Subject: [PATCH 2/3] + fix: remove needless function --- src/Helpers.c | 64 --------------------------------------------------- 1 file changed, 64 deletions(-) diff --git a/src/Helpers.c b/src/Helpers.c index 813d50f4f..e8107fdea 100644 --- a/src/Helpers.c +++ b/src/Helpers.c @@ -1628,70 +1628,6 @@ unsigned int UnSlashLowOctal(char* s) { } -/* - * transform control chars into backslash sequence - */ -size_t SlashA(LPSTR pchOutput, size_t cchOutLen, LPCSTR pchInput) -{ - if (!pchOutput || cchOutLen < 2 || !pchInput) { return 0; } - - size_t i = 0; - size_t k = 0; - size_t const maxcnt = cchOutLen - 2; - while ((pchInput[k] != '\0') && (i < maxcnt)) - { - switch (pchInput[k]) { - case '\\': - pchOutput[i++] = '\\'; - pchOutput[i++] = '\\'; - break; - case '\n': - pchOutput[i++] = '\\'; - pchOutput[i++] = 'n'; - break; - case '\r': - pchOutput[i++] = '\\'; - pchOutput[i++] = 'r'; - break; - case '\t': - pchOutput[i++] = '\\'; - pchOutput[i++] = 't'; - break; - case '\f': - pchOutput[i++] = '\\'; - pchOutput[i++] = 'f'; - break; - case '\v': - pchOutput[i++] = '\\'; - pchOutput[i++] = 'v'; - break; - case '\a': - pchOutput[i++] = '\\'; - pchOutput[i++] = 'a'; - break; - case '\b': - pchOutput[i++] = '\\'; - pchOutput[i++] = 'b'; - break; - case '\x1B': - pchOutput[i++] = '\\'; - pchOutput[i++] = 'e'; - break; - default: - pchOutput[i++] = pchInput[k]; - break; - } - ++k; - } - pchOutput[i] = pchInput[k]; - // ensure string end - if (pchInput[k] != '\0') { - pchOutput[++i] = '\0'; - } - return i; -} - - /** ****************************************************************************** * * UnSlash functions From ea8931f3c71d0d2d765a55e0e04834b7222f1a0f Mon Sep 17 00:00:00 2001 From: Rainer Kottenhoff Date: Sun, 30 Aug 2020 09:27:10 +0200 Subject: [PATCH 3/3] + fix: F/R Dialog: Clear F/R History - remove entries from .ini-file too --- src/Edit.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Edit.c b/src/Edit.c index b43927d6d..c1725072e 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -6378,11 +6378,17 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd,UINT umsg,WPARAM wParam case IDACC_CLEAR_FIND_HISTORY: MRU_Empty(Globals.pMRUfind, true); + if (Globals.bCanSaveIniFile) { + MRU_Save(Globals.pMRUfind); + } while ((int)SendDlgItemMessage(hwnd, IDC_FINDTEXT, CB_DELETESTRING, 0, 0) > 0) {}; break; case IDACC_CLEAR_REPL_HISTORY: MRU_Empty(Globals.pMRUreplace, true); + if (Globals.bCanSaveIniFile) { + MRU_Save(Globals.pMRUreplace); + } while ((int)SendDlgItemMessage(hwnd, IDC_REPLACETEXT, CB_DELETESTRING, 0, 0) > 0) {}; break;