{ lib, helpers, ... }: let inherit (lib) map mapAttrsToList toUpper substring mapCartesianProduct toLower ; inherit (lib.marleyos.keys) mkKeymap mkKeymapWithOpts; inherit (helpers) mkRaw; in { globals = { mapleader = " "; maplocalleader = "\\"; }; keymaps = # Disable Arrow Key Movement - - - - - - - - - - - - - - - - - - - - - - - - ( map (d: { mode = [ "i" "n" ]; key = d; action = ""; }) [ "" "" "" "" ] ) # Better Up/Down - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ ( mapAttrsToList (d: k: { mode = [ "n" "x" ]; key = k; action = "v:count == 0 ? 'g${k}' : '${k}'"; options = { desc = d; expr = true; silent = true; }; }) { Down = "j"; Up = "k"; } ) # Resize Windows - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ ( mapAttrsToList ( k: d: let s = if (d == "Increase") then "+" else "-"; in { mode = ["n"]; key = ""; action = "resize ${s}4"; options = { desc = "${d} Window Height"; }; } ) { Up = "Increase"; Down = "Decrease"; Left = "Decrease"; Right = "Increase"; } ) # Buffers - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ ( mapAttrsToList ( k: d: let # previous -> Prev prettyDir = (toUpper (substring 0 1 d)) + (substring 1 3 d); in { mode = ["n"]; key = k; action = "b${d}"; options = { desc = "${prettyDir} Buffer"; }; } ) { "" = "previous"; "" = "next"; "[b" = "previous"; "]b" = "next"; } ) ++ [ (mkKeymap ["n"] "bD" "bd" "Delete Buffer and Window") ] # Clear Search/Diff Update/Redraw - - - - - - - - - - - - - - - - - - - - - ++ [ (mkKeymap [ "i" "n" ] "" "noh" "Escape and Clear hlsearch") ( mkKeymap ["n"] "ur" "nohlsearchdiffupdatenormal! " "Redraw / Clear hlsearch / Diff Update" ) ] # Better n & N - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ ( mapCartesianProduct ( { m, d, }: let nn = if (d == "Next") then "Nn" else "nN"; zv = if (m == "n") then ".'zv'" else ""; in { mode = [m]; key = if (d == "Next") then "n" else "N"; action = "'${nn}'[v:searchforward]${zv}"; options = { desc = "${d} Search Result"; expr = true; }; } ) { m = [ "n" "x" "o" ]; d = [ "Next" "Prev" ]; } ) # Undo Break-Points - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ ( map (c: { mode = ["i"]; key = c; action = "${c}u"; }) [ "," "." ";" ] ) # Search Docs (keywordprog) - - - - - - - - - - - - - - - - - - - - - - - - # https://til.codeinthehole.com/posts/about-how-to-use-keywordprg-effectively/ ++ [(mkKeymap ["n"] "K" "norm! K" "Search for word")] # Better Indenting - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ ( map (c: { mode = ["v"]; key = c; action = "${c}gv"; }) [ "<" ">" ] ) # Commenting - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ ( mapAttrsToList (k: d: { mode = ["n"]; key = "gc${k}"; action = "${k}Vcxnormal gccfxa"; options = { desc = "Add Comment ${d}"; }; }) { o = "Below"; O = "Above"; } ) # New File - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ [(mkKeymap ["n"] "fn" "enew" "New File")] # Locations/Quickfixes - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ [ (mkKeymap ["n"] "xl" "lopen" "Location List") (mkKeymap ["n"] "xq" "copen" "Quickfix list") ] ++ ( mapAttrsToList ( k: d: let cmd = substring 0 4 (toLower d); in { mode = ["n"]; key = "${k}q"; action = "c${cmd}"; options = { desc = "${d} Quickfix"; }; } ) { "[" = "Previous"; "]" = "Next"; } ) # Diagnostics - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ [ (mkKeymap ["n"] "cd" ( mkRaw # lua "function() vim.diagnostic.open_float() end" ) "Line Diagnostics") ] ++ ( mapCartesianProduct ( { dir, key, }: let sevs = { d = { key = "nil"; desc = "Diagnostic"; }; e = { key = "vim.diagnostic.severity.ERROR"; desc = "Error"; }; w = { key = "vim.diagnostic.severity.WARN"; desc = "Warning"; }; }; cmd = toLower dir; sev = sevs."${key}".key; in { mode = ["n"]; key = "${ if (dir == "Next") then "]" else "[" }${key}"; action.__raw = # lua "function() vim.diagnostic.goto_${cmd}({ severity = ${sev} }) end"; options = { desc = "${dir} ${sevs."${key}".desc}"; }; } ) { dir = [ "Next" "Prev" ]; key = [ "d" "e" "w" ]; } ) # Quit - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ [(mkKeymap ["n"] "qq" "qa" "Quit All")] # Inspect - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ [(mkKeymap ["n"] "ui" (mkRaw "vim.show_pos") "Inspect Position")] # Window Management - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ [ (mkKeymapWithOpts ["n"] "w" "" "Windows" {remap = true;}) (mkKeymapWithOpts ["n"] "-" "s" "Split Window Below" {remap = true;}) (mkKeymapWithOpts ["n"] "|" "v" "Split Window Right" {remap = true;}) (mkKeymapWithOpts ["n"] "wd" "c" "Delete Window" {remap = true;}) ] # Tab Management - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ++ ( mapAttrsToList (k: a: { mode = ["n"]; key = "${k}"; action = "tab${toLower a}"; options = { desc = if (a == "Only") then "Close Other Tabs" else "${a} Tab"; }; }) { l = "Last"; o = "Only"; f = "First"; "" = "New"; "]" = "Next"; d = "Close"; "[" = "Previous"; } ); }