From e7d18ae97ecbe5db4c9a511afc4c05fa9bf8204b Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sat, 18 Jun 2016 12:45:56 +0000 Subject: MirLua: interpolate can use local variables with ${name} pattern git-svn-id: http://svn.miranda-ng.org/main/trunk@17005 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/mlua_utils.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'plugins/MirLua/src') diff --git a/plugins/MirLua/src/mlua_utils.cpp b/plugins/MirLua/src/mlua_utils.cpp index f02ca6e147..574de71ba2 100644 --- a/plugins/MirLua/src/mlua_utils.cpp +++ b/plugins/MirLua/src/mlua_utils.cpp @@ -208,6 +208,8 @@ int luaM_interpolate(lua_State *L) { const char *string = luaL_checkstring(L, 1); + char pattern[128]; + if (lua_istable(L, 2)) { for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 2)) @@ -216,7 +218,6 @@ int luaM_interpolate(lua_State *L) const char *key = lua_tostring(L, -1); const char *val = lua_tostring(L, -2); - char pattern[32]; mir_snprintf(pattern, "{%s}", key); string = luaL_gsub(L, string, pattern, val); lua_pop(L, 1); @@ -229,13 +230,26 @@ int luaM_interpolate(lua_State *L) { const char *val = lua_tostring(L, i); - char pattern[32]; mir_snprintf(pattern, "{%d}", i - 1); string = luaL_gsub(L, string, pattern, val); lua_pop(L, 1); } } + lua_Debug ar; + lua_getstack(L, 1, &ar); + const char *name = lua_getlocal(L, &ar, 1); + + size_t i = 1; + while (const char *key = lua_getlocal(L, &ar, i++)) + { + const char *val = lua_tostring(L, -1); + + mir_snprintf(pattern, "${%s}", name); + string = luaL_gsub(L, string, pattern, val); + lua_pop(L, 1); + } + lua_pushstring(L, string); return 1; -- cgit v1.2.3