comparison runtime/doc/if_lua.txt @ 16076:a2f0e93a5857 v8.1.1043

patch 8.1.1043: Lua interface does not support Blob commit https://github.com/vim/vim/commit/b78286903300477bb8578a47b8170b4551e290c8 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Mar 23 13:57:02 2019 +0100 patch 8.1.1043: Lua interface does not support Blob Problem: Lua interface does not support Blob. Solution: Add support to Blob. (Ozaki Kiichi, closes https://github.com/vim/vim/issues/4151)
author Bram Moolenaar <Bram@vim.org>
date Sat, 23 Mar 2019 14:00:06 +0100
parents 4a588e3afd4a
children 0e473e9e70c2
comparison
equal deleted inserted replaced
16075:5ca428f99af9 16076:a2f0e93a5857
8 8
9 1. Commands |lua-commands| 9 1. Commands |lua-commands|
10 2. The vim module |lua-vim| 10 2. The vim module |lua-vim|
11 3. List userdata |lua-list| 11 3. List userdata |lua-list|
12 4. Dict userdata |lua-dict| 12 4. Dict userdata |lua-dict|
13 5. Funcref userdata |lua-funcref| 13 5. Blob userdata |lua-blob|
14 6. Buffer userdata |lua-buffer| 14 6. Funcref userdata |lua-funcref|
15 7. Window userdata |lua-window| 15 7. Buffer userdata |lua-buffer|
16 8. The luaeval function |lua-luaeval| 16 8. Window userdata |lua-window|
17 9. Dynamic loading |lua-dynamic| 17 9. luaeval() Vim function |lua-luaeval|
18 10. Dynamic loading |lua-dynamic|
18 19
19 {Vi does not have any of these commands} 20 {Vi does not have any of these commands}
20 21
21 The Lua interface is available only when Vim was compiled with the 22 The Lua interface is available only when Vim was compiled with the
22 |+lua| feature. 23 |+lua| feature.
139 :lua t = {math.pi, false, say = 'hi'} 140 :lua t = {math.pi, false, say = 'hi'}
140 :echo luaeval('vim.dict(t)') 141 :echo luaeval('vim.dict(t)')
141 :" {'1': 3.141593, '2': v:false, 142 :" {'1': 3.141593, '2': v:false,
142 :" 'say': 'hi'} 143 :" 'say': 'hi'}
143 < 144 <
145 vim.blob([arg]) Returns an empty blob or, if "arg" is a Lua
146 string, returns a blob b such that b is
147 equivalent to "arg" as a byte string.
148 Examples: >
149 :lua s = "12ab\x00\x80\xfe\xff"
150 :echo luaeval('vim.blob(s)')
151 :" 0z31326162.0080FEFF
152 <
144 vim.funcref({name}) Returns a Funcref to function {name} (see 153 vim.funcref({name}) Returns a Funcref to function {name} (see
145 |Funcref|). It is equivalent to Vim's 154 |Funcref|). It is equivalent to Vim's
146 function(). 155 function().
147 156
148 vim.buffer([arg]) If "arg" is a number, returns buffer with 157 vim.buffer([arg]) If "arg" is a number, returns buffer with
258 :lua d.self = nil -- remove entry 267 :lua d.self = nil -- remove entry
259 :echo d 268 :echo d
260 < 269 <
261 270
262 ============================================================================== 271 ==============================================================================
263 5. Funcref userdata *lua-funcref* 272 5. Blob userdata *lua-blob*
273
274 Blob userdata represent vim blobs. A blob "b" has the following properties:
275
276 Properties
277 ----------
278 o "#b" is the length of blob "b", equivalent to "len(b)" in Vim.
279 o "b[k]" returns the k-th item in "b"; "b" is zero-indexed, as in Vim.
280 To modify the k-th item, simply do "b[k] = number"; in particular,
281 "b[#b] = number" can append a byte to tail.
282
283 Methods
284 -------
285 o "b:add(bytes)" appends "bytes" to the end of "b".
286
287 Examples:
288 >
289 :let b = 0z001122
290 :lua b = vim.eval('b') -- same 'b'
291 :lua print(b, b[0], #b)
292 :lua b[1] = 32
293 :lua b[#b] = 0x33 -- append a byte to tail
294 :lua b:add("\x80\x81\xfe\xff")
295 :echo b
296 <
297
298 ==============================================================================
299 6. Funcref userdata *lua-funcref*
264 300
265 Funcref userdata represent funcref variables in Vim. Funcrefs that were 301 Funcref userdata represent funcref variables in Vim. Funcrefs that were
266 defined with a "dict" attribute need to be obtained as a dictionary key 302 defined with a "dict" attribute need to be obtained as a dictionary key
267 in order to have "self" properly assigned to the dictionary (see examples 303 in order to have "self" properly assigned to the dictionary (see examples
268 below.) A funcref "f" has the following properties: 304 below.) A funcref "f" has the following properties:
291 :lua l = d.len -- assign d as 'self' 327 :lua l = d.len -- assign d as 'self'
292 :lua print(l()) 328 :lua print(l())
293 < 329 <
294 330
295 ============================================================================== 331 ==============================================================================
296 6. Buffer userdata *lua-buffer* 332 7. Buffer userdata *lua-buffer*
297 333
298 Buffer userdata represent vim buffers. A buffer userdata "b" has the following 334 Buffer userdata represent vim buffers. A buffer userdata "b" has the following
299 properties and methods: 335 properties and methods:
300 336
301 Properties 337 Properties
343 EOF 379 EOF
344 endfunction 380 endfunction
345 < 381 <
346 382
347 ============================================================================== 383 ==============================================================================
348 7. Window userdata *lua-window* 384 8. Window userdata *lua-window*
349 385
350 Window objects represent vim windows. A window userdata "w" has the following 386 Window objects represent vim windows. A window userdata "w" has the following
351 properties and methods: 387 properties and methods:
352 388
353 Properties 389 Properties
375 :lua n,w = 0,vim.window(true) while w~=nil do n,w = n + 1,w:next() end 411 :lua n,w = 0,vim.window(true) while w~=nil do n,w = n + 1,w:next() end
376 :lua print("There are " .. n .. " windows") 412 :lua print("There are " .. n .. " windows")
377 < 413 <
378 414
379 ============================================================================== 415 ==============================================================================
380 8. The luaeval function *lua-luaeval* *lua-eval* 416 9. luaeval() Vim function *lua-luaeval* *lua-eval*
381 417
382 The (dual) equivalent of "vim.eval" for passing Lua values to Vim is 418 The (dual) equivalent of "vim.eval" for passing Lua values to Vim is
383 "luaeval". "luaeval" takes an expression string and an optional argument and 419 "luaeval". "luaeval" takes an expression string and an optional argument and
384 returns the result of the expression. It is semantically equivalent in Lua to: 420 returns the result of the expression. It is semantically equivalent in Lua to:
385 > 421 >
388 local chunk = assert(loadstring(chunkheader .. expstr, "luaeval")) 424 local chunk = assert(loadstring(chunkheader .. expstr, "luaeval"))
389 return chunk(arg) -- return typval 425 return chunk(arg) -- return typval
390 end 426 end
391 < 427 <
392 Note that "_A" receives the argument to "luaeval". Lua numbers, strings, and 428 Note that "_A" receives the argument to "luaeval". Lua numbers, strings, and
393 list, dict, and funcref userdata are converted to their Vim respective types, 429 list, dict, blob, and funcref userdata are converted to their Vim respective
394 while Lua booleans are converted to numbers. An error is thrown if conversion 430 types, while Lua booleans are converted to numbers. An error is thrown if
395 of any of the remaining Lua types, including userdata other than lists, dicts, 431 conversion of any of the remaining Lua types, including userdata other than
396 and funcrefs, is attempted. 432 lists, dicts, blobs, and funcrefs, is attempted.
397 433
398 Examples: > 434 Examples: >
399 435
400 :echo luaeval('math.pi') 436 :echo luaeval('math.pi')
401 :lua a = vim.list():add('newlist') 437 :lua a = vim.list():add('newlist')
406 : endfunction 442 : endfunction
407 :echo Rand(1,10) 443 :echo Rand(1,10)
408 444
409 445
410 ============================================================================== 446 ==============================================================================
411 9. Dynamic loading *lua-dynamic* 447 10. Dynamic loading *lua-dynamic*
412 448
413 On MS-Windows and Unix the Lua library can be loaded dynamically. The 449 On MS-Windows and Unix the Lua library can be loaded dynamically. The
414 |:version| output then includes |+lua/dyn|. 450 |:version| output then includes |+lua/dyn|.
415 451
416 This means that Vim will search for the Lua DLL or shared library file only 452 This means that Vim will search for the Lua DLL or shared library file only