Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/vimPublic

Commitbd84617

Browse files
committed
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.
1 parent7147820 commitbd84617

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

‎runtime/doc/if_lua.txt‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,15 @@ Vim's syntax for lists. Since lists are objects, changes in list references in
217217
Lua are reflected in Vim and vice-versa. A list "l" has the following
218218
properties and methods:
219219

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+
220224
Properties
221225
----------
222226
o "#l" is the number of items in list "l", equivalent to "len(l)"
223227
in Vim.
224-
o "l[k]" returns the k-th item in "l"; "l" iszero-indexed, as inVim.
228+
o "l[k]" returns the k-th item in "l"; "l" isone-indexed, as inLua.
225229
To modify the k-th item, simply do "l[k] = newitem"; in
226230
particular, "l[k] = nil" removes the k-th item from "l".
227231
o "l()" returns an iterator for "l".
@@ -237,11 +241,11 @@ Examples:
237241
:let l = [1, 'item']
238242
:lua l = vim.eval('l') -- same 'l'
239243
:lua l:add(vim.list())
240-
:lua l[0] = math.pi
244+
:lua l[1] = math.pi
241245
:echo l[0] " 3.141593
242-
:lua l[0] = nil -- remove first item
246+
:lua l[1] = nil -- remove first item
243247
: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])
245249
:lua for item in l() do print(item) end
246250
<
247251

‎src/if_lua.c‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,13 @@ luaV_list_index(lua_State *L)
871871
list_T*l=luaV_unbox(L,luaV_List,1);
872872
if (lua_isnumber(L,2))// list item?
873873
{
874-
listitem_T*li=list_find(l, (long)luaL_checkinteger(L,2));
874+
longn= (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);
875881
if (li==NULL)
876882
lua_pushnil(L);
877883
else
@@ -900,6 +906,10 @@ luaV_list_newindex(lua_State *L)
900906
list_T*l=luaV_unbox(L,luaV_List,1);
901907
longn= (long)luaL_checkinteger(L,2);
902908
listitem_T*li;
909+
910+
// Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
911+
n-=1;
912+
903913
if (l->lv_lock)
904914
luaL_error(L,"list is locked");
905915
li=list_find(l,n);

‎src/testdir/test_lua.vim‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ func Test_lua_list()
327327
callassert_equal(7,luaeval('#l'))
328328
callassert_match('^list: \%(0x\)\?\x\+$',luaeval('tostring(l)'))
329329

330-
lual[0]=124
331-
lual[5]= nil
330+
lual[1]=124
331+
lual[6]= nil
332332
lual:insert('first')
333333
lual:insert('xx',3)
334334
callassert_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()
367367
lual=vim.list():add(1):add(2)
368368
lual=l:add(l)
369369

370-
callassert_equal(1,luaeval('l[0]'))
371-
callassert_equal(2,luaeval('l[1]'))
370+
callassert_equal(1,luaeval('l[1]'))
371+
callassert_equal(2,luaeval('l[2]'))
372372

373-
callassert_equal(1,luaeval('l[2][0]'))
374-
callassert_equal(2,luaeval('l[2][1]'))
373+
callassert_equal(1,luaeval('l[3][1]'))
374+
callassert_equal(2,luaeval('l[3][2]'))
375375

376-
callassert_equal(1,luaeval('l[2][2][0]'))
377-
callassert_equal(2,luaeval('l[2][2][1]'))
376+
callassert_equal(1,luaeval('l[3][3][1]'))
377+
callassert_equal(2,luaeval('l[3][3][2]'))
378378

379379
callassert_equal('[1, 2, [...]]',string(luaeval('l')))
380380

381381
callassert_match('^list: \%(0x\)\?\x\+$',luaeval('tostring(l)'))
382-
callassert_equal(luaeval('tostring(l)'),luaeval('tostring(l[2])'))
382+
callassert_equal(luaeval('tostring(l)'),luaeval('tostring(l[3])'))
383383

384-
callassert_equal(luaeval('l'),luaeval('l[2]'))
385-
callassert_equal(luaeval('l'),luaeval('l[2][2]'))
384+
callassert_equal(luaeval('l'),luaeval('l[3]'))
385+
callassert_equal(luaeval('l'),luaeval('l[3][3]'))
386386

387387
lual= nil
388388
endfunc

‎src/version.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,8 @@ static char *(features[]) =
754754

755755
staticintincluded_patches[]=
756756
{/* Add new patch number below this line */
757+
/**/
758+
1066,
757759
/**/
758760
1065,
759761
/**/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp