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
|
|
|
|
|
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
Lua engine *lua* *Lua*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
Type |gO| to see the table of contents.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2022-05-11 09:23:46 -07:00
|
|
|
|
INTRODUCTION *lua-intro*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-10-09 05:21:52 -07:00
|
|
|
|
The Lua 5.1 script engine is builtin and always available. Try this command to
|
2022-11-22 05:50:50 -07:00
|
|
|
|
get an idea of what lurks beneath: >vim
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-06-23 03:16:55 -07:00
|
|
|
|
:lua vim.print(package.loaded)
|
2022-10-09 05:21:52 -07:00
|
|
|
|
|
|
|
|
|
Nvim includes a "standard library" |lua-stdlib| for Lua. It complements the
|
|
|
|
|
"editor stdlib" (|builtin-functions| and |Ex-commands|) and the |API|, all of
|
|
|
|
|
which can be used from Lua code (|lua-vimscript| |vim.api|). Together these
|
|
|
|
|
"namespaces" form the Nvim programming interface.
|
|
|
|
|
|
2021-09-20 19:00:50 -07:00
|
|
|
|
Lua plugins and user config are automatically discovered and loaded, just like
|
|
|
|
|
Vimscript. See |lua-guide| for practical guidance.
|
2022-10-09 05:21:52 -07:00
|
|
|
|
|
2021-09-20 19:00:50 -07:00
|
|
|
|
You can also run Lua scripts from your shell using the |-l| argument: >
|
|
|
|
|
nvim -l foo.lua [args...]
|
|
|
|
|
<
|
2022-10-09 05:21:52 -07:00
|
|
|
|
*lua-compat*
|
2024-07-21 04:18:25 -07:00
|
|
|
|
Lua 5.1 is the permanent interface for Nvim Lua. Plugins should target Lua 5.1
|
|
|
|
|
as specified in |luaref|; later versions (which are essentially different,
|
|
|
|
|
incompatible, dialects) are not supported. This includes extensions such as
|
|
|
|
|
`goto` that some Lua 5.1 interpreters like LuaJIT may support.
|
2022-10-09 05:21:52 -07:00
|
|
|
|
|
2022-11-28 14:43:10 -07:00
|
|
|
|
*lua-luajit*
|
2024-07-21 04:18:25 -07:00
|
|
|
|
While Nvim officially only requires Lua 5.1 support, it should be built with
|
|
|
|
|
LuaJIT or a compatible fork on supported platforms for performance reasons.
|
|
|
|
|
LuaJIT also comes with useful extensions such as `ffi`, |lua-profile|, and
|
|
|
|
|
enhanced standard library functions; these cannot be assumed to be available,
|
|
|
|
|
and Lua code in |init.lua| or plugins should check the `jit` global variable
|
|
|
|
|
before using them: >lua
|
2022-11-28 14:43:10 -07:00
|
|
|
|
if jit then
|
|
|
|
|
-- code for luajit
|
|
|
|
|
else
|
|
|
|
|
-- code for plain lua 5.1
|
|
|
|
|
end
|
|
|
|
|
<
|
|
|
|
|
*lua-bit*
|
2024-07-21 04:18:25 -07:00
|
|
|
|
One exception is the LuaJIT `bit` extension, which is always available: when
|
|
|
|
|
built with PUC Lua, Nvim includes a fallback implementation which provides
|
|
|
|
|
`require("bit")`.
|
2024-05-02 06:57:21 -07:00
|
|
|
|
|
|
|
|
|
*lua-profile*
|
2024-07-21 04:18:25 -07:00
|
|
|
|
If Nvim is built with LuaJIT, Lua code can be profiled via >lua
|
2024-05-02 06:57:21 -07:00
|
|
|
|
-- Start a profiling session:
|
|
|
|
|
require('jit.p').start('ri1', '/tmp/profile')
|
|
|
|
|
|
|
|
|
|
-- Perform arbitrary tasks (use plugins, scripts, etc.) ...
|
|
|
|
|
|
|
|
|
|
-- Stop the session. Profile is written to /tmp/profile.
|
|
|
|
|
require('jit.p').stop()
|
|
|
|
|
|
2024-07-21 04:18:25 -07:00
|
|
|
|
See https://luajit.org/ext_profiler.html or the `p.lua` source for details: >
|
2024-05-02 06:57:21 -07:00
|
|
|
|
:lua vim.cmd.edit(package.searchpath('jit.p', package.path))
|
2022-11-28 14:43:10 -07:00
|
|
|
|
|
2021-09-20 19:00:50 -07:00
|
|
|
|
==============================================================================
|
2022-10-09 05:21:52 -07:00
|
|
|
|
LUA CONCEPTS AND IDIOMS *lua-concepts*
|
|
|
|
|
|
|
|
|
|
Lua is very simple: this means that, while there are some quirks, once you
|
|
|
|
|
internalize those quirks, everything works the same everywhere. Scopes
|
|
|
|
|
(closures) in particular are very consistent, unlike JavaScript or most other
|
|
|
|
|
languages.
|
|
|
|
|
|
|
|
|
|
Lua has three fundamental mechanisms—one for "each major aspect of
|
|
|
|
|
programming": tables, closures, and coroutines.
|
|
|
|
|
https://www.lua.org/doc/cacm2018.pdf
|
|
|
|
|
- Tables are the "object" or container datastructure: they represent both
|
|
|
|
|
lists and maps, you can extend them to represent your own datatypes and
|
2023-08-03 08:35:10 -07:00
|
|
|
|
change their behavior using |metatable|s (like Python's "datamodel").
|
2022-10-09 05:21:52 -07:00
|
|
|
|
- EVERY scope in Lua is a closure: a function is a closure, a module is
|
2023-08-03 08:35:10 -07:00
|
|
|
|
a closure, a `do` block (|lua-do|) is a closure--and they all work the same.
|
|
|
|
|
A Lua module is literally just a big closure discovered on the "path"
|
2022-10-09 05:21:52 -07:00
|
|
|
|
(where your modules are found: |package.cpath|).
|
|
|
|
|
- Stackful coroutines enable cooperative multithreading, generators, and
|
|
|
|
|
versatile control for both Lua and its host (Nvim).
|
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
|
*iterator*
|
2023-07-12 10:27:14 -07:00
|
|
|
|
An iterator is just a function that can be called repeatedly to get the "next"
|
|
|
|
|
value of a collection (or any other |iterable|). This interface is expected by
|
|
|
|
|
|for-in| loops, produced by |pairs()|, supported by |vim.iter|, etc.
|
|
|
|
|
https://www.lua.org/pil/7.1.html
|
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
|
*iterable*
|
2023-07-12 10:27:14 -07:00
|
|
|
|
An "iterable" is anything that |vim.iter()| can consume: tables, dicts, lists,
|
|
|
|
|
iterator functions, tables implementing the |__call()| metamethod, and
|
|
|
|
|
|vim.iter()| objects.
|
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
|
*list-iterator*
|
|
|
|
|
Iterators on |lua-list| tables have a "middle" and "end", whereas iterators in
|
|
|
|
|
general may be logically infinite. Therefore some |vim.iter| operations (e.g.
|
|
|
|
|
|Iter:rev()|) make sense only on list-like tables (which are finite by
|
|
|
|
|
definition).
|
|
|
|
|
|
2023-08-03 08:35:10 -07:00
|
|
|
|
*lua-function-call*
|
2022-11-22 05:50:50 -07:00
|
|
|
|
Lua functions can be called in multiple ways. Consider the function: >lua
|
2022-10-09 05:21:52 -07:00
|
|
|
|
local foo = function(a, b)
|
|
|
|
|
print("A: ", a)
|
|
|
|
|
print("B: ", b)
|
|
|
|
|
end
|
|
|
|
|
|
2022-11-22 05:50:50 -07:00
|
|
|
|
The first way to call this function is: >lua
|
2022-10-09 05:21:52 -07:00
|
|
|
|
foo(1, 2)
|
|
|
|
|
-- ==== Result ====
|
|
|
|
|
-- A: 1
|
|
|
|
|
-- B: 2
|
|
|
|
|
|
2023-08-03 08:35:10 -07:00
|
|
|
|
This way of calling a function is familiar from most scripting languages. In
|
|
|
|
|
Lua, any missing arguments are passed as `nil`, and extra parameters are
|
|
|
|
|
silently discarded. Example: >lua
|
2022-10-09 05:21:52 -07:00
|
|
|
|
foo(1)
|
|
|
|
|
-- ==== Result ====
|
|
|
|
|
-- A: 1
|
|
|
|
|
-- B: nil
|
2023-08-03 08:35:10 -07:00
|
|
|
|
<
|
2021-09-20 19:00:50 -07:00
|
|
|
|
*kwargs*
|
|
|
|
|
When calling a function, you can omit the parentheses if the function takes
|
|
|
|
|
exactly one string literal (`"foo"`) or table literal (`{1,2,3}`). The latter
|
2023-08-03 08:35:10 -07:00
|
|
|
|
is often used to mimic "named parameters" ("kwargs" or "keyword args") as in
|
|
|
|
|
languages like Python and C#. Example: >lua
|
2022-10-09 05:21:52 -07:00
|
|
|
|
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" }
|
2022-05-11 09:23:46 -07:00
|
|
|
|
<
|
2023-12-28 05:49:44 -07:00
|
|
|
|
There's nothing special going on here except that parentheses are implicitly
|
|
|
|
|
added. But visually, this small bit of sugar gets reasonably close to a
|
|
|
|
|
"keyword args" interface.
|
2022-10-09 05:21:52 -07:00
|
|
|
|
|
2023-12-28 05:49:44 -07:00
|
|
|
|
*lua-regex*
|
2022-10-09 05:21:52 -07:00
|
|
|
|
Lua intentionally does not support regular expressions, instead it has limited
|
2023-12-28 05:49:44 -07:00
|
|
|
|
|lua-patterns| which avoid the performance pitfalls of extended regex. Lua
|
|
|
|
|
scripts can also use Vim regex via |vim.regex()|.
|
2022-10-09 05:21:52 -07:00
|
|
|
|
|
2023-08-03 08:35:10 -07:00
|
|
|
|
Examples: >lua
|
2022-10-09 05:21:52 -07:00
|
|
|
|
|
|
|
|
|
print(string.match("foo123bar123", "%d+"))
|
|
|
|
|
-- 123
|
|
|
|
|
print(string.match("foo123bar123", "[^%d]+"))
|
|
|
|
|
-- foo
|
|
|
|
|
print(string.match("foo123bar123", "[abc]+"))
|
|
|
|
|
-- ba
|
|
|
|
|
print(string.match("foo.bar", "%.bar"))
|
|
|
|
|
-- .bar
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
==============================================================================
|
2023-08-03 08:35:10 -07:00
|
|
|
|
IMPORTING LUA MODULES *lua-module-load*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2021-08-26 02:39:29 -07:00
|
|
|
|
Modules are searched for under the directories specified in 'runtimepath', in
|
2022-10-09 05:21:52 -07:00
|
|
|
|
the order they appear. Any "." in the module name is treated as a directory
|
|
|
|
|
separator when searching. For a module `foo.bar`, each directory is searched
|
|
|
|
|
for `lua/foo/bar.lua`, then `lua/foo/bar/init.lua`. If no files are found,
|
2021-11-21 04:11:32 -07:00
|
|
|
|
the directories are searched again for a shared library with a name matching
|
2022-05-11 09:23:46 -07:00
|
|
|
|
`lua/foo/bar.?`, where `?` is a list of suffixes (such as `so` or `dll`) derived from
|
2022-08-08 09:58:32 -07:00
|
|
|
|
the initial value of |package.cpath|. If still no files are found, Nvim falls
|
2022-05-11 09:23:46 -07:00
|
|
|
|
back to Lua's default search mechanism. The first script found is run and
|
|
|
|
|
`require()` returns the value returned by the script if any, else `true`.
|
|
|
|
|
|
|
|
|
|
The return value is cached after the first call to `require()` for each module,
|
|
|
|
|
with subsequent calls returning the cached value without searching for, or
|
2023-08-03 08:35:10 -07:00
|
|
|
|
executing any script. For further details see |require()|.
|
2021-08-26 02:39:29 -07:00
|
|
|
|
|
2022-08-08 09:58:32 -07:00
|
|
|
|
For example, if 'runtimepath' is `foo,bar` and |package.cpath| was
|
2021-11-21 04:11:32 -07:00
|
|
|
|
`./?.so;./?.dll` at startup, `require('mod')` searches these paths in order
|
2022-10-09 09:19:43 -07:00
|
|
|
|
and loads the first module found ("first wins"): >
|
2021-08-26 02:39:29 -07:00
|
|
|
|
foo/lua/mod.lua
|
|
|
|
|
foo/lua/mod/init.lua
|
2021-11-21 04:11:32 -07:00
|
|
|
|
bar/lua/mod.lua
|
2021-08-26 02:39:29 -07:00
|
|
|
|
bar/lua/mod/init.lua
|
2021-11-21 04:11:32 -07:00
|
|
|
|
foo/lua/mod.so
|
|
|
|
|
foo/lua/mod.dll
|
|
|
|
|
bar/lua/mod.so
|
|
|
|
|
bar/lua/mod.dll
|
2022-10-09 09:19:43 -07:00
|
|
|
|
<
|
2022-10-09 05:21:52 -07:00
|
|
|
|
*lua-package-path*
|
2022-08-08 09:58:32 -07:00
|
|
|
|
Nvim automatically adjusts |package.path| and |package.cpath| according to the
|
2022-05-11 09:23:46 -07:00
|
|
|
|
effective 'runtimepath' value. Adjustment happens whenever 'runtimepath' is
|
2022-10-09 05:21:52 -07:00
|
|
|
|
changed. `package.path` is adjusted by simply appending `/lua/?.lua` and
|
2019-11-17 20:06:59 -07:00
|
|
|
|
`/lua/?/init.lua` to each directory from 'runtimepath' (`/` is actually the
|
|
|
|
|
first character of `package.config`).
|
|
|
|
|
|
2022-08-08 09:58:32 -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
|
2019-11-17 20:06:59 -07:00
|
|
|
|
`/lua/?/init.lua` to each runtimepath, all unique `?`-containing suffixes of
|
2022-08-08 09:58:32 -07:00
|
|
|
|
the existing |package.cpath| are used. Example:
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-10-09 09:19:43 -07:00
|
|
|
|
- 1. Given that
|
2019-11-17 20:06:59 -07:00
|
|
|
|
- 'runtimepath' contains `/foo/bar,/xxx;yyy/baz,/abc`;
|
2022-10-09 09:19:43 -07:00
|
|
|
|
- initial |package.cpath| (defined at compile-time or derived from
|
|
|
|
|
`$LUA_CPATH` / `$LUA_INIT`) 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 a 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 a `/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
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
Note:
|
|
|
|
|
|
|
|
|
|
- To track 'runtimepath' updates, paths added at previous update are
|
|
|
|
|
remembered and removed at the next update, while all paths derived from the
|
2022-05-11 09:23:46 -07:00
|
|
|
|
new 'runtimepath' are prepended as described above. This allows removing
|
2019-11-17 20:06:59 -07:00
|
|
|
|
paths when path is removed from 'runtimepath', adding paths when they are
|
2022-08-08 09:58:32 -07:00
|
|
|
|
added and reordering |package.path|/|package.cpath| content if 'runtimepath'
|
2019-11-17 20:06:59 -07:00
|
|
|
|
was reordered.
|
|
|
|
|
|
|
|
|
|
- Although adjustments happen automatically, Nvim does not track current
|
2022-08-08 09:58:32 -07:00
|
|
|
|
values of |package.path| or |package.cpath|. If you happen to delete some
|
2022-11-22 05:50:50 -07:00
|
|
|
|
paths from there you can set 'runtimepath' to trigger an update: >vim
|
2019-11-17 20:06:59 -07:00
|
|
|
|
let &runtimepath = &runtimepath
|
|
|
|
|
|
|
|
|
|
- Skipping paths from 'runtimepath' which contain semicolons applies both to
|
2022-08-08 09:58:32 -07:00
|
|
|
|
|package.path| and |package.cpath|. Given that there are some badly written
|
2022-05-11 09:23:46 -07:00
|
|
|
|
plugins using shell, which will not work with paths containing semicolons,
|
|
|
|
|
it is better to not have them in 'runtimepath' at all.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2022-05-11 09:23:46 -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
|
2022-09-25 16:58:27 -07:00
|
|
|
|
|package.path| are available.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
The Lua print() function redirects its output to the Nvim message area, with
|
|
|
|
|
arguments separated by " " (space) instead of "\t" (tab).
|
|
|
|
|
|
2023-03-20 13:11:10 -07:00
|
|
|
|
*:lua=* *:lua*
|
2022-06-14 17:49:54 -07:00
|
|
|
|
:lua {chunk}
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Executes Lua chunk {chunk}. If {chunk} starts with "=" the rest of the
|
2024-05-02 06:57:21 -07:00
|
|
|
|
chunk is evaluated as an expression and printed. `:lua =expr` and `:=expr`
|
|
|
|
|
are equivalent to `:lua vim.print(expr)`.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2022-11-22 05:50:50 -07:00
|
|
|
|
Examples: >vim
|
2022-08-11 05:25:48 -07:00
|
|
|
|
:lua vim.api.nvim_command('echo "Hello, Nvim!"')
|
2022-11-22 05:50:50 -07:00
|
|
|
|
< To see the Lua version: >vim
|
2022-08-11 05:25:48 -07:00
|
|
|
|
:lua print(_VERSION)
|
2022-11-22 05:50:50 -07:00
|
|
|
|
< To see the LuaJIT version: >vim
|
2022-08-11 05:25:48 -07:00
|
|
|
|
:lua =jit.version
|
2019-11-17 20:06:59 -07:00
|
|
|
|
<
|
2024-01-26 18:00:50 -07:00
|
|
|
|
:{range}lua
|
2024-01-27 11:40:30 -07:00
|
|
|
|
Executes buffer lines in {range} as Lua code. Unlike |:source|, this
|
|
|
|
|
always treats the lines as Lua code.
|
2024-01-26 18:00:50 -07:00
|
|
|
|
|
2024-01-27 11:40:30 -07:00
|
|
|
|
Example: select the following code and type ":lua<Enter>" to execute it: >lua
|
|
|
|
|
print(string.format(
|
|
|
|
|
'unix time: %s', os.time()))
|
|
|
|
|
<
|
2022-05-11 09:23:46 -07:00
|
|
|
|
*:lua-heredoc*
|
2023-04-28 17:12:32 -07:00
|
|
|
|
:lua << [trim] [{endmarker}]
|
2019-11-17 20:06:59 -07:00
|
|
|
|
{script}
|
|
|
|
|
{endmarker}
|
2023-04-28 17:12:32 -07:00
|
|
|
|
Executes Lua script {script} from within Vimscript. You can omit
|
|
|
|
|
[endmarker] after the "<<" and use a dot "." after {script} (similar to
|
2023-06-27 10:21:27 -07:00
|
|
|
|
|:append|, |:insert|). Refer to |:let-heredoc| for more information.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2022-11-22 05:50:50 -07:00
|
|
|
|
Example: >vim
|
2022-08-11 05:25:48 -07:00
|
|
|
|
function! CurrentLineInfo()
|
|
|
|
|
lua << EOF
|
|
|
|
|
local linenr = vim.api.nvim_win_get_cursor(0)[1]
|
2024-01-27 11:40:30 -07:00
|
|
|
|
local curline = vim.api.nvim_buf_get_lines(0, linenr - 1, linenr, false)[1]
|
|
|
|
|
print(string.format('Line [%d] has %d bytes', linenr, #curline))
|
2022-08-11 05:25:48 -07:00
|
|
|
|
EOF
|
|
|
|
|
endfunction
|
2022-05-11 09:23:46 -07:00
|
|
|
|
<
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Note that the `local` variables will disappear when the block finishes.
|
|
|
|
|
But not globals.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
*:luado*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
:[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,$".
|
|
|
|
|
|
2022-11-22 05:50:50 -07:00
|
|
|
|
Examples: >vim
|
2022-08-11 05:25:48 -07:00
|
|
|
|
: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 * ")" }
|
2022-10-09 09:19:43 -07:00
|
|
|
|
:luado if bp:match(line) then return "=>\t" .. line end
|
2019-11-17 20:06:59 -07:00
|
|
|
|
<
|
2022-05-11 09:23:46 -07:00
|
|
|
|
*:luafile*
|
2022-06-14 17:49:54 -07:00
|
|
|
|
:luafile {file}
|
2022-08-11 05:25:48 -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.
|
|
|
|
|
|
2022-11-22 05:50:50 -07:00
|
|
|
|
Examples: >vim
|
2022-08-11 05:25:48 -07:00
|
|
|
|
:luafile script.lua
|
|
|
|
|
:luafile %
|
2019-11-17 20:06:59 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
==============================================================================
|
2023-07-28 06:48:41 -07:00
|
|
|
|
luaeval() *lua-eval*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
The (dual) equivalent of "vim.eval" for passing Lua values to Nvim is
|
2022-05-11 09:23: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
|
2022-11-22 05:50:50 -07:00
|
|
|
|
semantically equivalent in Lua to: >lua
|
2022-05-11 09:23:46 -07:00
|
|
|
|
|
2019-11-17 20:06:59 -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
|
2022-05-11 09:23:46 -07:00
|
|
|
|
<
|
2019-11-17 20:06:59 -07:00
|
|
|
|
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().
|
|
|
|
|
|
2022-11-22 05:50:50 -07:00
|
|
|
|
Example: >vim
|
2019-11-17 20:06:59 -07:00
|
|
|
|
:echo luaeval('_A[1] + _A[2]', [40, 2])
|
2022-11-22 05:50:50 -07:00
|
|
|
|
" 42
|
2019-11-17 20:06:59 -07:00
|
|
|
|
:echo luaeval('string.match(_A, "[a-z]+")', 'XYXfoo123')
|
2022-11-22 05:50:50 -07:00
|
|
|
|
" foo
|
2022-05-11 09:23:46 -07:00
|
|
|
|
<
|
2023-08-03 08:35:10 -07:00
|
|
|
|
*lua-table-ambiguous*
|
2019-11-17 20:06:59 -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:
|
2023-08-03 08:35:10 -07:00
|
|
|
|
*lua-list*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
0. Empty table is empty list.
|
2023-11-25 07:35:31 -07:00
|
|
|
|
1. Table with N consecutive integer indices starting from 1 and ending with
|
|
|
|
|
N is considered a list. See also |list-iterator|.
|
2023-08-03 08:35:10 -07:00
|
|
|
|
*lua-dict*
|
2022-05-11 09:23:46 -07:00
|
|
|
|
2. Table with string keys, none of which contains NUL byte, is considered to
|
2019-11-17 20:06:59 -07:00
|
|
|
|
be a dictionary.
|
2022-05-11 09:23:46 -07:00
|
|
|
|
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
|
2019-11-17 20:06:59 -07:00
|
|
|
|
a |msgpack-special-map|.
|
2023-08-03 08:35:10 -07:00
|
|
|
|
*lua-special-tbl*
|
2022-05-11 09:23:46 -07:00
|
|
|
|
4. Table with `vim.type_idx` key may be a dictionary, a list or floating-point
|
2019-11-17 20:06:59 -07:00
|
|
|
|
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-11-27 09:10:48 -07:00
|
|
|
|
- `{[vim.type_idx]=vim.types.array}` is converted to an empty list. As well
|
|
|
|
|
as `{[vim.type_idx]=vim.types.array, [42]=1}`: integral keys that do not
|
2021-09-14 10:20:33 -07:00
|
|
|
|
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.
|
|
|
|
|
|
2022-11-22 05:50:50 -07:00
|
|
|
|
Examples: >vim
|
2019-11-17 20:06:59 -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)
|
2022-05-11 09:23:46 -07:00
|
|
|
|
<
|
|
|
|
|
Note: Second argument to `luaeval` is converted ("marshalled") from Vimscript
|
2021-09-14 10:20:33 -07:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
==============================================================================
|
2022-05-11 09:23:46 -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
|
2022-11-22 05:50:50 -07:00
|
|
|
|
which are global or accessible from global tables. The expression >vim
|
2022-11-22 10:41:00 -07:00
|
|
|
|
call v:lua.func(arg1, arg2)
|
2022-11-22 05:50:50 -07:00
|
|
|
|
is equivalent to the Lua chunk >lua
|
2019-11-17 20:06:59 -07:00
|
|
|
|
return func(...)
|
2022-11-22 05:50:50 -07:00
|
|
|
|
where the args are converted to Lua values. The expression >vim
|
2022-11-22 10:41:00 -07:00
|
|
|
|
call v:lua.somemod.func(args)
|
2022-11-22 05:50:50 -07:00
|
|
|
|
is equivalent to the Lua chunk >lua
|
2019-11-17 20:06:59 -07:00
|
|
|
|
return somemod.func(...)
|
|
|
|
|
|
2022-11-22 05:50:50 -07:00
|
|
|
|
In addition, functions of packages can be accessed like >vim
|
2022-11-22 10:41:00 -07:00
|
|
|
|
call v:lua.require'mypack'.func(arg1, arg2)
|
|
|
|
|
call v:lua.require'mypack.submod'.func(arg1, arg2)
|
2022-05-11 09:23:46 -07:00
|
|
|
|
Note: Only single quote form without parens is allowed. Using
|
2021-10-23 09:20:19 -07:00
|
|
|
|
`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
|
2022-11-22 05:50:50 -07:00
|
|
|
|
example: >vim
|
2022-11-22 10:41:00 -07:00
|
|
|
|
:eval arg1->v:lua.somemod.func(arg2)
|
2022-05-11 09:23:46 -07:00
|
|
|
|
<
|
2019-11-17 20:06:59 -07:00
|
|
|
|
You can use `v:lua` in "func" options like 'tagfunc', 'omnifunc', etc.
|
2022-11-22 05:50:50 -07:00
|
|
|
|
For example consider the following Lua omnifunc handler: >lua
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
function mymod.omnifunc(findstart, base)
|
|
|
|
|
if findstart == 1 then
|
|
|
|
|
return 0
|
|
|
|
|
else
|
|
|
|
|
return {'stuff', 'steam', 'strange things'}
|
|
|
|
|
end
|
|
|
|
|
end
|
2022-12-19 09:37:45 -07:00
|
|
|
|
vim.bo[buf].omnifunc = 'v:lua.mymod.omnifunc'
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
Note: The module ("mymod" in the above example) must either be a Lua global,
|
2021-09-20 19:00:50 -07:00
|
|
|
|
or use require() as shown 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:
|
2022-11-22 05:50:50 -07:00
|
|
|
|
|Funcref|s cannot represent Lua functions. The following are errors: >vim
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
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
|
2022-05-11 09:23:46 -07:00
|
|
|
|
<
|
2019-11-17 20:06:59 -07:00
|
|
|
|
==============================================================================
|
2022-05-11 09:23:46 -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
|
2022-05-11 09:23:46 -07:00
|
|
|
|
various functions and sub-modules. It is always loaded, thus `require("vim")`
|
2019-11-17 20:06:59 -07:00
|
|
|
|
is unnecessary.
|
|
|
|
|
|
2022-11-22 05:50:50 -07:00
|
|
|
|
You can peek at the module properties: >vim
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-06-23 03:16:55 -07:00
|
|
|
|
:lua vim.print(vim)
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
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>,
|
|
|
|
|
...
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-22 05:50:50 -07:00
|
|
|
|
To find documentation on e.g. the "deepcopy" function: >vim
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
:help vim.deepcopy()
|
|
|
|
|
|
|
|
|
|
Note that underscore-prefixed functions (e.g. "_os_proc_children") are
|
|
|
|
|
internal/private and must not be used by plugins.
|
|
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
2023-06-03 03:06:00 -07:00
|
|
|
|
VIM.UV *lua-loop* *vim.uv*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-06-03 03:06:00 -07:00
|
|
|
|
`vim.uv` exposes the "luv" Lua bindings for the libUV library that Nvim uses
|
|
|
|
|
for networking, filesystem, and process management, see |luvref.txt|.
|
|
|
|
|
In particular, it allows interacting with the main Nvim |luv-event-loop|.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-05-11 09:23:46 -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
|
2023-06-03 03:06:00 -07:00
|
|
|
|
`vim.uv` callbacks. For example, this is an error: >lua
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-06-03 03:06:00 -07:00
|
|
|
|
local timer = vim.uv.new_timer()
|
2019-11-17 20:06:59 -07:00
|
|
|
|
timer:start(1000, 0, function()
|
|
|
|
|
vim.api.nvim_command('echomsg "test"')
|
|
|
|
|
end)
|
2022-05-11 09:23:46 -07:00
|
|
|
|
<
|
2022-11-22 05:50:50 -07:00
|
|
|
|
To avoid the error use |vim.schedule_wrap()| to defer the callback: >lua
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-06-03 03:06:00 -07:00
|
|
|
|
local timer = vim.uv.new_timer()
|
2019-11-17 20:06:59 -07:00
|
|
|
|
timer:start(1000, 0, vim.schedule_wrap(function()
|
|
|
|
|
vim.api.nvim_command('echomsg "test"')
|
|
|
|
|
end))
|
2022-05-11 09:23:46 -07:00
|
|
|
|
<
|
|
|
|
|
(For one-shot timers, see |vim.defer_fn()|, which automatically adds the
|
|
|
|
|
wrapping.)
|
2020-07-07 01:55:40 -07:00
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
Example: repeating timer
|
|
|
|
|
1. Save this code to a file.
|
2022-11-22 05:50:50 -07:00
|
|
|
|
2. Execute it with ":luafile %". >lua
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
-- Create a timer handle (implementation detail: uv_timer_t).
|
2023-06-03 03:06:00 -07:00
|
|
|
|
local timer = vim.uv.new_timer()
|
2019-11-17 20:06:59 -07:00
|
|
|
|
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');
|
2022-05-11 09:23:46 -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
|
2022-11-22 05:50:50 -07:00
|
|
|
|
|:checktime|). >lua
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-06-03 03:06:00 -07:00
|
|
|
|
local w = vim.uv.new_fs_event()
|
2019-11-17 20:06:59 -07:00
|
|
|
|
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>'))")
|
2022-05-11 09:23:46 -07:00
|
|
|
|
<
|
2024-07-06 02:44:19 -07:00
|
|
|
|
*inotify-limitations*
|
|
|
|
|
When on Linux you may need to increase the maximum number of `inotify` watches
|
|
|
|
|
and queued events as the default limit can be too low. To increase the limit,
|
|
|
|
|
run: >sh
|
|
|
|
|
sysctl fs.inotify.max_user_watches=494462
|
2024-03-10 18:02:52 -07:00
|
|
|
|
<
|
2024-07-06 02:44:19 -07:00
|
|
|
|
This will increase the limit to 494462 watches and queued events. These lines
|
2024-03-10 18:02:52 -07:00
|
|
|
|
can be added to `/etc/sysctl.conf` to make the changes persistent.
|
|
|
|
|
|
2024-07-06 02:44:19 -07:00
|
|
|
|
Note that each watch is a structure in the Kernel, thus available memory is
|
|
|
|
|
also a bottleneck for using inotify. In fact, a watch can take up to 1KB of
|
|
|
|
|
space. This means a million watches could result in 1GB of extra RAM usage.
|
|
|
|
|
|
2022-05-11 09:23:46 -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.
|
2022-11-22 05:50:50 -07:00
|
|
|
|
4. Connect from any TCP client (e.g. "nc 0.0.0.0 36795"): >lua
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
local function create_server(host, port, on_connect)
|
2023-06-03 03:06:00 -07:00
|
|
|
|
local server = vim.uv.new_tcp()
|
2019-11-17 20:06:59 -07:00
|
|
|
|
server:bind(host, port)
|
|
|
|
|
server:listen(128, function(err)
|
|
|
|
|
assert(not err, err) -- Check for errors.
|
2023-06-03 03:06:00 -07:00
|
|
|
|
local sock = vim.uv.new_tcp()
|
2019-11-17 20:06:59 -07:00
|
|
|
|
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)
|
2022-05-11 09:23:46 -07:00
|
|
|
|
<
|
|
|
|
|
Multithreading *lua-loop-threading*
|
2022-02-26 03:03:39 -07:00
|
|
|
|
|
|
|
|
|
Plugins can perform work in separate (os-level) threads using the threading
|
2023-06-03 03:06:00 -07:00
|
|
|
|
APIs in luv, for instance `vim.uv.new_thread`. Note that every thread
|
2023-06-22 03:44:51 -07:00
|
|
|
|
gets its own separate Lua interpreter state, with no access to Lua globals
|
2022-02-26 03:03:39 -07:00
|
|
|
|
in the main thread. Neither can the state of the editor (buffers, windows,
|
|
|
|
|
etc) be directly accessed from threads.
|
|
|
|
|
|
|
|
|
|
A subset of the `vim.*` API is available in threads. This includes:
|
|
|
|
|
|
2023-06-03 03:06:00 -07:00
|
|
|
|
- `vim.uv` with a separate event loop per thread.
|
2022-02-26 03:03:39 -07:00
|
|
|
|
- `vim.mpack` and `vim.json` (useful for serializing messages between threads)
|
2023-06-22 03:44:51 -07:00
|
|
|
|
- `require` in threads can use Lua packages from the global |package.path|
|
2022-02-26 03:03:39 -07:00
|
|
|
|
- `print()` and `vim.inspect`
|
|
|
|
|
- `vim.diff`
|
2023-06-22 03:44:51 -07:00
|
|
|
|
- most utility functions in `vim.*` for working with pure Lua values
|
2022-02-26 03:03:39 -07:00
|
|
|
|
like `vim.split`, `vim.tbl_*`, `vim.list_*`, and so on.
|
|
|
|
|
- `vim.is_thread()` returns true from a non-main thread.
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
VIM.HIGHLIGHT *vim.highlight*
|
2020-05-18 06:49:50 -07:00
|
|
|
|
|
2024-04-30 04:30:21 -07:00
|
|
|
|
vim.highlight.on_yank({opts}) *vim.highlight.on_yank()*
|
|
|
|
|
Highlight the yanked text during a |TextYankPost| event.
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
2024-04-30 04:30:21 -07:00
|
|
|
|
Add the following to your `init.vim`: >vim
|
|
|
|
|
autocmd TextYankPost * silent! lua vim.highlight.on_yank {higroup='Visual', timeout=300}
|
2020-06-03 07:51:25 -07:00
|
|
|
|
<
|
2022-02-21 13:21:42 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {opts} (`table?`) Optional parameters
|
2023-07-15 08:55:32 -07:00
|
|
|
|
• higroup highlight group for yanked region (default
|
|
|
|
|
"IncSearch")
|
|
|
|
|
• 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)
|
|
|
|
|
• event event structure (default vim.v.event)
|
|
|
|
|
• priority integer priority (default
|
|
|
|
|
|vim.highlight.priorities|`.user`)
|
|
|
|
|
|
2023-07-18 07:42:30 -07:00
|
|
|
|
vim.highlight.priorities *vim.highlight.priorities*
|
|
|
|
|
Table with default priorities used for highlighting:
|
|
|
|
|
• `syntax`: `50`, used for standard syntax highlighting
|
2024-01-28 18:53:14 -07:00
|
|
|
|
• `treesitter`: `100`, used for treesitter-based highlighting
|
2023-07-18 07:42:30 -07:00
|
|
|
|
• `semantic_tokens`: `125`, used for LSP semantic token highlighting
|
|
|
|
|
• `diagnostics`: `150`, used for code analysis such as diagnostics
|
|
|
|
|
• `user`: `200`, used for user-triggered highlights such as LSP document
|
|
|
|
|
symbols or `on_yank` autocommands
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
*vim.highlight.range()*
|
|
|
|
|
vim.highlight.range({bufnr}, {ns}, {higroup}, {start}, {finish}, {opts})
|
|
|
|
|
Apply highlight group to range of text.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {bufnr} (`integer`) Buffer number to apply highlighting to
|
|
|
|
|
• {ns} (`integer`) Namespace to add highlight to
|
|
|
|
|
• {higroup} (`string`) Highlight group to use for highlighting
|
|
|
|
|
• {start} (`integer[]|string`) Start of region as a (line, column)
|
|
|
|
|
tuple or string accepted by |getpos()|
|
|
|
|
|
• {finish} (`integer[]|string`) End of region as a (line, column)
|
|
|
|
|
tuple or string accepted by |getpos()|
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {opts} (`table?`) A table with the following fields:
|
2024-05-25 05:37:33 -07:00
|
|
|
|
• {regtype}? (`string`, default: `'v'` i.e. charwise) Type
|
|
|
|
|
of range. See |getregtype()|
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {inclusive}? (`boolean`, default: `false`) Indicates
|
|
|
|
|
whether the range is end-inclusive
|
|
|
|
|
• {priority}? (`integer`, default:
|
|
|
|
|
`vim.highlight.priorities.user`) Indicates priority of
|
|
|
|
|
highlight
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
VIM.DIFF *vim.diff*
|
2021-08-22 04:52:56 -07:00
|
|
|
|
|
|
|
|
|
vim.diff({a}, {b}, {opts}) *vim.diff()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Run diff on strings {a} and {b}. Any indices returned by this function,
|
|
|
|
|
either directly or via callback arguments, are 1-based.
|
|
|
|
|
|
2022-11-22 10:41:00 -07:00
|
|
|
|
Examples: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.diff('a\n', 'b\nc\n')
|
|
|
|
|
-- =>
|
|
|
|
|
-- @@ -1 +1,2 @@
|
|
|
|
|
-- -a
|
|
|
|
|
-- +b
|
|
|
|
|
-- +c
|
2021-08-22 04:52:56 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.diff('a\n', 'b\nc\n', {result_type = 'indices'})
|
|
|
|
|
-- =>
|
|
|
|
|
-- {
|
|
|
|
|
-- {1, 1, 1, 2}
|
|
|
|
|
-- }
|
2023-07-15 08:55:32 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {a} (`string`) First string to compare
|
|
|
|
|
• {b} (`string`) Second string to compare
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• {opts} (`table`) Optional parameters:
|
|
|
|
|
• {on_hunk}
|
|
|
|
|
(`fun(start_a: integer, count_a: integer, start_b: integer, count_b: integer): integer`)
|
|
|
|
|
Invoked for each hunk in the diff. Return a negative number
|
|
|
|
|
to cancel the callback for any remaining hunks. Arguments:
|
|
|
|
|
• `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} (`'unified'|'indices'`, default: `'unified'`)
|
|
|
|
|
Form of the returned diff:
|
|
|
|
|
• `unified`: String in unified format.
|
|
|
|
|
• `indices`: Array of hunk locations. Note: This option is
|
2023-07-15 08:55:32 -07:00
|
|
|
|
ignored if `on_hunk` is used.
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• {linematch} (`boolean|integer`) Run linematch on the
|
2023-07-15 08:55:32 -07:00
|
|
|
|
resulting hunks from xdiff. When integer, only hunks upto
|
|
|
|
|
this size in lines are run through linematch. Requires
|
|
|
|
|
`result_type = indices`, ignored otherwise.
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• {algorithm} (`'myers'|'minimal'|'patience'|'histogram'`,
|
|
|
|
|
default: `'myers'`) Diff algorithm to use. Values:
|
|
|
|
|
• `myers`: the default algorithm
|
|
|
|
|
• `minimal`: spend extra time to generate the smallest
|
2023-07-15 08:55:32 -07:00
|
|
|
|
possible diff
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• `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
|
2023-07-15 08:55:32 -07:00
|
|
|
|
change
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• {ignore_whitespace_change_at_eol} (`boolean`) Ignore
|
2023-07-15 08:55:32 -07:00
|
|
|
|
whitespace change at end-of-line.
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• {ignore_cr_at_eol} (`boolean`) Ignore carriage return at
|
2023-07-15 08:55:32 -07:00
|
|
|
|
end-of-line
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• {ignore_blank_lines} (`boolean`) Ignore blank lines
|
|
|
|
|
• {indent_heuristic} (`boolean`) Use the indent heuristic for
|
2023-07-15 08:55:32 -07:00
|
|
|
|
the internal diff library.
|
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-04-26 06:07:47 -07:00
|
|
|
|
(`string|integer[]`) See {opts.result_type}. `nil` if {opts.on_hunk}
|
|
|
|
|
is given.
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
VIM.MPACK *vim.mpack*
|
2021-09-04 08:39:22 -07:00
|
|
|
|
|
2023-07-17 07:13:54 -07:00
|
|
|
|
This 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
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.mpack.decode({str}) *vim.mpack.decode()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Decodes (or "unpacks") the msgpack-encoded {str} to a Lua object.
|
2021-09-04 08:39:22 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`)
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Return: ~
|
|
|
|
|
(`any`)
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.mpack.encode({obj}) *vim.mpack.encode()*
|
|
|
|
|
Encodes (or "packs") Lua object {obj} as msgpack in a Lua string.
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Parameters: ~
|
|
|
|
|
• {obj} (`any`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`string`)
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
VIM.JSON *vim.json*
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
2023-07-17 07:13:54 -07:00
|
|
|
|
This module provides encoding and decoding of Lua objects to and from
|
|
|
|
|
JSON-encoded strings. Supports |vim.NIL| and |vim.empty_dict()|.
|
2022-12-27 05:22:33 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.json.decode({str}, {opts}) *vim.json.decode()*
|
2022-12-27 05:22:33 -07:00
|
|
|
|
Decodes (or "unpacks") the JSON-encoded {str} to a Lua object.
|
2023-07-15 08:55:32 -07:00
|
|
|
|
• Decodes JSON "null" as |vim.NIL| (controllable by {opts}, see below).
|
|
|
|
|
• Decodes empty object as |vim.empty_dict()|.
|
|
|
|
|
• Decodes empty array as `{}` (empty Lua table).
|
2023-06-21 01:10:32 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.print(vim.json.decode('{"bar":[],"foo":{},"zub":null}'))
|
|
|
|
|
-- { bar = {}, foo = vim.empty_dict(), zub = vim.NIL }
|
|
|
|
|
<
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
2023-06-21 01:10:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`) Stringified JSON data.
|
|
|
|
|
• {opts} (`table<string,any>?`) Options table with keys:
|
2023-09-14 06:23:01 -07:00
|
|
|
|
• luanil: (table) Table with keys:
|
|
|
|
|
• object: (boolean) When true, converts `null` in JSON
|
|
|
|
|
objects to Lua `nil` instead of |vim.NIL|.
|
|
|
|
|
• array: (boolean) When true, converts `null` in JSON arrays
|
|
|
|
|
to Lua `nil` instead of |vim.NIL|.
|
2022-12-27 05:22:33 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`any`)
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
|
|
|
|
vim.json.encode({obj}) *vim.json.encode()*
|
|
|
|
|
Encodes (or "packs") Lua object {obj} as JSON in a Lua string.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {obj} (`any`)
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`)
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
|
|
|
|
|
2023-10-31 07:15:32 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
VIM.BASE64 *vim.base64*
|
|
|
|
|
|
|
|
|
|
vim.base64.decode({str}) *vim.base64.decode()*
|
|
|
|
|
Decode a Base64 encoded string.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`) Base64 encoded string
|
2023-10-31 07:15:32 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`) Decoded string
|
2023-10-31 07:15:32 -07:00
|
|
|
|
|
|
|
|
|
vim.base64.encode({str}) *vim.base64.encode()*
|
|
|
|
|
Encode {str} using Base64.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`) String to encode
|
2023-10-31 07:15:32 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`) Encoded string
|
2023-10-31 07:15:32 -07:00
|
|
|
|
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
VIM.SPELL *vim.spell*
|
2021-12-25 12:36:56 -07:00
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
vim.spell.check({str}) *vim.spell.check()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Check {str} for spelling errors. Similar to the Vimscript function
|
|
|
|
|
|spellbadword()|.
|
2021-12-25 12:36:56 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Note: The behaviour of this function is dependent on: 'spelllang',
|
|
|
|
|
'spellfile', 'spellcapcheck' and 'spelloptions' which can all be local to
|
|
|
|
|
the buffer. Consider calling this with |nvim_buf_call()|.
|
2021-12-25 12:36:56 -07:00
|
|
|
|
|
2022-11-22 10:41:00 -07:00
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.spell.check("the quik brown fox")
|
|
|
|
|
-- =>
|
|
|
|
|
-- {
|
|
|
|
|
-- {'quik', 'bad', 5}
|
|
|
|
|
-- }
|
2021-12-25 12:36:56 -07:00
|
|
|
|
<
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`)
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-06-04 06:06:02 -07:00
|
|
|
|
(`[string, 'bad'|'rare'|'local'|'caps', integer][]`) List of tuples
|
|
|
|
|
with three items:
|
2023-07-15 08:55:32 -07:00
|
|
|
|
• The badly spelled word.
|
|
|
|
|
• The type of the spelling error: "bad" spelling mistake "rare" rare
|
|
|
|
|
word "local" word only valid in another region "caps" word should
|
|
|
|
|
start with Capital
|
|
|
|
|
• The position in {str} where the word begins.
|
2021-12-25 12:36:56 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
VIM *vim.builtin*
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
vim.api.{func}({...}) *vim.api*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Invokes Nvim |API| function {func} with arguments {...}.
|
2022-11-22 05:50:50 -07:00
|
|
|
|
Example: call the "nvim_get_current_line()" API function: >lua
|
2022-08-11 05:25:48 -07:00
|
|
|
|
print(tostring(vim.api.nvim_get_current_line()))
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
vim.NIL *vim.NIL*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
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
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
vim.type_idx *vim.type_idx*
|
2023-07-15 08:55:32 -07:00
|
|
|
|
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.
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
vim.val_idx *vim.val_idx*
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Value index for tables representing |Float|s. A table representing
|
|
|
|
|
floating-point value 1.0 looks like this: >lua
|
|
|
|
|
{
|
|
|
|
|
[vim.type_idx] = vim.types.float,
|
|
|
|
|
[vim.val_idx] = 1.0,
|
|
|
|
|
}
|
|
|
|
|
< See also |vim.type_idx| and |lua-special-tbl|.
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
vim.types *vim.types*
|
2023-07-15 08:55:32 -07:00
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
*log_levels* *vim.log.levels*
|
|
|
|
|
Log levels are one of the values defined in `vim.log.levels`:
|
|
|
|
|
|
|
|
|
|
vim.log.levels.DEBUG
|
|
|
|
|
vim.log.levels.ERROR
|
|
|
|
|
vim.log.levels.INFO
|
|
|
|
|
vim.log.levels.TRACE
|
|
|
|
|
vim.log.levels.WARN
|
|
|
|
|
vim.log.levels.OFF
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|
|
|
|
|
|
2021-09-14 10:20:33 -07:00
|
|
|
|
vim.empty_dict() *vim.empty_dict()*
|
2023-07-22 00:42:25 -07:00
|
|
|
|
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
|
|
|
|
|
2022-08-11 05:25:48 -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
|
|
|
|
|
2024-02-15 03:53:51 -07:00
|
|
|
|
Return: ~
|
|
|
|
|
(`table`)
|
|
|
|
|
|
2024-04-26 06:07:47 -07:00
|
|
|
|
vim.iconv({str}, {from}, {to}) *vim.iconv()*
|
2023-07-15 08:55:32 -07:00
|
|
|
|
The result is a String, which is the text {str} converted from encoding
|
|
|
|
|
{from} to encoding {to}. When the conversion fails `nil` is returned. When
|
|
|
|
|
some characters could not be converted they are replaced with "?". The
|
|
|
|
|
encoding names are whatever the iconv() library function can accept, see
|
|
|
|
|
":Man 3 iconv".
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`) Text to convert
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• {from} (`string`) Encoding of {str}
|
|
|
|
|
• {to} (`string`) Target encoding
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string?`) Converted string if conversion succeeds, `nil` otherwise.
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
|
|
|
|
vim.in_fast_event() *vim.in_fast_event()*
|
|
|
|
|
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|).
|
|
|
|
|
|
2024-03-07 01:16:05 -07:00
|
|
|
|
vim.rpcnotify({channel}, {method}, {...}) *vim.rpcnotify()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
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
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
This function also works in a fast callback |lua-loop-callbacks|.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {channel} (`integer`)
|
|
|
|
|
• {method} (`string`)
|
|
|
|
|
• {...} (`any?`)
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
2024-03-07 01:16:05 -07:00
|
|
|
|
vim.rpcrequest({channel}, {method}, {...}) *vim.rpcrequest()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
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
|
|
|
|
|
2022-08-11 05:25:48 -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
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {channel} (`integer`)
|
|
|
|
|
• {method} (`string`)
|
|
|
|
|
• {...} (`any?`)
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-09-21 05:34:56 -07:00
|
|
|
|
vim.schedule({fn}) *vim.schedule()*
|
|
|
|
|
Schedules {fn} to be invoked soon by the main event-loop. Useful to avoid
|
|
|
|
|
|textlock| or other temporary restrictions.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• {fn} (`fun()`)
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.str_byteindex({str}, {index}, {use_utf16}) *vim.str_byteindex()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
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.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2024-03-27 18:32:32 -07:00
|
|
|
|
Invalid UTF-8 and NUL is treated like in |vim.str_utfindex()|. An {index}
|
2023-07-15 08:55:32 -07:00
|
|
|
|
in the middle of a UTF-16 sequence is rounded upwards to the end of that
|
|
|
|
|
sequence.
|
2022-08-24 06:41:31 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`)
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• {index} (`integer`)
|
|
|
|
|
• {use_utf16} (`boolean?`)
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2024-06-11 04:05:18 -07:00
|
|
|
|
Return: ~
|
|
|
|
|
(`integer`)
|
|
|
|
|
|
2023-07-29 07:08:32 -07:00
|
|
|
|
vim.str_utf_end({str}, {index}) *vim.str_utf_end()*
|
|
|
|
|
Gets the distance (in bytes) from the last byte of the codepoint
|
|
|
|
|
(character) that {index} points to.
|
|
|
|
|
|
|
|
|
|
Examples: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- The character 'æ' is stored as the bytes '\xc3\xa6' (using UTF-8)
|
2023-07-29 07:08:32 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- Returns 0 because the index is pointing at the last byte of a character
|
|
|
|
|
vim.str_utf_end('æ', 2)
|
2023-07-29 07:08:32 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- Returns 1 because the index is pointing at the penultimate byte of a character
|
|
|
|
|
vim.str_utf_end('æ', 1)
|
2023-07-29 07:08:32 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`)
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• {index} (`integer`)
|
2023-07-29 07:08:32 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-04-26 06:07:47 -07:00
|
|
|
|
(`integer`)
|
2023-07-29 07:08:32 -07:00
|
|
|
|
|
|
|
|
|
vim.str_utf_pos({str}) *vim.str_utf_pos()*
|
|
|
|
|
Gets a list of the starting byte positions of each UTF-8 codepoint in the
|
|
|
|
|
given string.
|
|
|
|
|
|
|
|
|
|
Embedded NUL bytes are treated as terminating the string.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`)
|
2023-07-29 07:08:32 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-04-26 06:07:47 -07:00
|
|
|
|
(`integer[]`)
|
2023-07-29 07:08:32 -07:00
|
|
|
|
|
|
|
|
|
vim.str_utf_start({str}, {index}) *vim.str_utf_start()*
|
|
|
|
|
Gets the distance (in bytes) from the starting byte of the codepoint
|
|
|
|
|
(character) that {index} points to.
|
|
|
|
|
|
|
|
|
|
The result can be added to {index} to get the starting byte of a
|
|
|
|
|
character.
|
|
|
|
|
|
|
|
|
|
Examples: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- The character 'æ' is stored as the bytes '\xc3\xa6' (using UTF-8)
|
2023-07-29 07:08:32 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- Returns 0 because the index is pointing at the first byte of a character
|
|
|
|
|
vim.str_utf_start('æ', 1)
|
2023-07-29 07:08:32 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- Returns -1 because the index is pointing at the second byte of a character
|
|
|
|
|
vim.str_utf_start('æ', 2)
|
2023-07-29 07:08:32 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`)
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• {index} (`integer`)
|
2023-07-29 07:08:32 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-04-26 06:07:47 -07:00
|
|
|
|
(`integer`)
|
2023-07-29 07:08:32 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.str_utfindex({str}, {index}) *vim.str_utfindex()*
|
|
|
|
|
Convert byte index to UTF-32 and UTF-16 indices. If {index} is not
|
|
|
|
|
supplied, the length of the string is used. All indices are zero-based.
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
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.
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
2020-10-06 09:58:05 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`)
|
2024-04-26 06:07:47 -07:00
|
|
|
|
• {index} (`integer?`)
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Return (multiple): ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`integer`) UTF-32 index
|
|
|
|
|
(`integer`) UTF-16 index
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.stricmp({a}, {b}) *vim.stricmp()*
|
|
|
|
|
Compares strings case-insensitively.
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {a} (`string`)
|
|
|
|
|
• {b} (`string`)
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`0|1|-1`) if strings are equal, {a} is greater than {b} or {a} is
|
|
|
|
|
lesser than {b}, respectively.
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.ui_attach({ns}, {options}, {callback}) *vim.ui_attach()*
|
|
|
|
|
Attach to ui events, similar to |nvim_ui_attach()| but receive events as
|
|
|
|
|
Lua callback. Can be used to implement screen elements like popupmenu or
|
|
|
|
|
message handling in Lua.
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
{options} should be a dictionary-like table, where `ext_...` options
|
|
|
|
|
should be set to true to receive events for the respective external
|
|
|
|
|
element.
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
{callback} receives event name plus additional parameters. See
|
|
|
|
|
|ui-popupmenu| and the sections below for event format for respective
|
|
|
|
|
events.
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
WARNING: This api is considered experimental. Usability will vary for
|
|
|
|
|
different screen elements. In particular `ext_messages` behavior is
|
|
|
|
|
subject to further changes and usability improvements. This is expected to
|
|
|
|
|
be used to handle messages when setting 'cmdheight' to zero (which is
|
|
|
|
|
likewise experimental).
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Example (stub for a |ui-popupmenu| implementation): >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
ns = vim.api.nvim_create_namespace('my_fancy_pum')
|
|
|
|
|
|
|
|
|
|
vim.ui_attach(ns, {ext_popupmenu=true}, function(event, ...)
|
|
|
|
|
if event == "popupmenu_show" then
|
|
|
|
|
local items, selected, row, col, grid = ...
|
|
|
|
|
print("display pum ", #items)
|
|
|
|
|
elseif event == "popupmenu_select" then
|
|
|
|
|
local selected = ...
|
|
|
|
|
print("selected", selected)
|
|
|
|
|
elseif event == "popupmenu_hide" then
|
|
|
|
|
print("FIN")
|
|
|
|
|
end
|
|
|
|
|
end)
|
2020-05-20 08:08:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {ns} (`integer`)
|
|
|
|
|
• {options} (`table<string, any>`)
|
|
|
|
|
• {callback} (`fun()`)
|
2022-06-30 00:26:31 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.ui_detach({ns}) *vim.ui_detach()*
|
|
|
|
|
Detach a callback previously attached with |vim.ui_attach()| for the given
|
|
|
|
|
namespace {ns}.
|
2022-06-30 00:26:31 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {ns} (`integer`)
|
2022-06-30 00:26:31 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.wait({time}, {callback}, {interval}, {fast_only}) *vim.wait()*
|
|
|
|
|
Wait for {time} in milliseconds until {callback} returns `true`.
|
2022-09-28 07:16:02 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Executes {callback} immediately and at approximately {interval}
|
|
|
|
|
milliseconds (default 200). Nvim still processes other events during this
|
|
|
|
|
time.
|
2022-06-30 00:26:31 -07:00
|
|
|
|
|
2023-11-21 04:24:30 -07:00
|
|
|
|
Cannot be called while in an |api-fast| event.
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Examples: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
---
|
|
|
|
|
-- Wait for 100 ms, allowing other events to process
|
|
|
|
|
vim.wait(100, function() end)
|
2022-06-30 00:26:31 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
---
|
|
|
|
|
-- Wait for 100 ms or until global variable set.
|
|
|
|
|
vim.wait(100, function() return vim.g.waiting_for_var end)
|
2022-06-30 00:26:31 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
---
|
|
|
|
|
-- 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)
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
---
|
|
|
|
|
-- Schedule a function to set a value in 100ms
|
|
|
|
|
vim.defer_fn(function() vim.g.timer_result = true end, 100)
|
2021-09-14 10:20:33 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- 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
|
2023-07-15 08:55:32 -07:00
|
|
|
|
<
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {time} (`integer`) Number of milliseconds to wait
|
|
|
|
|
• {callback} (`fun(): boolean?`) Optional callback. Waits until
|
2023-07-15 08:55:32 -07:00
|
|
|
|
{callback} returns true
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {interval} (`integer?`) (Approximate) number of milliseconds to wait
|
|
|
|
|
between polls
|
|
|
|
|
• {fast_only} (`boolean?`) If true, only |api-fast| events will be
|
2023-11-21 04:24:30 -07:00
|
|
|
|
processed.
|
2021-12-28 10:15:16 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Return (multiple): ~
|
|
|
|
|
(`boolean`)
|
|
|
|
|
(`-1|-2?`)
|
2023-07-15 08:55:32 -07:00
|
|
|
|
• 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.
|
2021-12-28 10:15:16 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2022-05-11 09:23:46 -07:00
|
|
|
|
LUA-VIMSCRIPT BRIDGE *lua-vimscript*
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2023-06-19 08:40:33 -07:00
|
|
|
|
Nvim Lua provides an interface or "bridge" to Vimscript variables and
|
|
|
|
|
functions, and editor commands and options.
|
|
|
|
|
|
|
|
|
|
Objects passed over this bridge are COPIED (marshalled): there are no
|
2024-02-15 10:16:04 -07:00
|
|
|
|
"references". |lua-guide-variables| For example, using `vim.fn.remove()` on a
|
|
|
|
|
Lua list copies the list object to Vimscript and does NOT modify the Lua list: >lua
|
2023-06-19 08:40:33 -07:00
|
|
|
|
local list = { 1, 2, 3 }
|
|
|
|
|
vim.fn.remove(list, 0)
|
|
|
|
|
vim.print(list) --> "{ 1, 2, 3 }"
|
2023-07-15 08:55:32 -07:00
|
|
|
|
<
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2024-03-09 05:21:01 -07:00
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
vim.call({func}, {...}) *vim.call()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Invokes |vim-function| or |user-function| {func} with arguments {...}.
|
|
|
|
|
See also |vim.fn|.
|
2022-11-22 05:50:50 -07:00
|
|
|
|
Equivalent to: >lua
|
2022-08-11 05:25:48 -07:00
|
|
|
|
vim.fn[func]({...})
|
2023-07-15 08:55:32 -07:00
|
|
|
|
<
|
2022-05-12 06:34:38 -07:00
|
|
|
|
vim.cmd({command})
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See |vim.cmd()|.
|
2022-05-12 06:34:38 -07:00
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
vim.fn.{func}({...}) *vim.fn*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Invokes |vim-function| or |user-function| {func} with arguments {...}.
|
2022-11-22 05:50:50 -07:00
|
|
|
|
To call autoload functions, use the syntax: >lua
|
2022-08-11 05:25:48 -07:00
|
|
|
|
vim.fn['some#function']({...})
|
2020-05-17 20:29:34 -07:00
|
|
|
|
<
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Unlike vim.api.|nvim_call_function()| this converts directly between Vim
|
|
|
|
|
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
|
|
|
|
|
2022-08-11 05:25:48 -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
|
|
|
|
|
2022-08-11 05:25:48 -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
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Note: The majority of functions cannot run in |api-fast| callbacks with some
|
|
|
|
|
undocumented exceptions which are allowed.
|
2020-05-17 20:29:34 -07:00
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
*lua-vim-variables*
|
2020-08-31 00:51:35 -07:00
|
|
|
|
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
|
|
|
|
|
2022-11-22 05:50:50 -07:00
|
|
|
|
Example: >lua
|
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
|
2022-05-11 09:23:46 -07:00
|
|
|
|
<
|
2022-09-22 06:17:49 -07:00
|
|
|
|
|
|
|
|
|
Note that setting dictionary fields directly will not write them back into
|
|
|
|
|
Nvim. This is because the index into the namespace simply returns a copy.
|
|
|
|
|
Instead the whole dictionary must be written as one. This can be achieved by
|
|
|
|
|
creating a short-lived temporary.
|
|
|
|
|
|
2022-11-22 05:50:50 -07:00
|
|
|
|
Example: >lua
|
2022-09-22 06:17:49 -07:00
|
|
|
|
|
|
|
|
|
vim.g.my_dict.field1 = 'value' -- Does not work
|
|
|
|
|
|
|
|
|
|
local my_dict = vim.g.my_dict --
|
|
|
|
|
my_dict.field1 = 'value' -- Instead do
|
|
|
|
|
vim.g.my_dict = my_dict --
|
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
vim.g *vim.g*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Global (|g:|) editor variables.
|
|
|
|
|
Key with no value returns `nil`.
|
2020-08-31 00:51:35 -07:00
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
vim.b *vim.b*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Buffer-scoped (|b:|) variables for the current buffer.
|
|
|
|
|
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
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
vim.w *vim.w*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Window-scoped (|w:|) variables for the current window.
|
|
|
|
|
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
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
vim.t *vim.t*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Tabpage-scoped (|t:|) variables for the current tabpage.
|
|
|
|
|
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
|
|
|
|
|
2022-05-11 09:23:46 -07:00
|
|
|
|
vim.v *vim.v*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|v:| variables.
|
|
|
|
|
Invalid or unset key returns `nil`.
|
2020-08-31 00:51:35 -07:00
|
|
|
|
|
2024-03-09 05:21:01 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
*lua-options*
|
2021-05-28 08:24:48 -07:00
|
|
|
|
*lua-vim-options*
|
|
|
|
|
*lua-vim-set*
|
|
|
|
|
*lua-vim-setlocal*
|
|
|
|
|
|
2022-08-30 05:46:38 -07:00
|
|
|
|
Vim options can be accessed through |vim.o|, which behaves like Vimscript
|
|
|
|
|
|:set|.
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
|
|
|
|
Examples: ~
|
|
|
|
|
|
|
|
|
|
To set a boolean toggle:
|
2022-09-22 06:17:49 -07:00
|
|
|
|
Vimscript: `set number`
|
|
|
|
|
Lua: `vim.o.number = true`
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
2022-08-30 05:46:38 -07:00
|
|
|
|
To set a string value:
|
2022-09-22 06:17:49 -07:00
|
|
|
|
Vimscript: `set wildignore=*.o,*.a,__pycache__`
|
|
|
|
|
Lua: `vim.o.wildignore = '*.o,*.a,__pycache__'`
|
2022-08-30 05:46:38 -07:00
|
|
|
|
|
2022-09-22 06:17:49 -07:00
|
|
|
|
Similarly, there is |vim.bo| and |vim.wo| for setting buffer-scoped and
|
|
|
|
|
window-scoped options. Note that this must NOT be confused with
|
|
|
|
|
|local-options| and |:setlocal|. There is also |vim.go| that only accesses the
|
|
|
|
|
global value of a |global-local| option, see |:setglobal|.
|
2022-08-30 05:46:38 -07:00
|
|
|
|
|
2024-03-09 05:21:01 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
*vim.opt_local*
|
|
|
|
|
*vim.opt_global*
|
|
|
|
|
*vim.opt*
|
|
|
|
|
|
2022-08-30 05:46:38 -07:00
|
|
|
|
|
|
|
|
|
A special interface |vim.opt| exists for conveniently interacting with list-
|
|
|
|
|
and map-style option from Lua: It allows accessing them as Lua tables and
|
|
|
|
|
offers object-oriented method for adding and removing entries.
|
|
|
|
|
|
|
|
|
|
Examples: ~
|
|
|
|
|
|
|
|
|
|
The following methods of setting a list-style option are equivalent:
|
2022-11-22 10:41:00 -07:00
|
|
|
|
In Vimscript: >vim
|
|
|
|
|
set wildignore=*.o,*.a,__pycache__
|
|
|
|
|
<
|
|
|
|
|
In Lua using `vim.o`: >lua
|
|
|
|
|
vim.o.wildignore = '*.o,*.a,__pycache__'
|
|
|
|
|
<
|
|
|
|
|
In Lua using `vim.opt`: >lua
|
|
|
|
|
vim.opt.wildignore = { '*.o', '*.a', '__pycache__' }
|
|
|
|
|
<
|
|
|
|
|
To replicate the behavior of |:set+=|, use: >lua
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
|
|
|
|
vim.opt.wildignore:append { "*.pyc", "node_modules" }
|
|
|
|
|
<
|
2022-11-22 10:41:00 -07:00
|
|
|
|
To replicate the behavior of |:set^=|, use: >lua
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
|
|
|
|
vim.opt.wildignore:prepend { "new_first_value" }
|
|
|
|
|
<
|
2022-11-22 10:41:00 -07:00
|
|
|
|
To replicate the behavior of |:set-=|, use: >lua
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
|
|
|
|
vim.opt.wildignore:remove { "node_modules" }
|
|
|
|
|
<
|
2022-08-30 05:46:38 -07:00
|
|
|
|
The following methods of setting a map-style option are equivalent:
|
2022-11-22 10:41:00 -07:00
|
|
|
|
In Vimscript: >vim
|
|
|
|
|
set listchars=space:_,tab:>~
|
|
|
|
|
<
|
|
|
|
|
In Lua using `vim.o`: >lua
|
|
|
|
|
vim.o.listchars = 'space:_,tab:>~'
|
|
|
|
|
<
|
|
|
|
|
In Lua using `vim.opt`: >lua
|
|
|
|
|
vim.opt.listchars = { space = '_', tab = '>~' }
|
|
|
|
|
<
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
2022-08-30 05:46:38 -07:00
|
|
|
|
Note that |vim.opt| returns an `Option` object, not the value of the option,
|
2022-09-25 16:58:27 -07:00
|
|
|
|
which is accessed through |vim.opt:get()|:
|
2022-08-30 05:46:38 -07:00
|
|
|
|
|
|
|
|
|
Examples: ~
|
|
|
|
|
|
|
|
|
|
The following methods of getting a list-style option are equivalent:
|
2022-11-22 10:41:00 -07:00
|
|
|
|
In Vimscript: >vim
|
|
|
|
|
echo wildignore
|
|
|
|
|
<
|
|
|
|
|
In Lua using `vim.o`: >lua
|
|
|
|
|
print(vim.o.wildignore)
|
|
|
|
|
<
|
|
|
|
|
In Lua using `vim.opt`: >lua
|
refactor!: rename vim.pretty_print => vim.print
Problem:
The function name `vim.pretty_print`:
1. is verbose, which partially defeats its purpose as sugar
2. does not draw from existing precedent or any sort of convention
(except external projects like penlight or python?), which reduces
discoverability, and degrades signaling about best practices.
Solution:
- Rename to `vim.print`.
- Change the behavior so that
1. strings are printed without quotes
2. each arg is printed on its own line
3. tables are indented with 2 instead of 4 spaces
- Example:
:lua ='a', 'b', 42, {a=3}
a
b
42
{
a = 3
}
Comparison of alternatives:
- `vim.print`:
- pro: consistent with Lua's `print()`
- pro: aligns with potential `nvim_print` API function which will
replace nvim_echo, nvim_notify, etc.
- con: behaves differently than Lua's `print()`, slightly misleading?
- `vim.echo`:
- pro: `:echo` has similar "pretty print" behavior.
- con: inconsistent with Lua idioms.
- `vim.p`:
- pro: very short, fits with `vim.o`, etc.
- con: not as discoverable as "echo"
- con: less opportunity for `local p = vim.p` because of potential shadowing.
2023-03-07 08:04:57 -07:00
|
|
|
|
vim.print(vim.opt.wildignore:get())
|
2022-11-22 10:41:00 -07:00
|
|
|
|
<
|
2022-08-30 05:46:38 -07:00
|
|
|
|
|
2022-09-25 16:58:27 -07:00
|
|
|
|
In any of the above examples, to replicate the behavior |:setlocal|, use
|
|
|
|
|
`vim.opt_local`. Additionally, to replicate the behavior of |:setglobal|, use
|
2021-05-28 08:24:48 -07:00
|
|
|
|
`vim.opt_global`.
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Option:append({value}) *vim.opt:append()*
|
|
|
|
|
Append a value to string-style options. See |:set+=|
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
These are equivalent: >lua
|
|
|
|
|
vim.opt.formatoptions:append('j')
|
|
|
|
|
vim.opt.formatoptions = vim.opt.formatoptions + 'j'
|
|
|
|
|
<
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {value} (`string`) Value to append
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Option:get() *vim.opt:get()*
|
2023-06-22 03:44:51 -07:00
|
|
|
|
Returns a Lua-representation of the option. Boolean, number and string
|
2021-05-28 08:24:48 -07:00
|
|
|
|
values will be returned in exactly the same fashion.
|
|
|
|
|
|
|
|
|
|
For values that are comma-separated lists, an array will be returned with
|
2022-11-22 05:50:50 -07:00
|
|
|
|
the values as entries in the array: >lua
|
2021-05-28 08:24:48 -07:00
|
|
|
|
vim.cmd [[set wildignore=*.pyc,*.o]]
|
|
|
|
|
|
refactor!: rename vim.pretty_print => vim.print
Problem:
The function name `vim.pretty_print`:
1. is verbose, which partially defeats its purpose as sugar
2. does not draw from existing precedent or any sort of convention
(except external projects like penlight or python?), which reduces
discoverability, and degrades signaling about best practices.
Solution:
- Rename to `vim.print`.
- Change the behavior so that
1. strings are printed without quotes
2. each arg is printed on its own line
3. tables are indented with 2 instead of 4 spaces
- Example:
:lua ='a', 'b', 42, {a=3}
a
b
42
{
a = 3
}
Comparison of alternatives:
- `vim.print`:
- pro: consistent with Lua's `print()`
- pro: aligns with potential `nvim_print` API function which will
replace nvim_echo, nvim_notify, etc.
- con: behaves differently than Lua's `print()`, slightly misleading?
- `vim.echo`:
- pro: `:echo` has similar "pretty print" behavior.
- con: inconsistent with Lua idioms.
- `vim.p`:
- pro: very short, fits with `vim.o`, etc.
- con: not as discoverable as "echo"
- con: less opportunity for `local p = vim.p` because of potential shadowing.
2023-03-07 08:04:57 -07:00
|
|
|
|
vim.print(vim.opt.wildignore:get())
|
2021-05-28 08:24:48 -07:00
|
|
|
|
-- { "*.pyc", "*.o", }
|
|
|
|
|
|
|
|
|
|
for _, ignore_pattern in ipairs(vim.opt.wildignore:get()) do
|
|
|
|
|
print("Will ignore:", ignore_pattern)
|
|
|
|
|
end
|
|
|
|
|
-- Will ignore: *.pyc
|
|
|
|
|
-- Will ignore: *.o
|
|
|
|
|
<
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
2021-05-28 08:24:48 -07:00
|
|
|
|
For values that are comma-separated maps, a table will be returned with
|
2022-11-22 05:50:50 -07:00
|
|
|
|
the names as keys and the values as entries: >lua
|
2021-05-28 08:24:48 -07:00
|
|
|
|
vim.cmd [[set listchars=space:_,tab:>~]]
|
|
|
|
|
|
refactor!: rename vim.pretty_print => vim.print
Problem:
The function name `vim.pretty_print`:
1. is verbose, which partially defeats its purpose as sugar
2. does not draw from existing precedent or any sort of convention
(except external projects like penlight or python?), which reduces
discoverability, and degrades signaling about best practices.
Solution:
- Rename to `vim.print`.
- Change the behavior so that
1. strings are printed without quotes
2. each arg is printed on its own line
3. tables are indented with 2 instead of 4 spaces
- Example:
:lua ='a', 'b', 42, {a=3}
a
b
42
{
a = 3
}
Comparison of alternatives:
- `vim.print`:
- pro: consistent with Lua's `print()`
- pro: aligns with potential `nvim_print` API function which will
replace nvim_echo, nvim_notify, etc.
- con: behaves differently than Lua's `print()`, slightly misleading?
- `vim.echo`:
- pro: `:echo` has similar "pretty print" behavior.
- con: inconsistent with Lua idioms.
- `vim.p`:
- pro: very short, fits with `vim.o`, etc.
- con: not as discoverable as "echo"
- con: less opportunity for `local p = vim.p` because of potential shadowing.
2023-03-07 08:04:57 -07:00
|
|
|
|
vim.print(vim.opt.listchars:get())
|
2021-05-28 08:24:48 -07:00
|
|
|
|
-- { space = "_", tab = ">~", }
|
|
|
|
|
|
|
|
|
|
for char, representation in pairs(vim.opt.listchars:get()) do
|
2022-10-09 09:19:43 -07:00
|
|
|
|
print(char, "=>", representation)
|
2021-05-28 08:24:48 -07:00
|
|
|
|
end
|
|
|
|
|
<
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
2021-05-28 08:24:48 -07:00
|
|
|
|
For values that are lists of flags, a set will be returned with the flags
|
2022-11-22 05:50:50 -07:00
|
|
|
|
as keys and `true` as entries. >lua
|
2021-05-28 08:24:48 -07:00
|
|
|
|
vim.cmd [[set formatoptions=njtcroql]]
|
|
|
|
|
|
refactor!: rename vim.pretty_print => vim.print
Problem:
The function name `vim.pretty_print`:
1. is verbose, which partially defeats its purpose as sugar
2. does not draw from existing precedent or any sort of convention
(except external projects like penlight or python?), which reduces
discoverability, and degrades signaling about best practices.
Solution:
- Rename to `vim.print`.
- Change the behavior so that
1. strings are printed without quotes
2. each arg is printed on its own line
3. tables are indented with 2 instead of 4 spaces
- Example:
:lua ='a', 'b', 42, {a=3}
a
b
42
{
a = 3
}
Comparison of alternatives:
- `vim.print`:
- pro: consistent with Lua's `print()`
- pro: aligns with potential `nvim_print` API function which will
replace nvim_echo, nvim_notify, etc.
- con: behaves differently than Lua's `print()`, slightly misleading?
- `vim.echo`:
- pro: `:echo` has similar "pretty print" behavior.
- con: inconsistent with Lua idioms.
- `vim.p`:
- pro: very short, fits with `vim.o`, etc.
- con: not as discoverable as "echo"
- con: less opportunity for `local p = vim.p` because of potential shadowing.
2023-03-07 08:04:57 -07:00
|
|
|
|
vim.print(vim.opt.formatoptions:get())
|
2021-05-28 08:24:48 -07:00
|
|
|
|
-- { n = true, j = true, c = true, ... }
|
|
|
|
|
|
|
|
|
|
local format_opts = vim.opt.formatoptions:get()
|
|
|
|
|
if format_opts.j then
|
|
|
|
|
print("J is enabled!")
|
|
|
|
|
end
|
|
|
|
|
<
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string|integer|boolean?`) value of option
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Option:prepend({value}) *vim.opt:prepend()*
|
2021-05-28 08:24:48 -07:00
|
|
|
|
Prepend a value to string-style options. See |:set^=|
|
|
|
|
|
|
2022-11-22 10:41:00 -07:00
|
|
|
|
These are equivalent: >lua
|
|
|
|
|
vim.opt.wildignore:prepend('*.o')
|
|
|
|
|
vim.opt.wildignore = vim.opt.wildignore ^ '*.o'
|
|
|
|
|
<
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {value} (`string`) Value to prepend
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
|
|
|
|
Option:remove({value}) *vim.opt:remove()*
|
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
|
|
|
|
|
2022-11-22 10:41:00 -07:00
|
|
|
|
These are equivalent: >lua
|
|
|
|
|
vim.opt.wildignore:remove('*.pyc')
|
|
|
|
|
vim.opt.wildignore = vim.opt.wildignore - '*.pyc'
|
|
|
|
|
<
|
2021-05-28 08:24:48 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {value} (`string`) Value to remove
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
2024-05-15 04:39:35 -07:00
|
|
|
|
vim.bo[{bufnr}] *vim.bo*
|
2024-03-03 16:33:09 -07:00
|
|
|
|
Get or set buffer-scoped |options| for the buffer with number {bufnr}. If
|
2024-03-08 05:25:18 -07:00
|
|
|
|
{bufnr} is omitted then the current buffer is used. Invalid {bufnr} or key
|
|
|
|
|
is an error.
|
2023-07-18 07:42:30 -07:00
|
|
|
|
|
2024-03-03 16:33:09 -07:00
|
|
|
|
Note: this is equivalent to `:setlocal` for |global-local| options and
|
|
|
|
|
`:set` otherwise.
|
2023-07-18 07:42:30 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
|
|
|
|
vim.bo[bufnr].buflisted = true -- same as vim.bo.buflisted = true
|
|
|
|
|
print(vim.bo.comments)
|
|
|
|
|
print(vim.bo.baz) -- error: invalid key
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
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
|
2023-09-14 06:23:01 -07:00
|
|
|
|
returns `nil`.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
2023-07-18 07:42:30 -07:00
|
|
|
|
vim.env.FOO = 'bar'
|
|
|
|
|
print(vim.env.TERM)
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
vim.go *vim.go*
|
|
|
|
|
Get or set global |options|. Like `:setglobal`. Invalid key is an error.
|
|
|
|
|
|
|
|
|
|
Note: this is different from |vim.o| because this accesses the global
|
|
|
|
|
option value and thus is mostly useful for use with |global-local|
|
|
|
|
|
options.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
vim.go.cmdheight = 4
|
|
|
|
|
print(vim.go.columns)
|
|
|
|
|
print(vim.go.bar) -- error: invalid key
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
vim.o *vim.o*
|
|
|
|
|
Get or set |options|. Like `:set`. Invalid key is an error.
|
|
|
|
|
|
|
|
|
|
Note: this works on both buffer-scoped and window-scoped options using the
|
|
|
|
|
current buffer and window.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
vim.o.cmdheight = 4
|
|
|
|
|
print(vim.o.columns)
|
|
|
|
|
print(vim.o.foo) -- error: invalid key
|
|
|
|
|
<
|
|
|
|
|
|
2024-05-15 04:39:35 -07:00
|
|
|
|
vim.wo[{winid}][{bufnr}] *vim.wo*
|
2023-07-18 07:42:30 -07:00
|
|
|
|
Get or set window-scoped |options| for the window with handle {winid} and
|
2024-03-03 16:33:09 -07:00
|
|
|
|
buffer with number {bufnr}. Like `:setlocal` if setting a |global-local|
|
2024-03-08 05:25:18 -07:00
|
|
|
|
option or if {bufnr} is provided, like `:set` otherwise. If {winid} is
|
2024-03-03 16:33:09 -07:00
|
|
|
|
omitted then the current window is used. Invalid {winid}, {bufnr} or key
|
|
|
|
|
is an error.
|
2023-07-18 07:42:30 -07:00
|
|
|
|
|
|
|
|
|
Note: only {bufnr} with value `0` (the current buffer in the window) is
|
|
|
|
|
supported.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
local winid = vim.api.nvim_get_current_win()
|
|
|
|
|
vim.wo[winid].number = true -- same as vim.wo.number = true
|
|
|
|
|
print(vim.wo.foldmarker)
|
|
|
|
|
print(vim.wo.quux) -- error: invalid key
|
|
|
|
|
vim.wo[winid][0].spell = false -- like ':setlocal nospell'
|
|
|
|
|
<
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
Lua module: vim *lua-vim*
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
vim.cmd({command}) *vim.cmd()*
|
2023-11-02 16:22:02 -07:00
|
|
|
|
Executes Vim script commands.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
|
|
|
|
Note that `vim.cmd` can be indexed with a command name to return a
|
|
|
|
|
callable function to the command.
|
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.cmd('echo 42')
|
|
|
|
|
vim.cmd([[
|
|
|
|
|
augroup My_group
|
|
|
|
|
autocmd!
|
|
|
|
|
autocmd FileType c setlocal cindent
|
|
|
|
|
augroup END
|
|
|
|
|
]])
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- Ex command :echo "foo"
|
|
|
|
|
-- Note string literals need to be double quoted.
|
|
|
|
|
vim.cmd('echo "foo"')
|
|
|
|
|
vim.cmd { cmd = 'echo', args = { '"foo"' } }
|
|
|
|
|
vim.cmd.echo({ args = { '"foo"' } })
|
|
|
|
|
vim.cmd.echo('"foo"')
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- Ex command :write! myfile.txt
|
|
|
|
|
vim.cmd('write! myfile.txt')
|
|
|
|
|
vim.cmd { cmd = 'write', args = { "myfile.txt" }, bang = true }
|
|
|
|
|
vim.cmd.write { args = { "myfile.txt" }, bang = true }
|
|
|
|
|
vim.cmd.write { "myfile.txt", bang = true }
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- Ex command :colorscheme blue
|
|
|
|
|
vim.cmd('colorscheme blue')
|
|
|
|
|
vim.cmd.colorscheme('blue')
|
2022-05-12 06:34:38 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {command} (`string|table`) Command(s) to execute. If a string,
|
|
|
|
|
executes multiple lines of Vim script at once. In this
|
|
|
|
|
case, it is an alias to |nvim_exec2()|, where `opts.output`
|
|
|
|
|
is set to false. Thus it works identical to |:source|. If a
|
|
|
|
|
table, executes a single command. In this case, it is an
|
|
|
|
|
alias to |nvim_cmd()| where `opts` is empty.
|
2022-05-12 06:34:38 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• |ex-cmd-index|
|
2022-05-12 06:34:38 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.defer_fn({fn}, {timeout}) *vim.defer_fn()*
|
2023-06-12 05:08:08 -07:00
|
|
|
|
Defers calling {fn} until {timeout} ms passes.
|
2021-05-31 10:47:51 -07:00
|
|
|
|
|
2023-06-12 05:08:08 -07:00
|
|
|
|
Use to do a one-shot timer that calls {fn} Note: The {fn} is
|
|
|
|
|
|vim.schedule_wrap()|ped automatically, so API functions are safe to call.
|
2021-05-31 10:47:51 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {fn} (`function`) Callback to call once `timeout` expires
|
|
|
|
|
• {timeout} (`integer`) Number of milliseconds to wait before calling
|
2023-02-04 07:58:38 -07:00
|
|
|
|
`fn`
|
2021-05-31 10:47:51 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table`) timer luv timer object
|
2021-05-31 10:47:51 -07:00
|
|
|
|
|
2022-05-15 18:07:36 -07:00
|
|
|
|
*vim.deprecate()*
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.deprecate({name}, {alternative}, {version}, {plugin}, {backtrace})
|
2023-03-15 05:56:13 -07:00
|
|
|
|
Shows a deprecation message to the user.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {name} (`string`) Deprecated feature (function, API, etc.).
|
|
|
|
|
• {alternative} (`string?`) Suggested alternative feature.
|
|
|
|
|
• {version} (`string`) Version when the deprecated function will be
|
2024-01-04 09:09:13 -07:00
|
|
|
|
removed.
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {plugin} (`string?`) Name of the plugin that owns the deprecated
|
2023-03-15 05:56:13 -07:00
|
|
|
|
feature. Defaults to "Nvim".
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {backtrace} (`boolean?`) Prints backtrace. Defaults to true.
|
2022-05-03 06:42:41 -07:00
|
|
|
|
|
2023-03-15 05:56:13 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string?`) Deprecated message, or nil if no message was shown.
|
2023-06-05 20:05:51 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
vim.inspect() *vim.inspect()*
|
2022-10-09 05:21:52 -07:00
|
|
|
|
Gets a human-readable representation of the given object.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-08-27 01:41:32 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`)
|
2023-08-27 01:41:32 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-06-23 03:16:55 -07:00
|
|
|
|
• |vim.print()|
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• https://github.com/kikito/inspect.lua
|
|
|
|
|
• https://github.com/mpeterv/vinspect
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.keycode({str}) *vim.keycode()*
|
2023-11-02 16:22:02 -07:00
|
|
|
|
Translates keycodes.
|
2023-04-25 07:52:44 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
local k = vim.keycode
|
|
|
|
|
vim.g.mapleader = k'<bs>'
|
2023-04-25 07:52:44 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`) String to be converted.
|
2023-04-25 07:52:44 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`)
|
2023-04-25 07:52:44 -07:00
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
• |nvim_replace_termcodes()|
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
vim.lua_omnifunc({find_start}) *vim.lua_omnifunc()*
|
2023-06-22 03:44:51 -07:00
|
|
|
|
Omnifunc for completing Lua values from the runtime Lua interpreter,
|
2021-01-27 01:00:28 -07:00
|
|
|
|
similar to the builtin completion for the `:lua` command.
|
|
|
|
|
|
2023-06-22 03:44:51 -07:00
|
|
|
|
Activate using `set omnifunc=v:lua.vim.lua_omnifunc` in a Lua buffer.
|
2021-01-27 01:00:28 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Parameters: ~
|
|
|
|
|
• {find_start} (`1|0`)
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.notify({msg}, {level}, {opts}) *vim.notify()*
|
2023-11-02 16:22:02 -07:00
|
|
|
|
Displays a notification to the user.
|
2021-08-22 13:55:28 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
This function can be overridden by plugins to display notifications using
|
|
|
|
|
a custom provider (such as the system notification provider). By default,
|
|
|
|
|
writes to |:messages|.
|
2021-05-31 10:47:51 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {msg} (`string`) Content of the notification to show to the user.
|
|
|
|
|
• {level} (`integer?`) One of the values from |vim.log.levels|.
|
|
|
|
|
• {opts} (`table?`) Optional parameters. Unused by default.
|
2021-05-31 10:47:51 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.notify_once({msg}, {level}, {opts}) *vim.notify_once()*
|
2023-11-02 16:22:02 -07:00
|
|
|
|
Displays a notification only one time.
|
2022-01-06 11:10:56 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Like |vim.notify()|, but subsequent calls with the same message will not
|
|
|
|
|
display a notification.
|
2022-01-06 11:10:56 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {msg} (`string`) Content of the notification to show to the user.
|
|
|
|
|
• {level} (`integer?`) One of the values from |vim.log.levels|.
|
|
|
|
|
• {opts} (`table?`) Optional parameters. Unused by default.
|
2021-08-22 13:55:28 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) true if message was displayed, else false
|
2022-05-15 18:07:36 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.on_key({fn}, {ns_id}) *vim.on_key()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Adds Lua function {fn} with namespace id {ns_id} as a listener to every,
|
|
|
|
|
yes every, input key.
|
2021-10-05 10:48:48 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
The Nvim command-line option |-w| is related but does not support
|
|
|
|
|
callbacks and cannot be toggled dynamically.
|
2021-10-05 10:48:48 -07:00
|
|
|
|
|
2023-07-06 13:47:27 -07:00
|
|
|
|
Note: ~
|
|
|
|
|
• {fn} will be removed on error.
|
|
|
|
|
• {fn} will not be cleared by |nvim_buf_clear_namespace()|
|
2021-10-05 10:48:48 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-03-30 20:20:05 -07:00
|
|
|
|
• {fn} (`fun(key: string, typed: string)?`) Function invoked on
|
|
|
|
|
every key press. |i_CTRL-V| {key} is the key after mappings
|
|
|
|
|
have been applied, and {typed} is the key(s) before mappings
|
|
|
|
|
are applied, which may be empty if {key} is produced by
|
|
|
|
|
non-typed keys. When {fn} is nil and {ns_id} is specified,
|
|
|
|
|
the callback associated with namespace {ns_id} is removed.
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {ns_id} (`integer?`) Namespace ID. If nil or 0, generates and returns
|
|
|
|
|
a new |nvim_create_namespace()| id.
|
2021-10-05 10:48:48 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`integer`) Namespace id associated with {fn}. Or count of all
|
|
|
|
|
callbacks if on_key() is called without arguments.
|
2021-10-05 10:48:48 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.paste({lines}, {phase}) *vim.paste()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Paste handler, invoked by |nvim_paste()| when a conforming UI (such as the
|
|
|
|
|
|TUI|) pastes text into the editor.
|
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Example: To remove ANSI color codes when pasting: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
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)
|
2019-11-17 20:06:59 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {lines} (`string[]`) |readfile()|-style list of lines to paste.
|
2022-10-05 04:21:45 -07:00
|
|
|
|
|channel-lines|
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {phase} (`-1|1|2|3`) -1: "non-streaming" paste: the call contains all
|
2024-02-15 10:16:04 -07:00
|
|
|
|
lines. If paste is "streamed", `phase` indicates the stream
|
|
|
|
|
state:
|
2022-08-11 05:25:48 -07:00
|
|
|
|
• 1: starts the paste (exactly once)
|
|
|
|
|
• 2: continues the paste (zero or more times)
|
|
|
|
|
• 3: ends the paste (exactly once)
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) result false if client should cancel the paste.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• |paste|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.print({...}) *vim.print()*
|
refactor!: rename vim.pretty_print => vim.print
Problem:
The function name `vim.pretty_print`:
1. is verbose, which partially defeats its purpose as sugar
2. does not draw from existing precedent or any sort of convention
(except external projects like penlight or python?), which reduces
discoverability, and degrades signaling about best practices.
Solution:
- Rename to `vim.print`.
- Change the behavior so that
1. strings are printed without quotes
2. each arg is printed on its own line
3. tables are indented with 2 instead of 4 spaces
- Example:
:lua ='a', 'b', 42, {a=3}
a
b
42
{
a = 3
}
Comparison of alternatives:
- `vim.print`:
- pro: consistent with Lua's `print()`
- pro: aligns with potential `nvim_print` API function which will
replace nvim_echo, nvim_notify, etc.
- con: behaves differently than Lua's `print()`, slightly misleading?
- `vim.echo`:
- pro: `:echo` has similar "pretty print" behavior.
- con: inconsistent with Lua idioms.
- `vim.p`:
- pro: very short, fits with `vim.o`, etc.
- con: not as discoverable as "echo"
- con: less opportunity for `local p = vim.p` because of potential shadowing.
2023-03-07 08:04:57 -07:00
|
|
|
|
"Pretty prints" the given arguments and returns them unmodified.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
2023-09-24 21:39:59 -07:00
|
|
|
|
local hl_normal = vim.print(vim.api.nvim_get_hl(0, { name = 'Normal' }))
|
2022-01-06 11:42:31 -07:00
|
|
|
|
<
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Parameters: ~
|
|
|
|
|
• {...} (`any`)
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`any`) given arguments.
|
2022-01-06 11:42:31 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• |vim.inspect()|
|
2023-06-23 03:16:55 -07:00
|
|
|
|
• |:=|
|
2022-01-06 11:42:31 -07:00
|
|
|
|
|
2023-09-20 22:53:05 -07:00
|
|
|
|
vim.schedule_wrap({fn}) *vim.schedule_wrap()*
|
|
|
|
|
Returns a function which calls {fn} via |vim.schedule()|.
|
|
|
|
|
|
|
|
|
|
The returned function passes all arguments to {fn}.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
function notify_readable(_err, readable)
|
|
|
|
|
vim.notify("readable? " .. tostring(readable))
|
|
|
|
|
end
|
|
|
|
|
vim.uv.fs_access(vim.fn.stdpath("config"), "R", vim.schedule_wrap(notify_readable))
|
|
|
|
|
<
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-10-05 04:21:45 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {fn} (`function`)
|
2022-10-05 04:21:45 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`function`)
|
2022-10-05 04:21:45 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• |lua-loop-callbacks|
|
|
|
|
|
• |vim.schedule()|
|
|
|
|
|
• |vim.in_fast_event()|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.system({cmd}, {opts}, {on_exit}) *vim.system()*
|
2023-11-02 16:22:02 -07:00
|
|
|
|
Runs a system command or throws an error if {cmd} cannot be run.
|
2023-06-07 05:52:23 -07:00
|
|
|
|
|
|
|
|
|
Examples: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
local on_exit = function(obj)
|
|
|
|
|
print(obj.code)
|
|
|
|
|
print(obj.signal)
|
|
|
|
|
print(obj.stdout)
|
|
|
|
|
print(obj.stderr)
|
|
|
|
|
end
|
2023-06-07 05:52:23 -07:00
|
|
|
|
|
2023-11-02 16:22:02 -07:00
|
|
|
|
-- Runs asynchronously:
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.system({'echo', 'hello'}, { text = true }, on_exit)
|
2023-06-07 05:52:23 -07:00
|
|
|
|
|
2023-11-02 16:22:02 -07:00
|
|
|
|
-- Runs synchronously:
|
2023-09-14 06:23:01 -07:00
|
|
|
|
local obj = vim.system({'echo', 'hello'}, { text = true }):wait()
|
|
|
|
|
-- { code = 0, signal = 0, stdout = 'hello', stderr = '' }
|
2023-06-07 05:52:23 -07:00
|
|
|
|
<
|
|
|
|
|
|
2023-11-02 16:22:02 -07:00
|
|
|
|
See |uv.spawn()| for more details. Note: unlike |uv.spawn()|, vim.system
|
|
|
|
|
throws an error if {cmd} cannot be run.
|
2023-06-07 05:52:23 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {cmd} (`string[]`) Command to execute
|
|
|
|
|
• {opts} (`vim.SystemOpts?`) Options:
|
2023-06-07 05:52:23 -07:00
|
|
|
|
• cwd: (string) Set the current working directory for the
|
|
|
|
|
sub-process.
|
|
|
|
|
• env: table<string,string> Set environment variables for
|
|
|
|
|
the new process. Inherits the current environment with
|
|
|
|
|
`NVIM` set to |v:servername|.
|
|
|
|
|
• clear_env: (boolean) `env` defines the job environment
|
|
|
|
|
exactly, instead of merging current environment.
|
|
|
|
|
• stdin: (string|string[]|boolean) If `true`, then a pipe
|
|
|
|
|
to stdin is opened and can be written to via the
|
|
|
|
|
`write()` method to SystemObj. If string or string[] then
|
|
|
|
|
will be written to stdin and closed. Defaults to `false`.
|
|
|
|
|
• stdout: (boolean|function) Handle output from stdout.
|
|
|
|
|
When passed as a function must have the signature
|
|
|
|
|
`fun(err: string, data: string)`. Defaults to `true`
|
2023-09-13 21:05:27 -07:00
|
|
|
|
• stderr: (boolean|function) Handle output from stderr.
|
2023-06-07 05:52:23 -07:00
|
|
|
|
When passed as a function must have the signature
|
|
|
|
|
`fun(err: string, data: string)`. Defaults to `true`.
|
|
|
|
|
• text: (boolean) Handle stdout and stderr as text.
|
|
|
|
|
Replaces `\r\n` with `\n`.
|
2023-09-04 03:30:16 -07:00
|
|
|
|
• timeout: (integer) Run the command with a time limit.
|
|
|
|
|
Upon timeout the process is sent the TERM signal (15) and
|
|
|
|
|
the exit code is set to 124.
|
2023-06-07 05:52:23 -07:00
|
|
|
|
• detach: (boolean) If true, spawn the child process in a
|
|
|
|
|
detached state - this will make it a process group
|
|
|
|
|
leader, and will effectively enable the child to keep
|
|
|
|
|
running after the parent exits. Note that the child
|
|
|
|
|
process will still keep the parent's event loop alive
|
|
|
|
|
unless the parent process calls |uv.unref()| on the
|
|
|
|
|
child's process handle.
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {on_exit} (`fun(out: vim.SystemCompleted)?`) Called when subprocess
|
|
|
|
|
exits. When provided, the command runs asynchronously.
|
|
|
|
|
Receives SystemCompleted object, see return of
|
|
|
|
|
SystemObj:wait().
|
2023-06-07 05:52:23 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`vim.SystemObj`) Object with the fields:
|
2024-04-15 04:33:09 -07:00
|
|
|
|
• cmd (string[]) Command name and args
|
2023-06-07 05:52:23 -07:00
|
|
|
|
• pid (integer) Process ID
|
2023-09-04 03:30:16 -07:00
|
|
|
|
• wait (fun(timeout: integer|nil): SystemCompleted) Wait for the
|
|
|
|
|
process to complete. Upon timeout the process is sent the KILL
|
2023-11-21 04:24:30 -07:00
|
|
|
|
signal (9) and the exit code is set to 124. Cannot be called in
|
|
|
|
|
|api-fast|.
|
2023-06-07 05:52:23 -07:00
|
|
|
|
• SystemCompleted is an object with the fields:
|
|
|
|
|
• code: (integer)
|
|
|
|
|
• signal: (integer)
|
|
|
|
|
• stdout: (string), nil if stdout argument is passed
|
|
|
|
|
• stderr: (string), nil if stderr argument is passed
|
2023-09-04 03:30:16 -07:00
|
|
|
|
• kill (fun(signal: integer|string))
|
2023-06-07 05:52:23 -07:00
|
|
|
|
• write (fun(data: string|nil)) Requires `stdin=true`. Pass `nil` to
|
|
|
|
|
close the stream.
|
|
|
|
|
• is_closing (fun(): boolean)
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-12-14 02:46:54 -07:00
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
Lua module: vim.inspector *vim.inspector*
|
2022-12-14 02:46:54 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.inspect_pos({bufnr}, {row}, {col}, {filter}) *vim.inspect_pos()*
|
2022-12-14 02:46:54 -07:00
|
|
|
|
Get all the items at a given buffer position.
|
|
|
|
|
|
|
|
|
|
Can also be pretty-printed with `:Inspect!`. *:Inspect!*
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {bufnr} (`integer?`) defaults to the current buffer
|
|
|
|
|
• {row} (`integer?`) row to inspect, 0-based. Defaults to the row of
|
|
|
|
|
the current cursor
|
|
|
|
|
• {col} (`integer?`) col to inspect, 0-based. Defaults to the col of
|
|
|
|
|
the current cursor
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {filter} (`table?`) Table with key-value pairs to filter the items
|
2024-03-05 05:06:15 -07:00
|
|
|
|
• {syntax} (`boolean`, default: `true`) Include syntax based
|
|
|
|
|
highlight groups.
|
|
|
|
|
• {treesitter} (`boolean`, default: `true`) Include
|
|
|
|
|
treesitter based highlight groups.
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {extmarks} (`boolean|"all"`, default: true) Include
|
|
|
|
|
extmarks. When `all`, then extmarks without a `hl_group`
|
|
|
|
|
will also be included.
|
2024-03-05 05:06:15 -07:00
|
|
|
|
• {semantic_tokens} (`boolean`, default: true) Include
|
|
|
|
|
semantic token highlights.
|
2022-12-14 02:46:54 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table`) a table with the following key-value pairs. Items are in
|
2022-12-14 02:46:54 -07:00
|
|
|
|
"traversal order":
|
|
|
|
|
• treesitter: a list of treesitter captures
|
|
|
|
|
• syntax: a list of syntax groups
|
|
|
|
|
• semantic_tokens: a list of semantic tokens
|
|
|
|
|
• extmarks: a list of extmarks
|
|
|
|
|
• buffer: the buffer used to get the items
|
|
|
|
|
• row: the row used to get the items
|
|
|
|
|
• col: the col used to get the items
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.show_pos({bufnr}, {row}, {col}, {filter}) *vim.show_pos()*
|
2022-12-14 02:46:54 -07:00
|
|
|
|
Show all the items at a given buffer position.
|
|
|
|
|
|
|
|
|
|
Can also be shown with `:Inspect`. *:Inspect*
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {bufnr} (`integer?`) defaults to the current buffer
|
|
|
|
|
• {row} (`integer?`) row to inspect, 0-based. Defaults to the row of
|
|
|
|
|
the current cursor
|
|
|
|
|
• {col} (`integer?`) col to inspect, 0-based. Defaults to the col of
|
|
|
|
|
the current cursor
|
2024-03-05 05:06:15 -07:00
|
|
|
|
• {filter} (`table?`) A table with the following fields:
|
|
|
|
|
• {syntax} (`boolean`, default: `true`) Include syntax based
|
|
|
|
|
highlight groups.
|
|
|
|
|
• {treesitter} (`boolean`, default: `true`) Include
|
|
|
|
|
treesitter based highlight groups.
|
|
|
|
|
• {extmarks} (`boolean|"all"`, default: true) Include
|
|
|
|
|
extmarks. When `all`, then extmarks without a `hl_group`
|
|
|
|
|
will also be included.
|
|
|
|
|
• {semantic_tokens} (`boolean`, default: true) Include
|
|
|
|
|
semantic token highlights.
|
2022-12-14 02:46:54 -07:00
|
|
|
|
|
|
|
|
|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
|
*vim.Ringbuf*
|
|
|
|
|
|
|
|
|
|
Fields: ~
|
|
|
|
|
• {clear} (`fun()`) Clear all items
|
|
|
|
|
• {push} (`fun(item: T)`) Adds an item, overriding the oldest item if
|
|
|
|
|
the buffer is full.
|
|
|
|
|
• {pop} (`fun(): T?`) Removes and returns the first unread item
|
|
|
|
|
• {peek} (`fun(): T?`) Returns the first unread item without removing
|
|
|
|
|
it
|
|
|
|
|
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Ringbuf:clear() *Ringbuf:clear()*
|
|
|
|
|
Clear all items
|
|
|
|
|
|
|
|
|
|
Ringbuf:peek() *Ringbuf:peek()*
|
|
|
|
|
Returns the first unread item without removing it
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`any?`)
|
|
|
|
|
|
|
|
|
|
Ringbuf:pop() *Ringbuf:pop()*
|
|
|
|
|
Removes and returns the first unread item
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`any?`)
|
|
|
|
|
|
|
|
|
|
Ringbuf:push({item}) *Ringbuf:push()*
|
|
|
|
|
Adds an item, overriding the oldest item if the buffer is full.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {item} (`any`)
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.deep_equal({a}, {b}) *vim.deep_equal()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Deep compare values for equality
|
2021-09-10 06:22:40 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Tables are compared recursively unless they both provide the `eq`
|
|
|
|
|
metamethod. All other types are compared using the equality `==` operator.
|
2021-09-10 06:22:40 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {a} (`any`) First value
|
|
|
|
|
• {b} (`any`) Second value
|
2021-09-10 06:22:40 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) `true` if values are equals, else `false`
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2024-01-02 08:47:55 -07:00
|
|
|
|
vim.deepcopy({orig}, {noref}) *vim.deepcopy()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Returns a deep copy of the given object. Non-table objects are copied as
|
|
|
|
|
in a typical Lua assignment, whereas table objects 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
|
|
|
|
|
2024-01-02 08:47:55 -07:00
|
|
|
|
Note: `noref=true` is much more performant on tables with unique table
|
|
|
|
|
fields, while `noref=false` is more performant on tables that reuse table
|
|
|
|
|
fields.
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {orig} (`table`) Table to copy
|
|
|
|
|
• {noref} (`boolean?`) When `false` (default) a contained table is only
|
|
|
|
|
copied once and all references point to this single copy.
|
|
|
|
|
When `true` every occurrence of a table results in a new
|
|
|
|
|
copy. This also means that a cyclic reference can cause
|
2024-01-02 08:47:55 -07:00
|
|
|
|
`deepcopy()` to fail.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table`) Table of copied keys and (nested) values.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-09-20 04:15:23 -07:00
|
|
|
|
vim.defaulttable({createfn}) *vim.defaulttable()*
|
|
|
|
|
Creates a table whose missing keys are provided by {createfn} (like
|
|
|
|
|
Python's "defaultdict").
|
2022-09-06 23:39:56 -07:00
|
|
|
|
|
2023-09-20 04:15:23 -07:00
|
|
|
|
If {createfn} is `nil` it defaults to defaulttable() itself, so accessing
|
|
|
|
|
nested keys creates nested tables: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
local a = vim.defaulttable()
|
|
|
|
|
a.b.c = 1
|
2022-09-06 23:39:56 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {createfn} (`fun(key:any):any?`) Provides the value for a missing
|
2023-09-20 04:15:23 -07:00
|
|
|
|
`key`.
|
2022-09-06 23:39:56 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table`) Empty table with `__index` metamethod.
|
2022-09-06 23:39:56 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.endswith({s}, {suffix}) *vim.endswith()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Tests if `s` ends with `suffix`.
|
2020-01-13 00:41:55 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {s} (`string`) String
|
|
|
|
|
• {suffix} (`string`) Suffix to match
|
2020-01-13 00:41:55 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) `true` if `suffix` is a suffix of `s`
|
2020-01-13 00:41:55 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.gsplit({s}, {sep}, {opts}) *vim.gsplit()*
|
2023-07-12 10:27:14 -07:00
|
|
|
|
Gets an |iterator| that splits a string at each instance of a separator,
|
|
|
|
|
in "lazy" fashion (as opposed to |vim.split()| which is "eager").
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-03-20 00:12:33 -07:00
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
for s in vim.gsplit(':aa::b:', ':', {plain=true}) do
|
|
|
|
|
print(s)
|
|
|
|
|
end
|
2023-03-20 00:12:33 -07:00
|
|
|
|
<
|
|
|
|
|
|
2023-03-22 07:14:51 -07:00
|
|
|
|
If you want to also inspect the separator itself (instead of discarding
|
|
|
|
|
it), use |string.gmatch()|. Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
for word, num in ('foo111bar222'):gmatch('([^0-9]*)(%d*)') do
|
|
|
|
|
print(('word: %s num: %s'):format(word, num))
|
|
|
|
|
end
|
2023-03-22 07:14:51 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {s} (`string`) String to split
|
|
|
|
|
• {sep} (`string`) Separator or pattern
|
|
|
|
|
• {opts} (`table?`) Keyword arguments |kwargs|:
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {plain}? (`boolean`) Use `sep` literally (as in
|
|
|
|
|
string.find).
|
|
|
|
|
• {trimempty}? (`boolean`) Discard empty segments at start and
|
|
|
|
|
end of the sequence.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-03-05 05:06:15 -07:00
|
|
|
|
(`fun():string?`) Iterator over the split components
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-03-22 07:14:51 -07:00
|
|
|
|
• |string.gmatch()|
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• |vim.split()|
|
2023-08-03 08:35:10 -07:00
|
|
|
|
• |lua-patterns|
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• https://www.lua.org/pil/20.2.html
|
|
|
|
|
• http://lua-users.org/wiki/StringLibraryTutorial
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.is_callable({f}) *vim.is_callable()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Returns true if object `f` can be called as a function.
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {f} (`any`) Any object
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) `true` if `f` is callable, else `false`
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2024-04-21 08:29:10 -07:00
|
|
|
|
vim.isarray({t}) *vim.isarray()*
|
|
|
|
|
Tests if `t` is an "array": a table indexed only by integers (potentially
|
|
|
|
|
non-contiguous).
|
|
|
|
|
|
|
|
|
|
If the indexes start from 1 and are contiguous then the array is also a
|
|
|
|
|
list. |vim.islist()|
|
|
|
|
|
|
|
|
|
|
Empty table `{}` is 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|.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {t} (`table?`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`boolean`) `true` if array-like table, else `false`.
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
• https://github.com/openresty/luajit2#tableisarray
|
|
|
|
|
|
2024-04-21 06:19:43 -07:00
|
|
|
|
vim.islist({t}) *vim.islist()*
|
|
|
|
|
Tests if `t` is a "list": a table indexed only by contiguous integers
|
|
|
|
|
starting from 1 (what |lua-length| calls a "regular array").
|
|
|
|
|
|
|
|
|
|
Empty table `{}` is a list, 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|.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {t} (`table?`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`boolean`) `true` if list-like table, else `false`.
|
|
|
|
|
|
|
|
|
|
See also: ~
|
2024-04-21 08:29:10 -07:00
|
|
|
|
• |vim.isarray()|
|
2024-04-21 06:19:43 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.list_contains({t}, {value}) *vim.list_contains()*
|
2023-04-14 01:39:57 -07:00
|
|
|
|
Checks if a list-like table (integer keys without gaps) contains `value`.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {t} (`table`) Table to check (must be list-like, not validated)
|
|
|
|
|
• {value} (`any`) Value to compare
|
2023-04-14 01:39:57 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) `true` if `t` contains `value`
|
2023-04-14 01:39:57 -07:00
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
• |vim.tbl_contains()| for checking values in general tables
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.list_extend({dst}, {src}, {start}, {finish}) *vim.list_extend()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Extends a list-like table with the values of another list-like table.
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
NOTE: This mutates dst!
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {dst} (`table`) List which will be modified and appended to
|
|
|
|
|
• {src} (`table`) List from which values will be inserted
|
|
|
|
|
• {start} (`integer?`) Start index on src. Defaults to 1
|
|
|
|
|
• {finish} (`integer?`) Final index on src. Defaults to `#src`
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table`) dst
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• |vim.tbl_extend()|
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.list_slice({list}, {start}, {finish}) *vim.list_slice()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Creates a copy of a table containing only elements from start to end
|
|
|
|
|
(inclusive)
|
2021-03-11 08:11:11 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-02-15 10:16:04 -07:00
|
|
|
|
• {list} (`any[]`) Table
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {start} (`integer?`) Start range of slice
|
|
|
|
|
• {finish} (`integer?`) End range of slice
|
2021-03-11 08:11:11 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-02-15 10:16:04 -07:00
|
|
|
|
(`any[]`) Copy of table sliced from start to finish (inclusive)
|
2021-03-11 08:11:11 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.pesc({s}) *vim.pesc()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Escapes magic chars in |lua-patterns|.
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {s} (`string`) String to escape
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`) %-escaped pattern string
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• https://github.com/rxi/lume
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.ringbuf({size}) *vim.ringbuf()*
|
2023-06-08 03:11:24 -07:00
|
|
|
|
Create a ring buffer limited to a maximal number of items. Once the buffer
|
2023-09-14 06:23:01 -07:00
|
|
|
|
is full, adding a new entry overrides the oldest entry. >lua
|
|
|
|
|
local ringbuf = vim.ringbuf(4)
|
|
|
|
|
ringbuf:push("a")
|
|
|
|
|
ringbuf:push("b")
|
|
|
|
|
ringbuf:push("c")
|
|
|
|
|
ringbuf:push("d")
|
|
|
|
|
ringbuf:push("e") -- overrides "a"
|
|
|
|
|
print(ringbuf:pop()) -- returns "b"
|
|
|
|
|
print(ringbuf:pop()) -- returns "c"
|
|
|
|
|
|
|
|
|
|
-- Can be used as iterator. Pops remaining items:
|
|
|
|
|
for val in ringbuf do
|
|
|
|
|
print(val)
|
|
|
|
|
end
|
2023-06-08 03:11:24 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Returns a Ringbuf instance with the following methods:
|
|
|
|
|
• |Ringbuf:push()|
|
|
|
|
|
• |Ringbuf:pop()|
|
|
|
|
|
• |Ringbuf:peek()|
|
|
|
|
|
• |Ringbuf:clear()|
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {size} (`integer`)
|
2023-06-08 03:11:24 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-03-05 05:06:15 -07:00
|
|
|
|
(`vim.Ringbuf`) ringbuf See |vim.Ringbuf|.
|
2023-06-08 03:11:24 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.spairs({t}) *vim.spairs()*
|
2023-09-20 04:15:23 -07:00
|
|
|
|
Enumerates key-value pairs of a table, ordered by key.
|
2023-01-23 02:26:46 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {t} (`table`) Dict-like table
|
2023-01-23 02:26:46 -07:00
|
|
|
|
|
2024-03-05 05:06:15 -07:00
|
|
|
|
Return (multiple): ~
|
|
|
|
|
(`fun(table: table<K, V>, index?: K):K, V`) |for-in| iterator over
|
|
|
|
|
sorted keys and their values
|
|
|
|
|
(`table`)
|
2023-01-23 02:26:46 -07:00
|
|
|
|
|
|
|
|
|
See also: ~
|
2024-02-15 10:16:04 -07:00
|
|
|
|
• Based on
|
|
|
|
|
https://github.com/premake/premake-core/blob/master/src/base/table.lua
|
2023-01-23 02:26:46 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.split({s}, {sep}, {opts}) *vim.split()*
|
2023-07-12 10:27:14 -07:00
|
|
|
|
Splits a string at each instance of a separator and returns the result as
|
|
|
|
|
a table (unlike |vim.gsplit()|).
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Examples: >lua
|
2023-09-14 06:23:01 -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
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {s} (`string`) String to split
|
|
|
|
|
• {sep} (`string`) Separator or pattern
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {opts} (`table?`) Keyword arguments |kwargs|:
|
|
|
|
|
• {plain}? (`boolean`) Use `sep` literally (as in
|
|
|
|
|
string.find).
|
|
|
|
|
• {trimempty}? (`boolean`) Discard empty segments at start and
|
|
|
|
|
end of the sequence.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string[]`) List of split components
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-10-24 05:53:53 -07:00
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• |vim.gsplit()|
|
2023-03-22 07:14:51 -07:00
|
|
|
|
• |string.gmatch()|
|
2022-10-24 05:53:53 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.startswith({s}, {prefix}) *vim.startswith()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Tests if `s` starts with `prefix`.
|
2020-01-13 00:41:55 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {s} (`string`) String
|
|
|
|
|
• {prefix} (`string`) Prefix to match
|
2020-01-13 00:41:55 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) `true` if `prefix` is a prefix of `s`
|
2020-01-13 00:41:55 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.tbl_contains({t}, {value}, {opts}) *vim.tbl_contains()*
|
2023-04-14 01:39:57 -07:00
|
|
|
|
Checks if a table contains a given value, specified either directly or via
|
|
|
|
|
a predicate that is checked for each value.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.tbl_contains({ 'a', { 'b', 'c' } }, function(v)
|
|
|
|
|
return vim.deep_equal(v, { 'b', 'c' })
|
|
|
|
|
end, { predicate = true })
|
|
|
|
|
-- true
|
2023-04-14 01:39:57 -07:00
|
|
|
|
<
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {t} (`table`) Table to check
|
|
|
|
|
• {value} (`any`) Value to compare or predicate function reference
|
|
|
|
|
• {opts} (`table?`) Keyword arguments |kwargs|:
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {predicate}? (`boolean`) `value` is a function reference to
|
|
|
|
|
be checked (default false)
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) `true` if `t` contains `value`
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-04-14 01:39:57 -07:00
|
|
|
|
See also: ~
|
|
|
|
|
• |vim.list_contains()| for checking values in list-like tables
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.tbl_count({t}) *vim.tbl_count()*
|
2023-09-14 06:23:01 -07:00
|
|
|
|
Counts the number of non-nil values in table `t`. >lua
|
|
|
|
|
vim.tbl_count({ a=1, b=2 }) --> 2
|
|
|
|
|
vim.tbl_count({ 1, 2 }) --> 2
|
2020-07-02 04:09:17 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {t} (`table`) Table
|
2020-07-02 04:09:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`integer`) Number of non-nil values in table
|
2020-07-02 04:09:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• https://github.com/Tieske/Penlight/blob/master/lua/pl/tablex.lua
|
2020-07-02 04:09:17 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.tbl_deep_extend({behavior}, {...}) *vim.tbl_deep_extend()*
|
2023-05-13 12:33:22 -07:00
|
|
|
|
Merges recursively two or more tables.
|
2020-07-02 04:09:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2023-12-30 22:08:07 -07:00
|
|
|
|
• {behavior} (`'error'|'keep'|'force'`) Decides what to do if a key is
|
|
|
|
|
found in more than one map:
|
2022-08-11 05:25:48 -07:00
|
|
|
|
• "error": raise an error
|
|
|
|
|
• "keep": use value from the leftmost map
|
|
|
|
|
• "force": use value from the rightmost map
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {...} (`table`) Two or more tables
|
2022-05-11 09:23:46 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table`) Merged table
|
2020-07-02 04:09:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• |vim.tbl_extend()|
|
2020-07-02 04:09:17 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.tbl_extend({behavior}, {...}) *vim.tbl_extend()*
|
2023-05-13 12:33:22 -07:00
|
|
|
|
Merges two or more tables.
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2023-12-30 22:08:07 -07:00
|
|
|
|
• {behavior} (`'error'|'keep'|'force'`) Decides what to do if a key is
|
|
|
|
|
found in more than one map:
|
2022-08-11 05:25:48 -07:00
|
|
|
|
• "error": raise an error
|
|
|
|
|
• "keep": use value from the leftmost map
|
|
|
|
|
• "force": use value from the rightmost map
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {...} (`table`) Two or more tables
|
2022-05-11 09:23:46 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table`) Merged table
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• |extend()|
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.tbl_filter({func}, {t}) *vim.tbl_filter()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Filter a table using a predicate function
|
2020-07-02 04:09:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {func} (`function`) Function
|
|
|
|
|
• {t} (`table`) Table
|
2022-05-11 09:23:46 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-02-15 10:16:04 -07:00
|
|
|
|
(`any[]`) Table of filtered values
|
2020-07-02 04:09:17 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.tbl_get({o}, {...}) *vim.tbl_get()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Index into a table (first argument) via string keys passed as subsequent
|
|
|
|
|
arguments. Return `nil` if the key does not exist.
|
2022-05-11 09:23:46 -07:00
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Examples: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.tbl_get({ key = { nested_key = true }}, 'key', 'nested_key') == true
|
|
|
|
|
vim.tbl_get({ key = {}}, 'key', 'nested_key') == nil
|
2022-03-24 12:01:04 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {o} (`table`) Table to index
|
|
|
|
|
• {...} (`any`) Optional keys (0 or more, variadic) via which to index
|
|
|
|
|
the table
|
2022-03-24 12:01:04 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`any`) Nested value indexed by key (if it exists), else nil
|
2022-03-24 12:01:04 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.tbl_isempty({t}) *vim.tbl_isempty()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Checks if a table is empty.
|
2020-08-31 00:51:35 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {t} (`table`) Table to check
|
2022-05-11 09:23:46 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) `true` if `t` is empty
|
2020-08-31 00:51:35 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• https://github.com/premake/premake-core/blob/master/src/base/table.lua
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.tbl_keys({t}) *vim.tbl_keys()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
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
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {t} (`table`) Table
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-02-15 10:16:04 -07:00
|
|
|
|
(`any[]`) List of keys
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2024-02-15 10:16:04 -07:00
|
|
|
|
• From
|
|
|
|
|
https://github.com/premake/premake-core/blob/master/src/base/table.lua
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.tbl_map({func}, {t}) *vim.tbl_map()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Apply a function to all values of a table.
|
2020-07-02 04:09:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-03-05 05:06:15 -07:00
|
|
|
|
• {func} (`fun(value: T): any`) Function
|
|
|
|
|
• {t} (`table<any, T>`) Table
|
2022-05-11 09:23:46 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table`) Table of transformed values
|
2020-07-02 04:09:17 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.tbl_values({t}) *vim.tbl_values()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
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
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {t} (`table`) Table
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-02-15 10:16:04 -07:00
|
|
|
|
(`any[]`) List of values
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.trim({s}) *vim.trim()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Trim whitespace (Lua pattern "%s") from both sides of a string.
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {s} (`string`) String to trim
|
2019-12-28 04:27:25 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`) String with whitespace removed from its beginning and end
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-08-03 08:35:10 -07:00
|
|
|
|
• |lua-patterns|
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• https://www.lua.org/pil/20.2.html
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.validate({opt}) *vim.validate()*
|
2024-05-27 06:08:23 -07:00
|
|
|
|
Validate function arguments.
|
|
|
|
|
|
|
|
|
|
This function has two valid forms:
|
|
|
|
|
1. vim.validate(name: str, value: any, type: string, optional?: bool)
|
|
|
|
|
2. vim.validate(spec: table)
|
|
|
|
|
|
|
|
|
|
Form 1 validates that argument {name} with value {value} has the type
|
|
|
|
|
{type}. {type} must be a value returned by |lua-type()|. If {optional} is
|
|
|
|
|
true, then {value} may be null. This form is significantly faster and
|
|
|
|
|
should be preferred for simple cases.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
function vim.startswith(s, prefix)
|
|
|
|
|
vim.validate('s', s, 'string')
|
|
|
|
|
vim.validate('prefix', prefix, 'string')
|
|
|
|
|
...
|
|
|
|
|
end
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Form 2 validates a parameter specification (types and values). Specs are
|
2024-04-16 07:31:43 -07:00
|
|
|
|
evaluated in alphanumeric order, until the first failure.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Usage example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
function user.new(name, age, hobbies)
|
|
|
|
|
vim.validate{
|
|
|
|
|
name={name, 'string'},
|
|
|
|
|
age={age, 'number'},
|
|
|
|
|
hobbies={hobbies, 'table'},
|
|
|
|
|
}
|
|
|
|
|
...
|
|
|
|
|
end
|
2019-11-17 20:06:59 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Examples with explicit argument values (can be run directly): >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.validate{arg1={{'foo'}, 'table'}, arg2={'foo', 'string'}}
|
|
|
|
|
--> NOP (success)
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.validate{arg1={1, 'table'}}
|
|
|
|
|
--> error('arg1: expected table, got number')
|
2021-11-28 04:33:44 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
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
|
|
|
|
<
|
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
If multiple types are valid they can be given as a list. >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.validate{arg1={{'foo'}, {'table', 'string'}}, arg2={'foo', {'table', 'string'}}}
|
|
|
|
|
-- NOP (success)
|
2022-01-01 12:35:15 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.validate{arg1={1, {'string', 'table'}}}
|
|
|
|
|
-- error('arg1: expected string|table, got number')
|
2022-01-01 12:35:15 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {opt} (`table`) Names of parameters to validate. Each key is a
|
2022-08-11 05:25:48 -07:00
|
|
|
|
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|table type name, one of: ("table", "t",
|
|
|
|
|
"string", "s", "number", "n", "boolean", "b", "function",
|
|
|
|
|
"f", "nil", "thread", "userdata") or list of them.
|
|
|
|
|
• 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 valid. Can optionally return
|
|
|
|
|
an additional informative error message as the second
|
|
|
|
|
returned value.
|
|
|
|
|
• msg: (optional) error string if validation fails
|
2019-11-17 20:06:59 -07:00
|
|
|
|
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2023-03-26 03:42:15 -07:00
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
Lua module: vim.loader *vim.loader*
|
2023-03-26 03:42:15 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.loader.disable() *vim.loader.disable()*
|
2023-03-26 03:42:15 -07:00
|
|
|
|
Disables the experimental Lua module loader:
|
|
|
|
|
• removes the loaders
|
2023-06-22 03:44:51 -07:00
|
|
|
|
• adds the default Nvim loader
|
2023-03-26 03:42:15 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.loader.enable() *vim.loader.enable()*
|
2023-03-26 03:42:15 -07:00
|
|
|
|
Enables the experimental Lua module loader:
|
|
|
|
|
• overrides loadfile
|
2023-06-22 03:44:51 -07:00
|
|
|
|
• adds the Lua loader using the byte-compilation cache
|
2023-03-26 03:42:15 -07:00
|
|
|
|
• adds the libs loader
|
2023-06-22 03:44:51 -07:00
|
|
|
|
• removes the default Nvim loader
|
2023-03-26 03:42:15 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.loader.find({modname}, {opts}) *vim.loader.find()*
|
2023-06-22 03:44:51 -07:00
|
|
|
|
Finds Lua modules for the given module name.
|
2023-03-26 03:42:15 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {modname} (`string`) Module name, or `"*"` to find the top-level
|
2023-03-26 03:42:15 -07:00
|
|
|
|
modules instead
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {opts} (`table?`) Options for finding a module:
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {rtp}? (`boolean`, default: `true`) Search for modname in
|
|
|
|
|
the runtime path.
|
|
|
|
|
• {paths}? (`string[]`, default: `{}`) Extra paths to
|
|
|
|
|
search for modname
|
2024-03-08 05:25:18 -07:00
|
|
|
|
• {patterns}? (`string[]`, default:
|
|
|
|
|
`{"/init.lua", ".lua"}`) List of patterns to use when
|
|
|
|
|
searching for modules. A pattern is a string added to the
|
|
|
|
|
basename of the Lua module being searched.
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {all}? (`boolean`, default: `false`) Search for all
|
|
|
|
|
matches.
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`table[]`) A list of objects with the following fields:
|
|
|
|
|
• {modpath} (`string`) Path of the module
|
|
|
|
|
• {modname} (`string`) Name of the module
|
|
|
|
|
• {stat}? (`uv.uv_fs_t`) The fs_stat of the module path. Won't be
|
|
|
|
|
returned for `modname="*"`
|
2023-03-26 03:42:15 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.loader.reset({path}) *vim.loader.reset()*
|
2023-03-31 05:05:22 -07:00
|
|
|
|
Resets the cache for the path, or all the paths if path is nil.
|
2023-03-26 03:42:15 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {path} (`string?`) path to reset
|
2023-03-26 03:42:15 -07:00
|
|
|
|
|
|
|
|
|
|
2020-09-14 06:12:17 -07:00
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
Lua module: vim.uri *vim.uri*
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2023-08-01 08:28:28 -07:00
|
|
|
|
vim.uri_decode({str}) *vim.uri_decode()*
|
|
|
|
|
URI-decodes a string containing percent escapes.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`) string to decode
|
2023-08-01 08:28:28 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`) decoded string
|
2023-08-01 08:28:28 -07:00
|
|
|
|
|
|
|
|
|
vim.uri_encode({str}, {rfc}) *vim.uri_encode()*
|
|
|
|
|
URI-encodes a string using percent escapes.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`) string to encode
|
|
|
|
|
• {rfc} (`"rfc2396"|"rfc2732"|"rfc3986"?`)
|
2023-08-01 08:28:28 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`) encoded string
|
2023-08-01 08:28:28 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.uri_from_bufnr({bufnr}) *vim.uri_from_bufnr()*
|
2023-08-01 08:28:28 -07:00
|
|
|
|
Gets a URI from a bufnr.
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {bufnr} (`integer`)
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`) URI
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.uri_from_fname({path}) *vim.uri_from_fname()*
|
2023-08-01 08:28:28 -07:00
|
|
|
|
Gets a URI from a file path.
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {path} (`string`) Path to file
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`) URI
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.uri_to_bufnr({uri}) *vim.uri_to_bufnr()*
|
2023-08-01 08:28:28 -07:00
|
|
|
|
Gets the buffer for a uri. Creates a new unloaded buffer if no buffer for
|
2022-08-11 05:25:48 -07:00
|
|
|
|
the uri already exists.
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {uri} (`string`)
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`integer`) bufnr
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.uri_to_fname({uri}) *vim.uri_to_fname()*
|
2023-08-01 08:28:28 -07:00
|
|
|
|
Gets a filename from a URI.
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {uri} (`string`)
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`) filename or unchanged URI for non-file URIs
|
2020-09-14 06:12:17 -07:00
|
|
|
|
|
2021-09-27 12:57:28 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
Lua module: vim.ui *vim.ui*
|
2021-09-27 12:57:28 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.ui.input({opts}, {on_confirm}) *vim.ui.input()*
|
2022-12-14 11:58:18 -07:00
|
|
|
|
Prompts the user for input, allowing arbitrary (potentially asynchronous)
|
|
|
|
|
work until `on_confirm`.
|
2021-11-07 08:13:53 -07:00
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.ui.input({ prompt = 'Enter value for shiftwidth: ' }, function(input)
|
|
|
|
|
vim.o.shiftwidth = tonumber(input)
|
|
|
|
|
end)
|
2022-02-13 06:44:51 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-02-18 19:46:19 -07:00
|
|
|
|
• {opts} (`table?`) Additional options. See |input()|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
• prompt (string|nil) Text of the prompt
|
|
|
|
|
• 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.
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {on_confirm} (`function`) ((input|nil) -> ()) Called once the user
|
2022-08-11 05:25:48 -07:00
|
|
|
|
confirms or abort the input. `input` is what the user
|
2022-11-07 17:15:15 -07:00
|
|
|
|
typed (it might be an empty string if nothing was
|
|
|
|
|
entered), or `nil` if the user aborted the dialog.
|
2021-11-07 08:13:53 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.ui.open({path}) *vim.ui.open()*
|
fix(gx): visual selection, expand env vars
---
Rejected experiment: move vim.ui.open() to vim.env.open()
Problem:
`vim.ui` is where user-interface "providers" live, which can be
overridden. It would also be useful to have a "providers" namespace for
platform-specific features such as "open", clipboard, python, and the other
providers listed in `:help providers`. We could overload `vim.ui` to
serve that purpose as the single "providers" namespace, but
`vim.ui.nodejs()` for example seems awkward.
Solution:
`vim.env` currently has too narrow of a purpose. Overload it to also be
a namespace for `vim.env.open`.
diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua
index 913f1fe20348..17d05ff37595 100644
--- a/runtime/lua/vim/_meta.lua
+++ b/runtime/lua/vim/_meta.lua
@@ -37,8 +37,28 @@ local options_info = setmetatable({}, {
end,
})
-vim.env = setmetatable({}, {
- __index = function(_, k)
+vim.env = setmetatable({
+ open = setmetatable({}, {
+ __call = function(_, uri)
+ print('xxxxx'..uri)
+ return true
+ end,
+ __tostring = function()
+ local v = vim.fn.getenv('open')
+ if v == vim.NIL then
+ return nil
+ end
+ return v
+ end,
+ })
+ },
+ {
+ __index = function(t, k, ...)
+ if k == 'open' then
+ error()
+ -- vim.print({...})
+ -- return rawget(t, k)
+ end
local v = vim.fn.getenv(k)
if v == vim.NIL then
return nil
2023-07-02 07:51:30 -07:00
|
|
|
|
Opens `path` with the system default handler (macOS `open`, Windows
|
2023-07-04 14:33:23 -07:00
|
|
|
|
`explorer.exe`, Linux `xdg-open`, …), or returns (but does not show) an
|
|
|
|
|
error message on failure.
|
2023-04-29 22:53:02 -07:00
|
|
|
|
|
fix(gx): visual selection, expand env vars
---
Rejected experiment: move vim.ui.open() to vim.env.open()
Problem:
`vim.ui` is where user-interface "providers" live, which can be
overridden. It would also be useful to have a "providers" namespace for
platform-specific features such as "open", clipboard, python, and the other
providers listed in `:help providers`. We could overload `vim.ui` to
serve that purpose as the single "providers" namespace, but
`vim.ui.nodejs()` for example seems awkward.
Solution:
`vim.env` currently has too narrow of a purpose. Overload it to also be
a namespace for `vim.env.open`.
diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua
index 913f1fe20348..17d05ff37595 100644
--- a/runtime/lua/vim/_meta.lua
+++ b/runtime/lua/vim/_meta.lua
@@ -37,8 +37,28 @@ local options_info = setmetatable({}, {
end,
})
-vim.env = setmetatable({}, {
- __index = function(_, k)
+vim.env = setmetatable({
+ open = setmetatable({}, {
+ __call = function(_, uri)
+ print('xxxxx'..uri)
+ return true
+ end,
+ __tostring = function()
+ local v = vim.fn.getenv('open')
+ if v == vim.NIL then
+ return nil
+ end
+ return v
+ end,
+ })
+ },
+ {
+ __index = function(t, k, ...)
+ if k == 'open' then
+ error()
+ -- vim.print({...})
+ -- return rawget(t, k)
+ end
local v = vim.fn.getenv(k)
if v == vim.NIL then
return nil
2023-07-02 07:51:30 -07:00
|
|
|
|
Expands "~/" and environment variables in filesystem paths.
|
2023-04-29 22:53:02 -07:00
|
|
|
|
|
fix(gx): visual selection, expand env vars
---
Rejected experiment: move vim.ui.open() to vim.env.open()
Problem:
`vim.ui` is where user-interface "providers" live, which can be
overridden. It would also be useful to have a "providers" namespace for
platform-specific features such as "open", clipboard, python, and the other
providers listed in `:help providers`. We could overload `vim.ui` to
serve that purpose as the single "providers" namespace, but
`vim.ui.nodejs()` for example seems awkward.
Solution:
`vim.env` currently has too narrow of a purpose. Overload it to also be
a namespace for `vim.env.open`.
diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua
index 913f1fe20348..17d05ff37595 100644
--- a/runtime/lua/vim/_meta.lua
+++ b/runtime/lua/vim/_meta.lua
@@ -37,8 +37,28 @@ local options_info = setmetatable({}, {
end,
})
-vim.env = setmetatable({}, {
- __index = function(_, k)
+vim.env = setmetatable({
+ open = setmetatable({}, {
+ __call = function(_, uri)
+ print('xxxxx'..uri)
+ return true
+ end,
+ __tostring = function()
+ local v = vim.fn.getenv('open')
+ if v == vim.NIL then
+ return nil
+ end
+ return v
+ end,
+ })
+ },
+ {
+ __index = function(t, k, ...)
+ if k == 'open' then
+ error()
+ -- vim.print({...})
+ -- return rawget(t, k)
+ end
local v = vim.fn.getenv(k)
if v == vim.NIL then
return nil
2023-07-02 07:51:30 -07:00
|
|
|
|
Examples: >lua
|
2024-04-15 04:33:09 -07:00
|
|
|
|
-- Asynchronous.
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.ui.open("https://neovim.io/")
|
|
|
|
|
vim.ui.open("~/path/to/file")
|
2024-04-15 04:33:09 -07:00
|
|
|
|
-- Synchronous (wait until the process exits).
|
2024-05-03 03:20:03 -07:00
|
|
|
|
local cmd, err = vim.ui.open("$VIMRUNTIME")
|
|
|
|
|
if cmd then
|
2024-04-15 04:33:09 -07:00
|
|
|
|
cmd:wait()
|
|
|
|
|
end
|
2023-04-29 22:53:02 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {path} (`string`) Path or URL to open
|
2023-04-29 22:53:02 -07:00
|
|
|
|
|
2023-07-06 06:32:39 -07:00
|
|
|
|
Return (multiple): ~
|
2024-05-03 03:20:03 -07:00
|
|
|
|
(`vim.SystemObj?`) Command object, or nil if not found.
|
|
|
|
|
(`string?`) Error message on failure, or nil on success.
|
2023-04-29 22:53:02 -07:00
|
|
|
|
|
|
|
|
|
See also: ~
|
fix(gx): visual selection, expand env vars
---
Rejected experiment: move vim.ui.open() to vim.env.open()
Problem:
`vim.ui` is where user-interface "providers" live, which can be
overridden. It would also be useful to have a "providers" namespace for
platform-specific features such as "open", clipboard, python, and the other
providers listed in `:help providers`. We could overload `vim.ui` to
serve that purpose as the single "providers" namespace, but
`vim.ui.nodejs()` for example seems awkward.
Solution:
`vim.env` currently has too narrow of a purpose. Overload it to also be
a namespace for `vim.env.open`.
diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua
index 913f1fe20348..17d05ff37595 100644
--- a/runtime/lua/vim/_meta.lua
+++ b/runtime/lua/vim/_meta.lua
@@ -37,8 +37,28 @@ local options_info = setmetatable({}, {
end,
})
-vim.env = setmetatable({}, {
- __index = function(_, k)
+vim.env = setmetatable({
+ open = setmetatable({}, {
+ __call = function(_, uri)
+ print('xxxxx'..uri)
+ return true
+ end,
+ __tostring = function()
+ local v = vim.fn.getenv('open')
+ if v == vim.NIL then
+ return nil
+ end
+ return v
+ end,
+ })
+ },
+ {
+ __index = function(t, k, ...)
+ if k == 'open' then
+ error()
+ -- vim.print({...})
+ -- return rawget(t, k)
+ end
local v = vim.fn.getenv(k)
if v == vim.NIL then
return nil
2023-07-02 07:51:30 -07:00
|
|
|
|
• |vim.system()|
|
2023-04-29 22:53:02 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.ui.select({items}, {opts}, {on_choice}) *vim.ui.select()*
|
2022-12-14 11:58:18 -07:00
|
|
|
|
Prompts the user to pick from a list of items, allowing arbitrary
|
|
|
|
|
(potentially asynchronous) work until `on_choice`.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.ui.select({ 'tabs', 'spaces' }, {
|
|
|
|
|
prompt = 'Select tabs or spaces:',
|
|
|
|
|
format_item = function(item)
|
|
|
|
|
return "I'd like to choose " .. item
|
|
|
|
|
end,
|
|
|
|
|
}, function(choice)
|
|
|
|
|
if choice == 'spaces' then
|
|
|
|
|
vim.o.expandtab = true
|
|
|
|
|
else
|
|
|
|
|
vim.o.expandtab = false
|
|
|
|
|
end
|
|
|
|
|
end)
|
2022-02-13 06:44:51 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-02-18 19:46:19 -07:00
|
|
|
|
• {items} (`any[]`) Arbitrary items
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {opts} (`table`) Additional options
|
2022-08-11 05:25:48 -07:00
|
|
|
|
• 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`.
|
|
|
|
|
• kind (string|nil) Arbitrary hint string indicating the
|
|
|
|
|
item shape. Plugins reimplementing `vim.ui.select` may
|
|
|
|
|
wish to use this to infer the structure or semantics of
|
|
|
|
|
`items`, or the context in which select() was called.
|
2024-02-18 19:46:19 -07:00
|
|
|
|
• {on_choice} (`fun(item: any?, idx: integer?)`) Called once the user
|
|
|
|
|
made a choice. `idx` is the 1-based index of `item`
|
2022-08-11 05:25:48 -07:00
|
|
|
|
within `items`. `nil` if the user aborted the dialog.
|
2021-09-27 12:57:28 -07:00
|
|
|
|
|
2022-01-04 07:28:29 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
Lua module: vim.filetype *vim.filetype*
|
2022-01-04 07:28:29 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.filetype.add({filetypes}) *vim.filetype.add()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Add new filetype mappings.
|
|
|
|
|
|
|
|
|
|
Filetype mappings can be added either by extension or by filename (either
|
|
|
|
|
the "tail" or the full file path). The full file path is checked first,
|
|
|
|
|
followed by the file name. If a match is not found using the filename,
|
|
|
|
|
then the filename is matched against the list of |lua-patterns| (sorted by
|
|
|
|
|
priority) until a match is found. Lastly, if pattern matching does not
|
|
|
|
|
find a filetype, then the file extension is used.
|
|
|
|
|
|
|
|
|
|
The filetype can be either a string (in which case it is used as the
|
|
|
|
|
filetype directly) or a function. If a function, it takes the full path
|
|
|
|
|
and buffer number of the file as arguments (along with captures from the
|
|
|
|
|
matched pattern, if any) and should return a string that will be used as
|
|
|
|
|
the buffer's filetype. Optionally, the function can return a second
|
|
|
|
|
function value which, when called, modifies the state of the buffer. This
|
2023-08-24 10:48:21 -07:00
|
|
|
|
can be used to, for example, set filetype-specific buffer variables. This
|
|
|
|
|
function will be called by Nvim before setting the buffer's filetype.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
|
|
|
|
Filename patterns can specify an optional priority to resolve cases when a
|
|
|
|
|
file path matches multiple patterns. Higher priorities are matched first.
|
2022-09-21 14:58:57 -07:00
|
|
|
|
When omitted, the priority defaults to 0. A pattern can contain
|
|
|
|
|
environment variables of the form "${SOME_VAR}" that will be automatically
|
|
|
|
|
expanded. If the environment variable is not set, the pattern won't be
|
|
|
|
|
matched.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
|
|
|
|
See $VIMRUNTIME/lua/vim/filetype.lua for more examples.
|
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.filetype.add({
|
|
|
|
|
extension = {
|
|
|
|
|
foo = 'fooscript',
|
|
|
|
|
bar = function(path, bufnr)
|
|
|
|
|
if some_condition() then
|
|
|
|
|
return 'barscript', function(bufnr)
|
|
|
|
|
-- Set a buffer variable
|
|
|
|
|
vim.b[bufnr].barscript_version = 2
|
|
|
|
|
end
|
2022-08-11 05:25:48 -07:00
|
|
|
|
end
|
2023-09-14 06:23:01 -07:00
|
|
|
|
return 'bar'
|
|
|
|
|
end,
|
|
|
|
|
},
|
|
|
|
|
filename = {
|
|
|
|
|
['.foorc'] = 'toml',
|
|
|
|
|
['/etc/foo/config'] = 'toml',
|
|
|
|
|
},
|
|
|
|
|
pattern = {
|
2024-02-15 10:16:04 -07:00
|
|
|
|
['.*/etc/foo/.*'] = 'fooscript',
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- Using an optional priority
|
2024-02-15 10:16:04 -07:00
|
|
|
|
['.*/etc/foo/.*%.conf'] = { 'dosini', { priority = 10 } },
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- A pattern containing an environment variable
|
|
|
|
|
['${XDG_CONFIG_HOME}/foo/git'] = 'git',
|
|
|
|
|
['README.(%a+)$'] = function(path, bufnr, ext)
|
|
|
|
|
if ext == 'md' then
|
|
|
|
|
return 'markdown'
|
|
|
|
|
elseif ext == 'rst' then
|
|
|
|
|
return 'rst'
|
|
|
|
|
end
|
|
|
|
|
end,
|
|
|
|
|
},
|
|
|
|
|
})
|
2022-01-04 07:28:29 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
To add a fallback match on contents, use >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.filetype.add {
|
|
|
|
|
pattern = {
|
|
|
|
|
['.*'] = {
|
|
|
|
|
function(path, bufnr)
|
|
|
|
|
local content = vim.api.nvim_buf_get_lines(bufnr, 0, 1, false)[1] or ''
|
|
|
|
|
if vim.regex([[^#!.*\\<mine\\>]]):match_str(content) ~= nil then
|
|
|
|
|
return 'mine'
|
|
|
|
|
elseif vim.regex([[\\<drawing\\>]]):match_str(content) ~= nil then
|
|
|
|
|
return 'drawing'
|
|
|
|
|
end
|
|
|
|
|
end,
|
2024-05-14 16:18:33 -07:00
|
|
|
|
{ priority = -math.huge },
|
2023-09-14 06:23:01 -07:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2022-07-03 06:31:56 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {filetypes} (`table`) A table containing new filetype maps (see
|
2022-08-11 05:25:48 -07:00
|
|
|
|
example).
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {pattern}? (`vim.filetype.mapping`)
|
|
|
|
|
• {extension}? (`vim.filetype.mapping`)
|
|
|
|
|
• {filename}? (`vim.filetype.mapping`)
|
2022-01-04 07:28:29 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
*vim.filetype.get_option()*
|
|
|
|
|
vim.filetype.get_option({filetype}, {option})
|
2023-03-11 10:11:02 -07:00
|
|
|
|
Get the default option value for a {filetype}.
|
|
|
|
|
|
|
|
|
|
The returned value is what would be set in a new buffer after 'filetype'
|
|
|
|
|
is set, meaning it should respect all FileType autocmds and ftplugin
|
|
|
|
|
files.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.filetype.get_option('vim', 'commentstring')
|
2023-03-11 10:11:02 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Note: this uses |nvim_get_option_value()| but caches the result. This
|
|
|
|
|
means |ftplugin| and |FileType| autocommands are only triggered once and
|
|
|
|
|
may not reflect later changes.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {filetype} (`string`) Filetype
|
|
|
|
|
• {option} (`string`) Option name
|
2023-03-11 10:11:02 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string|boolean|integer`) Option value
|
2023-03-11 10:11:02 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.filetype.match({args}) *vim.filetype.match()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Perform filetype detection.
|
|
|
|
|
|
|
|
|
|
The filetype can be detected using one of three methods:
|
|
|
|
|
1. Using an existing buffer
|
|
|
|
|
2. Using only a file name
|
|
|
|
|
3. Using only file contents
|
|
|
|
|
|
|
|
|
|
Of these, option 1 provides the most accurate result as it uses both the
|
|
|
|
|
buffer's filename and (optionally) the buffer contents. Options 2 and 3
|
|
|
|
|
can be used without an existing buffer, but may not always provide a match
|
|
|
|
|
in cases where the filename (or contents) cannot unambiguously determine
|
|
|
|
|
the filetype.
|
|
|
|
|
|
|
|
|
|
Each of the three options is specified using a key to the single argument
|
2023-09-14 06:23:01 -07:00
|
|
|
|
of this function. Example: >lua
|
|
|
|
|
-- Using a buffer number
|
|
|
|
|
vim.filetype.match({ buf = 42 })
|
2022-06-26 09:41:20 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- Override the filename of the given buffer
|
|
|
|
|
vim.filetype.match({ buf = 42, filename = 'foo.c' })
|
2022-11-23 04:31:49 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- Using a filename without a buffer
|
|
|
|
|
vim.filetype.match({ filename = 'main.lua' })
|
2022-06-26 09:41:20 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- Using file contents
|
|
|
|
|
vim.filetype.match({ contents = {'#!/usr/bin/env bash'} })
|
2022-06-26 09:41:20 -07:00
|
|
|
|
<
|
2022-01-17 11:28:23 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {args} (`table`) Table specifying which matching strategy to use.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Accepted keys are:
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {buf}? (`integer`) Buffer number to use for matching.
|
|
|
|
|
Mutually exclusive with {contents}
|
|
|
|
|
• {filename}? (`string`) Filename to use for matching. When
|
|
|
|
|
{buf} is given, defaults to the filename of the given buffer
|
2022-08-11 05:25:48 -07:00
|
|
|
|
number. The file need not actually exist in the filesystem.
|
|
|
|
|
When used without {buf} only the name of the file is used
|
|
|
|
|
for filetype matching. This may result in failure to detect
|
|
|
|
|
the filetype in cases where the filename alone is not enough
|
|
|
|
|
to disambiguate the filetype.
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {contents}? (`string[]`) An array of lines representing file
|
2022-08-11 05:25:48 -07:00
|
|
|
|
contents to use for matching. Can be used with {filename}.
|
|
|
|
|
Mutually exclusive with {buf}.
|
|
|
|
|
|
2023-07-06 06:32:39 -07:00
|
|
|
|
Return (multiple): ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string?`) If a match was found, the matched filetype.
|
|
|
|
|
(`function?`) A function that modifies buffer state when called (for
|
2022-08-11 05:25:48 -07:00
|
|
|
|
example, to set some filetype specific buffer variables). The function
|
|
|
|
|
accepts a buffer number as its only argument.
|
2022-06-09 12:12:36 -07:00
|
|
|
|
|
feat(lua): add vim.keymap
This introduces two new functions `vim.keymap.set` & `vim.keymap.del`
differences compared to regular set_keymap:
- remap is used as opposite of noremap. By default it's true for <Plug> keymaps and false for others.
- rhs can be lua function.
- mode can be a list of modes.
- replace_keycodes option for lua function expr maps. (Default: true)
- handles buffer specific keymaps
Examples:
```lua
vim.keymap.set('n', 'asdf', function() print("real lua function") end)
vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, {buffer=true})
vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", {silent = true, buffer = 5 })
vim.keymap.set('i', '<Tab>', function()
return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
end, {expr = true})
vim.keymap.set('n', '[%', '<Plug>(MatchitNormalMultiBackward)')
vim.keymap.del('n', 'asdf')
vim.keymap.del({'n', 'i', 'v'}, '<leader>w', {buffer = 5 })
```
2021-12-30 00:30:49 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
Lua module: vim.keymap *vim.keymap*
|
feat(lua): add vim.keymap
This introduces two new functions `vim.keymap.set` & `vim.keymap.del`
differences compared to regular set_keymap:
- remap is used as opposite of noremap. By default it's true for <Plug> keymaps and false for others.
- rhs can be lua function.
- mode can be a list of modes.
- replace_keycodes option for lua function expr maps. (Default: true)
- handles buffer specific keymaps
Examples:
```lua
vim.keymap.set('n', 'asdf', function() print("real lua function") end)
vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, {buffer=true})
vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", {silent = true, buffer = 5 })
vim.keymap.set('i', '<Tab>', function()
return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
end, {expr = true})
vim.keymap.set('n', '[%', '<Plug>(MatchitNormalMultiBackward)')
vim.keymap.del('n', 'asdf')
vim.keymap.del({'n', 'i', 'v'}, '<leader>w', {buffer = 5 })
```
2021-12-30 00:30:49 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.keymap.del({modes}, {lhs}, {opts}) *vim.keymap.del()*
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Remove an existing mapping. Examples: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.keymap.del('n', 'lhs')
|
feat(lua): add vim.keymap
This introduces two new functions `vim.keymap.set` & `vim.keymap.del`
differences compared to regular set_keymap:
- remap is used as opposite of noremap. By default it's true for <Plug> keymaps and false for others.
- rhs can be lua function.
- mode can be a list of modes.
- replace_keycodes option for lua function expr maps. (Default: true)
- handles buffer specific keymaps
Examples:
```lua
vim.keymap.set('n', 'asdf', function() print("real lua function") end)
vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, {buffer=true})
vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", {silent = true, buffer = 5 })
vim.keymap.set('i', '<Tab>', function()
return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
end, {expr = true})
vim.keymap.set('n', '[%', '<Plug>(MatchitNormalMultiBackward)')
vim.keymap.del('n', 'asdf')
vim.keymap.del({'n', 'i', 'v'}, '<leader>w', {buffer = 5 })
```
2021-12-30 00:30:49 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
vim.keymap.del({'n', 'i', 'v'}, '<leader>w', { buffer = 5 })
|
feat(lua): add vim.keymap
This introduces two new functions `vim.keymap.set` & `vim.keymap.del`
differences compared to regular set_keymap:
- remap is used as opposite of noremap. By default it's true for <Plug> keymaps and false for others.
- rhs can be lua function.
- mode can be a list of modes.
- replace_keycodes option for lua function expr maps. (Default: true)
- handles buffer specific keymaps
Examples:
```lua
vim.keymap.set('n', 'asdf', function() print("real lua function") end)
vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, {buffer=true})
vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", {silent = true, buffer = 5 })
vim.keymap.set('i', '<Tab>', function()
return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
end, {expr = true})
vim.keymap.set('n', '[%', '<Plug>(MatchitNormalMultiBackward)')
vim.keymap.del('n', 'asdf')
vim.keymap.del({'n', 'i', 'v'}, '<leader>w', {buffer = 5 })
```
2021-12-30 00:30:49 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-02-15 10:16:04 -07:00
|
|
|
|
• {modes} (`string|string[]`)
|
|
|
|
|
• {lhs} (`string`)
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {opts} (`table?`) A table with the following fields:
|
|
|
|
|
• {buffer}? (`integer|boolean`) Remove a mapping from the
|
|
|
|
|
given buffer. When `0` or `true`, use the current buffer.
|
feat(lua): add vim.keymap
This introduces two new functions `vim.keymap.set` & `vim.keymap.del`
differences compared to regular set_keymap:
- remap is used as opposite of noremap. By default it's true for <Plug> keymaps and false for others.
- rhs can be lua function.
- mode can be a list of modes.
- replace_keycodes option for lua function expr maps. (Default: true)
- handles buffer specific keymaps
Examples:
```lua
vim.keymap.set('n', 'asdf', function() print("real lua function") end)
vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, {buffer=true})
vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", {silent = true, buffer = 5 })
vim.keymap.set('i', '<Tab>', function()
return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
end, {expr = true})
vim.keymap.set('n', '[%', '<Plug>(MatchitNormalMultiBackward)')
vim.keymap.del('n', 'asdf')
vim.keymap.del({'n', 'i', 'v'}, '<leader>w', {buffer = 5 })
```
2021-12-30 00:30:49 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• |vim.keymap.set()|
|
feat(lua): add vim.keymap
This introduces two new functions `vim.keymap.set` & `vim.keymap.del`
differences compared to regular set_keymap:
- remap is used as opposite of noremap. By default it's true for <Plug> keymaps and false for others.
- rhs can be lua function.
- mode can be a list of modes.
- replace_keycodes option for lua function expr maps. (Default: true)
- handles buffer specific keymaps
Examples:
```lua
vim.keymap.set('n', 'asdf', function() print("real lua function") end)
vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, {buffer=true})
vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", {silent = true, buffer = 5 })
vim.keymap.set('i', '<Tab>', function()
return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
end, {expr = true})
vim.keymap.set('n', '[%', '<Plug>(MatchitNormalMultiBackward)')
vim.keymap.del('n', 'asdf')
vim.keymap.del({'n', 'i', 'v'}, '<leader>w', {buffer = 5 })
```
2021-12-30 00:30:49 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.keymap.set({mode}, {lhs}, {rhs}, {opts}) *vim.keymap.set()*
|
2022-12-14 11:58:18 -07:00
|
|
|
|
Adds a new |mapping|. Examples: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- Map to a Lua function:
|
|
|
|
|
vim.keymap.set('n', 'lhs', function() print("real lua function") end)
|
|
|
|
|
-- Map to multiple modes:
|
|
|
|
|
vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, { buffer = true })
|
|
|
|
|
-- Buffer-local mapping:
|
|
|
|
|
vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", { silent = true, buffer = 5 })
|
|
|
|
|
-- Expr mapping:
|
|
|
|
|
vim.keymap.set('i', '<Tab>', function()
|
|
|
|
|
return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
|
|
|
|
|
end, { expr = true })
|
|
|
|
|
-- <Plug> mapping:
|
|
|
|
|
vim.keymap.set('n', '[%%', '<Plug>(MatchitNormalMultiBackward)')
|
feat(lua): add vim.keymap
This introduces two new functions `vim.keymap.set` & `vim.keymap.del`
differences compared to regular set_keymap:
- remap is used as opposite of noremap. By default it's true for <Plug> keymaps and false for others.
- rhs can be lua function.
- mode can be a list of modes.
- replace_keycodes option for lua function expr maps. (Default: true)
- handles buffer specific keymaps
Examples:
```lua
vim.keymap.set('n', 'asdf', function() print("real lua function") end)
vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, {buffer=true})
vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", {silent = true, buffer = 5 })
vim.keymap.set('i', '<Tab>', function()
return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
end, {expr = true})
vim.keymap.set('n', '[%', '<Plug>(MatchitNormalMultiBackward)')
vim.keymap.del('n', 'asdf')
vim.keymap.del({'n', 'i', 'v'}, '<leader>w', {buffer = 5 })
```
2021-12-30 00:30:49 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {mode} (`string|string[]`) Mode short-name, see |nvim_set_keymap()|.
|
|
|
|
|
Can also be list of modes to create mapping on multiple modes.
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {lhs} (`string`) Left-hand side |{lhs}| of the mapping.
|
|
|
|
|
• {rhs} (`string|function`) Right-hand side |{rhs}| of the mapping,
|
|
|
|
|
can be a Lua function.
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {opts} (`table?`) Table of |:map-arguments|. Same as
|
|
|
|
|
|nvim_set_keymap()| {opts}, except:
|
|
|
|
|
• {replace_keycodes} defaults to `true` if "expr" is `true`.
|
|
|
|
|
|
|
|
|
|
Also accepts:
|
|
|
|
|
• {buffer}? (`integer|boolean`) Creates buffer-local mapping,
|
|
|
|
|
`0` or `true` for current buffer.
|
|
|
|
|
• {remap}? (`boolean`, default: `false`) Make the mapping
|
|
|
|
|
recursive. Inverse of {noremap}.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• |nvim_set_keymap()|
|
2023-07-13 05:43:36 -07:00
|
|
|
|
• |maparg()|
|
|
|
|
|
• |mapcheck()|
|
|
|
|
|
• |mapset()|
|
feat(lua): add vim.keymap
This introduces two new functions `vim.keymap.set` & `vim.keymap.del`
differences compared to regular set_keymap:
- remap is used as opposite of noremap. By default it's true for <Plug> keymaps and false for others.
- rhs can be lua function.
- mode can be a list of modes.
- replace_keycodes option for lua function expr maps. (Default: true)
- handles buffer specific keymaps
Examples:
```lua
vim.keymap.set('n', 'asdf', function() print("real lua function") end)
vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, {buffer=true})
vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", {silent = true, buffer = 5 })
vim.keymap.set('i', '<Tab>', function()
return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
end, {expr = true})
vim.keymap.set('n', '[%', '<Plug>(MatchitNormalMultiBackward)')
vim.keymap.del('n', 'asdf')
vim.keymap.del({'n', 'i', 'v'}, '<leader>w', {buffer = 5 })
```
2021-12-30 00:30:49 -07:00
|
|
|
|
|
2022-05-15 13:38:19 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
Lua module: vim.fs *vim.fs*
|
2022-05-15 13:38:19 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.fs.basename({file}) *vim.fs.basename()*
|
2023-07-19 09:55:35 -07:00
|
|
|
|
Return the basename of the given path
|
2022-05-15 18:55:18 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-03-06 09:18:00 -07:00
|
|
|
|
• {file} (`string?`) Path
|
2022-05-15 18:55:18 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string?`) Basename of {file}
|
2022-05-15 18:55:18 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.fs.dir({path}, {opts}) *vim.fs.dir()*
|
2023-07-19 09:55:35 -07:00
|
|
|
|
Return an iterator over the items located in {path}
|
2022-05-15 19:10:12 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {path} (`string`) An absolute or relative path to the directory to
|
2022-08-11 05:25:48 -07:00
|
|
|
|
iterate over. The path is first normalized
|
|
|
|
|
|vim.fs.normalize()|.
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {opts} (`table?`) Optional keyword arguments:
|
2022-12-13 06:59:31 -07:00
|
|
|
|
• depth: integer|nil How deep the traverse (default 1)
|
|
|
|
|
• skip: (fun(dir_name: string): boolean)|nil Predicate to
|
|
|
|
|
control traversal. Return false to stop searching the
|
|
|
|
|
current directory. Only useful when depth > 1
|
2022-05-15 19:10:12 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`Iterator`) over items in {path}. Each iteration yields two values:
|
2023-07-19 09:55:35 -07:00
|
|
|
|
"name" and "type". "name" is the basename of the item relative to
|
|
|
|
|
{path}. "type" is one of the following: "file", "directory", "link",
|
|
|
|
|
"fifo", "socket", "char", "block", "unknown".
|
2022-05-15 19:10:12 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.fs.dirname({file}) *vim.fs.dirname()*
|
2023-07-19 09:55:35 -07:00
|
|
|
|
Return the parent directory of the given path
|
2022-05-15 18:53:23 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-03-06 09:18:00 -07:00
|
|
|
|
• {file} (`string?`) Path
|
2022-05-15 18:53:23 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string?`) Parent directory of {file}
|
2022-05-15 18:53:23 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.fs.find({names}, {opts}) *vim.fs.find()*
|
2023-07-19 09:55:35 -07:00
|
|
|
|
Find files or directories (or other items as specified by `opts.type`) in
|
|
|
|
|
the given path.
|
|
|
|
|
|
|
|
|
|
Finds items given in {names} starting from {path}. If {upward} is "true"
|
|
|
|
|
then the search traverses upward through parent directories; otherwise,
|
|
|
|
|
the search traverses downward. Note that downward searches are recursive
|
|
|
|
|
and may search through many directories! If {stop} is non-nil, then the
|
|
|
|
|
search stops when the directory given in {stop} is reached. The search
|
|
|
|
|
terminates when {limit} (default 1) matches are found. You can set {type}
|
|
|
|
|
to "file", "directory", "link", "socket", "char", "block", or "fifo" to
|
|
|
|
|
narrow the search to find only that type.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
2023-03-01 09:51:22 -07:00
|
|
|
|
Examples: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- list all test directories under the runtime directory
|
|
|
|
|
local test_dirs = vim.fs.find(
|
|
|
|
|
{'test', 'tst', 'testdir'},
|
|
|
|
|
{limit = math.huge, type = 'directory', path = './runtime/'}
|
|
|
|
|
)
|
2023-03-01 09:51:22 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
-- get all files ending with .cpp or .hpp inside lib/
|
|
|
|
|
local cpp_hpp = vim.fs.find(function(name, path)
|
|
|
|
|
return name:match('.*%.[ch]pp$') and path:match('[/\\\\]lib$')
|
|
|
|
|
end, {limit = math.huge, type = 'file'})
|
2023-03-01 09:51:22 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {names} (`string|string[]|fun(name: string, path: string): boolean`)
|
2023-08-08 03:58:29 -07:00
|
|
|
|
Names of the items to find. Must be base names, paths and
|
|
|
|
|
globs are not supported when {names} is a string or a table.
|
|
|
|
|
If {names} is a function, it is called for each traversed
|
|
|
|
|
item with args:
|
2023-03-01 09:51:22 -07:00
|
|
|
|
• name: base name of the current item
|
|
|
|
|
• path: full path of the current item The function should
|
2023-07-19 09:55:35 -07:00
|
|
|
|
return `true` if the given item is considered a match.
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {opts} (`table`) Optional keyword arguments:
|
2024-03-06 03:03:55 -07:00
|
|
|
|
• {path}? (`string`) Path to begin searching from. If
|
|
|
|
|
omitted, the |current-directory| is used.
|
|
|
|
|
• {upward}? (`boolean`, default: `false`) Search upward
|
2022-08-11 05:25:48 -07:00
|
|
|
|
through parent directories. Otherwise, search through child
|
|
|
|
|
directories (recursively).
|
2024-03-06 03:03:55 -07:00
|
|
|
|
• {stop}? (`string`) Stop searching when this directory is
|
2022-08-11 05:25:48 -07:00
|
|
|
|
reached. The directory itself is not searched.
|
2024-03-06 03:03:55 -07:00
|
|
|
|
• {type}? (`string`) Find only items of the given type. If
|
2023-07-19 09:55:35 -07:00
|
|
|
|
omitted, all items that match {names} are included.
|
2024-03-06 03:03:55 -07:00
|
|
|
|
• {limit}? (`number`, default: `1`) Stop the search after
|
2024-02-27 08:20:32 -07:00
|
|
|
|
finding this many matches. Use `math.huge` to place no
|
|
|
|
|
limit on the number of matches.
|
2022-08-11 05:25:48 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string[]`) Normalized paths |vim.fs.normalize()| of all matching
|
|
|
|
|
items
|
2022-05-15 19:37:35 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.fs.joinpath({...}) *vim.fs.joinpath()*
|
2023-06-09 18:37:05 -07:00
|
|
|
|
Concatenate directories and/or file paths into a single path with
|
|
|
|
|
normalization (e.g., `"foo/"` and `"bar"` get joined to `"foo/bar"`)
|
2023-05-20 08:30:48 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {...} (`string`)
|
2023-05-20 08:30:48 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`)
|
2023-05-20 08:30:48 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.fs.normalize({path}, {opts}) *vim.fs.normalize()*
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Normalize a path to a standard format. A tilde (~) character at the
|
2024-03-29 09:23:01 -07:00
|
|
|
|
beginning of the path is expanded to the user's home directory and
|
2024-04-16 12:13:44 -07:00
|
|
|
|
environment variables are also expanded. "." and ".." components are also
|
|
|
|
|
resolved, except when the path is relative and trying to resolve it would
|
|
|
|
|
result in an absolute path.
|
|
|
|
|
• "." as the only part in a relative path:
|
|
|
|
|
• "." => "."
|
|
|
|
|
• "././" => "."
|
|
|
|
|
• ".." when it leads outside the current directory
|
|
|
|
|
• "foo/../../bar" => "../bar"
|
|
|
|
|
• "../../foo" => "../../foo"
|
|
|
|
|
• ".." in the root directory returns the root directory.
|
|
|
|
|
• "/../../" => "/"
|
2024-03-29 09:23:01 -07:00
|
|
|
|
|
|
|
|
|
On Windows, backslash (\) characters are converted to forward slashes (/).
|
2022-05-17 07:49:33 -07:00
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Examples: >lua
|
2024-04-16 12:13:44 -07:00
|
|
|
|
[[C:\Users\jdoe]] => "C:/Users/jdoe"
|
|
|
|
|
"~/src/neovim" => "/home/jdoe/src/neovim"
|
|
|
|
|
"$XDG_CONFIG_HOME/nvim/init.vim" => "/Users/jdoe/.config/nvim/init.vim"
|
|
|
|
|
"~/src/nvim/api/../tui/./tui.c" => "/home/jdoe/src/nvim/tui/tui.c"
|
|
|
|
|
"./foo/bar" => "foo/bar"
|
|
|
|
|
"foo/../../../bar" => "../../bar"
|
|
|
|
|
"/home/jdoe/../../../bar" => "/bar"
|
|
|
|
|
"C:foo/../../baz" => "C:../baz"
|
|
|
|
|
"C:/foo/../../baz" => "C:/baz"
|
|
|
|
|
[[\\?\UNC\server\share\foo\..\..\..\bar]] => "//?/UNC/server/share/bar"
|
2022-05-17 07:49:33 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {path} (`string`) Path to normalize
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {opts} (`table?`) A table with the following fields:
|
2024-04-16 12:13:44 -07:00
|
|
|
|
• {expand_env}? (`boolean`, default: `true`) Expand
|
|
|
|
|
environment variables.
|
|
|
|
|
• {win}? (`boolean`, default: `true` in Windows, `false`
|
|
|
|
|
otherwise) Path is a Windows path.
|
2022-05-17 07:49:33 -07:00
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`) Normalized path
|
2022-05-17 07:49:33 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.fs.parents({start}) *vim.fs.parents()*
|
2023-07-19 09:55:35 -07:00
|
|
|
|
Iterate over all the parents of the given path.
|
2022-05-15 13:38:19 -07:00
|
|
|
|
|
2022-11-23 04:31:49 -07:00
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
local root_dir
|
|
|
|
|
for dir in vim.fs.parents(vim.api.nvim_buf_get_name(0)) do
|
|
|
|
|
if vim.fn.isdirectory(dir .. "/.git") == 1 then
|
|
|
|
|
root_dir = dir
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
2022-05-15 13:38:19 -07:00
|
|
|
|
|
2023-09-14 06:23:01 -07:00
|
|
|
|
if root_dir then
|
|
|
|
|
print("Found git repository at", root_dir)
|
|
|
|
|
end
|
2022-05-15 13:38:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {start} (`string`) Initial path.
|
2022-05-15 13:38:19 -07:00
|
|
|
|
|
2023-08-08 03:58:29 -07:00
|
|
|
|
Return (multiple): ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`fun(_, dir: string): string?`) Iterator
|
|
|
|
|
(`nil`)
|
|
|
|
|
(`string?`)
|
2022-05-15 13:38:19 -07:00
|
|
|
|
|
2024-04-24 19:43:46 -07:00
|
|
|
|
vim.fs.root({source}, {marker}) *vim.fs.root()*
|
|
|
|
|
Find the first parent directory containing a specific "marker", relative
|
2024-05-24 08:48:32 -07:00
|
|
|
|
to a file path or buffer.
|
|
|
|
|
|
|
|
|
|
If the buffer is unnamed (has no backing file) or has a non-empty
|
|
|
|
|
'buftype' then the search begins from Nvim's |current-directory|.
|
2024-04-24 19:43:46 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
-- Find the root of a Python project, starting from file 'main.py'
|
|
|
|
|
vim.fs.root(vim.fs.joinpath(vim.env.PWD, 'main.py'), {'pyproject.toml', 'setup.py' })
|
|
|
|
|
|
|
|
|
|
-- Find the root of a git repository
|
|
|
|
|
vim.fs.root(0, '.git')
|
|
|
|
|
|
|
|
|
|
-- Find the parent directory containing any file with a .csproj extension
|
|
|
|
|
vim.fs.root(0, function(name, path)
|
|
|
|
|
return name:match('%.csproj$') ~= nil
|
|
|
|
|
end)
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {source} (`integer|string`) Buffer number (0 for current buffer) or
|
2024-05-24 08:48:32 -07:00
|
|
|
|
file path (absolute or relative to the |current-directory|)
|
|
|
|
|
to begin the search from.
|
2024-04-24 19:43:46 -07:00
|
|
|
|
• {marker} (`string|string[]|fun(name: string, path: string): boolean`)
|
|
|
|
|
A marker, or list of markers, to search for. If a function,
|
|
|
|
|
the function is called for each evaluated item and should
|
|
|
|
|
return true if {name} and {path} are a match.
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`string?`) Directory path containing one of the given markers, or nil
|
|
|
|
|
if no directory was found.
|
|
|
|
|
|
2022-11-05 12:37:05 -07:00
|
|
|
|
|
2024-01-02 06:32:43 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
Lua module: vim.glob *vim.glob*
|
|
|
|
|
|
|
|
|
|
vim.glob.to_lpeg({pattern}) *vim.glob.to_lpeg()*
|
|
|
|
|
Parses a raw glob into an |lua-lpeg| pattern.
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
This uses glob semantics from LSP 3.17.0:
|
|
|
|
|
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#pattern
|
2024-01-02 06:32:43 -07:00
|
|
|
|
|
|
|
|
|
Glob patterns can have the following syntax:
|
|
|
|
|
• `*` to match one or more characters in a path segment
|
|
|
|
|
• `?` to match on one character in a path segment
|
|
|
|
|
• `**` to match any number of path segments, including none
|
|
|
|
|
• `{}` to group conditions (e.g. `*.{ts,js}` matches TypeScript and
|
|
|
|
|
JavaScript files)
|
|
|
|
|
• `[]` to declare a range of characters to match in a path segment (e.g.,
|
|
|
|
|
`example.[0-9]` to match on `example.0`, `example.1`, …)
|
|
|
|
|
• `[!...]` to negate a range of characters to match in a path segment
|
|
|
|
|
(e.g., `example.[!0-9]` to match on `example.a`, `example.b`, but not
|
|
|
|
|
`example.0`)
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {pattern} (`string`) The raw glob pattern
|
2024-01-02 06:32:43 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`vim.lpeg.Pattern`) pattern An |lua-lpeg| representation of the
|
|
|
|
|
pattern
|
2024-01-02 06:32:43 -07:00
|
|
|
|
|
|
|
|
|
|
2024-01-11 10:24:44 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
VIM.LPEG *vim.lpeg*
|
|
|
|
|
|
|
|
|
|
|
2024-05-02 06:57:21 -07:00
|
|
|
|
LPeg is a pattern-matching library for Lua, based on Parsing Expression
|
|
|
|
|
Grammars (PEGs). https://bford.info/packrat/
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
2024-05-02 06:57:21 -07:00
|
|
|
|
*lua-lpeg* *vim.lpeg.Pattern*
|
2024-01-11 10:24:44 -07:00
|
|
|
|
The LPeg library for parsing expression grammars is included as `vim.lpeg`
|
|
|
|
|
(https://www.inf.puc-rio.br/~roberto/lpeg/).
|
|
|
|
|
|
|
|
|
|
In addition, its regex-like interface is available as |vim.re|
|
|
|
|
|
(https://www.inf.puc-rio.br/~roberto/lpeg/re.html).
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|
|
|
|
|
|
2024-07-15 12:55:57 -07:00
|
|
|
|
Pattern:match({subject}, {init}, {...}) *Pattern:match()*
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Matches the given `pattern` against the `subject` string. If the match
|
|
|
|
|
succeeds, returns the index in the subject of the first character after
|
|
|
|
|
the match, or the captured values (if the pattern captured any value). An
|
|
|
|
|
optional numeric argument `init` makes the match start at that position in
|
|
|
|
|
the subject string. As usual in Lua libraries, a negative value counts
|
|
|
|
|
from the end. Unlike typical pattern-matching functions, `match` works
|
|
|
|
|
only in anchored mode; that is, it tries to match the pattern with a
|
|
|
|
|
prefix of the given subject string (at position `init`), not with an
|
|
|
|
|
arbitrary substring of the subject. So, if we want to find a pattern
|
|
|
|
|
anywhere in a string, we must either write a loop in Lua or write a
|
|
|
|
|
pattern that matches anywhere.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
2024-02-09 11:35:13 -07:00
|
|
|
|
local pattern = lpeg.R('az') ^ 1 * -1
|
|
|
|
|
assert(pattern:match('hello') == 6)
|
|
|
|
|
assert(lpeg.match(pattern, 'hello') == 6)
|
|
|
|
|
assert(pattern:match('1 hello') == nil)
|
2024-02-15 10:16:04 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {subject} (`string`)
|
|
|
|
|
• {init} (`integer?`)
|
2024-07-15 12:55:57 -07:00
|
|
|
|
• {...} (`any`)
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-07-15 12:55:57 -07:00
|
|
|
|
(`any`) ...
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|
2024-01-11 10:24:44 -07:00
|
|
|
|
vim.lpeg.B({pattern}) *vim.lpeg.B()*
|
|
|
|
|
Returns a pattern that matches only if the input string at the current
|
|
|
|
|
position is preceded by `patt`. Pattern `patt` must match only strings
|
2024-02-09 11:35:13 -07:00
|
|
|
|
with some fixed length, and it cannot contain captures. Like the `and`
|
2024-01-11 10:24:44 -07:00
|
|
|
|
predicate, this pattern never consumes any input, independently of success
|
|
|
|
|
or failure.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-07-15 12:55:57 -07:00
|
|
|
|
• {pattern} (`vim.lpeg.Pattern|string|integer|boolean|table`)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Pattern`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.C({patt}) *vim.lpeg.C()*
|
|
|
|
|
Creates a simple capture, which captures the substring of the subject that
|
|
|
|
|
matches `patt`. The captured value is a string. If `patt` has other
|
|
|
|
|
captures, their values are returned after this one.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
local function split (s, sep)
|
|
|
|
|
sep = lpeg.P(sep)
|
2024-02-09 11:35:13 -07:00
|
|
|
|
local elem = lpeg.C((1 - sep) ^ 0)
|
|
|
|
|
local p = elem * (sep * elem) ^ 0
|
2024-01-11 10:24:44 -07:00
|
|
|
|
return lpeg.match(p, s)
|
|
|
|
|
end
|
|
|
|
|
local a, b, c = split('a,b,c', ',')
|
|
|
|
|
assert(a == 'a')
|
|
|
|
|
assert(b == 'b')
|
|
|
|
|
assert(c == 'c')
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-07-15 12:55:57 -07:00
|
|
|
|
• {patt} (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Capture`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.Carg({n}) *vim.lpeg.Carg()*
|
|
|
|
|
Creates an argument capture. This pattern matches the empty string and
|
2024-02-15 10:16:04 -07:00
|
|
|
|
produces the value given as the nth extra argument given in the call to
|
|
|
|
|
`lpeg.match`.
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {n} (`integer`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Capture`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.Cb({name}) *vim.lpeg.Cb()*
|
|
|
|
|
Creates a back capture. This pattern matches the empty string and produces
|
|
|
|
|
the values produced by the most recent group capture named `name` (where
|
|
|
|
|
`name` can be any Lua value). Most recent means the last complete
|
|
|
|
|
outermost group capture with the given name. A Complete capture means that
|
|
|
|
|
the entire pattern corresponding to the capture has matched. An Outermost
|
|
|
|
|
capture means that the capture is not inside another complete capture. In
|
|
|
|
|
the same way that LPeg does not specify when it evaluates captures, it
|
|
|
|
|
does not specify whether it reuses values previously produced by the group
|
|
|
|
|
or re-evaluates them.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {name} (`any`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Capture`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.Cc({...}) *vim.lpeg.Cc()*
|
|
|
|
|
Creates a constant capture. This pattern matches the empty string and
|
|
|
|
|
produces all given values as its captured values.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {...} (`any`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Capture`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.Cf({patt}, {func}) *vim.lpeg.Cf()*
|
|
|
|
|
Creates a fold capture. If `patt` produces a list of captures C1 C2 ...
|
2024-03-08 05:25:18 -07:00
|
|
|
|
Cn, this capture will produce the value
|
|
|
|
|
`func(...func(func(C1, C2), C3)...,Cn)`, that is, it will fold (or
|
|
|
|
|
accumulate, or reduce) the captures from `patt` using function `func`.
|
|
|
|
|
This capture assumes that `patt` should produce at least one capture with
|
|
|
|
|
at least one value (of any type), which becomes the initial value of an
|
|
|
|
|
accumulator. (If you need a specific initial value, you may prefix a
|
2024-07-15 12:55:57 -07:00
|
|
|
|
constant capture to `patt`.) For each subsequent capture, LPeg calls
|
|
|
|
|
`func` with this accumulator as the first argument and all values produced
|
|
|
|
|
by the capture as extra arguments; the first result from this call becomes
|
|
|
|
|
the new value for the accumulator. The final value of the accumulator
|
|
|
|
|
becomes the captured value.
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2024-02-09 11:35:13 -07:00
|
|
|
|
local number = lpeg.R('09') ^ 1 / tonumber
|
|
|
|
|
local list = number * (',' * number) ^ 0
|
2024-01-11 10:24:44 -07:00
|
|
|
|
local function add(acc, newvalue) return acc + newvalue end
|
|
|
|
|
local sum = lpeg.Cf(list, add)
|
2024-02-09 11:35:13 -07:00
|
|
|
|
assert(sum:match('10,30,43') == 83)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-07-15 12:55:57 -07:00
|
|
|
|
• {patt} (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
• {func} (`fun(acc, newvalue)`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Capture`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.Cg({patt}, {name}) *vim.lpeg.Cg()*
|
|
|
|
|
Creates a group capture. It groups all values returned by `patt` into a
|
|
|
|
|
single capture. The group may be anonymous (if no name is given) or named
|
|
|
|
|
with the given name (which can be any non-nil Lua value).
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-07-15 12:55:57 -07:00
|
|
|
|
• {patt} (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
• {name} (`string?`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Capture`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.Cmt({patt}, {fn}) *vim.lpeg.Cmt()*
|
|
|
|
|
Creates a match-time capture. Unlike all other captures, this one is
|
|
|
|
|
evaluated immediately when a match occurs (even if it is part of a larger
|
|
|
|
|
pattern that fails later). It forces the immediate evaluation of all its
|
|
|
|
|
nested captures and then calls `function`. The given function gets as
|
|
|
|
|
arguments the entire subject, the current position (after the match of
|
|
|
|
|
`patt`), plus any capture values produced by `patt`. The first value
|
|
|
|
|
returned by `function` defines how the match happens. If the call returns
|
|
|
|
|
a number, the match succeeds and the returned number becomes the new
|
2024-02-09 11:35:13 -07:00
|
|
|
|
current position. (Assuming a subject sand current position `i`, the
|
|
|
|
|
returned number must be in the range `[i, len(s) + 1]`.) If the call
|
|
|
|
|
returns `true`, the match succeeds without consuming any input (so, to
|
|
|
|
|
return true is equivalent to return `i`). If the call returns `false`,
|
|
|
|
|
`nil`, or no value, the match fails. Any extra values returned by the
|
|
|
|
|
function become the values produced by the capture.
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-07-15 12:55:57 -07:00
|
|
|
|
• {patt} (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
|
|
|
|
|
• {fn} (`fun(s: string, i: integer, ...: any)`) (position:
|
|
|
|
|
boolean|integer, ...: any)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Capture`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.Cp() *vim.lpeg.Cp()*
|
|
|
|
|
Creates a position capture. It matches the empty string and captures the
|
|
|
|
|
position in the subject where the match occurs. The captured value is a
|
|
|
|
|
number.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
local I = lpeg.Cp()
|
|
|
|
|
local function anywhere(p) return lpeg.P({I * p * I + 1 * lpeg.V(1)}) end
|
2024-02-09 11:35:13 -07:00
|
|
|
|
local match_start, match_end = anywhere('world'):match('hello world!')
|
2024-01-11 10:24:44 -07:00
|
|
|
|
assert(match_start == 7)
|
|
|
|
|
assert(match_end == 12)
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Capture`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.Cs({patt}) *vim.lpeg.Cs()*
|
|
|
|
|
Creates a substitution capture. This function creates a substitution
|
|
|
|
|
capture, which captures the substring of the subject that matches `patt`,
|
|
|
|
|
with substitutions. For any capture inside `patt` with a value, the
|
|
|
|
|
substring that matched the capture is replaced by the capture value (which
|
|
|
|
|
should be a string). The final captured value is the string resulting from
|
|
|
|
|
all replacements.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
local function gsub (s, patt, repl)
|
|
|
|
|
patt = lpeg.P(patt)
|
2024-02-09 11:35:13 -07:00
|
|
|
|
patt = lpeg.Cs((patt / repl + 1) ^ 0)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
return lpeg.match(patt, s)
|
|
|
|
|
end
|
|
|
|
|
assert(gsub('Hello, xxx!', 'xxx', 'World') == 'Hello, World!')
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-07-15 12:55:57 -07:00
|
|
|
|
• {patt} (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Capture`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.Ct({patt}) *vim.lpeg.Ct()*
|
|
|
|
|
Creates a table capture. This capture returns a table with all values from
|
|
|
|
|
all anonymous captures made by `patt` inside this table in successive
|
|
|
|
|
integer keys, starting at 1. Moreover, for each named capture group
|
|
|
|
|
created by `patt`, the first value of the group is put into the table with
|
|
|
|
|
the group name as its key. The captured value is only the table.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-07-15 12:55:57 -07:00
|
|
|
|
• {patt} (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Capture`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.locale({tab}) *vim.lpeg.locale()*
|
|
|
|
|
Returns a table with patterns for matching some character classes
|
|
|
|
|
according to the current locale. The table has fields named `alnum`,
|
|
|
|
|
`alpha`, `cntrl`, `digit`, `graph`, `lower`, `print`, `punct`, `space`,
|
|
|
|
|
`upper`, and `xdigit`, each one containing a correspondent pattern. Each
|
|
|
|
|
pattern matches any single character that belongs to its class. If called
|
|
|
|
|
with an argument `table`, then it creates those fields inside the given
|
|
|
|
|
table and returns that table.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
lpeg.locale(lpeg)
|
2024-02-09 11:35:13 -07:00
|
|
|
|
local space = lpeg.space ^ 0
|
|
|
|
|
local name = lpeg.C(lpeg.alpha ^ 1) * space
|
|
|
|
|
local sep = lpeg.S(',;') * space
|
|
|
|
|
local pair = lpeg.Cg(name * '=' * space * name) * sep ^ -1
|
|
|
|
|
local list = lpeg.Cf(lpeg.Ct('') * pair ^ 0, rawset)
|
|
|
|
|
local t = list:match('a=b, c = hi; next = pi')
|
2024-02-15 10:16:04 -07:00
|
|
|
|
assert(t.a == 'b')
|
|
|
|
|
assert(t.c == 'hi')
|
|
|
|
|
assert(t.next == 'pi')
|
|
|
|
|
local locale = lpeg.locale()
|
|
|
|
|
assert(type(locale.digit) == 'userdata')
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {tab} (`table?`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Locale`)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
2024-07-15 12:55:57 -07:00
|
|
|
|
vim.lpeg.match({pattern}, {subject}, {init}, {...}) *vim.lpeg.match()*
|
2024-01-11 10:24:44 -07:00
|
|
|
|
Matches the given `pattern` against the `subject` string. If the match
|
|
|
|
|
succeeds, returns the index in the subject of the first character after
|
|
|
|
|
the match, or the captured values (if the pattern captured any value). An
|
|
|
|
|
optional numeric argument `init` makes the match start at that position in
|
|
|
|
|
the subject string. As usual in Lua libraries, a negative value counts
|
|
|
|
|
from the end. Unlike typical pattern-matching functions, `match` works
|
|
|
|
|
only in anchored mode; that is, it tries to match the pattern with a
|
|
|
|
|
prefix of the given subject string (at position `init`), not with an
|
|
|
|
|
arbitrary substring of the subject. So, if we want to find a pattern
|
|
|
|
|
anywhere in a string, we must either write a loop in Lua or write a
|
|
|
|
|
pattern that matches anywhere.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
2024-02-09 11:35:13 -07:00
|
|
|
|
local pattern = lpeg.R('az') ^ 1 * -1
|
|
|
|
|
assert(pattern:match('hello') == 6)
|
|
|
|
|
assert(lpeg.match(pattern, 'hello') == 6)
|
|
|
|
|
assert(pattern:match('1 hello') == nil)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-07-15 12:55:57 -07:00
|
|
|
|
• {pattern} (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
• {subject} (`string`)
|
|
|
|
|
• {init} (`integer?`)
|
2024-07-15 12:55:57 -07:00
|
|
|
|
• {...} (`any`)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-07-15 12:55:57 -07:00
|
|
|
|
(`any`) ...
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
vim.lpeg.P({value}) *vim.lpeg.P()*
|
2024-02-09 11:35:13 -07:00
|
|
|
|
Converts the given value into a proper pattern. The following rules are
|
2024-01-11 10:24:44 -07:00
|
|
|
|
applied:
|
|
|
|
|
• If the argument is a pattern, it is returned unmodified.
|
|
|
|
|
• If the argument is a string, it is translated to a pattern that matches
|
|
|
|
|
the string literally.
|
|
|
|
|
• If the argument is a non-negative number `n`, the result is a pattern
|
|
|
|
|
that matches exactly `n` characters.
|
|
|
|
|
• If the argument is a negative number `-n`, the result is a pattern that
|
|
|
|
|
succeeds only if the input string has less than `n` characters left:
|
|
|
|
|
`lpeg.P(-n)` is equivalent to `-lpeg.P(n)` (see the unary minus
|
|
|
|
|
operation).
|
|
|
|
|
• If the argument is a boolean, the result is a pattern that always
|
|
|
|
|
succeeds or always fails (according to the boolean value), without
|
|
|
|
|
consuming any input.
|
|
|
|
|
• If the argument is a table, it is interpreted as a grammar (see
|
|
|
|
|
Grammars).
|
|
|
|
|
• If the argument is a function, returns a pattern equivalent to a
|
2024-02-09 11:35:13 -07:00
|
|
|
|
match-time capture over the empty string.
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {value} (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Pattern`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.R({...}) *vim.lpeg.R()*
|
|
|
|
|
Returns a pattern that matches any single character belonging to one of
|
|
|
|
|
the given ranges. Each `range` is a string `xy` of length 2, representing
|
|
|
|
|
all characters with code between the codes of `x` and `y` (both
|
2024-02-28 15:14:49 -07:00
|
|
|
|
inclusive). As an example, the pattern `lpeg.R('09')` matches any digit,
|
|
|
|
|
and `lpeg.R('az', 'AZ')` matches any ASCII letter.
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2024-02-09 11:35:13 -07:00
|
|
|
|
local pattern = lpeg.R('az') ^ 1 * -1
|
|
|
|
|
assert(pattern:match('hello') == 6)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {...} (`string`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Pattern`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.S({string}) *vim.lpeg.S()*
|
|
|
|
|
Returns a pattern that matches any single character that appears in the
|
|
|
|
|
given string (the `S` stands for Set). As an example, the pattern
|
2024-02-28 15:14:49 -07:00
|
|
|
|
`lpeg.S('+-*/')` matches any arithmetic operator. Note that, if `s` is a
|
2024-02-15 10:16:04 -07:00
|
|
|
|
character (that is, a string of length 1), then `lpeg.P(s)` is equivalent
|
|
|
|
|
to `lpeg.S(s)` which is equivalent to `lpeg.R(s..s)`. Note also that both
|
2024-02-28 15:14:49 -07:00
|
|
|
|
`lpeg.S('')` and `lpeg.R()` are patterns that always fail.
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {string} (`string`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Pattern`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.setmaxstack({max}) *vim.lpeg.setmaxstack()*
|
|
|
|
|
Sets a limit for the size of the backtrack stack used by LPeg to track
|
|
|
|
|
calls and choices. The default limit is `400`. Most well-written patterns
|
|
|
|
|
need little backtrack levels and therefore you seldom need to change this
|
|
|
|
|
limit; before changing it you should try to rewrite your pattern to avoid
|
|
|
|
|
the need for extra space. Nevertheless, a few useful patterns may
|
|
|
|
|
overflow. Also, with recursive grammars, subjects with deep recursion may
|
|
|
|
|
also need larger limits.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {max} (`integer`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.type({value}) *vim.lpeg.type()*
|
|
|
|
|
Returns the string `"pattern"` if the given value is a pattern, otherwise
|
|
|
|
|
`nil`.
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Parameters: ~
|
|
|
|
|
• {value} (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
|
|
|
|
|
|
2024-01-11 10:24:44 -07:00
|
|
|
|
Return: ~
|
|
|
|
|
(`"pattern"?`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.V({v}) *vim.lpeg.V()*
|
|
|
|
|
Creates a non-terminal (a variable) for a grammar. This operation creates
|
|
|
|
|
a non-terminal (a variable) for a grammar. The created non-terminal refers
|
|
|
|
|
to the rule indexed by `v` in the enclosing grammar.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
2024-02-09 11:35:13 -07:00
|
|
|
|
local b = lpeg.P({'(' * ((1 - lpeg.S '()') + lpeg.V(1)) ^ 0 * ')'})
|
2024-01-11 10:24:44 -07:00
|
|
|
|
assert(b:match('((string))') == 11)
|
|
|
|
|
assert(b:match('(') == nil)
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-07-15 12:55:57 -07:00
|
|
|
|
• {v} (`boolean|string|number|function|table|thread|userdata|lightuserdata`)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Pattern`)
|
|
|
|
|
|
|
|
|
|
vim.lpeg.version() *vim.lpeg.version()*
|
|
|
|
|
Returns a string with the running version of LPeg.
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`string`)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
|
VIM.RE *vim.re*
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
The `vim.re` module provides a conventional regex-like syntax for pattern
|
2024-05-02 06:57:21 -07:00
|
|
|
|
usage within LPeg |vim.lpeg|. (Unrelated to |vim.regex| which provides Vim
|
|
|
|
|
|regexp| from Lua.)
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
See https://www.inf.puc-rio.br/~roberto/lpeg/re.html for the original
|
2024-05-02 06:57:21 -07:00
|
|
|
|
documentation including regex syntax and examples.
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|
2024-01-11 10:24:44 -07:00
|
|
|
|
vim.re.compile({string}, {defs}) *vim.re.compile()*
|
|
|
|
|
Compiles the given {string} and returns an equivalent LPeg pattern. The
|
|
|
|
|
given string may define either an expression or a grammar. The optional
|
|
|
|
|
{defs} table provides extra Lua values to be used by the pattern.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {string} (`string`)
|
|
|
|
|
• {defs} (`table?`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.lpeg.Pattern`)
|
|
|
|
|
|
|
|
|
|
vim.re.find({subject}, {pattern}, {init}) *vim.re.find()*
|
|
|
|
|
Searches the given {pattern} in the given {subject}. If it finds a match,
|
|
|
|
|
returns the index where this occurrence starts and the index where it
|
|
|
|
|
ends. Otherwise, returns nil.
|
|
|
|
|
|
|
|
|
|
An optional numeric argument {init} makes the search starts at that
|
|
|
|
|
position in the subject string. As usual in Lua libraries, a negative
|
|
|
|
|
value counts from the end.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {subject} (`string`)
|
|
|
|
|
• {pattern} (`vim.lpeg.Pattern|string`)
|
|
|
|
|
• {init} (`integer?`)
|
|
|
|
|
|
|
|
|
|
Return (multiple): ~
|
|
|
|
|
(`integer?`) the index where the occurrence starts, nil if no match
|
|
|
|
|
(`integer?`) the index where the occurrence ends, nil if no match
|
|
|
|
|
|
|
|
|
|
vim.re.gsub({subject}, {pattern}, {replacement}) *vim.re.gsub()*
|
|
|
|
|
Does a global substitution, replacing all occurrences of {pattern} in the
|
|
|
|
|
given {subject} by {replacement}.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {subject} (`string`)
|
|
|
|
|
• {pattern} (`vim.lpeg.Pattern|string`)
|
|
|
|
|
• {replacement} (`string`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`string`)
|
|
|
|
|
|
|
|
|
|
vim.re.match({subject}, {pattern}, {init}) *vim.re.match()*
|
|
|
|
|
Matches the given {pattern} against the given {subject}, returning all
|
|
|
|
|
captures.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {subject} (`string`)
|
|
|
|
|
• {pattern} (`vim.lpeg.Pattern|string`)
|
|
|
|
|
• {init} (`integer?`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`integer|vim.lpeg.Capture?`)
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
• vim.lpeg.match()
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
vim.re.updatelocale() *vim.re.updatelocale()*
|
|
|
|
|
Updates the pre-defined character classes to the current locale.
|
|
|
|
|
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
|
VIM.REGEX *vim.regex*
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Vim regexes can be used directly from Lua. Currently they only allow matching
|
|
|
|
|
within a single line.
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*regex:match_line()*
|
2024-02-15 10:16:04 -07:00
|
|
|
|
regex:match_line({bufnr}, {line_idx}, {start}, {end_})
|
2024-01-11 10:24:44 -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}.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {bufnr} (`integer`)
|
|
|
|
|
• {line_idx} (`integer`)
|
|
|
|
|
• {start} (`integer?`)
|
|
|
|
|
• {end_} (`integer?`)
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
regex:match_str({str}) *regex:match_str()*
|
2024-01-11 10:24:44 -07:00
|
|
|
|
Match the string against the regex. If the string should match the regex
|
2024-02-15 10:16:04 -07:00
|
|
|
|
precisely, surround the regex with `^` and `$`. If there was a match, the
|
2024-01-11 10:24:44 -07:00
|
|
|
|
byte indices for the beginning and end of the match are returned. When
|
2024-02-15 10:16:04 -07:00
|
|
|
|
there is no match, `nil` is returned. Because any integer is "truthy",
|
|
|
|
|
`regex:match_str()` can be directly used as a condition in an
|
|
|
|
|
if-statement.
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {str} (`string`)
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
vim.regex({re}) *vim.regex()*
|
|
|
|
|
Parse the Vim regex {re} and return a regex object. Regexes are "magic"
|
|
|
|
|
and case-sensitive by default, regardless of 'magic' and 'ignorecase'.
|
|
|
|
|
They can be controlled with flags, see |/magic| and |/ignorecase|.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {re} (`string`)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`vim.regex`)
|
|
|
|
|
|
2024-01-11 10:24:44 -07:00
|
|
|
|
|
2022-11-05 12:37:05 -07:00
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
Lua module: vim.secure *vim.secure*
|
2022-11-05 12:37:05 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.secure.read({path}) *vim.secure.read()*
|
2022-11-05 12:37:05 -07:00
|
|
|
|
Attempt to read the file at {path} prompting the user if the file should
|
|
|
|
|
be trusted. The user's choice is persisted in a trust database at
|
|
|
|
|
$XDG_STATE_HOME/nvim/trust.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {path} (`string`) Path to a file to read.
|
2022-11-05 12:37:05 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string?`) The contents of the given file if it exists and is
|
2022-11-05 12:37:05 -07:00
|
|
|
|
trusted, or nil otherwise.
|
|
|
|
|
|
2022-11-28 12:23:04 -07:00
|
|
|
|
See also: ~
|
2023-03-15 04:51:44 -07:00
|
|
|
|
• |:trust|
|
2022-11-28 12:23:04 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.secure.trust({opts}) *vim.secure.trust()*
|
2022-11-28 12:23:04 -07:00
|
|
|
|
Manage the trust database.
|
|
|
|
|
|
|
|
|
|
The trust database is located at |$XDG_STATE_HOME|/nvim/trust.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-03-16 10:11:42 -07:00
|
|
|
|
• {opts} (`table`) A table with the following fields:
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {action} (`'allow'|'deny'|'remove'`) - `'allow'` to add a
|
|
|
|
|
file to the trust database and trust it,
|
|
|
|
|
• `'deny'` to add a file to the trust database and deny it,
|
|
|
|
|
• `'remove'` to remove file from the trust database
|
|
|
|
|
• {path}? (`string`) Path to a file to update. Mutually
|
2022-11-28 12:23:04 -07:00
|
|
|
|
exclusive with {bufnr}. Cannot be used when {action} is
|
|
|
|
|
"allow".
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {bufnr}? (`integer`) Buffer number to update. Mutually
|
2022-11-28 12:23:04 -07:00
|
|
|
|
exclusive with {path}.
|
|
|
|
|
|
2023-08-09 02:06:13 -07:00
|
|
|
|
Return (multiple): ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`) success true if operation was successful
|
|
|
|
|
(`string`) msg full path if operation was successful, else error
|
|
|
|
|
message
|
2022-11-28 12:23:04 -07:00
|
|
|
|
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
Lua module: vim.version *vim.version*
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
The `vim.version` module provides functions for comparing versions and ranges
|
|
|
|
|
conforming to the https://semver.org spec. Plugins, and plugin managers, can
|
|
|
|
|
use this to check available tools and dependencies on the current system.
|
2023-03-16 17:12:33 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
local v = vim.version.parse(vim.fn.system({'tmux', '-V'}), {strict=false})
|
|
|
|
|
if vim.version.gt(v, {3, 2, 0}) then
|
|
|
|
|
-- ...
|
|
|
|
|
end
|
2023-03-16 17:12:33 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
*vim.version()* returns the version of the current Nvim process.
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
VERSION RANGE SPEC *version-range*
|
2023-03-16 17:12:33 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
A version "range spec" defines a semantic version range which can be tested
|
|
|
|
|
against a version, using |vim.version.range()|.
|
2023-03-16 17:12:33 -07:00
|
|
|
|
|
|
|
|
|
Supported range specs are shown in the following table. Note: suffixed
|
|
|
|
|
versions (1.2.3-rc1) are not matched. >
|
2023-09-14 06:23:01 -07:00
|
|
|
|
1.2.3 is 1.2.3
|
|
|
|
|
=1.2.3 is 1.2.3
|
|
|
|
|
>1.2.3 greater than 1.2.3
|
|
|
|
|
<1.2.3 before 1.2.3
|
|
|
|
|
>=1.2.3 at least 1.2.3
|
|
|
|
|
~1.2.3 is >=1.2.3 <1.3.0 "reasonably close to 1.2.3"
|
|
|
|
|
^1.2.3 is >=1.2.3 <2.0.0 "compatible with 1.2.3"
|
|
|
|
|
^0.2.3 is >=0.2.3 <0.3.0 (0.x.x is special)
|
|
|
|
|
^0.0.1 is =0.0.1 (0.0.x is special)
|
|
|
|
|
^1.2 is >=1.2.0 <2.0.0 (like ^1.2.0)
|
|
|
|
|
~1.2 is >=1.2.0 <1.3.0 (like ~1.2.0)
|
|
|
|
|
^1 is >=1.0.0 <2.0.0 "compatible with 1"
|
|
|
|
|
~1 same "reasonably close to 1"
|
|
|
|
|
1.x same
|
|
|
|
|
1.* same
|
|
|
|
|
1 same
|
|
|
|
|
* any version
|
|
|
|
|
x same
|
|
|
|
|
|
|
|
|
|
1.2.3 - 2.3.4 is >=1.2.3 <=2.3.4
|
|
|
|
|
|
|
|
|
|
Partial right: missing pieces treated as x (2.3 => 2.3.x).
|
|
|
|
|
1.2.3 - 2.3 is >=1.2.3 <2.4.0
|
|
|
|
|
1.2.3 - 2 is >=1.2.3 <3.0.0
|
|
|
|
|
|
|
|
|
|
Partial left: missing pieces treated as 0 (1.2 => 1.2.0).
|
|
|
|
|
1.2 - 2.3.0 is 1.2.0 - 2.3.0
|
2023-03-16 17:12:33 -07:00
|
|
|
|
<
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.version.cmp({v1}, {v2}) *vim.version.cmp()*
|
2023-05-13 12:33:22 -07:00
|
|
|
|
Parses and compares two version objects (the result of
|
2023-03-16 17:12:33 -07:00
|
|
|
|
|vim.version.parse()|, or specified literally as a `{major, minor, patch}`
|
|
|
|
|
tuple, e.g. `{1, 0, 3}`).
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
if vim.version.cmp({1,0,3}, {0,2,1}) == 0 then
|
|
|
|
|
-- ...
|
|
|
|
|
end
|
|
|
|
|
local v1 = vim.version.parse('1.0.3-pre')
|
|
|
|
|
local v2 = vim.version.parse('0.2.1')
|
|
|
|
|
if vim.version.cmp(v1, v2) == 0 then
|
|
|
|
|
-- ...
|
|
|
|
|
end
|
2023-03-16 17:12:33 -07:00
|
|
|
|
<
|
|
|
|
|
|
2023-07-06 13:47:27 -07:00
|
|
|
|
Note: ~
|
|
|
|
|
• Per semver, build metadata is ignored when comparing two
|
2023-03-16 17:12:33 -07:00
|
|
|
|
otherwise-equivalent versions.
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {v1} (`vim.Version|number[]|string`) Version object.
|
|
|
|
|
• {v2} (`vim.Version|number[]|string`) Version to compare with `v1`.
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`integer`) -1 if `v1 < v2`, 0 if `v1 == v2`, 1 if `v1 > v2`.
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.version.eq({v1}, {v2}) *vim.version.eq()*
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Returns `true` if the given versions are equal. See |vim.version.cmp()|
|
|
|
|
|
for usage.
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {v1} (`vim.Version|number[]|string`)
|
|
|
|
|
• {v2} (`vim.Version|number[]|string`)
|
2024-01-15 10:13:09 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`boolean`)
|
|
|
|
|
|
|
|
|
|
vim.version.ge({v1}, {v2}) *vim.version.ge()*
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Returns `true` if `v1 >= v2`. See |vim.version.cmp()| for usage.
|
2024-01-15 10:13:09 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {v1} (`vim.Version|number[]|string`)
|
|
|
|
|
• {v2} (`vim.Version|number[]|string`)
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`)
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.version.gt({v1}, {v2}) *vim.version.gt()*
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Returns `true` if `v1 > v2`. See |vim.version.cmp()| for usage.
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {v1} (`vim.Version|number[]|string`)
|
|
|
|
|
• {v2} (`vim.Version|number[]|string`)
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`)
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.version.last({versions}) *vim.version.last()*
|
2023-03-16 17:12:33 -07:00
|
|
|
|
TODO: generalize this, move to func.lua
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {versions} (`vim.Version[]`)
|
2023-03-16 17:12:33 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
(`vim.Version?`)
|
2023-03-16 17:12:33 -07:00
|
|
|
|
|
2024-01-15 10:13:09 -07:00
|
|
|
|
vim.version.le({v1}, {v2}) *vim.version.le()*
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Returns `true` if `v1 <= v2`. See |vim.version.cmp()| for usage.
|
2024-01-15 10:13:09 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {v1} (`vim.Version|number[]|string`)
|
|
|
|
|
• {v2} (`vim.Version|number[]|string`)
|
2024-01-15 10:13:09 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`boolean`)
|
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.version.lt({v1}, {v2}) *vim.version.lt()*
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Returns `true` if `v1 < v2`. See |vim.version.cmp()| for usage.
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {v1} (`vim.Version|number[]|string`)
|
|
|
|
|
• {v2} (`vim.Version|number[]|string`)
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`)
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.version.parse({version}, {opts}) *vim.version.parse()*
|
2023-03-16 17:12:33 -07:00
|
|
|
|
Parses a semantic version string and returns a version object which can be
|
2023-09-14 06:23:01 -07:00
|
|
|
|
used with other `vim.version` functions. For example "1.0.1-rc1+build.2"
|
|
|
|
|
returns: >
|
|
|
|
|
{ major = 1, minor = 0, patch = 1, prerelease = "rc1", build = "build.2" }
|
2023-03-06 05:23:03 -07:00
|
|
|
|
<
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {version} (`string`) Version string to parse.
|
|
|
|
|
• {opts} (`table?`) Optional keyword arguments:
|
2023-03-16 17:12:33 -07:00
|
|
|
|
• strict (boolean): Default false. If `true`, no coercion
|
|
|
|
|
is attempted on input not conforming to semver v2.0.0. If
|
|
|
|
|
`false`, `parse()` attempts to coerce input such as
|
|
|
|
|
"1.0", "0-x", "tmux 3.2a" into valid versions.
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-02-27 08:20:32 -07:00
|
|
|
|
(`vim.Version?`) parsed_version Version object or `nil` if input is
|
2024-01-15 10:13:09 -07:00
|
|
|
|
invalid.
|
2023-03-16 17:12:33 -07:00
|
|
|
|
|
|
|
|
|
See also: ~
|
2024-02-15 10:16:04 -07:00
|
|
|
|
• https://semver.org/spec/v2.0.0.html
|
2023-03-16 17:12:33 -07:00
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
|
vim.version.range({spec}) *vim.version.range()*
|
2023-03-16 17:12:33 -07:00
|
|
|
|
Parses a semver |version-range| "spec" and returns a range object: >
|
2023-09-14 06:23:01 -07:00
|
|
|
|
{
|
|
|
|
|
from: Version
|
|
|
|
|
to: Version
|
|
|
|
|
has(v: string|Version)
|
|
|
|
|
}
|
2023-03-16 17:12:33 -07:00
|
|
|
|
<
|
|
|
|
|
|
2023-06-06 06:38:45 -07:00
|
|
|
|
`:has()` checks if a version is in the range (inclusive `from`, exclusive
|
|
|
|
|
`to`).
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
2023-09-14 06:23:01 -07:00
|
|
|
|
local r = vim.version.range('1.0.0 - 2.0.0')
|
|
|
|
|
print(r:has('1.9.9')) -- true
|
|
|
|
|
print(r:has('2.0.0')) -- false
|
|
|
|
|
print(r:has(vim.version())) -- check against current Nvim version
|
2023-03-16 17:12:33 -07:00
|
|
|
|
<
|
|
|
|
|
|
2024-01-15 10:13:09 -07:00
|
|
|
|
Or use cmp(), le(), lt(), ge(), gt(), and/or eq() to compare a version
|
|
|
|
|
against `.to` and `.from` directly: >lua
|
|
|
|
|
local r = vim.version.range('1.0.0 - 2.0.0') -- >=1.0, <2.0
|
|
|
|
|
print(vim.version.ge({1,0,3}, r.from) and vim.version.lt({1,0,3}, r.to))
|
2023-03-16 17:12:33 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {spec} (`string`) Version range "spec"
|
2023-03-16 17:12:33 -07:00
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
|
Return: ~
|
2024-03-06 05:15:25 -07:00
|
|
|
|
(`table?`) A table with the following fields:
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {from} (`vim.Version`)
|
|
|
|
|
• {to}? (`vim.Version`)
|
|
|
|
|
|
2023-03-16 17:12:33 -07:00
|
|
|
|
See also: ~
|
2024-02-15 10:16:04 -07:00
|
|
|
|
• https://github.com/npm/node-semver#ranges
|
2023-02-19 04:33:57 -07:00
|
|
|
|
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
2023-07-17 07:13:54 -07:00
|
|
|
|
Lua module: vim.iter *vim.iter*
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-07-12 10:27:14 -07:00
|
|
|
|
*vim.iter()* is an interface for |iterable|s: it wraps a table or function
|
|
|
|
|
argument into an *Iter* object with methods (such as |Iter:filter()| and
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|Iter:map()|) that transform the underlying source data. These methods can be
|
|
|
|
|
chained to create iterator "pipelines": the output of each pipeline stage is
|
|
|
|
|
input to the next stage. The first stage depends on the type passed to
|
|
|
|
|
`vim.iter()`:
|
2023-11-25 07:35:31 -07:00
|
|
|
|
• List tables (arrays, |lua-list|) yield only the value of each element.
|
2024-05-17 12:17:25 -07:00
|
|
|
|
• Holes (nil values) are allowed.
|
2023-11-25 07:35:31 -07:00
|
|
|
|
• Use |Iter:enumerate()| to also pass the index to the next stage.
|
|
|
|
|
• Or initialize with ipairs(): `vim.iter(ipairs(…))`.
|
2024-02-15 10:16:04 -07:00
|
|
|
|
• Non-list tables (|lua-dict|) yield both the key and value of each element.
|
|
|
|
|
• Function |iterator|s yield all values returned by the underlying function.
|
2023-11-25 07:35:31 -07:00
|
|
|
|
• Tables with a |__call()| metamethod are treated as function iterators.
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
The iterator pipeline terminates when the underlying |iterable| is exhausted
|
|
|
|
|
(for function iterators this means it returned nil).
|
2023-11-25 07:35:31 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Note: `vim.iter()` scans table input to decide if it is a list or a dict; to
|
|
|
|
|
avoid this cost you can wrap the table with an iterator e.g.
|
2023-11-25 07:35:31 -07:00
|
|
|
|
`vim.iter(ipairs({…}))`, but that precludes the use of |list-iterator|
|
|
|
|
|
operations such as |Iter:rev()|).
|
2023-08-09 13:41:45 -07:00
|
|
|
|
|
2023-04-24 18:57:40 -07:00
|
|
|
|
Examples: >lua
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter({ 1, 2, 3, 4, 5 })
|
|
|
|
|
it:map(function(v)
|
|
|
|
|
return v * 3
|
|
|
|
|
end)
|
|
|
|
|
it:rev()
|
|
|
|
|
it:skip(2)
|
|
|
|
|
it:totable()
|
|
|
|
|
-- { 9, 6, 3 }
|
|
|
|
|
|
|
|
|
|
-- ipairs() is a function iterator which returns both the index (i) and the value (v)
|
|
|
|
|
vim.iter(ipairs({ 1, 2, 3, 4, 5 })):map(function(i, v)
|
|
|
|
|
if i > 2 then return v end
|
|
|
|
|
end):totable()
|
|
|
|
|
-- { 3, 4, 5 }
|
|
|
|
|
|
|
|
|
|
local it = vim.iter(vim.gsplit('1,2,3,4,5', ','))
|
|
|
|
|
it:map(function(s) return tonumber(s) end)
|
|
|
|
|
for i, d in it:enumerate() do
|
|
|
|
|
print(string.format("Column %d is %d", i, d))
|
|
|
|
|
end
|
|
|
|
|
-- Column 1 is 1
|
|
|
|
|
-- Column 2 is 2
|
|
|
|
|
-- Column 3 is 3
|
|
|
|
|
-- Column 4 is 4
|
|
|
|
|
-- Column 5 is 5
|
|
|
|
|
|
|
|
|
|
vim.iter({ a = 1, b = 2, c = 3, z = 26 }):any(function(k, v)
|
|
|
|
|
return k == 'z'
|
|
|
|
|
end)
|
|
|
|
|
-- true
|
2023-04-24 18:57:40 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local rb = vim.ringbuf(3)
|
|
|
|
|
rb:push("a")
|
|
|
|
|
rb:push("b")
|
|
|
|
|
vim.iter(rb):totable()
|
|
|
|
|
-- { "a", "b" }
|
2023-04-24 18:57:40 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:all({pred}) *Iter:all()*
|
2023-11-25 07:35:31 -07:00
|
|
|
|
Returns true if all items in the iterator match the given predicate.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-18 06:01:57 -07:00
|
|
|
|
• {pred} (`fun(...):boolean`) Predicate function. Takes all values
|
2023-04-17 11:54:19 -07:00
|
|
|
|
returned from the previous stage in the pipeline as arguments
|
|
|
|
|
and returns true if the predicate matches.
|
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:any({pred}) *Iter:any()*
|
2023-11-25 07:35:31 -07:00
|
|
|
|
Returns true if any of the items in the iterator match the given
|
|
|
|
|
predicate.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-18 06:01:57 -07:00
|
|
|
|
• {pred} (`fun(...):boolean`) Predicate function. Takes all values
|
2023-04-17 11:54:19 -07:00
|
|
|
|
returned from the previous stage in the pipeline as arguments
|
|
|
|
|
and returns true if the predicate matches.
|
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:each({f}) *Iter:each()*
|
2023-11-25 07:35:31 -07:00
|
|
|
|
Calls a function once for each item in the pipeline, draining the
|
|
|
|
|
iterator.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
|
For functions with side effects. To modify the values in the iterator, use
|
|
|
|
|
|Iter:map()|.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {f} (`fun(...)`) Function to execute for each item in the pipeline.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
Takes all of the values returned by the previous stage in the
|
|
|
|
|
pipeline as arguments.
|
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:enumerate() *Iter:enumerate()*
|
2023-11-25 07:35:31 -07:00
|
|
|
|
Yields the item index (count) and value for each item of an iterator
|
|
|
|
|
pipeline.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
|
For list tables, this is more efficient: >lua
|
2023-09-13 06:38:28 -07:00
|
|
|
|
vim.iter(ipairs(t))
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
|
instead of: >lua
|
2023-09-13 06:38:28 -07:00
|
|
|
|
vim.iter(t):enumerate()
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
2024-03-09 05:21:01 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter(vim.gsplit('abc', '')):enumerate()
|
|
|
|
|
it:next()
|
2024-02-15 10:16:04 -07:00
|
|
|
|
-- 1 'a'
|
2023-09-13 06:38:28 -07:00
|
|
|
|
it:next()
|
2024-02-15 10:16:04 -07:00
|
|
|
|
-- 2 'b'
|
2023-09-13 06:38:28 -07:00
|
|
|
|
it:next()
|
2024-02-15 10:16:04 -07:00
|
|
|
|
-- 3 'c'
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`Iter`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:filter({f}) *Iter:filter()*
|
2023-11-25 07:35:31 -07:00
|
|
|
|
Filters an iterator pipeline.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local bufs = vim.iter(vim.api.nvim_list_bufs()):filter(vim.api.nvim_buf_is_loaded)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-18 06:01:57 -07:00
|
|
|
|
• {f} (`fun(...):boolean`) Takes all values returned from the previous
|
2023-04-17 11:54:19 -07:00
|
|
|
|
stage in the pipeline and returns false or nil if the current
|
|
|
|
|
iterator element should be removed.
|
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`Iter`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:find({f}) *Iter:find()*
|
2023-04-17 11:54:19 -07:00
|
|
|
|
Find the first value in the iterator that satisfies the given predicate.
|
|
|
|
|
|
|
|
|
|
Advances the iterator. Returns nil and drains the iterator if no value is
|
|
|
|
|
found.
|
|
|
|
|
|
|
|
|
|
Examples: >lua
|
2024-03-09 05:21:01 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter({ 3, 6, 9, 12 })
|
|
|
|
|
it:find(12)
|
|
|
|
|
-- 12
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter({ 3, 6, 9, 12 })
|
|
|
|
|
it:find(20)
|
|
|
|
|
-- nil
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter({ 3, 6, 9, 12 })
|
|
|
|
|
it:find(function(v) return v % 4 == 0 end)
|
|
|
|
|
-- 12
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Parameters: ~
|
|
|
|
|
• {f} (`any`)
|
|
|
|
|
|
2023-04-17 11:54:19 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`any`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2024-01-10 19:57:51 -07:00
|
|
|
|
Iter:flatten({depth}) *Iter:flatten()*
|
|
|
|
|
Flattens a |list-iterator|, un-nesting nested values up to the given
|
|
|
|
|
{depth}. Errors if it attempts to flatten a dict-like value.
|
|
|
|
|
|
|
|
|
|
Examples: >lua
|
|
|
|
|
vim.iter({ 1, { 2 }, { { 3 } } }):flatten():totable()
|
|
|
|
|
-- { 1, 2, { 3 } }
|
|
|
|
|
|
|
|
|
|
vim.iter({1, { { a = 2 } }, { 3 } }):flatten():totable()
|
|
|
|
|
-- { 1, { a = 2 }, 3 }
|
|
|
|
|
|
|
|
|
|
vim.iter({ 1, { { a = 2 } }, { 3 } }):flatten(math.huge):totable()
|
|
|
|
|
-- error: attempt to flatten a dict-like table
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {depth} (`number?`) Depth to which |list-iterator| should be
|
2024-01-10 19:57:51 -07:00
|
|
|
|
flattened (defaults to 1)
|
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`Iter`)
|
2024-01-10 19:57:51 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:fold({init}, {f}) *Iter:fold()*
|
2024-04-30 04:30:21 -07:00
|
|
|
|
Folds ("reduces") an iterator into a single value. *Iter:reduce()*
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-06-02 07:59:58 -07:00
|
|
|
|
Examples: >lua
|
2023-09-13 06:38:28 -07:00
|
|
|
|
-- Create a new table with only even values
|
2024-04-30 04:30:21 -07:00
|
|
|
|
vim.iter({ a = 1, b = 2, c = 3, d = 4 })
|
|
|
|
|
:filter(function(k, v) return v % 2 == 0 end)
|
|
|
|
|
:fold({}, function(acc, k, v)
|
|
|
|
|
acc[k] = v
|
|
|
|
|
return acc
|
|
|
|
|
end) --> { b = 2, d = 4 }
|
|
|
|
|
|
|
|
|
|
-- Get the "maximum" item of an iterable.
|
|
|
|
|
vim.iter({ -99, -4, 3, 42, 0, 0, 7 })
|
|
|
|
|
:fold({}, function(acc, v)
|
2024-05-14 16:18:33 -07:00
|
|
|
|
acc.max = math.max(v, acc.max or v)
|
|
|
|
|
return acc
|
2024-04-30 04:30:21 -07:00
|
|
|
|
end) --> { max = 42 }
|
fix(iter): remove special case totable for map-like tables
This was originally meant as a convenience but prevents possible
functionality. For example:
-- Get the keys of the table with even values
local t = { a = 1, b = 2, c = 3, d = 4 }
vim.iter(t):map(function(k, v)
if v % 2 == 0 then return k end
end):totable()
The example above would not work, because the map() function returns
only a single value, and cannot be converted back into a table (there
are many such examples like this).
Instead, to convert an iterator into a map-like table, users can use
fold():
vim.iter(t):fold({}, function(t, k, v)
t[k] = v
return t
end)
2023-04-19 06:05:04 -07:00
|
|
|
|
<
|
|
|
|
|
|
2023-04-17 11:54:19 -07:00
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {init} (`any`) Initial value of the accumulator.
|
2024-02-15 10:16:04 -07:00
|
|
|
|
• {f} (`fun(acc:A, ...):A`) Accumulation function.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`any`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-12-05 19:35:22 -07:00
|
|
|
|
Iter:join({delim}) *Iter:join()*
|
|
|
|
|
Collect the iterator into a delimited string.
|
|
|
|
|
|
|
|
|
|
Each element in the iterator is joined into a string separated by {delim}.
|
|
|
|
|
|
|
|
|
|
Consumes the iterator.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {delim} (`string`) Delimiter
|
2023-12-05 19:35:22 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`)
|
2023-12-05 19:35:22 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:last() *Iter:last()*
|
2023-11-25 07:35:31 -07:00
|
|
|
|
Drains the iterator and returns the last item.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2024-03-09 05:21:01 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter(vim.gsplit('abcdefg', ''))
|
|
|
|
|
it:last()
|
|
|
|
|
-- 'g'
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter({ 3, 6, 9, 12, 15 })
|
|
|
|
|
it:last()
|
|
|
|
|
-- 15
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`any`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2024-04-26 08:43:29 -07:00
|
|
|
|
See also: ~
|
|
|
|
|
• Iter.rpeek
|
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:map({f}) *Iter:map()*
|
2023-11-25 07:35:31 -07:00
|
|
|
|
Maps the items of an iterator pipeline to the values returned by `f`.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
If the map function returns nil, the value is filtered from the iterator.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter({ 1, 2, 3, 4 }):map(function(v)
|
|
|
|
|
if v % 2 == 0 then
|
|
|
|
|
return v * 3
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
it:totable()
|
|
|
|
|
-- { 6, 12 }
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-06-01 00:18:59 -07:00
|
|
|
|
• {f} (`fun(...):...:any`) Mapping function. Takes all values returned
|
|
|
|
|
from the previous stage in the pipeline as arguments and returns
|
|
|
|
|
one or more new values, which are used in the next pipeline
|
|
|
|
|
stage. Nil return values are filtered from the output.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`Iter`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:next() *Iter:next()*
|
2023-11-25 07:35:31 -07:00
|
|
|
|
Gets the next value from the iterator.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2024-03-09 05:21:01 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter(string.gmatch('1 2 3', '%d+')):map(tonumber)
|
|
|
|
|
it:next()
|
|
|
|
|
-- 1
|
|
|
|
|
it:next()
|
|
|
|
|
-- 2
|
|
|
|
|
it:next()
|
|
|
|
|
-- 3
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`any`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:nth({n}) *Iter:nth()*
|
2023-11-25 07:35:31 -07:00
|
|
|
|
Gets the nth value of an iterator (and advances to it).
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2024-04-26 08:43:29 -07:00
|
|
|
|
If `n` is negative, offsets from the end of a |list-iterator|.
|
2024-03-09 05:21:01 -07:00
|
|
|
|
|
2024-04-26 08:43:29 -07:00
|
|
|
|
Example: >lua
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter({ 3, 6, 9, 12 })
|
|
|
|
|
it:nth(2)
|
|
|
|
|
-- 6
|
|
|
|
|
it:nth(2)
|
|
|
|
|
-- 12
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2024-04-26 08:43:29 -07:00
|
|
|
|
local it2 = vim.iter({ 3, 6, 9, 12 })
|
|
|
|
|
it2:nth(-2)
|
2023-09-13 06:38:28 -07:00
|
|
|
|
-- 9
|
2024-04-26 08:43:29 -07:00
|
|
|
|
it2:nth(-2)
|
2023-09-13 06:38:28 -07:00
|
|
|
|
-- 3
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-04-26 08:43:29 -07:00
|
|
|
|
• {n} (`number`) Index of the value to return. May be negative if the
|
|
|
|
|
source is a |list-iterator|.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`any`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:peek() *Iter:peek()*
|
2023-11-25 07:35:31 -07:00
|
|
|
|
Gets the next value in a |list-iterator| without consuming it.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2024-03-09 05:21:01 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter({ 3, 6, 9, 12 })
|
|
|
|
|
it:peek()
|
|
|
|
|
-- 3
|
|
|
|
|
it:peek()
|
|
|
|
|
-- 3
|
|
|
|
|
it:next()
|
|
|
|
|
-- 3
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`any`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2024-04-26 08:43:29 -07:00
|
|
|
|
Iter:pop() *Iter:pop()*
|
|
|
|
|
"Pops" a value from a |list-iterator| (gets the last value and decrements
|
|
|
|
|
the tail).
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter({1, 2, 3, 4})
|
2024-04-26 08:43:29 -07:00
|
|
|
|
it:pop()
|
2023-09-13 06:38:28 -07:00
|
|
|
|
-- 4
|
2024-04-26 08:43:29 -07:00
|
|
|
|
it:pop()
|
|
|
|
|
-- 3
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`any`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:rev() *Iter:rev()*
|
2023-11-25 07:35:31 -07:00
|
|
|
|
Reverses a |list-iterator| pipeline.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2024-03-09 05:21:01 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter({ 3, 6, 9, 12 }):rev()
|
|
|
|
|
it:totable()
|
|
|
|
|
-- { 12, 9, 6, 3 }
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`Iter`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:rfind({f}) *Iter:rfind()*
|
2024-04-26 08:43:29 -07:00
|
|
|
|
Gets the first value satisfying a predicate, from the end of a
|
|
|
|
|
|list-iterator|.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Advances the iterator. Returns nil and drains the iterator if no value is
|
|
|
|
|
found.
|
|
|
|
|
|
|
|
|
|
Examples: >lua
|
2024-03-09 05:21:01 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
local it = vim.iter({ 1, 2, 3, 2, 1 }):enumerate()
|
|
|
|
|
it:rfind(1)
|
2024-02-15 10:16:04 -07:00
|
|
|
|
-- 5 1
|
2023-09-13 06:38:28 -07:00
|
|
|
|
it:rfind(1)
|
2024-02-15 10:16:04 -07:00
|
|
|
|
-- 1 1
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Parameters: ~
|
|
|
|
|
• {f} (`any`)
|
|
|
|
|
|
2023-04-17 11:54:19 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`any`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
• Iter.find
|
|
|
|
|
|
2024-04-26 08:43:29 -07:00
|
|
|
|
Iter:rpeek() *Iter:rpeek()*
|
|
|
|
|
Gets the last value of a |list-iterator| without consuming it.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2024-04-26 08:43:29 -07:00
|
|
|
|
local it = vim.iter({1, 2, 3, 4})
|
|
|
|
|
it:rpeek()
|
|
|
|
|
-- 4
|
|
|
|
|
it:rpeek()
|
|
|
|
|
-- 4
|
|
|
|
|
it:pop()
|
|
|
|
|
-- 4
|
|
|
|
|
<
|
2024-03-09 05:21:01 -07:00
|
|
|
|
|
2024-04-26 08:43:29 -07:00
|
|
|
|
Return: ~
|
|
|
|
|
(`any`)
|
|
|
|
|
|
|
|
|
|
See also: ~
|
|
|
|
|
• Iter.last
|
|
|
|
|
|
|
|
|
|
Iter:rskip({n}) *Iter:rskip()*
|
|
|
|
|
Discards `n` values from the end of a |list-iterator| pipeline.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
local it = vim.iter({ 1, 2, 3, 4, 5 }):rskip(2)
|
2023-09-13 06:38:28 -07:00
|
|
|
|
it:next()
|
2024-04-26 08:43:29 -07:00
|
|
|
|
-- 1
|
|
|
|
|
it:pop()
|
|
|
|
|
-- 3
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {n} (`number`) Number of values to skip.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`Iter`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2024-04-26 08:43:29 -07:00
|
|
|
|
Iter:skip({n}) *Iter:skip()*
|
|
|
|
|
Skips `n` values of an iterator pipeline.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Example: >lua
|
2024-04-26 08:43:29 -07:00
|
|
|
|
|
|
|
|
|
local it = vim.iter({ 3, 6, 9, 12 }):skip(2)
|
2023-09-13 06:38:28 -07:00
|
|
|
|
it:next()
|
2024-04-26 08:43:29 -07:00
|
|
|
|
-- 9
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {n} (`number`) Number of values to skip.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`Iter`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:slice({first}, {last}) *Iter:slice()*
|
2023-11-25 07:35:31 -07:00
|
|
|
|
Sets the start and end of a |list-iterator| pipeline.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2024-04-26 08:43:29 -07:00
|
|
|
|
Equivalent to `:skip(first - 1):rskip(len - last + 1)`.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {first} (`number`)
|
|
|
|
|
• {last} (`number`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`Iter`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-12-12 13:27:24 -07:00
|
|
|
|
Iter:take({n}) *Iter:take()*
|
|
|
|
|
Transforms an iterator to yield only the first n values.
|
|
|
|
|
|
|
|
|
|
Example: >lua
|
|
|
|
|
local it = vim.iter({ 1, 2, 3, 4 }):take(2)
|
|
|
|
|
it:next()
|
|
|
|
|
-- 1
|
|
|
|
|
it:next()
|
|
|
|
|
-- 2
|
|
|
|
|
it:next()
|
|
|
|
|
-- nil
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {n} (`integer`)
|
2023-12-12 13:27:24 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`Iter`)
|
2023-12-12 13:27:24 -07:00
|
|
|
|
|
2023-07-17 02:39:52 -07:00
|
|
|
|
Iter:totable() *Iter:totable()*
|
2023-04-17 11:54:19 -07:00
|
|
|
|
Collect the iterator into a table.
|
|
|
|
|
|
|
|
|
|
The resulting table depends on the initial source in the iterator
|
2024-05-17 12:17:25 -07:00
|
|
|
|
pipeline. Array-like tables and function iterators will be collected into
|
|
|
|
|
an array-like table. If multiple values are returned from the final stage
|
|
|
|
|
in the iterator pipeline, each value will be included in a table.
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
|
|
|
|
Examples: >lua
|
2023-09-13 06:38:28 -07:00
|
|
|
|
vim.iter(string.gmatch('100 20 50', '%d+')):map(tonumber):totable()
|
|
|
|
|
-- { 100, 20, 50 }
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
vim.iter({ 1, 2, 3 }):map(function(v) return v, 2 * v end):totable()
|
|
|
|
|
-- { { 1, 2 }, { 2, 4 }, { 3, 6 } }
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-09-13 06:38:28 -07:00
|
|
|
|
vim.iter({ a = 1, b = 2, c = 3 }):filter(function(k, v) return v % 2 ~= 0 end):totable()
|
|
|
|
|
-- { { 'a', 1 }, { 'c', 3 } }
|
2023-04-17 11:54:19 -07:00
|
|
|
|
<
|
|
|
|
|
|
2024-05-17 12:17:25 -07:00
|
|
|
|
The generated table is an array-like table with consecutive, numeric
|
fix(iter): remove special case totable for map-like tables
This was originally meant as a convenience but prevents possible
functionality. For example:
-- Get the keys of the table with even values
local t = { a = 1, b = 2, c = 3, d = 4 }
vim.iter(t):map(function(k, v)
if v % 2 == 0 then return k end
end):totable()
The example above would not work, because the map() function returns
only a single value, and cannot be converted back into a table (there
are many such examples like this).
Instead, to convert an iterator into a map-like table, users can use
fold():
vim.iter(t):fold({}, function(t, k, v)
t[k] = v
return t
end)
2023-04-19 06:05:04 -07:00
|
|
|
|
indices. To create a map-like table with arbitrary keys, use
|
|
|
|
|
|Iter:fold()|.
|
|
|
|
|
|
2023-04-17 11:54:19 -07:00
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`table`)
|
2023-04-17 11:54:19 -07:00
|
|
|
|
|
2023-10-20 23:51:26 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
|
Lua module: vim.snippet *vim.snippet*
|
|
|
|
|
|
2024-04-29 13:45:53 -07:00
|
|
|
|
*vim.snippet.ActiveFilter*
|
|
|
|
|
|
|
|
|
|
Fields: ~
|
|
|
|
|
• {direction} (`vim.snippet.Direction`) Navigation direction. -1 for
|
|
|
|
|
previous, 1 for next.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vim.snippet.active({filter}) *vim.snippet.active()*
|
|
|
|
|
Returns `true` if there's an active snippet in the current buffer,
|
|
|
|
|
applying the given filter if provided.
|
|
|
|
|
|
|
|
|
|
You can use this function to navigate a snippet as follows: >lua
|
|
|
|
|
vim.keymap.set({ 'i', 's' }, '<Tab>', function()
|
|
|
|
|
if vim.snippet.active({ direction = 1 }) then
|
2024-06-17 16:00:32 -07:00
|
|
|
|
return '<Cmd>lua vim.snippet.jump(1)<CR>'
|
2024-04-29 13:45:53 -07:00
|
|
|
|
else
|
|
|
|
|
return '<Tab>'
|
|
|
|
|
end
|
|
|
|
|
end, { expr = true })
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {filter} (`vim.snippet.ActiveFilter?`) Filter to constrain the search
|
|
|
|
|
with:
|
|
|
|
|
• `direction` (vim.snippet.Direction): Navigation direction.
|
|
|
|
|
Will return `true` if the snippet can be jumped in the
|
|
|
|
|
given direction. See |vim.snippet.ActiveFilter|.
|
2023-10-20 23:51:26 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`boolean`)
|
2023-10-20 23:51:26 -07:00
|
|
|
|
|
2023-11-16 09:53:25 -07:00
|
|
|
|
vim.snippet.expand({input}) *vim.snippet.expand()*
|
2024-02-15 10:16:04 -07:00
|
|
|
|
Expands the given snippet text. Refer to
|
|
|
|
|
https://microsoft.github.io/language-server-protocol/specification/#snippet_syntax
|
|
|
|
|
for the specification of valid input.
|
2023-10-20 23:51:26 -07:00
|
|
|
|
|
2024-05-02 01:25:21 -07:00
|
|
|
|
Tabstops are highlighted with |hl-SnippetTabstop|.
|
2023-10-20 23:51:26 -07:00
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {input} (`string`)
|
2023-10-20 23:51:26 -07:00
|
|
|
|
|
2023-11-16 09:53:25 -07:00
|
|
|
|
vim.snippet.jump({direction}) *vim.snippet.jump()*
|
2024-05-02 06:57:21 -07:00
|
|
|
|
Jumps to the next (or previous) placeholder in the current snippet, if
|
|
|
|
|
possible.
|
2023-10-20 23:51:26 -07:00
|
|
|
|
|
2024-05-02 06:57:21 -07:00
|
|
|
|
For example, map `<Tab>` to jump while a snippet is active: >lua
|
2023-10-20 23:51:26 -07:00
|
|
|
|
vim.keymap.set({ 'i', 's' }, '<Tab>', function()
|
2024-05-14 16:18:33 -07:00
|
|
|
|
if vim.snippet.active({ direction = 1 }) then
|
2024-06-17 16:00:32 -07:00
|
|
|
|
return '<Cmd>lua vim.snippet.jump(1)<CR>'
|
2023-10-20 23:51:26 -07:00
|
|
|
|
else
|
|
|
|
|
return '<Tab>'
|
|
|
|
|
end
|
|
|
|
|
end, { expr = true })
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {direction} (`vim.snippet.Direction`) Navigation direction. -1 for
|
2023-10-20 23:51:26 -07:00
|
|
|
|
previous, 1 for next.
|
|
|
|
|
|
2024-05-06 04:42:30 -07:00
|
|
|
|
vim.snippet.stop() *vim.snippet.stop()*
|
|
|
|
|
Exits the current snippet.
|
|
|
|
|
|
2023-11-16 10:35:54 -07:00
|
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
|
Lua module: vim.text *vim.text*
|
|
|
|
|
|
|
|
|
|
vim.text.hexdecode({enc}) *vim.text.hexdecode()*
|
|
|
|
|
Hex decode a string.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {enc} (`string`) String to decode
|
2023-11-16 10:35:54 -07:00
|
|
|
|
|
2024-05-30 17:57:47 -07:00
|
|
|
|
Return (multiple): ~
|
|
|
|
|
(`string?`) Decoded string
|
|
|
|
|
(`string?`) Error message, if any
|
2023-11-16 10:35:54 -07:00
|
|
|
|
|
|
|
|
|
vim.text.hexencode({str}) *vim.text.hexencode()*
|
|
|
|
|
Hex encode a string.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
• {str} (`string`) String to encode
|
2023-11-16 10:35:54 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
2024-01-09 10:36:46 -07:00
|
|
|
|
(`string`) Hex encoded string
|
2023-11-16 10:35:54 -07:00
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
|
|
2024-02-26 12:42:51 -07:00
|
|
|
|
==============================================================================
|
|
|
|
|
Lua module: tohtml *vim.tohtml*
|
|
|
|
|
|
|
|
|
|
|
2024-07-11 09:16:51 -07:00
|
|
|
|
:[range]TOhtml {file} *:TOhtml*
|
2024-02-26 12:42:51 -07:00
|
|
|
|
Converts the buffer shown in the current window to HTML, opens the generated
|
|
|
|
|
HTML in a new split window, and saves its contents to {file}. If {file} is not
|
|
|
|
|
given, a temporary file (created by |tempname()|) is used.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tohtml.tohtml({winid}, {opt}) *tohtml.tohtml.tohtml()*
|
|
|
|
|
Converts the buffer shown in the window {winid} to HTML and returns the
|
|
|
|
|
output as a list of string.
|
|
|
|
|
|
|
|
|
|
Parameters: ~
|
|
|
|
|
• {winid} (`integer?`) Window to convert (defaults to current window)
|
|
|
|
|
• {opt} (`table?`) Optional parameters.
|
2024-02-27 08:20:32 -07:00
|
|
|
|
• {title}? (`string|false`, default: buffer name) Title tag
|
|
|
|
|
to set in the generated HTML code.
|
|
|
|
|
• {number_lines}? (`boolean`, default: `false`) Show line
|
|
|
|
|
numbers.
|
|
|
|
|
• {font}? (`string[]|string`, default: `guifont`) Fonts to
|
|
|
|
|
use.
|
|
|
|
|
• {width}? (`integer`, default: 'textwidth' if non-zero or
|
|
|
|
|
window width otherwise) Width used for items which are
|
|
|
|
|
either right aligned or repeat a character infinitely.
|
2024-07-11 09:16:51 -07:00
|
|
|
|
• {range}? (`integer[]`, default: entire buffer) Range of
|
|
|
|
|
rows to use.
|
2024-02-26 12:42:51 -07:00
|
|
|
|
|
|
|
|
|
Return: ~
|
|
|
|
|
(`string[]`)
|
|
|
|
|
|
|
|
|
|
|
2022-08-11 05:25:48 -07:00
|
|
|
|
vim:tw=78:ts=8:sw=4:sts=4:et:ft=help:norl:
|