Mercurial > vim
comparison runtime/doc/if_lua.txt @ 21030:08e284594211 v8.2.1066
patch 8.2.1066: Lua arrays are zero based
Commit: https://github.com/vim/vim/commit/bd84617d1a6766efd59c94aabebb044bef805b99
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Jun 27 12:32:57 2020 +0200
patch 8.2.1066: Lua arrays are zero based
Problem: Lua arrays are zero based.
Solution: Make Lua arrays one based. (Prabir Shrestha, closes https://github.com/vim/vim/issues/6347)
Note: this is not backwards compatible.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 27 Jun 2020 12:45:04 +0200 |
parents | ae185f35e256 |
children | 89aba7895bb2 |
comparison
equal
deleted
inserted
replaced
21029:857b879cfc59 | 21030:08e284594211 |
---|---|
215 List userdata represent vim lists, and the interface tries to follow closely | 215 List userdata represent vim lists, and the interface tries to follow closely |
216 Vim's syntax for lists. Since lists are objects, changes in list references in | 216 Vim's syntax for lists. Since lists are objects, changes in list references in |
217 Lua are reflected in Vim and vice-versa. A list "l" has the following | 217 Lua are reflected in Vim and vice-versa. A list "l" has the following |
218 properties and methods: | 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 Properties | 224 Properties |
221 ---------- | 225 ---------- |
222 o "#l" is the number of items in list "l", equivalent to "len(l)" | 226 o "#l" is the number of items in list "l", equivalent to "len(l)" |
223 in Vim. | 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 To modify the k-th item, simply do "l[k] = newitem"; in | 229 To modify the k-th item, simply do "l[k] = newitem"; in |
226 particular, "l[k] = nil" removes the k-th item from "l". | 230 particular, "l[k] = nil" removes the k-th item from "l". |
227 o "l()" returns an iterator for "l". | 231 o "l()" returns an iterator for "l". |
228 | 232 |
229 Methods | 233 Methods |
235 Examples: | 239 Examples: |
236 > | 240 > |
237 :let l = [1, 'item'] | 241 :let l = [1, 'item'] |
238 :lua l = vim.eval('l') -- same 'l' | 242 :lua l = vim.eval('l') -- same 'l' |
239 :lua l:add(vim.list()) | 243 :lua l:add(vim.list()) |
240 :lua l[0] = math.pi | 244 :lua l[1] = math.pi |
241 :echo l[0] " 3.141593 | 245 :echo l[0] " 3.141593 |
242 :lua l[0] = nil -- remove first item | 246 :lua l[1] = nil -- remove first item |
243 :lua l:insert(true, 1) | 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 :lua for item in l() do print(item) end | 249 :lua for item in l() do print(item) end |
246 < | 250 < |
247 | 251 |
248 ============================================================================== | 252 ============================================================================== |
249 4. Dict userdata *lua-dict* | 253 4. Dict userdata *lua-dict* |