diff --git a/src/text_editor/buffer_history.cpp b/src/text_editor/buffer_history.cpp index 15d17a0..4b73f74 100644 --- a/src/text_editor/buffer_history.cpp +++ b/src/text_editor/buffer_history.cpp @@ -1,10 +1,12 @@ void SaveHistoryBeforeMergeCursor(Buffer *buffer, Array *stack, Array &carets) { + if (buffer->no_history) return; HistoryEntry *entry = Alloc(stack); entry->carets = TightCopy(GetSystemAllocator(), carets); } void SaveHistoryBeforeApplyEdits(Buffer *buffer, Array *stack, Array &edits) { ProfileFunction(); + if (buffer->no_history) return; HistoryEntry *entry = GetLast(*stack); Allocator sys_allocator = GetSystemAllocator(); entry->edits = TightCopy(sys_allocator, edits); @@ -39,6 +41,7 @@ void SaveHistoryBeforeApplyEdits(Buffer *buffer, Array *stack, Arr void RedoEdit(Buffer *buffer, Array *carets) { ProfileFunction(); + if (buffer->no_history) return; if (buffer->redo_stack.len <= 0) return; HistoryEntry entry = Pop(&buffer->redo_stack); @@ -57,6 +60,7 @@ void RedoEdit(Buffer *buffer, Array *carets) { void UndoEdit(Buffer *buffer, Array *carets) { ProfileFunction(); + if (buffer->no_history) return; if (buffer->undo_stack.len <= 0) return; HistoryEntry entry = Pop(&buffer->undo_stack); @@ -102,13 +106,15 @@ void AfterEdit(Buffer *buffer, Array *edits, Array *carets) { ProfileFunction(); Assert(buffer->debug_edit_phase == 2); buffer->debug_edit_phase -= 2; - HistoryEntry *entry = GetLast(buffer->undo_stack); #if BUFFER_DEBUG - Assert(entry->carets.len); - Assert(entry->edits.len); - for (Int i = 0; i < edits->len - 1; i += 1) { - Assert(edits->data[i].range.min <= edits->data[i + 1].range.min); + if (buffer->no_history == false) { + HistoryEntry *entry = GetLast(buffer->undo_stack); + Assert(entry->carets.len); + Assert(entry->edits.len); + for (Int i = 0; i < edits->len - 1; i += 1) { + Assert(edits->data[i].range.min <= edits->data[i + 1].range.min); + } } #endif diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 4d9dc29..5a67831 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -224,7 +224,7 @@ void HandleGlobalCommands() { } if (CtrlPress(KEY_P)) { if (command_window->visible) { - SetActiveWindow(LastActiveWindow); + SetActiveWindow(GetLastActiveWindow()); } else { SetActiveWindow(command_window->id); } diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 066b1f3..c08844e 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -384,7 +384,7 @@ void HandleActiveWindowBindings(Window *window) { String16 string = GetString(*buffer, range); String16 eval_result = EvalString(scratch, string); - if (Ctrl()) { + if (Ctrl() && eval_result.len) { Command_SelectEntireBuffer(&view); Command_Replace(&view, eval_result); Command_SelectRange(&view, {}); @@ -395,11 +395,11 @@ void HandleActiveWindowBindings(Window *window) { if (window->id.id == CommandWindowID.id) { Window *command_window = GetWindow(CommandWindowID); command_window->visible = !command_window->visible; - ActiveWindow = LastActiveWindow; + ActiveWindow = GetLastActiveWindow(); } { - Window *window = GetWindow(LastActiveWindow); + Window *window = GetWindow(GetLastActiveWindow()); View *view = GetView(window->active_view); SetActiveWindow(window->id); Command_Replace(view, eval_result); @@ -574,13 +574,15 @@ void ChangeActiveWindowAndScroll(Array &order) { void ReplaceInfobarData() { Window *window = GetWindow(InfoBarWindowID); + if (IsActive(window)) return; + View *view = GetView(window->active_view); Buffer *buffer = GetBuffer(view->buffer_id); XY xy = {}; String name = {}; { - Window *window = GetActiveWindow(); + Window *window = GetWindow(GetLastActiveWindow()); View *view = GetActiveView(window); Buffer *buffer = GetBuffer(view->buffer_id); Scratch scratch; @@ -589,10 +591,10 @@ void ReplaceInfobarData() { name = buffer->name; } - Scratch scratch; - String error = {}; - if (InfoBarErrorMessage.len) error = Format(scratch, "| error: %.*s", FmtString(InfoBarErrorMessage)); - String s = Format(scratch, "line: %5lld col: %5lld name: %.*s %.*s", (long long)xy.line + 1ll, (long long)xy.col + 1ll, FmtString(name), FmtString(error)); + Scratch scratch; + String error = {}; + String s = Format(scratch, "line: %5lld col: %5lld name: %.*s", (long long)xy.line + 1ll, (long long)xy.col + 1ll, FmtString(name)); String16 string = ToString16(scratch, s); ReplaceText(buffer, {0, buffer->len}, string); + Command_SelectRange(view, {}); } \ No newline at end of file diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 5a99232..788b49b 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -3,7 +3,7 @@ String16 LuaCommandResult = {}; int LuaOpenFile(lua_State *L) { const char *text = luaL_checkstring(L, 1); - Window *window = GetWindow(LastActiveWindow); + Window *window = GetWindow(GetLastActiveWindow()); View *view = ViewOpenFile(window, text); SetActiveWindow(window->id); return 0; // number of results @@ -71,13 +71,6 @@ void InitLua() { lua_setglobal(LuaState, "list_windows"); } -// @todo: also log to buffer or something -void SetInfoBarErrorMessage(String string) { - Allocator sys = GetSystemAllocator(); - if (InfoBarErrorMessage.data) Dealloc(sys, &InfoBarErrorMessage.data); - if (string.len) InfoBarErrorMessage = Copy(sys, string); -} - String16 EvalString(Allocator allocator, String16 string16) { Scratch scratch((Arena *)allocator.object); String string = ToString(scratch, string16); diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 9f490b8..261bad6 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -50,11 +50,21 @@ View *CreateView(BufferID buffer_id) { return w; } +inline bool IsSpecial(WindowID window) { + bool result = window.id == CommandWindowID.id || window.id == InfoBarWindowID.id; + return result; +} + +WindowID GetLastActiveWindow() { + For(IterateInReverse(&WindowSwitchHistory)) { + if (!IsSpecial(it)) return it; + } + return NullWindowID; +} + void SetActiveWindow(WindowID window) { - if (LastFrameIDWhenSwitchedActiveWindow != FrameID) { - if (ActiveWindow.id != CommandWindowID.id) { - LastActiveWindow = ActiveWindow; - } + if (window.id != ActiveWindow.id && LastFrameIDWhenSwitchedActiveWindow != FrameID) { + Add(&WindowSwitchHistory, window); ActiveWindow = window; LastFrameIDWhenSwitchedActiveWindow = FrameID; } diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 6b0091f..f0c1dc5 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -116,7 +116,8 @@ int main(void) { w->draw_scrollbar = false; w->draw_line_numbers = false; Buffer *b = CreateBuffer(sys_allocator, "*infobar*"); - View *v = CreateView(b->id); + b->no_history = true; + View *v = CreateView(b->id); AddView(w, v->id); InfoBarWindowID = w->id; } diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 2a919e6..706d202 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -35,6 +35,7 @@ struct Buffer { Array undo_stack; Array redo_stack; int debug_edit_phase; + bool no_history; }; struct View { @@ -88,10 +89,9 @@ WindowID CommandWindowID = {0}; WindowID InfoBarWindowID = {0}; // WindowID ExecBarWindowID = {0}; -Array Buffers = {}; -Array Views = {}; -Array Windows = {}; -WindowID ActiveWindow = {}; +Array Buffers = {}; +Array Views = {}; +Array Windows = {}; float MenuFontSize; Font MenuFont; @@ -105,9 +105,9 @@ Int FontCharSpacing; Int FrameID; Int LastFrameIDWhenScrolled; -WindowID LastActiveWindow; -Int LastFrameIDWhenSwitchedActiveWindow; -String InfoBarErrorMessage; +Int LastFrameIDWhenSwitchedActiveWindow; +Array WindowSwitchHistory; +WindowID ActiveWindow = {}; String16 EvalString(Allocator allocator, String16 string16); Rect2I GetVisibleCells(Window &window);