+32
-16
lines changedFilter options
+32
-16
lines changed Original file line number Diff line number Diff line change
@@ -217,11 +217,15 @@ Vim's syntax for lists. Since lists are objects, changes in list references in
217
217
Lua are reflected in Vim and vice-versa. A list "l" has the following
218
218
properties and methods:
219
219
220
+
NOTE: In patch 8.2.1066 array indexes were changed from zero-based to
221
+
one-based. You can check with: >
222
+
if has("patch-8.2.1066")
223
+
220
224
Properties
221
225
----------
222
226
o "#l" is the number of items in list "l", equivalent to "len(l)"
223
227
in Vim.
224
-
o "l[k]" returns the k-th item in "l"; "l" is zero-indexed, as in Vim.
228
+
o "l[k]" returns the k-th item in "l"; "l" is one-indexed, as in Lua.
225
229
To modify the k-th item, simply do "l[k] = newitem"; in
226
230
particular, "l[k] = nil" removes the k-th item from "l".
227
231
o "l()" returns an iterator for "l".
@@ -237,11 +241,11 @@ Examples:
237
241
:let l = [1, 'item']
238
242
:lua l = vim.eval('l') -- same 'l'
239
243
:lua l:add(vim.list())
240
-
:lua l[0] = math.pi
244
+
:lua l[1] = math.pi
241
245
:echo l[0] " 3.141593
242
-
:lua l[0] = nil -- remove first item
246
+
:lua l[1] = nil -- remove first item
243
247
:lua l:insert(true, 1)
244
-
:lua print(l, #l, l[0], l[1], l[-1])
248
+
:lua print(l, #l, l[1], l[2])
245
249
:lua for item in l() do print(item) end
246
250
<
247
251
Original file line number Diff line number Diff line change
@@ -871,7 +871,13 @@ luaV_list_index(lua_State *L)
871
871
list_T *l = luaV_unbox(L, luaV_List, 1);
872
872
if (lua_isnumber(L, 2)) // list item?
873
873
{
874
-
listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
874
+
long n = (long) luaL_checkinteger(L, 2);
875
+
listitem_T *li;
876
+
877
+
// Lua array index starts with 1 while Vim uses 0, subtract 1 to
878
+
// normalize.
879
+
n -= 1;
880
+
li = list_find(l, n);
875
881
if (li == NULL)
876
882
lua_pushnil(L);
877
883
else
@@ -900,6 +906,10 @@ luaV_list_newindex(lua_State *L)
900
906
list_T *l = luaV_unbox(L, luaV_List, 1);
901
907
long n = (long) luaL_checkinteger(L, 2);
902
908
listitem_T *li;
909
+
910
+
// Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
911
+
n -= 1;
912
+
903
913
if (l->lv_lock)
904
914
luaL_error(L, "list is locked");
905
915
li = list_find(l, n);
Original file line number Diff line number Diff line change
@@ -327,8 +327,8 @@ func Test_lua_list()
327
327
call assert_equal(7, luaeval('#l'))
328
328
call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
329
329
330
-
lua l[0] = 124
331
-
lua l[5] = nil
330
+
lua l[1] = 124
331
+
lua l[6] = nil
332
332
lua l:insert('first')
333
333
lua l:insert('xx', 3)
334
334
call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l)
@@ -367,22 +367,22 @@ func Test_lua_recursive_list()
367
367
lua l = vim.list():add(1):add(2)
368
368
lua l = l:add(l)
369
369
370
-
call assert_equal(1, luaeval('l[0]'))
371
-
call assert_equal(2, luaeval('l[1]'))
370
+
call assert_equal(1, luaeval('l[1]'))
371
+
call assert_equal(2, luaeval('l[2]'))
372
372
373
-
call assert_equal(1, luaeval('l[2][0]'))
374
-
call assert_equal(2, luaeval('l[2][1]'))
373
+
call assert_equal(1, luaeval('l[3][1]'))
374
+
call assert_equal(2, luaeval('l[3][2]'))
375
375
376
-
call assert_equal(1, luaeval('l[2][2][0]'))
377
-
call assert_equal(2, luaeval('l[2][2][1]'))
376
+
call assert_equal(1, luaeval('l[3][3][1]'))
377
+
call assert_equal(2, luaeval('l[3][3][2]'))
378
378
379
379
call assert_equal('[1, 2, [...]]', string(luaeval('l')))
380
380
381
381
call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
382
-
call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[2])'))
382
+
call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])'))
383
383
384
-
call assert_equal(luaeval('l'), luaeval('l[2]'))
385
-
call assert_equal(luaeval('l'), luaeval('l[2][2]'))
384
+
call assert_equal(luaeval('l'), luaeval('l[3]'))
385
+
call assert_equal(luaeval('l'), luaeval('l[3][3]'))
386
386
387
387
lua l = nil
388
388
endfunc
Original file line number Diff line number Diff line change
@@ -754,6 +754,8 @@ static char *(features[]) =
754
754
755
755
static int included_patches[] =
756
756
{ /* Add new patch number below this line */
757
+
/**/
758
+
1066,
757
759
/**/
758
760
1065,
759
761
/**/
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4