Skip to content

Commit d414ec9

Browse files
augiedoggieKapiX
authored andcommitted
EditorWindow: avoid creating empty new files
This change adds a sort of "phantom mode" where a filename is passed on the command line that doesn't exist yet but the editor treats it as an existing file. Generic error checking was added to SaveFile() to catch other types of errors. For the case of phantom mode, this includes attempting trying to run something like `Koder /system/foo.txt` and then trying to save the file to a read-only location.
1 parent faa98be commit d414ec9

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

locales/en.catkeys

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1 English x-vnd.KapiX-Koder 2781821189
1+
1 English x-vnd.KapiX-Koder 3292494141
22
Something wrong has happened while opening the configuration file. Your personal settings will not be %s%. Preferences Something wrong has happened while opening the configuration file. Your personal settings will not be %s%.
33
Access denied EditorWindow Access denied
44
Line endings EditorWindow Line endings
@@ -36,6 +36,7 @@ There is not enough memory available to %s% the configuration file. Try closing
3636
Folds AppPreferencesWindow Folds
3737
Visual AppPreferencesWindow Visual
3838
Wrap around FindWindow Wrap around
39+
Save error EditorWindow Save error
3940
Highlight current line AppPreferencesWindow Highlight current line
4041
Left margin AppPreferencesWindow Left margin
4142
Go to line… EditorWindow Go to line…
@@ -162,6 +163,7 @@ Highlight trailing whitespace AppPreferencesWindow Highlight trailing whitespac
162163
Up to the next/previous non-empty line AppPreferencesWindow Up to the next/previous non-empty line
163164
Show indentation guides AppPreferencesWindow Show indentation guides
164165
Koder preferences AppPreferencesWindow Koder preferences
166+
An error occurred while attempting to save the file. EditorWindow An error occurred while attempting to save the file.
165167
Don't save QuitAlert Don't save
166168
Replace FindWindow Replace
167169
[read-only] EditorWindow [read-only]

src/editor/EditorWindow.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,20 @@ EditorWindow::OpenFile(const entry_ref* ref, Sci_Position line, Sci_Position col
294294
}
295295

296296
BEntry entry(ref);
297-
uint32 openMode = B_READ_ONLY;
298-
if(!entry.Exists()) {
299-
// TODO: alert/phantom mode?
300-
openMode |= B_CREATE_FILE;
297+
298+
File file(&entry, B_READ_ONLY);
299+
if(entry.Exists()) {
300+
fReadOnly = !File::CanWrite(&file);
301+
file.Monitor(true, this);
302+
file.GetModificationTime(&fOpenedFileModificationTime);
303+
fEditor->SetText(file.Read().data());
304+
} else {
305+
// TODO check if we have directory permissions to create a new file?
306+
fReadOnly = false;
301307
}
302308

303-
File file(&entry, openMode);
304-
file.Monitor(true, this);
305-
file.GetModificationTime(&fOpenedFileModificationTime);
306309
fModifiedOutside = false;
307310

308-
fEditor->SetText(file.Read().data());
309-
310311
fEditor->SendMessage(SCI_SETSAVEPOINT);
311312
fEditor->SendMessage(SCI_EMPTYUNDOBUFFER);
312313

@@ -325,7 +326,6 @@ EditorWindow::OpenFile(const entry_ref* ref, Sci_Position line, Sci_Position col
325326
entry.GetName(name);
326327
_SetLanguageByFilename(name);
327328

328-
fReadOnly = !File::CanWrite(&file);
329329
fEditor->SetReadOnly(fReadOnly);
330330
fEditor->SetRef(*ref);
331331

@@ -381,10 +381,15 @@ EditorWindow::SaveFile(entry_ref* ref)
381381

382382
// TODO error checking
383383
File file(path.c_str(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
384-
if(file.InitCheck() == B_PERMISSION_DENIED) {
384+
status_t result = file.InitCheck();
385+
if(result == B_PERMISSION_DENIED) {
385386
OKAlert(B_TRANSLATE("Access denied"), B_TRANSLATE("You don't have "
386387
"sufficient permissions to edit this file."), B_STOP_ALERT);
387388
return;
389+
} else if(result != B_OK) {
390+
OKAlert(B_TRANSLATE("Save error"), B_TRANSLATE("An error occurred "
391+
"while attempting to save the file."), B_STOP_ALERT);
392+
return;
388393
}
389394
file.Monitor(false, this);
390395

0 commit comments

Comments
 (0)