fix(api): fix not capturing output in cmdline mode (#35322)

This commit is contained in:
zeertzjq
2025-08-13 20:12:47 +08:00
committed by GitHub
parent 44fdbd6589
commit 7b9512e613
3 changed files with 12 additions and 0 deletions

View File

@@ -726,6 +726,7 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Arena
garray_T capture_local;
const int save_msg_silent = msg_silent;
const bool save_redir_off = redir_off;
garray_T * const save_capture_ga = capture_ga;
const int save_msg_col = msg_col;
@@ -737,6 +738,7 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Arena
TRY_WRAP(err, {
if (opts->output) {
msg_silent++;
redir_off = false;
msg_col = 0; // prevent leading spaces
}
@@ -747,6 +749,7 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Arena
if (opts->output) {
capture_ga = save_capture_ga;
msg_silent = save_msg_silent;
redir_off = save_redir_off;
// Put msg_col back where it was, since nothing should have been written.
msg_col = save_msg_col;
}

View File

@@ -72,6 +72,7 @@ Dict nvim_exec2(uint64_t channel_id, String src, Dict(exec_opts) *opts, Error *e
String exec_impl(uint64_t channel_id, String src, Dict(exec_opts) *opts, Error *err)
{
const int save_msg_silent = msg_silent;
const bool save_redir_off = redir_off;
garray_T *const save_capture_ga = capture_ga;
const int save_msg_col = msg_col;
garray_T capture_local;
@@ -83,6 +84,7 @@ String exec_impl(uint64_t channel_id, String src, Dict(exec_opts) *opts, Error *
TRY_WRAP(err, {
if (opts->output) {
msg_silent++;
redir_off = false;
msg_col = 0; // prevent leading spaces
}
@@ -92,6 +94,7 @@ String exec_impl(uint64_t channel_id, String src, Dict(exec_opts) *opts, Error *
if (opts->output) {
capture_ga = save_capture_ga;
msg_silent = save_msg_silent;
redir_off = save_redir_off;
// Put msg_col back where it was, since nothing should have been written.
msg_col = save_msg_col;
}

View File

@@ -385,6 +385,9 @@ describe('API', function()
)
eq({ output = '' }, api.nvim_exec2('echo', { output = true }))
eq({ output = 'foo 42' }, api.nvim_exec2('echo "foo" 42', { output = true }))
-- Returns output in cmdline mode #35321
feed(':')
eq({ output = 'foo 42' }, api.nvim_exec2('echo "foo" 42', { output = true }))
end)
it('displays messages when opts.output=false', function()
@@ -4945,6 +4948,9 @@ describe('API', function()
it('captures output', function()
eq('foo', api.nvim_cmd({ cmd = 'echo', args = { '"foo"' } }, { output = true }))
-- Returns output in cmdline mode #35321
feed(':')
eq('foo', api.nvim_cmd({ cmd = 'echo', args = { '"foo"' } }, { output = true }))
end)
it('sets correct script context', function()