drop multi-dim-key-iterator.patch
[nedit-bw.git] / remove_zero_sized_files.patch
blobd03f228fe9c8c734222e20872ca857a7b2e3cefe
1 Subject: remove zero sized files on close
3 If a window will be closed, check the file size and never/prompt/always
4 remove the file if the size is 0.
6 ---
8 doc/help.etx | 3 +++
9 source/file.c | 30 ++++++++++++++++++++++++++++++
10 source/menu.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
11 source/nedit.h | 5 +++++
12 source/preferences.c | 20 ++++++++++++++++++++
13 source/preferences.h | 2 ++
14 6 files changed, 108 insertions(+)
16 diff --quilt old/source/file.c new/source/file.c
17 --- old/source/file.c
18 +++ new/source/file.c
19 @@ -898,6 +898,33 @@ int CloseAllFilesAndWindows(void)
20 return TRUE;
23 +static void removeZeroSizedFile(WindowInfo *window)
25 + int response;
26 + struct stat statbuf;
27 + char fullname[MAXPATHLEN + 1];
29 + /* Get the full name of the file */
30 + strcpy(fullname, window->path);
31 + strcat(fullname, window->filename);
33 + if (GetPrefZeroSizedMode() == ZEROSIZED_NEVER)
34 + return;
36 + if (stat(fullname, &statbuf) == 0
37 + && statbuf.st_size == 0
38 + && !S_ISFIFO(statbuf.st_mode)) {
39 + if (GetPrefZeroSizedMode() == ZEROSIZED_PROMPT) {
40 + response = DialogF(DF_QUES, window->shell, 2, "Zero Sized File",
41 + "%s has a file size of 0.\nDo you want to remove this file?",
42 + "Yes", "No", fullname);
43 + if (response == 2)
44 + return;
45 + }
46 + remove(fullname);
47 + }
50 int CloseFileAndWindow(WindowInfo *window, int preResponse)
52 int response, stat;
53 @@ -921,6 +948,7 @@ int CloseFileAndWindow(WindowInfo *windo
54 /* File deleted/modified externally, ignored by user. */
55 !GetPrefWarnFileMods())))
57 + removeZeroSizedFile(window);
58 RemoveBackupFile(window);
59 CloseWindow(window);
60 /* up-to-date windows don't have outstanding backup files to close */
61 @@ -941,6 +969,7 @@ int CloseFileAndWindow(WindowInfo *windo
62 stat = SaveWindow(window);
63 if (stat)
65 + removeZeroSizedFile(window);
66 CloseWindow(window);
67 } else
69 @@ -948,6 +977,7 @@ int CloseFileAndWindow(WindowInfo *windo
71 } else if (response == NO_SBC_DIALOG_RESPONSE)
73 + removeZeroSizedFile(window);
74 /* Don't Save */
75 RemoveBackupFile(window);
76 CloseWindow(window);
77 diff --quilt old/source/menu.c new/source/menu.c
78 --- old/source/menu.c
79 +++ new/source/menu.c
80 @@ -144,6 +144,9 @@ static void preserveDefCB(Widget w, Wind
81 static void ignoreHardlinkDefCB(Widget w, WindowInfo *window, caddr_t callData);
82 static void promptHardlinkDefCB(Widget w, WindowInfo *window, caddr_t callData);
83 static void unlinkHardlinkDefCB(Widget w, WindowInfo *window, caddr_t callData);
84 +static void neverZeroSizedDefCB(Widget w, WindowInfo *window, caddr_t callData);
85 +static void promptZeroSizedDefCB(Widget w, WindowInfo *window, caddr_t callData);
86 +static void alwaysZeroSizedDefCB(Widget w, WindowInfo *window, caddr_t callData);
87 static void noWrapDefCB(Widget w, WindowInfo *window, caddr_t callData);
88 static void newlineWrapDefCB(Widget w, WindowInfo *window, caddr_t callData);
89 static void contWrapDefCB(Widget w, WindowInfo *window, caddr_t callData);
90 @@ -1034,6 +1037,19 @@ Widget CreateMenuBar(Widget parent, Wind
91 subSubPane, "unlink", "Unlink", 'U', unlinkHardlinkDefCB, window,
92 GetPrefHardlinkMode() == HARDLINK_UNLINK, SHORT);
94 + /* zero sized mode default sub menu */
95 + subSubPane = createMenu(subPane, "zeroSizedMode", "Remove Zero Sized Files", 'Z',
96 + NULL, FULL);
97 + window->zeroSizedDefItem[ZEROSIZED_NEVER] = createMenuRadioToggle(
98 + subSubPane, "never", "Never", 'N', neverZeroSizedDefCB, window,
99 + GetPrefZeroSizedMode() == ZEROSIZED_NEVER, SHORT);
100 + window->zeroSizedDefItem[ZEROSIZED_PROMPT] = createMenuRadioToggle(
101 + subSubPane, "prompt", "Prompt", 'P', promptZeroSizedDefCB, window,
102 + GetPrefZeroSizedMode() == ZEROSIZED_PROMPT, SHORT);
103 + window->zeroSizedDefItem[ZEROSIZED_ALWAYS] = createMenuRadioToggle(
104 + subSubPane, "always", "Always", 'A', alwaysZeroSizedDefCB, window,
105 + GetPrefZeroSizedMode() == ZEROSIZED_ALWAYS, SHORT);
107 /* Show Matching sub menu */
108 subSubPane = createMenu(subPane, "showMatching", "Show Matching (..)", 'M',
109 NULL, FULL);
110 @@ -1911,6 +1927,38 @@ static void unlinkHardlinkDefCB(Widget w
111 setHardlinkModeMenu(HARDLINK_UNLINK);
114 +static void setZeroSizedModeMenu(enum zeroSizedMode mode)
116 + WindowInfo *win;
117 + int i;
119 + if (mode >= N_ZEROSIZED_MODES)
120 + return;
122 + /* Set the preference and make the other windows' menus agree */
123 + SetPrefZeroSizedMode(mode);
124 + for (win = WindowList; win != NULL; win = win->next) {
125 + for (i = 0; i < N_ZEROSIZED_MODES; i++)
126 + XmToggleButtonSetState(win->zeroSizedDefItem[i],
127 + mode == i ? True : False, False);
131 +static void neverZeroSizedDefCB(Widget w, WindowInfo *window, caddr_t callData)
133 + setZeroSizedModeMenu(ZEROSIZED_NEVER);
136 +static void promptZeroSizedDefCB(Widget w, WindowInfo *window, caddr_t callData)
138 + setZeroSizedModeMenu(ZEROSIZED_PROMPT);
141 +static void alwaysZeroSizedDefCB(Widget w, WindowInfo *window, caddr_t callData)
143 + setZeroSizedModeMenu(ZEROSIZED_ALWAYS);
146 static void fontDefCB(Widget w, WindowInfo *window, caddr_t callData)
148 HidePointerOnKeyedEvent(WidgetToWindow(MENU_WIDGET(w))->lastFocus,
149 diff --quilt old/source/nedit.h new/source/nedit.h
150 --- old/source/nedit.h
151 +++ new/source/nedit.h
152 @@ -117,6 +117,10 @@ enum truncSubstitution {TRUNCSUBST_SILEN
153 enum hardlinkMode {HARDLINK_IGNORE, HARDLINK_PROMPT, HARDLINK_UNLINK,
154 N_HARDLINK_MODES};
156 +/* This enum must be kept in sync with ZeroSizedModes[] in in preferences.c */
157 +enum zeroSizedMode {ZEROSIZED_NEVER, ZEROSIZED_PROMPT, ZEROSIZED_ALWAYS,
158 + N_ZEROSIZED_MODES};
160 #define NO_FLASH_STRING "off"
161 #define FLASH_DELIMIT_STRING "delimiter"
162 #define FLASH_RANGE_STRING "range"
163 @@ -576,6 +580,7 @@ typedef struct _WindowInfo {
164 int inMacroHook; /* to protect GC in MacroApplyHook() */
165 Widget hardlinkDefItem[N_HARDLINK_MODES];
166 Boolean hardlinkDontPromptAgain;
167 + Widget zeroSizedDefItem[N_ZEROSIZED_MODES];
168 } WindowInfo;
170 extern WindowInfo *WindowList;
171 diff --quilt old/source/preferences.c new/source/preferences.c
172 --- old/source/preferences.c
173 +++ new/source/preferences.c
174 @@ -176,6 +176,13 @@ static char *HardlinkModes[] = {
175 NULL
178 +static char *ZeroSizedModes[] = {
179 + "Never",
180 + "Prompt",
181 + "Always",
182 + NULL
185 /* list of available language modes and language specific preferences */
186 static int NLanguageModes = 0;
187 typedef struct {
188 @@ -347,6 +354,7 @@ static struct prefData {
189 Boolean forceOSConversion;
190 Boolean showScrolltip;
191 int hardlinkMode;
192 + int zeroSizedMode;
193 } PrefData;
195 /* Temporary storage for preferences strings which are discarded after being
196 @@ -1179,6 +1187,8 @@ static PrefDescripRec PrefDescrip[] = {
197 // &PrefData.showScrolltip, NULL, False},
198 {"hardlinkMode", "HardlinkMode", PREF_ENUM, "Ignore",
199 &PrefData.hardlinkMode, HardlinkModes, True},
200 + {"zeroSizedMode", "ZeroSizedMode", PREF_ENUM, "Never",
201 + &PrefData.zeroSizedMode, ZeroSizedModes, True},
204 static XrmOptionDescRec OpTable[] = {
205 @@ -2334,6 +2344,16 @@ int GetPrefHardlinkMode(void)
206 return PrefData.hardlinkMode;
209 +void SetPrefZeroSizedMode(int mode)
211 + setIntPref(&PrefData.zeroSizedMode, mode);
214 +int GetPrefZeroSizedMode(void)
216 + return PrefData.zeroSizedMode;
220 ** If preferences don't get saved, ask the user on exit whether to save
222 diff --quilt old/source/preferences.h new/source/preferences.h
223 --- old/source/preferences.h
224 +++ new/source/preferences.h
225 @@ -218,5 +218,7 @@ void SetPrefFocusOnRaise(Boolean);
226 Boolean GetPrefShowScrolltip(void);
227 void SetPrefHardlinkMode(int mode);
228 int GetPrefHardlinkMode(void);
229 +void SetPrefZeroSizedMode(int mode);
230 +int GetPrefZeroSizedMode(void);
232 #endif /* NEDIT_PREFERENCES_H_INCLUDED */
233 diff --quilt old/doc/help.etx new/doc/help.etx
234 --- old/doc/help.etx
235 +++ new/doc/help.etx
236 @@ -4144,6 +4144,9 @@ Preferences
237 ~Unlink~
238 Always make a destine copy of the file.
240 +**Remove Zero Sized Files**
241 + NEdit can delete files after close, that have a size of 0.
243 **Show Cursorline**
244 Background the current line with a colored bar. Use the color dialog to
245 change the background color.