From 49a87bd09c1cce498760d3eacdc4be7191aef8f0 Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Thu, 22 Jan 2026 18:16:51 +0200 Subject: [PATCH] Lua pushkey and getlin Allow lua nh.pushkey to push multiple keys, make getlin return the keys in the command queue. --- src/nhlua.c | 5 ++++- src/windows.c | 21 +++++++++++++++++++++ test/test_src.lua | 10 ++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/nhlua.c b/src/nhlua.c index 4f96e24c4..767726277 100644 --- a/src/nhlua.c +++ b/src/nhlua.c @@ -1381,7 +1381,10 @@ nhl_pushkey(lua_State *L) if (argc == 1) { const char *key = luaL_checkstring(L, 1); - cmdq_add_key(CQ_CANNED, key[0]); + while (*key) { + cmdq_add_key(CQ_CANNED, *key); + key++; + } } return 0; diff --git a/src/windows.c b/src/windows.c index b929cf62f..ff9fd3863 100644 --- a/src/windows.c +++ b/src/windows.c @@ -1867,6 +1867,27 @@ void getlin(const char *query, char *bufp) { boolean old_bot_disabled = gb.bot_disabled; + char *obufp = bufp; + boolean got_cmdq = FALSE; + struct _cmd_queue *cmdq = NULL; + + while ((cmdq = cmdq_pop()) != 0) { + if (cmdq->typ == CMDQ_KEY) { + got_cmdq = TRUE; + *bufp = (cmdq->key != '\n') ? cmdq->key : '\0'; + bufp++; + if (cmdq->key == '\n') + break; + } else { + break; + } + } + + if (got_cmdq) { + *bufp = '\0'; + pline("%s %s", query, obufp); + return; + } program_state.in_getlin = 1; gb.bot_disabled = TRUE; diff --git a/test/test_src.lua b/test/test_src.lua index 2ae45d0ac..645811033 100644 --- a/test/test_src.lua +++ b/test/test_src.lua @@ -121,3 +121,13 @@ for func, fval in pairs(tests) do end end end + +function test_getlin() + nh.pushkey("AbC"); + local str = nh.getlin("What?"); + if str ~= "AbC" then + error("nh.getlin fail, got \"" .. str .. "\""); + end +end + +test_getlin();