Merge pull request #3968 from RaiKoHoff/Dev_Master

Fill Find pattern on F/R-dialog init, prefer clipboard over search history
This commit is contained in:
Rainer Kottenhoff 2022-02-16 18:10:53 +01:00 committed by GitHub
commit ada0d753bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 45 deletions

View File

@ -5237,16 +5237,21 @@ int ComboBox_GetTextW2MB(HWND hDlg, int nIDDlgItem, LPSTR lpString, size_t cch)
void ComboBox_SetTextHW(HWND hDlg, int nIDDlgItem, const HSTRINGW hstr)
{
ComboBox_SetText(GetDlgItem(hDlg, nIDDlgItem), StrgGet(hstr));
ComboBox_SetText(GetDlgItem(hDlg, nIDDlgItem), StrgIsNotEmpty(hstr) ? StrgGet(hstr) : L"");
}
void ComboBox_SetTextMB2W(HWND hDlg, int nIDDlgItem, LPCSTR lpString)
{
int const len = MultiByteToWideChar(Encoding_SciCP, 0, lpString, -1, NULL, 0) + 1;
LPWSTR const buf = AllocMem(len * sizeof(wchar_t), HEAP_ZERO_MEMORY);
MultiByteToWideChar(Encoding_SciCP, 0, lpString, -1, buf, len);
ComboBox_SetText(GetDlgItem(hDlg, nIDDlgItem), buf);
FreeMem(buf);
if (StrIsEmptyA(lpString)) {
ComboBox_SetText(GetDlgItem(hDlg, nIDDlgItem), L"");
}
else {
int const size = MultiByteToWideChar(Encoding_SciCP, 0, lpString, -1, NULL, 0) + 1;
LPWSTR const buf = AllocMem(size * sizeof(WCHAR), HEAP_ZERO_MEMORY);
MultiByteToWideChar(Encoding_SciCP, 0, lpString, -1, buf, size);
ComboBox_SetText(GetDlgItem(hDlg, nIDDlgItem), buf);
FreeMem(buf);
}
}
#if 0

View File

@ -5775,28 +5775,34 @@ static LPCWSTR _EditGetFindStrg(HWND hwnd, const LPEDITFINDREPLACE lpefr, bool b
}
static HSTRINGW hfind = NULL;
// TODO: get rid of const FNDRPL_BUFFER size and StrgDestroy(hfind);
if (!hfind) {
hfind = StrgCreate(NULL);
}
// 1st: try last used pattern
if (StrgIsEmpty(lpefr->chFindPattern) && bFillEmpty) {
StrgReset(hfind, GetFindPattern());
} else {
StrgReset(hfind, StrgGet(lpefr->chFindPattern));
}
// 2nd: try get clipboard content
if (StrgIsEmpty(hfind) && bFillEmpty) {
// get most recently used find pattern
LPWSTR const buf = StrgWriteAccessBuf(hfind, 0);
MRU_Enum(Globals.pMRUfind, 0, buf, (int)StrgGetAllocLength(hfind));
StrgSanitize(hfind);
}
if (StrgIsEmpty(hfind) && bFillEmpty) {
// get clipboard content
LPWSTR const buf = StrgWriteAccessBuf(hfind, 0);
LPWSTR const buf = StrgWriteAccessBuf(hfind, FNDRPL_BUFFER);
EditGetClipboardW(buf, StrgGetAllocLength(hfind));
StrgSanitize(hfind);
}
// 3rd: try get most recently used find pattern
if (StrgIsEmpty(hfind) && bFillEmpty) {
LPWSTR const buf = StrgWriteAccessBuf(hfind, FNDRPL_BUFFER);
MRU_Enum(Globals.pMRUfind, 0, buf, (int)StrgGetAllocLength(hfind));
StrgSanitize(hfind);
}
// 4th: return if still empty
if (StrgIsEmpty(hfind)) {
//~StrgDestroy(hfind); - static for speed
return NULL;
@ -5807,14 +5813,14 @@ static LPCWSTR _EditGetFindStrg(HWND hwnd, const LPEDITFINDREPLACE lpefr, bool b
SetFindPattern(StrgGet(hfind));
if (lpefr->bWildcardSearch) {
LPWSTR const buf = StrgWriteAccessBuf(hfind, 0);
LPWSTR const buf = StrgWriteAccessBuf(hfind, FNDRPL_BUFFER);
_EscapeWildcards(buf, StrgGetAllocLength(hfind), lpefr);
StrgSanitize(hfind);
}
bool const bIsRegEx = (lpefr->fuFlags & SCFIND_REGEXP);
if (lpefr->bTransformBS || bIsRegEx) {
LPWSTR const buf = StrgWriteAccessBuf(hfind, 0);
LPWSTR const buf = StrgWriteAccessBuf(hfind, FNDRPL_BUFFER);
TransformBackslashesW(buf, bIsRegEx, CP_UTF8, NULL);
StrgSanitize(hfind);
}
@ -6166,7 +6172,6 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
SetWindowSubclass(cbInfoR.hwndItem, EditBoxForPasteFixes, 0, (DWORD_PTR) &(s_wchBufOut[0]));
SHAutoComplete(cbInfoR.hwndItem, SHACF_FILESYS_ONLY | SHACF_AUTOAPPEND_FORCE_OFF | SHACF_AUTOSUGGEST_FORCE_OFF);
}
ComboBox_SetTextHW(hwnd, IDC_REPLACETEXT, s_pEfrData->chReplaceTemplate);
}
@ -6510,15 +6515,17 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
if ((cchSelection > 0) && (LOWORD(wParam) != IDC_REPLACETEXT)) {
lpszSelection = AllocMem((cchSelection + 1), HEAP_ZERO_MEMORY);
SciCall_GetSelText(lpszSelection);
} else { // (cchSelection <= 1)
} else { // (cchSelection <= 0)
// nothing is selected in the editor:
LPCWSTR wchFind = _EditGetFindStrg(Globals.hwndEdit, s_pEfrData, true);
int const len = WideCharToMultiByte(Encoding_SciCP, 0, wchFind, -1, NULL, 0, NULL, NULL);
lpszSelection = AllocMem(len, HEAP_ZERO_MEMORY);
WideCharToMultiByte(Encoding_SciCP, 0, wchFind, -1, lpszSelection, len, NULL, NULL);
// if first time you bring up find/replace dialog,
// use most recent search pattern to find box
// in case of no history: paste clipboard
LPCWSTR wchFind = _EditGetFindStrg(Globals.hwndEdit, s_pEfrData, true);
if (StrIsNotEmpty(wchFind)) {
int const size = WideCharToMultiByte(Encoding_SciCP, 0, wchFind, -1, NULL, 0, NULL, NULL);
lpszSelection = AllocMem(size * sizeof(CHAR), HEAP_ZERO_MEMORY);
WideCharToMultiByte(Encoding_SciCP, 0, wchFind, -1, lpszSelection, size, NULL, NULL);
}
}
if (lpszSelection) {
ComboBox_SetTextMB2W(hwnd, IDC_FINDTEXT, lpszSelection);

View File

@ -1460,7 +1460,7 @@ void GetFindPatternMB(LPSTR chPattern, int cch)
//
void SetFindPattern(LPCWSTR wchFindPattern)
{
StrgReset(s_hstrCurrentFindPattern, wchFindPattern);
StrgReset(s_hstrCurrentFindPattern, StrIsNotEmpty(wchFindPattern) ? wchFindPattern : L"");
}
//=============================================================================
@ -1469,7 +1469,7 @@ void SetFindPattern(LPCWSTR wchFindPattern)
//
void SetFindPatternMB(LPCSTR chFindPattern)
{
StrgResetFromUTF8(s_hstrCurrentFindPattern, chFindPattern);
StrgResetFromUTF8(s_hstrCurrentFindPattern, !StrIsEmptyA(chFindPattern) ? chFindPattern : "");
}
//=============================================================================
@ -11045,6 +11045,35 @@ bool DoElevatedRelaunch(EditFileIOStatus* pFioStatus, bool bAutoSaveOnRelaunch)
//=============================================================================
static void _MRU_UpdateSession()
{
int idx = 0;
if (MRU_FindPath(Globals.pFileMRU, Paths.CurrentFile, &idx)) {
Globals.pFileMRU->iEncoding[idx] = Encoding_GetCurrent();
Globals.pFileMRU->iCaretPos[idx] = (Settings.PreserveCaretPos) ? SciCall_GetCurrentPos() : -1;
Globals.pFileMRU->iSelAnchPos[idx] = (Settings.PreserveCaretPos) ? (Sci_IsMultiOrRectangleSelection() ? -1 : SciCall_GetAnchor()) : -1;
WCHAR wchBookMarks[MRU_BMRK_SIZE] = { L'\0' };
EditGetBookmarkList(Globals.hwndEdit, wchBookMarks, COUNTOF(wchBookMarks));
if (Globals.pFileMRU->pszBookMarks[idx]) {
LocalFree(Globals.pFileMRU->pszBookMarks[idx]); // StrDup()
Globals.pFileMRU->pszBookMarks[idx] = NULL;
}
Globals.pFileMRU->pszBookMarks[idx] = StrDup(wchBookMarks);
}
}
static void _MRU_AddSession()
{
cpi_enc_t iCurrEnc = Encoding_GetCurrent();
const DocPos iCaretPos = SciCall_GetCurrentPos();
const DocPos iAnchorPos = Sci_IsMultiOrRectangleSelection() ? -1 : SciCall_GetAnchor();
WCHAR wchBookMarks[MRU_BMRK_SIZE] = { L'\0' };
EditGetBookmarkList(Globals.hwndEdit, wchBookMarks, COUNTOF(wchBookMarks));
MRU_AddPath(Globals.pFileMRU, Paths.CurrentFile, Flags.RelativeFileMRU, Flags.PortableMyDocs, iCurrEnc, iCaretPos, iAnchorPos, wchBookMarks);
}
// ----------------------------------------------------------------------------
//
// FileSave()
//
@ -11078,19 +11107,7 @@ bool FileSave(FileSaveFlags fSaveFlags)
if (!(fSaveFlags & FSF_SaveAlways) && (!IsDocumentModified() || IsFileChangedFlagSet() || bIsEmptyNewFile) && !(fSaveFlags & FSF_SaveAs)) {
int idx;
if (MRU_FindPath(Globals.pFileMRU, Paths.CurrentFile, &idx)) {
Globals.pFileMRU->iEncoding[idx] = Encoding_GetCurrent();
Globals.pFileMRU->iCaretPos[idx] = (Settings.PreserveCaretPos) ? SciCall_GetCurrentPos() : -1;
Globals.pFileMRU->iSelAnchPos[idx] = (Settings.PreserveCaretPos) ? SciCall_GetAnchor() : -1;
WCHAR wchBookMarks[MRU_BMRK_SIZE] = {L'\0'};
EditGetBookmarkList(Globals.hwndEdit, wchBookMarks, COUNTOF(wchBookMarks));
if (Globals.pFileMRU->pszBookMarks[idx]) {
LocalFree(Globals.pFileMRU->pszBookMarks[idx]); // StrDup()
Globals.pFileMRU->pszBookMarks[idx] = NULL;
}
Globals.pFileMRU->pszBookMarks[idx] = StrDup(wchBookMarks);
}
_MRU_UpdateSession();
AutoSaveStop();
ResetFileObservationData(true);
return true;
@ -11111,6 +11128,7 @@ bool FileSave(FileSaveFlags fSaveFlags)
case IDCANCEL:
return false;
case IDNO:
_MRU_UpdateSession();
AutoSaveStop();
return true;
default:
@ -11195,23 +11213,20 @@ bool FileSave(FileSaveFlags fSaveFlags)
}
if (fSuccess) {
if (!((fSaveFlags & FSF_SaveCopy) || Flags.bDoRelaunchElevated)) {
cpi_enc_t iCurrEnc = Encoding_GetCurrent();
const DocPos iCaretPos = SciCall_GetCurrentPos();
const DocPos iAnchorPos = Sci_IsMultiOrRectangleSelection() ? -1 : SciCall_GetAnchor();
WCHAR wchBookMarks[MRU_BMRK_SIZE] = { L'\0' };
EditGetBookmarkList(Globals.hwndEdit, wchBookMarks, COUNTOF(wchBookMarks));
MRU_AddPath(Globals.pFileMRU, Paths.CurrentFile, Flags.RelativeFileMRU, Flags.PortableMyDocs, iCurrEnc, iCaretPos, iAnchorPos, wchBookMarks);
_MRU_AddSession();
AddFilePathToRecentDocs(Paths.CurrentFile);
SetSavePoint();
// Install watching of the current file
if ((fSaveFlags & FSF_SaveAs) && Settings.ResetFileWatching) {
_ResetFileWatchingMode();
}
InstallFileWatching(true);
}
else {
_MRU_UpdateSession();
}
// if current file is settings/config file: ask to start
if (Flags.bSettingsFileSoftLocked && !s_flagAppIsClosing) {
@ -11258,6 +11273,7 @@ bool FileSave(FileSaveFlags fSaveFlags)
}
}
}
if (fSuccess) {
AutoSaveStop();
ResetFileObservationData(true);