2019-11-17 20:06:59 -07:00
|
|
|
|
*lua.txt* Nvim
|
|
|
|
|
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
NVIM REFERENCE MANUAL
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
Lua engine *lua* *Lua*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
Type |gO| to see the table of contents.
|
|
|
|
|
|
|
|
|
|
==============================================================================
|
2021-09-14 10:20:33 -07:00
|
|
|
|
INTRODUCTION *lua-intro*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
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
|
2021-04-09 10:36:23 -07:00
|
|
|
|
be used from Lua code. A good overview of using Lua in neovim is given by
|
|
|
|
|
https://github.com/nanotee/nvim-lua-guide.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
Module conflicts are resolved by "last wins". For example if both of these
|
|
|
|
|
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
|
2021-09-14 10:20:33 -07:00
|
|
|
|
finds and loads Lua modules. The conventions are similar to those of
|
|
|
|
|
Vimscript |plugin|s, with some extra features. See |lua-require-example| for
|
|
|
|
|
a walkthrough.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2021-09-14 10:20:33 -07:00
|
|
|
|
IMPORTING LUA MODULES *lua-require*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
*lua-package-path*
|
|
|
|
|
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`).
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
1. Given that
|
|
|
|
|
- 'runtimepath' contains `/foo/bar,/xxx;yyy/baz,/abc`;
|
|
|
|
|
- initial (defined at compile-time or derived from
|
|
|
|
|
`$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
|
|
|
|
|
|
|
|
|
|
- `/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`
|
|
|
|
|
|
|
|
|
|
Note:
|
|
|
|
|
|
|
|
|
|
- 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.
|
|
|
|
|
|
|
|
|
|
- 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.
|
|
|
|
|
|
2020-07-08 07:42:34 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
Lua Syntax Information *lua-syntax-help*
|
|
|
|
|
|
|
|
|
|
While Lua has a simple syntax, there are a few things to understand,
|
|
|
|
|
particularly when looking at the documentation above.
|
|
|
|
|
|
|
|
|
|
*lua-syntax-call-function*
|
|
|
|
|
|
|
|
|
|
Lua functions can be called in multiple ways. Consider the function: >
|
|
|
|
|
|
|
|
|
|
local example_func = function(a, b)
|
|
|
|
|
print("A is: ", a)
|
|
|
|
|
print("B is: ", b)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The first way to call a function is: >
|
|
|
|
|
|
|
|
|
|
example_func(1, 2)
|
|
|
|
|
-- ==== Result ====
|
|
|
|
|
-- A is: 1
|
|
|
|
|
-- B is: 2
|
|
|
|
|
<
|
|
|
|
|
This way of calling a function is familiar to most scripting languages.
|
|
|
|
|
In Lua, it's important to understand that any function arguments that are
|
|
|
|
|
not supplied are automatically set to `nil`. For example: >
|
|
|
|
|
|
|
|
|
|
example_func(1)
|
|
|
|
|
-- ==== Result ====
|
|
|
|
|
-- A is: 1
|
|
|
|
|
-- B is: nil
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Additionally, if any extra parameters are passed, they are discarded
|
|
|
|
|
completely.
|
|
|
|
|
|
|
|
|
|
In Lua, it is also possible (when only one argument is passed) to call the
|
|
|
|
|
function without any parentheses. This is most often used to approximate
|
|
|
|
|
"keyword"-style arguments with a single dictionary. For example: >
|
|
|
|
|
|
|
|
|
|
local func_with_opts = function(opts)
|
|
|
|
|
local will_do_foo = opts.foo
|
|
|
|
|
local filename = opts.filename
|
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
func_with_opts { foo = true, filename = "hello.world" }
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
In this style, each "parameter" is passed via keyword. It is still valid
|
|
|
|
|
to call the function in this style: >
|
|
|
|
|
|
|
|
|
|
func_with_opts({ foo = true, filename = "hello.world" })
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
But often in the documentation, you will see the former rather than the
|
|
|
|
|
latter style, due to its brevity (this is vim after all!).
|
|
|
|
|
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
------------------------------------------------------------------------------
|
2021-09-14 10:20:33 -07:00
|
|
|
|
LUA PLUGIN EXAMPLE *lua-require-example*
|
2019-11-17 20:06:59 -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: >
|
|
|
|
|
|
|
|
|
|
function charblob#encode_buffer()
|
|
|
|
|
call setline(1, luaeval(
|
|
|
|
|
\ 'require("charblob").encode(unpack(_A))',
|
|
|
|
|
\ [getline(1, '$'), &textwidth, ' ']))
|
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
plugin/charblob.vim: >
|
|
|
|
|
|
|
|
|
|
if exists('g:charblob_loaded')
|
|
|
|
|
finish
|
|
|
|
|
endif
|
|
|
|
|
let g:charblob_loaded = 1
|
|
|
|
|
|
|
|
|
|
command MakeCharBlob :call charblob#encode_buffer()
|
|
|
|
|
|
|
|
|
|
lua/charblob.lua: >
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
==============================================================================
|
2021-09-14 10:20:33 -07:00
|
|
|
|
COMMANDS *lua-commands*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
These commands execute a Lua chunk from either the command line (:lua, :luado)
|
|
|
|
|
or a file (:luafile) on the given line [range]. As always in Lua, each chunk
|
|
|
|
|
has its own scope (closure), so only global variables are shared between
|
|
|
|
|
command calls. The |lua-stdlib| modules, user modules, and anything else on
|
|
|
|
|
|lua-package-path| are available.
|
|
|
|
|
|
|
|
|
|
The Lua print() function redirects its output to the Nvim message area, with
|
|
|
|
|
arguments separated by " " (space) instead of "\t" (tab).
|
|
|
|
|
|
|
|
|
|
*:lua*
|
|
|
|
|
:[range]lua {chunk}
|
|
|
|
|
Executes Lua chunk {chunk}.
|
|
|
|
|
|
|
|
|
|
Examples: >
|
|
|
|
|
:lua vim.api.nvim_command('echo "Hello, Nvim!"')
|
|
|
|
|
< To see the Lua version: >
|
|
|
|
|
:lua print(_VERSION)
|
|
|
|
|
< To see the LuaJIT version: >
|
|
|
|
|
:lua print(jit.version)
|
|
|
|
|
<
|
|
|
|
|
*:lua-heredoc*
|
|
|
|
|
:[range]lua << [endmarker]
|
|
|
|
|
{script}
|
|
|
|
|
{endmarker}
|
|
|
|
|
Executes Lua script {script} from within Vimscript.
|
|
|
|
|
{endmarker} must NOT be preceded by whitespace. You
|
|
|
|
|
can omit [endmarker] after the "<<" and use a dot "."
|
|
|
|
|
after {script} (similar to |:append|, |:insert|).
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
>
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
< 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:
|
|
|
|
|
>
|
|
|
|
|
: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
|
|
|
|
|
<
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
*:luafile*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
:[range]luafile {file}
|
2021-09-14 10:20:33 -07:00
|
|
|
|
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.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
Examples: >
|
2019-11-17 20:06:59 -07:00
|
|
|
|
:luafile script.lua
|
|
|
|
|
:luafile %
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
==============================================================================
|
2021-09-14 10:20:33 -07:00
|
|
|
|
luaeval() *lua-eval* *luaeval()*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
>
|
|
|
|
|
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
|
2021-07-31 13:45:58 -07:00
|
|
|
|
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.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
The magic global "_A" contains the second argument to luaeval().
|
|
|
|
|
|
|
|
|
|
Example: >
|
|
|
|
|
:echo luaeval('_A[1] + _A[2]', [40, 2])
|
|
|
|
|
42
|
|
|
|
|
:echo luaeval('string.match(_A, "[a-z]+")', 'XYXfoo123')
|
|
|
|
|
foo
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
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|.
|
2021-09-14 10:20:33 -07:00
|
|
|
|
*lua-special-tbl*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
4. Table with `vim.type_idx` key may be a dictionary, a list or floating-point
|
|
|
|
|
value:
|
2021-09-14 10:20:33 -07:00
|
|
|
|
- `{[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
|
2019-11-17 20:06:59 -07:00
|
|
|
|
variant allows integral |Float|s.
|
2021-09-14 10:20:33 -07:00
|
|
|
|
- `{[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.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
are errors.
|
2021-09-14 10:20:33 -07:00
|
|
|
|
- `{[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
|
2019-11-17 20:06:59 -07:00
|
|
|
|
non-integral keys.
|
|
|
|
|
|
|
|
|
|
Examples: >
|
|
|
|
|
|
|
|
|
|
: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)
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
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.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2021-09-14 10:20:33 -07:00
|
|
|
|
Vimscript v:lua interface *v:lua-call*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
From Vimscript the special `v:lua` prefix can be used to call Lua functions
|
|
|
|
|
which are global or accessible from global tables. The expression >
|
|
|
|
|
v:lua.func(arg1, arg2)
|
|
|
|
|
is equivalent to the Lua chunk >
|
|
|
|
|
return func(...)
|
|
|
|
|
where the args are converted to Lua values. The expression >
|
|
|
|
|
v:lua.somemod.func(args)
|
|
|
|
|
is equivalent to the Lua chunk >
|
|
|
|
|
return somemod.func(...)
|
|
|
|
|
|
2021-10-23 09:20:19 -07:00
|
|
|
|
In addition, functions of packages can be accessed like >
|
|
|
|
|
v:lua.require'mypack'.func(arg1, arg2)
|
|
|
|
|
v:lua.require'mypack.submod'.func(arg1, arg2)
|
|
|
|
|
Note: only single quote form without parens is allowed. Using
|
|
|
|
|
`require"mypack"` or `require('mypack')` as prefixes do NOT work (the latter
|
|
|
|
|
is still valid as a function call of itself, in case require returns a useful
|
|
|
|
|
value).
|
|
|
|
|
|
2021-08-11 05:47:33 -07:00
|
|
|
|
The `v:lua` prefix may be used to call Lua functions as |method|s. For
|
|
|
|
|
example: >
|
|
|
|
|
arg1->v:lua.somemod.func(arg2)
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
You can use `v:lua` in "func" options like 'tagfunc', 'omnifunc', etc.
|
|
|
|
|
For example consider the following Lua omnifunc handler: >
|
|
|
|
|
|
|
|
|
|
function mymod.omnifunc(findstart, base)
|
|
|
|
|
if findstart == 1 then
|
|
|
|
|
return 0
|
|
|
|
|
else
|
|
|
|
|
return {'stuff', 'steam', 'strange things'}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
vim.api.nvim_buf_set_option(0, 'omnifunc', 'v:lua.mymod.omnifunc')
|
|
|
|
|
|
2021-10-23 09:20:19 -07:00
|
|
|
|
Note: the module ("mymod" in the above example) must either be a Lua global,
|
|
|
|
|
or use the require syntax as specified above to access it from a package.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
Note: `v:lua` without a call is not allowed in a Vimscript expression:
|
|
|
|
|
|Funcref|s cannot represent Lua functions. The following are errors: >
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
==============================================================================
|
2021-09-14 10:20:33 -07:00
|
|
|
|
Lua standard modules *lua-stdlib*
|
2019-11-17 20:06:59 -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.
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
Note that underscore-prefixed functions (e.g. "_os_proc_children") are
|
|
|
|
|
internal/private and must not be used by plugins.
|
|
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
2021-09-14 10:20:33 -07:00
|
|
|
|
VIM.LOOP *lua-loop* *vim.loop*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
`vim.loop` exposes all features of the Nvim event-loop. This is a low-level
|
|
|
|
|
API that provides functionality for networking, filesystem, and process
|
|
|
|
|
management. Try this command to see available functions: >
|
|
|
|
|
|
|
|
|
|
:lua print(vim.inspect(vim.loop))
|
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
Reference: https://github.com/luvit/luv/blob/master/docs.md
|
2019-11-17 20:06:59 -07:00
|
|
|
|
Examples: https://github.com/luvit/luv/tree/master/examples
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
*E5560* *lua-loop-callbacks*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
It is an error to directly invoke `vim.api` functions (except |api-fast|) in
|
|
|
|
|
`vim.loop` callbacks. For example, this is an error: >
|
|
|
|
|
|
|
|
|
|
local timer = vim.loop.new_timer()
|
|
|
|
|
timer:start(1000, 0, function()
|
|
|
|
|
vim.api.nvim_command('echomsg "test"')
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
To avoid the error use |vim.schedule_wrap()| to defer the callback: >
|
|
|
|
|
|
|
|
|
|
local timer = vim.loop.new_timer()
|
|
|
|
|
timer:start(1000, 0, vim.schedule_wrap(function()
|
|
|
|
|
vim.api.nvim_command('echomsg "test"')
|
|
|
|
|
end))
|
|
|
|
|
|
2020-07-07 01:55:40 -07:00
|
|
|
|
(For one-shot timers, see |vim.defer_fn()|, which automatically adds the wrapping.)
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
Example: repeating timer
|
|
|
|
|
1. Save this code to a file.
|
|
|
|
|
2. Execute it with ":luafile %". >
|
|
|
|
|
|
|
|
|
|
-- Create a timer handle (implementation detail: uv_timer_t).
|
|
|
|
|
local timer = vim.loop.new_timer()
|
|
|
|
|
local i = 0
|
|
|
|
|
-- Waits 1000ms, then repeats every 750ms until timer:close().
|
|
|
|
|
timer:start(1000, 750, function()
|
|
|
|
|
print('timer invoked! i='..tostring(i))
|
|
|
|
|
if i > 4 then
|
|
|
|
|
timer:close() -- Always close handles to avoid leaks.
|
|
|
|
|
end
|
|
|
|
|
i = i + 1
|
|
|
|
|
end)
|
|
|
|
|
print('sleeping');
|
|
|
|
|
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
Example: File-change detection *watch-file*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
1. Save this code to a file.
|
|
|
|
|
2. Execute it with ":luafile %".
|
|
|
|
|
3. Use ":Watch %" to watch any file.
|
|
|
|
|
4. Try editing the file from another text editor.
|
|
|
|
|
5. Observe that the file reloads in Nvim (because on_change() calls
|
|
|
|
|
|:checktime|). >
|
|
|
|
|
|
|
|
|
|
local w = vim.loop.new_fs_event()
|
|
|
|
|
local function on_change(err, fname, status)
|
|
|
|
|
-- Do work...
|
|
|
|
|
vim.api.nvim_command('checktime')
|
|
|
|
|
-- Debounce: stop/start.
|
|
|
|
|
w:stop()
|
|
|
|
|
watch_file(fname)
|
|
|
|
|
end
|
|
|
|
|
function watch_file(fname)
|
|
|
|
|
local fullpath = vim.api.nvim_call_function(
|
|
|
|
|
'fnamemodify', {fname, ':p'})
|
|
|
|
|
w:start(fullpath, {}, vim.schedule_wrap(function(...)
|
|
|
|
|
on_change(...) end))
|
|
|
|
|
end
|
|
|
|
|
vim.api.nvim_command(
|
|
|
|
|
"command! -nargs=1 Watch call luaeval('watch_file(_A)', expand('<args>'))")
|
|
|
|
|
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
Example: TCP echo-server *tcp-server*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
1. Save this code to a file.
|
|
|
|
|
2. Execute it with ":luafile %".
|
|
|
|
|
3. Note the port number.
|
|
|
|
|
4. Connect from any TCP client (e.g. "nc 0.0.0.0 36795"): >
|
|
|
|
|
|
|
|
|
|
local function create_server(host, port, on_connect)
|
|
|
|
|
local server = vim.loop.new_tcp()
|
|
|
|
|
server:bind(host, port)
|
|
|
|
|
server:listen(128, function(err)
|
|
|
|
|
assert(not err, err) -- Check for errors.
|
|
|
|
|
local sock = vim.loop.new_tcp()
|
|
|
|
|
server:accept(sock) -- Accept client connection.
|
|
|
|
|
on_connect(sock) -- Start reading messages.
|
|
|
|
|
end)
|
|
|
|
|
return server
|
|
|
|
|
end
|
|
|
|
|
local server = create_server('0.0.0.0', 0, function(sock)
|
|
|
|
|
sock:read_start(function(err, chunk)
|
|
|
|
|
assert(not err, err) -- Check for errors.
|
|
|
|
|
if chunk then
|
|
|
|
|
sock:write(chunk) -- Echo received messages to the channel.
|
|
|
|
|
else -- EOF (stream closed).
|
|
|
|
|
sock:close() -- Always close handles to avoid leaks.
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
end)
|
|
|
|
|
print('TCP echo-server listening on port: '..server:getsockname().port)
|
|
|
|
|
|
2020-05-18 06:49:50 -07:00
|
|
|
|
------------------------------------------------------------------------------
|
2021-09-14 10:20:33 -07:00
|
|
|
|
VIM.HIGHLIGHT *lua-highlight*
|
2020-05-18 06:49:50 -07:00
|
|
|
|
|
|
|
|
|
Nvim includes a function for highlighting a selection on yank (see for example
|
|
|
|
|
https://github.com/machakann/vim-highlightedyank). To enable it, add
|
|
|
|
|
>
|
2020-07-05 18:30:12 -07:00
|
|
|
|
au TextYankPost * silent! lua vim.highlight.on_yank()
|
2020-05-18 06:49:50 -07:00
|
|
|
|
<
|
|
|
|
|
to your `init.vim`. You can customize the highlight group and the duration of
|
|
|
|
|
the highlight via
|
|
|
|
|
>
|
2020-07-05 18:30:12 -07:00
|
|
|
|
au TextYankPost * silent! lua vim.highlight.on_yank {higroup="IncSearch", timeout=150}
|
2020-05-18 06:49:50 -07:00
|
|
|
|
<
|
2020-06-03 07:51:25 -07:00
|
|
|
|
If you want to exclude visual selections from highlighting on yank, use
|
|
|
|
|
>
|
2020-07-05 18:30:12 -07:00
|
|
|
|
au TextYankPost * silent! lua vim.highlight.on_yank {on_visual=false}
|
2020-06-03 07:51:25 -07:00
|
|
|
|
<
|
2020-05-18 06:49:50 -07:00
|
|
|
|
|
2020-07-05 18:30:12 -07:00
|
|
|
|
vim.highlight.on_yank({opts}) *vim.highlight.on_yank()*
|
|
|
|
|
Highlights the yanked text. The fields of the optional dict {opts}
|
|
|
|
|
control the highlight:
|
2021-08-27 03:59:13 -07:00
|
|
|
|
- {higroup} highlight group for yanked region (default |hl-IncSearch|)
|
2020-07-05 18:30:12 -07:00
|
|
|
|
- {timeout} time in ms before highlight is cleared (default `150`)
|
|
|
|
|
- {on_macro} highlight when executing macro (default `false`)
|
|
|
|
|
- {on_visual} highlight when yanking visual selection (default `true`)
|
2021-08-27 03:59:13 -07:00
|
|
|
|
- {event} event structure (default |v:event|)
|
2020-05-31 11:56:00 -07:00
|
|
|
|
|
|
|
|
|
vim.highlight.range({bufnr}, {ns}, {higroup}, {start}, {finish}, {rtype}, {inclusive})
|
|
|
|
|
*vim.highlight.range()*
|
|
|
|
|
Highlights the range between {start} and {finish} (tuples of {line,col})
|
|
|
|
|
in buffer {bufnr} with the highlight group {higroup} using the namespace
|
|
|
|
|
{ns}. Optional arguments are the type of range (characterwise, linewise,
|
|
|
|
|
or blockwise, see |setreg|; default to characterwise) and whether the
|
|
|
|
|
range is inclusive (default false).
|
|
|
|
|
|
2019-11-04 12:40:30 -07:00
|
|
|
|
------------------------------------------------------------------------------
|
2021-09-14 10:20:33 -07:00
|
|
|
|
VIM.REGEX *lua-regex*
|
2019-11-04 12:40:30 -07:00
|
|
|
|
|
|
|
|
|
Vim regexes can be used directly from lua. Currently they only allow
|
|
|
|
|
matching within a single line.
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.regex({re}) *vim.regex()*
|
|
|
|
|
Parse the Vim regex {re} and return a regex object. Regexes are
|
|
|
|
|
"magic" and case-insensitive by default, regardless of 'magic' and
|
|
|
|
|
'ignorecase'. The can be controlled with flags, see |/magic|.
|
2019-11-04 12:40:30 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
Methods on the regex object:
|
2019-11-04 12:40:30 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
regex:match_str({str}) *regex:match_str()*
|
2019-11-04 12:40:30 -07:00
|
|
|
|
Match the string against the regex. If the string should match the
|
|
|
|
|
regex precisely, surround the regex with `^` and `$`.
|
|
|
|
|
If the was a match, the byte indices for the beginning and end of
|
|
|
|
|
the match is returned. When there is no match, `nil` is returned.
|
|
|
|
|
As any integer is truth-y, `regex:match()` can be directly used
|
|
|
|
|
as a condition in an if-statement.
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
regex:match_line({bufnr}, {line_idx}[, {start}, {end}]) *regex:match_line()*
|
2019-11-04 12:40:30 -07:00
|
|
|
|
Match line {line_idx} (zero-based) in buffer {bufnr}. If {start} and
|
|
|
|
|
{end} are supplied, match only this byte index range. Otherwise see
|
|
|
|
|
|regex:match_str()|. If {start} is used, then the returned byte
|
|
|
|
|
indices will be relative {start}.
|
|
|
|
|
|
2021-08-22 04:52:56 -07:00
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
VIM.DIFF *lua-diff*
|
|
|
|
|
|
|
|
|
|
vim.diff({a}, {b}, {opts}) *vim.diff()*
|
|
|
|
|
Run diff on strings {a} and {b}. Any indices returned by this
|
|
|
|
|
function, either directly or via callback arguments, are
|
|
|
|
|
1-based.
|
|
|
|
|
|
|
|
|
|
Examples: >
|
|
|
|
|
vim.diff('a\n', 'b\nc\n')
|
|
|
|
|
-->
|
|
|
|
|
@@ -1 +1,2 @@
|
|
|
|
|
-a
|
|
|
|
|
+b
|
|
|
|
|
+c
|
|
|
|
|
|
2021-08-23 04:47:54 -07:00
|
|
|
|
vim.diff('a\n', 'b\nc\n', {result_type = 'indices'})
|
2021-08-22 04:52:56 -07:00
|
|
|
|
-->
|
|
|
|
|
{
|
|
|
|
|
{1, 1, 1, 2}
|
|
|
|
|
}
|
|
|
|
|
<
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{a} First string to compare
|
|
|
|
|
{b} Second string to compare
|
|
|
|
|
{opts} Optional parameters:
|
|
|
|
|
• `on_hunk` (callback):
|
|
|
|
|
Invoked for each hunk in the diff. Return a
|
|
|
|
|
negative number to cancel the callback for any
|
|
|
|
|
remaining hunks.
|
|
|
|
|
Args:
|
|
|
|
|
• `start_a` (integer): Start line of hunk in {a}.
|
|
|
|
|
• `count_a` (integer): Hunk size in {a}.
|
|
|
|
|
• `start_b` (integer): Start line of hunk in {b}.
|
|
|
|
|
• `count_b` (integer): Hunk size in {b}.
|
|
|
|
|
• `result_type` (string): Form of the returned diff:
|
|
|
|
|
• "unified": (default) String in unified format.
|
|
|
|
|
• "indices": Array of hunk locations.
|
|
|
|
|
Note this option is ignored if `on_hunk` is
|
|
|
|
|
used.
|
|
|
|
|
• `algorithm` (string):
|
|
|
|
|
Diff algorithm to use. Values:
|
|
|
|
|
• "myers" the default algorithm
|
|
|
|
|
• "minimal" spend extra time to generate the
|
|
|
|
|
smallest possible diff
|
|
|
|
|
• "patience" patience diff algorithm
|
|
|
|
|
• "histogram" histogram diff algorithm
|
|
|
|
|
• `ctxlen` (integer): Context length
|
|
|
|
|
• `interhunkctxlen` (integer):
|
|
|
|
|
Inter hunk context length
|
|
|
|
|
• `ignore_whitespace` (boolean):
|
|
|
|
|
Ignore whitespace
|
|
|
|
|
• `ignore_whitespace_change` (boolean):
|
|
|
|
|
Ignore whitespace change
|
|
|
|
|
• `ignore_whitespace_change_at_eol` (boolean)
|
|
|
|
|
Ignore whitespace change at end-of-line.
|
|
|
|
|
• `ignore_cr_at_eol` (boolean)
|
|
|
|
|
Ignore carriage return at end-of-line
|
|
|
|
|
• `ignore_blank_lines` (boolean)
|
|
|
|
|
Ignore blank lines
|
|
|
|
|
• `indent_heuristic` (boolean):
|
|
|
|
|
Use the indent heuristic for the internal
|
|
|
|
|
diff library.
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
See {opts.result_type}. nil if {opts.on_hunk} is given.
|
|
|
|
|
|
2021-09-04 08:39:22 -07:00
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
VIM.MPACK *lua-mpack*
|
|
|
|
|
|
2021-10-30 06:59:59 -07:00
|
|
|
|
The *vim.mpack* module provides encoding and decoding of Lua objects to and
|
|
|
|
|
from msgpack-encoded strings. Supports |vim.NIL| and |vim.empty_dict()|.
|
2021-09-04 08:39:22 -07:00
|
|
|
|
|
2021-10-30 06:59:59 -07:00
|
|
|
|
vim.mpack.encode({obj}) *vim.mpack.encode*
|
|
|
|
|
Encodes (or "packs") Lua object {obj} as msgpack in a Lua string.
|
2021-09-04 08:39:22 -07:00
|
|
|
|
|
2021-10-30 06:59:59 -07:00
|
|
|
|
vim.mpack.decode({str}) *vim.mpack.decode*
|
|
|
|
|
Decodes (or "unpacks") the msgpack-encoded {str} to a Lua object.
|
2021-09-04 08:39:22 -07:00
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
------------------------------------------------------------------------------
|
2021-09-14 10:20:33 -07:00
|
|
|
|
VIM *lua-builtin*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.api.{func}({...}) *vim.api*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
Invokes Nvim |API| function {func} with arguments {...}.
|
|
|
|
|
Example: call the "nvim_get_current_line()" API function: >
|
|
|
|
|
print(tostring(vim.api.nvim_get_current_line()))
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.version() *vim.version*
|
|
|
|
|
Gets the version of the current Nvim build.
|
2021-01-18 01:37:18 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.in_fast_event() *vim.in_fast_event()*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
Returns true if the code is executing as part of a "fast" event
|
|
|
|
|
handler, where most of the API is disabled. These are low-level events
|
|
|
|
|
(e.g. |lua-loop-callbacks|) which can be invoked whenever Nvim polls
|
|
|
|
|
for input. When this is `false` most API functions are callable (but
|
|
|
|
|
may be subject to other restrictions such as |textlock|).
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.NIL *vim.NIL*
|
|
|
|
|
Special value representing NIL in |RPC| and |v:null| in Vimscript
|
|
|
|
|
conversion, and similar cases. Lua `nil` cannot be used as part of
|
|
|
|
|
a Lua table representing a Dictionary or Array, because it is
|
|
|
|
|
treated as missing: `{"foo", nil}` is the same as `{"foo"}`.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.empty_dict() *vim.empty_dict()*
|
|
|
|
|
Creates a special empty table (marked with a metatable), which Nvim
|
|
|
|
|
converts to an empty dictionary when translating Lua values to
|
|
|
|
|
Vimscript or API types. Nvim by default converts an empty table `{}`
|
|
|
|
|
without this metatable to an list/array.
|
2019-11-27 12:45:41 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
Note: if numeric keys are present in the table, Nvim ignores the
|
|
|
|
|
metatable marker and converts the dict to a list/array anyway.
|
2019-11-27 12:45:41 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.rpcnotify({channel}, {method}[, {args}...]) *vim.rpcnotify()*
|
|
|
|
|
Sends {event} to {channel} via |RPC| and returns immediately. If
|
|
|
|
|
{channel} is 0, the event is broadcast to all channels.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
This function also works in a fast callback |lua-loop-callbacks|.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.rpcrequest({channel}, {method}[, {args}...]) *vim.rpcrequest()*
|
|
|
|
|
Sends a request to {channel} to invoke {method} via |RPC| and blocks
|
|
|
|
|
until a response is received.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
Note: NIL values as part of the return value is represented as
|
|
|
|
|
|vim.NIL| special value
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.stricmp({a}, {b}) *vim.stricmp()*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
Compares strings case-insensitively. Returns 0, 1 or -1 if strings
|
|
|
|
|
are equal, {a} is greater than {b} or {a} is lesser than {b},
|
|
|
|
|
respectively.
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.str_utfindex({str}[, {index}]) *vim.str_utfindex()*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
Convert byte index to UTF-32 and UTF-16 indicies. If {index} is not
|
|
|
|
|
supplied, the length of the string is used. All indicies are zero-based.
|
|
|
|
|
Returns two values: the UTF-32 and UTF-16 indicies respectively.
|
|
|
|
|
|
|
|
|
|
Embedded NUL bytes are treated as terminating the string. Invalid
|
|
|
|
|
UTF-8 bytes, and embedded surrogates are counted as one code
|
|
|
|
|
point each. An {index} in the middle of a UTF-8 sequence is rounded
|
|
|
|
|
upwards to the end of that sequence.
|
|
|
|
|
|
|
|
|
|
vim.str_byteindex({str}, {index}[, {use_utf16}]) *vim.str_byteindex()*
|
|
|
|
|
Convert UTF-32 or UTF-16 {index} to byte index. If {use_utf16} is not
|
|
|
|
|
supplied, it defaults to false (use UTF-32). Returns the byte index.
|
|
|
|
|
|
|
|
|
|
Invalid UTF-8 and NUL is treated like by |vim.str_byteindex()|. An {index}
|
|
|
|
|
in the middle of a UTF-16 sequence is rounded upwards to the end of that
|
|
|
|
|
sequence.
|
|
|
|
|
|
|
|
|
|
vim.schedule({callback}) *vim.schedule()*
|
|
|
|
|
Schedules {callback} to be invoked soon by the main event-loop. Useful
|
|
|
|
|
to avoid |textlock| or other temporary restrictions.
|
|
|
|
|
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
|
|
|
|
vim.defer_fn({fn}, {timeout}) *vim.defer_fn*
|
|
|
|
|
Defers calling {fn} until {timeout} ms passes. Use to do a one-shot timer
|
|
|
|
|
that calls {fn}.
|
|
|
|
|
|
2020-07-07 01:55:40 -07:00
|
|
|
|
Note: The {fn} is |schedule_wrap|ped automatically, so API functions are
|
|
|
|
|
safe to call.
|
|
|
|
|
|
2020-05-20 08:08:19 -07:00
|
|
|
|
Parameters: ~
|
|
|
|
|
{fn} Callback to call once {timeout} expires
|
|
|
|
|
{timeout} Time in ms to wait before calling {fn}
|
|
|
|
|
|
|
|
|
|
Returns: ~
|
|
|
|
|
|vim.loop|.new_timer() object
|
|
|
|
|
|
2020-10-06 09:58:05 -07:00
|
|
|
|
vim.wait({time} [, {callback}, {interval}, {fast_only}]) *vim.wait()*
|
2020-05-20 08:08:19 -07:00
|
|
|
|
Wait for {time} in milliseconds until {callback} returns `true`.
|
|
|
|
|
|
|
|
|
|
Executes {callback} immediately and at approximately {interval}
|
|
|
|
|
milliseconds (default 200). Nvim still processes other events during
|
|
|
|
|
this time.
|
|
|
|
|
|
2020-10-06 09:58:05 -07:00
|
|
|
|
Parameters: ~
|
|
|
|
|
{time} Number of milliseconds to wait
|
|
|
|
|
{callback} Optional callback. Waits until {callback} returns true
|
|
|
|
|
{interval} (Approximate) number of milliseconds to wait between polls
|
|
|
|
|
{fast_only} If true, only |api-fast| events will be processed.
|
|
|
|
|
If called from while in an |api-fast| event, will
|
|
|
|
|
automatically be set to `true`.
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
|
|
|
|
Returns: ~
|
|
|
|
|
If {callback} returns `true` during the {time}:
|
|
|
|
|
`true, nil`
|
|
|
|
|
|
|
|
|
|
If {callback} never returns `true` during the {time}:
|
|
|
|
|
`false, -1`
|
|
|
|
|
|
|
|
|
|
If {callback} is interrupted during the {time}:
|
|
|
|
|
`false, -2`
|
|
|
|
|
|
|
|
|
|
If {callback} errors, the error is raised.
|
|
|
|
|
|
|
|
|
|
Examples: >
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
-- Wait for 100 ms, allowing other events to process
|
|
|
|
|
vim.wait(100, function() end)
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
-- Wait for 100 ms or until global variable set.
|
|
|
|
|
vim.wait(100, function() return vim.g.waiting_for_var end)
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
-- Wait for 1 second or until global variable set, checking every ~500 ms
|
|
|
|
|
vim.wait(1000, function() return vim.g.waiting_for_var end, 500)
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
-- Schedule a function to set a value in 100ms
|
|
|
|
|
vim.defer_fn(function() vim.g.timer_result = true end, 100)
|
|
|
|
|
|
|
|
|
|
-- Would wait ten seconds if results blocked. Actually only waits 100 ms
|
|
|
|
|
if vim.wait(10000, function() return vim.g.timer_result end) then
|
|
|
|
|
print('Only waiting a little bit of time!')
|
|
|
|
|
end
|
|
|
|
|
<
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.type_idx *vim.type_idx*
|
|
|
|
|
Type index for use in |lua-special-tbl|. Specifying one of the values
|
|
|
|
|
from |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.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.val_idx *vim.val_idx*
|
|
|
|
|
Value index for tables representing |Float|s. A table representing
|
|
|
|
|
floating-point value 1.0 looks like this: >
|
2019-11-17 20:06:59 -07:00
|
|
|
|
{
|
|
|
|
|
[vim.type_idx] = vim.types.float,
|
|
|
|
|
[vim.val_idx] = 1.0,
|
|
|
|
|
}
|
2021-09-14 10:20:33 -07:00
|
|
|
|
< See also |vim.type_idx| and |lua-special-tbl|.
|
|
|
|
|
|
|
|
|
|
vim.types *vim.types*
|
|
|
|
|
Table with possible values for |vim.type_idx|. Contains two sets of
|
|
|
|
|
key-value pairs: first maps possible values for |vim.type_idx| to
|
|
|
|
|
human-readable strings, second maps human-readable type names to
|
|
|
|
|
values for |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.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
LUA-VIMSCRIPT BRIDGE *lua-vimscript*
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
Nvim Lua provides an interface to Vimscript variables and functions, and
|
|
|
|
|
editor commands and options.
|
2021-04-09 10:36:23 -07:00
|
|
|
|
See also https://github.com/nanotee/nvim-lua-guide.
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
vim.call({func}, {...}) *vim.call()*
|
|
|
|
|
Invokes |vim-function| or |user-function| {func} with arguments {...}.
|
|
|
|
|
See also |vim.fn|.
|
|
|
|
|
Equivalent to: >
|
|
|
|
|
vim.fn[func]({...})
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
vim.cmd({cmd}) *vim.cmd()*
|
2021-05-01 04:09:05 -07:00
|
|
|
|
Executes multiple lines of Vimscript at once. It is an alias to
|
|
|
|
|
|nvim_exec()|, where `output` is set to false. Thus it works identical
|
|
|
|
|
to |:source|.
|
2020-08-31 00:51:35 -07:00
|
|
|
|
See also |ex-cmd-index|.
|
|
|
|
|
Example: >
|
|
|
|
|
vim.cmd('echo 42')
|
2021-05-01 04:09:05 -07:00
|
|
|
|
vim.cmd([[
|
|
|
|
|
augroup My_group
|
|
|
|
|
autocmd!
|
|
|
|
|
autocmd FileType c setlocal cindent
|
|
|
|
|
augroup END
|
|
|
|
|
]])
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
vim.fn.{func}({...}) *vim.fn*
|
|
|
|
|
Invokes |vim-function| or |user-function| {func} with arguments {...}.
|
|
|
|
|
To call autoload functions, use the syntax: >
|
|
|
|
|
vim.fn['some#function']({...})
|
2020-05-17 20:29:34 -07:00
|
|
|
|
<
|
2021-09-10 06:59:17 -07:00
|
|
|
|
Unlike vim.api.|nvim_call_function()| this converts directly between Vim
|
2020-08-31 00:51:35 -07:00
|
|
|
|
objects and Lua objects. If the Vim function returns a float, it will
|
|
|
|
|
be represented directly as a Lua number. Empty lists and dictionaries
|
|
|
|
|
both are represented by an empty table.
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
Note: |v:null| values as part of the return value is represented as
|
|
|
|
|
|vim.NIL| special value
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
Note: vim.fn keys are generated lazily, thus `pairs(vim.fn)` only
|
|
|
|
|
enumerates functions that were called at least once.
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
*lua-vim-variables*
|
|
|
|
|
The Vim editor global dictionaries |g:| |w:| |b:| |t:| |v:| can be accessed
|
|
|
|
|
from Lua conveniently and idiomatically by referencing the `vim.*` Lua tables
|
|
|
|
|
described below. In this way you can easily read and modify global Vimscript
|
|
|
|
|
variables from Lua.
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
Example: >
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
vim.g.foo = 5 -- Set the g:foo Vimscript variable.
|
|
|
|
|
print(vim.g.foo) -- Get and print the g:foo Vimscript variable.
|
|
|
|
|
vim.g.foo = nil -- Delete (:unlet) the Vimscript variable.
|
2021-09-23 07:00:25 -07:00
|
|
|
|
vim.b[2].foo = 6 -- Set b:foo for buffer 2
|
2020-08-31 00:51:35 -07:00
|
|
|
|
|
|
|
|
|
vim.g *vim.g*
|
|
|
|
|
Global (|g:|) editor variables.
|
|
|
|
|
Key with no value returns `nil`.
|
|
|
|
|
|
|
|
|
|
vim.b *vim.b*
|
|
|
|
|
Buffer-scoped (|b:|) variables for the current buffer.
|
2021-09-23 07:00:25 -07:00
|
|
|
|
Invalid or unset key returns `nil`. Can be indexed with
|
|
|
|
|
an integer to access variables for a specific buffer.
|
2020-08-31 00:51:35 -07:00
|
|
|
|
|
|
|
|
|
vim.w *vim.w*
|
|
|
|
|
Window-scoped (|w:|) variables for the current window.
|
2021-09-23 07:00:25 -07:00
|
|
|
|
Invalid or unset key returns `nil`. Can be indexed with
|
|
|
|
|
an integer to access variables for a specific window.
|
2020-08-31 00:51:35 -07:00
|
|
|
|
|
|
|
|
|
vim.t *vim.t*
|
|
|
|
|
Tabpage-scoped (|t:|) variables for the current tabpage.
|
2021-09-23 07:00:25 -07:00
|
|
|
|
Invalid or unset key returns `nil`. Can be indexed with
|
|
|
|
|
an integer to access variables for a specific tabpage.
|
2020-08-31 00:51:35 -07:00
|
|
|
|
|
|
|
|
|
vim.v *vim.v*
|
|
|
|
|
|v:| variables.
|
|
|
|
|
Invalid or unset key returns `nil`.
|
|
|
|
|
|
|
|
|
|
vim.env *vim.env*
|
|
|
|
|
Environment variables defined in the editor session.
|
|
|
|
|
See |expand-env| and |:let-environment| for the Vimscript behavior.
|
|
|
|
|
Invalid or unset key returns `nil`.
|
|
|
|
|
Example: >
|
|
|
|
|
vim.env.FOO = 'bar'
|
|
|
|
|
print(vim.env.TERM)
|
|
|
|
|
<
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2021-05-28 08:24:48 -07:00
|
|
|
|
*lua-vim-options*
|
|
|
|
|
*lua-vim-opt*
|
|
|
|
|
*lua-vim-set*
|
|
|
|
|
*lua-vim-optlocal*
|
|
|
|
|
*lua-vim-setlocal*
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
In Vimscript, there is an way to set options |set-option|. In Lua, the
|
|
|
|
|
corresponding method is `vim.opt`.
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
|
|
|
|
`vim.opt` provides several conveniences for setting and controlling options
|
|
|
|
|
from within Lua.
|
|
|
|
|
|
|
|
|
|
Examples: ~
|
|
|
|
|
|
|
|
|
|
To set a boolean toggle:
|
2021-09-14 10:20:33 -07:00
|
|
|
|
In Vimscript:
|
2021-05-28 08:24:48 -07:00
|
|
|
|
`set number`
|
|
|
|
|
|
|
|
|
|
In Lua:
|
|
|
|
|
`vim.opt.number = true`
|
|
|
|
|
|
|
|
|
|
To set an array of values:
|
2021-09-14 10:20:33 -07:00
|
|
|
|
In Vimscript:
|
2021-05-28 08:24:48 -07:00
|
|
|
|
`set wildignore=*.o,*.a,__pycache__`
|
|
|
|
|
|
|
|
|
|
In Lua, there are two ways you can do this now. One is very similar to
|
2021-09-14 10:20:33 -07:00
|
|
|
|
the Vimscript form:
|
2021-05-28 08:24:48 -07:00
|
|
|
|
`vim.opt.wildignore = '*.o,*.a,__pycache__'`
|
|
|
|
|
|
|
|
|
|
However, vim.opt also supports a more elegent way of setting
|
|
|
|
|
list-style options, but using lua tables:
|
|
|
|
|
`vim.opt.wildignore = { '*.o', '*.a', '__pycache__' }`
|
|
|
|
|
|
|
|
|
|
To replicate the behavior of |:set+=|, use: >
|
|
|
|
|
|
|
|
|
|
-- vim.opt supports appending options via the "+" operator
|
|
|
|
|
vim.opt.wildignore = vim.opt.wildignore + { "*.pyc", "node_modules" }
|
|
|
|
|
|
|
|
|
|
-- or using the `:append(...)` method
|
|
|
|
|
vim.opt.wildignore:append { "*.pyc", "node_modules" }
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
To replicate the behavior of |:set^=|, use: >
|
|
|
|
|
|
|
|
|
|
-- vim.opt supports prepending options via the "^" operator
|
|
|
|
|
vim.opt.wildignore = vim.opt.wildignore ^ { "new_first_value" }
|
|
|
|
|
|
|
|
|
|
-- or using the `:prepend(...)` method
|
|
|
|
|
vim.opt.wildignore:prepend { "new_first_value" }
|
|
|
|
|
<
|
|
|
|
|
To replicate the behavior of |:set-=|, use: >
|
|
|
|
|
|
|
|
|
|
-- vim.opt supports removing options via the "-" operator
|
|
|
|
|
vim.opt.wildignore = vim.opt.wildignore - { "node_modules" }
|
|
|
|
|
|
|
|
|
|
-- or using the `:remove(...)` method
|
|
|
|
|
vim.opt.wildignore:remove { "node_modules" }
|
|
|
|
|
<
|
|
|
|
|
To set a map of values:
|
2021-09-14 10:20:33 -07:00
|
|
|
|
In Vimscript:
|
2021-05-28 08:24:48 -07:00
|
|
|
|
`set listchars=space:_,tab:>~`
|
|
|
|
|
|
|
|
|
|
In Lua:
|
|
|
|
|
`vim.opt.listchars = { space = '_', tab = '>~' }`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In any of the above examples, to replicate the behavior |setlocal|, use
|
|
|
|
|
`vim.opt_local`. Additionally, to replicate the behavior of |setglobal|, use
|
|
|
|
|
`vim.opt_global`.
|
|
|
|
|
*vim.opt*
|
|
|
|
|
|
|
|
|
|
|vim.opt| returns an Option object.
|
|
|
|
|
|
|
|
|
|
For example: `local listchar_object = vim.opt.listchar`
|
|
|
|
|
|
|
|
|
|
An `Option` has the following methods:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*vim.opt:get()*
|
|
|
|
|
Option:get()
|
|
|
|
|
|
|
|
|
|
Returns a lua-representation of the option. Boolean, number and string
|
|
|
|
|
values will be returned in exactly the same fashion.
|
|
|
|
|
|
|
|
|
|
For values that are comma-separated lists, an array will be returned with
|
|
|
|
|
the values as entries in the array: >
|
|
|
|
|
vim.cmd [[set wildignore=*.pyc,*.o]]
|
|
|
|
|
|
|
|
|
|
print(vim.inspect(vim.opt.wildignore:get()))
|
|
|
|
|
-- { "*.pyc", "*.o", }
|
|
|
|
|
|
|
|
|
|
for _, ignore_pattern in ipairs(vim.opt.wildignore:get()) do
|
|
|
|
|
print("Will ignore:", ignore_pattern)
|
|
|
|
|
end
|
|
|
|
|
-- Will ignore: *.pyc
|
|
|
|
|
-- Will ignore: *.o
|
|
|
|
|
<
|
|
|
|
|
For values that are comma-separated maps, a table will be returned with
|
|
|
|
|
the names as keys and the values as entries: >
|
|
|
|
|
vim.cmd [[set listchars=space:_,tab:>~]]
|
|
|
|
|
|
|
|
|
|
print(vim.inspect(vim.opt.listchars:get()))
|
|
|
|
|
-- { space = "_", tab = ">~", }
|
|
|
|
|
|
|
|
|
|
for char, representation in pairs(vim.opt.listchars:get()) do
|
|
|
|
|
print(char, "->", representation)
|
|
|
|
|
end
|
|
|
|
|
<
|
|
|
|
|
For values that are lists of flags, a set will be returned with the flags
|
|
|
|
|
as keys and `true` as entries. >
|
|
|
|
|
vim.cmd [[set formatoptions=njtcroql]]
|
|
|
|
|
|
|
|
|
|
print(vim.inspect(vim.opt.formatoptions:get()))
|
|
|
|
|
-- { n = true, j = true, c = true, ... }
|
|
|
|
|
|
|
|
|
|
local format_opts = vim.opt.formatoptions:get()
|
|
|
|
|
if format_opts.j then
|
|
|
|
|
print("J is enabled!")
|
|
|
|
|
end
|
|
|
|
|
<
|
|
|
|
|
*vim.opt:append()*
|
|
|
|
|
Option:append(value)
|
|
|
|
|
|
|
|
|
|
Append a value to string-style options. See |:set+=|
|
|
|
|
|
|
|
|
|
|
These are equivalent:
|
|
|
|
|
`vim.opt.formatoptions:append('j')`
|
|
|
|
|
`vim.opt.formatoptions = vim.opt.formatoptions + 'j'`
|
|
|
|
|
|
|
|
|
|
*vim.opt:prepend()*
|
|
|
|
|
Option:prepend(value)
|
|
|
|
|
|
|
|
|
|
Prepend a value to string-style options. See |:set^=|
|
|
|
|
|
|
|
|
|
|
These are equivalent:
|
|
|
|
|
`vim.opt.wildignore:prepend('*.o')`
|
|
|
|
|
`vim.opt.wildignore = vim.opt.wildignore ^ '*.o'`
|
|
|
|
|
|
|
|
|
|
*vim.opt:remove()*
|
|
|
|
|
Option:remove(value)
|
|
|
|
|
|
2021-05-29 21:09:30 -07:00
|
|
|
|
Remove a value from string-style options. See |:set-=|
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
|
|
|
|
These are equivalent:
|
|
|
|
|
`vim.opt.wildignore:remove('*.pyc')`
|
|
|
|
|
`vim.opt.wildignore = vim.opt.wildignore - '*.pyc'`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In general, using `vim.opt` will provide the expected result when the user is
|
|
|
|
|
used to interacting with editor |options| via `set`. There are still times
|
|
|
|
|
where the user may want to set particular options via a shorthand in Lua,
|
|
|
|
|
which is where |vim.o|, |vim.bo|, |vim.wo|, and |vim.go| come into play.
|
|
|
|
|
|
|
|
|
|
The behavior of |vim.o|, |vim.bo|, |vim.wo|, and |vim.go| is designed to
|
|
|
|
|
follow that of |:set|, |:setlocal|, and |:setglobal| which can be seen in the
|
|
|
|
|
table below:
|
|
|
|
|
|
|
|
|
|
lua command global_value local_value ~
|
|
|
|
|
vim.o :set set set
|
|
|
|
|
vim.bo/vim.wo :setlocal - set
|
|
|
|
|
vim.go :setglobal set -
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
vim.o *vim.o*
|
|
|
|
|
Get or set editor options, like |:set|. Invalid key is an error.
|
|
|
|
|
Example: >
|
|
|
|
|
vim.o.cmdheight = 4
|
|
|
|
|
print(vim.o.columns)
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
|
|
|
|
vim.go *vim.go*
|
|
|
|
|
Get or set an |option|. Invalid key is an error.
|
|
|
|
|
|
|
|
|
|
This is a wrapper around |nvim_set_option()| and |nvim_get_option()|.
|
|
|
|
|
|
|
|
|
|
NOTE: This is different than |vim.o| because this ONLY sets the global
|
|
|
|
|
option, which generally produces confusing behavior for options with
|
|
|
|
|
|global-local| values.
|
|
|
|
|
|
|
|
|
|
Example: >
|
|
|
|
|
vim.go.cmdheight = 4
|
|
|
|
|
<
|
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
vim.bo *vim.bo*
|
|
|
|
|
Get or set buffer-scoped |local-options|. Invalid key is an error.
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
|
|
|
|
This is a wrapper around |nvim_buf_set_option()| and
|
|
|
|
|
|nvim_buf_get_option()|.
|
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
Example: >
|
|
|
|
|
vim.bo.buflisted = true
|
|
|
|
|
print(vim.bo.comments)
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
vim.wo *vim.wo*
|
|
|
|
|
Get or set window-scoped |local-options|. Invalid key is an error.
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
|
|
|
|
This is a wrapper around |nvim_win_set_option()| and
|
|
|
|
|
|nvim_win_get_option()|.
|
|
|
|
|
|
2020-08-31 00:51:35 -07:00
|
|
|
|
Example: >
|
|
|
|
|
vim.wo.cursorcolumn = true
|
|
|
|
|
print(vim.wo.foldmarker)
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
Lua module: vim *lua-vim*
|
|
|
|
|
|
2021-05-31 10:47:51 -07:00
|
|
|
|
defer_fn({fn}, {timeout}) *vim.defer_fn()*
|
|
|
|
|
Defers calling `fn` until `timeout` ms passes.
|
|
|
|
|
|
|
|
|
|
Use to do a one-shot timer that calls `fn` Note: The {fn} is |schedule_wrap|ped automatically, so API
|
|
|
|
|
functions are safe to call.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{fn} Callback to call once `timeout` expires
|
|
|
|
|
{timeout} Number of milliseconds to wait before calling
|
|
|
|
|
`fn`
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
timer luv timer object
|
|
|
|
|
|
|
|
|
|
insert_keys({obj}) *vim.insert_keys()*
|
|
|
|
|
TODO: Documentation
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
inspect({object}, {options}) *vim.inspect()*
|
|
|
|
|
Return a human-readable representation of the given object.
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
https://github.com/kikito/inspect.lua
|
|
|
|
|
https://github.com/mpeterv/vinspect
|
|
|
|
|
|
2021-05-31 10:47:51 -07:00
|
|
|
|
make_dict_accessor({scope}) *vim.make_dict_accessor()*
|
2020-07-02 04:09:17 -07:00
|
|
|
|
TODO: Documentation
|
|
|
|
|
|
2021-05-31 10:47:51 -07:00
|
|
|
|
notify({msg}, {log_level}, {_opts}) *vim.notify()*
|
2021-08-22 13:55:28 -07:00
|
|
|
|
Notification provider
|
|
|
|
|
|
|
|
|
|
Without a runtime, writes to :Messages
|
2021-05-31 10:47:51 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{msg} Content of the notification to show to the
|
|
|
|
|
user
|
|
|
|
|
{log_level} Optional log level
|
|
|
|
|
{opts} Dictionary with optional options (timeout,
|
|
|
|
|
etc)
|
|
|
|
|
|
2021-08-22 13:55:28 -07:00
|
|
|
|
See also: ~
|
|
|
|
|
:help nvim_notify
|
|
|
|
|
|
2021-10-05 10:48:48 -07:00
|
|
|
|
on_key({fn}, {ns_id}) *vim.on_key()*
|
|
|
|
|
Adds Lua function {fn} with namespace id {ns_id} as a listener
|
|
|
|
|
to every, yes every, input key.
|
|
|
|
|
|
|
|
|
|
The Nvim command-line option |-w| is related but does not
|
|
|
|
|
support callbacks and cannot be toggled dynamically.
|
|
|
|
|
|
|
|
|
|
Note:
|
|
|
|
|
{fn} will not be cleared by |nvim_buf_clear_namespace()|
|
|
|
|
|
|
|
|
|
|
Note:
|
|
|
|
|
{fn} will receive the keys after mappings have been
|
|
|
|
|
evaluated
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{fn} function: Callback function. It should take one
|
|
|
|
|
string argument. On each key press, Nvim passes
|
|
|
|
|
the key char to fn(). |i_CTRL-V| If {fn} is nil,
|
|
|
|
|
it removes the callback for the associated
|
|
|
|
|
{ns_id}
|
|
|
|
|
{ns_id} number? Namespace ID. If nil or 0, generates and
|
|
|
|
|
returns a new |nvim_create_namesapce()| id.
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
number Namespace id associated with {fn}. Or count of all
|
|
|
|
|
callbacks if on_key() is called without arguments.
|
|
|
|
|
|
|
|
|
|
Note:
|
|
|
|
|
{fn} will be removed if an error occurs while calling.
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
paste({lines}, {phase}) *vim.paste()*
|
|
|
|
|
Paste handler, invoked by |nvim_paste()| when a conforming UI
|
|
|
|
|
(such as the |TUI|) pastes text into the editor.
|
|
|
|
|
|
|
|
|
|
Example: To remove ANSI color codes when pasting: >
|
|
|
|
|
|
|
|
|
|
vim.paste = (function(overridden)
|
|
|
|
|
return function(lines, phase)
|
|
|
|
|
for i,line in ipairs(lines) do
|
|
|
|
|
-- Scrub ANSI color codes from paste input.
|
|
|
|
|
lines[i] = line:gsub('\27%[[0-9;mK]+', '')
|
|
|
|
|
end
|
|
|
|
|
overridden(lines, phase)
|
|
|
|
|
end
|
|
|
|
|
end)(vim.paste)
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{lines} |readfile()|-style list of lines to paste.
|
|
|
|
|
|channel-lines|
|
|
|
|
|
{phase} -1: "non-streaming" paste: the call contains all
|
|
|
|
|
lines. If paste is "streamed", `phase` indicates the stream state:
|
|
|
|
|
• 1: starts the paste (exactly once)
|
|
|
|
|
• 2: continues the paste (zero or more times)
|
|
|
|
|
• 3: ends the paste (exactly once)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
false if client should cancel the paste.
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
|paste|
|
|
|
|
|
|
2021-05-31 10:47:51 -07:00
|
|
|
|
region({bufnr}, {pos1}, {pos2}, {regtype}, {inclusive}) *vim.region()*
|
|
|
|
|
Get a table of lines with start, end columns for a region
|
|
|
|
|
marked by two points
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{bufnr} number of buffer
|
|
|
|
|
{pos1} (line, column) tuple marking beginning of
|
|
|
|
|
region
|
|
|
|
|
{pos2} (line, column) tuple marking end of region
|
|
|
|
|
{regtype} type of selection (:help setreg)
|
|
|
|
|
{inclusive} boolean indicating whether the selection is
|
|
|
|
|
end-inclusive
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
region lua table of the form {linenr = {startcol,endcol}}
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
schedule_wrap({cb}) *vim.schedule_wrap()*
|
|
|
|
|
Defers callback `cb` until the Nvim API is safe to call.
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
|lua-loop-callbacks|
|
|
|
|
|
|vim.schedule()|
|
|
|
|
|
|vim.in_fast_event()|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-12-28 04:27:25 -07:00
|
|
|
|
deep_equal({a}, {b}) *vim.deep_equal()*
|
2021-09-10 06:22:40 -07:00
|
|
|
|
Deep compare values for equality
|
|
|
|
|
|
2021-10-05 10:48:48 -07:00
|
|
|
|
Tables are compared recursively unless they both provide the `eq` methamethod. All other types are compared using the equality `==` operator.
|
2021-09-10 06:22:40 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{a} first value
|
|
|
|
|
{b} second value
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
`true` if values are equals, else `false` .
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
deepcopy({orig}) *vim.deepcopy()*
|
|
|
|
|
Returns a deep copy of the given object. Non-table objects are
|
|
|
|
|
copied as in a typical Lua assignment, whereas table objects
|
2020-04-18 16:04:37 -07:00
|
|
|
|
are copied recursively. Functions are naively copied, so
|
|
|
|
|
functions in the copied table point to the same functions as
|
|
|
|
|
those in the input table. Userdata and threads are not copied
|
|
|
|
|
and will throw an error.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{orig} Table to copy
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
New table of copied keys and (nested) values.
|
|
|
|
|
|
2020-01-13 00:41:55 -07:00
|
|
|
|
endswith({s}, {suffix}) *vim.endswith()*
|
|
|
|
|
Tests if `s` ends with `suffix` .
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{s} (string) a string
|
|
|
|
|
{suffix} (string) a suffix
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(boolean) true if `suffix` is a suffix of s
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
gsplit({s}, {sep}, {plain}) *vim.gsplit()*
|
|
|
|
|
Splits a string at each instance of a separator.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{s} String to split
|
|
|
|
|
{sep} Separator string or pattern
|
|
|
|
|
{plain} If `true` use `sep` literally (passed to
|
|
|
|
|
String.find)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
Iterator over the split components
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
|vim.split()|
|
|
|
|
|
https://www.lua.org/pil/20.2.html
|
|
|
|
|
http://lua-users.org/wiki/StringLibraryTutorial
|
|
|
|
|
|
2019-12-28 04:27:25 -07:00
|
|
|
|
is_callable({f}) *vim.is_callable()*
|
|
|
|
|
Returns true if object `f` can be called as a function.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{f} Any object
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
true if `f` is callable, else false
|
|
|
|
|
|
2020-10-20 12:57:07 -07:00
|
|
|
|
is_valid({opt}) *vim.is_valid()*
|
|
|
|
|
TODO: Documentation
|
|
|
|
|
|
2019-12-28 04:27:25 -07:00
|
|
|
|
list_extend({dst}, {src}, {start}, {finish}) *vim.list_extend()*
|
|
|
|
|
Extends a list-like table with the values of another list-like
|
|
|
|
|
table.
|
|
|
|
|
|
|
|
|
|
NOTE: This mutates dst!
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{dst} list which will be modified and appended to.
|
|
|
|
|
{src} list from which values will be inserted.
|
|
|
|
|
{start} Start index on src. defaults to 1
|
|
|
|
|
{finish} Final index on src. defaults to #src
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
dst
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
|vim.tbl_extend()|
|
|
|
|
|
|
2021-03-11 08:11:11 -07:00
|
|
|
|
list_slice({list}, {start}, {finish}) *vim.list_slice()*
|
|
|
|
|
Creates a copy of a table containing only elements from start
|
|
|
|
|
to end (inclusive)
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{list} table table
|
|
|
|
|
{start} integer Start range of slice
|
|
|
|
|
{finish} integer End range of slice
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
Copy of table sliced from start to finish (inclusive)
|
|
|
|
|
|
2019-12-28 04:27:25 -07:00
|
|
|
|
pesc({s}) *vim.pesc()*
|
2020-07-02 04:09:17 -07:00
|
|
|
|
Escapes magic chars in a Lua pattern.
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{s} String to escape
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
%-escaped pattern string
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
https://github.com/rxi/lume
|
|
|
|
|
|
2021-09-25 19:08:36 -07:00
|
|
|
|
split({s}, {sep}, {kwargs}) *vim.split()*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
Splits a string at each instance of a separator.
|
|
|
|
|
|
|
|
|
|
Examples: >
|
2021-09-25 19:08:36 -07:00
|
|
|
|
|
|
|
|
|
split(":aa::b:", ":") --> {'','aa','','b',''}
|
|
|
|
|
split("axaby", "ab?") --> {'','x','y'}
|
|
|
|
|
split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
|
|
|
|
|
split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
|
2019-11-17 20:06:59 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2021-09-25 19:08:36 -07:00
|
|
|
|
{s} String to split
|
|
|
|
|
{sep} Separator string or pattern
|
|
|
|
|
{kwargs} Keyword arguments:
|
|
|
|
|
• plain: (boolean) If `true` use `sep` literally
|
|
|
|
|
(passed to string.find)
|
|
|
|
|
• trimempty: (boolean) If `true` remove empty
|
|
|
|
|
items from the front and back of the list
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
List-like table of the split components.
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
|vim.gsplit()|
|
|
|
|
|
|
2020-01-13 00:41:55 -07:00
|
|
|
|
startswith({s}, {prefix}) *vim.startswith()*
|
|
|
|
|
Tests if `s` starts with `prefix` .
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{s} (string) a string
|
|
|
|
|
{prefix} (string) a prefix
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(boolean) true if `prefix` is a prefix of s
|
|
|
|
|
|
2019-12-28 04:27:25 -07:00
|
|
|
|
tbl_add_reverse_lookup({o}) *vim.tbl_add_reverse_lookup()*
|
|
|
|
|
Add the reverse lookup values to an existing table. For
|
2020-07-02 04:09:17 -07:00
|
|
|
|
example: tbl_add_reverse_lookup { A = 1 } == { [1] = 'A , A = 1 }`
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2019-12-28 04:27:25 -07:00
|
|
|
|
{o} table The table to add the reverse to.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
tbl_contains({t}, {value}) *vim.tbl_contains()*
|
|
|
|
|
Checks if a list-like (vector) table contains `value` .
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{t} Table to check
|
|
|
|
|
{value} Value to compare
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
true if `t` contains `value`
|
|
|
|
|
|
2020-07-02 04:09:17 -07:00
|
|
|
|
tbl_count({t}) *vim.tbl_count()*
|
|
|
|
|
Counts the number of non-nil values in table `t` .
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
vim.tbl_count({ a=1, b=2 }) => 2
|
|
|
|
|
vim.tbl_count({ 1, 2 }) => 2
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{t} Table
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
Number that is the number of the value in table
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
https://github.com/Tieske/Penlight/blob/master/lua/pl/tablex.lua
|
|
|
|
|
|
|
|
|
|
tbl_deep_extend({behavior}, {...}) *vim.tbl_deep_extend()*
|
|
|
|
|
Merges recursively two or more map-like tables.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{behavior} Decides what to do if a key is found in more
|
|
|
|
|
than one map:
|
|
|
|
|
• "error": raise an error
|
|
|
|
|
• "keep": use value from the leftmost map
|
|
|
|
|
• "force": use value from the rightmost map
|
|
|
|
|
{...} Two or more map-like tables.
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
|tbl_extend()|
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
tbl_extend({behavior}, {...}) *vim.tbl_extend()*
|
|
|
|
|
Merges two or more map-like tables.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{behavior} Decides what to do if a key is found in more
|
|
|
|
|
than one map:
|
|
|
|
|
• "error": raise an error
|
|
|
|
|
• "keep": use value from the leftmost map
|
|
|
|
|
• "force": use value from the rightmost map
|
|
|
|
|
{...} Two or more map-like tables.
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
|extend()|
|
|
|
|
|
|
2020-07-02 04:09:17 -07:00
|
|
|
|
tbl_filter({func}, {t}) *vim.tbl_filter()*
|
|
|
|
|
Filter a table using a predicate function
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{func} function or callable table
|
|
|
|
|
{t} table
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
tbl_flatten({t}) *vim.tbl_flatten()*
|
|
|
|
|
Creates a copy of a list-like table such that any nested
|
|
|
|
|
tables are "unrolled" and appended to the result.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{t} List-like table
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
Flattened copy of the given list-like table.
|
|
|
|
|
|
|
|
|
|
See also: ~
|
2021-10-19 12:55:22 -07:00
|
|
|
|
From https://github.com/premake/premake-core/blob/master/src/base/table.lua
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2019-12-28 04:27:25 -07:00
|
|
|
|
tbl_isempty({t}) *vim.tbl_isempty()*
|
2020-08-31 00:51:35 -07:00
|
|
|
|
Checks if a table is empty.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{t} Table to check
|
|
|
|
|
|
2019-12-28 04:27:25 -07:00
|
|
|
|
See also: ~
|
2020-08-31 00:51:35 -07:00
|
|
|
|
https://github.com/premake/premake-core/blob/master/src/base/table.lua
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
tbl_islist({t}) *vim.tbl_islist()*
|
2020-09-06 16:55:49 -07:00
|
|
|
|
Tests if a Lua table can be treated as an array.
|
2020-07-02 04:09:17 -07:00
|
|
|
|
|
2020-09-06 16:55:49 -07:00
|
|
|
|
Empty table `{}` is assumed to be an array, unless it was
|
|
|
|
|
created by |vim.empty_dict()| or returned as a dict-like |API|
|
|
|
|
|
or Vimscript result, for example from |rpcrequest()| or
|
|
|
|
|
|vim.fn|.
|
2020-07-02 04:09:17 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{t} Table
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2020-07-02 04:09:17 -07:00
|
|
|
|
`true` if array-like table, else `false` .
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2019-12-28 04:27:25 -07:00
|
|
|
|
tbl_keys({t}) *vim.tbl_keys()*
|
|
|
|
|
Return a list of all keys used in a table. However, the order
|
|
|
|
|
of the return table of keys is not guaranteed.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2019-12-28 04:27:25 -07:00
|
|
|
|
{t} Table
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2019-12-28 04:27:25 -07:00
|
|
|
|
list of keys
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
See also: ~
|
2021-10-19 12:55:22 -07:00
|
|
|
|
From https://github.com/premake/premake-core/blob/master/src/base/table.lua
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2020-07-02 04:09:17 -07:00
|
|
|
|
tbl_map({func}, {t}) *vim.tbl_map()*
|
|
|
|
|
Apply a function to all values of a table.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{func} function or callable table
|
|
|
|
|
{t} table
|
|
|
|
|
|
2019-12-28 04:27:25 -07:00
|
|
|
|
tbl_values({t}) *vim.tbl_values()*
|
|
|
|
|
Return a list of all values used in a table. However, the
|
|
|
|
|
order of the return table of values is not guaranteed.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2019-12-28 04:27:25 -07:00
|
|
|
|
{t} Table
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2019-12-28 04:27:25 -07:00
|
|
|
|
list of values
|
|
|
|
|
|
|
|
|
|
trim({s}) *vim.trim()*
|
|
|
|
|
Trim whitespace (Lua pattern "%s") from both sides of a
|
|
|
|
|
string.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{s} String to trim
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
String with whitespace removed from its beginning and end
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
See also: ~
|
2019-12-28 04:27:25 -07:00
|
|
|
|
https://www.lua.org/pil/20.2.html
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
validate({opt}) *vim.validate()*
|
|
|
|
|
Validates a parameter specification (types and values).
|
|
|
|
|
|
|
|
|
|
Usage example: >
|
|
|
|
|
|
|
|
|
|
function user.new(name, age, hobbies)
|
|
|
|
|
vim.validate{
|
|
|
|
|
name={name, 'string'},
|
|
|
|
|
age={age, 'number'},
|
|
|
|
|
hobbies={hobbies, 'table'},
|
|
|
|
|
}
|
|
|
|
|
...
|
|
|
|
|
end
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Examples with explicit argument values (can be run directly): >
|
|
|
|
|
|
|
|
|
|
vim.validate{arg1={{'foo'}, 'table'}, arg2={'foo', 'string'}}
|
|
|
|
|
=> NOP (success)
|
2021-10-05 10:48:48 -07:00
|
|
|
|
<
|
|
|
|
|
>
|
|
|
|
|
vim.validate{arg1={1, 'table'}}
|
|
|
|
|
=> error('arg1: expected table, got number')
|
|
|
|
|
<
|
|
|
|
|
>
|
|
|
|
|
vim.validate{arg1={3, function(a) return (a % 2) == 0 end, 'even number'}}
|
|
|
|
|
=> error('arg1: expected even number, got 3')
|
2019-11-17 20:06:59 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{opt} Map of parameter names to validations. Each key is
|
|
|
|
|
a parameter name; each value is a tuple in one of
|
|
|
|
|
these forms:
|
|
|
|
|
1. (arg_value, type_name, optional)
|
|
|
|
|
• arg_value: argument value
|
|
|
|
|
• type_name: string type name, one of: ("table",
|
|
|
|
|
"t", "string", "s", "number", "n", "boolean",
|
|
|
|
|
"b", "function", "f", "nil", "thread",
|
|
|
|
|
"userdata")
|
|
|
|
|
• optional: (optional) boolean, if true, `nil`
|
|
|
|
|
is valid
|
|
|
|
|
|
|
|
|
|
2. (arg_value, fn, msg)
|
|
|
|
|
• arg_value: argument value
|
|
|
|
|
• fn: any function accepting one argument,
|
|
|
|
|
returns true if and only if the argument is
|
2020-10-20 12:57:07 -07:00
|
|
|
|
valid. Can optionally return an additional
|
|
|
|
|
informative error message as the second
|
|
|
|
|
returned value.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
• msg: (optional) error string if validation
|
|
|
|
|
fails
|
|
|
|
|
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
|
Lua module: uri *lua-uri*
|
|
|
|
|
|
|
|
|
|
uri_from_bufnr({bufnr}) *vim.uri_from_bufnr()*
|
|
|
|
|
Get a URI from a bufnr
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{bufnr} (number): Buffer number
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
URI
|
|
|
|
|
|
|
|
|
|
uri_from_fname({path}) *vim.uri_from_fname()*
|
|
|
|
|
Get a URI from a file path.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{path} (string): Path to file
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
URI
|
|
|
|
|
|
|
|
|
|
uri_to_bufnr({uri}) *vim.uri_to_bufnr()*
|
|
|
|
|
Return or create a buffer for a uri.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{uri} (string): The URI
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
bufnr.
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
|
|
2020-09-14 06:12:17 -07:00
|
|
|
|
Note:
|
|
|
|
|
Creates buffer but does not load it
|
|
|
|
|
|
|
|
|
|
uri_to_fname({uri}) *vim.uri_to_fname()*
|
|
|
|
|
Get a filename from a URI
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{uri} (string): The URI
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
Filename
|
|
|
|
|
|
2021-09-27 12:57:28 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
|
Lua module: ui *lua-ui*
|
|
|
|
|
|
2021-11-07 08:13:53 -07:00
|
|
|
|
input({opts}, {on_confirm}) *vim.ui.input()*
|
|
|
|
|
Prompts the user for input
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{opts} table Additional options. See |input()|
|
|
|
|
|
• prompt (string|nil) Text of the prompt.
|
|
|
|
|
Defaults to `Input:` .
|
|
|
|
|
• default (string|nil) Default reply to the
|
|
|
|
|
input
|
|
|
|
|
• completion (string|nil) Specifies type of
|
|
|
|
|
completion supported for input. Supported
|
|
|
|
|
types are the same that can be supplied to
|
|
|
|
|
a user-defined command using the
|
|
|
|
|
"-complete=" argument. See
|
|
|
|
|
|:command-completion|
|
|
|
|
|
• highlight (function) Function that will be
|
|
|
|
|
used for highlighting user inputs.
|
|
|
|
|
{on_confirm} function ((input|nil) -> ()) Called once the
|
|
|
|
|
user confirms or abort the input. `input` is
|
|
|
|
|
what the user typed. `nil` if the user
|
|
|
|
|
aborted the dialog.
|
|
|
|
|
|
2021-09-27 12:57:28 -07:00
|
|
|
|
select({items}, {opts}, {on_choice}) *vim.ui.select()*
|
|
|
|
|
Prompts the user to pick a single item from a collection of
|
|
|
|
|
entries
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
{items} table Arbitrary items
|
|
|
|
|
{opts} table Additional options
|
|
|
|
|
• prompt (string|nil) Text of the prompt.
|
|
|
|
|
Defaults to `Select one of:`
|
|
|
|
|
• format_item (function item -> text)
|
|
|
|
|
Function to format an individual item from
|
|
|
|
|
`items` . Defaults to `tostring` .
|
|
|
|
|
{on_choice} function ((item|nil, idx|nil) -> ()) Called
|
|
|
|
|
once the user made a choice. `idx` is the
|
|
|
|
|
1-based index of `item` within `item` . `nil`
|
|
|
|
|
if the user aborted the dialog.
|
|
|
|
|
|
2020-09-06 16:55:49 -07:00
|
|
|
|
vim:tw=78:ts=8:ft=help:norl:
|