From fd8a319b18515331612a5d31df7cc77f0c3b0b2e Mon Sep 17 00:00:00 2001 From: krzosa Date: Thu, 1 May 2025 15:06:48 +0200 Subject: [PATCH] init.lua --- data/init.lua | 257 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 257 insertions(+) create mode 100644 data/init.lua diff --git a/data/init.lua b/data/init.lua new file mode 100644 index 0000000..577a374 --- /dev/null +++ b/data/init.lua @@ -0,0 +1,257 @@ +INTERNET_BROWSER = 'C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe' + +SDLK_CTRL = 1073742048 +SDLK_PAGE_DOWN = 1073741902 +SDLK_PAGE_UP = 1073741899 +SDLK_DOWN = 1073741905 -- 0x40000051 +SDLK_UP = 1073741906 -- 0x40000052u +SDLK_RIGHT = 1073741903 +SDLK_LEFT = 1073741904 +SDLK_Q = 113 +SDLK_BACKSLASH = 0x5c +SDLK_RETURN = 13 + +SDLK_F1 = 0x4000003a +SDLK_F2 = 0x4000003b +SDLK_F3 = 0x4000003c +SDLK_F4 = 0x4000003d +SDLK_F5 = 0x4000003e +SDLK_F6 = 0x4000003f +SDLK_F7 = 0x40000040 +SDLK_F8 = 0x40000041 +SDLK_F9 = 0x40000042 +SDLK_F10 = 0x40000043 +SDLK_F11 = 0x40000044 +SDLK_F12 = 0x40000045 + +SDLK_A = 0x00000061 +SDLK_B = 0x00000062 +SDLK_C = 0x00000063 +SDLK_D = 0x00000064 +SDLK_E = 0x00000065 +SDLK_F = 0x00000066 +SDLK_G = 0x00000067 +SDLK_H = 0x00000068 +SDLK_I = 0x00000069 +SDLK_J = 0x0000006a +SDLK_K = 0x0000006b +SDLK_L = 0x0000006c +SDLK_M = 0x0000006d +SDLK_N = 0x0000006e +SDLK_O = 0x0000006f +SDLK_P = 0x00000070 +SDLK_Q = 0x00000071 +SDLK_R = 0x00000072 +SDLK_S = 0x00000073 +SDLK_T = 0x00000074 +SDLK_U = 0x00000075 +SDLK_V = 0x00000076 +SDLK_W = 0x00000077 +SDLK_X = 0x00000078 +SDLK_Y = 0x00000079 +SDLK_Z = 0x0000007a + +function SkipLineAndColumn(s) + local line, col = "1", "1" + + function parse_line_and_column(line_and_col, delimiter) + ic, jc = line_and_col:find(delimiter) + line = line_and_col:sub(1, ic - 1) + col = line_and_col:sub(ic + 1) + return line, col + end + + do -- :line:column + local i, j = s:find("^:%d+:%d+") + if i then + skip_pattern = s:sub(j + 1) + line, col = parse_line_and_column(s:sub(2, j), ":") + return line, col, skip_pattern + end + end + + do -- :line + local i, j = s:find("^:%d+") + if i then + skip_pattern = s:sub(j + 1) + line = s:sub(2, j) + return line, col, skip_pattern + end + end + + do -- (line,column) + local i, j = s:find("^%(%d+,%d+%)") + if i then + skip_pattern = s:sub(j + 1) + line, col = parse_line_and_column(s:sub(2, j - 1), ",") + return line, col, skip_pattern + end + end + + do -- (line) + local i, j = s:find("^%(%d+%)") + if i then + skip_pattern = s:sub(j + 1) + line = s:sub(2, j - 1) + return line, col, skip_pattern + end + end + + return line, col, s +end + +function SkipSlashes(s) + found_slash = false + while s:sub(1,1) == '/' or s:sub(1,1) == '\\' do + s = s:sub(2) + found_slash = true + end + return s, found_slash +end + +function SkipDrive(s) + local i, j = s:find("^%a:") + if not i then + return s, nil, true + end + + local drive = s:sub(i, j) + local new_s = s:sub(j + 1) + + new_s, found_slash = SkipSlashes(new_s) + if not found_slash then + return s, drive, false + end + + return new_s, drive, true +end + +function SkipPathCell(s) + local i, j = s:find("^[%w_%.-% +]+") + if not i then return s, nil, false end + + local word = s:sub(i, j) + local new_s = s:sub(j + 1) + local new_s, found_slash = SkipSlashes(new_s) + + return new_s, word, found_slash +end + +function SkipPath(s) + local input_s = s + local s, drive, ok = SkipDrive(s) + if not ok then return s end + + local cells = {} + if drive ~= nil then + table.insert(cells, drive) + end + + while true do + s, word, slash_eaten = SkipPathCell(s) + if word then cells[#cells + 1] = word end + if not slash_eaten then break end + end + + if #cells == 0 then return s end + + local skip_size = input_s:len() - s:len() + local path = input_s:sub(1, skip_size) + return s, path, drive, cells +end + +function MatchWindowsPath(_s) + local s, file_path, drive = SkipPath(_s) + + if not file_path and FileExists(drive) then + return {kind = "text", file_path = drive, line = line, col = col} + end + + if not file_path then + return nil + end + + if not drive then + local d = GetDir() + file_path = d..'/'..file_path + end + local line, col, s = SkipLineAndColumn(s) + + local exists = FileExists(file_path) or BufferExists(file_path) + if not exists then return nil end + + return {kind = "text", file_path = file_path, line = line, col = col} +end + +function MatchGitCommit(s) + local i, j = string.find(s, "^commit ([a-zA-Z0-9]+)") + if i then + s = s:sub(8) + local command = "git --no-pager show "..s + return {kind = "exec", cmd = command, working_dir = GetDir()} + end + return nil +end + +function MatchURL(s) + local i, j = string.find(s, "^https://") + if i then + return {kind = "exec_console", cmd = '"'..INTERNET_BROWSER..'" '..s, working_dir = GetDir()} + end + return nil +end + +Rules = { + MatchWindowsPath, + MatchGitCommit, + MatchURL, +} + +function ApplyRules(s) + for i = #Rules,1,-1 do + rule = Rules[i] + result = rule(s) + if result then + return result + end + end + return nil +end + +Coroutines = {} +function AddCo(f) + local i = #Coroutines + 1 + Coroutines[i] = coroutine.create(f) + coroutine.resume(Coroutines[i]) + return Coroutines[i] +end + +OnCommandCallbacks = {} +table.insert(OnCommandCallbacks, function (e) + if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then + C("git grep -n "..GetLoadWord().." :/") end + if e.key == SDLK_L and e.ctrl == 1 then + Eval(GetLoadWord()) end +end) + +function OnUpdate(e) + local new_co_list = {} + for key, co in pairs(Coroutines) do + local status = coroutine.status(co) + if status ~= "dead" then + coroutine.resume(co, e) + new_co_list[#new_co_list + 1] = co + end + end + Coroutines = new_co_list +end + +function OnCommand(e) + for i = #OnCommandCallbacks,1,-1 do + on_command = OnCommandCallbacks[i] + on_command(e) + end +end + +function OnInit() +end \ No newline at end of file