lua api improvements

This commit is contained in:
krzosa
2025-04-30 22:34:00 +02:00
parent dc3861adfe
commit e652d5b0e2
10 changed files with 70 additions and 108 deletions

View File

@@ -120,7 +120,7 @@ void MouseLoadWord(Event event, bool cut_path) {
active.view->carets.len = 1;
active.view->carets[0] = MakeCaret(p);
Open(string);
Command_Open(string);
}
}
}
@@ -817,7 +817,7 @@ void Command_GotoNextInList(Window *window, Int line_offset = 1) {
if (line.len == 0) continue;
CheckpointBeforeGoto(window, active_view);
Open(line);
Command_Open(line);
opened = true;
break;
}

View File

@@ -483,7 +483,7 @@ void OnCommand(Event event) {
if (StartsWith(right_part, "/+")) {
CutLastSlash(&string);
}
Open(string);
Command_Open(string);
}
if (CtrlPress(SDLK_T)) {
@@ -506,14 +506,14 @@ void OnCommand(Event event) {
CutLastSlash(&string);
}
Open(string);
Command_Open(string);
} else if (CtrlPress(SDLK_Q)) {
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);
Open(string);
Command_Open(string);
}
if (Press(SDLK_ESCAPE)) {

View File

@@ -68,6 +68,8 @@ Style.FontFilter = 0
Style.Font = "C:/Windows/Fonts/consola.ttf"
Style.VCVarsall = "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Auxiliary/Build/vcvars64.bat"
INTERNET_BROWSER = 'C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe'
SDLK_CTRL = 1073742048
SDLK_PAGE_DOWN = 1073741902
SDLK_PAGE_UP = 1073741899
@@ -220,32 +222,19 @@ function SkipPath(s)
return s, path, drive, cells
end
function BufferNameExists(name)
buffers = GetBufferList()
for i = 1,#buffers do
buff = buffers[i]
if buff == name then
return true
end
end
return false
end
function MatchWindowsPath(_s)
local s, file_path, drive = SkipPath(_s)
if not file_path then return nil end
if not drive then
local d = GetActiveMainWindowBufferDir()
local d = GetDir()
file_path = d..'/'..file_path
end
local line, col, s = SkipLineAndColumn(s)
local exists = FileExists(file_path) or BufferNameExists(file_path)
local exists = FileExists(file_path) or BufferExists(file_path)
if not exists then return nil end
Print("OPEN :: INPUT = ".._s.." KIND = ".."text".." ".."FILE_PATH = "..file_path.."["..line..","..col.."]")
return {kind = "text", file_path = file_path, line = line, col = col}
end
@@ -254,16 +243,15 @@ function MatchGitCommit(s)
if i then
s = s:sub(8)
local command = "git --no-pager show "..s
return {kind = "exec", cmd = command, working_dir = GetActiveMainWindowBufferDir()}
return {kind = "exec", cmd = command, working_dir = GetDir()}
end
return nil
end
InternetBrowser = 'C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe'
function MatchURL(s)
local i, j = string.find(s, "^https://")
if i then
return {kind = "exec_console", cmd = '"'..InternetBrowser..'" '..s, working_dir = GetActiveMainWindowBufferDir()}
return {kind = "exec_console", cmd = '"'..INTERNET_BROWSER..'" '..s, working_dir = GetDir()}
end
return nil
end
@@ -313,8 +301,6 @@ table.insert(OnCommandCallbacks, function (e)
Eval(GetLoadWord()) end
end)
-- REMEBER: AS LITTLE ACTUAL CODE AS POSSIBLE IN LUA
-- ONLY CONFIGURABLES
function OnCommand(e)
for i = #OnCommandCallbacks,1,-1 do
on_command = OnCommandCallbacks[i]

View File

@@ -38,7 +38,7 @@ View *Command_ExecHidden(String buffer_name, String cmd, String working_dir) {
return view;
}
void Open(String path) {
void Command_Open(String path) {
Scratch scratch;
lua_getglobal(LuaState, "ApplyRules");
@@ -81,16 +81,16 @@ void Open(String path) {
}
}
void Open(String16 path) {
void Command_Open(String16 path) {
Scratch scratch;
String string = ToString(scratch, path);
Open(string);
Command_Open(string);
}
int Lua_AppendCmd(lua_State *L) {
String string = lua_tostring(L, 1);
lua_pop(L, 1);
String working_dir = GetActiveMainWindowBufferDir();
String working_dir = Command_GetDir();
Exec(NullViewID, true, string, working_dir);
return 0;
}
@@ -111,7 +111,7 @@ int Lua_C(lua_State *L) {
String string = lua_tostring(L, 1);
lua_pop(L, 1);
String working_dir = GetActiveMainWindowBufferDir();
String working_dir = Command_GetDir();
BSet set = GetActiveMainSet();
Command_ExecInNewBuffer(set, string, working_dir);
return 0;
@@ -152,6 +152,18 @@ int Lua_Cmd(lua_State *L) {
return 0;
}
int Lua_Print(lua_State *L) {
Scratch scratch;
int nargs = lua_gettop(L);
View *null_view = GetView(NullViewID);
for (int i = 1; i <= nargs; i += 1) {
String string = lua_tostring(L, i);
Command_Append(null_view, string, true);
}
lua_pop(L, nargs);
return 0;
}
int Lua_Kill(lua_State *L) {
BSet main = GetActiveMainSet();
KillProcess(main.view);
@@ -188,7 +200,7 @@ int Lua_Open(lua_State *L) {
Scratch scratch;
String path = luaL_checkstring(L, 1);
lua_pop(L, 1);
Open(path);
Command_Open(path);
return 0;
}
@@ -211,16 +223,6 @@ int Lua_ToggleFullscreen(lua_State *L) {
return 0;
}
int Lua_Print(lua_State *L) {
Scratch scratch;
String string = lua_tostring(L, 1);
lua_pop(L, 1);
View *null_view = GetView(NullViewID);
Command_Append(null_view, string, true);
return 0;
}
int Lua_ListBuffers(lua_State *L) {
Command_ListBuffers();
return 0;
@@ -240,28 +242,11 @@ int Lua_GetBufferList(lua_State *L) {
return 1;
}
int Lua_FileExists(lua_State *L) {
String path = luaL_checkstring(L, 1);
int Lua_BufferExists(lua_State *L) {
String string = lua_tostring(L, 1);
lua_pop(L, 1);
bool exists = FileExists(path);
lua_pushboolean(L, exists);
return 1;
}
int Lua_GetWorkingDir(lua_State *L) {
lua_pushlstring(L, WorkingDir.data, WorkingDir.len);
return 1;
}
int Lua_GetActiveMainWindowBufferName(lua_State *L) {
String name = GetActiveMainWindowBufferName();
lua_pushlstring(L, name.data, name.len);
return 1;
}
int Lua_GetActiveMainWindowBufferDir(lua_State *L) {
String name = GetActiveMainWindowBufferDir();
lua_pushlstring(L, name.data, name.len);
Buffer *buffer = BufferNameExists(string);
lua_pushboolean(L, buffer != NULL);
return 1;
}
@@ -284,7 +269,7 @@ int Lua_Ls(lua_State *L) {
String buffer_name = GetUniqueBufferName(scratch, GetDir(main.buffer), "+ls-");
Buffer *buffer = CreateBuffer(GetSystemAllocator(), buffer_name, 4096 * 4);
ListFilesRecursive(buffer, GetActiveMainWindowBufferDir());
ListFilesRecursive(buffer, Command_GetDir());
WindowOpenBufferView(main.window, buffer_name);
return 0;
@@ -338,6 +323,25 @@ int Lua_GetProjectPath(lua_State *L) {
return 1;
}
int Lua_FileExists(lua_State *L) {
String path = luaL_checkstring(L, 1);
lua_pop(L, 1);
bool exists = FileExists(path);
lua_pushboolean(L, exists);
return 1;
}
int Lua_GetWorkingDir(lua_State *L) {
lua_pushlstring(L, WorkingDir.data, WorkingDir.len);
return 1;
}
int Lua_GetDir(lua_State *L) {
String name = Command_GetDir();
lua_pushlstring(L, name.data, name.len);
return 1;
}
static void HookLuaForceExit(lua_State *L, lua_Debug *debug) {
SDL_PumpEvents();
int numkeys = 0;
@@ -500,20 +504,6 @@ void ReloadLuaConfigs() {
}
}
bool CallIsLoadWord(char16_t c) {
lua_getglobal(LuaState, "IsLoadWord");
lua_pushinteger(LuaState, c);
if (lua_pcall(LuaState, 1, 1, 0) != 0) {
const char *error_message = lua_tostring(LuaState, -1);
ReportWarningf("Failed the call to IsLoadWord! %s", error_message);
lua_pop(LuaState, 1);
return false;
}
bool result = lua_toboolean(LuaState, -1);
lua_pop(LuaState, 1);
return result;
}
void CallOnCommand(Event *event) {
lua_getglobal(LuaState, "OnCommand");
PushEvent(LuaState, event);

View File

@@ -3,6 +3,7 @@ luaL_Reg LuaFunctions[] = {
{"Eval", Lua_Eval},
{"C", Lua_C},
{"Cmd", Lua_Cmd},
{"Print", Lua_Print},
{"Kill", Lua_Kill},
{"GetLoadWord", Lua_GetLoadWord},
{"New", Lua_New},
@@ -10,13 +11,9 @@ luaL_Reg LuaFunctions[] = {
{"SetProjectFile", Lua_SetProjectFile},
{"Reopen", Lua_Reopen},
{"ToggleFullscreen", Lua_ToggleFullscreen},
{"Print", Lua_Print},
{"ListBuffers", Lua_ListBuffers},
{"GetBufferList", Lua_GetBufferList},
{"FileExists", Lua_FileExists},
{"GetWorkingDir", Lua_GetWorkingDir},
{"GetActiveMainWindowBufferName", Lua_GetActiveMainWindowBufferName},
{"GetActiveMainWindowBufferDir", Lua_GetActiveMainWindowBufferDir},
{"BufferExists", Lua_BufferExists},
{"Ls", Lua_Ls},
{"Rename", Lua_Rename},
{"GetSelection", Lua_GetSelection},
@@ -24,6 +21,9 @@ luaL_Reg LuaFunctions[] = {
{"GetClipboard", Lua_GetClipboard},
{"GetFilename", Lua_GetFilename},
{"GetProjectPath", Lua_GetProjectPath},
{"FileExists", Lua_FileExists},
{"GetWorkingDir", Lua_GetWorkingDir},
{"GetDir", Lua_GetDir},
{"Play", Lua_Play},
{NULL, NULL},
};

View File

@@ -238,7 +238,7 @@ String GetUniqueBufferName(Allocator allocator, String working_dir, String p
return buffer_name;
}
String GetActiveMainWindowBufferName() {
String Command_GetFilename() {
BSet set = GetActiveMainSet();
return set.buffer->name;
}
@@ -248,7 +248,7 @@ String GetDir(Buffer *buffer) {
return name;
}
String GetActiveMainWindowBufferDir() {
String Command_GetDir() {
BSet set = GetActiveMainSet();
String name = ChopLastSlash(set.buffer->name);
return name;

View File

@@ -221,7 +221,7 @@ Array<Event> GetEventsForFrame(Allocator allocator) {
void Windows_SetupVCVarsall(mco_coro *co) {
Scratch scratch;
String working_dir = GetActiveMainWindowBufferDir();
String working_dir = Command_GetDir();
String buffer_name = GetUniqueBufferName(scratch, working_dir, "+cmd-");
String cmd = Format(scratch, "\"%.*s\" && set", FmtString(StyleVCVarsall));
View *view = Command_ExecHidden(buffer_name, cmd, working_dir);

View File

@@ -84,8 +84,8 @@ bool Command_EvalLua(View *view, String16 string);
Rect2I GetVisibleCells(Window *window);
void AfterEdit(View *view, Array<Edit> edits);
Scroller ComputeScrollerRect(Window *window);
void Open(String path);
void Open(String16 path);
void Command_Open(String path);
void Command_Open(String16 path);
void UpdateScroll(Window *window, bool update_caret_scrolling);
void Command_SelectEntireBuffer(View *view);