Compare commits

...

4 Commits

Author SHA1 Message Date
Gregory Anders
d13598daa6
Merge 8fb19682d1 into 160cbd0ef4 2024-12-18 13:06:20 -06:00
luukvbaal
160cbd0ef4
test(cursor_spec): global highlight definitions (#31613) 2024-12-18 19:06:16 +00:00
Gregory Anders
3db3947b0e
fix(terminal): restore cursor from 'guicursor' on TermLeave (#31620)
Fixes: https://github.com/neovim/neovim/issues/31612
2024-12-18 11:41:05 -06:00
Gregory Anders
8fb19682d1 feat(terminal)!: include cursor position in TermRequest event data
When a plugin registers a TermRequest handler there is currently no way
for the handler to know where the terminal's cursor position was when
the sequence was received. This is often useful information, e.g. for
OSC 133 sequences which are used to annotate shell prompts.

Modify the event data for the TermRequest autocommand to be a table
instead of just a string. The "payload" field of the table contains the
sequence string and the "row" and "col" fields contain the cursor
position when the sequence was received.

To maintain consistency between TermRequest and TermResponse (and to
future proof the latter), TermResponse's event data is also updated to
be a table with a "payload" field.

BREAKING CHANGE: event data for TermRequest and TermResponse is now a
table
2024-12-17 15:20:03 -06:00
12 changed files with 136 additions and 83 deletions

View File

@ -1006,21 +1006,30 @@ TermClose When a |terminal| job ends.
*TermRequest*
TermRequest When a |:terminal| child process emits an OSC
or DCS sequence. Sets |v:termrequest|. The
|event-data| is the request string.
|event-data| is a table with the following
fields:
payload: the received sequence
row: cursor row
col: cursor column
*TermResponse*
TermResponse When Nvim receives an OSC or DCS response from
the host terminal. Sets |v:termresponse|. The
|event-data| is the response string. May be
triggered during another event (file I/O,
a shell command, or anything else that takes
time). Example: >lua
|event-data| is a table with the following fields:
payload: the received sequence
May be triggered during another event (file
I/O, a shell command, or anything else that
takes time). Example: >lua
-- Query the terminal palette for the RGB value of color 1
-- (red) using OSC 4
vim.api.nvim_create_autocmd('TermResponse', {
once = true,
callback = function(args)
local resp = args.data
local resp = args.data.payload
local r, g, b = resp:match("\027%]4;1;rgb:(%w+)/(%w+)/(%w+)")
end,
})

View File

@ -86,6 +86,10 @@ EVENTS
• |vim.ui_attach()| callbacks for |ui-messages| `msg_show` events are executed in
|api-fast| context.
• |TermRequest| and |TermResponse| |event-data| is now a table. The "payload"
field contains the received sequence. |TermRequest| also contains "row" and
"col" fields indicating the cursor's position when the sequence was
received.
HIGHLIGHTS
@ -294,6 +298,8 @@ TERMINAL
means that the |TermCursorNC| highlight group is no longer supported: an
unfocused terminal window will have no cursor at all (so there is nothing to
highlight).
• |TermRequest| has "row" and "col" fields in its |event-data| indicating the
cursor position when the sequence was received.
TREESITTER

View File

@ -144,8 +144,8 @@ directory indicated in the request. >lua
vim.api.nvim_create_autocmd({ 'TermRequest' }, {
desc = 'Handles OSC 7 dir change requests',
callback = function(ev)
if string.sub(vim.v.termrequest, 1, 4) == '\x1b]7;' then
local dir = string.gsub(vim.v.termrequest, '\x1b]7;file://[^/]*', '')
if string.sub(ev.data.payload, 1, 4) == '\x1b]7;' then
local dir = string.gsub(ev.data.payload, '\x1b]7;file://[^/]*', '')
if vim.fn.isdirectory(dir) == 0 then
vim.notify('invalid dir: '..dir)
return

View File

@ -205,7 +205,9 @@ local function try_query_terminal_color(color)
once = true,
callback = function(args)
hex = '#'
.. table.concat({ args.data:match('\027%]%d+;%d*;?rgb:(%w%w)%w%w/(%w%w)%w%w/(%w%w)%w%w') })
.. table.concat({
args.data.payload:match('\027%]%d+;%d*;?rgb:(%w%w)%w%w/(%w%w)%w%w/(%w%w)%w%w'),
})
end,
})
if type(color) == 'number' then

View File

@ -463,8 +463,8 @@ do
if channel == 0 then
return
end
local fg_request = args.data == '\027]10;?'
local bg_request = args.data == '\027]11;?'
local fg_request = args.data.payload == '\027]10;?'
local bg_request = args.data.payload == '\027]11;?'
if fg_request or bg_request then
-- WARN: This does not return the actual foreground/background color,
-- but rather returns:
@ -660,7 +660,7 @@ do
nested = true,
desc = "Update the value of 'background' automatically based on the terminal emulator's background color",
callback = function(args)
local resp = args.data ---@type string
local resp = args.data.payload ---@type string
local r, g, b = parseosc11(resp)
if r and g and b then
local rr = parsecolor(r)
@ -736,7 +736,7 @@ do
group = group,
nested = true,
callback = function(args)
local resp = args.data ---@type string
local resp = args.data.payload ---@type string
local decrqss = resp:match('^\027P1%$r([%d;:]+)m$')
if decrqss then

View File

@ -34,7 +34,7 @@ function M.query(caps, cb)
local id = vim.api.nvim_create_autocmd('TermResponse', {
nested = true,
callback = function(args)
local resp = args.data ---@type string
local resp = args.data.payload ---@type string
local k, rest = resp:match('^\027P1%+r(%x+)(.*)$')
if k and rest then
local cap = vim.text.hexdecode(k)

View File

@ -25,7 +25,7 @@ function M.paste(reg)
local contents = nil
local id = vim.api.nvim_create_autocmd('TermResponse', {
callback = function(args)
local resp = args.data ---@type string
local resp = args.data.payload ---@type string
local encoded = resp:match('\027%]52;%w?;([A-Za-z0-9+/=]*)')
if encoded then
contents = vim.base64.decode(encoded)

View File

@ -505,7 +505,11 @@ void nvim_ui_term_event(uint64_t channel_id, String event, Object value, Error *
const String termresponse = value.data.string;
set_vim_var_string(VV_TERMRESPONSE, termresponse.data, (ptrdiff_t)termresponse.size);
apply_autocmds_group(EVENT_TERMRESPONSE, NULL, NULL, false, AUGROUP_ALL, NULL, NULL, &value);
MAXSIZE_TEMP_DICT(data, 1);
PUT_C(data, "payload", value);
apply_autocmds_group(EVENT_TERMRESPONSE, NULL, NULL, false, AUGROUP_ALL, NULL, NULL,
&DICT_OBJ(data));
}
}

View File

@ -204,12 +204,20 @@ static void emit_termrequest(void **argv)
char *payload = argv[1];
size_t payload_length = (size_t)argv[2];
StringBuilder *pending_send = argv[3];
int row = (int)(intptr_t)argv[4];
int col = (int)(intptr_t)argv[5];
set_vim_var_string(VV_TERMREQUEST, payload, (ptrdiff_t)payload_length);
MAXSIZE_TEMP_DICT(data, 3);
String termrequest = { .data = payload, .size = payload_length };
PUT_C(data, "payload", STRING_OBJ(termrequest));
PUT_C(data, "row", INTEGER_OBJ(row));
PUT_C(data, "col", INTEGER_OBJ(col));
buf_T *buf = handle_get_buffer(term->buf_handle);
String termrequest = { .data = payload, .size = payload_length };
Object data = STRING_OBJ(termrequest);
set_vim_var_string(VV_TERMREQUEST, payload, (ptrdiff_t)payload_length);
apply_autocmds_group(EVENT_TERMREQUEST, NULL, NULL, false, AUGROUP_ALL, buf, NULL, &data);
apply_autocmds_group(EVENT_TERMREQUEST, NULL, NULL, false, AUGROUP_ALL, buf, NULL,
&DICT_OBJ(data));
xfree(payload);
StringBuilder *term_pending_send = term->pending.send;
@ -229,7 +237,8 @@ static void schedule_termrequest(Terminal *term, char *payload, size_t payload_l
term->pending.send = xmalloc(sizeof(StringBuilder));
kv_init(*term->pending.send);
multiqueue_put(main_loop.events, emit_termrequest, term, payload, (void *)payload_length,
term->pending.send);
term->pending.send, (void *)(intptr_t)term->cursor.row,
(void *)(intptr_t)term->cursor.col);
}
static int parse_osc8(VTermStringFragment frag, int *attr)
@ -641,9 +650,6 @@ bool terminal_enter(void)
curwin->w_p_so = 0;
curwin->w_p_siso = 0;
// Save the existing cursor entry since it may be modified by the application
cursorentry_T save_cursorentry = shape_table[SHAPE_IDX_TERM];
// Update the cursor shape table and flush changes to the UI
s->term->pending.cursor = true;
refresh_cursor(s->term);
@ -674,8 +680,8 @@ bool terminal_enter(void)
RedrawingDisabled = s->save_rd;
apply_autocmds(EVENT_TERMLEAVE, NULL, NULL, false, curbuf);
shape_table[SHAPE_IDX_TERM] = save_cursorentry;
ui_mode_info_set();
// Restore the terminal cursor to what is set in 'guicursor'
(void)parse_shape_opt(SHAPE_CURSOR);
if (save_curwin == curwin->handle) { // Else: window was closed.
curwin->w_p_cul = save_w_p_cul;

View File

@ -362,7 +362,7 @@ describe(':terminal buffer', function()
})
vim.api.nvim_create_autocmd('TermRequest', {
callback = function(args)
if args.data == '\027]11;?' then
if args.data.payload == '\027]11;?' then
table.insert(_G.input, '\027]11;rgb:0000/0000/0000\027\\')
end
end
@ -378,6 +378,22 @@ describe(':terminal buffer', function()
}, exec_lua('return _G.input'))
end)
it('TermRequest includes cursor position #31609', function()
command('autocmd! nvim_terminal TermRequest')
local term = exec_lua([[
_G.cursor = {}
local term = vim.api.nvim_open_term(0, {})
vim.api.nvim_create_autocmd('TermRequest', {
callback = function(args)
_G.cursor = { row = args.data.row, col = args.data.col }
end
})
return term
]])
api.nvim_chan_send(term, 'Hello\nworld!\027]133;D\027\\')
eq({ row = 1, col = 6 }, exec_lua('return _G.cursor'))
end)
it('no heap-buffer-overflow when using termopen(echo) #3161', function()
local testfilename = 'Xtestfile-functional-terminal-buffers_spec'
write_file(testfilename, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa')

View File

@ -15,9 +15,20 @@ local skip = t.skip
describe(':terminal cursor', function()
local screen
local terminal_mode_idx ---@type number
before_each(function()
clear()
screen = tt.setup_screen()
if terminal_mode_idx == nil then
for i, v in ipairs(screen._mode_info) do
if v.name == 'terminal' then
terminal_mode_idx = i
end
end
assert(terminal_mode_idx)
end
end)
it('moves the screen cursor when focused', function()
@ -143,13 +154,6 @@ describe(':terminal cursor', function()
it('can be modified by application #3681', function()
skip(is_os('win'), '#31587')
local idx ---@type number
for i, v in ipairs(screen._mode_info) do
if v.name == 'terminal' then
idx = i
end
end
assert(idx)
local states = {
[1] = { blink = true, shape = 'block' },
@ -171,13 +175,13 @@ describe(':terminal cursor', function()
]],
condition = function()
if v.blink then
eq(500, screen._mode_info[idx].blinkon)
eq(500, screen._mode_info[idx].blinkoff)
eq(500, screen._mode_info[terminal_mode_idx].blinkon)
eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
else
eq(0, screen._mode_info[idx].blinkon)
eq(0, screen._mode_info[idx].blinkoff)
eq(0, screen._mode_info[terminal_mode_idx].blinkon)
eq(0, screen._mode_info[terminal_mode_idx].blinkoff)
end
eq(v.shape, screen._mode_info[idx].cursor_shape)
eq(v.shape, screen._mode_info[terminal_mode_idx].cursor_shape)
end,
})
end
@ -191,20 +195,13 @@ describe(':terminal cursor', function()
]])
-- Cursor returns to default on TermLeave
eq(500, screen._mode_info[idx].blinkon)
eq(500, screen._mode_info[idx].blinkoff)
eq('block', screen._mode_info[idx].cursor_shape)
eq(500, screen._mode_info[terminal_mode_idx].blinkon)
eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
eq('block', screen._mode_info[terminal_mode_idx].cursor_shape)
end)
it('can be modified per terminal', function()
skip(is_os('win'), '#31587')
local idx ---@type number
for i, v in ipairs(screen._mode_info) do
if v.name == 'terminal' then
idx = i
end
end
assert(idx)
-- Set cursor to vertical bar with blink
tt.feed_csi('5 q')
@ -216,9 +213,9 @@ describe(':terminal cursor', function()
{3:-- TERMINAL --} |
]],
condition = function()
eq(500, screen._mode_info[idx].blinkon)
eq(500, screen._mode_info[idx].blinkoff)
eq('vertical', screen._mode_info[idx].cursor_shape)
eq(500, screen._mode_info[terminal_mode_idx].blinkon)
eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
eq('vertical', screen._mode_info[terminal_mode_idx].cursor_shape)
end,
})
@ -231,9 +228,9 @@ describe(':terminal cursor', function()
{3:-- TERMINAL --} |
]],
condition = function()
eq(500, screen._mode_info[idx].blinkon)
eq(500, screen._mode_info[idx].blinkoff)
eq('vertical', screen._mode_info[idx].cursor_shape)
eq(500, screen._mode_info[terminal_mode_idx].blinkon)
eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
eq('vertical', screen._mode_info[terminal_mode_idx].cursor_shape)
end,
})
@ -256,9 +253,9 @@ describe(':terminal cursor', function()
]],
condition = function()
-- New terminal, cursor resets to defaults
eq(500, screen._mode_info[idx].blinkon)
eq(500, screen._mode_info[idx].blinkoff)
eq('block', screen._mode_info[idx].cursor_shape)
eq(500, screen._mode_info[terminal_mode_idx].blinkon)
eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
eq('block', screen._mode_info[terminal_mode_idx].cursor_shape)
end,
})
@ -275,9 +272,9 @@ describe(':terminal cursor', function()
{3:-- TERMINAL --} |
]],
condition = function()
eq(0, screen._mode_info[idx].blinkon)
eq(0, screen._mode_info[idx].blinkoff)
eq('horizontal', screen._mode_info[idx].cursor_shape)
eq(0, screen._mode_info[terminal_mode_idx].blinkon)
eq(0, screen._mode_info[terminal_mode_idx].blinkoff)
eq('horizontal', screen._mode_info[terminal_mode_idx].cursor_shape)
end,
})
@ -294,9 +291,9 @@ describe(':terminal cursor', function()
{3:-- TERMINAL --} |
]],
condition = function()
eq(500, screen._mode_info[idx].blinkon)
eq(500, screen._mode_info[idx].blinkoff)
eq('vertical', screen._mode_info[idx].cursor_shape)
eq(500, screen._mode_info[terminal_mode_idx].blinkon)
eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
eq('vertical', screen._mode_info[terminal_mode_idx].cursor_shape)
end,
})
end)
@ -326,6 +323,32 @@ describe(':terminal cursor', function()
{3:-- TERMINAL --} |
]])
end)
it('preserves guicursor value on TermLeave #31612', function()
eq(3, screen._mode_info[terminal_mode_idx].hl_id)
-- Change 'guicursor' while terminal mode is active
command('set guicursor+=t:Error')
local error_hl_id = call('hlID', 'Error')
screen:expect({
condition = function()
eq(error_hl_id, screen._mode_info[terminal_mode_idx].hl_id)
end,
})
-- Exit terminal mode
feed([[<C-\><C-N>]])
screen:expect([[
tty ready |
^ |
|*5
]])
eq(error_hl_id, screen._mode_info[terminal_mode_idx].hl_id)
end)
end)
describe('buffer cursor position is correct in terminal without number column', function()
@ -350,12 +373,6 @@ describe('buffer cursor position is correct in terminal without number column',
}, {
cols = 70,
})
screen:set_default_attr_ids({
[1] = { foreground = 253, background = 11 },
[2] = { reverse = true },
[3] = { bold = true },
[4] = { background = 11 },
})
-- Also check for real cursor position, as it is used for stuff like input methods
screen._handle_busy_start = function() end
screen._handle_busy_stop = function() end
@ -667,13 +684,6 @@ describe('buffer cursor position is correct in terminal with number column', fun
}, {
cols = 70,
})
screen:set_default_attr_ids({
[1] = { foreground = 253, background = 11 },
[2] = { reverse = true },
[3] = { bold = true },
[4] = { background = 11 },
[7] = { foreground = 130 },
})
-- Also check for real cursor position, as it is used for stuff like input methods
screen._handle_busy_start = function() end
screen._handle_busy_stop = function() end

View File

@ -92,7 +92,7 @@ describe('TUI', function()
_G.termresponse = nil
vim.api.nvim_create_autocmd('TermResponse', {
once = true,
callback = function(ev) _G.termresponse = ev.data end,
callback = function(ev) _G.termresponse = ev.data.payload end,
})
]])
feed_data('\027P0$r\027\\')
@ -2076,7 +2076,7 @@ describe('TUI', function()
vim.api.nvim_create_autocmd('TermRequest', {
buffer = buf,
callback = function(args)
local req = args.data
local req = args.data.payload
if not req then
return
end
@ -3070,7 +3070,7 @@ describe('TUI', function()
exec_lua([[
vim.api.nvim_create_autocmd('TermRequest', {
callback = function(args)
local req = args.data
local req = args.data.payload
local payload = req:match('^\027P%+q([%x;]+)$')
if payload then
local t = {}
@ -3124,7 +3124,7 @@ describe('TUI', function()
exec_lua([[
vim.api.nvim_create_autocmd('TermRequest', {
callback = function(args)
local req = args.data
local req = args.data.payload
vim.g.termrequest = req
local xtgettcap = req:match('^\027P%+q([%x;]+)$')
if xtgettcap then
@ -3179,7 +3179,7 @@ describe('TUI', function()
exec_lua([[
vim.api.nvim_create_autocmd('TermRequest', {
callback = function(args)
local req = args.data
local req = args.data.payload
local payload = req:match('^\027P%+q([%x;]+)$')
if payload and vim.text.hexdecode(payload) == 'Ms' then
vim.g.xtgettcap = 'Ms'
@ -3269,7 +3269,7 @@ describe('TUI bg color', function()
exec_lua([[
vim.api.nvim_create_autocmd('TermRequest', {
callback = function(args)
local req = args.data
local req = args.data.payload
if req == '\027]11;?' then
vim.g.oscrequest = true
return true