2017-05-11 08:45:11 -07:00
|
|
|
|
*if_lua.txt* Nvim
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
|
|
|
|
|
2017-10-22 07:29:36 -07:00
|
|
|
|
NVIM REFERENCE MANUAL
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
Lua engine *lua* *Lua*
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
2017-10-20 17:33:58 -07:00
|
|
|
|
Type |gO| to see the table of contents.
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
Introduction *lua-intro*
|
|
|
|
|
|
|
|
|
|
The Lua 5.1 language is builtin and always available. Try this command to get
|
|
|
|
|
an idea of what lurks beneath: >
|
|
|
|
|
|
|
|
|
|
:lua print(vim.inspect(package.loaded))
|
|
|
|
|
|
2019-02-04 05:21:35 -07:00
|
|
|
|
Nvim includes a "standard library" |lua-stdlib| for Lua. It complements the
|
|
|
|
|
"editor stdlib" (|functions| and Ex commands) and the |API|, all of which can
|
|
|
|
|
be used from Lua code.
|
2019-01-10 17:20:15 -07:00
|
|
|
|
|
2019-02-04 05:21:35 -07:00
|
|
|
|
Module conflicts are resolved by "last wins". For example if both of these
|
2019-01-10 17:20:15 -07:00
|
|
|
|
are on 'runtimepath':
|
|
|
|
|
runtime/lua/foo.lua
|
|
|
|
|
~/.config/nvim/lua/foo.lua
|
|
|
|
|
then `require('foo')` loads "~/.config/nvim/lua/foo.lua", and
|
|
|
|
|
"runtime/lua/foo.lua" is not used. See |lua-require| to understand how Nvim
|
|
|
|
|
finds and loads Lua modules. The conventions are similar to VimL plugins,
|
|
|
|
|
with some extra features. See |lua-require-example| for a walkthrough.
|
|
|
|
|
|
2017-01-28 19:09:54 -07:00
|
|
|
|
==============================================================================
|
2017-10-22 07:29:36 -07:00
|
|
|
|
Importing modules *lua-require*
|
2017-05-22 14:46:57 -07:00
|
|
|
|
|
2017-10-22 07:29:36 -07:00
|
|
|
|
Nvim automatically adjusts `package.path` and `package.cpath` according to
|
|
|
|
|
effective 'runtimepath' value. Adjustment happens whenever 'runtimepath' is
|
|
|
|
|
changed. `package.path` is adjusted by simply appending `/lua/?.lua` and
|
|
|
|
|
`/lua/?/init.lua` to each directory from 'runtimepath' (`/` is actually the
|
|
|
|
|
first character of `package.config`).
|
2017-05-25 14:17:36 -07:00
|
|
|
|
|
2017-10-22 07:29:36 -07:00
|
|
|
|
Similarly to `package.path`, modified directories from 'runtimepath' are also
|
|
|
|
|
added to `package.cpath`. In this case, instead of appending `/lua/?.lua` and
|
|
|
|
|
`/lua/?/init.lua` to each runtimepath, all unique `?`-containing suffixes of
|
|
|
|
|
the existing `package.cpath` are used. Example:
|
2017-05-22 14:46:57 -07:00
|
|
|
|
|
2017-05-25 15:31:02 -07:00
|
|
|
|
1. Given that
|
|
|
|
|
- 'runtimepath' contains `/foo/bar,/xxx;yyy/baz,/abc`;
|
2017-08-19 05:13:14 -07:00
|
|
|
|
- initial (defined at compile-time or derived from
|
2017-05-25 15:31:02 -07:00
|
|
|
|
`$LUA_CPATH`/`$LUA_INIT`) `package.cpath` contains
|
|
|
|
|
`./?.so;/def/ghi/a?d/j/g.elf;/def/?.so`.
|
|
|
|
|
2. It finds `?`-containing suffixes `/?.so`, `/a?d/j/g.elf` and `/?.so`, in
|
|
|
|
|
order: parts of the path starting from the first path component containing
|
|
|
|
|
question mark and preceding path separator.
|
|
|
|
|
3. The suffix of `/def/?.so`, namely `/?.so` is not unique, as it’s the same
|
|
|
|
|
as the suffix of the first path from `package.path` (i.e. `./?.so`). Which
|
|
|
|
|
leaves `/?.so` and `/a?d/j/g.elf`, in this order.
|
|
|
|
|
4. 'runtimepath' has three paths: `/foo/bar`, `/xxx;yyy/baz` and `/abc`. The
|
|
|
|
|
second one contains semicolon which is a paths separator so it is out,
|
|
|
|
|
leaving only `/foo/bar` and `/abc`, in order.
|
|
|
|
|
5. The cartesian product of paths from 4. and suffixes from 3. is taken,
|
|
|
|
|
giving four variants. In each variant `/lua` path segment is inserted
|
|
|
|
|
between path and suffix, leaving
|
2017-05-22 14:46:57 -07:00
|
|
|
|
|
2017-05-25 15:31:02 -07:00
|
|
|
|
- `/foo/bar/lua/?.so`
|
|
|
|
|
- `/foo/bar/lua/a?d/j/g.elf`
|
|
|
|
|
- `/abc/lua/?.so`
|
|
|
|
|
- `/abc/lua/a?d/j/g.elf`
|
|
|
|
|
|
|
|
|
|
6. New paths are prepended to the original `package.cpath`.
|
|
|
|
|
|
|
|
|
|
The result will look like this:
|
|
|
|
|
|
|
|
|
|
`/foo/bar,/xxx;yyy/baz,/abc` ('runtimepath')
|
|
|
|
|
× `./?.so;/def/ghi/a?d/j/g.elf;/def/?.so` (`package.cpath`)
|
|
|
|
|
|
|
|
|
|
= `/foo/bar/lua/?.so;/foo/bar/lua/a?d/j/g.elf;/abc/lua/?.so;/abc/lua/a?d/j/g.elf;./?.so;/def/ghi/a?d/j/g.elf;/def/?.so`
|
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
Note:
|
2017-05-22 14:46:57 -07:00
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
- To track 'runtimepath' updates, paths added at previous update are
|
|
|
|
|
remembered and removed at the next update, while all paths derived from the
|
|
|
|
|
new 'runtimepath' are prepended as described above. This allows removing
|
|
|
|
|
paths when path is removed from 'runtimepath', adding paths when they are
|
|
|
|
|
added and reordering `package.path`/`package.cpath` content if 'runtimepath'
|
|
|
|
|
was reordered.
|
2017-05-22 14:46:57 -07:00
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
- Although adjustments happen automatically, Nvim does not track current
|
|
|
|
|
values of `package.path` or `package.cpath`. If you happen to delete some
|
|
|
|
|
paths from there you can set 'runtimepath' to trigger an update: >
|
|
|
|
|
let &runtimepath = &runtimepath
|
|
|
|
|
|
|
|
|
|
- Skipping paths from 'runtimepath' which contain semicolons applies both to
|
|
|
|
|
`package.path` and `package.cpath`. Given that there are some badly written
|
|
|
|
|
plugins using shell which will not work with paths containing semicolons it
|
|
|
|
|
is better to not have them in 'runtimepath' at all.
|
2017-05-22 14:46:57 -07:00
|
|
|
|
|
2017-05-23 12:49:08 -07:00
|
|
|
|
------------------------------------------------------------------------------
|
2019-01-10 17:20:15 -07:00
|
|
|
|
LUA PLUGIN EXAMPLE *lua-require-example*
|
2017-05-23 12:49:08 -07:00
|
|
|
|
|
|
|
|
|
The following example plugin adds a command `:MakeCharBlob` which transforms
|
|
|
|
|
current buffer into a long `unsigned char` array. Lua contains transformation
|
|
|
|
|
function in a module `lua/charblob.lua` which is imported in
|
|
|
|
|
`autoload/charblob.vim` (`require("charblob")`). Example plugin is supposed
|
|
|
|
|
to be put into any directory from 'runtimepath', e.g. `~/.config/nvim` (in
|
|
|
|
|
this case `lua/charblob.lua` means `~/.config/nvim/lua/charblob.lua`).
|
|
|
|
|
|
|
|
|
|
autoload/charblob.vim: >
|
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
function charblob#encode_buffer()
|
|
|
|
|
call setline(1, luaeval(
|
|
|
|
|
\ 'require("charblob").encode(unpack(_A))',
|
|
|
|
|
\ [getline(1, '$'), &textwidth, ' ']))
|
|
|
|
|
endfunction
|
2017-05-23 12:49:08 -07:00
|
|
|
|
|
|
|
|
|
plugin/charblob.vim: >
|
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
if exists('g:charblob_loaded')
|
|
|
|
|
finish
|
|
|
|
|
endif
|
|
|
|
|
let g:charblob_loaded = 1
|
2017-05-23 12:49:08 -07:00
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
command MakeCharBlob :call charblob#encode_buffer()
|
2017-05-23 12:49:08 -07:00
|
|
|
|
|
|
|
|
|
lua/charblob.lua: >
|
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
local function charblob_bytes_iter(lines)
|
|
|
|
|
local init_s = {
|
|
|
|
|
next_line_idx = 1,
|
|
|
|
|
next_byte_idx = 1,
|
|
|
|
|
lines = lines,
|
|
|
|
|
}
|
|
|
|
|
local function next(s, _)
|
|
|
|
|
if lines[s.next_line_idx] == nil then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
if s.next_byte_idx > #(lines[s.next_line_idx]) then
|
|
|
|
|
s.next_line_idx = s.next_line_idx + 1
|
|
|
|
|
s.next_byte_idx = 1
|
|
|
|
|
return ('\n'):byte()
|
|
|
|
|
end
|
|
|
|
|
local ret = lines[s.next_line_idx]:byte(s.next_byte_idx)
|
|
|
|
|
if ret == ('\n'):byte() then
|
|
|
|
|
ret = 0 -- See :h NL-used-for-NUL.
|
|
|
|
|
end
|
|
|
|
|
s.next_byte_idx = s.next_byte_idx + 1
|
|
|
|
|
return ret
|
|
|
|
|
end
|
|
|
|
|
return next, init_s, nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function charblob_encode(lines, textwidth, indent)
|
|
|
|
|
local ret = {
|
|
|
|
|
'const unsigned char blob[] = {',
|
|
|
|
|
indent,
|
|
|
|
|
}
|
|
|
|
|
for byte in charblob_bytes_iter(lines) do
|
|
|
|
|
-- .- space + number (width 3) + comma
|
|
|
|
|
if #(ret[#ret]) + 5 > textwidth then
|
|
|
|
|
ret[#ret + 1] = indent
|
|
|
|
|
else
|
|
|
|
|
ret[#ret] = ret[#ret] .. ' '
|
|
|
|
|
end
|
|
|
|
|
ret[#ret] = ret[#ret] .. (('%3u,'):format(byte))
|
|
|
|
|
end
|
|
|
|
|
ret[#ret + 1] = '};'
|
|
|
|
|
return ret
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
bytes_iter = charblob_bytes_iter,
|
|
|
|
|
encode = charblob_encode,
|
|
|
|
|
}
|
2017-05-23 12:49:08 -07:00
|
|
|
|
|
2017-05-22 14:46:57 -07:00
|
|
|
|
==============================================================================
|
2017-10-22 07:29:36 -07:00
|
|
|
|
Commands *lua-commands*
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
|
|
|
|
*:lua*
|
|
|
|
|
:[range]lua {chunk}
|
2017-05-11 08:45:11 -07:00
|
|
|
|
Execute Lua chunk {chunk}.
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
>
|
2019-01-10 17:20:15 -07:00
|
|
|
|
:lua vim.api.nvim_command('echo "Hello, Nvim!"')
|
2017-01-28 19:09:54 -07:00
|
|
|
|
<
|
2017-08-19 05:13:14 -07:00
|
|
|
|
To see the Lua version: >
|
2019-01-10 17:20:15 -07:00
|
|
|
|
:lua print(_VERSION)
|
2017-08-19 05:13:14 -07:00
|
|
|
|
|
|
|
|
|
To see the LuaJIT version: >
|
2019-01-10 17:20:15 -07:00
|
|
|
|
:lua print(jit.version)
|
2017-08-19 05:13:14 -07:00
|
|
|
|
<
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
|
|
|
|
:[range]lua << {endmarker}
|
|
|
|
|
{script}
|
|
|
|
|
{endmarker}
|
2017-05-11 08:45:11 -07:00
|
|
|
|
Execute Lua script {script}.
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
|
|
|
|
{endmarker} must NOT be preceded by any white space. If {endmarker} is
|
|
|
|
|
omitted from after the "<<", a dot '.' must be used after {script}, like
|
|
|
|
|
for the |:append| and |:insert| commands.
|
|
|
|
|
This form of the |:lua| command is mainly useful for including Lua code
|
|
|
|
|
in Vim scripts.
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
>
|
2019-01-10 17:20:15 -07:00
|
|
|
|
function! CurrentLineInfo()
|
|
|
|
|
lua << EOF
|
|
|
|
|
local linenr = vim.api.nvim_win_get_cursor(0)[1]
|
|
|
|
|
local curline = vim.api.nvim_buf_get_lines(
|
|
|
|
|
0, linenr, linenr + 1, false)[1]
|
|
|
|
|
print(string.format("Current line [%d] has %d bytes",
|
|
|
|
|
linenr, #curline))
|
|
|
|
|
EOF
|
|
|
|
|
endfunction
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
2017-08-19 05:13:14 -07:00
|
|
|
|
Note that the `local` variables will disappear when block finishes. This is
|
|
|
|
|
not the case for globals.
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
|
|
|
|
*:luado*
|
|
|
|
|
:[range]luado {body} Execute Lua function "function (line, linenr) {body}
|
|
|
|
|
end" for each line in the [range], with the function
|
|
|
|
|
argument being set to the text of each line in turn,
|
|
|
|
|
without a trailing <EOL>, and the current line number.
|
|
|
|
|
If the value returned by the function is a string it
|
|
|
|
|
becomes the text of the line in the current turn. The
|
|
|
|
|
default for [range] is the whole file: "1,$".
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
>
|
2019-01-10 17:20:15 -07:00
|
|
|
|
:luado return string.format("%s\t%d", line:reverse(), #line)
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
: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
|
2017-01-28 19:09:54 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
*:luafile*
|
|
|
|
|
:[range]luafile {file}
|
2017-05-11 08:45:11 -07:00
|
|
|
|
Execute Lua script in {file}.
|
2017-01-28 19:09:54 -07:00
|
|
|
|
The whole argument is used as a single file name.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
>
|
2019-01-10 17:20:15 -07:00
|
|
|
|
:luafile script.lua
|
|
|
|
|
:luafile %
|
2017-01-28 19:09:54 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
All these commands execute a Lua chunk from either the command line (:lua and
|
|
|
|
|
:luado) or a file (:luafile) with the given line [range]. Similarly to the Lua
|
|
|
|
|
interpreter, each chunk has its own scope and so only global variables are
|
|
|
|
|
shared between command calls. All Lua default libraries are available. In
|
2017-10-22 07:29:36 -07:00
|
|
|
|
addition, Lua "print" function has its output redirected to the Nvim message
|
2017-01-28 19:09:54 -07:00
|
|
|
|
area, with arguments separated by a white space instead of a tab.
|
|
|
|
|
|
2018-11-04 18:47:22 -07:00
|
|
|
|
Lua uses the "vim" module (see |lua-vim|) to issue commands to Nvim. However,
|
2017-01-28 19:09:54 -07:00
|
|
|
|
procedures that alter buffer content, open new buffers, and change cursor
|
|
|
|
|
position are restricted when the command is executed in the |sandbox|.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
==============================================================================
|
2019-01-10 17:20:15 -07:00
|
|
|
|
vim.* *lua-vim* *lua-stdlib*
|
|
|
|
|
|
2019-02-04 05:21:35 -07:00
|
|
|
|
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.
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
You can peek at the module properties: >
|
|
|
|
|
|
|
|
|
|
:lua print(vim.inspect(vim))
|
|
|
|
|
|
|
|
|
|
Result is something like this: >
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
_os_proc_children = <function 1>,
|
|
|
|
|
_os_proc_info = <function 2>,
|
|
|
|
|
...
|
|
|
|
|
api = {
|
|
|
|
|
nvim__id = <function 5>,
|
|
|
|
|
nvim__id_array = <function 6>,
|
|
|
|
|
...
|
|
|
|
|
},
|
|
|
|
|
deepcopy = <function 106>,
|
|
|
|
|
gsplit = <function 107>,
|
|
|
|
|
...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
To find documentation on e.g. the "deepcopy" function: >
|
|
|
|
|
|
|
|
|
|
:help vim.deepcopy
|
|
|
|
|
|
2019-02-04 05:21:35 -07:00
|
|
|
|
Note that underscore-prefixed functions (e.g. "_os_proc_children") are
|
2019-01-10 17:20:15 -07:00
|
|
|
|
internal/private and must not be used by plugins.
|
2017-10-22 07:29:36 -07:00
|
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
vim.api.* functions
|
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
`vim.api` exposes the full Nvim |API| as a table of Lua functions.
|
2017-10-22 07:29:36 -07:00
|
|
|
|
|
|
|
|
|
For example, to use the "nvim_get_current_line()" API function, call
|
|
|
|
|
"vim.api.nvim_get_current_line()": >
|
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
print(tostring(vim.api.nvim_get_current_line()))
|
2017-10-22 07:29:36 -07:00
|
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
2019-01-07 07:42:20 -07:00
|
|
|
|
vim.* builtin functions
|
|
|
|
|
|
|
|
|
|
vim.deepcopy({object}) *vim.deepcopy*
|
|
|
|
|
Performs a deep copy of the given object, and returns that copy.
|
|
|
|
|
For a non-table object, that just means a usual copy of the object,
|
|
|
|
|
while for a table all subtables are copied recursively.
|
|
|
|
|
|
|
|
|
|
vim.gsplit({s}, {sep}, {plain}) *vim.gsplit*
|
|
|
|
|
Split a given string by a separator. Returns an iterator of the
|
|
|
|
|
split components. The separator can be a lua pattern, see
|
|
|
|
|
https://www.lua.org/pil/20.2.html
|
|
|
|
|
Setting {plain} to `true` turns off pattern matching, as it is passed
|
|
|
|
|
to `string:find`, see
|
|
|
|
|
http://lua-users.org/wiki/StringLibraryTutorial
|
|
|
|
|
|
|
|
|
|
Parameters:~
|
|
|
|
|
{s} String: String to split
|
|
|
|
|
{sep} String: Separator pattern. If empty, split by chars.
|
|
|
|
|
{plain} Boolean: If false, match {sep} verbatim
|
|
|
|
|
|
|
|
|
|
Return:~
|
|
|
|
|
Iterator of strings, which are the components of {s} after
|
|
|
|
|
splitting
|
|
|
|
|
|
|
|
|
|
vim.split({s}, {sep}, {plain}) *vim.split*
|
|
|
|
|
Split a given string by a separator. Returns a table containing the
|
|
|
|
|
split components. The separator can be a lua pattern, see
|
|
|
|
|
https://www.lua.org/pil/20.2.html
|
|
|
|
|
Setting {plain} to `true` turns off pattern matching, as it is passed
|
|
|
|
|
to `string:find`, see
|
|
|
|
|
http://lua-users.org/wiki/StringLibraryTutorial
|
|
|
|
|
|
|
|
|
|
Parameters:~
|
|
|
|
|
{s} String: String to split
|
|
|
|
|
{sep} String: Separator pattern. If empty, split by chars.
|
|
|
|
|
{plain} Boolean: If false, match {sep} verbatim
|
|
|
|
|
|
|
|
|
|
Return:~
|
|
|
|
|
Table of strings, which are the components of {s} after
|
|
|
|
|
splitting
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
2017-08-15 06:32:06 -07:00
|
|
|
|
vim.stricmp(a, b) *lua-vim.stricmp*
|
|
|
|
|
Function used for case-insensitive string comparison. Takes two
|
|
|
|
|
string arguments and returns 0, 1 or -1 if strings are equal, a is
|
|
|
|
|
greater then b or a is lesser then b respectively.
|
|
|
|
|
|
2019-01-07 07:42:20 -07:00
|
|
|
|
vim.trim({string}) *vim.trim*
|
|
|
|
|
Returns the string with all leading and trailing whitespace removed.
|
|
|
|
|
|
2017-08-15 14:44:52 -07:00
|
|
|
|
vim.type_idx *lua-vim.type_idx*
|
2018-11-04 18:47:22 -07:00
|
|
|
|
Type index for use in |lua-special-tbl|. Specifying one of the
|
2017-08-15 14:44:52 -07:00
|
|
|
|
values from |lua-vim.types| allows typing the empty table (it is
|
|
|
|
|
unclear whether empty lua table represents empty list or empty array)
|
|
|
|
|
and forcing integral numbers to be |Float|. See |lua-special-tbl| for
|
|
|
|
|
more details.
|
|
|
|
|
|
|
|
|
|
vim.val_idx *lua-vim.val_idx*
|
|
|
|
|
Value index for tables representing |Float|s. A table representing
|
|
|
|
|
floating-point value 1.0 looks like this: >
|
2019-01-10 17:20:15 -07:00
|
|
|
|
{
|
|
|
|
|
[vim.type_idx] = vim.types.float,
|
|
|
|
|
[vim.val_idx] = 1.0,
|
|
|
|
|
}
|
2017-08-15 14:44:52 -07:00
|
|
|
|
< See also |lua-vim.type_idx| and |lua-special-tbl|.
|
|
|
|
|
|
|
|
|
|
vim.types *lua-vim.types*
|
|
|
|
|
Table with possible values for |lua-vim.type_idx|. Contains two sets
|
|
|
|
|
of key-value pairs: first maps possible values for |lua-vim.type_idx|
|
|
|
|
|
to human-readable strings, second maps human-readable type names to
|
|
|
|
|
values for |lua-vim.type_idx|. Currently contains pairs for `float`,
|
|
|
|
|
`array` and `dictionary` types.
|
|
|
|
|
|
|
|
|
|
Note: one must expect that values corresponding to `vim.types.float`,
|
|
|
|
|
`vim.types.array` and `vim.types.dictionary` fall under only two
|
|
|
|
|
following assumptions:
|
|
|
|
|
1. Value may serve both as a key and as a value in a table. Given the
|
|
|
|
|
properties of lua tables this basically means “value is not `nil`”.
|
|
|
|
|
2. For each value in `vim.types` table `vim.types[vim.types[value]]`
|
|
|
|
|
is the same as `value`.
|
|
|
|
|
No other restrictions are put on types, and it is not guaranteed that
|
|
|
|
|
values corresponding to `vim.types.float`, `vim.types.array` and
|
|
|
|
|
`vim.types.dictionary` will not change or that `vim.types` table will
|
|
|
|
|
only contain values for these three types.
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
2019-01-07 07:42:20 -07:00
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
vim.* runtime functions
|
|
|
|
|
|
|
|
|
|
Those functions are only available after the runtime files have been loaded.
|
|
|
|
|
In particular, they are not available when using `nvim -u NONE`.
|
|
|
|
|
|
|
|
|
|
vim.inspect({object}, {options}) *vim.inspect*
|
|
|
|
|
Return a human-readable representation of the passed object. See
|
|
|
|
|
https://github.com/kikito/inspect.lua
|
|
|
|
|
for details and possible options.
|
2019-01-10 17:20:15 -07:00
|
|
|
|
|
2017-01-28 19:09:54 -07:00
|
|
|
|
==============================================================================
|
2019-01-10 17:20:15 -07:00
|
|
|
|
luaeval() *lua-luaeval* *lua-eval*
|
2017-01-28 19:09:54 -07:00
|
|
|
|
*luaeval()*
|
|
|
|
|
|
2017-10-22 07:29:36 -07:00
|
|
|
|
The (dual) equivalent of "vim.eval" for passing Lua values to Nvim is
|
2017-01-29 10:02:46 -07:00
|
|
|
|
"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:
|
2017-01-28 19:09:54 -07:00
|
|
|
|
>
|
2019-01-10 17:20:15 -07:00
|
|
|
|
local chunkheader = "local _A = select(1, ...) return "
|
|
|
|
|
function luaeval (expstr, arg)
|
|
|
|
|
local chunk = assert(loadstring(chunkheader .. expstr, "luaeval"))
|
|
|
|
|
return chunk(arg) -- return typval
|
|
|
|
|
end
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
2018-06-29 10:27:50 -07:00
|
|
|
|
Lua nils, numbers, strings, tables and booleans are converted to their
|
|
|
|
|
respective VimL types. An error is thrown if conversion of any other Lua types
|
|
|
|
|
is attempted.
|
|
|
|
|
|
|
|
|
|
The magic global "_A" contains the second argument to luaeval().
|
|
|
|
|
|
|
|
|
|
Example: >
|
2019-01-10 17:20:15 -07:00
|
|
|
|
:echo luaeval('_A[1] + _A[2]', [40, 2])
|
|
|
|
|
42
|
|
|
|
|
:echo luaeval('string.match(_A, "[a-z]+")', 'XYXfoo123')
|
|
|
|
|
foo
|
2018-06-29 10:27:50 -07:00
|
|
|
|
|
|
|
|
|
Lua tables are used as both dictionaries and lists, so it is impossible to
|
|
|
|
|
determine whether empty table is meant to be empty list or empty dictionary.
|
|
|
|
|
Additionally lua does not have integer numbers. To distinguish between these
|
|
|
|
|
cases there is the following agreement:
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
|
|
|
|
0. Empty table is empty list.
|
|
|
|
|
1. Table with N incrementally growing integral numbers, starting from 1 and
|
|
|
|
|
ending with N is considered to be a list.
|
|
|
|
|
2. Table with string keys, none of which contains NUL byte, is considered to
|
|
|
|
|
be a dictionary.
|
|
|
|
|
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|.
|
2017-08-15 14:44:52 -07:00
|
|
|
|
*lua-special-tbl*
|
2017-01-28 19:09:54 -07:00
|
|
|
|
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.list}` is converted to an empty list. As well
|
|
|
|
|
as `{[vim.type_idx]=vim.types.list, [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: >
|
|
|
|
|
|
2019-01-10 17:20:15 -07:00
|
|
|
|
: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)
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
|
|
|
|
Note that currently second argument to `luaeval` undergoes VimL to lua
|
2017-01-29 10:02:46 -07:00
|
|
|
|
conversion, so changing containers in lua do not affect values in VimL. Return
|
|
|
|
|
value is also always converted. When converting, |msgpack-special-dict|s are
|
|
|
|
|
treated specially.
|
2017-01-28 19:09:54 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2019-01-10 17:20:15 -07:00
|
|
|
|
vim:tw=78:ts=8:et:ft=help:norl:
|