Skip to content

Commit ed5312c

Browse files
committed
#480: renamed "scratch" to "draft", prevent working file change when saving draft
1 parent b8435f6 commit ed5312c

File tree

7 files changed

+64
-62
lines changed

7 files changed

+64
-62
lines changed

src/Edit.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ BOOL EditSaveFile(
14661466
LPCWSTR pszFile,
14671467
int iEncoding,
14681468
BOOL *pbCancelDataLoss,
1469-
BOOL bSaveCopy)
1469+
enum ESaveCopyMode saveCopyMode)
14701470
{
14711471

14721472
HANDLE hFile;
@@ -1509,13 +1509,13 @@ BOOL EditSaveFile(
15091509
return FALSE;
15101510

15111511
// ensure consistent line endings
1512-
if (bFixLineEndings)
1512+
if (bFixLineEndings && (saveCopyMode != SCM_DRAFT)) // [2e]: Autosaving directory for unsaved windows #480
15131513
{
15141514
SendMessage(hwnd, SCI_CONVERTEOLS, SendMessage(hwnd, SCI_GETEOLMODE, 0, 0), 0);
15151515
EditFixPositions(hwnd);
15161516
}
15171517
// strip trailing blanks
1518-
if (bAutoStripBlanks)
1518+
if (bAutoStripBlanks && (saveCopyMode != SCM_DRAFT)) // [2e]: Autosaving directory for unsaved windows #480
15191519
EditStripTrailingBlanks(hwnd, TRUE);
15201520

15211521
// get text
@@ -1630,7 +1630,7 @@ BOOL EditSaveFile(
16301630

16311631
if (bWriteSuccess)
16321632
{
1633-
if (!bSaveCopy)
1633+
if (saveCopyMode == SCM_NO) // [2e]: Autosaving directory for unsaved windows #480
16341634
SendMessage(hwnd, SCI_SETSAVEPOINT, 0, 0);
16351635

16361636
return TRUE;

src/Edit.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ char* EditGetClipboardText(HWND);
7676
BOOL EditCopyAppend(HWND);
7777
int EditDetectEOLMode(HWND, char*, DWORD);
7878
BOOL EditLoadFile(HWND, LPCWSTR, BOOL, int*, int*, BOOL*, BOOL*);
79-
BOOL EditSaveFile(HWND, LPCWSTR, int, BOOL*, BOOL);
79+
BOOL EditSaveFile(HWND, LPCWSTR, int, BOOL*, enum ESaveCopyMode);
8080

8181
void EditInvertCase(HWND);
8282
void EditTitleCase(HWND);

src/Extension/Externals.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
#include "Extension/StringRecoding-fwd.h"
55
#include "Scintilla.h"
66
#include "Styles.h"
7+
#include "Utils.h"
78

89
extern HWND hwndEdit;
910
extern int iEncoding;
1011
extern int iEOLMode;
1112
extern DWORD dwLastIOError;
1213
extern int iLongLinesLimit;
13-
BOOL FileIO(BOOL, LPCWSTR, BOOL, int*, int*, BOOL*, BOOL*, BOOL*, BOOL);
14+
15+
BOOL FileIO(BOOL, LPCWSTR, BOOL, int*, int*, BOOL*, BOOL*, BOOL*, enum ESaveCopyMode);
1416
BOOL n2e_IsSingleLineCommentStyleAtPos(const HWND hwnd, const int iLexer, const int iPos, EncodingData* pED);
1517
void EditSelectEx(HWND, int, int);
1618

src/Extension/Utils.c

+25-25
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
#define INI_SETTING_DISPLAY_TECHNOLOGY L"DisplayTechnology"
5151
#define INI_SETTING_SPLIT_LINES L"SplitLines"
5252
#define INI_SETTING_STARTING_LINE_NUMBER L"StartingLineNumber"
53-
#define INI_SETTING_UNSAVED_SCRATCH_PATH L"UnsavedScratchPath"
53+
#define INI_SETTING_DRAFTS_PATH L"DraftsPath"
5454

5555
#ifdef LPEG_LEXER
5656
#define INI_SETTING_LPEG_PATH L"LPegPath"
@@ -91,10 +91,10 @@ BOOL bFindWordWrapAround = FALSE;
9191
int iDisplayTechnology = SC_TECHNOLOGY_DIRECTWRITE;
9292
BOOL bExtendedSplitLines = TRUE;
9393
int iStartingLineNumber = 1;
94-
WCHAR wchUnsavedScratchPath[MAX_PATH] = { 0 };
95-
WCHAR wchScratchFileName[MAX_PATH] = { 0 };
96-
int iUnsavedScratchIndex = 0;
97-
UINT_PTR iAutoSaveTimer = 0;
94+
WCHAR wchDraftsPath[MAX_PATH] = { 0 };
95+
WCHAR wchDraftFileName[MAX_PATH] = { 0 };
96+
int iDraftIndex = 0;
97+
UINT_PTR iDraftSaveTimer = 0;
9898

9999
HWND hwndStatusProgressBar = NULL;
100100
BOOL bShowProgressBar = FALSE;
@@ -571,14 +571,14 @@ void n2e_LoadINI()
571571
iDisplayTechnology = IniGetInt(N2E_INI_SECTION, INI_SETTING_DISPLAY_TECHNOLOGY, iDisplayTechnology);
572572
bExtendedSplitLines = IniGetInt(N2E_INI_SECTION, INI_SETTING_SPLIT_LINES, bExtendedSplitLines);
573573
iStartingLineNumber = IniGetInt(N2E_INI_SECTION, INI_SETTING_STARTING_LINE_NUMBER, iStartingLineNumber);
574-
IniGetString(N2E_INI_SECTION, INI_SETTING_UNSAVED_SCRATCH_PATH, L"", wchUnsavedScratchPath, COUNTOF(wchUnsavedScratchPath));
575-
if (lstrlen(wchUnsavedScratchPath))
574+
IniGetString(N2E_INI_SECTION, INI_SETTING_DRAFTS_PATH, L"", wchDraftsPath, COUNTOF(wchDraftsPath));
575+
if (lstrlen(wchDraftsPath))
576576
{
577-
ExpandEnvironmentStringsImpl(wchUnsavedScratchPath, COUNTOF(wchUnsavedScratchPath));
578-
if (!PathFileExists(wchUnsavedScratchPath) || !PathIsDirectory(wchUnsavedScratchPath))
579-
wchUnsavedScratchPath[0] = 0;
580-
else if (!lstrlen(wchScratchFileName))
581-
n2e_InitScratchFile();
577+
ExpandEnvironmentStringsImpl(wchDraftsPath, COUNTOF(wchDraftsPath));
578+
if (!PathFileExists(wchDraftsPath) || !PathIsDirectory(wchDraftsPath))
579+
wchDraftsPath[0] = 0;
580+
else if (!lstrlen(wchDraftFileName))
581+
n2e_InitDraftFile();
582582
}
583583

584584
#ifdef LPEG_LEXER
@@ -690,32 +690,32 @@ void n2e_Reset()
690690
n2e_Init(hwndEdit);
691691
}
692692

693-
void n2e_InitScratchFile()
693+
void n2e_InitDraftFile()
694694
{
695695
WCHAR wchFileName[MAX_PATH] = { 0 };
696-
while (!lstrlen(wchScratchFileName) || PathFileExists(wchScratchFileName))
696+
while (!lstrlen(wchDraftFileName) || PathFileExists(wchDraftFileName))
697697
{
698-
iUnsavedScratchIndex++;
699-
wsprintf(wchFileName, L"%i-%i.txt", GetCurrentProcessId(), iUnsavedScratchIndex);
700-
lstrcpy(wchScratchFileName, wchUnsavedScratchPath);
701-
PathAppend(wchScratchFileName, wchFileName);
698+
iDraftIndex++;
699+
wsprintf(wchFileName, L"%i-%i.txt", GetCurrentProcessId(), iDraftIndex);
700+
lstrcpy(wchDraftFileName, wchDraftsPath);
701+
PathAppend(wchDraftFileName, wchFileName);
702702
}
703703
}
704704

705-
void n2e_CleanupScratchFile()
705+
void n2e_CleanupDraftFile()
706706
{
707-
if (lstrlen(wchScratchFileName) && PathFileExists(wchScratchFileName))
707+
if (lstrlen(wchDraftFileName) && PathFileExists(wchDraftFileName))
708708
{
709-
DeleteFile(wchScratchFileName);
710-
wchScratchFileName[0] = 0;
711-
iUnsavedScratchIndex = 0;
712-
n2e_InitScratchFile();
709+
DeleteFile(wchDraftFileName);
710+
wchDraftFileName[0] = 0;
711+
iDraftIndex = 0;
712+
n2e_InitDraftFile();
713713
}
714714
}
715715

716716
BOOL n2e_IsAutoSaveRequired()
717717
{
718-
return lstrlen(wchScratchFileName)
718+
return lstrlen(wchDraftFileName)
719719
&& n2e_IsDocumentModified() && !n2e_IsDocumentAutoSaved()
720720
&& (SciCall_GetLength() <= FileSizeLimit());
721721
}

src/Extension/Utils.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ typedef enum
133133
{
134134
SCM_NO = 0,
135135
SCM_YES = 1,
136-
SCM_SCRATCH = 2
136+
SCM_DRAFT = 2
137137
} ESaveCopyMode;
138138

139139
#define N2E_INI_SECTION L"Notepad2e"
@@ -171,8 +171,8 @@ void n2e_Release();
171171
void n2e_Reset();
172172
void n2e_Reload_Settings();
173173
BOOL n2e_CanSaveINISection(const BOOL bCheckSaveSettingsMode, const ESaveSettingsMode modeRequired);
174-
void n2e_InitScratchFile();
175-
void n2e_CleanupScratchFile();
174+
void n2e_InitDraftFile();
175+
void n2e_CleanupDraftFile();
176176
BOOL n2e_IsAutoSaveRequired();
177177
BOOL n2e_IsTextEmpty(LPCWSTR txt);
178178
BOOL n2e_IsRectangularSelection();
@@ -211,10 +211,10 @@ extern WCHAR g_wchWorkingDirectory[MAX_PATH];
211211
extern BOOL bLPegEnabled;
212212
extern WCHAR g_wchLPegHome[MAX_PATH];
213213
extern int iStartingLineNumber;
214-
extern WCHAR wchUnsavedScratchPath[MAX_PATH];
215-
extern WCHAR wchScratchFileName[MAX_PATH];
216-
extern int iUnsavedScratchIndex;
217-
extern UINT_PTR iAutoSaveTimer;
214+
extern WCHAR wchDraftsPath[MAX_PATH];
215+
extern WCHAR wchDraftFileName[MAX_PATH];
216+
extern int iDraftIndex;
217+
extern UINT_PTR iDraftSaveTimer;
218218

219219
void n2e_CreateProgressBarInStatusBar();
220220
void n2e_DestroyProgressBarInStatusBar();

src/Notepad2.c

+20-20
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ HWND hDlgGotoLine = NULL;
7575
BOOL bFileSaveInProgress = FALSE;
7676
// [2e]: Open/Save dialogs - configurable filters #258
7777
int iOpenSaveFilterIndex = 1;
78-
// [2e] : Use non-proportional font in search/replace dialog #381
78+
// [2e]: Use non-proportional font in search/replace dialog #381
7979
HFONT hMonospacedFont = NULL;
8080

8181
#define NUMTOOLBITMAPS 34
@@ -960,17 +960,17 @@ LRESULT CALLBACK MainWndProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
960960
// [2e]: Autosaving directory for unsaved windows #480
961961
else if (n2e_IsAutoSaveRequired())
962962
{
963-
iAutoSaveTimer = SetTimer(hwndMain, ID_AUTOSAVETIMER, AUTOSAVETIMEOUT, AutoSaveTimer);
963+
iDraftSaveTimer = SetTimer(hwndMain, ID_DRAFTSAVETIMER, DRAFTSAVETIMEOUT, DraftSaveTimer);
964964
}
965965
// [/2e]
966966
}
967967
else
968968
{
969969
// [2e]: Autosaving directory for unsaved windows #480
970-
if (iAutoSaveTimer)
970+
if (iDraftSaveTimer)
971971
{
972-
KillTimer(hwndMain, iAutoSaveTimer);
973-
iAutoSaveTimer = 0;
972+
KillTimer(hwndMain, iDraftSaveTimer);
973+
iDraftSaveTimer = 0;
974974
}
975975
// [2e]: Split view #316
976976
n2e_RestoreActiveEdit(FALSE);
@@ -1799,7 +1799,7 @@ void CreateBars(HWND hwnd, HINSTANCE hInstance)
17991799
bExternalBitmap = TRUE;
18001800
else
18011801
{
1802-
// [2e] Enable toolbar scaling (DPI) #327
1802+
// [2e]: Enable toolbar scaling (DPI) #327
18031803
hbmp = DPICreateToolbarBitmap(hwnd, hInstance);
18041804
hbmpCopy = CopyImage(hbmp, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
18051805
}
@@ -7476,7 +7476,7 @@ void HideMatchBraces(HWND hwnd)
74767476
//
74777477
BOOL FileIO(BOOL fLoad, LPCWSTR psz, BOOL bNoEncDetect, int *ienc, int *ieol,
74787478
BOOL *pbUnicodeErr, BOOL *pbFileTooBig,
7479-
BOOL *pbCancelDataLoss, BOOL bSaveCopy)
7479+
BOOL *pbCancelDataLoss, enum ESaveCopyMode saveCopyMode)
74807480
{
74817481
WCHAR tch[MAX_PATH + 40];
74827482
BOOL fSuccess;
@@ -7495,7 +7495,7 @@ BOOL FileIO(BOOL fLoad, LPCWSTR psz, BOOL bNoEncDetect, int *ienc, int *ieol,
74957495
if (fLoad)
74967496
fSuccess = EditLoadFile(hwndEdit, psz, bNoEncDetect, ienc, ieol, pbUnicodeErr, pbFileTooBig);
74977497
else
7498-
fSuccess = EditSaveFile(hwndEdit, psz, *ienc, pbCancelDataLoss, bSaveCopy);
7498+
fSuccess = EditSaveFile(hwndEdit, psz, *ienc, pbCancelDataLoss, saveCopyMode);
74997499

75007500
dwFileAttributes = GetFileAttributes(psz);
75017501
bReadOnly = (dwFileAttributes != INVALID_FILE_ATTRIBUTES && dwFileAttributes & FILE_ATTRIBUTE_READONLY);
@@ -7791,17 +7791,17 @@ BOOL FileSaveImpl(BOOL bSaveAlways, BOOL bAsk, BOOL bSaveAs, enum ESaveCopyMode
77917791
PathAppend(tchFile, PathFindFileName(szCurFile));
77927792
}
77937793
// [2e]: Autosaving directory for unsaved windows #480
7794-
else if (saveCopyMode == SCM_SCRATCH)
7794+
else if (saveCopyMode == SCM_DRAFT)
77957795
{
7796-
if (lstrlen(wchScratchFileName))
7797-
lstrcpy(tchFile, wchScratchFileName);
7796+
if (lstrlen(wchDraftFileName))
7797+
lstrcpy(tchFile, wchDraftFileName);
77987798
else
77997799
return FALSE;
78007800
}
78017801
else
78027802
lstrcpy(tchFile, szCurFile);
78037803

7804-
if ((saveCopyMode == SCM_SCRATCH) || SaveFileDlg(hwndMain, tchFile, COUNTOF(tchFile), tchInitialDir))
7804+
if ((saveCopyMode == SCM_DRAFT) || SaveFileDlg(hwndMain, tchFile, COUNTOF(tchFile), tchInitialDir))
78057805
{
78067806
// [2e]: Rename To fails if new name only differs in char case #140
78077807
if (lstrcmp(szCurFile, tchFile) == 0)
@@ -7815,7 +7815,7 @@ BOOL FileSaveImpl(BOOL bSaveAlways, BOOL bAsk, BOOL bSaveAs, enum ESaveCopyMode
78157815
return FALSE;
78167816
}
78177817
// [/2e]
7818-
else if (fSuccess = FileIO(FALSE, tchFile, FALSE, &iEncoding, &iEOLMode, NULL, NULL, &bCancelDataLoss, saveCopyMode != SCM_NO)
7818+
else if (fSuccess = FileIO(FALSE, tchFile, FALSE, &iEncoding, &iEOLMode, NULL, NULL, &bCancelDataLoss, saveCopyMode)
78197819
// [2e]: Process elevation #166
78207820
|| n2e_ParentProcess_ElevatedFileIO(tchFile))
78217821
{
@@ -7842,7 +7842,7 @@ BOOL FileSaveImpl(BOOL bSaveAlways, BOOL bAsk, BOOL bSaveAs, enum ESaveCopyMode
78427842
lstrcpy(tchLastSaveCopyDir, tchFile);
78437843
PathRemoveFileSpec(tchLastSaveCopyDir);
78447844
}
7845-
else if (saveCopyMode == SCM_SCRATCH)
7845+
else if (saveCopyMode == SCM_DRAFT)
78467846
{
78477847
n2e_SetDocumentAutoSaved(TRUE);
78487848
}
@@ -7900,9 +7900,9 @@ BOOL FileSave(BOOL bSaveAlways, BOOL bAsk, BOOL bSaveAs, enum ESaveCopyMode save
79007900
res = FileSaveImpl(bSaveAlways, bAsk, bSaveAs, saveCopyMode, bDeleteOld);
79017901
bFileSaveInProgress = FALSE;
79027902
// [2e]: Autosaving directory for unsaved windows #480
7903-
if (res && (saveCopyMode != SCM_SCRATCH))
7903+
if (res && (saveCopyMode != SCM_DRAFT))
79047904
{
7905-
n2e_CleanupScratchFile();
7905+
n2e_CleanupDraftFile();
79067906
}
79077907
// [/2e]
79087908
return res;
@@ -8633,10 +8633,10 @@ void CALLBACK PasteBoardTimer(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTi
86338633
}
86348634

86358635
// [2e]: Autosaving directory for unsaved windows #480
8636-
void CALLBACK AutoSaveTimer(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
8636+
void CALLBACK DraftSaveTimer(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
86378637
{
8638-
KillTimer(hwnd, iAutoSaveTimer);
8639-
iAutoSaveTimer = 0;
8640-
FileSave(TRUE, FALSE, FALSE, SCM_SCRATCH, FALSE);
8638+
KillTimer(hwnd, iDraftSaveTimer);
8639+
iDraftSaveTimer = 0;
8640+
FileSave(TRUE, FALSE, FALSE, SCM_DRAFT, FALSE);
86418641
}
86428642
// [/2e]

src/Notepad2.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ typedef struct np2params {
9393
//==== Paste Board Timer ======================================================
9494
#define ID_PASTEBOARDTIMER 0xA001
9595
// [2e]: Autosaving directory for unsaved windows #480
96-
#define ID_AUTOSAVETIMER 0xA002
97-
#define AUTOSAVETIMEOUT 1000 * 60
96+
#define ID_DRAFTSAVETIMER 0xA002
97+
#define DRAFTSAVETIMEOUT 1000 * 60
9898

9999
//==== Reuse Window Lock Timeout ==============================================
100100
#define REUSEWINDOWLOCKTIMEOUT 250
@@ -114,7 +114,7 @@ void SetNotifyIconTitle(HWND);
114114
void InstallFileWatching(LPCWSTR);
115115
void CALLBACK WatchTimerProc(HWND, UINT, UINT_PTR, DWORD);
116116
void CALLBACK PasteBoardTimer(HWND, UINT, UINT_PTR, DWORD);
117-
void CALLBACK AutoSaveTimer(HWND, UINT, UINT_PTR, DWORD);
117+
void CALLBACK DraftSaveTimer(HWND, UINT, UINT_PTR, DWORD);
118118

119119
void LoadSettings();
120120
void SaveSettings(BOOL);
@@ -141,7 +141,7 @@ void SetViewEOL(HWND);
141141
void SetShowWordWrapSymbols(HWND);
142142
void HideMatchBraces(HWND);
143143

144-
BOOL FileIO(BOOL, LPCWSTR, BOOL, int*, int*, BOOL*, BOOL*, BOOL*, BOOL);
144+
BOOL FileIO(BOOL, LPCWSTR, BOOL, int*, int*, BOOL*, BOOL*, BOOL*, enum ESaveCopyMode);
145145
BOOL FileLoad(BOOL, BOOL, BOOL, BOOL, LPCWSTR);
146146
BOOL _FileLoad(BOOL, BOOL, BOOL, BOOL, LPCWSTR, BOOL);
147147
BOOL FileSave(BOOL, BOOL, BOOL, enum ESaveCopyMode, BOOL);

0 commit comments

Comments
 (0)