Skip to content

Commit

Permalink
modules/autocmd: factor out autocmd-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
GaetanLepage committed Nov 14, 2023
1 parent 06ebef8 commit bbbbfa2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 76 deletions.
79 changes: 79 additions & 0 deletions lib/autocmd-helpers.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{lib, ...}:
with lib; let
helpers = import ../lib/helpers.nix {inherit lib;};
in rec {
autoGroupOption = types.submodule {
options = {
clear = mkOption {
type = types.bool;
description = "Clear existing commands if the group already exists.";
default = true;
};
};
};

autoCmdOptions = {
event = helpers.mkNullOrOption (with types; either str (listOf str)) ''
The event or events to register this autocommand.
'';

group = helpers.mkNullOrOption (with types; either str int) ''
The autocommand group name or id to match against.
'';

pattern = helpers.mkNullOrOption (with types; either str (listOf str)) ''
Pattern or patterns to match literally against.
'';

buffer = helpers.mkNullOrOption types.int ''
Buffer number for buffer local autocommands |autocmd-buflocal|.
Cannot be used with `pattern`.
'';

# Introduced early October 2023.
# TODO remove in early December 2023.
description = helpers.mkNullOrOption types.str ''
DEPRECATED, please use `desc`.
'';

desc = helpers.mkNullOrOption types.str ''
A textual description of this autocommand.
'';

callback = helpers.mkNullOrOption (with types; either str helpers.rawType) ''
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 "" ''
Vim command to execute on event. Cannot be used with `callback`.
'';

once = helpers.defaultNullOpts.mkBool false "Run the autocommand only once.";

nested = helpers.defaultNullOpts.mkBool false "Run nested autocommands.";
};

autoCmdOption = types.submodule {
options = autoCmdOptions;
};
}
1 change: 1 addition & 0 deletions lib/helpers.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{lib, ...}:
with lib; rec {
keymaps = import ./keymap-helpers.nix {inherit lib;};
autocmd = import ./autocmd-helpers.nix {inherit lib;};

# vim dictionaries are, in theory, compatible with JSON
toVimDict = args:
Expand Down
79 changes: 3 additions & 76 deletions modules/autocmd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,10 @@
config,
...
}:
with lib; let
autoGroupOption = types.submodule {
options = {
clear = mkOption {
type = types.bool;
description = "Clear existing commands if the group already exists.";
default = true;
};
};
};

autoCmdOption = types.submodule {
options = {
event = helpers.mkNullOrOption (types.either types.str (types.listOf types.str)) ''
The event or events to register this autocommand.
'';

group = helpers.mkNullOrOption (types.either types.str types.int) ''
The autocommand group name or id to match against.
'';

pattern = helpers.mkNullOrOption (types.either types.str (types.listOf types.str)) ''
Pattern or patterns to match literally against.
'';

buffer = helpers.mkNullOrOption types.int ''
Buffer number for buffer local autocommands |autocmd-buflocal|.
Cannot be used with `pattern`.
'';

# Introduced early October 2023.
# TODO remove in early December 2023.
description = helpers.mkNullOrOption types.str ''
DEPRECATED, please use `desc`.
'';

desc = helpers.mkNullOrOption types.str ''
A textual description of this autocommand.
'';

callback = helpers.mkNullOrOption (types.either types.str helpers.rawType) ''
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 "" ''
Vim command to execute on event. Cannot be used with `callback`.
'';

once = helpers.defaultNullOpts.mkBool false "Run the autocommand only once.";

nested = helpers.defaultNullOpts.mkBool false "Run nested autocommands.";
};
};
in {
with lib; {
options = {
autoGroups = mkOption {
type = types.attrsOf autoGroupOption;
type = types.attrsOf helpers.autocmd.autoGroupOption;
default = {};
description = "augroup definitions";
example = ''
Expand All @@ -93,7 +20,7 @@ in {
};

autoCmd = mkOption {
type = types.listOf autoCmdOption;
type = types.listOf helpers.autocmd.autoCmdOption;
default = [];
description = "autocmd definitions";
example = ''
Expand Down

0 comments on commit bbbbfa2

Please sign in to comment.