fix(defaults): don't use nvim_feedkeys in default mappings (#24520)

Problem:    Using nvim_feedkeys in default mappings makes it hard to use
            them as a part of another mapping.
Solution:   Use an expression mapping and stop Visual mode later.

Fix #24518.
This commit is contained in:
zeertzjq 2023-08-01 18:07:02 +08:00 committed by GitHub
parent 9b5f58185e
commit ccb5a76e5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1016,8 +1016,13 @@ function vim._init_default_mappings()
local function _visual_search(cmd) local function _visual_search(cmd)
assert(cmd == '/' or cmd == '?') assert(cmd == '/' or cmd == '?')
vim.api.nvim_feedkeys('\27', 'nx', true) -- Escape visual mode. local region = vim.region(
local region = vim.region(0, "'<", "'>", vim.fn.visualmode(), vim.o.selection == 'inclusive') 0,
'.',
'v',
vim.api.nvim_get_mode().mode:sub(1, 1),
vim.o.selection == 'inclusive'
)
local chunks = region_chunks(region) local chunks = region_chunks(region)
local esc_chunks = vim local esc_chunks = vim
.iter(chunks) .iter(chunks)
@ -1027,7 +1032,7 @@ function vim._init_default_mappings()
:totable() :totable()
local esc_pat = table.concat(esc_chunks, [[\n]]) local esc_pat = table.concat(esc_chunks, [[\n]])
local search_cmd = ([[%s\V%s%s]]):format(cmd, esc_pat, '\n') local search_cmd = ([[%s\V%s%s]]):format(cmd, esc_pat, '\n')
vim.api.nvim_feedkeys(search_cmd, 'nx', true) return '\27' .. search_cmd
end end
local function map(mode, lhs, rhs) local function map(mode, lhs, rhs)
@ -1040,11 +1045,11 @@ function vim._init_default_mappings()
map('i', '<C-U>', '<C-G>u<C-U>') map('i', '<C-U>', '<C-G>u<C-U>')
map('i', '<C-W>', '<C-G>u<C-W>') map('i', '<C-W>', '<C-G>u<C-W>')
vim.keymap.set('x', '*', function() vim.keymap.set('x', '*', function()
_visual_search('/') return _visual_search('/')
end, { desc = ':help v_star-default', silent = true }) end, { desc = ':help v_star-default', expr = true, silent = true })
vim.keymap.set('x', '#', function() vim.keymap.set('x', '#', function()
_visual_search('?') return _visual_search('?')
end, { desc = ':help v_#-default', silent = true }) end, { desc = ':help v_#-default', expr = true, silent = true })
-- Use : instead of <Cmd> so that ranges are supported. #19365 -- Use : instead of <Cmd> so that ranges are supported. #19365
map('n', '&', ':&&<CR>') map('n', '&', ':&&<CR>')