Command_CloseAll, Command_Quit, Command_NewWindow

This commit is contained in:
krzosa
2025-12-22 11:31:06 +01:00
parent 3ba7eeb1f2
commit 6243cace7d
9 changed files with 98 additions and 48 deletions

View File

@@ -1460,21 +1460,22 @@ String GetUniqueBufferName(String working_dir, String prepend_name, String exten
void InitBuffers() { void InitBuffers() {
Allocator sys_allocator = GetSystemAllocator(); Allocator sys_allocator = GetSystemAllocator();
Scratch scratch; Scratch scratch;
Buffer *null_buffer = CreateBuffer(sys_allocator, GetUniqueBufferName(GetWorkingDir(scratch), "console")); Buffer *null_buffer = CreateBuffer(sys_allocator, GetUniqueBufferName(GetWorkingDir(scratch), "console"));
null_buffer->dont_warn_on_save = true; null_buffer->special = true;
View *null_view = CreateView(null_buffer->id); View *null_view = CreateView(null_buffer->id);
null_view->special = true;
Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID); Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID);
TraceBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "trace")); TraceBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "trace"));
TraceBuffer->dont_warn_on_save = true; TraceBuffer->special = true;
TraceView = CreateView(TraceBuffer->id); TraceView = CreateView(TraceBuffer->id);
GCInfoBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "gc")); GCInfoBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "gc"));
GCInfoBuffer->dont_warn_on_save = true; GCInfoBuffer->special = true;
GCInfoBuffer->no_history = true; GCInfoBuffer->no_history = true;
EventBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "events")); EventBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "events"));
EventBuffer->dont_warn_on_save = true;
EventBuffer->no_history = true; EventBuffer->no_history = true;
EventBuffer->special = true;
ScratchBuffer = BufferOpenFile(GetUniqueBufferName(WorkDir, "scratch")); ScratchBuffer = BufferOpenFile(GetUniqueBufferName(WorkDir, "scratch"));
ScratchBuffer->dont_warn_on_save = true; ScratchBuffer->special = true;
} }
Int ConvertUTF8ToUTF16UnixLine(String string, char16_t *buffer, Int buffer_cap) { Int ConvertUTF8ToUTF16UnixLine(String string, char16_t *buffer, Int buffer_cap) {

View File

@@ -39,8 +39,9 @@ struct Buffer {
unsigned dirty : 1; unsigned dirty : 1;
unsigned changed_on_disk : 1; unsigned changed_on_disk : 1;
unsigned garbage : 1; unsigned garbage : 1;
unsigned dont_warn_on_save : 1; unsigned dont_try_to_save_in_bulk_ops : 1;
unsigned kill : 1; unsigned kill : 1;
unsigned special : 1;
}; };
}; };

View File

@@ -193,14 +193,20 @@ bool YesNoMessageBox(const char *title, const char *msg) {
return result; return result;
} }
int SaveMessageBox(String filename) { enum SaveResult {
SAVE_CANCEL,
SAVE_NO,
SAVE_YES,
};
SaveResult SaveMessageBox(String filename) {
Scratch scratch; Scratch scratch;
const char *title = "There are unsaved changes, do you really want to quit?"; const char *title = "There are unsaved changes, do you really want to quit?";
const char *msg = Format(scratch, "Do you really want to quit? Unsaved buffer: %S", filename).data; const char *msg = Format(scratch, "Do you really want to quit? Unsaved buffer: %S", filename).data;
SDL_MessageBoxButtonData buttons[] = { SDL_MessageBoxButtonData buttons[] = {
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 2, "Save" }, { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, SAVE_YES, "Save" },
{ 0, 1, "Don't save" }, { 0, SAVE_NO, "Don't save" },
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 0, "Cancel" }, { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, SAVE_CANCEL, "Cancel" },
}; };
SDL_MessageBoxData data = {}; SDL_MessageBoxData data = {};
data.flags = SDL_MESSAGEBOX_WARNING; data.flags = SDL_MESSAGEBOX_WARNING;
@@ -211,7 +217,7 @@ int SaveMessageBox(String filename) {
int resp; int resp;
bool status = SDL_ShowMessageBox(&data, &resp); bool status = SDL_ShowMessageBox(&data, &resp);
Assert(status); Assert(status);
return resp; return (SaveResult)resp;
} }
void ReportErrorf(const char *fmt, ...) { void ReportErrorf(const char *fmt, ...) {
@@ -447,7 +453,7 @@ void ListFilesRecursive(Buffer *buffer, String dir) {
View *ExecHidden(String buffer_name, String cmd, String working_dir) { View *ExecHidden(String buffer_name, String cmd, String working_dir) {
View *view = OpenBufferView(buffer_name); View *view = OpenBufferView(buffer_name);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
buffer->dont_warn_on_save = true; buffer->dont_try_to_save_in_bulk_ops = true;
Exec(view->id, true, cmd, working_dir); Exec(view->id, true, cmd, working_dir);
return view; return view;
} }
@@ -672,16 +678,42 @@ void Command_CloseWindow() {
main.window->kill = true; main.window->kill = true;
} RegisterCommand(Command_CloseWindow, ""); } RegisterCommand(Command_CloseWindow, "");
SaveResult TrySavingBuffer(Buffer *buffer) {
if (buffer->dirty) {
SaveResult save = SaveMessageBox(buffer->name);
if (save == SAVE_CANCEL) {
return SAVE_CANCEL;
} else if (save == SAVE_YES) {
SaveBuffer(buffer);
return SAVE_YES;
} else {
return SAVE_NO;
}
}
return SAVE_YES;
}
SaveResult TrySavingAllBuffers() {
For (Buffers) {
if (it->garbage || it->special || it->dont_try_to_save_in_bulk_ops) {
continue;
}
if (it->dirty) {
SaveResult save = SaveMessageBox(it->name);
if (save == SAVE_CANCEL) {
return SAVE_CANCEL;
} else if (save == SAVE_YES) {
SaveBuffer(it);
}
}
}
return SAVE_YES;
}
void Command_Close() { void Command_Close() {
BSet main = GetBSet(LastActiveLayoutWindowID); BSet main = GetBSet(LastActiveLayoutWindowID);
if (TrySavingBuffer(main.buffer) == SAVE_CANCEL) {
if (main.buffer->dirty) { return;
int save = SaveMessageBox(main.buffer->name);
if (save == 0) {
return;
} else if (save == 2) {
SaveBuffer(main.buffer);
}
} }
main.window->active_view = FindInactiveView(); main.window->active_view = FindInactiveView();
@@ -703,6 +735,29 @@ void Command_Close() {
} }
} RegisterCommand(Command_Close, "ctrl-w"); } RegisterCommand(Command_Close, "ctrl-w");
void Command_Quit() {
if (TrySavingAllBuffers() != SAVE_CANCEL) {
AppIsRunning = false;
}
} RegisterCommand(Command_Quit, "");
void Command_CloseAll() {
if (TrySavingAllBuffers() != SAVE_CANCEL) {
For (Views) {
if (it->special) {
continue;
}
it->kill = true;
}
For (Buffers) {
if (it->special) {
continue;
}
it->kill = true;
}
}
} RegisterCommand(Command_CloseAll, "");
void Command_GotoBackward() { void Command_GotoBackward() {
BSet main = GetBSet(LastActiveLayoutWindowID); BSet main = GetBSet(LastActiveLayoutWindowID);
GotoBackward(main.window); GotoBackward(main.window);
@@ -1020,6 +1075,10 @@ void Command_FocusWindow3() {
} }
} RegisterCommand(Command_FocusWindow3, "ctrl-3"); } RegisterCommand(Command_FocusWindow3, "ctrl-3");
void Command_NewWindow() {
CreateWind();
} RegisterCommand(Command_NewWindow, "ctrl-backslash");
void Command_ClearCarets() { void Command_ClearCarets() {
BSet active = GetBSet(ActiveWindowID); BSet active = GetBSet(ActiveWindowID);
active.view->carets.len = 1; active.view->carets.len = 1;

View File

@@ -410,25 +410,7 @@ void OnCommand(Event event) {
} }
if (event.kind == EVENT_QUIT) { if (event.kind == EVENT_QUIT) {
Scratch scratch; Command_Quit();
bool do_quit = true;
For (Buffers) {
if (it->garbage || it->dont_warn_on_save) {
continue;
}
if (it->dirty) {
int save = SaveMessageBox(it->name);
if (save == 0) {
break;
} else if (save == 2) {
SaveBuffer(it);
}
}
}
if (do_quit) {
AppIsRunning = false;
}
} }
} }
@@ -469,8 +451,8 @@ void GarbageCollect() {
IterRemove(Views) { IterRemove(Views) {
IterRemovePrepare(Views); IterRemovePrepare(Views);
Buffer *buffer = GetBuffer(it->active_buffer);
if (it->kill == 0) { if (it->kill == 0) {
Buffer *buffer = GetBuffer(it->active_buffer);
if (!buffer->garbage) { if (!buffer->garbage) {
continue; continue;
} }
@@ -481,6 +463,7 @@ void GarbageCollect() {
} }
} }
RawAppendf(GCInfoBuffer, "View %d %S\n", (int)it->id.id, buffer->name);
remove_item = true; remove_item = true;
Dealloc(&it->carets); Dealloc(&it->carets);
Dealloc(sys_allocator, it); Dealloc(sys_allocator, it);
@@ -506,6 +489,7 @@ void GarbageCollect() {
} }
} }
RawAppendf(GCInfoBuffer, "Buff %d %S\n", (int)it->id.id, it->name);
remove_item = true; remove_item = true;
DeallocBuffer(it); DeallocBuffer(it);
} }
@@ -513,6 +497,7 @@ void GarbageCollect() {
IterRemove(Windows) { IterRemove(Windows) {
IterRemovePrepare(Windows); IterRemovePrepare(Windows);
if (it->kill) { if (it->kill) {
RawAppendf(GCInfoBuffer, "Wind %d %S\n", (int)it->id.id);
Dealloc(&it->goto_history); Dealloc(&it->goto_history);
Dealloc(&it->goto_redo); Dealloc(&it->goto_redo);
Dealloc(sys_allocator, it); Dealloc(sys_allocator, it);

View File

@@ -14,7 +14,7 @@ struct View {
String16 prev_search_line; String16 prev_search_line;
struct { struct {
unsigned kill : 1; unsigned kill : 1;
unsigned special : 1;
}; };
}; };

View File

@@ -2,8 +2,9 @@ void CommandWindowInit() {
Window *window = CreateWind(); Window *window = CreateWind();
CommandBarWindowID = window->id; CommandBarWindowID = window->id;
Buffer *buffer = CreateBuffer(SysAllocator, "command_bar"); Buffer *buffer = CreateBuffer(SysAllocator, "command_bar");
buffer->dont_warn_on_save = true; buffer->special = true;
View *view = CreateView(buffer->id); View *view = CreateView(buffer->id);
view->special = true;
window->active_view = view->id; window->active_view = view->id;
window->draw_line_numbers = false; window->draw_line_numbers = false;
window->draw_scrollbar = false; window->draw_scrollbar = false;

View File

@@ -9,11 +9,12 @@ void DebugWindowInit() {
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "debug")); Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "debug"));
DebugBufferID = buffer->id; DebugBufferID = buffer->id;
buffer->dont_warn_on_save = true;
buffer->no_history = true; buffer->no_history = true;
buffer->special = true;
View *view = CreateView(buffer->id); View *view = CreateView(buffer->id);
DebugViewID = view->id; view->special = true;
DebugViewID = view->id;
window->active_view = view->id; window->active_view = view->id;
window->visible = false; window->visible = false;
} }

View File

@@ -2,9 +2,10 @@ void SearchWindowInit() {
Window *window = CreateWind(); Window *window = CreateWind();
SearchBarWindowID = window->id; SearchBarWindowID = window->id;
Buffer *buffer = CreateBuffer(SysAllocator, "search_bar"); Buffer *buffer = CreateBuffer(SysAllocator, "search_bar");
buffer->dont_warn_on_save = true; buffer->special = true;
SearchBufferID = buffer->id; SearchBufferID = buffer->id;
View *view = CreateView(buffer->id); View *view = CreateView(buffer->id);
view->special = true;
SearchViewID = view->id; SearchViewID = view->id;
window->active_view = view->id; window->active_view = view->id;
window->draw_line_numbers = false; window->draw_line_numbers = false;

View File

@@ -2,8 +2,9 @@ void StatusWindowInit() {
Window *window = CreateWind(); Window *window = CreateWind();
StatusBarWindowID = window->id; StatusBarWindowID = window->id;
Buffer *buffer = CreateBuffer(SysAllocator, "status_bar"); Buffer *buffer = CreateBuffer(SysAllocator, "status_bar");
buffer->dont_warn_on_save = true; buffer->special = true;
View *view = CreateView(buffer->id); View *view = CreateView(buffer->id);
view->special = true;
window->active_view = view->id; window->active_view = view->id;
window->font = &SecondaryFont; window->font = &SecondaryFont;
window->draw_line_numbers = false; window->draw_line_numbers = false;