From 58ce7218ec9aa85cc090cee4654e4573f6dd8570 Mon Sep 17 00:00:00 2001 From: bfredl Date: Thu, 24 Sep 2026 14:31:40 +0200 Subject: feat(lua): in script mode -l, use raw output for print() consider the following test_script.lua : print 'hello\n' -- TWO new lines print 'this is \027[31mRED FOR AN AMAZING REASON\027[0m' print 'foo' print '' -- newline despite empty! print 'bar' -- a bit later: os.execute 'sleep 2' -- CPU goes BRR, cursor in column zero print 'more bar' before this PR, `nvim -l test_script.lua` had some weird behaviors compared to `lua test_script.lua` or the now removed `nvim -ll test_script.lua`. - one extra '\n' at the end of a print is ignored, one would need TWO '\n' for a single extra '\n' - an empty print'' is supposed to print an empty line - print'\027[31mRED' is being escaped so it does not work - the cursor gets stuck at the end of a message, instead of after emitting the new line, this looks like the CPU lag has something to do with the _last_ printed message which is likely not the case. I consider these behaviors to be unintentional (a consequence of stragglig through the message.c meta-machine, which is optimized for message handling in _an interative text editor_) and getting closer to plain lua behavior would be better. An alternative would be to restore the builtin print(), but our implemenation still has some benefits, like try `print(setmetatable({}, {__tostring=error})` in plain lua.. --- src/nvim/lua/executor.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index a34f08a36c..a0b982ab0f 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -20,6 +20,7 @@ #include "nvim/ascii_defs.h" #include "nvim/buffer_defs.h" #include "nvim/change.h" +#include "nvim/channel.h" #include "nvim/cmdexpand_defs.h" #include "nvim/cursor.h" #include "nvim/drawscreen.h" @@ -1117,12 +1118,18 @@ static void nlua_common_free_all_mem(lua_State *lstate) static void nlua_print_event(void **argv) { - HlMessage msg = KV_INITIAL_VALUE; - HlMessageChunk chunk = { { .data = argv[0], .size = (size_t)(intptr_t)argv[1] - 1 }, 0 }; - kv_push(msg, chunk); - bool needs_clear = false; msg_ext_no_fast(); - msg_multihl(NIL, msg, "lua_print", true, false, NULL, &needs_clear); + if (msg_use_printf() && on_print.type == kCallbackNone) { + msg_start(); // flush incomplete message + printf("%.*s\n", (int)(intptr_t)argv[1] - 1, (char *)argv[0]); + xfree(argv[0]); + } else { + HlMessage msg = KV_INITIAL_VALUE; + HlMessageChunk chunk = { { .data = argv[0], .size = (size_t)(intptr_t)argv[1] - 1 }, 0 }; + kv_push(msg, chunk); + bool needs_clear = false; + msg_multihl(NIL, msg, "lua_print", true, false, NULL, &needs_clear); + } } /// Implements Lua print() as a Nvim message. -- cgit