commit bf1d4e9793fcad74085ff6c32a6da5c43fe891e3
parent 3b6084ddf4f0d49fb0e5d135919c9daacea2a46d
Author: luukvbaal <luukvbaal@gmail.com>
Date: Fri, 6 Jun 2025 16:45:30 +0200
fix(messages): capture execute("messages") with ext_messages (#34342)
Problem: "msg_history_show" event is emitted when `msg_silent > 0`.
E.g. when capturing its output with `execute()`, which also
doesn't work with ext_messages.
Solution: Don't emit the "msg_history_show" event when `msg_silent > 0`.
Call regular messaging functions when `redirecting()`, to
execute `redir_write()` while ensuring the message itself
is not emitted.
Diffstat:
2 files changed, 35 insertions(+), 5 deletions(-)
diff --git a/src/nvim/message.c b/src/nvim/message.c
@@ -1187,10 +1187,12 @@ void ex_messages(exarg_T *eap)
Array entries = ARRAY_DICT_INIT;
MessageHistoryEntry *p = eap->skip ? msg_hist_temp : msg_hist_first;
int skip = eap->addr_count ? (msg_hist_len - eap->line2) : 0;
- while (p != NULL) {
+ for (; p != NULL; p = p->next) {
+ // Skip over count or temporary "g<" messages.
if ((p->temp && !eap->skip) || skip-- > 0) {
- // Skipping over count or temporary "g<" messages.
- } else if (ui_has(kUIMessages)) {
+ continue;
+ }
+ if (ui_has(kUIMessages) && !msg_silent) {
Array entry = ARRAY_DICT_INIT;
ADD(entry, CSTR_TO_OBJ(p->kind));
Array content = ARRAY_DICT_INIT;
@@ -1204,10 +1206,12 @@ void ex_messages(exarg_T *eap)
}
ADD(entry, ARRAY_OBJ(content));
ADD(entries, ARRAY_OBJ(entry));
- } else {
+ }
+ if (redirecting() || !ui_has(kUIMessages)) {
+ msg_silent += ui_has(kUIMessages);
msg_multihl(p->msg, p->kind, false, false);
+ msg_silent -= ui_has(kUIMessages);
}
- p = p->next;
}
if (kv_size(entries) > 0) {
ui_call_msg_history_show(entries);
diff --git a/test/functional/ui/messages_spec.lua b/test/functional/ui/messages_spec.lua
@@ -1722,6 +1722,32 @@ stack traceback:
},
})
end)
+
+ it('can capture execute("messages"))', function()
+ feed('Q')
+ screen:expect({
+ grid = [[
+ ^ |
+ {1:~ }|*4
+ ]],
+ messages = {
+ {
+ content = { { "E354: Invalid register name: '^@'", 9, 6 } },
+ history = true,
+ kind = 'emsg',
+ },
+ },
+ })
+ feed(':let msg = execute("messages")<CR>')
+ screen:expect({
+ grid = [[
+ ^ |
+ {1:~ }|*4
+ ]],
+ cmdline = { { abort = false } },
+ })
+ eq("E354: Invalid register name: '^@'", eval('msg'):gsub('\n', ''))
+ end)
end)
describe('ui/builtin messages', function()