Home Explore Blog CI



neovim

6th chunk of `runtime/doc/lua.txt`
ee8c290b5675a824cb6b0c3aa8b3c630643d89b64ae7ec890000000100000fa0
                                  *lua-dict*
2. Table with string keys, none of which contains NUL byte, is a dict.
3. Table with string keys, at least one of which contains NUL byte, is also
   considered to be a dictionary, but this time it is converted to
   a |msgpack-special-map|.
                                                             *lua-special-tbl*
4. Table with `vim.type_idx` key may be a dictionary, a list or floating-point
   value:
   - `{[vim.type_idx]=vim.types.float, [vim.val_idx]=1}` is converted to
     a floating-point 1.0. Note that by default integral Lua numbers are
     converted to |Number|s, non-integral are converted to |Float|s. This
     variant allows integral |Float|s.
   - `{[vim.type_idx]=vim.types.dictionary}` is converted to an empty
     dictionary, `{[vim.type_idx]=vim.types.dictionary, [42]=1, a=2}` is
     converted to a dictionary `{'a': 42}`: non-string keys are ignored.
     Without `vim.type_idx` key tables with keys not fitting in 1., 2. or 3.
     are errors.
   - `{[vim.type_idx]=vim.types.array}` is converted to an empty list. As well
     as `{[vim.type_idx]=vim.types.array, [42]=1}`: integral keys that do not
     form a 1-step sequence from 1 to N are ignored, as well as all
     non-integral keys.

Examples: >vim

    :echo luaeval('math.pi')
    :function Rand(x,y) " random uniform between x and y
    :  return luaeval('(_A.y-_A.x)*math.random()+_A.x', {'x':a:x,'y':a:y})
    :  endfunction
    :echo Rand(1,10)
<
Note: Second argument to `luaeval` is converted ("marshalled") from Vimscript
to Lua, so changes to Lua containers do not affect values in Vimscript. Return
value is also always converted. When converting, |msgpack-special-dict|s are
treated specially.

==============================================================================
Vimscript v:lua interface                                         *v:lua-call*

From Vimscript the special `v:lua` prefix can be used to call Lua functions
which are global or accessible from global tables. The expression >vim
    call v:lua.func(arg1, arg2)
is equivalent to the Lua chunk >lua
    return func(...)
where the args are converted to Lua values. The expression >vim
    call v:lua.somemod.func(args)
is equivalent to the Lua chunk >lua
    return somemod.func(...)

Lua module functions can be accessed like: >vim
    call v:lua.require'mypack'.func(arg1, arg2)
    call v:lua.require'mypack.submod'.func(arg1, arg2)
Note: Only single quote form without parens is allowed. Using
`require"mypack"` or `require('mypack')` as a prefix does NOT work.

You can use `v:lua` in "func" options like 'tagfunc', 'omnifunc', etc.
For example consider the following Lua omnifunc handler: >lua

    function mymod.omnifunc(findstart, base)
      if findstart == 1 then
        return 0
      else
        return {'stuff', 'steam', 'strange things'}
      end
    end
    -- Note: The module ("mymod") must be a Lua global, or use require() as
    -- shown above to access it from a package.
    vim.bo[buf].omnifunc = 'v:lua.mymod.omnifunc'

You can also use `v:lua` to call Lua functions as Vimscript |method|s: >vim
    :eval arg1->v:lua.somemod.func(arg2)
<
Note: `v:lua` without a call is not allowed in a Vimscript expression:
|Funcref|s cannot represent Lua functions. The following are errors: >vim

    let g:Myvar = v:lua.myfunc        " Error
    call SomeFunc(v:lua.mycallback)   " Error
    let g:foo = v:lua                 " Error
    let g:foo = v:['lua']             " Error
<
==============================================================================
Lua standard modules                                              *lua-stdlib*

The Nvim Lua "standard library" (stdlib) is the `vim` module, which exposes
various functions and sub-modules. It is always loaded, thus `require("vim")`
is unnecessary.

You can peek at the module properties: >vim

    :lua vim.print(vim)

Result is something like this: >

    {
      _os_proc_children = <function 1>,

Title: Lua Table Conversion and Vimscript Interface (v:lua)
Summary
This section elaborates on Lua table conversion rules to Vimscript types, focusing on dictionaries and special tables using `vim.type_idx`. It also provides examples of using `luaeval()` and introduces the `v:lua` interface, which allows calling Lua functions from Vimscript. It covers how to access Lua module functions and use them in Vimscript functions like 'tagfunc' and 'omnifunc'. It also highlights the restrictions of using `v:lua` in Vimscript expressions.