modules/autocmd: autoCmd.callback: support raw value (#172)

This commit is contained in:
Gaétan Lepage 2023-02-20 11:53:18 +01:00 committed by GitHub
parent 6392856852
commit fc62ff7d4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,22 +30,42 @@ with lib; let
Pattern or patterns to match literally against. Pattern or patterns to match literally against.
''; '';
buffer = helpers.defaultNullOpts.mkInt "" '' buffer = helpers.mkNullOrOption types.int ''
Buffer number for buffer local autocommands |autocmd-buflocal|. Buffer number for buffer local autocommands |autocmd-buflocal|.
Cannot be used with <pattern>. Cannot be used with `pattern`.
''; '';
description = helpers.defaultNullOpts.mkStr "" "A textual description of this autocommand."; description = helpers.mkNullOrOption types.str "A textual description of this autocommand.";
callback = helpers.defaultNullOpts.mkStr "" '' callback = helpers.mkNullOrOption (types.either types.str helpers.rawType) ''
The name of a Vimscript function to call when this autocommand is triggered. Cannot be used with <command>. A function or a string.
- if a string, the name of a Vimscript function to call when this autocommand is triggered.
- Otherwise, a Lua function which is called when this autocommand is triggered.
Cannot be used with `command`.
Lua callbacks can return true to delete the autocommand; in addition, they accept a single
table argument with the following keys:
- id: (number) the autocommand id
- event: (string) the name of the event that triggered the autocommand |autocmd-events|
- group: (number|nil) the autocommand group id, if it exists
- match: (string) the expanded value of |<amatch>|
- buf: (number) the expanded value of |<abuf>|
- file: (string) the expanded value of |<afile>|
- data: (any) arbitrary data passed to |nvim_exec_autocmds()|
Example using callback:
autoCmd = [
{
event = [ "BufEnter" "BufWinEnter" ];
pattern = [ "*.c" "*.h" ];
callback = { __raw = "function() print("This buffer enters") end"; };
}
''; '';
command = helpers.defaultNullOpts.mkStr "" '' command = helpers.defaultNullOpts.mkStr "" ''
Vim command to execute on event. Cannot be used with <callback> Vim command to execute on event. Cannot be used with `callback`.
''; '';
once = helpers.defaultNullOpts.mkBool false "Run the autocommand only once"; once = helpers.defaultNullOpts.mkBool false "Run the autocommand only once.";
nested = helpers.defaultNullOpts.mkBool false "Run nested autocommands."; nested = helpers.defaultNullOpts.mkBool false "Run nested autocommands.";
}; };
@ -81,12 +101,15 @@ in {
}; };
}; };
config = mkIf (config.autoGroups != {} || config.autoCmd != {}) { config = let
inherit (config) autoGroups autoCmd;
in
mkIf (autoGroups != {} || autoCmd != {}) {
extraConfigLuaPost = extraConfigLuaPost =
(optionalString (config.autoGroups != {}) '' (optionalString (autoGroups != {}) ''
-- Set up autogroups {{ -- Set up autogroups {{
do do
local __nixvim_autogroups = ${helpers.toLuaObject config.autoGroups} local __nixvim_autogroups = ${helpers.toLuaObject autoGroups}
for group_name, options in pairs(__nixvim_autogroups) do for group_name, options in pairs(__nixvim_autogroups) do
vim.api.nvim_create_augroup(group_name, options) vim.api.nvim_create_augroup(group_name, options)
@ -94,10 +117,10 @@ in {
end end
-- }} -- }}
'') '')
+ (optionalString (config.autoCmd != []) '' + (optionalString (autoCmd != []) ''
-- Set up autocommands {{ -- Set up autocommands {{
do do
local __nixvim_autocommands = ${helpers.toLuaObject config.autoCmd} local __nixvim_autocommands = ${helpers.toLuaObject autoCmd}
for _, autocmd in ipairs(__nixvim_autocommands) do for _, autocmd in ipairs(__nixvim_autocommands) do
vim.api.nvim_create_autocmd( vim.api.nvim_create_autocmd(