mirror of
https://github.com/neovim/neovim.git
synced 2024-12-19 18:55:14 -07:00
docs(api): add example showing necessity to wrap callback function (#18179)
Some people ran into issues trying to use `callback = myluafun` because of the event data payload. Co-authored-by: Gregory Anders <8965202+gpanders@users.noreply.github.com>
This commit is contained in:
parent
7e7fdca163
commit
a48a0a4f7b
@ -3221,6 +3221,22 @@ nvim_create_autocmd({event}, {*opts}) *nvim_create_autocmd()*
|
||||
})
|
||||
<
|
||||
|
||||
Lua functions receive a table with information about the
|
||||
autocmd event as an argument. To use a function which itself
|
||||
accepts another (optional) parameter, wrap the function in a
|
||||
lambda:
|
||||
>
|
||||
-- Lua function with an optional parameter.
|
||||
-- The autocmd callback would pass a table as argument but this
|
||||
-- function expects number|nil
|
||||
local myluafun = function(bufnr) bufnr = bufnr or vim.api.nvim_get_current_buf() end
|
||||
|
||||
vim.api.nvim_create_autocmd({"BufEnter", "BufWinEnter"}, {
|
||||
pattern = {"*.c", "*.h"},
|
||||
callback = function() myluafun() end,
|
||||
})
|
||||
<
|
||||
|
||||
Example using command: >
|
||||
vim.api.nvim_create_autocmd({"BufEnter", "BufWinEnter"}, {
|
||||
pattern = {"*.c", "*.h"},
|
||||
|
@ -346,6 +346,22 @@ cleanup:
|
||||
/// })
|
||||
/// </pre>
|
||||
///
|
||||
/// Lua functions receive a table with information about the autocmd event as an argument. To use
|
||||
/// a function which itself accepts another (optional) parameter, wrap the function
|
||||
/// in a lambda:
|
||||
///
|
||||
/// <pre>
|
||||
/// -- Lua function with an optional parameter.
|
||||
/// -- The autocmd callback would pass a table as argument but this
|
||||
/// -- function expects number|nil
|
||||
/// local myluafun = function(bufnr) bufnr = bufnr or vim.api.nvim_get_current_buf() end
|
||||
///
|
||||
/// vim.api.nvim_create_autocmd({"BufEnter", "BufWinEnter"}, {
|
||||
/// pattern = {"*.c", "*.h"},
|
||||
/// callback = function() myluafun() end,
|
||||
/// })
|
||||
/// </pre>
|
||||
///
|
||||
/// Example using command:
|
||||
/// <pre>
|
||||
/// vim.api.nvim_create_autocmd({"BufEnter", "BufWinEnter"}, {
|
||||
|
Loading…
Reference in New Issue
Block a user