mirror of
https://github.com/hyzendust/KickestEnd.nvim.git
synced 2026-02-15 09:21:12 +01:00
Update: smooth double q to exit
This commit is contained in:
@@ -688,20 +688,65 @@ vim.keymap.set('n', 'gf', function()
|
|||||||
end, { desc = 'Smart gf: open file under cursor in new tab or reuse buffer' })
|
end, { desc = 'Smart gf: open file under cursor in new tab or reuse buffer' })
|
||||||
|
|
||||||
-- [[ Remap double q to exit insert/visual/terminal modes ]]
|
-- [[ Remap double q to exit insert/visual/terminal modes ]]
|
||||||
vim.o.timeoutlen = 300 -- wait 300ms for second q
|
local double_q_timeout = 400
|
||||||
-- handler
|
local q_timer = nil
|
||||||
local function double_q_exit(mode)
|
local q_pending = false
|
||||||
if mode == 't' then
|
local function send_keys(keys)
|
||||||
-- send <C-\><C-n> to exit terminal mode
|
return vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(keys, true, false, true), 'n', false)
|
||||||
return vim.api.nvim_replace_termcodes('<C-\\><C-n>', true, false, true)
|
end
|
||||||
|
local function q_double_escape()
|
||||||
|
if q_timer then
|
||||||
|
q_timer:stop()
|
||||||
|
q_timer:close()
|
||||||
|
q_timer = nil
|
||||||
|
q_pending = false
|
||||||
|
local mode = vim.api.nvim_get_mode().mode
|
||||||
|
if mode:match '[iIcR]' then
|
||||||
|
return vim.api.nvim_replace_termcodes('<Esc>', true, false, true)
|
||||||
|
elseif mode:match '[vV\x16]' then
|
||||||
|
return vim.api.nvim_replace_termcodes('<Esc>', true, false, true)
|
||||||
|
elseif mode:match 't' then
|
||||||
|
return vim.api.nvim_replace_termcodes('<C-\\><C-n>', true, false, true)
|
||||||
|
else
|
||||||
|
return ''
|
||||||
|
end
|
||||||
else
|
else
|
||||||
-- send <Esc> to exit insert/visual modes
|
q_pending = true
|
||||||
return vim.api.nvim_replace_termcodes('<Esc>', true, false, true)
|
q_timer = vim.loop.new_timer()
|
||||||
|
q_timer:start(
|
||||||
|
double_q_timeout,
|
||||||
|
0,
|
||||||
|
vim.schedule_wrap(function()
|
||||||
|
send_keys 'q'
|
||||||
|
q_timer:stop()
|
||||||
|
q_timer:close()
|
||||||
|
q_timer = nil
|
||||||
|
q_pending = false
|
||||||
|
end)
|
||||||
|
)
|
||||||
|
return ''
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
vim.keymap.set({ 'i', 'v' }, 'qq', function()
|
local function flush_pending_q_before(ch)
|
||||||
return double_q_exit 'i'
|
if q_pending then
|
||||||
end, { expr = true, noremap = true })
|
q_timer:stop()
|
||||||
vim.keymap.set('t', 'qq', function()
|
q_timer:close()
|
||||||
return double_q_exit 't'
|
q_timer = nil
|
||||||
end, { expr = true, noremap = true })
|
q_pending = false
|
||||||
|
send_keys('q' .. ch)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
for i = 32, 126 do
|
||||||
|
local ch = string.char(i)
|
||||||
|
if ch ~= 'q' then
|
||||||
|
vim.keymap.set({ 'i', 'v', 't' }, ch, function()
|
||||||
|
if flush_pending_q_before(ch) then
|
||||||
|
return ''
|
||||||
|
end
|
||||||
|
return ch
|
||||||
|
end, { noremap = true, expr = true, silent = true })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
vim.keymap.set({ 'i', 'v', 't' }, 'q', q_double_escape, { noremap = true, expr = true, silent = true })
|
||||||
|
|||||||
Reference in New Issue
Block a user