diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index ee482f7104..8b29fcfa42 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -173,11 +173,13 @@ static void emit_term_request(void **argv) { char *payload = argv[0]; 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 }; 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, curbuf, NULL, &data); + apply_autocmds_group(EVENT_TERMREQUEST, NULL, NULL, false, AUGROUP_ALL, buf, NULL, &data); xfree(payload); } @@ -190,7 +192,7 @@ static int on_osc(int command, VTermStringFragment frag, void *user) StringBuilder request = KV_INITIAL_VALUE; kv_printf(request, "\x1b]%d;", command); 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; } @@ -203,7 +205,7 @@ static int on_dcs(const char *command, size_t commandlen, VTermStringFragment fr StringBuilder request = KV_INITIAL_VALUE; kv_printf(request, "\x1bP%*s", (int)commandlen, command); 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; } diff --git a/test/functional/terminal/buffer_spec.lua b/test/functional/terminal/buffer_spec.lua index d0462b5619..497d7668e8 100644 --- a/test/functional/terminal/buffer_spec.lua +++ b/test/functional/terminal/buffer_spec.lua @@ -318,16 +318,23 @@ describe(':terminal buffer', function() ) end) - it('emits TermRequest events', function() + it('emits TermRequest events #26972', function() command('split') command('enew') 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("")') + command('wincmd p') + -- cwd will be inserted in a file URI, which cannot contain backs local cwd = funcs.getcwd():gsub('\\', '/') local parent = cwd:match('^(.+/)') local expected = '\027]7;file://host' .. parent meths.chan_send(term, string.format('%s\027\\', expected)) eq(expected, eval('v:termrequest')) + eq(termbuf, eval('g:termbuf')) end) end)