Home Explore Blog CI



neovim

5th chunk of `runtime/doc/lua.txt`
1a00739e771abed4379228f368576fff92b6980b1ccb3e700000000100000fa3
 local curline = vim.api.nvim_buf_get_lines(0, linenr - 1, linenr, false)[1]
        print(string.format('Line [%d] has %d bytes', linenr, #curline))
        EOF
        endfunction
<
    Note that the `local` variables will disappear when the block finishes.
    But not globals.

                                                                      *:luado*
:[range]luado {body}
    Executes Lua chunk "function(line, linenr) {body} end" for each buffer
    line in [range], where `line` is the current line text (without <EOL>),
    and `linenr` is the current line number. If the function returns a string
    that becomes the text of the corresponding buffer line. Default [range] is
    the whole file: "1,$".

    Examples: >vim
        :luado return string.format("%s\t%d", line:reverse(), #line)

        :lua require"lpeg"
        :lua -- balanced parenthesis grammar:
        :lua bp = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" }
        :luado if bp:match(line) then return "=>\t" .. line end
<
                                                                    *:luafile*
:luafile {file}
    Execute Lua script in {file}.
    The whole argument is used as the filename (like |:edit|), spaces do not
    need to be escaped. Alternatively you can |:source| Lua files.

    Examples: >vim
        :luafile script.lua
        :luafile %
<

==============================================================================
luaeval()                                                 *lua-eval*

The (dual) equivalent of "vim.eval" for passing Lua values to Nvim is
"luaeval". "luaeval" takes an expression string and an optional argument used
for _A inside expression and returns the result of the expression. It is
semantically equivalent in Lua to: >lua

    local chunkheader = "local _A = select(1, ...) return "
    function luaeval (expstr, arg)
        local chunk = assert(loadstring(chunkheader .. expstr, "luaeval"))
        return chunk(arg) -- return typval
    end
<
Lua nils, numbers, strings, tables and booleans are converted to their
respective Vimscript types. If a Lua string contains a NUL byte, it will be
converted to a |Blob|. Conversion of other Lua types is an error.

The magic global "_A" contains the second argument to luaeval().

Example: >vim
    :echo luaeval('_A[1] + _A[2]', [40, 2])
    " 42
    :echo luaeval('string.match(_A, "[a-z]+")', 'XYXfoo123')
    " foo
<
                                                         *lua-table-ambiguous*
Lua tables are used as both dictionaries and lists, so it is impossible to
decide whether empty table is a list or a dict. Also Lua does not have integer
numbers. To disambiguate these cases, we define:
                                                                    *lua-list*
0. Empty table is a list. Use |vim.empty_dict()| to represent empty dict.
1. Table with N consecutive (no `nil` values, aka "holes") integer keys 1…N is
   a list. See also |list-iterator|.
                                                                    *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.

Title: Lua Commands: :luado, :luafile, and luaeval() Function
Summary
This section details the `:luado` and `:luafile` commands for executing Lua code on buffer lines and from external files, respectively. It also introduces the `luaeval()` function for passing Lua values to Nvim and describes how Lua data types, particularly tables (lists and dictionaries), are converted to Vimscript types, including special cases like empty tables and the use of `vim.type_idx` for disambiguation.