diff --git a/init.lua b/init.lua index 6a6b555..0d65b3a 100644 --- a/init.lua +++ b/init.lua @@ -1,24 +1,4 @@ --- Disable Kitty keyboard protocol to avoid double Enter/Tab/Backspace on --- Kitty and Alacritty (common on WSL GUI terminals with Neovim 0.11+). -local function kitty_protocol_terminal() - local term = vim.env.TERM or "" - if term:match("kitty") or term:match("alacritty") then - return true - end - return vim.env.KITTY_WINDOW_ID ~= nil or vim.env.ALACRITTY_WINDOW_ID ~= nil -end - -if kitty_protocol_terminal() then - vim.api.nvim_create_autocmd({ "VimEnter", "VimLeavePre" }, { - callback = function(ev) - if ev.event == "VimEnter" then - io.stdout:write("\027[>1u") - else - io.stdout:write("\027[<1u") - end - end, - }) -end +require("config.term").setup() -- bootstrap lazy.nvim, LazyVim and your plugins require("config.lazy") diff --git a/lua/config/autocmds.lua b/lua/config/autocmds.lua index 3d140ed..e5c7bfa 100644 --- a/lua/config/autocmds.lua +++ b/lua/config/autocmds.lua @@ -102,15 +102,51 @@ vim.api.nvim_create_user_command( { desc = "show hi name" } ) +local in_cmdline = false +local colorscheme_picker_opening = false + local function open_colorscheme_picker() - -- Defer past cmdline teardown; keymaps don't need this, :commands do. - vim.schedule(function() + -- Double Enter (kitty protocol) can invoke the command twice and toggle-close. + if colorscheme_picker_opening then + return + end + colorscheme_picker_opening = true + + vim.defer_fn(function() + colorscheme_picker_opening = false + + local ok, picker_api = pcall(require, 'snacks.picker') + if not ok then + return + end + + if picker_api.get({ source = "colorschemes" })[1] then + return + end + require("snacks").picker.colorschemes({ auto_close = false }) - end) + end, 100) end +vim.api.nvim_create_autocmd("CmdlineEnter", { + callback = function() + in_cmdline = true + pcall(vim.diagnostic.hide) + end, +}) + +vim.api.nvim_create_autocmd("CmdlineLeave", { + callback = function() + in_cmdline = false + end, +}) + vim.api.nvim_create_autocmd("CursorHold", { callback = function() + if in_cmdline then + return + end + -- Cmdline/wildmenu: avoid diagnostic float stealing focus (WSL tab completion jump). local mode = vim.api.nvim_get_mode().mode if mode:find("^c") or vim.fn.getcmdwintype() ~= "" then @@ -122,7 +158,7 @@ vim.api.nvim_create_autocmd("CursorHold", { return end - local ok, picker = pcall(require, "snacks.picker") + local ok, picker = pcall(require, 'snacks.picker') if ok and #picker.get() > 0 then return end diff --git a/lua/config/term.lua b/lua/config/term.lua new file mode 100644 index 0000000..3c75269 --- /dev/null +++ b/lua/config/term.lua @@ -0,0 +1,65 @@ +local M = {} + +--- Kitty/Alacritty on WSL often reports a generic $TERM; detect via env too. +function M.needs_key_protocol_fix() + if vim.env.NVIM_DISABLE_KEY_PROTOCOL == "1" then + return true + end + + if vim.env.KITTY_WINDOW_ID or vim.env.ALACRITTY_WINDOW_ID then + return true + end + + local term = vim.env.TERM or "" + if term:match("kitty") or term:match("alacritty") then + return true + end + + if vim.env.WSL_DISTRO_NAME and (vim.env.WAYLAND_DISPLAY or vim.env.DISPLAY) then + return true + end + + return false +end + +--- Neovim 0.12+ enables kitty protocol with flags=3 (disambiguate + report key-up). +--- That makes Enter/Tab/Backspace fire on keydown and keyup in Kitty/Alacritty (#31806). +--- Push flags=1 (disambiguate only) after Neovim starts so each keypress fires once. +function M.fix_key_protocol() + io.stdout:write("\027[>1u") + io.stdout:flush() +end + +local function schedule_fix() + for _, delay in ipairs({ 0, 50, 150, 500 }) do + vim.defer_fn(M.fix_key_protocol, delay) + end +end + +function M.setup() + if not M.needs_key_protocol_fix() then + return + end + + schedule_fix() + + vim.api.nvim_create_autocmd("VimEnter", { + callback = schedule_fix, + }) + + if vim.fn.exists("##UIEnter") == 1 then + vim.api.nvim_create_autocmd("UIEnter", { + once = true, + callback = schedule_fix, + }) + end + + vim.api.nvim_create_autocmd("VimLeavePre", { + callback = function() + io.stdout:write("\027[<1u") + io.stdout:flush() + end, + }) +end + +return M