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);
}
void Command_JumpNew(BSet *set) {
void JumpGarbageBuffer(BSet *set) {
CheckpointBeforeGoto(set->window);
String current_dir = ChopLastSlash(set->buffer->name);
String buffer_name = GetUniqueBufferName(current_dir, "temp");
@@ -820,11 +820,11 @@ void Command_GotoNextInList(Window *window, Int line_offset = 1) {
bool opened = false;
for (Int i = line + line_offset; i >= 0 && i < buffer_goto->line_starts.len; i += line_offset) {
Range line_range = GetLineRangeWithoutNL(buffer_goto, i);
String16 line = GetString(buffer_goto, line_range);
view_goto->carets[0] = MakeCaret(line_range.min);
Range line_range = GetLineRangeWithoutNL(buffer_goto, i);
String16 line = GetString(buffer_goto, line_range);
view_goto->carets[0] = MakeCaret(line_range.min);
window->goto_list_pos = line_range.min;
line = Trim(line);
line = Trim(line);
MergeCarets(buffer_goto, &view_goto->carets);
IF_DEBUG(AssertRanges(view_goto->carets));
@@ -922,21 +922,6 @@ int Lua_GetCFiles(lua_State *L) {
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 *view = OpenBufferView(buffer_name);
Buffer *buffer = GetBuffer(view->active_buffer);
@@ -945,10 +930,10 @@ View *Command_ExecHidden(String buffer_name, String cmd, String working_dir) {
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();
ActiveWindow = set.window->id;
Command_JumpNew(&set);
if (set_active) ActiveWindow = set.window->id;
JumpGarbageBuffer(&set);
Exec(set.view->id, true, cmd, working_dir);
return set;
}
@@ -959,61 +944,53 @@ int Lua_C(lua_State *L) {
return 0;
}
BSet Command_Open(String path, String meta) {
BSet Command_Open(Window *window, String path, String meta, bool set_active = true) {
Scratch scratch;
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 {};
}
BSet main = GetActiveMainSet();
BSet set = GetBSet(window);
OnOpenResult ores = CallOnOpen(path, meta);
String kind = FieldString(LuaState, "kind");
if (kind == "text") {
String file_path = FieldString(LuaState, "file_path");
String line_string = FieldString(LuaState, "line");
Int line = strtoll(line_string.data, NULL, 10);
String col_string = FieldString(LuaState, "col");
Int col = strtoll(col_string.data, NULL, 10);
ActiveWindow = main.window->id;
if (IsDir(file_path)) {
Command_JumpNew(&main);
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));
if (set_active) {
ActiveWindow = set.window->id;
}
if (IsDir(ores.file_path)) {
JumpGarbageBuffer(&set);
set.buffer->name = GetUniqueBufferName(ores.file_path, "temp");
Command_Appendf(set.view, "..\n", FmtString(ores.file_path));
for (FileIter it = IterateFiles(scratch, ores.file_path); IsValid(it); Advance(&it)) {
Command_Appendf(set.view, "%.*s\n", FmtString(it.filename));
}
} else {
CheckpointBeforeGoto(main.window);
View *view = WindowOpenBufferView(main.window, file_path);
CheckpointBeforeGoto(set.window);
View *view = WindowOpenBufferView(set.window, ores.file_path);
Buffer *buffer = GetBuffer(view->active_buffer);
if (line != -1 && col != -1) {
Int pos = XYToPos(buffer, {col - 1, line - 1});
if (ores.line != -1 && ores.col != -1) {
Int pos = XYToPos(buffer, {ores.col - 1, ores.line - 1});
view->carets[0] = MakeCaret(pos);
}
}
UpdateScroll(main.window, true);
UpdateScroll(set.window, true);
} else if (kind == "exec") {
String cmd = FieldString(LuaState, "cmd");
String working_dir = FieldString(LuaState, "working_dir");
Command_Exec(cmd, Command_GetMainDir());
if (set_active) {
ActiveWindow = set.window->id;
}
JumpGarbageBuffer(&set);
Exec(set.view->id, true, ores.cmd, ores.working_dir);
} else if (kind == "exec_console") {
// this shouldn't change the focus/window/view
String cmd = FieldString(LuaState, "cmd");
String working_dir = FieldString(LuaState, "working_dir");
Exec(NullViewID, true, cmd, working_dir);
Exec(NullViewID, true, ores.cmd, ores.working_dir);
} else if (kind == "skip") {
return {};
} else {
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;
}
@@ -1061,12 +1038,12 @@ int Lua_Cmd(lua_State *L) {
Exec(set.view->id, true, cmd, working_dir);
Command_EndJump(set);
} else if (kind == "fuzzy") {
Command_JumpNew(&main);
JumpGarbageBuffer(&main);
Exec(main.view->id, true, cmd, working_dir);
main.view->fuzzy_search = true;
ActiveWindow = main.window->id;
} else {
Command_JumpNew(&main);
JumpGarbageBuffer(&main);
Exec(main.view->id, true, cmd, working_dir);
ActiveWindow = main.window->id;
}
@@ -1077,7 +1054,7 @@ int Lua_Cmd(lua_State *L) {
void Command_ListBuffers() {
BSet main = GetActiveMainSet();
ActiveWindow = main.window->id;
Command_JumpNew(&main);
JumpGarbageBuffer(&main);
for (Buffer *it = FirstBuffer; it; it = it->next) {
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) {
lua_getglobal(LuaState, "OnCommand");
PushEvent(LuaState, event);

View File

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

View File

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