-- Default user-commands, autocmds, mappings, menus.
local nvim_on = require('vim._core.util').nvim_on
--- Default user commands
do
vim.api.nvim_create_user_command('Inspect', function(cmd)
local opts = {} --- @type vim.inspect_pos.Opts
local row, col --- @type integer?, integer?
if cmd.range > 0 then
row, col = cmd.line1 - 1, 0
opts.end_row, opts.end_col = cmd.line2, 0
end
if cmd.bang then
vim.print(vim.inspect_pos(0, row, col, opts))
else
vim.show_pos(0, row, col, opts)
end
end, {
desc = 'Inspect highlights and extmarks at the cursor or in a line range',
bang = true,
range = true,
})
vim.api.nvim_create_user_command('InspectTree', function(cmd)
local opts = { lang = cmd.fargs[1] }
if cmd.mods ~= '' or cmd.count ~= 0 then
local count = cmd.count ~= 0 and cmd.count or ''
local new = cmd.mods ~= '' and 'new' or 'vnew'
opts.command = ('%s %s%s'):format(cmd.mods, count, new)
end
vim.treesitter.inspect_tree(opts)
end, { desc = 'Inspect treesitter language tree for buffer', count = true, nargs = '?' })
vim.api.nvim_create_user_command('EditQuery', function(cmd)
vim.treesitter.query.edit(cmd.fargs[1])
end, {
desc = 'Edit treesitter query',
nargs = '?',
complete = function()
return vim.treesitter.language._complete()
end,
})
vim.api.nvim_create_user_command('Open', function(cmd)
if #cmd.fargs == 0 then
local current_file = vim.fn.expand('%')
if current_file ~= '' then
vim.ui.open(current_file)
end
else
vim.ui.open(assert(cmd.fargs[1]))
end
end, {
desc = 'Open file with system default handler. See :help vim.ui.open()',
nargs = '?',
complete = 'file',
})
end
--- Default mappings
do
--- Default maps for * and # in visual mode.
---
--- See |v_star-default| and |v_#-default|
do
--- @param forward 0|1
local function _visual_search(forward)
local pos = vim.fn.getpos('.')
local vpos = vim.fn.getpos('v')
local mode = vim.fn.mode()
local chunks = vim.fn.getregion(pos, vpos, { type = mode })
local esc_chunks = vim
.iter(chunks)
:map(function(v)
return vim.fn.escape(v, [[\]])
end)
:totable()
local esc_pat = table.concat(esc_chunks, [[\n]])
if #esc_pat == 0 then
vim.api.nvim_echo({ { 'E348: No string under cursor' } }, true, { err = true })
return '<Esc>'
end
local search = [[\V]] .. esc_pat
vim.fn.setreg('/', search)
vim.fn.histadd('/', search)
vim.v.searchforward = forward
-- The count has to be adjusted when searching backwards and the cursor
-- isn't positioned at the beginning of the selection
local count = vim.v.count1
if forward == 0 then
local _, line, col, _ = unpack(pos)
local _, vline, vcol, _ = unpack(vpos)
if
line > vline
or mode == 'v' and line == vline and col > vcol
or mode == 'V' and col ~= 1
or mode == '\22' and col > vcol
then
count = count + 1
end
end
return '<Esc>' .. count .. 'n'
end
vim.keymap.set('x', '*', function()
return _visual_search(1)
end, { desc = ':help v_star-default', expr = true })
vim.keymap.set('x', '#', function()
return _visual_search(0)
end, { desc = ':help v_#-default', expr = true })
end
--- Map Y to y$. This mimics the behavior of D and C. See |Y-default|
vim.keymap.set('n', 'Y', 'y$', { desc = ':help Y-default' })
--- Use normal! <C-L> to prevent inserting raw <C-L> when using i_<C-O>. #17473
---
--- See |CTRL-L-default|
vim.keymap.set(
'n',
'<C-L>',
'<Cmd>nohlsearch<Bar>diffupdate'
.. '<Bar>call nvim_buf_clear_namespace(0, nvim_create_namespace("nvim.multicursor"), 0, -1)'
.. '<Bar>normal! <C-L><CR>',
{ desc = ':help CTRL-L-default' }
)
--- Set undo points when deleting text in insert mode.
---
--- See |i_CTRL-U-default| and |i_CTRL-W-default|
vim.keymap.set('i', '<C-U>', '<C-G>u<C-U>', { desc = ':help i_CTRL-U-default' })
vim.keymap.set('i', '<C-W>', '<C-G>u<C-W>', { desc = ':help i_CTRL-W-default' })
--- Use the same flags as the previous substitution with &.
---
--- Use : instead of <Cmd> so that ranges are supported. #19365
---
--- See |&-default|
vim.keymap.set('n', '&', ':&&<CR>', { desc = ':help &-default' })
--- Use @ in Visual mode to execute a macro on each line of the selection. #21422
--- This only make sense in linewise Visual mode. #28287
---
--- Includes @@ too.
vim.keymap.set(
'x',
'@',
"mode() ==# 'V' ? ':normal! @'.getcharstr().'<CR>' : '@'",
{ silent = true
|