Use Lua autocommand and make TermClose autocommand global

This commit is contained in:
Gregory Anders 2023-08-10 09:53:56 -05:00
parent 6e703f778f
commit 3fb372eba4
5 changed files with 25 additions and 12 deletions

View File

@ -181,6 +181,10 @@ The following changes to existing APIs or features add new behavior.
supports it, unless |'keywordprg'| was customized before calling supports it, unless |'keywordprg'| was customized before calling
|vim.lsp.start()|. |vim.lsp.start()|.
• Terminal buffers started with no arguments (and use 'shell') close
automatically if the job exited without error, eliminating the (often
unwanted) "[Process exited 0]" message.
============================================================================== ==============================================================================
REMOVED FEATURES *news-removed* REMOVED FEATURES *news-removed*

View File

@ -249,6 +249,10 @@ gx Opens the current filepath or URL (decided by
Fails if changes have been made to the current buffer, Fails if changes have been made to the current buffer,
unless 'hidden' is set. unless 'hidden' is set.
If {cmd} is omitted, and the 'shell' job exits with no
error, the buffer is closed automatically
|default-autocmds|.
To enter |Terminal-mode| automatically: > To enter |Terminal-mode| automatically: >
autocmd TermOpen * startinsert autocmd TermOpen * startinsert
< <

View File

@ -133,6 +133,8 @@ remove them and ":autocmd {group}" to see how they're defined.
nvim_terminal: nvim_terminal:
- BufReadCmd: Treats "term://" buffers as |terminal| buffers. |terminal-start| - BufReadCmd: Treats "term://" buffers as |terminal| buffers. |terminal-start|
- TermClose: A |terminal| buffer started with no arguments (which thus uses
'shell') and which exits with no error is closed automatically.
nvim_cmdwin: nvim_cmdwin:
- CmdwinEnter: Limits syntax sync to maxlines=1 in the |cmdwin|. - CmdwinEnter: Limits syntax sync to maxlines=1 in the |cmdwin|.

View File

@ -1107,13 +1107,26 @@ end
function vim._init_default_autocmds() function vim._init_default_autocmds()
local nvim_terminal_augroup = vim.api.nvim_create_augroup('nvim_terminal', {}) local nvim_terminal_augroup = vim.api.nvim_create_augroup('nvim_terminal', {})
vim.api.nvim_create_autocmd({ 'bufreadcmd' }, { vim.api.nvim_create_autocmd({ 'BufReadCmd' }, {
pattern = 'term://*', pattern = 'term://*',
group = nvim_terminal_augroup, group = nvim_terminal_augroup,
nested = true, nested = true,
command = "if !exists('b:term_title')|call termopen(matchstr(expand(\"<amatch>\"), '\\c\\mterm://\\%(.\\{-}//\\%(\\d\\+:\\)\\?\\)\\?\\zs.*'), {'cwd': expand(get(matchlist(expand(\"<amatch>\"), '\\c\\mterm://\\(.\\{-}\\)//'), 1, ''))})", command = "if !exists('b:term_title')|call termopen(matchstr(expand(\"<amatch>\"), '\\c\\mterm://\\%(.\\{-}//\\%(\\d\\+:\\)\\?\\)\\?\\zs.*'), {'cwd': expand(get(matchlist(expand(\"<amatch>\"), '\\c\\mterm://\\(.\\{-}\\)//'), 1, ''))})",
}) })
vim.api.nvim_create_autocmd({ 'cmdwinenter' }, { vim.api.nvim_create_autocmd({ 'TermClose' }, {
group = nvim_terminal_augroup,
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
local info = vim.api.nvim_get_chan_info(vim.bo[args.buf].channel)
local argv = info.argv or {}
if #argv == 1 and argv[1] == vim.o.shell then
vim.cmd({ cmd = 'bdelete', args = { args.buf }, bang = true })
end
end
end,
})
vim.api.nvim_create_autocmd({ 'CmdwinEnter' }, {
pattern = '[:>]', pattern = '[:>]',
group = vim.api.nvim_create_augroup('nvim_cmdwin', {}), group = vim.api.nvim_create_augroup('nvim_cmdwin', {}),
command = 'syntax sync minlines=1 maxlines=1', command = 'syntax sync minlines=1 maxlines=1',

View File

@ -8502,16 +8502,6 @@ static void f_termopen(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
channel_terminal_open(curbuf, chan); channel_terminal_open(curbuf, chan);
channel_create_event(chan, NULL); channel_create_event(chan, NULL);
do_cmdline_cmd("augroup nvim_terminal_close");
do_cmdline_cmd("autocmd! TermClose <buffer> "
" if !v:event.status |"
" let info = nvim_get_chan_info(&channel) |"
" if get(info, 'argv', []) ==# [&shell] |"
" exec 'bdelete! ' .. expand('<abuf>') |"
" endif |"
" endif");
do_cmdline_cmd("augroup END");
} }
/// "timer_info([timer])" function /// "timer_info([timer])" function