diff options
| author | Ayaan <138162656+def3r@users.noreply.github.com> | 2026-03-18 17:24:41 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-18 07:54:41 -0400 |
| commit | 63642ebf80cb1142e84fa6ded98f7541f15739be (patch) | |
| tree | f4b57c5208ca036039916830411abf77dc647263 /runtime/lua/vim/_core/defaults.lua | |
| parent | 20225fc330a4808f8b95edbbf9f181730824a2ae (diff) | |
refactor(terminal): impl "[Process exited]" in Lua #38343
Problem:
"[Process exited]" is implemented in C with anonymous namespace
and users have no way to hide it.
Solution:
- Handle "TermClose" event in Lua.
- User can delete the "nvim.terminal" augroup to avoid "[Process exited]".
Diffstat (limited to 'runtime/lua/vim/_core/defaults.lua')
| -rw-r--r-- | runtime/lua/vim/_core/defaults.lua | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/runtime/lua/vim/_core/defaults.lua b/runtime/lua/vim/_core/defaults.lua index 5ef11ee94a..51f26d845b 100644 --- a/runtime/lua/vim/_core/defaults.lua +++ b/runtime/lua/vim/_core/defaults.lua @@ -578,6 +578,54 @@ do end, }) + local nvim_terminal_exitmsg_ns = vim.api.nvim_create_namespace('nvim.terminal.exitmsg') + + --- @param buf integer + --- @param msg string + --- @param pos integer + local function set_terminal_exitmsg(buf, msg, pos) + vim.api.nvim_buf_set_extmark(buf, nvim_terminal_exitmsg_ns, pos, 0, { + virt_text = { { msg, nil } }, + virt_text_pos = 'overlay', + }) + end + + vim.api.nvim_create_autocmd('TermClose', { + group = nvim_terminal_augroup, + nested = true, + desc = 'Displays the "[Process exited]" virtual text', + callback = function(ev) + if not vim.api.nvim_buf_is_valid(ev.buf) then + return + end + + local buf = vim.bo[ev.buf] + local pos = ev.data.pos ---@type integer + local buf_has_exitmsg = #( + vim.api.nvim_buf_get_extmarks(ev.buf, nvim_terminal_exitmsg_ns, 0, -1, {}) + ) > 0 + + -- `nvim_open_term` buffers do not have any attached chan + local msg = buf.channel == 0 and '[Terminal closed]' + or ('[Process exited %d]'):format(vim.v.event.status) + + -- TermClose may be queued before TermOpen if the process + -- exits before `terminal_open` is called. Don't display + -- the msg now, let TermOpen display it. + if buf.buftype ~= 'terminal' or buf_has_exitmsg then + vim.api.nvim_create_autocmd('TermOpen', { + buffer = ev.buf, + once = true, + callback = function() + set_terminal_exitmsg(ev.buf, msg, pos) + end, + }) + return + end + set_terminal_exitmsg(ev.buf, msg, pos) + end, + }) + vim.api.nvim_create_autocmd('TermRequest', { group = nvim_terminal_augroup, desc = 'Handles OSC foreground/background color requests', @@ -694,6 +742,9 @@ do vim.keymap.set({ 'n', 'x', 'o' }, ']]', function() jump_to_prompt(nvim_terminal_prompt_ns, 0, ev.buf, vim.v.count1) end, { buffer = ev.buf, desc = 'Jump [count] shell prompts forward' }) + + -- If the terminal buffer is being reused, clear the previous exit msg + vim.api.nvim_buf_clear_namespace(ev.buf, nvim_terminal_exitmsg_ns, 0, -1) end, }) |
