Ctrl + P to list buffers

This commit is contained in:
Krzosa Karol
2024-08-10 10:16:55 +02:00
parent b4bc061b76
commit 3fd728543c
4 changed files with 36 additions and 31 deletions

View File

@@ -162,6 +162,29 @@ void MouseExecWord(Event event) {
}
}
void Command_ListBuffers() {
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
CheckpointBeforeGoto(window->id);
Scratch scratch;
Array<String> strings = {scratch};
For(Buffers) {
String string = Format(scratch, "%.*s id=%d is_dir=%d", FmtString(it.o->name), (int)it.id, it.o->is_directory);
Add(&strings, string);
}
String result = Merge(scratch, strings, "\n");
String16 string16 = ToString16(scratch, result);
String buffer_name = GetUniqueBufferName(scratch, GetDir(buffer), "+list_buffers-");
View *new_view = WindowOpenBufferView(window, buffer_name);
Command_SelectEntireBuffer(new_view);
Command_Replace(new_view, string16);
Command_SelectRangeOneCursor(new_view, {});
}
void MouseLoadWord(Event event) {
Vec2I mouse = MouseVec2I();
Window *window = GetActiveWindow();
@@ -419,6 +442,10 @@ bool GlobalCommand(Event event) {
}
}
if (Ctrl(SDLK_P)) {
Command_ListBuffers();
}
if (CtrlShift(SDLK_BACKSLASH)) {
AddRowWindow();
} else if (Ctrl(SDLK_BACKSLASH)) {

View File

@@ -28,13 +28,6 @@ String FieldString(lua_State *L, String name) {
return result;
}
RandomSeed UniqueBufferNameSeed = {13};
String GetUniqueBufferName(Allocator allocator, String working_dir, String prepend_name) {
uint64_t number = GetRandomU64(&UniqueBufferNameSeed);
String buffer_name = Format(allocator, "%.*s/%.*s%llu", FmtString(working_dir), FmtString(prepend_name), number);
return buffer_name;
}
void ExecInNewBuffer(String cmd, String working_dir) {
Scratch scratch;
CheckpointBeforeGoto(GetActiveMainWindowID());
@@ -144,27 +137,7 @@ int Lua_Print(lua_State *L) {
}
int Lua_ListBuffers(lua_State *L) {
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
CheckpointBeforeGoto(window->id);
Scratch scratch;
Array<String> strings = {scratch};
For(Buffers) {
String string = Format(scratch, "%.*s id=%d is_dir=%d", FmtString(it.o->name), (int)it.id, it.o->is_directory);
Add(&strings, string);
}
String result = Merge(scratch, strings, "\n");
String16 string16 = ToString16(scratch, result);
String buffer_name = GetUniqueBufferName(scratch, GetDir(buffer), "+list_buffers-");
View *new_view = WindowOpenBufferView(window, buffer_name);
Command_SelectEntireBuffer(new_view);
Command_Replace(new_view, string16);
Command_SelectRangeOneCursor(new_view, {});
Command_ListBuffers();
return 0;
}

View File

@@ -186,6 +186,13 @@ WindowID GetActiveMainWindowID() {
return GetActiveMainWindow()->id;
}
RandomSeed UniqueBufferNameSeed = {13};
String GetUniqueBufferName(Allocator allocator, String working_dir, String prepend_name) {
uint64_t number = GetRandomU64(&UniqueBufferNameSeed);
String buffer_name = Format(allocator, "%.*s/%.*s%llu", FmtString(working_dir), FmtString(prepend_name), number);
return buffer_name;
}
String GetActiveMainWindowBufferName() {
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);

View File

@@ -1,8 +1,6 @@
- garbage collect the buffers, views - store them in free list on destroy
- 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
- adding items to directory should create files on save - it should ask the user (syntax: dir/ | file)
- apply clang format
- apply clang format on save