|
|
|
|
|
do |
|
|
vim.api.nvim_create_user_command('Inspect', function(cmd) |
|
|
if cmd.bang then |
|
|
vim.print(vim.inspect_pos()) |
|
|
else |
|
|
vim.show_pos() |
|
|
end |
|
|
end, { desc = 'Inspect highlights and extmarks at the cursor', bang = 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 = '?' }) |
|
|
|
|
|
vim.api.nvim_create_user_command('Open', function(cmd) |
|
|
vim.ui.open(cmd.fargs[1]) |
|
|
end, { |
|
|
desc = 'Open file with system default handler. See :help vim.ui.open()', |
|
|
nargs = 1, |
|
|
complete = 'file', |
|
|
}) |
|
|
end |
|
|
|
|
|
|
|
|
do |
|
|
|
|
|
|
|
|
|
|
|
do |
|
|
local function _visual_search(forward) |
|
|
assert(forward == 0 or forward == 1) |
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
vim.keymap.set('n', 'Y', 'y$', { desc = ':help Y-default' }) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vim.keymap.set('n', '<C-L>', '<Cmd>nohlsearch<Bar>diffupdate<Bar>normal! <C-L><CR>', { |
|
|
desc = ':help CTRL-L-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' }) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vim.keymap.set('n', '&', ':&&<CR>', { desc = ':help &-default' }) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vim.keymap.set( |
|
|
'x', |
|
|
'Q', |
|
|
"mode() ==# 'V' ? ':normal! @<C-R>=reg_recorded()<CR><CR>' : 'Q'", |
|
|
{ silent = true, expr = true, desc = ':help v_Q-default' } |
|
|
) |
|
|
vim.keymap.set( |
|
|
'x', |
|
|
'@', |
|
|
"mode() ==# 'V' ? ':normal! @'.getcharstr().'<CR>' : '@'", |
|
|
{ silent = true, expr = true, desc = ':help v_@-default' } |
|
|
) |
|
|
|
|
|
|
|
|
do |
|
|
local function do_open(uri) |
|
|
local cmd, err = vim.ui.open(uri) |
|
|
local rv = cmd and cmd:wait(1000) or nil |
|
|
if cmd and rv and rv.code ~= 0 then |
|
|
err = ('vim.ui.open: command %s (%d): %s'):format( |
|
|
(rv.code == 124 and 'timeout' or 'failed'), |
|
|
rv.code, |
|
|
vim.inspect(cmd.cmd) |
|
|
) |
|
|
end |
|
|
return err |
|
|
end |
|
|
|
|
|
local gx_desc = |
|
|
'Opens filepath or URI under cursor with the system handler (file explorer, web browser, …)' |
|
|
vim.keymap.set({ 'n' }, 'gx', function() |
|
|
for _, url in ipairs(require('vim.ui')._get_urls()) do |
|
|
local err = do_open(url) |
|
|
if err then |
|
|
vim.notify(err, vim.log.levels.ERROR) |
|
|
end |
|
|
end |
|
|
end, { desc = gx_desc }) |
|
|
vim.keymap.set({ 'x' }, 'gx', function() |
|
|
local lines = |
|
|
vim.fn.getregion(vim.fn.getpos('.'), vim.fn.getpos('v'), { type = vim.fn.mode() }) |
|
|
|
|
|
local err = do_open(table.concat(vim.iter(lines):map(vim.trim):totable())) |
|
|
if err then |
|
|
vim.notify(err, vim.log.levels.ERROR) |
|
|
end |
|
|
end, { desc = gx_desc }) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do |
|
|
local operator_rhs = function() |
|
|
return require('vim._comment').operator() |
|
|
end |
|
|
vim.keymap.set({ 'n', 'x' }, 'gc', operator_rhs, { expr = true, desc = 'Toggle comment' }) |
|
|
|
|
|
local line_rhs = function() |
|
|
return require('vim._comment').operator() .. '_' |
|
|
end |
|
|
vim.keymap.set('n', 'gcc', line_rhs, { expr = true, desc = 'Toggle comment line' }) |
|
|
|
|
|
local textobject_rhs = function() |
|
|
require('vim._comment').textobject() |
|
|
end |
|
|
vim.keymap.set({ 'o' }, 'gc', textobject_rhs, { desc = 'Comment textobject' }) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do |
|
|
vim.keymap.set('n', 'grn', function() |
|
|
vim.lsp.buf.rename() |
|
|
end, { desc = 'vim.lsp.buf.rename()' }) |
|
|
|
|
|
vim.keymap.set({ 'n', 'x' }, 'gra', function() |
|
|
vim.lsp.buf.code_action() |
|
|
end, { desc = 'vim.lsp.buf.code_action()' }) |
|
|
|
|
|
vim.keymap.set('n', 'grr', function() |
|
|
vim.lsp.buf.references() |
|
|
end, { desc = 'vim.lsp.buf.references()' }) |
|
|
|
|
|
vim.keymap.set('n', 'gri', function() |
|
|
vim.lsp.buf.implementation() |
|
|
end, { desc = 'vim.lsp.buf.implementation()' }) |
|
|
|
|
|
vim.keymap.set('n', 'grt', function() |
|
|
vim.lsp.buf.type_definition() |
|
|
end, { desc = 'vim.lsp.buf.type_definition()' }) |
|
|
|
|
|
vim.keymap.set('n', 'gO', function() |
|
|
vim.lsp.buf.document_symbol() |
|
|
end, { desc = 'vim.lsp.buf.document_symbol()' }) |
|
|
|
|
|
vim.keymap.set({ 'i', 's' }, '<C-S>', function() |
|
|
vim.lsp.buf.signature_help() |
|
|
end, { desc = 'vim.lsp.buf.signature_help()' }) |
|
|
end |
|
|
|
|
|
do |
|
|
|
|
|
|
|
|
local function set_snippet_jump(direction, key) |
|
|
vim.keymap.set({ 'i', 's' }, key, function() |
|
|
if vim.snippet.active({ direction = direction }) then |
|
|
return string.format('<Cmd>lua vim.snippet.jump(%d)<CR>', direction) |
|
|
else |
|
|
return key |
|
|
end |
|
|
end, { |
|
|
desc = 'vim.snippet.jump if active, otherwise ' .. key, |
|
|
expr = true, |
|
|
silent = true, |
|
|
}) |
|
|
end |
|
|
|
|
|
set_snippet_jump(1, '<Tab>') |
|
|
set_snippet_jump(-1, '<S-Tab>') |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do |
|
|
vim.keymap.set('n', ']d', function() |
|
|
vim.diagnostic.jump({ count = vim.v.count1 }) |
|
|
end, { desc = 'Jump to the next diagnostic in the current buffer' }) |
|
|
|
|
|
vim.keymap.set('n', '[d', function() |
|
|
vim.diagnostic.jump({ count = -vim.v.count1 }) |
|
|
end, { desc = 'Jump to the previous diagnostic in the current buffer' }) |
|
|
|
|
|
vim.keymap.set('n', ']D', function() |
|
|
vim.diagnostic.jump({ count = math.huge, wrap = false }) |
|
|
end, { desc = 'Jump to the last diagnostic in the current buffer' }) |
|
|
|
|
|
vim.keymap.set('n', '[D', function() |
|
|
vim.diagnostic.jump({ count = -math.huge, wrap = false }) |
|
|
end, { desc = 'Jump to the first diagnostic in the current buffer' }) |
|
|
|
|
|
vim.keymap.set('n', '<C-W>d', function() |
|
|
vim.diagnostic.open_float() |
|
|
end, { desc = 'Show diagnostics under the cursor' }) |
|
|
|
|
|
vim.keymap.set( |
|
|
'n', |
|
|
'<C-W><C-D>', |
|
|
'<C-W>d', |
|
|
{ remap = true, desc = 'Show diagnostics under the cursor' } |
|
|
) |
|
|
end |
|
|
|
|
|
|
|
|
do |
|
|
|
|
|
|
|
|
local function cmd(opts) |
|
|
local ok, err = pcall(vim.api.nvim_cmd, opts, {}) |
|
|
if not ok then |
|
|
vim.api.nvim_echo({ { err:sub(#'Vim:' + 1) } }, true, { err = true }) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
vim.keymap.set('n', '[q', function() |
|
|
cmd({ cmd = 'cprevious', count = vim.v.count1 }) |
|
|
end, { desc = ':cprevious' }) |
|
|
|
|
|
vim.keymap.set('n', ']q', function() |
|
|
cmd({ cmd = 'cnext', count = vim.v.count1 }) |
|
|
end, { desc = ':cnext' }) |
|
|
|
|
|
vim.keymap.set('n', '[Q', function() |
|
|
cmd({ cmd = 'crewind', count = vim.v.count ~= 0 and vim.v.count or nil }) |
|
|
end, { desc = ':crewind' }) |
|
|
|
|
|
vim.keymap.set('n', ']Q', function() |
|
|
cmd({ cmd = 'clast', count = vim.v.count ~= 0 and vim.v.count or nil }) |
|
|
end, { desc = ':clast' }) |
|
|
|
|
|
vim.keymap.set('n', '[<C-Q>', function() |
|
|
cmd({ cmd = 'cpfile', count = vim.v.count1 }) |
|
|
end, { desc = ':cpfile' }) |
|
|
|
|
|
vim.keymap.set('n', ']<C-Q>', function() |
|
|
cmd({ cmd = 'cnfile', count = vim.v.count1 }) |
|
|
end, { desc = ':cnfile' }) |
|
|
|
|
|
|
|
|
vim.keymap.set('n', '[l', function() |
|
|
cmd({ cmd = 'lprevious', count = vim.v.count1 }) |
|
|
end, { desc = ':lprevious' }) |
|
|
|
|
|
vim.keymap.set('n', ']l', function() |
|
|
cmd({ cmd = 'lnext', count = vim.v.count1 }) |
|
|
end, { desc = ':lnext' }) |
|
|
|
|
|
vim.keymap.set('n', '[L', function() |
|
|
cmd({ cmd = 'lrewind', count = vim.v.count ~= 0 and vim.v.count or nil }) |
|
|
end, { desc = ':lrewind' }) |
|
|
|
|
|
vim.keymap.set('n', ']L', function() |
|
|
cmd({ cmd = 'llast', count = vim.v.count ~= 0 and vim.v.count or nil }) |
|
|
end, { desc = ':llast' }) |
|
|
|
|
|
vim.keymap.set('n', '[<C-L>', function() |
|
|
cmd({ cmd = 'lpfile', count = vim.v.count1 }) |
|
|
end, { desc = ':lpfile' }) |
|
|
|
|
|
vim.keymap.set('n', ']<C-L>', function() |
|
|
cmd({ cmd = 'lnfile', count = vim.v.count1 }) |
|
|
end, { desc = ':lnfile' }) |
|
|
|
|
|
|
|
|
vim.keymap.set('n', '[a', function() |
|
|
cmd({ cmd = 'previous', count = vim.v.count1 }) |
|
|
end, { desc = ':previous' }) |
|
|
|
|
|
vim.keymap.set('n', ']a', function() |
|
|
|
|
|
cmd({ cmd = 'next', range = { vim.v.count1 } }) |
|
|
end, { desc = ':next' }) |
|
|
|
|
|
vim.keymap.set('n', '[A', function() |
|
|
if vim.v.count ~= 0 then |
|
|
cmd({ cmd = 'argument', count = vim.v.count }) |
|
|
else |
|
|
cmd({ cmd = 'rewind' }) |
|
|
end |
|
|
end, { desc = ':rewind' }) |
|
|
|
|
|
vim.keymap.set('n', ']A', function() |
|
|
if vim.v.count ~= 0 then |
|
|
cmd({ cmd = 'argument', count = vim.v.count }) |
|
|
else |
|
|
cmd({ cmd = 'last' }) |
|
|
end |
|
|
end, { desc = ':last' }) |
|
|
|
|
|
|
|
|
vim.keymap.set('n', '[t', function() |
|
|
|
|
|
cmd({ cmd = 'tprevious', range = { vim.v.count1 } }) |
|
|
end, { desc = ':tprevious' }) |
|
|
|
|
|
vim.keymap.set('n', ']t', function() |
|
|
|
|
|
cmd({ cmd = 'tnext', range = { vim.v.count1 } }) |
|
|
end, { desc = ':tnext' }) |
|
|
|
|
|
vim.keymap.set('n', '[T', function() |
|
|
|
|
|
cmd({ cmd = 'trewind', range = vim.v.count ~= 0 and { vim.v.count } or nil }) |
|
|
end, { desc = ':trewind' }) |
|
|
|
|
|
vim.keymap.set('n', ']T', function() |
|
|
|
|
|
if vim.v.count ~= 0 then |
|
|
cmd({ cmd = 'trewind', range = { vim.v.count } }) |
|
|
else |
|
|
cmd({ cmd = 'tlast' }) |
|
|
end |
|
|
end, { desc = ':tlast' }) |
|
|
|
|
|
vim.keymap.set('n', '[<C-T>', function() |
|
|
|
|
|
cmd({ cmd = 'ptprevious', range = { vim.v.count1 } }) |
|
|
end, { desc = ' :ptprevious' }) |
|
|
|
|
|
vim.keymap.set('n', ']<C-T>', function() |
|
|
|
|
|
cmd({ cmd = 'ptnext', range = { vim.v.count1 } }) |
|
|
end, { desc = ':ptnext' }) |
|
|
|
|
|
|
|
|
vim.keymap.set('n', '[b', function() |
|
|
cmd({ cmd = 'bprevious', count = vim.v.count1 }) |
|
|
end, { desc = ':bprevious' }) |
|
|
|
|
|
vim.keymap.set('n', ']b', function() |
|
|
cmd({ cmd = 'bnext', count = vim.v.count1 }) |
|
|
end, { desc = ':bnext' }) |
|
|
|
|
|
vim.keymap.set('n', '[B', function() |
|
|
if vim.v.count ~= 0 then |
|
|
cmd({ cmd = 'buffer', count = vim.v.count }) |
|
|
else |
|
|
cmd({ cmd = 'brewind' }) |
|
|
end |
|
|
end, { desc = ':brewind' }) |
|
|
|
|
|
vim.keymap.set('n', ']B', function() |
|
|
if vim.v.count ~= 0 then |
|
|
cmd({ cmd = 'buffer', count = vim.v.count }) |
|
|
else |
|
|
cmd({ cmd = 'blast' }) |
|
|
end |
|
|
end, { desc = ':blast' }) |
|
|
|
|
|
|
|
|
vim.keymap.set('n', '[<Space>', function() |
|
|
|
|
|
vim.go.operatorfunc = "v:lua.require'vim._buf'.space_above" |
|
|
return 'g@l' |
|
|
end, { expr = true, desc = 'Add empty line above cursor' }) |
|
|
|
|
|
vim.keymap.set('n', ']<Space>', function() |
|
|
|
|
|
vim.go.operatorfunc = "v:lua.require'vim._buf'.space_below" |
|
|
return 'g@l' |
|
|
end, { expr = true, desc = 'Add empty line below cursor' }) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
do |
|
|
|
|
|
vim.cmd([[ |
|
|
amenu PopUp.Open\ in\ web\ browser gx |
|
|
anoremenu PopUp.Inspect <Cmd>Inspect<CR> |
|
|
anoremenu PopUp.Go\ to\ definition <Cmd>lua vim.lsp.buf.definition()<CR> |
|
|
anoremenu PopUp.Show\ Diagnostics <Cmd>lua vim.diagnostic.open_float()<CR> |
|
|
anoremenu PopUp.Show\ All\ Diagnostics <Cmd>lua vim.diagnostic.setqflist()<CR> |
|
|
anoremenu PopUp.Configure\ Diagnostics <Cmd>help vim.diagnostic.config()<CR> |
|
|
anoremenu PopUp.-1- <Nop> |
|
|
vnoremenu PopUp.Cut "+x |
|
|
vnoremenu PopUp.Copy "+y |
|
|
anoremenu PopUp.Paste "+gP |
|
|
vnoremenu PopUp.Paste "+P |
|
|
vnoremenu PopUp.Delete "_x |
|
|
nnoremenu PopUp.Select\ All ggVG |
|
|
vnoremenu PopUp.Select\ All gg0oG$ |
|
|
inoremenu PopUp.Select\ All <C-Home><C-O>VG |
|
|
anoremenu PopUp.-2- <Nop> |
|
|
anoremenu PopUp.How-to\ disable\ mouse <Cmd>help disable-mouse<CR> |
|
|
]]) |
|
|
|
|
|
local function enable_ctx_menu() |
|
|
vim.cmd([[ |
|
|
amenu disable PopUp.Go\ to\ definition |
|
|
amenu disable PopUp.Open\ in\ web\ browser |
|
|
amenu disable PopUp.Show\ Diagnostics |
|
|
amenu disable PopUp.Show\ All\ Diagnostics |
|
|
amenu disable PopUp.Configure\ Diagnostics |
|
|
]]) |
|
|
|
|
|
local urls = require('vim.ui')._get_urls() |
|
|
if vim.startswith(urls[1], 'http') then |
|
|
vim.cmd([[amenu enable PopUp.Open\ in\ web\ browser]]) |
|
|
elseif vim.lsp.get_clients({ bufnr = 0 })[1] then |
|
|
vim.cmd([[anoremenu enable PopUp.Go\ to\ definition]]) |
|
|
end |
|
|
|
|
|
local lnum = vim.fn.getcurpos()[2] - 1 |
|
|
local diagnostic = false |
|
|
if next(vim.diagnostic.get(0, { lnum = lnum })) ~= nil then |
|
|
diagnostic = true |
|
|
vim.cmd([[anoremenu enable PopUp.Show\ Diagnostics]]) |
|
|
end |
|
|
|
|
|
if diagnostic or next(vim.diagnostic.count(0)) ~= nil then |
|
|
vim.cmd([[ |
|
|
anoremenu enable PopUp.Show\ All\ Diagnostics |
|
|
anoremenu enable PopUp.Configure\ Diagnostics |
|
|
]]) |
|
|
end |
|
|
end |
|
|
|
|
|
local nvim_popupmenu_augroup = vim.api.nvim_create_augroup('nvim.popupmenu', {}) |
|
|
vim.api.nvim_create_autocmd('MenuPopup', { |
|
|
pattern = '*', |
|
|
group = nvim_popupmenu_augroup, |
|
|
desc = 'Mouse popup menu', |
|
|
|
|
|
callback = function() |
|
|
enable_ctx_menu() |
|
|
end, |
|
|
}) |
|
|
end |
|
|
|
|
|
|
|
|
do |
|
|
local nvim_terminal_augroup = vim.api.nvim_create_augroup('nvim.terminal', {}) |
|
|
vim.api.nvim_create_autocmd('BufReadCmd', { |
|
|
pattern = 'term://*', |
|
|
group = nvim_terminal_augroup, |
|
|
desc = 'Treat term:// buffers as terminal buffers', |
|
|
nested = true, |
|
|
command = "if !exists('b:term_title')|call jobstart(matchstr(expand(\"<amatch>\"), '\\c\\mterm://\\%(.\\{-}//\\%(\\d\\+:\\)\\?\\)\\?\\zs.*'), {'term': v:true, 'cwd': expand(get(matchlist(expand(\"<amatch>\"), '\\c\\mterm://\\(.\\{-}\\)//'), 1, ''))})", |
|
|
}) |
|
|
|
|
|
vim.api.nvim_create_autocmd({ 'TermClose' }, { |
|
|
group = nvim_terminal_augroup, |
|
|
nested = true, |
|
|
desc = 'Automatically close terminal buffers when started with no arguments and exiting without an error', |
|
|
callback = function(args) |
|
|
if vim.v.event.status ~= 0 then |
|
|
return |
|
|
end |
|
|
local info = vim.api.nvim_get_chan_info(vim.bo[args.buf].channel) |
|
|
local argv = info.argv or {} |
|
|
if table.concat(argv, ' ') == vim.o.shell then |
|
|
vim.api.nvim_buf_delete(args.buf, { force = true }) |
|
|
end |
|
|
end, |
|
|
}) |
|
|
|
|
|
vim.api.nvim_create_autocmd('TermRequest', { |
|
|
group = nvim_terminal_augroup, |
|
|
desc = 'Handles OSC foreground/background color requests', |
|
|
callback = function(args) |
|
|
|
|
|
local channel = vim.bo[args.buf].channel |
|
|
if channel == 0 then |
|
|
return |
|
|
end |
|
|
local fg_request = args.data.sequence == '\027]10;?' |
|
|
local bg_request = args.data.sequence == '\027]11;?' |
|
|
if fg_request or bg_request then |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local red, green, blue = 0, 0, 0 |
|
|
local bg_option_dark = vim.o.background == 'dark' |
|
|
if (fg_request and bg_option_dark) or (bg_request and not bg_option_dark) then |
|
|
red, green, blue = 65535, 65535, 65535 |
|
|
end |
|
|
local command = fg_request and 10 or 11 |
|
|
local data = string.format('\027]%d;rgb:%04x/%04x/%04x\007', command, red, green, blue) |
|
|
vim.api.nvim_chan_send(channel, data) |
|
|
end |
|
|
end, |
|
|
}) |
|
|
|
|
|
local nvim_terminal_prompt_ns = vim.api.nvim_create_namespace('nvim.terminal.prompt') |
|
|
vim.api.nvim_create_autocmd('TermRequest', { |
|
|
group = nvim_terminal_augroup, |
|
|
desc = 'Mark shell prompts indicated by OSC 133 sequences for navigation', |
|
|
callback = function(args) |
|
|
if string.match(args.data.sequence, '^\027]133;A') then |
|
|
local lnum = args.data.cursor[1] |
|
|
vim.api.nvim_buf_set_extmark(args.buf, nvim_terminal_prompt_ns, lnum - 1, 0, {}) |
|
|
end |
|
|
end, |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function jump_to_prompt(ns, win, buf, count) |
|
|
local row, col = unpack(vim.api.nvim_win_get_cursor(win)) |
|
|
local start = -1 |
|
|
local end_ |
|
|
if count > 0 then |
|
|
start = row |
|
|
end_ = -1 |
|
|
elseif count < 0 then |
|
|
|
|
|
start = row - 2 |
|
|
end_ = 0 |
|
|
end |
|
|
|
|
|
if start < 0 then |
|
|
return |
|
|
end |
|
|
|
|
|
local extmarks = vim.api.nvim_buf_get_extmarks( |
|
|
buf, |
|
|
ns, |
|
|
{ start, col }, |
|
|
end_, |
|
|
{ limit = math.abs(count) } |
|
|
) |
|
|
if #extmarks > 0 then |
|
|
local extmark = extmarks[math.min(#extmarks, math.abs(count))] |
|
|
vim.api.nvim_win_set_cursor(win, { extmark[2] + 1, extmark[3] }) |
|
|
end |
|
|
end |
|
|
|
|
|
vim.api.nvim_create_autocmd('TermOpen', { |
|
|
group = nvim_terminal_augroup, |
|
|
desc = 'Default settings for :terminal buffers', |
|
|
callback = function(args) |
|
|
vim.bo[args.buf].modifiable = false |
|
|
vim.bo[args.buf].undolevels = -1 |
|
|
vim.bo[args.buf].scrollback = vim.o.scrollback < 0 and 10000 or math.max(1, vim.o.scrollback) |
|
|
vim.bo[args.buf].textwidth = 0 |
|
|
vim.wo[0][0].wrap = false |
|
|
vim.wo[0][0].list = false |
|
|
vim.wo[0][0].number = false |
|
|
vim.wo[0][0].relativenumber = false |
|
|
vim.wo[0][0].signcolumn = 'no' |
|
|
vim.wo[0][0].foldcolumn = '0' |
|
|
|
|
|
|
|
|
local winhl = vim.o.winhighlight |
|
|
if winhl ~= '' then |
|
|
winhl = winhl .. ',' |
|
|
end |
|
|
vim.wo[0][0].winhighlight = winhl .. 'StatusLine:StatusLineTerm,StatusLineNC:StatusLineTermNC' |
|
|
|
|
|
vim.keymap.set({ 'n', 'x', 'o' }, '[[', function() |
|
|
jump_to_prompt(nvim_terminal_prompt_ns, 0, args.buf, -vim.v.count1) |
|
|
end, { buffer = args.buf, desc = 'Jump [count] shell prompts backward' }) |
|
|
vim.keymap.set({ 'n', 'x', 'o' }, ']]', function() |
|
|
jump_to_prompt(nvim_terminal_prompt_ns, 0, args.buf, vim.v.count1) |
|
|
end, { buffer = args.buf, desc = 'Jump [count] shell prompts forward' }) |
|
|
end, |
|
|
}) |
|
|
|
|
|
vim.api.nvim_create_autocmd('CmdwinEnter', { |
|
|
pattern = '[:>]', |
|
|
desc = 'Limit syntax sync to maxlines=1 in the command window', |
|
|
group = vim.api.nvim_create_augroup('nvim.cmdwin', {}), |
|
|
command = 'syntax sync minlines=1 maxlines=1', |
|
|
}) |
|
|
|
|
|
vim.api.nvim_create_autocmd('SwapExists', { |
|
|
pattern = '*', |
|
|
desc = 'Skip the swapfile prompt when the swapfile is owned by a running Nvim process', |
|
|
group = vim.api.nvim_create_augroup('nvim.swapfile', {}), |
|
|
callback = function() |
|
|
local info = vim.fn.swapinfo(vim.v.swapname) |
|
|
local user = vim.uv.os_get_passwd().username |
|
|
local iswin = 1 == vim.fn.has('win32') |
|
|
if info.error or info.pid <= 0 or (not iswin and info.user ~= user) then |
|
|
vim.v.swapchoice = '' |
|
|
return |
|
|
end |
|
|
vim.v.swapchoice = 'e' |
|
|
vim.notify( |
|
|
('W325: Ignoring swapfile from Nvim process %d'):format(info.pid), |
|
|
vim.log.levels.WARN |
|
|
) |
|
|
end, |
|
|
}) |
|
|
|
|
|
|
|
|
local tty = nil |
|
|
for _, ui in ipairs(vim.api.nvim_list_uis()) do |
|
|
if ui.chan == 1 and ui.stdout_tty then |
|
|
tty = ui |
|
|
break |
|
|
end |
|
|
end |
|
|
|
|
|
if tty then |
|
|
local group = vim.api.nvim_create_augroup('nvim.tty', {}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function setoption(option, value, force) |
|
|
if not force and vim.api.nvim_get_option_info2(option, {}).was_set then |
|
|
|
|
|
return |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
if vim.v.vim_did_enter == 1 then |
|
|
|
|
|
vim.o[option] = value |
|
|
else |
|
|
vim.api.nvim_create_autocmd('VimEnter', { |
|
|
group = group, |
|
|
once = true, |
|
|
nested = true, |
|
|
callback = function() |
|
|
setoption(option, value, force) |
|
|
end, |
|
|
}) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function parsecolor(c) |
|
|
if #c == 0 or #c > 4 then |
|
|
return nil |
|
|
end |
|
|
|
|
|
local val = tonumber(c, 16) |
|
|
if not val then |
|
|
return nil |
|
|
end |
|
|
|
|
|
local max = tonumber(string.rep('f', #c), 16) |
|
|
return val / max |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function parseosc11(resp) |
|
|
local r, g, b |
|
|
r, g, b = resp:match('^\027%]11;rgb:(%x+)/(%x+)/(%x+)$') |
|
|
if not r and not g and not b then |
|
|
local a |
|
|
r, g, b, a = resp:match('^\027%]11;rgba:(%x+)/(%x+)/(%x+)/(%x+)$') |
|
|
if not a or #a > 4 then |
|
|
return nil, nil, nil |
|
|
end |
|
|
end |
|
|
|
|
|
if r and g and b and #r <= 4 and #g <= 4 and #b <= 4 then |
|
|
return r, g, b |
|
|
end |
|
|
|
|
|
return nil, nil, nil |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local force = false |
|
|
local id = vim.api.nvim_create_autocmd('TermResponse', { |
|
|
group = group, |
|
|
nested = true, |
|
|
desc = "Update the value of 'background' automatically based on the terminal emulator's background color", |
|
|
callback = function(args) |
|
|
local resp = args.data.sequence |
|
|
local r, g, b = parseosc11(resp) |
|
|
if r and g and b then |
|
|
local rr = parsecolor(r) |
|
|
local gg = parsecolor(g) |
|
|
local bb = parsecolor(b) |
|
|
|
|
|
if rr and gg and bb then |
|
|
local luminance = (0.299 * rr) + (0.587 * gg) + (0.114 * bb) |
|
|
local bg = luminance < 0.5 and 'dark' or 'light' |
|
|
setoption('background', bg, force) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not force then |
|
|
force = true |
|
|
end |
|
|
end |
|
|
end |
|
|
end, |
|
|
}) |
|
|
|
|
|
vim.api.nvim_create_autocmd('VimEnter', { |
|
|
group = group, |
|
|
nested = true, |
|
|
once = true, |
|
|
callback = function() |
|
|
if vim.api.nvim_get_option_info2('background', {}).was_set then |
|
|
vim.api.nvim_del_autocmd(id) |
|
|
end |
|
|
end, |
|
|
}) |
|
|
|
|
|
io.stdout:write('\027]11;?\007') |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do |
|
|
local colorterm = os.getenv('COLORTERM') |
|
|
if tty.rgb or colorterm == 'truecolor' or colorterm == '24bit' then |
|
|
|
|
|
|
|
|
setoption('termguicolors', true) |
|
|
elseif colorterm == nil or colorterm == '' then |
|
|
|
|
|
|
|
|
local caps = {} |
|
|
require('vim.termcap').query({ 'Tc', 'RGB', 'setrgbf', 'setrgbb' }, function(cap, found) |
|
|
if not found then |
|
|
return |
|
|
end |
|
|
|
|
|
caps[cap] = true |
|
|
if caps.Tc or caps.RGB or (caps.setrgbf and caps.setrgbb) then |
|
|
setoption('termguicolors', true) |
|
|
end |
|
|
end) |
|
|
|
|
|
local timer = assert(vim.uv.new_timer()) |
|
|
|
|
|
|
|
|
local r = 1 |
|
|
local g = 2 |
|
|
local b = 3 |
|
|
|
|
|
local id = vim.api.nvim_create_autocmd('TermResponse', { |
|
|
group = group, |
|
|
nested = true, |
|
|
callback = function(args) |
|
|
local resp = args.data.sequence |
|
|
local decrqss = resp:match('^\027P1%$r([%d;:]+)m$') |
|
|
|
|
|
if decrqss then |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local attrs = vim.split(decrqss, ';') |
|
|
if #attrs ~= 1 and (#attrs ~= 2 or attrs[1] ~= '0') then |
|
|
return false |
|
|
end |
|
|
|
|
|
|
|
|
local sgr = attrs[#attrs]:match('^48:2:([%d:]+)$') |
|
|
if not sgr then |
|
|
return false |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local params = vim.split(sgr, ':') |
|
|
if #params ~= 3 and (#params ~= 4 or (params[1] ~= '' and params[1] ~= '1')) then |
|
|
return true |
|
|
end |
|
|
|
|
|
if |
|
|
tonumber(params[#params - 2]) == r |
|
|
and tonumber(params[#params - 1]) == g |
|
|
and tonumber(params[#params]) == b |
|
|
then |
|
|
setoption('termguicolors', true) |
|
|
end |
|
|
|
|
|
return true |
|
|
end |
|
|
end, |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local decrqss = '\027P$qm\027\\' |
|
|
|
|
|
|
|
|
io.stdout:write(string.format('\027[0m\027[48;2;%d;%d;%dm%s', r, g, b, decrqss)) |
|
|
|
|
|
timer:start(1000, 0, function() |
|
|
|
|
|
vim.schedule(function() |
|
|
|
|
|
pcall(vim.api.nvim_del_autocmd, id) |
|
|
end) |
|
|
|
|
|
if not timer:is_closing() then |
|
|
timer:close() |
|
|
end |
|
|
end) |
|
|
end |
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
do |
|
|
|
|
|
if vim.fn.executable('rg') == 1 then |
|
|
|
|
|
vim.o.grepprg = 'rg --vimgrep -uu ' |
|
|
vim.o.grepformat = '%f:%l:%c:%m' |
|
|
end |
|
|
end |
|
|
|