Simplify active windows

This commit is contained in:
Krzosa Karol
2024-08-10 09:08:35 +02:00
parent d5f903eaa2
commit 08a385b050
13 changed files with 65 additions and 100 deletions

View File

@@ -289,7 +289,7 @@ end
function MatchWindowsPath(_s)
local s, file_path, drive = SkipPath(_s)
if not drive then
local d = GetCurrentBufferDir()
local d = GetActiveMainWindowBufferName()
file_path = d..'/'..file_path
end
local line, col, s = SkipLineAndColumn(s)
@@ -303,7 +303,7 @@ function MatchGitCommit(s)
if i then
s = s:sub(8)
local command = "git --no-pager show "..s
return {kind = "exec", cmd = command, working_dir = GetCurrentBufferDir()}
return {kind = "exec", cmd = command, working_dir = GetActiveMainWindowBufferName()}
end
return nil
end

View File

@@ -310,11 +310,11 @@ bool GlobalCommand(Event event) {
if (event.ctrl && event.shift && Mouse(RIGHT)) {
MouseExecWord(event);
} else if (event.alt && event.ctrl && Mouse(RIGHT)) {
GotoForward(GetLastActiveWindow());
GotoForward(GetActiveMainWindowID());
} else if (event.ctrl && Mouse(RIGHT)) {
MouseLoadWord(event);
} else if (event.alt && Mouse(RIGHT)) {
GotoBackward(GetLastActiveWindow());
GotoBackward(GetActiveMainWindowID());
} else if (Mouse(RIGHT)) {
Vec2I mouse = MouseVec2I();
Window *window = GetActiveWindow();
@@ -510,7 +510,7 @@ void ReportWarningf(const char *fmt, ...) {
Command_Append(NullViewID, string, true);
Window *window = GetWindowWithView(NullViewID);
if (!window) {
WindowID last_active_window_id = GetLastActiveWindow();
WindowID last_active_window_id = GetActiveMainWindowID();
window = GetWindow(last_active_window_id);
}
CheckpointBeforeGoto(window->id);

View File

@@ -652,7 +652,7 @@ void WindowCommand(Event event, Window *window, View *view) {
if (Press(SDLK_ESCAPE)) {
if (window->deactivate_on_escape) {
SetActiveWindow(GetLastActiveWindow());
SetActiveWindow(GetActiveMainWindowID());
} else {
view->carets.len = 1;
}
@@ -866,7 +866,7 @@ void WindowCommand(Event event, Window *window, View *view) {
}
if (Ctrl(SDLK_PERIOD)) {
Window *window = GetWindow(GetLastActiveWindow());
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
@@ -909,7 +909,7 @@ void WindowCommand(Event event, Window *window, View *view) {
Command_EvalLua(view, string);
} else if (CtrlAlt(SDLK_Q)) {
GotoForward(GetLastActiveWindow());
GotoForward(GetActiveMainWindowID());
} else if (Ctrl(SDLK_Q)) {
Caret caret = view->carets[0];
Range range = caret.range;
@@ -919,7 +919,7 @@ void WindowCommand(Event event, Window *window, View *view) {
window->active_goto_list = view->id;
Open(string);
} else if (Alt(SDLK_Q)) {
GotoBackward(GetLastActiveWindow());
GotoBackward(GetActiveMainWindowID());
}
IF_DEBUG(AssertRanges(view->carets));
}

View File

@@ -178,7 +178,7 @@ end
function MatchWindowsPath(_s)
local s, file_path, drive = SkipPath(_s)
if not drive then
local d = GetCurrentBufferDir()
local d = GetActiveMainWindowBufferName()
file_path = d..'/'..file_path
end
local line, col, s = SkipLineAndColumn(s)
@@ -192,7 +192,7 @@ function MatchGitCommit(s)
if i then
s = s:sub(8)
local command = "git --no-pager show "..s
return {kind = "exec", cmd = command, working_dir = GetCurrentBufferDir()}
return {kind = "exec", cmd = command, working_dir = GetActiveMainWindowBufferName()}
end
return nil
end

View File

@@ -37,9 +37,9 @@ String GetUniqueBufferName(Allocator allocator, String working_dir, String p
void ExecInNewBuffer(String cmd, String working_dir) {
Scratch scratch;
CheckpointBeforeGoto(GetLastActiveWindow());
CheckpointBeforeGoto(GetActiveMainWindowID());
String buffer_name = GetUniqueBufferName(scratch, working_dir, "+cmd-");
Window *window = GetWindow(GetLastActiveWindow());
Window *window = GetActiveMainWindow();
View *view = WindowOpenBufferView(window, buffer_name);
Buffer *buffer = GetBuffer(view->active_buffer);
view->carets[0] = MakeCaret({});
@@ -66,7 +66,7 @@ void Open(String path) {
String col_string = FieldString(LuaState, "col");
Int col = strtoll(col_string.data, NULL, 10);
WindowID window_id = GetLastActiveWindow();
WindowID window_id = GetActiveMainWindowID();
CheckpointBeforeGoto(window_id);
Window *window = GetWindow(window_id);
View *view = WindowOpenBufferView(window, file_path);
@@ -98,9 +98,8 @@ int Lua_FuzzySort(lua_State *L) {
Scratch scratch;
String16 string16 = ToString16(scratch, string);
WindowID window_id = GetLastActiveWindow();
Window *window = GetWindow(window_id);
View *view = GetView(window->active_view);
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Command_FuzzySort(view, string16);
return 0;
}
@@ -108,7 +107,7 @@ int Lua_FuzzySort(lua_State *L) {
int Lua_AppendCmd(lua_State *L) {
String string = lua_tostring(L, 1);
lua_pop(L, 1);
String working_dir = GetCurrentBufferDir();
String working_dir = GetActiveMainWindowBufferDir();
Exec(NullViewID, true, string, working_dir);
return 0;
}
@@ -117,13 +116,13 @@ int Lua_NewCmd(lua_State *L) {
String string = lua_tostring(L, 1);
lua_pop(L, 1);
String working_dir = GetCurrentBufferDir();
String working_dir = GetActiveMainWindowBufferDir();
ExecInNewBuffer(string, working_dir);
return 0;
}
int Lua_Kill(lua_State *L) {
Window *window = GetCurrentWindow();
Window *window = GetActiveMainWindow();
KillProcess(window->active_view);
return 0;
}
@@ -145,7 +144,7 @@ int Lua_Print(lua_State *L) {
}
int Lua_ListBuffers(lua_State *L) {
Window *window = GetWindow(GetLastActiveWindow());
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
CheckpointBeforeGoto(window->id);
@@ -153,7 +152,7 @@ int Lua_ListBuffers(lua_State *L) {
Scratch scratch;
Array<String> strings = {scratch};
For(Buffers) {
String string = Format(scratch, "%.*s", FmtString(it.name));
String string = Format(scratch, "%.*s id=%d is_dir=%d", FmtString(it.name), (int)it.id.id, it.is_directory);
Add(&strings, string);
}
String result = Merge(scratch, strings, "\n");
@@ -196,30 +195,30 @@ int Lua_GetWorkingDir(lua_State *L) {
return 1;
}
int Lua_GetCurrentBufferName(lua_State *L) {
String name = GetCurrentBufferName();
int Lua_GetActiveMainWindowBufferName(lua_State *L) {
String name = GetActiveMainWindowBufferName();
lua_pushlstring(LuaState, name.data, name.len);
return 1;
}
int Lua_GetCurrentBufferDir(lua_State *L) {
String name = GetCurrentBufferDir();
int Lua_GetActiveMainWindowBufferDir(lua_State *L) {
String name = GetActiveMainWindowBufferDir();
lua_pushlstring(LuaState, name.data, name.len);
return 1;
}
int Lua_OpenFilesHere(lua_State *L) {
Window *window = GetWindow(GetLastActiveWindow());
Window *window = GetActiveMainWindow();
Scratch scratch;
for (FileIter it = IterateFiles(scratch, GetCurrentBufferDir()); IsValid(it); Advance(&it)) {
for (FileIter it = IterateFiles(scratch, GetActiveMainWindowBufferDir()); IsValid(it); Advance(&it)) {
WindowOpenBufferView(window, it.absolute_path);
}
return 0;
}
int Lua_Search(lua_State *L) {
Window *seek_window = GetCurrentWindow();
Window *seek_window = GetActiveMainWindow();
seek_window->search_string = lua_tostring(L, 1);
lua_pop(L, 1);
View *seek_view = GetView(seek_window->active_view);
@@ -229,7 +228,7 @@ int Lua_Search(lua_State *L) {
}
int Lua_SearchB(lua_State *L) {
Window *seek_window = GetCurrentWindow();
Window *seek_window = GetActiveMainWindow();
seek_window->search_string = lua_tostring(L, 1);
lua_pop(L, 1);
View *seek_view = GetView(seek_window->active_view);

View File

@@ -9,8 +9,8 @@ luaL_Reg LuaFunctions[] = {
{"GetBufferList", Lua_GetBufferList},
{"FileExists", Lua_FileExists},
{"GetWorkingDir", Lua_GetWorkingDir},
{"GetCurrentBufferName", Lua_GetCurrentBufferName},
{"GetCurrentBufferDir", Lua_GetCurrentBufferDir},
{"GetActiveMainWindowBufferName", Lua_GetActiveMainWindowBufferName},
{"GetActiveMainWindowBufferDir", Lua_GetActiveMainWindowBufferDir},
{"OpenFilesHere", Lua_OpenFilesHere},
{"Search", Lua_Search},
{"SearchB", Lua_SearchB},

View File

@@ -117,22 +117,8 @@ View *CreateView(BufferID active_buffer) {
return w;
}
WindowID GetLastActiveWindow() {
For(IterateInReverse(&WindowSwitchHistory)) {
return it;
}
return NullWindowID;
}
void SetActiveWindow(WindowID window) {
bool new_active_window = window.id != ActiveWindow.id;
if (new_active_window) {
ActiveWindow = window;
Window *w = GetWindow(window);
if (!w->dont_save_in_active_window_history) {
Add(&WindowSwitchHistory, window);
}
}
ActiveWindow = window;
}
View *FindView(BufferID buffer_id) {
@@ -181,7 +167,7 @@ Window *GetTitlebarWindow(WindowID id) {
return window;
}
Window *GetCurrentWindow() {
Window *GetActiveMainWindow() {
Window *window = GetWindow(ActiveWindow);
if (window->is_title_bar) {
window = GetWindow(window->title_bar_window);
@@ -189,8 +175,12 @@ Window *GetCurrentWindow() {
return window;
}
String GetCurrentBufferName() {
Window *window = GetCurrentWindow();
WindowID GetActiveMainWindowID() {
return GetActiveMainWindow()->id;
}
String GetActiveMainWindowBufferName() {
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
return buffer->name;
@@ -201,8 +191,8 @@ String GetDir(Buffer *buffer) {
return name;
}
String GetCurrentBufferDir() {
Window *window = GetCurrentWindow();
String GetActiveMainWindowBufferDir() {
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
String name = buffer->is_directory ? buffer->name : ChopLastSlash(buffer->name);
@@ -262,6 +252,9 @@ Buffer *BufferOpenFile(String path) {
if (!IsNull(buffer)) {
return buffer;
}
if (IsNull(buffer) && buffer->name == path) {
return buffer;
}
if (!FileExists(path)) {
path = GetAbsolutePath(scratch, path);
@@ -284,6 +277,7 @@ Buffer *BufferOpenFile(String path) {
buffer->len = ConvertUTF8ToUTF16UnixLine(string, buffer->str, buffer->cap);
UpdateLines(buffer, {}, String16{(wchar_t *)buffer->data, buffer->len});
}
return buffer;
}
@@ -294,16 +288,6 @@ View *OpenBufferView(String name) {
}
View *WindowOpenBufferView(Window *new_parent_window, String name) {
// @todo: window history
// For(new_parent_window->views) {
// View *it_view = GetView(it);
// Buffer *it_buffer = GetBuffer(it_view->active_buffer);
// if (it_buffer->name == name) {
// new_parent_window->active_view = it;
// return it_view;
// }
// }
View *view = FindViewWithBufferName(name);
if (!view) {
View *result = OpenBufferView(name);

View File

@@ -189,18 +189,7 @@ void Update(Event event) {
For(IterateInReverse(&order)) {
Window *window = &Windows[it];
{
if (window->invisible_when_inactive) {
bool title_bar_is_active = ActiveWindow == window->title_bar_window;
bool is_active = IsActive(window);
if (is_active || title_bar_is_active) {
SetVisibility(window->id, true);
} else {
SetVisibility(window->id, false);
}
}
if (!window->visible) continue;
}
if (!window->visible) continue;
View *view = GetView(window->active_view);
UpdateScroll(window, !AreEqual(view->main_caret_on_begin_frame, view->carets[0]) && view->update_scroll);

View File

@@ -84,13 +84,11 @@ struct Window {
bool draw_scrollbar : 1;
bool draw_line_numbers : 1;
bool invisible_when_inactive : 1;
bool visible : 1;
bool absolute_position : 1;
bool is_title_bar : 1;
bool is_column : 1;
bool dont_save_in_active_window_history : 1;
bool deactivate_on_escape : 1;
};
};

View File

@@ -30,7 +30,7 @@ void UpdateDebugBuffer() {
View *view = GetActiveView(window);
if (view->active_buffer.id == buffer->id.id) return;
Window *last_window = GetWindow(GetLastActiveWindow());
Window *last_window = GetActiveMainWindow();
View *last_view = GetActiveView(last_window);
Buffer *last_buffer = GetBuffer(last_view->active_buffer);

View File

@@ -1,5 +1,8 @@
- Remove pointers and use ViewIDs (enable array debug while doing this)
- Remove console and command window, provide alternatives but unify the interface?
- We can create 2 buffers with same name
- window splitting leaves a whitegap at the end
- PageUp for some reason stops at 2 line before last line
- apply clang format
- apply clang format on save
@@ -9,27 +12,21 @@
- ctrl + f - should find Search and select content or add Search
- some split selection commands
- assign commands or lua functions to F1-F8 keys
- generate the lua function table for all functions prefixed with Lua_
- A lister which is going to show project without the full path and sorted by recency
- Fix fuzzy search look
- word complete
- Search all buffers in 10X style, incrementally searched results popping up on every key press (maybe we need coroutine library in C so this is easier?)
- Search and replace
- load in a next window
- load in new window
- kill view - killing all views for buffer ejects buffer (maybe also introduce kill buffer)
- ask if you want to close without saving on exit
- how to be better at marking buffers as modified?
- ask if you want to create new file?
- open lua file with proper name it should do the work for setting up a project (double-click on desktop)
- when do we regen directory buffers?
- load project command which loads files and config
- load all files in a directory
- global config and local config
- open project from cmd
- draw indentation levels like in sublime (those lines) - we render chars one by one so seems relatively easy to figure out if whitespace belongs to beginning of line (make sure to add max value like 40 because of big files)
- code sections, visual demarkation if beginning of line has a very specific text + goto next / goto prev section hotkey!

View File

@@ -39,11 +39,10 @@ Array<Int> GetWindowZOrder(Allocator allocator) {
}
Window *CreateTitlebar(WindowID parent_window_id) {
Window *window = CreateWindow();
window->draw_scrollbar = false;
window->dont_save_in_active_window_history = true;
window->deactivate_on_escape = true;
window->is_title_bar = true;
Window *window = CreateWindow();
window->draw_scrollbar = false;
window->deactivate_on_escape = true;
window->is_title_bar = true;
static int TitlebarCount;
Allocator sys_allocator = GetSystemAllocator();
@@ -148,16 +147,15 @@ void InitWindows() {
}
{
Window *window = CreateWindow();
WindowID window_id = window->id;
DebugWindowID = window->id;
window->draw_line_numbers = false;
window->absolute_position = true;
window->draw_line_numbers = false;
window->draw_scrollbar = false;
window->dont_save_in_active_window_history = true;
window->visible = false;
window->z = 2;
Window *window = CreateWindow();
WindowID window_id = window->id;
DebugWindowID = window->id;
window->draw_line_numbers = false;
window->absolute_position = true;
window->draw_line_numbers = false;
window->draw_scrollbar = false;
window->visible = false;
window->z = 2;
Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("+debug"));
DebugBufferID = buffer->id;

View File

@@ -109,7 +109,7 @@ void DrawWindow(Window *window, Event &event) {
View *view = GetActiveView(window);
Buffer *buffer = GetBuffer(view->active_buffer);
SetScissor(GetScreenRectF());
bool is_active = IsActive(window) || window->id.id == GetLastActiveWindow().id;
bool is_active = IsActive(window) || window->id.id == GetActiveMainWindowID().id;
Color color_whitespace_during_selection = ColorWhitespaceDuringSelection;
Color color_background = ColorBackground;