Refactoring Command_s, NextWindowID

This commit is contained in:
Krzosa Karol
2025-12-08 09:16:31 +01:00
parent 20207e6040
commit 9d29a1c187
11 changed files with 275 additions and 234 deletions

View File

@@ -68,13 +68,13 @@ void JumpGarbageBuffer(BSet *set, String buffer_name = "") {
set->buffer->garbage = true;
}
void Command_BeginJump(BSet *set, BufferID buffer_id = NullBufferID) {
void BeginJump(BSet *set, BufferID buffer_id = NullBufferID) {
CheckpointBeforeGoto(set->window);
set->buffer = GetBuffer(buffer_id);
set->view = WindowOpenBufferView(set->window, set->buffer->name);
}
void Command_EndJump(BSet set) {
void EndJump(BSet set) {
Int pos = XYToPos(set.buffer, {0, set.buffer->line_starts.len - 1});
set.view->carets[0] = MakeCaret(pos);
UpdateScroll(set.window, true);
@@ -100,7 +100,7 @@ Int ScreenSpaceToBufferPosErrorOutOfBounds(Window *window, View *view, Buffer *b
void MouseLoadWord(Event event, String meta = "") {
Vec2I mouse = MouseVec2I();
BSet active = GetActiveSet();
BSet active = GetBSet(ActiveWindowID);
bool mouse_in_document = AreOverlapping(mouse, active.window->document_rect);
if (mouse_in_document) {
@@ -123,13 +123,12 @@ View *GetViewForFixingWhenBufferCommand(Buffer *buffer, bool *is_active = NULL)
*is_active = false;
}
Window *active_window = GetWindow(ActiveWindow);
View *active_view = GetView(active_window->active_view);
if (active_view->active_buffer == buffer->id) {
BSet active = GetBSet(ActiveWindowID);
if (active.buffer->id == buffer->id) {
if (is_active) {
*is_active = true;
}
return active_view;
return active.view;
}
For(Views) {
@@ -152,15 +151,15 @@ void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string) {
Array<Caret> carets = Copy(GetSystemAllocator(), view->carets);
Scratch scratch;
Command_SelectRangeOneCursor(view, range);
Command_ReplaceEx(scratch, view, string);
SelectRange(view, range);
ReplaceEx(scratch, view, string);
Dealloc(&view->carets);
view->carets = carets;
}
// @todo: revamp interface since it scrolls ALL VIEWS??? or maybe not??
void Command_Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line) {
void Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line) {
Scratch scratch;
Buffer *buffer = GetBuffer(view->active_buffer);
@@ -191,8 +190,8 @@ void Command_Append(View *view, String16 string, bool scroll_to_end_if_cursor_on
Add(&view_info, vi);
}
Command_SelectRangeOneCursor(view, GetBufferEndAsRange(buffer));
Command_Replace(view, string);
SelectRange(view, GetBufferEndAsRange(buffer));
Replace(view, string);
For (view_info) {
if (it.scroll_to_end) {
@@ -204,16 +203,16 @@ void Command_Append(View *view, String16 string, bool scroll_to_end_if_cursor_on
}
}
void Command_Append(View *view, String string, bool scroll_to_end_if_cursor_on_last_line) {
void Append(View *view, String string, bool scroll_to_end_if_cursor_on_last_line) {
Scratch scratch;
String16 string16 = ToString16(scratch, string);
Command_Append(view, string16, scroll_to_end_if_cursor_on_last_line);
Append(view, string16, scroll_to_end_if_cursor_on_last_line);
}
void Command_Appendf(View *view, const char *fmt, ...) {
void Appendf(View *view, const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
Command_Append(view, string, true);
Append(view, string, true);
}
void ReportErrorf(const char *fmt, ...) {
@@ -222,8 +221,8 @@ void ReportErrorf(const char *fmt, ...) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", string.data, NULL);
View *view = GetView(NullViewID);
if (view) {
Command_Appendf(view, "%S\n", string);
ActiveWindow = NullWindowID;
Appendf(view, "%S\n", string);
NextActiveWindowID = NullWindowID;
}
}
@@ -231,23 +230,23 @@ void ReportConsolef(const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
View *view = GetView(NullViewID);
Command_Appendf(view, "%S\n", string);
Appendf(view, "%S\n", string);
}
void ReportWarningf(const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
View *null_view = GetView(NullViewID);
Command_Appendf(null_view, "%S\n", string);
Appendf(null_view, "%S\n", string);
}
void ReportDebugf(const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
Command_Appendf(TraceView, "%S\n", string);
Appendf(TraceView, "%S\n", string);
}
void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = false) {
void MoveCursorByPageSize(Window *window, int direction, bool shift = false) {
Assert(direction == DIR_UP || direction == DIR_DOWN);
BSet set = GetBSet(window);
@@ -274,7 +273,7 @@ void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = f
}
}
void Command_MoveCursorsToSide(View *view, int direction, bool shift = false) {
void MoveCursorToSide(View *view, int direction, bool shift = false) {
Assert(direction == DIR_LEFT || direction == DIR_RIGHT);
Buffer *buffer = GetBuffer(view->active_buffer);
@@ -400,7 +399,7 @@ Caret MoveCaret(Buffer *buffer, Caret it, int direction, bool ctrl = false, bool
return it;
}
void Command_Move(View *view, int direction, bool ctrl = false, bool shift = false) {
void MoveCarets(View *view, int direction, bool ctrl = false, bool shift = false) {
Assert(direction < DIR_COUNT);
Buffer *buffer = GetBuffer(view->active_buffer);
For(view->carets) {
@@ -408,7 +407,7 @@ void Command_Move(View *view, int direction, bool ctrl = false, bool shift = fal
}
}
void Command_MoveLine(View *view, int direction) {
void MoveCaretsLine(View *view, int direction) {
Assert(direction == DIR_DOWN || direction == DIR_UP);
Scratch scratch;
@@ -467,7 +466,7 @@ void Command_MoveLine(View *view, int direction) {
}
}
Array<Edit> Command_ReplaceEx(Allocator scratch, View *view, String16 string) {
Array<Edit> ReplaceEx(Allocator scratch, View *view, String16 string) {
Buffer *buffer = GetBuffer(view->active_buffer);
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
MergeCarets(buffer, &view->carets);
@@ -476,9 +475,9 @@ Array<Edit> Command_ReplaceEx(Allocator scratch, View *view, String16 string) {
return edits;
}
void Command_Replace(View *view, String16 string) {
void Replace(View *view, String16 string) {
Scratch scratch;
Command_ReplaceEx(scratch, view, string);
ReplaceEx(scratch, view, string);
}
void Command_DuplicateLine(View *view, int direction) {
@@ -702,10 +701,12 @@ void SaveBuffer(Buffer *buffer) {
ReportWarningf("Failed to save file with name: %S", buffer->name);
}
}
void Command_Save() {
BSet set = GetLastActiveLayoutSet();
SaveBuffer(set.buffer);
BSet active = GetBSet(ActiveWindowID);
SaveBuffer(active.buffer);
}
int Lua_Save(lua_State *L) {
Command_Save();
return 0;
@@ -718,6 +719,7 @@ void Command_SaveAll() {
}
}
}
int Lua_SaveAll(lua_State *L) {
Command_SaveAll();
return 0;
@@ -734,7 +736,7 @@ void Command_KillSelectedLines(View *view) {
it.range.min = GetFullLineStart(buffer, it.range.min);
it.range.min -= Clamp(eof, (Int)0, buffer->len);
}
Command_Replace(view, u"");
Replace(view, u"");
}
void EncloseLine(View *view) {
@@ -840,18 +842,18 @@ void Command_CreateCursorVertical(View *view, int direction) {
MergeCarets(buffer, &view->carets);
}
void Command_SelectRangeOneCursor(View *view, Caret caret) {
void SelectRange(View *view, Caret caret) {
view->carets.len = 1;
view->carets[0] = caret;
}
void Command_SelectRangeOneCursor(View *view, Range range) {
Command_SelectRangeOneCursor(view, MakeCaret(range.min, range.max));
void SelectRange(View *view, Range range) {
SelectRange(view, MakeCaret(range.min, range.max));
}
void Command_SelectEntireBuffer(View *view) {
void SelectEntireBuffer(View *view) {
Buffer *buffer = GetBuffer(view->active_buffer);
Command_SelectRangeOneCursor(view, GetRange(buffer));
SelectRange(view, GetRange(buffer));
}
Caret FindPrev(Buffer *buffer, String16 needle, Caret caret) {
@@ -890,7 +892,7 @@ Caret FindNext(Buffer *buffer, String16 needle, Caret caret) {
return result;
}
void Command_IdentedNewLine(View *view) {
void IdentedNewLine(View *view) {
Buffer *buffer = GetBuffer(view->active_buffer);
Scratch scratch;
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
@@ -911,7 +913,7 @@ void Command_Find(View *seek_view, String16 needle, bool forward = true) {
Caret caret = seek_view->carets[0];
if (forward) caret = FindNext(seek_buffer, needle, caret);
if (!forward) caret = FindPrev(seek_buffer, needle, caret);
Command_SelectRangeOneCursor(seek_view, caret);
SelectRange(seek_view, caret);
IF_DEBUG(AssertRanges(seek_view->carets));
}
@@ -967,9 +969,9 @@ void Command_FuzzySort(View *view, String16 needle) {
RawReplaceText(temp_buffer, GetBufferEndAsRange(temp_buffer), u"\n");
}
Command_SelectEntireBuffer(view);
Command_Replace(view, GetString(temp_buffer));
Command_SelectRangeOneCursor(view, MakeRange(0));
SelectEntireBuffer(view);
Replace(view, GetString(temp_buffer));
SelectRange(view, MakeRange(0));
}
void ReopenBuffer(Buffer *buffer) {
@@ -987,9 +989,9 @@ void ReopenBuffer(Buffer *buffer) {
}
void Command_Reopen() {
BSet set = GetLastActiveLayoutSet();
ReopenBuffer(set.buffer);
ActiveWindow = set.window->id;
BSet main = GetBSet(LastActiveLayoutWindowID);
ReopenBuffer(main.buffer);
NextActiveWindowID = main.window->id;
}
int Lua_Reopen(lua_State *L) {
@@ -1015,7 +1017,7 @@ void New(Window *window, String name = "") {
}
void Command_New(String name = "") {
BSet main = GetLastActiveLayoutSet();
BSet main = GetBSet(LastActiveLayoutWindowID);
New(main.window, name);
}
@@ -1048,7 +1050,7 @@ void NewDir(Window *window, String name = "") {
int Lua_NewDir(lua_State *L) {
String name = lua_tostring(L, 1);
lua_pop(L, 1);
BSet main = GetLastActiveLayoutSet();
BSet main = GetBSet(LastActiveLayoutWindowID);
NewDir(main.window, name);
return 0;
}
@@ -1094,12 +1096,12 @@ void ListFilesRecursive(Buffer *buffer, String dir) {
}
void Command_ListCode(String dir = WorkDir) {
BSet main = GetLastActiveLayoutSet();
BSet main = GetBSet(LastActiveLayoutWindowID);
JumpGarbageBuffer(&main);
ListFilesRecursive(main.buffer, dir);
main.view->fuzzy_search = true;
main.view->update_scroll = true;
Command_SelectRangeOneCursor(main.view, GetBufferEndAsRange(main.buffer));
SelectRange(main.view, GetBufferEndAsRange(main.buffer));
}
int Lua_ListCode(lua_State *L) {
@@ -1119,17 +1121,19 @@ View *Command_ExecHidden(String buffer_name, String cmd, String working_dir) {
}
BSet Command_Exec(String cmd, String working_dir, bool set_active = true) {
BSet set = GetLastActiveLayoutSet();
if (set_active) ActiveWindow = set.window->id;
JumpGarbageBuffer(&set);
Exec(set.view->id, true, cmd, working_dir);
return set;
BSet main = GetBSet(LastActiveLayoutWindowID);
if (set_active) {
NextActiveWindowID = main.window->id;
}
JumpGarbageBuffer(&main);
Exec(main.view->id, true, cmd, working_dir);
return main;
}
int Lua_C(lua_State *L) {
String string = lua_tostring(L, 1);
lua_pop(L, 1);
Command_Exec(string, Command_GetMainDir());
Command_Exec(string, GetMainDir());
return 0;
}
@@ -1140,13 +1144,13 @@ BSet Command_Open(Window *window, String path, String meta, bool set_active = tr
OnOpenResult ores = CallOnOpen(scratch, path, meta);
if (ores.kind == "text") {
if (set_active) {
ActiveWindow = set.window->id;
NextActiveWindowID = set.window->id;
}
if (IsDir(ores.file_path)) {
JumpGarbageBuffer(&set, GetUniqueBufferName(ores.file_path, "temp", ".dirlisting"));
Command_Appendf(set.view, "..\n");
Appendf(set.view, "..\n");
for (FileIter it = IterateFiles(scratch, ores.file_path); IsValid(it); Advance(&it)) {
Command_Appendf(set.view, "%S\n", it.filename);
Appendf(set.view, "%S\n", it.filename);
}
} else {
CheckpointBeforeGoto(set.window);
@@ -1161,7 +1165,7 @@ BSet Command_Open(Window *window, String path, String meta, bool set_active = tr
UpdateScroll(set.window, true);
} else if (ores.kind == "exec") {
if (set_active) {
ActiveWindow = set.window->id;
NextActiveWindowID = set.window->id;
}
JumpGarbageBuffer(&set);
Exec(set.view->id, true, ores.cmd, ores.working_dir);
@@ -1179,7 +1183,7 @@ BSet Command_Open(Window *window, String path, String meta, bool set_active = tr
}
BSet Command_Open(String path, String meta) {
BSet main = GetLastActiveLayoutSet();
BSet main = GetBSet(LastActiveLayoutWindowID);
main = Command_Open(main.window, path, meta);
return main;
}
@@ -1206,7 +1210,7 @@ int Lua_Cmd(lua_State *L) {
String working_dir = lua_tostring(L, -1);
lua_pop(L, 1);
if (working_dir == "") {
working_dir = Command_GetMainDir();
working_dir = GetMainDir();
}
lua_getfield(L, -1, "cmd");
@@ -1218,52 +1222,68 @@ int Lua_Cmd(lua_State *L) {
String kind = lua_tostring(L, -1);
lua_pop(L, 1);
BSet main = GetLastActiveLayoutSet();
BSet main = GetBSet(LastActiveLayoutWindowID);
if (kind == "console") {
BSet set = GetConsoleSet();
main.window->active_goto_list = set.view->id;
main.window->goto_list_pos = set.buffer->len;
Command_SelectRangeOneCursor(set.view, MakeRange(set.buffer->len));
Command_BeginJump(&set);
SelectRange(set.view, MakeRange(set.buffer->len));
BeginJump(&set);
Exec(set.view->id, true, cmd, working_dir);
Command_EndJump(set);
EndJump(set);
} else if (kind == "fuzzy") {
JumpGarbageBuffer(&main);
Exec(main.view->id, true, cmd, working_dir);
main.view->fuzzy_search = true;
ActiveWindow = main.window->id;
NextActiveWindowID = main.window->id;
} else {
JumpGarbageBuffer(&main);
main.window->active_goto_list = main.view->id;
main.window->goto_list_pos = 0;
Exec(main.view->id, true, cmd, working_dir);
ActiveWindow = main.window->id;
NextActiveWindowID = main.window->id;
}
return 0;
}
void Command_ShowBufferList() {
BSet main = GetBSet(CommandBarWindowID);
main.window->visible = true;
ActiveWindow = main.window->id;
ResetBuffer(main.buffer);
BSet command_bar = GetBSet(CommandBarWindowID);
command_bar.window->visible = true;
NextActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer);
For(Buffers) {
RawAppendf(main.buffer, "%-80S || %S\n", SkipToLastSlash(it->name), it->name);
RawAppendf(command_bar.buffer, "%-80S || %S\n", SkipToLastSlash(it->name), it->name);
}
main.view->fuzzy_search = true;
main.view->update_scroll = true;
Command_SelectRangeOneCursor(main.view, GetBufferEndAsRange(main.buffer));
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer));
}
void Command_ShowCommandList() {
BSet command_bar = GetBSet(CommandBarWindowID);
command_bar.window->visible = true;
NextActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer);
for (int i = 0; LuaFunctions[i].name != NULL; i += 1) {
Appendf(command_bar.view, "%s()\n ", LuaFunctions[i].name);
}
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer));
}
void Command_ListViews() {
BSet main = GetLastActiveLayoutSet();
ActiveWindow = main.window->id;
JumpGarbageBuffer(&main);
BSet command_bar = GetBSet(CommandBarWindowID);
command_bar.window->visible = true;
NextActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer);
For(Views) {
Buffer *buffer = GetBuffer(it->active_buffer);
Command_Appendf(main.view, "%d %S\n", (int)it->id.id, buffer->name);
Appendf(command_bar.view, "%d %S\n", (int)it->id.id, buffer->name);
}
command_bar.view->fuzzy_search = true;
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer));
}
int Lua_ListViews(lua_State *L) {
@@ -1298,34 +1318,47 @@ void SetProjectFile(Buffer *buffer) {
LuaProjectBuffer->user_change_id = -1;
}
void Command_SetProjectFile() {
BSet main = GetBSet(LastActiveLayoutWindowID);
SetProjectFile(main.buffer);
}
void Command_SetWorkDir() {
String dir = lua_tostring(LuaState, -1);
if (dir.len == 0) {
BSet main = GetBSet(LastActiveLayoutWindowID);
WorkDir = ChopLastSlash(main.buffer->name);
} else {
WorkDir = dir;
}
}
void Command_SetProject() {
Command_SetWorkDir();
Command_SetProjectFile();
}
int Lua_SetProjectFile(lua_State *L) {
BSet set = GetLastActiveLayoutSet();
SetProjectFile(set.buffer);
Command_SetProjectFile();
return 0;
}
int Lua_SetWorkDir(lua_State *L) {
String dir = lua_tostring(L, -1);
if (dir.len == 0) {
BSet set = GetLastActiveLayoutSet();
WorkDir = ChopLastSlash(set.buffer->name);
} else {
WorkDir = dir;
}
Command_SetWorkDir();
return 0;
}
int Lua_ListCommands(lua_State *L) {
BSet main = GetLastActiveLayoutSet();
Command_BeginJump(&main);
BSet main = GetBSet(LastActiveLayoutWindowID);
BeginJump(&main);
for (int i = 0; LuaFunctions[i].name != NULL; i += 1) {
Command_Appendf(main.view, "%20s() ", LuaFunctions[i].name);
Appendf(main.view, "%20s() ", LuaFunctions[i].name);
if (((i + 1) % 6) == 0) {
Command_Appendf(main.view, "\n");
Appendf(main.view, "\n");
}
}
Command_EndJump(main);
ActiveWindow = main.window->id;
EndJump(main);
NextActiveWindowID = main.window->id;
return 0;
}
@@ -1373,17 +1406,42 @@ Vec2I GetSideOfWindow(Window *window, int direction) {
}
Window *SwitchWindow(int direction) {
Window *window = GetWindow(ActiveWindow);
Window *window = GetWindow(ActiveWindowID);
Vec2I p = GetSideOfWindow(window, direction);
Window *result = GetOverlappingWindow(p, window);
return result;
}
String16 FetchLoadWord(void) {
BSet active = GetActiveSet();
BSet active = GetBSet(ActiveWindowID);
Caret caret = active.view->carets[0];
Range range = caret.range;
if (GetSize(caret.range) == 0) range = EncloseLoadWord(active.buffer, GetFront(caret));
String16 string = GetString(active.buffer, range);
return string;
}
void Command_ToggleDebug() {
Window *window = GetWindow(DebugWindowID);
window->visible = !window->visible;
}
void Command_KillProcess() {
BSet main = GetBSet(LastActiveLayoutWindowID);
KillProcess(main.view);
}
int Lua_KillProcess(lua_State *L) {
Command_KillProcess();
return 0;
}
void Command_KillWindow() {
BSet main = GetBSet(LastActiveLayoutWindowID);
main.window->kill = true;
}
int Lua_KillWindow(lua_State *L) {
Command_KillWindow();
return 0;
}

View File

@@ -117,7 +117,7 @@ void OnCommand(Event event) {
view->scroll.y = (Int)(v * (double)s.line_count * (double)window->font->line_spacing);
}
if (DocumentSelected != ActiveWindow) {
if (DocumentSelected != ActiveWindowID) {
DocumentSelected.id = -1;
} else if (IsDocumentSelectionValid() && MouseUp()) {
Assert(ScrollbarSelected.id == -1);
@@ -179,17 +179,19 @@ void OnCommand(Event event) {
}
bool mouse_in_document = AreOverlapping(mouse, it->document_rect);
if (mouse_in_document) {
ActiveWindow = it->id;
NextActiveWindowID = it->id;
break;
}
}
}
if (Mouse(X2)) {
GotoForward(GetLastActiveLayoutSet().window);
BSet main = GetBSet(LastActiveLayoutWindowID);
GotoForward(main.window);
}
if (Mouse(X1)) {
GotoBackward(GetLastActiveLayoutSet().window);
BSet main = GetBSet(LastActiveLayoutWindowID);
GotoBackward(main.window);
}
if (Ctrl() && Shift() && Mouse(RIGHT)) {
@@ -199,8 +201,8 @@ void OnCommand(Event event) {
} else if (Alt() && Mouse(RIGHT)) {
} else if (Mouse(RIGHT)) {
Vec2I mouse = MouseVec2I();
BSet active = GetActiveSet();
Vec2I mouse = MouseVec2I();
BSet active = GetBSet(ActiveWindowID);
bool mouse_in_document = AreOverlapping(mouse, active.window->document_rect);
if (mouse_in_document) {
Int p = ScreenSpaceToBufferPos(active.window, active.view, active.buffer, mouse);
@@ -235,7 +237,7 @@ void OnCommand(Event event) {
Assert(ScrollbarSelected.id == -1);
Assert(DocumentSelected.id == -1);
BSet active = GetActiveSet();
BSet active = GetBSet(NextActiveWindowID); // using next to make sure mouse works on first click after switching the window
bool mouse_in_document = AreOverlapping(mouse, active.window->document_rect);
bool mouse_in_line_numbers = AreOverlapping(mouse, active.window->line_numbers_rect);
if (mouse_in_document || mouse_in_line_numbers) {
@@ -297,27 +299,27 @@ void OnCommand(Event event) {
}
if (CtrlPress(SDLK_W)) {
BSet main = GetLastActiveLayoutSet();
main.window->kill = true;
Command_KillWindow();
}
if (CtrlAltPress(SDLK_P)) {
} else if (CtrlShiftPress(SDLK_P)) {
Command_ShowCommandList();
} else if (CtrlPress(SDLK_P)) {
Command_ShowBufferList();
}
if (CtrlPress(SDLK_0)) {
Window *window = GetWindow(DebugWindowID);
window->visible = !window->visible;
Command_ToggleDebug();
}
if (CtrlPress(SDLK_1)) {
ActiveWindow = GetOverlappingWindow({0,0}, GetWindow(ActiveWindow))->id;
NextActiveWindowID = GetOverlappingWindow({0,0}, GetWindow(ActiveWindowID))->id;
}
if (CtrlPress(SDLK_2)) {
Window *first = GetOverlappingWindow({0,0}, GetWindow(ActiveWindow));
Window *first = GetOverlappingWindow({0,0}, GetWindow(ActiveWindowID));
Vec2I p = GetSideOfWindow(first, DIR_RIGHT);
ActiveWindow = GetOverlappingWindow(p, GetWindow(ActiveWindow))->id;
NextActiveWindowID = GetOverlappingWindow(p, GetWindow(ActiveWindowID))->id;
}
if (CtrlPress(SDLK_3)) {
Window *first = GetOverlappingWindow({0,0});
@@ -326,14 +328,14 @@ void OnCommand(Event event) {
if (second) {
Window *third = GetOverlappingWindow(GetSideOfWindow(second, DIR_RIGHT));
if (third) {
ActiveWindow = third->id;
NextActiveWindowID = third->id;
}
}
}
}
BSet main = GetLastActiveLayoutSet();
BSet active = GetActiveSet();
BSet main = GetBSet(LastActiveLayoutWindowID);
BSet active = GetBSet(ActiveWindowID);
Int buffer_change_id = active.buffer->change_id;
bool skip = CallOnCommand(&event);
@@ -364,15 +366,15 @@ void OnCommand(Event event) {
} else if (AltShiftPress(SDLK_DOWN)) {
Command_CreateCursorVertical(active.view, DIR_DOWN);
} else if (CtrlShiftPress(SDLK_DOWN)) {
Command_Move(active.view, DIR_DOWN, CTRL_PRESSED, SHIFT_PRESS);
MoveCarets(active.view, DIR_DOWN, CTRL_PRESSED, SHIFT_PRESS);
} else if (AltPress(SDLK_DOWN)) {
Command_MoveLine(active.view, DIR_DOWN);
MoveCaretsLine(active.view, DIR_DOWN);
} else if (CtrlPress(SDLK_DOWN)) {
Command_Move(active.view, DIR_DOWN, CTRL_PRESSED);
MoveCarets(active.view, DIR_DOWN, CTRL_PRESSED);
} else if (ShiftPress(SDLK_DOWN)) {
Command_Move(active.view, DIR_DOWN, false, SHIFT_PRESS);
MoveCarets(active.view, DIR_DOWN, false, SHIFT_PRESS);
} else if (Press(SDLK_DOWN)) {
Command_Move(active.view, DIR_DOWN);
MoveCarets(active.view, DIR_DOWN);
}
if (CtrlAltPress(SDLK_UP)) {
@@ -380,39 +382,39 @@ void OnCommand(Event event) {
} else if (AltShiftPress(SDLK_UP)) {
Command_CreateCursorVertical(active.view, DIR_UP);
} else if (CtrlShiftPress(SDLK_UP)) {
Command_Move(active.view, DIR_UP, CTRL_PRESSED, SHIFT_PRESS);
MoveCarets(active.view, DIR_UP, CTRL_PRESSED, SHIFT_PRESS);
} else if (AltPress(SDLK_UP)) {
Command_MoveLine(active.view, DIR_UP);
MoveCaretsLine(active.view, DIR_UP);
} else if (CtrlPress(SDLK_UP)) {
Command_Move(active.view, DIR_UP, CTRL_PRESSED);
MoveCarets(active.view, DIR_UP, CTRL_PRESSED);
} else if (ShiftPress(SDLK_UP)) {
Command_Move(active.view, DIR_UP, false, SHIFT_PRESS);
MoveCarets(active.view, DIR_UP, false, SHIFT_PRESS);
} else if (Press(SDLK_UP)) {
Command_Move(active.view, DIR_UP);
MoveCarets(active.view, DIR_UP);
}
if (CtrlShiftPress(SDLK_LEFT)) {
Command_Move(active.view, DIR_LEFT, CTRL_PRESSED, SHIFT_PRESS);
MoveCarets(active.view, DIR_LEFT, CTRL_PRESSED, SHIFT_PRESS);
} else if (CtrlPress(SDLK_LEFT)) {
Command_Move(active.view, DIR_LEFT, CTRL_PRESSED);
MoveCarets(active.view, DIR_LEFT, CTRL_PRESSED);
} else if (ShiftPress(SDLK_LEFT)) {
Command_Move(active.view, DIR_LEFT, false, SHIFT_PRESS);
MoveCarets(active.view, DIR_LEFT, false, SHIFT_PRESS);
} else if (AltPress(SDLK_LEFT)) {
ActiveWindow = SwitchWindow(DIR_LEFT)->id;
NextActiveWindowID = SwitchWindow(DIR_LEFT)->id;
} else if (Press(SDLK_LEFT)) {
Command_Move(active.view, DIR_LEFT);
MoveCarets(active.view, DIR_LEFT);
}
if (CtrlShiftPress(SDLK_RIGHT)) {
Command_Move(active.view, DIR_RIGHT, CTRL_PRESSED, SHIFT_PRESS);
MoveCarets(active.view, DIR_RIGHT, CTRL_PRESSED, SHIFT_PRESS);
} else if (CtrlPress(SDLK_RIGHT)) {
Command_Move(active.view, DIR_RIGHT, CTRL_PRESSED);
MoveCarets(active.view, DIR_RIGHT, CTRL_PRESSED);
} else if (ShiftPress(SDLK_RIGHT)) {
Command_Move(active.view, DIR_RIGHT, false, SHIFT_PRESS);
MoveCarets(active.view, DIR_RIGHT, false, SHIFT_PRESS);
} else if (AltPress(SDLK_RIGHT)) {
ActiveWindow = SwitchWindow(DIR_RIGHT)->id;
NextActiveWindowID = SwitchWindow(DIR_RIGHT)->id;
} else if (Press(SDLK_RIGHT)) {
Command_Move(active.view, DIR_RIGHT);
MoveCarets(active.view, DIR_RIGHT);
}
if (CtrlShiftPress(SDLK_Z)) {
@@ -428,50 +430,50 @@ void OnCommand(Event event) {
} else if (CtrlPress(SDLK_X)) {
SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets);
Command_Copy(active.view);
Command_Replace(active.view, u"");
Replace(active.view, u"");
}
if (CtrlPress(SDLK_A)) {
Command_SelectEntireBuffer(active.view);
SelectEntireBuffer(active.view);
active.view->update_scroll = false;
}
if (ShiftPress(SDLK_PAGEUP)) {
CheckpointBeforeGoto(active.window);
Command_MoveCursorsByPageSize(active.window, DIR_UP, SHIFT_PRESS);
MoveCursorByPageSize(active.window, DIR_UP, SHIFT_PRESS);
} else if (CtrlPress(SDLK_PAGEUP)) {
CheckpointBeforeGoto(active.window);
Command_SelectRangeOneCursor(active.view, MakeRange(0));
SelectRange(active.view, MakeRange(0));
} else if (Press(SDLK_PAGEUP)) {
CheckpointBeforeGoto(active.window);
Command_MoveCursorsByPageSize(active.window, DIR_UP);
MoveCursorByPageSize(active.window, DIR_UP);
}
if (ShiftPress(SDLK_PAGEDOWN)) {
CheckpointBeforeGoto(active.window);
Command_MoveCursorsByPageSize(active.window, DIR_DOWN, SHIFT_PRESS);
MoveCursorByPageSize(active.window, DIR_DOWN, SHIFT_PRESS);
} else if (CtrlPress(SDLK_PAGEDOWN)) {
CheckpointBeforeGoto(active.window);
Command_SelectRangeOneCursor(active.view, MakeRange(active.buffer->len));
SelectRange(active.view, MakeRange(active.buffer->len));
} else if (Press(SDLK_PAGEDOWN)) {
CheckpointBeforeGoto(active.window);
Command_MoveCursorsByPageSize(active.window, DIR_DOWN);
MoveCursorByPageSize(active.window, DIR_DOWN);
}
if (ShiftPress(SDLK_HOME)) {
CheckpointBeforeGoto(active.window);
Command_MoveCursorsToSide(active.view, DIR_LEFT, SHIFT_PRESS);
MoveCursorToSide(active.view, DIR_LEFT, SHIFT_PRESS);
} else if (Press(SDLK_HOME)) {
CheckpointBeforeGoto(active.window);
Command_MoveCursorsToSide(active.view, DIR_LEFT);
MoveCursorToSide(active.view, DIR_LEFT);
}
if (ShiftPress(SDLK_END)) {
CheckpointBeforeGoto(active.window);
Command_MoveCursorsToSide(active.view, DIR_RIGHT, SHIFT_PRESS);
MoveCursorToSide(active.view, DIR_RIGHT, SHIFT_PRESS);
} else if (Press(SDLK_END)) {
CheckpointBeforeGoto(active.window);
Command_MoveCursorsToSide(active.view, DIR_RIGHT);
MoveCursorToSide(active.view, DIR_RIGHT);
}
if (CtrlShiftPress(SDLK_TAB)) {
@@ -509,7 +511,7 @@ void OnCommand(Event event) {
Scratch scratch;
String string = event.text;
String16 string16 = ToString16(scratch, string);
Command_Replace(active.view, string16);
Replace(active.view, string16);
}
if (CtrlPress(SDLK_D)) {
@@ -536,14 +538,14 @@ void OnCommand(Event event) {
}
if (CtrlShiftPress(SDLK_RETURN)) {
Command_MoveCursorsToSide(active.view, DIR_LEFT);
Command_IdentedNewLine(active.view);
Command_Move(active.view, DIR_UP);
MoveCursorToSide(active.view, DIR_LEFT);
IdentedNewLine(active.view);
MoveCarets(active.view, DIR_UP);
} else if (CtrlPress(SDLK_RETURN)) {
Command_MoveCursorsToSide(active.view, DIR_RIGHT);
Command_IdentedNewLine(active.view);
MoveCursorToSide(active.view, DIR_RIGHT);
IdentedNewLine(active.view);
} else if (Press(SDLK_RETURN)) {
Command_IdentedNewLine(active.view);
IdentedNewLine(active.view);
}
@@ -566,8 +568,8 @@ void OnCommand(Event event) {
Caret caret = active.view->carets[0];
SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets);
Command_SelectEntireBuffer(active.view);
Command_Replace(active.view, GetString(temp_buffer));
SelectEntireBuffer(active.view);
Replace(active.view, GetString(temp_buffer));
active.view->carets[0] = caret;
}
}
@@ -618,11 +620,11 @@ void OnCommand(Event event) {
active.view->carets.len = 1;
active.view->carets[0] = MakeCaret(GetFront(active.view->carets[0]));
if (active.window->lose_focus_on_escape && active.window->id == ActiveWindow) {
if (active.window->lose_focus_on_escape && active.window->id == ActiveWindowID) {
if (active.window->layout) {
//
} else {
ActiveWindow = LastActiveLayoutWindowID;
NextActiveWindowID = LastActiveLayoutWindowID;
}
}
}
@@ -631,22 +633,3 @@ void OnCommand(Event event) {
MergeCarets(active.buffer, &active.view->carets);
IF_DEBUG(AssertRanges(active.view->carets));
}
void PostCommandUpdate() {
For (Windows) {
if (it->sync_visibility_with_focus) {
if (it->id == ActiveWindow) {
it->visible = true;
} else {
it->visible = false;
}
}
}
if (ActiveWindow.id != LastActiveLayoutWindowID.id) {
Window *window = GetWindow(ActiveWindow);
if (window->layout) {
LastActiveLayoutWindowID = ActiveWindow;
}
}
}

View File

@@ -4,9 +4,9 @@ int Lua_print(lua_State *L) {
View *null_view = GetView(NullViewID);
for (int i = 1; i <= nargs; i += 1) {
String string = lua_tostring(L, i);
Command_Appendf(null_view, "%S ", string);
Appendf(null_view, "%S ", string);
}
Command_Appendf(null_view, "\n");
Appendf(null_view, "\n");
lua_pop(L, nargs);
return 0;
}
@@ -16,21 +16,15 @@ int Lua_Print(lua_State *L) {
int nargs = lua_gettop(L);
for (int i = 1; i <= nargs; i += 1) {
String string = lua_tostring(L, i);
Command_Appendf(TraceView, "%S ", string);
Appendf(TraceView, "%S ", string);
}
Command_Appendf(TraceView, "\n");
Appendf(TraceView, "\n");
lua_pop(L, nargs);
return 0;
}
int Lua_Kill(lua_State *L) {
BSet main = GetLastActiveLayoutSet();
KillProcess(main.view);
return 0;
}
int Lua_GetLoadWord(lua_State *L) {
BSet active = GetActiveSet();
BSet active = GetBSet(ActiveWindowID);
Range range = active.view->carets[0].range;
if (GetSize(range) == 0) {
range = EncloseLoadWord(active.buffer, range.min);
@@ -52,7 +46,7 @@ int Lua_BufferExists(lua_State *L) {
int Lua_GetSelection(lua_State *L) {
Scratch scratch;
BSet main = GetLastActiveLayoutSet();
BSet main = GetBSet(LastActiveLayoutWindowID);
String16 string16 = GetString(main.buffer, main.view->carets[0].range);
String string = ToString(scratch, string16);
lua_pushlstring(L, string.data, string.len);
@@ -61,7 +55,7 @@ int Lua_GetSelection(lua_State *L) {
int Lua_GetEntireBuffer(lua_State *L) {
Scratch scratch;
BSet main = GetLastActiveLayoutSet();
BSet main = GetBSet(LastActiveLayoutWindowID);
String16 string16 = GetString(main.buffer);
String string = ToString(scratch, string16);
lua_pushlstring(L, string.data, string.len);
@@ -76,13 +70,13 @@ int Lua_GetClipboard(lua_State *L) {
}
int Lua_GetFilename(lua_State *L) {
BSet main = GetLastActiveLayoutSet();
BSet main = GetBSet(LastActiveLayoutWindowID);
lua_pushlstring(L, main.buffer->name.data, main.buffer->name.len);
return 1;
}
int Lua_GetLine(lua_State *L) {
BSet main = GetLastActiveLayoutSet();
BSet main = GetBSet(LastActiveLayoutWindowID);
Caret caret = main.view->carets[0];
Int front = GetFront(caret);
Int line = PosToLine(main.buffer, front);
@@ -111,17 +105,11 @@ int Lua_GetExeDir(lua_State *L) {
}
int Lua_GetMainDir(lua_State *L) {
String name = Command_GetMainDir();
String name = GetMainDir();
lua_pushlstring(L, name.data, name.len);
return 1;
}
int Lua_KillWindow(lua_State *L) {
BSet set = GetLastActiveLayoutSet();
set.window->kill = true;
return 0;
}
static void HookLuaForceExit(lua_State *L, lua_Debug *debug) {
SDL_PumpEvents();
int numkeys = 0;
@@ -348,7 +336,7 @@ OnOpenResult CallOnOpen(Allocator allocator, String path, String meta) {
result.working_dir = working_dir;
result.file_path = file_path;
if (!IsAbsolute(result.file_path)) {
String dir = Command_GetMainDir();
String dir = GetMainDir();
result.file_path = Format(allocator, "%S/%S", dir, result.file_path);
}
if (col_string.len) {

View File

@@ -2,7 +2,7 @@ luaL_Reg LuaFunctions[] = {
{"print", Lua_print},
{"Print", Lua_Print},
{"SaveAll", Lua_SaveAll},
{"Kill", Lua_Kill},
{"KillProcess", Lua_KillProcess},
{"GetLoadWord", Lua_GetLoadWord},
{"BufferExists", Lua_BufferExists},
{"GetSelection", Lua_GetSelection},

View File

@@ -21,7 +21,8 @@ WindowID SearchBarWindowID;
ViewID SearchViewID;
BufferID SearchBufferID;
WindowID ActiveWindow;
WindowID ActiveWindowID;
WindowID NextActiveWindowID;
WindowID LastActiveLayoutWindowID;
WindowID ScrollbarSelected = {-1};
WindowID DocumentSelected = {-1};
@@ -140,7 +141,7 @@ inline View *GetView(ViewID id) {
}
inline bool IsNull(Buffer *buffer) { return buffer->id.id == NullBufferID.id; }
inline Window *GetActiveWind() { return GetWindow(ActiveWindow); }
inline Window *GetActiveWind() { return GetWindow(ActiveWindowID); }
Buffer *CreateBuffer(Allocator allocator, String name, Int size) {
Buffer *result = AllocBuffer(allocator, name, size);
@@ -244,15 +245,6 @@ BSet GetBSet(WindowID window_id) {
return result;
}
BSet GetActiveSet() {
Window *window = GetWindow(ActiveWindow);
return GetBSet(window);
}
BSet GetLastActiveLayoutSet() {
return GetBSet(LastActiveLayoutWindowID);
}
BSet GetConsoleSet() {
BSet result = {};
result.window = GetWindow(NullWindowID);
@@ -262,8 +254,8 @@ BSet GetConsoleSet() {
}
String Command_GetFilename() {
BSet set = GetLastActiveLayoutSet();
return set.buffer->name;
BSet main = GetBSet(LastActiveLayoutWindowID);
return main.buffer->name;
}
String GetDir(Buffer *buffer) {
@@ -271,9 +263,9 @@ String GetDir(Buffer *buffer) {
return name;
}
String Command_GetMainDir() {
BSet set = GetLastActiveLayoutSet();
String name = ChopLastSlash(set.buffer->name);
String GetMainDir() {
BSet main = GetBSet(LastActiveLayoutWindowID);
String name = ChopLastSlash(main.buffer->name);
return name;
}
@@ -421,6 +413,26 @@ bool BufferIsReferenced(BufferID buffer_id) {
void GarbageCollect() {
Allocator sys_allocator = GetSystemAllocator();
ActiveWindowID = NextActiveWindowID;
For (Windows) {
if (it->sync_visibility_with_focus) {
if (it->id == ActiveWindowID) {
it->visible = true;
} else {
it->visible = false;
}
}
}
if (ActiveWindowID.id != LastActiveLayoutWindowID.id) {
Window *window = GetWindow(ActiveWindowID);
if (window->layout) {
LastActiveLayoutWindowID = ActiveWindowID;
}
}
For(Buffers) {
if (it->file_mod_time) {
int64_t new_file_mod_time = GetFileModTime(it->name);

View File

@@ -15,7 +15,7 @@ void UpdateProcesses() {
String poll = PollStdout(scratch, &it, false);
if (poll.len) {
Command_Append(view, poll, it.scroll_to_end);
Append(view, poll, it.scroll_to_end);
}
if (!IsValid(&it)) {
ReportDebugf("process %lld exit code = %d", it.id, it.exit_code);
@@ -60,7 +60,7 @@ void KillProcess(View *view) {
KillProcess(&it);
remove_item = true;
String string = "process was killed by user\n";
Command_Append(view, string, it.scroll_to_end);
Append(view, string, it.scroll_to_end);
// dont break because that will fuck with removal ...
}
}

View File

@@ -246,7 +246,6 @@ void Update(Event event) {
}
OnCommand(event);
PostCommandUpdate();
UpdateProcesses();
CoUpdate(&event);
ReloadLuaConfigs();

View File

@@ -88,15 +88,14 @@ BSet Command_Open(String path, String meta = "");
BSet Command_Open(String16 path, String meta = "");
void UpdateScroll(Window *window, bool update_caret_scrolling);
void Command_SelectEntireBuffer(View *view);
void Command_Replace(View *view, String16 string);
void Command_SelectRangeOneCursor(View *view, Range range);
void Command_Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line);
void Command_Append(View *view, String string, bool scroll_to_end_if_cursor_on_last_line);
Array<Edit> Command_ReplaceEx(Allocator scratch, View *view, String16 string);
void SelectEntireBuffer(View *view);
void Replace(View *view, String16 string);
void SelectRange(View *view, Range range);
void Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line);
void Append(View *view, String string, bool scroll_to_end_if_cursor_on_last_line);
Array<Edit> ReplaceEx(Allocator scratch, View *view, String16 string);
void Command_Eval(String string);
void Command_Eval(String16 string);
String Command_GetMainDir();
void ReportDebugf(const char *fmt, ...);
void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string);
@@ -106,7 +105,7 @@ void Command_Paste(View *view);
void ReportConsolef(const char *fmt, ...);
void ReportErrorf(const char *fmt, ...);
void ReportWarningf(const char *fmt, ...);
void Command_Appendf(View *view, const char *fmt, ...);
void Appendf(View *view, const char *fmt, ...);
Buffer *CreateBuffer(Allocator allocator, String name, Int size = 4096);
View *CreateView(BufferID active_buffer);

View File

@@ -5,7 +5,7 @@ void UpdateDebugBuffer() {
View *view = GetView(window->active_view);
if (view->active_buffer.id == buffer->id.id) return;
BSet main = GetLastActiveLayoutSet();
BSet main = GetBSet(LastActiveLayoutWindowID);
Scratch scratch;
String s = Format(scratch, "wid: %d\nvid: %d\nbid: %d\nframe: %lld\n", (int)main.window->id.id, (int)main.view->id.id, (int)main.buffer->id.id, (long long)FrameID);
@@ -48,7 +48,7 @@ void StatusBarUpdate() {
}
Scratch scratch;
BSet main = GetLastActiveLayoutSet();
BSet main = GetBSet(LastActiveLayoutWindowID);
BSet title = GetBSet(status_bar_window);
title.view->scroll.y = 0;
@@ -57,7 +57,7 @@ void StatusBarUpdate() {
bool found_separator = Seek(buffer_string, u" |", &replace_range.max);
// Parse the title and line
if (title.window->id == ActiveWindow) {
if (title.window->id == ActiveWindowID) {
if (title.buffer->change_id == title.window->status_bar_last_buffer_change_id) {
return;
}
@@ -88,8 +88,8 @@ void StatusBarUpdate() {
// add separator at the end of buffer
if (!found_separator) {
Command_SelectRangeOneCursor(title.view, GetBufferEndAsRange(title.buffer));
Array<Edit> edits = Command_ReplaceEx(scratch, title.view, u" |");
SelectRange(title.view, GetBufferEndAsRange(title.buffer));
Array<Edit> edits = ReplaceEx(scratch, title.view, u" |");
}
@@ -105,10 +105,10 @@ void StatusBarUpdate() {
String16 string = ToString16(scratch, s);
String16 string_to_replace = GetString(title.buffer, replace_range);
if (string_to_replace != string) {
Command_SelectRangeOneCursor(title.view, replace_range);
Array<Edit> edits = Command_ReplaceEx(scratch, title.view, string);
SelectRange(title.view, replace_range);
Array<Edit> edits = ReplaceEx(scratch, title.view, string);
}
Command_SelectRangeOneCursor(title.view, MakeRange(0));
SelectRange(title.view, MakeRange(0));
ResetHistory(title.buffer);
}

View File

@@ -16,6 +16,7 @@ Int GetExpandingBarSize(Window *window) {
void InitWindows() {
Scratch scratch;
CreateWind();
CreateWind();
// COMMAND BAR
@@ -34,6 +35,7 @@ void InitWindows() {
window->sync_visibility_with_focus = true;
window->lose_focus_on_escape = true;
window->jump_history = false;
view->fuzzy_search = true;
}
// SEARCH BAR

View File

@@ -105,7 +105,7 @@ void DrawWindow(Window *window, Event &event) {
Rect2 screen_rect = Rect0Size(event.xwindow, event.ywindow);
SetScissor(screen_rect);
bool is_active = window->id == ActiveWindow;
bool is_active = window->id == ActiveWindowID;
bool active_layed_out_doc = window->id == LastActiveLayoutWindowID;
Color color_whitespace_during_selection = ColorWhitespaceDuringSelection;