fix(terminal): use terminal buffer for TermRequest autocommand (#26974)

This commit is contained in:
Gregory Anders 2024-01-09 21:31:37 -06:00 committed by GitHub
parent 63a17322dd
commit fa17a5ab49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -173,11 +173,13 @@ static void emit_term_request(void **argv)
{ {
char *payload = argv[0]; char *payload = argv[0];
size_t payload_length = (size_t)argv[1]; size_t payload_length = (size_t)argv[1];
Terminal *rv = argv[2];
buf_T *buf = handle_get_buffer(rv->buf_handle);
String termrequest = { .data = payload, .size = payload_length }; String termrequest = { .data = payload, .size = payload_length };
Object data = STRING_OBJ(termrequest); Object data = STRING_OBJ(termrequest);
set_vim_var_string(VV_TERMREQUEST, payload, (ptrdiff_t)payload_length); set_vim_var_string(VV_TERMREQUEST, payload, (ptrdiff_t)payload_length);
apply_autocmds_group(EVENT_TERMREQUEST, NULL, NULL, false, AUGROUP_ALL, curbuf, NULL, &data); apply_autocmds_group(EVENT_TERMREQUEST, NULL, NULL, false, AUGROUP_ALL, buf, NULL, &data);
xfree(payload); xfree(payload);
} }
@ -190,7 +192,7 @@ static int on_osc(int command, VTermStringFragment frag, void *user)
StringBuilder request = KV_INITIAL_VALUE; StringBuilder request = KV_INITIAL_VALUE;
kv_printf(request, "\x1b]%d;", command); kv_printf(request, "\x1b]%d;", command);
kv_concat_len(request, frag.str, frag.len); kv_concat_len(request, frag.str, frag.len);
multiqueue_put(main_loop.events, emit_term_request, request.items, (void *)request.size); multiqueue_put(main_loop.events, emit_term_request, request.items, (void *)request.size, user);
return 1; return 1;
} }
@ -203,7 +205,7 @@ static int on_dcs(const char *command, size_t commandlen, VTermStringFragment fr
StringBuilder request = KV_INITIAL_VALUE; StringBuilder request = KV_INITIAL_VALUE;
kv_printf(request, "\x1bP%*s", (int)commandlen, command); kv_printf(request, "\x1bP%*s", (int)commandlen, command);
kv_concat_len(request, frag.str, frag.len); kv_concat_len(request, frag.str, frag.len);
multiqueue_put(main_loop.events, emit_term_request, request.items, (void *)request.size); multiqueue_put(main_loop.events, emit_term_request, request.items, (void *)request.size, user);
return 1; return 1;
} }

View File

@ -318,16 +318,23 @@ describe(':terminal buffer', function()
) )
end) end)
it('emits TermRequest events', function() it('emits TermRequest events #26972', function()
command('split') command('split')
command('enew') command('enew')
local term = meths.open_term(0, {}) local term = meths.open_term(0, {})
local termbuf = meths.get_current_buf().id
-- Test that autocommand buffer is associated with the terminal buffer, not the current buffer
command('au TermRequest * let g:termbuf = +expand("<abuf>")')
command('wincmd p')
-- cwd will be inserted in a file URI, which cannot contain backs -- cwd will be inserted in a file URI, which cannot contain backs
local cwd = funcs.getcwd():gsub('\\', '/') local cwd = funcs.getcwd():gsub('\\', '/')
local parent = cwd:match('^(.+/)') local parent = cwd:match('^(.+/)')
local expected = '\027]7;file://host' .. parent local expected = '\027]7;file://host' .. parent
meths.chan_send(term, string.format('%s\027\\', expected)) meths.chan_send(term, string.format('%s\027\\', expected))
eq(expected, eval('v:termrequest')) eq(expected, eval('v:termrequest'))
eq(termbuf, eval('g:termbuf'))
end) end)
end) end)