about summary refs log tree commit diff stats
path: root/modules/by-name/nv/nvim/autocmds
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-11-09 12:35:44 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-11-09 12:54:51 +0100
commit2122a01f99c6da466b8f0f55c965c11a9043d117 (patch)
tree6c1697afc30a5bb72635bda7db9b5610386a5b71 /modules/by-name/nv/nvim/autocmds
parentfix(pkgs/stamp): Fallback to `dot-license` (diff)
downloadnixos-config-2122a01f99c6da466b8f0f55c965c11a9043d117.tar.gz
nixos-config-2122a01f99c6da466b8f0f55c965c11a9043d117.zip
refactor(modules/legacy/conf/nvim): Move to `by-name`
Diffstat (limited to 'modules/by-name/nv/nvim/autocmds')
-rw-r--r--modules/by-name/nv/nvim/autocmds/default.nix133
1 files changed, 133 insertions, 0 deletions
diff --git a/modules/by-name/nv/nvim/autocmds/default.nix b/modules/by-name/nv/nvim/autocmds/default.nix
new file mode 100644
index 00000000..54fd6831
--- /dev/null
+++ b/modules/by-name/nv/nvim/autocmds/default.nix
@@ -0,0 +1,133 @@
+{
+  config,
+  lib,
+  ...
+}: let
+  cfg = config.soispha.programs.nvim;
+in {
+  config.home-manager.users.soispha = lib.mkIf cfg.enable {
+    programs.nixvim = {
+      autoGroups = {
+        cursor_off = {clear = true;};
+        colorcolumn_toggle = {clear = true;};
+        numbertoggle = {clear = true;};
+        coloroverride = {clear = true;};
+        highlight_on_yank = {clear = true;};
+        create_dir = {clear = true;};
+      };
+
+      autoCmd = [
+        {
+          # Taken from: https://github.com/jghauser/mkdir.nvim
+          event = ["BufWritePre"];
+          pattern = ["*"];
+          callback = {
+            __raw = ''
+              function()
+                -- Get current filename, get full path (:p) and leave only the head (:h)
+                local dir = vim.fn.expand('<afile>:p:h')
+
+                -- This handles URLs using netrw. See ':help netrw-transparent' for details.
+                if dir:find('%l+://') == 1 then
+                  return
+                end
+
+                if vim.fn.isdirectory(dir) == 0 then
+                  vim.fn.mkdir(dir, 'p')
+                end
+              end
+            '';
+          };
+          group = "create_dir";
+          desc = "Create the directory of the target file on write";
+        }
+        {
+          event = ["TextYankPost"];
+          pattern = ["*"];
+          callback = {
+            __raw = ''
+              function()
+                vim.highlight.on_yank()
+              end
+            '';
+          };
+          group = "highlight_on_yank";
+          desc = "Highlight the yanked text";
+        }
+        {
+          event = ["BufWritePre"];
+          pattern = ["*"];
+          command = ''
+            ks | if search("\\s\\+$", 'n') != 0 | :%s/\s\+$// | endif | 's
+          '';
+          desc = ''
+            Remove trailing whitespace on safe
+            :%s/\s\+$\| \+\ze\t//g >> For trailing spaces and spaces before tabstops
+          '';
+        }
+
+        {
+          event = ["WinLeave"];
+          pattern = ["*"];
+          command = "set nocursorline"; # TODO: possible also nocursorcolumn
+          group = "cursor_off";
+          desc = "Display cursorline and cursorcolumn ONLY in active window.";
+        }
+        {
+          event = ["WinEnter"];
+          pattern = ["*"];
+          command = "set cursorline"; # TODO: possible also cursorcolumn
+          group = "cursor_off";
+          desc = "Display cursorline and cursorcolumn ONLY in active window.";
+        }
+
+        {
+          event = ["InsertEnter"];
+          pattern = ["*"];
+          command = "set colorcolumn=${config.home-manager.users.soispha.programs.nixvim.opts.colorcolumn}";
+          group = "colorcolumn_toggle";
+          desc = "Only activate the colorcolumn when focused";
+        }
+        {
+          event = ["BufLeave" "FocusLost" "InsertLeave" "WinLeave"];
+          pattern = ["*"];
+          command = "set colorcolumn=0";
+          group = "colorcolumn_toggle";
+          desc = "Only activate the colorcolumn when focused";
+        }
+
+        {
+          event = ["BufEnter" "FocusGained" "InsertLeave" "WinEnter"];
+          pattern = ["*"];
+          command = "if &nu && mode() != \"i\" | set rnu   | endif";
+          group = "numbertoggle";
+          desc = "Change line numbers, when not focused";
+        }
+        {
+          event = ["BufLeave" "FocusLost" "InsertEnter" "WinLeave"];
+          pattern = ["*"];
+          command = "if &nu | set nornu | endif";
+          group = "numbertoggle";
+          desc = "Change line numbers, when not focused";
+        }
+
+        {
+          # Override LineNr
+          event = ["ColorScheme"];
+          pattern = ["*"];
+          command = "highlight LineNr  ctermfg=DarkGrey guifg=DarkGrey ";
+          group = "coloroverride";
+          desc = "Changes Line number colors";
+        }
+        {
+          # Override CursorLineNr
+          event = ["ColorScheme"];
+          pattern = ["*"];
+          command = "highlight CursorLineNr  ctermfg=White guifg=White ";
+          group = "coloroverride";
+          desc = "Changes Line number colors";
+        }
+      ];
+    };
+  };
+}