patch 8.2.1066: Lua arrays are zero based
Problem: Lua arrays are zero based.
Solution: Make Lua arrays one based. (Prabir Shrestha, closes #6347)
Note: this is not backwards compatible.
diff --git a/src/if_lua.c b/src/if_lua.c
index 4cab3f4..2d02f7c 100644
--- a/src/if_lua.c
+++ b/src/if_lua.c
@@ -871,7 +871,13 @@
list_T *l = luaV_unbox(L, luaV_List, 1);
if (lua_isnumber(L, 2)) // list item?
{
- listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
+ long n = (long) luaL_checkinteger(L, 2);
+ listitem_T *li;
+
+ // Lua array index starts with 1 while Vim uses 0, subtract 1 to
+ // normalize.
+ n -= 1;
+ li = list_find(l, n);
if (li == NULL)
lua_pushnil(L);
else
@@ -900,6 +906,10 @@
list_T *l = luaV_unbox(L, luaV_List, 1);
long n = (long) luaL_checkinteger(L, 2);
listitem_T *li;
+
+ // Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
+ n -= 1;
+
if (l->lv_lock)
luaL_error(L, "list is locked");
li = list_find(l, n);