Open improvements

This commit is contained in:
Krzosa Karol
2025-05-09 10:50:40 +02:00
parent d2ca655178
commit 3180625a41
4 changed files with 74 additions and 65 deletions

View File

@@ -40,7 +40,7 @@ void GotoForward(Window *window) {
UpdateScroll(window, true); UpdateScroll(window, true);
} }
void Command_JumpNew(BSet *set) { void JumpGarbageBuffer(BSet *set) {
CheckpointBeforeGoto(set->window); CheckpointBeforeGoto(set->window);
String current_dir = ChopLastSlash(set->buffer->name); String current_dir = ChopLastSlash(set->buffer->name);
String buffer_name = GetUniqueBufferName(current_dir, "temp"); String buffer_name = GetUniqueBufferName(current_dir, "temp");
@@ -922,21 +922,6 @@ int Lua_GetCFiles(lua_State *L) {
return 0; return 0;
} }
void Command_ExecInNewBuffer(String cmd, String working_dir) {
BSet main = GetActiveMainSet();
Command_JumpNew(&main);
Exec(main.view->id, true, cmd, working_dir);
ActiveWindow = main.window->id;
}
int Lua_ExecInNewBuffer(lua_State *L) {
String string = lua_tostring(L, 1);
lua_pop(L, 1);
String working_dir = Command_GetMainDir();
Command_ExecInNewBuffer(string, working_dir);
return 0;
}
View *Command_ExecHidden(String buffer_name, String cmd, String working_dir) { View *Command_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);
@@ -945,10 +930,10 @@ View *Command_ExecHidden(String buffer_name, String cmd, String working_dir) {
return view; return view;
} }
BSet Command_Exec(String cmd, String working_dir) { BSet Command_Exec(String cmd, String working_dir, bool set_active = true) {
BSet set = GetActiveMainSet(); BSet set = GetActiveMainSet();
ActiveWindow = set.window->id; if (set_active) ActiveWindow = set.window->id;
Command_JumpNew(&set); JumpGarbageBuffer(&set);
Exec(set.view->id, true, cmd, working_dir); Exec(set.view->id, true, cmd, working_dir);
return set; return set;
} }
@@ -959,61 +944,53 @@ int Lua_C(lua_State *L) {
return 0; return 0;
} }
BSet Command_Open(String path, String meta) { BSet Command_Open(Window *window, String path, String meta, bool set_active = true) {
Scratch scratch; Scratch scratch;
BSet set = GetBSet(window);
lua_getglobal(LuaState, "OnOpen"); OnOpenResult ores = CallOnOpen(path, meta);
lua_pushlstring(LuaState, path.data, path.len);
lua_pushlstring(LuaState, meta.data, meta.len);
if (lua_pcall(LuaState, 2, 1, 0) != 0) {
const char *error_message = lua_tostring(LuaState, -1);
ReportWarningf("Failed the call to OnOpen! %s", error_message);
lua_pop(LuaState, 1);
return {};
}
BSet main = GetActiveMainSet();
String kind = FieldString(LuaState, "kind"); String kind = FieldString(LuaState, "kind");
if (kind == "text") { if (kind == "text") {
String file_path = FieldString(LuaState, "file_path"); if (set_active) {
String line_string = FieldString(LuaState, "line"); ActiveWindow = set.window->id;
Int line = strtoll(line_string.data, NULL, 10); }
String col_string = FieldString(LuaState, "col"); if (IsDir(ores.file_path)) {
Int col = strtoll(col_string.data, NULL, 10); JumpGarbageBuffer(&set);
set.buffer->name = GetUniqueBufferName(ores.file_path, "temp");
ActiveWindow = main.window->id; Command_Appendf(set.view, "..\n", FmtString(ores.file_path));
if (IsDir(file_path)) { for (FileIter it = IterateFiles(scratch, ores.file_path); IsValid(it); Advance(&it)) {
Command_JumpNew(&main); Command_Appendf(set.view, "%.*s\n", FmtString(it.filename));
main.buffer->name = GetUniqueBufferName(file_path, "temp");
Command_Appendf(main.view, "..\n", FmtString(file_path));
for (FileIter it = IterateFiles(scratch, file_path); IsValid(it); Advance(&it)) {
Command_Appendf(main.view, "%.*s\n", FmtString(it.filename));
} }
} else { } else {
CheckpointBeforeGoto(main.window); CheckpointBeforeGoto(set.window);
View *view = WindowOpenBufferView(main.window, file_path); View *view = WindowOpenBufferView(set.window, ores.file_path);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
if (line != -1 && col != -1) { if (ores.line != -1 && ores.col != -1) {
Int pos = XYToPos(buffer, {col - 1, line - 1}); Int pos = XYToPos(buffer, {ores.col - 1, ores.line - 1});
view->carets[0] = MakeCaret(pos); view->carets[0] = MakeCaret(pos);
} }
} }
UpdateScroll(main.window, true); UpdateScroll(set.window, true);
} else if (kind == "exec") { } else if (kind == "exec") {
String cmd = FieldString(LuaState, "cmd"); if (set_active) {
String working_dir = FieldString(LuaState, "working_dir"); ActiveWindow = set.window->id;
Command_Exec(cmd, Command_GetMainDir()); }
JumpGarbageBuffer(&set);
Exec(set.view->id, true, ores.cmd, ores.working_dir);
} else if (kind == "exec_console") { } else if (kind == "exec_console") {
// this shouldn't change the focus/window/view // this shouldn't change the focus/window/view
String cmd = FieldString(LuaState, "cmd"); Exec(NullViewID, true, ores.cmd, ores.working_dir);
String working_dir = FieldString(LuaState, "working_dir");
Exec(NullViewID, true, cmd, working_dir);
} else if (kind == "skip") { } else if (kind == "skip") {
return {}; return {};
} else { } else {
ReportWarningf("Failed to match any of OnOpen results!"); ReportWarningf("Failed to match any of OnOpen results!");
} }
set = GetBSet(window);
return set;
}
BSet Command_Open(String path, String meta) {
BSet main = GetActiveMainSet();
main = Command_Open(main.window, path, meta);
return main; return main;
} }
@@ -1061,12 +1038,12 @@ int Lua_Cmd(lua_State *L) {
Exec(set.view->id, true, cmd, working_dir); Exec(set.view->id, true, cmd, working_dir);
Command_EndJump(set); Command_EndJump(set);
} else if (kind == "fuzzy") { } else if (kind == "fuzzy") {
Command_JumpNew(&main); JumpGarbageBuffer(&main);
Exec(main.view->id, true, cmd, working_dir); Exec(main.view->id, true, cmd, working_dir);
main.view->fuzzy_search = true; main.view->fuzzy_search = true;
ActiveWindow = main.window->id; ActiveWindow = main.window->id;
} else { } else {
Command_JumpNew(&main); JumpGarbageBuffer(&main);
Exec(main.view->id, true, cmd, working_dir); Exec(main.view->id, true, cmd, working_dir);
ActiveWindow = main.window->id; ActiveWindow = main.window->id;
} }
@@ -1077,7 +1054,7 @@ int Lua_Cmd(lua_State *L) {
void Command_ListBuffers() { void Command_ListBuffers() {
BSet main = GetActiveMainSet(); BSet main = GetActiveMainSet();
ActiveWindow = main.window->id; ActiveWindow = main.window->id;
Command_JumpNew(&main); JumpGarbageBuffer(&main);
for (Buffer *it = FirstBuffer; it; it = it->next) { for (Buffer *it = FirstBuffer; it; it = it->next) {
Command_Appendf(main.view, "%.*s\n", FmtString(it->name)); Command_Appendf(main.view, "%.*s\n", FmtString(it->name));
} }

View File

@@ -300,6 +300,40 @@ void ReloadLuaConfigs() {
} }
} }
struct OnOpenResult {
String file_path;
Int line, col;
String working_dir;
String cmd;
};
OnOpenResult CallOnOpen(String path, String meta) {
lua_getglobal(LuaState, "OnOpen");
lua_pushlstring(LuaState, path.data, path.len);
lua_pushlstring(LuaState, meta.data, meta.len);
if (lua_pcall(LuaState, 2, 1, 0) != 0) {
const char *error_message = lua_tostring(LuaState, -1);
ReportWarningf("Failed the call to OnOpen! %s", error_message);
lua_pop(LuaState, 1);
return {};
}
String file_path = FieldString(LuaState, "file_path");
String line_string = FieldString(LuaState, "line");
String col_string = FieldString(LuaState, "col");
String cmd = FieldString(LuaState, "cmd");
String working_dir = FieldString(LuaState, "working_dir");
OnOpenResult result = {};
result.cmd = cmd;
result.working_dir = working_dir;
result.file_path = file_path;
if (col_string.len) result.col = strtoll(col_string.data, NULL, 10);
if (line_string.len) result.line = strtoll(line_string.data, NULL, 10);
return result;
}
void CallOnCommand(Event *event) { void CallOnCommand(Event *event) {
lua_getglobal(LuaState, "OnCommand"); lua_getglobal(LuaState, "OnCommand");
PushEvent(LuaState, event); PushEvent(LuaState, event);

View File

@@ -17,7 +17,6 @@ luaL_Reg LuaFunctions[] = {
{"Reopen", Lua_Reopen}, {"Reopen", Lua_Reopen},
{"ToggleFullscreen", Lua_ToggleFullscreen}, {"ToggleFullscreen", Lua_ToggleFullscreen},
{"GetCFiles", Lua_GetCFiles}, {"GetCFiles", Lua_GetCFiles},
{"ExecInNewBuffer", Lua_ExecInNewBuffer},
{"C", Lua_C}, {"C", Lua_C},
{"Open", Lua_Open}, {"Open", Lua_Open},
{"Cmd", Lua_Cmd}, {"Cmd", Lua_Cmd},

View File

@@ -9,7 +9,6 @@
- Delete directory/file on disk command - Delete directory/file on disk command
- Create directory command (probably should enter it automatically - Create directory command (probably should enter it automatically
- Convert more commands to taking buffer instead of view - Convert more commands to taking buffer instead of view
- Add Command_Open that takes window/view as param
- save all relevant buffers and build - save all relevant buffers and build
- shift down on last line should move the cursor to end of line!!! same for up - shift down on last line should move the cursor to end of line!!! same for up