Style.TrimWhitespaceOnSave = true Style.ClangFormatOnSave = false 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_EQUALS = 0x0000003d SDLK_MINUS = 0x0000002d 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 IsAlpha(a) if a == nil then return false end local x = string.byte(a) local result = (x >= string.byte('a') and x <= string.byte('z')) or (x >= string.byte('A') and x <= string.byte('Z')) return result end 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, meta) 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 = GetMainDir() 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, meta) 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 = GetMainDir()} end return nil end function MatchURL(s, meta) local i, j = string.find(s, "^https://") if i then return {kind = "exec_console", cmd = '"'..INTERNET_BROWSER..'" '..s, working_dir = GetMainDir()} end return nil end function MatchGotoBuild(s, meta) if meta ~= "goto_build" then return nil end local windows_path = IsAlpha(s:sub(1,1)) and s:sub(2,2) == ':' local windows_rela = s:sub(1,1) == '.' and s:sub(2,2) == '\\' local unix_path = s:sub(1,1) == '/' local unix_rela = s:sub(1,1) == '.' and s:sub(2,2) == '/' if not windows_path and not windows_rela and not unix_path and not windows_rela then return {kind = "skip"} end return nil end function MatchExec(s, meta) if meta ~= "exec" then return nil end if s:match(".exe$") then return {kind = "exec_console", cmd = s, working_dir = GetMainDir()} end Eval(s) return {kind = "skip"} end OnOpenMatchers = { MatchWindowsPath, MatchGitCommit, MatchURL, MatchGotoBuild, MatchExec, } function OnOpen(path, meta) for i = #OnOpenMatchers,1,-1 do rule = OnOpenMatchers[i] result = rule(path, meta) 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_B and e.ctrl == 1 then Cmd { working_dir = GetProjectDir(), kind = "console", cmd = "build.bat" } 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 function OnSave(buffer_id) local dont_trim_lines_with_caret = false ConvertLineEndingsToLF(buffer_id, dont_trim_lines_with_caret) if Style.TrimWhitespaceOnSave then TrimTrailingWhitespace(buffer_id, dont_trim_lines_with_caret) end local name = GetBufferNameByID(buffer_id) if Style.ClangFormatOnSave and (name:match(".c$") or name:match(".h$") or name:match(".cpp$") or name:match(".hpp$")) then ApplyClangFormat(buffer_id) end end