commit 7b9512e613e1cc01f5a3ec05c2309a1ad2fba890
parent 44fdbd65896b3807744327bec1b0595a41e4dcaa
Author: zeertzjq <zeertzjq@outlook.com>
Date: Wed, 13 Aug 2025 20:12:47 +0800
fix(api): fix not capturing output in cmdline mode (#35322)
Diffstat:
3 files changed, 12 insertions(+), 0 deletions(-)
diff --git a/src/nvim/api/command.c b/src/nvim/api/command.c
@@ -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;
}
diff --git a/src/nvim/api/vimscript.c b/src/nvim/api/vimscript.c
@@ -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;
}
diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua
@@ -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()